/* Includes ------------------------------------------------------------------*/ #include #include #include "main.h" #include "cmsis_os.h" /* Enumerated Types ------------------------------------------------------------*/ enum systemState { START, NEXT_STATE } simState; /* Private variables ---------------------------------------------------------*/ UART_HandleTypeDef huart2; DMA_HandleTypeDef hdma_usart2_rx; /* Global variables ---------------------------------------------------------*/ uint8_t buffer_dma_sim808[DMA_BUF_SIZE]; DMA_Event_t dma_uart_sim808 = {FALSE, 0, DMA_BUF_SIZE, FALSE, 0, '\0', FALSE}; /* Definitions for defaultTask */ osThreadId_t defaultTaskHandle, mainTaskHandle; const osThreadAttr_t defaultTask_attributes = { .name = "defaultTask", .priority = (osPriority_t) osPriorityNormal, .stack_size = 256 }; const osThreadAttr_t mainTask_attributes = { .name = "mainTask", .priority = (osPriority_t) osPriorityNormal, .stack_size = 256 }; /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_DMA_Init(void); static void MX_USART2_UART_Init(void); void StartDefaultTask(void *argument); void mainTask(void *argument); /** * @brief The application entry point. * @retval int */ int main(void) { /* MCU Configuration--------------------------------------------------------*/ /* 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_USART2_UART_Init(); /* Start DMA */ if(HAL_UART_Receive_DMA(&huart2, (uint8_t*)buffer_dma_sim808, DMA_BUF_SIZE) != HAL_OK) Error_Handler(); /* Disable Half Transfer Interrupt */ __HAL_DMA_DISABLE_IT(huart2.hdmarx, DMA_IT_HT); /* USER CODE END 2 */ /* Init scheduler */ osKernelInitialize(); /* Create the thread(s) */ mainTaskHandle = osThreadNew(mainTask, NULL, &mainTask_attributes); defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes); simState = START; /* Start scheduler */ osKernelStart(); /* We should never get here as control is now taken by the scheduler */ /* Infinite loop */ while (1) { } } void mainTask(void *argument) { /* Infinite loop */ while(1) { if(simState == START) { if(HAL_UART_Transmit(&huart2, (uint8_t*)"AT\r\n", 4, HAL_MAX_DELAY) != HAL_OK) Error_Handler(); simState = NEXT_STATE; } } } void StartDefaultTask(void *argument) { /* Infinite loop */ for(;;) { osThreadFlagsWait(0x0001, osFlagsWaitAll, osWaitForever); // Execute some operations HAL_GPIO_TogglePin(LED_NUCLEO_GPIO_Port, LED_NUCLEO_Pin); } } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance == USART2) { osThreadFlagsSet(defaultTaskHandle, 0x0001); } } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLM = 16; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; RCC_OscInitStruct.PLL.PLLQ = 7; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /** * @brief USART2 Initialization Function * @param None * @retval None */ static void MX_USART2_UART_Init(void) { huart2.Instance = USART2; huart2.Init.BaudRate = 9600; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } /* UART2 IDLE Interrupt Configuration */ SET_BIT(USART2->CR1, USART_CR1_IDLEIE); } /** * Enable DMA controller clock */ static void MX_DMA_Init(void) { /* DMA controller clock enable */ __HAL_RCC_DMA1_CLK_ENABLE(); /* DMA interrupt init */ /* DMA1_Stream5_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 6, 0); HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn); } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LED_NUCLEO_GPIO_Port, LED_NUCLEO_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : BUTTON_NUCLEO_Pin */ GPIO_InitStruct.Pin = BUTTON_NUCLEO_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(BUTTON_NUCLEO_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : LED_NUCLEO_Pin */ GPIO_InitStruct.Pin = LED_NUCLEO_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED_NUCLEO_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PB8 PB9 */ GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* EXTI interrupt init*/ HAL_NVIC_SetPriority(EXTI15_10_IRQn, 7, 0); HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); } /** * @brief Period elapsed callback in non blocking mode * @note This function is called when TIM2 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 * @retval None */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { /* USER CODE BEGIN Callback 0 */ /* USER CODE END Callback 0 */ if (htim->Instance == TIM2) { HAL_IncTick(); /* DMA timer sim808 -> overflow dopo 10ms */ if(dma_uart_sim808.timer == 1) { /* DMA Timeout event: set Timeout Flag and call DMA Rx Complete Callback */ dma_uart_sim808.flag = TRUE; hdma_usart2_rx.XferCpltCallback(&hdma_usart2_rx); } if(dma_uart_sim808.timer) --dma_uart_sim808.timer; } } /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { HAL_GPIO_WritePin(LED_NUCLEO_GPIO_Port, LED_NUCLEO_Pin, GPIO_PIN_SET); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/