Need help with SPI code please

orbitcoms
Posts: 141
Joined: Fri Aug 03, 2018 10:08 pm
Location: Sydney, Australia

Need help with SPI code please

Postby orbitcoms » Sat Jul 24, 2021 11:58 pm

I am having a bit of trouble getting SPI running for my project.
The functions I need to interface with my SPI slave are:

Write Register - Send a register address and value (both 8-bit)
Write Command - Send 1 byte command
Read Register - Send 1 byte address/command byte and read back 1 byte.

Below is my code, When I read back I get all "0". I am a bit confused how to send the address and also read back the value from that reg and whether this ends up in rx_data[0]?

Thanks ins advance for any help.

[code]
#define TAG "SPI"

spi_device_handle_t spi;

void lf_write_reg(uint8_t address, uint8_t reg_val)
{
esp_err_t ret;
spi_transaction_t t;
memset(&t,0,sizeof(t));
t.length = 8 * 2; //total bytes * 8 bits
t.flags = SPI_TRANS_USE_RXDATA;
t.tx_data[0] = address;
t.tx_data[1] = reg_val;
t.tx_buffer = t.tx_data;
ret = spi_device_polling_transmit(spi,&t);
assert(ret == ESP_OK);
}

void lf_write_command(uint8_t command)
{
esp_err_t ret;
spi_transaction_t t;
memset(&t,0,sizeof(t));
t.length = 8; //total bytes * 8 bits
t.flags = SPI_TRANS_USE_RXDATA;
t.tx_data[0] = command;
t.tx_buffer = t.tx_data;
ret = spi_device_polling_transmit(spi,&t);
assert(ret == ESP_OK);
}

void lf_init_regs(void)
{
ESP_LOGI(TAG,"Init reg values");
uint8_t reg_val[10] = { 0x5e, 0x4B, 0x20, 0x3F, 0xF0, 0x69, 0x96, 0x5F, 0x00, 0x00 };
for (uint8_t i = 0; i < 10; i++)
{
lf_write_reg(i, reg_val[i]);
}
}

uint8_t lf_read_byte(uint8_t address)
{
esp_err_t ret;
uint8_t rx_buf[5];

spi_transaction_t t;
memset(&t,0,sizeof(t));
t.length = t.rxlength = 8 * 2;
t.flags = SPI_TRANS_USE_RXDATA;
t.tx_data[0] = address;
t.tx_data[1] = 0x00;
t.tx_buffer = t.tx_data;
t.rx_buffer = rx_buf;
ret = spi_device_polling_transmit(spi,&t);
assert(ret == ESP_OK);
return rx_buf[1];
}

void read_lf_regs(void)
{
ESP_LOGI(TAG,"Read regs");
uint8_t rx_data[10];
for (int8_t i = 0; i < 10; i++)
{
rx_data[i] = lf_read_byte(i | 0x40);
}
esp_log_buffer_hex(TAG, rx_data, 10);
}

void spi_init(void)
{
esp_err_t ret;

spi_bus_config_t buscfg={
.miso_io_num= MISO,
.mosi_io_num= MOSI,
.sclk_io_num= SCK,
.quadwp_io_num=-1,
.quadhd_io_num=-1,
.max_transfer_sz = 32,
};

spi_device_interface_config_t devcfg={
.clock_speed_hz=1*1000*1000, //Clock out at 1 MHz
.mode=0, //SPI mode 0
.spics_io_num= CS, //CS pin
.dummy_bits = 0,
.address_bits = 0,
.command_bits = 0,
.queue_size=1, //We want to be able to queue 1 transactions at a time
};

ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
ESP_ERROR_CHECK(ret);
ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG,"SPI installed ok\n");
lf_init_regs();
read_lf_regs();
}

[/code]

User avatar
fasani
Posts: 195
Joined: Wed Jan 30, 2019 12:00 pm
Location: Barcelona
Contact:

Re: Need help with SPI code please

Postby fasani » Sun Aug 15, 2021 6:02 am

Hello @orbitcoms
First of all check again IDF documentation on SPI master I believe there are examples for reading a value.
I also have an epaper component for esp-IDF that reads temperature on some einks that have 4 wire SPI since this displays do not work well when they are outside an acceptable temperature range:
https://github.com/martinberlin/cale-id ... spi2cs.cpp

It’s written in CPP but for the example it does not matter. If I remember well, the response is coming on a pointer * rx_data
Good luck!
epdiy collaborator | http://fasani.de Fan of Espressif MCUs and electronic design

Who is online

Users browsing this forum: No registered users and 62 guests