示例#1
0
/*!
 * @brief Main function
 */
void test_audio(void) {
  if(!yesno("Audio?")) return;

  const gpio_pin_config_t OUTFALSE = {kGPIO_DigitalOutput, 0};

  CLOCK_EnableClock(kCLOCK_PortC);
  PORT_SetPinMux(PORTC, 12U, kPORT_MuxAsGpio);
  PORT_SetPinMux(PORTC, 13U, kPORT_MuxAsGpio);

  GPIO_PinInit(GPIOC, 12U, &OUTFALSE);
  GPIO_PinInit(GPIOC, 13U, &OUTFALSE);

  GPIO_WritePinOutput(GPIOC, 12, true);
  GPIO_WritePinOutput(GPIOC, 13, true);

  i2c_init(&i2c_config_default);

  uint8_t cmd[1] = {0b00001111};
  send_cmd(cmd, sizeof(cmd));

  GPIO_WritePinOutput(GPIOC, 12, false);

  PORT_SetPinMux(PORTC, 9U, kPORT_MuxAlt3);

  ftm_config_t ftmInfo;
  uint8_t updatedDutycycle = 100U;
  ftm_chnl_pwm_signal_param_t ftmParam[2];

  /* Configure ftm params with frequency 24kHZ */
  ftmParam[0].chnlNumber = (ftm_chnl_t) BOARD_SECOND_FTM_CHANNEL;
  ftmParam[0].level = kFTM_LowTrue;
  ftmParam[0].dutyCyclePercent = 0U;
  ftmParam[0].firstEdgeDelayPercent = 0U;

  FTM_GetDefaultConfig(&ftmInfo);
  FTM_Init(BOARD_FTM_BASEADDR, &ftmInfo);
  FTM_SetupPwm(BOARD_FTM_BASEADDR, ftmParam, 1U, kFTM_EdgeAlignedPwm, 1000000, FTM_SOURCE_CLOCK);
  FTM_StartTimer(BOARD_FTM_BASEADDR, kFTM_SystemClock);

  FTM_UpdatePwmDutycycle(BOARD_FTM_BASEADDR, (ftm_chnl_t) BOARD_SECOND_FTM_CHANNEL, kFTM_EdgeAlignedPwm,
                         updatedDutycycle);

  int idx = 0;
  int cnt = 0;
  while (1) {
    if(cnt++ % 100 == 0) {
      uint8_t duty_cycle = (uint8_t) abs((sin(idx++ * 3.14159265358979f / 180) * 100.0));
      if (idx > 360) idx = 0;
      FTM_UpdatePwmDutycycle(BOARD_FTM_BASEADDR, (ftm_chnl_t) BOARD_SECOND_FTM_CHANNEL, kFTM_EdgeAlignedPwm,
                             (uint8_t) (duty_cycle++ % 100));

      /* Software trigger to update registers */
      FTM_SetSoftwareTrigger(BOARD_FTM_BASEADDR, true);
    }
  }
}
示例#2
0
void PWM_Init(uint8_t instance)
{
#if FSL_FEATURE_SOC_FTM_COUNT
    ftm_config_t config;
    FTM_GetDefaultConfig(&config);
    FTM_Init(mFtmBase[instance], &config);
    /* Enable TPM compatibility. Free running counter and synchronization compatible with TPM */
    mFtmBase[instance]->MODE &= ~(FTM_MODE_FTMEN_MASK);
    FTM_StartTimer(mFtmBase[instance], kFTM_SystemClock);
#else
    tpm_config_t config;
    TPM_GetDefaultConfig(&config);
    TPM_Init(mTpmBase[instance], &config);
    TPM_StartTimer(mTpmBase[instance], kTPM_SystemClock);
#endif  
}