message consolidation and more use of validators

This commit is contained in:
Dan Halbert 2022-05-13 15:33:43 -04:00
parent f975c97c63
commit a01dec1df9
151 changed files with 557 additions and 1035 deletions

View File

@ -57,9 +57,9 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
self->value = mp_obj_new_bytes(initial_value_bufinfo->buf, initial_value_bufinfo->len); self->value = mp_obj_new_bytes(initial_value_bufinfo->buf, initial_value_bufinfo->len);
const mp_int_t max_length_max = 512; const mp_int_t max_length_max = 512;
if (max_length < 0 || max_length > max_length_max) {
mp_raise_ValueError(translate("max_length must be <= 512")); mp_arg_validate_int_range(max_length, 0, max_length_max, MP_QSTR_max_length);
}
self->max_length = max_length; self->max_length = max_length;
self->fixed_length = fixed_length; self->fixed_length = fixed_length;

View File

@ -30,6 +30,11 @@
#define mp_obj_fat_vfs_t fs_user_mount_t #define mp_obj_fat_vfs_t fs_user_mount_t
// Factoring this common call saves about 90 bytes.
STATIC NORETURN void mp_raise_OSError_fresult(FRESULT res) {
mp_raise_OSError(fresult_to_errno_table[res]);
}
STATIC mp_import_stat_t fat_vfs_import_stat(void *vfs_in, const char *path) { STATIC mp_import_stat_t fat_vfs_import_stat(void *vfs_in, const char *path) {
fs_user_mount_t *vfs = vfs_in; fs_user_mount_t *vfs = vfs_in;
FILINFO fno; FILINFO fno;
@ -64,7 +69,7 @@ STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, size_
// don't error out if no filesystem, to let mkfs()/mount() create one if wanted // don't error out if no filesystem, to let mkfs()/mount() create one if wanted
vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NO_FILESYSTEM; vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NO_FILESYSTEM;
} else if (res != FR_OK) { } else if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
return MP_OBJ_FROM_PTR(vfs); return MP_OBJ_FROM_PTR(vfs);
@ -97,7 +102,7 @@ STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) {
res = f_mkfs(&vfs->fatfs, FM_FAT32, 0, working_buf, sizeof(working_buf)); res = f_mkfs(&vfs->fatfs, FM_FAT32, 0, working_buf, sizeof(working_buf));
} }
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
return mp_const_none; return mp_const_none;
@ -172,7 +177,7 @@ STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
iter->is_str = is_str_type; iter->is_str = is_str_type;
FRESULT res = f_opendir(&self->fatfs, &iter->dir, path); FRESULT res = f_opendir(&self->fatfs, &iter->dir, path);
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
return MP_OBJ_FROM_PTR(iter); return MP_OBJ_FROM_PTR(iter);
@ -188,7 +193,7 @@ STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_in
FRESULT res = f_stat(&self->fatfs, path, &fno); FRESULT res = f_stat(&self->fatfs, path, &fno);
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
// check if path is a file or directory // check if path is a file or directory
@ -196,7 +201,7 @@ STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_in
res = f_unlink(&self->fatfs, path); res = f_unlink(&self->fatfs, path);
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
return mp_const_none; return mp_const_none;
} else { } else {
@ -226,7 +231,7 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_
FILINFO fno; FILINFO fno;
FRESULT res = f_stat(&self->fatfs, old_path, &fno); FRESULT res = f_stat(&self->fatfs, old_path, &fno);
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
if ((fno.fattrib & AM_DIR) != 0 && if ((fno.fattrib & AM_DIR) != 0 &&
strlen(new_path) > strlen(old_path) && strlen(new_path) > strlen(old_path) &&
@ -245,7 +250,7 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_
if (res == FR_OK) { if (res == FR_OK) {
return mp_const_none; return mp_const_none;
} else { } else {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
} }
@ -259,7 +264,7 @@ STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
if (res == FR_OK) { if (res == FR_OK) {
return mp_const_none; return mp_const_none;
} else { } else {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir); STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
@ -273,7 +278,7 @@ STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) {
FRESULT res = f_chdir(&self->fatfs, path); FRESULT res = f_chdir(&self->fatfs, path);
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
return mp_const_none; return mp_const_none;
@ -286,7 +291,7 @@ STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) {
char buf[MICROPY_ALLOC_PATH_MAX + 1]; char buf[MICROPY_ALLOC_PATH_MAX + 1];
FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf)); FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf));
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
return mp_obj_new_str(buf, strlen(buf)); return mp_obj_new_str(buf, strlen(buf));
} }
@ -307,7 +312,7 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
} else { } else {
FRESULT res = f_stat(&self->fatfs, path, &fno); FRESULT res = f_stat(&self->fatfs, path, &fno);
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
} }
@ -357,7 +362,7 @@ STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
FATFS *fatfs = &self->fatfs; FATFS *fatfs = &self->fatfs;
FRESULT res = f_getfree(fatfs, &nclst); FRESULT res = f_getfree(fatfs, &nclst);
if (FR_OK != res) { if (FR_OK != res) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
@ -395,7 +400,7 @@ STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs
res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
} }
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
self->blockdev.flags &= ~MP_BLOCKDEV_FLAG_NO_FILESYSTEM; self->blockdev.flags &= ~MP_BLOCKDEV_FLAG_NO_FILESYSTEM;
@ -416,7 +421,7 @@ STATIC mp_obj_t vfs_fat_getlabel(mp_obj_t self_in) {
char working_buf[12]; char working_buf[12];
FRESULT res = f_getlabel(&self->fatfs, working_buf, NULL); FRESULT res = f_getlabel(&self->fatfs, working_buf, NULL);
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
return mp_obj_new_str(working_buf, strlen(working_buf)); return mp_obj_new_str(working_buf, strlen(working_buf));
} }
@ -431,7 +436,7 @@ STATIC mp_obj_t vfs_fat_setlabel(mp_obj_t self_in, mp_obj_t label_in) {
if (res == FR_WRITE_PROTECTED) { if (res == FR_WRITE_PROTECTED) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Read-only filesystem")); mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Read-only filesystem"));
} }
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError_fresult(res);
} }
return mp_const_none; return mp_const_none;
} }

View File

@ -182,7 +182,7 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar
} }
if (rwxa_count != 1 || plus_count > 1 || bt_count > 1 || bad_mode) { if (rwxa_count != 1 || plus_count > 1 || bt_count > 1 || bad_mode) {
mp_raise_ValueError(translate("Invalid mode")); mp_arg_error_invalid(MP_QSTR_mode);
} }
assert(vfs != NULL); assert(vfs != NULL);

File diff suppressed because it is too large Load Diff

View File

@ -234,7 +234,7 @@ static void pinalarm_set_alarms_light(size_t n_alarms, const mp_obj_t *alarms) {
// raise ValueError here // raise ValueError here
MP_FALLTHROUGH MP_FALLTHROUGH
case PINALARM_ERR_NOEXTINT: case PINALARM_ERR_NOEXTINT:
mp_raise_RuntimeError(translate("No hardware support on pin")); raise_ValueError_invalid_pin();
case PINALARM_ERR_NOCHANNEL: case PINALARM_ERR_NOCHANNEL:
mp_raise_RuntimeError(translate("A hardware interrupt channel is already in use")); mp_raise_RuntimeError(translate("A hardware interrupt channel is already in use"));
default: default:
@ -261,7 +261,7 @@ static void pinalarm_set_alarms_deep(size_t n_alarms, const mp_obj_t *alarms) {
} }
} }
if (t->n < 0) { if (t->n < 0) {
mp_raise_ValueError(translate("Pin cannot wake from Deep Sleep")); mp_raise_ValueError(translate("Pin cannot wake from deep sleep"));
} }
// It is strange, but to my experiment, interrupt during sleep // It is strange, but to my experiment, interrupt during sleep

View File

@ -100,7 +100,7 @@ void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_
continue; continue;
} }
if (timealarm_set) { if (timealarm_set) {
mp_raise_ValueError(translate("Only one alarm.time alarm can be set.")); mp_raise_ValueError(translate("Only one alarm.time alarm can be set"));
} }
timealarm = MP_OBJ_TO_PTR(alarms[i]); timealarm = MP_OBJ_TO_PTR(alarms[i]);
timealarm_set = true; timealarm_set = true;

View File

@ -36,6 +36,7 @@
#include "samd/adc.h" #include "samd/adc.h"
#include "shared-bindings/analogio/AnalogIn.h" #include "shared-bindings/analogio/AnalogIn.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
#include "atmel_start_pins.h" #include "atmel_start_pins.h"
@ -60,7 +61,7 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self,
} }
if (adc_channel == 0xff) { if (adc_channel == 0xff) {
// No ADC function on that pin // No ADC function on that pin
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); raise_ValueError_invalid_pin();
} }
claim_pin(pin); claim_pin(pin);

View File

@ -70,7 +70,7 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self,
#endif #endif
default: default:
mp_raise_ValueError(translate("AnalogOut not supported on given pin")); raise_ValueError_invalid_pin();
return; return;
} }

View File

@ -255,7 +255,7 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t *self,
} }
uint8_t channel_count = audiosample_channel_count(sample); uint8_t channel_count = audiosample_channel_count(sample);
if (channel_count > 2) { if (channel_count > 2) {
mp_raise_ValueError(translate("Too many channels in sample.")); mp_raise_ValueError(translate("Too many channels in sample"));
} }
#ifdef SAMD21 #ifdef SAMD21
uint32_t serctrl = (self->clock_unit << I2S_SERCTRL_CLKSEL_Pos) | SERCTRL(SERMODE_TX) | I2S_SERCTRL_TXSAME_SAME | I2S_SERCTRL_EXTEND_MSBIT | I2S_SERCTRL_TXDEFAULT_ONE | I2S_SERCTRL_SLOTADJ_LEFT; uint32_t serctrl = (self->clock_unit << I2S_SERCTRL_CLKSEL_Pos) | SERCTRL(SERMODE_TX) | I2S_SERCTRL_TXSAME_SAME | I2S_SERCTRL_EXTEND_MSBIT | I2S_SERCTRL_TXDEFAULT_ONE | I2S_SERCTRL_SLOTADJ_LEFT;

View File

