stmhal: Use OSError with POSIX error code for HAL errors.

Addresses issue #921.
This commit is contained in:
Damien George 2014-10-23 14:25:32 +01:00
parent e7bb0443cd
commit 185cb0d943
7 changed files with 42 additions and 38 deletions

View file

@ -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 \

View file

@ -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

View file

@ -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;

20
stmhal/mphal.c Normal file
View file

@ -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]));
}

View file

@ -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);

View file

@ -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

View file

@ -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;
}
}