-
Notifications
You must be signed in to change notification settings - Fork 0
/
monocle-drivers.c
316 lines (267 loc) · 8.47 KB
/
monocle-drivers.c
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* This file is part of the MicroPython for Monocle project:
* https://github.com/brilliantlabsAR/monocle-micropython
*
* Authored by: Josuah Demangeon (me@josuah.net)
* Raj Nakarja / Brilliant Labs Ltd. (raj@itsbrilliant.co)
*
* ISC Licence
*
* Copyright © 2023 Brilliant Labs Ltd.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "monocle.h"
#include "py/mphal.h"
#include "py/runtime.h"
#include "nrf_gpio.h"
#include "nrfx_spim.h"
#include "nrfx_twim.h"
#include "nrfx_systick.h"
nrfx_twim_t i2c_bus = NRFX_TWIM_INSTANCE(0);
nrfx_spim_t spi_bus = NRFX_SPIM_INSTANCE(2);
void monocle_set_led(led_t led, bool enable)
{
switch (led)
{
case RED_LED:
nrf_gpio_pin_write(LED_1_PIN, !enable);
break;
case GREEN_LED:
nrf_gpio_pin_write(LED_2_PIN, !enable);
break;
}
}
i2c_response_t monocle_i2c_read(uint8_t device_address_7bit,
uint16_t register_address,
uint8_t register_mask)
{
i2c_response_t resp = {.fail = false, .value = 0x00};
// Create the tx payload, bus handle and transfer descriptors
uint8_t tx_payload[2] = {(uint8_t)(register_address), 0};
nrfx_twim_xfer_desc_t i2c_tx = NRFX_TWIM_XFER_DESC_TX(device_address_7bit,
tx_payload,
1);
// Switch bus and use 16-bit addressing if the camera is requested
if (device_address_7bit == CAMERA_I2C_ADDRESS)
{
tx_payload[0] = (uint8_t)(register_address >> 8);
tx_payload[1] = (uint8_t)register_address;
i2c_tx.primary_length = 2;
}
nrfx_twim_xfer_desc_t i2c_rx = NRFX_TWIM_XFER_DESC_RX(device_address_7bit,
&resp.value,
1);
// Try several times
for (uint8_t i = 0; i < 3; i++)
{
nrfx_err_t tx_err = nrfx_twim_xfer(&i2c_bus, &i2c_tx, 0);
if (tx_err == NRFX_ERROR_BUSY ||
tx_err == NRFX_ERROR_NOT_SUPPORTED ||
tx_err == NRFX_ERROR_INTERNAL ||
tx_err == NRFX_ERROR_INVALID_ADDR ||
tx_err == NRFX_ERROR_DRV_TWI_ERR_OVERRUN)
{
app_err(tx_err);
}
nrfx_err_t rx_err = nrfx_twim_xfer(&i2c_bus, &i2c_rx, 0);
if (rx_err == NRFX_ERROR_BUSY ||
rx_err == NRFX_ERROR_NOT_SUPPORTED ||
rx_err == NRFX_ERROR_INTERNAL ||
rx_err == NRFX_ERROR_INVALID_ADDR ||
rx_err == NRFX_ERROR_DRV_TWI_ERR_OVERRUN)
{
app_err(rx_err);
}
if (tx_err == NRFX_SUCCESS && rx_err == NRFX_SUCCESS)
{
resp.fail = false;
break;
}
}
resp.value &= register_mask;
return resp;
}
i2c_response_t monocle_i2c_write(uint8_t device_address_7bit,
uint16_t register_address,
uint8_t register_mask,
uint8_t set_value)
{
i2c_response_t resp = {.fail = false, .value = 0x00};
if (register_mask != 0xFF)
{
resp = monocle_i2c_read(device_address_7bit, register_address, 0xFF);
if (resp.fail)
{
return resp;
}
}
// Create a combined value with the existing data and the new value
uint8_t updated_value = (resp.value & ~register_mask) |
(set_value & register_mask);
// Create the tx payload, bus handle and transfer descriptor
uint8_t tx_payload[3] = {(uint8_t)register_address, updated_value, 0};
nrfx_twim_xfer_desc_t i2c_tx = NRFX_TWIM_XFER_DESC_TX(device_address_7bit,
tx_payload,
2);
// Switch bus and use 16-bit addressing if the camera is requested
if (device_address_7bit == CAMERA_I2C_ADDRESS)
{
tx_payload[0] = (uint8_t)(register_address >> 8);
tx_payload[1] = (uint8_t)register_address;
tx_payload[2] = updated_value;
i2c_tx.primary_length = 3;
}
// Try several times
for (uint8_t i = 0; i < 3; i++)
{
nrfx_err_t err = nrfx_twim_xfer(&i2c_bus, &i2c_tx, 0);
if (err == NRFX_ERROR_BUSY ||
err == NRFX_ERROR_NOT_SUPPORTED ||
err == NRFX_ERROR_INTERNAL ||
err == NRFX_ERROR_INVALID_ADDR ||
err == NRFX_ERROR_DRV_TWI_ERR_OVERRUN)
{
app_err(err);
}
if (err == NRFX_SUCCESS)
{
break;
}
// If the last try failed. Don't continue
if (i == 2)
{
resp.fail = true;
return resp;
}
}
return resp;
}
void monocle_spi_enable(bool enable)
{
if (enable == false)
{
nrfx_spim_uninit(&spi_bus);
return;
}
nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG(
FPGA_FLASH_SPI_SCK_PIN,
FPGA_FLASH_SPI_SDO_PIN,
FPGA_FLASH_SPI_SDI_PIN,
NRFX_SPIM_PIN_NOT_USED);
config.frequency = NRF_SPIM_FREQ_4M;
config.mode = NRF_SPIM_MODE_3;
config.bit_order = NRF_SPIM_BIT_ORDER_LSB_FIRST;
app_err(nrfx_spim_init(&spi_bus, &config, NULL, NULL));
}
static uint8_t bit_reverse(uint8_t byte)
{
byte = (byte & 0xF0) >> 4 | (byte & 0x0F) << 4;
byte = (byte & 0xCC) >> 2 | (byte & 0x33) << 2;
byte = (byte & 0xAA) >> 1 | (byte & 0x55) << 1;
return byte;
}
void monocle_spi_read(spi_device_t spi_device, uint8_t *data, size_t length,
bool hold_down_cs)
{
return;
uint8_t cs_pin;
switch (spi_device)
{
case DISPLAY:
cs_pin = DISPLAY_CS_PIN;
break;
case FPGA:
cs_pin = FPGA_CS_MODE_PIN;
break;
case FLASH:
cs_pin = FLASH_CS_PIN;
break;
}
nrf_gpio_pin_clear(cs_pin);
// TODO prevent blocking here
nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_RX(data, length);
app_err(nrfx_spim_xfer(&spi_bus, &xfer, 0));
if (!hold_down_cs)
{
nrf_gpio_pin_set(cs_pin);
}
// Flash is LSB first, so we need to flip all the bytes before returning
if (spi_device == FLASH)
{
for (size_t i = 0; i < length; i++)
{
data[i] = bit_reverse(data[i]);
}
}
}
void monocle_spi_write(spi_device_t spi_device, uint8_t *data, size_t length,
bool hold_down_cs)
{
return;
uint8_t cs_pin;
switch (spi_device)
{
case DISPLAY:
cs_pin = DISPLAY_CS_PIN;
break;
case FPGA:
cs_pin = FPGA_CS_MODE_PIN;
break;
case FLASH:
cs_pin = FLASH_CS_PIN;
break;
}
nrf_gpio_pin_clear(cs_pin);
// Flash is LSB first, so we need to flip all the bytes before sending
if (spi_device == FLASH)
{
for (size_t i = 0; i < length; i++)
{
data[i] = bit_reverse(data[i]);
}
}
// TODO prevent blocking here
if (!nrfx_is_in_ram(data))
{
uint8_t *m_data = m_malloc(length);
memcpy(m_data, data, length);
nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_TX(m_data, length);
app_err(nrfx_spim_xfer(&spi_bus, &xfer, 0));
m_free(m_data);
}
else
{
nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_TX(data, length);
app_err(nrfx_spim_xfer(&spi_bus, &xfer, 0));
}
if (!hold_down_cs)
{
nrf_gpio_pin_set(cs_pin);
}
}
void monocle_flash_read(uint8_t *buffer, size_t address, size_t length)
{
memset(buffer, '\xFF', length);
(void)address;
}
void monocle_flash_write(uint8_t *buffer, size_t address, size_t length)
{
(void) buffer;
(void) address;
(void) length;
}
void monocle_flash_page_erase(size_t address)
{
(void)address;
}