2017-12-21 07:49:14 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Sandeep Mistry All right reserved.
|
|
|
|
* Copyright (c) 2017 hathach
|
2018-06-28 16:45:02 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
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"
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/runtime.h"
|
2018-08-16 17:21:44 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-06-28 16:45:02 -04:00
|
|
|
#include "nrfx_twim.h"
|
|
|
|
#include "nrf_gpio.h"
|
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
#include "nrfx_spim.h"
|
|
|
|
#include "nrf_gpio.h"
|
|
|
|
|
|
|
|
STATIC twim_peripheral_t twim_peripherals[] = {
|
|
|
|
#if NRFX_CHECK(NRFX_TWIM0_ENABLED)
|
|
|
|
// SPIM0 and TWIM0 share an address.
|
|
|
|
{ .twim = NRFX_TWIM_INSTANCE(0),
|
|
|
|
.in_use = false,
|
|
|
|
.max_xfer_size = TWIM0_EASYDMA_MAXCNT_SIZE,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if NRFX_CHECK(NRFX_TWIM1_ENABLED)
|
|
|
|
// SPIM1 and TWIM1 share an address.
|
|
|
|
{ .twim = NRFX_TWIM_INSTANCE(1),
|
|
|
|
.in_use = false,
|
|
|
|
.max_xfer_size = TWIM1_EASYDMA_MAXCNT_SIZE,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2018-09-25 15:12:10 -04:00
|
|
|
void i2c_reset(void) {
|
|
|
|
for (size_t i = 0 ; i < MP_ARRAY_SIZE(twim_peripherals); i++) {
|
|
|
|
twim_peripherals[i].in_use = false;
|
|
|
|
}
|
|
|
|
}
|
2018-06-28 16:45:02 -04:00
|
|
|
|
|
|
|
static uint8_t twi_error_to_mp(const nrfx_err_t err) {
|
|
|
|
switch (err) {
|
|
|
|
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:
|
|
|
|
return MP_EIO;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
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) {
|
2018-08-16 17:21:44 -04:00
|
|
|
mp_raise_ValueError(translate("Invalid pins"));
|
2018-09-20 20:45:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find a free instance.
|
|
|
|
self->twim_peripheral = NULL;
|
|
|
|
for (size_t i = 0 ; i < MP_ARRAY_SIZE(twim_peripherals); i++) {
|
|
|
|
if (!twim_peripherals[i].in_use) {
|
|
|
|
self->twim_peripheral = &twim_peripherals[i];
|
|
|
|
self->twim_peripheral->in_use = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
if (self->twim_peripheral == NULL) {
|
|
|
|
mp_raise_ValueError(translate("All I2C peripherals are in use"));
|
|
|
|
}
|
2018-06-28 16:45:02 -04:00
|
|
|
|
|
|
|
nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG;
|
2018-08-30 21:42:25 -04:00
|
|
|
config.scl = scl->number;
|
|
|
|
config.sda = sda->number;
|
2018-06-28 16:45:02 -04:00
|
|
|
|
|
|
|
// change freq. only if it's less than the default 400K
|
|
|
|
if (frequency < 100000) {
|
|
|
|
config.frequency = NRF_TWIM_FREQ_100K;
|
|
|
|
} else if (frequency < 250000) {
|
|
|
|
config.frequency = NRF_TWIM_FREQ_250K;
|
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);
|
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_err_t err = nrfx_twim_init(&self->twim_peripheral->twim, &config, NULL, NULL);
|
2018-06-28 16:45:02 -04:00
|
|
|
|
|
|
|
// A soft reset doesn't uninit the driver so we might end up with a invalid state
|
|
|
|
if (err == NRFX_ERROR_INVALID_STATE) {
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_twim_uninit(&self->twim_peripheral->twim);
|
|
|
|
err = nrfx_twim_init(&self->twim_peripheral->twim, &config, NULL, NULL);
|
2018-06-28 16:45:02 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
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) {
|
2018-06-28 16:45:02 -04:00
|
|
|
if (common_hal_busio_i2c_deinited(self))
|
2017-12-21 07:49:14 -05:00
|
|
|
return;
|
|
|
|
|
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;
|
|
|
|
|
2018-09-20 20:45:30 -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 &&
|
|
|
|
nrf_twim_event_check(reg, NRF_TWIM_EVENT_ERROR) == 0);
|
|
|
|
nrf_twim_event_clear(reg, NRF_TWIM_EVENT_TXSTARTED);
|
|
|
|
|
|
|
|
nrf_twim_task_trigger(reg, NRF_TWIM_TASK_STOP);
|
|
|
|
while (nrf_twim_event_check(reg, NRF_TWIM_EVENT_STOPPED) == 0);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const uint8_t *data, size_t len, bool stopBit) {
|
2018-06-28 16:45:02 -04:00
|
|
|
if(len == 0)
|
|
|
|
return common_hal_busio_i2c_probe(self, addr) ? 0 : MP_ENODEV;
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
const uint32_t max_xfer_size = self->twim_peripheral->max_xfer_size;
|
|
|
|
const uint32_t parts = len / max_xfer_size;
|
|
|
|
const uint32_t remainder = len % max_xfer_size;
|
2018-07-04 14:33:01 -04:00
|
|
|
nrfx_err_t err = NRFX_SUCCESS;
|
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_twim_enable(&self->twim_peripheral->twim);
|
2018-07-04 14:33:01 -04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < parts; ++i) {
|
2018-09-20 20:45:30 -04:00
|
|
|
err = nrfx_twim_tx(&self->twim_peripheral->twim, addr, data + i * max_xfer_size, max_xfer_size, !stopBit);
|
2018-07-04 14:33:01 -04:00
|
|
|
if (err != NRFX_SUCCESS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((remainder > 0) && (err == NRFX_SUCCESS))
|
2018-09-20 20:45:30 -04:00
|
|
|
err = nrfx_twim_tx(&self->twim_peripheral->twim, addr, data + parts * max_xfer_size, remainder, !stopBit);
|
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
|
|
|
|
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) {
|
|
|
|
if(len == 0)
|
|
|
|
return 0;
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
const uint32_t max_xfer_size = self->twim_peripheral->max_xfer_size;
|
|
|
|
const uint32_t parts = len / max_xfer_size;
|
|
|
|
const uint32_t remainder = len % max_xfer_size;
|
2018-07-04 14:33:01 -04:00
|
|
|
nrfx_err_t err = NRFX_SUCCESS;
|
|
|
|
|
2018-09-20 20:45:30 -04:00
|
|
|
nrfx_twim_enable(&self->twim_peripheral->twim);
|
2018-07-04 14:33:01 -04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < parts; ++i) {
|
2018-09-20 20:45:30 -04:00
|
|
|
err = nrfx_twim_rx(&self->twim_peripheral->twim, addr, data + i * max_xfer_size, max_xfer_size);
|
2018-07-04 14:33:01 -04:00
|
|
|
if (err != NRFX_SUCCESS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((remainder > 0) && (err == NRFX_SUCCESS))
|
2018-09-20 20:45:30 -04:00
|
|
|
err = nrfx_twim_rx(&self->twim_peripheral->twim, addr, data + parts * max_xfer_size, remainder);
|
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
|
|
|
}
|