Exemplo n.º 1
0
/*!
 * @brief FTM interrupt handler.
 */
void BOARD_FTM_IRQ_HANDLER(void)
{
    // The overflow interrupt is the start of each cycle and is handled first.
    if(FTM_HAL_HasTimerOverflowed(BOARD_FTM_BASE))
    {
        // Clear the interrupt.
        FTM_HAL_ClearTimerOverflow(BOARD_FTM_BASE);

        // Update LED state/duty cycle for the X-axis.
        FTM_HAL_SetChnCountVal(BOARD_FTM_BASE, BOARD_FTM_X_CHANNEL, g_xValue);

        // Only turn on the LED if the new duty cycle is not 0.
        if(g_xValue)
        {
            LED3_ON;
            FTM_HAL_EnableChnInt(BOARD_FTM_BASE, BOARD_FTM_X_CHANNEL);
        }
        else
        {
            LED3_OFF;
            FTM_HAL_DisableChnInt(BOARD_FTM_BASE, BOARD_FTM_X_CHANNEL);
        }

        // Update LED state/duty cycle for the Y-axis.
        FTM_HAL_SetChnCountVal(BOARD_FTM_BASE, BOARD_FTM_Y_CHANNEL, g_yValue);

        // Only turn on the LED if the new duty cycle is not 0.
        if(g_yValue)
        {
            LED2_ON;
            FTM_HAL_EnableChnInt(BOARD_FTM_BASE, BOARD_FTM_Y_CHANNEL);
        }
        else
        {
            LED2_OFF;
            FTM_HAL_DisableChnInt(BOARD_FTM_BASE, BOARD_FTM_Y_CHANNEL);
        }

        // Perform a software sync to update the counter registers.
        FTM_HAL_SetSoftwareTriggerCmd(BOARD_FTM_BASE, true);
    }

    // X-axis match: Clear interrupt and turn LED off.
    if(FTM_HAL_HasChnEventOccurred(BOARD_FTM_BASE, BOARD_FTM_X_CHANNEL))
    {
        FTM_HAL_ClearChnEventFlag(BOARD_FTM_BASE, BOARD_FTM_X_CHANNEL);
        LED3_OFF;
    }

    // Y-axis match: Clear interrupt and turn LED off.
    if(FTM_HAL_HasChnEventOccurred(BOARD_FTM_BASE, BOARD_FTM_Y_CHANNEL))
    {
        FTM_HAL_ClearChnEventFlag(BOARD_FTM_BASE, BOARD_FTM_Y_CHANNEL);
        LED2_OFF;
    }
}
Exemplo n.º 2
0
/*************************************************************************
 * Function Name: FTM0_SetPwmOutput
 * Parameters: sector number
 * Return: none
 * Description: set PWM output configuration based on selected sector
 *************************************************************************/
void FTM0_SetPwmOutput(int16 sector)
{
  FTM_HAL_SetInvctrlReg(FTM0_BASE_PTR, bldcCommutationTableComp[sector].swap);
  FTM_HAL_SetOutmaskReg(FTM0_BASE_PTR, bldcCommutationTableComp[sector].mask);
  FTM_HAL_SetSoftwareTriggerCmd(FTM0_BASE_PTR, true);
}
Exemplo n.º 3
0
/*************************************************************************
 * Function Name: FTM0_init
 * Parameters: none
 * Return: none
 * Description: FlexTimer 0 initialization
 *************************************************************************/
