1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #include "fsl_aoi.h"
9 
10 /*******************************************************************************
11  * Definitions
12  ******************************************************************************/
13 /* Component ID definition, used by tools. */
14 #ifndef FSL_COMPONENT_ID
15 #define FSL_COMPONENT_ID "platform.drivers.aoi"
16 #endif
17 
18 #if defined(AOI_RSTS)
19 #define AOI_RESETS_ARRAY AOI_RSTS
20 #endif
21 
22 /*******************************************************************************
23  * Variables
24  ******************************************************************************/
25 /*! @brief Pointers to aoi bases for each instance. */
26 static AOI_Type *const s_aoiBases[] = AOI_BASE_PTRS;
27 
28 #if defined(AOI_RESETS_ARRAY)
29 /* Reset array */
30 static const reset_ip_name_t s_aoiResets[] = AOI_RESETS_ARRAY;
31 #endif
32 
33 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
34 /*! @brief Pointers to aoi clocks for each instance. */
35 static const clock_ip_name_t s_aoiClocks[] = AOI_CLOCKS;
36 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
37 /*******************************************************************************
38  * Prototypes
39  ******************************************************************************/
40 /*!
41  * @brief Get instance number for AOI module.
42  *
43  * @param base AOI peripheral base address
44  *
45  * @return The AOI instance
46  */
47 static uint32_t AOI_GetInstance(AOI_Type *base);
48 /*******************************************************************************
49  * Code
50  ******************************************************************************/
51 
AOI_GetInstance(AOI_Type * base)52 static uint32_t AOI_GetInstance(AOI_Type *base)
53 {
54     uint32_t instance;
55 
56     /* Find the instance index from base address mappings. */
57     for (instance = 0; instance < ARRAY_SIZE(s_aoiBases); instance++)
58     {
59         if (s_aoiBases[instance] == base)
60         {
61             break;
62         }
63     }
64 
65     assert(instance < ARRAY_SIZE(s_aoiBases));
66 
67     return instance;
68 }
69 
70 /*!
71  * brief Initializes an AOI instance for operation.
72  *
73  * This function un-gates the AOI clock.
74  *
75  * param base AOI peripheral address.
76  */
AOI_Init(AOI_Type * base)77 void AOI_Init(AOI_Type *base)
78 {
79 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
80     /* Enable the clock gate from clock manager. */
81     CLOCK_EnableClock(s_aoiClocks[AOI_GetInstance(base)]);
82 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
83 
84 #if defined(AOI_RESETS_ARRAY)
85     RESET_ReleasePeripheralReset(s_aoiResets[AOI_GetInstance(base)]);
86 #endif
87 }
88 
89 /*!
90  * brief Deinitializes an AOI instance for operation.
91  *
92  * This function shutdowns AOI module.
93  *
94  * param  base AOI peripheral address.
95  */
AOI_Deinit(AOI_Type * base)96 void AOI_Deinit(AOI_Type *base)
97 {
98 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
99     /* Disable the clock gate from clock manager */
100     CLOCK_DisableClock(s_aoiClocks[AOI_GetInstance(base)]);
101 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
102 }
103 
104 /*!
105  * brief Gets the Boolean evaluation associated.
106  *
107  * This function returns the Boolean evaluation associated.
108  *
109  * Example:
110   code
111     aoi_event_config_t demoEventLogicStruct;
112 
113     AOI_GetEventLogicConfig(AOI, kAOI_Event0, &demoEventLogicStruct);
114   endcode
115  *
116  * param  base AOI peripheral address.
117  * param  event Index of the event which will be set of type aoi_event_t.
118  * param  config Selected input configuration .
119  */
AOI_GetEventLogicConfig(AOI_Type * base,aoi_event_t event,aoi_event_config_t * config)120 void AOI_GetEventLogicConfig(AOI_Type *base, aoi_event_t event, aoi_event_config_t *config)
121 {
122     assert((uint32_t)event < (uint32_t)FSL_FEATURE_AOI_EVENT_COUNT);
123     assert(config != NULL);
124 
125     uint16_t value;
126     uint16_t temp;
127     /* Read BFCRT01 register at event index. */
128     value = base->BFCRT[event].BFCRT01;
129 
130     temp          = (value & AOI_BFCRT01_PT0_AC_MASK) >> AOI_BFCRT01_PT0_AC_SHIFT;
131     config->PT0AC = (aoi_input_config_t)temp;
132     temp          = (value & AOI_BFCRT01_PT0_BC_MASK) >> AOI_BFCRT01_PT0_BC_SHIFT;
133     config->PT0BC = (aoi_input_config_t)temp;
134     temp          = (value & AOI_BFCRT01_PT0_CC_MASK) >> AOI_BFCRT01_PT0_CC_SHIFT;
135     config->PT0CC = (aoi_input_config_t)temp;
136     temp          = (value & AOI_BFCRT01_PT0_DC_MASK) >> AOI_BFCRT01_PT0_DC_SHIFT;
137     config->PT0DC = (aoi_input_config_t)temp;
138 
139     temp          = (value & AOI_BFCRT01_PT1_AC_MASK) >> AOI_BFCRT01_PT1_AC_SHIFT;
140     config->PT1AC = (aoi_input_config_t)temp;
141     temp          = (value & AOI_BFCRT01_PT1_BC_MASK) >> AOI_BFCRT01_PT1_BC_SHIFT;
142     config->PT1BC = (aoi_input_config_t)temp;
143     temp          = (value & AOI_BFCRT01_PT1_CC_MASK) >> AOI_BFCRT01_PT1_CC_SHIFT;
144     config->PT1CC = (aoi_input_config_t)temp;
145     temp          = (value & AOI_BFCRT01_PT1_DC_MASK) >> AOI_BFCRT01_PT1_DC_SHIFT;
146     config->PT1DC = (aoi_input_config_t)temp;
147 
148     /* Read BFCRT23 register at event index. */
149     value = base->BFCRT[event].BFCRT23;
150 
151     temp          = (value & AOI_BFCRT23_PT2_AC_MASK) >> AOI_BFCRT23_PT2_AC_SHIFT;
152     config->PT2AC = (aoi_input_config_t)temp;
153     temp          = (value & AOI_BFCRT23_PT2_BC_MASK) >> AOI_BFCRT23_PT2_BC_SHIFT;
154     config->PT2BC = (aoi_input_config_t)temp;
155     temp          = (value & AOI_BFCRT23_PT2_CC_MASK) >> AOI_BFCRT23_PT2_CC_SHIFT;
156     config->PT2CC = (aoi_input_config_t)temp;
157     temp          = (value & AOI_BFCRT23_PT2_DC_MASK) >> AOI_BFCRT23_PT2_DC_SHIFT;
158     config->PT2DC = (aoi_input_config_t)temp;
159 
160     temp          = (value & AOI_BFCRT23_PT3_AC_MASK) >> AOI_BFCRT23_PT3_AC_SHIFT;
161     config->PT3AC = (aoi_input_config_t)temp;
162     temp          = (value & AOI_BFCRT23_PT3_BC_MASK) >> AOI_BFCRT23_PT3_BC_SHIFT;
163     config->PT3BC = (aoi_input_config_t)temp;
164     temp          = (value & AOI_BFCRT23_PT3_CC_MASK) >> AOI_BFCRT23_PT3_CC_SHIFT;
165     config->PT3CC = (aoi_input_config_t)temp;
166     temp          = (value & AOI_BFCRT23_PT3_DC_MASK) >> AOI_BFCRT23_PT3_DC_SHIFT;
167     config->PT3DC = (aoi_input_config_t)temp;
168 }
169 
170 /*!
171  * brief Configures an AOI event.
172  *
173  * This function configures an AOI event according
174  * to the aoiEventConfig structure. This function configures all  inputs (A, B, C, and D)
175  * of all  product terms (0, 1, 2, and 3) of a desired event.
176  *
177  * Example:
178   code
179     aoi_event_config_t demoEventLogicStruct;
180 
181     demoEventLogicStruct.PT0AC = kAOI_InvInputSignal;
182     demoEventLogicStruct.PT0BC = kAOI_InputSignal;
183     demoEventLogicStruct.PT0CC = kAOI_LogicOne;
184     demoEventLogicStruct.PT0DC = kAOI_LogicOne;
185 
186     demoEventLogicStruct.PT1AC = kAOI_LogicZero;
187     demoEventLogicStruct.PT1BC = kAOI_LogicOne;
188     demoEventLogicStruct.PT1CC = kAOI_LogicOne;
189     demoEventLogicStruct.PT1DC = kAOI_LogicOne;
190 
191     demoEventLogicStruct.PT2AC = kAOI_LogicZero;
192     demoEventLogicStruct.PT2BC = kAOI_LogicOne;
193     demoEventLogicStruct.PT2CC = kAOI_LogicOne;
194     demoEventLogicStruct.PT2DC = kAOI_LogicOne;
195 
196     demoEventLogicStruct.PT3AC = kAOI_LogicZero;
197     demoEventLogicStruct.PT3BC = kAOI_LogicOne;
198     demoEventLogicStruct.PT3CC = kAOI_LogicOne;
199     demoEventLogicStruct.PT3DC = kAOI_LogicOne;
200 
201     AOI_SetEventLogicConfig(AOI, kAOI_Event0, demoEventLogicStruct);
202   endcode
203  *
204  * param  base AOI peripheral address.
205  * param  event Event which will be configured of type aoi_event_t.
206  * param  eventConfig Pointer to type aoi_event_config_t structure. The user is responsible for
207  * filling out the members of this structure and passing the pointer to this function.
208  */
AOI_SetEventLogicConfig(AOI_Type * base,aoi_event_t event,const aoi_event_config_t * eventConfig)209 void AOI_SetEventLogicConfig(AOI_Type *base, aoi_event_t event, const aoi_event_config_t *eventConfig)
210 {
211     assert(eventConfig != NULL);
212     assert((uint32_t)event < (uint32_t)FSL_FEATURE_AOI_EVENT_COUNT);
213 
214     uint16_t value;
215     /* Calculate value to configure product term 0, 1 */
216     value = AOI_BFCRT01_PT0_AC(eventConfig->PT0AC) | AOI_BFCRT01_PT0_BC(eventConfig->PT0BC) |
217             AOI_BFCRT01_PT0_CC(eventConfig->PT0CC) | AOI_BFCRT01_PT0_DC(eventConfig->PT0DC) |
218             AOI_BFCRT01_PT1_AC(eventConfig->PT1AC) | AOI_BFCRT01_PT1_BC(eventConfig->PT1BC) |
219             AOI_BFCRT01_PT1_CC(eventConfig->PT1CC) | AOI_BFCRT01_PT1_DC(eventConfig->PT1DC);
220     /* Write value to register */
221     base->BFCRT[event].BFCRT01 = value;
222 
223     /* Reset and calculate value to configure product term 2, 3 */
224     value = AOI_BFCRT23_PT2_AC(eventConfig->PT2AC) | AOI_BFCRT23_PT2_BC(eventConfig->PT2BC) |
225             AOI_BFCRT23_PT2_CC(eventConfig->PT2CC) | AOI_BFCRT23_PT2_DC(eventConfig->PT2DC) |
226             AOI_BFCRT23_PT3_AC(eventConfig->PT3AC) | AOI_BFCRT23_PT3_BC(eventConfig->PT3BC) |
227             AOI_BFCRT23_PT3_CC(eventConfig->PT3CC) | AOI_BFCRT23_PT3_DC(eventConfig->PT3DC);
228     /* Write value to register */
229     base->BFCRT[event].BFCRT23 = value;
230 }
231