forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 171
/
machine_spi.c
210 lines (173 loc) · 7.33 KB
/
machine_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
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright 2021 NXP
*
* 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 <stdio.h>
#include <stdint.h>
#include <string.h>
#include <zephyr/zephyr.h>
#include <zephyr/drivers/spi.h>
#include "py/runtime.h"
#include "py/gc.h"
#include "py/mphal.h"
#include "py/mperrno.h"
#include "extmod/machine_spi.h"
#include "modmachine.h"
#if MICROPY_PY_MACHINE_SPI
#define DEFAULT_SPI_BAUDRATE (50000)
#define DEFAULT_SPI_POLARITY (0)
#define DEFAULT_SPI_PHASE (0)
#define DEFAULT_SPI_BITS (8)
#define DEFAULT_SPI_FIRSTBIT (SPI_TRANSFER_MSB)
#define SPI_LOOP (0) // For testing, enable loop mode by setting SPI_LOOP (1)
typedef struct _machine_hard_spi_obj_t {
mp_obj_base_t base;
const struct device *dev;
struct spi_config config;
} machine_hard_spi_obj_t;
STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_hard_spi_obj_t *self = self_in;
mp_printf(print, "SPI(%s, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%s)",
self->dev->name,
self->config.frequency,
(self->config.operation & 0x2) >> 1,
(self->config.operation & 0x4) >> 2,
(self->config.operation & ~0x1F) >> 5,
((self->config.operation & 0x10) >> 4) == SPI_TRANSFER_MSB ? "MSB" : "LSB");
}
mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum {ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso};
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = DEFAULT_SPI_BAUDRATE} },
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_POLARITY} },
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_PHASE} },
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_BITS} },
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_FIRSTBIT} },
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
const char *dev_name = mp_obj_str_get_str(args[ARG_id].u_obj);
const struct device *dev = device_get_binding(dev_name);
if (dev == NULL) {
mp_raise_ValueError(MP_ERROR_TEXT("device not found"));
}
if ((args[ARG_sck].u_obj != MP_OBJ_NULL) || (args[ARG_miso].u_obj != MP_OBJ_NULL) || (args[ARG_mosi].u_obj != MP_OBJ_NULL)) {
mp_raise_NotImplementedError(MP_ERROR_TEXT("explicit choice of sck/miso/mosi is not implemented"));
}
struct spi_config cfg = {
.frequency = args[ARG_baudrate].u_int,
.operation = (SPI_OP_MODE_MASTER |
args[ARG_polarity].u_int << 1 |
args[ARG_phase].u_int << 2 |
SPI_LOOP << 3 |
args[ARG_firstbit].u_int << 4 |
args[ARG_bits].u_int << 5 |
SPI_LINES_SINGLE),
.slave = 0,
.cs = NULL
};
machine_hard_spi_obj_t *self = mp_obj_malloc(machine_hard_spi_obj_t, &machine_spi_type);
self->dev = dev;
self->config = cfg;
return MP_OBJ_FROM_PTR(self);
}
STATIC void machine_hard_spi_init(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit};
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t *)obj;
uint32_t baudrate;
uint16_t operation = self->config.operation;
if (args[ARG_baudrate].u_int != -1) {
baudrate = args[ARG_baudrate].u_int;
} else {
baudrate = self->config.frequency;
}
if (args[ARG_polarity].u_int != -1) {
operation = (operation & ~0x2) | (args[ARG_polarity].u_int << 1);
}
if (args[ARG_phase].u_int != -1) {
operation = (operation & ~0x4) | (args[ARG_phase].u_int << 2);
}
if (args[ARG_bits].u_int != -1) {
operation = (operation & 0x1F) | (args[ARG_bits].u_int << 5);
}
if (args[ARG_firstbit].u_int != -1) {
operation = (operation & ~0x10) | (args[ARG_firstbit].u_int << 4);
}
struct spi_config cfg = {
.frequency = baudrate,
.operation = operation,
.slave = 0,
.cs = NULL
};
self->config = cfg;
}
STATIC void machine_hard_spi_transfer(mp_obj_base_t *obj, size_t len, const uint8_t *src, uint8_t *dest) {
machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t *)obj;
int ret;
struct spi_buf tx_bufs[1];
tx_bufs[0].buf = (uint8_t *)src;
tx_bufs[0].len = len;
const struct spi_buf_set tx = {
.buffers = tx_bufs,
.count = ARRAY_SIZE(tx_bufs)
};
struct spi_buf rx_bufs[1];
rx_bufs[0].buf = dest;
rx_bufs[0].len = len;
const struct spi_buf_set rx = {
.buffers = rx_bufs,
.count = ARRAY_SIZE(rx_bufs)
};
ret = spi_transceive(self->dev, &self->config, &tx, &rx);
if (ret < 0) {
mp_raise_OSError(-ret);
}
}
STATIC const mp_machine_spi_p_t machine_hard_spi_p = {
.init = machine_hard_spi_init,
.transfer = machine_hard_spi_transfer,
};
MP_DEFINE_CONST_OBJ_TYPE(
machine_spi_type,
MP_QSTR_SPI,
MP_TYPE_FLAG_NONE,
make_new, machine_hard_spi_make_new,
print, machine_hard_spi_print,
protocol, &machine_hard_spi_p,
locals_dict, &mp_machine_spi_locals_dict
);
#endif // MICROPY_PY_MACHINE_SPI