kopia lustrzana https://github.com/micropython/micropython
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>pull/6669/head
rodzic
4ce6427bd7
commit
1e4e2644ec
|
@ -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);
|
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;
|
return i2c_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
pyb_uart_obj_t *self;
|
||||||
if (MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1] == NULL) {
|
if (MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1] == NULL) {
|
||||||
// create new UART object
|
// create new UART object
|
||||||
|
|
|
@ -127,6 +127,31 @@
|
||||||
#define MICROPY_HW_FLASH_FS_LABEL "pybflash"
|
#define MICROPY_HW_FLASH_FS_LABEL "pybflash"
|
||||||
#endif
|
#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
|
// General configuration
|
||||||
|
|
||||||
|
|
|
@ -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);
|
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;
|
pyb_can_obj_t *self;
|
||||||
if (MP_STATE_PORT(pyb_can_obj_all)[can_idx - 1] == NULL) {
|
if (MP_STATE_PORT(pyb_can_obj_all)[can_idx - 1] == NULL) {
|
||||||
self = m_new_obj(pyb_can_obj_t);
|
self = m_new_obj(pyb_can_obj_t);
|
||||||
|
|
|
@ -167,45 +167,52 @@ void spi_init0(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int spi_find_index(mp_obj_t id) {
|
int spi_find_index(mp_obj_t id) {
|
||||||
|
int spi_id;
|
||||||
if (mp_obj_is_str(id)) {
|
if (mp_obj_is_str(id)) {
|
||||||
// given a string id
|
// given a string id
|
||||||
const char *port = mp_obj_str_get_str(id);
|
const char *port = mp_obj_str_get_str(id);
|
||||||
if (0) {
|
if (0) {
|
||||||
#ifdef MICROPY_HW_SPI1_NAME
|
#ifdef MICROPY_HW_SPI1_NAME
|
||||||
} else if (strcmp(port, MICROPY_HW_SPI1_NAME) == 0) {
|
} else if (strcmp(port, MICROPY_HW_SPI1_NAME) == 0) {
|
||||||
return 1;
|
spi_id = 1;
|
||||||
#endif
|
#endif
|
||||||
#ifdef MICROPY_HW_SPI2_NAME
|
#ifdef MICROPY_HW_SPI2_NAME
|
||||||
} else if (strcmp(port, MICROPY_HW_SPI2_NAME) == 0) {
|
} else if (strcmp(port, MICROPY_HW_SPI2_NAME) == 0) {
|
||||||
return 2;
|
spi_id = 2;
|
||||||
#endif
|
#endif
|
||||||
#ifdef MICROPY_HW_SPI3_NAME
|
#ifdef MICROPY_HW_SPI3_NAME
|
||||||
} else if (strcmp(port, MICROPY_HW_SPI3_NAME) == 0) {
|
} else if (strcmp(port, MICROPY_HW_SPI3_NAME) == 0) {
|
||||||
return 3;
|
spi_id = 3;
|
||||||
#endif
|
#endif
|
||||||
#ifdef MICROPY_HW_SPI4_NAME
|
#ifdef MICROPY_HW_SPI4_NAME
|
||||||
} else if (strcmp(port, MICROPY_HW_SPI4_NAME) == 0) {
|
} else if (strcmp(port, MICROPY_HW_SPI4_NAME) == 0) {
|
||||||
return 4;
|
spi_id = 4;
|
||||||
#endif
|
#endif
|
||||||
#ifdef MICROPY_HW_SPI5_NAME
|
#ifdef MICROPY_HW_SPI5_NAME
|
||||||
} else if (strcmp(port, MICROPY_HW_SPI5_NAME) == 0) {
|
} else if (strcmp(port, MICROPY_HW_SPI5_NAME) == 0) {
|
||||||
return 5;
|
spi_id = 5;
|
||||||
#endif
|
#endif
|
||||||
#ifdef MICROPY_HW_SPI6_NAME
|
#ifdef MICROPY_HW_SPI6_NAME
|
||||||
} else if (strcmp(port, MICROPY_HW_SPI6_NAME) == 0) {
|
} else if (strcmp(port, MICROPY_HW_SPI6_NAME) == 0) {
|
||||||
return 6;
|
spi_id = 6;
|
||||||
#endif
|
#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 {
|
} else {
|
||||||
// given an integer id
|
// given an integer id
|
||||||
int spi_id = mp_obj_get_int(id);
|
spi_id = mp_obj_get_int(id);
|
||||||
if (spi_id >= 1 && spi_id <= MP_ARRAY_SIZE(spi_obj)
|
if (spi_id < 1 || spi_id > MP_ARRAY_SIZE(spi_obj) || spi_obj[spi_id - 1].spi == NULL) {
|
||||||
&& spi_obj[spi_id - 1].spi != NULL) {
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id);
|
||||||
return 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) {
|
STATIC uint32_t spi_get_source_freq(SPI_HandleTypeDef *spi) {
|
||||||
|
|
|
@ -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);
|
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;
|
pyb_timer_obj_t *tim;
|
||||||
if (MP_STATE_PORT(pyb_timer_obj_all)[tim_id - 1] == NULL) {
|
if (MP_STATE_PORT(pyb_timer_obj_all)[tim_id - 1] == NULL) {
|
||||||
// create new Timer object
|
// create new Timer object
|
||||||
|
|
Ładowanie…
Reference in New Issue