WS2812 library does not work on STM32F40
Hello,
I've been trying to find a solution on the forums for several hours now, to no avail!
I have just put my programs on STM32F401, I compile with MBED Studio. I used to light my WS2812 LEDs with a KL25Z (NXP) always using Mbed Studio and my library worked perfectly!
But with the STM32, I couldn't get the LEDs to work, so I did some tests on different pins, with no success.
that the signal wasn't square...
I concluded that the code was wrong. After several searches I see that you have to activate certain functions to use the WS2812 with STM32 but I don't understand what's missing from my code.
Can you please help me?
Thank you in advance,
Sincerely
Antoine
Here is the program:
WS2812.h
#ifndef MBED_WS2812_H
#define MBED_WS2812_H
#include "mbed.h"
class WS2812
{
private:
void int_to_binary(int value, int* buffer, int bufferSize);
DigitalOut Data;
int nLed;
public:
void WriteDataOut(int dataLed[]);
WS2812(PinName _data, int _nLed);
};
#endifWS2812.cpp
#include <mbed.h>
#include "WS2812.h"
WS2812::WS2812(PinName _data, int _nLed):
Data(_data),
nLed(_nLed)
{}
void WS2812::int_to_binary(int value, int* buffer, int bufferSize)
{
int *nextChar = buffer + bufferSize-1;//................location to write the least significant bit
for (int i = 0; i<(bufferSize); i++)
{ // for each bit
if(value & (1<<i)) {*nextChar = 1;} else{*nextChar = 0;} // if set set to '1' else '0'
nextChar --;
}
}
void WS2812::WriteDataOut(int dataLed[])
{
const int nBit8 = 8,
nBytePerLed = 3,
nBit = nLed * (nBytePerLed * nBit8);
int data[nBit+1];
int buff[nBit8];
for(int x = 0; x < nLed; x ++)
{
int_to_binary(dataLed[x * nBytePerLed], buff, nBit8);
for(int bit = 0; bit < 8; bit ++){data[bit + (x * (nBit8 * nBytePerLed))] = buff[bit];}
int_to_binary(dataLed[(x * nBytePerLed)+1], buff, nBit8);
for(int bit = 0; bit < 8; bit ++){data[bit + (x * (nBit8 * nBytePerLed)) + nBit8] = buff[bit];}
int_to_binary(dataLed[(x * nBytePerLed)+2], buff, nBit8);
for(int bit = 0; bit < 8; bit ++){data[bit + (x * (nBit8 * nBytePerLed)) + (2 * nBit8)] = buff[bit];}
}
__disable_irq();
for (int i = 0; i < (nLed * (nBytePerLed * nBit8)); i++)
{
int j = 0;
if (data[i])
{
Data = 1;
for (; j < 5; j++) {__nop();}
Data = 0;
for (; j < 0; j++) {__nop();}
}
else
{
Data = 1;
for (; j < 0; j++) {__nop();}
Data = 0;
for (; j < 5; j++) {__nop();}
}
}
__enable_irq();
}
Main.cpp
#include "mbed.h"
#include "WS2812.h"
DigitalIn bpUser(PA_10);
WS2812 WSLed(PA_14, 2);
int buff[6] = {50, 100, 150, 200, 127, 255};
int main()
{
WSLed.WriteDataOut(buff);
while(1) {
if (bpUser == 0)
{
}
}
