2017-12-21 07:49:14 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
|
2018-06-28 16:45:02 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2017 hathach
|
|
|
|
* Copyright (c) 2016 Sandeep Mistry All right reserved.
|
2017-12-21 07:49:14 -05: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared-bindings/busio/I2C.h"
|
2019-11-02 22:47:03 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2022-05-13 15:33:43 -04:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2018-06-28 16:45:02 -04:00
|
|
|
#include "nrfx_twim.h"
|
2018-09-20 20:45:30 -04:00
|
|
|
#include "nrfx_spim.h"
|
|
|
|
#include "nrf_gpio.h"
|
|
|
|
|
2018-12-28 04:30:11 -05:00
|
|
|
// all TWI instances have the same max size
|
|
|
|
// 16 bits for 840, 10 bits for 810, 8 bits for 832
|
2018-12-28 09:14:27 -05:00
|
|
|
#define I2C_MAX_XFER_LEN ((1UL << TWIM0_EASYDMA_MAXCNT_SIZE) - 1)
|
2018-12-28 04:30:11 -05:00
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
STATIC twim_peripheral_t twim_peripherals[] = {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if NRFX_CHECK(NRFX_TWIM0_ENABLED)
|
2018-09-20 20:45:30 -04:00
|
|
|
// SPIM0 and TWIM0 share an address.
|
|
|
|
{ .twim = NRFX_TWIM_INSTANCE(0),
|
2021-03-15 09:57:36 -04:00
|
|
|
.in_use = false},
|
|
|
|
#endif
|
|
|
|
#if NRFX_CHECK(NRFX_TWIM1_ENABLED)
|
2018-09-20 20:45:30 -04:00
|
|
|
// SPIM1 and TWIM1 share an address.
|
|
|
|
{ .twim = NRFX_TWIM_INSTANCE(1),
|
2021-03-15 09:57:36 -04:00
|
|
|
.in_use = false},
|
|
|
|
#endif
|
2018-09-20 20:45:30 -04:00
|
|
|
};
|
|
|
|
|
2019-07-05 22:01:54 -04:00
|
|
|
STATIC bool never_reset[MP_ARRAY_SIZE(twim_peripherals)];
|
|
|
|
|
2018-09-25 15:12:10 -04:00
|
|
|
void i2c_reset(void) {
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(twim_peripherals); i++) {
|
2019-07-05 22:01:54 -04:00
|
|
|
if (never_reset[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-03 18:14:37 -05:00
|
|
|
nrfx_twim_uninit(&twim_peripherals[i].twim);
|
2018-09-25 15:12:10 -04:00
|
|
|
twim_peripherals[i].in_use = false;
|
|
|
|
}
|
|
|
|
}
|
2018-06-28 16:45:02 -04:00
|
|
|
|
2019-07-05 22:01:54 -04:00
|
|
|
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(twim_peripherals); i++) {
|
2019-07-05 22:01:54 -04:00
|
|
|
if (self->twim_peripheral == &twim_peripherals[i]) {
|
|
|
|
never_reset[i] = true;
|
|
|
|
|
|
|
|
never_reset_pin_number(self->scl_pin_number);
|
|
|
|
never_reset_pin_number(self->sda_pin_number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 16:45:02 -04:00
|
|
|
static uint8_t twi_error_to_mp(const nrfx_err_t err) {
|
|
|
|
switch (err) {
|
2021-03-15 09:57:36 -04:00
|
|
|
case NRFX_ERROR_DRV_TWI_ERR_ANACK:
|
|
|
|
return MP_ENODEV;
|
|
|
|
case NRFX_ERROR_BUSY:
|
|
|
|
return MP_EBUSY;
|
|
|
|
case NRFX_ERROR_DRV_TWI_ERR_DNACK:
|
|
|
|
case NRFX_ERROR_INVALID_ADDR:
|
2023-06-19 17:20:46 -04:00
|
|
|
return MP_EIO;
|
2023-06-23 13:20:41 -04:00
|
|
|
default:
|
|
|
|
break;
|
2018-06-28 16:45:02 -04:00
|
|
|
}
|
2023-06-23 13:20:41 -04:00
|
|
|
|
|
|
|
return 0;
|
2018-06-28 16:45:02 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-06-28 16:45:02 -04:00
|
|
|
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) {
|
2018-09-20 20:45:30 -04:00
|
|
|
if (scl->number == sda->number) {
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_ValueError_invalid_pins();
|
2018-09-20 20:45:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find a free instance.
|
|
|
|
self->twim_peripheral = NULL;
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(twim_peripherals); i++) {
|
2018-09-20 20:45:30 -04:00
|
|
|
if (!twim_peripherals[i].in_use) {
|
|
|
|
self->twim_peripheral = &twim_peripherals[i];
|
2019-11-02 22:47:03 -04:00
|
|
|
// Mark it as in_use later after other validation is finished.
|
2018-09-20 20:45:30 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
if (self->twim_peripheral == NULL) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("All I2C peripherals are in use"));
|
2018-09-20 20:45:30 -04:00
|
|
|
}
|
2018-06-28 16:45:02 -04:00
|
|
|
|
2023-06-23 13:20:41 -04:00
|
|
|
#if CIRCUITPY_REQUIRE_I2C_PULLUPS
|
|
|
|
// Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
|
|
|
|
nrf_gpio_cfg_input(scl->number, NRF_GPIO_PIN_PULLDOWN);
|
|
|
|
nrf_gpio_cfg_input(sda->number, NRF_GPIO_PIN_PULLDOWN);
|
|
|
|
|
|
|
|
common_hal_mcu_delay_us(10);
|
|
|
|
|
|
|
|
nrf_gpio_cfg_input(scl->number, NRF_GPIO_PIN_NOPULL);
|
|
|
|
nrf_gpio_cfg_input(sda->number, NRF_GPIO_PIN_NOPULL);
|
|
|
|
|
|
|
|
// We must pull up within 3us to achieve 400khz.
|
|
|
|
common_hal_mcu_delay_us(3);
|
|
|
|
|
|
|
|
if (!nrf_gpio_pin_read(sda->number) || !nrf_gpio_pin_read(scl->number)) {
|
2019-11-02 22:47:03 -04:00
|
|
|
reset_pin_number(sda->number);
|
|
|
|
reset_pin_number(scl->number);
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_RuntimeError(MP_ERROR_TEXT("No pull up found on SDA or SCL; check your wiring"));
|
2019-11-02 22:47:03 -04:00
|
|
|
}
|
2023-06-23 13:20:41 -04:00
|
|
|
#endif
|
2019-11-02 22:47:03 -04:00
|
|
|
|
2019-12-17 09:39:52 -05:00
|
|
|
nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG(scl->number, sda->number);
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if defined(TWIM_FREQUENCY_FREQUENCY_K1000)
|
2020-02-05 11:25:31 -05:00
|
|
|
if (frequency >= 1000000) {
|
|
|
|
config.frequency = NRF_TWIM_FREQ_1000K;
|
|
|
|
} else
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2020-02-05 11:25:31 -05:00
|
|
|
if (frequency >= 400000) {
|
2021-03-15 09:57:36 -04:00
|
|
|
config.frequency = NRF_TWIM_FREQ_400K;
|
2020-02-05 11:25:31 -05:00
|
|
|
} else if (frequency >= 250000) {
|
2021-03-15 09:57:36 -04:00
|
|
|
config.frequency = NRF_TWIM_FREQ_250K;
|
2020-02-05 11:25:31 -05:00
|
|
|
} else {
|
|
|
|
config.frequency = NRF_TWIM_FREQ_100K;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
self->scl_pin_number = scl->number;
|
|
|
|
self->sda_pin_number = sda->number;
|
|
|
|
claim_pin(sda);
|
|
|
|
claim_pin(scl);
|
|
|
|
|
2019-11-02 22:47:03 -04:00
|
|
|
// About to init. If we fail after this point, common_hal_busio_i2c_deinit() will set in_use to false.
|
|
|
|
self->twim_peripheral->in_use = true;
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_err_t err = nrfx_twim_init(&self->twim_peripheral->twim, &config, NULL, NULL);
|
2018-08-31 17:46:03 -04:00
|
|
|
if (err != NRFX_SUCCESS) {
|
|
|
|
common_hal_busio_i2c_deinit(self);
|
2018-06-28 16:45:02 -04:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2018-08-31 17:46:03 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
|
2018-08-31 17:46:03 -04:00
|
|
|
return self->sda_pin_number == NO_PIN;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
|
2019-11-02 22:47:03 -04:00
|
|
|
if (common_hal_busio_i2c_deinited(self)) {
|
2017-12-21 07:49:14 -05:00
|
|
|
return;
|
2019-11-02 22:47:03 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_twim_uninit(&self->twim_peripheral->twim);
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
reset_pin_number(self->sda_pin_number);
|
|
|
|
reset_pin_number(self->scl_pin_number);
|
|
|
|
self->sda_pin_number = NO_PIN;
|
|
|
|
self->scl_pin_number = NO_PIN;
|
2018-09-20 20:45:30 -04:00
|
|
|
|
|
|
|
self->twim_peripheral->in_use = false;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 16:45:02 -04:00
|
|
|
// nrfx_twim_tx doesn't support 0-length data so we fall back to the hal API
|
2017-12-21 07:49:14 -05:00
|
|
|
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
|
2018-09-20 20:45:30 -04:00
|
|
|
NRF_TWIM_Type *reg = self->twim_peripheral->twim.p_twim;
|
2018-06-28 16:45:02 -04:00
|
|
|
bool found = true;
|
|
|
|
|
2023-06-23 13:20:41 -04:00
|
|
|
nrfx_twim_enable(&self->twim_peripheral->twim);
|
2018-06-28 16:45:02 -04:00
|
|
|
|
|
|
|
nrf_twim_address_set(reg, addr);
|
|
|
|
nrf_twim_tx_buffer_set(reg, NULL, 0);
|
|
|
|
|
|
|
|
nrf_twim_task_trigger(reg, NRF_TWIM_TASK_RESUME);
|
|
|
|
|
|
|
|
nrf_twim_task_trigger(reg, NRF_TWIM_TASK_STARTTX);
|
|
|
|
while (nrf_twim_event_check(reg, NRF_TWIM_EVENT_TXSTARTED) == 0 &&
|
2021-03-15 09:57:36 -04:00
|
|
|
nrf_twim_event_check(reg, NRF_TWIM_EVENT_ERROR) == 0) {
|
|
|
|
;
|
|
|
|
}
|
2018-06-28 16:45:02 -04:00
|
|
|
nrf_twim_event_clear(reg, NRF_TWIM_EVENT_TXSTARTED);
|
|
|
|
|
|
|
|
nrf_twim_task_trigger(reg, NRF_TWIM_TASK_STOP);
|
2021-03-15 09:57:36 -04:00
|
|
|
while (nrf_twim_event_check(reg, NRF_TWIM_EVENT_STOPPED) == 0) {
|
|
|
|
;
|
|
|
|
}
|
2018-06-28 16:45:02 -04:00
|
|
|
nrf_twim_event_clear(reg, NRF_TWIM_EVENT_STOPPED);
|
|
|
|
|
|
|
|
if (nrf_twim_event_check(reg, NRF_TWIM_EVENT_ERROR)) {
|
|
|
|
nrf_twim_event_clear(reg, NRF_TWIM_EVENT_ERROR);
|
|
|
|
|
|
|
|
nrf_twim_errorsrc_get_and_clear(reg);
|
|
|
|
found = false;
|
|
|
|
}
|
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_twim_disable(&self->twim_peripheral->twim);
|
2018-06-28 16:45:02 -04:00
|
|
|
|
|
|
|
return found;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
|
|
|
|
bool grabbed_lock = false;
|
2018-09-20 20:45:30 -04:00
|
|
|
// NRFX_CRITICAL_SECTION_ENTER();
|
|
|
|
if (!self->has_lock) {
|
|
|
|
grabbed_lock = true;
|
|
|
|
self->has_lock = true;
|
|
|
|
}
|
|
|
|
// NRFX_CRITICAL_SECTION_EXIT();
|
2017-12-21 07:49:14 -05:00
|
|
|
return grabbed_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) {
|
|
|
|
return self->has_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
|
|
|
|
self->has_lock = false;
|
|
|
|
}
|
|
|
|
|
2022-01-31 22:03:30 -05:00
|
|
|
STATIC uint8_t _common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const uint8_t *data, size_t len, bool stopBit) {
|
2021-03-15 09:57:36 -04:00
|
|
|
if (len == 0) {
|
2018-06-28 16:45:02 -04:00
|
|
|
return common_hal_busio_i2c_probe(self, addr) ? 0 : MP_ENODEV;
|
2018-12-28 04:30:11 -05:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-07-04 14:33:01 -04:00
|
|
|
nrfx_err_t err = NRFX_SUCCESS;
|
|
|
|
|
2023-06-23 13:20:41 -04:00
|
|
|
nrfx_twim_enable(&self->twim_peripheral->twim);
|
2018-07-04 14:33:01 -04:00
|
|
|
|
2018-12-28 04:30:11 -05:00
|
|
|
// break into MAX_XFER_LEN transaction
|
2021-03-15 09:57:36 -04:00
|
|
|
while (len) {
|
2018-12-28 04:30:11 -05:00
|
|
|
const size_t xact_len = MIN(len, I2C_MAX_XFER_LEN);
|
2021-03-15 09:57:36 -04:00
|
|
|
nrfx_twim_xfer_desc_t xfer_desc = NRFX_TWIM_XFER_DESC_TX(addr, (uint8_t *)data, xact_len);
|
2019-12-17 09:39:52 -05:00
|
|
|
uint32_t const flags = (stopBit ? 0 : NRFX_TWIM_FLAG_TX_NO_STOP);
|
2018-12-28 04:30:11 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if (NRFX_SUCCESS != (err = nrfx_twim_xfer(&self->twim_peripheral->twim, &xfer_desc, flags))) {
|
2018-07-04 14:33:01 -04:00
|
|
|
break;
|
2018-12-28 04:30:11 -05:00
|
|
|
}
|
2018-07-04 14:33:01 -04:00
|
|
|
|
2018-12-28 04:30:11 -05:00
|
|
|
len -= xact_len;
|
|
|
|
data += xact_len;
|
|
|
|
}
|
2018-07-04 14:33:01 -04:00
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_twim_disable(&self->twim_peripheral->twim);
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-06-28 16:45:02 -04:00
|
|
|
return twi_error_to_mp(err);
|
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2022-01-31 22:03:30 -05:00
|
|
|
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const uint8_t *data, size_t len) {
|
|
|
|
return _common_hal_busio_i2c_write(self, addr, data, len, true);
|
|
|
|
}
|
|
|
|
|
2018-06-28 16:45:02 -04:00
|
|
|
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t *data, size_t len) {
|
2021-03-15 09:57:36 -04:00
|
|
|
if (len == 0) {
|
2018-06-28 16:45:02 -04:00
|
|
|
return 0;
|
2018-12-28 04:30:11 -05:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-07-04 14:33:01 -04:00
|
|
|
nrfx_err_t err = NRFX_SUCCESS;
|
|
|
|
|
2023-06-23 13:20:41 -04:00
|
|
|
nrfx_twim_enable(&self->twim_peripheral->twim);
|
2018-07-04 14:33:01 -04:00
|
|
|
|
2018-12-28 04:30:11 -05:00
|
|
|
// break into MAX_XFER_LEN transaction
|
2021-03-15 09:57:36 -04:00
|
|
|
while (len) {
|
2018-12-28 04:30:11 -05:00
|
|
|
const size_t xact_len = MIN(len, I2C_MAX_XFER_LEN);
|
2019-12-17 09:39:52 -05:00
|
|
|
nrfx_twim_xfer_desc_t xfer_desc = NRFX_TWIM_XFER_DESC_RX(addr, data, xact_len);
|
2018-12-28 04:30:11 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if (NRFX_SUCCESS != (err = nrfx_twim_xfer(&self->twim_peripheral->twim, &xfer_desc, 0))) {
|
2018-07-04 14:33:01 -04:00
|
|
|
break;
|
2018-12-28 04:30:11 -05:00
|
|
|
}
|
2018-07-04 14:33:01 -04:00
|
|
|
|
2018-12-28 04:30:11 -05:00
|
|
|
len -= xact_len;
|
|
|
|
data += xact_len;
|
|
|
|
}
|
2018-07-04 14:33:01 -04:00
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_twim_disable(&self->twim_peripheral->twim);
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-06-28 16:45:02 -04:00
|
|
|
return twi_error_to_mp(err);
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
2022-01-31 22:03:30 -05:00
|
|
|
|
|
|
|
uint8_t common_hal_busio_i2c_write_read(busio_i2c_obj_t *self, uint16_t addr,
|
|
|
|
uint8_t *out_data, size_t out_len, uint8_t *in_data, size_t in_len) {
|
|
|
|
uint8_t result = _common_hal_busio_i2c_write(self, addr, out_data, out_len, false);
|
|
|
|
if (result != 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return common_hal_busio_i2c_read(self, addr, in_data, in_len);
|
|
|
|
}
|