Skip to content

Commit 22d9a94

Browse files
committed
Use write_value, add missing pin exceptions
1 parent 8eda917 commit 22d9a94

File tree

1 file changed

+44
-5
lines changed
  • ports/esp32s2/common-hal/busio

1 file changed

+44
-5
lines changed

ports/esp32s2/common-hal/busio/SPI.c

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ static void spi_bus_intr_disable(void *self)
115115
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
116116
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
117117
const mcu_pin_obj_t * miso) {
118+
119+
//SCK is not optional. MOSI and MISO are
120+
if (!clock) {
121+
mp_raise_ValueError(translate("Must provide SCK pin"));
122+
}
123+
124+
if (!miso && !mosi) {
125+
mp_raise_ValueError(translate("Must provide MISO or MOSI pin"));
126+
}
127+
118128
spi_bus_config_t bus_config;
119129
bus_config.mosi_io_num = mosi != NULL ? mosi->number : -1;
120130
bus_config.miso_io_num = miso != NULL ? miso->number : -1;
@@ -212,8 +222,12 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
212222
spi_never_reset[self->host_id] = true;
213223

214224
common_hal_never_reset_pin(self->clock_pin);
215-
common_hal_never_reset_pin(self->MOSI_pin);
216-
common_hal_never_reset_pin(self->MISO_pin);
225+
if (self->MOSI_pin != NULL) {
226+
common_hal_never_reset_pin(self->MOSI_pin);
227+
}
228+
if (self->MISO_pin != NULL) {
229+
common_hal_never_reset_pin(self->MISO_pin);
230+
}
217231
}
218232

219233
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
@@ -236,9 +250,15 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
236250
spi_bus_free(self->host_id);
237251

238252
common_hal_reset_pin(self->clock_pin);
239-
common_hal_reset_pin(self->MOSI_pin);
240-
common_hal_reset_pin(self->MISO_pin);
253+
if (self->MOSI_pin != NULL) {
254+
common_hal_reset_pin(self->MOSI_pin);
255+
}
256+
if (self->MISO_pin != NULL) {
257+
common_hal_reset_pin(self->MISO_pin);
258+
}
241259
self->clock_pin = NULL;
260+
self->MISO_pin = NULL;
261+
self->MOSI_pin = NULL;
242262
}
243263

244264
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
@@ -293,18 +313,37 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
293313

294314
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
295315
const uint8_t *data, size_t len) {
316+
if (self->MOSI_pin == NULL) {
317+
mp_raise_ValueError(translate("No MOSI Pin"));
318+
}
296319
return common_hal_busio_spi_transfer(self, data, NULL, len);
297320
}
298321

299322
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
300323
uint8_t *data, size_t len, uint8_t write_value) {
301-
return common_hal_busio_spi_transfer(self, NULL, data, len);
324+
325+
if (self->MISO_pin == NULL) {
326+
mp_raise_ValueError(translate("No MISO Pin"));
327+
}
328+
if (self->MOSI_pin == NULL) {
329+
return common_hal_busio_spi_transfer(self, NULL, data, len);
330+
} else {
331+
memset(data, write_value, len);
332+
return common_hal_busio_spi_transfer(self, data, data, len);
333+
}
302334
}
303335

304336
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_out, uint8_t *data_in, size_t len) {
305337
if (len == 0) {
306338
return true;
307339
}
340+
// Other than the read special case, stop transfers that don't have a pin/array match
341+
if (!self->MOSI_pin && (data_out != data_in)) {
342+
mp_raise_ValueError(translate("No MOSI Pin"));
343+
}
344+
if (!self->MISO_pin && data_in) {
345+
mp_raise_ValueError(translate("No MISO Pin"));
346+
}
308347

309348
spi_hal_context_t* hal = &self->hal_context;
310349
hal->send_buffer = NULL;

0 commit comments

Comments
 (0)