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

SDK • How to send 9-bit data words via PICO hardware_spi API

$
0
0
I am trying to communicate from a standard RP Pico to an LCD module using a 3-wire SPI interface that uses 9-bits of data per command (9th bit indicates whether it is a command or data). However, I only need to send commands, not read anything so I am hoping to use the SDK SPI API.
The API command spi_set_format in the Hardware API documentation states that the data_bits is "Number of data bits per transfer. Valid values 4..16" so that sounds perfect. I set the SPI channel up for 9 bits of data OK but the problem is, I cannot find any way to actually send it because the spi_write_blocking command gives a compile error if I try to specify the source data as anything but a uint8_t value.
I have tried building the source data in a 9-bit member of a STRUCT, and also just using a uint16_t but either way, I can't write the data to the SPI interface.

Code:

#include <stdio.h>#include <stdint.h>#include <string.h>#include "pico/stdlib.h"#include "pico/binary_info.h"#include "hardware/spi.h"const uint cs_pin = 13; // Chip Selectconst uint sck_pin = 10; // Clockconst uint mosi_pin = 11; // TXconst uint miso_pin = 12; // RXconst uint tft_reset =  14; // Panel reset gpio// Portsspi_inst_t *spi = spi1;void SPI_writedat ( spi_inst_t *spi,                    const uint8_t data ) {    uint16_t newdata =  0x0100 | data;    gpio_put(cs_pin, 0);    int bytes = spi_write_blocking(spi1, newdata, 1); // Send one 'byte' from packet.word;    gpio_put(cs_pin, 1);}int main() {        gpio_init(tft_reset);    gpio_set_dir(tft_reset, GPIO_OUT);    // Initialize chosen serial port    stdio_init_all();    // Initialize CS pin high    gpio_init(cs_pin);    gpio_set_dir(cs_pin, GPIO_OUT);    gpio_put(cs_pin, 1);    spi_init(spi, 1000 * 1000);    spi_set_format( spi1,   // SPI instance                    9,      // Number of bits per transfer. 3-wire SPI uses first bit as Cmd(0)/Data(1) indicator                    1,      // Polarity (CPOL)                    1,      // Phase (CPHA)                    SPI_MSB_FIRST);    // Initialize SPI pins    gpio_set_function(sck_pin, GPIO_FUNC_SPI);    gpio_set_function(mosi_pin, GPIO_FUNC_SPI);    gpio_set_function(miso_pin, GPIO_FUNC_SPI);    sleep_ms(1000);    SPI_writedat(spi, 0x77);}
Can anyone explain how to send SPI data with anything other than 8 or 16-bit packets?
Thanks

Statistics: Posted by all2coolnick — Mon Nov 11, 2024 5:56 pm



Viewing all articles
Browse latest Browse all 7849

Trending Articles