This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTC DS1307 UART SOMETIMES hangs after reset button is pressed

Hi all,

I am experiencing something weird when i'm interfacing ds1307 with tiva tm4c123g. I am able to uart the data correctly... but sometimes when i press the on-board reset button, it gets stuck in 00:00 (i am displaying seconds:minutes) and it does not continue counting. A weird fix to rectify this is to jerk the backup battery a little and the counter restores. I wonder if this is a problem with my coding or is it simply hardware problem.

Please find below a screenshot of the uart output. Note: sometimes it works fine after reset..

Thanks for any help :)

  • Hello Jeunn

    Could be a combination of both. How is the HW wired up?

    Regards
    Amit
  • Hi Amit,

    I am currently using breadboard connection. SDA and SCL is connected to PB3 and PB2 respectively with pull-up resistors.  Vbat connected to CMOS battery  using crocodile clips. Crystal used is 32kHz.

    #include "utils/ustdlib.h"
    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_i2c.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/gpio.h"
    #include "driverlib/i2c.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    
    #define SLAVE_ADDRESS 0x68
    
    void SRAMRead(void);
    void SRAMWrite(int a,int b);
    void initI2C();
    void ConfigureUART(void);
    unsigned char dec2bcd(unsigned char val) ;
    unsigned char bcd2dec(unsigned char val) ;
    
    unsigned int sec=3,min=59,tsec,tmin;
    
    int main()
    {
    
    	unsigned char Last_Value=1;
    
    	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ);
    
    	initI2C();
    
    	ConfigureUART();
    
    				SRAMRead();
    				I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS , false);
    				I2CMasterDataPut(I2C0_BASE, 0);
    				I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    				while(I2CMasterBusy(I2C0_BASE));
    
    				I2CMasterDataPut(I2C0_BASE, dec2bcd(sec));
    				I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
    				while(I2CMasterBusy(I2C0_BASE));
    
    				I2CMasterDataPut(I2C0_BASE, dec2bcd(min));
    				I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    				while(I2CMasterBusy(I2C0_BASE));
    
    	while(1)
    	{
    		I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS , false);
    		I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    		I2CMasterDataPut(I2C0_BASE, 0x00);
    		I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    		while(I2CMasterBusy(I2C0_BASE));
    
    		I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS , true);
    		I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
    		while(I2CMasterBusy(I2C0_BASE));
    
    		sec = I2CMasterDataGet(I2C0_BASE);
    		I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    		while(I2CMasterBusy(I2C0_BASE));
    
    		min = I2CMasterDataGet(I2C0_BASE);
    		I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    		while(I2CMasterBusy(I2C0_BASE));
    
    //		SRAMWrite(sec,min);
    
    
    		tsec = bcd2dec(sec)&0x7f;
    		tmin = bcd2dec(min);
    		if(Last_Value != tsec)
    		{
    			UARTprintf("%02d:%02d\n",tsec,tmin);
    			Last_Value = tsec;
    			tsec = 0;
    		}
    	}
    }
    
    void SRAMRead(void)
    {
    	I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS , false);
    			I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    			I2CMasterDataPut(I2C0_BASE, 0x08);
    			I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    			while(I2CMasterBusy(I2C0_BASE));
    
    			I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS , true);
    					I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
    					while(I2CMasterBusy(I2C0_BASE));
    
    					sec = I2CMasterDataGet(I2C0_BASE);
    					I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    					while(I2CMasterBusy(I2C0_BASE));
    
    					min = I2CMasterDataGet(I2C0_BASE);
    					I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    					while(I2CMasterBusy(I2C0_BASE));
    
    }
    void SRAMWrite(int a,int b)
    {
    	I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS , false);
    	I2CMasterDataPut(I2C0_BASE, 0x08);
    	I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    	while(I2CMasterBusy(I2C0_BASE));
    	I2CMasterDataPut(I2C0_BASE, 2);
    	I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
    	while(I2CMasterBusy(I2C0_BASE));
    
    	I2CMasterDataPut(I2C0_BASE, 3);
    	I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    	while(I2CMasterBusy(I2C0_BASE));
    
    }
    
    void initI2C()
    {
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    
    	GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); // special I2CSCL treatment for M4F devices //EXTRA
    	GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); //special
    
    	GPIOPinConfigure(GPIO_PB2_I2C0SCL); //special
    	GPIOPinConfigure(GPIO_PB3_I2C0SDA);//special
    
    	SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C0);
    
    	I2CMasterInitExpClk( I2C0_BASE, SysCtlClockGet(), false);
    }
    
    unsigned char dec2bcd(unsigned char val)
    {
    	return ((val/0xA*0x10)+(val%0xA));
    }
    
    unsigned char bcd2dec(unsigned char val) // convert BCD to binary
    {
    	return ((val/0x10*0xA)+(val%0x10));
    }
    
    void ConfigureUART(void)
    {
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    
    	GPIOPinConfigure(GPIO_PA0_U0RX);
    	GPIOPinConfigure(GPIO_PA1_U0TX);
    	GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
    	UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    
    	UARTStdioConfig(0, 115200, 16000000);
    }
    

    My code is as above, I added a SRAMRead and SRAMWrite to test writing into the RAM address.

  • Hello Jeun,

    The CMOS battery is connected to the DS1307 as well? My suspicion is that an incomplete transfer is causing the DS1307 to be unresponsive. A good idea would be to capture the first transaction after board reset every time the issue comes!!!

    Regards
    Amit
  • Hi Amit,
    Yes the CMOS is connected to DS1307. Every time the issue comes the only UART transaction is 00:00 and it gets stuck there unless i disturb the connections. Although logically, DS1307 was still counting even during the stuck. I am wondering why tiva could not read from the address when it was still counting.
  • Hello Jeunn,

    I am suspecting that the last transaction got interrupted causing DS1307 to not respond till the battery is not yanked. That is why the whole idea of debugging with the code with scope plot.

    Alternatively, another idea to test would be to call SysCtlReset in the while(1) loop after every transaction. If it does not fail then our earlier hypothesis gets more support. If it still fails, then clearly there is some other issue that needs to be debugged.

    Regards
    Amit
  • Thanks Amit. I will work on your suggested ideas.
  • Hello,

    Have you found any solution..? I referred to your code and facing the same issue.

    Please let me know if you have found any solution.

    Thank you.

  • Unfortunately not, I am sorry..

  • Why then have (two) "Verified Answers" been issued?

    That leads new users here - and disappoints.
  • im sorry cb1.. i verified the answers because i found that i obtained valuable help and assistance. I learned a lot from the process as well. However, I really couldn't figure out the issue after much attempts. Sorry for that.

  • No one seeks your sorrow - others may well come here - lured by the, "Verified Answer" - and become frustrated when they learn that's NOT the case!

    It may be that forum's "new" use of "Like" (at the base of each post) requires better (i.e. some) explanation. If you check "like" you've signaled your appreciation - yet NOT led others to expect a "real/verified" solution!
  • As fate has it - just before she departs for well-earned US holiday - our TI dedicated salesperson visited (w/fresh devices I might add)  and noted that, "All such posts marked w/"Verified Answer" are unlikely to be (further - or deeply) reviewed by TI staff.      

    Seems evident that serves as another reason to choose, "Like button" instead of, "Verify Answer!"    (i.e. that "premature/errant" Verify may "Stop" on-going efforts by those "most qualified" to resolve & present a real answer!)     Like button "applauds" - yet does not "kill" on-going solution efforts!

  • Alright, I will keep that in mind the next time I verify answers. 

  • Hello,

    I am having below situation:

    I am getting repeated numbers continuously. I have tried giving additional delay with SysCtlDelay(3000) after every while loop, but the situation remains the same. Why it is giving 37.165..?..Is there something else I should take care...?

  • Hello Anup

    How are you handling the Reset at the RTC slave device?

    Regards
    Amit
  • Hello Amit,

    I press the reset button on the TIVA C Launchpad or turn OFF and ON the 5V Power supply of DS1307.

    I have observed the sometimes clock and data pulses are coming (observed on scope) but values doesn't get updated on PuTTy.

    When I press the reset button on Launchpad it shows value of 00:00 and stays there then if I tweak the connections it will update few values and stops again.

    Below is my code:

    // This is part of revision 2.1.0.12573 of the Tiva Firmware Development Package.
    //
    //*****************************************************************************
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_i2c.h"
    #include "inc/hw_gpio.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/gpio.h"
    #include "driverlib/i2c.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    #include "utils/ustdlib.h"
    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/debug.h"
    #include "driverlib/fpu.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/rom.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    
    //*****************************************************************************
    //
    
    #define SLAVE_ADDRESS 0x68
    void ConfigureUART(void);
    void initI2C();
    unsigned char dec2bcd(unsigned char val);
    unsigned char bcd2dec(unsigned char val);
    
    unsigned int sec=3, min=59,tsec=6,tmin=10;
    
    //
    
    int main()
    {
    
    	unsigned char Last_Value = 1;
    	ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
    	                 SYSCTL_XTAL_16MHZ);
    	initI2C();
    	ConfigureUART();
    
    	I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
    	I2CMasterDataPut(I2C0_BASE,0);
    	I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    	while(I2CMasterBusy(I2C0_BASE));
        ROM_SysCtlDelay(3000);
    
        I2CMasterDataPut(I2C0_BASE,dec2bcd(sec));
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
        while(I2CMasterBusy(I2C0_BASE));
        ROM_SysCtlDelay(3000);
    
        I2CMasterDataPut(I2C0_BASE,dec2bcd(min));
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
        while(I2CMasterBusy(I2C0_BASE));
        ROM_SysCtlDelay(3000);
    
    
     while(1)
     {
    	 I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
    	 I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    	 while(I2CMasterBusy(I2C0_BASE));
    	 ROM_SysCtlDelay(3000);
    
    	 I2CMasterDataPut(I2C0_BASE,0x00);
    	// I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    	 while(I2CMasterBusy(I2C0_BASE));
    	 ROM_SysCtlDelay(3000);
    
    	 I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
    	 I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
    	 while(I2CMasterBusy(I2C0_BASE));
    	 ROM_SysCtlDelay(3000);
    
    	 sec = I2CMasterDataGet(I2C0_BASE);
    	 I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    	 while(I2CMasterBusy(I2C0_BASE));
    	 ROM_SysCtlDelay(3000);
    
    	 min = I2CMasterDataGet(I2C0_BASE);
    	 I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    	 while(I2CMasterBusy(I2C0_BASE));
    	 ROM_SysCtlDelay(3000);
    
    	 tsec = bcd2dec(sec)&0x7f;
    	 tmin = bcd2dec(min);
    	 if(Last_Value != tsec)
    	 {
    		 UARTprintf("%02d:%02d\n",tsec,tmin);
    		 Last_Value = tsec;
    		 tsec = 0;
    	 }
    
     }
    
    
    }
    
    
    void initI2C()
    {
    	ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);
    
    	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    
        GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
        ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
    
        ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL);
        ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA);
    
        I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
        ROM_SysCtlDelay(10000);
    }
    
    unsigned char dec2bcd(unsigned char val)
    {
    	return ((val/0xA*0x10)+(val%0xA));
    }
    
    unsigned char bcd2dec(unsigned char val)
    {
    	return ((val/0x10*0xA)+(val%0x10));
    }
    
    void ConfigureUART(void)
    {
    	    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    	    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    		ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    	    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    		ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
    
    	// Use the internal 16MHz oscillator as the UART clock source.
    		 UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    	// Initialize the UART for console I/O.
    		  UARTStdioConfig(0, 115200, 16000000);
    }
    

    I am not able to find that is it the problem of the hardware or the software. I have connected 10kOhm Pull up to SCL and SDA line.

    Please let me know if I am missing something.

    Thank you.

  • Hello Anup,

    To me it looks like that the RTC IC is not completing an existing transaction and hanging. Does the same happen with power disconnect as well? Did you check the RTC IC's power on specification?

    Regards
    Amit
  • Hello Amit,

    Thank you for prompt reply.
    Yes, when I disconnect the IC supply it hangs. I'll post after verifying the IC power spec.
  • Hello Amit,

    I have verified the spec sheet, didn't find anything that I am missing.

    I have soldered the crystal at the closest and commented the

    I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);

    line in while loop and the code started working. Now when I press reset button on Launchpad it hangs to 00:00 and after tweaking or pressing any connection it starts to count.

    In case 2 when I turn off the +5V supply of DS1307 (Launchpad is still powered up) it hangs on 37:165 .

    Please let me know if I should check something to find the issue.

    Thank you once again.

  • Hello Anup,

    If after completing a valid working transaction you use the API to call SysCtlReset and reset the MCU alone then does on the next run the issue occur.

    In case 2 if you turn off the power as well to both the devices, then power up 5V followed by TM4C power does it work?

    Regards
    Amit
  • Hello Amit,


    Regret for delayed reply.

    After reading some posts on internet about DS1307, I found out that it is very sensitive with power supply variations. So I placed some capacitors across IC and supply connector.

    I connected +5V supply to the pin Vbus , now if I turn off the +5V it shuts down DS1307 (it will go on battery mode) and Launchpad. But when I turn it on it starts perfectly without hanging and shows the time correctly on LCD.

    Problem is there with reset button but as in actual application it will not be there, I have stopped my efforts on that.

    Thank you so much for your suggestions.