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 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.
|
|
|
|
*/
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/busio/I2C.h"
|
2017-02-19 11:02:29 -05:00
|
|
|
#include "py/mperrno.h"
|
2017-02-24 09:13:07 -05:00
|
|
|
#include "py/runtime.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-11-06 18:35:23 -05:00
|
|
|
#include "hal/include/hal_gpio.h"
|
|
|
|
#include "hal/include/hal_i2c_m_sync.h"
|
|
|
|
#include "hal/include/hpl_i2c_m_sync.h"
|
|
|
|
|
|
|
|
#include "peripherals.h"
|
|
|
|
#include "pins.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
// Number of times to try to send packet if failed.
|
2017-11-06 18:35:23 -05:00
|
|
|
#define ATTEMPTS 2
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
|
2016-11-30 18:08:34 -05:00
|
|
|
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency) {
|
2016-11-03 18:50:59 -04:00
|
|
|
Sercom* sercom = NULL;
|
2017-11-06 18:35:23 -05:00
|
|
|
uint8_t sercom_index;
|
2016-11-03 18:50:59 -04:00
|
|
|
uint32_t sda_pinmux = 0;
|
|
|
|
uint32_t scl_pinmux = 0;
|
|
|
|
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
|
|
|
|
Sercom* potential_sercom = sda->sercom[i].sercom;
|
|
|
|
if (potential_sercom == NULL ||
|
|
|
|
potential_sercom->I2CM.CTRLA.bit.ENABLE != 0 ||
|
|
|
|
sda->sercom[i].pad != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-01 16:47:18 -05:00
|
|
|
sda_pinmux = PINMUX(sda->pin, (i == 0) ? MUX_C : MUX_D);
|
2016-11-03 18:50:59 -04:00
|
|
|
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
|
|
|
|
if (potential_sercom == scl->sercom[j].sercom &&
|
|
|
|
scl->sercom[j].pad == 1) {
|
2016-12-01 16:47:18 -05:00
|
|
|
scl_pinmux = PINMUX(scl->pin, (j == 0) ? MUX_C : MUX_D);
|
2016-11-03 18:50:59 -04:00
|
|
|
sercom = potential_sercom;
|
2017-11-06 18:35:23 -05:00
|
|
|
sercom_index = scl->sercom[j].index; // 2 for SERCOM2, etc.
|
2016-11-03 18:50:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sercom != NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sercom == NULL) {
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_ValueError("Invalid pins");
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-11-06 18:35:23 -05:00
|
|
|
|
|
|
|
// Set up I2C clocks on sercom.
|
2017-11-14 20:00:29 -05:00
|
|
|
samd_peripheral_sercom_clock_init(sercom, sercom_index);
|
2017-11-06 18:35:23 -05:00
|
|
|
|
|
|
|
if (i2c_m_sync_init(&self->i2c_desc, sercom) != ERR_NONE) {
|
|
|
|
mp_raise_OSError(MP_EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_set_pin_pull_mode(sda->pin, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(sda->pin, sda_pinmux);
|
|
|
|
|
|
|
|
gpio_set_pin_pull_mode(scl->pin, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(scl->pin, scl_pinmux);
|
|
|
|
|
|
|
|
// clkrate is always 0. baud_rate is in kHz.
|
|
|
|
|
|
|
|
// Frequency must be set before the I2C device is enabled.
|
|
|
|
if (i2c_m_sync_set_baudrate(&self->i2c_desc, 0, frequency / 1000) != ERR_NONE) {
|
|
|
|
mp_raise_ValueError("Unsupported baudrate");
|
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2016-12-06 13:31:38 -05:00
|
|
|
self->sda_pin = sda->pin;
|
|
|
|
self->scl_pin = scl->pin;
|
2017-04-12 18:24:50 -04:00
|
|
|
claim_pin(sda);
|
|
|
|
claim_pin(scl);
|
2016-12-06 13:31:38 -05:00
|
|
|
|
2017-11-06 18:35:23 -05:00
|
|
|
if (i2c_m_sync_enable(&self->i2c_desc) != ERR_NONE) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_busio_i2c_deinit(self);
|
2017-11-06 18:35:23 -05:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:49:40 -04:00
|
|
|
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
|
|
|
|
return self->sda_pin == NO_PIN;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
|
2017-10-02 20:49:40 -04:00
|
|
|
if (common_hal_busio_i2c_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-06 18:35:23 -05:00
|
|
|
|
|
|
|
i2c_m_sync_disable(&self->i2c_desc);
|
|
|
|
i2c_m_sync_deinit(&self->i2c_desc);
|
|
|
|
|
2016-12-06 13:31:38 -05:00
|
|
|
reset_pin(self->sda_pin);
|
|
|
|
reset_pin(self->scl_pin);
|
2017-10-02 20:49:40 -04:00
|
|
|
self->sda_pin = NO_PIN;
|
|
|
|
self->scl_pin = NO_PIN;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
|
2017-11-06 18:35:23 -05:00
|
|
|
struct io_descriptor *i2c_io;
|
|
|
|
i2c_m_sync_get_io_descriptor(&self->i2c_desc, &i2c_io);
|
|
|
|
i2c_m_sync_set_slaveaddr(&self->i2c_desc, addr, I2C_M_SEVEN);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-11-06 18:35:23 -05:00
|
|
|
// Write no data when just probing
|
|
|
|
return io_write(i2c_io, NULL, 0) == ERR_NONE;
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
|
2017-11-06 18:35:23 -05:00
|
|
|
bool grabbed_lock = false;
|
|
|
|
CRITICAL_SECTION_ENTER()
|
|
|
|
if (!self->has_lock) {
|
|
|
|
grabbed_lock = true;
|
|
|
|
self->has_lock = true;
|
|
|
|
}
|
|
|
|
CRITICAL_SECTION_LEAVE();
|
|
|
|
return grabbed_lock;
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) {
|
2016-11-30 18:08:34 -05:00
|
|
|
return self->has_lock;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
|
2016-11-30 18:08:34 -05:00
|
|
|
self->has_lock = false;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
|
2017-11-06 18:35:23 -05:00
|
|
|
const uint8_t *data, size_t len, bool transmit_stop_bit) {
|
|
|
|
|
|
|
|
uint16_t attempts = ATTEMPTS;
|
|
|
|
int32_t status;
|
|
|
|
do {
|
|
|
|
struct _i2c_m_msg msg;
|
|
|
|
msg.addr = addr;
|
|
|
|
msg.len = len;
|
|
|
|
msg.flags = transmit_stop_bit ? I2C_M_STOP : 0;
|
|
|
|
msg.buffer = (uint8_t *) data;
|
|
|
|
status = _i2c_m_sync_transfer(&self->i2c_desc.device, &msg);
|
|
|
|
|
|
|
|
// Give up after ATTEMPTS tries.
|
|
|
|
if (--attempts == 0) {
|
2016-11-03 18:50:59 -04:00
|
|
|
break;
|
|
|
|
}
|
2017-11-06 18:35:23 -05:00
|
|
|
} while (status != I2C_OK);
|
|
|
|
if (status == I2C_OK) {
|
2017-02-19 11:02:29 -05:00
|
|
|
return 0;
|
2017-11-06 18:35:23 -05:00
|
|
|
} else if (status == I2C_ERR_BAD_ADDRESS) {
|
2017-02-19 11:02:29 -05:00
|
|
|
return MP_ENODEV;
|
|
|
|
}
|
|
|
|
return MP_EIO;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
|
2016-11-03 18:50:59 -04:00
|
|
|
uint8_t *data, size_t len) {
|
2017-11-06 18:35:23 -05:00
|
|
|
|
|
|
|
uint16_t attempts = ATTEMPTS;
|
|
|
|
int32_t status;
|
|
|
|
do {
|
|
|
|
struct _i2c_m_msg msg;
|
|
|
|
msg.addr = addr;
|
|
|
|
msg.len = len;
|
|
|
|
msg.flags = I2C_M_STOP | I2C_M_RD;
|
|
|
|
msg.buffer = data;
|
|
|
|
status = _i2c_m_sync_transfer(&self->i2c_desc.device, &msg);
|
|
|
|
|
|
|
|
// Give up after ATTEMPTS tries.
|
|
|
|
if (--attempts == 0) {
|
2016-11-03 18:50:59 -04:00
|
|
|
break;
|
|
|
|
}
|
2017-11-06 18:35:23 -05:00
|
|
|
} while (status != I2C_OK);
|
|
|
|
if (status == ERR_NONE) {
|
2017-02-19 11:02:29 -05:00
|
|
|
return 0;
|
2017-11-06 18:35:23 -05:00
|
|
|
} else if (status == I2C_ERR_BAD_ADDRESS) {
|
2017-02-19 11:02:29 -05:00
|
|
|
return MP_ENODEV;
|
|
|
|
}
|
|
|
|
return MP_EIO;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|