summaryrefslogtreecommitdiffstats
path: root/boot/usart.c
blob: c602710bd1dd2e83d68f43075c8dfc5011bfecde (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "project.h"


#define TX2   GPIO5
#define TX2_PORT  GPIOD

#define RX2   GPIO6
#define RX2_PORT  GPIOD


void
usart2_xmit_chr (char d)
{
  usart_send_blocking (USART2, d);
}


void
usart2_xmit_str (const char *s)
{
  while (*s)
    usart_send_blocking (USART2, * (s++));
}


void usart2_xmit_nl (void)
{
  usart2_xmit_str ("\r\n");
}

void usart2_xmit_xdigit (unsigned d)
{
  if (d < 0xa) usart2_xmit_chr ('0' + d);
  else usart2_xmit_chr ('a' + (d - 0xa));
}

void usart2_xmit_uint32 (uint32_t v)
{
  unsigned i;

  for (i = 0; i < 8; ++i) {
    usart2_xmit_xdigit (v >> 28);
    v <<= 4;
  }
}


void
usart_init (void)
{
  rcc_periph_clock_enable (RCC_GPIOD);
  rcc_periph_clock_enable (RCC_USART2);


  MAP_INPUT (RX2);
  MAP_AF (TX2, GPIO_AF7);
  MAP_AF_PU (RX2, GPIO_AF7);


  usart_set_baudrate (USART2, 38400);
  usart_set_databits (USART2, 8);
  usart_set_stopbits (USART2, USART_STOPBITS_1);
  usart_set_parity (USART2, USART_PARITY_NONE);
  usart_set_flow_control (USART2, USART_FLOWCONTROL_NONE);
  usart_set_mode (USART2, USART_MODE_TX_RX);

  usart_enable (USART2);
}