Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

EEPROM - Code example
[EEPROM - Electrically Erasable Programmable read-Only Memory]

 #include <otEEprom.h>
 #include <otFPGA.h>
 #include <otPrintfLite.h>
 #include <otSpi.h>
 #include <otSerial.h>
 #include <otTimer.h>
 
 void setup()
 {
        // Setup the UART0 used by printf
        UartEnableSignals(UART0);
        UartSetBaud(UART0, 115200);
        // Init printf
        setStdout(UART0); // printf output is now directed on UART0
        // Init the FPGA, slave selection are made with it
        FpgaInit();
        // Setup a SPI channel
        SpiChannel(SA_EEPROM, 10, 0, 0);
 }
 
 void loop()
 {
        U32     ad;
        U8      v;
        
        DelayMs(2000);  // Wait terminal start ...
        
        // Select EEPROM channel
        SpiSet(SA_EEPROM);
        
        // Read the EEPROM contents, first 256 bytes *******************************
        printf("0000 ");
        for(ad = 0; ad < 256;)
        {
                v = EEread(&ad);
                printf("%02X ", v);
                if((ad % 16) == 0 && ad < 256) 
                        printf("\n%04X ", ad);
        }
        
        // Write *******************************************************************
        stdOutLF();
        printf("Write EEPROM\n");
        for(ad = 0; ad < 256;)
                EEwrite(&ad, ad);
        
        // Verify ******************************************************************
        printf("Verify EEPROM\n");
        printf("0000 ");
        for(ad = 0; ad < 256;)
        {
                v = EEread(&ad);
                if(v == (ad - 1))
                {
                        printf("%02X ", v);
                        if((ad % 16) == 0 && ad < 256) 
                                printf("\n%04X ", ad);
                }
                else
                {
                        printf("Compare error! Found %02X instead of %02X\n", v, ad);
                        IDLE;
                }
        }
        
        // Erase *******************************************************************
        stdOutLF();
        printf("Erase EEPROM\n");
        for(ad = 0; ad < 256;)
                EEwrite(&ad, 0xFF);
 
        printf("EEPROM test done.\n");
        DelayMs(200);  // Wait something ... hibernate stop all and kill buffer cache
        HIBE;
 }
footer
otStudio - Library Reference - (C) 2020-23 Officina Turini, All Rights Reserved
Document built with Doxygen 1.4.0