0

I´m trying to set up an interrupt function, that gets triggerd, when the 8bit RX register of my SPI port is filled on my Raspberry Pi Pico. The Raspberry is running in Slave Mode.

I have checked the SDK documentary and also tried to find examples on how to use SPI interrupts, but wasn´t able to find any. As there is an Interrupt source for SPI interrupts, i assume i should be able to achive what i need, but the interrupt never gets triggerd.

I´ve attached the code of my program:

//setup of the SPI Pins on the Raspberry Board
gpio_set_function(8, GPIO_FUNC_SPI);
gpio_set_function(9, GPIO_FUNC_SPI);
gpio_set_function(10, GPIO_FUNC_SPI);
gpio_set_function(11, GPIO_FUNC_SPI);

//Setting Baudrate to 1000000 and spi1 to slave Mode
spi_init(spi1, 1000 * 1000);
spi_set_slave(spi1, 1);

//Setting Interrupt Source to SPI interrupt
irq_set_enabled(SPI1_IRQ,1);

//setting up a function to handle the interrupt
irq_set_exclusive_handler(SPI1_IRQ, spiIRQroutine);

//Interrupt Service Routine
void spiIRQroutine() {
    Serial.println("Interrupt"); //print "interrupt to console"
    spi_write_read_blocking(spi1, 0, spi1in_buf, BUF_LEN); //read data on RX Pin to spi1in_buf

    for(int i=0; i< sizeof(spi1in_buf); i++) {
        Serial.println(spi1in_buf); //write the RX buffer to console
    }
}

//Blink program
void loop() {
    gpio_put(LED_BUILTIN, 1);
    sleep_ms(4 * 1000);
    gpio_put(LED_BUILTIN, 0);
    sleep_ms(4 * 1000);
}

1 Answer 1

1

I can't help with the detail of your code.

However I do have working code (at least it was at the time it was published).

See my picod daemon code.

Search for my_spi_handler for the right area to be looking at.

5
  • Hi, thank you. For what reason did you use "SPI0_IRQ+channel"? I am referring to: irq_set_exclusive_handler(SPI0_IRQ+channel, my_spi_handler); irq_set_enabled(SPI0_IRQ+channel, true); SPI0_IRQ is from the Interrupt Source chart, but why did you add channel? I´d really apprechiate if you could help me out with this! 7
    – 0i9n
    Jul 26, 2022 at 11:28
  • @0i9n There are two SPI channels (0 and 1). My software might be using either. Use the appropriate value for the channel you are using. See github.com/joan2937/picod/blob/master/DAEMON/picod.c#L370
    – joan
    Jul 26, 2022 at 12:05
  • ok that makes sense. so is there a certain config needed to get started with the SPI interrupts or interrupts in general? Or are the 2 lines i mentioned above the only setting to be done. I am wondering because for the UART theres a "set enable IRQ" command thats necessary. Also I am using the Arduino IDE so that might be an issue? Thanks so far btw
    – 0i9n
    Jul 27, 2022 at 6:33
  • I haven't used the Arduino IDE with the Pi. As for initialisation, anything needed will be done in SPI_OPEN.
    – joan
    Jul 27, 2022 at 8:32
  • hi @joan, Thanks the SPI + Interrupt is working now. But for some reason the interrupt comes early sometimes. The Interrupt should trigger after i recieve 16 Bits at the RX Pin. But sometimes the Interrupt is getting triggerd after 6 Bits or 9 Bits (Or any other - i cant see why it triggers before 16 bits are recieved). Do i have to clear the FIFO at the beginning of my code? It seems like sometimes there are bits "left" in the FIFO and the interrupt is getting triggert at bits other then the 16th.
    – 0i9n
    Aug 22, 2022 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.