11/**
22 ******************************************************************************
33 * @file uart.c
4- * @author WI6LABS
5- * @version V1.0.0
6- * @date 01-August-2016
4+ * @author WI6LABS, fpistm
75 * @brief provide the UART interface
86 *
97 ******************************************************************************
3533 *
3634 ******************************************************************************
3735 */
38- /** @addtogroup CMSIS
39- * @{
40- */
41-
42- /** @addtogroup stm32f4xx_system
43- * @{
44- */
45-
46- /** @addtogroup STM32F4xx_System_Private_Includes
47- * @{
48- */
4936#include "uart.h"
5037#include "Arduino.h"
5138#include "PinAF_STM32F1.h"
5542#endif
5643#if defined(HAL_UART_MODULE_ENABLED )
5744
58- // if DEBUG_UART is not defined assume this is the one
59- // linked to PIN_SERIAL_TX
45+ /* If DEBUG_UART is not defined assume this is the one linked to PIN_SERIAL_TX */
6046#if !defined(DEBUG_UART )
6147#if defined(PIN_SERIAL_TX )
6248#define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX)
6349#define DEBUG_PINNAME_TX digitalPinToPinName(PIN_SERIAL_TX)
6450#else
65- // No debug UART defined
51+ /* No debug UART defined */
6652#define DEBUG_UART NP
6753#define DEBUG_PINNAME_TX NC
6854#endif
7157#define DEBUG_UART_BAUDRATE 9600
7258#endif
7359
74- // @brief uart caracteristics
60+ /* @brief uart caracteristics */
7561#if defined(STM32F4xx )
7662#define UART_NUM (10)
7763#elif defined(STM32F0xx ) || defined(STM32F7xx )
@@ -109,25 +95,28 @@ void uart_init(serial_t *obj)
10995 GPIO_TypeDef * port ;
11096 uint32_t function = (uint32_t )NC ;
11197
112- // Determine the UART to use (UART_1, UART_2 , ...)
98+ /* Determine the U(S)ART peripheral to use (USART1, USART2 , ...) */
11399 USART_TypeDef * uart_tx = pinmap_peripheral (obj -> pin_tx , PinMap_UART_TX );
114100 USART_TypeDef * uart_rx = pinmap_peripheral (obj -> pin_rx , PinMap_UART_RX );
115101
116- // Pins Rx/Tx must not be NP
102+ /* Pins Rx/Tx must not be NP */
117103 if (uart_rx == NP || uart_tx == NP ) {
118104 printf ("ERROR: at least one UART pin has no peripheral\n" );
119105 return ;
120106 }
121107
122- // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
108+ /*
109+ * Get the peripheral name (USART1, USART2, ...) from the pin
110+ * and assign it to the object
111+ */
123112 obj -> uart = pinmap_merge_peripheral (uart_tx , uart_rx );
124113
125114 if (obj -> uart == NP ) {
126- printf ("ERROR: UART pins mismatch\n" );
115+ printf ("ERROR: U(S)ART pins mismatch\n" );
127116 return ;
128117 }
129118
130- // Enable USART clock
119+ /* Enable USART clock */
131120#if defined(USART1_BASE )
132121 else if (obj -> uart == USART1 ) {
133122 __HAL_RCC_USART1_FORCE_RESET ();
@@ -264,8 +253,8 @@ void uart_init(serial_t *obj)
264253 }
265254#endif
266255
267- // Configure GPIOs
268- //RX
256+ /* Configure GPIOs */
257+ /* RX */
269258 port = set_GPIO_Port_Clock (STM_PORT (obj -> pin_rx ));
270259 function = pinmap_function (obj -> pin_rx , PinMap_UART_RX );
271260 GPIO_InitStruct .Pin = STM_GPIO_PIN (obj -> pin_rx );
@@ -284,7 +273,7 @@ void uart_init(serial_t *obj)
284273#endif
285274 HAL_GPIO_Init (port , & GPIO_InitStruct );
286275
287- //TX
276+ /* TX */
288277 port = set_GPIO_Port_Clock (STM_PORT (obj -> pin_tx ));
289278 function = pinmap_function (obj -> pin_tx , PinMap_UART_TX );
290279 GPIO_InitStruct .Pin = STM_GPIO_PIN (obj -> pin_tx );
@@ -293,7 +282,7 @@ void uart_init(serial_t *obj)
293282 HAL_GPIO_Init (port , & GPIO_InitStruct );
294283
295284
296- // Configure uart
285+ /* Configure uart */
297286 uart_handlers [obj -> index ] = huart ;
298287 huart -> Instance = (USART_TypeDef * )(obj -> uart );
299288 huart -> Init .BaudRate = obj -> baudrate ;
@@ -312,7 +301,11 @@ void uart_init(serial_t *obj)
312301#endif
313302
314303#if defined(LPUART1_BASE )
315- /* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
304+ /*
305+ * Note that LPUART clock source must be in the range
306+ * [3 x baud rate, 4096 x baud rate]
307+ * check Reference Manual
308+ */
316309 if (obj -> uart == LPUART1 ) {
317310 if (obj -> baudrate <= 9600 ) {
318311 HAL_UARTEx_EnableClockStopMode (huart );
@@ -362,7 +355,7 @@ void uart_init(serial_t *obj)
362355 */
363356void uart_deinit (serial_t * obj )
364357{
365- // Reset UART and disable clock
358+ /* Reset UART and disable clock */
366359 switch (obj -> index ) {
367360#if defined(USART1_BASE )
368361 case 0 :
@@ -659,11 +652,11 @@ int uart_getc(serial_t *obj, unsigned char* c)
659652 }
660653
661654 if (serial_rx_active (obj )) {
662- return -1 ; // transaction ongoing
655+ return -1 ; /* Transaction ongoing */
663656 }
664657
665658 * c = (unsigned char )(obj -> recv );
666- // Restart RX irq
659+ /* Restart RX irq */
667660 UART_HandleTypeDef * huart = uart_handlers [obj -> index ];
668661 HAL_UART_Receive_IT (huart , & (obj -> recv ), 1 );
669662
@@ -683,7 +676,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t*))
683676 return ;
684677 }
685678
686- // Exit if a reception is already on-going
679+ /* Exit if a reception is already on-going */
687680 if (serial_rx_active (obj )) {
688681 return ;
689682 }
@@ -715,11 +708,11 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t*))
715708 tx_callback [obj -> index ] = callback ;
716709 tx_callback_obj [obj -> index ] = obj ;
717710
718- // Enable interrupt
711+ /* Enable interrupt */
719712 HAL_NVIC_SetPriority (obj -> irq , 0 , 2 );
720713 HAL_NVIC_EnableIRQ (obj -> irq );
721714
722- // the following function will enable UART_IT_TXE and error interrupts
715+ /* The following function will enable UART_IT_TXE and error interrupts */
723716 if (HAL_UART_Transmit_IT (uart_handlers [obj -> index ], & obj -> tx_buff [obj -> tx_tail ], 1 ) != HAL_OK ) {
724717 return ;
725718 }
@@ -788,23 +781,23 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
788781 volatile uint32_t tmpval ;
789782#if defined(STM32F1xx ) || defined(STM32F2xx ) || defined(STM32F4xx ) || defined(STM32L1xx )
790783 if (__HAL_UART_GET_FLAG (huart , UART_FLAG_PE ) != RESET ) {
791- tmpval = huart -> Instance -> DR ; // Clear PE flag
784+ tmpval = huart -> Instance -> DR ; /* Clear PE flag */
792785 } else if (__HAL_UART_GET_FLAG (huart , UART_FLAG_FE ) != RESET ) {
793- tmpval = huart -> Instance -> DR ; // Clear FE flag
786+ tmpval = huart -> Instance -> DR ; /* Clear FE flag */
794787 } else if (__HAL_UART_GET_FLAG (huart , UART_FLAG_NE ) != RESET ) {
795- tmpval = huart -> Instance -> DR ; // Clear NE flag
788+ tmpval = huart -> Instance -> DR ; /* Clear NE flag */
796789 } else if (__HAL_UART_GET_FLAG (huart , UART_FLAG_ORE ) != RESET ) {
797- tmpval = huart -> Instance -> DR ; // Clear ORE flag
790+ tmpval = huart -> Instance -> DR ; /* Clear ORE flag */
798791 }
799792#else
800793 if (__HAL_UART_GET_FLAG (huart , UART_FLAG_PE ) != RESET ) {
801- tmpval = huart -> Instance -> RDR ; // Clear PE flag
794+ tmpval = huart -> Instance -> RDR ; /* Clear PE flag */
802795 } else if (__HAL_UART_GET_FLAG (huart , UART_FLAG_FE ) != RESET ) {
803- tmpval = huart -> Instance -> RDR ; // Clear FE flag
796+ tmpval = huart -> Instance -> RDR ; /* Clear FE flag */
804797 } else if (__HAL_UART_GET_FLAG (huart , UART_FLAG_NE ) != RESET ) {
805- tmpval = huart -> Instance -> RDR ; // Clear NE flag
798+ tmpval = huart -> Instance -> RDR ; /* Clear NE flag */
806799 } else if (__HAL_UART_GET_FLAG (huart , UART_FLAG_ORE ) != RESET ) {
807- tmpval = huart -> Instance -> RDR ; // Clear ORE flag
800+ tmpval = huart -> Instance -> RDR ; /* Clear ORE flag */
808801 }
809802#endif
810803
@@ -876,7 +869,7 @@ void USART3_IRQHandler(void)
876869 HAL_UART_IRQHandler (uart_handlers [2 ]);
877870 }
878871#if defined(STM32F0xx )
879- // USART3_4_IRQn
872+ /* USART3_4_IRQn */
880873 if (uart_handlers [3 ] != NULL ) {
881874 HAL_UART_IRQHandler (uart_handlers [3 ]);
882875 }
@@ -887,9 +880,9 @@ void USART3_IRQHandler(void)
887880 if (uart_handlers [5 ] != NULL ) {
888881 HAL_UART_IRQHandler (uart_handlers [5 ]);
889882 }
890- #endif // STM32F030xC
891- #endif // STM32F0xx
892- #endif // STM32F091xC || STM32F098xx
883+ #endif /* STM32F030xC */
884+ #endif /* STM32F0xx */
885+ #endif /* STM32F091xC || STM32F098xx */
893886}
894887#endif
895888
0 commit comments