Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 8 months ago.
SPISlave is note replying to spidev
Hi , I am trying to connect a beaglebone with spidev interface to STM32F411RE Nucleo board using spi. Master: beaglebone (spidev linux driver) Slave :STM32F411RE Nucleo mbed board.
Heres my code SPI Master:
/********************************************************************** *Filename : spi_test.c *Purpose : this file sends a byte data and recieves a signle byte. ***********************************************************************/ #include <stdint.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <string.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <linux/types.h> #include <linux/spi/spidev.h> void spi_init(int fd) { uint8_t tx=0xdd; uint8_t rx=10; int ret; struct spi_ioc_transfer xfer; xfer.tx_buf= (unsigned long)&tx; xfer.rx_buf= (unsigned long)℞ xfer.len = 1; xfer.bits_per_word = 8; xfer.speed_hz = 1000000; //xfer.cs_change = 0; ret=ioctl(fd,SPI_IOC_MESSAGE(1),&xfer); if(ret < 1){ perror("Error while sending spi_messages"); exit(1); } printf("rx:%x\n",rx); }; int main() { int fd; fd=open("/dev/spidev1.0",O_RDWR); if(fd<0){ perror("Not able to open device file"); return -1; } while(1) spi_init(fd); return 0; }
And Here is my SPI Slave Mbed Program:
#include "mbed.h" //SPI Slave SPISlave device(PA_7,PA_6,PA_5,PA_4); // mosi, miso, sclk, ssel Serial pc(SERIAL_TX,SERIAL_RX); int main() { device.format(8,0); device.frequency(1000000); printf("Program Starting\r\n"); device.reply(0x00); // Prime SPI with first reply while(1) { if(device.receive()) { if(!ss){ int v = device.read(); // Read byte from master // Add one to it, modulo 256 device.reply(v); printf("Read 0x%x Reply 0x%x",v,v); } } } }
using uart i am able to see the received byte but my master is not receiving any data. its just zeroing the buffers.
1 Answer
9 years, 8 months ago.
I suppose that tx and rx into spi_init shoud be array of char...
try with :
uint8_t tx[10]; uint8_t rx[10];
Look at the internet about spi examples with linux/raspberry, you will find a lot a sources.