Пример #1
0
void time_init(void)
{
    extern const hwtimer_devif_t kSystickDevif;
    extern const hwtimer_devif_t kPitDevif;
    #define HWTIMER_LL_DEVIF    kPitDevif      // Use hardware timer PIT
    #define HWTIMER_LL_SRCCLK   kBusClock     // Source Clock for PIT
    #define HWTIMER_LL_ID       1
    #define HWTIMER_PERIOD      1000      // 1 ms interval

    if (kHwtimerSuccess != HWTIMER_SYS_Init(&hwtimer, &HWTIMER_LL_DEVIF, HWTIMER_LL_ID, 5, NULL))
    {
        USB_PRINTF("\r\nError: hwtimer initialization.\r\n");
    }
    if (kHwtimerSuccess != HWTIMER_SYS_SetPeriod(&hwtimer, HWTIMER_LL_SRCCLK, HWTIMER_PERIOD))
    {
        USB_PRINTF("\r\nError: hwtimer set period.\r\n");
    }
    if (kHwtimerSuccess != HWTIMER_SYS_RegisterCallback(&hwtimer, hwtimer_callback, NULL))
    {
        USB_PRINTF("\r\nError: hwtimer callback registration.\r\n");
    }
    if (kHwtimerSuccess != HWTIMER_SYS_Start(&hwtimer))
    {
        USB_PRINTF("\r\nError: hwtimer start.\r\n");
    }
}
Пример #2
0
void time_init(void)
{

    if (kHwtimerSuccess != HWTIMER_SYS_Init(&hwtimer, &HWTIMER_LL_DEVIF, HWTIMER_LL_ID, NULL))
    {
        USB_PRINTF("\r\nError: hwtimer initialization.\r\n");
    }
    if (kHwtimerSuccess != HWTIMER_SYS_SetPeriod(&hwtimer, HWTIMER_PERIOD))
    {
        USB_PRINTF("\r\nError: hwtimer set period.\r\n");
    }
    if (kHwtimerSuccess != HWTIMER_SYS_RegisterCallback(&hwtimer, hwtimer_callback, NULL))
    {
        USB_PRINTF("\r\nError: hwtimer callback registration.\r\n");
    }
    if (kHwtimerSuccess != HWTIMER_SYS_Start(&hwtimer))
    {
        USB_PRINTF("\r\nError: hwtimer start.\r\n");
    }

    OS_intr_init(HWTIMER_IRQ_NUM, HWTIMER_IRQ_PRI, 0, TRUE);
}
Пример #3
0
/*!
* \cond DOXYGEN_PRIVATE
* \brief Pre initialization - initializing requested modules for basic run of MQX.
*/
int _bsp_pre_init(void)
{
    uint32_t result;

/******************************************************************************
         Init gpio platform pins for LEDs, setup board clock source
******************************************************************************/
/* Macro PEX_MQX_KSDK used by PEX team */
#ifndef PEX_MQX_KSDK
    hardware_init();
    /* Configure PINS for default UART instance */
  #if defined(BOARD_USE_LPSCI)
    configure_lpsci_pins(BOARD_DEBUG_UART_INSTANCE);
  #elif defined(BOARD_USE_LPUART)
    configure_lpuart_pins(BOARD_DEBUG_UART_INSTANCE);
  #elif defined(BOARD_USE_UART)
    configure_uart_pins(BOARD_DEBUG_UART_INSTANCE);
  #else
    #error Default serial module is unsupported or undefined.
  #endif
#endif

#if MQX_EXIT_ENABLED
    extern void  _bsp_exit_handler(void);
    /* Set the bsp exit handler, called by _mqx_exit */
    _mqx_set_exit_handler(_bsp_exit_handler);
#endif

    result = _psp_int_init(BSP_FIRST_INTERRUPT_VECTOR_USED, BSP_LAST_INTERRUPT_VECTOR_USED);
    if (result != MQX_OK) {
        return result;
    }


/******************************************************************************
                        Init MQX tick timer
******************************************************************************/
    /* Initialize , set and run system hwtimer */
    result = HWTIMER_SYS_Init(&systimer, &BSP_SYSTIMER_DEV, BSP_SYSTIMER_ID, NULL);
    if (kStatus_OSA_Success != result) {
        return MQX_INVALID_POINTER;
    }
    /* Set isr for timer*/
    if (NULL == OSA_InstallIntHandler(BSP_SYSTIMER_INTERRUPT_VECTOR, HWTIMER_SYS_SystickIsrAction))
    {
        return kHwtimerRegisterHandlerError;
    }
    /* Set interrupt priority */
    NVIC_SetPriority(BSP_SYSTIMER_INTERRUPT_VECTOR, MQX_TO_NVIC_PRIOR(BSP_SYSTIMER_ISR_PRIOR));

    /* Disable  interrupts to ensure ticks are not active until call of _sched_start_internal */
    _int_disable();

    result = HWTIMER_SYS_SetPeriod(&systimer, BSP_ALARM_PERIOD);
    if (kStatus_OSA_Success != result) {
        HWTIMER_SYS_Deinit(&systimer);
        return MQX_INVALID_POINTER;
    }
    result = HWTIMER_SYS_RegisterCallback(&systimer,(hwtimer_callback_t)_time_notify_kernel, NULL);
    if (kStatus_OSA_Success != result) {
        HWTIMER_SYS_Deinit(&systimer);
        return MQX_INVALID_POINTER;
    }
    result = HWTIMER_SYS_Start(&systimer);
    if (kStatus_OSA_Success != result) {
        HWTIMER_SYS_Deinit(&systimer);
        return MQX_INVALID_POINTER;
    }

    /* Initialize the system ticks */
    _time_set_ticks_per_sec(BSP_ALARM_FREQUENCY);
    _time_set_hwticks_per_tick(HWTIMER_SYS_GetModulo(&systimer));
    _time_set_hwtick_function(_bsp_get_hwticks, (void *)NULL);

    return MQX_OK;
}
Пример #4
0
/*!
 * @brief Use DAC fifo to generate sine wave on DACx_OUT
 */
