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 isi_gray ISI Gray Scale Example
00032  *
00033  * \section Purpose
00034  *
00035  * This example demonstrates the ISI (Image Sensor Interface) of an SAM V71
00036  * Xplained Ultra.
00037  *
00038  * \section Requirements
00039  *
00040  * This package can be used with SAM V71 Xplained Ultra with
00041  * On-board ISI interface and a external CMOS-type image sensor board.
00042  *
00043  * \section Description
00044  * The provided program uses the Image Sensor Interface to connects a CMOS-type
00045  * image sensor to the processor and displays in VGA format.
00046  *
00047  *  \section Note
00048  *
00049  *  Some pins conflict between LCD pins and JTAG pins, this example can not run
00050  * in debug mode.
00051  *
00052  * \section Usage
00053  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board.
00054  *  Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00055  * -# On the computer, open and configure a terminal application
00056  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00057  *   - 115200 bauds
00058  *   - 8 bits of data
00059  *   - No parity
00060  *   - 1 stop bit
00061  *   - No flow control
00062  * -# Start the application.
00063  * -# In the terminal window, the following text should appear:
00064  *    \code
00065  *     -- ISI Example xxx --
00066  *     -- SAMxxxxx-xx
00067  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00068  *    \endcode
00069  * The user can then choose any of the available options to perform the
00070  * described action.
00071  *
00072  * \section References
00073  * - lcdc.c
00074  * - twi.c
00075  * - twid.c
00076  * - isi.c
00077  */
00078 
00079 /**
00080  * \file
00081  *
00082  * This file contains all the specific code for the ISI example.
00083  */
00084 
00085 /*----------------------------------------------------------------------------
00086  *        Headers
00087  *----------------------------------------------------------------------------*/
00088 
00089 #include "board.h"
00090 #include "string.h"
00091 
00092 /*----------------------------------------------------------------------------
00093  *        Local definitions
00094  *----------------------------------------------------------------------------*/
00095 
00096 COMPILER_SECTION("sdram_region")
00097 static uint8_t gIsiBuffer[640 * 480];
00098 COMPILER_SECTION("sdram_region")
00099 static uint16_t gDisplayBuffer[320 * 240];
00100 static uint16_t lut[256];
00101 
00102 #define ISI_BASE                gIsiBuffer
00103 #define DISPLAY_BASE            gDisplayBuffer
00104 
00105 /** TWI clock frequency in Hz. */
00106 #define TWCK                    400000
00107 /** TWI peripheral ID for Sensor configuration */
00108 #define BOARD_ID_TWI_ISI        ID_TWIHS0
00109 /** TWI base address for Sensor configuration */
00110 #define BOARD_BASE_TWI_ISI      TWIHS0
00111 #define CAMX_MT9V022_SLAVE_ADDR  (0x30>>1)
00112 
00113 /** Frame Buffer Descriptors */
00114 #define ISI_MAX_PREV_BUFFER     1
00115 
00116 /** TWI Pins definition */
00117 #define BOARD_PINS_TWI_ISI      PINS_TWI0
00118 
00119 #define LCD_NORMAL_MODE   0
00120 #define LCD_REVERSE_MODE  1    // Row/column exchange
00121 #define LCD_MODE            ILI9488_EBIMODE
00122 
00123 /*----------------------------------------------------------------------------
00124  *        Local variables
00125  *----------------------------------------------------------------------------*/
00126 extern const sensorProfile_t mt9v022Profile;
00127 
00128 /** ISI pins to configure. */
00129 const Pin pinsTWI[] = BOARD_PINS_TWI_ISI;
00130 const Pin pin_ISI_RST = BOARD_ISI_RST;
00131 const Pin pin_ISI_PWD = BOARD_ISI_PWD;
00132 const Pin pPinsISI[]= {BOARD_ISI_PINS};
00133 
00134 /** TWI driver instance.*/
00135 static Twid twid;
00136 
00137 /* ISI DMA descriptor for preview path */
00138 ISI_FrameBufferDescriptors preBufDescList;
00139 
00140 /* Image size in preview mode */
00141 static uint32_t wImageWidth, wImageHeight;
00142 
00143 /* Image output format */
00144 static sensorOutputFormat_t wImageFormat;
00145 
00146 /* LCD display mode Normal or reverse mode */
00147 static uint8_t lcdDisplayMode = LCD_REVERSE_MODE;
00148 
00149 /** Global DMA driver for all transfer */
00150 static sXdmad lcdEbiDma;
00151 
00152 /*----------------------------------------------------------------------------
00153  *        Local functions
00154  *----------------------------------------------------------------------------*/
00155 /**
00156  * \brief TWI interrupt handler. Forwards the interrupt to the TWI driver handler.
00157  */
00158 void TWIHS0_Handler(void)
00159 {
00160     TWID_Handler(&twid);
00161 }
00162 
00163 /**
00164  * ISR for XDMA interrupt
00165  */
00166 void XDMAC_Handler(void)
00167 {
00168     XDMAD_Handler(&lcdEbiDma);
00169 }
00170 
00171 static void y2rgb565(void)
00172 {
00173     uint16_t i, j;
00174     uint8_t *s;
00175     uint16_t *p;
00176 
00177     s = (uint8_t*)ISI_BASE;
00178     p = (uint16_t*)DISPLAY_BASE;
00179 
00180     for (i = 0; i < wImageWidth; i++) {
00181         for (j = 0; j < wImageHeight; j++) {
00182             *p++ = lut[*s];
00183             s++;
00184         }
00185     }
00186 }
00187 
00188 /**
00189  * \brief ISI interrupt handler.
00190  */
00191 void ISI_Handler(void)
00192 {
00193     uint32_t status,imr;
00194 
00195     status = ISI->ISI_SR;
00196     imr = ISI->ISI_IMR;
00197     if ((status & ISI_SR_PXFR_DONE) && (imr & ISI_IMR_PXFR_DONE)) {
00198         if (ISI->ISI_DMA_P_ADDR != (uint32_t)ISI_BASE + (wImageWidth * wImageHeight ))
00199             return;
00200         ISI_DisableInterrupt(ISI_IDR_PXFR_DONE);
00201         ISI_DmaChannelDisable(ISI_DMA_CHDR_P_CH_DIS);
00202         y2rgb565();
00203         LCDD_UpdateWindow();
00204         ISI_setDmaInPreviewPath((uint32_t)&preBufDescList,
00205                             ISI_DMA_P_CTRL_P_FETCH, (uint32_t)ISI_BASE);
00206         ISI_DmaChannelEnable(ISI_DMA_CHER_P_CH_EN);
00207         ISI_EnableInterrupt(ISI_IER_PXFR_DONE);
00208     }
00209 }
00210 
00211 /**
00212  * \brief TWI initialization.
00213  */
00214 static void _twiInit(void)
00215 {
00216     /* Configure TWI pins. */
00217     PIO_Configure(pinsTWI, PIO_LISTSIZE(pinsTWI));
00218     /* Enable TWI peripheral clock */
00219     PMC_EnablePeripheral(BOARD_ID_TWI_ISI);
00220     /* Configure TWI */
00221     TWI_ConfigureMaster(BOARD_BASE_TWI_ISI, TWCK, BOARD_MCK);
00222     TWID_Initialize(&twid, BOARD_BASE_TWI_ISI);
00223 
00224     /* Configure TWI interrupts */
00225     NVIC_ClearPendingIRQ(TWIHS0_IRQn);
00226     NVIC_EnableIRQ(TWIHS0_IRQn);
00227 }
00228 
00229 /**
00230  * \brief ISI PCK initialization.
00231  */
00232 static void _isiPckInit(void)
00233 {
00234     /* Configure ISI pins. */
00235     PIO_Configure(pPinsISI, PIO_LISTSIZE(pPinsISI));
00236 
00237     /* Disable programmable clock 1 output */
00238     REG_PMC_SCDR = PMC_SCER_PCK0;
00239     /* Enable the DAC master clock */
00240     PMC->PMC_PCK[0] = PMC_PCK_CSS_PLLA_CLK | (9 << 4);
00241     /* Enable programmable clock 0 output */
00242     REG_PMC_SCER = PMC_SCER_PCK0;
00243     /* Wait for the PCKRDY0 bit to be set in the PMC_SR register*/
00244     while ((REG_PMC_SR & PMC_SR_PCKRDY0) == 0);
00245     /* ISI PWD OFF*/
00246     PIO_Clear(&pin_ISI_PWD);
00247     PIO_Clear(&pin_ISI_RST);
00248 }
00249 
00250 
00251 /**
00252  * \brief Configure LCD
00253  */
00254 static void  _lcd_Configure(void)
00255 {
00256     rect rc;
00257 
00258     LCDD_Initialize(LCD_MODE, &lcdEbiDma, lcdDisplayMode);
00259     LCDD_SetCavasBuffer((void*)gDisplayBuffer, wImageWidth * wImageHeight * 2);
00260     rc.x = 0;
00261     rc.y = 0;
00262     rc.width = wImageWidth - 1;
00263     rc.height = wImageHeight - 1;
00264     LCDD_SetUpdateWindowSize(rc);
00265     LCDD_DrawRectangleWithFill(0, 0, 0, rc.width -1, rc.height -1,
00266             RGB_24_TO_RGB565(COLOR_BLACK));
00267     LCDD_UpdateWindow();
00268     Wait(500);
00269 }
00270 
00271 /**
00272  * \brief ISI  initialization.
00273  */
00274 static void _isiInit(void)
00275 {
00276     /* Enable ISI peripheral clock */
00277     PMC_EnablePeripheral(ID_ISI);
00278     /* Reset ISI peripheral */
00279     ISI_Reset();
00280     /* Set the windows blank */
00281     ISI_SetBlank(0, 0);
00282     /* Set vertical and horizontal Size of the Image Sensor for preview path*/
00283     ISI_SetSensorSize(wImageWidth/2, wImageHeight);
00284     /* Set preview size to fit LCD size*/
00285     ISI_setPreviewSize(wImageWidth/2, wImageHeight);
00286     /* calculate scaler factor automatically. */
00287     ISI_calcScalerFactor();
00288     /*  Set data stream in YUV format.*/
00289     ISI_setInputStream(RGB_INPUT);
00290     ISI_RgbPixelMapping(1);
00291     /* Configure DMA for preview path. */
00292     preBufDescList.Current = (uint32_t)ISI_BASE;
00293     preBufDescList.Control = ISI_DMA_P_CTRL_P_FETCH;
00294     preBufDescList.Next    = (uint32_t)&preBufDescList;
00295     ISI_setDmaInPreviewPath((uint32_t)&preBufDescList, ISI_DMA_P_CTRL_P_FETCH,(uint32_t)ISI_BASE);
00296     NVIC_ClearPendingIRQ(ISI_IRQn);
00297     NVIC_SetPriority(ISI_IRQn, 5);
00298     NVIC_EnableIRQ(ISI_IRQn);
00299 }
00300 
00301 /*----------------------------------------------------------------------------
00302  *        Global functions
00303  *----------------------------------------------------------------------------*/
00304 /**
00305  * \brief Application entry point for ISI example.
00306  *
00307  * \return Unused (ANSI-C compatibility).
00308  */
00309 extern int main(void)
00310 {
00311     uint8_t reg = 0x0;
00312     uint8_t val = 0xFD;
00313     uint16_t i;
00314 
00315     WDT_Disable(WDT);
00316 
00317     /* Enable I and D cache */
00318     SCB_EnableICache();
00319     SCB_EnableDCache();
00320 
00321     /* Output example information */
00322     printf("-- ISI Gray Example %s --\n\r", SOFTPACK_VERSION);
00323     printf("-- %s\n\r", BOARD_NAME);
00324     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00325 
00326     /* Enable SDRAM for Frame buffer */
00327     BOARD_ConfigureSdram();
00328 
00329     /* Configure Time Tick */
00330     TimeTick_Configure();
00331 
00332     /* TWI Initialize */
00333     _twiInit();
00334 
00335     /* ISI PCK clock Initialize */
00336     _isiPckInit();
00337 
00338     PIO_Set(&pin_ISI_RST);
00339 
00340     TWID_Write(&twid, CAMX_MT9V022_SLAVE_ADDR, 0x03, 1, &reg, 1, 0);
00341     TWID_Write(&twid, CAMX_MT9V022_SLAVE_ADDR, 1, 1, &val, 1, 0);
00342     if (sensor_setup(&twid, &mt9v022Profile, QVGA) != SENSOR_OK) {
00343         printf("-E- Sensor setup failed.");
00344         while (1);
00345     }
00346 
00347     /* Retrieve sensor output format and size */
00348     sensor_get_output(&wImageFormat, &wImageWidth, &wImageHeight, QVGA);
00349 
00350     /* Configure LCD */
00351     _lcd_Configure();
00352 
00353     /* Init RBG565 lookup table */
00354     for(i = 0; i < 256; i++)
00355         lut[i] = ((i >> 3) << 11) | ((i >> 2) << 5) | (i >> 3);
00356 
00357     /* ISI Initialize */
00358     _isiInit();
00359 
00360     ISI_Enable();
00361     ISI_DisableInterrupt(0xFFFFFFFF);
00362     ISI_EnableInterrupt(ISI_IER_PXFR_DONE);
00363     ISI_DmaChannelDisable(ISI_DMA_CHDR_C_CH_DIS);
00364     SCB_CleanInvalidateDCache();
00365 
00366     ISI_DmaChannelEnable(ISI_DMA_CHER_P_CH_EN);
00367 
00368     printf("preview in RGB 565 mode\n\r");
00369     while (1);
00370 }
00371 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines