2019-07-31 14:58:55 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Scott Shawcroft
|
2019-08-15 17:45:40 -04:00
|
|
|
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
|
2019-07-31 14:58:55 -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.
|
|
|
|
*/
|
2019-09-20 14:33:37 -04:00
|
|
|
#include <stdbool.h>
|
2019-07-31 14:58:55 -04:00
|
|
|
|
|
|
|
#include "shared-bindings/busio/I2C.h"
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "stm32f4xx_hal.h"
|
|
|
|
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
#include "supervisor/shared/translate.h"
|
2019-09-18 16:49:15 -04:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2019-07-31 14:58:55 -04:00
|
|
|
|
2019-09-20 14:33:37 -04:00
|
|
|
STATIC bool reserved_i2c[3];
|
2019-12-03 16:18:49 -05:00
|
|
|
STATIC bool never_reset[3];
|
2019-09-20 14:33:37 -04:00
|
|
|
|
2019-09-19 16:02:52 -04:00
|
|
|
void i2c_reset(void) {
|
2019-09-20 14:33:37 -04:00
|
|
|
//Note: I2Cs are also forcibly reset in construct, due to silicon error
|
|
|
|
#ifdef I2C1
|
|
|
|
reserved_i2c[0] = false;
|
|
|
|
__HAL_RCC_I2C1_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#ifdef I2C2
|
|
|
|
reserved_i2c[1] = false;
|
|
|
|
__HAL_RCC_I2C2_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#ifdef I2C3
|
2019-12-03 16:18:49 -05:00
|
|
|
reserved_i2c[2] = false;
|
2019-09-20 14:33:37 -04:00
|
|
|
__HAL_RCC_I2C3_CLK_DISABLE();
|
|
|
|
#endif
|
2019-09-19 16:02:52 -04:00
|
|
|
}
|
|
|
|
|
2019-12-03 16:18:49 -05:00
|
|
|
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
|
|
|
|
for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_i2c_banks); i++) {
|
|
|
|
if (self->handle.Instance == mcu_i2c_banks[i]) {
|
|
|
|
never_reset[i] = true;
|
|
|
|
|
|
|
|
never_reset_pin_number(self->scl->pin->port, self->scl->pin->number);
|
|
|
|
never_reset_pin_number(self->sda->pin->port, self->scl->pin->number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-31 14:58:55 -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) {
|
2019-09-18 16:49:15 -04:00
|
|
|
|
2019-09-19 12:18:18 -04:00
|
|
|
//match pins to I2C objects
|
2019-09-18 16:49:15 -04:00
|
|
|
I2C_TypeDef * I2Cx;
|
2019-09-19 16:02:52 -04:00
|
|
|
|
2019-09-19 12:18:18 -04:00
|
|
|
uint8_t sda_len = sizeof(mcu_i2c_sda_list)/sizeof(*mcu_i2c_sda_list);
|
|
|
|
uint8_t scl_len = sizeof(mcu_i2c_scl_list)/sizeof(*mcu_i2c_scl_list);
|
|
|
|
for(uint i=0; i<sda_len;i++) {
|
2019-09-20 14:33:37 -04:00
|
|
|
if (mcu_i2c_sda_list[i].pin == sda) {
|
2019-09-19 12:18:18 -04:00
|
|
|
for(uint j=0; j<scl_len;j++) {
|
2019-09-20 14:33:37 -04:00
|
|
|
if ((mcu_i2c_scl_list[j].pin == scl)
|
|
|
|
&& (mcu_i2c_scl_list[j].i2c_index == mcu_i2c_sda_list[i].i2c_index)) {
|
|
|
|
self->scl = &mcu_i2c_scl_list[j];
|
|
|
|
self->sda = &mcu_i2c_sda_list[i];
|
2019-09-19 12:18:18 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 19:00:02 -04:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 12:18:18 -04:00
|
|
|
|
2019-09-19 12:45:47 -04:00
|
|
|
//handle typedef selection, errors
|
2019-09-19 16:02:52 -04:00
|
|
|
if(self->sda!=NULL && self->scl!=NULL ) {
|
2019-09-19 12:45:47 -04:00
|
|
|
I2Cx = mcu_i2c_banks[self->sda->i2c_index-1];
|
2019-09-18 13:42:54 -04:00
|
|
|
} else {
|
2019-09-12 19:00:02 -04:00
|
|
|
mp_raise_RuntimeError(translate("Invalid I2C pin selection"));
|
2019-09-19 16:32:38 -04:00
|
|
|
}
|
2019-09-19 16:02:52 -04:00
|
|
|
|
2019-09-20 14:33:37 -04:00
|
|
|
if(reserved_i2c[self->sda->i2c_index-1]) {
|
|
|
|
mp_raise_RuntimeError(translate("Hardware busy, try alternative pins"));
|
|
|
|
}
|
|
|
|
|
2019-09-19 12:18:18 -04:00
|
|
|
//Start GPIO for each pin
|
2019-09-18 16:49:15 -04:00
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
2019-09-19 12:18:18 -04:00
|
|
|
GPIO_InitStruct.Pin = pin_mask(sda->number);
|
2019-09-18 16:49:15 -04:00
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
|
|
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
2019-09-19 14:15:12 -04:00
|
|
|
GPIO_InitStruct.Alternate = self->sda->altfn_index;
|
2019-09-18 16:49:15 -04:00
|
|
|
HAL_GPIO_Init(pin_port(sda->port), &GPIO_InitStruct);
|
2019-09-19 12:18:18 -04:00
|
|
|
|
|
|
|
GPIO_InitStruct.Pin = pin_mask(scl->number);
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
|
|
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
2019-09-19 14:15:12 -04:00
|
|
|
GPIO_InitStruct.Alternate = self->scl->altfn_index;
|
2019-09-19 12:18:18 -04:00
|
|
|
HAL_GPIO_Init(pin_port(scl->port), &GPIO_InitStruct);
|
|
|
|
|
2019-09-19 16:32:38 -04:00
|
|
|
//Fix for HAL error caused by soft reboot GPIO init SDA pin voltage drop. See Eratta.
|
|
|
|
//Must be in this exact spot or I2C will get stuck in infinite loop.
|
2019-09-20 14:33:37 -04:00
|
|
|
//TODO: See git issue #2172
|
2019-09-19 16:10:26 -04:00
|
|
|
#ifdef I2C1
|
|
|
|
__HAL_RCC_I2C1_FORCE_RESET();
|
|
|
|
HAL_Delay(2);
|
|
|
|
__HAL_RCC_I2C1_RELEASE_RESET();
|
|
|
|
#endif
|
|
|
|
#ifdef I2C2
|
|
|
|
__HAL_RCC_I2C2_FORCE_RESET();
|
|
|
|
HAL_Delay(2);
|
|
|
|
__HAL_RCC_I2C2_RELEASE_RESET();
|
|
|
|
#endif
|
|
|
|
#ifdef I2C3
|
|
|
|
__HAL_RCC_I2C3_FORCE_RESET();
|
|
|
|
HAL_Delay(2);
|
|
|
|
__HAL_RCC_I2C3_RELEASE_RESET();
|
|
|
|
#endif
|
|
|
|
|
2019-09-20 14:33:37 -04:00
|
|
|
//Keep separate so above hack can be cleanly replaced
|
2019-09-19 12:18:18 -04:00
|
|
|
#ifdef I2C1
|
2019-09-20 14:33:37 -04:00
|
|
|
if(I2Cx==I2C1) {
|
|
|
|
reserved_i2c[0] = true;
|
|
|
|
__HAL_RCC_I2C1_CLK_ENABLE();
|
|
|
|
}
|
2019-09-19 12:18:18 -04:00
|
|
|
#endif
|
|
|
|
#ifdef I2C2
|
2019-09-20 14:33:37 -04:00
|
|
|
if(I2Cx==I2C2) {
|
|
|
|
reserved_i2c[1] = true;
|
|
|
|
__HAL_RCC_I2C2_CLK_ENABLE();
|
|
|
|
}
|
2019-09-19 12:18:18 -04:00
|
|
|
#endif
|
|
|
|
#ifdef I2C3
|
2019-09-20 14:33:37 -04:00
|
|
|
if(I2Cx==I2C3) {
|
|
|
|
reserved_i2c[2] = true;
|
|
|
|
__HAL_RCC_I2C3_CLK_ENABLE();
|
|
|
|
}
|
2019-09-19 12:18:18 -04:00
|
|
|
#endif
|
|
|
|
|
2019-09-19 14:15:12 -04:00
|
|
|
self->handle.Instance = I2Cx;
|
2019-09-19 12:18:18 -04:00
|
|
|
self->handle.Init.ClockSpeed = 100000;
|
|
|
|
self->handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
|
|
|
self->handle.Init.OwnAddress1 = 0;
|
|
|
|
self->handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
|
|
self->handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
|
|
|
self->handle.Init.OwnAddress2 = 0;
|
|
|
|
self->handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
|
|
|
self->handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
|
|
|
if(HAL_I2C_Init(&(self->handle)) != HAL_OK) {
|
2019-07-31 14:58:55 -04:00
|
|
|
mp_raise_RuntimeError(translate("I2C Init Error"));
|
|
|
|
}
|
|
|
|
claim_pin(sda);
|
|
|
|
claim_pin(scl);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
|
2019-09-19 12:18:18 -04:00
|
|
|
return self->sda->pin == mp_const_none;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
|
|
|
|
if (common_hal_busio_i2c_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-19 12:18:18 -04:00
|
|
|
#ifdef I2C1
|
2019-09-20 14:33:37 -04:00
|
|
|
if(self->handle.Instance==I2C1) {
|
2019-12-03 16:18:49 -05:00
|
|
|
never_reset[0] = false;
|
|
|
|
reserved_i2c[0] = false;
|
2019-09-20 14:33:37 -04:00
|
|
|
__HAL_RCC_I2C1_CLK_DISABLE();
|
|
|
|
}
|
2019-09-19 12:18:18 -04:00
|
|
|
#endif
|
|
|
|
#ifdef I2C2
|
2019-09-20 14:33:37 -04:00
|
|
|
if(self->handle.Instance==I2C2) {
|
2019-12-03 16:18:49 -05:00
|
|
|
never_reset[1] = false;
|
|
|
|
reserved_i2c[1] = false;
|
2019-09-20 14:33:37 -04:00
|
|
|
__HAL_RCC_I2C2_CLK_DISABLE();
|
|
|
|
}
|
2019-09-19 12:18:18 -04:00
|
|
|
#endif
|
|
|
|
#ifdef I2C3
|
2019-09-20 14:33:37 -04:00
|
|
|
if(self->handle.Instance==I2C3) {
|
2019-12-03 16:18:49 -05:00
|
|
|
never_reset[2] = false;
|
|
|
|
reserved_i2c[2] = false;
|
2019-09-20 14:33:37 -04:00
|
|
|
__HAL_RCC_I2C3_CLK_DISABLE();
|
|
|
|
}
|
2019-09-19 12:18:18 -04:00
|
|
|
#endif
|
2019-09-20 14:33:37 -04:00
|
|
|
reset_pin_number(self->sda->pin->port,self->sda->pin->number);
|
|
|
|
reset_pin_number(self->scl->pin->port,self->scl->pin->number);
|
2019-09-19 12:18:18 -04:00
|
|
|
self->sda = mp_const_none;
|
|
|
|
self->scl = mp_const_none;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
|
2019-09-19 12:18:18 -04:00
|
|
|
return HAL_I2C_IsDeviceReady(&(self->handle), (uint16_t)(addr<<1),2,2) == HAL_OK;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
|
|
|
|
bool grabbed_lock = false;
|
|
|
|
|
|
|
|
//Critical section code that may be required at some point.
|
|
|
|
// uint32_t store_primask = __get_PRIMASK();
|
|
|
|
// __disable_irq();
|
|
|
|
// __DMB();
|
|
|
|
|
|
|
|
if (!self->has_lock) {
|
|
|
|
grabbed_lock = true;
|
|
|
|
self->has_lock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// __DMB();
|
|
|
|
// __set_PRIMASK(store_primask);
|
|
|
|
|
|
|
|
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 transmit_stop_bit) {
|
2019-10-02 17:59:42 -04:00
|
|
|
HAL_StatusTypeDef result = HAL_I2C_Master_Transmit(&(self->handle), (uint16_t)(addr<<1), (uint8_t *)data, (uint16_t)len, 500);
|
2019-09-19 16:02:52 -04:00
|
|
|
return result == HAL_OK ? 0 : MP_EIO;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
|
|
|
|
uint8_t *data, size_t len) {
|
2019-10-02 17:59:42 -04:00
|
|
|
return HAL_I2C_Master_Receive(&(self->handle), (uint16_t)(addr<<1), data, (uint16_t)len, 500) == HAL_OK ? 0 : MP_EIO;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|