void FTM0_init(void)
{
  FTM_HAL_SetWriteProtectionCmd(FTM0_BASE_PTR, false);//false: Write-protection is disabled
  FTM_HAL_Enable(FTM0_BASE_PTR, true);//true: all registers including FTM-specific registers are available

  FTM_HAL_SetCounterInitVal(FTM0_BASE_PTR, (uint16_t)(-PWM_MODULO/2));
  FTM_HAL_SetMod(FTM0_BASE_PTR, (uint16_t)PWM_MODULO/2-1); // 20 kHz

  FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 0, 2);
  FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 1, 2);
  FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 2, 2);
  FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 3, 2);
  FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 4, 2);
  FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 5, 2);

  // output mask updated on PWM synchronization (not on rising edge of clock)
  FTM_HAL_SetMaxLoadingCmd(FTM0_BASE_PTR, true);//True to enable minimum loading point
  FTM_HAL_SetOutmaskPwmSyncModeCmd(FTM0_BASE_PTR, 1);//true if OUTMASK register is updated only by PWM sync\n

  FTM_HAL_SetDualChnPwmSyncCmd(FTM0_BASE_PTR, 0, true);//True to enable PWM synchronization
  FTM_HAL_SetDualChnPwmSyncCmd(FTM0_BASE_PTR, 1, true);
  FTM_HAL_SetDualChnPwmSyncCmd(FTM0_BASE_PTR, 2, true);
  FTM_HAL_SetDualChnDeadtimeCmd(FTM0_BASE_PTR, 0, true);//True to enable deadtime insertion, false to disable
  FTM_HAL_SetDualChnDeadtimeCmd(FTM0_BASE_PTR, 1, true);
  FTM_HAL_SetDualChnDeadtimeCmd(FTM0_BASE_PTR, 2, true);
  FTM_HAL_SetDualChnCompCmd(FTM0_BASE_PTR, 0, true);//True to enable complementary mode, false to disable
  FTM_HAL_SetDualChnCompCmd(FTM0_BASE_PTR, 1, true);
  FTM_HAL_SetDualChnCompCmd(FTM0_BASE_PTR, 2, true);
  FTM_HAL_SetDualChnCombineCmd(FTM0_BASE_PTR, 0, true);// True to enable channel pair to combine, false to disable
  FTM_HAL_SetDualChnCombineCmd(FTM0_BASE_PTR, 1, true);
  FTM_HAL_SetDualChnCombineCmd(FTM0_BASE_PTR, 2, true);

  // High transistors have negative polarity (MC33927/37)
  FTM_HAL_SetDeadtimePrescale(FTM0_BASE_PTR, kFtmDivided1);
  FTM_HAL_SetDeadtimeCount(FTM0_BASE_PTR, FTM_DEADTIME_DTVAL(63)); // DTVAL - deadtime value (0-63): deadtime period = DTPS x DTVAL
  FTM_HAL_SetInitTriggerCmd(FTM0_BASE_PTR, true);//True to enable, false to disable
  FTM_HAL_SetChnOutputPolarityCmd(FTM0_BASE_PTR, 0,  1);
  FTM_HAL_SetChnOutputPolarityCmd(FTM0_BASE_PTR, 2,  1);
  FTM_HAL_SetChnOutputPolarityCmd(FTM0_BASE_PTR, 4,  1);


  /* Following line configures:
     - enhanced PWM synchronization, FTM counter reset on SW sync
     - output SW control / polarity registers updated on PWM synchronization (not on rising edge of clock)
     - output SW control/inverting(swap)/mask registers updated from buffers on SW synchronization */  
  FTM_HAL_SetPwmSyncModeCmd(FTM0_BASE_PTR, true);// true means use Enhanced PWM synchronization\n
  FTM_HAL_SetCounterSoftwareSyncModeCmd(FTM0_BASE_PTR, true);//true means software trigger activates register sync\n
  FTM_HAL_SetSwoctrlPwmSyncModeCmd(FTM0_BASE_PTR, true);//true means SWOCTRL register is updated by PWM synch\n
  FTM_HAL_SetInvctrlPwmSyncModeCmd(FTM0_BASE_PTR, true);//true means INVCTRL register is updated by PWM synch\n
  FTM_HAL_SetSwoctrlSoftwareSyncModeCmd(FTM0_BASE_PTR, true);//true means software trigger activates register sync\n
  FTM_HAL_SetInvctrlSoftwareSyncModeCmd(FTM0_BASE_PTR, true);//true means software trigger activates register sync\n
  FTM_HAL_SetOutmaskSoftwareSyncModeCmd(FTM0_BASE_PTR, true);//true means software

  FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 0, 1);//Sets the FTM peripheral timer channel output mask.
  FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 1, 1);
  FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 2, 1);
  FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 3, 1);
  FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 4, 1);
  FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 5, 1);

  FTM_HAL_SetCounter(FTM0_BASE_PTR, 1U);         // update of FTM settings
  // no ISR, counting up, system clock, divide by 1
  FTM_HAL_SetClockSource(FTM0_BASE_PTR, kClock_source_FTM_SystemClk);
  
  FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 0, (uint16_t)(-PWM_MODULO/4));
  FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 1,(uint16_t) PWM_MODULO/4);
  FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 2, (uint16_t)(-PWM_MODULO/4));
  FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 3,(uint16_t) PWM_MODULO/4);
  FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 4, (uint16_t)(-PWM_MODULO/4));
  FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 5,(uint16_t) PWM_MODULO/4);

  FTM_HAL_SetSoftwareTriggerCmd(FTM0_BASE_PTR, 1);
  
  FTM_HAL_SetPwmLoadCmd(FTM0_BASE_PTR, 1);

  // FTM0 PWM output pins
  PORT_HAL_SetMuxMode(PORTC_BASE_PTR, 1, kPortMuxAlt4);
  PORT_HAL_SetMuxMode(PORTC_BASE_PTR, 3, kPortMuxAlt4);
  PORT_HAL_SetMuxMode(PORTC_BASE_PTR, 4, kPortMuxAlt4);
  PORT_HAL_SetMuxMode(PORTD_BASE_PTR, 4, kPortMuxAlt4);
  PORT_HAL_SetMuxMode(PORTD_BASE_PTR, 5, kPortMuxAlt4);
