SAMV71 Xplained Ultra Software Package 1.5

usb_video.c

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  *        Headers
00032  *----------------------------------------------------------------------------*/
00033 
00034 #include "board.h"
00035 
00036 /*----------------------------------------------------------------------------
00037  *        Local definitions
00038  *----------------------------------------------------------------------------*/
00039 
00040 /** TWI clock frequency in Hz. */
00041 #define TWCK            400000
00042 
00043 /** TWI Pins definition */
00044 #define BOARD_PINS_TWI_ISI PINS_TWI0
00045 /** TWI peripheral ID for Sensor configuration */
00046 #define BOARD_ID_TWI_ISI        ID_TWIHS0
00047 /** TWI base address for Sensor configuration */
00048 #define BOARD_BASE_TWI_ISI      TWIHS0
00049 
00050 
00051 /*----------------------------------------------------------------------------
00052  *          Exported functions
00053  *----------------------------------------------------------------------------*/
00054 uint32_t UVC_preCondition(void);
00055 
00056 
00057 /*----------------------------------------------------------------------------
00058  *        External variables
00059  *----------------------------------------------------------------------------*/
00060 extern const sensorProfile_t ov2643Profile;
00061 extern const sensorProfile_t ov5640Profile;
00062 extern const sensorProfile_t ov7740Profile;
00063 extern const sensorProfile_t ov9740Profile;
00064 
00065 static const sensorProfile_t * sensorProfiles[] = {
00066     &ov2643Profile, &ov5640Profile, &ov7740Profile, &ov9740Profile};
00067 
00068 static const char sensorName[][8] = {
00069     "OV2643", "OV5640", "OV7740", "OV9740"
00070 };
00071 
00072 
00073 /*----------------------------------------------------------------------------
00074  *        Local variables
00075  *----------------------------------------------------------------------------*/
00076 
00077 /** ISI pins to configure. */
00078 const Pin pinsTWI[] = BOARD_PINS_TWI_ISI;
00079 const Pin pin_ISI_RST = BOARD_ISI_RST;
00080 const Pin pin_ISI_PWD = BOARD_ISI_PWD;
00081 const Pin pPinsISI[]= {BOARD_ISI_PINS};
00082 
00083 /** TWI driver instance.*/
00084 static Twid twid;
00085 
00086 
00087 /*----------------------------------------------------------------------------
00088  *        Local functions
00089  *----------------------------------------------------------------------------*/
00090 /**
00091  * \brief TWI interrupt handler. Forwards the interrupt to the TWI driver handler.
00092  */
00093 void TWIHS0_Handler(void)
00094 {
00095     TWID_Handler( &twid );
00096 }
00097 
00098 /**
00099  * \brief TWI initialization.
00100  */
00101 static void _twiInit(void)
00102 {
00103     /* Configure TWI pins. */
00104     PIO_Configure(pinsTWI, PIO_LISTSIZE(pinsTWI));
00105     /* Enable TWI peripheral clock */
00106     PMC_EnablePeripheral(BOARD_ID_TWI_ISI);
00107     /* Configure TWI */
00108     TWI_ConfigureMaster(BOARD_BASE_TWI_ISI, TWCK, BOARD_MCK);
00109     TWID_Initialize(&twid, BOARD_BASE_TWI_ISI);
00110 
00111     /* Configure TWI interrupts */
00112     NVIC_ClearPendingIRQ(TWIHS0_IRQn);
00113     NVIC_EnableIRQ(TWIHS0_IRQn);
00114 }
00115 
00116 /**
00117  * \brief ISI PCK initialization.
00118  */
00119 static void _isiPckInit(void)
00120 {
00121     /* Configure ISI pins. */
00122     PIO_Configure(pPinsISI, PIO_LISTSIZE(pPinsISI));
00123 
00124     /* Disable programmable clock 1 output */
00125     REG_PMC_SCDR = PMC_SCER_PCK0;
00126     /* Enable the DAC master clock */
00127     PMC->PMC_PCK[0] = PMC_PCK_CSS_PLLA_CLK | (9 << 4);
00128     /* Enable programmable clock 0 output */
00129     REG_PMC_SCER = PMC_SCER_PCK0;
00130     /* Wait for the PCKRDY0 bit to be set in the PMC_SR register*/
00131     while ((REG_PMC_SR & PMC_SR_PCKRDY0) == 0);
00132     /* ISI PWD OFF*/
00133     PIO_Clear(&pin_ISI_PWD);
00134     PIO_Clear(&pin_ISI_RST);
00135 }
00136 
00137 /*----------------------------------------------------------------------------
00138  *        Exported functions
00139  *----------------------------------------------------------------------------*/
00140 uint32_t UVC_preCondition(void)
00141 {
00142     uint32_t rc = 1;
00143     uint32_t i;
00144 
00145     TimeTick_Configure();
00146 
00147     /* TWI Initialize */
00148     _twiInit();
00149 
00150     /* ISI PCK clock Initialize */
00151     _isiPckInit();
00152     PIO_Set(&pin_ISI_RST);
00153 
00154     for (i = 0; i < (sizeof(sensorProfiles)/sizeof(sensorProfiles[0])); i++) {
00155         if (sensor_setup(&twid, sensorProfiles[i], QVGA) == SENSOR_OK){
00156             printf("\n\r-I- Sensor %s setup succeed.", sensorName[i]);
00157             rc = 0;
00158             break;
00159         }
00160     }
00161     if (rc) {
00162         printf("\n\r-I- No image sensor detected.");
00163     }
00164 
00165     NVIC_DisableIRQ(TWIHS0_IRQn);
00166     return rc;
00167 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines