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.c142
1 files changed, 76 insertions, 66 deletions
diff --git a/app/i2c_hw.c b/app/i2c_hw.c
index 35bde08..59d2451 100644
--- a/app/i2c_hw.c
+++ b/app/i2c_hw.c
@@ -1,20 +1,20 @@
#include "project.h"
-#define SCL1 GPIO6
-#define SDA1 GPIO7
-#define SCL1_PORT GPIOB
-#define SDA1_PORT GPIOB
+#define SCL1 GPIO6
+#define SDA1 GPIO7
+#define SCL1_PORT GPIOB
+#define SDA1_PORT GPIOB
-#define SCL2 GPIO10
-#define SDA2 GPIO11
-#define SCL2_PORT GPIOB
-#define SDA2_PORT GPIOB
+#define SCL2 GPIO10
+#define SDA2 GPIO11
+#define SCL2_PORT GPIOB
+#define SDA2_PORT GPIOB
void
i2c_clear_start (uint32_t i2c)
{
- I2C_CR1 (i2c) &= ~(uint32_t) I2C_CR1_START;
+ I2C_CR1 (i2c) &= ~ (uint32_t) I2C_CR1_START;
}
@@ -23,8 +23,9 @@ i2cp_start (uint32_t i2c)
{
uint32_t timeout = 1000;
i2c_send_start (i2c);
- while (!((I2C_SR1 (i2c) & I2C_SR1_SB)
- & (I2C_SR2 (i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))) && (timeout--));
+
+ while (! ((I2C_SR1 (i2c) & I2C_SR1_SB)
+ & (I2C_SR2 (i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))) && (timeout--));
return timeout ? 0 : -1;
}
@@ -60,12 +61,13 @@ i2cp_send (uint32_t i2c, uint8_t v)
while (!
((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)) && (timeout--));
+ I2C_SR1 (i2c)) & (I2C_SR1_BTF | I2C_SR1_BERR | I2C_SR1_ARLO |
+ I2C_SR1_AF | I2C_SR1_PECERR | I2C_SR1_TIMEOUT |
+ 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);
+ reg & ~ (I2C_SR1_BERR | I2C_SR1_ARLO | I2C_SR1_AF | I2C_SR1_PECERR |
+ I2C_SR1_TIMEOUT | I2C_SR1_SMBALERT);
if (!timeout)
return 1;
@@ -79,12 +81,13 @@ int
i2cp_start_transaction (uint32_t i2c, uint8_t a, int wnr)
{
uint32_t reg;
- uint32_t __attribute__((unused)) dummy;
+ 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))) && (timeout--));
+
+ while (! ((I2C_SR1 (i2c) & I2C_SR1_SB)
+ & (I2C_SR2 (i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))) && (timeout--));
if (!timeout)
return -1;
@@ -93,24 +96,25 @@ i2cp_start_transaction (uint32_t i2c, uint8_t a, int wnr)
while (!
((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)) && (timeout--));
+ I2C_SR1 (i2c)) & (I2C_SR1_ADDR | I2C_SR1_BERR | I2C_SR1_ARLO |
+ I2C_SR1_AF | I2C_SR1_PECERR | I2C_SR1_TIMEOUT |
+ I2C_SR1_SMBALERT)) && (timeout--));
+
if (!timeout)
return -1;
dummy = I2C_SR2 (i2c);
I2C_SR1 (i2c) =
- reg & ~(I2C_SR1_BERR | I2C_SR1_ARLO | I2C_SR1_AF | I2C_SR1_PECERR |
- I2C_SR1_TIMEOUT | I2C_SR1_SMBALERT);
+ reg & ~ (I2C_SR1_BERR | I2C_SR1_ARLO | I2C_SR1_AF | I2C_SR1_PECERR |
+ I2C_SR1_TIMEOUT | I2C_SR1_SMBALERT);
return (reg == 0x82) ? 0 : -1;
}
- /*This is stupid, it bit bangs a dummy transaction to get the host i2c controller back in the game */
+/*This is stupid, it bit bangs a dummy transaction to get the host i2c controller back in the game */
void
i2cp1_reset_sm (void)
{
@@ -126,14 +130,14 @@ i2cp1_reset_sm (void)
delay_us (10);
CLEAR (SCL1);
- for (i = 0; i < 9; ++i)
- {
- delay_us (10);
- SET (SCL1);
- delay_us (10);
- CLEAR (SCL1);
- delay_us (10);
- }
+ for (i = 0; i < 9; ++i) {
+ delay_us (10);
+ SET (SCL1);
+ delay_us (10);
+ CLEAR (SCL1);
+ delay_us (10);
+ }
+
SET (SCL1);
delay_us (10);
SET (SDA1);
@@ -159,14 +163,14 @@ i2cp2_reset_sm (void)
delay_us (10);
CLEAR (SCL2);
- for (i = 0; i < 9; ++i)
- {
- delay_us (10);
- SET (SCL2);
- delay_us (10);
- CLEAR (SCL2);
- delay_us (10);
- }
+ for (i = 0; i < 9; ++i) {
+ delay_us (10);
+ SET (SCL2);
+ delay_us (10);
+ CLEAR (SCL2);
+ delay_us (10);
+ }
+
SET (SCL2);
delay_us (10);
SET (SDA2);
@@ -180,7 +184,7 @@ i2cp2_reset_sm (void)
void
-i2cp1_start_dma (uint8_t * buf, int len)
+i2cp1_start_dma (uint8_t *buf, int len)
{
dma_channel_reset (DMA1, DMA_CHANNEL6);
dma_set_peripheral_address (DMA1, DMA_CHANNEL6, (uint32_t) & I2C1_DR);
@@ -200,7 +204,7 @@ i2cp1_start_dma (uint8_t * buf, int len)
void
-i2cp2_start_dma (uint8_t * buf, int len)
+i2cp2_start_dma (uint8_t *buf, int len)
{
dma_channel_reset (DMA1, DMA_CHANNEL4);
dma_set_peripheral_address (DMA1, DMA_CHANNEL4, (uint32_t) & I2C2_DR);
@@ -222,7 +226,7 @@ i2cp2_start_dma (uint8_t * buf, int len)
int
i2cp1_dma_in_progress (void)
{
- return !(DMA1_ISR & (DMA_ISR_TCIF6 | DMA_ISR_TEIF6));
+ return ! (DMA1_ISR & (DMA_ISR_TCIF6 | DMA_ISR_TEIF6));
}
void
@@ -253,7 +257,8 @@ i2cp2_stop_dma (void)
void
i2cp_reset (uint32_t i2c)
{
- while (i2c_lock ());
+ while (i2c_lock());
+
i2cp_start (i2c);
i2cp_abort_start (i2c);
i2cp_stop (i2c);
@@ -263,7 +268,7 @@ i2cp_reset (uint32_t i2c)
i2cp_abort_start (i2c);
i2cp_stop (i2c);
i2cp_abort_stop (i2c);
- i2c_unlock ();
+ i2c_unlock();
}
@@ -276,22 +281,27 @@ i2cp_scan (uint32_t i2c)
i2cp_reset (i2c);
printf ("Probing bus\n");
- for (i = 0; i < 128; ++i)
- {
- printf ("%d\n", i);
- while (i2c_lock ());
- i2cp_start (i2c);
- i2cp_abort_start (i2c);
- i2cp_stop (i2c);
- i2cp_abort_stop (i2c);
- delay_ms (10);
- r = i2cp_start_transaction (i2c, i, I2C_WRITE);
- i2cp_stop (i2c);
- i2c_unlock ();
- if (!r)
- printf ("Found device at address 0x%x\n", i);
- usart1_drain ();
- }
+
+ for (i = 0; i < 128; ++i) {
+ printf ("%d\n", i);
+
+ while (i2c_lock());
+
+ i2cp_start (i2c);
+ i2cp_abort_start (i2c);
+ i2cp_stop (i2c);
+ i2cp_abort_stop (i2c);
+ delay_ms (10);
+ r = i2cp_start_transaction (i2c, i, I2C_WRITE);
+ i2cp_stop (i2c);
+ i2c_unlock();
+
+ if (!r)
+ printf ("Found device at address 0x%x\n", i);
+
+ usart1_drain();
+ }
+
printf ("Done\n");
i2cp_reset (i2c);
@@ -312,9 +322,9 @@ i2cp_bringup (uint32_t i2c)
if (i2c == I2C1)
- i2cp1_reset_sm ();
+ i2cp1_reset_sm();
else
- i2cp2_reset_sm ();
+ i2cp2_reset_sm();
i2c_set_clock_frequency (i2c, I2C_CR2_FREQ_36MHZ);
@@ -335,10 +345,10 @@ i2cp_init (void)
rcc_periph_clock_enable (RCC_I2C2);
rcc_periph_clock_enable (RCC_DMA1);
- while (i2c_lock ());
+ while (i2c_lock());
i2cp_bringup (I2C1);
i2cp_bringup (I2C2);
- i2c_unlock ();
+ i2c_unlock();
}