Hierodule 1.6.2
Utility module set for STM32 MCUs
Loading...
Searching...
No Matches
hierodule_usart.c
Go to the documentation of this file.
1
12#include <hierodule_usart.h>
13
28
33#if ( (defined __STM32F103xB_H) || (defined __STM32F401xC_H) )
36#endif
42#ifdef __STM32F103xB_H
45#endif
51#ifdef __STM32F401xC_H
54#endif
62{
64 #ifdef __STM32F030x6_H
65 return (uint8_t)(READ_BIT(Wrapper->USART->RDR, USART_RDR_RDR));
67 #else
68 return (uint8_t)(READ_BIT(Wrapper->USART->DR, USART_DR_DR));
70 #endif
71}
72
88(
89 USART_TypeDef *USART,
90 uint16_t RX_BufferSize,
91 void (*RX_Handler)(uint8_t)
92)
93{
94 if( USART == NULL )
95 {
96 return NULL;
97 }
98
100
101 uint32_t USART_Address = (uint32_t)USART;
102
103 switch(USART_Address)
104 {
105 case ( (uint32_t)USART1 ):
107 break;
109 #if ( (defined __STM32F103xB_H) || (defined __STM32F401xC_H) )
110 case ( (uint32_t)USART2 ):
112 break;
114 #endif
115 #ifdef __STM32F103xB_H
116 case ( (uint32_t)USART3 ):
118 break;
120 #endif
121 #ifdef __STM32F401xC_H
122 case ( (uint32_t)USART6 ):
124 break;
126 #endif
127 default:
128 return NULL;
129 break;
130 }
131
132 (*Wrapper) = (HIERODULE_USART_Wrapper*)malloc(sizeof(HIERODULE_USART_Wrapper));
133
134 (*Wrapper)->USART = USART;
135
136 (*Wrapper)->RX_Index = 0;
137 (*Wrapper)->RX_New = 0;
138
139 (*Wrapper)->RX_BufferSize = RX_BufferSize;
140 (*Wrapper)->RX_Buffer = (uint8_t*)malloc( ((*Wrapper)->RX_BufferSize) * sizeof(uint8_t));
141 for( uint16_t _rxb_init = 0 ; _rxb_init < (*Wrapper)->RX_BufferSize ; _rxb_init++ )
142 {
143 ((*Wrapper)->RX_Buffer)[_rxb_init] = '\0';
144 }
145 (*Wrapper)->RX_Handler = RX_Handler;
146
147 CLEAR_BIT((*Wrapper)->USART->CR1, USART_CR1_RE);
148
150 #ifdef __STM32F030x6_H
151 CLEAR_BIT((*Wrapper)->USART->ISR, USART_ISR_RXNE);
153 #else
154 CLEAR_BIT((*Wrapper)->USART->SR, USART_SR_RXNE);
156 #endif
158 return Wrapper;
159}
160
165{
166
167 CLEAR_BIT(Wrapper->USART->CR1, USART_CR1_RE);
168
170 #ifdef __STM32F030x6_H
171 CLEAR_BIT(Wrapper->USART->ISR, USART_ISR_RXNE);
173 #else
174 CLEAR_BIT(Wrapper->USART->SR, USART_SR_RXNE);
176 #endif
178 free(Wrapper->RX_Buffer);
179 Wrapper->RX_Buffer = NULL;
180
181 free(Wrapper);
182 Wrapper = NULL;
183}
184
188{
189 SET_BIT(Wrapper->USART->CR1, USART_CR1_RXNEIE);
190 SET_BIT(Wrapper->USART->CR1, USART_CR1_RE);
191}
192
196{
197 CLEAR_BIT(Wrapper->USART->CR1, USART_CR1_RXNEIE);
198 CLEAR_BIT(Wrapper->USART->CR1, USART_CR1_RE);
199}
200
204{
206 #ifdef __STM32F030x6_H
207 return (READ_BIT(Wrapper->USART->ISR, USART_ISR_RXNE) == (USART_ISR_RXNE));
209 #else
210 return (READ_BIT(Wrapper->USART->SR, USART_SR_RXNE) == (USART_SR_RXNE));
212 #endif
213}
214
218{
220 #ifdef __STM32F030x6_H
221 return (READ_BIT(Wrapper->USART->ISR, USART_ISR_TXE) == (USART_ISR_TXE));
223 #else
224 return (READ_BIT(Wrapper->USART->SR, USART_SR_TXE) == (USART_SR_TXE));
226 #endif
227}
228
234{
235 if(Wrapper->RX_New > 0)
236 {
237 Wrapper->RX_New--;
238
239 return Wrapper->RX_Buffer
240 [
243 ];
244 }
245 else
246 {
247 return '\0';
248 }
249}
250
256{
257 while
258 (
260 ||
262 );
263
265 #ifdef __STM32F030x6_H
266 Wrapper->USART->TDR = Byte;
268 #else
269 Wrapper->USART->DR = Byte;
271 #endif
272}
273
280{
281 volatile uint32_t _ctr = 0;
282 while( 1 )
283 {
284 if( String[_ctr] == '\0' )
285 {
286 break;
287 }
288 else
289 {
290 HIERODULE_USART_TransmitByte( Wrapper, String[_ctr] );
291 _ctr++;
292 }
293 }
294}
295
314{
316 {
317 if(Wrapper != NULL)
318 {
321
322 Wrapper->RX_Index++;
324
326 Wrapper->RX_New++;
327
328 if(Wrapper->RX_Handler != NULL)
329 {
330 Wrapper->RX_Handler
331 (
333 );
334 }
335 }
336 }
337}
350extern void USART1_IRQHandler(void)
351{
353}
354
356#if ( (defined __STM32F103xB_H) || (defined __STM32F401xC_H) )
362extern void USART2_IRQHandler(void)
363{
365}
367#endif
368
369#ifdef __STM32F103xB_H
375extern void USART3_IRQHandler(void)
376{
378}
380#endif
381
382#ifdef __STM32F401xC_H
388extern void USART6_IRQHandler(void)
389{
391}
393#endif
static HIERODULE_USART_Wrapper * USART1_Wrapper
USART wrapper pointer meant for USART1.
void USART_IRQHandler(HIERODULE_USART_Wrapper *Wrapper)
The base IRQ body to be used for all USART IRQs.
static HIERODULE_USART_Wrapper * USART6_Wrapper
USART wrapper pointer meant for USART6. Requires the device specific macro __STM32F401xC_H to be defi...
static HIERODULE_USART_Wrapper * USART3_Wrapper
USART wrapper pointer meant for USART3. Requires the device specific macro __STM32F103xB_H to be defi...
static HIERODULE_USART_Wrapper * USART2_Wrapper
USART wrapper pointer meant for USART2. Requires the device specific macro __STM32F103xB_H or __STM32...
uint8_t ReceiveByte(HIERODULE_USART_Wrapper *Wrapper)
Reads and returns a single byte received by the USART peripheral.
void USART2_IRQHandler(void)
USART2 IRQ implementation. Requires the device specific macro __STM32F103xB_H or __STM32F401xC_H to b...
void USART3_IRQHandler(void)
USART3 IRQ implementation. Requires the device specific macro __STM32F103xB_H to be defined.
void USART6_IRQHandler(void)
USART6 IRQ implementation. Requires the device specific macro __STM32F401xC_H to be defined.
uint32_t HIERODULE_USART_IsActiveFlag_TXE(HIERODULE_USART_Wrapper *Wrapper)
Checks the TX is empty interrupt flag of the USART peripheral.
uint32_t HIERODULE_USART_IsActiveFlag_RXNE(HIERODULE_USART_Wrapper *Wrapper)
Checks the RX not empty interrupt flag of the USART peripheral.
void HIERODULE_USART_TransmitByte(HIERODULE_USART_Wrapper *Wrapper, uint8_t Byte)
Transmits a single byte.
void USART1_IRQHandler(void)
USART1 IRQ implementation.
void HIERODULE_USART_ReleaseWrapper(HIERODULE_USART_Wrapper *Wrapper)
Frees the memory allocated to a USART wrapper and clears USART status flags and control bits.
uint8_t HIERODULE_USART_GetNextByte(HIERODULE_USART_Wrapper *Wrapper)
Fetches the next byte in the ring buffer.
void HIERODULE_USART_TransmitString(HIERODULE_USART_Wrapper *Wrapper, char *String)
Transmits a string.
void HIERODULE_USART_Disable_IT_RXNE(HIERODULE_USART_Wrapper *Wrapper)
Disables the RX not empty interrupt of the USART peripheral , also disables the RE bit of the control...
void HIERODULE_USART_Enable_IT_RXNE(HIERODULE_USART_Wrapper *Wrapper)
Enables the RX not empty interrupt of the USART peripheral , also enables the RE bit of the control r...
HIERODULE_USART_Wrapper ** HIERODULE_USART_InitWrapper(USART_TypeDef *USART, uint16_t RX_BufferSize, void(*RX_Handler)(uint8_t))
Initializes a wrapper for the specified USART peripheral.
HIERODULE_USB_Wrapper Wrapper
Extern declaration for the wrapper instance in the source file.
: Header file for the USART module.
Struct that keeps variables for the ring buffer, a pointer to the USART peripheral and a pointer to t...
uint8_t * RX_Buffer
The ring buffer where the data received is appended.
uint32_t RX_New
Number of new bytes in the ring buffer.
uint16_t RX_BufferSize
Number of elements in the ring buffer.
uint32_t RX_Index
Index of next-byte-to-be-received in the ring buffer.