1 /*
2 * Copyright (c) 2015-2017, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * ======== Camera.c ========
34 */
35
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40
41 #include <ti/drivers/Camera.h>
42 #include <ti/drivers/dpl/HwiP.h>
43
44 extern const Camera_Config Camera_config[];
45 extern const uint_least8_t Camera_count;
46
47 /* Default Camera parameters structure */
48 const Camera_Params Camera_defaultParams = {
49 Camera_MODE_BLOCKING, /* captureMode */
50 24000000, /* outputClock */
51 Camera_HSYNC_POLARITY_HIGH, /* hsyncPolarity */
52 Camera_VSYNC_POLARITY_HIGH, /* vsyncPolarity */
53 Camera_PCLK_CONFIG_RISING_EDGE, /* pixelClkConfig */
54 Camera_BYTE_ORDER_NORMAL, /* byteOrder */
55 Camera_INTERFACE_SYNC_ON, /* interfaceSync */
56 Camera_STOP_CAPTURE_FRAME_END, /* stopConfig */
57 Camera_START_CAPTURE_FRAME_START, /* startConfig */
58 Camera_WAIT_FOREVER, /* captureTimeout */
59 NULL, /* captureCallback */
60 NULL
61 };
62
63 static bool isInitialized = false;
64
65 /*
66 * ======== Camera_close ========
67 */
Camera_close(Camera_Handle handle)68 void Camera_close(Camera_Handle handle)
69 {
70 handle->fxnTablePtr->closeFxn(handle);
71 }
72
73 /*
74 * ======== Camera_control ========
75 */
Camera_control(Camera_Handle handle,uint_fast16_t cmd,void * arg)76 int_fast16_t Camera_control(Camera_Handle handle, uint_fast16_t cmd, void *arg)
77 {
78 return (handle->fxnTablePtr->controlFxn(handle, cmd, arg));
79 }
80
81 /*
82 * ======== Camera_init ========
83 */
Camera_init(void)84 void Camera_init(void)
85 {
86 uint_least8_t i;
87 uint_fast32_t key;
88
89 key = HwiP_disable();
90
91 if (!isInitialized) {
92 isInitialized = (bool) true;
93
94 /* Call each driver's init function */
95 for (i = 0; i < Camera_count; i++) {
96 Camera_config[i].fxnTablePtr->initFxn((Camera_Handle)&(Camera_config[i]));
97 }
98 }
99
100 HwiP_restore(key);
101 }
102
103 /*
104 * ======== Camera_open ========
105 */
Camera_open(uint_least8_t index,Camera_Params * params)106 Camera_Handle Camera_open(uint_least8_t index, Camera_Params *params)
107 {
108 Camera_Handle handle = NULL;
109
110 /* Verify driver index and state */
111 if (isInitialized && (index < Camera_count)) {
112 /* If params are NULL use defaults. */
113 if (params == NULL) {
114 params = (Camera_Params *)&Camera_defaultParams;
115 }
116
117 /* Get handle for this driver instance */
118 handle = (Camera_Handle)&(Camera_config[index]);
119 handle = handle->fxnTablePtr->openFxn(handle, params);
120 }
121
122 return (handle);
123 }
124
125 /*
126 * ======== Camera_Params_init ========
127 */
Camera_Params_init(Camera_Params * params)128 void Camera_Params_init(Camera_Params *params)
129 {
130 *params = Camera_defaultParams;
131 }
132
133 /*
134 * ======== Camera_capture ========
135 */
Camera_capture(Camera_Handle handle,void * buffer,size_t bufferlen,size_t * frameLen)136 int_fast16_t Camera_capture(Camera_Handle handle, void *buffer,
137 size_t bufferlen, size_t *frameLen)
138 {
139 return (handle->fxnTablePtr->captureFxn(handle, buffer, bufferlen, frameLen));
140 }
141