summaryrefslogtreecommitdiffstats
path: root/app/usart.c
blob: e826a22c8f3115742b408cfaf0c4dd91556fe67c (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "project.h"

#define BUFFER_SIZE 256
#define BIG_BUFFER_SIZE 600


volatile ring_t rx3_ring;
static uint8_t rx3_ring_buf[BUFFER_SIZE];

volatile ring_t tx3_ring;
static uint8_t tx3_ring_buf[BUFFER_SIZE];

volatile ring_t rx1_ring;
static uint8_t rx1_ring_buf[BUFFER_SIZE];

volatile ring_t tx1_ring;
static uint8_t tx1_ring_buf[BUFFER_SIZE];


#define TX1   GPIO9
#define TX1_PORT  GPIOA

#define RX1   GPIO10
#define RX1_PORT  GPIOA

#define TX3   GPIO10
#define TX3_PORT  GPIOC

#define RX3   GPIO11
#define RX3_PORT  GPIOC


void usart3_isr (void)
{
  uint8_t data;

  /* Check if we were called because of RXNE. */
  if (((USART_CR1 (USART3) & USART_CR1_RXNEIE) != 0) &&
      ((USART_SR (USART3) & USART_SR_RXNE) != 0)) {

    /* Retrieve the data from the peripheral. */
    data = usart_recv (USART3);

    ring_write_byte (&rx3_ring, data);
    //usart6_queue(data);
  }

  /* Check if we were called because of TXE. */
  if (((USART_CR1 (USART3) & USART_CR1_TXEIE) != 0) &&
      ((USART_SR (USART3) & USART_SR_TXE) != 0)) {

    if (ring_read_byte (&tx3_ring, &data)) {
      /*No more data, Disable the TXE interrupt, it's no longer needed. */
      usart_disable_tx_interrupt (USART3);
    } else
      usart_send_blocking (USART3, data);
  }

}

void
usart3_queue (uint8_t d)
{
  ring_write_byte (&tx3_ring, d);
  usart_enable_tx_interrupt (USART3);
}

void
usart3_drain (void)
{
  while (!ring_empty (&tx3_ring));
}


int
usart3_write (char *ptr, int len, int blocking)
{
  int ret;

  ret = ring_write (&tx3_ring, (uint8_t *) ptr, len, blocking);
  usart_enable_tx_interrupt (USART3);
  return ret;
}



void usart1_isr (void)
{
  uint8_t data;

  /* Check if we were called because of RXNE. */
  if (((USART_CR1 (USART1) & USART_CR1_RXNEIE) != 0) &&
      ((USART_SR (USART1) & USART_SR_RXNE) != 0)) {

    /* Retrieve the data from the peripheral. */
    data = usart_recv (USART1);

    ring_write_byte (&rx1_ring, data);
  }

  /* Check if we were called because of TXE. */
  if (((USART_CR1 (USART1) & USART_CR1_TXEIE) != 0) &&
      ((USART_SR (USART1) & USART_SR_TXE) != 0)) {

    if (ring_read_byte (&tx1_ring, &data)) {
      /*No more data, Disable the TXE interrupt, it's no longer needed. */
      usart_disable_tx_interrupt (USART1);
    } else
      usart_send_blocking (USART1, data);
  }

}

void
usart1_queue (uint8_t d)
{
  ring_write_byte (&tx1_ring, d);
  usart_enable_tx_interrupt (USART1);
}

void
usart1_drain (void)
{
  while (!ring_empty (&tx1_ring));
}


int
usart1_write (char *ptr, int len, int blocking)
{
  int ret;

  ret = ring_write (&tx1_ring, (uint8_t *) ptr, len, blocking);
  usart_enable_tx_interrupt (USART1);
  return ret;
}


void
usart_init (void)
{

  ring_init (&rx3_ring, rx3_ring_buf, sizeof (rx3_ring_buf));
  ring_init (&tx3_ring, tx3_ring_buf, sizeof (tx3_ring_buf));

  MAP_AF (TX3, GPIO_AF7);
  MAP_AF_PU (RX3, GPIO_AF7);

  usart_set_baudrate (USART3, 9600);
  usart_set_databits (USART3, 8);
  usart_set_stopbits (USART3, USART_STOPBITS_1);
  usart_set_parity (USART3, USART_PARITY_NONE);
  usart_set_flow_control (USART3, USART_FLOWCONTROL_NONE);
  usart_set_mode (USART3, USART_MODE_TX_RX);

  usart_enable_rx_interrupt (USART3);

  usart_enable (USART3);

  nvic_enable_irq (NVIC_USART3_IRQ);


  ring_init (&rx1_ring, rx1_ring_buf, sizeof (rx1_ring_buf));
  ring_init (&tx1_ring, tx1_ring_buf, sizeof (tx1_ring_buf));

  MAP_AF (TX1, GPIO_AF7);
  MAP_AF_PU (RX1, GPIO_AF7);

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

  usart_enable_rx_interrupt (USART1);

  usart_enable (USART1);

  nvic_enable_irq (NVIC_USART1_IRQ);
}