SAMV71 Xplained Ultra Software Package 1.5

main.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
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 SAMV71 Xplained Ultra board or SAME70 Xplained board.
00041  *
00042  * \section Description
00043  *
00044  * The code can be roughly broken down as follows:
00045  * <ul>
00046  * <li>Configure TWI pins.</li>
00047  * <li>Enable TWI peripheral clock.</li>
00048  * <li>Configure TWI clock.</li>
00049  * <li>Initialize TWI as twi master.</li>
00050  * <li>TWI interrupt handler.</li>
00051  * <li>The main function, which implements the program behaviour.</li>
00052  * <ol>
00053  * <li>Set the first and second page of the EEPROM to all zeroes.</li>
00054  * <li>Write pattern in page 0. </li>
00055  * <li>Read back data in page 0 and compare with original pattern (polling).</li>
00056  * <li>Write pattern in page 1. </li>
00057  * <li>Read back data in page 1 and compare with original pattern (interrupts).</li>
00058  * </ol>
00059  * </ul>
00060  *
00061  * \section Usage
00062  *
00063  *  -# Build the program and download it inside the board.
00064  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00065  * -# On the computer, open and configure a terminal application
00066  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00067  *   - 115200 baud rate
00068  *   - 8 bits of data
00069  *   - No parity
00070  *   - 1 stop bit
00071  *   - No flow control
00072  * -# Start the application.
00073  * -# In the terminal window, the following text should appear:
00074  *    \code
00075  *     -- TWI EEPROM Example xxx --
00076  *     -- SAMxxxxx-xx
00077  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00078  *    \endcode
00079  * -# The following traces detail operations on the EEPROM, displaying success
00080  *    or error messages depending on the results of the commands.
00081  *
00082  * \section References
00083  * - twi_eeprom/main.c
00084  * - twi.c
00085  * - twid.c
00086  */
00087 
00088 /**
00089  * \file
00090  *
00091  * This file contains all the specific code for the twi eeprom example.
00092  */
00093 
00094 /*----------------------------------------------------------------------------
00095  *        Headers
00096  *----------------------------------------------------------------------------*/
00097 
00098 #include "board.h"
00099 
00100 #include <stdint.h>
00101 #include <stdio.h>
00102 #include <stdarg.h>
00103 #include <string.h>
00104 #include <assert.h>
00105 
00106 /*----------------------------------------------------------------------------
00107  *        Local definitions
00108  *----------------------------------------------------------------------------*/
00109 
00110 /** TWI clock frequency in Hz. */
00111 #define TWCK            400000
00112 
00113 /** Slave address of twi_eeprom example.*/
00114 #define AT24MAC_ADDRESS         0x57
00115 #define AT24MAC_SERIAL_NUM_ADD  0x5F
00116 
00117 /** Page size of an AT24MAC402 chip (in bytes)*/
00118 #define PAGE_SIZE       16
00119 
00120 /** Page numbers of an AT24MAC402 chip */
00121 #define EEPROM_PAGES    16
00122 
00123 /** EEPROM Pins definition */
00124 #define BOARD_PINS_TWI_EEPROM PINS_TWI0
00125 
00126 /** TWI0 peripheral ID for EEPROM device*/
00127 #define BOARD_ID_TWI_EEPROM   ID_TWIHS0
00128 
00129 /** TWI0 base address for EEPROM device */
00130 #define BOARD_BASE_TWI_EEPROM TWIHS0
00131 
00132 /*----------------------------------------------------------------------------
00133  *        Local variables
00134  *----------------------------------------------------------------------------*/
00135 /** PIO pins to configure. */
00136 static const Pin pins[] = BOARD_PINS_TWI_EEPROM;
00137 static TwihsDma twi_dma;
00138 
00139 /** TWI driver instance.*/
00140 static Twid twid;
00141 
00142 /** Page buffer.*/
00143 COMPILER_ALIGNED(32) static uint8_t pData[PAGE_SIZE];
00144 
00145 static uint8_t CallBackFired = 0;
00146 
00147 /*----------------------------------------------------------------------------
00148  *        Local functions
00149  *----------------------------------------------------------------------------*/
00150 /**
00151  * \brief TWI interrupt handler. Forwards the interrupt to the TWI driver handler.
00152  */
00153 void TWIHS0_Handler(void)
00154 {
00155     TWID_Handler(&twid);
00156 }
00157 
00158 /**
00159  * DMA interrupt handler.
00160  */
00161 void XDMAC_Handler(void)
00162 {
00163     XDMAD_Handler(twi_dma.pTwiDma);
00164 }
00165 
00166 /**
00167  * \brief Dummy callback, to test asynchronous transfer modes.
00168  */
00169 static void TestCallback(void)
00170 {
00171     CallBackFired++;
00172 }
00173 
00174 static void _fillBuffer(uint8_t *pbuff)
00175 {
00176     uint32_t i;
00177 
00178     /* Write checkerboard pattern in first page */
00179     for (i = 0; i < PAGE_SIZE; i++) {
00180         /* Even*/
00181         if ((i & 1) == 0)
00182             pbuff[i] = 0xA5;
00183         /* Odd */
00184         else
00185             pbuff[i] = 0x5A;
00186     }
00187 }
00188 
00189 static void _checkReadBuffer(uint8_t *pBuff)
00190 {
00191     uint16_t i, NoError;
00192 
00193     NoError = 0;
00194     for (i = 0; i < PAGE_SIZE; i++) {
00195         /* Even */
00196         if (((i & 1) == 0) && (pBuff[i] != 0xA5)) {
00197             printf( "-E- Data mismatch at offset #%u: expected 0xA5, \
00198                 read 0x%02x\n\r", (unsigned int)i, (unsigned int)pData[i] );
00199             NoError++;
00200         }
00201         /* Odd */
00202         else {
00203             if (((i & 1) == 1) && (pBuff[i] != 0x5A)) {
00204                 printf( "-E- Data mismatch at offset #%u: expected 0x5A, read\
00205                     0x%02x\n\r", (unsigned int)i, (unsigned int)pData[i]);
00206                 NoError++;
00207             }
00208         }
00209     }
00210     printf("-I- %u comparison error(s) found \r", (unsigned int)NoError);
00211 }
00212 
00213 /*----------------------------------------------------------------------------
00214  *        Global functions
00215  *----------------------------------------------------------------------------*/
00216 
00217 /**
00218  * \brief Application entry point for TWI EEPROM example.
00219  *
00220  * \return Unused (ANSI-C compatibility).
00221  */
00222 
00223 extern int main( void )
00224 {
00225     uint8_t i;
00226     uint8_t SNo[16];
00227     Async async;
00228 
00229     /* Disable watchdog */
00230     WDT_Disable(WDT);
00231 
00232     /* Enable I and D cache */
00233     SCB_EnableICache();
00234     SCB_EnableDCache();
00235 
00236     CallBackFired = 0;
00237 
00238     /* Output example information */
00239     printf("-- TWI EEPROM Example %s --\n\r", SOFTPACK_VERSION);
00240     printf("-- %s\n\r", BOARD_NAME);
00241     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00242 
00243     TimeTick_Configure();
00244 
00245     /* Configure TWI pins. */
00246     PIO_Configure(pins, PIO_LISTSIZE(pins));
00247     PMC_EnablePeripheral(BOARD_ID_TWI_EEPROM);
00248     /* Configure TWI */
00249 
00250     twi_dma.pTwid = malloc(sizeof(Twid));
00251     twi_dma.pTwiDma = malloc(sizeof(sXdmad));
00252 
00253     TWI_ConfigureMaster(BOARD_BASE_TWI_EEPROM, TWCK, BOARD_MCK);
00254     TWID_Initialize(&twid, BOARD_BASE_TWI_EEPROM);
00255     TWID_DmaInitialize(&twi_dma, BOARD_BASE_TWI_EEPROM, 0);
00256 
00257     TWID_Read(&twid, AT24MAC_SERIAL_NUM_ADD, 0x80, 1, SNo, PAGE_SIZE, 0);
00258 
00259     /* Erase all page */
00260     memset(pData, 0, PAGE_SIZE);
00261     for (i = 0; i < EEPROM_PAGES; i++) {
00262         printf("-I- Filling page #%d with zeroes ...\r\n", i);
00263         TWID_Write(&twid, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, 0);
00264         /* Wait at least 10 ms */
00265         Wait(10);
00266     }
00267     printf("\r\n");
00268 
00269     _fillBuffer(pData);
00270 
00271     /* Synchronous operation */
00272     for (i = 0; i < EEPROM_PAGES; i++) {
00273         printf("\n\r-I- Filling page #%d with checkerboard pattern ...\r", i);
00274         TWID_DmaWrite(&twi_dma, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, 0);
00275         /* Wait at least 10 ms */
00276         Wait(10);
00277     }
00278     printf("\r\n");
00279 
00280     /* Read back data */
00281     memset(pData, 0, PAGE_SIZE);
00282     for (i = 0; i < EEPROM_PAGES; i++) {
00283         printf("-I- Reading page #%d... ", i);
00284         TWID_DmaRead(&twi_dma, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, 0);
00285         /* Wait at least 10 ms */
00286         Wait(10);
00287         _checkReadBuffer(pData);
00288     }
00289      printf("\r\n");
00290 
00291     /* Configure TWI interrupts */
00292     NVIC_ClearPendingIRQ(TWIHS0_IRQn);
00293     NVIC_EnableIRQ(TWIHS0_IRQn);
00294 
00295     /* Asynchronous operation */
00296     printf("-I- Read/write on page #0 (IRQ mode)\n\r");
00297 
00298     /* Write checkerboard pattern in first page */
00299     _fillBuffer(pData);
00300     memset(&async, 0, sizeof(async));
00301     async.callback = (void *) TestCallback;
00302 
00303     for (i = 0; i < EEPROM_PAGES; i++) {
00304         printf("-I- Filling page #%d with checkerboard pattern ...\r", i);
00305         TWID_Write(&twid, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, &async);
00306         while (!ASYNC_IsFinished(&async));
00307         /* Wait at least 10 ms */
00308         Wait(10);
00309     }
00310     printf("\r\n");
00311 
00312 
00313     /* Read back data */
00314     memset(pData, 0, PAGE_SIZE);
00315     memset(&async, 0, sizeof(async));
00316     async.callback = (void *) TestCallback;
00317     for (i = 0; i < EEPROM_PAGES; i++) {
00318         printf("-I- Reading page # %d...\r", i);
00319         TWID_Read(&twid, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, &async);
00320         while (!ASYNC_IsFinished(&async));
00321         /* Wait at least 10 ms */
00322         Wait(10);
00323         _checkReadBuffer(pData);
00324     }
00325     printf("\r\n Callback Fired %d times", CallBackFired);
00326 
00327     return 0;
00328 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines