1+ #pragma once
2+
3+ /**
4+ * @brief Initialize USART peripheral
5+ * Enable clock source for USART peripheral and associated GPIO port
6+ */
7+ static inline void USART_init (USART_TypeDef * pusart )
8+ {
9+ if (pusart == USART1 ){
10+ // enable clock for GPIOA and USART1
11+ RCC -> APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_IOPAEN ;
12+
13+ // reset pin configurations for PA9 and PA10
14+ GPIOA -> CRH &= ~(GPIO_CRH_MODE10 | GPIO_CRH_MODE9 | GPIO_CRH_CNF10 | GPIO_CRH_CNF9 );
15+
16+ // PA9 as Output@50Hz Alternate function
17+ GPIOA -> CRH |= GPIO_CRH_MODE9_0 | GPIO_CRH_MODE9_1 | GPIO_CRH_CNF9_1 ;
18+
19+ // PA10 as floating input
20+ GPIOA -> CRH |= GPIO_CRH_CNF10_0 ;
21+ }
22+
23+ }
24+
25+ /**
26+ * @brief Enable clock for complete USART peripheral
27+ *
28+ * @param pusart USART instance
29+ */
30+ static inline USART_enable (USART_TypeDef * pusart ) {
31+ pusart -> CR1 |= USART_CR1_UE ;
32+ }
33+
34+
35+ /**
36+ * @brief Enable clock for complete USART peripheral
37+ *
38+ * @param pusart USART instance
39+ */
40+ static inline USART_disable (USART_TypeDef * pusart ) {
41+ pusart -> CR1 &= ~USART_CR1_UE ;
42+ }
43+
44+ /**
45+ * @brief Enable clock for complete USART peripheral
46+ *
47+ * @param pusart USART instance
48+ */
49+ USART_set_baud (USART_TypeDef * pusart , uint32_t baudrate ){
50+ uint32_t baud = (uint32_t )(SystemCoreClock / baudrate );
51+ USART1 -> BRR = baud ;
52+ }
53+
54+ /**
55+ * @brief Enable transmitter clock
56+ *
57+ * @param pusart USART instance
58+ */
59+ static inline USART_tx_enable (USART_TypeDef * pusart ) {
60+ pusart -> CR1 |= USART_CR1_TE ;
61+ }
62+
63+ /**
64+ * @brief Enable dma requests from transmitter
65+ *
66+ * @param pusart USART instance
67+ */
68+ static inline USART_tx_dma_enable (USART_TypeDef * pusart ) {
69+ pusart -> CR3 |= USART_CR3_DMAT ;
70+ }
71+ /**
72+ * @brief Enable clock for complete USART peripheral
73+ *
74+ * @param pusart USART instance
75+ */
76+ static inline USART_tx_dma_disable (USART_TypeDef * pusart ) {
77+ pusart -> CR3 &= ~USART_CR3_DMAT ;
78+ }
79+
80+ /**
81+ * @brief Enable receiver buffer
82+ *
83+ * @param pusart USART instance
84+ */
85+ static inline USART_rx_enable (USART_TypeDef * pusart ) {
86+ pusart -> CR1 |= USART_CR1_RE ;
87+ }
88+
89+ /**
90+ * @brief Enable clock for complete USART peripheral
91+ *
92+ * @param pusart USART instance
93+ */
94+ static inline USART_rx_interrupt_enable (USART_TypeDef * pusart ) {
95+ pusart -> CR1 |= USART_CR1_TXEIE ;
96+ }
97+
98+ /**
99+ * @brief Enable clock for complete USART peripheral
100+ *
101+ * @param pusart USART instance
102+ */
103+ static inline USART_rx_dma_enable (USART_TypeDef * pusart ) {
104+ pusart -> CR3 | USART_CR3_DMAR ;
105+ }
106+
107+ /**
108+ * @brief Transmit 1 character
109+ *
110+ * @param c character to be transmitted
111+ */
112+ void USART1_putch (char c );
113+
114+ /**
115+ * @brief Transmit string
116+ *
117+ * @param str pointer to the string
118+ */
119+ void USART1_puts (const char * str );
120+
121+ /**
122+ * @brief Interrupt service routine for USART1 related interrupts
123+ *
124+ */
125+ void USART1_IRQHandler (void );
0 commit comments