AnsweredAssumed Answered

STM32F051C8 UART2 or 1 don't trigged

Question asked by aguilar_maria.enriqu on Mar 13, 2016
Hello i'm trying to configure an uart 1 or 2 interrupt in sw4STM32 and cube mx, but never responses the interrupt i can recive data from uart and send data in uart 1 and 2 in pooling mode mi code.

Main 
001.#include "stm32f0xx_hal.h"
002. 
003./* USER CODE BEGIN Includes */
004. 
005./* USER CODE END Includes */
006. 
007./* Private variables ---------------------------------------------------------*/
008.UART_HandleTypeDef huart1;
009.UART_HandleTypeDef huart2;
010. 
011./* USER CODE BEGIN PV */
012./* Private variables ---------------------------------------------------------*/
013. 
014./* USER CODE END PV */
015. 
016./* Private function prototypes -----------------------------------------------*/
017.void SystemClock_Config(void);
018.static void MX_GPIO_Init(void);
019.static void MX_USART1_UART_Init(void);
020.static void MX_USART2_UART_Init(void);
021. 
022./* USER CODE BEGIN PFP */
023./* Private function prototypes -----------------------------------------------*/
024. 
025./* USER CODE END PFP */
026. 
027./* USER CODE BEGIN 0 */
028. 
029./* USER CODE END 0 */
030. 
031.int main(void)
032.{
033. 
034.  /* USER CODE BEGIN 1 */
035.uint8_t msg[5]="12345";
036.  /* USER CODE END 1 */
037. 
038.  /* MCU Configuration----------------------------------------------------------*/
039. 
040.  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
041.  HAL_Init();
042. 
043.  /* Configure the system clock */
044.  SystemClock_Config();
045. 
046.  /* Initialize all configured peripherals */
047.  MX_GPIO_Init();
048.  MX_USART1_UART_Init();
049.  MX_USART2_UART_Init();
050. 
051.  /* USER CODE BEGIN 2 */
052. 
053.  /* USER CODE END 2 */
054. 
055.  /* Infinite loop */
056.  /* USER CODE BEGIN WHILE */
057. 
058. 
059.  while (1)
060.  {
061.      HAL_GPIO_WritePin(GPIOF, GPIO_PIN_7, GPIO_PIN_SET);
062.      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
063.      HAL_Delay(1);
064.      HAL_GPIO_WritePin(GPIOF, GPIO_PIN_7, GPIO_PIN_RESET);
065.      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
066.      HAL_Delay(1);
067.      HAL_UART_Receive(&huart1,msg,5,1000);
068.      HAL_UART_Transmit(&huart1,msg,5,1000);
069.      HAL_UART_Transmit(&huart2,msg,5,1000);
070.  /* USER CODE END WHILE */
071. 
072.  /* USER CODE BEGIN 3 */
073. 
074.  }
075.  /* USER CODE END 3 */
076. 
077.}
078. 
079./** System Clock Configuration
080.*/
081.void SystemClock_Config(void)
082.{
083. 
084.  RCC_OscInitTypeDef RCC_OscInitStruct;
085.  RCC_ClkInitTypeDef RCC_ClkInitStruct;
086.  RCC_PeriphCLKInitTypeDef PeriphClkInit;
087. 
088.  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
089.  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
090.  RCC_OscInitStruct.HSICalibrationValue = 16;
091.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
092.  HAL_RCC_OscConfig(&RCC_OscInitStruct);
093. 
094.  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
095.                              |RCC_CLOCKTYPE_PCLK1;
096.  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
097.  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
098.  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
099.  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
100. 
101.  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
102. 
103.  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
104.  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
105. 
106.  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
107. 
108.  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
109. 
110.  /* SysTick_IRQn interrupt configuration */
111.  HAL_NVIC_SetPriority(SysTick_IRQn, 3, 0);
112.}
113. 
114./* USART1 init function */
115.void MX_USART1_UART_Init(void)
116.{
117. 
118.  huart1.Instance = USART1;
119.  huart1.Init.BaudRate = 38400;
120.  huart1.Init.WordLength = UART_WORDLENGTH_8B;
121.  huart1.Init.StopBits = UART_STOPBITS_1;
122.  huart1.Init.Parity = UART_PARITY_NONE;
123.  huart1.Init.Mode = UART_MODE_TX_RX;
124.  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
125.  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
126.  huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;
127.  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
128.  HAL_UART_Init(&huart1);
129. 
130.}
131. 
132./* USART2 init function */
133.void MX_USART2_UART_Init(void)
134.{
135. 
136.  huart2.Instance = USART2;
137.  huart2.Init.BaudRate = 9600;
138.  huart2.Init.WordLength = UART_WORDLENGTH_8B;
139.  huart2.Init.StopBits = UART_STOPBITS_1;
140.  huart2.Init.Parity = UART_PARITY_NONE;
141.  huart2.Init.Mode = UART_MODE_TX_RX;
142.  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
143.  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
144.  huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;
145.  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT|UART_ADVFEATURE_DMADISABLEONERROR_INIT;
146.  huart2.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
147.  huart2.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR;
148.  HAL_UART_Init(&huart2);
149. 
150. 
151.  HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
152.  HAL_NVIC_EnableIRQ(USART2_IRQn);
153. 
154.}
155. 
156./** Configure pins as
157.        * Analog
158.        * Input
159.        * Output
160.        * EVENT_OUT
161.        * EXTI
162.*/
163.void MX_GPIO_Init(void)
164.{
165. 
166.  GPIO_InitTypeDef GPIO_InitStruct;
167. 
168.  /* GPIO Ports Clock Enable */
169.  __GPIOA_CLK_ENABLE();
170.  __GPIOB_CLK_ENABLE();
171.  __GPIOF_CLK_ENABLE();
172. 
173.  /*Configure GPIO pin : PB12 */
174.  GPIO_InitStruct.Pin = GPIO_PIN_12;
175.  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
176.  GPIO_InitStruct.Pull = GPIO_PULLUP;
177.  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
178. 
179.  /*Configure GPIO pins : PF6 PF7 */
180.  GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
181.  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
182.  GPIO_InitStruct.Pull = GPIO_NOPULL;
183.  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
184.  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
185. 
186.  /*Configure GPIO pins : PB8 PB9 */
187.  GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
188.  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
189.  GPIO_InitStruct.Pull = GPIO_NOPULL;
190.  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
191.  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
192. 
193.  /*Configure GPIO pin Output Level */
194.  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
195. 
196.  /*Configure GPIO pin Output Level */
197.  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
198. 
199.  /* EXTI interrupt init*/
200.  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
201.  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
202. 
203.}

