-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEVE_MCU.cpp
More file actions
292 lines (232 loc) · 7.89 KB
/
EVE_MCU.cpp
File metadata and controls
292 lines (232 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
@file eve_arch_arduino.ino
*/
/*
* ============================================================================
* (C) Copyright Bridgetek Pte Ltd
* ============================================================================
*
* This source code ("the Software") is provided by Bridgetek Pte Ltd
* ("Bridgetek") subject to the licence terms set out
* https://brtchip.com/wp-content/uploads/2021/11/BRT_Software_License_Agreement.pdf ("the Licence Terms").
* You must read the Licence Terms before downloading or using the Software.
* By installing or using the Software you agree to the Licence Terms. If you
* do not agree to the Licence Terms then do not download or use the Software.
*
* Without prejudice to the Licence Terms, here is a summary of some of the key
* terms of the Licence Terms (and in the event of any conflict between this
* summary and the Licence Terms then the text of the Licence Terms will
* prevail).
*
* The Software is provided "as is".
* There are no warranties (or similar) in relation to the quality of the
* Software. You use it at your own risk.
* The Software should not be used in, or for, any medical device, system or
* appliance. There are exclusions of Bridgetek liability for certain types of loss
* such as: special loss or damage; incidental loss or damage; indirect or
* consequential loss or damage; loss of income; loss of business; loss of
* profits; loss of revenue; loss of contracts; business interruption; loss of
* the use of money or anticipated savings; loss of information; loss of
* opportunity; loss of goodwill or reputation; and/or loss of, damage to or
* corruption of data.
* There is a monetary cap on Bridgetek's liability.
* The Software may have subsequently been amended by another user and then
* distributed by that other user ("Adapted Software"). If so that user may
* have additional licence terms that apply to those amendments. However, Bridgetek
* has no liability in relation to those amendments.
* ============================================================================
*/
// Guard against being used for incorrect CPU type.
#if defined(ARDUINO)
#include <Arduino.h>
#include <SPI.h>
/** @brief Library Includes
* NOTE That all the file used in the example sketch must be in the same directory
* as the sketch. Copy this file, the include files from the include directory,
* the source files from the source directory of the library to the sketch
* location.
* It will be possible to make a library with these files.
* Include these files as "C" files.
*/
//@{
extern "C" {
#include <EVE4.h>
#include <MCU.h>
}
//@}
/** @brief Pin definitions
*/
//@{
/// Standard SPI pinouts 10(CS), 11(COPI), 12(CIPO), 13(SCK)
#define PIN_SPICLOCK 13 // SCK
#define PIN_DATAOUT 11 // MOSI (COPI)
#define PIN_DATAIN 12 // MISO (CIPO)
#define PIN_CHIPSELECT 10 // CS#
/// Additional pin for power down on EVE
#define PIN_POWERDOWN 9 // PD#
//@}
void MCU_Init(void) {
uint8_t clr;
SPI.begin();
pinMode(PIN_CHIPSELECT, OUTPUT);
pinMode(PIN_POWERDOWN, OUTPUT);
digitalWrite(PIN_CHIPSELECT, HIGH); //disable CS#
digitalWrite(PIN_POWERDOWN, HIGH); //disable HD#
// Set SPI speed to 1 MHz
// 1 MHz allows all EVE devices to initialise correctly
// After initialisation the SPI speed can be increased in the MCU_Setup()
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
}
void MCU_Deinit(void) {
SPI.endTransaction();
SPI.end();
}
void MCU_Setup(void) {
SPI.endTransaction();
// Increase SPI speed to 8 MHz after initialisation is complete
// See the notes for MCU_SPI_TIMEOUT in the MCU.h file.
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
}
// Simple endian alignment for tested Arduino devices
#define bswap16(x) __builtin_bswap16(x)
#define bswap32(x) __builtin_bswap32(x)
// ########################### GPIO CONTROL ####################################
// --------------------- Chip Select line low ----------------------------------
void MCU_CSlow(void) {
digitalWrite(PIN_CHIPSELECT, LOW); // disable CS#
delayMicroseconds(10);
}
// --------------------- Chip Select line high ---------------------------------
void MCU_CShigh(void) {
digitalWrite(PIN_CHIPSELECT, HIGH); // disable CS#
delayMicroseconds(10);
}
// -------------------------- PD line low --------------------------------------
void MCU_PDlow(void) {
digitalWrite(PIN_POWERDOWN, LOW); // enable HD#
}
// ------------------------- PD line high --------------------------------------
void MCU_PDhigh(void) {
digitalWrite(PIN_POWERDOWN, HIGH); // disable HD#
}
// Exchange a single byte on the SPI bus
char MCU_SPIReadWrite8(uint8_t val) {
uint8_t v = SPI.transfer(val);
return v;
}
uint16_t MCU_SPIReadWrite16(uint16_t DataToWrite) {
uint16_t DataRead = 0;
uint16_t temp;
temp = (MCU_SPIReadWrite8((DataToWrite >> 0) & 0xff) & 0xff);
DataRead |= (temp << 0);
temp = (MCU_SPIReadWrite8((DataToWrite >> 8) & 0xff) & 0xff);
DataRead |= (temp << 8);
return DataRead;
}
uint32_t MCU_SPIReadWrite24(uint32_t DataToWrite) {
uint32_t DataRead = 0;
uint32_t temp;
temp = (MCU_SPIReadWrite8((DataToWrite >> 0) & 0xff) & 0xff);
DataRead |= (temp << 8);
temp = (MCU_SPIReadWrite8((DataToWrite >> 8) & 0xff) & 0xff);
DataRead |= (temp << 16);
temp = (MCU_SPIReadWrite8((DataToWrite >> 16) & 0xff) & 0xff);
DataRead |= (temp << 24);
return DataRead;
}
uint32_t MCU_SPIReadWrite32(uint32_t DataToWrite) {
uint32_t DataRead = 0;
uint32_t temp;
temp = (MCU_SPIReadWrite8((DataToWrite >> 0) & 0xff) & 0xff);
DataRead |= (temp << 0);
temp = (MCU_SPIReadWrite8((DataToWrite >> 8) & 0xff) & 0xff);
DataRead |= (temp << 8);
temp = (MCU_SPIReadWrite8((DataToWrite >> 16) & 0xff) & 0xff);
DataRead |= (temp << 16);
temp = (MCU_SPIReadWrite8((DataToWrite >> 24) & 0xff) & 0xff);
DataRead |= (temp << 24);
return DataRead;
}
void MCU_Delay_20ms(void) {
delay(20);
}
void MCU_Delay_500ms(void) {
delay(500);
}
// --------------------- SPI Send and Receive ----------------------------------
uint8_t MCU_SPIRead8(void) {
uint8_t DataRead = 0;
DataRead = MCU_SPIReadWrite8(0);
return DataRead;
}
void MCU_SPIWrite8(uint8_t DataToWrite) {
MCU_SPIReadWrite8(DataToWrite);
}
uint16_t MCU_SPIRead16(void) {
uint16_t DataRead = 0;
DataRead = MCU_SPIReadWrite16(0);
return DataRead;
}
void MCU_SPIWrite16(uint16_t DataToWrite) {
MCU_SPIReadWrite16(DataToWrite);
}
uint32_t MCU_SPIRead24(void) {
uint32_t DataRead = 0;
DataRead = MCU_SPIReadWrite24(0);
return DataRead;
}
void MCU_SPIWrite24(uint32_t DataToWrite) {
MCU_SPIReadWrite24(DataToWrite);
}
uint32_t MCU_SPIRead32(void) {
uint32_t DataRead = 0;
DataRead = MCU_SPIReadWrite32(0);
return DataRead;
}
void MCU_SPIWrite32(uint32_t DataToWrite) {
MCU_SPIReadWrite32(DataToWrite);
}
void MCU_SPIWrite(const uint8_t *DataToWrite, uint32_t length) {
//TODO: replace with SPI.transfer(DataToWrite, length);
// Note that DataToWrite is overwritten.
uint16_t DataPointer = 0;
while (DataPointer < length) {
MCU_SPIWrite8(DataToWrite[DataPointer]); // Send data byte-by-byte from array
DataPointer++;
}
}
void MCU_SPIRead(uint8_t *DataToRead, uint32_t length) {
//TODO: replace with SPI.transfer(DataToRead, length);
uint16_t DataPointer = 0;
while (DataPointer < length) {
DataToRead[DataPointer] = MCU_SPIRead8(); // Receive data byte-by-byte to array
DataPointer++;
}
}
// Arduino is Little Endian.
// Use toolchain defined functions.
uint16_t MCU_htobe16(uint16_t h) {
return bswap16(h);
}
uint32_t MCU_htobe32(uint32_t h) {
return bswap32(h);
}
uint16_t MCU_htole16(uint16_t h) {
return h;
}
uint32_t MCU_htole32(uint32_t h) {
return h;
}
uint16_t MCU_be16toh(uint16_t h) {
return bswap16(h);
}
uint32_t MCU_be32toh(uint32_t h) {
return bswap32(h);
}
uint16_t MCU_le16toh(uint16_t h) {
return h;
}
uint32_t MCU_le32toh(uint32_t h) {
return h;
}
#endif /* defined(ARDUINO) */