YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
spi_rp2350.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// SPI driver for RP2350. It support master
15// and client mode. Interrupts are supported
16// for client mode (RX interrupts).
17//
18#ifndef _SPI_RP2350_H_
19#define _SPI_RP2350_H_
20
21#include "spi_interface.h"
22#include "gpio_rp2350.h"
23#include "RP2350.h"
24
25using namespace _SPI0_;
26using namespace _SPI1_;
27
28extern "C" {
29void SPI0_IRQ_Handler(void);
30void SPI1_IRQ_Handler(void);
31}
32
33namespace SPI {
34const uint16_t CPHA_0 = 0x0000;
35const uint16_t CPHA_1 = 0x0080;
36const uint16_t CPOL_0 = 0x0000;
37const uint16_t CPOL_1 = 0x0040;
38const uint16_t _4_BIT = 0x0003;
39const uint16_t _5_BIT = 0x0004;
40const uint16_t _6_BIT = 0x0005;
41const uint16_t _7_BIT = 0x0006;
42const uint16_t _8_BIT = 0x0007;
43const uint16_t _9_BIT = 0x0008;
44const uint16_t _10_BIT = 0x0009;
45const uint16_t _11_BIT = 0x000a;
46const uint16_t _12_BIT = 0x000b;
47const uint16_t _13_BIT = 0x000c;
48const uint16_t _14_BIT = 0x000d;
49const uint16_t _15_BIT = 0x000e;
50const uint16_t _16_BIT = 0x000f;
51
52// Verbose constants for SPI role
53const bool MASTER = true;
54const bool CLIENT = false;
55}
56
57class spi_rp2350 : public spi_interface {
58
59public:
60
61 spi_rp2350(gpio_pin_t miso_pin,
62 gpio_pin_t mosi_pin,
63 gpio_pin_t sclk_pin,
64 gpio_interface & cs_pin,
65 bool spi_master = SPI::MASTER,
66 uint16_t mode = SPI::CPOL_0 | SPI::CPHA_0 | SPI::_8_BIT);
67
68 ~spi_rp2350() override;
69
70 int16_t spiTxRx(const uint8_t *txbuf, uint8_t *rxbuf, uint16_t len) override;
71 int16_t spiTx (const uint8_t *txbuf, uint16_t len) override ;
72 int16_t spiRx (uint8_t txbyte, uint8_t *rxbuf, uint16_t len) override;
73
74 void setSpeed(uint32_t) override;
75 void generateCS(bool val) override;
76 void setCS(bool val) override;
77
78 void spiAttachRxIrq(function<void(uint8_t data)> f) override;
79
80 // IRQ handlers are our best friends
82 friend void SPI0_IRQ_Handler(void);
83 friend void SPI1_IRQ_Handler(void);
84
85private:
86 int _index;
87 gpio_rp2350 _miso;
88 gpio_rp2350 _mosi;
89 gpio_rp2350 _sclk;
90 gpio_interface& _cs;
91 bool _master;
92 uint16_t _mode;
93
94 bool _init;
95 bool _generate_CS;
96 uint32_t _baud;
97
98 SPI0_t * _spi;
99 SPI0_t * _spi_set;
100 SPI0_t * _spi_clr;
101
102 void init();
103
104 static int8_t _spi_miso_pins[2][6];
105
106 static function<void(uint8_t)> _intHandler[2];
107};
108
109#endif // _SPI_RP2350_H_