dma.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Direct Memory Access Controller Driver
  5. *
  6. * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef DMA_H_INCLUDED
  47. #define DMA_H_INCLUDED
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /**
  52. * \defgroup asfdoc_sam0_dma_group SAM Direct Memory Access Controller (DMAC) Driver
  53. *
  54. * This driver for Atmel&reg; | SMART ARM®-based microcontrollers provides an interface for the configuration
  55. * and management of the Direct Memory Access Controller(DMAC) module within
  56. * the device. The DMAC can transfer data between memories and peripherals, and
  57. * thus off-load these tasks from the CPU. The module supports peripheral to
  58. * peripheral, peripheral to memory, memory to peripheral, and memory to memory
  59. * transfers.
  60. *
  61. * The following peripheral is used by the DMAC Driver:
  62. * - DMAC (Direct Memory Access Controller)
  63. *
  64. * The following devices can use this module:
  65. * - Atmel | SMART SAM D21
  66. * - Atmel | SMART SAM R21
  67. * - Atmel | SMART SAM D09/D10/D11
  68. * - Atmel | SMART SAM L21/L22
  69. * - Atmel | SMART SAM DA1
  70. * - Atmel | SMART SAM C20/C21
  71. * - Atmel | SMART SAM HA1
  72. * - Atmel | SMART SAM R30
  73. *
  74. * The outline of this documentation is as follows:
  75. * - \ref asfdoc_sam0_dma_prerequisites
  76. * - \ref asfdoc_sam0_dma_module_overview
  77. * - \ref asfdoc_sam0_dma_special_considerations
  78. * - \ref asfdoc_sam0_dma_extra_info
  79. * - \ref asfdoc_sam0_dma_examples
  80. * - \ref asfdoc_sam0_dma_api_overview
  81. *
  82. *
  83. * \section asfdoc_sam0_dma_prerequisites Prerequisites
  84. *
  85. * There are no prerequisites for this module.
  86. *
  87. *
  88. * \section asfdoc_sam0_dma_module_overview Module Overview
  89. *
  90. * SAM devices with DMAC enables high data transfer rates with minimum
  91. * CPU intervention and frees up CPU time. With access to all peripherals,
  92. * the DMAC can handle automatic transfer of data to/from modules.
  93. * It supports static and incremental addressing for both source and
  94. * destination.
  95. *
  96. * The DMAC when used with Event System or peripheral triggers, provides a
  97. * considerable advantage by reducing the power consumption and performing
  98. * data transfer in the background.
  99. * For example, if the ADC is configured to generate an event, it can trigger
  100. * the DMAC to transfer the data into another peripheral or SRAM.
  101. * The CPU can remain in sleep during this time to reduce the power consumption.
  102. *
  103. * <table>
  104. * <tr>
  105. * <th>Device</th>
  106. * <th>Dma channel number</th>
  107. * </tr>
  108. * <tr>
  109. * <td>SAM D21/R21/C20/C21</td>
  110. * <td>12</td>
  111. * </tr>
  112. * <tr>
  113. * <td>SAM D09/D10/D11</td>
  114. * <td>6</td>
  115. * </tr>
  116. * <tr>
  117. * <td>SAM L21,SAMR30</td>
  118. * <td>16</td>
  119. * </tr>
  120. * </table>
  121. * The DMA channel operation can be suspended at any time by software, by events
  122. * from event system, or after selectable descriptor execution. The operation
  123. * can be resumed by software or by events from the event system.
  124. * The DMAC driver for SAM supports four types of transfers such as
  125. * peripheral to peripheral, peripheral to memory, memory to peripheral, and
  126. * memory to memory.
  127. *
  128. * The basic transfer unit is a beat, which is defined as a single bus access.
  129. * There can be multiple beats in a single block transfer and multiple block
  130. * transfers in a DMA transaction.
  131. * DMA transfer is based on descriptors, which holds transfer properties
  132. * such as the source and destination addresses, transfer counter, and other
  133. * additional transfer control information.
  134. * The descriptors can be static or linked. When static, a single block transfer
  135. * is performed. When linked, a number of transfer descriptors can be used to
  136. * enable multiple block transfers within a single DMA transaction.
  137. *
  138. * The implementation of the DMA driver is based on the idea that the DMA channel
  139. * is a finite resource of entities with the same abilities. A DMA channel resource
  140. * is able to move a defined set of data from a source address to destination
  141. * address triggered by a transfer trigger. On the SAM devices there are 12
  142. * DMA resources available for allocation. Each of these DMA resources can trigger
  143. * interrupt callback routines and peripheral events.
  144. * The other main features are:
  145. *
  146. * - Selectable transfer trigger source
  147. * - Software
  148. * - Event System
  149. * - Peripheral
  150. * - Event input and output is supported for the four lower channels
  151. * - Four level channel priority
  152. * - Optional interrupt generation on transfer complete, channel error, or channel suspend
  153. * - Supports multi-buffer or circular buffer mode by linking multiple descriptors
  154. * - Beat size configurable as 8-bit, 16-bit, or 32-bit
  155. *
  156. * A simplified block diagram of the DMA Resource can be seen in
  157. * \ref asfdoc_sam0_dma_module_block_diagram "the figure below".
  158. *
  159. * \anchor asfdoc_sam0_dma_module_block_diagram
  160. * \dot
  161. * digraph overview {
  162. * splines = false;
  163. * rankdir=LR;
  164. *
  165. * mux1 [label="Transfer Trigger", shape=box];
  166. *
  167. * dma [label="DMA Channel", shape=polygon, sides=6, orientation=60, style=filled, fillcolor=darkolivegreen1, height=1, width=1];
  168. * descriptor [label="Transfer Descriptor", shape=box, style=filled, fillcolor=lightblue];
  169. *
  170. * mux1 -> dma;
  171. * descriptor -> dma;
  172. *
  173. * interrupt [label="Interrupt", shape=box];
  174. * events [label="Events", shape=box];
  175. *
  176. * dma:e -> interrupt:w;
  177. * dma:e -> events:w;
  178. *
  179. * {rank=same; descriptor dma}
  180. *
  181. * }
  182. * \enddot
  183. *
  184. * \subsection asfdoc_sam0_dma_features Driver Feature Macro Definition
  185. * <table>
  186. * <tr>
  187. * <th>Driver Feature Macro</th>
  188. * <th>Supported devices</th>
  189. * </tr>
  190. * <tr>
  191. * <td>FEATURE_DMA_CHANNEL_STANDBY</td>
  192. * <td>SAM L21/L22/C20/C21/R30</td>
  193. * </tr>
  194. * </table>
  195. * \note The specific features are only available in the driver when the
  196. * selected device supports those features.
  197. *
  198. * \subsection asfdoc_sam0_dma_module_overview_dma_transf_term Terminology Used in DMAC Transfers
  199. *
  200. * <table border="0" cellborder="1" cellspacing="0" >
  201. * <tr>
  202. * <th> Name </th> <th> Description </th>
  203. * </tr>
  204. * <tr>
  205. * <td > Beat </td>
  206. * <td > It is a single bus access by the DMAC.
  207. * Configurable as 8-bit, 16-bit, or 32-bit.
  208. * </td>
  209. * </tr>
  210. * <tr>
  211. * <td > Burst </td>
  212. * <td> It is a transfer of n-beats (n=1,4,8,16).
  213. * For the DMAC module in SAM, the burst size is one beat.
  214. * Arbitration takes place each time a burst transfer is completed.
  215. * </td>
  216. * </tr>
  217. * <tr>
  218. * <td > Block transfer </td>
  219. * <td> A single block transfer is a configurable number of (1 to 64k)
  220. * beat transfers
  221. * </td>
  222. * </tr>
  223. * </table>
  224. *
  225. * \subsection asfdoc_sam0_dma_module_overview_dma_channels DMA Channels
  226. * The DMAC in each device consists of several DMA channels, which
  227. * along with the transfer descriptors defines the data transfer properties.
  228. * - The transfer control descriptor defines the source and destination
  229. * addresses, source and destination address increment settings, the
  230. * block transfer count, and event output condition selection
  231. * - Dedicated channel registers control the peripheral trigger source,
  232. * trigger mode settings, event input actions, and channel priority level
  233. * settings
  234. *
  235. * With a successful DMA resource allocation, a dedicated
  236. * DMA channel will be assigned. The channel will be occupied until the
  237. * DMA resource is freed. A DMA resource handle is used to identify the specific
  238. * DMA resource.
  239. * When there are multiple channels with active requests, the arbiter prioritizes
  240. * the channels requesting access to the bus.
  241. *
  242. * \subsection asfdoc_sam0_dma_module_overview_dma_trigger DMA Triggers
  243. * DMA transfer can be started only when a DMA transfer request is acknowledged/granted by the arbiter. A
  244. * transfer request can be triggered from software, peripheral, or an event. There
  245. * are dedicated source trigger selections for each DMA channel usage.
  246. *
  247. * \subsection asfdoc_sam0_dma_module_overview_dma_transfer_descriptor DMA Transfer Descriptor
  248. * The transfer descriptor resides in the SRAM and
  249. * defines these channel properties.
  250. * <table border="0" cellborder="1" cellspacing="0" >
  251. * <tr>
  252. * <th> Field name </th> <th> Field width </th>
  253. * </tr>
  254. * <tr>
  255. * <td > Descriptor Next Address </td> <td > 32 bits </td>
  256. * </tr>
  257. * <tr>
  258. * <td > Destination Address </td> <td> 32 bits </td>
  259. * </tr>
  260. * <tr>
  261. * <td > Source Address </td> <td> 32 bits </td>
  262. * </tr>
  263. * <tr>
  264. * <td > Block Transfer Counter </td> <td> 16 bits </td>
  265. * </tr>
  266. * <tr>
  267. * <td > Block Transfer Control </td> <td> 16 bits </td>
  268. * </tr>
  269. * </table>
  270. *
  271. * Before starting a transfer, at least one descriptor should be configured.
  272. * After a successful allocation of a DMA channel, the transfer descriptor can
  273. * be added with a call to \ref dma_add_descriptor(). If there is a transfer
  274. * descriptor already allocated to the DMA resource, the descriptor will
  275. * be linked to the next descriptor address.
  276. *
  277. * \subsection asfdoc_sam0_dma_module_overview_dma_output DMA Interrupts/Events
  278. * Both an interrupt callback and an peripheral event can be triggered by the
  279. * DMA transfer. Three types of callbacks are supported by the DMA driver:
  280. * transfer complete, channel suspend, and transfer error. Each of these callback
  281. * types can be registered and enabled for each channel independently through
  282. * the DMA driver API.
  283. *
  284. * The DMAC module can also generate events on transfer complete. Event
  285. * generation is enabled through the DMA channel, event channel configuration,
  286. * and event user multiplexing is done through the events driver.
  287. *
  288. * The DMAC can generate events in the below cases:
  289. *
  290. * - When a block transfer is complete
  291. *
  292. * - When each beat transfer within a block transfer is complete
  293. *
  294. * \section asfdoc_sam0_dma_special_considerations Special Considerations
  295. *
  296. * There are no special considerations for this module.
  297. *
  298. *
  299. * \section asfdoc_sam0_dma_extra_info Extra Information
  300. *
  301. * For extra information, see \ref asfdoc_sam0_dma_extra. This includes:
  302. * - \ref asfdoc_sam0_dma_extra_acronyms
  303. * - \ref asfdoc_sam0_dma_extra_dependencies
  304. * - \ref asfdoc_sam0_dma_extra_errata
  305. * - \ref asfdoc_sam0_dma_extra_history
  306. *
  307. *
  308. * \section asfdoc_sam0_dma_examples Examples
  309. *
  310. * For a list of examples related to this driver, see
  311. * \ref asfdoc_sam0_dma_exqsg.
  312. *
  313. *
  314. * \section asfdoc_sam0_dma_api_overview API Overview
  315. * @{
  316. */
  317. #include <compiler.h>
  318. #include "conf_dma.h"
  319. #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30) || defined(__DOXYGEN__)
  320. #define FEATURE_DMA_CHANNEL_STANDBY
  321. #endif
  322. /** DMA invalid channel number. */
  323. #define DMA_INVALID_CHANNEL 0xff
  324. /** ExInitial description section. */
  325. extern DmacDescriptor descriptor_section[CONF_MAX_USED_CHANNEL_NUM];
  326. /* DMA channel interrup flag. */
  327. extern uint8_t g_chan_interrupt_flag[CONF_MAX_USED_CHANNEL_NUM];
  328. /** DMA priority level. */
  329. enum dma_priority_level {
  330. /** Priority level 0. */
  331. DMA_PRIORITY_LEVEL_0,
  332. /** Priority level 1. */
  333. DMA_PRIORITY_LEVEL_1,
  334. /** Priority level 2. */
  335. DMA_PRIORITY_LEVEL_2,
  336. /** Priority level 3. */
  337. DMA_PRIORITY_LEVEL_3,
  338. };
  339. /** DMA input actions. */
  340. enum dma_event_input_action {
  341. /** No action. */
  342. DMA_EVENT_INPUT_NOACT,
  343. /** Normal transfer and periodic transfer trigger. */
  344. DMA_EVENT_INPUT_TRIG,
  345. /** Conditional transfer trigger. */
  346. DMA_EVENT_INPUT_CTRIG,
  347. /** Conditional block transfer. */
  348. DMA_EVENT_INPUT_CBLOCK,
  349. /** Channel suspend operation. */
  350. DMA_EVENT_INPUT_SUSPEND,
  351. /** Channel resume operation. */
  352. DMA_EVENT_INPUT_RESUME,
  353. /** Skip next block suspend action. */
  354. DMA_EVENT_INPUT_SSKIP,
  355. };
  356. /**
  357. * Address increment step size. These bits select the address increment step
  358. * size. The setting apply to source or destination address, depending on
  359. * STEPSEL setting.
  360. */
  361. enum dma_address_increment_stepsize {
  362. /** The address is incremented by (beat size * 1). */
  363. DMA_ADDRESS_INCREMENT_STEP_SIZE_1 = 0,
  364. /** The address is incremented by (beat size * 2). */
  365. DMA_ADDRESS_INCREMENT_STEP_SIZE_2,
  366. /** The address is incremented by (beat size * 4). */
  367. DMA_ADDRESS_INCREMENT_STEP_SIZE_4,
  368. /** The address is incremented by (beat size * 8). */
  369. DMA_ADDRESS_INCREMENT_STEP_SIZE_8,
  370. /** The address is incremented by (beat size * 16). */
  371. DMA_ADDRESS_INCREMENT_STEP_SIZE_16,
  372. /** The address is incremented by (beat size * 32). */
  373. DMA_ADDRESS_INCREMENT_STEP_SIZE_32,
  374. /** The address is incremented by (beat size * 64). */
  375. DMA_ADDRESS_INCREMENT_STEP_SIZE_64,
  376. /** The address is incremented by (beat size * 128). */
  377. DMA_ADDRESS_INCREMENT_STEP_SIZE_128,
  378. };
  379. /**
  380. * DMA step selection. This bit determines whether the step size setting
  381. * is applied to source or destination address.
  382. */
  383. enum dma_step_selection {
  384. /** Step size settings apply to the destination address. */
  385. DMA_STEPSEL_DST = 0,
  386. /** Step size settings apply to the source address. */
  387. DMA_STEPSEL_SRC,
  388. };
  389. /** The basic transfer unit in DMAC is a beat, which is defined as a
  390. * single bus access. Its size is configurable and applies to both read
  391. * and write. */
  392. enum dma_beat_size {
  393. /** 8-bit access. */
  394. DMA_BEAT_SIZE_BYTE = 0,
  395. /** 16-bit access. */
  396. DMA_BEAT_SIZE_HWORD,
  397. /** 32-bit access. */
  398. DMA_BEAT_SIZE_WORD,
  399. };
  400. /**
  401. * Block action definitions.
  402. */
  403. enum dma_block_action {
  404. /** No action. */
  405. DMA_BLOCK_ACTION_NOACT = 0,
  406. /** Channel in normal operation and sets transfer complete interrupt flag
  407. * after block transfer. */
  408. DMA_BLOCK_ACTION_INT,
  409. /** Trigger channel suspend after block transfer and sets channel
  410. * suspend interrupt flag once the channel is suspended. */
  411. DMA_BLOCK_ACTION_SUSPEND,
  412. /** Sets transfer complete interrupt flag after a block transfer and
  413. * trigger channel suspend. The channel suspend interrupt flag will be set
  414. * once the channel is suspended. */
  415. DMA_BLOCK_ACTION_BOTH,
  416. };
  417. /** Event output selection. */
  418. enum dma_event_output_selection {
  419. /** Event generation disable. */
  420. DMA_EVENT_OUTPUT_DISABLE = 0,
  421. /** Event strobe when block transfer complete. */
  422. DMA_EVENT_OUTPUT_BLOCK,
  423. /** Event output reserved. */
  424. DMA_EVENT_OUTPUT_RESERVED,
  425. /** Event strobe when beat transfer complete. */
  426. DMA_EVENT_OUTPUT_BEAT,
  427. };
  428. /** DMA trigger action type. */
  429. enum dma_transfer_trigger_action{
  430. /** Perform a block transfer when triggered. */
  431. DMA_TRIGGER_ACTION_BLOCK = DMAC_CHCTRLB_TRIGACT_BLOCK_Val,
  432. /** Perform a beat transfer when triggered. */
  433. DMA_TRIGGER_ACTION_BEAT = DMAC_CHCTRLB_TRIGACT_BEAT_Val,
  434. /** Perform a transaction when triggered. */
  435. DMA_TRIGGER_ACTION_TRANSACTION = DMAC_CHCTRLB_TRIGACT_TRANSACTION_Val,
  436. };
  437. /**
  438. * Callback types for DMA callback driver.
  439. */
  440. enum dma_callback_type {
  441. /** Callback for any of transfer errors. A transfer error is flagged
  442. * if a bus error is detected during an AHB access or when the DMAC
  443. * fetches an invalid descriptor. */
  444. DMA_CALLBACK_TRANSFER_ERROR,
  445. /** Callback for transfer complete. */
  446. DMA_CALLBACK_TRANSFER_DONE,
  447. /** Callback for channel suspend. */
  448. DMA_CALLBACK_CHANNEL_SUSPEND,
  449. /** Number of available callbacks. */
  450. DMA_CALLBACK_N,
  451. };
  452. /**
  453. * DMA transfer descriptor configuration. When the source or destination address
  454. * increment is enabled, the addresses stored into the configuration structure
  455. * must correspond to the end of the transfer.
  456. *
  457. */
  458. struct dma_descriptor_config {
  459. /** Descriptor valid flag used to identify whether a descriptor is
  460. valid or not */
  461. bool descriptor_valid;
  462. /** This is used to generate an event on specific transfer action in
  463. a channel. Supported only in four lower channels. */
  464. enum dma_event_output_selection event_output_selection;
  465. /** Action taken when a block transfer is completed */
  466. enum dma_block_action block_action;
  467. /** Beat size is configurable as 8-bit, 16-bit, or 32-bit */
  468. enum dma_beat_size beat_size;
  469. /** Used for enabling the source address increment */
  470. bool src_increment_enable;
  471. /** Used for enabling the destination address increment */
  472. bool dst_increment_enable;
  473. /** This bit selects whether the source or destination address is
  474. using the step size settings */
  475. enum dma_step_selection step_selection;
  476. /** The step size for source/destination address increment.
  477. The next address is calculated
  478. as next_addr = addr + (2^step_size * beat size). */
  479. enum dma_address_increment_stepsize step_size;
  480. /** It is the number of beats in a block. This count value is
  481. * decremented by one after each beat data transfer. */
  482. uint16_t block_transfer_count;
  483. /** Transfer source address */
  484. uint32_t source_address;
  485. /** Transfer destination address */
  486. uint32_t destination_address;
  487. /** Set to zero for static descriptors. This must have a valid memory
  488. address for linked descriptors. */
  489. uint32_t next_descriptor_address;
  490. };
  491. /** Configurations for DMA events. */
  492. struct dma_events_config {
  493. /** Event input actions */
  494. enum dma_event_input_action input_action;
  495. /** Enable DMA event output */
  496. bool event_output_enable;
  497. };
  498. /** DMA configurations for transfer. */
  499. struct dma_resource_config {
  500. /** DMA transfer priority */
  501. enum dma_priority_level priority;
  502. /**DMA peripheral trigger index */
  503. uint8_t peripheral_trigger;
  504. /** DMA trigger action */
  505. enum dma_transfer_trigger_action trigger_action;
  506. #ifdef FEATURE_DMA_CHANNEL_STANDBY
  507. /** Keep DMA channel enabled in standby sleep mode if true */
  508. bool run_in_standby;
  509. #endif
  510. /** DMA events configurations */
  511. struct dma_events_config event_config;
  512. };
  513. /** Forward definition of the DMA resource. */
  514. struct dma_resource;
  515. /** Type definition for a DMA resource callback function. */
  516. typedef void (*dma_callback_t)(struct dma_resource *const resource);
  517. /** Structure for DMA transfer resource. */
  518. struct dma_resource {
  519. /** Allocated DMA channel ID */
  520. uint8_t channel_id;
  521. /** Array of callback functions for DMA transfer job */
  522. dma_callback_t callback[DMA_CALLBACK_N];
  523. /** Bit mask for enabled callbacks */
  524. uint8_t callback_enable;
  525. /** Status of the last job */
  526. volatile enum status_code job_status;
  527. /** Transferred data size */
  528. uint32_t transfered_size;
  529. /** DMA transfer descriptor */
  530. DmacDescriptor* descriptor;
  531. };
  532. /**
  533. * \brief Get DMA resource status.
  534. *
  535. * \param[in] resource Pointer to the DMA resource
  536. *
  537. * \return Status of the DMA resource.
  538. */
  539. static inline enum status_code dma_get_job_status(struct dma_resource *resource)
  540. {
  541. Assert(resource);
  542. return resource->job_status;
  543. }
  544. /**
  545. * \brief Check if the given DMA resource is busy.
  546. *
  547. * \param[in] resource Pointer to the DMA resource
  548. *
  549. * \return Status which indicates whether the DMA resource is busy.
  550. *
  551. * \retval true The DMA resource has an on-going transfer
  552. * \retval false The DMA resource is not busy
  553. */
  554. static inline bool dma_is_busy(struct dma_resource *resource)
  555. {
  556. Assert(resource);
  557. return (resource->job_status == STATUS_BUSY);
  558. }
  559. /**
  560. * \brief Enable a callback function for a dedicated DMA resource.
  561. *
  562. * \param[in] resource Pointer to the DMA resource
  563. * \param[in] type Callback function type
  564. *
  565. */
  566. static inline void dma_enable_callback(struct dma_resource *resource,
  567. enum dma_callback_type type)
  568. {
  569. Assert(resource);
  570. resource->callback_enable |= 1 << type;
  571. g_chan_interrupt_flag[resource->channel_id] |= (1UL << type);
  572. }
  573. /**
  574. * \brief Disable a callback function for a dedicated DMA resource.
  575. *
  576. * \param[in] resource Pointer to the DMA resource
  577. * \param[in] type Callback function type
  578. *
  579. */
  580. static inline void dma_disable_callback(struct dma_resource *resource,
  581. enum dma_callback_type type)
  582. {
  583. Assert(resource);
  584. resource->callback_enable &= ~(1 << type);
  585. g_chan_interrupt_flag[resource->channel_id] &= (~(1UL << type) & DMAC_CHINTENSET_MASK);
  586. DMAC->CHINTENCLR.reg = (1UL << type);
  587. }
  588. /**
  589. * \brief Register a callback function for a dedicated DMA resource.
  590. *
  591. * There are three types of callback functions, which can be registered:
  592. * - Callback for transfer complete
  593. * - Callback for transfer error
  594. * - Callback for channel suspend
  595. *
  596. * \param[in] resource Pointer to the DMA resource
  597. * \param[in] callback Pointer to the callback function
  598. * \param[in] type Callback function type
  599. *
  600. */
  601. static inline void dma_register_callback(struct dma_resource *resource,
  602. dma_callback_t callback, enum dma_callback_type type)
  603. {
  604. Assert(resource);
  605. resource->callback[type] = callback;
  606. }
  607. /**
  608. * \brief Unregister a callback function for a dedicated DMA resource.
  609. *
  610. * There are three types of callback functions:
  611. * - Callback for transfer complete
  612. * - Callback for transfer error
  613. * - Callback for channel suspend
  614. *
  615. * The application can unregister any of the callback functions which
  616. * are already registered and are no longer needed.
  617. *
  618. * \param[in] resource Pointer to the DMA resource
  619. * \param[in] type Callback function type
  620. *
  621. */
  622. static inline void dma_unregister_callback(struct dma_resource *resource,
  623. enum dma_callback_type type)
  624. {
  625. Assert(resource);
  626. resource->callback[type] = NULL;
  627. }
  628. /**
  629. * \brief Will set a software trigger for resource.
  630. *
  631. * This function is used to set a software trigger on the DMA channel
  632. * associated with resource. If a trigger is already pending no new trigger
  633. * will be generated for the channel.
  634. *
  635. * \param[in] resource Pointer to the DMA resource
  636. */
  637. static inline void dma_trigger_transfer(struct dma_resource *resource) {
  638. Assert(resource);
  639. DMAC->SWTRIGCTRL.reg |= (1 << resource->channel_id);
  640. }
  641. /**
  642. * \brief Initializes DMA transfer configuration with predefined default values.
  643. *
  644. * This function will initialize a given DMA descriptor configuration structure to
  645. * a set of known default values. This function should be called on
  646. * any new instance of the configuration structure before being
  647. * modified by the user application.
  648. *
  649. * The default configuration is as follows:
  650. * \li Set the descriptor as valid
  651. * \li Disable event output
  652. * \li No block action
  653. * \li Set beat size as byte
  654. * \li Enable source increment
  655. * \li Enable destination increment
  656. * \li Step size is applied to the destination address
  657. * \li Address increment is beat size multiplied by 1
  658. * \li Default transfer size is set to 0
  659. * \li Default source address is set to NULL
  660. * \li Default destination address is set to NULL
  661. * \li Default next descriptor not available
  662. * \param[out] config Pointer to the configuration
  663. *
  664. */
  665. static inline void dma_descriptor_get_config_defaults(struct dma_descriptor_config *config)
  666. {
  667. Assert(config);
  668. /* Set descriptor as valid */
  669. config->descriptor_valid = true;
  670. /* Disable event output */
  671. config->event_output_selection = DMA_EVENT_OUTPUT_DISABLE;
  672. /* No block action */
  673. config->block_action = DMA_BLOCK_ACTION_NOACT;
  674. /* Set beat size to one byte */
  675. config->beat_size = DMA_BEAT_SIZE_BYTE;
  676. /* Enable source increment */
  677. config->src_increment_enable = true;
  678. /* Enable destination increment */
  679. config->dst_increment_enable = true;
  680. /* Step size is applied to the destination address */
  681. config->step_selection = DMA_STEPSEL_DST;
  682. /* Address increment is beat size multiplied by 1*/
  683. config->step_size = DMA_ADDRESS_INCREMENT_STEP_SIZE_1;
  684. /* Default transfer size is set to 0 */
  685. config->block_transfer_count = 0;
  686. /* Default source address is set to NULL */
  687. config->source_address = (uint32_t)NULL;
  688. /* Default destination address is set to NULL */
  689. config->destination_address = (uint32_t)NULL;
  690. /** Next descriptor address set to 0 */
  691. config->next_descriptor_address = 0;
  692. }
  693. /**
  694. * \brief Update DMA descriptor.
  695. *
  696. * This function can update the descriptor of an allocated DMA resource.
  697. *
  698. */
  699. static inline void dma_update_descriptor(struct dma_resource *resource,
  700. DmacDescriptor* descriptor)
  701. {
  702. Assert(resource);
  703. resource->descriptor = descriptor;
  704. }
  705. /**
  706. * \brief Reset DMA descriptor.
  707. *
  708. * This function will clear the DESCADDR register of an allocated DMA resource.
  709. *
  710. */
  711. static inline void dma_reset_descriptor(struct dma_resource *resource)
  712. {
  713. Assert(resource);
  714. resource->descriptor = NULL;
  715. }
  716. void dma_get_config_defaults(struct dma_resource_config *config);
  717. enum status_code dma_allocate(struct dma_resource *resource,
  718. struct dma_resource_config *config);
  719. enum status_code dma_free(struct dma_resource *resource);
  720. enum status_code dma_start_transfer_job(struct dma_resource *resource);
  721. void dma_abort_job(struct dma_resource *resource);
  722. void dma_suspend_job(struct dma_resource *resource);
  723. void dma_resume_job(struct dma_resource *resource);
  724. void dma_descriptor_create(DmacDescriptor* descriptor,
  725. struct dma_descriptor_config *config);
  726. enum status_code dma_add_descriptor(struct dma_resource *resource,
  727. DmacDescriptor* descriptor);
  728. /** @} */
  729. /**
  730. * \page asfdoc_sam0_dma_extra Extra Information for DMAC Driver
  731. *
  732. * \section asfdoc_sam0_dma_extra_acronyms Acronyms
  733. * Below is a table listing the acronyms used in this module, along with their
  734. * intended meanings.
  735. *
  736. * <table>
  737. * <tr>
  738. * <th>Acronym</th>
  739. * <th>Description</th>
  740. * </tr>
  741. * <tr>
  742. * <td>DMA</td>
  743. * <td>Direct Memory Access</td>
  744. * </tr>
  745. * <tr>
  746. * <td>DMAC</td>
  747. * <td>Direct Memory Access Controller </td>
  748. * </tr>
  749. * <tr>
  750. * <td>CPU</td>
  751. * <td>Central Processing Unit</td>
  752. * </tr>
  753. * </table>
  754. *
  755. *
  756. * \section asfdoc_sam0_dma_extra_dependencies Dependencies
  757. * This driver has the following dependencies:
  758. *
  759. * - \ref asfdoc_sam0_system_clock_group "System Clock Driver"
  760. *
  761. *
  762. * \section asfdoc_sam0_dma_extra_errata Errata
  763. * There are no errata related to this driver.
  764. *
  765. *
  766. * \section asfdoc_sam0_dma_extra_history Module History
  767. * An overview of the module history is presented in the table below, with
  768. * details on the enhancements and fixes made to the module since its first
  769. * release. The current version of this corresponds to the newest version in
  770. * the table.
  771. *
  772. * <table>
  773. * <tr>
  774. * <th>Changelog</th>
  775. * </tr>
  776. * <tr>
  777. * <td>Add SAM C21 support</td>
  778. * </tr>
  779. * <tr>
  780. * <td>Add SAM L21 support</td>
  781. * </tr>
  782. * <tr>
  783. * <td>Add SAM R30 support</td>
  784. * </tr>
  785. * <tr>
  786. * <td>Initial Release</td>
  787. * </tr>
  788. * </table>
  789. */
  790. /**
  791. * \page asfdoc_sam0_dma_exqsg Examples for DMAC Driver
  792. *
  793. * This is a list of the available Quick Start Guides (QSGs) and example
  794. * applications for \ref asfdoc_sam0_dma_group. QSGs are simple examples with
  795. * step-by-step instructions to configure and use this driver in a selection of
  796. * use cases. Note that a QSG can be compiled as a standalone application or be
  797. * added to the user application.
  798. *
  799. * - \subpage asfdoc_sam0_dma_basic_use_case
  800. *
  801. * \note More DMA usage examples are available in peripheral QSGs.
  802. * A quick start guide for TC/TCC
  803. * shows the usage of DMA event trigger; SERCOM SPI/USART/I<SUP>2</SUP>C has example for
  804. * DMA transfer from peripheral to memory or from memory to peripheral;
  805. * ADC/DAC shows peripheral to peripheral transfer.
  806. *
  807. * \page asfdoc_sam0_dma_document_revision_history Document Revision History
  808. *
  809. * <table>
  810. * <tr>
  811. * <th>Doc. Rev.</th>
  812. * <th>Date</th>
  813. * <th>Comments</th>
  814. * </tr>
  815. * <tr>
  816. * <td>42257C</td>
  817. * <td>12/2015</td>
  818. * <td>Added suppport for SAM L21/L22, SAM C21, SAM D09, SAMR30 and SAM DA1</td>
  819. * </tr>
  820. * <tr>
  821. * <td>42257B</td>
  822. * <td>12/2014</td>
  823. * <td>Added support for SAM R21 and SAM D10/D11</td>
  824. * </tr>
  825. * <tr>
  826. * <td>42257A</td>
  827. * <td>02/2014</td>
  828. * <td>Initial release</td>
  829. * </tr>
  830. * </table>
  831. */
  832. #ifdef __cplusplus
  833. }
  834. #endif
  835. #endif /* DMA_H_INCLUDED */