From f09ef538552bfe4acc5f76ba0f8f6ab1712b7c88 Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Mon, 20 Jan 2025 16:09:17 +0000 Subject: [PATCH] I2C partial implementation - I2C partial implementation - TIMINGR value needs to be manually calculated and set via I2C_TIMINGS_REG option --- .../hal/boards/stm32/boardmap_generic_h750.h | 26 ++++++------- uCNC/src/hal/mcus/stm32h7x/mcu_stm32h7x.c | 16 ++++---- uCNC/src/hal/mcus/stm32h7x/mcumap_stm32h7x.h | 38 +++++++++---------- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/uCNC/src/hal/boards/stm32/boardmap_generic_h750.h b/uCNC/src/hal/boards/stm32/boardmap_generic_h750.h index 26856f520..f5ec476ee 100644 --- a/uCNC/src/hal/boards/stm32/boardmap_generic_h750.h +++ b/uCNC/src/hal/boards/stm32/boardmap_generic_h750.h @@ -71,14 +71,14 @@ extern "C" // #define LIMIT_A_ISR // Setup control input pins -#define ESTOP_BIT 5 -#define ESTOP_PORT B -#define FHOLD_BIT 6 -#define FHOLD_PORT B -#define CS_RES_BIT 7 -#define CS_RES_PORT B -#define SAFETY_DOOR_BIT 8 -#define SAFETY_DOOR_PORT B +// #define ESTOP_BIT 5 +// #define ESTOP_PORT B +// #define FHOLD_BIT 6 +// #define FHOLD_PORT B +// #define CS_RES_BIT 7 +// #define CS_RES_PORT B +// #define SAFETY_DOOR_BIT 8 +// #define SAFETY_DOOR_PORT B // Setup probe pin // #define PROBE_BIT 9 // #define PROBE_PORT B @@ -146,11 +146,11 @@ extern "C" // On STM32F1x cores this will default to Timer 3 // #define SERVO_TIMER 3 - // #define I2C_CLK_BIT 6 - // #define I2C_CLK_PORT B - // #define I2C_DATA_BIT 7 - // #define I2C_DATA_PORT B - // #define I2C_PORT 1 + #define I2C_CLK_BIT 6 + #define I2C_CLK_PORT B + #define I2C_DATA_BIT 7 + #define I2C_DATA_PORT B + #define I2C_PORT 1 // #define DIN7_BIT 5 // #define DIN7_PORT A // #define DIN7_ISR diff --git a/uCNC/src/hal/mcus/stm32h7x/mcu_stm32h7x.c b/uCNC/src/hal/mcus/stm32h7x/mcu_stm32h7x.c index 8524ca772..dc7fa9e44 100644 --- a/uCNC/src/hal/mcus/stm32h7x/mcu_stm32h7x.c +++ b/uCNC/src/hal/mcus/stm32h7x/mcu_stm32h7x.c @@ -23,6 +23,7 @@ #include "core_cm7.h" #include "stm32h7xx.h" #include "mcumap_stm32h7x.h" +#include "stm32h7xx_hal.h" #include #ifdef MCU_HAS_USB @@ -536,7 +537,8 @@ void mcu_usart_init(void) // enable usb vreg PWR->CR3 |= PWR_CR3_USBREGEN; - while(!CHECKFLAG(PWR->CR3, PWR_FLAG_USB33RDY)); + while (!CHECKFLAG(PWR->CR3, PWR_FLAG_USB33RDY)) + ; PWR->CR3 |= PWR_CR3_USB33DEN; // /* Disable all interrupts. */ @@ -789,7 +791,9 @@ void mcu_init(void) SPI2_REG->CR1 |= SPI_CR1_SPE; #endif #ifdef MCU_HAS_I2C - RCC->APB1ENR |= I2C_APBEN; + RCC->APB1LENR |= __helper__(RCC_APB1LENR_I2C, I2C_PORT, EN); + RCC->APB1LRSTR |= (__helper__(RCC_APB1LRSTR_I2C, I2C_PORT, RST)); + RCC->APB1LRSTR &= ~(__helper__(RCC_APB1LRSTR_I2C, I2C_PORT, RST)); mcu_config_af(I2C_CLK, I2C_AFIO); mcu_config_af(I2C_DATA, I2C_AFIO); mcu_config_pullup(I2C_CLK); @@ -797,13 +801,9 @@ void mcu_init(void) // set opendrain mcu_config_opendrain(I2C_CLK); mcu_config_opendrain(I2C_DATA); - // reset I2C - I2C_REG->CR1 |= I2C_CR1_SWRST; - I2C_REG->CR1 &= ~I2C_CR1_SWRST; + // set max freq - I2C_REG->CR2 |= I2C_SPEEDRANGE; - I2C_REG->TRISE = (I2C_SPEEDRANGE + 1); - I2C_REG->CCR |= (I2C_FREQ <= 100000UL) ? ((I2C_SPEEDRANGE * 5) & 0x0FFF) : (((I2C_SPEEDRANGE * 5 / 6) & 0x0FFF) | I2C_CCR_FS); + I2C_REG->TIMINGR = I2C_TIMINGS_REG; // initialize the I2C configuration register I2C_REG->CR1 |= I2C_CR1_PE; #endif diff --git a/uCNC/src/hal/mcus/stm32h7x/mcumap_stm32h7x.h b/uCNC/src/hal/mcus/stm32h7x/mcumap_stm32h7x.h index 8ee979b96..67f944c7e 100644 --- a/uCNC/src/hal/mcus/stm32h7x/mcumap_stm32h7x.h +++ b/uCNC/src/hal/mcus/stm32h7x/mcumap_stm32h7x.h @@ -2065,25 +2065,25 @@ extern "C" #define DIO207_RCCEN SPI_CS_RCCEN #define DIO207_GPIO SPI_CS_GPIO #endif -#if (defined(I2C_SCL_PORT) && defined(I2C_SCL_BIT)) -#define I2C_SCL 208 -#define I2C_SCL_RCCEN (__rccgpioen__(I2C_SCL_PORT)) -#define I2C_SCL_GPIO (__gpio__(I2C_SCL_PORT)) +#if (defined(I2C_CLK_PORT) && defined(I2C_CLK_BIT)) +#define I2C_CLK 208 +#define I2C_CLK_RCCEN (__rccgpioen__(I2C_CLK_PORT)) +#define I2C_CLK_GPIO (__gpio__(I2C_CLK_PORT)) #define DIO208 208 -#define DIO208_PORT I2C_SCL_PORT -#define DIO208_BIT I2C_SCL_BIT -#define DIO208_RCCEN I2C_SCL_RCCEN -#define DIO208_GPIO I2C_SCL_GPIO -#endif -#if (defined(I2C_SDA_PORT) && defined(I2C_SDA_BIT)) -#define I2C_SDA 209 -#define I2C_SDA_RCCEN (__rccgpioen__(I2C_SDA_PORT)) -#define I2C_SDA_GPIO (__gpio__(I2C_SDA_PORT)) +#define DIO208_PORT I2C_CLK_PORT +#define DIO208_BIT I2C_CLK_BIT +#define DIO208_RCCEN I2C_CLK_RCCEN +#define DIO208_GPIO I2C_CLK_GPIO +#endif +#if (defined(I2C_DATA_PORT) && defined(I2C_DATA_BIT)) +#define I2C_DATA 209 +#define I2C_DATA_RCCEN (__rccgpioen__(I2C_DATA_PORT)) +#define I2C_DATA_GPIO (__gpio__(I2C_DATA_PORT)) #define DIO209 209 -#define DIO209_PORT I2C_SDA_PORT -#define DIO209_BIT I2C_SDA_BIT -#define DIO209_RCCEN I2C_SDA_RCCEN -#define DIO209_GPIO I2C_SDA_GPIO +#define DIO209_PORT I2C_DATA_PORT +#define DIO209_BIT I2C_DATA_BIT +#define DIO209_RCCEN I2C_DATA_RCCEN +#define DIO209_GPIO I2C_DATA_GPIO #endif #if (defined(TX2_PORT) && defined(TX2_BIT)) #define TX2 210 @@ -4388,9 +4388,9 @@ extern "C" #define I2C_PORT 1 #endif -#define I2C_APBEN __helper__(RCC_APB1ENR_I2C, I2C_PORT, EN) +#define I2C_APBEN __helper__(RCC_APB1LENR_I2C, I2C_PORT, EN) #define I2C_REG __helper__(I2C, I2C_PORT, ) -#define I2C_SPEEDRANGE (HAL_RCC_GetPCLK1Freq() / 1000000UL) +#define I2C_TIMINGS_REG 0x0 #define I2C_AFIO 4 #define I2C_IRQ __helper__(I2C, I2C_PORT, _EV_IRQn)