aboutsummaryrefslogtreecommitdiff
path: root/src/uart-test
diff options
context:
space:
mode:
Diffstat (limited to 'src/uart-test')
-rw-r--r--src/uart-test/main.c25
-rw-r--r--src/uart-test/stm32f4xx_hal_msp.c20
-rw-r--r--src/uart-test/stm32f4xx_it.c25
-rw-r--r--src/uart-test/stm_init.c50
-rw-r--r--src/uart-test/stm_init.h1
5 files changed, 75 insertions, 46 deletions
diff --git a/src/uart-test/main.c b/src/uart-test/main.c
index 77c17f4..55ec6fb 100644
--- a/src/uart-test/main.c
+++ b/src/uart-test/main.c
@@ -9,24 +9,45 @@
#define DELAY() HAL_Delay(250)
+UART_HandleTypeDef *huart;
+
+/*
+ * If a newline is received on UART1 or UART2, redirect output to that UART.
+ */
+void check_uart_rx(UART_HandleTypeDef *this) {
+ uint8_t rx = 0;
+ if (HAL_UART_Receive(this, &rx, 1, 0) == HAL_OK) {
+ if (rx == '\n') {
+ HAL_GPIO_TogglePin(LED_PORT, LED_GREEN);
+
+ huart = this;
+ }
+ }
+}
+
int
main()
{
uint8_t c = 'a';
- uint32_t i = 0;
stm_init();
+ huart = &huart1;
+
while (1)
{
HAL_GPIO_TogglePin(LED_PORT, LED_YELLOW);
- HAL_UART_Transmit(&huart1, (uint8_t *) &c, 1, 0xff);
+ HAL_UART_Transmit(huart, (uint8_t *) &c, 1, 0xff);
DELAY();
if (c++ == 'z') {
c = 'a';
HAL_GPIO_TogglePin(LED_PORT, LED_BLUE);
}
+
+ /* Check for UART change request */
+ check_uart_rx(&huart1);
+ check_uart_rx(&huart2);
}
}
diff --git a/src/uart-test/stm32f4xx_hal_msp.c b/src/uart-test/stm32f4xx_hal_msp.c
index 7cee9ce..4416195 100644
--- a/src/uart-test/stm32f4xx_hal_msp.c
+++ b/src/uart-test/stm32f4xx_hal_msp.c
@@ -5,6 +5,8 @@
*
* PA9: USART1_TX
* PA10: USART1_RX
+ * PA2: USART2_TX
+ * PA3: USART2_RX
*/
#include "stm32f4xx_hal.h"
@@ -19,14 +21,19 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
__USART1_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- /* NVIC for interrupt mode */
- HAL_NVIC_SetPriority(USART1_IRQn, 0, 1);
- HAL_NVIC_EnableIRQ(USART1_IRQn);
+ } else if (huart->Instance == USART2) {
+ /* Peripheral clock enable */
+ __USART2_CLK_ENABLE();
+ GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
+ GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
+ HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
@@ -37,6 +44,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
if (huart->Instance == USART1) {
__USART1_CLK_DISABLE();
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
+ } else if (huart->Instance == USART2) {
+ __USART2_CLK_DISABLE();
+ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
}
}
diff --git a/src/uart-test/stm32f4xx_it.c b/src/uart-test/stm32f4xx_it.c
index 4e5bd89..73e7735 100644
--- a/src/uart-test/stm32f4xx_it.c
+++ b/src/uart-test/stm32f4xx_it.c
@@ -53,10 +53,6 @@
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
-
-/* UART handler declared in "stm_init.h" file */
-//extern UART_HandleTypeDef huart2;
-
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
@@ -170,27 +166,6 @@ void SysTick_Handler(void)
/******************************************************************************/
/**
- * @brief This function handles UART interrupt request.
- * @param None
- * @retval None
- * @Note This function is redefined in "main.h" and related to DMA stream
- * used for USART data transmission
- */
-void USART1_IRQHandler(void)
-{
- HAL_UART_IRQHandler(&huart1);
-}
-
-/**
- * @brief This function handles PPP interrupt request.
- * @param None
- * @retval None
- */
-/*void PPP_IRQHandler(void)
-{
-}*/
-
-/**
* @}
*/
diff --git a/src/uart-test/stm_init.c b/src/uart-test/stm_init.c
index ad39080..384c4f8 100644
--- a/src/uart-test/stm_init.c
+++ b/src/uart-test/stm_init.c
@@ -40,11 +40,13 @@
#include "stm_init.h"
UART_HandleTypeDef huart1;
+UART_HandleTypeDef huart2;
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define UART1_BAUD_RATE 460800
+#define UART2_BAUD_RATE 115200
/* Private macro -------------------------------------------------------------*/
@@ -57,6 +59,7 @@ static void SystemClock_Config(void);
static void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
+static void MX_USART2_UART_Init(void);
/* Private functions ---------------------------------------------------------*/
/**
@@ -89,6 +92,7 @@ void stm_init(void)
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
+ MX_USART2_UART_Init();
}
@@ -138,8 +142,7 @@ static void SystemClock_Config(void)
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV8;
RCC_OscInitStruct.PLL.PLLQ = 7;
- if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
+ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
@@ -149,8 +152,7 @@ static void SystemClock_Config(void)
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; /* AHB prescaler */
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; /* APB1 prescaler /1 gives 42 MHz APB1 */
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; /* APB2 prescaler /1 gives 42 MHz APB2 */
- if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
+ if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
Error_Handler();
}
@@ -179,11 +181,11 @@ void MX_GPIO_Init(void)
__GPIOC_CLK_ENABLE();
/*Configure LED GPIO pins PB12==red, PB13==yellow, PB14==green, PB15==blue */
- GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
+ GPIO_InitStruct.Pin = LED_RED | LED_YELLOW | LED_GREEN | LED_BLUE;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+ HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
/*Configure GPIO pin : PC9 (RCC_MCO_2) */
GPIO_InitStruct.Pin = GPIO_PIN_9;
@@ -209,22 +211,42 @@ void MX_GPIO_Init(void)
void MX_USART1_UART_Init(void)
{
- huart1.Instance = USART1;
- huart1.Init.BaudRate = UART1_BAUD_RATE;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+ huart1.Instance = USART1;
+ huart1.Init.BaudRate = UART1_BAUD_RATE;
+ huart1.Init.WordLength = UART_WORDLENGTH_8B;
+ huart1.Init.StopBits = UART_STOPBITS_1;
+ huart1.Init.Parity = UART_PARITY_NONE;
+ huart1.Init.Mode = UART_MODE_TX_RX;
+ huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- if(HAL_UART_Init(&huart1) != HAL_OK) {
+ if (HAL_UART_Init(&huart1) != HAL_OK) {
/* Initialization Error */
Error_Handler();
}
}
+/* USART2 init function */
+void MX_USART2_UART_Init(void)
+{
+
+ huart2.Instance = USART2;
+ huart2.Init.BaudRate = UART2_BAUD_RATE;
+ 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) {
+ /* Initialization Error */
+ Error_Handler();
+ }
+}
+
+
/**
* @brief This function is executed in case of error occurrence.
* @param None
diff --git a/src/uart-test/stm_init.h b/src/uart-test/stm_init.h
index e1cdfb8..838ca77 100644
--- a/src/uart-test/stm_init.h
+++ b/src/uart-test/stm_init.h
@@ -10,6 +10,7 @@
#define LED_BLUE GPIO_PIN_15
extern UART_HandleTypeDef huart1;
+extern UART_HandleTypeDef huart2;
extern void stm_init(void);