@@ -50,10 +50,10 @@ static volatile bool uart_is_running[UART_NUMBER] = {false};
50
50
static mutex_t send_mutex [UART_NUMBER ];
51
51
static bool send_mutex_init_done [UART_NUMBER ] = {false};
52
52
static SerialConfig uart_cfg [UART_NUMBER ] = {{
53
- BAUDRATE ,
54
- 0 ,
55
- USART_CR2_LINEN ,
56
- 0
53
+ BAUDRATE ,
54
+ 0 ,
55
+ USART_CR2_LINEN ,
56
+ 0
57
57
}};
58
58
59
59
// Different for Rx and Tx because it is possible for hardware to use different UART driver
@@ -67,48 +67,51 @@ static PACKET_STATE_t packet_state[UART_NUMBER];
67
67
68
68
// Private functions
69
69
static void process_packet (unsigned char * data , unsigned int len , unsigned int port_number );
70
- static void write_packet (unsigned char * data , unsigned int , unsigned int port_number );
70
+ static void write_packet (unsigned char * data , unsigned int len , unsigned int port_number );
71
71
72
72
static void process_packet_1 (unsigned char * data , unsigned int len ) {process_packet (data ,len ,0 );}
73
73
static void write_packet_1 (unsigned char * data , unsigned int len ) {write_packet (data ,len ,0 );}
74
- static void send_packet_1 (unsigned char * data , unsigned int len ) {app_uartcomm_send_packet (data ,len ,0 );}
74
+ static void send_packet_1 (unsigned char * data , unsigned int len ) {app_uartcomm_send_packet (data ,len ,UART_PORT_COMM_HEADER );}
75
75
76
76
static void process_packet_2 (unsigned char * data , unsigned int len ) {process_packet (data ,len ,1 );}
77
77
static void write_packet_2 (unsigned char * data , unsigned int len ) {write_packet (data ,len ,1 );}
78
- static void send_packet_2 (unsigned char * data , unsigned int len ) {app_uartcomm_send_packet (data ,len ,1 );}
78
+ static void send_packet_2 (unsigned char * data , unsigned int len ) {app_uartcomm_send_packet (data ,len ,UART_PORT_BUILTIN );}
79
79
80
80
static void process_packet_3 (unsigned char * data , unsigned int len ) {process_packet (data ,len ,2 );}
81
81
static void write_packet_3 (unsigned char * data , unsigned int len ) {write_packet (data ,len ,2 );}
82
- static void send_packet_3 (unsigned char * data , unsigned int len ) {app_uartcomm_send_packet (data ,len ,2 );}
82
+ static void send_packet_3 (unsigned char * data , unsigned int len ) {app_uartcomm_send_packet (data ,len ,UART_PORT_EXTRA_HEADER );}
83
83
84
84
typedef void (* data_func ) (unsigned char * data , unsigned int len );
85
85
static data_func write_functions [3 ] = {write_packet_1 , write_packet_2 , write_packet_3 };
86
86
static data_func process_functions [3 ] = {process_packet_1 , process_packet_2 , process_packet_3 };
87
87
static data_func send_functions [3 ] = {send_packet_1 , send_packet_2 , send_packet_3 };
88
88
89
89
static void write_packet (unsigned char * data , unsigned int len , unsigned int port_number ) {
90
- if (port_number >= UART_NUMBER ){
90
+ if (port_number >= UART_NUMBER ) {
91
91
return ;
92
92
}
93
+
93
94
if (uart_is_running [port_number ]) {
94
95
sdWrite (serialPortDriverTx [port_number ], data , len );
95
96
}
96
97
}
98
+
97
99
static void process_packet (unsigned char * data , unsigned int len , unsigned int port_number ) {
98
- if (port_number >= UART_NUMBER ){
100
+ if (port_number >= UART_NUMBER ) {
99
101
return ;
100
102
}
103
+
101
104
commands_process_packet (data , len , send_functions [port_number ]);
102
105
}
103
106
104
-
105
107
void app_uartcomm_initialize (void ) {
106
108
serialPortDriverTx [0 ] = & HW_UART_DEV ;
107
109
serialPortDriverRx [0 ] = & HW_UART_DEV ;
108
110
uart_cfg [0 ].speed = BAUDRATE ;
109
111
RxGpioPort [0 ] = HW_UART_RX_PORT ; RxGpioPin [0 ] = HW_UART_RX_PIN ;
110
112
TxGpioPort [0 ] = HW_UART_TX_PORT ; TxGpioPin [0 ] = HW_UART_TX_PIN ;
111
113
gpioAF [0 ] = HW_UART_GPIO_AF ;
114
+
112
115
#ifdef HW_UART_P_DEV
113
116
#ifdef HW_UART_P_DEV_TX
114
117
serialPortDriverTx [1 ] = & HW_UART_P_DEV_TX ;
@@ -121,6 +124,7 @@ void app_uartcomm_initialize(void) {
121
124
TxGpioPort [1 ] = HW_UART_P_TX_PORT ; TxGpioPin [1 ] = HW_UART_P_TX_PIN ;
122
125
gpioAF [1 ] = HW_UART_P_GPIO_AF ;
123
126
#endif
127
+
124
128
#ifdef HW_UART_3_DEV
125
129
serialPortDriverTx [2 ] = & HW_UART_3_DEV ;
126
130
serialPortDriverRx [2 ] = & HW_UART_3_DEV ;
@@ -131,32 +135,34 @@ void app_uartcomm_initialize(void) {
131
135
#endif
132
136
}
133
137
134
- void app_uartcomm_start (unsigned int port_number ) {
138
+ void app_uartcomm_start (UART_PORT port_number ) {
135
139
if (port_number >= UART_NUMBER ){
136
140
return ;
137
141
}
138
142
139
143
packet_init (write_functions [port_number ], process_functions [port_number ], & packet_state [port_number ]);
144
+
140
145
if (!thread_is_running ) {
141
146
chThdCreateStatic (packet_process_thread_wa , sizeof (packet_process_thread_wa ),
142
- NORMALPRIO , packet_process_thread , NULL );
147
+ NORMALPRIO , packet_process_thread , NULL );
143
148
thread_is_running = true;
144
149
}
150
+
145
151
sdStart (serialPortDriverRx [port_number ], & uart_cfg [port_number ]);
146
152
sdStart (serialPortDriverTx [port_number ], & uart_cfg [port_number ]);
147
153
uart_is_running [port_number ] = true;
154
+
148
155
palSetPadMode (TxGpioPort [port_number ], TxGpioPin [port_number ], PAL_MODE_ALTERNATE (gpioAF [port_number ]) |
149
- PAL_STM32_OSPEED_HIGHEST |
150
- PAL_STM32_PUDR_PULLUP );
156
+ PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP );
151
157
palSetPadMode (RxGpioPort [port_number ], RxGpioPin [port_number ], PAL_MODE_ALTERNATE (gpioAF [port_number ]) |
152
- PAL_STM32_OSPEED_HIGHEST |
153
- PAL_STM32_PUDR_PULLUP );
158
+ PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP );
154
159
}
155
160
156
- void app_uartcomm_stop (unsigned int port_number ) {
157
- if (port_number >= UART_NUMBER ){
161
+ void app_uartcomm_stop (UART_PORT port_number ) {
162
+ if (port_number >= UART_NUMBER ) {
158
163
return ;
159
164
}
165
+
160
166
if (uart_is_running [port_number ]) {
161
167
sdStop (serialPortDriverRx [port_number ]);
162
168
sdStop (serialPortDriverTx [port_number ]);
@@ -167,29 +173,34 @@ void app_uartcomm_stop(unsigned int port_number) {
167
173
// Notice that the processing thread is kept running in case this call is made from it.
168
174
}
169
175
170
- void app_uartcomm_send_packet (unsigned char * data , unsigned int len , unsigned int port_number ) {
171
- if (port_number >= UART_NUMBER ){
176
+ void app_uartcomm_send_packet (unsigned char * data , unsigned int len , UART_PORT port_number ) {
177
+ if (port_number >= UART_NUMBER ) {
172
178
return ;
173
179
}
180
+
174
181
if (!send_mutex_init_done [port_number ]) {
175
182
chMtxObjectInit (& send_mutex [port_number ]);
176
183
send_mutex_init_done [port_number ] = true;
177
184
}
185
+
178
186
chMtxLock (& send_mutex [port_number ]);
179
187
packet_send_packet (data , len , & packet_state [port_number ]);
180
188
chMtxUnlock (& send_mutex [port_number ]);
181
189
}
182
190
183
- void app_uartcomm_configure (uint32_t baudrate , bool enabled , unsigned int port_number ) {
184
- if (port_number >= UART_NUMBER ){
191
+ void app_uartcomm_configure (uint32_t baudrate , bool enabled , UART_PORT port_number ) {
192
+ if (port_number >= UART_NUMBER ) {
185
193
return ;
186
194
}
187
- if (baudrate > 0 ){
195
+
196
+ if (baudrate > 0 ) {
188
197
uart_cfg [port_number ].speed = baudrate ;
189
198
}
199
+
190
200
if (thread_is_running && uart_is_running [port_number ]) {
191
201
sdStart (serialPortDriverRx [port_number ], & uart_cfg [port_number ]);
192
202
}
203
+
193
204
if (enabled ) {
194
205
palSetPadMode (TxGpioPort [port_number ], TxGpioPin [port_number ], PAL_MODE_ALTERNATE (gpioAF [port_number ]) |
195
206
PAL_STM32_OSPEED_HIGHEST |
@@ -205,13 +216,17 @@ void app_uartcomm_configure(uint32_t baudrate, bool enabled, unsigned int port_n
205
216
206
217
static THD_FUNCTION (packet_process_thread , arg ) {
207
218
(void )arg ;
219
+
208
220
chRegSetThreadName ("uartcomm proc" );
221
+
209
222
event_listener_t el [UART_NUMBER ];
210
223
for (int port_number = 0 ; port_number < UART_NUMBER ; port_number ++ ) {
211
224
chEvtRegisterMaskWithFlags (& (* serialPortDriverRx [port_number ]).event , & el [port_number ], EVENT_MASK (0 ), CHN_INPUT_AVAILABLE );
212
225
}
226
+
213
227
for (;;) {
214
228
chEvtWaitAnyTimeout (ALL_EVENTS , ST2MS (10 ));
229
+
215
230
bool rx = true;
216
231
while (rx ) {
217
232
rx = false;
0 commit comments