#if defined(KV10Z7_SERIES)
  PORT_HAL_SetMuxMode(PORTE_BASE_PTR, 25, kPortMuxAlt3);
#elif (defined(KV10Z1287_SERIES) || defined(KV11Z7_SERIES))
  PORT_HAL_SetMuxMode(PORTC_BASE_PTR, 2, kPortMuxAlt4);
#endif

  GPIO_HAL_SetPinDir(GPIOC_BASE_PTR, 1, kGpioDigitalOutput);
  GPIO_HAL_SetPinDir(GPIOC_BASE_PTR, 3, kGpioDigitalOutput);
  GPIO_HAL_SetPinDir(GPIOC_BASE_PTR, 4, kGpioDigitalOutput);

  GPIO_HAL_SetPinDir(GPIOD_BASE_PTR, 4, kGpioDigitalOutput);
  GPIO_HAL_SetPinDir(GPIOD_BASE_PTR, 5, kGpioDigitalOutput);
#if defined(KV10Z7_SERIES)
  GPIO_HAL_SetPinDir(GPIOE_BASE_PTR, 25, kGpioDigitalOutput);
#elif (defined(KV10Z1287_SERIES) || defined(KV11Z7_SERIES))
  GPIO_HAL_SetPinDir(GPIOC_BASE_PTR, 2, kGpioDigitalOutput);
