1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_flexio_camera.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.flexio_camera"
14 #endif
15 
16 /*******************************************************************************
17  * Prototypes
18  ******************************************************************************/
19 
20 /*******************************************************************************
21  * Variables
22  ******************************************************************************/
23 
24 /*******************************************************************************
25  * Codes
26  ******************************************************************************/
27 
FLEXIO_CAMERA_GetInstance(FLEXIO_CAMERA_Type * base)28 static uint32_t FLEXIO_CAMERA_GetInstance(FLEXIO_CAMERA_Type *base)
29 {
30     return FLEXIO_GetInstance(base->flexioBase);
31 }
32 
33 /*!
34  * brief Gets the default configuration to configure the FlexIO Camera. The configuration
35  * can be used directly for calling the FLEXIO_CAMERA_Init().
36  * Example:
37    code
38    flexio_camera_config_t config;
39    FLEXIO_CAMERA_GetDefaultConfig(&userConfig);
40    endcode
41  * param config Pointer to the flexio_camera_config_t structure
42 */
FLEXIO_CAMERA_GetDefaultConfig(flexio_camera_config_t * config)43 void FLEXIO_CAMERA_GetDefaultConfig(flexio_camera_config_t *config)
44 {
45     assert(config);
46 
47     /* Initializes the configure structure to zero. */
48     (void)memset(config, 0, sizeof(*config));
49 
50     config->enablecamera     = false;
51     config->enableInDoze     = false;
52     config->enableInDebug    = false;
53     config->enableFastAccess = false;
54 }
55 
56 /*!
57  * brief Ungates the FlexIO clock, resets the FlexIO module, and configures the FlexIO Camera.
58  *
59  * param base Pointer to FLEXIO_CAMERA_Type structure
60  * param config Pointer to flexio_camera_config_t structure
61  */
FLEXIO_CAMERA_Init(FLEXIO_CAMERA_Type * base,const flexio_camera_config_t * config)62 void FLEXIO_CAMERA_Init(FLEXIO_CAMERA_Type *base, const flexio_camera_config_t *config)
63 {
64     assert((base != NULL) && (config != NULL));
65     assert(base->shifterCount > 0U);
66 
67     uint32_t i                   = 0U;
68     volatile uint32_t controlVal = 0U;
69 
70 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
71     /* Ungate flexio clock. */
72     CLOCK_EnableClock(s_flexioClocks[FLEXIO_CAMERA_GetInstance(base)]);
73 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
74 
75     flexio_shifter_config_t shifterConfig;
76     flexio_timer_config_t timerConfig;
77 
78     /* Clear the shifterConfig & timerConfig struct. */
79     (void)memset(&shifterConfig, 0, sizeof(shifterConfig));
80     (void)memset(&timerConfig, 0, sizeof(timerConfig));
81 
82     /* Configure flexio camera */
83     controlVal = base->flexioBase->CTRL;
84     controlVal &=
85         ~(FLEXIO_CTRL_DOZEN_MASK | FLEXIO_CTRL_DBGE_MASK | FLEXIO_CTRL_FASTACC_MASK | FLEXIO_CTRL_FLEXEN_MASK);
86     controlVal |= (FLEXIO_CTRL_DBGE(config->enableInDebug) | FLEXIO_CTRL_FASTACC(config->enableFastAccess) |
87                    FLEXIO_CTRL_FLEXEN(config->enablecamera));
88     if (!config->enableInDoze)
89     {
90         controlVal |= FLEXIO_CTRL_DOZEN_MASK;
91     }
92     base->flexioBase->CTRL = controlVal;
93 
94     /* FLEXIO_CAMERA shifter config */
95     shifterConfig.timerSelect   = base->timerIdx;
96     shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
97     shifterConfig.pinConfig     = kFLEXIO_PinConfigOutputDisabled;
98     shifterConfig.pinSelect     = base->datPinStartIdx;
99     shifterConfig.pinPolarity   = kFLEXIO_PinActiveHigh;
100     shifterConfig.shifterMode   = kFLEXIO_ShifterModeReceive;
101     shifterConfig.parallelWidth = FLEXIO_CAMERA_PARALLEL_DATA_WIDTH - 1U;
102     shifterConfig.inputSource   = kFLEXIO_ShifterInputFromNextShifterOutput;
103     shifterConfig.shifterStop   = kFLEXIO_ShifterStopBitDisable;
104     shifterConfig.shifterStart  = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
105     /* Configure the shifters as FIFO buffer. */
106     for (i = base->shifterStartIdx; i < (base->shifterStartIdx + base->shifterCount - 1U); i++)
107     {
108         FLEXIO_SetShifterConfig(base->flexioBase, (uint8_t)i, &shifterConfig);
109     }
110     shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
111     FLEXIO_SetShifterConfig(base->flexioBase, (uint8_t)i, &shifterConfig);
112 
113     /* FLEXIO_CAMERA timer config, the PCLK's clk is source of timer to drive the shifter, the HREF is the selecting
114      * signal for available data. */
115     timerConfig.triggerSelect   = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->hrefPinIdx);
116     timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
117     timerConfig.triggerSource   = kFLEXIO_TimerTriggerSourceInternal;
118     timerConfig.pinConfig       = kFLEXIO_PinConfigOutputDisabled;
119     timerConfig.pinSelect       = base->pclkPinIdx;
120     timerConfig.pinPolarity     = kFLEXIO_PinActiveHigh;
121     timerConfig.timerMode       = kFLEXIO_TimerModeSingle16Bit;
122     timerConfig.timerOutput     = kFLEXIO_TimerOutputZeroNotAffectedByReset;
123     timerConfig.timerDecrement  = kFLEXIO_TimerDecSrcOnPinInputShiftPinInput;
124     timerConfig.timerReset      = kFLEXIO_TimerResetOnTimerTriggerRisingEdge;
125     timerConfig.timerDisable    = kFLEXIO_TimerDisableOnTriggerFallingEdge;
126     timerConfig.timerEnable     = kFLEXIO_TimerEnableOnTriggerRisingEdge;
127     timerConfig.timerStop       = kFLEXIO_TimerStopBitDisabled;
128     timerConfig.timerStart      = kFLEXIO_TimerStartBitDisabled;
129     timerConfig.timerCompare    = 8U * base->shifterCount - 1U;
130 
131     FLEXIO_SetTimerConfig(base->flexioBase, (uint8_t)base->timerIdx, &timerConfig);
132     /* Clear flags. */
133     FLEXIO_ClearShifterErrorFlags(base->flexioBase, (((1UL << (base->shifterCount)) - 1U) << (base->shifterStartIdx)));
134     FLEXIO_ClearTimerStatusFlags(base->flexioBase, 1UL << (base->timerIdx));
135 }
136 
137 /*!
138  * brief Resets the FLEXIO_CAMERA shifer and timer config.
139  *
140  * note After calling this API, call FLEXO_CAMERA_Init to use the FlexIO Camera module.
141  *
142  * param base Pointer to FLEXIO_CAMERA_Type structure
143  */
FLEXIO_CAMERA_Deinit(FLEXIO_CAMERA_Type * base)144 void FLEXIO_CAMERA_Deinit(FLEXIO_CAMERA_Type *base)
145 {
146     base->flexioBase->SHIFTCFG[base->shifterStartIdx] = 0;
147     base->flexioBase->SHIFTCTL[base->shifterStartIdx] = 0;
148     base->flexioBase->TIMCFG[base->timerIdx]          = 0;
149     base->flexioBase->TIMCMP[base->timerIdx]          = 0;
150     base->flexioBase->TIMCTL[base->timerIdx]          = 0;
151     /* Clear the shifter flag. */
152     base->flexioBase->SHIFTSTAT = (1UL << base->shifterStartIdx);
153     /* Clear the timer flag. */
154     base->flexioBase->TIMSTAT = (1UL << base->timerIdx);
155 }
156 
157 /*!
158  * brief Gets the FlexIO Camera status flags.
159  *
160  * param base Pointer to FLEXIO_CAMERA_Type structure
161  * return FlexIO shifter status flags
162  *          arg FLEXIO_SHIFTSTAT_SSF_MASK
163  *          arg 0
164  */
FLEXIO_CAMERA_GetStatusFlags(FLEXIO_CAMERA_Type * base)165 uint32_t FLEXIO_CAMERA_GetStatusFlags(FLEXIO_CAMERA_Type *base)
166 {
167     uint32_t status = 0;
168     status          = ((FLEXIO_GetShifterStatusFlags(base->flexioBase) >> (base->shifterStartIdx)) &
169               ((1U << (base->shifterCount)) - 1U));
170     return status;
171 }
172 
173 /*!
174  * brief Clears the receive buffer full flag manually.
175  *
176  * param base Pointer to the device.
177  * param mask status flag
178  *      The parameter can be any combination of the following values:
179  *          arg kFLEXIO_CAMERA_RxDataRegFullFlag
180  *          arg kFLEXIO_CAMERA_RxErrorFlag
181  */
FLEXIO_CAMERA_ClearStatusFlags(FLEXIO_CAMERA_Type * base,uint32_t mask)182 void FLEXIO_CAMERA_ClearStatusFlags(FLEXIO_CAMERA_Type *base, uint32_t mask)
183 {
184     if ((mask & (uint32_t)kFLEXIO_CAMERA_RxDataRegFullFlag) != 0U)
185     {
186         FLEXIO_ClearShifterStatusFlags(base->flexioBase, ((1UL << (base->shifterCount)) - 1U)
187                                                              << (base->shifterStartIdx));
188     }
189     if ((mask & (uint32_t)kFLEXIO_CAMERA_RxErrorFlag) != 0U)
190     { /* Clear error flags if they are asserted to make sure the buffer would be available. */
191         FLEXIO_ClearShifterErrorFlags(base->flexioBase, ((1UL << (base->shifterCount)) - 1U)
192                                                             << (base->shifterStartIdx));
193     }
194 }
195 
196 /*!
197  * brief Switches on the interrupt for receive buffer full event.
198  *
199  * param base Pointer to the device.
200  */
FLEXIO_CAMERA_EnableInterrupt(FLEXIO_CAMERA_Type * base)201 void FLEXIO_CAMERA_EnableInterrupt(FLEXIO_CAMERA_Type *base)
202 {
203     FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1UL << (base->shifterStartIdx));
204 }
205 
206 /*!
207  * brief Switches off the interrupt for receive buffer full event.
208  *
209  * param base Pointer to the device.
210  *
211  */
FLEXIO_CAMERA_DisableInterrupt(FLEXIO_CAMERA_Type * base)212 void FLEXIO_CAMERA_DisableInterrupt(FLEXIO_CAMERA_Type *base)
213 {
214     FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1UL << (base->shifterStartIdx));
215 }
216