0

so I was hoping someone could help me out, pretty much I am using a raspberry pi pico with visual studio to program it in c++. I need to send a 4 digit hex value (16bits) using SPI, this can be done by either two 8 bit words or one 16 bit word. The library being used is "hardware/spi.h", I can get it to send 8 bits, for example if I want to send 0x2100, I can see the first 8 bits but nothing after.

Does anyone know a way or a different library to send the full 0x2100 as either 1 send or two 8 bit sends to have the full 0x2100.

Please see my code below, any help with this would be greatly appreciated.

#include "pico/stdlib.h"
#include <stdio.h>
#include <iostream>
#include <string.h>
#include "hardware/spi.h"

#define MOSI 3
#define SCK 2
#define CS 5

#define SPI_PORT spi0

using namespace std;


int main(){

    stdio_init_all();

    // 29-31 is setting up the SPI and SPI modes
    spi_init(SPI_PORT, 5000000);
    gpio_set_function(MOSI, GPIO_FUNC_SPI);
    gpio_set_function(SCK, GPIO_FUNC_SPI);

    //Config Chip Select
    gpio_init(CS);  // Initialise CS Pin
    gpio_set_dir(CS, GPIO_OUT); // Set as output
    gpio_put(CS, 1);



     gpio_init(25);
     gpio_set_dir(25, GPIO_OUT);


    while (1){
    gpio_put(1, 1);

    uint16_t data [2];
    uint8_t data1 [2]; //testing
    
    data[0] = 0x2100;

    

    gpio_put(CS, 0);
    spi_write16_blocking(SPI_PORT, data, 1);
   // spi_write_blocking(SPI_PORT, data1, 1);
    gpio_put(CS, 1);

    sleep_ms(10);

    gpio_put(25, 1);
    }

}     


Dean
  • 63
  • 5
  • Please review the SPI channel specs. You may have to set up the SPI channel for 16 bits in order for it to handle 16 bits. Looks like the default is 8 bits. – Thomas Matthews Aug 22 '22 at 17:00
  • @Thomas Matthews I did see that but in the documentation I wasnt sure what value the CPOL and CPHA parameters needed to be to do this. – Dean Aug 22 '22 at 17:24
  • try 0, 0 for CPOL, CPHA? I'm not sure what they should be either, but your options are only 0 and 1 for both of them, so shouldn't be too difficult to find the right combination. – Daniel Garcia Aug 27 '22 at 20:58

0 Answers0