int32_t dac_gen_wave(void)
{
    dac_converter_config_t dacUserConfig;
    uint32_t period;

    // Fill the structure with configuration of software trigger
    DAC_DRV_StructInitUserConfigNormal(&dacUserConfig);
    
    // Initialize the DAC Converter
    DAC_DRV_Init(DAC_INST, &dacUserConfig);

    // Enable the feature of DAC internal buffer
    dacBuffConfig.bufferEnable = true;
    
#if FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
    dacBuffConfig.idxWatermarkIntEnable = false;
    dacBuffConfig.watermarkMode = kDacBuffWatermarkFromUpperAs2Word;
#endif
    dacBuffConfig.triggerMode = kDacTriggerBySoftware;
    dacBuffConfig.dmaEnable = false;
    dacBuffConfig.idxStartIntEnable = false;
    dacBuffConfig.idxUpperIntEnable = false;
    dacBuffConfig.upperIdx = FSL_FEATURE_DAC_BUFFER_SIZE - 1U;
#if (FSL_FEATURE_DAC_HAS_BUFFER_SWING_MODE && (FSL_FEATURE_DAC_BUFFER_SIZE == DAC_DATA_BUF_SIZE))
    dacBuffConfig.buffWorkMode = kDacBuffWorkAsSwingMode;    
#else
    dacBuffConfig.buffWorkMode = kDacBuffWorkAsNormalMode;
#endif

    DAC_DRV_ConfigBuffer(DAC_INST, &dacBuffConfig);

#if (FSL_FEATURE_DAC_HAS_BUFFER_SWING_MODE && (FSL_FEATURE_DAC_BUFFER_SIZE == DAC_DATA_BUF_SIZE))
    // Fill the buffer with setting data, applicable only if the data and buffer lenght are equal
    DAC_DRV_SetBuffValue(DAC_INST, 0U, FSL_FEATURE_DAC_BUFFER_SIZE, (uint16_t *)dacBuf);
#endif
    
    // Use HW timer of systick to do SW trigger of DAC,
    if (kHwtimerSuccess != HWTIMER_SYS_Init(&hwtimer, &kSystickDevif, 0, NULL))
    {
        return -1;
    }

    // Get the period the systick triggered.
    // There's 30 times of systick interrupt for one period of sine wave,
    // as we only have 16 data depth buffer for DAC, so we need to trigger
    // 30 times of interrupt to do software trigger.

    period = INPUT_SIGNAL_FREQ * (2 * DAC_DATA_BUF_SIZE - 2);
    if (kHwtimerSuccess != HWTIMER_SYS_SetPeriod(&hwtimer, 1000000/period))
    {
        return -1;
    }

    // install call back for SW trigger for DAC
    if (kHwtimerSuccess != HWTIMER_SYS_RegisterCallback(&hwtimer, hwtimer_callback, 0))
    {
        return -1;
    }

    // start systick timer
    if (kHwtimerSuccess != HWTIMER_SYS_Start(&hwtimer))
    {
        return -1;
    }

    return 0;
}
//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);
}