@ -136,7 +136,7 @@ void common_hal_audioio_audioout_construct(audioio_audioout_obj_t *self,
mp_raise_ValueError(translate("Right channel unsupported")); mp_raise_ValueError(translate("Right channel unsupported"));
} }
if (left_channel != &pin_PA02) { if (left_channel != &pin_PA02) {
mp_raise_ValueError(translate("Invalid pin")); raise_ValueError_invalid_pin();
} }
claim_pin(left_channel); claim_pin(left_channel);
#endif #endif
@ -376,14 +376,13 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t *self,
audio_dma_result result = AUDIO_DMA_OK; audio_dma_result result = AUDIO_DMA_OK;
uint32_t sample_rate = audiosample_sample_rate(sample); uint32_t sample_rate = audiosample_sample_rate(sample);
#ifdef SAMD21 #ifdef SAMD21
uint32_t max_sample_rate = 350000; const uint32_t max_sample_rate = 350000;
#endif #endif
#ifdef SAM_D5X_E5X #ifdef SAM_D5X_E5X
uint32_t max_sample_rate = 1000000; const uint32_t max_sample_rate = 1000000;
#endif #endif
if (sample_rate > max_sample_rate) { mp_arg_validate_int_max(sample_rate, max_sample_rate, MP_QSTR_sample_rate);
mp_raise_ValueError_varg(translate("Sample rate too high. It must be less than %d"), max_sample_rate);
}
#ifdef SAMD21 #ifdef SAMD21
result = audio_dma_setup_playback(&self->left_dma, sample, loop, true, 0, result = audio_dma_setup_playback(&self->left_dma, sample, loop, true, 0,
false /* output unsigned */, false /* output unsigned */,

View File

@ -34,6 +34,7 @@
#include "samd/sercom.h" #include "samd/sercom.h"
#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
#include "common-hal/busio/__init__.h" #include "common-hal/busio/__init__.h"
@ -76,7 +77,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
self->sda_pin = NO_PIN; self->sda_pin = NO_PIN;
Sercom *sercom = samd_i2c_get_sercom(scl, sda, &sercom_index, &sda_pinmux, &scl_pinmux); Sercom *sercom = samd_i2c_get_sercom(scl, sda, &sercom_index, &sda_pinmux, &scl_pinmux);
if (sercom == NULL) { if (sercom == NULL) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
#if CIRCUITPY_REQUIRE_I2C_PULLUPS #if CIRCUITPY_REQUIRE_I2C_PULLUPS
@ -122,15 +123,12 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
// The maximum frequency divisor gives a clock rate of around 48MHz/2/255 // The maximum frequency divisor gives a clock rate of around 48MHz/2/255
// but set_baudrate does not diagnose this problem. (This is not the // but set_baudrate does not diagnose this problem. (This is not the
// exact cutoff, but no frequency well under 100kHz is available) // exact cutoff, but no frequency well under 100kHz is available)
if (frequency < 95000) { if (frequency < 95000 &&
mp_raise_ValueError(translate("Unsupported baudrate")); i2c_m_sync_set_baudrate(&self->i2c_desc, 0, frequency / 1000) != ERR_NONE) {
}
if (i2c_m_sync_set_baudrate(&self->i2c_desc, 0, frequency / 1000) != ERR_NONE) {
reset_pin_number(sda->number); reset_pin_number(sda->number);
reset_pin_number(scl->number); reset_pin_number(scl->number);
common_hal_busio_i2c_deinit(self); common_hal_busio_i2c_deinit(self);
mp_raise_ValueError(translate("Unsupported baudrate")); mp_raise_ValueError(translate("Unsupported frequency"));
} }
self->sda_pin = sda->number; self->sda_pin = sda->number;

View File

@ -25,6 +25,7 @@
*/ */
#include "shared-bindings/busio/SPI.h" #include "shared-bindings/busio/SPI.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/runtime.h" #include "py/runtime.h"
@ -33,7 +34,6 @@
#include "supervisor/board.h" #include "supervisor/board.h"
#include "common-hal/busio/__init__.h" #include "common-hal/busio/__init__.h"
#include "common-hal/microcontroller/Pin.h"
#include "hal/include/hal_gpio.h" #include "hal/include/hal_gpio.h"
#include "hal/include/hal_spi_m_sync.h" #include "hal/include/hal_spi_m_sync.h"
@ -133,7 +133,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
} }
} }
if (sercom == NULL) { if (sercom == NULL) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
// Set up SPI clocks on SERCOM. // Set up SPI clocks on SERCOM.

View File

@ -25,6 +25,7 @@
*/ */
#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/UART.h" #include "shared-bindings/busio/UART.h"
#include "mpconfigport.h" #include "mpconfigport.h"
@ -79,9 +80,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_raise_ValueError(translate("RTS/CTS/RS485 Not yet supported on this device")); mp_raise_ValueError(translate("RTS/CTS/RS485 Not yet supported on this device"));
} }
if (bits > 8) { mp_arg_validate_int_max(bits, 8, MP_QSTR_bits);
mp_raise_NotImplementedError(translate("bytes > 8 bits not supported"));
}
bool have_tx = tx != NULL; bool have_tx = tx != NULL;
bool have_rx = rx != NULL; bool have_rx = rx != NULL;
@ -145,7 +144,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
} }
} }
if (sercom == NULL) { if (sercom == NULL) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
if (!have_tx) { if (!have_tx) {
tx_pad = 0; tx_pad = 0;
@ -175,7 +174,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
self->buffer = (uint8_t *)gc_alloc(self->buffer_length * sizeof(uint8_t), false, true); self->buffer = (uint8_t *)gc_alloc(self->buffer_length * sizeof(uint8_t), false, true);
if (self->buffer == NULL) { if (self->buffer == NULL) {
common_hal_busio_uart_deinit(self); common_hal_busio_uart_deinit(self);
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), self->buffer_length * sizeof(uint8_t)); m_malloc_fail(self->buffer_length * sizeof(uint8_t));
} }
} }
} else { } else {

View File

@ -45,6 +45,7 @@
#include "peripheral_clk_config.h" #include "peripheral_clk_config.h"
#include "hpl_gclk_config.h" #include "hpl_gclk_config.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/time/__init__.h" #include "shared-bindings/time/__init__.h"
#include "supervisor/shared/tick.h" #include "supervisor/shared/tick.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
@ -282,11 +283,11 @@ static void frequencyin_samd51_stop_dpll(void) {
void common_hal_frequencyio_frequencyin_construct(frequencyio_frequencyin_obj_t* self, const mcu_pin_obj_t* pin, const uint16_t capture_period) { void common_hal_frequencyio_frequencyin_construct(frequencyio_frequencyin_obj_t* self, const mcu_pin_obj_t* pin, const uint16_t capture_period) {
if (!pin->has_extint) { if (!pin->has_extint) {
mp_raise_RuntimeError(translate("No hardware support on pin")); raise_ValueError_invalid_pin();
}
if ((capture_period == 0) || (capture_period > 500)) {
mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
} }
mp_arg_validate_int_range(capture_period, 0, 500, MP_QSTR_capture_period);
uint32_t mask = 1 << pin->extint_channel; uint32_t mask = 1 << pin->extint_channel;
if (eic_get_enable() == 1 && if (eic_get_enable() == 1 &&
#ifdef SAMD21 #ifdef SAMD21
@ -569,9 +570,7 @@ uint16_t common_hal_frequencyio_frequencyin_get_capture_period(frequencyio_frequ
} }
void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequencyin_obj_t *self, uint16_t capture_period) { void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequencyin_obj_t *self, uint16_t capture_period) {
if ((capture_period == 0) || (capture_period > 500)) { mp_arg_validate_int_range(capture_period, 1, 500, MP_QSTR_capture_period);
mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
}
self->capture_period = capture_period; self->capture_period = capture_period;

View File

@ -25,6 +25,7 @@
*/ */
#include "shared-bindings/i2cperipheral/I2CPeripheral.h" #include "shared-bindings/i2cperipheral/I2CPeripheral.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "common-hal/busio/I2C.h" #include "common-hal/busio/I2C.h"
#include "shared/runtime/interrupt_char.h" #include "shared/runtime/interrupt_char.h"
@ -42,7 +43,7 @@ void common_hal_i2cperipheral_i2c_peripheral_construct(i2cperipheral_i2c_periphe
uint32_t sda_pinmux, scl_pinmux; uint32_t sda_pinmux, scl_pinmux;
Sercom *sercom = samd_i2c_get_sercom(scl, sda, &sercom_index, &sda_pinmux, &scl_pinmux); Sercom *sercom = samd_i2c_get_sercom(scl, sda, &sercom_index, &sda_pinmux, &scl_pinmux);
if (sercom == NULL) { if (sercom == NULL) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
self->sercom = sercom; self->sercom = sercom;

View File

@ -63,7 +63,7 @@ void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_paralle
} }
// The peripheral supports 8, 10, 12, or 14 data bits, but the code only supports 8 at present // The peripheral supports 8, 10, 12, or 14 data bits, but the code only supports 8 at present
if (data_count != 8) { if (data_count != 8) {
mp_raise_ValueError_varg(translate("Invalid data_count %d"), data_count); mp_arg_error_invalid(MP_QSTR_datacount);
} }
if (vertical_sync && vertical_sync->number != PIN_PCC_DEN1) { if (vertical_sync && vertical_sync->number != PIN_PCC_DEN1) {
mp_raise_ValueError_varg(translate("Invalid %q pin"), MP_QSTR_vsync); mp_raise_ValueError_varg(translate("Invalid %q pin"), MP_QSTR_vsync);

View File

@ -66,7 +66,7 @@ void common_hal_mcu_enable_interrupts(void) {
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
if (runmode == RUNMODE_BOOTLOADER) { if (runmode == RUNMODE_BOOTLOADER) {
if (!bootloader_available()) { if (!bootloader_available()) {
mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present.")); mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present"));
} }
// Pretend to be the first of the two reset presses needed to enter the // Pretend to be the first of the two reset presses needed to enter the
// bootloader. That way one reset will end in the bootloader. // bootloader. That way one reset will end in the bootloader.

View File

@ -79,7 +79,7 @@ static void resume_interrupt(ps2io_ps2_obj_t *self) {
disable_interrupt(self); disable_interrupt(self);
self->state = STATE_IDLE; self->state = STATE_IDLE;
gpio_set_pin_function(self->clk_pin, GPIO_PIN_FUNCTION_A); gpio_set_pin_function(self->clock_pin, GPIO_PIN_FUNCTION_A);
uint32_t mask = 1 << self->channel; uint32_t mask = 1 << self->channel;
EIC->INTFLAG.reg = mask << EIC_INTFLAG_EXTINT_Pos; EIC->INTFLAG.reg = mask << EIC_INTFLAG_EXTINT_Pos;
EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos; EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos;
@ -90,14 +90,14 @@ static void resume_interrupt(ps2io_ps2_obj_t *self) {
static void clk_hi(ps2io_ps2_obj_t *self) { static void clk_hi(ps2io_ps2_obj_t *self) {
// External pull-up // External pull-up
// Must set pull after setting direction. // Must set pull after setting direction.
gpio_set_pin_direction(self->clk_pin, GPIO_DIRECTION_IN); gpio_set_pin_direction(self->clock_pin, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(self->clk_pin, GPIO_PULL_OFF); gpio_set_pin_pull_mode(self->clock_pin, GPIO_PULL_OFF);
} }
static bool wait_clk_lo(ps2io_ps2_obj_t *self, uint32_t us) { static bool wait_clk_lo(ps2io_ps2_obj_t *self, uint32_t us) {
clk_hi(self); clk_hi(self);
common_hal_mcu_delay_us(1); common_hal_mcu_delay_us(1);
while (gpio_get_pin_level(self->clk_pin) && us) { while (gpio_get_pin_level(self->clock_pin) && us) {
--us; --us;
common_hal_mcu_delay_us(1); common_hal_mcu_delay_us(1);
} }
@ -107,7 +107,7 @@ static bool wait_clk_lo(ps2io_ps2_obj_t *self, uint32_t us) {
static bool wait_clk_hi(ps2io_ps2_obj_t *self, uint32_t us) { static bool wait_clk_hi(ps2io_ps2_obj_t *self, uint32_t us) {
clk_hi(self); clk_hi(self);
common_hal_mcu_delay_us(1); common_hal_mcu_delay_us(1);
while (!gpio_get_pin_level(self->clk_pin) && us) { while (!gpio_get_pin_level(self->clock_pin) && us) {
--us; --us;
common_hal_mcu_delay_us(1); common_hal_mcu_delay_us(1);
} }
@ -115,9 +115,9 @@ static bool wait_clk_hi(ps2io_ps2_obj_t *self, uint32_t us) {
} }
static void clk_lo(ps2io_ps2_obj_t *self) { static void clk_lo(ps2io_ps2_obj_t *self) {
gpio_set_pin_pull_mode(self->clk_pin, GPIO_PULL_OFF); gpio_set_pin_pull_mode(self->clock_pin, GPIO_PULL_OFF);
gpio_set_pin_direction(self->clk_pin, GPIO_DIRECTION_OUT); gpio_set_pin_direction(self->clock_pin, GPIO_DIRECTION_OUT);
gpio_set_pin_level(self->clk_pin, 0); gpio_set_pin_level(self->clock_pin, 0);
} }
static void data_hi(ps2io_ps2_obj_t *self) { static void data_hi(ps2io_ps2_obj_t *self) {
@ -244,19 +244,19 @@ void ps2_interrupt_handler(uint8_t channel) {
} }
void common_hal_ps2io_ps2_construct(ps2io_ps2_obj_t *self, void common_hal_ps2io_ps2_construct(ps2io_ps2_obj_t *self,
const mcu_pin_obj_t *data_pin, const mcu_pin_obj_t *clk_pin) { const mcu_pin_obj_t *data_pin, const mcu_pin_obj_t *clock_pin) {
if (!clk_pin->has_extint) { if (!clock_pin->has_extint) {
mp_raise_RuntimeError(translate("No hardware support on clk pin")); mp_arg_error_invalid(MP_QSTR_clock_pin);
} }
if (eic_get_enable() && !eic_channel_free(clk_pin->extint_channel)) { if (eic_get_enable() && !eic_channel_free(clock_pin->extint_channel)) {
mp_raise_RuntimeError(translate("EXTINT channel already in use")); mp_raise_RuntimeError(translate("EXTINT channel already in use"));
} }
clk_hi(self); clk_hi(self);
data_hi(self); data_hi(self);
self->channel = clk_pin->extint_channel; self->channel = clock_pin->extint_channel;
self->clk_pin = clk_pin->number; self->clock_pin = clock_pin->number;
self->data_pin = data_pin->number; self->data_pin = data_pin->number;
self->state = STATE_IDLE; self->state = STATE_IDLE;
self->bufcount = 0; self->bufcount = 0;
@ -264,19 +264,19 @@ void common_hal_ps2io_ps2_construct(ps2io_ps2_obj_t *self,
self->bufposw = 0; self->bufposw = 0;
self->waiting_cmd_response = false; self->waiting_cmd_response = false;
set_eic_channel_data(clk_pin->extint_channel, (void *)self); set_eic_channel_data(clock_pin->extint_channel, (void *)self);
// Check to see if the EIC is enabled and start it up if its not.' // Check to see if the EIC is enabled and start it up if its not.'
if (eic_get_enable() == 0) { if (eic_get_enable() == 0) {
turn_on_external_interrupt_controller(); turn_on_external_interrupt_controller();
} }
gpio_set_pin_function(clk_pin->number, GPIO_PIN_FUNCTION_A); gpio_set_pin_function(clock_pin->number, GPIO_PIN_FUNCTION_A);
gpio_set_pin_function(data_pin->number, GPIO_PIN_FUNCTION_A); gpio_set_pin_function(data_pin->number, GPIO_PIN_FUNCTION_A);
turn_on_cpu_interrupt(self->channel); turn_on_cpu_interrupt(self->channel);
claim_pin(clk_pin); claim_pin(clock_pin);
claim_pin(data_pin); claim_pin(data_pin);
// Set config will enable the EIC. // Set config will enable the EIC.
@ -284,7 +284,7 @@ void common_hal_ps2io_ps2_construct(ps2io_ps2_obj_t *self,
} }
bool common_hal_ps2io_ps2_deinited(ps2io_ps2_obj_t *self) { bool common_hal_ps2io_ps2_deinited(ps2io_ps2_obj_t *self) {
return self->clk_pin == NO_PIN; return self->clock_pin == NO_PIN;
} }
void common_hal_ps2io_ps2_deinit(ps2io_ps2_obj_t *self) { void common_hal_ps2io_ps2_deinit(ps2io_ps2_obj_t *self) {
@ -293,9 +293,9 @@ void common_hal_ps2io_ps2_deinit(ps2io_ps2_obj_t *self) {
} }
set_eic_handler(self->channel, EIC_HANDLER_NO_INTERRUPT); set_eic_handler(self->channel, EIC_HANDLER_NO_INTERRUPT);
turn_off_eic_channel(self->channel); turn_off_eic_channel(self->channel);
reset_pin_number(self->clk_pin); reset_pin_number(self->clock_pin);
reset_pin_number(self->data_pin); reset_pin_number(self->data_pin);
self->clk_pin = NO_PIN; self->clock_pin = NO_PIN;
self->data_pin = NO_PIN; self->data_pin = NO_PIN;
} }

View File

@ -35,7 +35,7 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
uint8_t channel; uint8_t channel;
uint8_t clk_pin; uint8_t clock_pin;
uint8_t data_pin; uint8_t data_pin;
uint8_t state; uint8_t state;

View File

@ -41,6 +41,7 @@
#include "samd/pins.h" #include "samd/pins.h"
#include "samd/timers.h" #include "samd/timers.h"
#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/pulseio/PulseIn.h" #include "shared-bindings/pulseio/PulseIn.h"
#include "supervisor/shared/tick.h" #include "supervisor/shared/tick.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
@ -152,7 +153,7 @@ void pulsein_reset() {
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) { const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) {
if (!pin->has_extint) { if (!pin->has_extint) {
mp_raise_RuntimeError(translate("No hardware support on pin")); raise_ValueError_invalid_pin();
} }
if (eic_get_enable() && !eic_channel_free(pin->extint_channel)) { if (eic_get_enable() && !eic_channel_free(pin->extint_channel)) {
mp_raise_RuntimeError(translate("EXTINT channel already in use")); mp_raise_RuntimeError(translate("EXTINT channel already in use"));
@ -160,7 +161,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false); self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
if (self->buffer == NULL) { if (self->buffer == NULL) {
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); m_malloc_fail(maxlen * sizeof(uint16_t));
} }
self->channel = pin->extint_channel; self->channel = pin->extint_channel;
self->pin = pin->number; self->pin = pin->number;
@ -344,15 +345,14 @@ bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
return (EIC->INTENSET.reg & (mask << EIC_INTENSET_EXTINT_Pos)) == 0; return (EIC->INTENSET.reg & (mask << EIC_INTENSET_EXTINT_Pos)) == 0;
} }
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, int16_t index) {
int16_t index) {
common_hal_mcu_disable_interrupts(); common_hal_mcu_disable_interrupts();
if (index < 0) { if (index < 0) {
index += self->len; index += self->len;
} }
if (index < 0 || index >= self->len) { if (index < 0 || index >= self->len) {
common_hal_mcu_enable_interrupts(); common_hal_mcu_enable_interrupts();
mp_raise_IndexError_varg(translate("%q index out of range"), MP_QSTR_PulseIn); mp_raise_IndexError_varg(translate("%q out of range"), MP_QSTR_index);
} }
uint16_t value = self->buffer[(self->start + index) % self->maxlen]; uint16_t value = self->buffer[(self->start + index) % self->maxlen];
common_hal_mcu_enable_interrupts(); common_hal_mcu_enable_interrupts();

View File

@ -377,7 +377,7 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self,
uint32_t frequency) { uint32_t frequency) {
uint32_t system_clock = common_hal_mcu_processor_get_frequency(); uint32_t system_clock = common_hal_mcu_processor_get_frequency();
if (frequency == 0 || frequency > system_clock / 2) { if (frequency == 0 || frequency > system_clock / 2) {
mp_raise_ValueError(translate("Invalid PWM frequency")); mp_arg_error_invalid(MP_QSTR_frequency);
} }
const pin_timer_t *t = self->timer; const pin_timer_t *t = self->timer;
uint8_t resolution; uint8_t resolution;

View File

@ -68,13 +68,7 @@ int common_hal_rtc_get_calibration(void) {
} }
void common_hal_rtc_set_calibration(int calibration) { void common_hal_rtc_set_calibration(int calibration) {
if (calibration > 127 || calibration < -127) { mp_arg_validate_int_range(calibration, -127, 127, MP_QSTR_calibration);
#if CIRCUITPY_FULL_BUILD
mp_raise_ValueError(translate("calibration value out of range +/-127"));
#else
mp_raise_ValueError(translate("calibration is out of range"));
#endif
}
hri_rtcmode0_write_FREQCORR_SIGN_bit(RTC, calibration < 0 ? 0 : 1); hri_rtcmode0_write_FREQCORR_SIGN_bit(RTC, calibration < 0 ? 0 : 1);
hri_rtcmode0_write_FREQCORR_VALUE_bf(RTC, abs(calibration)); hri_rtcmode0_write_FREQCORR_VALUE_bf(RTC, abs(calibration));

View File

@ -31,6 +31,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "py/binary.h" #include "py/binary.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/touchio/TouchIn.h" #include "shared-bindings/touchio/TouchIn.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
@ -60,7 +61,7 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
const mcu_pin_obj_t *pin) { const mcu_pin_obj_t *pin) {
if (!pin->has_touch) { if (!pin->has_touch) {
mp_raise_ValueError(translate("Invalid pin")); raise_ValueError_invalid_pin();
} }
claim_pin(pin); claim_pin(pin);

View File

@ -90,7 +90,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
} }
} }
if (instance_index == NUM_I2C) { if (instance_index == NUM_I2C) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
i2c_in_use[instance_index] = true; i2c_in_use[instance_index] = true;
self->index = instance_index; self->index = instance_index;

View File

@ -104,7 +104,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
break; break;
} }
if (instance_index == NUM_SPI) { if (instance_index == NUM_SPI) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
self->clock = clock; self->clock = clock;

View File

@ -162,13 +162,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer, mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer,
bool sigint_enabled) { bool sigint_enabled) {
if (bits > 8) { mp_arg_validate_int_max(bits, 8, MP_QSTR_bits);
mp_raise_ValueError(translate("Invalid word/bit length")); mp_arg_validate_int_min(receiver_buffer_size, 1, MP_QSTR_receiver_buffer_size);
}
if (receiver_buffer_size == 0) {
mp_raise_ValueError(translate("Invalid buffer size"));
}
if ((rs485_dir != NULL) || (rs485_invert)) { if ((rs485_dir != NULL) || (rs485_invert)) {
mp_raise_NotImplementedError(translate("RS485 Not yet supported on this device")); mp_raise_NotImplementedError(translate("RS485 Not yet supported on this device"));
@ -203,7 +198,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
break; break;
} }
if (instance_index == NUM_UART) { if (instance_index == NUM_UART) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
self->rx_pin = rx; self->rx_pin = rx;
@ -224,7 +219,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
// in the long-lived pool is not strictly necessary) // in the long-lived pool is not strictly necessary)
// (This is a macro.) // (This is a macro.)
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) { if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer")); m_malloc_fail(receiver_buffer_size);
} }
} }
} }

View File

@ -34,6 +34,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/analogio/AnalogIn.h" #include "shared-bindings/analogio/AnalogIn.h"
#include "shared-bindings/microcontroller/Pin.h"
typedef struct { typedef struct {
const char *devpath; const char *devpath;
@ -52,7 +53,7 @@ STATIC analogin_dev_t analogin_dev[] = {
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
if (!pin->analog) { if (!pin->analog) {
mp_raise_ValueError(translate("AnalogIn not supported on given pin")); raise_ValueError_invalid_pin();
} }
self->number = -1; self->number = -1;
@ -65,13 +66,13 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const
} }
if (self->number < 0) { if (self->number < 0) {
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); raise_ValueError_invalid_pin();
} }
if (analogin_dev[self->number].fd < 0) { if (analogin_dev[self->number].fd < 0) {
analogin_dev[self->number].fd = open(analogin_dev[self->number].devpath, O_RDONLY); analogin_dev[self->number].fd = open(analogin_dev[self->number].devpath, O_RDONLY);
if (analogin_dev[self->number].fd < 0) { if (analogin_dev[self->number].fd < 0) {
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); raise_ValueError_invalid_pin();
} }
} }

View File

@ -29,7 +29,7 @@
#include "shared-bindings/analogio/AnalogOut.h" #include "shared-bindings/analogio/AnalogOut.h"
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) { void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) {
mp_raise_RuntimeError(translate("AnalogOut functionality not supported")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_AnalogOut);
} }
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {

View File

@ -32,6 +32,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/busio/I2C.h" #include "shared-bindings/busio/I2C.h"
#include "shared-bindings/microcontroller/Pin.h"
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl, void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl,
const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) { const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
@ -40,7 +41,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *
} }
if (scl->number != PIN_I2C0_BCK || sda->number != PIN_I2C0_BDT) { if (scl->number != PIN_I2C0_BCK || sda->number != PIN_I2C0_BDT) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
claim_pin(scl); claim_pin(scl);

View File

@ -33,6 +33,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/busio/SPI.h" #include "shared-bindings/busio/SPI.h"
#include "shared-bindings/microcontroller/Pin.h"
void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *clock, void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *clock,
const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso, bool half_duplex) { const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso, bool half_duplex) {
@ -55,7 +56,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *
} }
if (port < 0) { if (port < 0) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
claim_pin(clock); claim_pin(clock);

View File

@ -40,6 +40,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/busio/UART.h" #include "shared-bindings/busio/UART.h"
#include "shared-bindings/microcontroller/Pin.h"
typedef struct { typedef struct {
const char *devpath; const char *devpath;
@ -68,17 +69,9 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_raise_ValueError(translate("RTS/CTS/RS485 Not yet supported on this device")); mp_raise_ValueError(translate("RTS/CTS/RS485 Not yet supported on this device"));
} }
if (bits != 8) { mp_arg_validate_int(bits, 8, MP_QSTR_bits);
mp_raise_ValueError(translate("Could not initialize UART")); mp_arg_validate_int(parity, BUSIO_UART_PARITY_NONE, MP_QSTR_parity);
} mp_arg_validate_int(stop, 1, MP_QSTR_stop);
if (parity != BUSIO_UART_PARITY_NONE) {
mp_raise_ValueError(translate("Could not initialize UART"));
}
if (stop != 1) {
mp_raise_ValueError(translate("Could not initialize UART"));
}
self->number = -1; self->number = -1;
@ -91,7 +84,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
} }
if (self->number < 0) { if (self->number < 0) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
if (busio_uart_dev[self->number].fd < 0) { if (busio_uart_dev[self->number].fd < 0) {

View File

@ -121,11 +121,11 @@ static void camera_start_preview() {
void common_hal_camera_construct(camera_obj_t *self) { void common_hal_camera_construct(camera_obj_t *self) {
if (camera_dev.fd < 0) { if (camera_dev.fd < 0) {
if (video_initialize(camera_dev.devpath) < 0) { if (video_initialize(camera_dev.devpath) < 0) {
mp_raise_ValueError(translate("Could not initialize Camera")); mp_raise_ValueError_varg(translate("Could not initialize %q"), MP_QSTR_Camera);
} }
camera_dev.fd = open(camera_dev.devpath, 0); camera_dev.fd = open(camera_dev.devpath, 0);
if (camera_dev.fd < 0) { if (camera_dev.fd < 0) {
mp_raise_ValueError(translate("Could not initialize Camera")); mp_raise_ValueError_varg(translate("Could not initialize %q"), MP_QSTR_Camera);
} }
} }

View File

@ -29,10 +29,11 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/microcontroller/Pin.h"
digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) {
if (pin->analog) { if (pin->analog) {
mp_raise_ValueError(translate("DigitalInOut not supported on given pin")); raise_ValueError_invalid_pin();
} }
claim_pin(pin); claim_pin(pin);

View File

@ -56,7 +56,7 @@ void common_hal_gnss_construct(gnss_obj_t *self, unsigned long selection) {
if (gnss_dev.fd < 0) { if (gnss_dev.fd < 0) {
gnss_dev.fd = open(gnss_dev.devpath, O_RDONLY); gnss_dev.fd = open(gnss_dev.devpath, O_RDONLY);
if (gnss_dev.fd < 0) { if (gnss_dev.fd < 0) {
mp_raise_ValueError(translate("Could not initialize GNSS")); mp_raise_ValueError_varg(translate("Could not initialize %q"), MP_QSTR_GNSS);
} }
} }

View File

@ -72,7 +72,7 @@ void common_hal_mcu_enable_interrupts(void) {
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
if (runmode == RUNMODE_BOOTLOADER) { if (runmode == RUNMODE_BOOTLOADER) {
mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present.")); mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present"));
} else if (runmode == RUNMODE_SAFE_MODE) { } else if (runmode == RUNMODE_SAFE_MODE) {
safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE); safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE);
} }

View File

@ -87,7 +87,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) { const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) {
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false); self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
if (self->buffer == NULL) { if (self->buffer == NULL) {
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); m_malloc_fail(maxlen * sizeof(uint16_t));
} }
self->pin = pin; self->pin = pin;
@ -190,7 +190,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, int16_
} }
if (index < 0 || index >= self->len) { if (index < 0 || index >= self->len) {
common_hal_mcu_enable_interrupts(); common_hal_mcu_enable_interrupts();
mp_raise_IndexError_varg(translate("%q index out of range"), MP_QSTR_PulseIn); mp_raise_IndexError_varg(translate("%q out of range"), MP_QSTR_index);
} }
uint16_t value = self->buffer[(self->start + index) % self->maxlen]; uint16_t value = self->buffer[(self->start + index) % self->maxlen];
common_hal_mcu_enable_interrupts(); common_hal_mcu_enable_interrupts();

View File

@ -74,7 +74,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self,
self->variable_frequency = variable_frequency; self->variable_frequency = variable_frequency;
if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) { if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) {
mp_raise_ValueError(translate("Invalid PWM frequency")); mp_arg_error_invalid(MP_QSTR_frequency);
} }
ioctl(pwmout_dev[self->number].fd, PWMIOC_START, 0); ioctl(pwmout_dev[self->number].fd, PWMIOC_START, 0);
@ -116,7 +116,7 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self, uint32_t fr
self->info.frequency = frequency; self->info.frequency = frequency;
if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) { if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) {
mp_raise_ValueError(translate("Invalid PWM frequency")); mp_arg_error_invalid(MP_QSTR_frequency);
} }
} }

