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.

CCS/TM4C123GH6PM: variable data storing in EEPROM memory

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

Hello, I am presently working on EEPROM module of TM4C123GXL launchpad and i am having some trouble in understanding EEPROM.

The issue is how to declare a variable and the variable will be updated again and again in the program. If the power is removed and connected back and if i am printing that variable content means it should print the updated value not the first  initialized value.

So please please help to get resolved with this issue.

Thank you.

  • Hi,

     The below example code shows how to program the EEprom which can also be found in section 9.3 of the TivaWare Peripheral Driver Library user's guide. If you want to print the updated value you will need to first program the data using EEpromProgram() to store the data in EEprom and then read it out using EEpromRead().

    uint32_t ui32EEPROMInit;
    uint32_t pui32Data[2];
    uint32_t pui32Read[2];
    //
    // Enable the EEPROM module.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0);
    //
    // Wait for the EEPROM module to be ready.
    //
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_EEPROM0))
    {
    }
    //
    // Wait for the EEPROM Initialization to complete
    //
    ui32EEPROMInit = EEPROMInit();
    //
    // Check if the EEPROM Initialization returned an error
    // and inform the application
    //
    if(ui32EEPROMInit != EEPROM_INIT_OK)
    {
    while(1)
    {
    }
    }
    //
    // Program some data into the EEPROM at address 0x400.
    //
    pui32Data[0] = 0x12345678;
    pui32Data[1] = 0x56789abc;
    EEPROMProgram(pui32Data, 0x400, sizeof(pui32Data));
    //
    // Read it back.
    //
    EEPROMRead(pui32Read, 0x400, sizeof(pui32Read));

  • Thank you Charles, the program which you posted i already executed that and there no issue with that code. The is 

    int b=10,a;

    void main()
    {

    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_4MA,GPIO_PIN_TYPE_STD_WPU);
    GPIOPinConfigure(GPIO_PA0_U0RX); // PA0 IS CONFIGURED TO UART RX
    GPIOPinConfigure(GPIO_PA1_U0TX); // PA1 IS CONFIGURED TO UART TX
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    EEPROMInit();
    EEPROMMassErase();
    EEPROMProgram(b, 0x0, sizeof(b));

    while (1)

    {

    if (!GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4))

    {

    EEPROMRead(a, 0x0, sizeof(a));
    a=a-2;
    UARTprintf("\n Remaining balance is %i\n",a);
    EEPROMProgram(a, 0x0, sizeof(a));
    SysCtlDelay(20000);
    }
    }
    }

    In the above code if i press switch once the value is decremented by 2 and printed on UART terminal next time if i press again same thing so there is no problem with that. But lets consider i have pressed switch two times and the value becomes 6 and it is printed on terminal now if i remove the power and connect it back and if i press the switch one more time means i should get 4 on terminal because i am programming EEPROM but i am getting 8 not 4. Why it is happening so. Is there any way to get the output which i am expecting.

    Thank you.

  • How did you know the second program command succeeds when you call EEPROMProgram(a, 0x0, sizeof(a)). The EEPROMProgram() will return 0 when success or non-0 value on failure. You should check for the return value on any call to EEPROMProgram as well as any API calls that have return value.
  • The thing is not the API here....the above program i have just given has example it is untested code. My doubt is how we can recover the updated data when programmed in EEPROM if power goes off and returned back. 

    In the above example we can see that the value of 'b' will be updated to initial value 10 when the power goes and returned back because the flash will be executed from the beginning. So the same thing we are programming to EEPROM so the result we will get is first decremented value only i.e, 8, i won't get the value which was there before the power goes(decremented by 2).

    So please post one example that explains EEPROM other than the Tivaware example.

    Thank you.

  • When you lose power and later power back, you will rerun the same code again. You will execute the EEPROMProgram(b, 0x0, sizeof(b)) first which will overwrite what was previously stored in the EEprom which is a 6 if you had pressed the switch twiced before the power went off. You need to add some intelligence into the code. For example, you can check the RESC register to see if there is reset due to POR. Base on this information, you can choose to read the EEprom content directly without first writing a 10 to it.
  • Hello Charles,

    As you to check the status of the read i have used one API SysCtlResetCauseGet() and i am printing the return value of that API by storing it in a 32 bit integer variable. After uploading the code for first time i am getting the status as '19'. If i remove the power and connect it back i am getting status as '3'. So in the program  i am using the condition 

    v=SysCtlResetCauseGet();
    UARTprintf("reset due to %i",v);
    if(v==19)
    {
    EEPROMProgram(amount, 0x0, sizeof(amount));

    }

    After uploading the code for first time the 'if' block is executing. If i remove the power and connect it back means 'if' is not executing so EEPROM again it is not initialized with the base value i.e,10.

    But the EEPROM is not retaining the value before power went off. It is initializing with the value 0.

    What is the reason behind this? How to retain the EEPROM content? Any API or any settings are there to retain the EEPROM?

    Thank You.

      

     

  • Which version of the silicon do you have? Not sure if you are hitting one of the errata. Please check the errata document for some of the EEprom issues.

    When you program the 'amount' the first time with v=19, can you first read it back to see if it is programmed successfully?
  • Yes when v=19 , the EEPROMProgram() is loading the data into EEPROM memory means it is working properly.  There is problem in the EEPROM API's all are working properly i checked all of them.

    But if i remove the power the data in the EEEPROM memory is erasing. As we know EEPROM is a non volatile memory it should retain the data but here it is not happening like that. If the launchpad is powered up second time means i can see only 0's in the EEPROM memory.

  • Can you show me your complete code.
  • #include<stdint.h>
    #include<stdbool.h>
    #include"inc/hw_memmap.h"
    #include"driverlib/gpio.h"
    #include"driverlib/pin_map.h"
    #include"driverlib/sysctl.h"
    #include"driverlib/uart.h"
    #include "inc/tm4c123gh6pm.h"
    #include "utils/uartstdio.h"
    #include "driverlib/debug.h"
    #include "driverlib/eeprom.h"

    #define GPIO_PA0_U0RX 0x00000001 // UART PIN ADDRESS FOR UART RX
    #define GPIO_PA1_U0TX 0x00000401 // UART PIN ADDRESS FOR UART TX
    #define GPIO_PB0_U1RX 0x00010001

    unsigned char a[]="00002A5AB9C9";

    int i=0;
    uint32_t *amount,*bal;
    uint32_t am=25 , b;

    int main()
    {
    uint32_t v;
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
    // ENABLE PERIPHERAL UART 0
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0);

    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);

    GPIOPinConfigure(GPIO_PA0_U0RX); // PA0 IS CONFIGURED TO UART RX
    GPIOPinConfigure(GPIO_PA1_U0TX); // PA1 IS CONFIGURED TO UART TX
    GPIOPinConfigure(GPIO_PB0_U1RX); // PB0 IS CONFIGURED TO UART RX

    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0);
    amount = &am;
    bal=&b;
    EEPROMInit();
    EEPROMMassErase();

    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_4MA,GPIO_PIN_TYPE_STD_WPU);

    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    UARTStdioConfig(0, 9600, 16000000);
    UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), 9600,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
    UARTprintf(" Welcome to RFID demo\n");

    IntEnable(INT_UART1);
    UARTIntEnable(UART1_BASE, UART_INT_RX|UART_INT_RT);
    IntMasterEnable();

    v=SysCtlResetCauseGet();
    UARTprintf("reset due to %i",v);
    if(v==19)
    {
    EEPROMProgram(amount, 0x0, sizeof(amount));
    GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_3,0X08);
    SysCtlDelay(1333333);
    GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_3,0X00);
    }
    while(1);
    }

    void uartint()
    {
    unsigned char comp[12];
    unsigned int m=0,n=0;
    UARTIntClear(UART1_BASE, UART_INT_RX);
    comp[i]= UARTCharGet(UART1_BASE);
    UARTCharPut(UART0_BASE,comp[i]);
    i++;

    if(i==12)
    {
    for(i=0;i<12;i++)
    {
    if(a[i]==comp[i])
    m++;
    else
    m=0;
    }
    if(m>10)
    {
    GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1,0X02);
    SysCtlDelay(20000);
    //bal=&b;
    EEPROMRead(bal, 0x0, sizeof(bal));
    *bal=*bal-2;
    //b=b-2;
    UARTprintf("\nRemaining balance is %i\n",*bal);
    GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1,0X00);
    EEPROMProgram(bal, 0x0, sizeof(bal));

    }
    else
    {
    GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1,0X00);
    SysCtlDelay(20000);
    UARTprintf("\nInvalied Card\n");
    }
    i=0;
    }

    }

    This is the code in this i am using an RFID reader and card. After uploading the code if i scan card once means it will be deducted by 2,once again if i scan again it will be deducted by 2 and so on. The updated value will be stored in EEPROM.If i remove the power and connect it back and scan the card means it should print the "updated value - 2" but it is printing  '-3'. What is the problem in the code??

  • I wrote this small program that might help. This program requires to use another EEprom location as a flag to determine when to initialize the data to 10 or when to resume decrementing from where it left off after power lost. You must write another program that simply initialize the flag to 0 first. The flag uses the first location of the EEprom. The application data where the value will be decremented each time the SW1 is pressed is stored at 0x400. After you first run this program, depending on the flag value after the SW2 is pressed, it will restart with a value of 10 to program or resume from where it left off. To test it, you will do:

    1. Create another program to first program the first EEprom location (the flag) to zero.

    2. Run this below program.

    3. Press the SW2 switch and it will initialize the application value to 10

    4. Press the SW1 and it will decrement by 1 each time you press.

    5. You can try to unplug the power before the application value reaches 0. For example, when the application value is 8 you can unplug the power.

    6. Reconnect the power back

    7. Press SW1 again. You should see the application value to be programmed will start from 7 and then downward.

    int
    main(void)
    {
        uint32_t ui32EEPROMInit;
        uint32_t flag=0x12345678;
        uint32_t pui32Data;
        uint32_t pui32Read;
        //
        // Enable the EEPROM module.
        //
        SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
        GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
        GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
    
        HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
        HWREG(GPIO_PORTF_BASE+GPIO_O_CR) |= GPIO_PIN_0;
        GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0);
        GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
        //
        // Wait for the EEPROM module to be ready.
        //
        while(!SysCtlPeripheralReady(SYSCTL_PERIPH_EEPROM0))
        {
        }
    
        ConfigureUART();
    
        //!
        //
        UARTprintf("EEprom testing!\n");
    
        //
        // Wait for the EEPROM Initialization to complete
        //
        ui32EEPROMInit = EEPROMInit();
        //
        // Check if the EEPROM Initialization returned an error
        // and inform the application
        //
        if(ui32EEPROMInit != EEPROM_INIT_OK)
        {
            while(1)
            {
            }
        }
    
        EEPROMRead(&flag, 0x0, 4);
    
        while (1){
            if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0) == 0x0){ //If SW2 is pressed and flag equal 0,
    
                if (flag == 0) { // initialize the flag and reset pui32Data to 10
                    pui32Data = 10;
                    flag = 0x12345678;
                    EEPROMProgram(&flag, 0x0, 4);
                    EEPROMRead(&flag, 0x0, 4);
    
                    EEPROMProgram(&pui32Data, 0x400, 4);
                    EEPROMRead(&pui32Read, 0x400, 4);
                    UARTprintf("\nInitialize the data to 10\n");
    
    
                } else if (flag == 0x12345678) { // if flag was not clear then it may indicate a power loss
                    EEPROMRead(&pui32Read, 0x400, 4);
                    pui32Data = pui32Read;       // Resume EEprom programming from it left off before
                    UARTprintf("\nRestart EEprom program from where it left off\n");
    
    
                } else {
                    while (1);
                }
                break;
    
            }
        }
        SysCtlDelay(1000000);
    
        do {
    
            while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4));
    
    //        if (flag == 0x12345678) {
                UARTprintf("\nRemaining balance is %i\n",pui32Read);
                pui32Data--;
                EEPROMProgram(&pui32Data, 0x400, 4);
                EEPROMRead(&pui32Read, 0x400, 4);
    
    
    //        }
    
            SysCtlDelay(1000000);
    
    
        } while (pui32Data > 0);
    
        if (flag == 0x12345678) {
            flag = 0;
            EEPROMProgram(&flag, 0x0, 4);
        }
    
        while (1);
    }

  • Thank you Charles....I got the output.