it.c

01.#include "stm32f0xx_hal.h"
02.#include "stm32f0xx.h"
03.#include "stm32f0xx_it.h"
04. 
05./* USER CODE BEGIN 0 */
06.uint8_t msg[5]="12345";
07./* USER CODE END 0 */
08. 
09./* External variables --------------------------------------------------------*/
10.extern UART_HandleTypeDef huart2;
11. 
12./******************************************************************************/
13./*            Cortex-M0 Processor Interruption and Exception Handlers         */
14./******************************************************************************/
15. 
16./**
17.* @brief This function handles System tick timer.
18.*/
19.void SysTick_Handler(void)
20.{
21.  /* USER CODE BEGIN SysTick_IRQn 0 */
22. 
23.  /* USER CODE END SysTick_IRQn 0 */
24.  HAL_IncTick();
25.  HAL_SYSTICK_IRQHandler();
26.  /* USER CODE BEGIN SysTick_IRQn 1 */
27.//  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
28. 
29.  /* USER CODE END SysTick_IRQn 1 */
30.}
31. 
32./******************************************************************************/
33./* STM32F0xx Peripheral Interrupt Handlers                                    */
34./* Add here the Interrupt Handlers for the used peripherals.                  */
35./* For the available peripheral interrupt handler names,                      */
36./* please refer to the startup file (startup_stm32f0xx.s).                    */
37./******************************************************************************/
38. 
39./**
40.* @brief This function handles EXTI line 4 to 15 interrupts.
41.*/
42.void EXTI4_15_IRQHandler(void)
43.{
44.  /* USER CODE BEGIN EXTI4_15_IRQn 0 */
45. 
46.  /* USER CODE END EXTI4_15_IRQn 0 */
47.  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12);
48.  /* USER CODE BEGIN EXTI4_15_IRQn 1 */
49.  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
50.  /* USER CODE END EXTI4_15_IRQn 1 */
51.}
52. 
53./**
54.* @brief This function handles USART2 global interrupt.
55.*/
56.void USART2_IRQHandler(void)
57.{
58.  /* USER CODE BEGIN USART2_IRQn 0 */
62.    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_SET);
63.  /* USER CODE END USART2_IRQn 0 */
64.  HAL_UART_IRQHandler(&huart2);
65.  /* USER CODE BEGIN USART2_IRQn 1 */
66.  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_SET);
67.  /* USER CODE END USART2_IRQn 1 */
68.}
69. 
70./* USER CODE BEGIN 1 */
71. 
72./* USER CODE END 1 */
73./************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


Outcomes