View File

@ -50,5 +50,5 @@ int common_hal_rtc_get_calibration(void) {
} }
void common_hal_rtc_set_calibration(int calibration) { void common_hal_rtc_set_calibration(int calibration) {
mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration);
} }

View File

@ -31,6 +31,7 @@
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/sdioio/SDCard.h" #include "shared-bindings/sdioio/SDCard.h"
#include "shared-bindings/util.h" #include "shared-bindings/util.h"
@ -42,7 +43,7 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
struct geometry geo; struct geometry geo;
if (clock->number != PIN_SDIO_CLK || command->number != PIN_SDIO_CMD) { if (clock->number != PIN_SDIO_CLK || command->number != PIN_SDIO_CMD) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
uint8_t data_pins_num = 0; uint8_t data_pins_num = 0;
@ -54,11 +55,11 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
} }
if (data_pins_num != DATA_PINS_NUM) { if (data_pins_num != DATA_PINS_NUM) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
if (open_blockdriver("/dev/mmcsd0", 0, &self->inode) < 0) { if (open_blockdriver("/dev/mmcsd0", 0, &self->inode) < 0) {
mp_raise_ValueError(translate("Could not initialize SDCard")); mp_raise_ValueError_varg(translate("Could not initialize %q"), MP_QSTR_SDCard);
} }
self->inode->u.i_bops->geometry(self->inode, &geo); self->inode->u.i_bops->geometry(self->inode, &geo);

View File

@ -26,6 +26,7 @@
#include "shared-bindings/alarm/touch/TouchAlarm.h" #include "shared-bindings/alarm/touch/TouchAlarm.h"
#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "esp_sleep.h" #include "esp_sleep.h"
#include "peripherals/touch.h" #include "peripherals/touch.h"
@ -36,7 +37,7 @@ static volatile bool woke_up = false;
void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) { void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) {
if (pin->touch_channel == TOUCH_PAD_MAX) { if (pin->touch_channel == TOUCH_PAD_MAX) {
mp_raise_ValueError(translate("Invalid pin")); raise_ValueError_invalid_pin();
} }
claim_pin(pin); claim_pin(pin);
self->pin = pin; self->pin = pin;

View File

@ -51,7 +51,7 @@
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self,
const mcu_pin_obj_t *pin) { const mcu_pin_obj_t *pin) {
if (pin->adc_index == 0 || pin->adc_channel == ADC_CHANNEL_MAX) { if (pin->adc_index == 0 || pin->adc_channel == ADC_CHANNEL_MAX) {
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); raise_ValueError_invalid_pin();
} }
common_hal_mcu_pin_claim(pin); common_hal_mcu_pin_claim(pin);
self->pin = pin; self->pin = pin;
@ -80,7 +80,7 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
} else if (self->pin->adc_index == ADC_UNIT_2) { } else if (self->pin->adc_index == ADC_UNIT_2) {
adc2_config_channel_atten((adc2_channel_t)self->pin->adc_channel, ATTENUATION); adc2_config_channel_atten((adc2_channel_t)self->pin->adc_channel, ATTENUATION);
} else { } else {
mp_raise_ValueError(translate("Invalid Pin")); raise_ValueError_invalid_pin();
} }
// Automatically select calibration process depending on status of efuse // Automatically select calibration process depending on status of efuse

