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 ISI 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 uint16_t gIsiBuffer[640 * 480 * 2];
00098 
00099 #define ISI_BASE                gIsiBuffer
00100 
00101 /** TWI clock frequency in Hz. */
00102 #define TWCK                    400000
00103 /** TWI peripheral ID for Sensor configuration */
00104 #define BOARD_ID_TWI_ISI        ID_TWIHS0
00105 /** TWI base address for Sensor configuration */
00106 #define BOARD_BASE_TWI_ISI      TWIHS0
00107 
00108 /** Frame Buffer Descriptors */
00109 #define ISI_MAX_PREV_BUFFER     1
00110 
00111 /** TWI Pins definition */
00112 #define BOARD_PINS_TWI_ISI      PINS_TWI0
00113 
00114 #define LCD_NORMAL_MODE   0
00115 #define LCD_REVERSE_MODE  1    // Row/column exchange
00116 #define LCD_MODE            ILI9488_EBIMODE
00117 
00118 /*----------------------------------------------------------------------------
00119  *        Local variables
00120  *----------------------------------------------------------------------------*/
00121 
00122 /** ISI pins to configure. */
00123 const Pin pinsTWI[] = BOARD_PINS_TWI_ISI;
00124 const Pin pin_ISI_RST = BOARD_ISI_RST;
00125 const Pin pin_ISI_PWD = BOARD_ISI_PWD;
00126 const Pin pPinsISI[]= {BOARD_ISI_PINS};
00127 
00128 /** TWI driver instance.*/
00129 static Twid twid;
00130 
00131 /* ISI DMA descriptor for preview path */
00132 COMPILER_ALIGNED(32) ISI_FrameBufferDescriptors preBufDescList[ISI_MAX_PREV_BUFFER];
00133 
00134 /* Image size in preview mode */
00135 static uint32_t wImageWidth, wImageHeight;
00136 
00137 /* Image output format */
00138 static sensorOutputFormat_t wImageFormat;
00139 
00140 /* Display size in preview mode */
00141 static uint32_t wDisplayWidth, wDisplayHeight;
00142 
00143 /* LCD display mode Normal or reverse mode */
00144 static uint8_t lcdDisplayMode = LCD_REVERSE_MODE;
00145 
00146 /** Global DMA driver for all transfer */
00147 static sXdmad lcdEbiDma;
00148 
00149 extern const sensorProfile_t ov2640Profile;
00150 extern const sensorProfile_t ov2643Profile;
00151 extern const sensorProfile_t ov5640Profile;
00152 extern const sensorProfile_t ov7740Profile;
00153 extern const sensorProfile_t ov9740Profile;
00154 
00155 /** Supported sensor profiles */
00156 static const sensorProfile_t *sensorsProfile[5] = {&ov2640Profile,
00157                                                   &ov2643Profile,
00158                                                   &ov5640Profile,
00159                                                   &ov7740Profile,
00160                                                   &ov9740Profile
00161 };
00162 
00163 /*----------------------------------------------------------------------------
00164  *        Local functions
00165  *----------------------------------------------------------------------------*/
00166 /**
00167  * \brief TWI interrupt handler. Forwards the interrupt to the TWI driver handler.
00168  */
00169 void TWIHS0_Handler(void)
00170 {
00171     TWID_Handler(&twid);
00172 }
00173 
00174 /**
00175  * ISR for XDMA interrupt
00176  */
00177 void XDMAC_Handler(void)
00178 {
00179     XDMAD_Handler(&lcdEbiDma);
00180 }
00181 
00182 /**
00183  * \brief ISI interrupt handler.
00184  */
00185 void ISI_Handler(void)
00186 {
00187     uint32_t status,imr;
00188     status = ISI->ISI_SR;
00189     imr = ISI->ISI_IMR;
00190     if ((status & ISI_SR_PXFR_DONE) && (imr & ISI_IMR_PXFR_DONE)) {
00191         ISI_DmaChannelDisable(ISI_DMA_CHDR_P_CH_DIS);
00192         ISI_DisableInterrupt(ISI_IDR_PXFR_DONE);
00193         LCDD_UpdateWindow();
00194         ISI_DmaChannelEnable(ISI_DMA_CHER_P_CH_EN);
00195         ISI_EnableInterrupt(ISI_IER_PXFR_DONE);
00196     }
00197 }
00198 
00199 /**
00200  * \brief TWI initialization.
00201  */
00202 static void _twiInit(void)
00203 {
00204     /* Configure TWI pins. */
00205     PIO_Configure(pinsTWI, PIO_LISTSIZE(pinsTWI));
00206     /* Enable TWI peripheral clock */
00207     PMC_EnablePeripheral(BOARD_ID_TWI_ISI);
00208     /* Configure TWI */
00209     TWI_ConfigureMaster(BOARD_BASE_TWI_ISI, TWCK, BOARD_MCK);
00210     TWID_Initialize(&twid, BOARD_BASE_TWI_ISI);
00211 
00212     /* Configure TWI interrupts */
00213     NVIC_ClearPendingIRQ(TWIHS0_IRQn);
00214     NVIC_EnableIRQ(TWIHS0_IRQn);
00215 }
00216 
00217 /**
00218  * \brief ISI PCK initialization.
00219  */
00220 static void _isiPckInit(void)
00221 {
00222     /* Configure ISI pins. */
00223     PIO_Configure(pPinsISI, PIO_LISTSIZE(pPinsISI));
00224 
00225     /* Disable programmable clock 1 output */
00226     REG_PMC_SCDR = PMC_SCER_PCK0;
00227     /* Enable the DAC master clock */
00228     PMC->PMC_PCK[0] = PMC_PCK_CSS_MCK | (4 << 4);
00229     /* Enable programmable clock 0 output */
00230     REG_PMC_SCER = PMC_SCER_PCK0;
00231     /* Wait for the PCKRDY0 bit to be set in the PMC_SR register*/
00232     while ((REG_PMC_SR & PMC_SR_PCKRDY0) == 0);
00233     /* ISI PWD OFF*/
00234 
00235     PIO_Clear(&pin_ISI_PWD);
00236     PIO_Clear(&pin_ISI_RST);
00237 }
00238 
00239 /**
00240  * \brief Set up Frame Buffer Descriptors(FBD) for preview path.
00241  */
00242 static void _isi_AllocateFBD(void)
00243 {
00244     uint32_t i;
00245     for(i = 0; i < ISI_MAX_PREV_BUFFER; i++) {
00246         preBufDescList[i].Current = (uint32_t)ISI_BASE;
00247         preBufDescList[i].Control = ISI_DMA_P_CTRL_P_FETCH;
00248         preBufDescList[i].Next    = (uint32_t)&preBufDescList[0];
00249     }
00250 }
00251 
00252 /**
00253  * \brief Configure LCD
00254  */
00255 static void  _lcd_Configure(void)
00256 {
00257     rect rc;
00258     LCDD_Initialize(LCD_MODE, &lcdEbiDma, lcdDisplayMode);
00259     LCDD_SetCavasBuffer((void*)gIsiBuffer, BOARD_LCD_HEIGHT * BOARD_LCD_WIDTH * 2);
00260     //LCDD_SetCavasBuffer((void*)gIsiBuffer, 320 * 240 * 2);
00261     rc.x = 0;
00262     rc.y = 0;
00263     if (lcdDisplayMode == LCD_REVERSE_MODE) {
00264         rc.width = BOARD_LCD_HEIGHT - 1;
00265         rc.height = BOARD_LCD_WIDTH - 1;
00266         //rc.width = 320 - 1;
00267         //rc.height = 240 - 1;
00268     } else {
00269         rc.width = BOARD_LCD_WIDTH - 1;
00270         rc.height = BOARD_LCD_HEIGHT - 1;
00271     }
00272     LCDD_SetUpdateWindowSize(rc);
00273     LCDD_DrawRectangleWithFill(0, 0, 0, rc.width - 1, rc.height - 1,
00274             RGB_24_TO_RGB565(COLOR_BLACK));
00275     LCDD_UpdateWindow();
00276     Wait(500);
00277 }
00278 
00279 /**
00280  * \brief ISI  initialization.
00281  */
00282 static void _isiInit(void)
00283 {
00284     ISI_Y2R y2r;
00285 
00286     /* Enable ISI peripheral clock */
00287     PMC_EnablePeripheral(ID_ISI);
00288     /* Set up Frame Buffer Descriptors(FBD) for preview path. */
00289     _isi_AllocateFBD();
00290     /* Reset ISI peripheral */
00291     ISI_Reset();
00292     /* Set the windows blank */
00293     ISI_SetBlank(0, 0);
00294     /* Set vertical and horizontal Size of the Image Sensor for preview path*/
00295     ISI_SetSensorSize(wImageWidth, wImageHeight);
00296 
00297     /* Set preview size to fit LCD size*/
00298     if (lcdDisplayMode == LCD_REVERSE_MODE) {
00299         /* LCD Height = BOARD_LCD_WIDTH  LCD Width = BOARD_LCD_HEIGHT */
00300         wDisplayHeight = wImageHeight * 1000 / (wImageWidth * 1000 / BOARD_LCD_HEIGHT);
00301         wDisplayWidth = BOARD_LCD_HEIGHT;
00302         ISI_setPreviewSize(wDisplayWidth, wDisplayHeight);
00303 
00304     } else {
00305         /* LCD Height = BOARD_LCD_HEIGHT  LCD Width = BOARD_LCD_WIDTH */
00306         wDisplayHeight = wImageHeight * 1000 / (wImageWidth * 1000 / BOARD_LCD_WIDTH);
00307         wDisplayWidth = BOARD_LCD_WIDTH;
00308         ISI_setPreviewSize(BOARD_LCD_WIDTH, wDisplayHeight );
00309     }
00310     /*  Set data stream in YUV format.*/
00311     ISI_setInputStream(YUV_INPUT);
00312     /* Defines YCrCb swap format.*/
00313     ISI_YCrCbFormat(ISI_CFG2_YCC_SWAP_MODE2);
00314     /* calculate scaler factor automatically. */
00315     ISI_calcScalerFactor();
00316     /* Configure DMA for preview path. */
00317     ISI_setDmaInPreviewPath((uint32_t)&preBufDescList[0], ISI_DMA_P_CTRL_P_FETCH,
00318             (uint32_t)ISI_BASE);
00319     /* */
00320     /* Set Matrix color space for YUV to RBG convert */
00321     y2r.C0 = 0x95;
00322     y2r.C1 = 0xFF;
00323     y2r.C2 = 0x68;
00324     y2r.C3 = 0x32;
00325     y2r.C4 = 0xCC;
00326     y2r.Yoff = 1;
00327     y2r.Croff = 1;
00328     y2r.Cboff = 1;
00329     ISI_SetMatrix4Yuv2Rgb(&y2r);
00330     NVIC_ClearPendingIRQ(ISI_IRQn);
00331     NVIC_SetPriority(ISI_IRQn, 7);
00332     NVIC_EnableIRQ(ISI_IRQn);
00333 }
00334 
00335 /*----------------------------------------------------------------------------
00336  *        Global functions
00337  *----------------------------------------------------------------------------*/
00338 /**
00339  * \brief Application entry point for ISI example.
00340  *
00341  * \return Unused (ANSI-C compatibility).
00342  */
00343 extern int main( void )
00344 {
00345     uint8_t key;
00346     WDT_Disable(WDT);
00347 
00348     /* Enable I and D cache */
00349     SCB_EnableICache();
00350     SCB_EnableDCache();
00351 
00352     /* Output example information */
00353     printf("-- ISI Example %s --\n\r", SOFTPACK_VERSION);
00354     printf("-- %s\n\r", BOARD_NAME );
00355     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00356 
00357     /* Enable SDRAM for Frame buffer */
00358     BOARD_ConfigureSdram();
00359 
00360     /* Configure Time Tick */
00361     TimeTick_Configure();
00362 
00363     /* Configure LCD */
00364     _lcd_Configure();
00365 
00366     /* TWI Initialize */
00367     _twiInit();
00368 
00369     /* ISI PCK clock Initialize */
00370     _isiPckInit();
00371 
00372     PIO_Set(&pin_ISI_RST);
00373 
00374     printf("Press [0|1|2|3|4][5] to select supported sensor \n\r");
00375     printf("- '0' omnivision 2640 \n\r");
00376     printf("- '1' omnivision 2643 \n\r");
00377     printf("- '2' omnivision 5640 \n\r");
00378     printf("- '3' omnivision 7740 \n\r");
00379     printf("- '4' omnivision 9740 \n\r");
00380     for(;;) {
00381         key = DBG_GetChar();
00382         if ((key >= '0') && (key <='5')) {
00383             if (sensor_setup(&twid, sensorsProfile[key- '0'], VGA) != SENSOR_OK) {
00384                 printf("-E- Sensor setup failed.");
00385                 while (1);
00386             } else {
00387                 break;
00388             }
00389         }
00390     }
00391     /* Retrieve sensor output format and size */
00392     sensor_get_output(&wImageFormat, &wImageWidth, &wImageHeight, VGA);
00393     printf("Sensor <%d, %d> \n\r", (int)wImageWidth, (int)wImageHeight );
00394     if (wImageFormat == MONO_12_BIT) {
00395         printf("-I- Monochrome sensor do not support in this example!");
00396         while (1);
00397     }
00398     if (wImageFormat == RAW_BAYER_12_BIT || wImageFormat == RAW_BAYER_10_BIT)
00399         wImageFormat = (sensorOutputFormat_t)RGB_INPUT;
00400     else
00401         wImageFormat = (sensorOutputFormat_t)YUV_INPUT;
00402 
00403     /* ISI Initialize */
00404     _isiInit();
00405 
00406     printf("preview in RGB 565 mode\n\r");
00407     ISI_Enable();
00408     ISI_DisableInterrupt(0xFFFFFFFF);
00409     ISI_EnableInterrupt(ISI_IER_PXFR_DONE);
00410     ISI_DmaChannelDisable(ISI_DMA_CHDR_C_CH_DIS);
00411     SCB_CleanDCache_by_Addr((uint32_t *)preBufDescList, sizeof(preBufDescList));
00412     ISI_DmaChannelEnable(ISI_DMA_CHER_P_CH_EN);
00413     while (1);
00414 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines