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.

I2C - Read and write the data particular Location from the RTC Chip.

Other Parts Discussed in Thread: TM4C123GH6PM, SYSBIOS

Hi all,

Chip No. :-  Tiva series TM4C123GH6PM.

                    TI-RTOS V2.01,

                     CCS Ver. 6.1.0.00104

  In  TI - RTOS  environment throw I2C Protocol how can i read  or write the RTC chip particular location, Any example Please...  

             I tried  I2C_transfer  function but i am not successful..

Best Regards,

 Murali 

 

  • Hi Murali,
    What RTC chip are you using? You said you unsuccessfully tried using I2C driver, how are you using it? Can I see the code that's not working?

    Thanks,
    Moses

  • The TI-RTOS Users' Guide has several I2C examples.

  • Hi Moses
    I am using DS1307 RTC chip i have to write /read RTC location 00H to 07H then RAM Location (08H to 3FH)also i can use

    Below Given UART echo example program, i modified Print the time in the PC terminal

    Thanks,
    Murali


    *
    * ======== uartecho.c ========
    */

    /* XDCtools Header files */
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>

    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>

    /* TI-RTOS Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    #include <ti/drivers/I2C.h>

    /* Example/Board Header files */
    #include "Board.h"

    #include <stdint.h>

    /*
    * ======== echoFxn ========
    * Task for this function is created statically. See the project's .cfg file.
    */
    Void echoFxn(UArg arg0, UArg arg1)
    {
    char input;//,data1;
    char test1 = 'A';
    UART_Handle uart;
    UART_Params uartParams;
    const char echoPrompt1[] = "\fEchoing characters:1\r\n";
    const char echoPrompt2[] = "\fEchoing characters:2\r\n";
    const char echoPrompt3[] = "\fEchoing characters:3\r\n";
    const char newline[] ="\r\n";

    /* Create a UART with data processing off. */
    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode = UART_RETURN_FULL;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.baudRate = 9600;
    uart = UART_open(Board_UART0, &uartParams);

    if (uart == NULL) {
    System_abort("Error opening the UART");
    }

    // UART_write(uart, echoPrompt1, sizeof(echoPrompt1));


    /* I2C Opening */
    I2C_Handle i2c;
    I2C_Params i2cParams;
    uint8_t rxBuffer[2],test,txBuffer[1],PrxBuffer[2];//,data;
    I2C_Transaction i2cTransaction;

    i2cTransaction.slaveAddress = DS1307_RTC;
    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_100kHz;
    i2c = I2C_open(Board_I2C2, &i2cParams);
    if (i2c == NULL) {
    System_abort("Error Initializing I2C\n");
    }
    else {
    System_printf("I2C Initialized!\n");
    }


    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = sizeof(rxBuffer);

    txBuffer[0]= 0x07;
    txBuffer[1]= 0x00;


    do{
    test = I2C_transfer(i2c, &i2cTransaction);
    } while(!test);

    /* HEX to ASCII conversion */
    PrxBuffer[0] = (0x30 ||(rxBuffer[0]&& 0x3F));
    PrxBuffer[1] = (0x3F && (rxBuffer[0] >> 4));
    // PrxBuffer[1] = 0x32 ;

    UART_write(uart, echoPrompt1, sizeof(echoPrompt1));
    UART_write(uart,&PrxBuffer[0],1);
    UART_write(uart, newline,sizeof(newline));
    Task_sleep(1000);
    // UART_write(uart, echoPrompt1, sizeof(echoPrompt1));
    // data = &rxBuffer[0];
    UART_write(uart, echoPrompt2, sizeof(echoPrompt2));
    UART_write(uart,&PrxBuffer[1],1);
    UART_write(uart, newline,sizeof(newline));
    // data1 = &rxBuffer[1];
    Task_sleep(1000);
    // UART_write(uart, echoPrompt3, sizeof(echoPrompt3));
    // UART_write(uart,&rxBuffer,1);

    // UART_write(uart, &test1, 1);

    /* Loop forever echoing */
    /* while (1) {
    UART_read(uart, &input, 1);
    UART_write(uart, &input, 1);

    }*/
    }

    /*
    * ======== main ========
    */
    int main(void)
    {
    /* Call board init functions */
    Board_initGeneral();
    Board_initGPIO();
    Board_initUART();
    Board_initI2C();

    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_ON);

    /* This example has logging and many other debug capabilities enabled */
    System_printf("This example does not attempt to minimize code or data "
    "footprint\n");
    System_flush();

    System_printf("Starting the UART Echo example\nSystem provider is set to "
    "SysMin. Halt the target to view any SysMin contents in "
    "ROV.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();

    /* Start BIOS */
    BIOS_start();

    return (0);
    }
  • HI TOM
    TI-RTOS for Beginners not sufficient.

    Thanks an Regards,
    Murali
  • HI Murali,
    First thing that stuck out to me is that you created txBuffer has txBuffer[1] but in your I2C transaction, you set the writeCount field as 2. Sending extra data can cause the I2C slave not to respond correctly. Besides that how are you seeing I2C_transfer fail? Is it hanging or is it returning an error?

    Regards,
    Moses
  • Hi Moses

      I2C_transfer function  not failing, But I try to read the data from 0x00 to 0x07 (Seconds, minute, hour, AM/PM Month and year data) location DS1307 RTC not updating in the variable (I am checking variable values in the CCS debugging window) and converting binary data to ASCII writing to UART and display in PC Terminal.   Next if I write 0x07 locations and change the SQRE WAVE out put frequency 1Hz to 32.768 KHz, this way I can validate I2C transaction write function  

                         I need two type coding examples Please..

                               1.  Writing to 0x00H to 0x07H Register Location.

                               2.  Reading  from  0x00h to 0x07H Location  

    Thank you & Best Regards,

    Murali

  • Can you check  your HEX -> ASCII conversion. I think you want this:

        /* HEX to ASCII conversion */
        PrxBuffer[0] = (0x30 + (rxBuffer[0] & 0x3F));
        PrxBuffer[1] = (0x3F & (rxBuffer[0] >> 4));

    Does the RTC I2C peripheral state that you should write 2 bytes to the internal address register pointer? Can't you read all of the registers all at once?

    i2cTransaction.slaveAddress = DS1307_RTC;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 8; //Registers 0x00-0x07
    
    txBuffer[0] = 0x00;
    
    while (youWantData == true)  {
    
        i2cComplete = I2C_transfer(.., &i2cTransaction);
        if (i2cComplete == false) {
            System_printf("NACK from RTC\n");
        }
    
        rxBuffer[0] has Register 0;
        rxBuffer[1] has Register 1;
        ...
        rxBuffer[7] has Register 7;
    
        Task_sleep(yourDelay);
    }

    This way you should have a cohesive block of data that wasn't updated by the RTC between individual I2C_transfers.

  • Hi Tom Kopriva,

      I successful completed  HEX to ASCII conversion and RTC Read and write Task's .

    Thank you very much.....

     Best regards,

     Murali.

  • Are the readings from the RTC OK now?
  • Hi Tom.

        Yes ... Now readings are OK...

    Best Regards,

     Murali