- Sat Apr 18, 2015 9:24 am
#14906
Major update to the code.
Implemented one single "SPI Transaction" function to handle all combinations of CMD/ADDR/MISO/MOSI/DUMMY.
Now has the ability to both Read and Write up to 32bits of data (plus 16bits of Command data + 32bits of address data!)
Dummy bits don't seem too useful. Some more info here:
http://d.av.id.au/blog/esp8266-spi-dummy-bits/Now, you aren't going to want to write out the massive spi_transaction command and it's variables every time. I have provided a number of macro functions for simple read/write operations of 8/16/32 bits or an arbitrary number of bits up to 32. These are in spi.h and are fairly self explanatory.
Writing Data:
Code: Select alluint8 byte = 0xEF;
uint16 word = 0xBEEF;
uint32 dword = 0xDEADBEEF;
uint16 9bit = 0b101010101;
spi_tx8(HSPI, byte);
spi_tx16(HSPI, word);
spi_tx32(HSPI, dword);
spi_txd(HSPI, 9, 9bit);
Reading Data:
Code: Select alluint8 byte;
uint16 word;
uint32 dword;
uint16 9bit;
byte = (uint8) spi_rx8(HSPI); //returned value is uint32. Cast to uint8
word = (uint16) spi_rx16(HSPI); //returned value is uint32. Cast to uint16
dword = spi_rx32(HSPI); //No type casting needed
9bit = (uint16) spi_rxd(HSPI, 9); //returned value is uint32. Cast to uint16
The function always returns a 32bit unsigned integer. You should probably cast this when assigning to your own variable when it's returned.
You can define your own custom macros. I have the following two for reading/writing data to an SPI EEPROM:
Code: Select all#define eeprom_write(addr, data) spi_transaction(HSPI, 3, 0b101, 9, addr, 8, data, 0, 0)
#define eeprom_read(addr) spi_transaction(HSPI, 3, 0b110, 9, addr, 0, 0, 8, 0)
Enjoy. Let me know if there's any bugs. I haven't tested every single combinations of bit lengths etc.
Best to make sure SPI_WR_BYTE_ORDER and SPI_RD_BYTE_ORDER are set in the SPI_USER register. This is already done for you in the default spi_init() function.