summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/atsam/libraries/libchip/source/isi.c
blob: 52aed9772cb313d59316e9574921e31d5b9635fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* ---------------------------------------------------------------------------- */
/*                  Atmel Microcontroller Software Support                      */
/*                       SAM Software Package License                           */
/* ---------------------------------------------------------------------------- */
/* Copyright (c) 2015, Atmel Corporation                                        */
/*                                                                              */
/* All rights reserved.                                                         */
/*                                                                              */
/* Redistribution and use in source and binary forms, with or without           */
/* modification, are permitted provided that the following condition is met:    */
/*                                                                              */
/* - Redistributions of source code must retain the above copyright notice,     */
/* this list of conditions and the disclaimer below.                            */
/*                                                                              */
/* Atmel's name may not be used to endorse or promote products derived from     */
/* this software without specific prior written permission.                     */
/*                                                                              */
/* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
/* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
/* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
/* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
/* ---------------------------------------------------------------------------- */

/*----------------------------------------------------------------------------
 *        Headers
 *----------------------------------------------------------------------------*/

#include "chip.h"

/*----------------------------------------------------------------------------
 *        Local functions
 *----------------------------------------------------------------------------*/

/**
 * \brief Workaround for ISI CFG2 register read.
 * \note The ISI_CFG2[31:27] can be written correctly, because the input writing
 * data are assigned directly to the internal control bits as specified,
 * the mismatch only happens in reading operation.
 * [31:28] are shift right 1 bit, so [31:27] can be read from [30:27].
 */
__STATIC_INLINE uint32_t _ISI_GetCFG2_Workaround(void)
{
	uint32_t wrongfield;
	wrongfield = ISI->ISI_CFG2 >> (ISI_CFG2_YCC_SWAP_Pos - 1);
	return (ISI->ISI_CFG2 & 0x07FFFFFF) | (wrongfield << ISI_CFG2_YCC_SWAP_Pos);
}

/*----------------------------------------------------------------------------
 *        Export functions
 *----------------------------------------------------------------------------*/

/**
 * \brief Enable ISI
 */
void ISI_Enable(void)
{
	ISI->ISI_CR |= ISI_CR_ISI_EN;

	while ((ISI->ISI_SR & ISI_CR_ISI_EN) != ISI_CR_ISI_EN);
}

/**
 * \brief Enable ISI Dma channel
 * \param  channel to be enabled
 */
void ISI_DmaChannelEnable(uint32_t channel)
{
	ISI->ISI_DMA_CHER |= channel;
}

/**
 * \brief Disable ISI Dma channel
 * \param  channel to be disabled
 */
void ISI_DmaChannelDisable(uint32_t channel)
{
	ISI->ISI_DMA_CHDR |= channel;
}

/**
 * \brief Disable ISI
 */
void ISI_Disable(void)
{
	/* Write one to this field to disable the module */
	ISI->ISI_CR |= ISI_CR_ISI_DIS;

	/* Software must poll DIS_DONE field in the ISI_STATUS register to verify that the command
	has successfully completed.*/
	while ((ISI->ISI_SR & ISI_SR_DIS_DONE) != ISI_SR_DIS_DONE);
}


/**
 * \brief Enable ISI interrupt
 * \param  flag of interrupt to enable
 */
void ISI_EnableInterrupt(uint32_t flag)
{
	ISI->ISI_IER = flag;
}

/**
 * \brief Disable ISI interrupt
 * \param  flag of interrupt to disable
 */
void ISI_DisableInterrupt(uint32_t flag)
{
	ISI->ISI_IDR = flag;
}

/**
 * \brief Return ISI status register
 * \return Status of ISI register
 */
uint32_t ISI_StatusRegister(void)
{
	return (ISI->ISI_SR);
}

/**
 * \brief Enable Codec path for capture next frame
 */
void ISI_CodecPathFull(void)
{
	// The codec path is enabled and the next frame is captured.
	// Both codec and preview data-paths are working simultaneously
	ISI->ISI_CR |= ISI_CR_ISI_CDC;
	ISI->ISI_CFG1 |= ISI_CFG1_FULL;
}

/**
 * \brief Set frame rate
 * \param frame frame rate capture
 */
void ISI_SetFrameRate(uint32_t frame)
{
	if (frame > 7) {
		TRACE_ERROR("rate too big\n\r");
		frame = 7;
	}

	ISI->ISI_CFG1 |= ISI_CFG1_FRATE(frame);
}

/**
 * \brief Get the number of byte per pixels
 * \param bmpRgb BMP type can be YUV or RGB
 */
