Hi,
I tried running an spi sample program with code as in below, shorted the MOSI and MISO pins and data was visibly received in print statement:
#include <zephyr.h> #include <sys/printk.h> #include <drivers/spi.h> #include <drivers/gpio.h> uint32_t freq = 4000000; static const struct spi_config spi_cfg = {freq,(SPI_WORD_SET(8) | SPI_TRANSFER_MSB),0,nullptr}; const device * spi_dev; static void spi_init(void) { const char* spiName = "SPI_1"; //const char* const spiName = "spi1"; spi_dev = device_get_binding(spiName); if (spi_dev == NULL) { printk("Could not get %s device\n", spiName); return; } } void spi_test_send(void) { int err; static uint32_t tx_buffer[1]={0x05}; //static uint8_t tx_buffer = 128; //static char tx_buffer[3]={'D','s','s'}; static uint32_t rx_buffer[1]; //static uint8_t rx_buffer; const struct spi_buf tx_buf = { .buf = tx_buffer, .len = sizeof(tx_buffer) }; const struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 }; struct spi_buf rx_buf = { .buf = rx_buffer, .len = sizeof(rx_buffer), }; const struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1 }; err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx); if (err) { printk("SPI error: %d\n", err); } else { /* Connect MISO to MOSI for loopback */ printk("TX sent: %d\n", tx_buffer[0]); printk("RX recv: %d\n", rx_buffer[0]); } } void main(void) { printk("SPIM Example\n"); spi_init(); while (1) { spi_test_send(); k_sleep(K_SECONDS(1)); } }
While in the same code if i use spi_write( ) & spi_read( ) apis, which inturn call spi transceive, I am not receiving any data, code as in below:
#include <zephyr.h> #include <sys/printk.h> #include <drivers/spi.h> #include <drivers/gpio.h> uint32_t freq = 4000000; static const struct spi_config spi_cfg = {freq,(SPI_WORD_SET(8) | SPI_TRANSFER_MSB),0,nullptr}; const device * spi_dev; static void spi_init(void) { const char* spiName = "SPI_1"; //const char* const spiName = "spi1"; spi_dev = device_get_binding(spiName); if (spi_dev == NULL) { printk("Could not get %s device\n", spiName); return; } } void spi_test_send(void) { int err; static uint32_t tx_buffer[1]={0x05}; //static uint8_t tx_buffer = 128; //static char tx_buffer[3]={'D','s','s'}; static uint32_t rx_buffer[1]; //static uint8_t rx_buffer; const struct spi_buf tx_buf = { .buf = tx_buffer, .len = sizeof(tx_buffer) }; const struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 }; struct spi_buf rx_buf = { .buf = rx_buffer, .len = sizeof(rx_buffer), }; const struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1 }; err = spi_write(spi_dev, &spi_cfg, &tx); err = spi_read(spi_dev, &spi_cfg, &rx); if (err) { printk("SPI error: %d\n", err); } else { /* Connect MISO to MOSI for loopback */ printk("TX sent: %d\n", tx_buffer[0]); printk("RX recv: %d\n", rx_buffer[0]); } } void main(void) { printk("SPIM Example\n"); spi_init(); while (1) { spi_test_send(); k_sleep(K_SECONDS(1)); } }
I am getting just -1 in receive buffer.
Kindly suggest what i can do to make spi_write & spi_read work..?