View File

@ -49,7 +49,7 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self,
} else if (pin == &pin_GPIO18) { } else if (pin == &pin_GPIO18) {
self->channel = DAC_CHANNEL_2; self->channel = DAC_CHANNEL_2;
} else { } else {
mp_raise_ValueError(translate("Invalid DAC pin supplied")); raise_ValueError_invalid_pin();
} }
dac_output_enable(self->channel); dac_output_enable(self->channel);
#else #else

View File

@ -42,7 +42,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
// //
// 46 is also input-only so it'll never work. // 46 is also input-only so it'll never work.
if (scl->number == 45 || scl->number == 46 || sda->number == 45 || sda->number == 46) { if (scl->number == 45 || scl->number == 46 || sda->number == 45 || sda->number == 46) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
#if CIRCUITPY_REQUIRE_I2C_PULLUPS #if CIRCUITPY_REQUIRE_I2C_PULLUPS

View File

@ -28,6 +28,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/busio/SPI.h" #include "shared-bindings/busio/SPI.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "driver/spi_common_internal.h" #include "driver/spi_common_internal.h"
@ -101,7 +102,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
if (result == ESP_ERR_NO_MEM) { if (result == ESP_ERR_NO_MEM) {
mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed")); mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed"));
} else if (result == ESP_ERR_INVALID_ARG) { } else if (result == ESP_ERR_INVALID_ARG) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
set_spi_config(self, 250000, 0, 0, 8); set_spi_config(self, 250000, 0, 0, 8);

View File

@ -101,9 +101,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer, mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer,
bool sigint_enabled) { bool sigint_enabled) {
if (bits > 8) { mp_arg_validate_int_max(bits, 8, MP_QSTR_bytes);
mp_raise_NotImplementedError(translate("bytes > 8 bits not supported"));
}
bool have_tx = tx != NULL; bool have_tx = tx != NULL;
bool have_rx = rx != NULL; bool have_rx = rx != NULL;

View File

@ -133,7 +133,7 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t *
if (result == ESP_ERR_NO_MEM) { if (result == ESP_ERR_NO_MEM) {
mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed")); mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed"));
} else if (result == ESP_ERR_INVALID_ARG) { } else if (result == ESP_ERR_INVALID_ARG) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} else if (result != ESP_OK) { } else if (result != ESP_OK) {
mp_raise_OSError_msg_varg(translate("twai_driver_install returned esp-idf error #%d"), (int)result); mp_raise_OSError_msg_varg(translate("twai_driver_install returned esp-idf error #%d"), (int)result);
} }

View File

@ -130,9 +130,7 @@ static void init_timer(frequencyio_frequencyin_obj_t *self) {
void common_hal_frequencyio_frequencyin_construct(frequencyio_frequencyin_obj_t *self, void common_hal_frequencyio_frequencyin_construct(frequencyio_frequencyin_obj_t *self,
const mcu_pin_obj_t *pin, const uint16_t capture_period) { const mcu_pin_obj_t *pin, const uint16_t capture_period) {
if ((capture_period == 0) || (capture_period > 500)) { mp_arg_validate_int_range(capture_period, 1, 500, MP_QSTR_capture_period);
mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
}
self->pin = pin->number; self->pin = pin->number;
self->handle = NULL; self->handle = NULL;
@ -188,9 +186,8 @@ uint16_t common_hal_frequencyio_frequencyin_get_capture_period(frequencyio_frequ
} }
void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequencyin_obj_t *self, uint16_t capture_period) { void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequencyin_obj_t *self, uint16_t capture_period) {
if ((capture_period == 0) || (capture_period > 500)) { mp_arg_validate_int_range(capture_period, 1, 500, MP_QSTR_capture_period);
mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
}
self->capture_period = capture_period; self->capture_period = capture_period;
common_hal_frequencyio_frequencyin_clear(self); common_hal_frequencyio_frequencyin_clear(self);
timer_set_alarm_value(self->timer.group, self->timer.idx, capture_period * 1000000); timer_set_alarm_value(self->timer.group, self->timer.idx, capture_period * 1000000);

View File

@ -30,6 +30,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "common-hal/i2cperipheral/I2CPeripheral.h" #include "common-hal/i2cperipheral/I2CPeripheral.h"
#include "shared-bindings/microcontroller/Pin.h"
void common_hal_i2cperipheral_i2c_peripheral_construct(i2cperipheral_i2c_peripheral_obj_t *self, void common_hal_i2cperipheral_i2c_peripheral_construct(i2cperipheral_i2c_peripheral_obj_t *self,
const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda,
@ -39,7 +40,7 @@ void common_hal_i2cperipheral_i2c_peripheral_construct(i2cperipheral_i2c_periphe
// support I2C on these pins. // support I2C on these pins.
// Also 46 is input-only so it'll never work. // Also 46 is input-only so it'll never work.
if (scl->number == 45 || scl->number == 46 || sda->number == 45 || sda->number == 46) { if (scl->number == 45 || scl->number == 46 || sda->number == 45 || sda->number == 46) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
if (num_addresses > 1) { if (num_addresses > 1) {
@ -72,7 +73,7 @@ void common_hal_i2cperipheral_i2c_peripheral_construct(i2cperipheral_i2c_periphe
if (err == ESP_FAIL) { if (err == ESP_FAIL) {
mp_raise_OSError(MP_EIO); mp_raise_OSError(MP_EIO);
} else { } else {
mp_raise_ValueError(translate("Invalid argument")); mp_arg_error_invalid(MP_QSTR_I2CPeripheral);
} }
} }

View File

@ -41,9 +41,7 @@ void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_paralle
const mcu_pin_obj_t *horizontal_reference) { const mcu_pin_obj_t *horizontal_reference) {
// only 8 bits is supported at present // only 8 bits is supported at present
if (data_count < 8 || data_count > 16) { mp_arg_validate_int_range(data_count, 8, 16, MP_QSTR_data_count);
mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_data_count, 8, 16);
}
// This will throw if unsuccessful. Everything following is guaranteed to succeed. // This will throw if unsuccessful. Everything following is guaranteed to succeed.
port_i2s_allocate_i2s0(); port_i2s_allocate_i2s0();

View File

@ -88,7 +88,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu
uint16_t maxlen, bool idle_state) { uint16_t maxlen, bool idle_state) {
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false); self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
if (self->buffer == NULL) { if (self->buffer == NULL) {
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); m_malloc_fail(maxlen * sizeof(uint16_t));
} }
self->pin = pin; self->pin = pin;
self->maxlen = maxlen; self->maxlen = maxlen;

View File

@ -217,7 +217,7 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self, uint32_t fr
// Calculate duty cycle // Calculate duty cycle
uint32_t duty_bits = calculate_duty_cycle(frequency); uint32_t duty_bits = calculate_duty_cycle(frequency);
if (duty_bits == 0) { if (duty_bits == 0) {
mp_raise_ValueError(translate("Invalid PWM frequency")); mp_arg_error_invalid(MP_QSTR_frequency);
} }
self->duty_resolution = duty_bits; self->duty_resolution = duty_bits;
ledc_set_freq(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num, frequency); ledc_set_freq(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num, frequency);

View File

@ -48,5 +48,5 @@ int common_hal_rtc_get_calibration(void) {
} }
void common_hal_rtc_set_calibration(int calibration) { void common_hal_rtc_set_calibration(int calibration) {
mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration);
} }

View File

@ -28,6 +28,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "peripherals/touch.h" #include "peripherals/touch.h"
#include "shared-bindings/microcontroller/Pin.h"
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
uint32_t touch_value; uint32_t touch_value;
@ -41,7 +42,7 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self, void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
const mcu_pin_obj_t *pin) { const mcu_pin_obj_t *pin) {
if (pin->touch_channel == TOUCH_PAD_MAX) { if (pin->touch_channel == TOUCH_PAD_MAX) {
mp_raise_ValueError(translate("Invalid pin")); raise_ValueError_invalid_pin();
} }
claim_pin(pin); claim_pin(pin);

View File

@ -209,7 +209,7 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_
authmode = WIFI_AUTH_WPA_WPA2_PSK; authmode = WIFI_AUTH_WPA_WPA2_PSK;
break; break;
default: default:
mp_raise_ValueError(translate("Invalid AuthMode")); mp_arg_error_invalid(MP_QSTR_authmode);
break; break;
} }
@ -221,9 +221,8 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_
config->ap.channel = channel; config->ap.channel = channel;
config->ap.authmode = authmode; config->ap.authmode = authmode;
if (max_connections < 0 || max_connections > 10) { mp_arg_validate_int_range(max_connections, 0, 10, MP_QSTR_max_connections);
mp_raise_ValueError(translate("max_connections must be between 0 and 10"));
}
config->ap.max_connection = max_connections; config->ap.max_connection = max_connections;
esp_wifi_set_config(WIFI_IF_AP, config); esp_wifi_set_config(WIFI_IF_AP, config);

