35
35
#include "extmod/machine_spi.h"
36
36
#include "machine_hspi.h"
37
37
#include "modmachine.h"
38
- #include "hspi.h"
38
+
39
39
40
40
// if a port didn't define MSB/LSB constants then provide them
41
41
#ifndef MICROPY_PY_MACHINE_SPI_MSB
@@ -55,11 +55,12 @@ STATIC void machine_hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint
55
55
struct spi_transaction_t transaction = {
56
56
.flags = 0 ,
57
57
.length = bits_to_send ,
58
- .tx_buffer = src ,
59
- .rx_buffer = dest ,
58
+ .tx_buffer = NULL ,
59
+ .rx_buffer = NULL ,
60
60
};
61
61
bool shortMsg = len <= 4 ;
62
62
63
+
63
64
if (shortMsg ) {
64
65
if (src != NULL ) {
65
66
memcpy (& transaction .tx_data , src , len );
@@ -68,6 +69,9 @@ STATIC void machine_hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint
68
69
if (dest != NULL ) {
69
70
transaction .flags |= SPI_TRANS_USE_RXDATA ;
70
71
}
72
+ } else {
73
+ transaction .tx_buffer = src ;
74
+ transaction .rx_buffer = dest ;
71
75
}
72
76
73
77
spi_device_transmit (self -> spi , & transaction );
@@ -108,7 +112,12 @@ STATIC void machine_hspi_init(mp_obj_base_t *self_in, size_t n_args, const mp_ob
108
112
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ),
109
113
allowed_args , args );
110
114
111
- self -> host = args [ARG_id ].u_int ;
115
+ int host = args [ARG_id ].u_int ;
116
+ if (host != HSPI_HOST && host != VSPI_HOST ) {
117
+ mp_raise_ValueError ("SPI ID must be either HSPI(1) or VSPI(2)" );
118
+ }
119
+
120
+ self -> host = host ;
112
121
self -> baudrate = args [ARG_baudrate ].u_int ;
113
122
self -> polarity = args [ARG_polarity ].u_int ? 1 : 0 ;
114
123
self -> phase = args [ARG_phase ].u_int ? 1 : 0 ;
@@ -131,14 +140,15 @@ STATIC void machine_hspi_init(mp_obj_base_t *self_in, size_t n_args, const mp_ob
131
140
.mode = self -> phase | (self -> polarity << 1 ),
132
141
.spics_io_num = -1 , // No CS pin
133
142
.queue_size = 1 ,
143
+ .flags = self -> firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0 ,
134
144
.pre_cb = NULL
135
145
};
136
146
137
147
//Initialize the SPI bus
138
148
// FIXME: Does the DMA matter? There are two
139
- ret = spi_bus_initialize (self -> host , & buscfg , 1 );
149
+ ret = spi_bus_initialize (self -> host , & buscfg , 1 );
140
150
assert (ret == ESP_OK );
141
- ret = spi_bus_add_device (self -> host , & devcfg , & self -> spi );
151
+ ret = spi_bus_add_device (self -> host , & devcfg , & self -> spi );
142
152
assert (ret == ESP_OK );
143
153
}
144
154
@@ -156,11 +166,6 @@ STATIC void machine_hspi_deinit(mp_obj_base_t *self_in) {
156
166
157
167
mp_obj_t machine_hspi_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
158
168
// args[0] holds the id of the peripheral
159
- int host = mp_obj_get_int (args [0 ]);
160
- if (host != HSPI_HOST && host != VSPI_HOST ) {
161
- mp_raise_ValueError ("SPI ID must be either HSPI(1) or VSPI(2)" );
162
- }
163
-
164
169
machine_hspi_obj_t * self = m_new_obj (machine_hspi_obj_t );
165
170
self -> base .type = & machine_hspi_type ;
166
171
// set defaults
0 commit comments