uint8_t ISI_BytesForOnePixel(uint8_t bmpRgb)
{
	uint8_t nbByte_Pixel;

	if (bmpRgb == RGB) {
		if ((_ISI_GetCFG2_Workaround() & ISI_CFG2_RGB_MODE) == ISI_CFG2_RGB_MODE) {
			// RGB: 5:6:5 16bits/pixels
			nbByte_Pixel = 2;
		} else {
			// RGB: 8:8:8 24bits/pixels
			nbByte_Pixel = 3;
		}
	} else {
		// YUV: 2 pixels for 4 bytes
		nbByte_Pixel = 2;
	}

	return nbByte_Pixel;
}

/**
 * \brief Reset ISI
 */
void ISI_Reset(void)
{
	uint32_t timeout = 0;

	// Resets the image sensor interface.
	// Finish capturing the current frame and then shut down the module.
	ISI->ISI_CR = ISI_CR_ISI_SRST | ISI_CR_ISI_DIS;

	// wait Software reset has completed successfully.
	while ((!(ISI->ISI_SR & ISI_SR_SRST))
			&& (timeout < 0x5000))
		timeout++;

	if (timeout == 0x5000)
		TRACE_ERROR("ISI-Reset timeout\n\r");
}


/**
 * \brief Set the windows blank
 * \param hBlank  pixel clock periods to wait before the beginning of a line.
 * \param vBlank  lines are skipped at the beginning of the frame.
 */
void ISI_SetBlank(uint8_t hBlank, uint8_t vBlank)
{
	ISI->ISI_CFG1 |= ISI_CFG1_SLD(hBlank) + ISI_CFG1_SFD(vBlank);
}


/**
 * \brief Set vertical and horizontal Size of the Image Sensor
 * \param hSize  Horizontal size of the Image sensor [0..2047].
 * \param vSize  Vertical size of the Image sensor [0..2047].
 */
void ISI_SetSensorSize(uint32_t hSize, uint32_t vSize)
{
	uint32_t val;
	val = _ISI_GetCFG2_Workaround();
	val &= (~ISI_CFG2_IM_VSIZE_Msk);
	val &= (~ISI_CFG2_IM_HSIZE_Msk);
	// IM_VSIZE: Vertical size of the Image sensor [0..2047]
	// Vertical size = IM_VSIZE + 1
	// IM_HSIZE: Horizontal size of the Image sensor [0..2047]
	// Horizontal size = IM_HSIZE + 1
	ISI->ISI_CFG2 &= (~ISI_CFG2_IM_VSIZE_Msk);
	ISI->ISI_CFG2 &= (~ISI_CFG2_IM_HSIZE_Msk);
	ISI->ISI_CFG2 = val | ISI_CFG2_IM_VSIZE(vSize - 1) | ISI_CFG2_IM_HSIZE(
						hSize - 1);
}

/**
 * \brief Defines RGB pattern when RGB_MODE is set to 1.
 * \param wRgbPixelMapping  RGB pattern
 */
void ISI_RgbPixelMapping(uint32_t wRgbPixelMapping)
{
	ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() & (~ISI_CFG2_RGB_CFG_Msk);

	if (wRgbPixelMapping != ISI_CFG2_RGB_CFG_DEFAULT)
		ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() | wRgbPixelMapping
						| ISI_CFG2_RGB_MODE;
	else
		ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround();
}

/**
 * \brief Enables RGB swap
 * \param swapMode  0: D7-R7, 1: D0-R7
 */
void ISI_RgbSwapMode(uint32_t swapMode)
{
	ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() & (~ISI_CFG2_RGB_SWAP);

	if (swapMode) ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() | ISI_CFG2_RGB_SWAP;
}

/**
 * \brief Defines YCrCb swap format.
 * \param wYuvSwapMode YUV Swap format
 */
void ISI_YCrCbFormat(uint32_t wYuvSwapMode)
{
	ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() & (~ISI_CFG2_YCC_SWAP_Msk);
	ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() | wYuvSwapMode;
}

/**
 * \brief Input image is assumed to be grayscale-coded.
 * \param wPixelFormat  0: 2 pixels per word, 1:1 pixel per word.
 */
void ISI_setGrayScaleMode(uint32_t wPixelFormat)
{
	ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() | ISI_CFG2_GRAYSCALE;

	if (wPixelFormat) ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() | ISI_CFG2_GS_MODE;

}

/**
 * \brief Set data stream format.
 * \param wStreamMode  0: YUV input, 1: RGB 8:8:8/5:6:5 input
 */
void ISI_setInputStream(uint32_t wStreamMode)
{
	ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() & (~ISI_CFG2_COL_SPACE);

	if (wStreamMode) ISI->ISI_CFG2 = _ISI_GetCFG2_Workaround() | ISI_CFG2_COL_SPACE;
}

/**
 * \brief Set preview size.
 * \param hSize  Horizontal Preview size  (640 max only in RGB mode).
 * \param vSize  Vertical Preview size  (480 max only in RGB mode).
 */