View File

@ -42,7 +42,7 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self,
adc_config_t config = {0}; adc_config_t config = {0};
if (pin->adc == NULL) { if (pin->adc == NULL) {
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); raise_ValueError_invalid_pin();
} }
ADC_GetDefaultConfig(&config); ADC_GetDefaultConfig(&config);

View File

@ -32,7 +32,7 @@
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) { void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) {
mp_raise_NotImplementedError(translate("AnalogOut functionality not supported")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_AnalogOut);
} }
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {

View File

@ -138,7 +138,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
} }
if (self->sda == NULL || self->scl == NULL) { if (self->sda == NULL || self->scl == NULL) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} else { } else {
self->i2c = mcu_i2c_banks[self->sda->bank_idx - 1]; self->i2c = mcu_i2c_banks[self->sda->bank_idx - 1];
} }

View File

@ -168,7 +168,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
if (spi_taken) { if (spi_taken) {
mp_raise_ValueError(translate("Hardware busy, try alternative pins")); mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
} else { } else {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
} }

View File

@ -117,7 +117,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
self->timeout_ms = timeout * 1000; self->timeout_ms = timeout * 1000;
if (self->character_bits != 7 && self->character_bits != 8) { if (self->character_bits != 7 && self->character_bits != 8) {
mp_raise_ValueError(translate("Invalid word/bit length")); mp_arg_validate_int_range(self->character_bits, 7, 8, MP_QSTR_bits);
} }
DBGPrintf(&mp_plat_print, "uart_construct: tx:%p rx:%p rts:%p cts:%p rs485:%p\n", tx, rx, rts, cts, rs485_dir); DBGPrintf(&mp_plat_print, "uart_construct: tx:%p rx:%p rts:%p cts:%p rs485:%p\n", tx, rx, rts, cts, rs485_dir);
@ -350,7 +350,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
if (!self->ringbuf) { if (!self->ringbuf) {
LPUART_Deinit(self->uart); LPUART_Deinit(self->uart);
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer")); m_malloc_fail(receiver_buffer_size);
} }
LPUART_TransferCreateHandle(self->uart, &self->handle, LPUART_UserCallback, self); LPUART_TransferCreateHandle(self->uart, &self->handle, LPUART_UserCallback, self);

View File

@ -70,7 +70,7 @@ void common_hal_mcu_enable_interrupts(void) {
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
if (runmode == RUNMODE_BOOTLOADER) { if (runmode == RUNMODE_BOOTLOADER) {
if (!bootloader_available()) { if (!bootloader_available()) {
mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present.")); mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present"));
} }
// Pretend to be the first of the two reset presses needed to enter the // Pretend to be the first of the two reset presses needed to enter the
// bootloader. That way one reset will end in the bootloader. // bootloader. That way one reset will end in the bootloader.

View File

@ -301,7 +301,7 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self,
int pulse_count = calculate_pulse_count(frequency, &self->prescaler); int pulse_count = calculate_pulse_count(frequency, &self->prescaler);
if (pulse_count == 0) { if (pulse_count == 0) {
mp_raise_ValueError(translate("Invalid PWM frequency")); mp_arg_error_invalid(MP_QSTR_frequency);
} }
self->pulse_count = pulse_count; self->pulse_count = pulse_count;

View File

@ -75,5 +75,5 @@ int common_hal_rtc_get_calibration(void) {
void common_hal_rtc_set_calibration(int calibration) { void common_hal_rtc_set_calibration(int calibration) {
// SNVS has HPCALB_VAL bits for calibration. // SNVS has HPCALB_VAL bits for calibration.
mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration);
} }

View File

@ -24,9 +24,8 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "shared-bindings/usb_host/Port.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/usb_host/Port.h"
#include "py/runtime.h" #include "py/runtime.h"
@ -43,7 +42,7 @@ void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin
supported_dm = &pin_USB_OTG2_DN; supported_dm = &pin_USB_OTG2_DN;
} }
if (dp != supported_dp || dm != supported_dm) { if (dp != supported_dp || dm != supported_dm) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
self->init = true; self->init = true;
usb_host_init = true; usb_host_init = true;

View File

@ -49,10 +49,10 @@ extern uint32_t reset_reason_saved;
void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull) { void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull) {
if (edge) { if (edge) {
mp_raise_ValueError(translate("Cannot wake on pin edge. Only level.")); mp_raise_ValueError(translate("Cannot wake on pin edge, only level"));
} }
if (pin->number >= NUMBER_OF_PINS) { if (pin->number >= NUMBER_OF_PINS) {
mp_raise_ValueError(translate("Invalid pin")); raise_ValueError_invalid_pin();
} }
self->pin = pin; self->pin = pin;
self->value = value; self->value = value;

View File

@ -85,7 +85,7 @@ void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_
continue; continue;
} }
if (timealarm_set) { if (timealarm_set) {
mp_raise_ValueError(translate("Only one alarm.time alarm can be set.")); mp_raise_ValueError(translate("Only one alarm.time alarm can be set"));
} }
timealarm = MP_OBJ_TO_PTR(alarms[i]); timealarm = MP_OBJ_TO_PTR(alarms[i]);
timealarm_set = true; timealarm_set = true;

View File

@ -27,6 +27,7 @@
#include "common-hal/analogio/AnalogIn.h" #include "common-hal/analogio/AnalogIn.h"
#include "shared-bindings/analogio/AnalogIn.h" #include "shared-bindings/analogio/AnalogIn.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
@ -48,7 +49,7 @@ void analogin_init(void) {
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
if (pin->adc_channel == 0) { if (pin->adc_channel == 0) {
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); raise_ValueError_invalid_pin();
} }
nrf_gpio_cfg_default(pin->number); nrf_gpio_cfg_default(pin->number);

View File

@ -34,7 +34,7 @@
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) { void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) {
mp_raise_RuntimeError(translate("AnalogOut functionality not supported")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_AnalogOut);
} }
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {

View File

@ -263,9 +263,8 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
&spacing); &spacing);
self->sample_channel_count = audiosample_channel_count(sample); self->sample_channel_count = audiosample_channel_count(sample);
if (max_buffer_length > UINT16_MAX) { mp_arg_validate_length_max(max_buffer_length, UINT16_MAX, MP_QSTR_buffer);
mp_raise_ValueError_varg(translate("Buffer length %d too big. It must be less than %d"), max_buffer_length, UINT16_MAX);
}
uint16_t buffer_length = (uint16_t)max_buffer_length; uint16_t buffer_length = (uint16_t)max_buffer_length;
self->buffers[0] = m_malloc(buffer_length * 2 * sizeof(uint16_t), false); self->buffers[0] = m_malloc(buffer_length * 2 * sizeof(uint16_t), false);
if (!self->single_buffer) { if (!self->single_buffer) {

View File

@ -29,6 +29,7 @@
#include "shared-bindings/busio/I2C.h" #include "shared-bindings/busio/I2C.h"
#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
@ -96,7 +97,7 @@ static uint8_t twi_error_to_mp(const nrfx_err_t err) {
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) { void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
if (scl->number == sda->number) { if (scl->number == sda->number) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
// Find a free instance. // Find a free instance.

View File

@ -164,12 +164,10 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer, mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer,
bool sigint_enabled) { bool sigint_enabled) {
if (bits != 8) { mp_arg_validate_int(bits, 8, MP_QSTR_bits);
mp_raise_ValueError(translate("Invalid word/bit length"));
}
if ((rs485_dir != NULL) || (rs485_invert)) { if ((rs485_dir != NULL) || (rs485_invert)) {
mp_raise_ValueError(translate("RS485 Not yet supported on this device")); mp_raise_NotImplementedError(translate("RS485"));
} }
// Find a free UART peripheral. // Find a free UART peripheral.
@ -189,9 +187,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_raise_ValueError(translate("tx and rx cannot both be None")); mp_raise_ValueError(translate("tx and rx cannot both be None"));
} }
if (receiver_buffer_size == 0) { mp_arg_validate_int_min(receiver_buffer_size, 1, MP_QSTR_receiver_buffer_size);
mp_raise_ValueError(translate("Invalid buffer size"));
}
if (parity == BUSIO_UART_PARITY_ODD) { if (parity == BUSIO_UART_PARITY_ODD) {
mp_raise_ValueError(translate("Odd parity is not supported")); mp_raise_ValueError(translate("Odd parity is not supported"));
@ -234,7 +230,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
// (This is a macro.) // (This is a macro.)
} else if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) { } else if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
nrfx_uarte_uninit(self->uarte); nrfx_uarte_uninit(self->uarte);
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer")); m_malloc_fail(receiver_buffer_size);
} }
self->rx_pin_number = rx->number; self->rx_pin_number = rx->number;

View File

@ -129,7 +129,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false); self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
if (self->buffer == NULL) { if (self->buffer == NULL) {
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); m_malloc_fail(maxlen * sizeof(uint16_t));
} }
if (refcount == 0) { if (refcount == 0) {
@ -271,7 +271,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, int16_
if (!self->paused) { if (!self->paused) {
nrfx_gpiote_in_event_enable(self->pin, true); nrfx_gpiote_in_event_enable(self->pin, true);
} }
mp_raise_IndexError_varg(translate("%q index out of range"), MP_QSTR_PulseIn); mp_raise_IndexError_varg(translate("%q out of range"), MP_QSTR_index);
} }
uint16_t value = self->buffer[(self->start + index) % self->maxlen]; uint16_t value = self->buffer[(self->start + index) % self->maxlen];

View File

@ -297,7 +297,7 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self, uint32_t fr
uint16_t countertop; uint16_t countertop;
nrf_pwm_clk_t base_clock; nrf_pwm_clk_t base_clock;
if (frequency == 0 || !convert_frequency(frequency, &countertop, &base_clock)) { if (frequency == 0 || !convert_frequency(frequency, &countertop, &base_clock)) {
mp_raise_ValueError(translate("Invalid PWM frequency")); mp_arg_error_invalid(MP_QSTR_frequency);
} }
self->frequency = frequency; self->frequency = frequency;

View File

@ -80,5 +80,5 @@ int common_hal_rtc_get_calibration(void) {
} }
void common_hal_rtc_set_calibration(int calibration) { void common_hal_rtc_set_calibration(int calibration) {
mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration);
} }

View File

