2016-11-03 18:50:59 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2016-11-03 18:50:59 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "py/mpconfig.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "py/obj.h"
|
2018-07-31 16:39:12 -04:00
|
|
|
#include "py/runtime.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-12 18:24:50 -04:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2021-03-02 15:16:55 -05:00
|
|
|
#include "shared-bindings/bitbangio/SPI.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2021-03-02 15:16:55 -05:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
#define MAX_BAUDRATE (common_hal_mcu_get_clock_frequency() / 48)
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
|
|
|
|
const mcu_pin_obj_t *miso) {
|
2017-04-10 16:32:19 -04:00
|
|
|
digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->clock, clock);
|
2016-11-03 18:50:59 -04:00
|
|
|
if (result != DIGITALINOUT_OK) {
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_raise_ValueError_varg(translate("%q init failed"), MP_QSTR_clock);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2019-10-26 16:06:02 -04:00
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->clock, self->polarity == 1, DRIVE_MODE_PUSH_PULL);
|
|
|
|
|
2020-02-28 23:32:24 -05:00
|
|
|
if (mosi != NULL) {
|
2017-04-10 16:32:19 -04:00
|
|
|
result = common_hal_digitalio_digitalinout_construct(&self->mosi, mosi);
|
2016-11-29 19:54:20 -05:00
|
|
|
if (result != DIGITALINOUT_OK) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->clock);
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_raise_ValueError_varg(translate("%q init failed"), MP_QSTR_mosi);
|
2016-11-29 19:54:20 -05:00
|
|
|
}
|
|
|
|
self->has_mosi = true;
|
2019-10-26 16:06:02 -04:00
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->mosi, false, DRIVE_MODE_PUSH_PULL);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2019-10-26 16:06:02 -04:00
|
|
|
|
2020-02-28 23:32:24 -05:00
|
|
|
if (miso != NULL) {
|
2019-10-26 16:06:02 -04:00
|
|
|
// Starts out as input by default, no need to change.
|
2017-04-10 16:32:19 -04:00
|
|
|
result = common_hal_digitalio_digitalinout_construct(&self->miso, miso);
|
2016-11-29 19:54:20 -05:00
|
|
|
if (result != DIGITALINOUT_OK) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->clock);
|
2020-02-28 23:32:24 -05:00
|
|
|
if (mosi != NULL) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->mosi);
|
2016-11-29 19:54:20 -05:00
|
|
|
}
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_raise_ValueError_varg(translate("%q init failed"), MP_QSTR_miso);
|
2016-11-29 19:54:20 -05:00
|
|
|
}
|
|
|
|
self->has_miso = true;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2016-11-30 18:08:34 -05:00
|
|
|
self->delay_half = 5;
|
2016-11-03 18:50:59 -04:00
|
|
|
self->polarity = 0;
|
|
|
|
self->phase = 0;
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:49:40 -04:00
|
|
|
bool shared_module_bitbangio_spi_deinited(bitbangio_spi_obj_t *self) {
|
|
|
|
return common_hal_digitalio_digitalinout_deinited(&self->clock);
|
|
|
|
}
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self) {
|
2017-10-02 20:49:40 -04:00
|
|
|
if (shared_module_bitbangio_spi_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->clock);
|
2016-11-29 19:54:20 -05:00
|
|
|
if (self->has_mosi) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->mosi);
|
2016-11-29 19:54:20 -05:00
|
|
|
}
|
|
|
|
if (self->has_miso) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->miso);
|
2016-11-29 19:54:20 -05:00
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
void shared_module_bitbangio_spi_configure(bitbangio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
|
2016-11-30 18:08:34 -05:00
|
|
|
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;
|
2016-11-29 19:54:20 -05:00
|
|
|
}
|
2016-11-30 18:08:34 -05:00
|
|
|
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) {
|
2023-07-20 14:16:31 -04:00
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_mosi);
|
2016-11-29 19:54:20 -05:00
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
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) {
|
2016-11-30 18:08:34 -05:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
uint8_t data_out = data[i];
|
2016-11-03 18:50:59 -04:00
|
|
|
for (int j = 0; j < 8; ++j, data_out <<= 1) {
|
2017-04-10 16:32:19 -04:00
|
|
|
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);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
if (dest != NULL) {
|
|
|
|
dest[i] = data_in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
uint8_t data_out = data[i];
|
2016-11-03 18:50:59 -04:00
|
|
|
for (int j = 0; j < 8; ++j, data_out <<= 1) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
|
2016-11-03 18:50:59 -04:00
|
|
|
if (self->phase == 0) {
|
|
|
|
common_hal_mcu_delay_us(delay_half);
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
|
2016-11-03 18:50:59 -04:00
|
|
|
common_hal_mcu_delay_us(delay_half);
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
|
2016-11-03 18:50:59 -04:00
|
|
|
} else {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
|
2016-12-09 22:27:41 -05:00
|
|
|
common_hal_mcu_delay_us(delay_half);
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
|
2016-11-03 18:50:59 -04:00
|
|
|
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
|
|
|
|
}
|
2016-11-30 18:08:34 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads in len bytes while outputting zeroes.
|
2021-03-26 13:06:25 -04:00
|
|
|
bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_data) {
|
2016-11-30 18:08:34 -05:00
|
|
|
if (len > 0 && !self->has_miso) {
|
2023-07-20 14:16:31 -04:00
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_miso);
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->mosi, false);
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
uint8_t data_in = 0;
|
|
|
|
for (int j = 0; j < 8; ++j, data_out <<= 1) {
|
2017-04-10 16:32:19 -04:00
|
|
|
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);
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
data[i] = data_in;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2016-11-29 19:54:20 -05:00
|
|
|
if (self->has_mosi) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->mosi, false);
|
2016-11-29 19:54:20 -05:00
|
|
|
}
|
2016-11-30 18:08:34 -05:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2021-03-26 13:06:25 -04:00
|
|
|
uint8_t data_out = write_data;
|
2016-11-03 18:50:59 -04:00
|
|
|
uint8_t data_in = 0;
|
2021-03-26 13:06:25 -04:00
|
|
|
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);
|
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
if (self->phase == 0) {
|
|
|
|
common_hal_mcu_delay_us(delay_half);
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
|
2016-11-03 18:50:59 -04:00
|
|
|
} else {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
|
2016-11-03 18:50:59 -04:00
|
|
|
common_hal_mcu_delay_us(delay_half);
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso);
|
2016-11-03 18:50:59 -04:00
|
|
|
if (self->phase == 0) {
|
|
|
|
common_hal_mcu_delay_us(delay_half);
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
|
2016-11-03 18:50:59 -04:00
|
|
|
} else {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
|
2016-11-03 18:50:59 -04:00
|
|
|
common_hal_mcu_delay_us(delay_half);
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 18:08:34 -05:00
|
|
|
data[i] = data_in;
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2018-01-30 17:18:57 -05:00
|
|
|
|
|
|
|
// transfer
|
|
|
|
bool shared_module_bitbangio_spi_transfer(bitbangio_spi_obj_t *self, const uint8_t *dout, uint8_t *din, size_t len) {
|
2023-07-20 14:16:31 -04:00
|
|
|
if (!self->has_mosi && dout != NULL) {
|
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_mosi);
|
|
|
|
}
|
|
|
|
if (!self->has_miso && din != NULL) {
|
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_miso);
|
2018-01-30 17:18:57 -05:00
|
|
|
}
|
|
|
|
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;
|
2018-02-05 10:44:29 -05:00
|
|
|
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;
|
2018-01-30 17:18:57 -05:00
|
|
|
|
|
|
|
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) {
|
2018-02-05 10:44:29 -05:00
|
|
|
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);
|
|
|
|
}
|
2018-01-30 17:18:57 -05:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|