Skip to content
Snippets Groups Projects
Commit 221d1d54 authored by Eric Brombaugh's avatar Eric Brombaugh
Browse files

Updated with selectable I2C prescaler.

parent 4917d9d4
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,12 @@ https://user-images.githubusercontent.com/1132011/230734071-dee305de-5aad-4ca0-a
## Build options
There are a few build-time options in the i2c.h source:
* I2C_CLKRATE - defines the I2C bus clock rate. Both 100kHz and 400kHz are supported.
800kHz has been seen to work but 1MHz did not.
800kHz has been seen to work when I2C_PRERATE is 1000000, but 1MHz did not. To
use higher bus rates you must increase I2C_PRERATE at the expense of higher power
consumption.
* I2C_PRERATE - defines the I2C logic clock rate. Must be higher than I2C_CLKRATE.
Keep this value as low as possible (but not lower than 1000000) to ensure low power
operaton.
* I2C_DUTY - for I2C_CLKRATE > 100kHz this specifies the duty cycle, either 33% or 36%.
* TIMEOUT_MAX - the amount of tries in busy-wait loops before giving up. This value
depends on the I2C_CLKRATE and should not affect normal operation.
......
......@@ -6,8 +6,11 @@
#ifndef _I2C_H
#define _I2C_H
// I2C clock rate
#define I2C_CLKRATE 800000
// I2C Bus clock rate - must be lower the Logic clock rate
#define I2C_CLKRATE 1000000
// I2C Logic clock rate - must be higher than Bus clock rate
#define I2C_PRERATE 2000000
// uncomment this for high-speed 36% duty cycle, otherwise 33%
#define I2C_DUTY
......@@ -40,7 +43,7 @@ void i2c_setup(void)
// set freq
tempreg = I2C1->CTLR2;
tempreg &= ~I2C_CTLR2_FREQ;
tempreg |= (APB_CLOCK/1000000)&I2C_CTLR2_FREQ;
tempreg |= (APB_CLOCK/I2C_PRERATE)&I2C_CTLR2_FREQ;
I2C1->CTLR2 = tempreg;
// Set clock config
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment