The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Thu Nov 08 11:45:42 2018 +0000
Revision:
171:3a7713b1edbc
Parent:
TARGET_SAMR21G18A/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/dma/dma_crc.h@111:4336505e4b1c
mbed library. Release version 164

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 111:4336505e4b1c 1 /**
Kojto 111:4336505e4b1c 2 * \file
Kojto 111:4336505e4b1c 3 *
Kojto 111:4336505e4b1c 4 * \brief SAM DMA cyclic redundancy check (CRC) Driver
Kojto 111:4336505e4b1c 5 *
Kojto 111:4336505e4b1c 6 * Copyright (C) 2014-2015 Atmel Corporation. All rights reserved.
Kojto 111:4336505e4b1c 7 *
Kojto 111:4336505e4b1c 8 * \asf_license_start
Kojto 111:4336505e4b1c 9 *
Kojto 111:4336505e4b1c 10 * \page License
Kojto 111:4336505e4b1c 11 *
Kojto 111:4336505e4b1c 12 * Redistribution and use in source and binary forms, with or without
Kojto 111:4336505e4b1c 13 * modification, are permitted provided that the following conditions are met:
Kojto 111:4336505e4b1c 14 *
Kojto 111:4336505e4b1c 15 * 1. Redistributions of source code must retain the above copyright notice,
Kojto 111:4336505e4b1c 16 * this list of conditions and the following disclaimer.
Kojto 111:4336505e4b1c 17 *
Kojto 111:4336505e4b1c 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Kojto 111:4336505e4b1c 19 * this list of conditions and the following disclaimer in the documentation
Kojto 111:4336505e4b1c 20 * and/or other materials provided with the distribution.
Kojto 111:4336505e4b1c 21 *
Kojto 111:4336505e4b1c 22 * 3. The name of Atmel may not be used to endorse or promote products derived
Kojto 111:4336505e4b1c 23 * from this software without specific prior written permission.
Kojto 111:4336505e4b1c 24 *
Kojto 111:4336505e4b1c 25 * 4. This software may only be redistributed and used in connection with an
Kojto 111:4336505e4b1c 26 * Atmel microcontroller product.
Kojto 111:4336505e4b1c 27 *
Kojto 111:4336505e4b1c 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
Kojto 111:4336505e4b1c 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Kojto 111:4336505e4b1c 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
Kojto 111:4336505e4b1c 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
Kojto 111:4336505e4b1c 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Kojto 111:4336505e4b1c 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Kojto 111:4336505e4b1c 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Kojto 111:4336505e4b1c 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Kojto 111:4336505e4b1c 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
Kojto 111:4336505e4b1c 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Kojto 111:4336505e4b1c 38 * POSSIBILITY OF SUCH DAMAGE.
Kojto 111:4336505e4b1c 39 *
Kojto 111:4336505e4b1c 40 * \asf_license_stop
Kojto 111:4336505e4b1c 41 *
Kojto 111:4336505e4b1c 42 */
Kojto 111:4336505e4b1c 43 /*
Kojto 111:4336505e4b1c 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
Kojto 111:4336505e4b1c 45 */
Kojto 111:4336505e4b1c 46 #ifndef DMA_CRC_H_INCLUDED
Kojto 111:4336505e4b1c 47 #define DMA_CRC_H_INCLUDED
Kojto 111:4336505e4b1c 48
Kojto 111:4336505e4b1c 49 #include <compiler.h>
Kojto 111:4336505e4b1c 50
Kojto 111:4336505e4b1c 51 #ifdef __cplusplus
Kojto 111:4336505e4b1c 52 extern "C" {
Kojto 111:4336505e4b1c 53 #endif
Kojto 111:4336505e4b1c 54
Kojto 111:4336505e4b1c 55 /** DMA channel n offset. */
Kojto 111:4336505e4b1c 56 #define DMA_CRC_CHANNEL_N_OFFSET 0x20
Kojto 111:4336505e4b1c 57
Kojto 111:4336505e4b1c 58 /** CRC Polynomial Type. */
Kojto 111:4336505e4b1c 59 enum crc_polynomial_type {
Kojto 111:4336505e4b1c 60 /** CRC16 (CRC-CCITT). */
Kojto 111:4336505e4b1c 61 CRC_TYPE_16,
Kojto 111:4336505e4b1c 62 /** CRC32 (IEEE 802.3). */
Kojto 111:4336505e4b1c 63 CRC_TYPE_32,
Kojto 111:4336505e4b1c 64 };
Kojto 111:4336505e4b1c 65
Kojto 111:4336505e4b1c 66 /** CRC Beat Type. */
Kojto 111:4336505e4b1c 67 enum crc_beat_size {
Kojto 111:4336505e4b1c 68 /** Byte bus access. */
Kojto 111:4336505e4b1c 69 CRC_BEAT_SIZE_BYTE,
Kojto 111:4336505e4b1c 70 /** Half-word bus access. */
Kojto 111:4336505e4b1c 71 CRC_BEAT_SIZE_HWORD,
Kojto 111:4336505e4b1c 72 /** Word bus access. */
Kojto 111:4336505e4b1c 73 CRC_BEAT_SIZE_WORD,
Kojto 111:4336505e4b1c 74 };
Kojto 111:4336505e4b1c 75
Kojto 111:4336505e4b1c 76 /** Configurations for CRC calculation. */
Kojto 111:4336505e4b1c 77 struct dma_crc_config {
Kojto 111:4336505e4b1c 78 /** CRC polynomial type. */
Kojto 111:4336505e4b1c 79 enum crc_polynomial_type type;
Kojto 111:4336505e4b1c 80 /** CRC beat size. */
Kojto 111:4336505e4b1c 81 enum crc_beat_size size;
Kojto 111:4336505e4b1c 82 };
Kojto 111:4336505e4b1c 83
Kojto 111:4336505e4b1c 84 /**
Kojto 111:4336505e4b1c 85 * \brief Get DMA CRC default configurations.
Kojto 111:4336505e4b1c 86 *
Kojto 111:4336505e4b1c 87 * The default configuration is as follows:
Kojto 111:4336505e4b1c 88 * \li Polynomial type is set to CRC-16(CRC-CCITT)
Kojto 111:4336505e4b1c 89 * \li CRC Beat size: BYTE
Kojto 111:4336505e4b1c 90 *
Kojto 111:4336505e4b1c 91 * \param[in] config default configurations
Kojto 111:4336505e4b1c 92 */
Kojto 111:4336505e4b1c 93 static inline void dma_crc_get_config_defaults(struct dma_crc_config *config)
Kojto 111:4336505e4b1c 94 {
Kojto 111:4336505e4b1c 95 Assert(config);
Kojto 111:4336505e4b1c 96
Kojto 111:4336505e4b1c 97 config->type = CRC_TYPE_16;
Kojto 111:4336505e4b1c 98 config->size = CRC_BEAT_SIZE_BYTE;
Kojto 111:4336505e4b1c 99 }
Kojto 111:4336505e4b1c 100
Kojto 111:4336505e4b1c 101 /**
Kojto 111:4336505e4b1c 102 * \brief Enable DMA CRC module with an DMA channel.
Kojto 111:4336505e4b1c 103 *
Kojto 111:4336505e4b1c 104 * This function enables a CRC calculation with an allocated DMA channel. This channel ID
Kojto 111:4336505e4b1c 105 * can be gotten from a successful \ref dma_allocate.
Kojto 111:4336505e4b1c 106 *
Kojto 111:4336505e4b1c 107 * \param[in] channel_id DMA channel expected with CRC calculation
Kojto 111:4336505e4b1c 108 * \param[in] config CRC calculation configurations
Kojto 111:4336505e4b1c 109 *
Kojto 111:4336505e4b1c 110 * \return Status of the DMC CRC.
Kojto 111:4336505e4b1c 111 * \retval STATUS_OK Get the DMA CRC module
Kojto 111:4336505e4b1c 112 * \retval STATUS_BUSY DMA CRC module is already taken and not ready yet
Kojto 111:4336505e4b1c 113 */
Kojto 111:4336505e4b1c 114 static inline enum status_code dma_crc_channel_enable(uint32_t channel_id,
Kojto 111:4336505e4b1c 115 struct dma_crc_config *config)
Kojto 111:4336505e4b1c 116 {
Kojto 111:4336505e4b1c 117 if (DMAC->CRCSTATUS.reg & DMAC_CRCSTATUS_CRCBUSY) {
Kojto 111:4336505e4b1c 118 return STATUS_BUSY;
Kojto 111:4336505e4b1c 119 }
Kojto 111:4336505e4b1c 120
Kojto 111:4336505e4b1c 121 DMAC->CRCCTRL.reg = DMAC_CRCCTRL_CRCBEATSIZE(config->size) |
Kojto 111:4336505e4b1c 122 DMAC_CRCCTRL_CRCPOLY(config->type) |
Kojto 111:4336505e4b1c 123 DMAC_CRCCTRL_CRCSRC(channel_id+DMA_CRC_CHANNEL_N_OFFSET);
Kojto 111:4336505e4b1c 124
Kojto 111:4336505e4b1c 125 DMAC->CTRL.reg |= DMAC_CTRL_CRCENABLE;
Kojto 111:4336505e4b1c 126
Kojto 111:4336505e4b1c 127 return STATUS_OK;
Kojto 111:4336505e4b1c 128 }
Kojto 111:4336505e4b1c 129
Kojto 111:4336505e4b1c 130 /**
Kojto 111:4336505e4b1c 131 * \brief Disable DMA CRC module.
Kojto 111:4336505e4b1c 132 *
Kojto 111:4336505e4b1c 133 */
Kojto 111:4336505e4b1c 134 static inline void dma_crc_disable(void)
Kojto 111:4336505e4b1c 135 {
Kojto 111:4336505e4b1c 136 DMAC->CTRL.reg &= ~DMAC_CTRL_CRCENABLE;
Kojto 111:4336505e4b1c 137 DMAC->CRCCTRL.reg = 0;
Kojto 111:4336505e4b1c 138 }
Kojto 111:4336505e4b1c 139
Kojto 111:4336505e4b1c 140 /**
Kojto 111:4336505e4b1c 141 * \brief Get DMA CRC checksum value.
Kojto 111:4336505e4b1c 142 *
Kojto 111:4336505e4b1c 143 * \return Calculated CRC checksum.
Kojto 111:4336505e4b1c 144 */
Kojto 111:4336505e4b1c 145 static inline uint32_t dma_crc_get_checksum(void)
Kojto 111:4336505e4b1c 146 {
Kojto 111:4336505e4b1c 147 if (DMAC->CRCCTRL.bit.CRCSRC == DMAC_CRCCTRL_CRCSRC_IO_Val) {
Kojto 111:4336505e4b1c 148 DMAC->CRCSTATUS.reg = DMAC_CRCSTATUS_CRCBUSY;
Kojto 111:4336505e4b1c 149 }
Kojto 111:4336505e4b1c 150
Kojto 111:4336505e4b1c 151 return DMAC->CRCCHKSUM.reg;
Kojto 111:4336505e4b1c 152 }
Kojto 111:4336505e4b1c 153
Kojto 111:4336505e4b1c 154 /**
Kojto 111:4336505e4b1c 155 * \brief Enable DMA CRC module with I/O.
Kojto 111:4336505e4b1c 156 *
Kojto 111:4336505e4b1c 157 * This function enables a CRC calculation with I/O mode.
Kojto 111:4336505e4b1c 158 *
Kojto 111:4336505e4b1c 159 * \param[in] config CRC calculation configurations.
Kojto 111:4336505e4b1c 160 *
Kojto 111:4336505e4b1c 161 * \return Status of the DMC CRC.
Kojto 111:4336505e4b1c 162 * \retval STATUS_OK Get the DMA CRC module
Kojto 111:4336505e4b1c 163 * \retval STATUS_BUSY DMA CRC module is already taken and not ready yet
Kojto 111:4336505e4b1c 164 */
Kojto 111:4336505e4b1c 165 static inline enum status_code dma_crc_io_enable(
Kojto 111:4336505e4b1c 166 struct dma_crc_config *config)
Kojto 111:4336505e4b1c 167 {
Kojto 111:4336505e4b1c 168 if (DMAC->CRCSTATUS.reg & DMAC_CRCSTATUS_CRCBUSY) {
Kojto 111:4336505e4b1c 169 return STATUS_BUSY;
Kojto 111:4336505e4b1c 170 }
Kojto 111:4336505e4b1c 171
Kojto 111:4336505e4b1c 172 if (DMAC->CTRL.reg & DMAC_CTRL_CRCENABLE) {
Kojto 111:4336505e4b1c 173 return STATUS_BUSY;
Kojto 111:4336505e4b1c 174 }
Kojto 111:4336505e4b1c 175
Kojto 111:4336505e4b1c 176 DMAC->CRCCTRL.reg = DMAC_CRCCTRL_CRCBEATSIZE(config->size) |
Kojto 111:4336505e4b1c 177 DMAC_CRCCTRL_CRCPOLY(config->type) |
Kojto 111:4336505e4b1c 178 DMAC_CRCCTRL_CRCSRC_IO;
Kojto 111:4336505e4b1c 179
Kojto 111:4336505e4b1c 180 if (config->type == CRC_TYPE_32) {
Kojto 111:4336505e4b1c 181 DMAC->CRCCHKSUM.reg = 0xFFFFFFFF;
Kojto 111:4336505e4b1c 182 }
Kojto 111:4336505e4b1c 183
Kojto 111:4336505e4b1c 184 DMAC->CTRL.reg |= DMAC_CTRL_CRCENABLE;
Kojto 111:4336505e4b1c 185
Kojto 111:4336505e4b1c 186 return STATUS_OK;
Kojto 111:4336505e4b1c 187 }
Kojto 111:4336505e4b1c 188
Kojto 111:4336505e4b1c 189 /**
Kojto 111:4336505e4b1c 190 * \brief Calculate CRC with I/O.
Kojto 111:4336505e4b1c 191 *
Kojto 111:4336505e4b1c 192 * This function calculate the CRC of the input data buffer.
Kojto 111:4336505e4b1c 193 *
Kojto 111:4336505e4b1c 194 * \param[in] buffer CRC Pointer to calculation buffer
Kojto 111:4336505e4b1c 195 * \param[in] total_beat_size Total beat size to be calculated
Kojto 111:4336505e4b1c 196 *
Kojto 111:4336505e4b1c 197 * \return Calculated CRC checksum value.
Kojto 111:4336505e4b1c 198 */
Kojto 111:4336505e4b1c 199 static inline void dma_crc_io_calculation(void *buffer,
Kojto 111:4336505e4b1c 200 uint32_t total_beat_size)
Kojto 111:4336505e4b1c 201 {
Kojto 111:4336505e4b1c 202 uint32_t counter = total_beat_size;
Kojto 111:4336505e4b1c 203 uint8_t *buffer_8;
Kojto 111:4336505e4b1c 204 uint16_t *buffer_16;
Kojto 111:4336505e4b1c 205 uint32_t *buffer_32;
Kojto 111:4336505e4b1c 206
Kojto 111:4336505e4b1c 207 for (counter=0; counter<total_beat_size; counter++) {
Kojto 111:4336505e4b1c 208 if (DMAC->CRCCTRL.bit.CRCBEATSIZE == CRC_BEAT_SIZE_BYTE) {
Kojto 111:4336505e4b1c 209 buffer_8 = buffer;
Kojto 111:4336505e4b1c 210 DMAC->CRCDATAIN.reg = buffer_8[counter];
Kojto 111:4336505e4b1c 211 } else if (DMAC->CRCCTRL.bit.CRCBEATSIZE == CRC_BEAT_SIZE_HWORD) {
Kojto 111:4336505e4b1c 212 buffer_16 = buffer;
Kojto 111:4336505e4b1c 213 DMAC->CRCDATAIN.reg = buffer_16[counter];
Kojto 111:4336505e4b1c 214 } else if (DMAC->CRCCTRL.bit.CRCBEATSIZE == CRC_BEAT_SIZE_WORD) {
Kojto 111:4336505e4b1c 215 buffer_32 = buffer;
Kojto 111:4336505e4b1c 216 DMAC->CRCDATAIN.reg = buffer_32[counter];
Kojto 111:4336505e4b1c 217 }
Kojto 111:4336505e4b1c 218 /* Wait several cycle to make sure CRC complete */
Kojto 111:4336505e4b1c 219 nop();
Kojto 111:4336505e4b1c 220 nop();
Kojto 111:4336505e4b1c 221 nop();
Kojto 111:4336505e4b1c 222 nop();
Kojto 111:4336505e4b1c 223 }
Kojto 111:4336505e4b1c 224 }
Kojto 111:4336505e4b1c 225
Kojto 111:4336505e4b1c 226 #ifdef __cplusplus
Kojto 111:4336505e4b1c 227 }
Kojto 111:4336505e4b1c 228 #endif
Kojto 111:4336505e4b1c 229
Kojto 111:4336505e4b1c 230 #endif /* DMA_CRC_H_INCLUDED */