Simone: a game of visual memory and speed
stm32f4_rgb_light.c File Reference

Portable functions to interact with the RGB light system FSM library. All portable functions must be implemented in this file. More...

#include <stdio.h>
#include "port_rgb_light.h"
#include "port_system.h"
#include "stm32f4_system.h"
#include "stm32f4_rgb_light.h"

Functions

stm32f4_rgb_light_hw_t_stm32f4_rgb_light_get (uint8_t rgb_light_id)
 Get the RGB light struct with the given ID. More...
 
void _timer_pwm_config (uint8_t rgb_light_id)
 Configure the timer that controls the PWM of each one of the RGB LEDs of the RGB light system. More...
 
void port_rgb_light_init (uint8_t rgb_light_id)
 Configure the HW specifications of a given RGB light. More...
 
void port_rgb_light_set_rgb (uint8_t rgb_light_id, rgb_color_t color)
 Set the Capture/Compare register values for each channel of the RGB LED given a color. More...
 

Variables

stm32f4_rgb_light_hw_t rgb_lights_arr []
 Array of elements that represents the HW characteristics of the RGB LED of the RGB light systems connected to the STM32F4 platform. More...
 

Detailed Description

Portable functions to interact with the RGB light system FSM library. All portable functions must be implemented in this file.

Author
Sistemas Digitales II
Date
2026-01-01

Function Documentation

◆ _stm32f4_rgb_light_get()

stm32f4_rgb_light_hw_t* _stm32f4_rgb_light_get ( uint8_t  rgb_light_id)

Get the RGB light struct with the given ID.

Parameters
rgb_light_idButton ID.
Returns
Pointer to the RGB light struct.
NULL If the RGB light ID is not valid.

◆ _timer_pwm_config()

void _timer_pwm_config ( uint8_t  rgb_light_id)

Configure the timer that controls the PWM of each one of the RGB LEDs of the RGB light system.

This function is called by the port_rgb_light_init() public function to configure the timer that controls the PWM of the RGB LEDs of the RGB light.

Warning
The timer is common for all the RGB LEDs of one RGB RGB light despite each one has its own channel. If you introduce more RGB rgb_lights, you must configure a new timer for each one. Because of this, you **must encapsulate the configuration of the timer in a switch or if statement.*
Note
This function does not set the duty cycle of the PWM signal. This is done in the port_rgb_light_set_rgb() function where the color is set.

TODO alumnos:

PWM timer setup

✅ 1. Enable the clock source of the timer
    💡 To know if the source clock is APB1 or APB2, check the clock tree in the user manual (Figure 3. STM32F446xC/E block diagram)
✅ 2. Disable the counter (register CR1) and enable the autoreload preaload (bit ARPE) ✅ 3. Reset the counter (register CNT), set the autoreload value (register ARR) and the prescaler (register PSC) for a frequency of 50 Hz.
PWM mode configuration

Note
Check table "Table 11. Alternate function" in the datasheet to select the channel more alternate functions.

✅ 5. Disable the output compare (register CCER) for each one of the corresponding channels. Take into account the channel number (1, 2, 3, or 4) and the channel enable bit (CCxE)
✅ 6. Clear the P and NP bits (CCxP and CCxNP) of the output compare register (CCER) for each one of the corresponding channels .
✅ 7. Set both (i) mode PWM 1, and (ii) enable preload (register CCMRx) for each one of the corresponding channels.
    💡 To set the mode PWM to mode 1 you must set the bits OCxM in the CCMRx register to the corresponding values according to the explanations of the manufacturer. You can find it in the explanations for the TIMx_CCMRx register for timers TIM2-TIM5 in Sections 17.4.7 and 17.4.8 in the reference manual
    💡 To enable the preload you must set the bit OCxPE to 1 in the same register. ✅ 8. Generate an update event (register EGR) by setting the UG bit. This will load the values of the ARR and PSC registers into the active registers.

Parameters
rgb_light_idRGB light system identifier number.

◆ port_rgb_light_init()

void port_rgb_light_init ( uint8_t  rgb_light_id)

Configure the HW specifications of a given RGB light.

Assuming we are using an STM32F4-based platform, this function must call the following functions:

TODO alumnos:

✅ 1. Retrieve the RGB light configuration struct calling _stm32f4_rgb_light_get().
✅ 2. Call function stm32f4_system_gpio_config() with the right arguments to configure each RGB LED as in alternate mode and no pull up neither pull down connection.
✅ 3. Call function stm32f4_system_gpio_config_alternate() with the right arguments to configure the alternate function of the each RGB LED.
✅ 4. Call function _timer_pwm_config() to configure the timer and the PWM signal of the RGB light.
✅ 5. Call function port_rgb_light_set_rgb() to set the RGB LED to off.

Parameters
rgb_light_idRGB light ID. This index is used to select the element of the rgb_lights_arr[] array

◆ port_rgb_light_set_rgb()

void port_rgb_light_set_rgb ( uint8_t  rgb_light_id,
rgb_color_t  color 
)

Set the Capture/Compare register values for each channel of the RGB LED given a color.

This function disables the timer associated to the RGB LEDs, sets the Capture/Compare register values for each channel of the RGB LED, and enables the timer.

Warning
This function is valid for any given RGB LED, however, each RGB LED has its own timer. Write the code in a conditional statement to check the rgb_light_id.

TODO alumnos:

✅ 1. Retrieve the individual RGB values from the color parameter and follow the flowchart given in the lab notebook to set the RGB LED color by configuring the PWM signals appropriately.

Parameters
rgb_light_idRGB light system identifier number.
colorRGB color to set.

Variable Documentation

◆ rgb_lights_arr

stm32f4_rgb_light_hw_t rgb_lights_arr[]

Array of elements that represents the HW characteristics of the RGB LED of the RGB light systems connected to the STM32F4 platform.

  • This is an extern variable that is declared in stm32f4_rgb_light.h. It represents an array of RGB lights.