Skip to content

Commit

Permalink
I2C partial implementation
Browse files Browse the repository at this point in the history
- I2C partial implementation
- TIMINGR value needs to be manually calculated and set via I2C_TIMINGS_REG option
  • Loading branch information
Paciente8159 committed Jan 20, 2025
1 parent be89913 commit f09ef53
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
26 changes: 13 additions & 13 deletions uCNC/src/hal/boards/stm32/boardmap_generic_h750.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions uCNC/src/hal/mcus/stm32h7x/mcu_stm32h7x.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "core_cm7.h"
#include "stm32h7xx.h"
#include "mcumap_stm32h7x.h"
#include "stm32h7xx_hal.h"
#include <math.h>

#ifdef MCU_HAS_USB
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -789,21 +791,19 @@ 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);
mcu_config_pullup(I2C_DATA);
// 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
Expand Down
38 changes: 19 additions & 19 deletions uCNC/src/hal/mcus/stm32h7x/mcumap_stm32h7x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f09ef53

Please sign in to comment.