summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-02-13 16:46:57 +0100
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-02-13 16:46:57 +0100
commit9ed3e11aec6ed40a86fa846b9fe2fa76224d8c05 (patch)
tree953a349ddbb060b4ae8b353c4171f278482b053d
parent26c61d43f414b2b2de3c97dbd8559d206c24fc9f (diff)
atmel-samd: Clean up I2C correctly when an initialization error occurs. Fixes #95.
-rw-r--r--atmel-samd/common-hal/nativeio/I2C.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/atmel-samd/common-hal/nativeio/I2C.c b/atmel-samd/common-hal/nativeio/I2C.c
index 8e3615232..fb1080268 100644
--- a/atmel-samd/common-hal/nativeio/I2C.c
+++ b/atmel-samd/common-hal/nativeio/I2C.c
@@ -81,15 +81,21 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
enum status_code status = i2c_master_init(&self->i2c_master_instance,
sercom, &config_i2c_master);
+
if (status != STATUS_OK) {
- nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus init error"));
+ common_hal_nativeio_i2c_deinit(self);
+ if (status == STATUS_ERR_BAUDRATE_UNAVAILABLE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Unsupported baudrate"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus init error"));
+ }
}
i2c_master_enable(&self->i2c_master_instance);
}
void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self) {
- i2c_master_disable(&self->i2c_master_instance);
+ i2c_master_reset(&self->i2c_master_instance);
reset_pin(self->sda_pin);
reset_pin(self->scl_pin);
}