Urbanite: Parking aid system
|
This file implements port layer for the system functions in the STM32F4 platform. More...
Macros | |
#define | HSI_VALUE ((uint32_t)16000000) |
#define | RCC_HSI_CALIBRATION_DEFAULT 0x10U |
#define | TICK_FREQ_1KHZ 1U |
#define | NVIC_PRIORITY_GROUP_0 ((uint32_t)0x00000007) |
#define | NVIC_PRIORITY_GROUP_4 ((uint32_t)0x00000003) |
#define | POWER_REGULATOR_VOLTAGE_SCALE3 0x01 |
Functions | |
static void | system_clock_config (void) |
System Clock Configuration. More... | |
void | SystemInit (void) |
Setup the microcontroller system Initialize the FPU setting, vector table location and External memory configuration. More... | |
uint32_t | port_system_init () |
Initializes the system. | |
void | port_system_delay_ms (uint32_t ms) |
Delays the program execution for the specified number of milliseconds. More... | |
void | port_system_delay_until_ms (uint32_t *p_t, uint32_t ms) |
Delays the program execution until the specified number of milliseconds since the system started. More... | |
uint32_t | port_system_get_millis () |
Returns the number of milliseconds since the system started. More... | |
void | port_system_set_millis (uint32_t ms) |
Sets the number of milliseconds since the system started. More... | |
void | stm32f4_system_gpio_config (GPIO_TypeDef *p_port, uint8_t pin, uint8_t mode, uint8_t pupd) |
Configure the mode and pull of a GPIO. More... | |
void | stm32f4_system_gpio_config_exti (GPIO_TypeDef *p_port, uint8_t pin, uint32_t mode) |
Configure the external interruption or event of a GPIO. More... | |
void | stm32f4_system_gpio_exti_enable (uint8_t pin, uint8_t priority, uint8_t subpriority) |
Enable interrupts of a GPIO line (pin) More... | |
void | stm32f4_system_gpio_exti_disable (uint8_t pin) |
Disable interrupts of a GPIO line (pin) More... | |
bool | stm32f4_system_gpio_read (GPIO_TypeDef *p_port, uint8_t pin) |
Read the digital value of a GPIO. More... | |
void | stm32f4_system_gpio_write (GPIO_TypeDef *p_port, uint8_t pin, bool value) |
Write a digital value in a GPIO atomically. More... | |
void | stm32f4_system_gpio_toggle (GPIO_TypeDef *p_port, uint8_t pin) |
Toggle the value of a GPIO. More... | |
void | stm32f4_system_gpio_config_alternate (GPIO_TypeDef *p_port, uint8_t pin, uint8_t alternate) |
Configure the alternate function of a GPIO. More... | |
void | port_system_systick_resume () |
Resume Tick increment. More... | |
void | port_system_systick_suspend () |
Suspend Tick increment. More... | |
void | port_system_power_stop () |
Set the system in stop mode for low power consumption. More... | |
void | port_system_power_sleep () |
Set the system in sleep mode for low power consumption. More... | |
void | port_system_sleep () |
Enable low power consumption in sleep mode. More... | |
Variables | |
static volatile uint32_t | msTicks = 0 |
uint32_t | SystemCoreClock = HSI_VALUE |
const uint8_t | AHBPrescTable [16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9} |
const uint8_t | APBPrescTable [8] = {0, 0, 0, 0, 1, 2, 3, 4} |
This file implements port layer for the system functions in the STM32F4 platform.
#define HSI_VALUE ((uint32_t)16000000) |
Value of the Internal oscillator in Hz
#define NVIC_PRIORITY_GROUP_0 ((uint32_t)0x00000007) |
0 bit for pre-emption priority, \ 4 bits for subpriority
#define NVIC_PRIORITY_GROUP_4 ((uint32_t)0x00000003) |
4 bits for pre-emption priority, \ 0 bit for subpriority
#define POWER_REGULATOR_VOLTAGE_SCALE3 0x01 |
Scale 3 mode: the maximum value of fHCLK is 120 MHz.
#define RCC_HSI_CALIBRATION_DEFAULT 0x10U |
Default HSI calibration trimming value
#define TICK_FREQ_1KHZ 1U |
Freqency in kHz of the System tick
void port_system_delay_ms | ( | uint32_t | ms | ) |
Delays the program execution for the specified number of milliseconds.
ms | Number of milliseconds to delay. |
void port_system_delay_until_ms | ( | uint32_t * | t, |
uint32_t | ms | ||
) |
Delays the program execution until the specified number of milliseconds since the system started.
t | Pointer to the variable that stores the number of milliseconds to delay until. |
ms | Number of milliseconds to delay until. |
uint32_t port_system_get_millis | ( | void | ) |
Returns the number of milliseconds since the system started.
number | of milliseconds since the system started. |
void port_system_power_sleep | ( | void | ) |
Set the system in sleep mode for low power consumption.
The function sets the state of the power regulator in sleep mode.
After that, the function sets the system mode in sleep mode, to enter in sleep mode when calling __WFI()
(wait for interruption).
The system remains in this line until an external interruption or timer interruption occurs.
ONLY IF YOU NEED IT! TODO alumnos Version 4:
✅ 1. Copy the following code in the file stm32f4_system.c.
void port_system_power_stop | ( | void | ) |
Set the system in stop mode for low power consumption.
The function sets the state of the power regulator in stop mode.
After that, the function sets the system mode in sleep mode, to enter in stop mode when calling __WFI()
(wait for interruption).
The system remains in this line until an external interruption or timer interruption occurs.
If an interruption occurs the system wakes up and resets the system mode.
TODO alumnos Version 4:
✅ 1. Copy the following code in the file stm32f4_system.c.
void port_system_set_millis | ( | uint32_t | ms | ) |
Sets the number of milliseconds since the system started.
ms | New number of milliseconds since the system started. |
void port_system_sleep | ( | void | ) |
Enable low power consumption in sleep mode.
TODO alumnos Version 4:
✅ 1. Call function
port_system_systick_suspend()
to suspend the count of the SysTick.
✅ 2. Call functionport_system_power_sleep()
to enter sleep in sleep mode.
void port_system_systick_resume | ( | void | ) |
Resume Tick increment.
The SysTick timer is the source of time base. It is used to generate interrupts at regular time intervals. Once this function is called, the SysTick interrupt will be enabled and so Tick increment is resumed.
TODO alumnos Version 4:
✅ 1. Copy the following code in the file stm32f4_system.c.
void port_system_systick_suspend | ( | void | ) |
Suspend Tick increment.
The SysTick timer is the source of time base. It is used to generate interrupts at regular time intervals. Once this function is called, the SysTick interrupt will be disabled so it saves more energy and it does not generate any interruption.
TODO alumnos Version 4:
✅ 1. Copy the following code in the file stm32f4_system.c.
void stm32f4_system_gpio_config | ( | GPIO_TypeDef * | p_port, |
uint8_t | pin, | ||
uint8_t | mode, | ||
uint8_t | pupd | ||
) |
Configure the mode and pull of a GPIO.
============================================================================== ##### How to use GPIOs ##### ============================================================================== [..] (#) Enable the GPIO AHB clock using the RCC->AHB1ENR register. (#) Configure the GPIO pin. (++) Configure the IO mode. (++) Activate Pull-up, Pull-down resistor. (++) In case of Output or alternate function mode, configure the speed if needed. (++) Configure digital or analog mode. (++) In case of external interrupt/event select the type (interrupt or event) and the corresponding trigger event (rising or falling or both). (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority mapped to the EXTI line and enable it using. (#) To get the level of a pin configured in input mode use the GPIOx_IDR register. (#) To set/reset the level of a pin configured in output mode use the GPIOx_BSRR register to SET (bits 0..15) or RESET (bits 16..31) the GPIO.
- Enable GPIOx clock in AHB1ENR
- Set mode in MODER
- Set pull up/down configuration
p_port | Port of the GPIO (CMSIS struct like) |
pin | Pin/line of the GPIO (index from 0 to 15) |
mode | Input, output, alternate, or analog |
pupd | Pull-up, pull-down, or no-pull |
None |
void stm32f4_system_gpio_config_alternate | ( | GPIO_TypeDef * | p_port, |
uint8_t | pin, | ||
uint8_t | alternate | ||
) |
Configure the alternate function of a GPIO.
- Create a 4-bit base mask.
- Shift left the mask depending on the value of the given **
pin
modulo 8.**
💡 The value ofpin
ranges from 0 to 15. The registers GPIOx_AFRH and GPIOx_AFRL implement 8 groups of 4 bits each. In order to use the value ofpin
as index to select the corresponding group of bits, we can use the remainder of the division by 8.
- Clean and set the bits as shown in the tutorial document.
💡 Clean the corresponding bit on element0
or1
of the AFR array (e.g,GPIOA->AFR[0]
for GPIOx_AFRL)
💡 Set the given value (alternate
) of the alternate function, using bit shifting, for example.
💡 You can define your own masks for each alternate function (not recommended), or you can use the macro
BASE_MASK_TO_POS(m, p)
to get the mask of a base mask. Example:
A base maskm
equals0x03
(0b 0000 0011
in binary) can be shiftedp
equals8
positionsBASE_MASK_TO_POS(0x03, 8)
resulting in0x300
(0b 0011 0000 0000
in binary).
p_port | Port of the GPIO (CMSIS struct like) |
pin | Pin/line of the GPIO (index from 0 to 15) |
alternate | Alternate function number (values from 0 to 15) according to table of the datasheet: "Table 11. Alternate function". |
None |
void stm32f4_system_gpio_config_exti | ( | GPIO_TypeDef * | p_port, |
uint8_t | pin, | ||
uint32_t | mode | ||
) |
Configure the external interruption or event of a GPIO.
- Enable the System configuration controller clock (SYSCFG). Enable the SYSCFG by setting the bit SYSCFGEN of the peripheral clock enable register (RCC_APB2ENR). The system configuration controller is used here to manage the external interrupt line connection to the GPIOs.
💡 As usual, you can access to the register (APB2ENR
) as element of the structureRCC
. You can use the macroRCC_APB2ENR_SYSCFGEN
defined instm32f446xx.h
to set the bit. Look for the "RCC_APB2ENR" register in the Reference Manual if you need more information.
- Associate the external interruption line to the given port. Clean and set the bits as shown in the tutorial document.
💡 Depending on the pin number, use the register SYSCFG_EXTICR1, SYSCFG_EXTICR2, SYSCFG_EXTICR3, or SYSCFG_EXTICR4. The structureSYSCFG
contains a 4-element array calledEXTICR
; the first element (EXTICR[0]
) configures the register SYSCFG_EXTICR1, and so on.
💡 To clean the EXTIx bits, you can create a mask depending on thepin
value.
💡 To associate the external interruption to the given port, i.e. to set the EXTIx bits, you can create another mask depending on theport
value.
- Select the direction of the trigger: rising edge, falling edge, or both, depending on the value of the given
mode
.
💡 If rising edge: activate the corresponding bit on the EXTI_RTSR register (elementRTSR
) of theEXTI
structure.
💡 If falling edge: activate the corresponding bit on the EXTI_FTSR register (elementFTSR
) of theEXTI
structure.
💡 If both: activate the corresponding bit on both registers.
- Select the interrupt and/or event request: depending on the value of the given
mode
.
💡 If event request enable: activate the corresponding bit on the EXTI_EMR register (elementEMR
) of theEXTI
structure.
💡 If interrupt request enable: activate the corresponding bit on the EXTI_IMR register (elementIMR
) of theEXTI
structure.
💡 You can define your own masks for each pin value (not recommended), or you can use the
BIT_POS_TO_MASK(pin)
macro to get the mask of a pin.
RSTR
, FTSR
, EMR
, IMR
) before activating it.p_port | Port of the GPIO (CMSIS struct like) |
pin | Pin/line of the GPIO (index from 0 to 15) |
mode | Trigger mode can be a combination (OR) of: (i) direction: rising edge (0x01), falling edge (0x02), (ii) event request (0x04), or (iii) interrupt request (0x08). |
None |
void stm32f4_system_gpio_exti_disable | ( | uint8_t | pin | ) |
Disable interrupts of a GPIO line (pin)
pin | Pin/line of the GPIO (index from 0 to 15) |
None |
void stm32f4_system_gpio_exti_enable | ( | uint8_t | pin, |
uint8_t | priority, | ||
uint8_t | subpriority | ||
) |
Enable interrupts of a GPIO line (pin)
pin | Pin/line of the GPIO (index from 0 to 15) |
priority | Priority level (from highest priority: 0, to lowest priority: 15) |
subpriority | Subpriority level (from highest priority: 0, to lowest priority: 15) |
None |
bool stm32f4_system_gpio_read | ( | GPIO_TypeDef * | p_port, |
uint8_t | pin | ||
) |
Read the digital value of a GPIO.
TODO alumnos:
✅ 1. Retrieve the value of the IDR register.
💡 You must cast the read value to return abool
.
💡 You might use theBIT_POS_TO_MASK(pin)
macro.
✅ 2. Return the value.
p_port | Port of the GPIO (CMSIS struct like) |
pin | Pin/line of the GPIO (index from 0 to 15) |
true
if the GPIO was HIGH false
if the GPIO was LOW void stm32f4_system_gpio_toggle | ( | GPIO_TypeDef * | p_port, |
uint8_t | pin | ||
) |
Toggle the value of a GPIO.
TODO alumnos:
✅ 1. Read the value of the GPIO.
✅ 2. Write the opposite value in the GPIO.
💡 You might use functionsstm32f4_system_gpio_read()
andstm32f4_system_gpio_write()
to help you.
💡 You might use the macrosHIGH
andLOW
.
p_port | Port of the GPIO (CMSIS struct like) |
pin | Pin/line of the GPIO (index from 0 to 15) |
None |
void stm32f4_system_gpio_write | ( | GPIO_TypeDef * | p_port, |
uint8_t | pin, | ||
bool | value | ||
) |
Write a digital value in a GPIO atomically.
TODO alumnos:
✅ 1. Set the corresponding bit value of the BSRR register to set or reset the output depending on the given
value
.
💡 You might use the macrosHIGH
andLOW
.
💡 You might use theBIT_POS_TO_MASK(pin)
macro.
BIT_POS_TO_MASK(pin)
macro to get the mask when you go to clear a GPIO. Otherwise, you can calculate the pin mask first and then use a 16-position left shift of the mask.p_port | Port of the GPIO (CMSIS struct like) |
pin | Pin/line of the GPIO (index from 0 to 15) |
value | Boolean value to set the GPIO to HIGH (1, true ) or LOW (0, false ) |
None |
|
static |
System Clock Configuration.
Configure the main internal regulator output voltage
void SystemInit | ( | void | ) |
Setup the microcontroller system Initialize the FPU setting, vector table location and External memory configuration.
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9} |
Prescaler values for AHB bus
const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4} |
Prescaler values for APB bus
|
static |
Variable to store millisecond ticks.
uint32_t SystemCoreClock = HSI_VALUE |
Frequency of the System clock