MetalPhreak / ESP8266_SPI_Driver

Driver code to interface with ESP8266 built-in hardware SPI functions.
172 stars 58 forks source link

Support gpio CS? #2

Open kanflo opened 9 years ago

kanflo commented 9 years ago

Your driver seems like a nice fit for my project but I will have more than one SPI device leading to the need for GPIO chip selects. Of course the pin toggling could be made outside of the SPI driver but imho it would be nicer to have it inside the driver as it belongs to "the SPI domain". What do you think?

MetalPhreak commented 9 years ago

It's an idea I've thought about a while back when writing this, but haven't gotten around to implementing it. If I get some time over the next couple of weeks I'll take a look at doing it. I'm sure I'll need to do it at some point in the future for one of my projects anyway! :)

elmorejd commented 8 years ago

I second this! Is there a register for setting which GPIO is used for CS?

kanflo commented 8 years ago

I removed the line PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2); from spi_init_gpio(...) and added toggling of my chosen GPIO CS manually:

gpio_write(cs_pin, 0);
spi_tx16(iHSPI, data[0] << 8 | data[1]);
gpio_write(cs_pin, 1);

The hardware SPI block can only use GPIO15 as CS. Note that in SPI the CS signal is active when low.

elmorejd commented 8 years ago

Great that works pretty well. Thanks.

KaDw commented 7 years ago

@kanflo @elmorejd You had any problems with software CS? I'm trying to use GPIO4 as CS.

void max7219_write(uint8 reg, uint8 value){
    uint16_t data = (reg << 8) | value;
    GPIO_OUTPUT_SET(4, 0); // cs low
    spi_tx16(HSPI, data);
    GPIO_OUTPUT_SET(4, 1); // cs high
}

This is what I get with the code above: (only CS and CLK lines)

spi

I'm a bit lucky because first 4 bits of data for MAX7219 doesn't matter so this is working. Also last transaction is skipped. If I switch CS to hardware mode everything works like a charm. Unfortunately I have boards manufactered and CS is wired to GPIO4 Any ideas what could go wrong? I know it's a bit old issue :P

MetalPhreak commented 7 years ago

You need to wait for the hardware spi buffer to write out before bring cs pin low again. Add something after spi_tx16 to check the status. Spi_tx16 is non blocking.

Sent from my iPhone

On 8 Oct 2017, at 9:48 pm, Karol notifications@github.com wrote:

@kanflo @elmorejd You had any problems with software CS? I'm trying to use GPIO4 as CS.

void max7219_write(uint8 reg, uint8 value){ uint16_t data = (reg << 8) | value; GPIO_OUTPUT_SET(4, 0); // cs low spi_tx16(HSPI, data); GPIO_OUTPUT_SET(4, 1); // cs high } This is what I get with the code above: (only CS and CLK lines)

I'm a bit lucky because first 4 bits of data for MAX7219 doesn't matter so this is working. Also last transaction is skipped. If I switch CS to hardware mode everything works like a charm. Unfortunately I have boards manufactered and CS is wired to GPIO4 Any ideas what could go wrong? I know it's a bit old issue :P

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

KaDw commented 7 years ago

Thanks for very fast reply! I'm checking if spi is busy like you said and now everything is OK

Working code:

void max7219_write(uint8 reg, uint8 value){
    uint16_t data = (reg << 8) | value;
    GPIO_OUTPUT_SET(4, 0); // cs low
    spi_tx16(HSPI, data);
    while(spi_busy(HSPI)); // ADDED
    GPIO_OUTPUT_SET(4, 1); // cs high
}