@ -223,50 +223,31 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
// We don't validate pin in use here because we may be ok sharing them within a PIO. // We don't validate pin in use here because we may be ok sharing them within a PIO.
const mcu_pin_obj_t *first_out_pin = validate_obj_is_pin_or_none(args[ARG_first_out_pin].u_obj); const mcu_pin_obj_t *first_out_pin = validate_obj_is_pin_or_none(args[ARG_first_out_pin].u_obj);
if (args[ARG_out_pin_count].u_int < 1) { mp_int_t out_pin_count = mp_arg_validate_int_min(args[ARG_out_pin_count].u_int, 1, MP_QSTR_out_pin_count);
mp_raise_ValueError(translate("Pin count must be at least 1"));
}
const mcu_pin_obj_t *first_in_pin = validate_obj_is_pin_or_none(args[ARG_first_in_pin].u_obj); const mcu_pin_obj_t *first_in_pin = validate_obj_is_pin_or_none(args[ARG_first_in_pin].u_obj);
if (args[ARG_in_pin_count].u_int < 1) { mp_int_t in_pin_count = mp_arg_validate_int_min(args[ARG_in_pin_count].u_int, 1, MP_QSTR_in_pin_count);
mp_raise_ValueError(translate("Pin count must be at least 1"));
}
const mcu_pin_obj_t *first_set_pin = validate_obj_is_pin_or_none(args[ARG_first_set_pin].u_obj); const mcu_pin_obj_t *first_set_pin = validate_obj_is_pin_or_none(args[ARG_first_set_pin].u_obj);
if (args[ARG_set_pin_count].u_int < 1) { mp_int_t set_pin_count = mp_arg_validate_int_range(args[ARG_set_pin_count].u_int, 1, 5, MP_QSTR_set_pin_count);
mp_raise_ValueError(translate("Pin count must be at least 1"));
}
if (args[ARG_set_pin_count].u_int > 5) {
mp_raise_ValueError(translate("Set pin count must be between 1 and 5"));
}
const mcu_pin_obj_t *first_sideset_pin = validate_obj_is_pin_or_none(args[ARG_first_sideset_pin].u_obj); const mcu_pin_obj_t *first_sideset_pin = validate_obj_is_pin_or_none(args[ARG_first_sideset_pin].u_obj);
if (args[ARG_sideset_pin_count].u_int < 1) { mp_int_t sideset_pin_count = mp_arg_validate_int_range(args[ARG_sideset_pin_count].u_int, 1, 5, MP_QSTR_set_pin_count);
mp_raise_ValueError(translate("Pin count must be at least 1"));
}
if (args[ARG_sideset_pin_count].u_int > 5) {
mp_raise_ValueError(translate("Side set pin count must be between 1 and 5"));
}
const mcu_pin_obj_t *jmp_pin = validate_obj_is_pin_or_none(args[ARG_jmp_pin].u_obj); const mcu_pin_obj_t *jmp_pin = validate_obj_is_pin_or_none(args[ARG_jmp_pin].u_obj);
digitalio_pull_t jmp_pin_pull = validate_pull(args[ARG_jmp_pin_pull].u_rom_obj, MP_QSTR_jmp_pull); digitalio_pull_t jmp_pin_pull = validate_pull(args[ARG_jmp_pin_pull].u_rom_obj, MP_QSTR_jmp_pull);
mp_int_t pull_threshold = args[ARG_pull_threshold].u_int; mp_int_t pull_threshold =
mp_int_t push_threshold = args[ARG_push_threshold].u_int; mp_arg_validate_int_range(args[ARG_pull_threshold].u_int, 1, 32, MP_QSTR_pull_threshold);
if (pull_threshold < 1 || pull_threshold > 32) { mp_int_t push_threshold =
mp_raise_ValueError(translate("pull_threshold must be between 1 and 32")); mp_arg_validate_int_range(args[ARG_push_threshold].u_int, 1, 32, MP_QSTR_push_threshold);
}
if (push_threshold < 1 || push_threshold > 32) {
mp_raise_ValueError(translate("push_threshold must be between 1 and 32"));
}
if (bufinfo.len < 2) { mp_arg_validate_length_range(bufinfo.len, 2, 64, MP_QSTR_program);
mp_raise_ValueError(translate("Program must contain at least one 16-bit instruction."));
}
if (bufinfo.len % 2 != 0) { if (bufinfo.len % 2 != 0) {
mp_raise_ValueError(translate("Program size invalid")); mp_raise_ValueError(translate("Program size invalid"));
} }
if (bufinfo.len > 64) {
mp_raise_ValueError(translate("Program too large"));
}
mp_arg_validate_length_range(init_bufinfo.len, 2, 64, MP_QSTR_init);
if (init_bufinfo.len % 2 != 0) { if (init_bufinfo.len % 2 != 0) {
mp_raise_ValueError(translate("Init program size invalid")); mp_raise_ValueError(translate("Init program size invalid"));
} }

View File

@ -26,6 +26,7 @@
#include "common-hal/analogio/AnalogIn.h" #include "common-hal/analogio/AnalogIn.h"
#include "shared-bindings/analogio/AnalogIn.h" #include "shared-bindings/analogio/AnalogIn.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
@ -36,7 +37,7 @@
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
if (pin->number < ADC_FIRST_PIN_NUMBER || pin->number > ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT) { if (pin->number < ADC_FIRST_PIN_NUMBER || pin->number > ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT) {
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); raise_ValueError_invalid_pin();
} }
adc_init(); adc_init();

View File

@ -34,7 +34,7 @@
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) { void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) {
mp_raise_RuntimeError(translate("AnalogOut functionality not supported")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_AnalogOut);
} }
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {

View File

@ -30,6 +30,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/bitbangio/I2C.h" #include "shared-bindings/bitbangio/I2C.h"
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
@ -65,14 +66,14 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
self->peripheral = i2c[sda_instance]; self->peripheral = i2c[sda_instance];
} }
if (self->peripheral == NULL) { if (self->peripheral == NULL) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
if ((i2c_get_hw(self->peripheral)->enable & I2C_IC_ENABLE_ENABLE_BITS) != 0) { if ((i2c_get_hw(self->peripheral)->enable & I2C_IC_ENABLE_ENABLE_BITS) != 0) {
mp_raise_ValueError(translate("I2C peripheral in use")); mp_raise_ValueError(translate("I2C peripheral in use"));
} }
if (frequency > 1000000) {
mp_raise_ValueError(translate("Unsupported baudrate")); mp_arg_validate_int_max(frequency, 1000000, MP_QSTR_frequency);
}
#if CIRCUITPY_REQUIRE_I2C_PULLUPS #if CIRCUITPY_REQUIRE_I2C_PULLUPS
// Test that the pins are in a high state. (Hopefully indicating they are pulled up.) // Test that the pins are in a high state. (Hopefully indicating they are pulled up.)

View File

@ -82,7 +82,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
// TODO: Check to see if we're sharing the SPI with a native APA102. // TODO: Check to see if we're sharing the SPI with a native APA102.
if (instance_index > 1) { if (instance_index > 1) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
if (instance_index == 0) { if (instance_index == 0) {

View File

@ -32,6 +32,7 @@
#include "supervisor/shared/tick.h" #include "supervisor/shared/tick.h"
#include "shared/runtime/interrupt_char.h" #include "shared/runtime/interrupt_char.h"
#include "common-hal/microcontroller/Pin.h" #include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "src/rp2_common/hardware_irq/include/hardware/irq.h" #include "src/rp2_common/hardware_irq/include/hardware/irq.h"
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
@ -66,7 +67,7 @@ static uint8_t pin_init(const uint8_t uart, const mcu_pin_obj_t *pin, const uint
return NO_PIN; return NO_PIN;
} }
if (!(((pin->number % 4) == pin_type) && ((((pin->number + 4) / 8) % NUM_UARTS) == uart))) { if (!(((pin->number % 4) == pin_type) && ((((pin->number + 4) / 8) % NUM_UARTS) == uart))) {
mp_raise_ValueError(translate("Invalid pins")); raise_ValueError_invalid_pins();
} }
claim_pin(pin); claim_pin(pin);
gpio_set_function(pin->number, GPIO_FUNC_UART); gpio_set_function(pin->number, GPIO_FUNC_UART);
@ -104,13 +105,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer, mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer,
bool sigint_enabled) { bool sigint_enabled) {
if (bits > 8) { mp_arg_validate_int_max(bits, 8, MP_QSTR_bits);
mp_raise_ValueError(translate("Invalid word/bit length")); mp_arg_validate_int_min(receiver_buffer_size, 1, MP_QSTR_receiver_buffer_size);
}
if (receiver_buffer_size == 0) {
mp_raise_ValueError(translate("Invalid buffer size"));
}
uint8_t uart_id = ((((tx != NULL) ? tx->number : rx->number) + 4) / 8) % NUM_UARTS; uint8_t uart_id = ((((tx != NULL) ? tx->number : rx->number) + 4) / 8) % NUM_UARTS;
@ -167,7 +163,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
// in the long-lived pool is not strictly necessary) // in the long-lived pool is not strictly necessary)
// (This is a macro.) // (This is a macro.)
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) { if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer")); m_malloc_fail(receiver_buffer_size);
} }
active_uarts[uart_id] = self; active_uarts[uart_id] = self;
if (uart_id == 1) { if (uart_id == 1) {

View File

@ -49,7 +49,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false); self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
if (self->buffer == NULL) { if (self->buffer == NULL) {
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); m_malloc_fail(maxlen * sizeof(uint16_t));
} }
self->pin = pin->number; self->pin = pin->number;
self->maxlen = maxlen; self->maxlen = maxlen;
@ -237,7 +237,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self,
index += self->len; index += self->len;
} }
if (index < 0 || index >= self->len) { if (index < 0 || index >= self->len) {
mp_raise_IndexError_varg(translate("%q index out of range"), MP_QSTR_PulseIn); mp_raise_IndexError_varg(translate("%q out of range"), MP_QSTR_index);
} }
uint16_t value = self->buffer[(self->start + index) % self->maxlen]; uint16_t value = self->buffer[(self->start + index) % self->maxlen];
return value; return value;

View File

@ -259,7 +259,7 @@ void pwmio_pwmout_set_top(pwmio_pwmout_obj_t *self, uint16_t top) {
void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self, uint32_t frequency) { void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self, uint32_t frequency) {
if (frequency == 0 || frequency > (common_hal_mcu_processor_get_frequency() / 2)) { if (frequency == 0 || frequency > (common_hal_mcu_processor_get_frequency() / 2)) {
mp_raise_ValueError(translate("Invalid PWM frequency")); mp_arg_error_invalid(MP_QSTR_frequency);
} }
target_slice_frequencies[self->slice] = frequency; target_slice_frequencies[self->slice] = frequency;

View File

@ -92,5 +92,5 @@ int common_hal_rtc_get_calibration(void) {
} }
void common_hal_rtc_set_calibration(int calibration) { void common_hal_rtc_set_calibration(int calibration) {
mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration);
} }

View File

@ -84,7 +84,7 @@ void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_
continue; continue;
} }
if (timealarm_set) { if (timealarm_set) {
mp_raise_ValueError(translate("Only one alarm.time alarm can be set.")); mp_raise_ValueError(translate("Only one alarm.time alarm can be set"));
} }
timealarm = MP_OBJ_TO_PTR(alarms[i]); timealarm = MP_OBJ_TO_PTR(alarms[i]);
timealarm_set = true; timealarm_set = true;

View File

@ -51,7 +51,7 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self,
// No ADC function on pin // No ADC function on pin
if (pin->adc_unit == 0x00) { if (pin->adc_unit == 0x00) {
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); raise_ValueError_invalid_pin();
} }
// TODO: add ADC traits to structure? // TODO: add ADC traits to structure?

View File

@ -65,7 +65,7 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self,
self->channel = DAC_CHANNEL_2; self->channel = DAC_CHANNEL_2;
self->dac_index = 1; self->dac_index = 1;
} else { } else {
mp_raise_ValueError(translate("Invalid DAC pin supplied")); raise_ValueError_invalid_pin();
} }
// Only init if the shared DAC is empty or reset // Only init if the shared DAC is empty or reset

View File

@ -120,7 +120,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
if (i2c_taken) { if (i2c_taken) {
mp_raise_ValueError(translate("Hardware busy, try alternative pins")); mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
} else { } else {
mp_raise_ValueError_varg(translate("Invalid %q pin selection"), MP_QSTR_I2C); mp_raise_ValueError_varg(translate("Invalid %q pin"), MP_QSTR_I2C);
} }
} }
@ -155,7 +155,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
} else if (frequency == 100000) { } else if (frequency == 100000) {
self->handle.Init.Timing = CPY_I2CSTANDARD_TIMINGR; self->handle.Init.Timing = CPY_I2CSTANDARD_TIMINGR;
} else { } else {
mp_raise_ValueError(translate("Unsupported baudrate")); mp_raise_ValueError(translate("Unsupported frequency"));
} }
#else #else
self->handle.Init.ClockSpeed = frequency; self->handle.Init.ClockSpeed = frequency;
@ -171,7 +171,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
self->handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; self->handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
self->handle.State = HAL_I2C_STATE_RESET; self->handle.State = HAL_I2C_STATE_RESET;
if (HAL_I2C_Init(&(self->handle)) != HAL_OK) { if (HAL_I2C_Init(&(self->handle)) != HAL_OK) {
mp_raise_RuntimeError(translate("I2C Init Error")); mp_raise_RuntimeError(translate("I2C init error"));
} }
common_hal_mcu_pin_claim(sda); common_hal_mcu_pin_claim(sda);
common_hal_mcu_pin_claim(scl); common_hal_mcu_pin_claim(scl);

