From 185cb0d943aa73485a83e1c146d9db899973378d Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 23 Oct 2014 14:25:32 +0100 Subject: [PATCH] stmhal: Use OSError with POSIX error code for HAL errors. Addresses issue #921. --- stmhal/Makefile | 1 + stmhal/can.c | 9 +++------ stmhal/i2c.c | 17 ++++++----------- stmhal/mphal.c | 20 ++++++++++++++++++++ stmhal/mphal.h | 4 ++++ stmhal/spi.c | 14 +++++--------- stmhal/uart.c | 15 +++------------ 7 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 stmhal/mphal.c diff --git a/stmhal/Makefile b/stmhal/Makefile index b3b2d7a9b4..878e6ca10b 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -80,6 +80,7 @@ SRC_C = \ usbd_desc_cdc_msc.c \ usbd_cdc_interface.c \ usbd_msc_storage.c \ + mphal.c \ irq.c \ pendsv.c \ systick.c \ diff --git a/stmhal/can.c b/stmhal/can.c index 920a4ad0b4..5600bfae04 100644 --- a/stmhal/can.c +++ b/stmhal/can.c @@ -29,8 +29,6 @@ #include #include -#include "stm32f4xx_hal.h" - #include "mpconfig.h" #include "nlr.h" #include "misc.h" @@ -41,6 +39,7 @@ #include "bufhelper.h" #include "can.h" #include "pybioctl.h" +#include MICROPY_HAL_H #if MICROPY_HW_ENABLE_CAN @@ -334,8 +333,7 @@ STATIC mp_obj_t pyb_can_send(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_ HAL_StatusTypeDef status = HAL_CAN_Transmit(&self->can, args[2].u_int); if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_CAN_Transmit failed with code %d", status)); + mp_hal_raise(status); } return mp_const_none; @@ -367,8 +365,7 @@ STATIC mp_obj_t pyb_can_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_ HAL_StatusTypeDef status = HAL_CAN_Receive(&self->can, args[0].u_int, args[1].u_int); if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_CAN_Receive failed with code %d", status)); + mp_hal_raise(status); } // return the received data diff --git a/stmhal/i2c.c b/stmhal/i2c.c index 3be981842e..7678e9a234 100644 --- a/stmhal/i2c.c +++ b/stmhal/i2c.c @@ -27,8 +27,6 @@ #include #include -#include "stm32f4xx_hal.h" - #include "mpconfig.h" #include "nlr.h" #include "misc.h" @@ -39,6 +37,7 @@ #include "genhdr/pins.h" #include "bufhelper.h" #include "i2c.h" +#include MICROPY_HAL_H /// \moduleref pyb /// \class I2C - a two-wire serial protocol @@ -148,7 +147,7 @@ void i2c_init(I2C_HandleTypeDef *i2c) { // init error // TODO should raise an exception, but this function is not necessarily going to be // called via Python, so may not be properly wrapped in an NLR handler - printf("HardwareError: HAL_I2C_Init failed\n"); + printf("OSError: HAL_I2C_Init failed\n"); return; } } @@ -390,8 +389,7 @@ STATIC mp_obj_t pyb_i2c_send(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k } if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_xxx_Transmit failed with code %d", status)); + mp_hal_raise(status); } return mp_const_none; @@ -440,8 +438,7 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k } if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_xxx_Receive failed with code %d", status)); + mp_hal_raise(status); } // return the received data @@ -501,8 +498,7 @@ STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_ HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len, vals[3].u_int); if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Mem_Read failed with code %d", status)); + mp_hal_raise(status); } // return the read data @@ -554,8 +550,7 @@ STATIC mp_obj_t pyb_i2c_mem_write(mp_uint_t n_args, const mp_obj_t *args, mp_map HAL_StatusTypeDef status = HAL_I2C_Mem_Write(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len, vals[3].u_int); if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Mem_Write failed with code %d", status)); + mp_hal_raise(status); } return mp_const_none; diff --git a/stmhal/mphal.c b/stmhal/mphal.c new file mode 100644 index 0000000000..e8f242051e --- /dev/null +++ b/stmhal/mphal.c @@ -0,0 +1,20 @@ +#include + +#include "mpconfig.h" +#include "nlr.h" +#include "misc.h" +#include "qstr.h" +#include "obj.h" +#include "mphal.h" + +// this table converts from HAL_StatusTypeDef to POSIX errno +const byte mp_hal_status_to_errno_table[4] = { + [HAL_OK] = 0, + [HAL_ERROR] = EIO, + [HAL_BUSY] = EBUSY, + [HAL_TIMEOUT] = ETIMEDOUT, +}; + +NORETURN void mp_hal_raise(HAL_StatusTypeDef status) { + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, (mp_obj_t)(mp_uint_t)mp_hal_status_to_errno_table[status])); +} diff --git a/stmhal/mphal.h b/stmhal/mphal.h index 33407622c0..4b43cf886d 100644 --- a/stmhal/mphal.h +++ b/stmhal/mphal.h @@ -6,3 +6,7 @@ #define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRRL) = (pin_mask)) #define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRRH) = (pin_mask)) #define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1) + +extern const byte mp_hal_status_to_errno_table[4]; + +NORETURN void mp_hal_raise(HAL_StatusTypeDef status); diff --git a/stmhal/spi.c b/stmhal/spi.c index 007f22daf8..22dc664197 100644 --- a/stmhal/spi.c +++ b/stmhal/spi.c @@ -27,8 +27,6 @@ #include #include -#include "stm32f4xx_hal.h" - #include "mpconfig.h" #include "nlr.h" #include "misc.h" @@ -39,6 +37,7 @@ #include "genhdr/pins.h" #include "bufhelper.h" #include "spi.h" +#include MICROPY_HAL_H /// \moduleref pyb /// \class SPI - a master-driven serial protocol @@ -140,7 +139,7 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) { // init error // TODO should raise an exception, but this function is not necessarily going to be // called via Python, so may not be properly wrapped in an NLR handler - printf("HardwareError: HAL_SPI_Init failed\n"); + printf("OSError: HAL_SPI_Init failed\n"); return; } } @@ -387,8 +386,7 @@ STATIC mp_obj_t pyb_spi_send(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k HAL_StatusTypeDef status = HAL_SPI_Transmit(self->spi, bufinfo.buf, bufinfo.len, vals[1].u_int); if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_SPI_Transmit failed with code %d", status)); + mp_hal_raise(status); } return mp_const_none; @@ -428,8 +426,7 @@ STATIC mp_obj_t pyb_spi_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k HAL_StatusTypeDef status = HAL_SPI_Receive(self->spi, bufinfo.buf, bufinfo.len, vals[1].u_int); if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_SPI_Receive failed with code %d", status)); + mp_hal_raise(status); } // return the received data @@ -503,8 +500,7 @@ STATIC mp_obj_t pyb_spi_send_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map HAL_StatusTypeDef status = HAL_SPI_TransmitReceive(self->spi, bufinfo_send.buf, bufinfo_recv.buf, bufinfo_send.len, vals[2].u_int); if (status != HAL_OK) { - // TODO really need a HardwareError object, or something - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_SPI_TransmitReceive failed with code %d", status)); + mp_hal_raise(status); } // return the received data diff --git a/stmhal/uart.c b/stmhal/uart.c index 9abe4713ed..e06ee6b46e 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -29,8 +29,6 @@ #include #include -#include "stm32f4xx_hal.h" - #include "mpconfig.h" #include "nlr.h" #include "misc.h" @@ -40,6 +38,7 @@ #include "stream.h" #include "uart.h" #include "pybioctl.h" +#include MICROPY_HAL_H /// \moduleref pyb /// \class UART - duplex serial communication bus @@ -94,14 +93,6 @@ struct _pyb_uart_obj_t { byte *read_buf; // byte or uint16_t, depending on char size }; -// this table converts from HAL_StatusTypeDef to POSIX errno -STATIC const byte hal_status_to_errno_table[4] = { - [HAL_OK] = 0, - [HAL_ERROR] = EIO, - [HAL_BUSY] = EBUSY, - [HAL_TIMEOUT] = ETIMEDOUT, -}; - // pointers to all UART objects (if they have been created) STATIC pyb_uart_obj_t *pyb_uart_obj_all[6]; @@ -563,7 +554,7 @@ STATIC mp_obj_t pyb_uart_writechar(mp_obj_t self_in, mp_obj_t char_in) { HAL_StatusTypeDef status = HAL_UART_Transmit(&self->uart, (uint8_t*)&data, 1, self->timeout); if (status != HAL_OK) { - nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, (mp_obj_t)(mp_uint_t)hal_status_to_errno_table[status])); + mp_hal_raise(status); } return mp_const_none; @@ -713,7 +704,7 @@ STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t // return number of bytes written return size; } else { - *errcode = hal_status_to_errno_table[status]; + *errcode = mp_hal_status_to_errno_table[status]; return MP_STREAM_ERROR; } }