2016-11-03 18:50:59 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Damien P. George, Scott Shawcroft
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared-bindings/bitbangio/I2C.h"
|
2017-02-19 11:02:29 -05:00
|
|
|
#include "py/mperrno.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "py/obj.h"
|
2018-05-13 22:21:05 -04:00
|
|
|
#include "py/runtime.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-12 18:24:50 -04:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "shared-module/bitbangio/types.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
STATIC void delay(bitbangio_i2c_obj_t *self) {
|
|
|
|
// We need to use an accurate delay to get acceptable I2C
|
|
|
|
// speeds (eg 1us should be not much more than 1us).
|
|
|
|
common_hal_mcu_delay_us(self->us_delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void scl_low(bitbangio_i2c_obj_t *self) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->scl, false);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void scl_release(bitbangio_i2c_obj_t *self) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->scl, true);
|
2018-05-13 21:54:44 -04:00
|
|
|
uint32_t count = self->us_timeout;
|
2016-11-03 18:50:59 -04:00
|
|
|
delay(self);
|
|
|
|
// For clock stretching, wait for the SCL pin to be released, with timeout.
|
2018-05-13 21:54:44 -04:00
|
|
|
for (; !common_hal_digitalio_digitalinout_get_value(&self->scl) && count; --count) {
|
2016-11-03 18:50:59 -04:00
|
|
|
common_hal_mcu_delay_us(1);
|
|
|
|
}
|
2018-05-16 14:19:51 -04:00
|
|
|
// raise exception on timeout
|
|
|
|
if (count == 0) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_msg(&mp_type_TimeoutError, translate("Clock stretch too long"));
|
2018-05-13 21:54:44 -04:00
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void sda_low(bitbangio_i2c_obj_t *self) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->sda, false);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void sda_release(bitbangio_i2c_obj_t *self) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->sda, true);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC bool sda_read(bitbangio_i2c_obj_t *self) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_switch_to_input(&self->sda, PULL_UP);
|
|
|
|
bool value = common_hal_digitalio_digitalinout_get_value(&self->sda);
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->sda, true, DRIVE_MODE_OPEN_DRAIN);
|
2016-11-03 18:50:59 -04:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void start(bitbangio_i2c_obj_t *self) {
|
|
|
|
sda_release(self);
|
|
|
|
delay(self);
|
|
|
|
scl_release(self);
|
|
|
|
sda_low(self);
|
|
|
|
delay(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void stop(bitbangio_i2c_obj_t *self) {
|
|
|
|
delay(self);
|
|
|
|
sda_low(self);
|
|
|
|
delay(self);
|
|
|
|
scl_release(self);
|
|
|
|
sda_release(self);
|
|
|
|
delay(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int write_byte(bitbangio_i2c_obj_t *self, uint8_t val) {
|
|
|
|
delay(self);
|
|
|
|
scl_low(self);
|
|
|
|
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
|
|
if ((val >> i) & 1) {
|
|
|
|
sda_release(self);
|
|
|
|
} else {
|
|
|
|
sda_low(self);
|
|
|
|
}
|
|
|
|
delay(self);
|
|
|
|
scl_release(self);
|
|
|
|
scl_low(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
sda_release(self);
|
|
|
|
delay(self);
|
|
|
|
scl_release(self);
|
|
|
|
|
|
|
|
int ret = sda_read(self);
|
|
|
|
delay(self);
|
|
|
|
scl_low(self);
|
|
|
|
|
|
|
|
return !ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC bool read_byte(bitbangio_i2c_obj_t *self, uint8_t *val, bool ack) {
|
|
|
|
delay(self);
|
|
|
|
scl_low(self);
|
|
|
|
delay(self);
|
|
|
|
|
|
|
|
uint8_t data = 0;
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
|
|
scl_release(self);
|
|
|
|
data = (data << 1) | sda_read(self);
|
|
|
|
scl_low(self);
|
|
|
|
delay(self);
|
|
|
|
}
|
|
|
|
*val = data;
|
|
|
|
|
|
|
|
// send ack/nack bit
|
|
|
|
if (ack) {
|
|
|
|
sda_low(self);
|
|
|
|
}
|
|
|
|
delay(self);
|
|
|
|
scl_release(self);
|
|
|
|
scl_low(self);
|
|
|
|
sda_release(self);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self,
|
|
|
|
const mcu_pin_obj_t * scl,
|
|
|
|
const mcu_pin_obj_t * sda,
|
2018-05-13 21:54:44 -04:00
|
|
|
uint32_t frequency,
|
|
|
|
uint32_t us_timeout) {
|
|
|
|
|
|
|
|
self->us_timeout = us_timeout;
|
2016-11-30 18:08:34 -05:00
|
|
|
self->us_delay = 500000 / frequency;
|
2016-11-03 18:50:59 -04:00
|
|
|
if (self->us_delay == 0) {
|
|
|
|
self->us_delay = 1;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->scl, scl);
|
2016-11-03 18:50:59 -04:00
|
|
|
if (result != DIGITALINOUT_OK) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
result = common_hal_digitalio_digitalinout_construct(&self->sda, sda);
|
2016-11-03 18:50:59 -04:00
|
|
|
if (result != DIGITALINOUT_OK) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->scl);
|
2016-11-03 18:50:59 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->scl, true, DRIVE_MODE_OPEN_DRAIN);
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->sda, true, DRIVE_MODE_OPEN_DRAIN);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
stop(self);
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:49:40 -04:00
|
|
|
bool shared_module_bitbangio_i2c_deinited(bitbangio_i2c_obj_t *self) {
|
|
|
|
// If one is deinited, both will be.
|
|
|
|
return common_hal_digitalio_digitalinout_deinited(&self->scl);
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self) {
|
2017-10-02 20:49:40 -04:00
|
|
|
if (shared_module_bitbangio_i2c_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->scl);
|
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->sda);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
bool shared_module_bitbangio_i2c_try_lock(bitbangio_i2c_obj_t *self) {
|
|
|
|
bool success = false;
|
|
|
|
common_hal_mcu_disable_interrupts();
|
|
|
|
if (!self->locked) {
|
|
|
|
self->locked = true;
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
common_hal_mcu_enable_interrupts();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shared_module_bitbangio_i2c_has_lock(bitbangio_i2c_obj_t *self) {
|
|
|
|
return self->locked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shared_module_bitbangio_i2c_unlock(bitbangio_i2c_obj_t *self) {
|
|
|
|
self->locked = false;
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
bool shared_module_bitbangio_i2c_probe(bitbangio_i2c_obj_t *self, uint8_t addr) {
|
|
|
|
start(self);
|
|
|
|
bool ok = write_byte(self, addr << 1);
|
|
|
|
stop(self);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2017-02-19 11:02:29 -05:00
|
|
|
uint8_t shared_module_bitbangio_i2c_write(bitbangio_i2c_obj_t *self, uint16_t addr,
|
2016-11-03 18:50:59 -04:00
|
|
|
const uint8_t *data, size_t len, bool transmit_stop_bit) {
|
|
|
|
// start the I2C transaction
|
|
|
|
start(self);
|
2017-02-19 11:02:29 -05:00
|
|
|
uint8_t status = 0;
|
|
|
|
if (!write_byte(self, addr << 1)) {
|
|
|
|
status = MP_ENODEV;
|
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-02-19 11:02:29 -05:00
|
|
|
if (status == 0) {
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
if (!write_byte(self, data[i])) {
|
|
|
|
status = MP_EIO;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transmit_stop_bit) {
|
|
|
|
stop(self);
|
|
|
|
}
|
2017-02-19 11:02:29 -05:00
|
|
|
return status;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-02-19 11:02:29 -05:00
|
|
|
uint8_t shared_module_bitbangio_i2c_read(bitbangio_i2c_obj_t *self, uint16_t addr,
|
2016-11-03 18:50:59 -04:00
|
|
|
uint8_t * data, size_t len) {
|
|
|
|
// start the I2C transaction
|
|
|
|
start(self);
|
2017-02-19 11:02:29 -05:00
|
|
|
uint8_t status = 0;
|
|
|
|
if (!write_byte(self, (addr << 1) | 1)) {
|
|
|
|
status = MP_ENODEV;
|
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-02-19 11:02:29 -05:00
|
|
|
if (status == 0) {
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
if (!read_byte(self, data + i, i < len - 1)) {
|
|
|
|
status = MP_EIO;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop(self);
|
2017-02-19 11:02:29 -05:00
|
|
|
return status;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|