Beispiel #1
0
// Reconfigure the HAL tick using a standard timer instead of systick.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
    // Enable timer clock
    TIM_MST_RCC;

    // Reset timer
    TIM_MST_RESET_ON;
    TIM_MST_RESET_OFF;

    // Update the SystemCoreClock variable
    SystemCoreClockUpdate();

    // Configure time base
    TimMasterHandle.Instance = TIM_MST;
    TimMasterHandle.Init.Period        = 0xFFFF;
    TimMasterHandle.Init.Prescaler     = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
    TimMasterHandle.Init.ClockDivision = 0;
    TimMasterHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
    HAL_TIM_Base_Init(&TimMasterHandle);

    // Configure output compare channel 1 for mbed timeout (enabled later when used)
    HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);

    // Configure output compare channel 2 for HAL tick
    HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
    PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
    __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);

    // Configure interrupts
    // Update interrupt used for 32-bit counter
    // Output compare channel 1 interrupt for mbed timeout
    // Output compare channel 2 interrupt for HAL tick
    NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler);
    NVIC_EnableIRQ(TIM_MST_UP_IRQ);
    NVIC_SetPriority(TIM_MST_UP_IRQ, 0);
    NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler);
    NVIC_EnableIRQ(TIM_MST_OC_IRQ);
    NVIC_SetPriority(TIM_MST_OC_IRQ, 1);

    // Enable interrupts
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick

    // Enable timer
    HAL_TIM_Base_Start(&TimMasterHandle);

#if 0 // For DEBUG only
    __GPIOB_CLK_ENABLE();
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
#endif

    return HAL_OK;
}
Beispiel #2
0
void set_compare(uint16_t count) {
    TimMasterHandle.Instance = TIM_MST;
    // Set new output compare value
    __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
    // Enable IT
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
}
Beispiel #3
0
void us_ticker_set_interrupt(timestamp_t timestamp)
{
    // Set new output compare value
    __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp);
    // Enable IT
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
}
void lp_ticker_set_interrupt(uint32_t now, uint32_t time) {
    (void)now;
    // Set new output compare value
    __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, time);
    // Enable IT
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
}
Beispiel #5
0
void HAL_ResumeTick(void)
{
    TimMasterHandle.Instance = TIM_MST;

	// Enable HAL tick and us_ticker update interrupts (used for 32 bit counter)
	__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
}
int timer_set_update_cb(hacs_timer_t tim, hacs_timer_cb_t overflow_cb)
{
  TIM_TypeDef *inst = hacs_tim_instances[tim];

  // Enable timer overflow interrupt if needed
  tim_overflow_cb[tim] = overflow_cb;
  if (overflow_cb != NULL) {
    // Configure NVIC on the appropriate IRQ
    IRQn_Type irq;

    if (inst == TIM1 || inst == TIM10) irq = TIM1_UP_TIM10_IRQn;
    else if (inst == TIM2) irq = TIM2_IRQn;
    else if (inst == TIM3) irq = TIM3_IRQn;
    else if (inst == TIM4) irq = TIM4_IRQn;
    else if (inst == TIM5) irq = TIM5_IRQn;
    else if (inst == TIM9) irq = TIM1_BRK_TIM9_IRQn;
    else if (inst == TIM11) irq = TIM1_TRG_COM_TIM11_IRQn;

    NVIC_SetPriority(irq, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 3);
    NVIC_EnableIRQ(irq);

    __HAL_TIM_ENABLE_IT(&tim_handles[tim], TIM_IT_UPDATE);
  }
  
  return HACS_NO_ERROR;
}
Beispiel #7
0
void servo_timer_irq_callback(void) {
    bool need_it = false;
    for (int i = 0; i < PYB_SERVO_NUM; i++) {
        pyb_servo_obj_t *s = &pyb_servo_obj[i];
        if (s->pulse_cur != s->pulse_dest) {
            // clamp pulse to within min/max
            if (s->pulse_dest < s->pulse_min) {
                s->pulse_dest = s->pulse_min;
            } else if (s->pulse_dest > s->pulse_max) {
                s->pulse_dest = s->pulse_max;
            }
            // adjust cur to get closer to dest
            if (s->time_left <= 1) {
                s->pulse_cur = s->pulse_dest;
                s->time_left = 0;
            } else {
                s->pulse_accum += s->pulse_dest - s->pulse_cur;
                s->pulse_cur += s->pulse_accum / s->time_left;
                s->pulse_accum %= s->time_left;
                s->time_left--;
                need_it = true;
            }
            // set the pulse width
            *(&TIM5->CCR1 + s->pin->pin) = s->pulse_cur;
        }
    }
    if (need_it) {
        __HAL_TIM_ENABLE_IT(&TIM5_Handle, TIM_IT_UPDATE);
    } else {
        __HAL_TIM_DISABLE_IT(&TIM5_Handle, TIM_IT_UPDATE);
    }
}
Beispiel #8
0
void us_ticker_init(void)
{

    if (us_ticker_inited) return;
    us_ticker_inited = 1;

    // Enable timer clock
    TIM_MST_RCC;

    // Configure time base
    TimMasterHandle.Instance = TIM_MST;
    TimMasterHandle.Init.Period        = 0xFFFF;
    TimMasterHandle.Init.Prescaler         = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick
    TimMasterHandle.Init.ClockDivision     = 0;
    TimMasterHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
    HAL_TIM_Base_Init(&TimMasterHandle);

    // Configure interrupts
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE);

    // Update interrupt used for 32-bit counter
    NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
    NVIC_EnableIRQ(TIM_MST_UP_IRQ);

    // Output compare interrupt used for timeout feature
    NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
    NVIC_EnableIRQ(TIM_MST_OC_IRQ);

    // Enable timer
    HAL_TIM_Base_Start(&TimMasterHandle);
}
/**
  * @brief  CDC_Itf_Init
  *         Initializes the CDC media low layer
  * @param  None
  * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
  */
