summaryrefslogtreecommitdiffstats
path: root/app/i2c_hw.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/i2c_hw.c')
-rw-r--r--app/i2c_hw.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/app/i2c_hw.c b/app/i2c_hw.c
index b709af0..d7989a9 100644
--- a/app/i2c_hw.c
+++ b/app/i2c_hw.c
@@ -62,6 +62,7 @@ int
i2cp_send (uint8_t v)
{
uint32_t reg;
+ uint32_t timeout=1000;
i2c_send_data (I2C, v);
@@ -69,11 +70,13 @@ i2cp_send (uint8_t v)
((reg =
I2C_SR1 (I2C)) & (I2C_SR1_BTF | I2C_SR1_BERR | I2C_SR1_ARLO |
I2C_SR1_AF | I2C_SR1_PECERR | I2C_SR1_TIMEOUT |
- I2C_SR1_SMBALERT)));
+ I2C_SR1_SMBALERT)) && (timeout--));
I2C_SR1 (I2C) =
reg & ~(I2C_SR1_BERR | I2C_SR1_ARLO | I2C_SR1_AF | I2C_SR1_PECERR |
I2C_SR1_TIMEOUT | I2C_SR1_SMBALERT);
+ if (!timeout) return 1;
+
return (reg & I2C_SR1_BTF) ? 0 : -1;
}
@@ -84,10 +87,13 @@ i2cp_start_transaction (uint8_t a, int wnr)
{
uint32_t reg;
uint32_t __attribute__((unused)) dummy;
+ uint32_t timeout=1000;
i2c_send_start (I2C);
while (!((I2C_SR1 (I2C) & I2C_SR1_SB)
- & (I2C_SR2 (I2C) & (I2C_SR2_MSL | I2C_SR2_BUSY))));
+ & (I2C_SR2 (I2C) & (I2C_SR2_MSL | I2C_SR2_BUSY))) && (timeout--));
+
+ if (!timeout) return -1;
i2c_send_7bit_address (I2C, a, wnr);
@@ -95,7 +101,9 @@ i2cp_start_transaction (uint8_t a, int wnr)
((reg =
I2C_SR1 (I2C)) & (I2C_SR1_ADDR | I2C_SR1_BERR | I2C_SR1_ARLO |
I2C_SR1_AF | I2C_SR1_PECERR | I2C_SR1_TIMEOUT |
- I2C_SR1_SMBALERT)));
+ I2C_SR1_SMBALERT)) && (timeout--));
+ if (!timeout) return -1;
+
dummy = I2C_SR2 (I2C);
I2C_SR1 (I2C) =
@@ -108,7 +116,7 @@ i2cp_start_transaction (uint8_t a, int wnr)
/*This is stupid, it bit bangs a dummy transaction to get the host i2c controller back in the game */
-static void
+void
i2cp_reset_sm (void)
{
int i;
@@ -136,6 +144,11 @@ i2cp_reset_sm (void)
gpio_set (GPIOB, GPIO_I2C1_SDA);
delay_us (10);
+ gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN,
+ GPIO_I2C1_SCL | GPIO_I2C1_SDA);
+
+
}
void
@@ -245,10 +258,6 @@ i2cp_init (void)
i2cp_reset_sm ();
- gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
- GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN,
- GPIO_I2C1_SCL | GPIO_I2C1_SDA);
-
i2c_set_clock_frequency (I2C, I2C_CR2_FREQ_36MHZ);
i2c_set_standard_mode (I2C);
i2c_set_ccr (I2C, 0x80); /* t_high=t_high=CCR * Tpclk1 */