View File

@ -165,7 +165,7 @@ STATIC int check_pins(busio_spi_obj_t *self,
if (spi_taken) { if (spi_taken) {
mp_raise_ValueError(translate("Hardware busy, try alternative pins")); mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
} else { } else {
mp_raise_ValueError_varg(translate("Invalid %q pin selection"), MP_QSTR_SPI); raise_ValueError_invalid_pin();
} }
} }
@ -224,7 +224,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
self->handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; self->handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
self->handle.Init.CRCPolynomial = 10; self->handle.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&self->handle) != HAL_OK) { if (HAL_SPI_Init(&self->handle) != HAL_OK) {
mp_raise_ValueError(translate("SPI Init Error")); mp_raise_ValueError(translate("SPI init error"));
} }
self->baudrate = (get_busclock(SPIx) / 16); self->baudrate = (get_busclock(SPIx) / 16);
self->prescaler = 16; self->prescaler = 16;
@ -306,7 +306,7 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
get_busclock(self->handle.Instance)); get_busclock(self->handle.Instance));
if (HAL_SPI_Init(&self->handle) != HAL_OK) { if (HAL_SPI_Init(&self->handle) != HAL_OK) {
mp_raise_ValueError(translate("SPI Re-initialization error")); mp_raise_ValueError(translate("SPI re-initialization error"));
} }
self->baudrate = baudrate; self->baudrate = baudrate;
@ -346,7 +346,7 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
bool common_hal_busio_spi_write(busio_spi_obj_t *self, bool common_hal_busio_spi_write(busio_spi_obj_t *self,
const uint8_t *data, size_t len) { const uint8_t *data, size_t len) {
if (self->mosi == NULL) { if (self->mosi == NULL) {
mp_raise_ValueError(translate("No MOSI Pin")); mp_raise_ValueError(translate("No MOSI pin"));
} }
HAL_StatusTypeDef result = HAL_SPI_Transmit(&self->handle, (uint8_t *)data, (uint16_t)len, HAL_MAX_DELAY); HAL_StatusTypeDef result = HAL_SPI_Transmit(&self->handle, (uint8_t *)data, (uint16_t)len, HAL_MAX_DELAY);
return result == HAL_OK; return result == HAL_OK;
@ -355,9 +355,9 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self,
bool common_hal_busio_spi_read(busio_spi_obj_t *self, bool common_hal_busio_spi_read(busio_spi_obj_t *self,
uint8_t *data, size_t len, uint8_t write_value) { uint8_t *data, size_t len, uint8_t write_value) {
if (self->miso == NULL && !self->half_duplex) { if (self->miso == NULL && !self->half_duplex) {
mp_raise_ValueError(translate("No MISO Pin")); mp_raise_ValueError(translate("No MISO pin"));
} else if (self->half_duplex && self->mosi == NULL) { } else if (self->half_duplex && self->mosi == NULL) {
mp_raise_ValueError(translate("No MOSI Pin")); mp_raise_ValueError(translate("No MOSI pin"));
} }
HAL_StatusTypeDef result = HAL_OK; HAL_StatusTypeDef result = HAL_OK;
if ((!self->half_duplex && self->mosi == NULL) || (self->half_duplex && self->mosi != NULL && self->miso == NULL)) { if ((!self->half_duplex && self->mosi == NULL) || (self->half_duplex && self->mosi != NULL && self->miso == NULL)) {
@ -372,7 +372,7 @@ bool common_hal_busio_spi_read(busio_spi_obj_t *self,
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
const uint8_t *data_out, uint8_t *data_in, size_t len) { const uint8_t *data_out, uint8_t *data_in, size_t len) {
if (self->miso == NULL || self->mosi == NULL) { if (self->miso == NULL || self->mosi == NULL) {
mp_raise_ValueError(translate("Missing MISO or MOSI Pin")); mp_raise_ValueError(translate("Missing MISO or MOSI pin"));
} }
HAL_StatusTypeDef result = HAL_SPI_TransmitReceive(&self->handle, HAL_StatusTypeDef result = HAL_SPI_TransmitReceive(&self->handle,
(uint8_t *)data_out, data_in, (uint16_t)len,HAL_MAX_DELAY); (uint8_t *)data_out, data_in, (uint16_t)len,HAL_MAX_DELAY);

View File

@ -58,7 +58,7 @@ STATIC USART_TypeDef *assign_uart_or_throw(busio_uart_obj_t *self, bool pin_eval
if (uart_taken) { if (uart_taken) {
mp_raise_ValueError(translate("Hardware in use, try alternative pins")); mp_raise_ValueError(translate("Hardware in use, try alternative pins"));
} else { } else {
mp_raise_ValueError_varg(translate("Invalid %q pin selection"), MP_QSTR_UART); raise_ValueError_invalid_pin();
} }
} }
} }
@ -164,12 +164,9 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
} }
// Other errors // Other errors
if (receiver_buffer_size == 0) { mp_arg_validate_length_min(receiver_buffer_size, 1, MP_QSTR_receiver_buffer_size);
mp_raise_ValueError(translate("Invalid buffer size")); mp_arg_validate_int_range(bits, 8, 9, MP_QSTR_bits);
}
if (bits != 8 && bits != 9) {
mp_raise_ValueError(translate("Invalid word/bit length"));
}
if (USARTx == NULL) { // this can only be hit if the periph file is wrong if (USARTx == NULL) { // this can only be hit if the periph file is wrong
mp_raise_ValueError(translate("Internal define error")); mp_raise_ValueError(translate("Internal define error"));
} }
@ -211,7 +208,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
self->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE; self->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
self->handle.Init.OverSampling = UART_OVERSAMPLING_16; self->handle.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&self->handle) != HAL_OK) { if (HAL_UART_Init(&self->handle) != HAL_OK) {
mp_raise_ValueError(translate("UART Init Error")); mp_raise_ValueError(translate("UART init error"));
} }
@ -221,7 +218,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
self->ringbuf = (ringbuf_t) { receiver_buffer, receiver_buffer_size }; self->ringbuf = (ringbuf_t) { receiver_buffer, receiver_buffer_size };
} else { } else {
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) { if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
mp_raise_ValueError(translate("UART Buffer allocation error")); m_malloc_fail(receiver_buffer_size);
} }
} }
common_hal_mcu_pin_claim(rx); common_hal_mcu_pin_claim(rx);
@ -408,11 +405,11 @@ void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrat
// Otherwise de-init and set new rate // Otherwise de-init and set new rate
if (HAL_UART_DeInit(&self->handle) != HAL_OK) { if (HAL_UART_DeInit(&self->handle) != HAL_OK) {
mp_raise_ValueError(translate("UART De-init error")); mp_raise_ValueError(translate("UART de-init error"));
} }
self->handle.Init.BaudRate = baudrate; self->handle.Init.BaudRate = baudrate;
if (HAL_UART_Init(&self->handle) != HAL_OK) { if (HAL_UART_Init(&self->handle) != HAL_OK) {
mp_raise_ValueError(translate("UART Re-init error")); mp_raise_ValueError(translate("UART re-init error"));
} }
self->baudrate = baudrate; self->baudrate = baudrate;

View File

@ -59,13 +59,13 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t *
const mcu_periph_obj_t *mcu_tx = find_pin_function(mcu_can_tx_list, can_tx_len, tx, -1); const mcu_periph_obj_t *mcu_tx = find_pin_function(mcu_can_tx_list, can_tx_len, tx, -1);
if (!mcu_tx) { if (!mcu_tx) {
mp_raise_ValueError_varg(translate("Invalid %q pin selection"), MP_QSTR_tx); mp_raise_ValueError_varg(translate("Invalid %q pin"), MP_QSTR_tx);
} }
int periph_index = mcu_tx->periph_index; int periph_index = mcu_tx->periph_index;
const mcu_periph_obj_t *mcu_rx = find_pin_function(mcu_can_rx_list, can_rx_len, rx, periph_index); const mcu_periph_obj_t *mcu_rx = find_pin_function(mcu_can_rx_list, can_rx_len, rx, periph_index);
if (!mcu_rx) { if (!mcu_rx) {
mp_raise_ValueError_varg(translate("Invalid %q pin selection"), MP_QSTR_rx); mp_raise_ValueError_varg(translate("Invalid %q pin"), MP_QSTR_rx);
} }
if (reserved_can[periph_index]) { if (reserved_can[periph_index]) {

View File

@ -123,8 +123,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false); self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
if (self->buffer == NULL) { if (self->buffer == NULL) {
// TODO: free the EXTI here? // TODO: free the EXTI here?
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), m_malloc_fail(maxlen * sizeof(uint16_t));
maxlen * sizeof(uint16_t));
} }
// Set internal variables // Set internal variables
@ -257,7 +256,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, int16_
} }
if (index < 0 || index >= self->len) { if (index < 0 || index >= self->len) {
stm_peripherals_exti_enable(self->pin->number); stm_peripherals_exti_enable(self->pin->number);
mp_raise_IndexError_varg(translate("%q index out of range"), MP_QSTR_PulseIn); mp_raise_IndexError_varg(translate("%q out of range"), MP_QSTR_index);
} }
uint16_t value = self->buffer[(self->start + index) % self->maxlen]; uint16_t value = self->buffer[(self->start + index) % self->maxlen];
stm_peripherals_exti_enable(self->pin->number); stm_peripherals_exti_enable(self->pin->number);

View File

@ -115,7 +115,7 @@ STATIC int check_pins(sdioio_sdcard_obj_t *self,
if (sdio_taken) { if (sdio_taken) {
mp_raise_ValueError(translate("Hardware busy, try alternative pins")); mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
} else { } else {
mp_raise_ValueError_varg(translate("Invalid %q pin selection"), MP_QSTR_SDIO); raise_ValueError_invalid_pin();
} }
} }

View File

