From fc440e7609590a24df2f8013c377e6871cf27ea5 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 19 Oct 2021 15:18:32 -0400 Subject: [PATCH] move sercom_reset() etc. out of busio/SPI.c to busio/__init__.c --- ports/atmel-samd/common-hal/busio/I2C.c | 2 +- ports/atmel-samd/common-hal/busio/SPI.c | 39 +----------- ports/atmel-samd/common-hal/busio/SPI.h | 4 -- ports/atmel-samd/common-hal/busio/UART.c | 2 +- ports/atmel-samd/common-hal/busio/__init__.c | 64 +++++++++++++++++++- ports/atmel-samd/common-hal/busio/__init__.h | 35 +++++++++++ ports/atmel-samd/supervisor/port.c | 4 +- 7 files changed, 104 insertions(+), 46 deletions(-) create mode 100644 ports/atmel-samd/common-hal/busio/__init__.h diff --git a/ports/atmel-samd/common-hal/busio/I2C.c b/ports/atmel-samd/common-hal/busio/I2C.c index 39aa6c9e97..7d77c0d43a 100644 --- a/ports/atmel-samd/common-hal/busio/I2C.c +++ b/ports/atmel-samd/common-hal/busio/I2C.c @@ -36,7 +36,7 @@ #include "shared-bindings/microcontroller/__init__.h" #include "supervisor/shared/translate.h" -#include "common-hal/busio/SPI.h" // for never_reset_sercom +#include "common-hal/busio/__init__.h" // Number of times to try to send packet if failed. #define ATTEMPTS 2 diff --git a/ports/atmel-samd/common-hal/busio/SPI.c b/ports/atmel-samd/common-hal/busio/SPI.c index 884c3e0414..3b60c1d2b4 100644 --- a/ports/atmel-samd/common-hal/busio/SPI.c +++ b/ports/atmel-samd/common-hal/busio/SPI.c @@ -32,7 +32,9 @@ #include "peripheral_clk_config.h" #include "supervisor/board.h" +#include "common-hal/busio/__init__.h" #include "common-hal/microcontroller/Pin.h" + #include "hal/include/hal_gpio.h" #include "hal/include/hal_spi_m_sync.h" #include "hal/include/hpl_spi_m_sync.h" @@ -40,43 +42,6 @@ #include "samd/dma.h" #include "samd/sercom.h" -bool never_reset_sercoms[SERCOM_INST_NUM]; - -void never_reset_sercom(Sercom *sercom) { - // Reset all SERCOMs except the ones being used by on-board devices. - Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS; - for (int i = 0; i < SERCOM_INST_NUM; i++) { - if (sercom_instances[i] == sercom) { - never_reset_sercoms[i] = true; - break; - } - } -} - -void allow_reset_sercom(Sercom *sercom) { - // Reset all SERCOMs except the ones being used by on-board devices. - Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS; - for (int i = 0; i < SERCOM_INST_NUM; i++) { - if (sercom_instances[i] == sercom) { - never_reset_sercoms[i] = false; - break; - } - } -} - -void reset_sercoms(void) { - // Reset all SERCOMs except the ones being used by on-board devices. - Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS; - for (int i = 0; i < SERCOM_INST_NUM; i++) { - if (never_reset_sercoms[i]) { - continue; - } - // SWRST is same for all modes of SERCOMs. - sercom_instances[i]->SPI.CTRLA.bit.SWRST = 1; - } -} - - 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) { diff --git a/ports/atmel-samd/common-hal/busio/SPI.h b/ports/atmel-samd/common-hal/busio/SPI.h index 27bbfdeb3a..2fced6d642 100644 --- a/ports/atmel-samd/common-hal/busio/SPI.h +++ b/ports/atmel-samd/common-hal/busio/SPI.h @@ -42,8 +42,4 @@ typedef struct { uint8_t MISO_pin; } busio_spi_obj_t; -void reset_sercoms(void); -void never_reset_sercom(Sercom *sercom); - - #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_SPI_H diff --git a/ports/atmel-samd/common-hal/busio/UART.c b/ports/atmel-samd/common-hal/busio/UART.c index c33bf6f440..2625283c83 100644 --- a/ports/atmel-samd/common-hal/busio/UART.c +++ b/ports/atmel-samd/common-hal/busio/UART.c @@ -45,7 +45,7 @@ #include "samd/sercom.h" -#include "common-hal/busio/SPI.h" // for never_reset_sercom +#include "common-hal/busio/__init__.h" #define UART_DEBUG(...) (void)0 // #define UART_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__) diff --git a/ports/atmel-samd/common-hal/busio/__init__.c b/ports/atmel-samd/common-hal/busio/__init__.c index 41761b6743..8ec549e931 100644 --- a/ports/atmel-samd/common-hal/busio/__init__.c +++ b/ports/atmel-samd/common-hal/busio/__init__.c @@ -1 +1,63 @@ -// No busio module functions. +/* + * 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 "samd/sercom.h" + +static bool never_reset_sercoms[SERCOM_INST_NUM]; + +void never_reset_sercom(Sercom *sercom) { + // Reset all SERCOMs except the ones being used by on-board devices. + Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS; + for (int i = 0; i < SERCOM_INST_NUM; i++) { + if (sercom_instances[i] == sercom) { + never_reset_sercoms[i] = true; + break; + } + } +} + +void allow_reset_sercom(Sercom *sercom) { + // Reset all SERCOMs except the ones being used by on-board devices. + Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS; + for (int i = 0; i < SERCOM_INST_NUM; i++) { + if (sercom_instances[i] == sercom) { + never_reset_sercoms[i] = false; + break; + } + } +} + +void reset_sercoms(void) { + // Reset all SERCOMs except the ones being used by on-board devices. + Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS; + for (int i = 0; i < SERCOM_INST_NUM; i++) { + if (never_reset_sercoms[i]) { + continue; + } + // SWRST is same for all modes of SERCOMs. + sercom_instances[i]->SPI.CTRLA.bit.SWRST = 1; + } +} diff --git a/ports/atmel-samd/common-hal/busio/__init__.h b/ports/atmel-samd/common-hal/busio/__init__.h new file mode 100644 index 0000000000..ded88f113d --- /dev/null +++ b/ports/atmel-samd/common-hal/busio/__init__.h @@ -0,0 +1,35 @@ +/* + * 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_ATMEL_SAMD_COMMON_HAL_BUSIO_INIT_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_INIT_H + +void reset_sercoms(void); +void allow_reset_sercom(Sercom *sercom); +void never_reset_sercom(Sercom *sercom); + + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_INIT_H diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index fec5d8e63d..932252a0d7 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -67,8 +67,8 @@ #include "common-hal/audioio/AudioOut.h" #endif -#if CIRCUITPY_BUSIO_SPI -#include "common-hal/busio/SPI.h" +#if CIRCUITPY_BUSIO +#include "common-hal/busio/__init__.h" #endif #include "common-hal/microcontroller/Pin.h"