|
|
|
@ -26,8 +26,8 @@
|
|
|
|
|
|
|
|
|
|
#include "mpconfigport.h"
|
|
|
|
|
|
|
|
|
|
#include "py/nlr.h"
|
|
|
|
|
#include "py/obj.h"
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
@ -41,15 +41,13 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
|
|
|
|
|
const mcu_pin_obj_t * miso) {
|
|
|
|
|
digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->clock, clock);
|
|
|
|
|
if (result != DIGITALINOUT_OK) {
|
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
|
|
|
|
"Clock pin init failed."));
|
|
|
|
|
mp_raise_ValueError("Clock pin init failed.");
|
|
|
|
|
}
|
|
|
|
|
if (mosi != mp_const_none) {
|
|
|
|
|
result = common_hal_digitalio_digitalinout_construct(&self->mosi, mosi);
|
|
|
|
|
if (result != DIGITALINOUT_OK) {
|
|
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->clock);
|
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
|
|
|
|
"MOSI pin init failed."));
|
|
|
|
|
mp_raise_ValueError("MOSI pin init failed.");
|
|
|
|
|
}
|
|
|
|
|
self->has_mosi = true;
|
|
|
|
|
}
|
|
|
|
@ -60,8 +58,7 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
|
|
|
|
|
if (mosi != mp_const_none) {
|
|
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->mosi);
|
|
|
|
|
}
|
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
|
|
|
|
"MISO pin init failed."));
|
|
|
|
|
mp_raise_ValueError("MISO pin init failed.");
|
|
|
|
|
}
|
|
|
|
|
self->has_miso = true;
|
|
|
|
|
}
|
|
|
|
@ -121,8 +118,7 @@ void shared_module_bitbangio_spi_unlock(bitbangio_spi_obj_t *self) {
|
|
|
|
|
// 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) {
|
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
|
|
|
|
"Cannot write without MOSI pin."));
|
|
|
|
|
mp_raise_ValueError("Cannot write without MOSI pin.");
|
|
|
|
|
}
|
|
|
|
|
uint32_t delay_half = self->delay_half;
|
|
|
|
|
|
|
|
|
@ -177,8 +173,7 @@ bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t
|
|
|
|
|
// Reads in len bytes while outputting zeroes.
|
|
|
|
|
bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len) {
|
|
|
|
|
if (len > 0 && !self->has_miso) {
|
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
|
|
|
|
"Cannot read without MISO pin."));
|
|
|
|
|
mp_raise_ValueError("Cannot read without MISO pin.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t delay_half = self->delay_half;
|
|
|
|
@ -242,8 +237,7 @@ bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data,
|
|
|
|
|
// 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) ) {
|
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
|
|
|
|
"Cannot transfer without MOSI and MISO pins."));
|
|
|
|
|
mp_raise_ValueError("Cannot transfer without MOSI and MISO pins.");
|
|
|
|
|
}
|
|
|
|
|
uint32_t delay_half = self->delay_half;
|
|
|
|
|
|
|
|
|
|