Skip to content

Commit 12fa5b3

Browse files
committed
Switch exception throwing to mp_raise helpers. It saves a little code space each time to share the call.
1 parent efd4294 commit 12fa5b3

57 files changed

Lines changed: 327 additions & 296 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

atmel-samd/common-hal/nativeio/AnalogIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
4646
const mcu_pin_obj_t *pin) {
4747
if (!pin->has_adc) {
4848
// No ADC function on that pin
49-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Pin does not have ADC capabilities"));
49+
mp_raise_ValueError("Pin does not have ADC capabilities");
5050
}
5151

5252
self->pin = pin;

atmel-samd/common-hal/nativeio/AnalogOut.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <stdint.h>
2828
#include <string.h>
2929

30+
#include "py/mperrno.h"
3031
#include "py/runtime.h"
3132

3233
#include "shared-bindings/nativeio/AnalogOut.h"
@@ -37,17 +38,15 @@
3738
void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
3839
const mcu_pin_obj_t *pin) {
3940
if (pin->pin != PIN_PA02) {
40-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
41-
"AnalogOut not supported on given pin."));
41+
mp_raise_ValueError("AnalogOut not supported on given pin");
4242
return;
4343
}
4444
struct dac_config config_dac;
4545
dac_get_config_defaults(&config_dac);
4646
config_dac.reference = DAC_REFERENCE_AVCC;
4747
enum status_code status = dac_init(&self->dac_instance, DAC, &config_dac);
4848
if (status != STATUS_OK) {
49-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
50-
"DAC init failed."));
49+
mp_raise_OSError(MP_EIO);
5150
return;
5251
}
5352

atmel-samd/common-hal/nativeio/DigitalInOut.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull(
171171
PortGroup *const port_base = port_get_group_from_gpio_pin(pin);
172172
uint32_t pin_mask = (1UL << (pin % 32));
173173
if (self->output) {
174-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
175-
"Cannot get pull while in output mode."));
174+
mp_raise_AttributeError("Cannot get pull while in output mode");
176175
return PULL_NONE;
177176
} else {
178177
if (port_base->PINCFG[pin % 32].bit.PULLEN == 0) {

atmel-samd/common-hal/nativeio/I2C.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "shared-bindings/nativeio/I2C.h"
3131
#include "py/mperrno.h"
3232
#include "py/nlr.h"
33+
#include "py/runtime.h"
3334

3435
#include "asf/sam0/drivers/sercom/i2c/i2c_master.h"
3536
#include "samd21_pins.h"
@@ -70,7 +71,7 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
7071
}
7172
}
7273
if (sercom == NULL) {
73-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pins."));
74+
mp_raise_ValueError("Invalid pins");
7475
}
7576

7677
config_i2c_master.pinmux_pad0 = sda_pinmux; // SDA
@@ -86,9 +87,9 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
8687
if (status != STATUS_OK) {
8788
common_hal_nativeio_i2c_deinit(self);
8889
if (status == STATUS_ERR_BAUDRATE_UNAVAILABLE) {
89-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Unsupported baudrate"));
90+
mp_raise_ValueError("Unsupported baudrate");
9091
} else {
91-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus init error"));
92+
mp_raise_OSError(MP_EIO);
9293
}
9394
}
9495

atmel-samd/common-hal/nativeio/PWMOut.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,11 @@ void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self,
7979
self->variable_frequency = variable_frequency;
8080

8181
if (pin->primary_timer.tc == 0 && pin->secondary_timer.tc == 0) {
82-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
83-
"Invalid pin"));
82+
mp_raise_ValueError("Invalid pin");
8483
}
8584

8685
if (frequency == 0 || frequency > 6000000) {
87-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
88-
"Invalid PWM frequency"));
86+
mp_raise_ValueError("Invalid PWM frequency");
8987
}
9088

9189
uint16_t primary_timer_index = 0xff;
@@ -136,7 +134,7 @@ void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self,
136134
index = primary_timer_index;
137135
}
138136
if (t == NULL) {
139-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "All timers in use"));
137+
mp_raise_RuntimeError("All timers in use");
140138
return;
141139
}
142140
uint8_t resolution = 0;
@@ -265,8 +263,7 @@ uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self)
265263
void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self,
266264
uint32_t frequency) {
267265
if (frequency == 0 || frequency > 6000000) {
268-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
269-
"Invalid PWM frequency"));
266+
mp_raise_ValueError("Invalid PWM frequency");
270267
}
271268
const pin_timer_t* t = self->timer;
272269
uint8_t resolution;

atmel-samd/common-hal/nativeio/SPI.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "shared-bindings/nativeio/SPI.h"
3131
#include "py/nlr.h"
32+
#include "py/runtime.h"
3233
#include "samd21_pins.h"
3334

3435
// We use ENABLE registers below we don't want to treat as a macro.
@@ -92,7 +93,7 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
9293
}
9394
}
9495
if (sercom == NULL) {
95-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pins"));
96+
mp_raise_ValueError("Invalid pins");
9697
}
9798

9899
// Depends on where MOSI and CLK are.
@@ -111,7 +112,7 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
111112
}
112113
}
113114
if (dopo == 8) {
114-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI MOSI and clock pins incompatible"));
115+
mp_raise_ValueError("MOSI and clock pins incompatible");
115116
}
116117

117118
config_spi_master.mux_setting = (dopo << SERCOM_SPI_CTRLA_DOPO_Pos) |