static int8_t CDC_Itf_Init(void)
{
#if 0
  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* USART configured as follow:
      - Word Length = 8 Bits
      - Stop Bit    = One Stop bit
      - Parity      = No parity
      - BaudRate    = 115200 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance        = USARTx;
  UartHandle.Init.BaudRate   = 115200;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /*##-2- Put UART peripheral in IT reception process ########################*/
  /* Any data received will be stored in "UserTxBuffer" buffer  */
  if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)UserTxBuffer, 1) != HAL_OK)
  {
    /* Transfer error in reception process */
    Error_Handler();
  }
  
  /*##-3- Configure the TIM Base generation  #################################*/
  now done in HAL_MspInit
  TIM_Config();
#endif
  
    /*##-4- Start the TIM Base generation in interrupt mode ####################*/
    /* Start Channel1 */
    __HAL_TIM_ENABLE_IT(&TIM3_Handle, TIM_IT_UPDATE);
  
    /*##-5- Set Application Buffers ############################################*/
    USBD_CDC_SetTxBuffer(&hUSBDDevice, UserTxBuffer, 0);
    USBD_CDC_SetRxBuffer(&hUSBDDevice, UserRxBuffer);

    UserRxBufCur = 0;
    UserRxBufLen = 0;
  
    /* NOTE: we cannot reset these here, because USBD_CDC_SetInterrupt
     * may be called before this init function to set these values.
     * This can happen if the USB enumeration occurs after the call to
     * USBD_CDC_SetInterrupt.
    user_interrupt_char = VCP_CHAR_NONE;
    user_interrupt_data = NULL;
    */

    return (USBD_OK);
}
Beispiel #10
0
// Reconfigure the HAL tick using a standard timer instead of systick.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    uint32_t PclkFreq;

    // Get clock configuration
    // Note: PclkFreq contains here the Latency (not used after)
    HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
  
    // Get TIM5 clock value
    PclkFreq = HAL_RCC_GetPCLK1Freq();
  
    // Enable timer clock
    TIM_MST_RCC;

    // Reset timer
    TIM_MST_RESET_ON;
    TIM_MST_RESET_OFF;
  
    // Configure time base
    TimMasterHandle.Instance = TIM_MST;
    TimMasterHandle.Init.Period            = 0xFFFFFFFF;
  
    // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
    if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1)
      TimMasterHandle.Init.Prescaler   = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
    else
      TimMasterHandle.Init.Prescaler   = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick  
  
    TimMasterHandle.Init.ClockDivision     = 0;
    TimMasterHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
    TimMasterHandle.Init.RepetitionCounter = 0;
    HAL_TIM_OC_Init(&TimMasterHandle);

    NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
    NVIC_EnableIRQ(TIM_MST_IRQ);

    // Channel 1 for mbed timeout
    HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);

    // Channel 2 for HAL tick
    HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
    PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
    __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);

