]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D4x_EK_IAR/AtmelFiles/libchip_sama5d4x/source/isi.c
Core kernel files:
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / AtmelFiles / libchip_sama5d4x / 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     /* Write one to this field to enable the module. */ \r
47     REG_ISI_CR |= ISI_CR_ISI_EN;\r
48     /* Software must poll ENABLE field in the ISI_STATUS register to verify that the command \r
49     has successfully completed.*/\r
50     while( (REG_ISI_SR & ISI_SR_ENABLE) != ISI_SR_ENABLE);\r
51 }\r
52 \r
53 /**\r
54  * \brief Enable ISI Dma channel\r
55  * \param  channel to be enabled\r
56  */\r
57 void ISI_DmaChannelEnable(uint32_t channel)\r
58 {\r
59     REG_ISI_DMA_CHER |= channel;\r
60 }\r
61 \r
62 /**\r
63  * \brief Disable ISI Dma channel\r
64  * \param  channel to be disabled\r
65  */\r
66 void ISI_DmaChannelDisable(uint32_t channel)\r
67 {\r
68     REG_ISI_DMA_CHDR |=channel;\r
69 }\r
70 \r
71 /**\r
72  * \brief Disable ISI\r
73  */\r
74 void ISI_Disable(void)\r
75 {\r
76     /* Write one to this field to disable the module */\r
77     REG_ISI_CR |= ISI_CR_ISI_DIS;\r
78     /* Software must poll DIS_DONE field in the ISI_STATUS register to verify that the command \r
79     has successfully completed.*/\r
80     while( (REG_ISI_SR & ISI_SR_DIS_DONE) != ISI_SR_DIS_DONE);\r
81 }\r
82 \r
83 \r
84 /**\r
85  * \brief Enable ISI interrupt\r
86  * \param  flag of interrupt to enable\r
87  */\r
88 void ISI_EnableInterrupt(uint32_t flag)\r
89 {\r
90     REG_ISI_IER = flag;\r
91 }\r
92 \r
93 /**\r
94  * \brief Disable ISI interrupt\r
95  * \param  flag of interrupt to disable\r
96  */\r
97 void ISI_DisableInterrupt(uint32_t flag)\r
98 {\r
99     REG_ISI_IDR = flag;\r
100 }\r
101 \r
102 /**\r
103  * \brief Return ISI status register\r
104  * \return Status of ISI register\r
105  */\r
106 uint32_t ISI_StatusRegister(void)\r
107 {\r
108     return(REG_ISI_SR);\r
109 }\r
110 \r
111 /**\r
112  * \brief Enable Codec path for capture next frame\r
113  */\r
114 void ISI_CodecPathFull(void)\r
115 {\r
116     /* The codec path is enabled and the next frame is captured.\r
117        Both codec and preview datapaths are working simultaneously */\r
118     REG_ISI_CR |= ISI_CR_ISI_CDC;\r
119     REG_ISI_CFG1 |= ISI_CFG1_FULL;\r
120 }\r
121 \r
122 /**\r
123  * \brief Set frame rate\r
124  * \param frate frame rate capture\r
125  */\r
126 void ISI_SetFrame(uint32_t frate)\r
127 {\r
128     if( frate > 7 ) {\r
129         TRACE_ERROR("FRate too big\n\r");\r
130         frate = 7;\r
131     }\r
132     REG_ISI_CFG1 |= ISI_CFG1_FRATE(frate);\r
133 }\r
134 \r
135 /**\r
136  * \brief Get the number of byte per pixels\r
137  * \param bmpRgb BMP type can be YUV or RGB\r
138  */\r
139 uint8_t ISI_BytesForOnePixel(uint8_t bmpRgb)\r
140 {\r
141     uint8_t nbByte_Pixel;\r
142 \r
143     if (bmpRgb == RGB) {\r
144         if ((REG_ISI_CFG2 & ISI_CFG2_RGB_MODE) == ISI_CFG2_RGB_MODE){\r
145             // RGB: 5:6:5 16bits/pixels\r
146             nbByte_Pixel = 2;\r
147         } \r
148         else {\r
149             // RGB: 8:8:8 24bits/pixels\r
150             nbByte_Pixel = 3;\r
151         }\r
152     } \r
153     else {\r
154         // YUV: 2 pixels for 4 bytes\r
155         nbByte_Pixel = 2;\r
156     }\r
157     return nbByte_Pixel;\r
158 }\r
159 \r
160 \r
161 /**\r
162  * \brief Reset ISI\r
163  */\r
164 void ISI_Reset(void)\r
165 {\r
166     uint32_t timeout=0;\r
167 \r
168     /* Resets the image sensor interface.\r
169        Finish capturing the current frame and then shut down the module.*/\r
170     REG_ISI_CR = ISI_CR_ISI_SRST | ISI_CR_ISI_DIS;\r
171     /* wait Software reset has completed successfully. */\r
172     while( (!(REG_ISI_SR & ISI_SR_SRST))\r
173         && (timeout < 0x5000) ){\r
174         timeout++;\r
175     }\r
176     if( timeout == 0x500000 ) {\r
177         TRACE_ERROR("ISI-Reset timeout\n\r");\r
178     }\r
179 }\r
180 \r
181 /**\r
182  * \brief ISI initialize\r
183  * \param pVideo structure of video driver\r
184  */\r
185 void ISI_Init(pIsi_Video pVideo)\r
186 {\r
187     uint32_t hRatio, vRatio;\r
188     ISI_Reset();\r
189 \r
190     // SLD pixel clock periods to wait before the beginning of a line.\r
191     // SFD lines are skipped at the beginning of the frame.\r
192     REG_ISI_CFG1 |= ISI_CFG1_SLD(pVideo->Hblank) + ISI_CFG1_SFD(pVideo->Vblank);\r
193     TRACE_DEBUG("ISI_CFG1=0x%X\n\r", REG_ISI_CFG1);\r
194 \r
195     // IM_VSIZE: Vertical size of the Image sensor [0..2047]\r
196     // Vertical size = IM_VSIZE + 1\r
197     // IM_HSIZE: Horizontal size of the Image sensor [0..2047]\r
198     // Horizontal size = IM_HSIZE + 1\r
199     // YCC_SWAP : YCC image data    \r
200     REG_ISI_CFG2 = ISI_CFG2_IM_VSIZE(pVideo->codec_vsize - 1)\r
201                  + ISI_CFG2_IM_HSIZE(pVideo->codec_hsize - 1);\r
202     \r
203     if (pVideo->rgb_or_yuv == RGB) {\r
204         REG_ISI_CFG2 |= ISI_CFG2_COL_SPACE | ISI_CFG2_RGB_MODE ;\r
205     }\r
206     else {\r
207          REG_ISI_CFG2|= ISI_CFG2_YCC_SWAP(2) ;\r
208     }\r
209     TRACE_DEBUG("ISI_CFG2=0x%X\n\r", REG_ISI_CFG2);\r
210 \r
211     // Vertical Preview size = PREV_VSIZE + 1 (480 max only in RGB mode).\r
212     // Horizontal Preview size = PREV_HSIZE + 1 (640 max only in RGB mode).\r
213 \r
214     if( (pVideo->lcd_vsize > 480) || (pVideo->lcd_hsize > 800)) {\r
215         TRACE_ERROR("Size LCD bad define %u, %u\n\r",(unsigned int)pVideo->lcd_vsize ,(unsigned int)pVideo->lcd_hsize);\r
216         REG_ISI_PSIZE = ((480 - 1) ) + (((800-1) << 16) );\r
217     }\r
218     else {\r
219         REG_ISI_PSIZE = ((pVideo->lcd_vsize -1)) + (((pVideo->lcd_hsize -1) << 16) );\r
220     }\r
221     \r
222     // DEC_FACTOR is 8-bit width, range is from 16 to 255. \r
223     // Values from 0 to 16 do not perform any decimation.\r
224     //REG_ISI_PDECF = (16 * pVideo->codec_hsize)/640;\r
225     hRatio = (16 * pVideo->codec_hsize)/(pVideo->lcd_hsize); \r
226     vRatio = (16 * pVideo->codec_vsize)/(pVideo->lcd_vsize); \r
227     REG_ISI_PDECF = (hRatio > vRatio )? vRatio: hRatio;\r
228 \r
229     if (REG_ISI_PDECF < 16) REG_ISI_PDECF = 16;\r
230 \r
231     REG_ISI_DMA_P_DSCR = pVideo->Isi_fbd_base;\r
232     REG_ISI_DMA_P_CTRL = ISI_DMA_P_CTRL_P_FETCH;\r
233     REG_ISI_DMA_P_ADDR = pVideo->lcd_fb_addr;\r
234 \r
235     REG_ISI_DMA_C_DSCR = pVideo->codec_fbd_base;\r
236     REG_ISI_DMA_C_CTRL = ISI_DMA_C_CTRL_C_FETCH;\r
237     REG_ISI_DMA_C_ADDR = pVideo->codec_fb_addr;\r
238 \r
239     // C0: Color Space Conversion Matrix Coefficient C0\r
240     // C1: Color Space Conversion Matrix Coefficient C1\r
241     // C2: Color Space Conversion Matrix Coefficient C2\r
242     // C3: Color Space Conversion Matrix Coefficient C3\r
243     REG_ISI_Y2R_SET0  = ISI_Y2R_SET0_C0(0x95)\r
244                       + ISI_Y2R_SET0_C1(0xFF)\r
245                       + ISI_Y2R_SET0_C2(0x68)\r
246                       + ISI_Y2R_SET0_C3(0x32);\r
247 \r
248     // C4: Color Space Conversion Matrix coefficient C4\r
249     // Yoff: Color Space Conversion Luminance 128 offset\r
250     // Croff: Color Space Conversion Red Chrominance 16 offset\r
251     // Cboff: Color Space Conversion Blue Chrominance 16 offset\r
252     REG_ISI_Y2R_SET1  = ISI_Y2R_SET1_C4(0xCC)\r
253                       + ISI_Y2R_SET1_Yoff\r
254                       + ISI_Y2R_SET1_Croff\r
255                       + ISI_Y2R_SET1_Cboff;\r
256 }\r
257 \r