1 |
580 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
5 |
|
|
*
|
6 |
|
|
* All rights reserved.
|
7 |
|
|
*
|
8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
10 |
|
|
*
|
11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
12 |
|
|
* this list of conditions and the disclaimer below.
|
13 |
|
|
*
|
14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
15 |
|
|
* this software without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
* ----------------------------------------------------------------------------
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
//------------------------------------------------------------------------------
|
31 |
|
|
/// \unit
|
32 |
|
|
///
|
33 |
|
|
/// !Purpose
|
34 |
|
|
///
|
35 |
|
|
/// Image Sensor Interface (ISI) driver
|
36 |
|
|
///
|
37 |
|
|
/// !Usage
|
38 |
|
|
///
|
39 |
|
|
/// Explanation on the usage of the code made available through the header file.
|
40 |
|
|
//------------------------------------------------------------------------------
|
41 |
|
|
|
42 |
|
|
//------------------------------------------------------------------------------
|
43 |
|
|
// Headers
|
44 |
|
|
//------------------------------------------------------------------------------
|
45 |
|
|
|
46 |
|
|
#include <board.h>
|
47 |
|
|
#include <utility/trace.h>
|
48 |
|
|
#include <utility/video.h>
|
49 |
|
|
#include "isi.h"
|
50 |
|
|
|
51 |
|
|
#if !defined (BOARD_ISI_V200)
|
52 |
|
|
|
53 |
|
|
//-----------------------------------------------------------------------------
|
54 |
|
|
/// Enable ISI
|
55 |
|
|
//-----------------------------------------------------------------------------
|
56 |
|
|
void ISI_Enable(void)
|
57 |
|
|
{
|
58 |
|
|
AT91C_BASE_ISI->ISI_CR1 &= ~AT91C_ISI_DIS;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
//-----------------------------------------------------------------------------
|
62 |
|
|
/// Disable ISI
|
63 |
|
|
//-----------------------------------------------------------------------------
|
64 |
|
|
void ISI_Disable(void)
|
65 |
|
|
{
|
66 |
|
|
AT91C_BASE_ISI->ISI_CR1 |= AT91C_ISI_DIS;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
//-----------------------------------------------------------------------------
|
70 |
|
|
/// Enable ISI interrupt
|
71 |
|
|
/// \param flag of interrupt to enable
|
72 |
|
|
//-----------------------------------------------------------------------------
|
73 |
|
|
void ISI_EnableInterrupt(unsigned int flag)
|
74 |
|
|
{
|
75 |
|
|
AT91C_BASE_ISI->ISI_IER = flag;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
//-----------------------------------------------------------------------------
|
79 |
|
|
/// Disable ISI interrupt
|
80 |
|
|
/// \param flag of interrupt to disable
|
81 |
|
|
//-----------------------------------------------------------------------------
|
82 |
|
|
void ISI_DisableInterrupt(unsigned int flag)
|
83 |
|
|
{
|
84 |
|
|
AT91C_BASE_ISI->ISI_IDR = flag;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
//-----------------------------------------------------------------------------
|
88 |
|
|
/// Return ISI status register
|
89 |
|
|
/// \return Status of ISI register
|
90 |
|
|
//-----------------------------------------------------------------------------
|
91 |
|
|
unsigned int ISI_StatusRegister(void)
|
92 |
|
|
{
|
93 |
|
|
return(AT91C_BASE_ISI->ISI_SR);
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
//-----------------------------------------------------------------------------
|
97 |
|
|
/// Enable Codec path for capture next frame
|
98 |
|
|
//-----------------------------------------------------------------------------
|
99 |
|
|
void ISI_CodecPathFull(void)
|
100 |
|
|
{
|
101 |
|
|
// The codec path is enabled and the next frame is captured.
|
102 |
|
|
// Both codec and preview datapaths are working simultaneously
|
103 |
|
|
AT91C_BASE_ISI->ISI_CR1 |= AT91C_ISI_CODEC_ON | AT91C_ISI_FULL;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
//-----------------------------------------------------------------------------
|
107 |
|
|
/// Set frame rate
|
108 |
|
|
/// \param frate frame rate capture
|
109 |
|
|
/// \return
|
110 |
|
|
//-----------------------------------------------------------------------------
|
111 |
|
|
void ISI_SetFrame(unsigned int frate)
|
112 |
|
|
{
|
113 |
|
|
if( frate > 7 ) {
|
114 |
|
|
TRACE_ERROR("FRate too big\n\r");
|
115 |
|
|
frate = 7;
|
116 |
|
|
}
|
117 |
|
|
AT91C_BASE_ISI->ISI_CR1 |= ((frate<<8) & AT91C_ISI_FRATE);
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
//-----------------------------------------------------------------------------
|
121 |
|
|
/// Get the number of byte per pixels
|
122 |
|
|
/// \param bmpRgb BMP type can be YUV or RGB
|
123 |
|
|
/// \return Number of byte for one pixel
|
124 |
|
|
//-----------------------------------------------------------------------------
|
125 |
|
|
unsigned char ISI_BytesForOnePixel(unsigned char bmpRgb)
|
126 |
|
|
{
|
127 |
|
|
unsigned char nbByte_Pixel;
|
128 |
|
|
|
129 |
|
|
if (bmpRgb == RGB) {
|
130 |
|
|
if ((AT91C_BASE_ISI->ISI_CR2 & AT91C_ISI_RGB_MODE) == AT91C_ISI_RGB_MODE_RGB_565){
|
131 |
|
|
// RGB: 5:6:5 16bits/pixels
|
132 |
|
|
nbByte_Pixel = 2;
|
133 |
|
|
}
|
134 |
|
|
else {
|
135 |
|
|
// RGB: 8:8:8 24bits/pixels
|
136 |
|
|
nbByte_Pixel = 3;
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
|
|
else {
|
140 |
|
|
// YUV: 2 pixels for 4 bytes
|
141 |
|
|
nbByte_Pixel = 2;
|
142 |
|
|
}
|
143 |
|
|
return nbByte_Pixel;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
//-----------------------------------------------------------------------------
|
147 |
|
|
/// Reset ISI
|
148 |
|
|
//-----------------------------------------------------------------------------
|
149 |
|
|
void ISI_Reset(void)
|
150 |
|
|
{
|
151 |
|
|
unsigned int timeout=0;
|
152 |
|
|
|
153 |
|
|
// Resets the image sensor interface.
|
154 |
|
|
// Finish capturing the current frame and then shut down the module.
|
155 |
|
|
AT91C_BASE_ISI->ISI_CR1 = AT91C_ISI_RST | AT91C_ISI_DIS;
|
156 |
|
|
// wait Software reset has completed successfully.
|
157 |
|
|
while( (!(volatile int)AT91C_BASE_ISI->ISI_SR & AT91C_ISI_SOFTRST)
|
158 |
|
|
&& (timeout < 0x5000) ){
|
159 |
|
|
timeout++;
|
160 |
|
|
}
|
161 |
|
|
if( timeout == 0x5000 ) {
|
162 |
|
|
TRACE_ERROR("ISI-Reset timeout\n\r");
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
//-----------------------------------------------------------------------------
|
167 |
|
|
/// ISI initialize with the pVideo parameters.
|
168 |
|
|
/// By default, put ISI in RGB mode 565, YCC mode 3, different value for
|
169 |
|
|
/// Color Space Conversion Matrix Coefficient
|
170 |
|
|
/// \param pVideo structure of video driver
|
171 |
|
|
//-----------------------------------------------------------------------------
|
172 |
|
|
void ISI_Init(AT91PS_VIDEO pVideo)
|
173 |
|
|
{
|
174 |
|
|
ISI_Reset();
|
175 |
|
|
|
176 |
|
|
// AT91C_ISI_HSYNC_POL Horizontal synchronisation polarity
|
177 |
|
|
// AT91C_ISI_VSYNC_POL Vertical synchronisation polarity
|
178 |
|
|
// AT91C_ISI_PIXCLK_POL Pixel Clock Polarity
|
179 |
|
|
|
180 |
|
|
// SLD pixel clock periods to wait before the beginning of a line.
|
181 |
|
|
// SFD lines are skipped at the beginning of the frame.
|
182 |
|
|
AT91C_BASE_ISI->ISI_CR1 |= ((pVideo->Hblank << 16) & AT91C_ISI_SLD)
|
183 |
|
|
+ ((pVideo->Vblank << 24) & AT91C_ISI_SFD);
|
184 |
|
|
TRACE_DEBUG("ISI_CR1=0x%X\n\r", AT91C_BASE_ISI->ISI_CR1);
|
185 |
|
|
|
186 |
|
|
// IM_VSIZE: Vertical size of the Image sensor [0..2047]
|
187 |
|
|
// Vertical size = IM_VSIZE + 1
|
188 |
|
|
// IM_HSIZE: Horizontal size of the Image sensor [0..2047]
|
189 |
|
|
// Horizontal size = IM_HSIZE + 1
|
190 |
|
|
// YCC_SWAP : YCC image data
|
191 |
|
|
AT91C_BASE_ISI->ISI_CR2 = ((pVideo->codec_vsize-1) & AT91C_ISI_IM_VSIZE)
|
192 |
|
|
+ (((pVideo->codec_hsize-1) << 16) & AT91C_ISI_IM_HSIZE)
|
193 |
|
|
+ AT91C_ISI_YCC_SWAP_YCC_MODE2;
|
194 |
|
|
|
195 |
|
|
if (pVideo->rgb_or_yuv == RGB) {
|
196 |
|
|
AT91C_BASE_ISI->ISI_CR2 |= AT91C_ISI_COL_SPACE | AT91C_ISI_RGB_MODE_RGB_565
|
197 |
|
|
| AT91C_ISI_RGB_CFG_RGB_DEFAULT;
|
198 |
|
|
}
|
199 |
|
|
else {
|
200 |
|
|
// AT91C_BASE_HISI->ISI_CR2 &= ~AT91C_ISI_COL_SPACE;
|
201 |
|
|
}
|
202 |
|
|
TRACE_DEBUG("ISI_CR2=0x%X\n\r", AT91C_BASE_ISI->ISI_CR2);
|
203 |
|
|
|
204 |
|
|
// Vertical Preview size = PREV_VSIZE + 1 (480 max only in RGB mode).
|
205 |
|
|
// Horizontal Preview size = PREV_HSIZE + 1 (640 max only in RGB mode).
|
206 |
|
|
#if defined (AT91C_ID_LCDC)
|
207 |
|
|
if( (pVideo->lcd_vsize > 480) || (pVideo->lcd_hsize > 640)) {
|
208 |
|
|
TRACE_ERROR("Size LCD bad define\n\r");
|
209 |
|
|
AT91C_BASE_ISI->ISI_PSIZE = ((BOARD_LCD_HEIGHT-1) & AT91C_ISI_PREV_VSIZE)
|
210 |
|
|
+ (((BOARD_LCD_WIDTH-1) << 16) & AT91C_ISI_PREV_HSIZE);
|
211 |
|
|
}
|
212 |
|
|
else {
|
213 |
|
|
|
214 |
|
|
AT91C_BASE_ISI->ISI_PSIZE = ((pVideo->lcd_vsize -1) & AT91C_ISI_PREV_VSIZE)
|
215 |
|
|
+ (((pVideo->lcd_hsize -1) << 16) & AT91C_ISI_PREV_HSIZE);
|
216 |
|
|
}
|
217 |
|
|
#endif
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
// DEC_FACTOR is 8-bit width, range is from 16 to 255.
|
221 |
|
|
// Values from 0 to 16 do not perform any decimation.
|
222 |
|
|
AT91C_BASE_ISI->ISI_PDECF = (16 * pVideo->codec_hsize) / pVideo->lcd_hsize;
|
223 |
|
|
|
224 |
|
|
TRACE_DEBUG("codec_hsize: %d\n\r", pVideo->codec_hsize);
|
225 |
|
|
TRACE_DEBUG("lcd_hsize: %d\n\r", pVideo->lcd_hsize);
|
226 |
|
|
TRACE_DEBUG("ISI_PDECF: %d\n\r", AT91C_BASE_ISI->ISI_PDECF);
|
227 |
|
|
if( AT91C_BASE_ISI->ISI_PDECF <16) {
|
228 |
|
|
TRACE_ERROR("ISI_PDECF, forbidden value: %d\n\r", AT91C_BASE_ISI->ISI_PDECF);
|
229 |
|
|
AT91C_BASE_ISI->ISI_PDECF = 16;
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
// Written with the address of the start of the preview frame buffer queue,
|
233 |
|
|
// reads as a pointer to the current buffer being used.
|
234 |
|
|
// The frame buffer is forced to word alignment.
|
235 |
|
|
AT91C_BASE_ISI->ISI_PPFBD = pVideo->Isi_fbd_base;
|
236 |
|
|
|
237 |
|
|
// This register contains codec datapath start address of buffer location.
|
238 |
|
|
// CODEC_DMA_ADDR: Base address for codec DMA
|
239 |
|
|
AT91C_BASE_ISI->ISI_CDBA = pVideo->codec_fb_addr;
|
240 |
|
|
|
241 |
|
|
// C0: Color Space Conversion Matrix Coefficient C0
|
242 |
|
|
// C1: Color Space Conversion Matrix Coefficient C1
|
243 |
|
|
// C2: Color Space Conversion Matrix Coefficient C2
|
244 |
|
|
// C3: Color Space Conversion Matrix Coefficient C3
|
245 |
|
|
AT91C_BASE_ISI->ISI_Y2RSET0 = ( (0x95<< 0) & AT91C_ISI_Y2R_C0)
|
246 |
|
|
+ ( (0xFF<< 8) & AT91C_ISI_Y2R_C1)
|
247 |
|
|
+ ( (0x68<<16) & AT91C_ISI_Y2R_C2)
|
248 |
|
|
+ ( (0x32<<24) & AT91C_ISI_Y2R_C3);
|
249 |
|
|
|
250 |
|
|
// C4: Color Space Conversion Matrix coefficient C4
|
251 |
|
|
// Yoff: Color Space Conversion Luminance 128 offset
|
252 |
|
|
// Croff: Color Space Conversion Red Chrominance 16 offset
|
253 |
|
|
// Cboff: Color Space Conversion Blue Chrominance 16 offset
|
254 |
|
|
AT91C_BASE_ISI->ISI_Y2RSET1 = ( (0xCC<< 0) & AT91C_ISI_Y2R_C4)
|
255 |
|
|
+ ( AT91C_ISI_Y2R_YOFF_128)
|
256 |
|
|
+ ( AT91C_ISI_Y2R_CROFF_16)
|
257 |
|
|
+ ( AT91C_ISI_Y2R_CBOFF_16);
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
#endif // !defined (BOARD_ISI_V200)
|
261 |
|
|
|