#if 0 // For DEBUG only
    __GPIOB_CLK_ENABLE();
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
#endif

    return HAL_OK;
}
Beispiel #11
0
// Reconfigure the HAL tick using a standard timer instead of systick.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
    // Enable timer clock
    TIM_MST_RCC;

    // Reset timer
    TIM_MST_RESET_ON;
    TIM_MST_RESET_OFF;

    // Update the SystemCoreClock variable
    SystemCoreClockUpdate();

    // Configure time base
    TimMasterHandle.Instance = TIM_MST;
    TimMasterHandle.Init.Period        = 0xFFFFFFFF;
    TimMasterHandle.Init.Prescaler     = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
    TimMasterHandle.Init.ClockDivision = 0;
    TimMasterHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
    HAL_TIM_Base_Init(&TimMasterHandle);

    // Configure output compare channel 1 for mbed timeout (enabled later when used)
    HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);

    // Configure output compare channel 2 for HAL tick
    HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
    PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
    __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);

    // Configure interrupts
    // Update interrupt used for 32-bit counter
    // Output compare channel 1 interrupt for mbed timeout
    // Output compare channel 2 interrupt for HAL tick
    NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
    NVIC_EnableIRQ(TIM_MST_IRQ);

    // Enable interrupts
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick

    // Enable timer
    HAL_TIM_Base_Start(&TimMasterHandle);

    return HAL_OK;
}
Beispiel #12
0
void
vMBPortTimersEnable(  )
{
  /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
    /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
		__HAL_TIM_CLEAR_IT(&htim6,TIM_IT_UPDATE);
		__HAL_TIM_ENABLE_IT(&htim6,TIM_IT_UPDATE);
		__HAL_TIM_SetCounter(&htim6,0);
		__HAL_TIM_ENABLE(&htim6);  
}
Beispiel #13
0
void
vMBPortTimersEnable(  )
{
//  HAL_UART_Transmit(&huart1, "t" , 1, 0xFFFF);
#if MB_TIMER_DEBUG == 1
    PIO_Set( &xTimerDebugPins[0] );  
#endif  
      TIM1->CNT = 0;
    __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE);
    __HAL_TIM_ENABLE(&htim1);
}
Beispiel #14
0
void sleep(void)
{
    // Disable HAL tick interrupt
    TimMasterHandle.Instance = TIM5;
    __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);

    // Request to enter SLEEP mode
    HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);

    // Enable HAL tick interrupt
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
}
Beispiel #15
0
//Enables timer and setup 1ms periodic interrupt.
void bl_Init(void){
	TIM_ENPERCLK();
	Tmr.Instance = TIM_INSTANCE;
	Tmr.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	Tmr.Init.CounterMode = TIM_COUNTERMODE_UP;
	Tmr.Init.Period = TIM_CNTPERMS - 1;
	Tmr.Init.Prescaler = TIM_PRESC - 1;
	HAL_TIM_Base_Init(&Tmr);

	HAL_NVIC_SetPriority(TIM_IRQN, 3, 3);
	HAL_NVIC_EnableIRQ(TIM_IRQN);
	__HAL_TIM_ENABLE_IT(&Tmr, TIM_IT_UPDATE);
	HAL_TIM_Base_Start(&Tmr);
}
Beispiel #16
0
void deepsleep(void)
{
    // Disable HAL tick interrupt
    TimMasterHandle.Instance = TIM5;
    __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);

    // Request to enter STOP mode with regulator in low power mode
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

    // After wake-up from STOP reconfigure the PLL
    SetSysClock();

    // Enable HAL tick interrupt
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
}
Beispiel #17
0
// Reconfigure the HAL tick using a standard timer instead of systick.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
    // Enable timer clock
    TIM_MST_RCC;

    // Reset timer
    TIM_MST_RESET_ON;
    TIM_MST_RESET_OFF;
  
    // Configure time base
    TimMasterHandle.Instance = TIM_MST;
    TimMasterHandle.Init.Period            = 0xFFFFFFFF;
	if ( SystemCoreClock == 16000000 ) { 
		TimMasterHandle.Init.Prescaler         = (uint32_t)( SystemCoreClock / 1000000) - 1; // 1 µs tick
	} else {
		TimMasterHandle.Init.Prescaler         = (uint32_t)( SystemCoreClock / 2 / 1000000) - 1; // 1 µs tick
	}
    TimMasterHandle.Init.ClockDivision     = 0;
    TimMasterHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
    TimMasterHandle.Init.RepetitionCounter = 0;
    HAL_TIM_OC_Init(&TimMasterHandle);

    NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
    NVIC_EnableIRQ(TIM_MST_IRQ);

    // Channel 1 for mbed timeout
    HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);

    // Channel 2 for HAL tick
    HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
    PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
    __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);

