DevLab_ICM20948 1.0.0 Driver para sensor ICM-20948

Loading...
Searching...
No Matches
7Semi_SPI_Interface.h
Go to the documentation of this file.
1#pragma once
2#include "7Semi_Interface.h"
3
4/**
5 * 7Semi SPI Interface Implementation
6 */
7 class SPI_Interface : public Interface_7Semi
8{
9
10 public:
11 SPIClass *spi = nullptr;
12 uint8_t cs = 255;
13 uint32_t speed = 1000000;
14
15 /**
16 * beginSPI()
17 *
18 * - Initialize SPI interface
19 * - Configure CS and SPI bus
20 */
21 bool beginSPI(uint8_t csPin,
22 SPIClass &spiPort,
23 uint32_t spiSpeed,
24 uint8_t sck = 255,
25 uint8_t miso = 255,
26 uint8_t mosi = 255) override
27 {
28 if (csPin == 255)
29 return false;
30
31 spi = &spiPort;
32 cs = csPin;
33 speed = spiSpeed;
34
35 pinMode(cs, OUTPUT);
36 digitalWrite(cs, HIGH);
37
38#if defined(ESP32)
39 if (sck != 255 && miso != 255 && mosi != 255)
40 spi->begin(sck, miso, mosi, cs);
41 else
42 spi->begin();
43#else
44 spi->begin();
45#endif
46
47 delay(1);
48 return true;
49 }
50
51 bool beginI2C(uint8_t, TwoWire &, uint32_t,
52 uint8_t, uint8_t) override
53 {
54 return false;
55 }
56
57 /**
58 * read()
59 *
60 * - Read multiple bytes from register
61 */
62 int8_t read(uint8_t reg, uint8_t *data, uint32_t len) override
63 {
64 if (!spi || !data || len == 0)
65 return -1;
66
67 SPISettings settings(speed, MSBFIRST, SPI_MODE0);
68
69 spi->beginTransaction(settings);
70 digitalWrite(cs, LOW);
71 delayMicroseconds(10);
72
73 /**
74 * Send register address (read)
75 */
76 spi->transfer(reg | 0x80);
77
78 delayMicroseconds(10);
79
80 /**
81 * Read data
82 */
83 for (uint32_t i = 0; i < len; i++)
84 data[i] = spi->transfer(0x00);
85
86 digitalWrite(cs, HIGH);
87 delayMicroseconds(10);
88 spi->endTransaction();
89
90 // Serial.print("R-Reg: ");
91 // Serial.print(reg, HEX);
92 // Serial.print(" |Data: ");
93 // for (int i = 0; i < len; i++)
94 // {
95 // Serial.print(" ");
96 // Serial.print(data[i], HEX);
97 // }
98 // Serial.println();
99
100 return 0;
101 }
102
103 /**
104 * write()
105 *
106 * - Write multiple bytes to register
107 */
108 int8_t write(uint8_t reg, const uint8_t *data, uint32_t len) override
109 {
110 if (!spi || !data || len == 0)
111 return -1;
112
113 SPISettings settings(speed, MSBFIRST, SPI_MODE0);
114
115 spi->beginTransaction(settings);
116 digitalWrite(cs, LOW);
117 delayMicroseconds(10);
118
119 /**
120 * Send register address (write)
121 */
122 spi->transfer(reg & 0x7F);
123
124 /**
125 * Write data
126 */
127 for (uint32_t i = 0; i < len; i++)
128 spi->transfer(data[i]);
129
130 digitalWrite(cs, HIGH);
131 delayMicroseconds(10);
132 spi->endTransaction();
133
134 // Serial.print("W-Reg: ");
135 // Serial.print(reg, HEX);
136 // Serial.print(" |Data: ");
137 // for (int i = 0; i < len; i++)
138 // {
139 // Serial.print(" ");
140 // Serial.print(data[i], HEX);
141 // }
142 // Serial.println();
143
144 return 0;
145 }
146};
7Semi_Interface.h
Interface_7Semi
Definition 7Semi_Interface.h:15
SPI_Interface
Definition 7Semi_SPI_Interface.h:8
SPI_Interface::read
int8_t read(uint8_t reg, uint8_t *data, uint32_t len) override
Definition 7Semi_SPI_Interface.h:62
SPI_Interface::beginSPI
bool beginSPI(uint8_t csPin, SPIClass &spiPort, uint32_t spiSpeed, uint8_t sck=255, uint8_t miso=255, uint8_t mosi=255) override
Definition 7Semi_SPI_Interface.h:21
SPI_Interface::spi
SPIClass * spi
Definition 7Semi_SPI_Interface.h:11
SPI_Interface::beginI2C
bool beginI2C(uint8_t, TwoWire &, uint32_t, uint8_t, uint8_t) override
Definition 7Semi_SPI_Interface.h:51
SPI_Interface::cs
uint8_t cs
Definition 7Semi_SPI_Interface.h:12
SPI_Interface::speed
uint32_t speed
Definition 7Semi_SPI_Interface.h:13
SPI_Interface::write
int8_t write(uint8_t reg, const uint8_t *data, uint32_t len) override
Definition 7Semi_SPI_Interface.h:108