SAM3S8/SD8 Software Package: examples/twi_eeprom/main.c Source File

SAM3S8/SD8 Software Package 1.2

# main.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------------
    00002  *         ATMEL Microcontroller Software Support
    00003  * ----------------------------------------------------------------------------
    00004  * Copyright (c) 2011, Atmel Corporation
    00005  *
    00006  * All rights reserved.
    00007  *
    00008  * Redistribution and use in source and binary forms, with or without
    00009  * modification, are permitted provided that the following conditions are met:
    00010  *
    00011  * - Redistributions of source code must retain the above copyright notice,
    00012  * this list of conditions and the disclaimer below.
    00013  *
    00014  * Atmel's name may not be used to endorse or promote products derived from
    00015  * this software without specific prior written permission.
    00016  *
    00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
    00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
    00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
    00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00027  * ----------------------------------------------------------------------------
    00028  */
    00029
    00030 /**
    00031  * \page twi_eeprom TWI EEPROM Example
    00032  *
    00033  * \section Purpose
    00034  *
    00035  * This basic example program demonstrates how to use the TWI peripheral
    00036  * to access an external serial EEPROM chip.
    00037  *
    00038  * \section Requirements
    00039
    00040  *  This package can be used with SAM3S evaluation kits 2. An external USART_DB
    00041  *  board is needed for test.
    00042  *  Please connect the USART_DB board to SAM3S-EK board with following table:
    00043  *  - <b>SAM3S-EK  -- USART_DB (with JP1 closed)</b>
    00044  *   - 3V3         -- 3V3
    00045  *   - TWD1(PB4)   -- TWD0
    00046  *   - TWCK1(PB5)  -- TWCK0
    00047  *   - GND         -- GND
    00048  *
    00049  * By default, PB4 and PB5 are SWJ-DP pins, it can be used as a pin for TWI1
    00050  * peripheral in the end application. Mode selection between SWJ-DP (System IO mode)
    00051  * and general IO mode is performed through the AHB Matrix CCFG_SYSIO.
    00052  *
    00053  * <b>Note:</b>This example can only run out of flash with JTAG unconnected
    00054  * because of PB4 and PB5.
    00055  *
    00056  * \section Description
    00057  *
    00058  * The code can be roughly broken down as follows:
    00059  * <ul>
    00060  * <li>Configure TWI pins.</li>
    00061  * <li>Enable TWI peripheral clock.</li>
    00062  * <li>Configure TWI clock.</li>
    00063  * <li>Initialize TWI as twi master.</li>
    00064  * <li>TWI interrupt handler.</li>
    00065  * <li>The main function, which implements the program behavior.</li>
    00066  * <ol>
    00067  * <li>Sets the first and second page of the EEPROM to all zeroes.</li>
    00068  * <li>Writes pattern in page 0. </li>
    00069  * <li>Reads back data in page 0 and compare with original pattern (polling). </li>
    00070  * <li>Writes pattern in page 1. </li>
    00071  * <li>Reads back data in page 1 and compare with original pattern (interrupts). </li>
    00072  * </ol>
    00073  * </ul>
    00074  *
    00075  * \section Usage
    00076  *
    00077  * -# Build the program and download it inside the evaluation board. Please
    00078  *    refer to the
    00079  *    <a href="http://www.atmel.com/dyn/resources/prod_documents/6421B.pdf">
    00080  *    SAM-BA User Guide</a>, the
    00081  *    <a href="http://www.atmel.com/dyn/resources/prod_documents/doc6310.pdf">
    00082  *    GNU-Based Software Development</a> application note or to the
    00083  *    <a href="http://www.iar.com/website1/1.0.1.0/78/1/">
    00084  *    IAR EWARM User and reference guides</a>, depending on your chosen solution.
    00085  * -# On the computer, open and configure a terminal application
    00086  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
    00087  *   - 115200 bauds
    00088  *   - 8 bits of data
    00089  *   - No parity
    00090  *   - 1 stop bit
    00091  *   - No flow control
    00092  * -# Start the application.
    00093  * -# In the terminal window, the following text should appear:
    00094  *    \code
    00095  *     -- TWI EEPROM Example xxx --
    00096  *     -- xxxxxx-xx
    00097  *     -- Compiled: xxx xx xxxx xx:xx:xx --
    00098  *    \endcode
    00099  * -# The following traces detail operations on the EEPROM, displaying success
    00100  *    or error messages depending on the results of the commands.
    00101  *
    00102  * \section References
    00103  * - twi_eeprom/main.c
    00104  * - twi.c
    00105  * - twid.c
    00106  */
    00107
    00108 /**
    00109  * \file
    00110  *
    00111  * This file contains all the specific code for the twi eeprom example.
    00112  */
    00113
    00114 /*----------------------------------------------------------------------------
    00115  *        Headers
    00116  *----------------------------------------------------------------------------*/
    00117
    00118 #include "board.h"
    00119
    00120 #include <stdint.h>
    00121 #include <stdio.h>
    00122 #include <stdarg.h>
    00123 #include <string.h>
    00124 #include <assert.h>
    00125
    00126 /*----------------------------------------------------------------------------
    00127  *        Local definitions
    00128  *----------------------------------------------------------------------------*/
    00129
    00130 /** TWI clock frequency in Hz. */
    00131 #define TWCK            400000
    00132
    00133 /** Slave address of AT24C chips.*/
    00134 #define AT24C_ADDRESS   0x50
    00135
    00136 /** Page size of an AT24C512 chip (in bytes)*/
    00137 #define PAGE_SIZE       64
    00138
    00139
    00140 /*----------------------------------------------------------------------------
    00141  *        Local variables
    00142  *----------------------------------------------------------------------------*/
    00143 /** Pio pins to configure. */
    00144 static const Pin pins[] = {BOARD_PINS_TWI_EEPROM};
    00145
    00146 /** TWI driver instance.*/
    00147 static Twid twid;
    00148
    00149 /** Page buffer.*/
    00150 static uint8_t pData[PAGE_SIZE];
    00151
    00152 /*----------------------------------------------------------------------------
    00153  *        Local functions
    00154  *----------------------------------------------------------------------------*/
    00155 /**
    00156  * \brief TWI interrupt handler. Forwards the interrupt to the TWI driver handler.
    00157  */
    00158 void TWI1_IrqHandler( void )
    00159 {
    00160     TWID_Handler( &twid ) ;
    00161 }
    00162
    00163 /**
    00164  * \brief Dummy callback, to test asynchronous transfer modes.
    00165  */
    00166 static void TestCallback( void )
    00167 {
    00168     printf( "-I- Callback fired !\n\r" ) ;
    00169 }
    00170
    00171 /*----------------------------------------------------------------------------
    00172  *        Global functions
    00173  *----------------------------------------------------------------------------*/
    00174
    00175 /**
    00176  * \brief Application entry point for TWI eeprom example.
    00177  *
    00178  * \return Unused (ANSI-C compatibility).
    00179  */
    00180
    00181 extern int main( void )
    00182 {
    00183     uint32_t i;
    00184     volatile uint32_t delay;
    00185     Async async;
    00186     uint32_t numErrors;
    00187
    00188     /* Disable watchdog */
    00189     WDT_Disable( WDT ) ;
    00190
    00191     /* Output example information */
    00192     printf("-- TWI EEPROM Example %s --\n\r", SOFTPACK_VERSION);
    00193     printf("-- %s\n\r", BOARD_NAME);
    00194     printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
    00195
    00196     /* Select PB4 and PB5 as peripheral function */
    00197     printf("Press any key to continue (Make sure your JTAG is not connected now)\n\r");
    00198     UART_GetChar();
    00199     printf("Select PB4 and PB5 as peripheral function instead of SWJ-DP\n\r");
    00200     REG_CCFG_SYSIO = CCFG_SYSIO_SYSIO4 | CCFG_SYSIO_SYSIO5;
    00201
    00202     /* Configure TWI pins. */
    00203     PIO_Configure(pins, PIO_LISTSIZE(pins));
    00204
    00205     /* Enable TWI peripheral clock */
    00206     PMC_EnablePeripheral(BOARD_ID_TWI_EEPROM);
    00207
    00208     /* Configure TWI */
    00209     TWI_ConfigureMaster(BOARD_BASE_TWI_EEPROM, TWCK, BOARD_MCK);
    00210     TWID_Initialize(&twid, BOARD_BASE_TWI_EEPROM);
    00211
    00212     /* Configure TWI interrupts */
    00213     NVIC_DisableIRQ(TWI1_IRQn);
    00214     NVIC_ClearPendingIRQ(TWI1_IRQn);
    00215     NVIC_SetPriority(TWI1_IRQn, 0);
    00216     NVIC_EnableIRQ(TWI1_IRQn);
    00217
    00218     /* Erase page #0 and #1 */
    00219     memset(pData, 0, PAGE_SIZE);
    00220     printf("-I- Filling page #0 with zeroes ...\n\r");
    00221     TWID_Write(&twid, AT24C_ADDRESS, 0x0000, 2, pData, PAGE_SIZE, 0);
    00222
    00223     /* Wait at least 10 ms */
    00224     for (delay=0; delay < 1000000; delay++);
    00225
    00226     printf("-I- Filling page #1 with zeroes ...\n\r");
    00227     TWID_Write(&twid, AT24C_ADDRESS, 0x0100, 2, pData, PAGE_SIZE, 0);
    00228
    00229     /* Wait at least 10 ms */
    00230     for (delay=0; delay < 1000000; delay++);
    00231
    00232     /* Synchronous operation */
    00233     printf("-I- Read/write on page #0 (polling mode)\n\r");
    00234
    00235     /* Write checkerboard pattern in first page */
    00236     for ( i=0 ; i < PAGE_SIZE ; i++ )
    00237     {
    00238         /* Even*/
    00239         if ( (i & 1) == 0 )
    00240         {
    00241             pData[i] = 0xA5;
    00242         }
    00243         /* Odd */
    00244         else
    00245         {
    00246             pData[i] = 0x5A;
    00247         }
    00248     }
    00249     TWID_Write(&twid, AT24C_ADDRESS, 0x0000, 2, pData, PAGE_SIZE, 0);
    00250
    00251     /* Wait at least 10 ms */
    00252     for (delay=0; delay < 1000000; delay++);
    00253
    00254     /* Read back data */
    00255     memset(pData, 0, PAGE_SIZE);
    00256     TWID_Read(&twid, AT24C_ADDRESS, 0x0000, 2, pData, PAGE_SIZE, 0);
    00257
    00258     /* Compare */
    00259     numErrors = 0;
    00260     for (i=0; i < PAGE_SIZE; i++)
    00261     {
    00262         /* Even */
    00263         if ( ((i & 1) == 0) && (pData[i] != 0xA5) )
    00264         {
    00265             printf( "-E- Data mismatch at offset #%d: expected 0xA5, read 0x%02X\n\r", (int)i, pData[i] ) ;
    00266             numErrors++ ;
    00267         }
    00268         /* Odd */
    00269         else
    00270         {
    00271             if ( ((i & 1) == 1) && (pData[i] != 0x5A) )
    00272             {
    00273                 printf( "-E- Data mismatch at offset #%d: expected 0x5A, read 0x%02X\n\r", (int)i, pData[i] ) ;
    00274                 numErrors++;
    00275             }
    00276         }
    00277     }
    00278     printf("-I- %d comparison error(s) found\n\r", (int)numErrors);
    00279
    00280     /* Asynchronous operation */
    00281     printf("-I- Read/write on page #1 (IRQ mode)\n\r");
    00282
    00283     /* Write checkerboard pattern in first page */
    00284     for ( i=0 ; i < PAGE_SIZE ; i++ )
    00285     {
    00286         /* Even */
    00287         if ( (i & 1) == 0 )
    00288         {
    00289             pData[i] = 0xA5;
    00290         }
    00291         /* Odd */
    00292         else
    00293         {
    00294             pData[i] = 0x5A;
    00295         }
    00296     }
    00297
    00298     memset(&async, 0, sizeof(async));
    00299     async.callback = (void *) TestCallback;
    00300     TWID_Write(&twid, AT24C_ADDRESS, 0x0100, 2, pData, PAGE_SIZE, &async);
    00301     while (!ASYNC_IsFinished(&async));
    00302
    00303     /* Wait at least 10 ms */
    00304     for (delay=0; delay < 1000000; delay++);
    00305
    00306     /* Read back data */
    00307     memset(pData, 0, PAGE_SIZE);
    00308     memset(&async, 0, sizeof(async));
    00309     async.callback = (void *) TestCallback;
    00310     TWID_Read(&twid, AT24C_ADDRESS, 0x0100, 2, pData, PAGE_SIZE, &async);
    00311     while ( !ASYNC_IsFinished( &async ) ) ;
    00312
    00313     /* Compare */
    00314     numErrors = 0 ;
    00315     for ( i=0 ; i < PAGE_SIZE ; i++ )
    00316     {
    00317         /* Even */
    00318         if ( ((i & 1) == 0) && (pData[i] != 0xA5) )
    00319         {
    00320             printf( "-E- Data mismatch at offset #%d : expected 0xA5, read 0x%02X\n\r", (int)i, pData[i] ) ;
    00321             numErrors++ ;
    00322         }
    00323         /* Odd */
    00324         else
    00325         {
    00326             if ( ((i & 1) == 1) && (pData[i] != 0x5A) )
    00327             {
    00328                 printf( "-E- Data mismatch at offset #%d : expected 0x5A, read 0x%02X\n\r", (int)i, pData[i] ) ;
    00329                 numErrors++ ;
    00330             }
    00331         }
    00332     }
    00333     printf( "-I- %d comparison error(s) found\n\r", (int)numErrors ) ;
    00334
    00335     return 0 ;
    00336 }

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines