Blame view

RIOT/cpu/stm32f3/periph/uart.c 5.33 KB
fb11e647   vrobic   reseau statique a...
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  /*
   * Copyright (C) 2014 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser General
   * Public License v2.1. See the file LICENSE in the top level directory for more
   * details.
   */
  
  /**
   * @ingroup     cpu_stm32f3
   * @{
   *
   * @file
   * @brief       Low-level UART driver implementation
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include "cpu.h"
  #include "periph/uart.h"
  
  /**
   * @brief Allocate memory to store the callback functions.
   */
  static uart_isr_ctx_t uart_config[UART_NUMOF];
  
  static int init_base(uart_t uart, uint32_t baudrate);
  
  int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
  {
      /* do basic initialization */
      int res = init_base(uart, baudrate);
      if (res != UART_OK) {
          return res;
      }
  
      /* remember callback addresses */
      uart_config[uart].rx_cb = rx_cb;
      uart_config[uart].arg = arg;
  
      /* enable receive interrupt */
      switch (uart) {
  #if UART_0_EN
          case UART_0:
              NVIC_EnableIRQ(UART_0_IRQ_CHAN);
              UART_0_DEV->CR1 |= USART_CR1_RXNEIE;
              break;
  #endif
  #if UART_1_EN
          case UART_1:
              NVIC_EnableIRQ(UART_1_IRQ_CHAN);
              UART_1_DEV->CR1 |= USART_CR1_RXNEIE;
              break;
  #endif
  #if UART_2_EN
          case UART_2:
              NVIC_EnableIRQ(UART_2_IRQ_CHAN);
              UART_2_DEV->CR1 |= USART_CR1_RXNEIE;
              break;
  #endif
      }
  
      return UART_OK;
  }
  
  static int init_base(uart_t uart, uint32_t baudrate)
  {
      USART_TypeDef *dev = 0;
      GPIO_TypeDef *port = 0;
      uint32_t tx_pin = 0;
      uint32_t rx_pin = 0;
      uint8_t af = 0;
      uint32_t clk = 0;
      uint16_t mantissa;
      uint8_t fraction;
  
      switch (uart) {
  #if UART_0_EN
          case UART_0:
              dev = UART_0_DEV;
              port = UART_0_PORT;
              clk = UART_0_CLK;
              tx_pin = UART_0_TX_PIN;
              rx_pin = UART_0_RX_PIN;
              af = UART_0_AF;
              UART_0_CLKEN();
              UART_0_PORT_CLKEN();
              break;
  #endif
  #if UART_1_EN
          case UART_1:
              dev = UART_1_DEV;
              port = UART_1_PORT;
              clk = UART_1_CLK;
              tx_pin = UART_1_TX_PIN;
              rx_pin = UART_1_RX_PIN;
              af = UART_1_AF;
              UART_1_CLKEN();
              UART_1_PORT_CLKEN();
              break;
  #endif
  #if UART_2_EN
          case UART_2:
              dev = UART_2_DEV;
              port = UART_2_PORT;
              clk = UART_2_CLK;
              tx_pin = UART_2_TX_PIN;
              rx_pin = UART_2_RX_PIN;
              af = UART_2_AF;
              UART_2_CLKEN();
              UART_2_PORT_CLKEN();
              break;
  #endif
          default:
              return UART_NODEV;
      }
  
      /* Make sure port and dev are != NULL here, i.e. that the variables are
       * assigned in all non-returning branches of the switch at the top of this
       * function. */
      assert(port != NULL);
      assert(dev != NULL);
  
      /* uart_configure RX and TX pins, set pin to use alternative function mode */
      port->MODER &= ~(3 << (rx_pin * 2) | 3 << (tx_pin * 2));
      port->MODER |= 2 << (rx_pin * 2) | 2 << (tx_pin * 2);
      /* and assign alternative function */
      if (rx_pin < 8) {
          port->AFR[0] &= ~(0xf << (rx_pin * 4));
          port->AFR[0] |= af << (rx_pin * 4);
      }
      else {
          port->AFR[1] &= ~(0xf << ((rx_pin - 8) * 4));
          port->AFR[1] |= af << ((rx_pin - 8) * 4);
      }
      if (tx_pin < 8) {
          port->AFR[0] &= ~(0xf << (tx_pin * 4));
          port->AFR[0] |= af << (tx_pin * 4);
      }
      else {
          port->AFR[1] &= ~(0xf << ((tx_pin - 8) * 4));
          port->AFR[1] |= af << ((tx_pin - 8) * 4);
      }
  
      /* uart_configure UART to mode 8N1 with given baudrate */
      clk /= baudrate;
      mantissa = (uint16_t)(clk / 16);
      fraction = (uint8_t)(clk - (mantissa * 16));
      dev->BRR = ((mantissa & 0x0fff) << 4) | (0x0f & fraction);
  
      /* enable receive and transmit mode */
      dev->CR3 = 0;
      dev->CR2 = 0;
      dev->CR1 |= USART_CR1_UE | USART_CR1_TE | USART_CR1_RE;
  
      return UART_OK;
  }
  
  void uart_write(uart_t uart, const uint8_t *data, size_t len)
  {
      USART_TypeDef *dev = 0;
  
      switch (uart) {
  #if UART_0_EN
          case UART_0:
              dev = UART_0_DEV;
              break;
  #endif
  #if UART_1_EN
          case UART_1:
              dev = UART_1_DEV;
              break;
  #endif
  #if UART_2_EN
          case UART_2:
              dev = UART_2_DEV;
              break;
  #endif
          default:
              return;
      }
  
      /* Make sure dev is != NULL here, i.e. that the variable is assigned in
       * all non-returning branches of the switch at the top of this function. */
      assert(dev != NULL);
  
      for (size_t i = 0; i < len; i++) {
          while (!(dev->ISR & USART_ISR_TXE)) {}
          dev->TDR = data[i];
      }
  }
  
  static inline void irq_handler(uint8_t uartnum, USART_TypeDef *dev)
  {
      if (dev->ISR & USART_ISR_RXNE) {
          uint8_t data = (uint8_t)dev->RDR;
          uart_config[uartnum].rx_cb(uart_config[uartnum].arg, data);
      }
      else if (dev->ISR & USART_ISR_ORE) {
          /* do nothing on overrun */
          dev->ICR |= USART_ICR_ORECF;
      }
      cortexm_isr_end();
  }
  
  #if UART_0_EN
  void UART_0_ISR(void)
  {
      irq_handler(UART_0, UART_0_DEV);
  }
  #endif
  
  #if UART_1_EN
  void UART_1_ISR(void)
  {
      irq_handler(UART_1, UART_1_DEV);
  }
  #endif
  
  #if UART_2_EN
  void UART_2_ISR(void)
  {
      irq_handler(UART_2, UART_2_DEV);
  }
  #endif