Blame view

RIOT/cpu/msp430fxyz/periph/spi.c 6.54 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
  /*
   * Copyright (C) 2015 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_msp430fxyz
   * @{
   *
   * @file
   * @brief       Low-level SPI driver implementation
   *
   * This SPI driver implementation does only support one single SPI device for
   * now. This is sufficient, as most MSP430 CPU's only support two serial
   * devices - one used as UART and one as SPI.
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include "cpu.h"
  #include "mutex.h"
  #include "assert.h"
  #include "periph_cpu.h"
  #include "periph_conf.h"
  #include "periph/spi.h"
  
  /**
   * @brief   Mutex for locking the SPI device
   */
  static mutex_t spi_lock = MUTEX_INIT;
  
  /* per default, we use the legacy MSP430 USART module for UART functionality */
  #ifndef SPI_USE_USCI
  
  int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed)
  {
      if (dev != 0) {
          return -2;
      }
  
      /* reset SPI device */
      SPI_DEV->CTL = USART_CTL_SWRST;
      /* configure pins */
      spi_conf_pins(dev);
      /* configure USART to SPI mode with SMCLK driving it */
      SPI_DEV->CTL |= (USART_CTL_CHAR | USART_CTL_SYNC | USART_CTL_MM);
      SPI_DEV->RCTL = 0;
      SPI_DEV->TCTL = (USART_TCTL_SSEL_SMCLK | USART_TCTL_STC);
      /* set polarity and phase */
      switch (conf) {
          case SPI_CONF_FIRST_RISING:
              SPI_DEV->TCTL |= USART_TCTL_CKPH;
              break;
          case SPI_CONF_SECOND_RISING:
              /* nothing to be done here */
              break;
          case SPI_CONF_FIRST_FALLING:
              SPI_DEV->TCTL |= (USART_TCTL_CKPH & USART_TCTL_CKPL);
              break;
          case SPI_CONF_SECOND_FALLING:
              SPI_DEV->TCTL |= USART_TCTL_CKPL;
              break;
          default:
              /* invalid clock setting */
              return -2;
      }
      /* configure clock - we use no modulation for now */
      uint32_t br = CLOCK_CMCLK;
      switch (speed) {
          case SPI_SPEED_100KHZ:
              br /= 100000;
              break;
          case SPI_SPEED_400KHZ:
              br /= 400000;
              break;
          case SPI_SPEED_1MHZ:
              br /= 1000000;
              break;
          case SPI_SPEED_5MHZ:
              br /= 5000000;
              break;
          default:
              /* other clock speeds are not supported */
              return -1;
      }
  
      /* make sure the is not smaller then 2 */
      if (br < 2) {
          br = 2;
      }
  
      SPI_DEV->BR0 = (uint8_t)br;
      SPI_DEV->BR1 = (uint8_t)(br >> 8);
      SPI_DEV->MCTL = 0;
      /* enable SPI mode */
      SPI_ME |= SPI_ME_BIT;
      /* release from software reset */
      SPI_DEV->CTL &= ~(USART_CTL_SWRST);
      return 0;
  }
  
  /* we use alternative SPI code in case the board used the USCI module for SPI
   * instead of the (older) USART module */
  #else   /* SPI_USE_USCI */
  
  int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed)
  {
      if (dev != 0) {
          return -2;
      }
  
      /* reset SPI device */
      SPI_DEV->CTL1 |= USCI_SPI_CTL1_SWRST;
      /* configure pins */
      spi_conf_pins(dev);
      /* configure USART to SPI mode with SMCLK driving it */
      SPI_DEV->CTL0 |= (USCI_SPI_CTL0_UCSYNC | USCI_SPI_CTL0_MST
                        | USCI_SPI_CTL0_MODE_0 | USCI_SPI_CTL0_MSB);
      SPI_DEV->CTL1 |= (USCI_SPI_CTL1_SSEL_SMCLK);
  
      /* set polarity and phase */
      switch (conf) {
          case SPI_CONF_FIRST_RISING:
              SPI_DEV->CTL0 |= USCI_SPI_CTL0_CKPH;
              break;
          case SPI_CONF_SECOND_RISING:
              /* nothing to be done here */
              break;
          case SPI_CONF_FIRST_FALLING:
              SPI_DEV->CTL0 |= (USCI_SPI_CTL0_CKPH & USCI_SPI_CTL0_CKPL);
              break;
          case SPI_CONF_SECOND_FALLING:
              SPI_DEV->CTL0 |= USCI_SPI_CTL0_CKPL;
              break;
          default:
              /* invalid clock setting */
              return -2;
      }
      /* configure clock - we use no modulation for now */
      uint32_t br = CLOCK_CMCLK;
      switch (speed) {
          case SPI_SPEED_100KHZ:
              br /= 100000;
              break;
          case SPI_SPEED_400KHZ:
              br /= 400000;
              break;
          case SPI_SPEED_1MHZ:
              br /= 1000000;
              break;
          case SPI_SPEED_5MHZ:
              br /= 5000000;
              break;
          default:
              /* other clock speeds are not supported */
              return -1;
      }
  
      /* make sure the is not smaller then 2 */
      if (br < 2) {
          br = 2;
      }
  
      SPI_DEV->BR0 = (uint8_t)br;
      SPI_DEV->BR1 = (uint8_t)(br >> 8);
      /* release from software reset */
      SPI_DEV->CTL1 &= ~(USCI_SPI_CTL1_SWRST);
      return 0;
  }
  
  #endif  /* SPI_USE_USCI */
  
  int spi_init_slave(spi_t dev, spi_conf_t conf, char (*cb)(char data))
  {
      /* not supported so far */
      (void)dev;
      (void)conf;
      (void)cb;
      return -1;
  }
  
  void spi_transmission_begin(spi_t dev, char reset_val)
  {
      /* not supported so far */
      (void)dev;
      (void)reset_val;
  }
  
  int spi_conf_pins(spi_t dev)
  {
      (void)dev;
      gpio_periph_mode(SPI_PIN_MISO, true);
      gpio_periph_mode(SPI_PIN_MOSI, true);
      gpio_periph_mode(SPI_PIN_CLK, true);
      return 0;
  }
  
  int spi_acquire(spi_t dev)
  {
      (void)dev;
      mutex_lock(&spi_lock);
      return 0;
  }
  
  int spi_release(spi_t dev)
  {
      (void)dev;
      mutex_unlock(&spi_lock);
      return 0;
  }
  
  int spi_transfer_bytes(spi_t dev, char *out, char *in, unsigned int length)
  {
      (void)dev;
  
      assert(out || in);
  
      /* if we only send out data, we do this the fast way... */
      if (!in) {
          for (unsigned i = 0; i < length; i++) {
              while (!(SPI_IF & SPI_IE_TX_BIT)) {}
              SPI_DEV->TXBUF = (uint8_t)out[i];
          }
          /* finally we need to wait, until all transfers are complete */
  #ifndef SPI_USE_USCI
          while (!(SPI_IF & SPI_IE_TX_BIT) || !(SPI_IF & SPI_IE_RX_BIT)) {}
  #else
          while (SPI_DEV->STAT & USCI_SPI_STAT_UCBUSY) {}
  #endif
          SPI_DEV->RXBUF;
      }
      else if (!out) {
          for (unsigned i = 0; i < length; i++) {
              SPI_DEV->TXBUF = 0;
              while (!(SPI_IF & SPI_IE_RX_BIT)) {}
              in[i] = (char)SPI_DEV->RXBUF;
          }
      }
      else {
          for (unsigned i = 0; i < length; i++) {
              while (!(SPI_IF & SPI_IE_TX_BIT)) {}
              SPI_DEV->TXBUF = out[i];
              while (!(SPI_IF & SPI_IE_RX_BIT)) {}
              in[i] = (char)SPI_DEV->RXBUF;
          }
      }
  
      return length;
  }
  
  void spi_poweron(spi_t dev)
  {
      /* not supported so far */
      (void)dev;
  }
  
  void spi_poweroff(spi_t dev)
  {
      /* not supported so far */
      (void)dev;
  }