void ISI_setPreviewSize(uint32_t hSize, uint32_t vSize)
{
	if (hSize > 640) hSize = 640;

	if (vSize > 480) vSize = 480;

	ISI->ISI_PSIZE = ISI_PSIZE_PREV_VSIZE(vSize - 1) | ISI_PSIZE_PREV_HSIZE(
						 hSize - 1);
}

/**
 * \brief calculate scaler factor automatically.
 * \note The sensor size and preview size for LCD was configured before this setting.
 */
void ISI_calcScalerFactor(void)
{
	uint32_t hLcdSize, hSensorSize;
	uint32_t hRatio;
	hLcdSize = ((ISI->ISI_PSIZE & ISI_PSIZE_PREV_HSIZE_Msk) >>
				ISI_PSIZE_PREV_HSIZE_Pos) + 1;
	hSensorSize = ((_ISI_GetCFG2_Workaround() & ISI_CFG2_IM_HSIZE_Msk)
				   >> ISI_CFG2_IM_HSIZE_Pos) + 1;
	hRatio = 1600 * hSensorSize / hLcdSize;
	ISI->ISI_PDECF = (hRatio / 100);
}

/**
 * \brief Configure DMA for preview path.
 * \param baseFrameBufDesc  Preview Descriptor Address.
 * \param dmaCtrl  DMA Preview Control.
 * \param frameBufferStartAddr  DMA Preview Base Address.
 */
void ISI_setDmaInPreviewPath(uint32_t baseFrameBufDesc,
							 uint32_t dmaCtrl, uint32_t frameBufferStartAddr)
{
	ISI->ISI_DMA_P_DSCR = baseFrameBufDesc;
	ISI->ISI_DMA_P_CTRL = dmaCtrl;
	ISI->ISI_DMA_P_ADDR = frameBufferStartAddr;
}

/**
 * \brief Configure DMA for Codec path.
 * \param baseFrameBufDesc  Preview Descriptor Address.
 * \param dmaCtrl  DMA Preview Control.
 * \param frameBufferStartAddr  DMA Preview Base Address.
 */
void ISI_setDmaInCodecPath(uint32_t baseFrameBufDesc,
						   uint32_t dmaCtrl, uint32_t frameBufferStartAddr)
{
	ISI->ISI_DMA_C_DSCR = baseFrameBufDesc;
	ISI->ISI_DMA_C_CTRL = dmaCtrl;
	ISI->ISI_DMA_C_ADDR = frameBufferStartAddr;
}

/**
 * \brief ISI set matrix for YUV to RGB color space for preview path.
 * \param yuv2rgb structure of YUV to RBG parameters.
 */
void ISI_SetMatrix4Yuv2Rgb (ISI_Y2R *yuv2rgb)
{
	ISI->ISI_Y2R_SET0 = ISI_Y2R_SET0_C0(yuv2rgb->C0)
						| ISI_Y2R_SET0_C1(yuv2rgb->C1)
						| ISI_Y2R_SET0_C2(yuv2rgb->C2)
						| ISI_Y2R_SET0_C3(yuv2rgb->C3);

	ISI->ISI_Y2R_SET1 = ISI_Y2R_SET1_C4(yuv2rgb->C4)
						| ((yuv2rgb->Yoff == 1) ? ISI_Y2R_SET1_Yoff : 0)
						| ((yuv2rgb->Croff == 1) ? ISI_Y2R_SET1_Croff : 0)
						| ((yuv2rgb->Cboff == 1) ? ISI_Y2R_SET1_Cboff : 0);
}

/**
 * \brief ISI set matrix for RGB to YUV color space for codec path.
 * \param rgb2yuv structure of RGB to YUV parameters.
 */
void ISI_SetMatrix4Rgb2Yuv (ISI_R2Y *rgb2yuv)
{
	ISI->ISI_R2Y_SET0 = ISI_R2Y_SET0_C0(rgb2yuv->C0)
						| ISI_R2Y_SET0_C1(rgb2yuv->C1)
						| ISI_R2Y_SET0_C2(rgb2yuv->C2)
						| ((rgb2yuv->Roff == 1) ? ISI_R2Y_SET0_Roff : 0);

	ISI->ISI_R2Y_SET1 = ISI_R2Y_SET1_C3(rgb2yuv->C3)
						| ISI_R2Y_SET1_C4(rgb2yuv->C4)
						| ISI_R2Y_SET1_C5(rgb2yuv->C5)
						| ((rgb2yuv->Goff == 1) ? ISI_R2Y_SET1_Goff : 0);

	ISI->ISI_R2Y_SET2 = ISI_R2Y_SET2_C6(rgb2yuv->C6)
						| ISI_R2Y_SET2_C7(rgb2yuv->C7)
						| ISI_R2Y_SET2_C8(rgb2yuv->C8)
						| ((rgb2yuv->Boff == 1) ? ISI_R2Y_SET2_Boff : 0);
}