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