I have to connect a TLC5947 with SPI and can't quite get it to work right on the ESP8266. My first attempt was bit banging which works fine at low frequencies but as soon as it gets a little too high the ESP8266 crashes. I think that's because the loop doesn't give its internals enough time for all the other stuff it has to do. Here is the bit banging code:
void init() {
system_timer_reinit();
os_timer_setfn(&myTimer, timer_callback, NULL);
ets_timer_arm_new(&myTimer, 25, true, 0);
pinMode(LATCH_WIRE, OUTPUT);
pinMode(DATA_WIRE, OUTPUT);
pinMode(CLOCK_WIRE, OUTPUT);
}
void timer_callback(void *pArg)
{
// some data calculations...
uint8_t data = ...;
digitalWrite(LATCH_WIRE, 1);
// send bits 7..0
for (uint8_t i = 0; i < 8; i++)
{
// consider leftmost bit
// set line high if bit is 1, low if bit is 0
if (data & 0b10000000)
digitalWrite(DATA_WIRE, 1);
else
digitalWrite(DATA_WIRE, 0);
// pulse clock to indicate that bit value should be read
digitalWrite(CLOCK_WIRE, 0);
digitalWrite(CLOCK_WIRE, 1);
// shift byte left so next bit will be leftmost
data <<= 1;
}
digitalWrite(LATCH_WIRE, 0);
}
Then I tried this example from espressif: http://bbs.espressif.com/viewtopic.php?t=85
everything compiles fine but the LEDs don't light up the way I expect them to so there must be some error while transmitting the data. Here is the relevant part of the code I am running:
void init() {
system_timer_reinit();
os_timer_setfn(&myTimer, timer_callback, NULL);
ets_timer_arm_new(&myTimer, 25, true, 0);
spi_master_init(HSPI);
pinMode(LATCH_WIRE, OUTPUT);
}
void timer_callback(void *pArg)
{
// some data calculations...
uint8_t data = ...;
digitalWrite(LATCH_WIRE, 1);
spi_mast_byte_write(HSPI, data);
digitalWrite(LATCH_WIRE, 0);
}
spi_mast_byte_write should transmit one byte (data) with HSPI but it just doesn't seem to do so. I defined the pins for both bit banging and HSPI like this:
#define LATCH_WIRE 12
#define DATA_WIRE 13
#define CLOCK_WIRE 14
Can you help me there and find my mistake? Maybe I should use something else for handling SPI? I have been researching for days now and couldn't find a way to make it work without crashing the ESP8266. Thanks in advance!