aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/platforms/STM32/stm32_dma.c
blob: 2232df4486b58a2a1f4fec16411885faf54b545a (plain)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    stm32_dma.c
 * @brief   STM32 DMA helper driver code.
 *
 * @addtogroup STM32_DMA
 * @details DMA sharing helper driver. In the STM32 the DMA channels are a
 *          shared resource, this driver allows to allocate and free DMA
 *          channels at runtime in order to allow all the other device
 *          drivers to coordinate the access to the resource.
 * @note    The DMA ISR handlers are all declared into this module because
 *          sharing, the various device drivers can associate a callback to
 *          IRSs when allocating channels.
 * @{
 */

#include "ch.h"
#include "hal.h"

#if defined(STM32_DMA_REQUIRED) || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local variables and types.                                         */
/*===========================================================================*/

/**
 * @brief   DMA ISR redirector type.
 */
typedef struct {
  stm32_dmaisr_t        dmaisrfunc;
  void                  *dmaisrparam;
} dma_isr_redir_t;

static uint32_t dmamsk1;
static dma_isr_redir_t dma1[7];

#if STM32_HAS_DMA2
static uint32_t dmamsk2;
static dma_isr_redir_t dma2[5];
#endif

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

/**
 * @brief   DMA1 channel 1 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA1_Ch1_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA1->ISR >> (STM32_DMA_CHANNEL_1 * 4);
  dmaClearChannel(STM32_DMA1, STM32_DMA_CHANNEL_1);
  if (dma1[0].dmaisrfunc)
    dma1[0].dmaisrfunc(dma1[0].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA1 channel 2 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA1_Ch2_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA1->ISR >> (STM32_DMA_CHANNEL_2 * 4);
  dmaClearChannel(STM32_DMA1, STM32_DMA_CHANNEL_2);
  if (dma1[1].dmaisrfunc)
    dma1[1].dmaisrfunc(dma1[1].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA1 channel 3 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA1_Ch3_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA1->ISR >> (STM32_DMA_CHANNEL_3 * 4);
  dmaClearChannel(STM32_DMA1, STM32_DMA_CHANNEL_3);
  if (dma1[2].dmaisrfunc)
    dma1[2].dmaisrfunc(dma1[2].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA1 channel 4 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA1_Ch4_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA1->ISR >> (STM32_DMA_CHANNEL_4 * 4);
  dmaClearChannel(STM32_DMA1, STM32_DMA_CHANNEL_4);
  if (dma1[3].dmaisrfunc)
    dma1[3].dmaisrfunc(dma1[3].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA1 channel 5 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA1_Ch5_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA1->ISR >> (STM32_DMA_CHANNEL_5 * 4);
  dmaClearChannel(STM32_DMA1, STM32_DMA_CHANNEL_5);
  if (dma1[4].dmaisrfunc)
    dma1[4].dmaisrfunc(dma1[4].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA1 channel 6 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA1_Ch6_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA1->ISR >> (STM32_DMA_CHANNEL_6 * 4);
  dmaClearChannel(STM32_DMA1, STM32_DMA_CHANNEL_6);
  if (dma1[5].dmaisrfunc)
    dma1[5].dmaisrfunc(dma1[5].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA1 channel 7 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA1_Ch7_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA1->ISR >> (STM32_DMA_CHANNEL_7 * 4);
  dmaClearChannel(STM32_DMA1, STM32_DMA_CHANNEL_7);
  if (dma1[6].dmaisrfunc)
    dma1[6].dmaisrfunc(dma1[6].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

#if STM32_HAS_DMA2 || defined(__DOXYGEN__)
/**
 * @brief   DMA2 channel 1 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA2_Ch1_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA2->ISR >> (STM32_DMA_CHANNEL_1 * 4);
  dmaClearChannel(STM32_DMA2, STM32_DMA_CHANNEL_1);
  if (dma2[0].dmaisrfunc)
    dma2[0].dmaisrfunc(dma2[0].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA2 channel 2 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA2_Ch2_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA2->ISR >> (STM32_DMA_CHANNEL_2 * 4);
  dmaClearChannel(STM32_DMA2, STM32_DMA_CHANNEL_2);
  if (dma2[1].dmaisrfunc)
    dma2[1].dmaisrfunc(dma2[1].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA2 channel 3 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA2_Ch3_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA2->ISR >> (STM32_DMA_CHANNEL_3 * 4);
  dmaClearChannel(STM32_DMA2, STM32_DMA_CHANNEL_3);
  if (dma2[2].dmaisrfunc)
    dma2[2].dmaisrfunc(dma2[2].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

#if defined(STM32F10X_CL) || defined(__DOXYGEN__)
/**
 * @brief   DMA2 channel 4 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA2_Ch4_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA2->ISR >> (STM32_DMA_CHANNEL_4 * 4);
  dmaClearChannel(STM32_DMA2, STM32_DMA_CHANNEL_4);
  if (dma2[3].dmaisrfunc)
    dma2[3].dmaisrfunc(dma2[3].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

/**
 * @brief   DMA2 channel 5 shared interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA2_Ch5_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  isr = STM32_DMA2->ISR >> (STM32_DMA_CHANNEL_5 * 4);
  dmaClearChannel(STM32_DMA2, STM32_DMA_CHANNEL_5);
  if (dma2[4].dmaisrfunc)
    dma2[4].dmaisrfunc(dma2[4].dmaisrparam, isr);

  CH_IRQ_EPILOGUE();
}

#else /* !STM32F10X_CL */
/**
 * @brief   DMA2 channels 4 and 5 shared interrupt handler.
 * @note    This IRQ is shared between DMA2 channels 4 and 5 so it is a
 *          bit less efficient because an extra check.
 *
 * @isr
 */
CH_IRQ_HANDLER(DMA2_Ch4_5_IRQHandler) {
  uint32_t isr;

  CH_IRQ_PROLOGUE();

  /* Check on channel 4.*/
  isr = STM32_DMA2->ISR >> (STM32_DMA_CHANNEL_5 * 4);
  if (isr & DMA_ISR_GIF1) {
    dmaClearChannel(STM32_DMA2, STM32_DMA_CHANNEL_5);
    if (dma2[3].dmaisrfunc)
      dma2[3].dmaisrfunc(dma2[3].dmaisrparam, isr);
  }

  /* Check on channel 5.*/
  isr = STM32_DMA2->ISR >> (STM32_DMA_CHANNEL_4 * 4);
  if (isr & DMA_ISR_GIF1) {
    dmaClearChannel(STM32_DMA2, STM32_DMA_CHANNEL_5);
    if (dma2[4].dmaisrfunc)
      dma2[4].dmaisrfunc(dma2[4].dmaisrparam, isr);
  }

  CH_IRQ_EPILOGUE();
}
#endif /* !STM32F10X_CL */
#endif /* STM32_HAS_DMA2 */

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   STM32 DMA helper initialization.
 *
 * @init
 */
void dmaInit(void) {
  int i;

  dmamsk1 = 0;
  for (i = STM32_DMA_CHANNEL_7; i >= STM32_DMA_CHANNEL_1; i--) {
    dmaDisableChannel(STM32_DMA1, i);
    dma1[i].dmaisrfunc = NULL;
  }
  STM32_DMA1->IFCR = 0xFFFFFFFF;
#if STM32_HAS_DMA2
  dmamsk2 = 0;
  for (i = STM32_DMA_CHANNEL_5; i >= STM32_DMA_CHANNEL_1; i--) {
    dmaDisableChannel(STM32_DMA2, i);
    dma2[i].dmaisrfunc = NULL;
  }
  STM32_DMA1->IFCR = 0xFFFFFFFF;
#endif
}

/**
 * @brief   Allocates a DMA channel.
 * @details The channel is allocated and, if required, the DMA clock enabled.
 *          Trying to allocate a channel already allocated is an illegal
 *          operation and is trapped if assertions are enabled.
 * @pre     The channel must not be already in use.
 * @post    The channel is allocated and the default ISR handler redirected
 *          to the specified function.
 * @post    The channel must be freed using @p dmaRelease() before it can
 *          be reused with another peripheral.
 * @note    This function can be invoked in both ISR or thread context.
 *
 * @param[in] dma       DMA controller id
 * @param[in] channel   requested channel id
 * @param[in] func      handling function pointer, can be @p NULL
 * @param[in] param     a parameter to be passed to the handling function
 * @return              The operation status.
 * @retval FALSE        operation successfully allocated.
 * @retval TRUE         the channel was already in use.
 *
 * @special
 */
void dmaAllocate(uint32_t dma, uint32_t channel,
                 stm32_dmaisr_t func, void *param) {

  chDbgCheck(func != NULL, "dmaAllocate");

#if STM32_HAS_DMA2
  switch (dma) {
  case STM32_DMA1_ID:
#else
  (void)dma;
#endif
    /* Check if the channel is already taken.*/
    chDbgAssert((dmamsk1 & (1 << channel)) == 0,
                "dmaAllocate(), #1", "already allocated");

    /* If the DMA unit was idle then the clock is enabled.*/
    if (dmamsk1 == 0) {
      RCC->AHBENR |= RCC_AHBENR_DMA1EN;
      DMA1->IFCR = 0x0FFFFFFF;
    }

    dmamsk1 |= 1 << channel;
    dma1[channel].dmaisrfunc  = func;
    dma1[channel].dmaisrparam = param;
#if STM32_HAS_DMA2
    break;
  case STM32_DMA2_ID:
    /* Check if the channel is already taken.*/
    chDbgAssert((dmamsk2 & (1 << channel)) == 0,
                "dmaAllocate(), #2", "already allocated");

    /* If the DMA unit was idle then the clock is enabled.*/
    if (dmamsk2 == 0) {
      RCC->AHBENR |= RCC_AHBENR_DMA2EN;
      DMA2->IFCR = 0x0FFFFFFF;
    }

    dmamsk2 |= 1 << channel;
    dma2[channel].dmaisrfunc  = func;
    dma2[channel].dmaisrparam = param;
    break;
  }
#endif
}

/**
 * @brief   Releases a DMA channel.
 * @details The channel is freed and, if required, the DMA clock disabled.
 *          Trying to release a unallocated channel is an illegal operation
 *          and is trapped if assertions are enabled.
 * @pre     The channel must have been allocated using @p dmaRequest().
 * @post    The channel is again available.
 * @note    This function can be invoked in both ISR or thread context.
 *
 * @param[in] dma       DMA controller id
 * @param[in] channel   requested channel id
 *
 * @special
 */
void dmaRelease(uint32_t dma, uint32_t channel) {

#if STM32_HAS_DMA2
  switch (dma) {
  case STM32_DMA1_ID:
#else
  (void)dma;
#endif
    /* Check if the channel is not taken.*/
    chDbgAssert((dmamsk1 & (1 << channel)) != 0,
                "dmaRelease(), #1", "not allocated");

    dma1[channel].dmaisrfunc = NULL;
    dmamsk1 &= ~(1 << channel);
    if (dmamsk1 == 0)
      RCC->AHBENR &= ~RCC_AHBENR_DMA1EN;
#if STM32_HAS_DMA2
    break;
  case STM32_DMA2_ID:
    /* Check if the channel is not taken.*/
    chDbgAssert((dmamsk2 & (1 << channel)) != 0,
                "dmaRelease(), #2", "not allocated");

    dma2[channel].dmaisrfunc = NULL;
    dmamsk2 &= ~(1 << channel);
    if (dmamsk2 == 0)
      RCC->AHBENR &= ~RCC_AHBENR_DMA2EN;
    break;
  }
#endif
}

#endif /* STM32_DMA_REQUIRED */

/** @} */