C source code

From OpenMBD
Jump to: navigation, search

The C source code is written in an implementation file and in a header file.
The files must be placed in a folder as follows:

  • .\blockset\rtw\STM32\inc\Olimexino_STM32\ws2812b.h
  • .\blockset\rtw\STM32\src\Olimexino_STM32\ws2812b.c

The timing required for the WS2812B LEDs is generated by the SPI interface in master mode and using the MOSI pin only.
The C code exports three functions, which are briefly described below.

void ws2812b_init(uint8_t channel)

This function is used to initialize the target hardware. It should be called once during system setup.
The parameter channel is used to select the SPI bus and the corresponding MOSI pin:
channel = 0: SPI1 / MOSI pin PA7 (D11)
channel = 1: SPI2 / MOSI pin PB15 (D34)

void ws2812b_write(uint32_t layer0_grb, uint32_t layer1_grb, uint32_t layer2_grb, uint32_t layer3_rgb)

This function takes a parameter for each layer. Each parameter sets the values for R, G and B between 0 and 255.
The parameters are of type uint32_t and RGB colors are coded as follows:
0x00GGRRBB
This particular byte order was chosen, because the WS2812B LEDs require a specific bit order (MSB of green first, LSB of blue last)
This function should be called whenever an update of any of the LEDs is required.

Some important notes

  • It is not possible to use timer interrupts, because timers are already in use by other blocks.
  • The generation of the serial signal for the WS2812B is time critical. Instead of using a timer, direct memory access (DMA) has been implemented for transmitting the serial data.

void ws2812b_reset(void)

This is a convenience function that sets all LEDs off.


Next - Part 2: Matlab/Simulink files