forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathSPI.c
309 lines (280 loc) · 12.3 KB
/
SPI.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
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/mpconfig.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/bitbangio/SPI.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "supervisor/shared/translate.h"
#define MAX_BAUDRATE (common_hal_mcu_get_clock_frequency() / 48)
void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
const mcu_pin_obj_t *miso) {
digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->clock, clock);
if (result != DIGITALINOUT_OK) {
mp_raise_ValueError(translate("Clock pin init failed."));
}
common_hal_digitalio_digitalinout_switch_to_output(&self->clock, self->polarity == 1, DRIVE_MODE_PUSH_PULL);
if (mosi != NULL) {
result = common_hal_digitalio_digitalinout_construct(&self->mosi, mosi);
if (result != DIGITALINOUT_OK) {
common_hal_digitalio_digitalinout_deinit(&self->clock);
mp_raise_ValueError(translate("MOSI pin init failed."));
}
self->has_mosi = true;
common_hal_digitalio_digitalinout_switch_to_output(&self->mosi, false, DRIVE_MODE_PUSH_PULL);
}
if (miso != NULL) {
// Starts out as input by default, no need to change.
result = common_hal_digitalio_digitalinout_construct(&self->miso, miso);
if (result != DIGITALINOUT_OK) {
common_hal_digitalio_digitalinout_deinit(&self->clock);
if (mosi != NULL) {
common_hal_digitalio_digitalinout_deinit(&self->mosi);
}
mp_raise_ValueError(translate("MISO pin init failed."));
}
self->has_miso = true;
}
self->delay_half = 5;
self->polarity = 0;
self->phase = 0;
}
bool shared_module_bitbangio_spi_deinited(bitbangio_spi_obj_t *self) {
return common_hal_digitalio_digitalinout_deinited(&self->clock);
}
void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self) {
if (shared_module_bitbangio_spi_deinited(self)) {
return;
}
common_hal_digitalio_digitalinout_deinit(&self->clock);
if (self->has_mosi) {
common_hal_digitalio_digitalinout_deinit(&self->mosi);
}
if (self->has_miso) {
common_hal_digitalio_digitalinout_deinit(&self->miso);
}
}
void shared_module_bitbangio_spi_configure(bitbangio_spi_obj_t *self,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
self->delay_half = 500000 / baudrate;
// round delay_half up so that: actual_baudrate <= requested_baudrate
if (500000 % baudrate != 0) {
self->delay_half += 1;
}
self->polarity = polarity;
self->phase = phase;
}
bool shared_module_bitbangio_spi_try_lock(bitbangio_spi_obj_t *self) {
bool success = false;
common_hal_mcu_disable_interrupts();
if (!self->locked) {
self->locked = true;
success = true;
}
common_hal_mcu_enable_interrupts();
return success;
}
bool shared_module_bitbangio_spi_has_lock(bitbangio_spi_obj_t *self) {
return self->locked;
}
void shared_module_bitbangio_spi_unlock(bitbangio_spi_obj_t *self) {
self->locked = false;
}
// Writes out the given data.
bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t *data, size_t len) {
if (len > 0 && !self->has_mosi) {
mp_raise_ValueError(translate("Cannot write without MOSI pin."));
}
uint32_t delay_half = self->delay_half;
// only MSB transfer is implemented
// If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY, and the configured
// delay_half is equal to this value, then the software SPI implementation
// will run as fast as possible, limited only by CPU speed and GPIO time.
#ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
if (delay_half <= MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
for (size_t i = 0; i < len; ++i) {
uint8_t data_out = data[i];
for (int j = 0; j < 8; ++j, data_out <<= 1) {
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
}
if (dest != NULL) {
dest[i] = data_in;
}
}
return true;
}
#endif
for (size_t i = 0; i < len; ++i) {
uint8_t data_out = data[i];
for (int j = 0; j < 8; ++j, data_out <<= 1) {
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
if (self->phase == 0) {
common_hal_mcu_delay_us(delay_half);
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
common_hal_mcu_delay_us(delay_half);
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
} else {
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
common_hal_mcu_delay_us(delay_half);
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
common_hal_mcu_delay_us(delay_half);
}
}
// Some ports need a regular callback, but probably we don't need
// to do this every byte, or even at all.
#ifdef MICROPY_EVENT_POLL_HOOK
MICROPY_EVENT_POLL_HOOK;
#endif
}
return true;
}
// Reads in len bytes while outputting zeroes.
bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_data) {
if (len > 0 && !self->has_miso) {
mp_raise_ValueError(translate("Cannot read without MISO pin."));
}
uint32_t delay_half = self->delay_half;
// only MSB transfer is implemented
// If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY, and the configured
// delay_half is equal to this value, then the software SPI implementation
// will run as fast as possible, limited only by CPU speed and GPIO time.
#ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
if (delay_half <= MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
// Clock out zeroes while we read.
if (self->has_mosi) {
common_hal_digitalio_digitalinout_set_value(&self->mosi, false);
}
for (size_t i = 0; i < len; ++i) {
uint8_t data_in = 0;
for (int j = 0; j < 8; ++j, data_out <<= 1) {
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso);
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
}
data[i] = data_in;
}
return true;
}
#endif
if (self->has_mosi) {
common_hal_digitalio_digitalinout_set_value(&self->mosi, false);
}
for (size_t i = 0; i < len; ++i) {
uint8_t data_out = write_data;
uint8_t data_in = 0;
for (int j = 0; j < 8; ++j, data_out <<= 1) {
if (self->has_mosi) {
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
}
if (self->phase == 0) {
common_hal_mcu_delay_us(delay_half);
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
} else {
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
common_hal_mcu_delay_us(delay_half);
}
data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso);
if (self->phase == 0) {
common_hal_mcu_delay_us(delay_half);
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
} else {
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
common_hal_mcu_delay_us(delay_half);
}
}
data[i] = data_in;
// Some ports need a regular callback, but probably we don't need
// to do this every byte, or even at all.
#ifdef MICROPY_EVENT_POLL_HOOK
MICROPY_EVENT_POLL_HOOK;
#endif
}
return true;
}
// transfer
bool shared_module_bitbangio_spi_transfer(bitbangio_spi_obj_t *self, const uint8_t *dout, uint8_t *din, size_t len) {
if (len > 0 && (!self->has_mosi || !self->has_miso)) {
mp_raise_ValueError(translate("Cannot transfer without MOSI and MISO pins."));
}
uint32_t delay_half = self->delay_half;
// only MSB transfer is implemented
// If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY, and the configured
// delay_half is equal to this value, then the software SPI implementation
// will run as fast as possible, limited only by CPU speed and GPIO time.
#ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
if (delay_half <= MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
for (size_t i = 0; i < len; ++i) {
uint8_t data_out = dout[i];
uint8_t data_in = 0;
for (int j = 0; j < 8; ++j, data_out <<= 1) {
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso);
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
}
din[i] = data_in;
if (dest != NULL) {
dest[i] = data_in;
}
}
return true;
}
#endif
for (size_t i = 0; i < len; ++i) {
uint8_t data_out = dout[i];
uint8_t data_in = 0;
for (int j = 0; j < 8; ++j, data_out <<= 1) {
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
if (self->phase == 0) {
common_hal_mcu_delay_us(delay_half);
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
} else {
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
common_hal_mcu_delay_us(delay_half);
}
data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso);
if (self->phase == 0) {
common_hal_mcu_delay_us(delay_half);
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
} else {
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
common_hal_mcu_delay_us(delay_half);
}
}
din[i] = data_in;
// Some ports need a regular callback, but probably we don't need
// to do this every byte, or even at all.
#ifdef MICROPY_EVENT_POLL_HOOK
MICROPY_EVENT_POLL_HOOK;
#endif
}
return true;
}