atmel-samd/common-hal/nativeio/TouchIn.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <string.h>
2828

2929
#include "py/nlr.h"
30+
#include "py/mperrno.h"
3031
#include "py/runtime.h"
3132
#include "py/binary.h"
3233
#include "py/mphal.h"
@@ -186,8 +187,7 @@ nativeio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS];
186187
void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self,
187188
const mcu_pin_obj_t *pin) {
188189
if (!pin->has_touch) {
189-
// No ADC function on that pin
190-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %q does not have touch capabilities", pin->name));
190+
mp_raise_ValueError("Invalid pin");
191191
}
192192

193193
if (selfcap_config.num_channels > 0) {
@@ -230,18 +230,18 @@ void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self,
230230
PRIV_SELFCAP_RS_TABLE_INIT, PRIV_NM_TABLE_INIT,
231231
PRIV_FREQ_AUTO_TUNE_CHK, PRIV_MOIS_TOLERANCE_CHK);
232232
if (status != TOUCH_SUCCESS) {
233-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch init failed (%d)", status));
233+
mp_raise_OSError(MP_EIO);
234234
}
235235
for (int i = 0; i < selfcap_config.num_channels; i++) {
236236
status = touch_selfcap_sensor_config(SENSOR_TYPE_KEY, i, i,
237237
NO_AKS_GROUP, 10u, HYST_25, RES_8_BIT, &active_touchin_obj[i]->sensor_id);
238238
if (status != TOUCH_SUCCESS) {
239-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch pad config failed (%d)", status));
239+
mp_raise_OSError(MP_EIO);
240240
}
241241
}
242242
status = touch_selfcap_sensors_calibrate(AUTO_TUNE_RSEL);
243243
if (status != TOUCH_SUCCESS) {
244-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch pad calibration failed (%d)", status));
244+
mp_raise_OSError(MP_EIO);
245245
}
246246

247247
// Run a measurement to get calibrated.
@@ -275,7 +275,7 @@ void touch_selfcap_measure_complete_callback(void)
275275

276276
bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) {
277277
if (p_selfcap_measure_data->acq_status & TOUCH_CC_CALIB_ERROR) {
278-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch calibration error"));
278+
mp_raise_RuntimeError("Touch calibration error");
279279
}
280280
touch_acq_status = TOUCH_BURST_AGAIN;
281281
uint64_t start_ticks = ticks_ms;
@@ -287,7 +287,7 @@ bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) {
287287
touch_selfcap_measure_complete_callback);
288288

289289
if (touch_ret != TOUCH_SUCCESS) {
290-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch measure failed"));
290+
mp_raise_OSError(MP_EIO);
291291
}
292292

293293
while(!touch_read_ready && ticks_ms - start_ticks < 1000) {
@@ -299,7 +299,7 @@ bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) {
299299
}
300300

301301
if (touch_acq_status & TOUCH_BURST_AGAIN) {
302-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch read failed"));
302+
mp_raise_OSError(MP_EIO);
303303
}
304304

305305
return (p_selfcap_measure_data->p_sensor_states[self->sensor_id / 8] & (1 << (self->sensor_id % 8))) != 0;

atmel-samd/common-hal/nativeio/TouchInStub.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434

3535
void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self,
3636
const mcu_pin_obj_t *pin) {
37-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
38-
"No room in flash for capacitive touch hardware support."));
37+
mp_raise_NotImplementError("No capacitive touch support");
3938
}
4039

4140
void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) {

atmel-samd/common-hal/nativeio/UART.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "py/gc.h"
3232
#include "py/mperrno.h"
3333
#include "py/nlr.h"
34+
#include "py/runtime.h"
3435
#include "py/stream.h"
3536
#include "samd21_pins.h"
3637
#include "tick.h"
@@ -162,7 +163,7 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self,
162163
}
163164
}
164165
if (sercom == NULL) {
165-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid pins"));
166+
mp_raise_ValueError("Invalid pins");
166167
}
167168
if (tx == NULL) {
168169
tx_pad = 0;
@@ -216,12 +217,11 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self,
216217
self->buffer_length *= (bits + 7) / 8;
217218
self->buffer = (uint8_t *) gc_alloc(self->buffer_length * sizeof(uint8_t), false);
218219
if (self->buffer == NULL) {
219-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
220-
"Unable to allocate RX buffer."));
220+
mp_raise_msg(&mp_type_MemoryError, "Failed to allocate RX buffer");
221221
}
222222

223223
if (usart_init(&self->uart_instance, sercom, &config_usart) != STATUS_OK) {
224-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Unable to init UART"));
224+
mp_raise_OSError(MP_EIO);
225225
}
226226

227227
// We use our own interrupt handler because we want a circular buffer

atmel-samd/moduos.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/mpstate.h"
3131
#include "py/objtuple.h"
3232
#include "py/objstr.h"
33+
#include "py/runtime.h"
3334
#include "genhdr/mpversion.h"
3435
#include "lib/fatfs/ff.h"
3536
#include "lib/fatfs/diskio.h"
@@ -103,7 +104,7 @@ STATIC mp_obj_t os_getcwd(void) {
103104
FRESULT res = f_getcwd(buf, sizeof buf);
104105

105106
if (res != FR_OK) {
106-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
107+
mp_raise_OSError(fresult_to_errno_table[res]);
107108
}
108109

109110
return mp_obj_new_str(buf, strlen(buf), false);

0 commit comments

Comments
 (0)