Hierodule 1.6.2
Utility module set for STM32 MCUs
Loading...
Searching...
No Matches

Elements of the module that are not confined to the scope of the compilation unit. Perfectly corresponds to the module's header file, except the IRQ handlers. More...

Data Structures

struct  HIERODULE_USART_Wrapper
 Struct that keeps variables for the ring buffer, a pointer to the USART peripheral and a pointer to the ISR for RXNE. More...
 

Functions

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.
 
void HIERODULE_USART_ReleaseWrapper (HIERODULE_USART_Wrapper *Wrapper)
 Frees the memory allocated to a USART wrapper and clears USART status flags and control bits.
 
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 register..
 
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 register..
 
uint32_t HIERODULE_USART_IsActiveFlag_RXNE (HIERODULE_USART_Wrapper *Wrapper)
 Checks the RX not empty interrupt flag of the USART peripheral.
 
uint32_t HIERODULE_USART_IsActiveFlag_TXE (HIERODULE_USART_Wrapper *Wrapper)
 Checks the TX is empty interrupt flag of the USART peripheral.
 
uint8_t HIERODULE_USART_GetNextByte (HIERODULE_USART_Wrapper *Wrapper)
 Fetches the next byte in the ring buffer.
 
void HIERODULE_USART_TransmitByte (HIERODULE_USART_Wrapper *Wrapper, uint8_t Byte)
 Transmits a single byte.
 
void HIERODULE_USART_TransmitString (HIERODULE_USART_Wrapper *Wrapper, char *String)
 Transmits a string.
 
void USART1_IRQHandler (void)
 USART1 IRQ implementation.
 
void USART2_IRQHandler (void)
 USART2 IRQ implementation.
Requires the device specific macro __STM32F103xB_H or __STM32F401xC_H to be defined.
 
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.
 

Detailed Description

Consists of general USART comm routines, a USART wrapper initalizer, a function to get the next byte in the ring buffer and a typedef to be used for the wrapper routines.
Device-specific macro constants and type definitions are imported with an include directive to main.h, where the device driver headers are assumed to be included.
stddef.h and stdlib.h also included for NULL and malloc/free, respectively.

Function Documentation

◆ HIERODULE_USART_Disable_IT_RXNE()

void HIERODULE_USART_Disable_IT_RXNE ( HIERODULE_USART_Wrapper * Wrapper)
Parameters
WrapperPointer to the USART wrapper.
Returns
None

Self-explanatory code, nothing to elaborate.

Definition at line 195 of file hierodule_usart.c.

196{
197 CLEAR_BIT(Wrapper->USART->CR1, USART_CR1_RXNEIE);
198 CLEAR_BIT(Wrapper->USART->CR1, USART_CR1_RE);
199}
HIERODULE_USB_Wrapper Wrapper
Extern declaration for the wrapper instance in the source file.

References Wrapper.

◆ HIERODULE_USART_Enable_IT_RXNE()

void HIERODULE_USART_Enable_IT_RXNE ( HIERODULE_USART_Wrapper * Wrapper)
Parameters
WrapperPointer to the USART wrapper.
Returns
None

Self-explanatory code, nothing to elaborate.

Definition at line 187 of file hierodule_usart.c.

188{
189 SET_BIT(Wrapper->USART->CR1, USART_CR1_RXNEIE);
190 SET_BIT(Wrapper->USART->CR1, USART_CR1_RE);
191}

References Wrapper.

◆ HIERODULE_USART_GetNextByte()

uint8_t HIERODULE_USART_GetNextByte ( HIERODULE_USART_Wrapper * Wrapper)
Parameters
WrapperPointer to the USART wrapper.
Returns
The next byte in the ring buffer, null character if there's none.

RX_New and RX_Index are also updated on call, to revolve the ring buffer, so as to say.

Definition at line 233 of file hierodule_usart.c.

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}
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.

References HIERODULE_USB_Wrapper::RX_Buffer, HIERODULE_USB_Wrapper::RX_BufferSize, HIERODULE_USB_Wrapper::RX_Index, HIERODULE_USB_Wrapper::RX_New, and Wrapper.

Referenced by USART_IRQHandler().

◆ HIERODULE_USART_InitWrapper()

HIERODULE_USART_Wrapper ** HIERODULE_USART_InitWrapper ( USART_TypeDef * USART,
uint16_t RX_BufferSize,
void(* RX_Handler )(uint8_t) )
Parameters
USARTUSART peripheral of the wrapper.
RX_BufferSizeLength of the ring buffer array.
RX_HandlerPointer to the ISR for RXNE.
Returns
Double pointer to the initialized wrapper.

The ring buffer gets a new address allocated to it.
The ring buffer is filled with null characters.
Disables the RE bit of the control register, as well as the RXNE flag in the status register.
The wrapper pointer gets a new address allocated, to be freed at some future point via HIERODULE_USART_ReleaseWrapper, hence the reason a pointer is used for the wrapper; likewise, a double pointer is used to return it by reference.
Device specific checks are performed for the USART peripheral specified.

