Blame view

RIOT/cpu/sam0_common/periph/spi.c 5.5 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2014-2016 Freie Universität Berlin
   *               2015 Kaspar Schleiser <kaspar@schleiser.de>
   *               2015 FreshTemp, LLC.
   *
   * 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_sam0_common
   * @ingroup     drivers_periph_spi
   * @{
   *
   * @file
   * @brief       Low-level SPI driver implementation
   *
   * @author      Thomas Eichinger <thomas.eichinger@fu-berlin.de>
   * @author      Troels Hoffmeyer <troels.d.hoffmeyer@gmail.com>
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   *
   * @}
   */
  
  #include "cpu.h"
  #include "mutex.h"
  #include "assert.h"
  #include "periph/spi.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  /**
   * @brief Array holding one pre-initialized mutex for each SPI device
   */
  static mutex_t locks[SPI_NUMOF];
  
  /**
   * @brief   Shortcut for accessing the used SPI SERCOM device
   */
  static inline SercomSpi *dev(spi_t bus)
  {
      return spi_config[bus].dev;
  }
  
  static inline void poweron(spi_t bus)
  {
  #if defined(CPU_FAM_SAMD21)
      PM->APBCMASK.reg |= (PM_APBCMASK_SERCOM0 << sercom_id(dev(bus)));
  #elif defined(CPU_FAM_SAML21)
      MCLK->APBCMASK.reg |= (MCLK_APBCMASK_SERCOM0 << sercom_id(dev(bus)));
  #endif
  }
  
  static inline void poweroff(spi_t bus)
  {
  #if defined(CPU_FAM_SAMD21)
      PM->APBCMASK.reg &= ~(PM_APBCMASK_SERCOM0 << sercom_id(dev(bus)));
  #elif defined(CPU_FAM_SAML21)
      MCLK->APBCMASK.reg &= ~(MCLK_APBCMASK_SERCOM0 << sercom_id(dev(bus)));
  #endif
  }
  
  void spi_init(spi_t bus)
  {
      /* make sure given bus is good */
      assert(bus < SPI_NUMOF);
  
      /* initialize the device lock */
      mutex_init(&locks[bus]);
  
      /* configure pins and their muxes */
      spi_init_pins(bus);
  
      /* wake up device */
      poweron(bus);
  
      /* reset all device configuration */
      dev(bus)->CTRLA.reg |= SERCOM_SPI_CTRLA_SWRST;
      while ((dev(bus)->CTRLA.reg & SERCOM_SPI_CTRLA_SWRST) ||
             (dev(bus)->SYNCBUSY.reg & SERCOM_SPI_SYNCBUSY_SWRST));
  
      /* configure base clock: using GLK GEN 0 */
  #if defined(CPU_FAM_SAMD21)
      GCLK->CLKCTRL.reg = (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 |
                           (SERCOM0_GCLK_ID_CORE + sercom_id(dev(bus))));
      while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {}
  #elif defined(CPU_FAM_SAML21)
      GCLK->PCHCTRL[SERCOM0_GCLK_ID_CORE + sercom_id(dev(bus))].reg =
                                  (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0);
  #endif
  
      /* enable receiver and configure character size to 8-bit
       * no synchronization needed, as SERCOM device is not enabled */
      dev(bus)->CTRLB.reg = (SERCOM_SPI_CTRLB_CHSIZE(0) | SERCOM_SPI_CTRLB_RXEN);
  
      /* put device back to sleep */
      poweroff(bus);
  }
  
  void spi_init_pins(spi_t bus)
  {
      /* MISO must always have PD/PU, see #5968. This is a ~65uA difference */
      gpio_init(spi_config[bus].miso_pin, GPIO_IN_PD);
      gpio_init(spi_config[bus].mosi_pin, GPIO_OUT);
      gpio_init(spi_config[bus].clk_pin, GPIO_OUT);
      gpio_init_mux(spi_config[bus].miso_pin, spi_config[bus].miso_mux);
      gpio_init_mux(spi_config[bus].mosi_pin, spi_config[bus].mosi_mux);
      gpio_init_mux(spi_config[bus].clk_pin, spi_config[bus].clk_mux);
  }
  
  int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
  {
      /* get exclusive access to the device */
      mutex_lock(&locks[bus]);
      /* power on the device */
      poweron(bus);
  
      /* disable the device */
      dev(bus)->CTRLA.reg &= ~(SERCOM_SPI_CTRLA_ENABLE);
      while (dev(bus)->SYNCBUSY.reg & SERCOM_SPI_SYNCBUSY_ENABLE) {}
  
      /* configure bus clock, in synchronous mode its calculated from
       * BAUD.reg = (f_ref / (2 * f_bus) - 1)
       * with f_ref := CLOCK_CORECLOCK as defined by the board */
      dev(bus)->BAUD.reg = (uint8_t)(((uint32_t)CLOCK_CORECLOCK) / (2 * clk) - 1);
  
      /* configure device to be master and set mode and pads,
       *
       * NOTE: we could configure the pads already during spi_init, but for
       * efficiency reason we do that here, so we can do all in one single write
       * to the CTRLA register */
      dev(bus)->CTRLA.reg = (SERCOM_SPI_CTRLA_MODE(0x3) |     /* 0x3 -> master */
                             SERCOM_SPI_CTRLA_DOPO(spi_config[bus].mosi_pad) |
                             SERCOM_SPI_CTRLA_DIPO(spi_config[bus].miso_pad) |
                             (mode <<  SERCOM_SPI_CTRLA_CPOL_Pos));
      /* also no synchronization needed here, as CTRLA is write-synchronized */
  
      /* finally enable the device */
      dev(bus)->CTRLA.reg |= SERCOM_SPI_CTRLA_ENABLE;
      while (dev(bus)->SYNCBUSY.reg & SERCOM_SPI_SYNCBUSY_ENABLE) {}
  
      return SPI_OK;
  }
  
  void spi_release(spi_t bus)
  {
      /* release access to the device */
      mutex_unlock(&locks[bus]);
  }
  
  void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
                          const void *out, void *in, size_t len)
  {
      const uint8_t *out_buf = out;
      uint8_t *in_buf = in;
  
      assert(out || in);
  
      if (cs != SPI_CS_UNDEF) {
          gpio_clear((gpio_t)cs);
      }
  
      for (int i = 0; i < (int)len; i++) {
          uint8_t tmp = (out_buf) ? out_buf[i] : 0;
          while (!(dev(bus)->INTFLAG.reg & SERCOM_SPI_INTFLAG_DRE)) {}
          dev(bus)->DATA.reg = tmp;
          while (!(dev(bus)->INTFLAG.reg & SERCOM_SPI_INTFLAG_RXC)) {}
          tmp = (uint8_t)dev(bus)->DATA.reg;
          if (in_buf) {
              in_buf[i] = tmp;
          }
      }
  
      if ((!cont) && (cs != SPI_CS_UNDEF)) {
          gpio_set((gpio_t)cs);
      }
  }