Work with UART (C) » History » Version 4
krufter_multiclet, 06/11/2013 06:19 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 | 4 | krufter_multiclet | h2. б) Receive via UART |
108 | 1 | krufter_multiclet | |
109 | 4 | krufter_multiclet | For reception via UART: |
110 | 1 | krufter_multiclet | |
111 | 4 | krufter_multiclet | h3. 1. Execute 1,2 under letter а) |
112 | 1 | krufter_multiclet | |
113 | 4 | krufter_multiclet | h3. 2. Check receiver FIFO for new data |
114 | 1 | krufter_multiclet | |
115 | 4 | krufter_multiclet | For 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 | 4 | krufter_multiclet | For 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 | 4 | krufter_multiclet | h3. 3. Get data |
126 | 1 | krufter_multiclet | |
127 | 4 | krufter_multiclet | For 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 | 4 | krufter_multiclet | For 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 | 4 | krufter_multiclet | h2. API functions UART: |
139 | 1 | krufter_multiclet | |
140 | 4 | krufter_multiclet | Observe every functions, which declared in uart.h |
141 | 1 | krufter_multiclet | |
142 | 1 | krufter_multiclet | <pre>1) void uart_init(UART_TypeDef *UART, UART_InitTypeDef* UART_InitStruct)</pre> |
143 | 1 | krufter_multiclet | |
144 | 4 | krufter_multiclet | Function _uart_init()_ initialize definition number of UART. |
145 | 4 | krufter_multiclet | UART - available value: UART0, UART1, UART2, UART3 |
146 | 4 | krufter_multiclet | UART_InitStruct - structure, contain configuration elements UART |
147 | 1 | krufter_multiclet | |
148 | 1 | krufter_multiclet | <pre>2) void DM2UART(UART_TypeDef *UART, int start_address, int size_block)</pre> |
149 | 1 | krufter_multiclet | |
150 | 4 | krufter_multiclet | Функция _DM2UART ()_ output sector data memory via UART in a little endian format. |
151 | 4 | krufter_multiclet | UART - available value: UART0, UART1, UART2, UART3 |
152 | 4 | krufter_multiclet | start_address - start data memory address |
153 | 4 | krufter_multiclet | size_block - size data sector |
154 | 1 | krufter_multiclet | |
155 | 1 | krufter_multiclet | <pre>3) void uart_send_str(char *str, UART_TypeDef *UART)</pre> |
156 | 1 | krufter_multiclet | |
157 | 4 | krufter_multiclet | Function _uart_send_str()_ output string via UART. |
158 | 4 | krufter_multiclet | UART - available value: UART0, UART1, UART2, UART3 |
159 | 4 | krufter_multiclet | str - string |
160 | 1 | krufter_multiclet | |
161 | 1 | krufter_multiclet | <pre>4) void uart_send_int(int str, UART_TypeDef *UART)</pre> |
162 | 1 | krufter_multiclet | |
163 | 4 | krufter_multiclet | Function _uart_send_int()_ output variable int type via UART. |
164 | 4 | krufter_multiclet | UART - available value: UART0, UART1, UART2, UART3 |
165 | 4 | krufter_multiclet | str - variable int type |
166 | 1 | krufter_multiclet | |
167 | 1 | krufter_multiclet | |
168 | 4 | krufter_multiclet | h3. Note: |
169 | 1 | krufter_multiclet | |
170 | 4 | krufter_multiclet | In library versions before 01.04.13 needed before DM2UART (), uart_send_int() |
171 | 4 | krufter_multiclet | use consructions: |
172 | 1 | krufter_multiclet | |
173 | 1 | krufter_multiclet | <pre> |
174 | 1 | krufter_multiclet | while(UART_FIFO_TX_EMPTY(UART0) == 0); |
175 | 1 | krufter_multiclet | </pre> |
176 | 1 | krufter_multiclet | |
177 | 4 | krufter_multiclet | In library versions after 01.04.13 you can use functions independently. |