Home > Enterprise >  receiving wrong data sent from a pi pico on a raspberry pi 4 using wiringPi
receiving wrong data sent from a pi pico on a raspberry pi 4 using wiringPi

Time:01-10

So I am using the c/c sdk for the pi pico to try and send data to a raspberry pi 4 over spi. On the Pi 4 I am using wiringPi library for the SPI functionality. However when I send the value I want to send on SPI from my PICO (which is being sent as I checked with a scope), to the Pi 4, the pi 4 receives the information wrong, such as prints seemingly random characters instead of the intended value.

Any help with this would be greatly appreciated, see below the code from my Pico and my Pi 4, I am expecting the value of 5 to be received. I have changed the len parameter in both the pico and pi 4 to 1,2,4 and 8 and all gives the same result.

#include <iostream>
#include <stdio.h>

#include "hardware/uart.h"
#include "pico/stdlib.h"
#include "hardware/spi.h"

using namespace std;

//-------SPI Ports-------
#define SPI_PORT spi0
#define SPI_PORT_1 spi1
//-----------------------

//-------Port 0 SPI pins--------
#define MOSI 3
#define MISO 4
#define SCK 2
#define CS 5
//------------------------------

//-------UART Setup-------
#define UART_ID uart0
#define BAUD_RATE_UART 115200

#define UART_TX_PIN 0
#define UART_RX_PIN 1
//------------------------

uint8_t test_send;

uint8_t test_read[3];

int main() {

    stdio_init_all();

    //-------------------UART init------------------
    uart_init(UART_ID, BAUD_RATE_UART);

    gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
    gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
    //----------------------------------------------

    spi_init(SPI_PORT, 500000);

    gpio_init(CS);
    gpio_set_dir(CS, GPIO_OUT);
    gpio_put(CS, 1);

    spi_set_format(SPI_PORT, 12, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);

    gpio_set_function(SCK, GPIO_FUNC_SPI);
    gpio_set_function(MOSI, GPIO_FUNC_SPI);
    gpio_set_function(MISO, GPIO_FUNC_SPI);

    test_send = 5;

    while(1){

    //printf("here top");
    gpio_put(CS, 0);
    spi_write_blocking(SPI_PORT, &test_send, 1);
    //spi_read_blocking(SPI_PORT, 0, test_read, 1);
    gpio_put(CS, 1);
    //printf("here\n");

   
    }
}

And here is the code I am using for Pi 4

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
//#include </home/dean21/Documents/mosquitto-master/include/mosquitto.h>

#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <errno.h>
#include <string.h>


using namespace std;

static const int CHANNEL = 0;
unsigned char holder[100];
unsigned char test;

static int myFd;


int main(){
wiringPiSetupGpio();
//holder[0] = 0x5;

unsigned char result;

wiringPiSPISetup (CHANNEL, 500000);


// even though this function is for read and write im only using it as a read

while(1){

wiringPiSPIDataRW(CHANNEL, //Enable / CS pin
 holder, //Data
 1); //Lenth

cout << "holder value is: " << holder << endl;

//delay(1000);

}

}

output example looks like this, when it should be receiving the value of 5

holder value is:(

holder value is:

holder value is:@

holder value is:P

holder value is:

holder value is:

holder value is:

CodePudding user response:

There are a few problems with your code.

  1. Your unsigned char holder[100]; seems to be uninitialized, so there is no way to know what data you have in those 100 bytes. You should always initialize your data.
  2. It looks like you are only reading 1 byte at a time into holder, but you are trying to print the whole buffer. Your probably only want to cout << holder[0]; . What seems to be happening is that it will try to print until it gets to a \0 or 0 but since holder is not initialized the first \0 is in some random place.

CodePudding user response:

I am afraid that you have misunderstood the SPI protocol. In SPI you have one master and a number of slaves, what you have is two masters yelling at each other - that is never going to work.

As far as I know, there are no ready driver that allows you to turn the Raspberry Pi 4 into a SPI slave, but a search found support for turning your Raspberry Pi Pico into a SPI slave: https://github.com/raspberrypi/pico-examples/tree/master/spi/spi_master_slave

Try to read up on the SPI protocol and then see if you can rethink what you want to work with that. One issue is that a SPI slave is written to or read from by the master, but the slave never initiates a transfer (a workaround is to use a GPIO as a "I have data ready" signal, some SPI ICs use that method so that the master doesn't have to constantly poll for new data).

  • Related