Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8013

SDK • PICO SPI Slave mode problems

$
0
0
I've been trying to get the SPI Slave mode foe the pico working, and found a great resource https://sourcevu.sysprogs.com/rp2040/pi ... .h#tok1192 for the various commands available.
The Basic problem is to get the pico to transfer a 32 byte buffer to an external spi master on demand. However, whilst the buffer is sent to the SPI slave function of the pico, only the first byte is sent on the first read, second on the second read etc, the remaining 31 bytes of each transfer are filled with alternating 0xff and 0x00. So it's looking like either a firmware (SPI-lib) fault or Hardware fault. This happens with either write _blocking or write_read_blocking commands. I'm trying to get this going on SPI1, not SPI0.

Code for Pico:

Code:

#include <stdio.h>#include "pico/stdlib.h"#include "hardware/gpio.h"#include <hardware/irq.h>#include "hardware/spi.h"#ifndef PICO_DEFAULT_LED_PIN#define PICO_DEFAULT_LED_PIN 25#endif#define BUF_LEN         0x20const uint CS1 = 13;  //# may need changing to 14uint8_t buffer[15];uint8_t rbuff[33]={0xff} ;uint8_t rbuff1[33]={0xff} ;uint8_t spi2buf[16];static inline void cs_select(uint cs) {    asm volatile("nop \n nop \n nop");    gpio_put(cs, 0);  // Active low    asm volatile("nop \n nop \n nop");}static inline void cs_deselect(uint cs) {    asm volatile("nop \n nop \n nop");    gpio_put(cs, 1);    asm volatile("nop \n nop \n nop");}void gpio_event_string(char *buf, uint32_t events);void init_buff(){    for (int t=0;t<33;t++){        rbuff1[t]=0xff-t;    }}void gpio_callback(uint gpio, uint32_t events) {        //cs_select(CS1);    int f = spi_write_read_blocking(spi1, rbuff1, rbuff,32);    //printf("IRQ read");    //cs_deselect(CS1);    //init_buff();    printf("IRQ return %d\n",f);    }   int main() {    stdio_init_all();    spi_init(spi1,400000);    spi_set_slave(spi1,true);    gpio_set_function(12, GPIO_FUNC_SPI);    gpio_set_function(10, GPIO_FUNC_SPI);    gpio_set_function(11, GPIO_FUNC_SPI);    gpio_set_function(CS1, GPIO_FUNC_SPI);    //gpio_pull_up(11);   // gpio_pull_up(12);   // gpio_pull_up(13);    init_buff();    printf("Hello SPI IRQ\n");    for (int t=0;t<33;t++){        printf("%x   ",rbuff1[t]);    }    printf("\n");    gpio_init(PICO_DEFAULT_LED_PIN);    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);    gpio_put(PICO_DEFAULT_LED_PIN,true);       // gpio_set_irq_enabled_with_callback(CS1, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);    // Wait forever    while (1){    gpio_put(PICO_DEFAULT_LED_PIN, gpio_get(CS1));    init_buff();    int f = spi_write_blocking(spi1, rbuff1, 32);    printf("main loop %d\n",f);    //sleep_ms(500);    }}
Code for SPI Master:

Code:

import timeimport spidevspi_1 = spidev.SpiDev()spi_1.open(0,0)spi_1.max_speed_hz = 400000spi_1.mode = 0spi_can = spidev.SpiDev()spi_can.open(1,0)spi_can.max_speed_hz = 1000000spi_can.mode = 0Rd_buff = [0]*32def read_spi():    spi1.xfer(Rd_buff)    return()while True:    read_spi()    print(Rd_buff)    time.sleep(5)
The problem happens irrespective of whether the spi_write is in the main loop or in the callback function. The SPI slave mode should also have a dedicated call back, but this does not seem to be implemented.
program result:
debian@BeagleBone:~/BMS$ sudo python picotest.py
[sudo] password for debian:
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[252, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[251, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[249, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[247, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[246, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[245, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[243, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[241, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 2
(sample).

Statistics: Posted by MikeD2 — Tue Jan 06, 2026 8:01 pm



Viewing all articles
Browse latest Browse all 8013

Trending Articles