#if 0 // For DEBUG only
    __GPIOB_CLK_ENABLE();
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
#endif

    return HAL_OK;
}
Beispiel #18
0
void mbed_exit_sleep(sleep_t *obj)
{
    // Enable HAL tick interrupt
    __HAL_TIM_ENABLE_IT(&obj->TimMasterHandle, TIM_IT_CC2);
}
/**
  * @brief  Resume Tick increment.
  * @note   Enable the tick increment by Enabling TIM1 update interrupt.
  * @param  None
  * @retval None
  */
void HAL_ResumeTick(void)
{
  /* Enable TIM1 Update interrupt */
  __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE);
}
Beispiel #20
0
void HAL_ResumeTick(void)
{
    TimMasterHandle.Instance = TIM_MST;
    __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
}
/* 过零点触发中断 回调函数 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  
  if (GPIO_Pin != ZERO) 
    return;
  
  bool rising_edge;
  // 读Pin,判断是上升沿还是下降沿    
  if (HAL_GPIO_ReadPin(PORT_ZERO, ZERO) == GPIO_PIN_SET) {
    rising_edge = true;
  }
  else {
    rising_edge = false;
  }
  
  // t 顺序: 1 3 4 6
  
  // 上升沿
  if (rising_edge) {
    t6 = __HAL_TIM_GET_COUNTER(&htim3);
  }
  else {
    // 下降沿
    t4 = __HAL_TIM_GET_COUNTER(&htim3);
  }

  if (!rising_edge)  // 只在波谷计算,否则退出
    return;
  
/*----------------------------------------------------------------------------*/
    
  is_lower_blow = true;  // 设置波谷flag
  is_lower_exchange = true;
  is_lower_feed = true;
  
  /* 下降沿时计算下一个零点 */ 
  // t1 t3 t4 t6
  if ((t1<t3) && (t3<t4) && (t4<t6)) {  
    t2 = (t1+t3) / 2; 
    t5 = (t4+t6) / 2;
  }
  else if(t3<t1) {
    t2 = (t1+t3+0xffff) / 2;
    t5 = (t4+t6)/2 + 0xffff;
  }
  else if(t4<t3) {
    t2 = (t1+t3) / 2;
    t5 = (t4+t6)/2 + 0xffff;
  }
  else if(t6<t4) {
    t2 = (t1+t3) / 2;
    t5 = (t4+t6+0xffff) / 2;
  }
  
    T1 = t5 - t2;
    
    // 下一个零点    
  if (t6<t5) {
    t7 = t5 + T1/4 - t6 -0xffff;
  }
  else {
    t7 = t5 + T1/4 - t6;
  }  

  //  debug
