2020-10-24 21:48:35 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-10-26 17:54:24 -04:00
|
|
|
* Copyright (c) 2020 Mark Komus
|
2020-10-24 21:48:35 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-11-03 19:35:20 -05:00
|
|
|
#include "shared-bindings/adafruit_bus_device/I2CDevice.h"
|
2020-10-24 21:48:35 -04:00
|
|
|
#include "shared-bindings/busio/I2C.h"
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/nlr.h"
|
2020-10-25 11:15:45 -04:00
|
|
|
#include "py/runtime.h"
|
2020-11-11 10:58:08 -05:00
|
|
|
#include "lib/utils/interrupt_char.h"
|
2020-10-24 21:48:35 -04:00
|
|
|
|
2021-01-05 00:11:25 -05:00
|
|
|
void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t *i2c, uint8_t device_address) {
|
2020-10-24 21:48:35 -04:00
|
|
|
self->i2c = i2c;
|
|
|
|
self->device_address = device_address;
|
|
|
|
}
|
|
|
|
|
2020-11-03 19:35:20 -05:00
|
|
|
void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self) {
|
2021-01-05 00:11:25 -05:00
|
|
|
mp_obj_t dest[2];
|
|
|
|
mp_load_method(self->i2c, MP_QSTR_try_lock, dest);
|
2020-11-11 10:58:08 -05:00
|
|
|
|
2021-01-05 00:11:25 -05:00
|
|
|
mp_obj_t success = mp_call_method_n_kw(0, 0, dest);
|
|
|
|
|
|
|
|
while (!mp_obj_is_true(success)) {
|
2020-11-11 10:58:08 -05:00
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-05 00:11:25 -05:00
|
|
|
success = mp_call_method_n_kw(0, 0, dest);
|
2020-10-24 21:48:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 19:35:20 -05:00
|
|
|
void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self) {
|
2021-01-05 00:11:25 -05:00
|
|
|
mp_obj_t dest[2];
|
|
|
|
mp_load_method(self->i2c, MP_QSTR_unlock, dest);
|
|
|
|
mp_call_method_n_kw(0, 0, dest);
|
2020-10-24 21:48:35 -04:00
|
|
|
}
|
|
|
|
|
2020-11-03 19:35:20 -05:00
|
|
|
void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self) {
|
|
|
|
common_hal_adafruit_bus_device_i2cdevice_lock(self);
|
2020-10-24 21:48:35 -04:00
|
|
|
|
2021-01-05 00:11:25 -05:00
|
|
|
mp_buffer_info_t write_bufinfo;
|
|
|
|
mp_obj_t write_buffer = mp_obj_new_bytearray_of_zeros(0);
|
|
|
|
mp_get_buffer_raise(write_buffer, &write_bufinfo, MP_BUFFER_READ);
|
|
|
|
|
|
|
|
mp_obj_t dest[4];
|
2020-10-24 21:48:35 -04:00
|
|
|
|
2021-01-05 00:11:25 -05:00
|
|
|
/* catch exceptions that may be thrown while probing for the device */
|
2021-01-09 11:07:08 -05:00
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
2021-01-05 00:11:25 -05:00
|
|
|
mp_load_method(self->i2c, MP_QSTR_writeto, dest);
|
2021-02-01 18:58:34 -05:00
|
|
|
dest[2] = MP_OBJ_NEW_SMALL_INT(self->device_address);
|
2021-01-05 00:11:25 -05:00
|
|
|
dest[3] = write_buffer;
|
|
|
|
mp_call_method_n_kw(2, 0, dest);
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
2021-01-09 11:07:08 -05:00
|
|
|
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
|
2020-10-24 21:48:35 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
|
2021-01-09 11:07:08 -05:00
|
|
|
mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
|
2021-03-15 09:57:36 -04:00
|
|
|
} else {
|
2021-01-09 11:07:08 -05:00
|
|
|
/* In case we receive an unrelated exception pass it up */
|
|
|
|
nlr_raise(MP_OBJ_FROM_PTR(nlr.ret_val));
|
2021-01-05 00:11:25 -05:00
|
|
|
}
|
2020-10-25 11:15:45 -04:00
|
|
|
}
|
2020-10-27 10:13:14 -04:00
|
|
|
|
2020-11-03 19:35:20 -05:00
|
|
|
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
|
2020-10-27 10:13:14 -04:00
|
|
|
}
|