main
Default mainpagebasic-twi-eeprom-projectmain
Description Source Call Graph
Start Line: 190
int main()
{
    unsigned int i;
    Async async;
    unsigned int numErrors;

    PIO_Configure(pins, PIO_LISTSIZE(pins));
    TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
    printf("-- Basic TWI EEPROM Project %s --\n\r", SOFTPACK_VERSION);
    printf("-- %s\n\r", BOARD_NAME);
    printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

    // Configure TWI
    // In IRQ mode: to avoid problems, the priority of the TWI IRQ must be max.
    // In polling mode: try to disable all IRQs if possible.
    // (in this example it does not matter, there is only the TWI IRQ active)
    AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_TWI;
    TWI_ConfigureMaster(AT91C_BASE_TWI, TWCK, BOARD_MCK);
    TWID_Initialize(&twid, AT91C_BASE_TWI);
    AIC_ConfigureIT(AT91C_ID_TWI, 0, ISR_Twi);
    AIC_EnableIT(AT91C_ID_TWI);

    // Erase page #0 and #1
    memset(pData, 0, PAGE_SIZE);
    printf("-I- Filling page #0 with zeroes ...\n\r");
    TWID_Write(&twid, AT24C_ADDRESS, 0x0000, 2, pData, PAGE_SIZE, 0);

    // Wait at least 10 ms
    for (i=0; i < 1000000; i++);

    printf("-I- Filling page #1 with zeroes ...\n\r");
    TWID_Write(&twid, AT24C_ADDRESS, 0x0100, 2, pData, PAGE_SIZE, 0);

    // Wait at least 10 ms
    for (i=0; i < 1000000; i++);

    // Synchronous operation
    printf("-I- Read/write on page #0 (polling mode)\n\r");

    // Write checkerboard pattern in first page
    for (i=0; i < PAGE_SIZE; i++) {

        // Even
        if ((i & 1) == 0) {
        
            pData[i] = 0xA5;
        }
        // Odd
        else {

            pData[i] = 0x5A;
        }
    }
    TWID_Write(&twid, AT24C_ADDRESS, 0x0000, 2, pData, PAGE_SIZE, 0);

    // Wait at least 10 ms
    for (i=0; i < 1000000; i++);

    // Read back data
    memset(pData, 0, PAGE_SIZE);
    TWID_Read(&twid, AT24C_ADDRESS, 0x0000, 2, pData, PAGE_SIZE, 0);

    // Compare
    numErrors = 0;
    for (i=0; i < PAGE_SIZE; i++) {

        // Even
        if (((i & 1) == 0) && (pData[i] != 0xA5)) {

            printf("-E- Data mismatch at offset #%u: expected 0xA5, read 0x%02X\n\r", i, pData[i]);
            numErrors++;
        }
        // Odd
        else if (((i & 1) == 1) && (pData[i] != 0x5A)) {

            printf("-E- Data mismatch at offset #%u: expected 0x5A, read 0x%02X\n\r", i, pData[i]);
            numErrors++;
        }
    }
    printf("-I- %u comparison error(s) found\n\r", numErrors);

    // Asynchronous operation
    printf("-I- Read/write on page #1 (IRQ mode)\n\r");

    // Write checkerboard pattern in first page
    for (i=0; i < PAGE_SIZE; i++) {

        // Even
        if ((i & 1) == 0) {
        
            pData[i] = 0xA5;
        }
        // Odd
        else {

            pData[i] = 0x5A;
        }
    }
    memset(&async, 0, sizeof(async));
    async.callback = (void *) TestCallback;
    TWID_Write(&twid, AT24C_ADDRESS, 0x0100, 2, pData, PAGE_SIZE, &async);
    while (!ASYNC_IsFinished(&async));

    // Wait at least 10 ms
    for (i=0; i < 1000000; i++);

    // Read back data
    memset(pData, 0, PAGE_SIZE);
    memset(&async, 0, sizeof(async));
    async.callback = (void *) TestCallback;
    TWID_Read(&twid, AT24C_ADDRESS, 0x0100, 2, pData, PAGE_SIZE, &async);
    while (!ASYNC_IsFinished(&async));

    // Compare
    numErrors = 0;
    for (i=0; i < PAGE_SIZE; i++) {

        // Even
        if (((i & 1) == 0) && (pData[i] != 0xA5)) {

            printf("-E- Data mismatch at offset #%u: expected 0xA5, read 0x%02X\n\r", i, pData[i]);
            numErrors++;
        }
        // Odd
        else if (((i & 1) == 1) && (pData[i] != 0x5A)) {

            printf("-E- Data mismatch at offset #%u: expected 0x5A, read 0x%02X\n\r", i, pData[i]);
            numErrors++;
        }
    }
    printf("-I- %u comparison error(s) found\n\r", numErrors);

    return 0;
}