1 /* --COPYRIGHT--,BSD\r
2 * Copyright (c) 2014, Texas Instruments Incorporated\r
3 * All rights reserved.\r
4 *\r
5 * Redistribution and use in source and binary forms, with or without\r
6 * modification, are permitted provided that the following conditions\r
7 * are met:\r
8 *\r
9 * * Redistributions of source code must retain the above copyright\r
10 * notice, this list of conditions and the following disclaimer.\r
11 *\r
12 * * Redistributions in binary form must reproduce the above copyright\r
13 * notice, this list of conditions and the following disclaimer in the\r
14 * documentation and/or other materials provided with the distribution.\r
15 *\r
16 * * Neither the name of Texas Instruments Incorporated nor the names of\r
17 * its contributors may be used to endorse or promote products derived\r
18 * from this software without specific prior written permission.\r
19 *\r
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\r
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\r
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
31 * --/COPYRIGHT--*/\r
32 \r
33 //\r
34 //! \cond\r
35 //\r
36 \r
37 /* \r
38 * ======== dma.c ========\r
39 */\r
40 #include <string.h>\r
41 \r
42 #include "driverlib.h"\r
43 \r
44 #include "../USB_Common/device.h"\r
45 #include "../USB_Common/defMSP430USB.h"\r
46 #include <descriptors.h>\r
47 #include <string.h>\r
48 \r
49 #ifdef __REGISTER_MODEL__\r
50 /* for IAR */\r
51 # if __REGISTER_MODEL__ == __REGISTER_MODEL_REG20__\r
52 # define __DMA_ACCESS_REG__ (void __data20 *)\r
53 # else\r
54 # define __DMA_ACCESS_REG__ (uint16_t)\r
55 # endif\r
56 #else\r
57 /* for CCS */\r
58 # define __DMA_ACCESS_REG__ (__SFR_FARPTR)(uint32_t)\r
59 #endif\r
60 \r
61 //function pointers\r
62 void *(*USB_TX_memcpy)(void * dest, const void * source, size_t count);\r
63 void *(*USB_RX_memcpy)(void * dest, const void * source, size_t count);\r
64 \r
65 void * memcpyDMA0 (void * dest, const void * source, size_t count);\r
66 void * memcpyDMA1 (void * dest, const void * source, size_t count);\r
67 void * memcpyDMA2 (void * dest, const void * source, size_t count);\r
68 \r
69 //NOTE: this functin works only with data in the area <64k (small memory model)\r
70 void * memcpyV (void * dest, const void * source, size_t count)\r
71 {\r
72 uint16_t i;\r
73 volatile uint8_t bTmp;\r
74 \r
75 for (i = 0; i < count; i++)\r
76 {\r
77 bTmp = *((uint8_t*)source + i);\r
78 *((uint8_t*)dest + i) = bTmp;\r
79 }\r
80 return (dest);\r
81 }\r
82 \r
83 void * memcpyDMA (void * dest, const void * source, size_t count)\r
84 {\r
85 if (count == 0){ //do nothing if zero bytes to transfer\r
86 return (dest);\r
87 }\r
88 \r
89 //DMA4 workaround - disable DMA transfers during read-modify-write CPU \r
90 //operations\r
91 #ifndef DRIVERLIB_LEGACY_MODE\r
92 DMA_disableTransferDuringReadModifyWrite();\r
93 DMA_setSrcAddress(USB_DMA_CHAN, (uint32_t)source, DMA_DIRECTION_INCREMENT);\r
94 DMA_setDstAddress(USB_DMA_CHAN, (uint32_t)dest, DMA_DIRECTION_INCREMENT);\r
95 //DMA4 workaround - re-enable DMA transfers during read-modify-write CPU \r
96 //operations\r
97 DMA_enableTransferDuringReadModifyWrite();\r
98 DMA_setTransferSize(USB_DMA_CHAN, count);\r
99 DMA_enableTransfers(USB_DMA_CHAN);\r
100 DMA_startTransfer(USB_DMA_CHAN);\r
101 \r
102 while (DMA_getInterruptStatus(USB_DMA_CHAN) == DMA_INT_INACTIVE);\r
103 \r
104 DMA_disableTransfers(USB_DMA_CHAN);\r
105 #else\r
106 \r
107 DMA_disableTransferDuringReadModifyWrite(DMA_BASE);\r
108 DMA_setSrcAddress(DMA_BASE, USB_DMA_CHAN, (uint32_t)source, DMA_DIRECTION_INCREMENT);\r
109 DMA_setDstAddress(DMA_BASE, USB_DMA_CHAN, (uint32_t)dest, DMA_DIRECTION_INCREMENT);\r
110 //DMA4 workaround - re-enable DMA transfers during read-modify-write CPU \r
111 //operations\r
112 DMA_enableTransferDuringReadModifyWrite(DMA_BASE);\r
113 DMA_setTransferSize(DMA_BASE, USB_DMA_CHAN, count);\r
114 DMA_enableTransfers(DMA_BASE, USB_DMA_CHAN);\r
115 DMA_startTransfer(DMA_BASE, USB_DMA_CHAN);\r
116 \r
117 while (DMA_getInterruptStatus(DMA_BASE, USB_DMA_CHAN) == DMA_INT_INACTIVE);\r
118 \r
119 DMA_disableTransfers(DMA_BASE, USB_DMA_CHAN);\r
120 #endif\r
121 \r
122 return (dest);\r
123 }\r
124 \r
125 //this function inits the DMA\r
126 void USB_initMemcpy (void)\r
127 {\r
128 USB_TX_memcpy = memcpyV;\r
129 USB_RX_memcpy = memcpyV;\r
130 \r
131 if (USB_DMA_CHAN != 0xFF) {\r
132 #ifndef DRIVERLIB_LEGACY_MODE\r
133 DMA_init (USB_DMA_CHAN, DMA_TRANSFER_BLOCK, 0,\r
134 DMA_TRIGGERSOURCE_0, DMA_SIZE_SRCBYTE_DSTBYTE, DMA_TRIGGER_HIGH);\r
135 #else\r
136 DMA_init (DMA_BASE, USB_DMA_CHAN, DMA_TRANSFER_BLOCK, 0,\r
137 DMA_TRIGGERSOURCE_0, DMA_SIZE_SRCBYTE_DSTBYTE, DMA_TRIGGER_HIGH);\r
138 #endif\r
139 USB_TX_memcpy = memcpyDMA;\r
140 USB_RX_memcpy = memcpyDMA;\r
141 }\r
142 }\r
143 \r
144 //\r
145 //! \endcond\r
146 //\r
147 \r
148 /*----------------------------------------------------------------------------+\r
149 | End of source file |\r
150 +----------------------------------------------------------------------------*/\r
151 /*------------------------ Nothing Below This Line --------------------------*/\r
152 //Released_Version_4_10_02\r