Skip to content

Commit b5cbaaa

Browse files
committed
STM32: Serial - use TXE as tx_irq instead of TC
TXE indicates that a byte can be written to UART register for sending, while TC indicates that last byte was completely sent. So the TXE flag can be used in case of interrupt based Serial communication, to allow faster and efficient application buffer emptying. Also TXE flag will be erased from the interrupt when writing to register. In case there is nothing to write in the register, the application is expected to disable the interrupt.
1 parent 85711eb commit b5cbaaa

File tree

9 files changed

+37
-46
lines changed

9 files changed

+37
-46
lines changed

targets/TARGET_STM/TARGET_STM32F0/serial_device.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,9 @@ static void uart_irq(int id)
264264
UART_HandleTypeDef * huart = &uart_handlers[id];
265265

266266
if (serial_irq_ids[id] != 0) {
267-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
268-
if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) {
267+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
268+
if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) {
269269
irq_handler(serial_irq_ids[id], TxIrq);
270-
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
271270
}
272271
}
273272
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -418,7 +417,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
418417
if (irq == RxIrq) {
419418
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
420419
} else { // TxIrq
421-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
420+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
422421
}
423422
NVIC_SetVector(irq_n, vector);
424423
NVIC_EnableIRQ(irq_n);
@@ -432,7 +431,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
432431
all_disabled = 1;
433432
}
434433
} else { // TxIrq
435-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
434+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
436435
// Check if RxIrq is disabled too
437436
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
438437
all_disabled = 1;

targets/TARGET_STM/TARGET_STM32F1/serial_device.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,9 @@ static void uart_irq(int id)
159159
UART_HandleTypeDef * huart = &uart_handlers[id];
160160

161161
if (serial_irq_ids[id] != 0) {
162-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
163-
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) {
162+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
163+
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) {
164164
irq_handler(serial_irq_ids[id], TxIrq);
165-
__HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
166165
}
167166
}
168167
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -228,7 +227,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
228227
if (irq == RxIrq) {
229228
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
230229
} else { // TxIrq
231-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
230+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
232231
}
233232
NVIC_SetVector(irq_n, vector);
234233
NVIC_EnableIRQ(irq_n);
@@ -242,7 +241,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
242241
all_disabled = 1;
243242
}
244243
} else { // TxIrq
245-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
244+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
246245
// Check if RxIrq is disabled too
247246
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
248247
all_disabled = 1;

targets/TARGET_STM/TARGET_STM32F2/serial_device.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,9 @@ static void uart_irq(int id)
246246
UART_HandleTypeDef * huart = &uart_handlers[id];
247247

248248
if (serial_irq_ids[id] != 0) {
249-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
250-
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) {
249+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
250+
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) {
251251
irq_handler(serial_irq_ids[id], TxIrq);
252-
__HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
253252
}
254253
}
255254
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -385,7 +384,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
385384
if (irq == RxIrq) {
386385
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
387386
} else { // TxIrq
388-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
387+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
389388
}
390389
NVIC_SetVector(irq_n, vector);
391390
NVIC_EnableIRQ(irq_n);
@@ -399,7 +398,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
399398
all_disabled = 1;
400399
}
401400
} else { // TxIrq
402-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
401+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
403402
// Check if RxIrq is disabled too
404403
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
405404
all_disabled = 1;

targets/TARGET_STM/TARGET_STM32F3/serial_device.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ static void uart_irq(int id)
210210
UART_HandleTypeDef * huart = &uart_handlers[id];
211211

212212
if (serial_irq_ids[id] != 0) {
213-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
214-
if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) {
213+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
214+
if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) {
215215
irq_handler(serial_irq_ids[id], TxIrq);
216-
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
217216
}
218217
}
219218
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -311,7 +310,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
311310
if (irq == RxIrq) {
312311
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
313312
} else { // TxIrq
314-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
313+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
315314
}
316315
NVIC_SetVector(irq_n, vector);
317316
NVIC_EnableIRQ(irq_n);
@@ -325,7 +324,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
325324
all_disabled = 1;
326325
}
327326
} else { // TxIrq
328-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
327+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
329328
// Check if RxIrq is disabled too
330329
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
331330
all_disabled = 1;

targets/TARGET_STM/TARGET_STM32F4/serial_device.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,9 @@ static void uart_irq(int id)
273273
UART_HandleTypeDef * huart = &uart_handlers[id];
274274

275275
if (serial_irq_ids[id] != 0) {
276-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
277-
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) {
276+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
277+
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) {
278278
irq_handler(serial_irq_ids[id], TxIrq);
279-
__HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
280279
}
281280
}
282281
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -438,7 +437,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
438437
if (irq == RxIrq) {
439438
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
440439
} else { // TxIrq
441-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
440+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
442441
}
443442
NVIC_SetVector(irq_n, vector);
444443
NVIC_EnableIRQ(irq_n);
@@ -452,7 +451,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
452451
all_disabled = 1;
453452
}
454453
} else { // TxIrq
455-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
454+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
456455
// Check if RxIrq is disabled too
457456
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
458457
all_disabled = 1;

targets/TARGET_STM/TARGET_STM32F7/serial_device.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,9 @@ static void uart_irq(int id)
238238
UART_HandleTypeDef * huart = &uart_handlers[id];
239239

240240
if (serial_irq_ids[id] != 0) {
241-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
242-
if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) {
241+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
242+
if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) {
243243
irq_handler(serial_irq_ids[id], TxIrq);
244-
__HAL_UART_CLEAR_IT(huart, UART_CLEAR_TCF);
245244
}
246245
}
247246
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -373,7 +372,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
373372
if (irq == RxIrq) {
374373
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
375374
} else { // TxIrq
376-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
375+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
377376
}
378377
NVIC_SetVector(irq_n, vector);
379378
NVIC_EnableIRQ(irq_n);
@@ -387,7 +386,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
387386
all_disabled = 1;
388387
}
389388
} else { // TxIrq
390-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
389+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
391390
// Check if RxIrq is disabled too
392391
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
393392
all_disabled = 1;

targets/TARGET_STM/TARGET_STM32L0/serial_device.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,9 @@ static void uart_irq(int id)
201201
UART_HandleTypeDef * huart = &uart_handlers[id];
202202

203203
if (serial_irq_ids[id] != 0) {
204-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
205-
if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) {
204+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
205+
if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) {
206206
irq_handler(serial_irq_ids[id], TxIrq);
207-
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
208207
}
209208
}
210209
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -302,7 +301,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
302301
if (irq == RxIrq) {
303302
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
304303
} else { // TxIrq
305-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
304+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
306305
}
307306
NVIC_SetVector(irq_n, vector);
308307
NVIC_EnableIRQ(irq_n);
@@ -316,7 +315,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
316315
all_disabled = 1;
317316
}
318317
} else { // TxIrq
319-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
318+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
320319
// Check if RxIrq is disabled too
321320
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
322321
all_disabled = 1;

targets/TARGET_STM/TARGET_STM32L1/serial_device.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,9 @@ static void uart_irq(int id)
190190
UART_HandleTypeDef * huart = &uart_handlers[id];
191191

192192
if (serial_irq_ids[id] != 0) {
193-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
194-
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) {
193+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
194+
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) {
195195
irq_handler(serial_irq_ids[id], TxIrq);
196-
__HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
197196
}
198197
}
199198
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -284,7 +283,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
284283
if (irq == RxIrq) {
285284
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
286285
} else { // TxIrq
287-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
286+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
288287
}
289288
NVIC_SetVector(irq_n, vector);
290289
NVIC_EnableIRQ(irq_n);
@@ -298,7 +297,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
298297
all_disabled = 1;
299298
}
300299
} else { // TxIrq
301-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
300+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
302301
// Check if RxIrq is disabled too
303302
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
304303
all_disabled = 1;

targets/TARGET_STM/TARGET_STM32L4/serial_device.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,9 @@ static void uart_irq(int id)
217217
UART_HandleTypeDef * huart = &uart_handlers[id];
218218

219219
if (serial_irq_ids[id] != 0) {
220-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
221-
if (__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) {
222-
irq_handler(serial_irq_ids[id], TxIrq);
223-
__HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
220+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
221+
if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) {
222+
irq_handler(serial_irq_ids[id], TxIrq);
224223
}
225224
}
226225
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
@@ -330,7 +329,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
330329
if (irq == RxIrq) {
331330
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
332331
} else { // TxIrq
333-
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
332+
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
334333
}
335334
NVIC_SetVector(irq_n, vector);
336335
NVIC_EnableIRQ(irq_n);
@@ -344,7 +343,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
344343
all_disabled = 1;
345344
}
346345
} else { // TxIrq
347-
__HAL_UART_DISABLE_IT(huart, UART_IT_TC);
346+
__HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
348347
// Check if RxIrq is disabled too
349348
if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
350349
all_disabled = 1;

0 commit comments

Comments
 (0)