Work with UART (C) » History » Version 3
krufter_multiclet, 06/11/2013 06:03 PM
1 | 1 | krufter_multiclet | h1. Work with UART (C) |
---|---|---|---|
2 | 1 | krufter_multiclet | |
3 | 1 | krufter_multiclet | Observe transmit and receive messages via UART more details (in С). |
4 | 1 | krufter_multiclet | |
5 | 1 | krufter_multiclet | Configuration UART on transmission and reception message in C looks like simple as in Assembler of multicellular processor. |
6 | 1 | krufter_multiclet | Besides this in C for working with UART available usefull functions and macros, such as sending value int type, sending string, |
7 | 1 | krufter_multiclet | sending definition array of data memory. |
8 | 1 | krufter_multiclet | |
9 | 1 | krufter_multiclet | In code examples use UART library, which must required: |
10 | 1 | krufter_multiclet | <pre> |
11 | 1 | krufter_multiclet | #include <uart.h> |
12 | 1 | krufter_multiclet | </pre> |
13 | 1 | krufter_multiclet | |
14 | 2 | krufter_multiclet | h2. а) Transmit via UART |
15 | 1 | krufter_multiclet | |
16 | 2 | krufter_multiclet | h3. 1. Configurate UART on transmission |
17 | 1 | krufter_multiclet | |
18 | 2 | krufter_multiclet | First step - create structure (all elements of structure see in uart.h). |
19 | 1 | krufter_multiclet | <pre> |
20 | 1 | krufter_multiclet | UART_InitTypeDef UART_InitStructure; |
21 | 1 | krufter_multiclet | </pre> |
22 | 1 | krufter_multiclet | |
23 | 2 | krufter_multiclet | Initialize needed parameters (other parameters initialize as null). |
24 | 1 | krufter_multiclet | <pre> |
25 | 3 | krufter_multiclet | UART_InitStructure.BaudRate = 38400; //baudrate |
26 | 3 | krufter_multiclet | UART_InitStructure.TypeParity = 0x00000000; //type parity control |
27 | 3 | krufter_multiclet | UART_InitStructure.Parity = 0x00000000; //allow parity control |
28 | 3 | krufter_multiclet | UART_InitStructure.FlowControl = 0x00000000; //allow dataflow control |
29 | 3 | krufter_multiclet | UART_InitStructure.Mode = 0x00000002; //allow transmit |
30 | 1 | krufter_multiclet | </pre> |
31 | 2 | krufter_multiclet | Initialize for UART0: |
32 | 1 | krufter_multiclet | <pre> |
33 | 1 | krufter_multiclet | uart_init(UART0, &UART_InitStructure); |
34 | 1 | krufter_multiclet | </pre> |
35 | 2 | krufter_multiclet | For UART3: |
36 | 1 | krufter_multiclet | <pre> |
37 | 1 | krufter_multiclet | uart_init(UART3, &UART_InitStructure); |
38 | 1 | krufter_multiclet | </pre> |
39 | 1 | krufter_multiclet | |
40 | 2 | krufter_multiclet | h3. 2. Allow alternative port functions |
41 | 1 | krufter_multiclet | |
42 | 2 | krufter_multiclet | For UART0: |
43 | 1 | krufter_multiclet | <pre> |
44 | 1 | krufter_multiclet | GPIOB->BPS = 0x00000F00; |
45 | 1 | krufter_multiclet | </pre> |
46 | 1 | krufter_multiclet | |
47 | 2 | krufter_multiclet | For UART3: |
48 | 1 | krufter_multiclet | <pre> |
49 | 1 | krufter_multiclet | GPIOD->BPS = 0x00000F00; |
50 | 1 | krufter_multiclet | </pre> |
51 | 1 | krufter_multiclet | |
52 | 2 | krufter_multiclet | h3. 3. Check transmitter FIFO on full |
53 | 1 | krufter_multiclet | |
54 | 2 | krufter_multiclet | For UART0: |
55 | 1 | krufter_multiclet | <pre> |
56 | 1 | krufter_multiclet | while(UART_FIFO_TX_FULL(UART0) == 1); |
57 | 1 | krufter_multiclet | </pre> |
58 | 1 | krufter_multiclet | |
59 | 2 | krufter_multiclet | For UART3: |
60 | 1 | krufter_multiclet | <pre> |
61 | 1 | krufter_multiclet | while(UART_FIFO_TX_FULL(UART3) == 1); |
62 | 1 | krufter_multiclet | </pre> |
63 | 1 | krufter_multiclet | |
64 | 2 | krufter_multiclet | h3. 4. Transmit byte via UART |
65 | 1 | krufter_multiclet | |
66 | 2 | krufter_multiclet | For UART0: |
67 | 1 | krufter_multiclet | <pre> |
68 | 1 | krufter_multiclet | UART_SEND_BYTE(0xAB, UART0); |
69 | 1 | krufter_multiclet | </pre> |
70 | 1 | krufter_multiclet | |
71 | 2 | krufter_multiclet | For UART3: |
72 | 1 | krufter_multiclet | <pre> |
73 | 1 | krufter_multiclet | UART_SEND_BYTE(0xAB, UART3); |
74 | 1 | krufter_multiclet | </pre> |
75 | 1 | krufter_multiclet | |
76 | 2 | krufter_multiclet | As a result you can get simple example - transmission byte via UART with baudrate 38400 kbit/s: |
77 | 1 | krufter_multiclet | |
78 | 2 | krufter_multiclet | For UART0: |
79 | 1 | krufter_multiclet | <pre> |
80 | 1 | krufter_multiclet | #include <HDL51001_ccf.h> |
81 | 1 | krufter_multiclet | #include <uart.h> |
82 | 1 | krufter_multiclet | |
83 | 1 | krufter_multiclet | void main() |
84 | 1 | krufter_multiclet | { |
85 | 1 | krufter_multiclet | UART_InitTypeDef UART_InitStructure; |
86 | 1 | krufter_multiclet | |
87 | 3 | krufter_multiclet | UART_InitStructure.BaudRate = 38400; //baudrate |
88 | 3 | krufter_multiclet | UART_InitStructure.TypeParity = 0x00000000; //type parity control |
89 | 3 | krufter_multiclet | UART_InitStructure.Parity = 0x00000000; //allow parity control |
90 | 3 | krufter_multiclet | UART_InitStructure.FlowControl = 0x00000000; //allow dataflow control |
91 | 3 | krufter_multiclet | UART_InitStructure.Mode = 0x00000002; //allow transmit |
92 | 1 | krufter_multiclet | |
93 | 1 | krufter_multiclet | uart_init(UART0, &UART_InitStructure); |
94 | 1 | krufter_multiclet | |
95 | 1 | krufter_multiclet | GPIOB->BPS = 0x00000F00; |
96 | 1 | krufter_multiclet | |
97 | 1 | krufter_multiclet | while(UART_FIFO_TX_FULL(UART0) == 1); |
98 | 1 | krufter_multiclet | UART_SEND_BYTE(0xAB, UART0); |
99 | 1 | krufter_multiclet | } |
100 | 1 | krufter_multiclet | </pre> |
101 | 1 | krufter_multiclet | |
102 | 3 | krufter_multiclet | For UART3 example is analogie, replace port B on D and UART0 on UART3. |
103 | 1 | krufter_multiclet | |
104 | 3 | krufter_multiclet | Transmitted byte you can receive with PC application - any COM port monitor, setting on data with 1 stop-bit, |
105 | 3 | krufter_multiclet | baudrate 38400 kbit/s, data word - 8 bit. |
106 | 1 | krufter_multiclet | |
107 | 1 | krufter_multiclet | h2. б) Приём по UART |
108 | 1 | krufter_multiclet | |
109 | 1 | krufter_multiclet | Для приёма по UART необходимо: |
110 | 1 | krufter_multiclet | |
111 | 1 | krufter_multiclet | h3. 1. Выполнить пункты 1,2 под а) |
112 | 1 | krufter_multiclet | |
113 | 1 | krufter_multiclet | h3. 2. Проверить буфер приемника на наличие новых данных |
114 | 1 | krufter_multiclet | |
115 | 1 | krufter_multiclet | Для UART0: |
116 | 1 | krufter_multiclet | <pre> |
117 | 1 | krufter_multiclet | while(UART_NEW_DATA(UART0) == 0); |
118 | 1 | krufter_multiclet | </pre> |
119 | 1 | krufter_multiclet | |
120 | 1 | krufter_multiclet | Для UART3: |
121 | 1 | krufter_multiclet | <pre> |
122 | 1 | krufter_multiclet | while(UART_NEW_DATA(UART3) == 0); |
123 | 1 | krufter_multiclet | </pre> |
124 | 1 | krufter_multiclet | |
125 | 1 | krufter_multiclet | h3. 3. Прочитать данные |
126 | 1 | krufter_multiclet | |
127 | 1 | krufter_multiclet | Для UART0: |
128 | 1 | krufter_multiclet | <pre> |
129 | 1 | krufter_multiclet | rbyte = UART_GET_BYTE(UART0); |
130 | 1 | krufter_multiclet | </pre> |
131 | 1 | krufter_multiclet | |
132 | 1 | krufter_multiclet | Для UART3: |
133 | 1 | krufter_multiclet | <pre> |
134 | 1 | krufter_multiclet | rbyte = UART_GET_BYTE(UART3); |
135 | 1 | krufter_multiclet | </pre> |
136 | 1 | krufter_multiclet | |
137 | 1 | krufter_multiclet | Пример приёма байта данных составить довольно просто по аналогии с передачей байта. Следует заметить, что помимо анализа буфера приемника на |
138 | 1 | krufter_multiclet | наличие новых данных, необходимо проверять буфер приёма на полноту. |
139 | 1 | krufter_multiclet | |
140 | 1 | krufter_multiclet | h2. API функции UART: |
141 | 1 | krufter_multiclet | |
142 | 1 | krufter_multiclet | Кроме полезных макросов имеются функции для вывода данных по UART. |
143 | 1 | krufter_multiclet | Разберём подробнее каждую функцию, которая объявлена в uart.h |
144 | 1 | krufter_multiclet | |
145 | 1 | krufter_multiclet | <pre>1) void uart_init(UART_TypeDef *UART, UART_InitTypeDef* UART_InitStruct)</pre> |
146 | 1 | krufter_multiclet | |
147 | 1 | krufter_multiclet | Функция _uart_init()_ выполняет инициализацию заданного номера UART. |
148 | 1 | krufter_multiclet | UART - может принимать значения: UART0, UART1, UART2, UART3 |
149 | 1 | krufter_multiclet | UART_InitStruct - структура, содержащая элементы конфигурации UART |
150 | 1 | krufter_multiclet | |
151 | 1 | krufter_multiclet | <pre>2) void DM2UART(UART_TypeDef *UART, int start_address, int size_block)</pre> |
152 | 1 | krufter_multiclet | |
153 | 1 | krufter_multiclet | Функция _DM2UART ()_ выводит область памяти данных по UART младшим байтом вперёд. |
154 | 1 | krufter_multiclet | UART - может принимать значения: UART0, UART1, UART2, UART3 |
155 | 1 | krufter_multiclet | start_address - начальный адрес памяти данных |
156 | 1 | krufter_multiclet | size_block - размер области данных |
157 | 1 | krufter_multiclet | |
158 | 1 | krufter_multiclet | <pre>3) void uart_send_str(char *str, UART_TypeDef *UART)</pre> |
159 | 1 | krufter_multiclet | |
160 | 1 | krufter_multiclet | Функция _uart_send_str()_ выводит строку по UART. |
161 | 1 | krufter_multiclet | UART - может принимать значения: UART0, UART1, UART2, UART3 |
162 | 1 | krufter_multiclet | str - строка символов |
163 | 1 | krufter_multiclet | |
164 | 1 | krufter_multiclet | <pre>4) void uart_send_int(int str, UART_TypeDef *UART)</pre> |
165 | 1 | krufter_multiclet | |
166 | 1 | krufter_multiclet | Функция _uart_send_int()_ выводит переменную типа int по UART. |
167 | 1 | krufter_multiclet | UART - может принимать значения: UART0, UART1, UART2, UART3 |
168 | 1 | krufter_multiclet | str - переменная типа int |
169 | 1 | krufter_multiclet | |
170 | 1 | krufter_multiclet | |
171 | 1 | krufter_multiclet | h3. Замечание: |
172 | 1 | krufter_multiclet | |
173 | 1 | krufter_multiclet | В версиях библиотеки до 01.04.13 необходимо перед вызовом функций DM2UART (), uart_send_int() |
174 | 1 | krufter_multiclet | применять конструкцию: |
175 | 1 | krufter_multiclet | |
176 | 1 | krufter_multiclet | <pre> |
177 | 1 | krufter_multiclet | while(UART_FIFO_TX_EMPTY(UART0) == 0); |
178 | 1 | krufter_multiclet | </pre> |
179 | 1 | krufter_multiclet | |
180 | 1 | krufter_multiclet | В версиях библиотеки после 01.04.13 функции могут использоваться самостоятельно. |