/*! * @brief Use FTM in PWM mode * * This function use PWM to controll brightness of a LED. * LED is brighter and then dimmer, continuously. */ int main(void) { volatile uint32_t cnt = 0; int32_t step = 1; tpm_general_config_t driverInfo; tpm_pwm_param_t param = { .mode = kTpmEdgeAlignedPWM, .edgeMode = kTpmHighTrue, .uFrequencyHZ = 120000u, .uDutyCyclePercent = 99u }; // Init hardware hardware_init(); // Print a note. PRINTF("See the change of LED brightness \r\n"); // Prepare memory for initialization. memset(&driverInfo, 0, sizeof(driverInfo)); // Init TPM. TPM_DRV_Init(BOARD_TPM_INSTANCE, &driverInfo); // Set clock for TPM. TPM_DRV_SetClock(BOARD_TPM_INSTANCE, kTpmClockSourceModuleClk, kTpmDividedBy2); while(1) { // Init PWM module with updated configuration. TPM_DRV_PwmStart(BOARD_TPM_INSTANCE, ¶m, BOARD_TPM_CHANNEL); // Delay a while to see LED. for(cnt = 0; cnt < 200000u; cnt++) { __asm("nop"); } // Change up or down direction of brightness. if((param.uDutyCyclePercent >= 100) || (param.uDutyCyclePercent <= 0)) { step *= -1; } // Update duty cycle. param.uDutyCyclePercent += step; } }
void PIT_IRQHandler(void) { if (PIT_HAL_IsIntPending(g_pitBase[0], 0)) { PIT_HAL_ClearIntFlag(g_pitBase[0], 0); param.uFrequencyHZ = notes[beeps[position]]; position = (position + 1) % sizeof(beeps); if (param.uFrequencyHZ) TPM_DRV_PwmStart(0, ¶m, 3); else TPM_DRV_PwmStop(0, ¶m, 3); } if (PIT_HAL_IsIntPending(g_pitBase[0], 1)) { PIT_HAL_ClearIntFlag(g_pitBase[0], 1); } }
/*FUNCTION********************************************************************** * * Function Name : SMARTCARD_DRV_InterfaceClockInit * Description : This function initializes clock module used for card clock generation * *END**************************************************************************/ static uint16_t SMARTCARD_DRV_InterfaceClockInit(uint8_t clockModuleInstance, uint8_t clockModuleChannel, uint32_t cardClk) { #if defined(EMVSIM_INSTANCE_COUNT) assert(clockModuleInstance < EMVSIM_INSTANCE_COUNT); EMVSIM_Type * base = g_emvsimBase[clockModuleInstance]; uint32_t emvsimClkMhz = 0; uint8_t emvsimPRSCValue; /* Retrieve EMV SIM clock */ emvsimClkMhz = CLOCK_SYS_GetEmvsimFreq(clockModuleInstance)/1000000; /* Calculate MOD value */ emvsimPRSCValue = (emvsimClkMhz*1000)/(cardClk/1000); /* Set clock prescaler */ EMVSIM_HAL_SetClockPrescaler(base, emvsimPRSCValue); /* Enable smart card clock */ EMVSIM_HAL_EnableCardClock(base); return cardClk; #elif defined(FTM_INSTANCE_COUNT) assert(clockModuleInstance < FTM_INSTANCE_COUNT); ftm_user_config_t ftmInfo; uint32_t periph_clk_mhz = 0; uint16_t ftmModValue; FTM_Type * ftmBase = g_ftmBase[clockModuleInstance]; uint32_t chnlPairnum = FTM_HAL_GetChnPairIndex(clockModuleChannel); /* Retrieve FTM system clock */ periph_clk_mhz = CLOCK_SYS_GetBusClockFreq()/1000000; /* Calculate MOD value */ ftmModValue = ((periph_clk_mhz*1000/2)/(cardClk/1000)) -1; /* Clear FTM driver user configuration */ memset(&ftmInfo, 0, sizeof(ftmInfo)); ftmInfo.BDMMode = kFtmBdmMode_11; ftmInfo.syncMethod = kFtmUseSoftwareTrig; /* Initialize FTM driver */ FTM_DRV_Init(clockModuleInstance, &ftmInfo); /* Reset FTM prescaler to 'Divide by 1', i.e., to be same clock as peripheral clock */ FTM_HAL_SetClockPs(ftmBase, kFtmDividedBy1); /* Disable FTM counter firstly */ FTM_HAL_SetClockSource(ftmBase, kClock_source_FTM_None); /* Set initial counter value */ FTM_HAL_SetCounterInitVal(ftmBase, 0); /* Set MOD value */ FTM_HAL_SetMod(ftmBase, ftmModValue); /* Other initializations to defaults */ FTM_HAL_SetCpwms(ftmBase, 0); FTM_HAL_SetDualChnCombineCmd(ftmBase, chnlPairnum, false); FTM_HAL_SetDualEdgeCaptureCmd(ftmBase, chnlPairnum, false); /* Configure mode to output compare, tougle output on match */ FTM_HAL_SetChnEdgeLevel(ftmBase, clockModuleChannel, kFtmToggleOnMatch); FTM_HAL_SetChnMSnBAMode(ftmBase, clockModuleChannel, 1); /* Configure a match value to toggle output at */ FTM_HAL_SetChnCountVal(ftmBase, clockModuleChannel, 1); /* Set clock source to start the counter */ FTM_HAL_SetClockSource(ftmBase, kClock_source_FTM_SystemClk); /* Re-calculate the actually configured smartcard clock and return to caller */ return (uint32_t)(((periph_clk_mhz*1000/2)/(FTM_HAL_GetMod(ftmBase)+1))*1000); #elif defined(TPM_INSTANCE_COUNT) assert(clockModuleInstance < TPM_INSTANCE_COUNT); assert(clockModuleChannel < FSL_FEATURE_TPM_CHANNEL_COUNTn(clockModuleInstance)); /* Initialize TPM driver parameter */ tpm_pwm_param_t param = { kTpmCenterAlignedPWM, /* mode */ kTpmHighTrue, /* edgeMode */ cardClk, /* uFrequencyHZ */ 50 /* uDutyCyclePercent */ }; /* Initialize TPM driver */ tpm_general_config_t driverInfo; memset(&driverInfo, 0, sizeof(driverInfo)); driverInfo.isDBGMode = true; TPM_DRV_Init(clockModuleInstance, &driverInfo); /* Set TPM clock source, the user will have to call the Clocking API's to set the * TPM module clock before calling this function */ TPM_DRV_SetClock(clockModuleInstance, kTpmClockSourceModuleClk, kTpmDividedBy1); /* Start TPM in PWM mode to generate smart card clock */ TPM_DRV_PwmStart(clockModuleInstance, ¶m, clockModuleChannel); return cardClk; #else return 0; #endif }
/*! * @brief Main demo function. */ int main (void) { tpm_general_config_t driverInfo; accel_dev_t accDev; accel_dev_interface_t accDevice; accel_sensor_data_t accelData; accel_i2c_interface_t i2cInterface; tpm_pwm_param_t yAxisParams; tpm_pwm_param_t xAxisParams; int16_t xData, yData; int16_t xAngle, yAngle; xAxisParams.mode = kTpmEdgeAlignedPWM; xAxisParams.edgeMode = kTpmHighTrue; xAxisParams.uFrequencyHZ = 100000u; xAxisParams.uDutyCyclePercent = 0u; yAxisParams.mode = kTpmEdgeAlignedPWM; yAxisParams.edgeMode = kTpmHighTrue; yAxisParams.uFrequencyHZ = 100000u; yAxisParams.uDutyCyclePercent = 0u; // Register callback func for I2C i2cInterface.i2c_init = I2C_DRV_MasterInit; i2cInterface.i2c_read = I2C_DRV_MasterReceiveDataBlocking; i2cInterface.i2c_write = I2C_DRV_MasterSendDataBlocking; accDev.i2c = &i2cInterface; accDev.accel = &accDevice; accDev.slave.baudRate_kbps = BOARD_ACCEL_BAUDRATE; accDev.slave.address = BOARD_ACCEL_ADDR; accDev.bus = BOARD_ACCEL_I2C_INSTANCE; // Initialize standard SDK demo application pins. hardware_init(); // Accel device driver utilizes the OSA, so initialize it. OSA_Init(); // Print the initial banner. PRINTF("Bubble Level Demo!\r\n\r\n"); // Initialize the Accel. accel_init(&accDev); // Prepare memory for initialization. memset(&driverInfo, 0, sizeof(driverInfo)); // Init TPM. TPM_DRV_Init(BOARD_BUBBLE_TPM_INSTANCE, &driverInfo); // Set clock for TPM. TPM_DRV_SetClock(BOARD_BUBBLE_TPM_INSTANCE, kTpmClockSourceModuleClk, kTpmDividedBy2); // Main loop. Get sensor data and update duty cycle for the TPM timer. while(1) { // Wait 5 ms in between samples (accelerometer updates at 200Hz). OSA_TimeDelay(5); // Get new accelerometer data. accDev.accel->accel_read_sensor_data(&accDev,&accelData); // Init PWM module with updated configuration. TPM_DRV_PwmStart(BOARD_BUBBLE_TPM_INSTANCE, &xAxisParams, BOARD_TPM_X_CHANNEL); TPM_DRV_PwmStart(BOARD_BUBBLE_TPM_INSTANCE, &yAxisParams, BOARD_TPM_Y_CHANNEL); // Get the X and Y data from the sensor data structure.fxos_data xData = (int16_t)((accelData.data.accelXMSB << 8) | accelData.data.accelXLSB); yData = (int16_t)((accelData.data.accelYMSB << 8) | accelData.data.accelYLSB); // Convert raw data to angle (normalize to 0-90 degrees). No negative angles. xAngle = abs((int16_t)(xData * 0.011)); yAngle = abs((int16_t)(yData * 0.011)); // Update angles to turn on LEDs when angles ~ 90 if(xAngle > 85) xAngle = 100; if(yAngle > 85) yAngle = 100; // Update angles to turn off LEDs when angles ~ 0 if(xAngle < 5) xAngle = 0; if(yAngle < 5) yAngle = 0; // Update pwm duty cycle xAxisParams.uDutyCyclePercent = 100 - xAngle ; yAxisParams.uDutyCyclePercent = 100 - yAngle ; // Print out the raw accelerometer data. PRINTF("x= %d y = %d\r\n", xData, yData); } }