]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained/libchip_samv7/source/isi.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained / libchip_samv7 / source / isi.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License \r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2013, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 \r
31 /*----------------------------------------------------------------------------\r
32  *        Headers\r
33  *----------------------------------------------------------------------------*/\r
34 \r
35 #include "chip.h"\r
36 \r
37 /*----------------------------------------------------------------------------\r
38  *        Export functions\r
39  *----------------------------------------------------------------------------*/\r
40 \r
41 /**\r
42  * \brief Enable ISI\r
43  */\r
44 void ISI_Enable(void)\r
45 {\r
46     REG_ISI_CR |= ISI_CR_ISI_EN;\r
47     while( (REG_ISI_SR & ISI_CR_ISI_EN)!=ISI_CR_ISI_EN);\r
48     REG_ISI_DMA_CHER |= ISI_DMA_CHER_P_CH_EN | ISI_DMA_CHER_C_CH_EN;\r
49 }\r
50 \r
51 \r
52 /**\r
53  * \brief Disable ISI\r
54  */\r
55 void ISI_Disable(void)\r
56 {\r
57     REG_ISI_CR |= ISI_CR_ISI_DIS;\r
58     REG_ISI_DMA_CHDR |= ISI_DMA_CHDR_P_CH_DIS;\r
59 }\r
60 \r
61 \r
62 /**\r
63  * \brief Enable ISI interrupt\r
64  * \param  flag of interrupt to enable\r
65  */\r
66 void ISI_EnableInterrupt(uint32_t flag)\r
67 {\r
68     REG_ISI_IER = flag;\r
69 }\r
70 \r
71 \r
72 /**\r
73  * \brief Disable ISI interrupt\r
74  * \param  flag of interrupt to disable\r
75  */\r
76 void ISI_DisableInterrupt(uint32_t flag)\r
77 {\r
78     REG_ISI_IDR = flag;\r
79 }\r
80 \r
81 \r
82 \r
83 /**\r
84  * \brief Return ISI status register\r
85  * \return Status of ISI register\r
86  */\r
87 uint32_t ISI_StatusRegister(void)\r
88 {\r
89     return(REG_ISI_SR);\r
90 }\r
91 \r
92 \r
93 /**\r
94  * \brief Enable Codec path for capture next frame\r
95  */\r
96 void ISI_CodecPathFull(void)\r
97 {\r
98     // The codec path is enabled and the next frame is captured.\r
99     // Both codec and preview datapaths are working simultaneously\r
100     REG_ISI_CR |= ISI_CR_ISI_CDC;\r
101     REG_ISI_CFG1 |= ISI_CFG1_FULL;\r
102 }\r
103 \r
104 \r
105 /**\r
106  * \brief Set frame rate\r
107  * \param frate frame rate capture\r
108  */\r
109 void ISI_SetFrame(uint32_t frate)\r
110 {\r
111     if( frate > 7 ) {\r
112         TRACE_ERROR("FRate too big\n\r");\r
113         frate = 7;\r
114     }\r
115     REG_ISI_CFG1 |= ISI_CFG1_FRATE(frate);\r
116 }\r
117 \r
118 \r
119 /**\r
120  * \brief Get the number of byte per pixels\r
121  * \param bmpRgb BMP type can be YUV or RGB\r
122  */\r
123 uint8_t ISI_BytesForOnePixel(uint8_t bmpRgb)\r
124 {\r
125     uint8_t nbByte_Pixel;\r
126 \r
127     if (bmpRgb == RGB) {\r
128         if ((REG_ISI_CFG2 & ISI_CFG2_RGB_MODE) == ISI_CFG2_RGB_MODE){\r
129             // RGB: 5:6:5 16bits/pixels\r
130             nbByte_Pixel = 2;\r
131         } \r
132         else {\r
133             // RGB: 8:8:8 24bits/pixels\r
134             nbByte_Pixel = 3;\r
135         }\r
136     } \r
137     else {\r
138         // YUV: 2 pixels for 4 bytes\r
139         nbByte_Pixel = 2;\r
140     }\r
141     return nbByte_Pixel;\r
142 }\r
143 \r
144 \r
145 /**\r
146  * \brief Reset ISI\r
147  */\r
148 void ISI_Reset(void)\r
149 {\r
150     uint32_t timeout=0;\r
151 \r
152     // Resets the image sensor interface.\r
153     // Finish capturing the current frame and then shut down the module.\r
154     REG_ISI_CR = ISI_CR_ISI_SRST | ISI_CR_ISI_DIS;\r
155     // wait Software reset has completed successfully.\r
156     while( (!(REG_ISI_SR & ISI_SR_SRST))\r
157         && (timeout < 0x5000) ){\r
158         timeout++;\r
159     }\r
160     if( timeout == 0x5000 ) {\r
161         TRACE_ERROR("ISI-Reset timeout\n\r");\r
162     }\r
163 }\r
164 \r
165 /**\r
166  * \brief ISI initialize\r
167  * \param pVideo structure of video driver\r
168  */\r
169 void ISI_Init(pIsi_Video pVideo)\r
170 {\r
171     uint32_t hRatio, vRatio;\r
172     ISI_Reset();\r
173 \r
174     // SLD pixel clock periods to wait before the beginning of a line.\r
175     // SFD lines are skipped at the beginning of the frame.\r
176     REG_ISI_CFG1 |= ISI_CFG1_SLD(pVideo->Hblank) + ISI_CFG1_SFD(pVideo->Vblank);\r
177     REG_ISI_CFG1 |=ISI_CFG1_DISCR;\r
178     TRACE_DEBUG("ISI_CFG1=0x%X\n\r", REG_ISI_CFG1);\r
179 \r
180     // IM_VSIZE: Vertical size of the Image sensor [0..2047]\r
181     // Vertical size = IM_VSIZE + 1\r
182     // IM_HSIZE: Horizontal size of the Image sensor [0..2047]\r
183     // Horizontal size = IM_HSIZE + 1\r
184     // YCC_SWAP : YCC image data    \r
185     REG_ISI_CFG2 = ISI_CFG2_IM_VSIZE(pVideo->codec_vsize - 1)\r
186                  + ISI_CFG2_IM_HSIZE(pVideo->codec_hsize - 1);\r
187     \r
188     if (pVideo->rgb_or_yuv == RGB) {\r
189         REG_ISI_CFG2 |= ISI_CFG2_COL_SPACE | ISI_CFG2_RGB_MODE ;\r
190     }\r
191     else {\r
192          REG_ISI_CFG2|= ISI_CFG2_YCC_SWAP_MODE2 ;\r
193     }\r
194     TRACE_DEBUG("ISI_CFG2=0x%X\n\r", REG_ISI_CFG2);\r
195 \r
196     // Vertical Preview size = PREV_VSIZE + 1 (480 max only in RGB mode).\r
197     // Horizontal Preview size = PREV_HSIZE + 1 (640 max only in RGB mode).\r
198 \r
199     if( (pVideo->lcd_vsize > 480) || (pVideo->lcd_hsize > 640)) {\r
200         TRACE_ERROR("Size LCD bad define %d, %d\n\r",pVideo->lcd_vsize ,pVideo->lcd_hsize);\r
201         REG_ISI_PSIZE = ((480 - 1) ) + (((640-1) << 16) );\r
202     }\r
203     else {\r
204         REG_ISI_PSIZE = ((pVideo->lcd_vsize -1)) + (((pVideo->lcd_hsize -1) << 16) );\r
205     }\r
206     // DEC_FACTOR is 8-bit width, range is from 16 to 255. \r
207     // Values from 0 to 16 do not perform any decimation.\r
208     //REG_ISI_PDECF = (16 * pVideo->codec_hsize)/640;\r
209     hRatio = (16 * pVideo->codec_hsize)/(pVideo->lcd_hsize); \r
210     vRatio = (16 * pVideo->codec_vsize)/(pVideo->lcd_vsize); \r
211     REG_ISI_PDECF = (hRatio > vRatio )? vRatio: hRatio;\r
212 \r
213     if (REG_ISI_PDECF < 16) REG_ISI_PDECF = 16;\r
214 \r
215     REG_ISI_DMA_P_DSCR = pVideo->Isi_fbd_base;\r
216     REG_ISI_DMA_P_CTRL = ISI_DMA_P_CTRL_P_FETCH;\r
217     REG_ISI_DMA_P_ADDR = pVideo->lcd_fb_addr;\r
218 \r
219     REG_ISI_DMA_C_DSCR = pVideo->codec_fbd_base;\r
220     REG_ISI_DMA_C_CTRL = ISI_DMA_C_CTRL_C_FETCH;\r
221     REG_ISI_DMA_C_ADDR = pVideo->codec_fb_addr;\r
222 \r
223     // C0: Color Space Conversion Matrix Coefficient C0\r
224     // C1: Color Space Conversion Matrix Coefficient C1\r
225     // C2: Color Space Conversion Matrix Coefficient C2\r
226     // C3: Color Space Conversion Matrix Coefficient C3\r
227     REG_ISI_Y2R_SET0  = ISI_Y2R_SET0_C0(0x95)\r
228                       + ISI_Y2R_SET0_C1(0xFF)\r
229                       + ISI_Y2R_SET0_C2(0x68)\r
230                       + ISI_Y2R_SET0_C3(0x32);\r
231 \r
232     // C4: Color Space Conversion Matrix coefficient C4\r
233     // Yoff: Color Space Conversion Luminance 128 offset\r
234     // Croff: Color Space Conversion Red Chrominance 16 offset\r
235     // Cboff: Color Space Conversion Blue Chrominance 16 offset\r
236     REG_ISI_Y2R_SET1  = ISI_Y2R_SET1_C4(0xCC)\r
237                       + ISI_Y2R_SET1_Yoff\r
238                       + ISI_Y2R_SET1_Croff\r
239                       + ISI_Y2R_SET1_Cboff;\r
240 }\r
241 \r