#endif
}
//PTD2_UART_rx, PTD3_UART_tx
//PTC1,2,3,4
int main (void)
{
    memcpy(packet_upper_PC.trans_header, trans_header_table, sizeof(trans_header_table));
    // RX buffers
    //! @param receiveBuff Buffer used to hold received data
    uint8_t receiveBuff;

    // Initialize standard SDK demo application pins
    hardware_init();
    OSA_Init();
    // Call this function to initialize the console UART. This function
    // enables the use of STDIO functions (printf, scanf, etc.)
    dbg_uart_init();

/*Start***FTM Init*************************************************************/
    memset(&ftmInfo, 0, sizeof(ftmInfo));
    ftmInfo.syncMethod = kFtmUseSoftwareTrig;
    FTM_DRV_Init(0, &ftmInfo);
/*End*****FTM Init*************************************************************/

    // Print the initial banner
    PRINTF("\r\nHello World!\n\n\r");

    LED2_EN;    LED3_EN;    LED4_EN;    LED5_EN;
    LED2_OFF;   LED3_OFF;   LED4_OFF;   LED5_OFF;

    I2C_fxos8700Init();
    I2C_l3g4200dInit();

    FTM_DRV_PwmStart(0, &ftmParam0, 0);
    FTM_DRV_PwmStart(0, &ftmParam1, 1);
    FTM_DRV_PwmStart(0, &ftmParam2, 2);
    FTM_DRV_PwmStart(0, &ftmParam3, 3);
    FTM_HAL_SetSoftwareTriggerCmd(g_ftmBaseAddr[0], true);

        // Hwtimer initialization
    if (kHwtimerSuccess != HWTIMER_SYS_Init(&hwtimer, &HWTIMER_LL_DEVIF, HWTIMER_LL_ID, 5, NULL))
    {
        PRINTF("\r\nError: hwtimer initialization.\r\n");
    }
    if (kHwtimerSuccess != HWTIMER_SYS_SetPeriod(&hwtimer, HWTIMER_LL_SRCCLK, HWTIMER_PERIOD))
    {
        PRINTF("\r\nError: hwtimer set period.\r\n");
    }
//    if (kHwtimerSuccess != HWTIMER_SYS_RegisterCallback(&hwtimer, hwtimer_callback, NULL))
//    {
//        PRINTF("\r\nError: hwtimer callback registration.\r\n");
//    }
//    if (kHwtimerSuccess != HWTIMER_SYS_Start(&hwtimer))
//    {
//        PRINTF("\r\nError: hwtimer start.\r\n");
//    }
    
    /* A write of any value to current value register clears the field to 0, and also clears the SYST_CSR COUNTFLAG bit to 0. */
    SysTick->VAL = 0U;
    /* Run timer and disable interrupt */
    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk ;//| SysTick_CTRL_TICKINT_Msk;

    GPIO_DRV_Init(remoteControlPins,NULL);
//    GPIO_DRV_Init(fxos8700IntPins,NULL);
//    I2C_fxos8700AutoCalibration(); //cannot work , shit!
    
/*Start PIT init***************/    
    // Structure of initialize PIT channel No.0
    pit_user_config_t chn0Confg = {
      .isInterruptEnabled = true,
      .isTimerChained = false,
      .periodUs = 20000u //1000000 us
    };
    
    // Structure of initialize PIT channel No.1
    pit_user_config_t chn1Confg = {
      .isInterruptEnabled = true,
      .isTimerChained = false,
      .periodUs = 2000000u
    };  
    
    // Init pit module and enable run in debug
    PIT_DRV_Init(BOARD_PIT_INSTANCE, false);
    
    // Initialize PIT timer instance for channel 0 and 1
    PIT_DRV_InitChannel(BOARD_PIT_INSTANCE, 0, &chn0Confg);
//    PIT_DRV_InitChannel(BOARD_PIT_INSTANCE, 1, &chn1Confg);
    
    // Start channel 0
//    printf ("\n\rStarting channel No.0 ...");
    PIT_DRV_StartTimer(BOARD_PIT_INSTANCE, 0);
    
    // Start channel 1
//    printf ("\n\rStarting channel No.1 ...");
//    PIT_DRV_StartTimer(BOARD_PIT_INSTANCE, 1);
    
/*End PIT init***************/   

//    NVIC_SetPriority(SysTick_IRQn, 3);
//    NVIC_SetPriority(PORTB_IRQn,0);
    while(1)
    {
///*Start************Remote Controller Unlock *************/      
//      if(isRCunlock == true)
//      {    
//        LED3_ON;
//      }
//      else
//      {    
//        LED3_OFF;
//      }
//      static uint32_t unlock_times = 0;
//      static uint32_t lock_times = 0;
//      PRINTF("ThrottleValue = %6d ,YawValue = %6d \r\n" ,remoteControlValue[kThrottle],remoteControlValue[kYaw]);
//      if(isRCunlock == false)
//      {
//        if((remoteControlValue[kThrottle] < RC_THRESHOLD_L) && (remoteControlValue[kYaw] > RC_THRESHOLD_H))
//        {
//          unlock_times++;
//        }
//        else
//        {
//          unlock_times = 0;
//        }
//        if(unlock_times > 6)
//        {
//          isRCunlock = true; 
//        }
//      }
//      else
//      {
//        if((remoteControlValue[kThrottle] < RC_THRESHOLD_L) && (remoteControlValue[kYaw] < RC_THRESHOLD_L))
//        {
//          lock_times++;
//        }
//        else
//        {
//          lock_times = 0;
//        }
//        if(lock_times > 4)
//        {
//          isRCunlock = false;
//        }
//      }
///*End************Remote Controller Unlock *************/          

//      LED2_ON;
//      OSA_TimeDelay(200);
//      LED3_ON;
//      OSA_TimeDelay(200);
//      LED4_ON;
//      OSA_TimeDelay(200);
      LED5_ON;
      OSA_TimeDelay(100);

//      LED2_OFF;
//      OSA_TimeDelay(200);
//      LED3_OFF;
//      OSA_TimeDelay(200);
//      LED4_OFF;
//      OSA_TimeDelay(200);
      LED5_OFF;
      OSA_TimeDelay(100);
    }
}

