diff --git a/Core/Inc/gpio.h b/Core/Inc/gpio.h index 95c08b2..8bab632 100644 --- a/Core/Inc/gpio.h +++ b/Core/Inc/gpio.h @@ -28,7 +28,7 @@ #include "main.h" /* USER CODE BEGIN Includes */ - +#include "gpio.h" /* USER CODE END Includes */ /* USER CODE BEGIN Private defines */ @@ -39,6 +39,9 @@ void MX_GPIO_Init(void); /* USER CODE BEGIN Prototypes */ +void demoEnumCtlInit(GPIO_PinState PinState); +void demoLedInit(); +void demoLedBlink(); /* USER CODE END Prototypes */ #ifdef __cplusplus diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 7083c0a..e4d057e 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -62,6 +62,8 @@ void Error_Handler(void); #define POWER_BUTTON_GPIO_Port GPIOE #define LED_RUN_Pin GPIO_PIN_3 #define LED_RUN_GPIO_Port GPIOE +#define SYS_WKUP_Pin GPIO_PIN_0 +#define SYS_WKUP_GPIO_Port GPIOA #define USB3_VBUS_CTL_3_Pin GPIO_PIN_12 #define USB3_VBUS_CTL_3_GPIO_Port GPIOE #define USB3_VBUS_CTL_2_Pin GPIO_PIN_13 @@ -76,6 +78,10 @@ void Error_Handler(void); #define USB2_DM_G7_GPIO_Port GPIOA #define USB2_DP_G7_Pin GPIO_PIN_12 #define USB2_DP_G7_GPIO_Port GPIOA +#define SYS_SWDIO_Pin GPIO_PIN_13 +#define SYS_SWDIO_GPIO_Port GPIOA +#define SYS_SWCLK_Pin GPIO_PIN_14 +#define SYS_SWCLK_GPIO_Port GPIOA #define USB2_VBUS_CTL_1_Pin GPIO_PIN_2 #define USB2_VBUS_CTL_1_GPIO_Port GPIOD #define USB2_VBUS_CTL_2_Pin GPIO_PIN_3 @@ -88,6 +94,8 @@ void Error_Handler(void); #define USB2_VBUS_CTL_5_GPIO_Port GPIOD #define USB2_VBUS_CTL_6_Pin GPIO_PIN_7 #define USB2_VBUS_CTL_6_GPIO_Port GPIOD +#define SYS_SWO_Pin GPIO_PIN_3 +#define SYS_SWO_GPIO_Port GPIOB #define BUZZ_CTL_Pin GPIO_PIN_6 #define BUZZ_CTL_GPIO_Port GPIOB #define CHRG_STATUS_Pin GPIO_PIN_7 diff --git a/Core/Inc/stm32f1xx_it.h b/Core/Inc/stm32f1xx_it.h index 80fb020..2b19ed9 100644 --- a/Core/Inc/stm32f1xx_it.h +++ b/Core/Inc/stm32f1xx_it.h @@ -56,10 +56,10 @@ void DebugMon_Handler(void); void PVD_IRQHandler(void); void FLASH_IRQHandler(void); void RCC_IRQHandler(void); +void TIM1_UP_IRQHandler(void); void I2C1_EV_IRQHandler(void); void I2C1_ER_IRQHandler(void); void UART4_IRQHandler(void); -void TIM7_IRQHandler(void); void OTG_FS_IRQHandler(void); /* USER CODE BEGIN EFP */ diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 501ca92..fbc5c3e 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -58,42 +58,28 @@ const osThreadAttr_t defaultTask_attributes = { .priority = (osPriority_t) osPriorityNormal, .stack_size = 128 * 4 }; -/* Definitions for LEDBlinkTask */ -osThreadId_t LEDBlinkTaskHandle; -const osThreadAttr_t LEDBlinkTask_attributes = { - .name = "LEDBlinkTask", - .priority = (osPriority_t) osPriorityNormal, +/* Definitions for LedBlinkTask */ +osThreadId_t LedBlinkTaskHandle; +const osThreadAttr_t LedBlinkTask_attributes = { + .name = "LedBlinkTask", + .priority = (osPriority_t) osPriorityLow, .stack_size = 128 * 4 }; /* Definitions for IWDGRefreshTask */ osThreadId_t IWDGRefreshTaskHandle; const osThreadAttr_t IWDGRefreshTask_attributes = { .name = "IWDGRefreshTask", - .priority = (osPriority_t) osPriorityNormal, + .priority = (osPriority_t) osPriorityHigh, .stack_size = 128 * 4 }; -/* Definitions for printfTask */ -osThreadId_t printfTaskHandle; -const osThreadAttr_t printfTask_attributes = { - .name = "printfTask", - .priority = (osPriority_t) osPriorityLow, - .stack_size = 256 * 4 -}; -/* Definitions for printfQueue */ -osMessageQueueId_t printfQueueHandle; -const osMessageQueueAttr_t printfQueue_attributes = { - .name = "printfQueue" -}; /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ - /* USER CODE END FunctionPrototypes */ -void startDefaultTask(void *argument); -void startLEDBlinkTask(void *argument); -void startIWDGRefreshTask(void *argument); -void startPrintfTask(void *argument); +void StartDefaultTask(void *argument); +void StartLedBlinkTask(void *argument); +void StartIWDGRefreshTask(void *argument); extern void MX_USB_DEVICE_Init(void); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ @@ -136,26 +122,19 @@ void MX_FREERTOS_Init(void) { /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */ - /* Create the queue(s) */ - /* creation of printfQueue */ - printfQueueHandle = osMessageQueueNew (128, sizeof(uint8_t), &printfQueue_attributes); - /* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ /* creation of defaultTask */ - defaultTaskHandle = osThreadNew(startDefaultTask, NULL, &defaultTask_attributes); + defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes); - /* creation of LEDBlinkTask */ - LEDBlinkTaskHandle = osThreadNew(startLEDBlinkTask, NULL, &LEDBlinkTask_attributes); + /* creation of LedBlinkTask */ + LedBlinkTaskHandle = osThreadNew(StartLedBlinkTask, NULL, &LedBlinkTask_attributes); /* creation of IWDGRefreshTask */ - IWDGRefreshTaskHandle = osThreadNew(startIWDGRefreshTask, NULL, &IWDGRefreshTask_attributes); - - /* creation of printfTask */ - printfTaskHandle = osThreadNew(startPrintfTask, NULL, &printfTask_attributes); + IWDGRefreshTaskHandle = osThreadNew(StartIWDGRefreshTask, NULL, &IWDGRefreshTask_attributes); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ @@ -164,73 +143,62 @@ void MX_FREERTOS_Init(void) { } -/* USER CODE BEGIN Header_startDefaultTask */ +/* USER CODE BEGIN Header_StartDefaultTask */ /** * @brief Function implementing the defaultTask thread. * @param argument: Not used * @retval None */ -/* USER CODE END Header_startDefaultTask */ -void startDefaultTask(void *argument) +/* USER CODE END Header_StartDefaultTask */ +void StartDefaultTask(void *argument) { /* init code for USB_DEVICE */ MX_USB_DEVICE_Init(); - /* USER CODE BEGIN startDefaultTask */ - - osVersion_t version; - char id_buf[128]; - uint32_t id_size; - osKernelGetInfo(&version, &id_buf, id_size); - + /* USER CODE BEGIN StartDefaultTask */ + demoEnumCtlInit(GPIO_PIN_SET); /* Infinite loop */ for(;;) { - osDelay(1000); + osDelay(1000); printf("[%f],NumofTasks:%d\n",(float)(osKernelGetTickCount()/1000.0),(int)(osThreadGetCount())); - printf("Name:%s\n",osThreadGetName(defaultTaskHandle)); - printf("state:%s\n",osKernelGetState()); - +// printf("Name:%s\n",osThreadGetName(defaultTaskHandle)); +// printf("state:%s\n",osKernelGetState()); } - /* USER CODE END startDefaultTask */ + /* USER CODE END StartDefaultTask */ } -/* USER CODE BEGIN Header_startLEDBlinkTask */ +/* USER CODE BEGIN Header_StartLedBlinkTask */ /** -* @brief Function implementing the LEDBlinkTask thread. +* @brief Function implementing the LedBlinkTask thread. * @param argument: Not used * @retval None */ -/* USER CODE END Header_startLEDBlinkTask */ -void startLEDBlinkTask(void *argument) +/* USER CODE END Header_StartLedBlinkTask */ +void StartLedBlinkTask(void *argument) { - /* USER CODE BEGIN startLEDBlinkTask */ + /* USER CODE BEGIN StartLedBlinkTask */ /* Infinite loop */ - GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = POWER_BUTTON_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(POWER_BUTTON_GPIO_Port, &GPIO_InitStruct); + demoLedInit(); for(;;) { // HAL_GPIO_TogglePin(LED_RUN_GPIO_Port,LED_RUN_Pin); - HAL_GPIO_TogglePin(POWER_BUTTON_GPIO_Port,POWER_BUTTON_Pin); // printf("led toggled\n"); + demoLedBlink(); osDelay(500); } - /* USER CODE END startLEDBlinkTask */ + /* USER CODE END StartLedBlinkTask */ } -/* USER CODE BEGIN Header_startIWDGRefreshTask */ +/* USER CODE BEGIN Header_StartIWDGRefreshTask */ /** * @brief Function implementing the IWDGRefreshTask thread. * @param argument: Not used * @retval None */ -/* USER CODE END Header_startIWDGRefreshTask */ -void startIWDGRefreshTask(void *argument) +/* USER CODE END Header_StartIWDGRefreshTask */ +void StartIWDGRefreshTask(void *argument) { - /* USER CODE BEGIN startIWDGRefreshTask */ + /* USER CODE BEGIN StartIWDGRefreshTask */ /* Infinite loop */ for(;;) { @@ -238,31 +206,14 @@ void startIWDGRefreshTask(void *argument) // printf("iwdg refreshed\n"); osDelay(1000); } - /* USER CODE END startIWDGRefreshTask */ -} - -/* USER CODE BEGIN Header_startPrintfTask */ -/** -* @brief Function implementing the printfTask thread. -* @param argument: Not used -* @retval None -*/ -/* USER CODE END Header_startPrintfTask */ -void startPrintfTask(void *argument) -{ - /* USER CODE BEGIN startPrintfTask */ - /* Infinite loop */ - for(;;) - { -// osMessageQueueGet(); -// printfQueueHandle = osMessageQueueNew (128, sizeof(uint8_t), &printfQueue_attributes); - osDelay(1); - } - /* USER CODE END startPrintfTask */ + /* USER CODE END StartIWDGRefreshTask */ } /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ + + + #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) PUTCHAR_PROTOTYPE diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index dca7679..54833f5 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -110,7 +110,30 @@ void MX_GPIO_Init(void) } /* USER CODE BEGIN 2 */ +void demoEnumCtlInit(GPIO_PinState PinState) +{ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + GPIO_InitStruct.Pin = GPIO_PIN_8; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8, PinState); +} +void demoLedInit() +{ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + GPIO_InitStruct.Pin = POWER_BUTTON_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(POWER_BUTTON_GPIO_Port, &GPIO_InitStruct); +} +void demoLedBlink() +{ + HAL_GPIO_TogglePin(POWER_BUTTON_GPIO_Port,POWER_BUTTON_Pin); +} /* USER CODE END 2 */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/Core/Src/iwdg.c b/Core/Src/iwdg.c index 5057a2a..2415a08 100644 --- a/Core/Src/iwdg.c +++ b/Core/Src/iwdg.c @@ -21,7 +21,7 @@ #include "iwdg.h" /* USER CODE BEGIN 0 */ - +//PreScaler=16 Reload=0xfff =====> Overflow Time=1.683S /* USER CODE END 0 */ IWDG_HandleTypeDef hiwdg; diff --git a/Core/Src/main.c b/Core/Src/main.c index 7618d29..9ed8408 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -97,12 +97,13 @@ int main(void) MX_UART4_Init(); /* USER CODE BEGIN 2 */ HAL_IWDG_Refresh(&hiwdg); - printf("Core initial successfully\n"); + printf("Core initial successfully\n"); + demoEnumCtlInit(GPIO_PIN_RESET); /* USER CODE END 2 */ /* Init scheduler */ osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */ - MX_FREERTOS_Init(); + MX_FREERTOS_Init(); /* Start scheduler */ osKernelStart(); @@ -178,7 +179,7 @@ void SystemClock_Config(void) /** * @brief Period elapsed callback in non blocking mode - * @note This function is called when TIM7 interrupt took place, inside + * @note This function is called when TIM1 interrupt took place, inside * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment * a global variable "uwTick" used as application time base. * @param htim : TIM handle @@ -189,7 +190,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) /* USER CODE BEGIN Callback 0 */ /* USER CODE END Callback 0 */ - if (htim->Instance == TIM7) { + if (htim->Instance == TIM1) { HAL_IncTick(); } /* USER CODE BEGIN Callback 1 */ diff --git a/Core/Src/stm32f1xx_hal_timebase_tim.c b/Core/Src/stm32f1xx_hal_timebase_tim.c index b9d1a53..6f3785d 100644 --- a/Core/Src/stm32f1xx_hal_timebase_tim.c +++ b/Core/Src/stm32f1xx_hal_timebase_tim.c @@ -26,12 +26,12 @@ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ -TIM_HandleTypeDef htim7; +TIM_HandleTypeDef htim1; /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /** - * @brief This function configures the TIM7 as a time base source. + * @brief This function configures the TIM1 as a time base source. * The time source is configured to have 1ms time base with a dedicated * Tick interrupt priority. * @note This function is called automatically at the beginning of program after @@ -46,41 +46,41 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) uint32_t uwPrescalerValue = 0; uint32_t pFLatency; - /*Configure the TIM7 IRQ priority */ - HAL_NVIC_SetPriority(TIM7_IRQn, TickPriority ,0); + /*Configure the TIM1 IRQ priority */ + HAL_NVIC_SetPriority(TIM1_UP_IRQn, TickPriority ,0); - /* Enable the TIM7 global Interrupt */ - HAL_NVIC_EnableIRQ(TIM7_IRQn); + /* Enable the TIM1 global Interrupt */ + HAL_NVIC_EnableIRQ(TIM1_UP_IRQn); - /* Enable TIM7 clock */ - __HAL_RCC_TIM7_CLK_ENABLE(); + /* Enable TIM1 clock */ + __HAL_RCC_TIM1_CLK_ENABLE(); /* Get clock configuration */ HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); - /* Compute TIM7 clock */ - uwTimclock = 2*HAL_RCC_GetPCLK1Freq(); + /* Compute TIM1 clock */ + uwTimclock = HAL_RCC_GetPCLK2Freq(); - /* Compute the prescaler value to have TIM7 counter clock equal to 1MHz */ + /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1); - /* Initialize TIM7 */ - htim7.Instance = TIM7; + /* Initialize TIM1 */ + htim1.Instance = TIM1; /* Initialize TIMx peripheral as follow: - + Period = [(TIM7CLK/1000) - 1]. to have a (1/1000) s time base. + + Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. + ClockDivision = 0 + Counter direction = Up */ - htim7.Init.Period = (1000000 / 1000) - 1; - htim7.Init.Prescaler = uwPrescalerValue; - htim7.Init.ClockDivision = 0; - htim7.Init.CounterMode = TIM_COUNTERMODE_UP; - if(HAL_TIM_Base_Init(&htim7) == HAL_OK) + htim1.Init.Period = (1000000 / 1000) - 1; + htim1.Init.Prescaler = uwPrescalerValue; + htim1.Init.ClockDivision = 0; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + if(HAL_TIM_Base_Init(&htim1) == HAL_OK) { /* Start the TIM time Base generation in interrupt mode */ - return HAL_TIM_Base_Start_IT(&htim7); + return HAL_TIM_Base_Start_IT(&htim1); } /* Return function status */ @@ -89,26 +89,26 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) /** * @brief Suspend Tick increment. - * @note Disable the tick increment by disabling TIM7 update interrupt. + * @note Disable the tick increment by disabling TIM1 update interrupt. * @param None * @retval None */ void HAL_SuspendTick(void) { - /* Disable TIM7 update Interrupt */ - __HAL_TIM_DISABLE_IT(&htim7, TIM_IT_UPDATE); + /* Disable TIM1 update Interrupt */ + __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); } /** * @brief Resume Tick increment. - * @note Enable the tick increment by Enabling TIM7 update interrupt. + * @note Enable the tick increment by Enabling TIM1 update interrupt. * @param None * @retval None */ void HAL_ResumeTick(void) { - /* Enable TIM7 Update interrupt */ - __HAL_TIM_ENABLE_IT(&htim7, TIM_IT_UPDATE); + /* Enable TIM1 Update interrupt */ + __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/Core/Src/stm32f1xx_it.c b/Core/Src/stm32f1xx_it.c index 21771b1..6cd5ba3 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -61,7 +61,7 @@ extern PCD_HandleTypeDef hpcd_USB_OTG_FS; extern I2C_HandleTypeDef hi2c1; extern UART_HandleTypeDef huart4; -extern TIM_HandleTypeDef htim7; +extern TIM_HandleTypeDef htim1; /* USER CODE BEGIN EV */ @@ -205,6 +205,20 @@ void RCC_IRQHandler(void) /* USER CODE END RCC_IRQn 1 */ } +/** + * @brief This function handles TIM1 update interrupt. + */ +void TIM1_UP_IRQHandler(void) +{ + /* USER CODE BEGIN TIM1_UP_IRQn 0 */ + + /* USER CODE END TIM1_UP_IRQn 0 */ + HAL_TIM_IRQHandler(&htim1); + /* USER CODE BEGIN TIM1_UP_IRQn 1 */ + + /* USER CODE END TIM1_UP_IRQn 1 */ +} + /** * @brief This function handles I2C1 event interrupt. */ @@ -247,20 +261,6 @@ void UART4_IRQHandler(void) /* USER CODE END UART4_IRQn 1 */ } -/** - * @brief This function handles TIM7 global interrupt. - */ -void TIM7_IRQHandler(void) -{ - /* USER CODE BEGIN TIM7_IRQn 0 */ - - /* USER CODE END TIM7_IRQn 0 */ - HAL_TIM_IRQHandler(&htim7); - /* USER CODE BEGIN TIM7_IRQn 1 */ - - /* USER CODE END TIM7_IRQn 1 */ -} - /** * @brief This function handles USB OTG FS global interrupt. */ diff --git a/USB_DEVICE/Target/usbd_conf.h b/USB_DEVICE/Target/usbd_conf.h index ce7fcb3..e6fc6f8 100644 --- a/USB_DEVICE/Target/usbd_conf.h +++ b/USB_DEVICE/Target/usbd_conf.h @@ -69,7 +69,7 @@ /*---------- -----------*/ #define USBD_MAX_STR_DESC_SIZ 512 /*---------- -----------*/ -#define USBD_DEBUG_LEVEL 0 +#define USBD_DEBUG_LEVEL 3 /*---------- -----------*/ #define USBD_SELF_POWERED 1 /*---------- -----------*/