Add initial I2C support, not quite working fully though
This commit is contained in:
parent
9b8d648986
commit
d0401f02a9
|
@ -78,6 +78,7 @@ INC += -Iesp-idf/components/freertos/xtensa/include
|
|||
INC += -Iesp-idf/components/esp32s2/include
|
||||
INC += -Iesp-idf/components/xtensa/esp32s2/include
|
||||
INC += -Iesp-idf/components/esp_common/include
|
||||
INC += -Iesp-idf/components/esp_ringbuf/include
|
||||
INC += -Iesp-idf/components/esp_rom/include
|
||||
INC += -Iesp-idf/components/xtensa/include
|
||||
INC += -Iesp-idf/components/esp_timer/include
|
||||
|
|
|
@ -0,0 +1,239 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "shared-bindings/busio/I2C.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "driver/i2c.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "shared-bindings/microcontroller/__init__.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
// Number of times to try to send packet if failed.
|
||||
#define ATTEMPTS 2
|
||||
|
||||
static const char* TAG = "CircuitPython I2C";
|
||||
|
||||
typedef enum {
|
||||
STATUS_FREE = 0,
|
||||
STATUS_IN_USE,
|
||||
STATUS_NEVER_RESET
|
||||
} i2c_status_t;
|
||||
|
||||
static i2c_status_t i2c_status[I2C_NUM_MAX];
|
||||
|
||||
void never_reset_i2c(i2c_port_t num) {
|
||||
i2c_status[num] = STATUS_NEVER_RESET;
|
||||
}
|
||||
|
||||
void i2c_reset(void) {
|
||||
for (i2c_port_t num = 0; num < I2C_NUM_MAX; num++) {
|
||||
if (i2c_status[num] == STATUS_IN_USE) {
|
||||
i2c_driver_delete(num);
|
||||
i2c_status[num] = STATUS_FREE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
|
||||
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) {
|
||||
|
||||
// Make sure scl and sda aren't input only
|
||||
|
||||
#if CIRCUITPY_REQUIRE_I2C_PULLUPS
|
||||
// // Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
|
||||
// gpio_set_pin_function(sda->number, GPIO_PIN_FUNCTION_OFF);
|
||||
// gpio_set_pin_function(scl->number, GPIO_PIN_FUNCTION_OFF);
|
||||
// gpio_set_pin_direction(sda->number, GPIO_DIRECTION_IN);
|
||||
// gpio_set_pin_direction(scl->number, GPIO_DIRECTION_IN);
|
||||
|
||||
// gpio_set_pin_pull_mode(sda->number, GPIO_PULL_DOWN);
|
||||
// gpio_set_pin_pull_mode(scl->number, GPIO_PULL_DOWN);
|
||||
|
||||
// common_hal_mcu_delay_us(10);
|
||||
|
||||
// gpio_set_pin_pull_mode(sda->number, GPIO_PULL_OFF);
|
||||
// gpio_set_pin_pull_mode(scl->number, GPIO_PULL_OFF);
|
||||
|
||||
// // We must pull up within 3us to achieve 400khz.
|
||||
// common_hal_mcu_delay_us(3);
|
||||
|
||||
// if (!gpio_get_pin_level(sda->number) || !gpio_get_pin_level(scl->number)) {
|
||||
// reset_pin_number(sda->number);
|
||||
// reset_pin_number(scl->number);
|
||||
// mp_raise_RuntimeError(translate("SDA or SCL needs a pull up"));
|
||||
// }
|
||||
#endif
|
||||
ESP_EARLY_LOGW(TAG, "create I2C");
|
||||
|
||||
|
||||
self->semaphore_handle = xSemaphoreCreateBinaryStatic(&self->semaphore);
|
||||
xSemaphoreGive(self->semaphore_handle);
|
||||
ESP_EARLY_LOGW(TAG, "new I2C lock %x", self->semaphore_handle);
|
||||
self->sda_pin = sda;
|
||||
self->scl_pin = scl;
|
||||
ESP_EARLY_LOGW(TAG, "scl %d sda %d", self->sda_pin->number, self->scl_pin->number);
|
||||
self->i2c_num = I2C_NUM_MAX;
|
||||
for (i2c_port_t num = 0; num < I2C_NUM_MAX; num++) {
|
||||
if (i2c_status[num] == STATUS_FREE) {
|
||||
self->i2c_num = num;
|
||||
}
|
||||
}
|
||||
if (self->i2c_num == I2C_NUM_MAX) {
|
||||
mp_raise_ValueError(translate("All I2C peripherals are in use"));
|
||||
}
|
||||
i2c_status[self->i2c_num] = STATUS_IN_USE;
|
||||
i2c_config_t i2c_conf = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = self->sda_pin->number,
|
||||
.scl_io_num = self->scl_pin->number,
|
||||
.sda_pullup_en = GPIO_PULLUP_DISABLE, /*!< Internal GPIO pull mode for I2C sda signal*/
|
||||
.scl_pullup_en = GPIO_PULLUP_DISABLE, /*!< Internal GPIO pull mode for I2C scl signal*/
|
||||
|
||||
.master = {
|
||||
.clk_speed = frequency,
|
||||
}
|
||||
};
|
||||
// ESP_EARLY_LOGW(TAG, "param config %p %p %d %d", &i2c_conf, &(i2c_conf.mode), i2c_conf.mode, I2C_MODE_MAX);
|
||||
ESP_EARLY_LOGW(TAG, "param config %p %d %d", &i2c_conf, i2c_conf.mode, I2C_MODE_MAX);
|
||||
esp_err_t result = i2c_param_config(self->i2c_num, &i2c_conf);
|
||||
if (result != ESP_OK) {
|
||||
ESP_EARLY_LOGW(TAG, "error %d %p %d %d", result, &i2c_conf, (&i2c_conf)->mode, I2C_MODE_MAX);
|
||||
vTaskDelay(0);
|
||||
mp_raise_ValueError(translate("Invalid pins"));
|
||||
}
|
||||
ESP_EARLY_LOGW(TAG, "param config I2C done");
|
||||
result = i2c_driver_install(self->i2c_num,
|
||||
I2C_MODE_MASTER,
|
||||
0,
|
||||
0,
|
||||
0);
|
||||
if (result != ESP_OK) {
|
||||
mp_raise_OSError(MP_EIO);
|
||||
}
|
||||
|
||||
claim_pin(sda);
|
||||
claim_pin(scl);
|
||||
ESP_EARLY_LOGW(TAG, "create I2C done");
|
||||
}
|
||||
|
||||
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
|
||||
return self->sda_pin == NULL;
|
||||
}
|
||||
|
||||
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
|
||||
if (common_hal_busio_i2c_deinited(self)) {
|
||||
return;
|
||||
}
|
||||
|
||||
i2c_driver_delete(self->i2c_num);
|
||||
i2c_status[self->i2c_num] = STATUS_FREE;
|
||||
|
||||
reset_pin(self->sda_pin);
|
||||
reset_pin(self->scl_pin);
|
||||
self->sda_pin = NULL;
|
||||
self->scl_pin = NULL;
|
||||
}
|
||||
|
||||
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, addr << 1, true);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t result = i2c_master_cmd_begin(self->i2c_num, cmd, 100);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
|
||||
ESP_EARLY_LOGW(TAG, "locking I2C %x", self->semaphore_handle);
|
||||
self->has_lock = xSemaphoreTake(self->semaphore_handle, 0) == pdTRUE;
|
||||
if (self->has_lock) {
|
||||
ESP_EARLY_LOGW(TAG, "lock grabbed");
|
||||
} else {
|
||||
ESP_EARLY_LOGW(TAG, "unable to grab lock");
|
||||
}
|
||||
return self->has_lock;
|
||||
}
|
||||
|
||||
bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) {
|
||||
return self->has_lock;
|
||||
}
|
||||
|
||||
void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
|
||||
ESP_EARLY_LOGW(TAG, "unlocking I2C");
|
||||
xSemaphoreGive(self->semaphore_handle);
|
||||
self->has_lock = false;
|
||||
}
|
||||
|
||||
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
|
||||
const uint8_t *data, size_t len, bool transmit_stop_bit) {
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, addr << 1, true);
|
||||
i2c_master_write(cmd, (uint8_t*) data, len, true);
|
||||
if (transmit_stop_bit) {
|
||||
i2c_master_stop(cmd);
|
||||
}
|
||||
esp_err_t result = i2c_master_cmd_begin(self->i2c_num, cmd, 100 /* wait in ticks */);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
|
||||
if (result == ESP_OK) {
|
||||
return 0;
|
||||
} else if (result == ESP_FAIL) {
|
||||
return MP_ENODEV;
|
||||
}
|
||||
return MP_EIO;
|
||||
}
|
||||
|
||||
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
|
||||
uint8_t *data, size_t len) {
|
||||
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, addr << 1 | 1, true); // | 1 to indicate read
|
||||
i2c_master_read(cmd, data, len, true);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t result = i2c_master_cmd_begin(self->i2c_num, cmd, 100 /* wait in ticks */);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
|
||||
if (result == ESP_OK) {
|
||||
return 0;
|
||||
} else if (result == ESP_FAIL) {
|
||||
return MP_ENODEV;
|
||||
}
|
||||
return MP_EIO;
|
||||
}
|
||||
|
||||
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
|
||||
never_reset_i2c(self->i2c_num);
|
||||
|
||||
never_reset_pin(self->scl_pin);
|
||||
never_reset_pin(self->sda_pin);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_I2C_H
|
||||
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_I2C_H
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "esp-idf/components/soc/include/hal/i2c_types.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "py/obj.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
const mcu_pin_obj_t* scl_pin;
|
||||
const mcu_pin_obj_t* sda_pin;
|
||||
i2c_port_t i2c_num;
|
||||
StaticSemaphore_t semaphore;
|
||||
SemaphoreHandle_t semaphore_handle;
|
||||
bool has_lock;
|
||||
} busio_i2c_obj_t;
|
||||
|
||||
void i2c_reset(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_I2C_H
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Scott Shawcroft
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_ONEWIRE_H
|
||||
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_ONEWIRE_H
|
||||
|
||||
// Use bitbangio.
|
||||
#include "shared-module/busio/OneWire.h"
|
||||
|
||||
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_ONEWIRE_H
|
|
@ -0,0 +1,249 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "shared-bindings/busio/SPI.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "boards/board.h"
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
#include "supervisor/shared/rgb_led_status.h"
|
||||
|
||||
void spi_reset(void) {
|
||||
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
|
||||
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
|
||||
const mcu_pin_obj_t * miso) {
|
||||
// uint8_t sercom_index;
|
||||
// uint32_t clock_pinmux = 0;
|
||||
// bool mosi_none = mosi == NULL;
|
||||
// bool miso_none = miso == NULL;
|
||||
// uint32_t mosi_pinmux = 0;
|
||||
// uint32_t miso_pinmux = 0;
|
||||
// uint8_t clock_pad = 0;
|
||||
// uint8_t mosi_pad = 0;
|
||||
// uint8_t miso_pad = 0;
|
||||
// uint8_t dopo = 255;
|
||||
|
||||
// if (sercom == NULL) {
|
||||
// mp_raise_ValueError(translate("Invalid pins"));
|
||||
// }
|
||||
|
||||
// // Set up SPI clocks on SERCOM.
|
||||
// samd_peripherals_sercom_clock_init(sercom, sercom_index);
|
||||
|
||||
#if defined(MICROPY_HW_APA102_SCK) && defined(MICROPY_HW_APA102_MOSI) && !CIRCUITPY_BITBANG_APA102
|
||||
// // if we're re-using the dotstar sercom, make sure it is disabled or the init will fail out
|
||||
// hri_sercomspi_clear_CTRLA_ENABLE_bit(sercom);
|
||||
#endif
|
||||
// if (spi_m_sync_init(&self->spi_desc, sercom) != ERR_NONE) {
|
||||
// mp_raise_OSError(MP_EIO);
|
||||
// }
|
||||
|
||||
// Pads must be set after spi_m_sync_init(), which uses default values from
|
||||
// the prototypical SERCOM.
|
||||
// hri_sercomspi_write_CTRLA_DOPO_bf(sercom, dopo);
|
||||
// hri_sercomspi_write_CTRLA_DIPO_bf(sercom, miso_pad);
|
||||
|
||||
// Always start at 250khz which is what SD cards need. They are sensitive to
|
||||
// SPI bus noise before they are put into SPI mode.
|
||||
// uint8_t baud_value = samd_peripherals_spi_baudrate_to_baud_reg_value(250000);
|
||||
// if (spi_m_sync_set_baudrate(&self->spi_desc, baud_value) != ERR_NONE) {
|
||||
// // spi_m_sync_set_baudrate does not check for validity, just whether the device is
|
||||
// // busy or not
|
||||
// mp_raise_OSError(MP_EIO);
|
||||
// }
|
||||
|
||||
// gpio_set_pin_direction(clock->number, GPIO_DIRECTION_OUT);
|
||||
// gpio_set_pin_pull_mode(clock->number, GPIO_PULL_OFF);
|
||||
// gpio_set_pin_function(clock->number, clock_pinmux);
|
||||
// claim_pin(clock);
|
||||
// self->clock_pin = clock->number;
|
||||
|
||||
// if (mosi_none) {
|
||||
// self->MOSI_pin = NO_PIN;
|
||||
// } else {
|
||||
// gpio_set_pin_direction(mosi->number, GPIO_DIRECTION_OUT);
|
||||
// gpio_set_pin_pull_mode(mosi->number, GPIO_PULL_OFF);
|
||||
// gpio_set_pin_function(mosi->number, mosi_pinmux);
|
||||
// self->MOSI_pin = mosi->number;
|
||||
// claim_pin(mosi);
|
||||
// }
|
||||
|
||||
// if (miso_none) {
|
||||
// self->MISO_pin = NO_PIN;
|
||||
// } else {
|
||||
// gpio_set_pin_direction(miso->number, GPIO_DIRECTION_IN);
|
||||
// gpio_set_pin_pull_mode(miso->number, GPIO_PULL_OFF);
|
||||
// gpio_set_pin_function(miso->number, miso_pinmux);
|
||||
// self->MISO_pin = miso->number;
|
||||
// claim_pin(miso);
|
||||
// }
|
||||
|
||||
// spi_m_sync_enable(&self->spi_desc);
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
|
||||
// never_reset_sercom(self->spi_desc.dev.prvt);
|
||||
|
||||
never_reset_pin(self->clock_pin);
|
||||
never_reset_pin(self->MOSI_pin);
|
||||
never_reset_pin(self->MISO_pin);
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
|
||||
return self->clock_pin == NULL;
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
|
||||
if (common_hal_busio_spi_deinited(self)) {
|
||||
return;
|
||||
}
|
||||
// allow_reset_sercom(self->spi_desc.dev.prvt);
|
||||
|
||||
// spi_m_sync_disable(&self->spi_desc);
|
||||
// spi_m_sync_deinit(&self->spi_desc);
|
||||
reset_pin(self->clock_pin);
|
||||
reset_pin(self->MOSI_pin);
|
||||
reset_pin(self->MISO_pin);
|
||||
self->clock_pin = NULL;
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
|
||||
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
|
||||
// If the settings are already what we want then don't reset them.
|
||||
// if (hri_sercomspi_get_CTRLA_CPHA_bit(hw) == phase &&
|
||||
// hri_sercomspi_get_CTRLA_CPOL_bit(hw) == polarity &&
|
||||
// hri_sercomspi_read_CTRLB_CHSIZE_bf(hw) == ((uint32_t)bits - 8) &&
|
||||
// hri_sercomspi_read_BAUD_BAUD_bf(hw) == baud_reg_value) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// Disable, set values (most or all are enable-protected), and re-enable.
|
||||
// spi_m_sync_disable(&self->spi_desc);
|
||||
// hri_sercomspi_wait_for_sync(hw, SERCOM_SPI_SYNCBUSY_MASK);
|
||||
|
||||
// hri_sercomspi_write_CTRLA_CPHA_bit(hw, phase);
|
||||
// hri_sercomspi_write_CTRLA_CPOL_bit(hw, polarity);
|
||||
// hri_sercomspi_write_CTRLB_CHSIZE_bf(hw, bits - 8);
|
||||
// hri_sercomspi_write_BAUD_BAUD_bf(hw, baud_reg_value);
|
||||
// hri_sercomspi_wait_for_sync(hw, SERCOM_SPI_SYNCBUSY_MASK);
|
||||
|
||||
// spi_m_sync_enable(&self->spi_desc);
|
||||
// hri_sercomspi_wait_for_sync(hw, SERCOM_SPI_SYNCBUSY_MASK);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
|
||||
bool grabbed_lock = false;
|
||||
// CRITICAL_SECTION_ENTER()
|
||||
if (!self->has_lock) {
|
||||
grabbed_lock = true;
|
||||
self->has_lock = true;
|
||||
}
|
||||
// CRITICAL_SECTION_LEAVE();
|
||||
return grabbed_lock;
|
||||
}
|
||||
|
||||
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,
|
||||
const uint8_t *data, size_t len) {
|
||||
if (len == 0) {
|
||||
return true;
|
||||
}
|
||||
// int32_t status;
|
||||
if (len >= 16) {
|
||||
// status = sercom_dma_write(self->spi_desc.dev.prvt, data, len);
|
||||
} else {
|
||||
// struct io_descriptor *spi_io;
|
||||
// spi_m_sync_get_io_descriptor(&self->spi_desc, &spi_io);
|
||||
// status = spi_io->write(spi_io, data, len);
|
||||
}
|
||||
return false; // Status is number of chars read or an error code < 0.
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
|
||||
uint8_t *data, size_t len, uint8_t write_value) {
|
||||
if (len == 0) {
|
||||
return true;
|
||||
}
|
||||
// int32_t status;
|
||||
if (len >= 16) {
|
||||
// status = sercom_dma_read(self->spi_desc.dev.prvt, data, len, write_value);
|
||||
} else {
|
||||
// self->spi_desc.dev.dummy_byte = write_value;
|
||||
|
||||
// struct io_descriptor *spi_io;
|
||||
// spi_m_sync_get_io_descriptor(&self->spi_desc, &spi_io);
|
||||
|
||||
// status = spi_io->read(spi_io, data, len);
|
||||
}
|
||||
return false; // Status is number of chars read or an error code < 0.
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) {
|
||||
if (len == 0) {
|
||||
return true;
|
||||
}
|
||||
// int32_t status;
|
||||
if (len >= 16) {
|
||||
// status = sercom_dma_transfer(self->spi_desc.dev.prvt, data_out, data_in, len);
|
||||
} else {
|
||||
// struct spi_xfer xfer;
|
||||
// xfer.txbuf = data_out;
|
||||
// xfer.rxbuf = data_in;
|
||||
// xfer.size = len;
|
||||
// status = spi_m_sync_transfer(&self->spi_desc, &xfer);
|
||||
}
|
||||
return false; // Status is number of chars read or an error code < 0.
|
||||
}
|
||||
|
||||
uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t* self) {
|
||||
// return samd_peripherals_spi_baud_reg_value_to_baudrate(hri_sercomspi_read_BAUD_reg(self->spi_desc.dev.prvt));
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t* self) {
|
||||
// void * hw = self->spi_desc.dev.prvt;
|
||||
// return hri_sercomspi_get_CTRLA_CPHA_bit(hw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t* self) {
|
||||
// void * hw = self->spi_desc.dev.prvt;
|
||||
// return hri_sercomspi_get_CTRLA_CPOL_bit(hw);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_SPI_H
|
||||
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_SPI_H
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
bool has_lock;
|
||||
const mcu_pin_obj_t* clock_pin;
|
||||
const mcu_pin_obj_t* MOSI_pin;
|
||||
const mcu_pin_obj_t* MISO_pin;
|
||||
} busio_spi_obj_t;
|
||||
|
||||
void spi_reset(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_SPI_H
|
|
@ -0,0 +1,405 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Damien P. George
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "shared-bindings/microcontroller/__init__.h"
|
||||
#include "shared-bindings/busio/UART.h"
|
||||
|
||||
#include "mpconfigport.h"
|
||||
#include "lib/utils/interrupt_char.h"
|
||||
#include "py/gc.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/stream.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
#include "supervisor/shared/tick.h"
|
||||
|
||||
#define UART_DEBUG(...) (void)0
|
||||
// #define UART_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
|
||||
|
||||
// Do-nothing callback needed so that usart_async code will enable rx interrupts.
|
||||
// See comment below re usart_async_register_callback()
|
||||
// static void usart_async_rxc_callback(const struct usart_async_descriptor *const descr) {
|
||||
// // Nothing needs to be done by us.
|
||||
// }
|
||||
|
||||
void uart_reset(void) {
|
||||
|
||||
}
|
||||
|
||||
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
|
||||
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx,
|
||||
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
|
||||
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
|
||||
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
|
||||
mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer,
|
||||
bool sigint_enabled) {
|
||||
|
||||
// uint8_t sercom_index = 255; // Unset index
|
||||
// uint32_t rx_pinmux = 0;
|
||||
// uint8_t rx_pad = 255; // Unset pad
|
||||
// uint32_t tx_pinmux = 0;
|
||||
// uint8_t tx_pad = 255; // Unset pad
|
||||
|
||||
// if ((rts != NULL) || (cts != NULL) || (rs485_dir != NULL) || (rs485_invert)) {
|
||||
// mp_raise_ValueError(translate("RTS/CTS/RS485 Not yet supported on this device"));
|
||||
// }
|
||||
|
||||
// if (bits > 8) {
|
||||
// mp_raise_NotImplementedError(translate("bytes > 8 bits not supported"));
|
||||
// }
|
||||
|
||||
bool have_tx = tx != NULL;
|
||||
bool have_rx = rx != NULL;
|
||||
if (!have_tx && !have_rx) {
|
||||
mp_raise_ValueError(translate("tx and rx cannot both be None"));
|
||||
}
|
||||
|
||||
self->baudrate = baudrate;
|
||||
self->character_bits = bits;
|
||||
self->timeout_ms = timeout * 1000;
|
||||
|
||||
// This assignment is only here because the usart_async routines take a *const argument.
|
||||
// struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
|
||||
|
||||
// for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
|
||||
// Sercom* potential_sercom = NULL;
|
||||
// if (have_tx) {
|
||||
// sercom_index = tx->sercom[i].index;
|
||||
// if (sercom_index >= SERCOM_INST_NUM) {
|
||||
// continue;
|
||||
// }
|
||||
// potential_sercom = sercom_insts[sercom_index];
|
||||
// #ifdef SAMD21
|
||||
// if (potential_sercom->USART.CTRLA.bit.ENABLE != 0 ||
|
||||
// !(tx->sercom[i].pad == 0 ||
|
||||
// tx->sercom[i].pad == 2)) {
|
||||
// continue;
|
||||
// }
|
||||
// #endif
|
||||
// #ifdef SAMD51
|
||||
// if (potential_sercom->USART.CTRLA.bit.ENABLE != 0 ||
|
||||
// !(tx->sercom[i].pad == 0)) {
|
||||
// continue;
|
||||
// }
|
||||
// #endif
|
||||
// tx_pinmux = PINMUX(tx->number, (i == 0) ? MUX_C : MUX_D);
|
||||
// tx_pad = tx->sercom[i].pad;
|
||||
// if (rx == NULL) {
|
||||
// sercom = potential_sercom;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
|
||||
// if (((!have_tx && rx->sercom[j].index < SERCOM_INST_NUM &&
|
||||
// sercom_insts[rx->sercom[j].index]->USART.CTRLA.bit.ENABLE == 0) ||
|
||||
// sercom_index == rx->sercom[j].index) &&
|
||||
// rx->sercom[j].pad != tx_pad) {
|
||||
// rx_pinmux = PINMUX(rx->number, (j == 0) ? MUX_C : MUX_D);
|
||||
// rx_pad = rx->sercom[j].pad;
|
||||
// sercom = sercom_insts[rx->sercom[j].index];
|
||||
// sercom_index = rx->sercom[j].index;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if (sercom != NULL) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if (sercom == NULL) {
|
||||
// mp_raise_ValueError(translate("Invalid pins"));
|
||||
// }
|
||||
// if (!have_tx) {
|
||||
// tx_pad = 0;
|
||||
// if (rx_pad == 0) {
|
||||
// tx_pad = 2;
|
||||
// }
|
||||
// }
|
||||
// if (!have_rx) {
|
||||
// rx_pad = (tx_pad + 1) % 4;
|
||||
// }
|
||||
|
||||
// // Set up clocks on SERCOM.
|
||||
// samd_peripherals_sercom_clock_init(sercom, sercom_index);
|
||||
|
||||
// if (rx && receiver_buffer_size > 0) {
|
||||
// self->buffer_length = receiver_buffer_size;
|
||||
// // Initially allocate the UART's buffer in the long-lived part of the
|
||||
// // heap. UARTs are generally long-lived objects, but the "make long-
|
||||
// // lived" machinery is incapable of moving internal pointers like
|
||||
// // self->buffer, so do it manually. (However, as long as internal
|
||||
// // pointers like this are NOT moved, allocating the buffer
|
||||
// // in the long-lived pool is not strictly necessary)
|
||||
// self->buffer = (uint8_t *) gc_alloc(self->buffer_length * sizeof(uint8_t), false, true);
|
||||
// if (self->buffer == NULL) {
|
||||
// common_hal_busio_uart_deinit(self);
|
||||
// mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), self->buffer_length * sizeof(uint8_t));
|
||||
// }
|
||||
// } else {
|
||||
// self->buffer_length = 0;
|
||||
// self->buffer = NULL;
|
||||
// }
|
||||
|
||||
// if (usart_async_init(usart_desc_p, sercom, self->buffer, self->buffer_length, NULL) != ERR_NONE) {
|
||||
// mp_raise_ValueError(translate("Could not initialize UART"));
|
||||
// }
|
||||
|
||||
// usart_async_init() sets a number of defaults based on a prototypical SERCOM
|
||||
// which don't necessarily match what we need. After calling it, set the values
|
||||
// specific to this instantiation of UART.
|
||||
|
||||
// Set pads computed for this SERCOM.
|
||||
// TXPO:
|
||||
// 0x0: TX pad 0; no RTS/CTS
|
||||
// 0x1: TX pad 2; no RTS/CTS
|
||||
// 0x2: TX pad 0; RTS: pad 2, CTS: pad 3 (not used by us right now)
|
||||
// So divide by 2 to map pad to value.
|
||||
// RXPO:
|
||||
// 0x0: RX pad 0
|
||||
// 0x1: RX pad 1
|
||||
// 0x2: RX pad 2
|
||||
// 0x3: RX pad 3
|
||||
|
||||
// Doing a group mask and set of the registers saves 60 bytes over setting the bitfields individually.
|
||||
|
||||
// sercom->USART.CTRLA.reg &= ~(SERCOM_USART_CTRLA_TXPO_Msk |
|
||||
// SERCOM_USART_CTRLA_RXPO_Msk |
|
||||
// SERCOM_USART_CTRLA_FORM_Msk);
|
||||
// sercom->USART.CTRLA.reg |= SERCOM_USART_CTRLA_TXPO(tx_pad / 2) |
|
||||
// SERCOM_USART_CTRLA_RXPO(rx_pad) |
|
||||
// (parity == PARITY_NONE ? 0 : SERCOM_USART_CTRLA_FORM(1));
|
||||
|
||||
// Enable tx and/or rx based on whether the pins were specified.
|
||||
// CHSIZE is 0 for 8 bits, 5, 6, 7 for 5, 6, 7 bits. 1 for 9 bits, but we don't support that.
|
||||
// sercom->USART.CTRLB.reg &= ~(SERCOM_USART_CTRLB_TXEN |
|
||||
// SERCOM_USART_CTRLB_RXEN |
|
||||
// SERCOM_USART_CTRLB_PMODE |
|
||||
// SERCOM_USART_CTRLB_SBMODE |
|
||||
// SERCOM_USART_CTRLB_CHSIZE_Msk);
|
||||
// sercom->USART.CTRLB.reg |= (have_tx ? SERCOM_USART_CTRLB_TXEN : 0) |
|
||||
// (have_rx ? SERCOM_USART_CTRLB_RXEN : 0) |
|
||||
// (parity == PARITY_ODD ? SERCOM_USART_CTRLB_PMODE : 0) |
|
||||
// (stop > 1 ? SERCOM_USART_CTRLB_SBMODE : 0) |
|
||||
// SERCOM_USART_CTRLB_CHSIZE(bits % 8);
|
||||
|
||||
// Set baud rate
|
||||
// common_hal_busio_uart_set_baudrate(self, baudrate);
|
||||
|
||||
// Turn on rx interrupt handling. The UART async driver has its own set of internal callbacks,
|
||||
// which are set up by uart_async_init(). These in turn can call user-specified callbacks.
|
||||
// In fact, the actual interrupts are not enabled unless we set up a user-specified callback.
|
||||
// This is confusing. It's explained in the Atmel START User Guide -> Implementation Description ->
|
||||
// Different read function behavior in some asynchronous drivers. As of this writing:
|
||||
// http://start.atmel.com/static/help/index.html?GUID-79201A5A-226F-4FBB-B0B8-AB0BE0554836
|
||||
// Look at the ASFv4 code example for async USART.
|
||||
// usart_async_register_callback(usart_desc_p, USART_ASYNC_RXC_CB, usart_async_rxc_callback);
|
||||
|
||||
|
||||
// if (have_tx) {
|
||||
// gpio_set_pin_direction(tx->number, GPIO_DIRECTION_OUT);
|
||||
// gpio_set_pin_pull_mode(tx->number, GPIO_PULL_OFF);
|
||||
// gpio_set_pin_function(tx->number, tx_pinmux);
|
||||
// self->tx_pin = tx->number;
|
||||
// claim_pin(tx);
|
||||
// } else {
|
||||
// self->tx_pin = NO_PIN;
|
||||
// }
|
||||
|
||||
// if (have_rx) {
|
||||
// gpio_set_pin_direction(rx->number, GPIO_DIRECTION_IN);
|
||||
// gpio_set_pin_pull_mode(rx->number, GPIO_PULL_OFF);
|
||||
// gpio_set_pin_function(rx->number, rx_pinmux);
|
||||
// self->rx_pin = rx->number;
|
||||
// claim_pin(rx);
|
||||
// } else {
|
||||
// self->rx_pin = NO_PIN;
|
||||
// }
|
||||
|
||||
// usart_async_enable(usart_desc_p);
|
||||
}
|
||||
|
||||
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
|
||||
return self->rx_pin == NULL && self->tx_pin == NULL;
|
||||
}
|
||||
|
||||
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
|
||||
if (common_hal_busio_uart_deinited(self)) {
|
||||
return;
|
||||
}
|
||||
// // This assignment is only here because the usart_async routines take a *const argument.
|
||||
// struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
|
||||
// usart_async_disable(usart_desc_p);
|
||||
// usart_async_deinit(usart_desc_p);
|
||||
reset_pin(self->rx_pin);
|
||||
reset_pin(self->tx_pin);
|
||||
self->rx_pin = NULL;
|
||||
self->tx_pin = NULL;
|
||||
}
|
||||
|
||||
// Read characters.
|
||||
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
|
||||
if (self->rx_pin == NULL) {
|
||||
mp_raise_ValueError(translate("No RX pin"));
|
||||
}
|
||||
|
||||
// This assignment is only here because the usart_async routines take a *const argument.
|
||||
// struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
|
||||
|
||||
if (len == 0) {
|
||||
// Nothing to read.
|
||||
return 0;
|
||||
}
|
||||
|
||||
// struct io_descriptor *io;
|
||||
// usart_async_get_io_descriptor(usart_desc_p, &io);
|
||||
|
||||
// size_t total_read = 0;
|
||||
// uint64_t start_ticks = supervisor_ticks_ms64();
|
||||
|
||||
// Busy-wait until timeout or until we've read enough chars.
|
||||
// while (supervisor_ticks_ms64() - start_ticks <= self->timeout_ms) {
|
||||
// // Read as many chars as we can right now, up to len.
|
||||
// size_t num_read = io_read(io, data, len);
|
||||
|
||||
// // Advance pointer in data buffer, and decrease how many chars left to read.
|
||||
// data += num_read;
|
||||
// len -= num_read;
|
||||
// total_read += num_read;
|
||||
// if (len == 0) {
|
||||
// // Don't need to read any more: data buf is full.
|
||||
// break;
|
||||
// }
|
||||
// if (num_read > 0) {
|
||||
// // Reset the timeout on every character read.
|
||||
// start_ticks = supervisor_ticks_ms64();
|
||||
// }
|
||||
// RUN_BACKGROUND_TASKS;
|
||||
// // Allow user to break out of a timeout with a KeyboardInterrupt.
|
||||
// if (mp_hal_is_interrupted()) {
|
||||
// break;
|
||||
// }
|
||||
// // If we are zero timeout, make sure we don't loop again (in the event
|
||||
// // we read in under 1ms)
|
||||
// if (self->timeout_ms == 0) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (total_read == 0) {
|
||||
// *errcode = EAGAIN;
|
||||
// return MP_STREAM_ERROR;
|
||||
// }
|
||||
|
||||
// return total_read;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Write characters.
|
||||
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
|
||||
if (self->tx_pin == NULL) {
|
||||
mp_raise_ValueError(translate("No TX pin"));
|
||||
}
|
||||
|
||||
// This assignment is only here because the usart_async routines take a *const argument.
|
||||
// struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
|
||||
|
||||
// struct io_descriptor *io;
|
||||
// usart_async_get_io_descriptor(usart_desc_p, &io);
|
||||
|
||||
// // Start writing characters. This is non-blocking and will
|
||||
// // return immediately after setting up the write.
|
||||
// if (io_write(io, data, len) < 0) {
|
||||
// *errcode = MP_EAGAIN;
|
||||
// return MP_STREAM_ERROR;
|
||||
// }
|
||||
|
||||
// // Busy-wait until all characters transmitted.
|
||||
// struct usart_async_status async_status;
|
||||
// while (true) {
|
||||
// usart_async_get_status(usart_desc_p, &async_status);
|
||||
// if (async_status.txcnt >= len) {
|
||||
// break;
|
||||
// }
|
||||
// RUN_BACKGROUND_TASKS;
|
||||
// }
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) {
|
||||
return self->baudrate;
|
||||
}
|
||||
|
||||
void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrate) {
|
||||
// This assignment is only here because the usart_async routines take a *const argument.
|
||||
// struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
|
||||
// usart_async_set_baud_rate(usart_desc_p,
|
||||
// // Samples and ARITHMETIC vs FRACTIONAL must correspond to USART_SAMPR in
|
||||
// // hpl_sercom_config.h.
|
||||
// _usart_async_calculate_baud_rate(baudrate, // e.g. 9600 baud
|
||||
// PROTOTYPE_SERCOM_USART_ASYNC_CLOCK_FREQUENCY,
|
||||
// 16, // samples
|
||||
// USART_BAUDRATE_ASYNCH_ARITHMETIC,
|
||||
// 0 // fraction - not used for ARITHMETIC
|
||||
// ));
|
||||
self->baudrate = baudrate;
|
||||
}
|
||||
|
||||
mp_float_t common_hal_busio_uart_get_timeout(busio_uart_obj_t *self) {
|
||||
return (mp_float_t) (self->timeout_ms / 1000.0f);
|
||||
}
|
||||
|
||||
void common_hal_busio_uart_set_timeout(busio_uart_obj_t *self, mp_float_t timeout) {
|
||||
self->timeout_ms = timeout * 1000;
|
||||
}
|
||||
|
||||
uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
|
||||
// This assignment is only here because the usart_async routines take a *const argument.
|
||||
// struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
|
||||
// struct usart_async_status async_status;
|
||||
// usart_async_get_status(usart_desc_p, &async_status);
|
||||
// return async_status.rxcnt;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
|
||||
// This assignment is only here because the usart_async routines take a *const argument.
|
||||
// struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
|
||||
// usart_async_flush_rx_buffer(usart_desc_p);
|
||||
|
||||
}
|
||||
|
||||
// True if there are no characters still to be written.
|
||||
bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) {
|
||||
if (self->tx_pin == NULL) {
|
||||
return false;
|
||||
}
|
||||
// This assignment is only here because the usart_async routines take a *const argument.
|
||||
// struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
|
||||
// struct usart_async_status async_status;
|
||||
// usart_async_get_status(usart_desc_p, &async_status);
|
||||
// return !(async_status.flags & USART_ASYNC_STATUS_BUSY);
|
||||
return false;
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_UART_H
|
||||
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_UART_H
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
const mcu_pin_obj_t* rx_pin;
|
||||
const mcu_pin_obj_t* tx_pin;
|
||||
uint8_t character_bits;
|
||||
bool rx_error;
|
||||
uint32_t baudrate;
|
||||
uint32_t timeout_ms;
|
||||
uint32_t buffer_length;
|
||||
uint8_t* buffer;
|
||||
} busio_uart_obj_t;
|
||||
|
||||
void uart_reset(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_BUSIO_UART_H
|
|
@ -0,0 +1 @@
|
|||
// No busio module functions.
|
|
@ -49,6 +49,10 @@ void reset_pin_number(gpio_num_t pin_number) {
|
|||
in_use[pin_number / 32] &= ~(1 << pin_number % 32);
|
||||
}
|
||||
|
||||
void reset_pin(const mcu_pin_obj_t* pin) {
|
||||
reset_pin_number(pin->number);
|
||||
}
|
||||
|
||||
void reset_all_pins(void) {
|
||||
for (uint8_t i = 0; i < GPIO_PIN_COUNT; i++) {
|
||||
uint32_t iomux_address = GPIO_PIN_MUX_REG[i];
|
||||
|
|
|
@ -35,6 +35,7 @@ void reset_all_pins(void);
|
|||
// reset_pin_number takes the pin number instead of the pointer so that objects don't
|
||||
// need to store a full pointer.
|
||||
void reset_pin_number(gpio_num_t pin_number);
|
||||
void reset_pin(const mcu_pin_obj_t* pin);
|
||||
void claim_pin(const mcu_pin_obj_t* pin);
|
||||
bool pin_number_is_free(gpio_num_t pin_number);
|
||||
void never_reset_pin_number(gpio_num_t pin_number);
|
||||
|
|
|
@ -16,10 +16,10 @@ CIRCUITPY_FULL_BUILD = 0
|
|||
CIRCUITPY_ANALOGIO = 0
|
||||
CIRCUITPY_AUDIOBUSIO = 0
|
||||
CIRCUITPY_AUDIOIO = 0
|
||||
CIRCUITPY_BITBANGIO = 0
|
||||
CIRCUITPY_BITBANGIO = 1
|
||||
CIRCUITPY_BOARD = 1
|
||||
CIRCUITPY_DIGITALIO = 1
|
||||
CIRCUITPY_BUSIO = 0
|
||||
CIRCUITPY_BUSIO = 1
|
||||
CIRCUITPY_DISPLAYIO = 0
|
||||
CIRCUITPY_FREQUENCYIO = 0
|
||||
CIRCUITPY_I2CSLAVE = 0
|
||||
|
|
|
@ -35,6 +35,9 @@
|
|||
#include "freertos/task.h"
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
#include "common-hal/busio/I2C.h"
|
||||
#include "common-hal/busio/SPI.h"
|
||||
#include "common-hal/busio/UART.h"
|
||||
#include "supervisor/memory.h"
|
||||
#include "supervisor/shared/tick.h"
|
||||
|
||||
|
@ -64,8 +67,13 @@ safe_mode_t port_init(void) {
|
|||
}
|
||||
|
||||
void reset_port(void) {
|
||||
|
||||
reset_all_pins();
|
||||
|
||||
#if CIRCUITPY_BUSIO
|
||||
i2c_reset();
|
||||
spi_reset();
|
||||
uart_reset();
|
||||
#endif
|
||||
}
|
||||
|
||||
void reset_to_bootloader(void) {
|
||||
|
|
Loading…
Reference in New Issue