Definition at line 87 of file hierodule_usart.c.

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}
static HIERODULE_USART_Wrapper * USART1_Wrapper
USART wrapper pointer meant for USART1.
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...
Struct that keeps variables for the ring buffer, a pointer to the USART peripheral and a pointer to t...

References HIERODULE_USB_Wrapper::RX_Index, USART1_Wrapper, USART2_Wrapper, USART3_Wrapper, USART6_Wrapper, and Wrapper.

◆ HIERODULE_USART_IsActiveFlag_RXNE()

uint32_t HIERODULE_USART_IsActiveFlag_RXNE ( HIERODULE_USART_Wrapper * Wrapper)
Parameters
WrapperPointer to the USART wrapper.
Returns
1 if the interrupt flag is set. Returns 0 otherwise.

USART_ISR_RXNE/USART_SR_RXNE bit is isolated from the register with its bitmask and compared to the same bitmask instead of shifting since the amount of shift would depend on the bitmask.

Definition at line 203 of file hierodule_usart.c.

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}

References Wrapper.

Referenced by HIERODULE_USART_TransmitByte(), and USART_IRQHandler().

◆ HIERODULE_USART_IsActiveFlag_TXE()

uint32_t HIERODULE_USART_IsActiveFlag_TXE ( HIERODULE_USART_Wrapper * Wrapper)
Parameters
WrapperPointer to the USART wrapper.
Returns
1 if the interrupt flag is set. Returns 0 otherwise.

USART_ISR_TXE/USART_SR_TXE bit is isolated from the register with its bitmask and compared to the same bitmask instead of shifting since the amount of shift would depend on the bitmask.

Definition at line 217 of file hierodule_usart.c.

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}

References Wrapper.

Referenced by HIERODULE_USART_TransmitByte().

◆ HIERODULE_USART_ReleaseWrapper()

void HIERODULE_USART_ReleaseWrapper ( HIERODULE_USART_Wrapper * Wrapper)
Parameters
WrapperPointer to the USART wrapper.
Returns
None

Ring buffer address is also freed.
Using a released USART wrapper or its fields may result in unexpected behavior.
Keep in mind this clears up the USART wrapper pointer in this file scope; it is recommended to free your double pointer to the wrapper, likewise.

Definition at line 164 of file hierodule_usart.c.

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}

References HIERODULE_USB_Wrapper::RX_Buffer, and Wrapper.

◆ HIERODULE_USART_TransmitByte()

void HIERODULE_USART_TransmitByte ( HIERODULE_USART_Wrapper * Wrapper,
uint8_t Byte )
Parameters
WrapperPointer to the USART wrapper.
Byteto be transmitted.
Returns
None

Will block until both RDR and TDR are empty, which means it's safe to write to the transmit data register.
TE flag in the USART control register is assumed to be already set.

Definition at line 255 of file hierodule_usart.c.

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}
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.

References HIERODULE_USART_IsActiveFlag_RXNE(), HIERODULE_USART_IsActiveFlag_TXE(), and Wrapper.

Referenced by HIERODULE_USART_TransmitString().

◆ HIERODULE_USART_TransmitString()

void HIERODULE_USART_TransmitString ( HIERODULE_USART_Wrapper * Wrapper,
char * String )
Parameters
WrapperPointer to the USART wrapper.
Stringto be transmitted.
Returns
None

Basically calls HIERODULE_USART_TransmitByte for each byte in the string until a null character shows up.
TE flag in the USART control register is assumed to be already set.

Definition at line 279 of file hierodule_usart.c.

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}
void HIERODULE_USART_TransmitByte(HIERODULE_USART_Wrapper *Wrapper, uint8_t Byte)
Transmits a single byte.

References HIERODULE_USART_TransmitByte(), and Wrapper.

◆ USART1_IRQHandler()

void USART1_IRQHandler ( void )
extern
Returns
None

Self-explanatory code, nothing to elaborate.

Definition at line 350 of file hierodule_usart.c.

351{
353}
void USART_IRQHandler(HIERODULE_USART_Wrapper *Wrapper)
The base IRQ body to be used for all USART IRQs.

References USART1_Wrapper, and USART_IRQHandler().

◆ USART2_IRQHandler()

void USART2_IRQHandler ( void )
extern
Returns
None

Self-explanatory code, nothing to elaborate.

Definition at line 362 of file hierodule_usart.c.

References USART2_Wrapper, and USART_IRQHandler().

◆ USART3_IRQHandler()

void USART3_IRQHandler ( void )
extern
Returns
None

Self-explanatory code, nothing to elaborate.

Definition at line 375 of file hierodule_usart.c.

References USART3_Wrapper, and USART_IRQHandler().

◆ USART6_IRQHandler()

void USART6_IRQHandler ( void )
extern
Returns
None

Self-explanatory code, nothing to elaborate.

Definition at line 388 of file hierodule_usart.c.

References USART6_Wrapper, and USART_IRQHandler().