Problems configuring bit-banged I2C
Hi, all!
I use the following patch on my AT91SAM9260-based custom board to configure bit-banged I2C:
board/atmel/at91sam9260ek/at91sam9260ek.c +0 -0
@ -234,3 +234,46 @@ void reset_phy(void)
#endif
}
#endif
+
+#define GPIO_I2C_SCL AT91_PIN_PA24
+#define GPIO_I2C_SDA AT91_PIN_PA23
+
+void at91sam9260_i2c_init(void)
+{
+ __raw_writel(pin_to_mask(GPIO_I2C_SCL), pin_to_controller(GPIO_I2C_SCL) + PIO_IDR);
+ __raw_writel(pin_to_mask(GPIO_I2C_SCL), pin_to_controller(GPIO_I2C_SCL) + PIO_PUER);
+ __raw_writel(pin_to_mask(GPIO_I2C_SCL), pin_to_controller(GPIO_I2C_SCL) + PIO_PER);
+ __raw_writel(pin_to_mask(GPIO_I2C_SCL), pin_to_controller(GPIO_I2C_SCL) + PIO_MDER);
+ __raw_writel(pin_to_mask(GPIO_I2C_SCL), pin_to_controller(GPIO_I2C_SCL) + PIO_SODR);
+ __raw_writel(pin_to_mask(GPIO_I2C_SCL), pin_to_controller(GPIO_I2C_SCL) + PIO_OER);
+
+ __raw_writel(pin_to_mask(GPIO_I2C_SDA), pin_to_controller(GPIO_I2C_SDA) + PIO_IDR);
+ __raw_writel(pin_to_mask(GPIO_I2C_SDA), pin_to_controller(GPIO_I2C_SDA) + PIO_PUER);
+ __raw_writel(pin_to_mask(GPIO_I2C_SDA), pin_to_controller(GPIO_I2C_SDA) + PIO_PER);
+ __raw_writel(pin_to_mask(GPIO_I2C_SDA), pin_to_controller(GPIO_I2C_SDA) + PIO_MDER);
+ __raw_writel(pin_to_mask(GPIO_I2C_SDA), pin_to_controller(GPIO_I2C_SDA) + PIO_SODR);
+ __raw_writel(pin_to_mask(GPIO_I2C_SDA), pin_to_controller(GPIO_I2C_SDA) + PIO_OER);
+}
+void at91sam9260_i2c_scl(unsigned long bit)
+{
+ if(bit)
+ __raw_writel(pin_to_mask(GPIO_I2C_SCL), pin_to_controller(GPIO_I2C_SCL) + PIO_SODR);
+ else
+ __raw_writel(pin_to_mask(GPIO_I2C_SCL), pin_to_controller(GPIO_I2C_SCL) + PIO_CODR);
+}
+
+void at91sam9260_i2c_sda(unsigned long bit)
+{
+ printf("Setting %u\n", bit);
+ if(bit)
+ __raw_writel(pin_to_mask(GPIO_I2C_SDA), pin_to_controller(GPIO_I2C_SDA) + PIO_SODR);
+ else
+ __raw_writel(pin_to_mask(GPIO_I2C_SDA), pin_to_controller(GPIO_I2C_SDA) + PIO_CODR);
+}
+
+int at91sam9260_i2c_read(void)
+{
+ int c = (__raw_readl(pin_to_controller(GPIO_I2C_SDA) + PIO_PDSR) & pin_to_mask(GPIO_I2C_SDA)) ? 1 : 0;
+ printf("Reading %d\n", c);
+ return c;
+}
include/configs/at91sam9260ek.h +2 -0
@@ -85,6 +85,8 @@#define CONFIG_CMD_NAND 1#define CONFIG_CMD_USB 1 +#define CONFIG_CMD_I2C 1 +#define CONFIG_I2C_CMD_TREE 1/* SDRAM */ #define CONFIG_NR_DRAM_BANKS 1
@ -192,4 +194,20 @@
#error CONFIG_USE_IRQ not supported
#endif
+/*
+ * Software (bit-bang) I2C driver configuration
+ */
+#define CONFIG_SOFT_I2C 1
+#define CFG_I2C_SPEED 1000000
+#define I2C_INIT at91sam9260_i2c_init()
+
+#define I2C_ACTIVE
+#define I2C_TRISTATE
+#define I2C_SCL(bit) at91sam9260_i2c_scl(bit)
+#define I2C_SDA(bit) at91sam9260_i2c_sda(bit)
+#define I2C_DELAY udelay(2)
+#define I2C_READ at91sam9260_i2c_read()
+#define CFG_I2C_SLAVE 0
+#define DEBUG_I2C
#endif
+
But I'm unable to read proper values from lines - they always return 0s. Any ideas on fixing?
Linux i2c-gpio driver works perfectly.
Thanks a lot,
S.
Quoted 9 lines · Sergey Lapin
Hi, all!
I use the following patch on my AT91SAM9260-based custom board to configure bit-banged I2C:
board/atmel/at91sam9260ek/at91sam9260ek.c +0
-0
Quoted 4 lines
@ -234,3 +234,46 @@ void
reset_phy(void)
#endif
}
#endif
Why don't you use the gpio function?
Quoted 1 line
+
void at91sam9260_i2c_init(void)
{
at91_set_GPIO_periph(AT91_PIN_PA23, 0);
at91_set_multi_drive(AT91_PIN_PA23, 1);
at91_set_A_periph(AT91_PIN_PA24, 0);
at91_set_multi_drive(AT91_PIN_PA24, 1);
}void at91sam9260_i2c_scl(unsigned long bit)
{
at91_set_gpio_value(AT91_PIN_PA24, bit);
}void at91sam9260_i2c_sda(unsigned long bit)
{
at91_set_gpio_value(AT91_PIN_PA23, bit)
}
int at91sam9260_i2c_read(void)
{
retrun at91_get_gpio_value(AT91_PIN_PA23);
}
void at91sam9260_i2c_active()
{
gpio_direction_output(AT91_PIN_PA23, 0);
}void at91sam9260_i2c_tristate()
{
gpio_direction_input(AT91_PIN_PA23);
}Quoted 17 lines
include/configs/at91sam9260ek.h +2
-0@@ -85,6 +85,8 @@#define CONFIG_CMD_NAND 1#define CONFIG_CMD_USB 1 +#define CONFIG_CMD_I2C 1 +#define CONFIG_I2C_CMD_TREE 1/* SDRAM */ #define CONFIG_NR_DRAM_BANKS 1@ -192,4 +194,20 @@
#errorCONFIG_USE_IRQnot supported
#endif
include/configs/at91sam9260ek.h
Quoted 6 lines
include/configs/at91sam9260ek.h +6
-0+/* +* Software (bit-bang) I2C driver configuration +*/ +#define CONFIG_SOFT_I2C 1 +#define CFG_I2C_SPEED 1000000 +#define I2C_INIT at91sam9260_i2c_init()
#define I2C_ACTIVE at91sam9260_i2c_active()
#define I2C_TRISTATE at91sam9260_i2c_tristate()
include/configs/at91sam9260ek.h
Quoted 8 lines
include/configs/at91sam9260ek.h +7
-0+#define I2C_SCL (bit) at91sam9260_i2c_scl(bit) +#define I2C_SDA (bit) at91sam9260_i2c_sda(bit) +#define I2C_DELAY udelay(2) +#define I2C_READ at91sam9260_i2c_read() +#define CFG_I2C_SLAVE 0 +#define DEBUG_I2C #endif +
I sync it will be better to use the hardware stack which is not too complicate to implement
Best Regards,
J.
Quoted 1 line
Why don't you use the gpio function?
Well, I tried to use them but that lead to same result.
Quoted 7 lines
Quoted 1 line
+
void
at91sam9260_i2c_init(void){ at91_set_GPIO_periph(AT91_PIN_PA23, 0); at91_set_multi_drive(AT91_PIN_PA23, 1); at91_set_A_periph(AT91_PIN_PA24, 0);
Why?
Quoted 21 lines
at91_set_multi_drive(AT91_PIN_PA24, 1); }void
at91sam9260_i2c_scl(unsigned long bit){ at91_set_gpio_value(AT91_PIN_PA24, bit); }void
at91sam9260_i2c_sda(unsigned long bit){ at91_set_gpio_value(AT91_PIN_PA23, bit) } int at91sam9260_i2c_read(void) { retrun at91_get_gpio_value(AT91_PIN_PA23); } void at91sam9260_i2c_active() { gpio_direction_output(AT91_PIN_PA23, 0); }
Quoted 4 lines
void
at91sam9260_i2c_tristate(){ gpio_direction_input(AT91_PIN_PA23); }
Thanks a lot for your help, but this did not work either. iprobe returns full 0x0 - 0x7f range as devices.
Linux i2c-gpio works properly and returns proper set of devices (0x68, 0x50). Is it me or there's bug somewhere?
I can see output values with oscilloscope. I wonder what happens.
Quoted 3 lines
I sync it will be better to use the hardware stack which is not too complicate to implement
As commented in Linux kernel about i2c_at91, it is very buggy, and I was afraid that it could not work for me at all. i2c-gpio works in Linux. What prevents soft i2c from working ont the same CPU? This stuff is a mystery for me :(
Quoted 3 lines · Sergey Lapin
But I'm unable to read proper values from lines - they always return 0s. Any ideas on fixing?
Linux i2c-gpio driver works perfectly.
Please try this patch:
http://www.nabble.com/-PATCH--soft_i2c%3A-Pull-SDA-high-before-reading-p17270563.html
You shouldn't need to do anything in the I2C_ACTIVE and I2C_TRISTATE hooks since the PIO is properly configured in open drain mode.
Haavard
Quoted 11 lines · Haavard Skinnemoen
Quoted 3 lines · Sergey Lapin
But I'm unable to read proper values from lines - they always return 0s. Any ideas on fixing?
Linux i2c-gpio driver works perfectly.Please try this patch:
http://www.nabble.com/-PATCH--soft_i2c%3A-Pull-SDA-high-before-reading-p17270563.html
You shouldn't need to do anything in the
I2C_ACTIVEandI2C_TRISTATEhooks since the PIO is properly configured in open drain mode.
Alas, I was unable to make bit-banged I2C to work
even with this patch applied. As I mentioned above, it works in Linux, but I'd like to be able to set time to my RTC from u-boot. I have checked that pins are configured properly from status registers (pull-ups are enabled, multi-drive mode is enabled (open drain), etc.). I could read only zeroes from SDA line in u-boot, and using oscilloscope, I see data on line. I can't find differencies in setup with i2c-gpio from Linux. Any ideas?
Thanks a lot,
S.