Blame view

RIOT/cpu/kinetis_common/periph/pwm.c 9.19 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  /*
   * Copyright (C) 2014 Freie Universität Berlin
   * Copyright (C) 2014 PHYTEC Messtechnik GmbH
   * Copyright (C) 2015-2016 Eistec AB
   *
   * 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_kinetis_common_pwm
   *
   * @{
   *
   * @file
   * @brief       Low-level PWM driver implementation
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @author      Johann Fischer <j.fischer@phytec.de>
   * @author      Jonas Remmert <j.remmert@phytec.de>
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   *
   * @}
   */
  
  #include <stdint.h>
  #include <string.h>
  
  #include "cpu.h"
  #include "periph/pwm.h"
  #include "periph_conf.h"
  
  /* FTM channel look up tables */
  #if PWM_0_EN
  static const uint8_t ftm0chan[] = {
  #if PWM_0_CHANNELS > 0
      PWM_0_CH0_FTMCHAN,
  #endif
  #if PWM_0_CHANNELS > 1
      PWM_0_CH1_FTMCHAN,
  #endif
  #if PWM_0_CHANNELS > 2
      PWM_0_CH2_FTMCHAN,
  #endif
  #if PWM_0_CHANNELS > 3
      PWM_0_CH3_FTMCHAN,
  #endif
  #if PWM_0_CHANNELS > 4
      PWM_0_CH4_FTMCHAN,
  #endif
  #if PWM_0_CHANNELS > 5
      PWM_0_CH5_FTMCHAN,
  #endif
  #if PWM_0_CHANNELS > 6
      PWM_0_CH6_FTMCHAN,
  #endif
  #if PWM_0_CHANNELS > 7
      PWM_0_CH7_FTMCHAN,
  #endif
  };
  #endif
  #if PWM_1_EN
  static const uint8_t ftm1chan[] = {
  #if PWM_1_CHANNELS > 0
      PWM_1_CH0_FTMCHAN,
  #endif
  #if PWM_1_CHANNELS > 1
      PWM_1_CH1_FTMCHAN,
  #endif
  #if PWM_1_CHANNELS > 2
      PWM_1_CH2_FTMCHAN,
  #endif
  #if PWM_1_CHANNELS > 3
      PWM_1_CH3_FTMCHAN,
  #endif
  #if PWM_1_CHANNELS > 4
      PWM_1_CH4_FTMCHAN,
  #endif
  #if PWM_1_CHANNELS > 5
      PWM_1_CH5_FTMCHAN,
  #endif
  #if PWM_1_CHANNELS > 6
      PWM_1_CH6_FTMCHAN,
  #endif
  #if PWM_1_CHANNELS > 7
      PWM_1_CH7_FTMCHAN,
  #endif
  };
  #endif
  
  uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res)
  {
      FTM_Type *ftm;
      int channels = 0;
      uint32_t pwm_clk = 0;
      const uint8_t *ftmchan = NULL;
  
      switch (dev) {
  #if PWM_0_EN
  
          case PWM_0:
              channels = PWM_0_CHANNELS;
              pwm_clk = PWM_0_CLK;
              ftm = PWM_0_DEV;
              ftmchan = &ftm0chan[0];
              break;
  #endif
  #if PWM_1_EN
  
          case PWM_1:
              channels = PWM_1_CHANNELS;
              pwm_clk = PWM_1_CLK;
              ftm = PWM_1_DEV;
              ftmchan = &ftm1chan[0];
              break;
  #endif
  
          default:
              return 0;
      }
  
      switch (mode) {
          case PWM_LEFT:
          case PWM_RIGHT:
          case PWM_CENTER:
              break;
  
          default:
              return 0;
      }
  
      if ((unsigned int)res > (PWM_MAX_VALUE + 1) || (res * freq) > pwm_clk) {
          return 0;
      }
  
      /* Try to find a good prescaler value */
      /* The prescaler divides the module clock by a power of two, between 2^0 and 2^7 */
      uint8_t prescaler = 0;
      /* (resolution * frequency) is the number of timer ticks per second */
      while ((pwm_clk >> prescaler) > (res * freq)) {
          ++prescaler;
          if (prescaler > 7) {
              /* Module clock is too fast to reach the requested frequency using the
               * hardware supported prescaler values */
              /* Note: The frequency might be reachable if the requested resolution
               * is increased. */
              return 0;
          }
      }
      /* The chosen prescaler yields a timer frequency which is the
       * nearest possible frequency less than the requested frequency */
  
      /* Turn on the peripheral */
      pwm_poweron(dev);
  
      switch (dev) {
  #if PWM_0_EN
  
          case PWM_0:
              #if PWM_0_CHANNELS > 0
                  gpio_init_port(PWM_0_CH0_GPIO, PORT_PCR_MUX(PWM_0_CH0_AF));
              #endif
              #if PWM_0_CHANNELS > 1
                  gpio_init_port(PWM_0_CH1_GPIO, PORT_PCR_MUX(PWM_0_CH1_AF));
              #endif
              #if PWM_0_CHANNELS > 2
                  gpio_init_port(PWM_0_CH2_GPIO, PORT_PCR_MUX(PWM_0_CH2_AF));
              #endif
              #if PWM_0_CHANNELS > 3
                  gpio_init_port(PWM_0_CH3_GPIO, PORT_PCR_MUX(PWM_0_CH3_AF));
              #endif
              #if PWM_0_CHANNELS > 4
                  gpio_init_port(PWM_0_CH4_GPIO, PORT_PCR_MUX(PWM_0_CH4_AF));
              #endif
              #if PWM_0_CHANNELS > 5
                  gpio_init_port(PWM_0_CH5_GPIO, PORT_PCR_MUX(PWM_0_CH5_AF));
              #endif
              #if PWM_0_CHANNELS > 6
                  gpio_init_port(PWM_0_CH6_GPIO, PORT_PCR_MUX(PWM_0_CH6_AF));
              #endif
              #if PWM_0_CHANNELS > 7
                  gpio_init_port(PWM_0_CH7_GPIO, PORT_PCR_MUX(PWM_0_CH7_AF));
              #endif
              break;
  #endif
  #if PWM_1_EN
  
          case PWM_1:
              #if PWM_1_CHANNELS > 0
                  gpio_init_port(PWM_1_CH0_GPIO, PORT_PCR_MUX(PWM_1_CH0_AF));
              #endif
              #if PWM_1_CHANNELS > 1
                  gpio_init_port(PWM_1_CH1_GPIO, PORT_PCR_MUX(PWM_1_CH1_AF));
              #endif
              #if PWM_1_CHANNELS > 2
                  gpio_init_port(PWM_1_CH2_GPIO, PORT_PCR_MUX(PWM_1_CH2_AF));
              #endif
              #if PWM_1_CHANNELS > 3
                  gpio_init_port(PWM_1_CH3_GPIO, PORT_PCR_MUX(PWM_1_CH3_AF));
              #endif
              #if PWM_1_CHANNELS > 4
                  gpio_init_port(PWM_1_CH4_GPIO, PORT_PCR_MUX(PWM_1_CH4_AF));
              #endif
              #if PWM_1_CHANNELS > 5
                  gpio_init_port(PWM_1_CH5_GPIO, PORT_PCR_MUX(PWM_1_CH5_AF));
              #endif
              #if PWM_1_CHANNELS > 6
                  gpio_init_port(PWM_1_CH6_GPIO, PORT_PCR_MUX(PWM_1_CH6_AF));
              #endif
              #if PWM_1_CHANNELS > 7
                  gpio_init_port(PWM_1_CH7_GPIO, PORT_PCR_MUX(PWM_1_CH7_AF));
              #endif
              break;
  #endif
  
          default:
              return 0;
      }
  
      /* disable write protect for changing settings */
      ftm->MODE = FTM_MODE_WPDIS_MASK;
  
      /* reset timer match value */
      for (int i = 0; i < channels; i++) {
          ftm->CONTROLS[i].CnV = 0;
      }
  
      /* reset timer configuration registers */
      ftm->COMBINE = 0;
      ftm->CNTIN = 0;
      ftm->SWOCTRL = 0;
  
      /* set prescale and mod registers to matching values for resolution and frequency */
      ftm->SC = FTM_SC_PS(prescaler);
      ftm->MOD = res - 1;
  
      /* set PWM mode */
      uint32_t mode_mask = 0;
      switch (mode) {
          case PWM_LEFT:
              mode_mask = (1 << FTM_CnSC_MSB_SHIFT) | (1 << FTM_CnSC_ELSB_SHIFT);
              break;
  
          case PWM_RIGHT:
              mode_mask = (1 << FTM_CnSC_MSB_SHIFT) | (1 << FTM_CnSC_ELSA_SHIFT);
              break;
  
          case PWM_CENTER:
              mode_mask = (1 << FTM_CnSC_MSB_SHIFT);
              ftm->SC |= (1 << FTM_SC_CPWMS_SHIFT);
              break;
      }
      for (int i = 0; i < channels; i++) {
          /* cppcheck thinks ftmchan may be NULL here, but the variable is
           * assigned in all non-returning branches of the switch at the top of
           * this function. */
          /* cppcheck-suppress nullPointer ftmchan */
          ftm->CONTROLS[ftmchan[i]].CnSC = mode_mask;
      }
  
      /* enable timer ergo the PWM generation */
      pwm_start(dev);
  
      /* Return actual frequency */
      return (pwm_clk / (1 << prescaler)) / res;
  }
  
  uint8_t pwm_channels(pwm_t dev)
  {
      switch (dev) {
  #if PWM_0_EN
          case PWM_0:
              return PWM_0_CHANNELS;
  #endif
  #if PWM_1_EN
          case PWM_1:
              return PWM_1_CHANNELS;
  #endif
          default:
              return 0;
      }
  }
  
  void pwm_set(pwm_t dev, uint8_t channel, uint16_t value)
  {
      FTM_Type *ftm;
      const uint8_t *ftmchan = NULL;
  
      switch (dev) {
  #if PWM_0_EN
  
          case PWM_0:
              if (channel > PWM_0_CHANNELS) {
                  return;
              }
              ftm = PWM_0_DEV;
              ftmchan = &ftm0chan[0];
              break;
  #endif
  #if PWM_1_EN
  
          case PWM_1:
              if (channel > PWM_1_CHANNELS) {
                  return;
              }
              ftm = PWM_1_DEV;
              ftmchan = &ftm1chan[0];
              break;
  #endif
  
          default:
              return;
      }
  
      /* clamp value to maximum possible value */
      if (value > PWM_MAX_VALUE) {
          value = PWM_MAX_VALUE;
      }
  
      /* cppcheck thinks ftmchan may be NULL here, but the variable is
       * assigned in all non-returning branches of the switch at the top of
       * this function. */
      /* cppcheck-suppress nullPointer */
      ftm->CONTROLS[ftmchan[channel]].CnV = value;
  }
  
  void pwm_start(pwm_t dev)
  {
      switch (dev) {
  #if PWM_0_EN
  
          case PWM_0:
              PWM_0_DEV->SC |= FTM_SC_CLKS(1);
              break;
  #endif
  #if PWM_1_EN
  
          case PWM_1:
              PWM_1_DEV->SC |= FTM_SC_CLKS(1);
              break;
  #endif
      }
  }
  
  void pwm_stop(pwm_t dev)
  {
      switch (dev) {
  #if PWM_0_EN
  
          case PWM_0:
              PWM_0_DEV->SC &= ~FTM_SC_CLKS_MASK;
              break;
  #endif
  #if PWM_1_EN
  
          case PWM_1:
              PWM_1_DEV->SC &= ~FTM_SC_CLKS_MASK;
              break;
  #endif
      }
  }
  
  void pwm_poweron(pwm_t dev)
  {
      switch (dev) {
  #if PWM_0_EN
  
          case PWM_0:
              PWM_0_CLKEN();
              break;
  #endif
  #if PWM_1_EN
  
          case PWM_1:
              PWM_1_CLKEN();
              break;
  #endif
      }
  }
  
  void pwm_poweroff(pwm_t dev)
  {
      switch (dev) {
  #if PWM_0_EN
  
          case PWM_0:
              PWM_0_CLKDIS();
              break;
  #endif
  #if PWM_1_EN
  
          case PWM_1:
              PWM_1_CLKDIS();
              break;
  #endif
      }
  }