2022-11-11 05:21 AM
When I run this code, the value stored in colourBuf[0] changes after the for loop at line 73 completes. The colour0 value doesn't change. Debugging in STM32 Cube IDE.
/*
* LED_control.c - Version 2.
*
* Created on: 21 June 2022
* Author: jm2988
*/
#include "LED_control.h"
#include "stm32f7xx_hal.h"
extern TIM_HandleTypeDef htim1;
extern DMA_HandleTypeDef hdma_tim1_ch1;
/* BEGIN DEFINITIONS */
#define nPixels (4) //number of neopixels
#define nColours (3) //colours per pixel
#define nBits (8) //bits per colour
#define resetSize (50) //n low bits for reset (1.25uS per low bit approx).
#define pixelBits (nColours*nBits)
#define totalBits (nPixels*nColours*nBits) //total number of bits for the pixels
#define bufSize (totalBits+resetSize)
//buffer should be: [(nPixels*nColours*nBits)+resetSize]
uint16_t pwmData[bufSize] = {0};
uint32_t colourBuf[nPixels];
int datasentflag=0;
/* END DEFINITIONS */
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
//printf("PFC \r\n");
HAL_TIM_PWM_Stop_DMA(htim, TIM_CHANNEL_1);
//zero the buffer
for (int i=bufSize; i>=0; i--)
{
pwmData[i] = 0;
}
datasentflag = 1;
}
/*
void HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef *htim)
{
printf("HPFC \r\n");
HAL_TIM_PWM_Stop_DMA(htim, TIM_CHANNEL_1);
datasentflag = 1;
}
*/
void WS2812_Send (uint32_t colour0, uint32_t colour1, uint32_t colour2, uint32_t colour3)
{
//fill the colour buffer with the various LED colours - needs better approach!
colourBuf[0] = colour0;
colourBuf[1] = colour1;
colourBuf[2] = colour2;
colourBuf[3] = colour3;
//Fill the last 50 bits with 0's (for reset)
//for (int i=bufSize; i>(pixelBits-1); i--) //old code - working 1 LED
for (int i=bufSize; i>(totalBits-1); i--)
{
pwmData[i] = 0;
}
/* ************ New Code - multiple LEDs ********** */
//fill the next 24 bits with PWM data
for (int i=(nPixels-1); i>=0; i--)
{
for (int p=((pixelBits*(i+1))-1); p>=(pixelBits*i); p--){
if (colourBuf[i]&(1<<(p-(pixelBits*i)))) //move the pointer to each position, check if value is 1.
{
pwmData[p] = 80; //this value divided by ARR =0.67 (67%)
}
else pwmData[p] = 38; //this value divided by ARR =0.32 (32%)
}
}
HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t *)pwmData, bufSize);
//HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t *)pwmData, 24);
//while (!datasentflag){}; //while datasentflag = 0, loop nothing.
//datasentflag = 0;
}
What actually changes? Well, colour0 is hex DBDBDB, and interestingly the colourBuf[0] value changes to hex DB0000.
When I run this code extract just as a standalone C program in Visual Studio (Providing fixed 'colour' values), it runs as-expected.
Please be kind in your responses. I'm still learning the STM32 Cube IDE. If I can do anything to provide more information, I'd be very happy to do so. :)
Solved! Go to Solution.
2022-11-11 05:37 AM
> for (int i=bufSize; i>(totalBits-1); i--)
> {
> pwmData[i] = 0;
> }
Buffer overrun. The last index in pwmData is bufSize - 1, not bufSize.
Start at int i = bufSize - 1 instead.
2022-11-11 05:37 AM
> for (int i=bufSize; i>(totalBits-1); i--)
> {
> pwmData[i] = 0;
> }
Buffer overrun. The last index in pwmData is bufSize - 1, not bufSize.
Start at int i = bufSize - 1 instead.
2022-11-11 05:43 AM
Oh my word, can't believe I didn't notice that. :face_with_tears_of_joy:
Thanks so much! :beaming_face_with_smiling_eyes: