stm32: Add support for a board to reserve certain peripherals.

Allows reserving CAN, I2C, SPI, Timer and UART peripherals.  If reserved
the peripheral cannot be accessed from Python.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2020-12-03 13:07:37 +11:00
parent 4ce6427bd7
commit 1e4e2644ec
6 changed files with 65 additions and 12 deletions

View File

@ -534,6 +534,12 @@ int i2c_find_peripheral(mp_obj_t id) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
}
// check if the I2C is reserved for system use or not
if (MICROPY_HW_I2C_IS_RESERVED(i2c_id)) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) is reserved"), i2c_id);
}
return i2c_id;
}

View File

@ -345,6 +345,11 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size
}
}
// check if the UART is reserved for system use or not
if (MICROPY_HW_UART_IS_RESERVED(uart_id)) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) is reserved"), uart_id);
}
pyb_uart_obj_t *self;
if (MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1] == NULL) {
// create new UART object

View File

@ -127,6 +127,31 @@
#define MICROPY_HW_FLASH_FS_LABEL "pybflash"
#endif
// Function to determine if the given can_id is reserved for system use or not.
#ifndef MICROPY_HW_CAN_IS_RESERVED
#define MICROPY_HW_CAN_IS_RESERVED(can_id) (false)
#endif
// Function to determine if the given i2c_id is reserved for system use or not.
#ifndef MICROPY_HW_I2C_IS_RESERVED
#define MICROPY_HW_I2C_IS_RESERVED(i2c_id) (false)
#endif
// Function to determine if the given spi_id is reserved for system use or not.
#ifndef MICROPY_HW_SPI_IS_RESERVED
#define MICROPY_HW_SPI_IS_RESERVED(spi_id) (false)
#endif
// Function to determine if the given tim_id is reserved for system use or not.
#ifndef MICROPY_HW_TIM_IS_RESERVED
#define MICROPY_HW_TIM_IS_RESERVED(tim_id) (false)
#endif
// Function to determine if the given uart_id is reserved for system use or not.
#ifndef MICROPY_HW_UART_IS_RESERVED
#define MICROPY_HW_UART_IS_RESERVED(uart_id) (false)
#endif
/*****************************************************************************/
// General configuration

View File

@ -203,6 +203,11 @@ STATIC mp_obj_t pyb_can_make_new(const mp_obj_type_t *type, size_t n_args, size_
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("CAN(%d) doesn't exist"), can_idx);
}
// check if the CAN is reserved for system use or not
if (MICROPY_HW_CAN_IS_RESERVED(can_idx)) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("CAN(%d) is reserved"), can_idx);
}
pyb_can_obj_t *self;
if (MP_STATE_PORT(pyb_can_obj_all)[can_idx - 1] == NULL) {
self = m_new_obj(pyb_can_obj_t);

View File

@ -167,45 +167,52 @@ void spi_init0(void) {
}
int spi_find_index(mp_obj_t id) {
int spi_id;
if (mp_obj_is_str(id)) {
// given a string id
const char *port = mp_obj_str_get_str(id);
if (0) {
#ifdef MICROPY_HW_SPI1_NAME
} else if (strcmp(port, MICROPY_HW_SPI1_NAME) == 0) {
return 1;
spi_id = 1;
#endif
#ifdef MICROPY_HW_SPI2_NAME
} else if (strcmp(port, MICROPY_HW_SPI2_NAME) == 0) {
return 2;
spi_id = 2;
#endif
#ifdef MICROPY_HW_SPI3_NAME
} else if (strcmp(port, MICROPY_HW_SPI3_NAME) == 0) {
return 3;
spi_id = 3;
#endif
#ifdef MICROPY_HW_SPI4_NAME
} else if (strcmp(port, MICROPY_HW_SPI4_NAME) == 0) {
return 4;
spi_id = 4;
#endif
#ifdef MICROPY_HW_SPI5_NAME
} else if (strcmp(port, MICROPY_HW_SPI5_NAME) == 0) {
return 5;
spi_id = 5;
#endif
#ifdef MICROPY_HW_SPI6_NAME
} else if (strcmp(port, MICROPY_HW_SPI6_NAME) == 0) {
return 6;
spi_id = 6;
#endif
} else {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%s) doesn't exist"), port);
}
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%s) doesn't exist"), port);
} else {
// given an integer id
int spi_id = mp_obj_get_int(id);
if (spi_id >= 1 && spi_id <= MP_ARRAY_SIZE(spi_obj)
&& spi_obj[spi_id - 1].spi != NULL) {
return spi_id;
spi_id = mp_obj_get_int(id);
if (spi_id < 1 || spi_id > MP_ARRAY_SIZE(spi_obj) || spi_obj[spi_id - 1].spi == NULL) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id);
}
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id);
}
// check if the SPI is reserved for system use or not
if (MICROPY_HW_SPI_IS_RESERVED(spi_id)) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) is reserved"), spi_id);
}
return spi_id;
}
STATIC uint32_t spi_get_source_freq(SPI_HandleTypeDef *spi) {

View File

@ -896,6 +896,11 @@ STATIC mp_obj_t pyb_timer_make_new(const mp_obj_type_t *type, size_t n_args, siz
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Timer(%d) doesn't exist"), tim_id);
}
// check if the timer is reserved for system use or not
if (MICROPY_HW_TIM_IS_RESERVED(tim_id)) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Timer(%d) is reserved"), tim_id);
}
pyb_timer_obj_t *tim;
if (MP_STATE_PORT(pyb_timer_obj_all)[tim_id - 1] == NULL) {
// create new Timer object