Blame view

RIOT/cpu/nrf52/periph/spi.c 5.78 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
  /*
   * Copyright (C) 2015 Jan Wagner <mail@jwagner.eu>
   *               2016 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_nrf52
   * @{
   *
   * @file
   * @brief       Implementation of the peripheral SPI interface
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @author      Frank Holtz <frank-riot2015@holtznet.de>
   * @author      Jan Wagner <mail@jwagner.eu>
   *
   * @}
   */
  
  #include "cpu.h"
  #include "mutex.h"
  #include "periph/spi.h"
  #include "periph_conf.h"
  
  /* guard this file in case no SPI device is defined */
  #if SPI_NUMOF
  
  /* static port mapping */
  static NRF_SPI_Type *const spi[] = {
  #if SPI_0_EN
      SPI_0_DEV,
  #endif
  #if SPI_1_EN
      SPI_1_DEV
  #endif
  };
  
  /**
   * @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
  };
  
  int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed)
  {
      if (dev >= SPI_NUMOF) {
          return -1;
      }
  
      spi_poweron(dev);
  
      /* disable the device -> nRF51822 reference 3.0 26.1.1 and 27.1*/
      spi[dev]->ENABLE = 0;
  
      switch (dev) {
  #if SPI_0_EN
  
          case SPI_0:
              /* disable TWI Interface */
              NRF_TWI0->ENABLE = 0;
              break;
  #endif
  #if SPI_1_EN
  
          case SPI_1:
              /* disable SPI Slave */
              NRF_SPIS1->ENABLE = 0;
              /* disable TWI Interface */
              NRF_TWI1->ENABLE = 0;
              break;
  #endif
  
          default:
              return -1;
      }
  
      /* configure direction of used pins */
      spi_conf_pins(dev);
  
      /* configure SPI mode */
      switch (conf) {
          case SPI_CONF_FIRST_RISING:
              spi[dev]->CONFIG = (SPI_CONFIG_CPOL_ActiveHigh << 2) | (SPI_CONFIG_CPHA_Leading << 1);
              break;
  
          case SPI_CONF_SECOND_RISING:
              spi[dev]->CONFIG = (SPI_CONFIG_CPOL_ActiveHigh << 2) | (SPI_CONFIG_CPHA_Trailing << 1);
              break;
  
          case SPI_CONF_FIRST_FALLING:
              spi[dev]->CONFIG = (SPI_CONFIG_CPOL_ActiveLow << 2) | (SPI_CONFIG_CPHA_Leading << 1);
              break;
  
          case SPI_CONF_SECOND_FALLING:
              spi[dev]->CONFIG = (SPI_CONFIG_CPOL_ActiveLow << 2) | (SPI_CONFIG_CPHA_Trailing << 1);
              break;
      }
  
      /* select bus speed */
      switch (speed) {
          case SPI_SPEED_100KHZ:          /* 125 KHz for this device */
              spi[dev]->FREQUENCY = SPI_FREQUENCY_FREQUENCY_K125;
              break;
  
          case SPI_SPEED_400KHZ:          /* 500 KHz for this device */
              spi[dev]->FREQUENCY = SPI_FREQUENCY_FREQUENCY_K500;
              break;
  
          case SPI_SPEED_1MHZ:            /* 1 MHz for this device */
              spi[dev]->FREQUENCY = SPI_FREQUENCY_FREQUENCY_M1;
              break;
  
          case SPI_SPEED_5MHZ:            /* 4 MHz for this device */
              spi[dev]->FREQUENCY = SPI_FREQUENCY_FREQUENCY_M4;
              break;
  
          case SPI_SPEED_10MHZ:           /* 8 MHz for this device */
              spi[dev]->FREQUENCY = SPI_FREQUENCY_FREQUENCY_M8;
              break;
      }
  
      /* finally enable the device */
      spi[dev]->ENABLE = 1;
      return 0;
  }
  
  int spi_init_slave(spi_t dev, spi_conf_t conf, char (*cb)(char data))
  {
      /* This API is incompatible with nRF51 SPIS */
      return -1;
  }
  
  int spi_conf_pins(spi_t dev)
  {
      if (dev >= SPI_NUMOF) {
          return -1;
      }
  
      switch (dev) {
  #if SPI_0_EN
  
          case SPI_0:
              /* set pin direction */
              NRF_P0->DIRSET = (1 << SPI_0_PIN_MOSI) | (1 << SPI_0_PIN_SCK);
              NRF_P0->DIRCLR = (1 << SPI_0_PIN_MISO);
              /* select pins to be used by SPI */
              spi[dev]->PSELMOSI = SPI_0_PIN_MOSI;
              spi[dev]->PSELMISO = SPI_0_PIN_MISO;
              spi[dev]->PSELSCK = SPI_0_PIN_SCK;
              break;
  #endif
  #if SPI_1_EN
  
          case SPI_1:
              /* set pin direction */
              NRF_P0->DIRSET = (1 << SPI_1_PIN_MOSI) | (1 << SPI_1_PIN_SCK);
              NRF_P0->DIRCLR = (1 << SPI_1_PIN_MISO);
              /* select pins to be used by SPI */
              spi[dev]->PSELMOSI = SPI_1_PIN_MOSI;
              spi[dev]->PSELMISO = SPI_1_PIN_MISO;
              spi[dev]->PSELSCK = SPI_1_PIN_SCK;
              break;
  #endif
      }
  
      return 0;
  }
  
  int spi_acquire(spi_t dev)
  {
      if (dev >= SPI_NUMOF) {
          return -1;
      }
  
      mutex_lock(&locks[dev]);
      return 0;
  }
  
  int spi_release(spi_t dev)
  {
      if (dev >= SPI_NUMOF) {
          return -1;
      }
  
      mutex_unlock(&locks[dev]);
      return 0;
  }
  
  int spi_transfer_byte(spi_t dev, char out, char *in)
  {
      return spi_transfer_bytes(dev, &out, in, 1);
  }
  
  int spi_transfer_bytes(spi_t dev, char *out, char *in, unsigned int length)
  {
      if (dev >= SPI_NUMOF) {
          return -1;
      }
  
      for (int i = 0; i < length; i++) {
          char tmp = (out) ? out[i] : 0;
          spi[dev]->EVENTS_READY = 0;
          spi[dev]->TXD = (uint8_t)tmp;
  
          while (spi[dev]->EVENTS_READY != 1);
  
          tmp = (char)spi[dev]->RXD;
  
          if (in) {
              in[i] = tmp;
          }
      }
  
      return length;
  }
  
  int spi_transfer_reg(spi_t dev, uint8_t reg, char out, char *in)
  {
      spi_transfer_byte(dev, reg, 0);
      return spi_transfer_byte(dev, out, in);
  }
  
  int spi_transfer_regs(spi_t dev, uint8_t reg, char *out, char *in, unsigned int length)
  {
      spi_transfer_byte(dev, reg, 0);
      return spi_transfer_bytes(dev, out, in, length);
  }
  
  void spi_transmission_begin(spi_t dev, char reset_val)
  {
      /* spi slave is not implemented */
  }
  
  void spi_poweron(spi_t dev)
  {
      if (dev < SPI_NUMOF) {
          spi[dev]->POWER = 1;
      }
  }
  
  void spi_poweroff(spi_t dev)
  {
      if (dev < SPI_NUMOF) {
          spi[dev]->POWER = 0;
      }
  }
  
  #endif /* SPI_NUMOF */