@ -156,6 +156,13 @@ NORETURN void mp_arg_error_unimpl_kw(void) {
#endif #endif
mp_int_t mp_arg_validate_int(mp_int_t i, mp_int_t required_i, qstr arg_name) {
if (i != required_i) {
mp_raise_ValueError_varg(translate("%q must be %d"), arg_name, required_i);
}
return i;
}
mp_int_t mp_arg_validate_int_min(mp_int_t i, mp_int_t min, qstr arg_name) { mp_int_t mp_arg_validate_int_min(mp_int_t i, mp_int_t min, qstr arg_name) {
if (i < min) { if (i < min) {
mp_raise_ValueError_varg(translate("%q must be >= %d"), arg_name, min); mp_raise_ValueError_varg(translate("%q must be >= %d"), arg_name, min);
@ -194,6 +201,27 @@ mp_uint_t mp_arg_validate_length_range(mp_uint_t length, mp_uint_t min, mp_uint_
return length; return length;
} }
mp_uint_t mp_arg_validate_length_min(mp_uint_t length, mp_uint_t min, qstr arg_name) {
if (length < min) {
mp_raise_ValueError_varg(translate("%q length must be >= %d"), arg_name, min);
}
return length;
}
mp_uint_t mp_arg_validate_length_max(mp_uint_t length, mp_uint_t max, qstr arg_name) {
if (length > max) {
mp_raise_ValueError_varg(translate("%q length must be <= %d"), arg_name, max);
}
return length;
}
mp_uint_t mp_arg_validate_length(mp_uint_t length, mp_uint_t required_length, qstr arg_name) {
if (length != required_length) {
mp_raise_ValueError_varg(translate("%q length must be %d"), arg_name, required_length);
}
return length;
}
mp_obj_t mp_arg_validate_type(mp_obj_t obj, const mp_obj_type_t *type, qstr arg_name) { mp_obj_t mp_arg_validate_type(mp_obj_t obj, const mp_obj_type_t *type, qstr arg_name) {
if (!mp_obj_is_type(obj, type)) { if (!mp_obj_is_type(obj, type)) {
mp_raise_TypeError_varg(translate("%q must be of type %q"), arg_name, type->name); mp_raise_TypeError_varg(translate("%q must be of type %q"), arg_name, type->name);
@ -201,9 +229,21 @@ mp_obj_t mp_arg_validate_type(mp_obj_t obj, const mp_obj_type_t *type, qstr arg_
return obj; return obj;
} }
mp_obj_t mp_arg_validate_string(mp_obj_t obj, qstr arg_name) { mp_obj_t mp_arg_validate_type_string(mp_obj_t obj, qstr arg_name) {
if (!mp_obj_is_str(obj)) { if (!mp_obj_is_str(obj)) {
mp_raise_TypeError_varg(translate("%q must be a string"), arg_name); mp_raise_TypeError_varg(translate("%q must be a string"), arg_name);
} }
return obj; return obj;
} }
mp_int_t mp_arg_validate_type_int(mp_obj_t obj, qstr arg_name) {
mp_int_t an_int;
if (!mp_obj_get_int_maybe(obj, &an_int)) {
mp_raise_TypeError_varg(translate("%q must be an int"), arg_name);
}
return an_int;
}
NORETURN void mp_arg_error_invalid(qstr arg_name) {
mp_raise_ValueError_varg(translate("Invalid %q"), arg_name);
}

View File

@ -282,7 +282,7 @@ STATIC void evaluate_relative_import(mp_int_t level, const char **module_name, s
#endif #endif
// If we have a __path__ in the globals dict, then we're a package. // If we have a __path__ in the globals dict, then we're a package.
bool is_pkg = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(MP_QSTR___path__), MP_MAP_LOOKUP); bool is_pkg = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(MP_QSTR___path__), MP_MAP_LOOKUP) != NULL;
#if DEBUG_PRINT #if DEBUG_PRINT
DEBUG_printf("Current module/package: "); DEBUG_printf("Current module/package: ");

View File

@ -92,15 +92,21 @@ void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args,
NORETURN void mp_arg_error_terse_mismatch(void); NORETURN void mp_arg_error_terse_mismatch(void);
NORETURN void mp_arg_error_unimpl_kw(void); NORETURN void mp_arg_error_unimpl_kw(void);
NORETURN void mp_arg_error_invalid(qstr arg_name);
mp_int_t mp_arg_validate_int(mp_int_t i, mp_int_t required_i, qstr arg_name);
mp_int_t mp_arg_validate_int_min(mp_int_t i, mp_int_t min, qstr arg_name); mp_int_t mp_arg_validate_int_min(mp_int_t i, mp_int_t min, qstr arg_name);
mp_int_t mp_arg_validate_int_max(mp_int_t i, mp_int_t j, qstr arg_name); mp_int_t mp_arg_validate_int_max(mp_int_t i, mp_int_t j, qstr arg_name);
mp_int_t mp_arg_validate_int_range(mp_int_t i, mp_int_t min, mp_int_t max, qstr arg_name); mp_int_t mp_arg_validate_int_range(mp_int_t i, mp_int_t min, mp_int_t max, qstr arg_name);
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t default_for_null, qstr arg_name); mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t default_for_null, qstr arg_name);
#endif #endif
mp_uint_t mp_arg_validate_length_min(mp_uint_t length, mp_uint_t min, qstr arg_name);
mp_uint_t mp_arg_validate_length_max(mp_uint_t length, mp_uint_t max, qstr arg_name);
mp_uint_t mp_arg_validate_length_range(mp_uint_t length, mp_uint_t min, mp_uint_t max, qstr arg_name); mp_uint_t mp_arg_validate_length_range(mp_uint_t length, mp_uint_t min, mp_uint_t max, qstr arg_name);
mp_uint_t mp_arg_validate_length(mp_uint_t length, mp_uint_t required_length, qstr arg_name);
mp_obj_t mp_arg_validate_type(mp_obj_t obj, const mp_obj_type_t *type, qstr arg_name); mp_obj_t mp_arg_validate_type(mp_obj_t obj, const mp_obj_type_t *type, qstr arg_name);
mp_obj_t mp_arg_validate_string(mp_obj_t obj, qstr arg_name); mp_int_t mp_arg_validate_type_int(mp_obj_t obj, qstr arg_name);
mp_obj_t mp_arg_validate_type_string(mp_obj_t obj, qstr arg_name);
static inline mp_obj_dict_t *PLACE_IN_ITCM(mp_locals_get)(void) { static inline mp_obj_dict_t *PLACE_IN_ITCM(mp_locals_get)(void) {
return MP_STATE_THREAD(dict_locals); return MP_STATE_THREAD(dict_locals);

View File

@ -69,7 +69,7 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args,
const mp_int_t address_type = args[ARG_address_type].u_int; const mp_int_t address_type = args[ARG_address_type].u_int;
if (address_type < BLEIO_ADDRESS_TYPE_MIN || address_type > BLEIO_ADDRESS_TYPE_MAX) { if (address_type < BLEIO_ADDRESS_TYPE_MIN || address_type > BLEIO_ADDRESS_TYPE_MAX) {
mp_raise_ValueError(translate("Address type out of range")); mp_arg_error_invalid(MP_QSTR_address_type);
} }
common_hal_bleio_address_construct(self, buf_info.buf, address_type); common_hal_bleio_address_construct(self, buf_info.buf, address_type);

View File

@ -100,7 +100,7 @@ STATIC mp_obj_t bleio_characteristic_add_to_service(size_t n_args, const mp_obj_
const bleio_characteristic_properties_t properties = args[ARG_properties].u_int; const bleio_characteristic_properties_t properties = args[ARG_properties].u_int;
if (properties & ~CHAR_PROP_ALL) { if (properties & ~CHAR_PROP_ALL) {
mp_raise_ValueError(translate("Invalid properties")); mp_arg_error_invalid(MP_QSTR_properties);
} }
const bleio_attribute_security_mode_t read_perm = args[ARG_read_perm].u_int; const bleio_attribute_security_mode_t read_perm = args[ARG_read_perm].u_int;
@ -109,10 +109,8 @@ STATIC mp_obj_t bleio_characteristic_add_to_service(size_t n_args, const mp_obj_
const bleio_attribute_security_mode_t write_perm = args[ARG_write_perm].u_int; const bleio_attribute_security_mode_t write_perm = args[ARG_write_perm].u_int;
common_hal_bleio_attribute_security_mode_check_valid(write_perm); common_hal_bleio_attribute_security_mode_check_valid(write_perm);
const mp_int_t max_length_int = args[ARG_max_length].u_int; const mp_int_t max_length_int = mp_arg_validate_int_min(args[ARG_max_length].u_int, 0, MP_QSTR_max_length);
if (max_length_int < 0) {
mp_raise_ValueError(translate("max_length must be >= 0"));
}
const size_t max_length = (size_t)max_length_int; const size_t max_length = (size_t)max_length_int;
const bool fixed_length = args[ARG_fixed_length].u_bool; const bool fixed_length = args[ARG_fixed_length].u_bool;
mp_obj_t initial_value = args[ARG_initial_value].u_obj; mp_obj_t initial_value = args[ARG_initial_value].u_obj;

View File

@ -70,15 +70,9 @@ STATIC mp_obj_t bleio_characteristic_buffer_make_new(const mp_obj_type_t *type,
bleio_characteristic_obj_t *characteristic = mp_arg_validate_type(args[ARG_characteristic].u_obj, &bleio_characteristic_type, MP_QSTR_characteristic); bleio_characteristic_obj_t *characteristic = mp_arg_validate_type(args[ARG_characteristic].u_obj, &bleio_characteristic_type, MP_QSTR_characteristic);
mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj); mp_float_t timeout = mp_arg_validate_obj_float_non_negative(args[ARG_timeout].u_obj, 1.0f, MP_QSTR_timeout);
if (timeout < 0.0f) {
mp_raise_ValueError(translate("timeout must be >= 0.0"));
}
const int buffer_size = args[ARG_buffer_size].u_int; const mp_int_t buffer_size = mp_arg_validate_int_min(args[ARG_buffer_size].u_int, 1, MP_QSTR_buffer_size);
if (buffer_size < 1) {
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_buffer_size);
}
bleio_characteristic_buffer_obj_t *self = m_new_obj(bleio_characteristic_buffer_obj_t); bleio_characteristic_buffer_obj_t *self = m_new_obj(bleio_characteristic_buffer_obj_t);
self->base.type = &bleio_characteristic_buffer_type; self->base.type = &bleio_characteristic_buffer_type;

View File

@ -94,10 +94,8 @@ STATIC mp_obj_t bleio_descriptor_add_to_characteristic(size_t n_args, const mp_o
const bleio_attribute_security_mode_t write_perm = args[ARG_write_perm].u_int; const bleio_attribute_security_mode_t write_perm = args[ARG_write_perm].u_int;
common_hal_bleio_attribute_security_mode_check_valid(write_perm); common_hal_bleio_attribute_security_mode_check_valid(write_perm);
const mp_int_t max_length_int = args[ARG_max_length].u_int; const mp_int_t max_length_int = mp_arg_validate_int_min(args[ARG_max_length].u_int, 0, MP_QSTR_max_length);
if (max_length_int < 0) {
mp_raise_ValueError(translate("max_length must be >= 0"));
}
const size_t max_length = (size_t)max_length_int; const size_t max_length = (size_t)max_length_int;
const bool fixed_length = args[ARG_fixed_length].u_bool; const bool fixed_length = args[ARG_fixed_length].u_bool;
mp_obj_t initial_value = args[ARG_initial_value].u_obj; mp_obj_t initial_value = args[ARG_initial_value].u_obj;

View File

@ -73,10 +73,7 @@ STATIC mp_obj_t bleio_packet_buffer_make_new(const mp_obj_type_t *type, size_t n
bleio_characteristic_obj_t *characteristic = mp_arg_validate_type(args[ARG_characteristic].u_obj, &bleio_characteristic_type, MP_QSTR_characteristic); bleio_characteristic_obj_t *characteristic = mp_arg_validate_type(args[ARG_characteristic].u_obj, &bleio_characteristic_type, MP_QSTR_characteristic);
const mp_int_t buffer_size = args[ARG_buffer_size].u_int; const mp_int_t buffer_size = mp_arg_validate_int_min(args[ARG_buffer_size].u_int, 1, MP_QSTR_buffer_size);
if (buffer_size < 1) {
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_buffer_size);
}
size_t max_packet_size = common_hal_bleio_characteristic_get_max_length(characteristic); size_t max_packet_size = common_hal_bleio_characteristic_get_max_length(characteristic);
if (args[ARG_max_packet_size].u_obj != mp_const_none) { if (args[ARG_max_packet_size].u_obj != mp_const_none) {

View File

@ -44,6 +44,10 @@
#include "extmod/ulab/code/ndarray.h" #include "extmod/ulab/code/ndarray.h"
#endif #endif
static NORETURN void invalid_byteorder(void) {
mp_arg_error_invalid(MP_QSTR_byteorder);
}
static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t *parsed); static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t *parsed);
//| class PixelBuf: //| class PixelBuf:
@ -124,7 +128,7 @@ static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t
size_t bo_len; size_t bo_len;
const char *byteorder = mp_obj_str_get_data(byteorder_obj, &bo_len); const char *byteorder = mp_obj_str_get_data(byteorder_obj, &bo_len);
if (bo_len < 3 || bo_len > 4) { if (bo_len < 3 || bo_len > 4) {
mp_raise_ValueError(translate("Invalid byteorder string")); invalid_byteorder();
} }
parsed->order_string = byteorder_obj; parsed->order_string = byteorder_obj;
@ -136,7 +140,7 @@ static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t
char *w = strchr(byteorder, 'W'); char *w = strchr(byteorder, 'W');
int num_chars = (dotstar ? 1 : 0) + (w ? 1 : 0) + (r ? 1 : 0) + (g ? 1 : 0) + (b ? 1 : 0); int num_chars = (dotstar ? 1 : 0) + (w ? 1 : 0) + (r ? 1 : 0) + (g ? 1 : 0) + (b ? 1 : 0);
if ((num_chars < parsed->bpp) || !(r && b && g)) { if ((num_chars < parsed->bpp) || !(r && b && g)) {
mp_raise_ValueError(translate("Invalid byteorder string")); invalid_byteorder();
} }
parsed->is_dotstar = dotstar ? true : false; parsed->is_dotstar = dotstar ? true : false;
parsed->has_white = w ? true : false; parsed->has_white = w ? true : false;
@ -146,10 +150,10 @@ static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t
parsed->byteorder.w = w ? w - byteorder : 0; parsed->byteorder.w = w ? w - byteorder : 0;
// The dotstar brightness byte is always first (as it goes with the pixel start bits) // The dotstar brightness byte is always first (as it goes with the pixel start bits)
if (dotstar && byteorder[0] != 'P') { if (dotstar && byteorder[0] != 'P') {
mp_raise_ValueError(translate("Invalid byteorder string")); invalid_byteorder();
} }
if (parsed->has_white && parsed->is_dotstar) { if (parsed->has_white && parsed->is_dotstar) {
mp_raise_ValueError(translate("Invalid byteorder string")); invalid_byteorder();
} }
} }

Some files were not shown because too many files have changed in this diff Show More