Skip to content

Commit

Permalink
all possible I2C bitrates with runtime changing possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dewagter committed Nov 24, 2011
1 parent 9f52491 commit 4a4eb45
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion conf/modules/i2c_abuse_test.xml
Expand Up @@ -5,7 +5,7 @@
<file name="i2c_abuse_test.h"/>
</header>
<init fun="init_i2c_abuse_test()"/>
<periodic fun="periodic_50Hz_i2c_abuse_test()" period="0.02" autorun="TRUE" />
<periodic fun="periodic_50Hz_i2c_abuse_test()" period="0.02" autorun="TRUE" />
<event fun="event_i2c_abuse_test()" />
<makefile>
<file name="i2c_abuse_test.c"/>
Expand Down
58 changes: 54 additions & 4 deletions sw/airborne/arch/stm32/mcu_periph/i2c_arch.rewritten.c
Expand Up @@ -1203,15 +1203,65 @@ void i2c_setbitrate(struct i2c_periph *periph, int bitrate)
{
if (periph == &i2c2)
{
// I2C_DeInit(I2C1);
// I2C_Cmd(I2C2, DISABLE);
int devider;
int risetime;

I2C_TypeDef *regs = (I2C_TypeDef *) i2c2.reg_addr;

// store (just for fun)
I2C2_InitStruct.I2C_ClockSpeed = bitrate;
// I2C_Cmd(I2C2, ENABLE);
// I2C_Init(I2C2, i2c2.init_struct);

/*****************************************************
Bitrate:
-CR2 + CCR + TRISE registers
-only change when PE=0
e.g.
10kHz: 36MHz + Standard 0x708 + 0x25
70kHz: 36MHz + Standard 0x101 +
400kHz: 36MHz + Fast 0x1E + 0xb
// 1) Program peripheral input clock CR2: to get correct timings
// 2) Configure clock control registers
// 3) Configure rise time register
******************************************************/

if (bitrate < 3000)
bitrate = 3000;

// 36MHz, fast scl: 2counts low 1 count high -> / 3:
devider = 12000000UL / bitrate;

// never allow faster than 600kbps
if (devider < 20)
devider = 20;

// no overflow either
if (devider >=4095)
devider = 4095;

risetime = 1000000 / (bitrate/1000) / 8 / 28;

if (risetime < 10)
risetime = 10;

if (risetime >=31)
risetime = 31;


regs->CR1 &= ~ I2C_CR1_BIT_PE;

// 1)
regs->CR2 = 0x0324;
// 2)
regs->CCR = 0x8000 + devider;
// 3)
regs->TRISE = risetime;

regs->CR1 |= I2C_CR1_BIT_PE;

}

#ifdef I2C_DEBUG_LED
Expand Down
6 changes: 1 addition & 5 deletions sw/airborne/modules/benchmark/i2c_abuse_test.c
Expand Up @@ -169,9 +169,6 @@ static void i2c_abuse_send_transaction(uint8_t _init)

void event_i2c_abuse_test(void)
{
static uint8_t bit1 = 0;
static uint8_t bit2 = 0;

if (i2c_idle(&i2c2))
{
LED_ON(5); // green = idle
Expand Down Expand Up @@ -211,13 +208,12 @@ void event_i2c_abuse_test(void)
{
i2c_abuse_test_counter = 1;

//i2c_setbitrate(&i2c2, i2c_abuse_test_bitrate);
i2c_setbitrate(&i2c2, i2c_abuse_test_bitrate);

i2c_abuse_test_bitrate += 17000;
if (i2c_abuse_test_bitrate > 500000)
{
i2c_abuse_test_bitrate -= 500000;
bit1 = 1 - bit1;
}
}
}
Expand Down

0 comments on commit 4a4eb45

Please sign in to comment.