/** * @brief This function handles EXTI line0 interrupt. */ void EXTI0_IRQHandler(void) { /* USER CODE BEGIN EXTI0_IRQn 0 */ // Checks if the interrupt has been triggered by a rising edge( first // pressing of button), starts a 2secs timer and make the line falling // edge sensitive. if ((EXTI-> RTSR) & (GPIO_PIN_0)) { HAL_TIM_Base_Start_IT(&htim6); EXTI-> RTSR &= (~GPIO_PIN_0); EXTI-> FTSR |= GPIO_PIN_0; } else { // If a falling edge is detected( button is released) before the countdown // ends, we stop the countdown, reset it and make the line rising // edge sensitive again. HAL_TIM_Base_Stop_IT(&htim6); __HAL_TIM_SET_COUNTER(&htim6,0); EXTI-> FTSR &= (~GPIO_PIN_0); EXTI-> RTSR |= GPIO_PIN_0; } /* USER CODE END EXTI0_IRQn 0 */ HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); /* USER CODE BEGIN EXTI0_IRQn 1 */ /* USER CODE END EXTI0_IRQn 1 */ }
void Timer2_Cmd( uint32_t cmd ) { if (cmd == ENABLE) { HAL_TIM_Base_Start_IT(hTimer2.handle); } else { HAL_TIM_Base_Stop_IT(hTimer2.handle); } }
void vMBPortTimersDisable( ) { /* Stop Channel1 */ if (HAL_TIM_Base_Stop_IT(&TimHandle) != HAL_OK) { /* Stoping Error */ Error_Handler(); } }
/** * @brief Period elapsed callback in non blocking mode * @param htim : TIM handle * @retval None */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { __HAL_TIM_SET_COUNTER(htim, 0); switch (KB_state) { case BTN_Down: if ((len_KB_str == 0) || (pKB_str == NULL)) { HAL_TIM_Base_Stop_IT(htim); return; } KB_state = BTN_Up; char2KBID(*pKB_str); SEGGER_RTT_printf(0, "USB send char %c \r\n" , *pKB_str); pKB_str++; len_KB_str--; USBD_HID_SendReport(&USBD_Device, KB_USBBuf, HID_KB_SIZE); __HAL_TIM_SET_COUNTER(&TimHandle, 0); __HAL_TIM_SET_AUTORELOAD(&TimHandle, 200-1); // 200ms HAL_TIM_Base_Start_IT(&TimHandle); break; case BTN_Up: memset(KB_USBBuf, 0, 9); KB_USBBuf[0] = 1; USBD_HID_SendReport(&USBD_Device, KB_USBBuf, HID_KB_SIZE); if (len_KB_str == 0) { pKB_str = NULL; HAL_TIM_Base_Stop_IT(htim); } else { KB_state = BTN_Down; __HAL_TIM_SET_COUNTER(&TimHandle, 0); __HAL_TIM_SET_AUTORELOAD(&TimHandle, 200-1); // 200ms HAL_TIM_Base_Start_IT(&TimHandle); } break; default: pKB_str = NULL; HAL_TIM_Base_Stop_IT(htim); break; } }
/** * @brief Stop the execution for duration usec/2. * @param duration : The pause duration express in usec/2. */ void buzz_sleep(uint32_t duration) { uint64_t globalBuzzerTickstart = 0; globalBuzzerTickstart = globalBuzzerTick; /* Start the timer on Channel1 */ if (HAL_TIM_Base_Start_IT(&BuzzerTimHandle) != HAL_OK) { /* Starting Error */ } /* Wait */ while ((globalBuzzerTick - globalBuzzerTickstart) < duration) { } /* Stop the timer on Channel1 */ if (HAL_TIM_Base_Stop_IT(&BuzzerTimHandle) != HAL_OK) { /* Starting Error */ } }
void CAM_changeClockSpeed(CLOCKSPEED speed) { /* Wait for clock falling edge */ CAM_waitClockFalling(); /* Stop the timer */ HAL_TIM_PWM_Stop_IT(&htim, TIM_CHANNEL_2); HAL_TIM_Base_Stop_IT(&htim); /* De-initialize the timer */ HAL_TIM_PWM_DeInit(&htim); HAL_TIM_Base_DeInit(&htim); /* Re-initialize the timer */ CAM_TIM_init(speed); /* And finally restart it along with PWM */ HAL_TIM_Base_Start_IT(&htim); // Providing PeriodElapsed Callback HAL_TIM_PWM_Start_IT(&htim, TIM_CHANNEL_2); // Providing PulseFinished Callback }
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) { if(tim_baseHandle->Instance==TIM2) { if(HAL_TIM_Base_Stop_IT(tim_baseHandle) != HAL_OK) { Error_Handler(); } /* Peripheral clock disable */ __HAL_RCC_TIM2_CLK_DISABLE(); /* Peripheral interrupt Deinit*/ HAL_NVIC_DisableIRQ(TIM2_IRQn); } else if(tim_baseHandle->Instance==TIM3) { /* Peripheral clock disable */ __HAL_RCC_TIM3_CLK_DISABLE(); } }
/** * @brief The application entry point. * * @retval None */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); MX_TIM3_Init(); //MX_IWDG_Init(); MX_TIM14_Init(); MX_TIM16_Init(); MX_TIM17_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL); HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM3_IRQn); HAL_TIM_Base_Start_IT(&htim3); // Seconds Counter startup duty HAL_TIM_Base_Start_IT(&htim14); HAL_TIM_Base_Stop_IT(&htim14); HAL_TIM_Base_Start_IT(&htim16); HAL_TIM_Base_Start_IT(&htim17); HAL_GPIO_WritePin(GPIOA, LED_RED_PIN, GPIO_PIN_RESET); ssd1306_Init(); current_cursor = exposition; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ //HAL_IWDG_Refresh(&hiwdg); if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE) HAL_GPIO_WritePin(GPIOA, LED_GREEN_PIN, GPIO_PIN_SET); else HAL_GPIO_WritePin(GPIOA, LED_GREEN_PIN, GPIO_PIN_RESET); if (update_screen_flag) { ssd1306_Fill(Black); ssd1306_UpdateScreen(); update_screen_flag = 0; } if (seconds_counter > STOP_TIME_SEC) //if more than 10 s need to stop { // Entering STOP Mode Procedure seconds_counter = 0; GPIO_InitTypeDef GPIO_InitStruct; ssd1306_WriteCommand(0xAE); // OLED Off GPIO_InitStruct.Pin = ENC_BUTTON_PIN; GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING_FALLING; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); HAL_GPIO_WritePin(GPIOA, LED_GREEN_PIN|LED_RED_PIN|GATE_PIN|FOCUS_PIN, GPIO_PIN_RESET); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFE); // Exit from STOP Mode procedure SystemClock_Config(); ssd1306_WriteCommand(0xAF); // OLED On GPIO_InitStruct.Pin = ENC_BUTTON_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); current_state = menu_navigation; } Menu(); if (current_state == running_timer || current_state == running_interval) { if(gate_flag) { // Reset Gate & Focus HAL_GPIO_WritePin(GPIOA, FOCUS_PIN, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOA, GATE_PIN, GPIO_PIN_RESET); HAL_TIM_Base_Stop_IT(&htim14); HAL_Delay(500); __HAL_TIM_SET_COUNTER(&htim14, 0); HAL_TIM_Base_Start_IT(&htim14); gate_flag = 0; // If not last shot, go to Interval state if(tmp_num_shots != 0) { tmp_int_minutes = set_int_minutes; tmp_int_sec = set_int_sec; tmp_exp_minutes = set_exp_minutes; tmp_exp_sec = set_exp_sec; current_state = running_interval; HAL_GPIO_WritePin(GPIOA, LED_RED_PIN,GPIO_PIN_RESET); // 1s LED Off } else { HAL_TIM_Base_Stop_IT(&htim14); __HAL_TIM_SET_COUNTER(&htim14, 0); HAL_GPIO_WritePin(GPIOA, LED_RED_PIN,GPIO_PIN_RESET); // 1s LED Off tmp_exp_minutes = set_exp_minutes; tmp_exp_sec = set_exp_sec; current_state = menu_navigation; update_screen_flag = 1; } } } if (exiting_run) { HAL_TIM_Base_Stop_IT(&htim16); HAL_Delay(1000); exiting_run = 0; HAL_TIM_Base_Start_IT(&htim16); HAL_GPIO_WritePin(GPIOA, FOCUS_PIN, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOA, GATE_PIN, GPIO_PIN_RESET); } } /* USER CODE END 3 */ }
void StopTimeCount(void) { TimeCount = 0; HAL_TIM_Base_Stop_IT(&htim2); }
/** * @brief Suspend callback. * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it) * @param hpcd: PCD handle * @retval None */ void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) { U8 i,y; // USBD_HID_HandleTypeDef *ptr; // ptr = (USBD_HID_HandleTypeDef *)(hUsbDeviceFS.pClassData); // GPIO_InitTypeDef GPIO_InitStruct; /* Inform USB library that core enters in suspend Mode */ USBD_LL_Suspend(hpcd->pData); /*Enter in STOP mode */ /* USER CODE BEGIN 2 */ if (hpcd->Init.low_power_enable) { //[ slash // for (y = COLS; y < COLS+ROWS; y++) { // Scan, save, and update the current keyboard state // gpio_setDirvalue(matrix[y].port, matrix[y].pin, GPIO_MODE_OUTPUT_PP, GPIO_PIN_SET); // for (i = 0; i < 10; i++); // } if (HAL_TIM_Base_Stop_IT(&htim2) != HAL_OK) { /* Starting Error */ while(1); } HAL_I2CEx_AnalogFilter_Config(&hi2c2, I2C_ANALOGFILTER_DISABLE); if (HAL_I2C_DeInit(&hi2c2)!= HAL_OK) { while(1); } for (i=0;i<3;i++) { ((USBD_HID_HandleTypeDef *)&hUsbDeviceFS.pClassData)->state[i] = HID_BUSY; } HAL_GPIO_WritePin(LED_RESET_GPIO_Port, LED_RESET_Pin, GPIO_PIN_RESET); // Low to shut down led controller // for (i = 0; i < 10; i++); // GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 // |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7 // |GPIO_PIN_15; // GPIO_InitStruct.Pin = GPIO_PIN_15; // GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; // GPIO_InitStruct.Pull = GPIO_NOPULL; // HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); ////// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0); ////// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_1); ////// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_2); ////// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_3); ////// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4); ////// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_5); ////// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_6); ////// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_7); // __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_15); __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_All); //// HAL_NVIC_SetPriority(EXTI0_1_IRQn, 3, 0); HAL_NVIC_EnableIRQ(EXTI0_1_IRQn); //// HAL_NVIC_SetPriority(EXTI2_3_IRQn, 3, 0); HAL_NVIC_EnableIRQ(EXTI2_3_IRQn); // HAL_NVIC_SetPriority(EXTI4_15_IRQn, 3, 0); HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_1); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_2); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_3); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_5); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_6); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_7); //// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_15); // __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_All); //// HAL_NVIC_DisableIRQ(I2C2_IRQn); //] for (y = 0; y < COLS; y++) { // Scan, save, and update the current keyboard state gpio_setDirvalue(matrix[y].port, matrix[y].pin, GPIO_MODE_OUTPUT_PP, GPIO_PIN_RESET); for (i = 0; i < 10; i++); } for (y = 0; y < COLS; y++) { // Scan, save, and update the current keyboard state gpio_setDirvalue(matrix[y].port, matrix[y].pin, GPIO_MODE_OUTPUT_PP, GPIO_PIN_RESET); for (i = 0; i < 10; i++); } /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */ // SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk)); } /* USER CODE END 2 */ }
int main(void) { /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_ADC_Init(); MX_SPI1_Init(); MX_USB_DEVICE_Init(); MX_TIM14_Init(); HAL_FLASH_Unlock(); EE_Init(); EE_ReadVariable(VirtAddVarTab[0], &x_low_th); EE_ReadVariable(VirtAddVarTab[1], &x_high_th); EE_ReadVariable(VirtAddVarTab[2], &y_low_th); EE_ReadVariable(VirtAddVarTab[3], &y_high_th); HAL_FLASH_Lock(); HAL_ADC_Start_DMA(&hadc, (uint32_t*)axis, 5); while (1) { HAL_GPIO_WritePin(SPI1_nCS_GPIO_Port, SPI1_nCS_Pin, GPIO_PIN_SET); HAL_TIM_Base_Start_IT(&htim14); while (!u100ticks) /* do nothing for 100 us */; HAL_TIM_Base_Stop_IT(&htim14); u100ticks = 0; HAL_StatusTypeDef status = HAL_SPI_Receive(&hspi1, rx_buffer, sizeof(rx_buffer), 3000); switch(status) { case HAL_OK: report.buttons[0] = 0x00; // report.buttons[1] = 0x00; report.buttons[2] = 0x00; if ((rx_buffer[0] & 0xff) != 0xff) // if all bits of rx_buffer[0] is 1 assume shifter is disconnected { if (rx_buffer[0] & 4) report.buttons[0] |= 1; else report.buttons[0] &= ~1; if (rx_buffer[0] & 1) report.buttons[0] |= (1 << 1); else report.buttons[0] &= ~(1 << 1); if (rx_buffer[0] & 2) report.buttons[0] |= (1 << 2); else report.buttons[0] &= ~(1 << 2); if (rx_buffer[0] & 8) report.buttons[0] |= (1 << 3); else report.buttons[0] &= ~(1 << 3); if (rx_buffer[1] & 1) report.buttons[2] |= 1; else report.buttons[2] &= ~1; if (rx_buffer[1] & 2) report.buttons[2] |= (1 << 1); else report.buttons[2] &= ~(1 << 1); if (rx_buffer[1] & 4) report.buttons[2] |= (1 << 2); else report.buttons[2] &= ~(1 << 2); if (rx_buffer[1] & 8) report.buttons[2] |= (1 << 3); else report.buttons[2] &= ~(1 << 3); if (rx_buffer[1] & 32) report.buttons[0] |= (1 << 4); else report.buttons[0] &= ~(1 << 4); if (rx_buffer[1] & 128) report.buttons[0] |= (1 << 5); else report.buttons[0] &= ~(1 << 5); if (rx_buffer[1] & 64) report.buttons[0] |= (1 << 6); else report.buttons[0] &= ~(1 << 6); if (rx_buffer[1] & 16) report.buttons[0] |= (1 << 7); else report.buttons[0] &= ~(1 << 7); } break; case HAL_TIMEOUT: case HAL_BUSY: case HAL_ERROR: Error_Handler(); default: report.buttons[0] = 0xff; break; } HAL_GPIO_WritePin(SPI1_nCS_GPIO_Port, SPI1_nCS_Pin, GPIO_PIN_RESET); report.id = 0x01; x_axis = ((X_AXIS << 2) + x_axis * 96) / 100; y_axis = ((Y_AXIS << 2) + y_axis * 96) / 100; if (rx_buffer[0] & 16) { if (y_axis < y_low_th) // stick towards player { report.buttons[1] = 128; report.buttons[2] &= ~(1 << 4); } else if (y_axis > y_high_th) // stick opposite to player { report.buttons[1] = 0; // neutral report.buttons[2] |= (1 << 4); } else { report.buttons[1] = 0; // neutral report.buttons[2] &= ~(1 << 4); } } else { report.buttons[1] &= ~(1 << 7); report.buttons[2] &= ~(1 << 4); if (y_axis < y_low_th) // stick towards player { if (x_axis < x_low_th) { if (!report.buttons[1]) report.buttons[1] = 2; // 2nd gear } else if (!report.buttons[1]) { report.buttons[1] = (x_axis > x_high_th) ? ((rx_buffer[0] & 64) ? 64 : 32) : 8; } } else { if (y_axis > y_high_th) // stick opposite to player { if (x_axis < x_low_th) { if (!report.buttons[1]) report.buttons[1] = 1; // 1st gear } else if (!report.buttons[1]) { report.buttons[1] = (x_axis > x_high_th) ? 16 : 4; } } else { report.buttons[1] = 0; // neutral } } } report.axis[0] = x_axis; report.axis[1] = y_axis; if (report2send == 2) { HAL_FLASH_Unlock(); EE_WriteVariable(VirtAddVarTab[0], x_low_th); EE_WriteVariable(VirtAddVarTab[1], x_high_th); EE_WriteVariable(VirtAddVarTab[2], y_low_th); EE_WriteVariable(VirtAddVarTab[3], y_high_th); HAL_FLASH_Lock(); report2send = 0; } if (hUsbDeviceFS.pClassData && ((USBD_HID_HandleTypeDef *)hUsbDeviceFS.pClassData)->state == HID_IDLE) { USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&report, sizeof(report)); } } }
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { /* try to USB transmit, if we succeed & buffer empty then we can stop TX timer until next time */ if(CDC_Transmit_FS() == USBD_OK && usart2cdc_rx == usart2cdc_tx) HAL_TIM_Base_Stop_IT(&husbtimer); }
void Heartbeat_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { HAL_TIM_Base_Stop_IT(&htim10); heartbeat_ready = TRUE; }
void stopsyncpulse(void){ //Stops pulsing for VD_IN HAL_TIM_Base_Stop_IT(&htim8); HAL_TIM_OC_Stop_IT(&htim8,TIM_CHANNEL_1); }
void stopcounter(void){ HAL_TIM_Base_Stop_IT(&htim1); }
void TIMER5_Stop(void) { HAL_ASSERT(HAL_OK == HAL_TIM_Base_Stop_IT(&timer_hd)); }