3 #include "HardwareSPI.h"
4 #include "pico/stdlib.h"
5 #include "hardware/spi.h"
6 #include "hardware/irq.h"
30 begin(
false, -1,-1,-1,-1);
42 virtual void begin(
bool slave,
int pinRx=-1,
int pinTx=-1,
int pinCS=-1,
int pinSCK=-1) {
43 spi_set_slave(spi, slave);
44 setupPins(pinRx, pinTx, pinCS, pinSCK);
62 if (last_settings != settings || !is_init){
63 spi_init (spi, settings.getClockFreq() );
65 SPIMode mode = settings.getDataMode();
69 cpol=SPI_CPOL_0;cpha=SPI_CPHA_0;
72 cpol=SPI_CPOL_0;cpha=SPI_CPHA_1;
75 cpol=SPI_CPOL_1;cpha=SPI_CPHA_0;
78 cpol=SPI_CPOL_1;cpha=SPI_CPHA_1;
82 BitOrder order_arduino = settings.getBitOrder();
83 spi_order_t order = order_arduino == LSBFIRST ? SPI_LSB_FIRST : SPI_MSB_FIRST;
88 if (using_interrupt_no!=0)
89 irq_set_enabled(using_interrupt_no,
false);
90 is_transaction =
true;
98 is_transaction =
false;
100 if (using_interrupt_no!=0)
101 irq_set_enabled(using_interrupt_no,
true);
111 uint8_t array[1]={data};
124 spi_write16_read16_blocking(spi, &data, &result, 1);
136 spi_write_read_blocking(spi, (
const uint8_t*) array, (uint8_t*) array, len);
147 using_interrupt_no = interruptNumber;
150 virtual void notUsingInterrupt(
int interruptNumber) {
151 irq_set_enabled(interruptNumber,
true);
152 using_interrupt_no = 0;
155 virtual void attachInterrupt() {
156 int interrupt = getStandardInterrupt();
158 irq_set_enabled(interrupt,
true);
162 virtual void detachInterrupt() {
163 int interrupt = getStandardInterrupt();
165 irq_set_enabled(interrupt,
false);
177 int using_interrupt_no;
182 int getStandardInterrupt(){
186 }
else if (spi == spi0){
193 spi_set_format(spi, data_bits, cpol, cpha, order);
196 void setupPins(
int pinRx=-1,
int pinTx=-1,
int pinCS=-1,
int pinSCK=-1){
210 }
else if (spi == spi1){
224 gpio_set_function(pinRx, GPIO_FUNC_SPI);
225 gpio_set_function(pinSCK, GPIO_FUNC_SPI);
226 gpio_set_function(pinTx, GPIO_FUNC_SPI);
230 gpio_set_dir(pinCS, GPIO_OUT);
234 if (Logger.isLogging()) {
235 Logger.info(
"pinRx is ", toStr(pinRx));
236 Logger.info(
"pinTx is ", toStr(pinTx));
237 Logger.info(
"pinSCK is ", toStr(pinSCK));
238 Logger.info(
"pinCS is ", toStr(pinCS));
241 Logger.error(
"Invalid SPI device");
246 const char* toStr(
int value){
247 static char buffer[10];
248 itoa(value,buffer,10);
249 return (
const char*)buffer;
Arduino HardwareSPI interface using the Pico API. We use the following default pins spi0: pinRx = 16;...
Definition: PicoHardwareSPI.h:15
virtual void end()
Disables the SPI bus (leaving pin modes unchanged).
Definition: PicoHardwareSPI.h:51
virtual void begin(bool slave, int pinRx=-1, int pinTx=-1, int pinCS=-1, int pinSCK=-1)
Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low,...
Definition: PicoHardwareSPI.h:42
virtual void transfer(void *array, size_t len)
SPI transfer is based on a simultaneous send and receive of len bytes.
Definition: PicoHardwareSPI.h:135
virtual uint8_t transfer(uint8_t data)
SPI transfer is based on a simultaneous send and receive of 1 byte.
Definition: PicoHardwareSPI.h:110
virtual void usingInterrupt(int interruptNumber)
If your program will perform SPI transactions within an interrupt, call this function to register the...
Definition: PicoHardwareSPI.h:146
virtual void beginTransaction(SPISettings settings)
Initializes the SPI bus using the defined SPISettings.
Definition: PicoHardwareSPI.h:61
virtual void begin()
Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low,...
Definition: PicoHardwareSPI.h:29
virtual uint16_t transfer16(uint16_t data)
SPI transfer is based on a simultaneous send and receive of 2 bytes.
Definition: PicoHardwareSPI.h:122
virtual void endTransaction(void)
Stop using the SPI bus. Normally this is called after de-asserting the chip select,...
Definition: PicoHardwareSPI.h:97
Definition: HardwareSPI.h:105
Definition: HardwareSPI.h:37