Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2105 | - | 1 | #include "main.h" |
2 | #include "uart.h" |
||
3 | |||
4 | int uart0_filestream = -1; |
||
5 | int usb_stream = -1; |
||
6 | int data_length; //length of transmitting data string |
||
7 | |||
8 | |||
9 | //>> Adding checksum and transmitting data |
||
10 | //------------------------------------------------------------------------------------------------------ |
||
11 | void transmit_data(serial_data_struct* pdata_package){ |
||
12 | if (uart0_filestream != -1) |
||
13 | { |
||
14 | int count = write(uart0_filestream, pdata_package->txrxdata, pdata_package->cmdLength); |
||
15 | if (count < 0) |
||
16 | { |
||
17 | printf("UART TX error\n"); |
||
18 | } |
||
19 | } |
||
20 | } |
||
21 | |||
22 | //>> Adding address and command ID, encoding and checksum |
||
23 | //------------------------------------------------------------------------------------------------------ |
||
24 | void create_serial_frame(u8 address, u8 cmdID, u16 cmdLength, serial_data_struct* pdata_package){ |
||
25 | pdata_package->txrxdata[0] = '#'; |
||
26 | pdata_package->txrxdata[1] = address + 'a'; |
||
27 | pdata_package->txrxdata[2] = cmdID; |
||
28 | |||
29 | //encode data |
||
30 | u8 a,b,c; |
||
31 | int counter = 0; |
||
32 | int u = 3; |
||
33 | while(cmdLength){ |
||
34 | if(cmdLength) |
||
35 | { |
||
36 | cmdLength--; |
||
37 | a = pdata_package->data[counter++]; |
||
38 | }else{a = 0; counter++;}; |
||
39 | if(cmdLength) |
||
40 | { |
||
41 | cmdLength--; |
||
42 | b = pdata_package->data[counter++]; |
||
43 | }else{b = 0; counter++;}; |
||
44 | if(cmdLength) |
||
45 | { |
||
46 | cmdLength--; |
||
47 | c = pdata_package->data[counter++]; |
||
48 | }else{c = 0; counter++;}; |
||
49 | pdata_package->txrxdata[u++] = '=' + (a >> 2); |
||
50 | pdata_package->txrxdata[u++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
51 | pdata_package->txrxdata[u++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
52 | pdata_package->txrxdata[u++] = '=' + (c & 0x3f); |
||
53 | } |
||
54 | |||
55 | //add Checksum |
||
56 | u16 crc = 0; |
||
57 | u8 crc1, crc2; |
||
58 | for (int i = 0; i < u; i++) |
||
59 | { |
||
60 | crc += pdata_package->txrxdata[i]; |
||
61 | } |
||
62 | crc %= 4096; |
||
63 | crc1 = '=' + crc / 64; |
||
64 | crc2 = '=' + crc % 64; |
||
65 | pdata_package->txrxdata[u++] = crc1; |
||
66 | pdata_package->txrxdata[u++] = crc2; |
||
67 | pdata_package->txrxdata[u++] = '\r'; |
||
68 | |||
69 | pdata_package->cmdLength = u; |
||
70 | } |
||
71 | |||
72 | |||
73 | //>> Initializing serial interface |
||
74 | //------------------------------------------------------------------------------------------------------ |
||
75 | void uart_init(){ |
||
76 | uart0_filestream = open(SERIAL, O_RDWR | O_NOCTTY | O_NDELAY); |
||
77 | if (uart0_filestream == -1) |
||
78 | { |
||
79 | printf("Error - Unable to open UART. Ensure it is not in use by another application\n"); |
||
80 | } |
||
81 | //UART Options |
||
82 | struct termios options; |
||
83 | tcgetattr(uart0_filestream, &options); |
||
84 | options.c_cflag = B57600 | CS8 | CLOCAL | CREAD; |
||
85 | options.c_iflag = IGNPAR; |
||
86 | options.c_oflag = 0; |
||
87 | options.c_lflag = 0; |
||
88 | tcflush(uart0_filestream, TCIFLUSH); |
||
89 | tcsetattr(uart0_filestream, TCSANOW, &options); |
||
90 | //UART Options |
||
91 | } |