1 /*
2 * Copyright 2018 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_common.h"
10 #include "fsl_adapter_pwm.h"
11 #include "fsl_tpm.h"
12
13 /************************************************************************************
14 *************************************************************************************
15 * Private prototypes
16 *************************************************************************************
17 ************************************************************************************/
18 typedef struct _hal_pwm_handle_struct_t
19 {
20 uint32_t pwmClock_Hz;
21 uint8_t instance;
22 } hal_pwm_handle_struct_t;
23 /************************************************************************************
24 *************************************************************************************
25 * Private memory declarations
26 *************************************************************************************
27 ************************************************************************************/
28
29 static TPM_Type *const s_tpmBase[] = TPM_BASE_PTRS;
30 static uint8_t s_pwmUsedChannel[sizeof(s_tpmBase) / sizeof(s_tpmBase[0])] = {0};
31
32 /************************************************************************************
33 *************************************************************************************
34 * Public functions
35 *************************************************************************************
36 ************************************************************************************/
HAL_PwmInit(hal_pwm_handle_t halPwmHandle,uint8_t instance,uint32_t srcClock_Hz)37 hal_pwm_status_t HAL_PwmInit(hal_pwm_handle_t halPwmHandle, uint8_t instance, uint32_t srcClock_Hz)
38 {
39 tpm_config_t tpmInfo;
40 hal_pwm_handle_struct_t *halPwmState = halPwmHandle;
41
42 assert(instance < (sizeof(s_tpmBase) / sizeof(s_tpmBase[0])));
43 assert(s_tpmBase[instance]);
44 assert(halPwmHandle);
45 assert(sizeof(hal_pwm_handle_struct_t) == HAL_PWM_HANDLE_SIZE);
46 halPwmState->pwmClock_Hz = srcClock_Hz;
47 halPwmState->instance = instance;
48
49 /* Initialize tpm module */
50 if (0U == s_pwmUsedChannel[instance])
51 {
52 TPM_GetDefaultConfig(&tpmInfo);
53 TPM_Init(s_tpmBase[instance], (void *)&tpmInfo);
54 }
55
56 return kStatus_HAL_PwmSuccess;
57 }
58
HAL_PwmDeinit(hal_pwm_handle_t halPwmHandle)59 void HAL_PwmDeinit(hal_pwm_handle_t halPwmHandle)
60 {
61 hal_pwm_handle_struct_t *halPwmState = halPwmHandle;
62
63 assert(halPwmHandle);
64 assert(halPwmState->instance < (sizeof(s_tpmBase) / sizeof(s_tpmBase[0])));
65
66 /* Decrease the channel counter for the instance */
67 if (s_pwmUsedChannel[halPwmState->instance] > 0U)
68 {
69 s_pwmUsedChannel[halPwmState->instance] -= 1U;
70 }
71
72 /* DeInitialize tpm module */
73 if (0U == s_pwmUsedChannel[halPwmState->instance])
74 {
75 TPM_Deinit(s_tpmBase[halPwmState->instance]);
76 }
77 }
78
79 /*! -------------------------------------------------------------------------
80 * \brief Pwm setup.
81 *---------------------------------------------------------------------------*/
HAL_PwmSetupPwm(hal_pwm_handle_t halPwmHandle,uint8_t channel,hal_pwm_setup_config_t * setupConfig)82 hal_pwm_status_t HAL_PwmSetupPwm(hal_pwm_handle_t halPwmHandle, uint8_t channel, hal_pwm_setup_config_t *setupConfig)
83 {
84 hal_pwm_handle_struct_t *halPwmState = halPwmHandle;
85
86 assert(halPwmHandle);
87 assert(channel <= (uint8_t)kTPM_Chnl_7);
88 assert(halPwmState->instance < (sizeof(s_tpmBase) / sizeof(s_tpmBase[0])));
89 assert(setupConfig);
90 tpm_chnl_pwm_signal_param_t pwmChannelConfig = {
91 .chnlNumber = (tpm_chnl_t)channel,
92 .level = (tpm_pwm_level_select_t)setupConfig->level,
93 .dutyCyclePercent = setupConfig->dutyCyclePercent,
94 #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
95 .firstEdgeDelayPercent = 0
96 #endif
97 };
98
99 if ((int32_t)kStatus_Success != TPM_SetupPwm(s_tpmBase[halPwmState->instance], (void *)&pwmChannelConfig, 1,
100 (tpm_pwm_mode_t)setupConfig->mode, setupConfig->pwmFreq_Hz,
101 halPwmState->pwmClock_Hz))
102 {
103 return kStatus_HAL_PwmFail;
104 }
105 TPM_StartTimer(s_tpmBase[halPwmState->instance], kTPM_SystemClock);
106
107 /* Increase the channel counter for the instance */
108 s_pwmUsedChannel[halPwmState->instance] += 1U;
109
110 return kStatus_HAL_PwmSuccess;
111 }
112
113 /*! -------------------------------------------------------------------------
114 * \brief update the Duty cycle
115 *---------------------------------------------------------------------------*/
HAL_PwmUpdateDutycycle(hal_pwm_handle_t halPwmHandle,uint8_t channel,hal_pwm_mode_t mode,uint8_t dutyCyclePercent)116 hal_pwm_status_t HAL_PwmUpdateDutycycle(hal_pwm_handle_t halPwmHandle,
117 uint8_t channel,
118 hal_pwm_mode_t mode,
119 uint8_t dutyCyclePercent)
120 {
121 hal_pwm_handle_struct_t *halPwmState = halPwmHandle;
122
123 assert(halPwmHandle);
124 assert(channel <= (uint8_t)kTPM_Chnl_7);
125 assert(halPwmState->instance < (sizeof(s_tpmBase) / sizeof(s_tpmBase[0])));
126
127 (void)TPM_UpdatePwmDutycycle(s_tpmBase[halPwmState->instance], (tpm_chnl_t)channel, (tpm_pwm_mode_t)mode,
128 dutyCyclePercent);
129
130 return kStatus_HAL_PwmSuccess;
131 }
132