dma_crc.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * \file
  3. *
  4. * \brief SAM DMA cyclic redundancy check (CRC) Driver
  5. *
  6. * Copyright (C) 2014-2015 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_CRC_H_INCLUDED
  47. #define DMA_CRC_H_INCLUDED
  48. #include <compiler.h>
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /** DMA channel n offset. */
  53. #define DMA_CRC_CHANNEL_N_OFFSET 0x20
  54. /** CRC Polynomial Type. */
  55. enum crc_polynomial_type {
  56. /** CRC16 (CRC-CCITT). */
  57. CRC_TYPE_16,
  58. /** CRC32 (IEEE 802.3). */
  59. CRC_TYPE_32,
  60. };
  61. /** CRC Beat Type. */
  62. enum crc_beat_size {
  63. /** Byte bus access. */
  64. CRC_BEAT_SIZE_BYTE,
  65. /** Half-word bus access. */
  66. CRC_BEAT_SIZE_HWORD,
  67. /** Word bus access. */
  68. CRC_BEAT_SIZE_WORD,
  69. };
  70. /** Configurations for CRC calculation. */
  71. struct dma_crc_config {
  72. /** CRC polynomial type. */
  73. enum crc_polynomial_type type;
  74. /** CRC beat size. */
  75. enum crc_beat_size size;
  76. };
  77. /**
  78. * \brief Get DMA CRC default configurations.
  79. *
  80. * The default configuration is as follows:
  81. * \li Polynomial type is set to CRC-16(CRC-CCITT)
  82. * \li CRC Beat size: BYTE
  83. *
  84. * \param[in] config default configurations
  85. */
  86. static inline void dma_crc_get_config_defaults(struct dma_crc_config *config)
  87. {
  88. Assert(config);
  89. config->type = CRC_TYPE_16;
  90. config->size = CRC_BEAT_SIZE_BYTE;
  91. }
  92. /**
  93. * \brief Enable DMA CRC module with an DMA channel.
  94. *
  95. * This function enables a CRC calculation with an allocated DMA channel. This channel ID
  96. * can be gotten from a successful \ref dma_allocate.
  97. *
  98. * \param[in] channel_id DMA channel expected with CRC calculation
  99. * \param[in] config CRC calculation configurations
  100. *
  101. * \return Status of the DMC CRC.
  102. * \retval STATUS_OK Get the DMA CRC module
  103. * \retval STATUS_BUSY DMA CRC module is already taken and not ready yet
  104. */
  105. static inline enum status_code dma_crc_channel_enable(uint32_t channel_id,
  106. struct dma_crc_config *config)
  107. {
  108. if (DMAC->CRCSTATUS.reg & DMAC_CRCSTATUS_CRCBUSY) {
  109. return STATUS_BUSY;
  110. }
  111. DMAC->CRCCTRL.reg = DMAC_CRCCTRL_CRCBEATSIZE(config->size) |
  112. DMAC_CRCCTRL_CRCPOLY(config->type) |
  113. DMAC_CRCCTRL_CRCSRC(channel_id+DMA_CRC_CHANNEL_N_OFFSET);
  114. DMAC->CTRL.reg |= DMAC_CTRL_CRCENABLE;
  115. return STATUS_OK;
  116. }
  117. /**
  118. * \brief Disable DMA CRC module.
  119. *
  120. */
  121. static inline void dma_crc_disable(void)
  122. {
  123. DMAC->CTRL.reg &= ~DMAC_CTRL_CRCENABLE;
  124. DMAC->CRCCTRL.reg = 0;
  125. }
  126. /**
  127. * \brief Get DMA CRC checksum value.
  128. *
  129. * \return Calculated CRC checksum.
  130. */
  131. static inline uint32_t dma_crc_get_checksum(void)
  132. {
  133. if (DMAC->CRCCTRL.bit.CRCSRC == DMAC_CRCCTRL_CRCSRC_IO_Val) {
  134. DMAC->CRCSTATUS.reg = DMAC_CRCSTATUS_CRCBUSY;
  135. }
  136. return DMAC->CRCCHKSUM.reg;
  137. }
  138. /**
  139. * \brief Enable DMA CRC module with I/O.
  140. *
  141. * This function enables a CRC calculation with I/O mode.
  142. *
  143. * \param[in] config CRC calculation configurations.
  144. *
  145. * \return Status of the DMC CRC.
  146. * \retval STATUS_OK Get the DMA CRC module
  147. * \retval STATUS_BUSY DMA CRC module is already taken and not ready yet
  148. */
  149. static inline enum status_code dma_crc_io_enable(
  150. struct dma_crc_config *config)
  151. {
  152. if (DMAC->CRCSTATUS.reg & DMAC_CRCSTATUS_CRCBUSY) {
  153. return STATUS_BUSY;
  154. }
  155. if (DMAC->CTRL.reg & DMAC_CTRL_CRCENABLE) {
  156. return STATUS_BUSY;
  157. }
  158. DMAC->CRCCTRL.reg = DMAC_CRCCTRL_CRCBEATSIZE(config->size) |
  159. DMAC_CRCCTRL_CRCPOLY(config->type) |
  160. DMAC_CRCCTRL_CRCSRC_IO;
  161. if (config->type == CRC_TYPE_32) {
  162. DMAC->CRCCHKSUM.reg = 0xFFFFFFFF;
  163. }
  164. DMAC->CTRL.reg |= DMAC_CTRL_CRCENABLE;
  165. return STATUS_OK;
  166. }
  167. /**
  168. * \brief Calculate CRC with I/O.
  169. *
  170. * This function calculate the CRC of the input data buffer.
  171. *
  172. * \param[in] buffer CRC Pointer to calculation buffer
  173. * \param[in] total_beat_size Total beat size to be calculated
  174. *
  175. * \return Calculated CRC checksum value.
  176. */
  177. static inline void dma_crc_io_calculation(void *buffer,
  178. uint32_t total_beat_size)
  179. {
  180. uint32_t counter = total_beat_size;
  181. uint8_t *buffer_8;
  182. uint16_t *buffer_16;
  183. uint32_t *buffer_32;
  184. for (counter=0; counter<total_beat_size; counter++) {
  185. if (DMAC->CRCCTRL.bit.CRCBEATSIZE == CRC_BEAT_SIZE_BYTE) {
  186. buffer_8 = buffer;
  187. DMAC->CRCDATAIN.reg = buffer_8[counter];
  188. } else if (DMAC->CRCCTRL.bit.CRCBEATSIZE == CRC_BEAT_SIZE_HWORD) {
  189. buffer_16 = buffer;
  190. DMAC->CRCDATAIN.reg = buffer_16[counter];
  191. } else if (DMAC->CRCCTRL.bit.CRCBEATSIZE == CRC_BEAT_SIZE_WORD) {
  192. buffer_32 = buffer;
  193. DMAC->CRCDATAIN.reg = buffer_32[counter];
  194. }
  195. /* Wait several cycle to make sure CRC complete */
  196. nop();
  197. nop();
  198. nop();
  199. nop();
  200. }
  201. }
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif /* DMA_CRC_H_INCLUDED */