/** * \file * * * \brief STM32F10xx UART interface driver. * * \author Daniele Basile */ #ifndef SER_STM32_H #define SER_STM32_H #include #include /* cpu_relax() */ #include /* Serial hardware numbers */ enum { SER_UART1 = 0, SER_UART2, #if CPU_CM3_STM32F103RB || CPU_CM3_STM32F103RE SER_UART3, #endif SER_CNT //< Number of serial ports }; /* Software errors */ #define SERRF_RXFIFOOVERRUN BV(6) //< Rx FIFO buffer overrun #define SERRF_RXTIMEOUT BV(5) //< Receive timeout #define SERRF_TXTIMEOUT BV(4) //< Transmit timeout /* * Hardware errors. */ #define SERRF_RXSROVERRUN SR_ORE //< Input overrun #define SERRF_FRAMEERROR SR_FE //< Stop bit missing #define SERRF_PARITYERROR SR_PE //< Parity error #define SERRF_NOISEERROR SR_NE //< Noise error /* Serial error/status flags */ typedef uint32_t serstatus_t; INLINE void stm32_uartDisable(uint32_t base) { struct stm32_usart *_base = (struct stm32_usart *)base; _base->CR1 &= ~CR1_RUN_RESET; } INLINE void stm32_uartEnable(uint32_t base) { struct stm32_usart *_base = (struct stm32_usart *)base; _base->CR1 |= CR1_RUN_SET; } /* Clear the flags register */ INLINE void stm32_uartClear(uint32_t base) { struct stm32_usart *_base = (struct stm32_usart *)base; _base->SR &= ~USART_FLAG_MASK; } INLINE bool stm32_uartTxDone(uint32_t base) { struct stm32_usart *_base = (struct stm32_usart *)base; return (_base->SR & USART_FLAG_TC); } INLINE bool stm32_uartTxReady(uint32_t base) { struct stm32_usart *_base = (struct stm32_usart *)base; return (_base->SR & (BV(CR1_TXEIE) | BV(CR1_TCIE))); } INLINE bool stm32_uartRxReady(uint32_t base) { struct stm32_usart *_base = (struct stm32_usart *)base; return (_base->SR & BV(CR1_RXNEIE)); } INLINE int stm32_uartPutChar(uint32_t base, unsigned char c) { struct stm32_usart *_base = (struct stm32_usart *)base; while (!stm32_uartTxReady(base)) cpu_relax(); _base->DR = c; return c; } INLINE int stm32_uartGetChar(uint32_t base) { struct stm32_usart * _base = (struct stm32_usart *)base; return _base->DR; } void stm32_uartSetBaudRate(uint32_t base, unsigned long baud); void stm32_uartSetParity(uint32_t base, int parity); void stm32_uartInit(int port); #endif /* SER_STM32_H */