//  counter++;
//  if (counter >= 149) {
//    Zero *z;
//    z = (Zero*)osPoolAlloc(pool_ZeroHandle);
//    if (z!=NULL) {
//      z->t1 = t1;
//      z->t3 = t3;
//      z->t4 = t4;
//      z->t6 = t6;
//      z->t7 = t7;
//      z->T1 = T1;
//      osMessagePut(Q_ErrorHandle, (uint32_t)z, 0);
//    }
//    
//    counter = 0;
//  }  
//  
  
  // 更新数值
  t1 = t4;
  t3 = t6;
    
  /*--------------------------------------------------------------------------*/
  
  // 只处理下半波
  
  if (t7 == 0) {
    t7 = 220;
  }
  
  is_lower_blow = true;  // 设置波谷flag
  is_lower_exchange = true;
  is_lower_feed = true;
  
  //uint32_t compare = 0;
  
  // 检查Q_Triac队列,更新输出的控制状态
  if (Q_TriacHandle != NULL) {    
    // 读取值
    osEvent evt = osMessageGet(Q_TriacHandle, 0);
    if (evt.status == osEventMessage) {  
      Triac *p = (Triac *)evt.value.p;  
      
      triac_cur.fan_exchange_delay = p->fan_exchange_delay; 
      triac_cur.fan_exchange_is_on = p->fan_exchange_is_on;
      triac_cur.fan_smoke_is_on = p->fan_smoke_is_on;
      triac_cur.fan_smoke_power = p->fan_smoke_power;
      triac_cur.feed_by_manual = p->feed_by_manual;
      triac_cur.feed_duty = p->feed_duty;
      triac_cur.feed_full = p->feed_full;
      triac_cur.feed_is_on = p->feed_is_on;
      
      // 更新送料duty
      if (triac_cur.feed_duty != 0) {
        // 500×triac_cur.feed_duty÷100
        on_count = 5 * triac_cur.feed_duty;  
        off_count = 500 - on_count;  // 10s = 10000ms 10000 / 20 = 500
      }
       
      // 返还内存
      osStatus status = osPoolFree(pool_TriacHandle, evt.value.p);
      if (status != osOK)
        printf("Free Triac memory error:%x\r\n", status);
    }
  }
  
/*----------------------------------------------------------------------------*/
  
  
  // 设置period
  if (triac_cur.fan_smoke_power == 0) { 
    triac_cur.fan_smoke_power = 60;
  }
  
  // 最低延时2.5ms,最高延时6ms
  uint32_t delay = t7 + triac_cur.fan_smoke_power;  
  if (delay>8999) {
    delay = last_delay;    
  }
  else {
    last_delay = delay;
  }
  
  
    // 计数器复位
  __HAL_TIM_SET_COUNTER (&htim2, 0);
  __HAL_TIM_SET_COUNTER (&htim4, 0);
  
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, delay); 
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2, t7+T1/4);
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, t7+T1/4);
  
  
  /* 输出: 条件判断  */  

  //排烟通道
  if (triac_cur.fan_smoke_is_on) {
     __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC1);
  }
    
  //循环通道
  if (triac_cur.fan_exchange_is_on) {
    __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC2);
  }
    
  //送料通道
  if ((triac_cur.feed_by_manual || triac_cur.feed_full) && (!triac_cur.feed_is_on)) {
    __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC3);
  }
  //送料通道 ON计数 OFF计数
  if (triac_cur.feed_is_on && (!triac_cur.feed_by_manual)) {  // 0
    // 送料
    if (feed_run) {
      __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC3);      
      count_feed ++;
      if (count_feed >= on_count) {        
        count_feed = 0;
        feed_run = false;
      }
      
    } 
    else {
      // 不送料
      count_feed ++;
      if (count_feed >= off_count) {
        count_feed = 0;
        feed_run = true;        
      }
    }  
  } // if 0
  
}
Beispiel #22
0
/**
  * @brief  Resume Tick increment.
  * @note   Enable the tick increment by Enabling TIM6 update interrupt.
  * @param  None
  * @retval None
  */
