diff options
author | Damien George <damien.p.george@gmail.com> | 2014-10-23 14:25:32 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-10-23 14:25:32 +0100 |
commit | 185cb0d943aa73485a83e1c146d9db899973378d (patch) | |
tree | 4e3128ddb6e30ba15f666aabbddb82f8df3729c1 | |
parent | e7bb0443cda9bfb88791c3a99bf0331217390eb4 (diff) |
stmhal: Use OSError with POSIX error code for HAL errors.
Addresses issue #921.
-rw-r--r-- | stmhal/Makefile | 1 | ||||
-rw-r--r-- | stmhal/can.c | 9 | ||||
-rw-r--r-- | stmhal/i2c.c | 17 | ||||
-rw-r--r-- | stmhal/mphal.c | 20 | ||||
-rw-r--r-- | stmhal/mphal.h | 4 | ||||
-rw-r--r-- | stmhal/spi.c | 14 | ||||
-rw-r--r-- | stmhal/uart.c | 15 |
7 files changed, 42 insertions, 38 deletions
diff --git a/stmhal/Makefile b/stmhal/Makefile index b3b2d7a9b..878e6ca10 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 920a4ad0b..5600bfae0 100644 --- a/stmhal/can.c +++ b/stmhal/can.c @@ -29,8 +29,6 @@ #include <stdarg.h> #include <errno.h> -#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 3be981842..7678e9a23 100644 --- a/stmhal/i2c.c +++ b/stmhal/i2c.c @@ -27,8 +27,6 @@ #include <stdio.h> #include <string.h> -#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 000000000..e8f242051 --- /dev/null +++ b/stmhal/mphal.c @@ -0,0 +1,20 @@ +#include <errno.h> + +#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 33407622c..4b43cf886 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 007f22daf..22dc66419 100644 --- a/stmhal/spi.c +++ b/stmhal/spi.c @@ -27,8 +27,6 @@ #include <stdio.h> #include <string.h> -#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 9abe4713e..e06ee6b46 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -29,8 +29,6 @@ #include <stdarg.h> #include <errno.h> -#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; } } |