58
58
// linked to PIN_SERIAL_TX
59
59
#if !defined(DEBUG_UART )
60
60
#if defined(PIN_SERIAL_TX )
61
- #define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX)
61
+ #define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX)
62
+ #define DEBUG_PINNAME_TX digitalPinToPinName(PIN_SERIAL_TX)
62
63
#else
63
- #define DEBUG_UART NP
64
+ // No debug UART defined
65
+ #define DEBUG_UART NP
66
+ #define DEBUG_PINNAME_TX NC
64
67
#endif
65
68
#endif
69
+ #if !defined(DEBUG_UART_BAUDRATE )
70
+ #define DEBUG_UART_BAUDRATE 9600
71
+ #endif
72
+
66
73
// @brief uart caracteristics
67
74
#if defined(STM32F4xx )
68
75
#define UART_NUM (10)
@@ -83,6 +90,8 @@ static serial_t *rx_callback_obj[UART_NUM];
83
90
static int (* tx_callback [UART_NUM ])(serial_t * );
84
91
static serial_t * tx_callback_obj [UART_NUM ];
85
92
93
+ static serial_t serial_debug = { .uart = NP , .index = UART_NUM };
94
+
86
95
/**
87
96
* @brief Function called to initialize the uart interface
88
97
* @param obj : pointer to serial_t structure
@@ -97,6 +106,7 @@ void uart_init(serial_t *obj)
97
106
UART_HandleTypeDef * huart = & (obj -> handle );
98
107
GPIO_InitTypeDef GPIO_InitStruct ;
99
108
GPIO_TypeDef * port ;
109
+ uint32_t function = (uint32_t )NC ;
100
110
101
111
// Determine the UART to use (UART_1, UART_2, ...)
102
112
USART_TypeDef * uart_tx = pinmap_peripheral (obj -> pin_tx , PinMap_UART_TX );
@@ -238,27 +248,29 @@ void uart_init(serial_t *obj)
238
248
//Configure GPIOs
239
249
//RX
240
250
port = set_GPIO_Port_Clock (STM_PORT (obj -> pin_rx ));
251
+ function = pinmap_function (obj -> pin_rx , PinMap_UART_RX );
241
252
GPIO_InitStruct .Pin = STM_GPIO_PIN (obj -> pin_rx );
242
- GPIO_InitStruct .Mode = STM_PIN_MODE (pinmap_function ( obj -> pin_rx , PinMap_UART_RX ) );
253
+ GPIO_InitStruct .Mode = STM_PIN_MODE (function );
243
254
GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_HIGH ;
244
- GPIO_InitStruct .Pull = STM_PIN_PUPD (pinmap_function ( obj -> pin_rx , PinMap_UART_RX ) );
255
+ GPIO_InitStruct .Pull = STM_PIN_PUPD (function );
245
256
#ifdef STM32F1xx
246
- pin_SetF1AFPin (STM_PIN_AFNUM (pinmap_function ( obj -> pin_rx , PinMap_UART_RX ) ));
257
+ pin_SetF1AFPin (STM_PIN_AFNUM (function ));
247
258
#else
248
- GPIO_InitStruct .Alternate = STM_PIN_AFNUM (pinmap_function ( obj -> pin_rx , PinMap_UART_RX ) );
259
+ GPIO_InitStruct .Alternate = STM_PIN_AFNUM (function );
249
260
#endif /* STM32F1xx */
250
261
HAL_GPIO_Init (port , & GPIO_InitStruct );
251
262
252
263
//TX
253
264
port = set_GPIO_Port_Clock (STM_PORT (obj -> pin_tx ));
265
+ function = pinmap_function (obj -> pin_tx , PinMap_UART_TX );
254
266
GPIO_InitStruct .Pin = STM_GPIO_PIN (obj -> pin_tx );
255
- GPIO_InitStruct .Mode = STM_PIN_MODE (pinmap_function ( obj -> pin_tx , PinMap_UART_TX ) );
267
+ GPIO_InitStruct .Mode = STM_PIN_MODE (function );
256
268
GPIO_InitStruct .Speed = GPIO_SPEED_FREQ_HIGH ;
257
- GPIO_InitStruct .Pull = STM_PIN_PUPD (pinmap_function ( obj -> pin_tx , PinMap_UART_TX ) );
269
+ GPIO_InitStruct .Pull = STM_PIN_PUPD (function );
258
270
#ifdef STM32F1xx
259
- pin_SetF1AFPin (STM_PIN_AFNUM (pinmap_function ( obj -> pin_tx , PinMap_UART_TX ) ));
271
+ pin_SetF1AFPin (STM_PIN_AFNUM (function ));
260
272
#else
261
- GPIO_InitStruct .Alternate = STM_PIN_AFNUM (pinmap_function ( obj -> pin_tx , PinMap_UART_TX ) );
273
+ GPIO_InitStruct .Alternate = STM_PIN_AFNUM (function );
262
274
#endif /* STM32F1xx */
263
275
HAL_GPIO_Init (port , & GPIO_InitStruct );
264
276
@@ -399,28 +411,63 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size)
399
411
}
400
412
}
401
413
414
+ /**
415
+ * @brief Function called to initialize the debug uart interface
416
+ * @note Call only if debug U(S)ART peripheral is not already initialized
417
+ * by a Serial instance
418
+ * Default config: 8N1
419
+ * @retval None
420
+ */
421
+ void uart_debug_init (void )
422
+ {
423
+ if ( DEBUG_UART != NP ) {
424
+ serial_debug .pin_rx = pinmap_pin (DEBUG_UART , PinMap_UART_RX );
425
+ #if defined(DEBUG_PINNAME_TX )
426
+ serial_debug .pin_tx = DEBUG_PINNAME_TX ;
427
+ #else
428
+ serial_debug .pin_tx = pinmap_pin (DEBUG_UART , PinMap_UART_TX );
429
+ #endif
430
+ serial_debug .baudrate = DEBUG_UART_BAUDRATE ;
431
+ serial_debug .parity = UART_PARITY_NONE ;
432
+ serial_debug .databits = UART_WORDLENGTH_8B ;
433
+ serial_debug .stopbits = UART_STOPBITS_1 ;
434
+
435
+ uart_init (& serial_debug );
436
+ }
437
+ }
438
+
402
439
/**
403
440
* @brief write the data on the uart: used by printf for debug only (syscalls)
404
- * @param obj : pointer to serial_t structure
405
441
* @param data : bytes to write
406
442
* @param size : number of data to write
407
443
* @retval The number of bytes written
408
444
*/
409
445
size_t uart_debug_write (uint8_t * data , uint32_t size )
410
446
{
411
447
uint8_t index = 0 ;
412
- USART_TypeDef * dbg_uart = DEBUG_UART ;
413
448
uint32_t tickstart = HAL_GetTick ();
449
+
450
+ if (DEBUG_UART == NP ) {
451
+ return 0 ;
452
+ }
453
+ /* Search if DEBUG_UART already initialized */
414
454
for (index = 0 ; index < UART_NUM ; index ++ ) {
415
455
if (uart_handlers [index ] != NULL ) {
416
- if (dbg_uart == uart_handlers [index ]-> Instance ) {
456
+ if (DEBUG_UART == uart_handlers [index ]-> Instance ) {
417
457
break ;
418
458
}
419
459
}
420
460
}
421
461
422
462
if (index >= UART_NUM ) {
423
- return 0 ;
463
+ /* DEBUG_UART not initialized */
464
+ if ( serial_debug .index >= UART_NUM ) {
465
+ uart_debug_init ();
466
+ if ( serial_debug .index >= UART_NUM ) {
467
+ return 0 ;
468
+ }
469
+ }
470
+ index = serial_debug .index ;
424
471
}
425
472
426
473
while (HAL_UART_Transmit (uart_handlers [index ], data , size , TX_TIMEOUT ) != HAL_OK ) {
0 commit comments