volatile bool isRCunlock = false;
//below define value is in quad_common.h
//#define RC_THRESHOLD_H (220000U)
//#define RC_THRESHOLD_L (140000U)
//#define RC_THRESHOLD_ERROR (300000U)//由于IO采两个边沿中断,有可能算成低电平的时间,所以做一个剔除算法。
//#define HW_DIVIDER (2400000U) 
////120M core clock , 2400000 / 120 000 000 = 0.02 s , 50Hz , 
////遥控器信号 50Hz , 范围1~2ms,周期20ms,1.5ms中值.对应 120 000 - 240 000
void PORTB_IRQHandler(void)
{
  uint32_t intFlag = PORT_HAL_GetPortIntFlag(PORTB_BASE);
  uint32_t i =  0;
  uint32_t value = 0;
  static  uint32_t remoteControlValue1st[8] = {0};
  static  uint32_t remoteControlValue2nd[8] = {0};
  static  uint32_t remoteControlValueFlag[8] = {0};
  for(i=0 ; i<8;i++)
  {
    if (intFlag & (1 << remoteControlPinNum[i]))
    {
      if (remoteControlValueFlag[i] == 0)
      {
        remoteControlValue1st[i] = (SysTick->VAL);
        remoteControlValueFlag[i] = 1;
      }
      else
      {
        remoteControlValueFlag[i] = 0;
        remoteControlValue2nd[i] = (SysTick->VAL);
        if ( remoteControlValue1st[i] > remoteControlValue2nd[i] )
        { 
          value = remoteControlValue1st[i] - remoteControlValue2nd[i];
        }
        else
        {
          value = remoteControlValue1st[i] + HW_DIVIDER - remoteControlValue2nd[i];//hwtimer.divider
        }
        if( value > RC_THRESHOLD_ERROR)
        {
          remoteControlValueFlag[i] = 1;
          remoteControlValue1st[i] = (SysTick->VAL);
        }
        else
        {
          remoteControlValue[i] = value;
//          if(((remoteControlValue[3] <180000) ||(remoteControlValue[3] > 190000))&&remoteControlValue[3]> 100)
//            LED4_ON;
        }
      }
    }
    PORT_HAL_ClearPinIntFlag(PORTB_BASE,remoteControlPinNum[i]);
  }
  /* Clear interrupt flag.*/
 //   PORT_HAL_ClearPortIntFlag(PORTB_BASE);
}


void PORTE_IRQHandler(void)
{
  uint32_t intFlag = PORT_HAL_GetPortIntFlag(PORTE_BASE);
  if (intFlag & (1 << 11))
  {
    isFXOS8700Int1Trigger = true;
      PRINTF("\r\n PTE11 irq");
  }

  /* Clear interrupt flag.*/
  PORT_HAL_ClearPortIntFlag(PORTE_BASE);
}