SAMV71 Xplained Ultra Software Package 1.4

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2013, 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 SAM V71 Xplained Ultra 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 SAM V71 Xplained Ultra board. 
00064  *     Please refer to the Getting Started with SAM V71 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_WORD_ALIGNED 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     /* Write checkerboard pattern in first page */
00178     for ( i=0 ; i < PAGE_SIZE ; i++ ) {
00179         /* Even*/
00180         if ( (i & 1) == 0 ) {
00181             pbuff[i] = 0xA5;
00182         }
00183         /* Odd */
00184         else {
00185             pbuff[i] = 0x5A;
00186         }
00187     }
00188 }
00189 
00190 static void _checkReadBuffer(uint8_t *pBuff )
00191 {
00192     uint16_t i, NoError;
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     /* Disable watchdog */
00229     WDT_Disable( WDT ) ;
00230 
00231     /* Enable I and D cache */
00232     SCB_EnableICache();
00233     SCB_EnableDCache();
00234     
00235     CallBackFired = 0;
00236     
00237     /* Output example information */
00238     printf("-- TWI EEPROM Example %s --\n\r", SOFTPACK_VERSION);
00239     printf("-- %s\n\r", BOARD_NAME);
00240     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00241     
00242     TimeTick_Configure();
00243     
00244     /* Configure TWI pins. */
00245     PIO_Configure(pins, PIO_LISTSIZE(pins));
00246     PMC_EnablePeripheral(BOARD_ID_TWI_EEPROM);
00247     /* Configure TWI */
00248     
00249     twi_dma.pTwid = malloc(sizeof(Twid));
00250     twi_dma.pTwiDma = malloc(sizeof(sXdmad));
00251     
00252     TWI_ConfigureMaster(BOARD_BASE_TWI_EEPROM, TWCK, BOARD_MCK);
00253     TWID_Initialize(&twid, BOARD_BASE_TWI_EEPROM);
00254     TWID_DmaInitialize(&twi_dma, BOARD_BASE_TWI_EEPROM, 0);
00255 
00256     TWID_Read(&twid, AT24MAC_SERIAL_NUM_ADD, 0x80, 1, SNo, PAGE_SIZE, 0);
00257 
00258     /* Erase all page */
00259     memset(pData, 0, PAGE_SIZE);
00260     for(i=0; i< EEPROM_PAGES; i++) {
00261         printf("-I- Filling page #%d with zeroes ...\r\n", i);
00262         TWID_Write(&twid, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, 0);
00263         /* Wait at least 10 ms */
00264         Wait(10);
00265     }
00266     printf("\r\n");
00267 
00268     _fillBuffer(pData);
00269 
00270     /* Synchronous operation */
00271     for(i=0; i< EEPROM_PAGES; i++) {
00272         printf("\n\r-I- Filling page #%d with checkerboard pattern ...\r", i);
00273         TWID_DmaWrite(&twi_dma, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, 0);
00274         /* Wait at least 10 ms */
00275         Wait(10);
00276     }
00277     printf("\r\n");
00278 
00279     /* Read back data */
00280     memset(pData, 0, PAGE_SIZE);
00281     for(i=0; i< EEPROM_PAGES; i++) {
00282         printf("-I- Reading page #%d... ", i);
00283         TWID_DmaRead(&twi_dma, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, 0);
00284         /* Wait at least 10 ms */
00285         Wait(10);
00286         _checkReadBuffer(pData);
00287     }
00288      printf("\r\n");
00289 
00290     /* Configure TWI interrupts */
00291     NVIC_ClearPendingIRQ(TWIHS0_IRQn);
00292     NVIC_EnableIRQ(TWIHS0_IRQn);
00293 
00294     /* Asynchronous operation */
00295     printf("-I- Read/write on page #0 (IRQ mode)\n\r");
00296 
00297     /* Write checkerboard pattern in first page */
00298     _fillBuffer(pData);
00299     memset(&async, 0, sizeof(async));
00300     async.callback = (void *) TestCallback;
00301 
00302     for(i=0; i< EEPROM_PAGES; i++) {
00303         printf("-I- Filling page #%d with checkerboard pattern ...\r", i);
00304         TWID_Write(&twid, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, &async);
00305         while (!ASYNC_IsFinished(&async));
00306         /* Wait at least 10 ms */
00307         Wait(10);
00308     }
00309     printf("\r\n");
00310 
00311 
00312     /* Read back data */
00313     memset(pData, 0, PAGE_SIZE);
00314     memset(&async, 0, sizeof(async));
00315     async.callback = (void *) TestCallback;
00316     for(i=0; i< EEPROM_PAGES; i++) {
00317         printf("-I- Reading page # %d...\r", i);
00318         TWID_Read(&twid, AT24MAC_ADDRESS, i*PAGE_SIZE, 1, pData, PAGE_SIZE, &async);
00319         while ( !ASYNC_IsFinished( &async ) ) ;
00320         /* Wait at least 10 ms */
00321         Wait(10);
00322         _checkReadBuffer(pData);
00323     }
00324     printf("\r\n Callback Fired %d times", CallBackFired);
00325 
00326     return 0 ;
00327 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines