aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/TIVA/LLD/tiva_udma.c
blob: 9f122b276a3b48955cda76346b1aa52264439640 (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
/*
    Copyright (C) 2014..2016 Marco Veeneman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "hal.h"

/* The following macro is only defined if some driver requiring DMA services
   has been enabled.*/
#if defined(TIVA_UDMA_REQUIRED) || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

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

udmaControlTable_t udmaControlTable;

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

static uint32_t udma_channel_mask;

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

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

#if !defined(TIVA_UDMA_SW_HANDLER)
#error "TIVA_UDMA_SW_HANDLER not defined"
#endif
/**
 * @brief   UDMA software interrupt handler.
 *
 * @isr
 */
OSAL_IRQ_HANDLER(TIVA_UDMA_SW_HANDLER)
{
  OSAL_IRQ_PROLOGUE();

  /* TODO Process software transfer interrupts.*/

  OSAL_IRQ_EPILOGUE();
}

#if !defined(TIVA_UDMA_ERR_HANDLER)
#error "TIVA_UDMA_ERR_HANDLER not defined"
#endif
/**
 * @brief   UDMA error interrupt handler.
 *
 * @isr
 */
OSAL_IRQ_HANDLER(TIVA_UDMA_ERR_HANDLER)
{
  OSAL_IRQ_PROLOGUE();

  /* TODO Do we need to halt the system on a DMA error?*/

  if (UDMA->ERRCLR) {
    UDMA->ERRCLR = 1;
  }

  OSAL_IRQ_EPILOGUE();
}

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

/**
 * @brief   Initialize UDMA.
 *
 * @init
 */
void udmaInit(void)
{
  udma_channel_mask = 0;

  /* Enable UDMA module.*/
  SYSCTL->RCGCDMA = 1;
  while (!(SYSCTL->PRDMA & (1 << 0)))
    ;

  nvicEnableVector(TIVA_UDMA_ERR_NUMBER, TIVA_UDMA_ERR_IRQ_PRIORITY);
  nvicEnableVector(TIVA_UDMA_SW_NUMBER, TIVA_UDMA_SW_IRQ_PRIORITY);

  /* Enable UDMA controller.*/
  UDMA->CFG = 1;

  /* Set address of control table.*/
  UDMA->CTLBASE = (uint32_t)udmaControlTable.primary;
}

/**
 * @brief   Allocates a DMA channel.
 *
 * @special
 */
bool udmaChannelAllocate(uint8_t dmach)
{
  /* Checks if the channel is already taken.*/
  if ((udma_channel_mask & (1 << dmach)) != 0)
    return TRUE;

  /* Mark channel as used */
  udma_channel_mask |= (1 << dmach);

  return FALSE;
}

/**
 * @brief   Releases a DMA channel.
 *
 * @special
 */
void udmaChannelRelease(uint8_t dmach)
{
  /* Marks the channel as not used.*/
  udma_channel_mask &= ~(1 << dmach);
}

#endif