First pass at I2C pin authentication

This commit is contained in:
Hierophect 2019-09-12 19:00:02 -04:00
parent 6b6178613d
commit f5cb2cc14e
7 changed files with 227 additions and 55 deletions

View File

@ -190,6 +190,7 @@ SRC_C += \
peripherals/stm32f4/$(MCU_SUB_VARIANT)/pins.c \
peripherals/stm32f4/$(MCU_SUB_VARIANT)/clocks.c \
peripherals/stm32f4/$(MCU_SUB_VARIANT)/gpio.c \
peripherals/stm32f4/$(MCU_SUB_VARIANT)/periph.c \
lib/libc/string0.c \
lib/mp-readline/readline.c \
lib/oofatfs/ff.c \

View File

@ -32,23 +32,62 @@
#include "shared-bindings/microcontroller/__init__.h"
#include "supervisor/shared/translate.h"
#include "stm32f4/periph.h"
I2C_HandleTypeDef hi2c2;
I2C_HandleTypeDef hi2c;
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) {
//TODO: Rework this entire section to use LL so we can properly assign pins
// This will also be bundled with MSP removal
hi2c2.Instance = I2C1;
hi2c2.Init.ClockSpeed = 100000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if(HAL_I2C_Init(&hi2c2) != HAL_OK) {
// This will also be bundled with MSP removal
I2C_TypeDef * I2Cx = NULL;
I2C_TypeDef * I2Cx_check = NULL;
for(uint i=0; i<(sizeof(mcu_i2c_list)/sizeof(*mcu_i2c_list));i++) {
if (mcu_i2c_list[i]->sda_pin == sda) {
switch (mcu_i2c_list[i]->i2c_index) {
case 1: I2Cx = I2C1;
break;
#ifdef I2C2
case 2: I2Cx = I2C2;
break;
#endif
#ifdef I2C3
case 3: I2Cx = I2C3;
break;
#endif
}
} else if (mcu_i2c_list[i]->scl_pin == scl) {
switch (mcu_i2c_list[i]->i2c_index) {
case 1: I2Cx_check = I2C1;
break;
#ifdef I2C2
case 2: I2Cx_check = I2C2;
break;
#endif
#ifdef I2C3
case 3: I2Cx_check = I2C3;
break;
#endif
}
}
}
if (I2Cx!=I2Cx_check || I2Cx==NULL) {
mp_raise_RuntimeError(translate("Invalid I2C pin selection"));
}
hi2c.Instance = I2Cx;
hi2c.Init.ClockSpeed = 100000;
hi2c.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c.Init.OwnAddress1 = 0;
hi2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c.Init.OwnAddress2 = 0;
hi2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if(HAL_I2C_Init(&hi2c) != HAL_OK) {
mp_raise_RuntimeError(translate("I2C Init Error"));
} else {
mp_printf(&mp_plat_print, "I2C INIT OK");
@ -70,14 +109,14 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
return;
}
HAL_I2C_MspDeInit(&hi2c2);
HAL_I2C_MspDeInit(&hi2c);
self->sda_pin = NO_PIN;
self->scl_pin = NO_PIN;
}
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
return HAL_I2C_IsDeviceReady(&hi2c2, (uint16_t)(addr<<1),2,2) == HAL_OK;
return HAL_I2C_IsDeviceReady(&hi2c, (uint16_t)(addr<<1),2,2) == HAL_OK;
}
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
@ -109,10 +148,10 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
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) {
return HAL_I2C_Master_Transmit(&hi2c2, (uint16_t)(addr<<1), (uint8_t *)data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO;
return HAL_I2C_Master_Transmit(&hi2c, (uint16_t)(addr<<1), (uint8_t *)data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO;
}
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
uint8_t *data, size_t len) {
return HAL_I2C_Master_Receive(&hi2c2, (uint16_t)(addr<<1), data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO;
return HAL_I2C_Master_Receive(&hi2c, (uint16_t)(addr<<1), data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO;
}

View File

@ -24,9 +24,6 @@
* THE SOFTWARE.
*/
// DO NOT include this file directly. Use shared-bindings/microcontroller/Pin.h instead to ensure
// that all necessary includes are already included.
#ifndef __MICROPY_INCLUDED_STM32F4_PERIPHERALS_PERIPH_H__
#define __MICROPY_INCLUDED_STM32F4_PERIPHERALS_PERIPH_H__
@ -36,24 +33,25 @@
#include "stm32f4xx_hal.h"
#include "stm32f4/pins.h"
#define GPIO_A 0
#define GPIO_B 1
#define GPIO_C 2
#define GPIO_D 3
#define GPIO_E 4
#define GPIO_F 5
#define GPIO_G 6
#define GPIO_H 7
#define GPIO_I 8
#define GPIO_J 9
#define GPIO_K 10
#define PA 0
#define PB 1
#define PC 2
#define PD 3
#define PE 4
#define PF 5
#define PG 6
#define PH 7
#define PI 8
#define PJ 9
#define PK 10
#define NO_ALT 0x0F
extern const mp_obj_type_t prefix_fields;
// I2C
// Numerical Version
/*
typedef struct {
mp_obj_base_t base;
uint8_t i2c_index:3; // Index of the I2C unit
@ -64,32 +62,41 @@ typedef struct {
uint8_t scl_pin_number:4;
} mcu_i2c_periph_obj_t;
#define I2C(i2c_index, alt_index, sda_port, sda_number, scl_port, scl_number) \
#define SDA(port, number) \
.sda_pin_port = port, \
.sda_pin_number = number,
#define SCL(port, number) \
.scl_pin_port = port, \
.scl_pin_number = number
#define I2C(index, alt, sda, scl) \
{ \
{ &prefix_fields }, \
.i2c_index = i2c_index, \
.sda_pin_port = sda_port, \
.sda_pin_number = sda_number, \
.scl_pin_port = scl_port, \
.scl_pin_number = scl_number \
.i2c_index = index, \
.alt_index = alt, \
sda \
scl \
}
*/
// Address Version
typedef struct {
uint8_t i2c_index:4; // Index of the I2C unit
uint8_t alt_index:4; // Alt index is arbitrary, it just lists additional pin options
const mcu_pin_obj_t * sda_pin;
const mcu_pin_obj_t * scl_pin;
} mcu_i2c_periph_obj_t;
#define I2C(index, alt, sda, scl) \
{ \
.i2c_index = index, \
.alt_index = alt, \
.sda_pin = sda, \
.scl_pin = scl, \
}
//ALT
// typedef struct {
// mp_obj_base_t base;
// uint8_t i2c_index:4; // Index of the I2C unit
// uint8_t alt_index:4; // Alt index is arbitrary, it just lists additional pin options
// mcu_pin_obj_t * sda;
// mcu_pin_obj_t * scl;
// } mcu_i2c_periph_obj_t;
// #define I2C(i2c_index, alt_index, sda, scl) \
// { \
// { &prefix_fields }, \
// .i2c_index = i2c_index, \
// .sda = sda, \
// .scl = scl, \
// }
// TODO: SPI, UART, etc

View File

@ -0,0 +1,42 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* 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 "py/obj.h"
#include "py/mphal.h"
#include "stm32f4/pins.h"
#include "stm32f4/periph.h"
//const mcu_i2c_periph_obj_t periph_I2C1_0 = I2C(1, 0, SDA(PB,7), SCL(PB,6));
//const mcu_i2c_periph_obj_t periph_I2C1_1 = I2C(1, 1, SDA(PB,9), SCL(PB,8));
//const mcu_i2c_periph_obj_t periph_I2C2 = I2C(1, NO_ALT, SDA(PB,11), SCL(PB,10));
//const mcu_i2c_periph_obj_t periph_I2C3 = I2C(1, NO_ALT, SDA(PB,9), SCL(PB,8));
const mcu_i2c_periph_obj_t periph_I2C1_0 = I2C(1, 0, &pin_PB07, &pin_PB06);
const mcu_i2c_periph_obj_t periph_I2C1_1 = I2C(1, 1, &pin_PB09, &pin_PB08);
//const mcu_i2c_periph_obj_t periph_I2C2 = I2C(1, NO_ALT, &pin_PB11, &pin_PB10); //not on LQFP100
const mcu_i2c_periph_obj_t periph_I2C3 = I2C(1, NO_ALT, &pin_PB09, &pin_PB08);
const mcu_i2c_periph_obj_t* mcu_i2c_list[3] = {&periph_I2C1_0,&periph_I2C1_1,&periph_I2C3};

View File

@ -27,6 +27,10 @@
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
extern const mcu_i2c_periph_obj_t periph_I2C1;
extern const mcu_i2c_periph_obj_t periph_I2C1_0;
extern const mcu_i2c_periph_obj_t periph_I2C1_1;
extern const mcu_i2c_periph_obj_t periph_I2C3;
extern const mcu_i2c_periph_obj_t* mcu_i2c_list[3];
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H

View File

@ -0,0 +1,42 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* 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 "py/obj.h"
#include "py/mphal.h"
#include "stm32f4/pins.h"
#include "stm32f4/periph.h"
//const mcu_i2c_periph_obj_t periph_I2C1_0 = I2C(1, 0, SDA(PB,7), SCL(PB,6));
//const mcu_i2c_periph_obj_t periph_I2C1_1 = I2C(1, 1, SDA(PB,9), SCL(PB,8));
//const mcu_i2c_periph_obj_t periph_I2C2 = I2C(1, NO_ALT, SDA(PB,11), SCL(PB,10));
//const mcu_i2c_periph_obj_t periph_I2C3 = I2C(1, NO_ALT, SDA(PB,9), SCL(PB,8));
const mcu_i2c_periph_obj_t periph_I2C1_0 = I2C(1, 0, &pin_PB07, &pin_PB06);
const mcu_i2c_periph_obj_t periph_I2C1_1 = I2C(1, 1, &pin_PB09, &pin_PB08);
const mcu_i2c_periph_obj_t periph_I2C2 = I2C(1, NO_ALT, &pin_PB11, &pin_PB10); //not on LQFP100
const mcu_i2c_periph_obj_t periph_I2C3 = I2C(1, NO_ALT, &pin_PB09, &pin_PB08);
const mcu_i2c_periph_obj_t* mcu_i2c_list[4] = {&periph_I2C1_0,&periph_I2C1_1,&periph_I2C3};

View File

@ -0,0 +1,37 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* 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.
*/
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
extern const mcu_i2c_periph_obj_t periph_I2C1_0;
extern const mcu_i2c_periph_obj_t periph_I2C1_1;
extern const mcu_i2c_periph_obj_t periph_I2C2;
extern const mcu_i2c_periph_obj_t periph_I2C3;
extern const mcu_i2c_periph_obj_t* mcu_i2c_list[4];
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H