Dear Sir,
Above method trying is not work.
i got assert failed following error.
ASSERT ERROR " C:\Users\XXXX\Documents\MCUXpressoIDE_11.5.0_7232\workspace\lpcxpresso54628_ctimer_pwm_example\drivers/fsl_sctimer.h:1058 : 0U == (base->CONFIG & SCT_CONFIG_UNIFY_MASK)
My code :
#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "board.h"
#include "fsl_ctimer.h"
#include "fsl_sctimer.h"
#include "fsl_inputmux.h"
#include <stdbool.h>
/*******************************************************************************
* Definitions
******************************************************************************/
#define CTIMER CTIMER1 /* Timer 1 */
#define CTIMER_MAT_OUT kCTIMER_Match_1 /* Match output 1 */
#define CTIMER_CLK_FREQ CLOCK_GetFreq(kCLOCK_BusClk)
#ifndef CTIMER_MAT_PWM_PERIOD_CHANNEL
#define CTIMER_MAT_PWM_PERIOD_CHANNEL kCTIMER_Match_3
#endif
#define BUS_CLK_FREQ CLOCK_GetFreq(kCLOCK_BusClk)
void SCT_Detection_Callback(void);
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
volatile uint32_t g_pwmPeriod = 0U;
volatile uint32_t g_pulsePeriod = 0U;
/*******************************************************************************
* Code
******************************************************************************/
status_t CTIMER_GetPwmPeriodValue(uint32_t pwmFreqHz, uint8_t dutyCyclePercent, uint32_t timerClock_Hz)
{
/* Calculate PWM period match value */
g_pwmPeriod = (timerClock_Hz / pwmFreqHz) - 1U;
/* Calculate pulse width match value */
g_pulsePeriod = (g_pwmPeriod + 1U) * (100 - dutyCyclePercent) / 100;
return kStatus_Success;
}
sctimer_event_callback_t sctimer_callback_table={SCT_Detection_Callback};
uint32_t widths;
void SCT_Detection_Callback(void)
{
widths = SCT0->COUNT;
SCT0->CTRL |= (1<<19);//clear counter
uint32_t CTRL_Enable_Event = SCT0->CTRL;
CTRL_Enable_Event &= (~(SCT_CTRL_HALT_H_MASK) ); //clear halt H bit
CTRL_Enable_Event |= SCT_CTRL_STOP_H_MASK; //set stop H bit
SCT0->CTRL = CTRL_Enable_Event; //Write both changes at once
}
/*!
* @brief Main function
*/
int main(void)
{
ctimer_config_t config;
uint32_t srcClock_Hz;
uint32_t timerClock;
sctimer_config_t sctimerInfo;
uint32_t stateNumber;
uint32_t eventNumberInput, eventNumberInputPlus;
uint32_t sctimerClock;
/* Init hardware*/
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
/* Enable the asynchronous bridge */
SYSCON->ASYNCAPBCTRL = 1;
/* Use 12 MHz clock for some of the Ctimers */
CLOCK_AttachClk(kFRO12M_to_ASYNC_APB);
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
/* CTimer0 counter uses the AHB clock, some CTimer1 modules use the Aysnc clock */
srcClock_Hz = CTIMER_CLK_FREQ;
PRINTF("CTimer example to generate a PWM signal\r\n");
CTIMER_GetDefaultConfig(&config);
timerClock = srcClock_Hz / (config.prescale + 1);
CTIMER_Init(CTIMER, &config);
/* Get the PWM period match value and pulse width match value of 20Khz PWM signal with 20% dutycycle */
CTIMER_GetPwmPeriodValue(20000, 20, timerClock);
CTIMER_SetupPwmPeriod(CTIMER, CTIMER_MAT_PWM_PERIOD_CHANNEL, CTIMER_MAT_OUT, g_pwmPeriod, g_pulsePeriod, false);
CTIMER_StartTimer(CTIMER);
#if 1
sctimerClock = BUS_CLK_FREQ;
sctimerInfo.enableCounterUnify = 0;
sctimerInfo.inputsync = 1<<1;
SCTIMER_GetDefaultConfig(&sctimerInfo);
/* Initialize SCTimer module */
SCTIMER_Init(SCT0, &sctimerInfo);
stateNumber = SCTIMER_GetCurrentState(SCT0);
/* Schedule an event to look for a rising edge on input 1 in this state */
if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_InputRiseEvent, 0, kSCTIMER_Input_1, kSCTIMER_Counter_L,
&eventNumberInput) == kStatus_Fail)
{
return -1;
}
/* Transition to next state when a rising edge is detected on input 1 */
SCTIMER_SetupNextStateAction(SCT0, stateNumber + 1, eventNumberInput);
/* Go to next state; State 1 */
SCTIMER_IncreaseState(SCT0);
/* Schedule an event to look for a rising edge on input 1 in this state */
if (SCTIMER_CreateAndScheduleEvent(SCT0, kSCTIMER_InputFallEvent, 0, kSCTIMER_Input_1, kSCTIMER_Counter_L,
&eventNumberInputPlus) == kStatus_Fail)
{
return -1;
}
/* Transition to next state when a rising edge is detected on input 1 */
SCTIMER_SetupNextStateAction(SCT0, stateNumber - 2, eventNumberInputPlus);
//HALT counter when falling edge occurs
SCTIMER_SetupCounterHaltAction(SCT0,kSCTIMER_Counter_L,eventNumberInputPlus);
SCTIMER_SetCallback(SCT0, sctimer_callback_table, eventNumberInputPlus);
SCTIMER_EnableInterrupts(SCT0,1<<eventNumberInputPlus);
NVIC_EnableIRQ(SCT0_IRQn);
/* Start the timer, use counter L as we are operating counter in 32-bit mode */
SCTIMER_StartTimer(SCT0, kSCTIMER_Counter_L);
#endif
while (1)
{
}
}
Please advise
Thanks