STM32F1, STMCubeMx, uart_rx_interrupt
Uart Rx 인터럽트 동작을 유지하다가 Button이 눌려지면 Tx를 합니다.
Tx 동작은 HAL_UART_Transmit_IT 함수를 사용하였습니다.
Rx 동작은 HAL_UART_Receive, HAL_UART_Receive_IT 함수를 사용하지 않고
Rx 인터럽트를 통해 byte 단위로 데이터를 수신받습니다.
0. 참고 자료
1. 모듈
2. HW 구성

3. RCC > HSE > Crystal/Ceramic Resonator

4. Button Interrupt
PB1, GPIO_EXTI1
버튼을 누룰 때마다 TX를 합니다.

5. USART2 > Asynchronous
다른 보드와 테스트하기 위한 UART 라인입니다.

6. USART1 > Asynchronous
printf, 디버깅용입니다.

7. LED

8. Clock Configuration > 72MHz

9. Configuration > NVIC

10. NVIC Enabled
EXTI line 1 interrupt
USART2 global interrupt
우선순위는 0 ~ 15이며 0이 가장 높고, 15가 가장 낮은 우선순위를 가집니다.
아래 설정은 Button 인터럽트는 천천히 확인하고 UART 인터럽트를 먼저 확인하라는 의미입니다.

11. SW 구성
주요 코드만 정리합니다.
gpio.c, 함수 추가
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == GPIO_PIN_1) { gButtonPressed = 1; } } |
usart.c, 코드 추가
/* USART2 interrupt Init */ HAL_NVIC_SetPriority(USART2_IRQn, 1, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspInit 1 */
__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE); //__HAL_UART_ENABLE_IT(&huart2, UART_IT_TXE); /* USER CODE END USART2_MspInit 1 */ |
stm32f1xx_it.c, 코드 추가
void USART2_IRQHandler(void) { /* USER CODE BEGIN USART2_IRQn 0 */
/* USER CODE END USART2_IRQn 0 */ HAL_UART_IRQHandler(&huart2); /* USER CODE BEGIN USART2_IRQn 1 */
if ((USART2->SR & UART_FLAG_RXNE) != RESET) { LED2_CTRL(GPIO_TOGGLE); gRxBuffer[gRxCount] = USART2->DR; gRxCount +=1; if(gRxCount > 127) { gRxCount = 0; memset(gRxBuffer, 0x00, sizeof(gRxBuffer)); } } /* USER CODE END USART2_IRQn 1 */ } |
main.c, 코드 추가
/* USER CODE BEGIN WHILE */ while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */ if (gButtonPressed) { gButtonPressed = 0;
printf("\r\nHAL_UART_Transmit\r\n");
if (HAL_UART_Transmit_IT (&huart2, (uint8_t*)txData, sizeof(txData)) != HAL_OK) { printf("transmit error\r\n"); }
while (HAL_UART_GetState(&huart2) != HAL_UART_STATE_READY) { printf("."); } } if (gRxCount > 0) printf("rxbuffer [%s][%d]\r\n", gRxBuffer, gRxCount); HAL_Delay(500);
/* USER CODE END 2 */ } |
stm32f1 보드 로그입니다.
Rx 수신 중 Button 동작을 Tx를 하였습니다.

상대방으로 사용한 esp32 보드 로그입니다.
Tx 중에 Rx 데이터를 2번 수신하였습니다.

SD02C Stepper Driver, PC 프로그램 사용법 (0) |
2018.09.29 |
STM32F1, STMCubeMx, Timer, Stepper Motor (0) |
2018.09.29 |
STM32F1, STMCubeMX, GPIO Interrupt (0) |
2018.08.18 |
STM32F1, STMCubeMX, SDIO, SPI, VS1003B MP3 Board, Micro SD Storage Board (0) |
2018.08.06 |
STM32F1, VS1003B MP3 Board, Audacity Audio Program, Linux xxd Command (0) |
2018.08.06 |