aboutsummaryrefslogtreecommitdiff
path: root/src/uart-test/main.c
blob: 55ec6fb3a0f5c1dac11bb32aeb8ae05a7ec81159 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * Test code that just sends the letters 'a' to 'z' over and
 * over again using USART1.
 *
 * Toggles the BLUE LED slowly and the YELLOW LED for every
 * character sent.
 */
#include "stm_init.h"

#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';

  stm_init();

  huart = &huart1;

  while (1)
  {
    HAL_GPIO_TogglePin(LED_PORT, LED_YELLOW);

    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);
  }
}