2020-05-28 18:51:33 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2021-06-24 11:41:12 -04:00
|
|
|
* Copyright (c) 2021 microDev
|
2020-05-28 18:51:33 -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.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-05-28 18:51:33 -04:00
|
|
|
#include "py/runtime.h"
|
2021-06-24 11:41:12 -04:00
|
|
|
#include "shared-bindings/busio/SPI.h"
|
2020-05-28 18:51:33 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
#include "driver/spi_common_internal.h"
|
|
|
|
|
|
|
|
#define SPI_MAX_DMA_BITS (SPI_MAX_DMA_LEN * 8)
|
2020-05-28 18:51:33 -04:00
|
|
|
|
2020-06-17 16:45:21 -04:00
|
|
|
static bool spi_never_reset[SOC_SPI_PERIPH_NUM];
|
2021-06-24 11:41:12 -04:00
|
|
|
static spi_device_handle_t spi_handle[SOC_SPI_PERIPH_NUM];
|
2020-06-17 16:45:21 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
static bool spi_bus_is_free(spi_host_device_t host_id) {
|
|
|
|
return spi_bus_get_attr(host_id) == NULL;
|
|
|
|
}
|
2020-06-23 20:02:16 -04:00
|
|
|
|
2020-05-28 18:51:33 -04:00
|
|
|
void spi_reset(void) {
|
2020-06-17 16:45:21 -04:00
|
|
|
for (spi_host_device_t host_id = SPI2_HOST; host_id < SOC_SPI_PERIPH_NUM; host_id++) {
|
|
|
|
if (spi_never_reset[host_id]) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
if (!spi_bus_is_free(host_id)) {
|
|
|
|
spi_bus_remove_device(spi_handle[host_id]);
|
2020-06-23 20:02:16 -04:00
|
|
|
spi_bus_free(host_id);
|
|
|
|
}
|
2020-06-17 16:45:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 18:51:33 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
static void set_spi_config(busio_spi_obj_t *self,
|
|
|
|
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
|
|
|
|
const spi_device_interface_config_t device_config = {
|
|
|
|
.clock_speed_hz = baudrate,
|
|
|
|
.mode = phase | (polarity << 1),
|
|
|
|
.spics_io_num = -1, // No CS pin
|
|
|
|
.queue_size = 1,
|
|
|
|
.pre_cb = NULL
|
|
|
|
};
|
|
|
|
esp_err_t result = spi_bus_add_device(self->host_id, &device_config, &spi_handle[self->host_id]);
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
mp_raise_RuntimeError(translate("SPI configuration failed"));
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
self->baudrate = baudrate;
|
|
|
|
self->polarity = polarity;
|
|
|
|
self->phase = phase;
|
|
|
|
self->bits = bits;
|
2020-06-23 15:58:01 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 18:51:33 -04:00
|
|
|
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
|
2022-02-08 22:25:23 -05:00
|
|
|
const mcu_pin_obj_t *miso, bool half_duplex) {
|
2020-09-21 13:47:14 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
const spi_bus_config_t bus_config = {
|
|
|
|
.mosi_io_num = mosi != NULL ? mosi->number : -1,
|
|
|
|
.miso_io_num = miso != NULL ? miso->number : -1,
|
|
|
|
.sclk_io_num = clock != NULL ? clock->number : -1,
|
|
|
|
.quadwp_io_num = -1,
|
|
|
|
.quadhd_io_num = -1,
|
|
|
|
};
|
|
|
|
|
2022-02-08 23:13:22 -05:00
|
|
|
if (half_duplex) {
|
2022-02-08 22:25:23 -05:00
|
|
|
mp_raise_NotImplementedError(translate("Half duplex SPI is not implemented"));
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
for (spi_host_device_t host_id = SPI2_HOST; host_id < SOC_SPI_PERIPH_NUM; host_id++) {
|
|
|
|
if (spi_bus_is_free(host_id)) {
|
|
|
|
self->host_id = host_id;
|
|
|
|
}
|
2020-06-17 16:45:21 -04:00
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
|
|
|
|
if (self->host_id == 0) {
|
2020-06-17 16:45:21 -04:00
|
|
|
mp_raise_ValueError(translate("All SPI peripherals are in use"));
|
|
|
|
}
|
|
|
|
|
2021-12-04 09:56:42 -05:00
|
|
|
esp_err_t result = spi_bus_initialize(self->host_id, &bus_config, SPI_DMA_CH_AUTO);
|
2020-06-17 16:45:21 -04:00
|
|
|
if (result == ESP_ERR_NO_MEM) {
|
|
|
|
mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed"));
|
2020-06-23 15:58:01 -04:00
|
|
|
} else if (result == ESP_ERR_INVALID_ARG) {
|
2020-06-17 16:45:21 -04:00
|
|
|
mp_raise_ValueError(translate("Invalid pins"));
|
|
|
|
}
|
2020-06-23 20:02:16 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
set_spi_config(self, 250000, 0, 0, 8);
|
2020-06-17 16:45:21 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
self->MOSI = mosi;
|
|
|
|
self->MISO = miso;
|
|
|
|
self->clock = clock;
|
|
|
|
|
|
|
|
if (mosi != NULL) {
|
|
|
|
claim_pin(mosi);
|
2020-11-24 16:46:11 -05:00
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
if (miso != NULL) {
|
|
|
|
claim_pin(miso);
|
2020-11-24 16:46:11 -05:00
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
claim_pin(clock);
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
|
2020-06-17 16:45:21 -04:00
|
|
|
spi_never_reset[self->host_id] = true;
|
2021-06-24 11:41:12 -04:00
|
|
|
common_hal_never_reset_pin(self->clock);
|
2021-07-02 05:10:41 -04:00
|
|
|
if (self->MOSI != NULL) {
|
|
|
|
common_hal_never_reset_pin(self->MOSI);
|
|
|
|
}
|
|
|
|
if (self->MISO != NULL) {
|
|
|
|
common_hal_never_reset_pin(self->MISO);
|
|
|
|
}
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
|
2021-06-24 11:41:12 -04:00
|
|
|
return self->clock == NULL;
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
|
|
|
|
if (common_hal_busio_spi_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
|
2020-06-17 16:45:21 -04:00
|
|
|
spi_never_reset[self->host_id] = false;
|
2021-06-24 11:41:12 -04:00
|
|
|
spi_bus_remove_device(spi_handle[self->host_id]);
|
2020-06-17 16:45:21 -04:00
|
|
|
spi_bus_free(self->host_id);
|
2020-05-28 18:51:33 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
common_hal_reset_pin(self->MOSI);
|
|
|
|
common_hal_reset_pin(self->MISO);
|
|
|
|
common_hal_reset_pin(self->clock);
|
|
|
|
self->clock = NULL;
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
|
2021-06-24 11:41:12 -04:00
|
|
|
if (baudrate == self->baudrate &&
|
2020-06-17 16:45:21 -04:00
|
|
|
polarity == self->polarity &&
|
|
|
|
phase == self->phase &&
|
|
|
|
bits == self->bits) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
spi_bus_remove_device(spi_handle[self->host_id]);
|
|
|
|
set_spi_config(self, baudrate, polarity, phase, bits);
|
2020-05-28 18:51:33 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
|
2021-06-24 11:41:12 -04:00
|
|
|
bool grabbed_lock = false;
|
|
|
|
if (!self->has_lock) {
|
|
|
|
grabbed_lock = true;
|
|
|
|
self->has_lock = true;
|
2020-06-17 16:45:21 -04:00
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
return grabbed_lock;
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) {
|
|
|
|
return self->has_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
|
|
|
|
self->has_lock = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
const uint8_t *data, size_t len) {
|
2021-06-24 11:41:12 -04:00
|
|
|
if (self->MOSI == NULL) {
|
2020-09-21 13:47:14 -04:00
|
|
|
mp_raise_ValueError(translate("No MOSI Pin"));
|
|
|
|
}
|
2020-06-23 15:58:01 -04:00
|
|
|
return common_hal_busio_spi_transfer(self, data, NULL, len);
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t *data, size_t len, uint8_t write_value) {
|
2021-06-24 11:41:12 -04:00
|
|
|
if (self->MISO == NULL) {
|
2020-09-21 13:47:14 -04:00
|
|
|
mp_raise_ValueError(translate("No MISO Pin"));
|
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
if (self->MOSI == NULL) {
|
2020-09-21 13:47:14 -04:00
|
|
|
return common_hal_busio_spi_transfer(self, NULL, data, len);
|
|
|
|
} else {
|
|
|
|
memset(data, write_value, len);
|
|
|
|
return common_hal_busio_spi_transfer(self, data, data, len);
|
|
|
|
}
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
|
|
|
|
const uint8_t *data_out, uint8_t *data_in, size_t len) {
|
2020-05-28 18:51:33 -04:00
|
|
|
if (len == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-23 17:53:46 -04:00
|
|
|
if (self->MOSI == NULL && data_out != NULL) {
|
2020-09-21 13:47:14 -04:00
|
|
|
mp_raise_ValueError(translate("No MOSI Pin"));
|
|
|
|
}
|
2022-03-23 17:53:46 -04:00
|
|
|
if (self->MISO == NULL && data_in != NULL) {
|
2020-09-21 13:47:14 -04:00
|
|
|
mp_raise_ValueError(translate("No MISO Pin"));
|
|
|
|
}
|
2020-06-17 16:45:21 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
spi_transaction_t transaction = { 0 };
|
2020-09-10 20:32:20 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
// Round to nearest whole set of bits
|
|
|
|
int bits_to_send = len * 8 / self->bits * self->bits;
|
|
|
|
|
|
|
|
if (len <= 4) {
|
2020-06-24 15:46:36 -04:00
|
|
|
if (data_out != NULL) {
|
2021-06-24 11:41:12 -04:00
|
|
|
memcpy(&transaction.tx_data, data_out, len);
|
2020-06-24 15:46:36 -04:00
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
|
|
|
|
transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
|
|
|
|
transaction.length = bits_to_send;
|
|
|
|
spi_device_transmit(spi_handle[self->host_id], &transaction);
|
|
|
|
|
2020-06-24 15:46:36 -04:00
|
|
|
if (data_in != NULL) {
|
2021-06-24 11:41:12 -04:00
|
|
|
memcpy(data_in, &transaction.rx_data, len);
|
2020-06-24 15:46:36 -04:00
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
} else {
|
|
|
|
int offset = 0;
|
|
|
|
int bits_remaining = bits_to_send;
|
|
|
|
|
|
|
|
while (bits_remaining && !mp_hal_is_interrupted()) {
|
|
|
|
memset(&transaction, 0, sizeof(transaction));
|
|
|
|
|
|
|
|
transaction.length =
|
|
|
|
bits_remaining > SPI_MAX_DMA_BITS ? SPI_MAX_DMA_BITS : bits_remaining;
|
|
|
|
|
|
|
|
if (data_out != NULL) {
|
|
|
|
transaction.tx_buffer = data_out + offset;
|
|
|
|
}
|
|
|
|
if (data_in != NULL) {
|
|
|
|
transaction.rx_buffer = data_in + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
spi_device_transmit(spi_handle[self->host_id], &transaction);
|
|
|
|
bits_remaining -= transaction.length;
|
|
|
|
|
|
|
|
// doesn't need ceil(); loop ends when bits_remaining is 0
|
|
|
|
offset += transaction.length / 8;
|
2020-06-24 15:46:36 -04:00
|
|
|
|
2020-06-23 15:58:01 -04:00
|
|
|
RUN_BACKGROUND_TASKS;
|
2020-06-17 16:45:21 -04:00
|
|
|
}
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
2020-06-23 15:58:01 -04:00
|
|
|
return true;
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self) {
|
2021-06-24 11:41:12 -04:00
|
|
|
return self->baudrate;
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t *self) {
|
2020-06-17 16:45:21 -04:00
|
|
|
return self->polarity;
|
2020-05-28 18:51:33 -04:00
|
|
|
}
|
2021-06-24 11:41:12 -04:00
|
|
|
|
|
|
|
uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t *self) {
|
|
|
|
return self->phase;
|
|
|
|
}
|