|
Hierodule 1.6.2
Utility module set for STM32 MCUs
|
The module basically lets you
Also provided are routines to
HIERODULE_USART_Wrapper is the main item in the module, with which you can access the variables related to the ring buffer and use the module routines to implement your RXNE ISR.
The module does not address peripheral configuration and initialization, it is assumed those are performed beforehand. Make sure to have configured the baudrate and GPIO modes of the pins for your peripheral.
Also notice that the USART IRQs are defined in the module's source file. The compiler will throw a multiple definition error if an IRQ is defined somewhere else.
To start using the module, first thing you need is a double pointer to HIERODULE_USART_Wrapper to create an instance for the USART peripheral.
It has to be a double pointer, since the wrapper initializer passes you the wrapper pointer defined in the module by reference. The wrapper instance is handled in the module with a pointer and not with a variable of type HIERODULE_USART_Wrapper, since you use pointers for dynamic memory allocation.
Secondly, you need a void function with a uint8_t parameter for the RXNE ISR. You might as well just pass NULL if you intend to use the received data somewhere else. Or you can even maintain an array for the function pointers (i.e. void (*)(uint8_t)) to switch between ISRs on the fly.
Here's a simple example that assigns a task to the ISR that sums up the bytes received at USART1 and initializes the wrapper for USART1 with a ring buffer that's 12 bytes long.
Once initialized, the wrapper may be used to transmit data.
You need to enable the RE and RXNEIE bits at the control register to enable the USART IRQ and start receiving data. Simply call:
You can also disable the receive control bits via:
You can "release" the instance of your wrapper to free memory:
Keep in mind this only frees the wrapper pointer in the module and not your double pointer that points to it. It's best to free and nullify that, as well:
Notice that trying to access a freed memory address might turn out ugly for your run-time. However, you can reuse your double pointer to re-initialize the USART peripheral after releasing it.