mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

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

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Child:
592:a274ee790e56
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

Full URL: https://github.com/mbedmicro/mbed/commit/d5b4d2ab9c47edb4dc5776e7177b0c2263459081/

Initial version of drivers for SAMR21

Who changed what in which revision?

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