Hello,
I'm trying to write a simple I2C example for the DS1307. It is working fine on a TM4C123G but not on an TM4C129E. I added my code for both examples an also added the capture I2C data of my logic analyzer. I know that the DS1307 is a 5V chip. I added a logic level converter on SDA and SCL (5V to 3,3V).
Can anyone say what I am doing wrong?
Thanks in advance.
Bye,
Stanley
TM4C129E example:
#include <stdint.h> #include <stdbool.h> #include "inc/hw_i2c.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/gpio.h" #include "driverlib/i2c.h" // The error routine that is called if the driver library encounters an error. #ifdef DEBUG void __error__(char *pcFilename, uint32_t ui32Line) { } #endif uint32_t ui32SysClock; void initHardware(void) { ui32SysClock = SysCtlClockFreqSet( (SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); // IO's configuration SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)) { } GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); // I2C Controller configuration SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); while (!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0)) { } I2CMasterInitExpClk(I2C0_BASE, ui32SysClock, true); } void write_read_ram() { int i; char text[] = "Hello World!"; char text_ram[] = " "; int size = sizeof(text) / sizeof(char); /* * Write string to ram */ I2CMasterSlaveAddrSet(I2C0_BASE, 0x68, false); while (I2CMasterBusy(I2C0_BASE)) { } I2CMasterDataPut(I2C0_BASE, 0x08); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START); while (I2CMasterBusy(I2C0_BASE)) { } for (i = 0; i < size - 2; i++) { I2CMasterDataPut(I2C0_BASE, text[i]); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); while (I2CMasterBusy(I2C0_BASE)) { } } I2CMasterDataPut(I2C0_BASE, text[size - 2]); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); while (I2CMasterBusy(I2C0_BASE)) { } /* * Read String from ram */ // Request data // Specify slave address and the fact that we SEND I2CMasterSlaveAddrSet(I2C0_BASE, 0x68, false); // send one 0 to ask for data I2CMasterDataPut(I2C0_BASE, 0x08); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND); while (I2CMasterBusy(I2C0_BASE)) { } // Get data // Specify slave address and the fact that we RECEIVE I2CMasterSlaveAddrSet(I2C0_BASE, 0x68, true); // receive the data back I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); while (I2CMasterBusy(I2C0_BASE)) { } text_ram[0] = I2CMasterDataGet(I2C0_BASE); for (i = 1; i < size - 2; i++) { I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while (I2CMasterBusy(I2C0_BASE)) { } text_ram[i] = I2CMasterDataGet(I2C0_BASE); } I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); while (I2CMasterBusy(I2C0_BASE)) { } text_ram[size - 2] = I2CMasterDataGet(I2C0_BASE); } int main(void) { initHardware(); while (1) { write_read_ram(); SysCtlDelay(ui32SysClock / 3 / 2); } }
TM4C129E example result:
Time [s], Analyzer Name, Decoded Protocol Result
0.405383125000000,I2C,Setup Write to [h (0x68)] + ACK
0.405407375000000,I2C,'8' (0x08) + ACK
0.405433187500000,I2C,l (0x6C) + ACK
0.405459000000000,I2C,o (0x6F) + ACK
0.405484812500000,I2C,! (0x21) + ACK
0.405515562500000,I2C,Setup Write to [h (0x68)] + ACK
0.405539875000000,I2C,'8' (0x08) + ACK
0.405570625000000,I2C,Setup Read to [h (0x68)] + ACK
0.405594875000000,I2C,l (0x6C) + ACK
0.405620687500000,I2C,o (0x6F) + ACK
0.405646500000000,I2C,! (0x21) + ACK
0.405672312500000,I2C,l (0x6C) + NAK
TM4C123G example:
#include <stdint.h> #include <stdbool.h> #include "inc/hw_i2c.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/gpio.h" #include "driverlib/i2c.h" // The error routine that is called if the driver library encounters an error. #ifdef DEBUG void __error__(char *pcFilename, uint32_t ui32Line) { } #endif uint32_t ui32SysClock; void initHardware(void) { ui32SysClock = SysCtlClockFreqSet( (SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); // IO's configuration SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)) { } GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); // I2C Controller configuration SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); while (!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0)) { } I2CMasterInitExpClk(I2C0_BASE, ui32SysClock, true); } void write_read_ram() { int i; char text[] = "Hello World!"; char text_ram[] = " "; int size = sizeof(text) / sizeof(char); /* * Write string to ram */ I2CMasterSlaveAddrSet(I2C0_BASE, 0x68, false); while (I2CMasterBusy(I2C0_BASE)) { } I2CMasterDataPut(I2C0_BASE, 0x08); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START); while (I2CMasterBusy(I2C0_BASE)) { } for (i = 0; i < size - 2; i++) { I2CMasterDataPut(I2C0_BASE, text[i]); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); while (I2CMasterBusy(I2C0_BASE)) { } } I2CMasterDataPut(I2C0_BASE, text[size - 2]); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); while (I2CMasterBusy(I2C0_BASE)) { } /* * Read String from ram */ // Request data // Specify slave address and the fact that we SEND I2CMasterSlaveAddrSet(I2C0_BASE, 0x68, false); // send one 0 to ask for data I2CMasterDataPut(I2C0_BASE, 0x08); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND); while (I2CMasterBusy(I2C0_BASE)) { } // Get data // Specify slave address and the fact that we RECEIVE I2CMasterSlaveAddrSet(I2C0_BASE, 0x68, true); // receive the data back I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); while (I2CMasterBusy(I2C0_BASE)) { } text_ram[0] = I2CMasterDataGet(I2C0_BASE); for (i = 1; i < size - 2; i++) { I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while (I2CMasterBusy(I2C0_BASE)) { } text_ram[i] = I2CMasterDataGet(I2C0_BASE); } I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); while (I2CMasterBusy(I2C0_BASE)) { } text_ram[size - 2] = I2CMasterDataGet(I2C0_BASE); } int main(void) { initHardware(); while (1) { write_read_ram(); SysCtlDelay(ui32SysClock / 3 / 2); } }
TM4C123G example result:
Time [s], Analyzer Name, Decoded Protocol Result
0.750428250000000,I2C,Setup Write to [h (0x68)] + ACK
0.750455250000000,I2C,'8' (0x08) + ACK
0.750483125000000,I2C,H (0x48) + ACK
0.750510937500000,I2C,e (0x65) + ACK
0.750538812500000,I2C,l (0x6C) + ACK
0.750566625000000,I2C,l (0x6C) + ACK
0.750594500000000,I2C,o (0x6F) + ACK
0.750622312500000,I2C,' ' (0x20) + ACK
0.750650187500000,I2C,W (0x57) + ACK
0.750678000000000,I2C,o (0x6F) + ACK
0.750705875000000,I2C,r (0x72) + ACK
0.750733687500000,I2C,l (0x6C) + ACK
0.750761562500000,I2C,d (0x64) + ACK
0.750789375000000,I2C,! (0x21) + ACK
0.750824187500000,I2C,Setup Write to [h (0x68)] + ACK
0.750851187500000,I2C,'8' (0x08) + ACK
0.750885937500000,I2C,Setup Read to [h (0x68)] + ACK
0.750912937500000,I2C,H (0x48) + ACK
0.750940750000000,I2C,e (0x65) + ACK
0.750968625000000,I2C,l (0x6C) + ACK
0.750996437500000,I2C,l (0x6C) + ACK
0.751024312500000,I2C,o (0x6F) + ACK
0.751052125000000,I2C,' ' (0x20) + ACK
0.751080000000000,I2C,W (0x57) + ACK
0.751107812500000,I2C,o (0x6F) + ACK
0.751135687500000,I2C,r (0x72) + ACK
0.751163500000000,I2C,l (0x6C) + ACK
0.751191375000000,I2C,d (0x64) + ACK
0.751219187500000,I2C,! (0x21) + NAK