void HAL_ResumeTick(void)
{
  /* Enable TIM6 Update interrupt */
  __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
}
/* TIM4 打开触发脉冲 */
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
  uint32_t compare = 0;
  /* TIM4 */
  if (htim->Instance == TIM4) {
    // 1
    // 排烟风机 通道
    if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
      // 排烟风机 delay_on, 打开输出
      if (!is_lower_blow) {
        __HAL_TIM_DISABLE_IT(&htim4, TIM_IT_CC1);
      }
      
      ON_SMOKE;

      // 延时关断
      compare = __HAL_TIM_GET_COUNTER(&htim2) + uS_DELAY_OFF;
      __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, compare);
      __HAL_TIM_ENABLE_IT(&htim2, TIM_IT_CC1);
      
      // 下一个半波
      if (is_lower_blow) {
        is_lower_blow = false;
        
        compare = __HAL_TIM_GET_COUNTER(&htim4) + HALF_T1;        
        __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, compare);
        //__HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC1);
      }      
    }
    
    // 2
    // 循环风机通道
    if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {      
      // 循环风机 delay_on, 打开输出
      if (!is_lower_exchange){
        __HAL_TIM_DISABLE_IT(&htim4, TIM_IT_CC2); 
      }
      
      ON_EXCHANGE;
      
      // 延时关断
      compare = __HAL_TIM_GET_COUNTER(&htim2) + uS_DELAY_OFF;
      __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, compare);      
      __HAL_TIM_ENABLE_IT(&htim2, TIM_IT_CC2); 
      
      // 下一个半波
      if (is_lower_exchange) {
        is_lower_exchange = false;
        
        compare =  __HAL_TIM_GET_COUNTER(&htim4) + HALF_T1;        
        __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2, compare);
        //__HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC2); 
      }          
    }
    
    // 3
    // 送料电机 通道
    if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {
      // 送料
      if (!is_lower_feed) {
        __HAL_TIM_DISABLE_IT(&htim4, TIM_IT_CC3);
      }
      
      ON_FEED; 

      // 延时关断
      compare = __HAL_TIM_GET_COUNTER(&htim2) + uS_DELAY_OFF;
      __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, compare);
      __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC3); 
      
      // 下一个半波
      if (is_lower_feed){
        is_lower_feed = false;
        
        compare =  __HAL_TIM_GET_COUNTER(&htim4) + HALF_T1;        
        __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, compare);
        //__HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC3);
      }
    }
    //  
  } 
  
  /* TIM2 关闭触发脉冲 */
  // 延时x us关闭触发
  if (htim->Instance == TIM2) {
    // 1
    if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
      OFF_SMOKE;
      __HAL_TIM_DISABLE_IT(&htim2, TIM_IT_CC1);
    }
    
    // 2
    if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
      // 输出 循环:Off
      OFF_EXCHANGE;
      __HAL_TIM_DISABLE_IT(&htim2, TIM_IT_CC2);        
    }
    
    // 3
    if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {
      // 输出 送料: Off
      OFF_FEED;
      __HAL_TIM_DISABLE_IT(&htim2, TIM_IT_CC3);
    }
    
  }  // TIM2
  //
  
}