Blame view

RIOT/cpu/stm32f1/periph/spi.c 6.6 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  /*
   * 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_stm32f1
   * @{
   *
   * @file
   * @brief       Low-level SPI driver implementation
   *
   * @author      Thomas Eichinger <thomas.eichinger@fu-berlin.de>
   * @author      Fabian Nack <nack@inf.fu-berlin.de>
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   *
   * @}
   */
  
  #include "cpu.h"
  #include "mutex.h"
  #include "periph/gpio.h"
  #include "periph/spi.h"
  #include "periph_conf.h"
  #include "board.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  /* guard file in case no SPI device is defined */
  #if SPI_0_EN || SPI_1_EN || SPI_2_EN
  
  /**
   * @brief Array holding one pre-initialized mutex for each SPI device
   */
  static mutex_t locks[] =  {
  #if SPI_0_EN
      [SPI_0] = MUTEX_INIT,
  #endif
  #if SPI_1_EN
      [SPI_1] = MUTEX_INIT,
  #endif
  #if SPI_2_EN
      [SPI_2] = MUTEX_INIT
  #endif
  };
  
  int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed)
  {
      SPI_TypeDef *spi;
      uint16_t br_div;
      uint8_t bus_div;
  
      switch (dev) {
  #if SPI_0_EN
          case SPI_0:
              spi = SPI_0_DEV;
              bus_div = SPI_0_BUS_DIV;
              SPI_0_CLKEN();
              break;
  #endif
  
  #if SPI_1_EN
          case SPI_1:
              spi = SPI_1_DEV;
              bus_div = SPI_1_BUS_DIV;
              SPI_1_CLKEN();
              break;
  #endif
  
  #if SPI_2_EN
          case SPI_2:
              spi = SPI_2_DEV;
              bus_div = SPI_2_BUS_DIV;
              SPI_2_CLKEN();
              break;
  #endif
          default:
              return -1;
      }
  
      /* configure SCK, MISO and MOSI pin */
      spi_conf_pins(dev);
  
      /* configure SPI bus speed */
      switch (speed) {
          case SPI_SPEED_10MHZ:
              br_div = 0x01 + bus_div;      /* actual speed: 9MHz   */
              break;
          case SPI_SPEED_5MHZ:
              br_div = 0x02 + bus_div;     /* actual speed: 4.5MHz */
              break;
          case SPI_SPEED_1MHZ:
              br_div = 0x04 + bus_div;     /* actual speed: 1.1MHz */
              break;
          case SPI_SPEED_400KHZ:
              br_div = 0x05 + bus_div;    /* actual speed: 560kHz */
              break;
          case SPI_SPEED_100KHZ:
              br_div = 0x07;              /* actual speed: 280kHz on APB2, 140KHz on APB1 */
              break;
          default:
              return -2;
      }
  
      /* set up SPI */
      spi->CR1 = SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_MSTR | (conf & 0x3) | (br_div << 3);
      spi->I2SCFGR &= 0xF7FF;     /* select SPI mode */
      spi->CRCPR = 0x7;           /* reset CRC polynomial */
      /* enable the SPI device */
      spi->CR1 |= SPI_CR1_SPE;
  
      return 0;
  }
  
  int spi_init_slave(spi_t dev, spi_conf_t conf, char (*cb)(char))
  {
      (void) dev;
      (void) conf;
      (void) cb;
      /* TODO */
      return -1;
  }
  
  int spi_conf_pins(spi_t dev)
  {
      gpio_t mosi, miso, clk;
  
      switch (dev) {
  #if SPI_0_EN
          case SPI_0:
              clk = SPI_0_CLK_PIN;
              mosi = SPI_0_MOSI_PIN;
              miso = SPI_0_MISO_PIN;
              break;
  #endif
  
  #if SPI_1_EN
          case SPI_1:
              clk = SPI_1_CLK_PIN;
              mosi = SPI_1_MOSI_PIN;
              miso = SPI_1_MISO_PIN;
              break;
  #endif
  
  #if SPI_2_EN
          case SPI_2:
              clk = SPI_2_CLK_PIN;
              mosi = SPI_2_MOSI_PIN;
              miso = SPI_2_MISO_PIN;
              break;
  #endif
          default:
              return -1;
      }
  
      /* configure pins for alternate function input (MISO) or output (MOSI, CLK) */
      gpio_init_af(clk, GPIO_AF_OUT_PP);
      gpio_init_af(mosi, GPIO_AF_OUT_PP);
      gpio_init(miso, GPIO_IN);
      return 0;
  }
  
  int spi_acquire(spi_t dev)
  {
      if ((unsigned int)dev >= SPI_NUMOF) {
          return -1;
      }
      mutex_lock(&locks[dev]);
      return 0;
  }
  
  int spi_release(spi_t dev)
  {
      if ((unsigned int)dev >= SPI_NUMOF) {
          return -1;
      }
      mutex_unlock(&locks[dev]);
      return 0;
  }
  
  int spi_transfer_bytes(spi_t dev, char *out, char *in, unsigned int length)
  {
  
      SPI_TypeDef *spi;
  
      switch (dev) {
  #if SPI_0_EN
          case SPI_0:
              spi = SPI_0_DEV;
              break;
  #endif
  
  #if SPI_1_EN
          case SPI_1:
              spi = SPI_1_DEV;
              break;
  #endif
  
  #if SPI_2_EN
          case SPI_2:
              spi = SPI_2_DEV;
              break;
  #endif
          default:
              return -1;
      }
  
      if(!in){
          for (unsigned i = 0; i < length; i++) {
              while (!(spi->SR & SPI_SR_TXE)) {}
              spi->DR = (uint8_t)out[i];
          }
          /* SPI busy */
          while ((spi->SR & SPI_SR_BSY)) {}
          spi->DR;
      }
      else if(!out) {
          for (unsigned i = 0; i < length; i++) {
              spi->DR = 0;
              while (!(spi->SR & SPI_SR_RXNE)) {}
              in[i] = (char)spi->DR;
          }
      }
      else {
          for (unsigned i = 0; i < length; i++) {
              while (!(spi->SR & SPI_SR_TXE)) {}
              spi->DR = out[i];
              while (!(spi->SR & SPI_SR_RXNE)) {}
              in[i] = (char)spi->DR;
          }
      }
  
  #if ENABLE_DEBUG
      if (in != NULL) {
          DEBUG("\nSPI: transferred %i Bytes\n", length);
      }
  #endif /*ENABLE_DEBUG */
  
      return length;
  }
  
  void spi_transmission_begin(spi_t dev, char reset_val)
  {
      (void) dev;
      (void) reset_val;
      /* slave mode not implemented, yet */
  }
  
  void spi_poweron(spi_t dev)
  {
      switch (dev) {
  #if SPI_0_EN
          case SPI_0:
              SPI_0_CLKEN();
              SPI_0_DEV->CR1 |= SPI_CR1_SPE;   /* turn SPI peripheral on */
              break;
  #endif
  
  #if SPI_1_EN
          case SPI_1:
              SPI_1_CLKEN();
              SPI_1_DEV->CR1 |= SPI_CR1_SPE;   /* turn SPI peripheral on */
              break;
  #endif
  
  #if SPI_2_EN
          case SPI_2:
              SPI_2_CLKEN();
              SPI_2_DEV->CR1 |= SPI_CR1_SPE;   /* turn SPI peripheral on */
              break;
  #endif
      }
  }
  
  void spi_poweroff(spi_t dev)
  {
      switch (dev) {
  #if SPI_0_EN
          case SPI_0:
              while ((SPI_0_DEV->SR & SPI_SR_BSY)) {}
              SPI_0_DEV->CR1 &= ~(SPI_CR1_SPE);   /* turn SPI peripheral off */
              SPI_0_CLKDIS();
              break;
  #endif
  
  #if SPI_1_EN
          case SPI_1:
              while ((SPI_1_DEV->SR & SPI_SR_BSY)) {}
              SPI_1_DEV->CR1 &= ~(SPI_CR1_SPE);   /* turn SPI peripheral off */
              SPI_1_CLKDIS();
              break;
  #endif
  
  #if SPI_2_EN
          case SPI_2:
              while ((SPI_2_DEV->SR & SPI_SR_BSY)) {}
              SPI_2_DEV->CR1 &= ~(SPI_CR1_SPE);   /* turn SPI peripheral off */
              SPI_2_CLKDIS();
              break;
  #endif
      }
  }
  
  #endif /* SPI_0_EN */