1 /*
2  * Copyright 2021-2024 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /**
8 *   @file       Emios_Pwm_Ip_Irq.c
9 *
10 *   @addtogroup emios_pwm_ip Emios Pwm IPL
11 *   @{
12 */
13 
14 #ifdef __cplusplus
15 extern "C"{
16 #endif
17 
18 
19 /*==================================================================================================
20 *                                          INCLUDE FILES
21 * 1) system and project includes
22 * 2) needed interfaces from external units
23 * 3) internal and external interfaces from this unit
24 ==================================================================================================*/
25 #include "Emios_Pwm_Ip.h"
26 #include "Emios_Pwm_Ip_HwAccess.h"
27 #include "Emios_Pwm_Ip_Irq.h"
28 
29 /*==================================================================================================
30 *                                 SOURCE FILE VERSION INFORMATION
31 ==================================================================================================*/
32 #define EMIOS_PWM_IP_IRQ_VENDOR_ID_C                      43
33 #define EMIOS_PWM_IP_IRQ_AR_RELEASE_MAJOR_VERSION_C       4
34 #define EMIOS_PWM_IP_IRQ_AR_RELEASE_MINOR_VERSION_C       7
35 #define EMIOS_PWM_IP_IRQ_AR_RELEASE_REVISION_VERSION_C    0
36 #define EMIOS_PWM_IP_IRQ_SW_MAJOR_VERSION_C               2
37 #define EMIOS_PWM_IP_IRQ_SW_MINOR_VERSION_C               0
38 #define EMIOS_PWM_IP_IRQ_SW_PATCH_VERSION_C               0
39 
40 /*==================================================================================================
41 *                                       FILE VERSION CHECKS
42 ==================================================================================================*/
43 /* Check if source file and Emios_Pwm_Ip.h file are of the same vendor */
44 #if (EMIOS_PWM_IP_IRQ_VENDOR_ID_C != EMIOS_PWM_IP_VENDOR_ID)
45     #error "Vendor IDs of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip.h are different."
46 #endif
47 
48 /* Check if source file and Emios_Pwm_Ip.h file are of the same AUTOSAR version */
49 #if ((EMIOS_PWM_IP_IRQ_AR_RELEASE_MAJOR_VERSION_C    != EMIOS_PWM_IP_AR_RELEASE_MAJOR_VERSION) || \
50      (EMIOS_PWM_IP_IRQ_AR_RELEASE_MINOR_VERSION_C    != EMIOS_PWM_IP_AR_RELEASE_MINOR_VERSION) || \
51      (EMIOS_PWM_IP_IRQ_AR_RELEASE_REVISION_VERSION_C != EMIOS_PWM_IP_AR_RELEASE_REVISION_VERSION))
52     #error "AUTOSAR version numbers of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip.h are different."
53 #endif
54 
55 /* Check if source file and Emios_Pwm_Ip.h file are of the same Software version */
56 #if ((EMIOS_PWM_IP_IRQ_SW_MAJOR_VERSION_C != EMIOS_PWM_IP_SW_MAJOR_VERSION)  || \
57      (EMIOS_PWM_IP_IRQ_SW_MINOR_VERSION_C != EMIOS_PWM_IP_SW_MINOR_VERSION)  || \
58      (EMIOS_PWM_IP_IRQ_SW_PATCH_VERSION_C != EMIOS_PWM_IP_SW_PATCH_VERSION))
59     #error "Software version numbers of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip.h are different."
60 #endif
61 
62 /* Check if source file and Emios_Pwm_Ip_HwAccess.h file are of the same vendor */
63 #if (EMIOS_PWM_IP_IRQ_VENDOR_ID_C != EMIOS_PWM_IP_HWACCESS_VENDOR_ID)
64     #error "Vendor IDs of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip_HwAccess.h are different."
65 #endif
66 
67 /* Check if source file and Emios_Pwm_Ip_HwAccess.h file are of the same AUTOSAR version */
68 #if ((EMIOS_PWM_IP_IRQ_AR_RELEASE_MAJOR_VERSION_C    != EMIOS_PWM_IP_HWACCESS_AR_RELEASE_MAJOR_VERSION) || \
69      (EMIOS_PWM_IP_IRQ_AR_RELEASE_MINOR_VERSION_C    != EMIOS_PWM_IP_HWACCESS_AR_RELEASE_MINOR_VERSION) || \
70      (EMIOS_PWM_IP_IRQ_AR_RELEASE_REVISION_VERSION_C != EMIOS_PWM_IP_HWACCESS_AR_RELEASE_REVISION_VERSION))
71     #error "AUTOSAR version numbers of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip_HwAccess.h are different."
72 #endif
73 
74 /* Check if source file and Emios_Pwm_Ip_HwAccess.h file are of the same Software version */
75 #if ((EMIOS_PWM_IP_IRQ_SW_MAJOR_VERSION_C != EMIOS_PWM_IP_HWACCESS_SW_MAJOR_VERSION)  || \
76      (EMIOS_PWM_IP_IRQ_SW_MINOR_VERSION_C != EMIOS_PWM_IP_HWACCESS_SW_MINOR_VERSION)  || \
77      (EMIOS_PWM_IP_IRQ_SW_PATCH_VERSION_C != EMIOS_PWM_IP_HWACCESS_SW_PATCH_VERSION))
78     #error "Software version numbers of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip_HwAccess.h are different."
79 #endif
80 
81 /* Check if source file and Emios_Pwm_Ip_Irq.h file are of the same vendor */
82 #if (EMIOS_PWM_IP_IRQ_VENDOR_ID_C != EMIOS_PWM_IP_IRQ_VENDOR_ID)
83     #error "Vendor IDs of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip_Irq.h are different."
84 #endif
85 
86 /* Check if source file and Emios_Pwm_Ip_Irq.h file are of the same AUTOSAR version */
87 #if ((EMIOS_PWM_IP_IRQ_AR_RELEASE_MAJOR_VERSION_C    != EMIOS_PWM_IP_IRQ_AR_RELEASE_MAJOR_VERSION) || \
88      (EMIOS_PWM_IP_IRQ_AR_RELEASE_MINOR_VERSION_C    != EMIOS_PWM_IP_IRQ_AR_RELEASE_MINOR_VERSION) || \
89      (EMIOS_PWM_IP_IRQ_AR_RELEASE_REVISION_VERSION_C != EMIOS_PWM_IP_IRQ_AR_RELEASE_REVISION_VERSION))
90     #error "AUTOSAR version numbers of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip_Irq.h are different."
91 #endif
92 
93 /* Check if source file and Emios_Pwm_Ip_Irq.h file are of the same Software version */
94 #if ((EMIOS_PWM_IP_IRQ_SW_MAJOR_VERSION_C != EMIOS_PWM_IP_IRQ_SW_MAJOR_VERSION)  || \
95      (EMIOS_PWM_IP_IRQ_SW_MINOR_VERSION_C != EMIOS_PWM_IP_IRQ_SW_MINOR_VERSION)  || \
96      (EMIOS_PWM_IP_IRQ_SW_PATCH_VERSION_C != EMIOS_PWM_IP_IRQ_SW_PATCH_VERSION))
97     #error "Software version numbers of Emios_Pwm_Ip_Irq.c and Emios_Pwm_Ip_Irq.h are different."
98 #endif
99 
100 /*==================================================================================================
101 *                          LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
102 ==================================================================================================*/
103 
104 /*==================================================================================================
105 *                                       LOCAL MACROS
106 ==================================================================================================*/
107 
108 /*==================================================================================================
109 *                                      LOCAL CONSTANTS
110 ==================================================================================================*/
111 
112 /*==================================================================================================
113 *                                      LOCAL VARIABLES
114 ==================================================================================================*/
115 
116 /*==================================================================================================
117 *                                      GLOBAL CONSTANTS
118 ==================================================================================================*/
119 
120 /*==================================================================================================
121 *                                      GLOBAL VARIABLES
122 ==================================================================================================*/
123 
124 /*==================================================================================================
125 *                                   LOCAL FUNCTION PROTOTYPES
126 ==================================================================================================*/
127 
128 /*==================================================================================================
129 *                                       LOCAL FUNCTIONS
130 ==================================================================================================*/
131 
132 /*==================================================================================================
133 *                                       GLOBAL FUNCTIONS
134 ==================================================================================================*/
135 
136 #if (EMIOS_PWM_IP_USED == STD_ON)
137 
138 #define PWM_START_SEC_CODE
139 #include "Pwm_MemMap.h"
140 #ifdef EMIOS_PWM_IP_MODE_DAOC_USED
Emios_Pwm_Ip_IrqDaocHandler(uint8 Instance,uint8 Channel)141 static void Emios_Pwm_Ip_IrqDaocHandler(uint8 Instance, uint8 Channel)
142 {
143     Emios_Pwm_Ip_DutyType DaocDuty = 0U;
144     Emios_Pwm_Ip_DutyType DaocRegA = 0U;
145     Emios_Pwm_Ip_PeriodType DaocRegB = 0U;
146     Emios_Pwm_Ip_DutyType CounterMax = Emios_Pwm_Ip_GetUCRegA(Emios_Pwm_Ip_aBasePtr[Instance], Emios_Pwm_Ip_GetMasterBusChannel(Instance, Channel));
147     Emios_Pwm_Ip_PolarityType Polarity = Emios_Pwm_Ip_GetEdgePolarity(Emios_Pwm_Ip_aBasePtr[Instance], Channel);
148     boolean OutputPin = Emios_Pwm_Ip_GetOutputPinState(Emios_Pwm_Ip_aBasePtr[Instance], Channel);
149     /* Get the value of channel Index */
150     uint8 ChannelIdx = Emios_Pwm_Ip_GetChannelIndex(Instance, Channel);
151 
152     if (ChannelIdx < EMIOS_PWM_IP_NUM_OF_CHANNELS_USED_U8)
153     {
154         if (EMIOS_PWM_IP_MODE_DAOC_FLAG == Emios_Pwm_Ip_aCurrentModes[ChannelIdx])
155         {
156             DaocDuty = Emios_Pwm_Ip_aPeriod[ChannelIdx] - Emios_Pwm_Ip_aDutyCycle[ChannelIdx];
157             DaocRegA = ((DaocDuty + Emios_Pwm_Ip_GetUCRegB(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) % CounterMax);
158 
159             Emios_Pwm_Ip_SetUCRegA(Emios_Pwm_Ip_aBasePtr[Instance], Channel, (DaocRegA == 0U)? CounterMax : DaocRegA);
160 
161             DaocRegB = (Emios_Pwm_Ip_aPeriod[ChannelIdx] + Emios_Pwm_Ip_GetUCRegB(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) % CounterMax;
162             Emios_Pwm_Ip_SetUCRegB(Emios_Pwm_Ip_aBasePtr[Instance], Channel, (DaocRegB == 0U)? CounterMax : DaocRegB);
163 
164         }
165         else if (EMIOS_PWM_IP_MODE_DAOC_FLAG_BOTH == Emios_Pwm_Ip_aCurrentModes[ChannelIdx])
166         {
167             if (((EMIOS_PWM_IP_ACTIVE_HIGH == Polarity) && (FALSE == OutputPin)) ||
168                 ((EMIOS_PWM_IP_ACTIVE_LOW == Polarity) && (TRUE == OutputPin)))
169             {
170                 DaocRegA = (Emios_Pwm_Ip_aPeriod[ChannelIdx] + Emios_Pwm_Ip_GetUCRegA(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) % CounterMax;
171                 Emios_Pwm_Ip_SetUCRegA(Emios_Pwm_Ip_aBasePtr[Instance], Channel, (DaocRegA == 0U)? CounterMax : DaocRegA);
172             }
173             else
174             {
175                 DaocRegB = (Emios_Pwm_Ip_aDutyCycle[ChannelIdx] + Emios_Pwm_Ip_GetUCRegA(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) % CounterMax;
176                 Emios_Pwm_Ip_SetUCRegB(Emios_Pwm_Ip_aBasePtr[Instance], Channel, (DaocRegB == 0U)? CounterMax : DaocRegB);
177             }
178         }
179         else
180         {
181             /* Do nothing */
182         }
183     }
184     else
185     {
186         /* Do nothing */
187     }
188     (void)DaocDuty;
189     (void)DaocRegA;
190     (void)DaocRegB;
191     (void)Polarity;
192     (void)OutputPin;
193 }
194 #endif
195 
196 /**
197 * @brief          Interrupt handler for Emios Pwm channels.
198 * @details        Interrupt handler that clears the flags and calls the user notification function.
199 *
200 * @param[in]      Instance    Emios Instance id on which the interrupt occured.
201 * @param[in]      Channel     Channel id within the emios Instance that triggered the interrupt.
202 * @implements Emios_Pwm_Ip_IrqHandler_Activity
203 */
Emios_Pwm_Ip_IrqHandler(uint8 Instance,uint8 Channel)204 void Emios_Pwm_Ip_IrqHandler(uint8 Instance, uint8 Channel)
205 {
206     /* Get the value of channel Index */
207     uint8 ChannelIdx = Emios_Pwm_Ip_GetChannelIndex(Instance, Channel);
208 
209     if (ChannelIdx < EMIOS_PWM_IP_NUM_OF_CHANNELS_USED_U8)
210     {
211         /* Check the state of Channel is uninitialized state */
212         if ((0U == Emios_Pwm_Ip_aCheckState[ChannelIdx]) &&
213             ((TRUE == Emios_Pwm_Ip_GetOverRunFlag(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) ||
214             (TRUE == Emios_Pwm_Ip_GetOverFlagEvent(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) ||
215             (TRUE == Emios_Pwm_Ip_GetOverFlowFlag(Emios_Pwm_Ip_aBasePtr[Instance], Channel))))
216         {
217             /* Clear Interrupt flag */
218             Emios_Pwm_Ip_ClearFlagEvent(Emios_Pwm_Ip_aBasePtr[Instance], Channel);
219         }
220         else
221         {
222             /*Interrupt Flag and Interrupt Enable is set*/
223             if ((TRUE == Emios_Pwm_Ip_GetInterruptRequest(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) &&
224                 ((TRUE == Emios_Pwm_Ip_GetOverRunFlag(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) ||
225                 (TRUE == Emios_Pwm_Ip_GetOverFlagEvent(Emios_Pwm_Ip_aBasePtr[Instance], Channel)) ||
226                 (TRUE == Emios_Pwm_Ip_GetOverFlowFlag(Emios_Pwm_Ip_aBasePtr[Instance], Channel))))
227             {
228     #ifdef EMIOS_PWM_IP_MODE_DAOC_USED
229                 Emios_Pwm_Ip_IrqDaocHandler(Instance, Channel);
230     #endif
231                 /* Clear Interrupt flag */
232                 Emios_Pwm_Ip_ClearFlagEvent(Emios_Pwm_Ip_aBasePtr[Instance], Channel);
233 
234                 if ((NULL_PTR != Emios_Pwm_Ip_aNotificationPtr[ChannelIdx]->CbFunction) &&
235                     (1U == Emios_Pwm_Ip_aCheckEnableNotif[ChannelIdx]))
236                 {
237                     /* Call the user notification callback with the provided parameter */
238                     (Emios_Pwm_Ip_aNotificationPtr[ChannelIdx]->CbFunction)(Emios_Pwm_Ip_aNotificationPtr[ChannelIdx]->CbParameter);
239                 }
240             }
241         }
242     }
243     else
244     {
245         /* Do nothing */
246     }
247 }
248 
249 #define PWM_STOP_SEC_CODE
250 #include "Pwm_MemMap.h"
251 
252 #endif /* EMIOS_PWM_IP_USED == STD_ON */
253 #ifdef __cplusplus
254 }
255 #endif
256 
257 /** @} */
258