Skip to content

Commit

Permalink
I2C clock bugfixes
Browse files Browse the repository at this point in the history
Explictly set the I2C clock source, else was not clocked correctly
when started from BTT bootloader.

Allow for selection between Standard, Fast, and Fast Plus modes,
default to Fast mode (400kHz).
  • Loading branch information
dresco committed Apr 2, 2024
1 parent 152e814 commit 6087f31
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 14 additions & 1 deletion Src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

#ifdef I2C_PORT

#ifndef I2C_KHZ
#define I2C_KHZ 400
#endif

#ifdef I2C1_ALT_PINMAP
#define I2C1_SCL_PIN 6
#define I2C1_SDA_PIN 7
Expand All @@ -41,9 +45,18 @@

static uint8_t keycode = 0;
static keycode_callback_ptr keypad_callback = NULL;

static I2C_HandleTypeDef i2c_port = {
.Instance = I2CPORT,
.Init.Timing = 0x20303E5D,
#if I2C_KHZ == 100
.Init.Timing = 0x307075B1,
#elif I2C_KHZ == 400
.Init.Timing = 0x00B03FDB,
#elif I2C_KHZ == 1000
.Init.Timing = 0x0050174F,
#else
#error "I2C speed not defined"
#endif
.Init.OwnAddress1 = 0,
.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT,
.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE,
Expand Down
7 changes: 4 additions & 3 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,10 @@ void SystemClock_Config(void)
Error_Handler();
}

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPI123 | RCC_PERIPHCLK_SPI45;
PeriphClkInitStruct.Spi123ClockSelection = RCC_SPI123CLKSOURCE_PLL;
PeriphClkInitStruct.Spi45ClockSelection = RCC_SPI45CLKSOURCE_PLL2;
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPI123 | RCC_PERIPHCLK_SPI45 | RCC_PERIPHCLK_I2C123;
PeriphClkInitStruct.Spi123ClockSelection = RCC_SPI123CLKSOURCE_PLL;
PeriphClkInitStruct.Spi45ClockSelection = RCC_SPI45CLKSOURCE_PLL2;
PeriphClkInitStruct.I2c1235ClockSelection = RCC_I2C123CLKSOURCE_D2PCLK1;

#if RTC_ENABLE
PeriphClkInitStruct.PeriphClockSelection = PeriphClkInitStruct.PeriphClockSelection | RCC_PERIPHCLK_RTC;
Expand Down

0 comments on commit 6087f31

Please sign in to comment.