Christina Quast | 53b2105 | 2014-12-09 15:34:35 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2009, Atmel Corporation |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the disclaimer below. |
| 13 | * |
| 14 | * Atmel's name may not be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * ---------------------------------------------------------------------------- |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * \file |
| 32 | * |
| 33 | * \section Purpose |
| 34 | * |
| 35 | * Interface for configuration the Analog-to-Digital Converter (ADC) peripheral. |
| 36 | * |
| 37 | * \section Usage |
| 38 | * |
| 39 | * -# Configurate the pins for ADC |
| 40 | * -# Initialize the ADC with ADC_Initialize(). |
| 41 | * -# Select the active channel using ADC_EnableChannel() |
| 42 | * -# Start the conversion with ADC_StartConversion() |
| 43 | * -# Wait the end of the conversion by polling status with ADC_GetStatus() |
| 44 | * -# Finally, get the converted data using ADC_GetConvertedData() |
| 45 | * |
| 46 | */ |
| 47 | #ifndef _ADC_ |
| 48 | #define _ADC_ |
| 49 | |
| 50 | /*---------------------------------------------------------------------------- |
| 51 | * Headers |
| 52 | *----------------------------------------------------------------------------*/ |
| 53 | #include "chip.h" |
| 54 | |
| 55 | #include <assert.h> |
| 56 | #include <stdint.h> |
| 57 | |
| 58 | /*------------------------------------------------------------------------------ |
| 59 | * Definitions |
| 60 | *------------------------------------------------------------------------------*/ |
| 61 | /* SAM3S */ |
| 62 | #define ADC_FREQ_MAX 20000000 |
| 63 | #define ADC_FREQ_MIN 1000000 |
| 64 | |
| 65 | #define ADC_STARTUP_NORM 40 |
| 66 | #define ADC_STARTUP_FAST 12 |
| 67 | |
| 68 | #define ADC_CHANNEL_0 0 |
| 69 | #define ADC_CHANNEL_1 1 |
| 70 | #define ADC_CHANNEL_2 2 |
| 71 | #define ADC_CHANNEL_3 3 |
| 72 | #define ADC_CHANNEL_4 4 |
| 73 | #define ADC_CHANNEL_5 5 |
| 74 | #define ADC_CHANNEL_6 6 |
| 75 | #define ADC_CHANNEL_7 7 |
| 76 | #define ADC_CHANNEL_8 8 |
| 77 | #define ADC_CHANNEL_9 9 |
| 78 | #define ADC_CHANNEL_10 10 |
| 79 | #define ADC_CHANNEL_11 11 |
| 80 | #define ADC_CHANNEL_12 12 |
| 81 | #define ADC_CHANNEL_13 13 |
| 82 | #define ADC_CHANNEL_14 14 |
| 83 | #define ADC_CHANNEL_15 15 |
| 84 | |
| 85 | #ifdef __cplusplus |
| 86 | extern "C" { |
| 87 | #endif |
| 88 | |
| 89 | /*------------------------------------------------------------------------------ |
| 90 | * Macros function of register access |
| 91 | *------------------------------------------------------------------------------*/ |
| 92 | |
| 93 | #define ADC_GetModeReg( pAdc ) ((pAdc)->ADC_MR) |
| 94 | |
| 95 | #define ADC_StartConversion( pAdc ) ((pAdc)->ADC_CR = ADC_CR_START) |
| 96 | |
| 97 | |
| 98 | #define ADC_EnableChannel( pAdc, channel ) {\ |
| 99 | assert( channel < 16 ) ;\ |
| 100 | (pAdc)->ADC_CHER = (1 << (channel));\ |
| 101 | } |
| 102 | |
| 103 | #define ADC_DisableChannel(pAdc, channel) {\ |
| 104 | assert( (channel) < 16 ) ;\ |
| 105 | (pAdc)->ADC_CHDR = (1 << (channel));\ |
| 106 | } |
| 107 | |
| 108 | #define ADC_EnableIt(pAdc, dwMode) {\ |
| 109 | (pAdc)->ADC_IER = (dwMode);\ |
| 110 | } |
| 111 | |
| 112 | #define ADC_DisableIt(pAdc, dwMode) {\ |
| 113 | (pAdc)->ADC_IDR = (dwMode);\ |
| 114 | } |
| 115 | |
| 116 | #define ADC_EnableTS(pAdc,dwMode) {\ |
| 117 | (pAdc)->ADC_ACR |= dwMode;\ |
| 118 | } |
| 119 | |
| 120 | #define ADC_EnableDataReadyIt(pAdc) ((pAdc)->ADC_IER = AT91C_ADC_DRDY) |
| 121 | |
| 122 | #define ADC_GetStatus(pAdc) ((pAdc)->ADC_ISR) |
| 123 | |
| 124 | #define ADC_GetCompareMode(pAdc) (((pAdc)->ADC_EMR)& (ADC_EMR_CMPMODE_Msk)) |
| 125 | |
| 126 | #define ADC_GetChannelStatus(pAdc) ((pAdc)->ADC_CHSR) |
| 127 | |
| 128 | #define ADC_GetInterruptMaskStatus(pAdc) ((pAdc)->ADC_IMR) |
| 129 | |
| 130 | #define ADC_GetLastConvertedData(pAdc) ((pAdc)->ADC_LCDR) |
| 131 | |
| 132 | /*------------------------------------------------------------------------------ |
| 133 | * Exported functions |
| 134 | *------------------------------------------------------------------------------*/ |
| 135 | extern void ADC_Initialize( Adc* pAdc, uint32_t idAdc ); |
| 136 | extern void ADC_CfgTiming( Adc* pAdc, uint32_t tracking, uint32_t settling, uint32_t transfer ); |
| 137 | extern void ADC_cfgFrequency( Adc* pAdc, uint32_t startup, uint32_t prescal ); |
| 138 | extern void ADC_CfgTrigering( Adc* pAdc, uint32_t trgEn, uint32_t trgSel, uint32_t freeRun ); |
| 139 | extern void ADC_CfgLowRes( Adc* pAdc, uint32_t resolution ); |
| 140 | extern void ADC_CfgPowerSave( Adc* pAdc, uint32_t sleep, uint32_t fwup ); |
| 141 | extern void ADC_CfgChannelMode( Adc* pAdc, uint32_t useq, uint32_t anach ); |
| 142 | extern void ADC_check( Adc* pAdc, uint32_t mck_freq ); |
| 143 | |
| 144 | extern uint32_t ADC_GetConvertedData( Adc* pAdc, uint32_t dwChannel ) ; |
| 145 | extern void ADC_SetCompareChannel( Adc* pAdc, uint32_t dwChannel ) ; |
| 146 | extern void ADC_SetCompareMode( Adc* pAdc, uint32_t dwMode ) ; |
| 147 | extern void ADC_SetComparisonWindow( Adc* pAdc, uint32_t dwHi_Lo ) ; |
| 148 | extern uint32_t ADC_IsInterruptMasked( Adc* pAdc, uint32_t dwFlag ) ; |
| 149 | extern uint32_t ADC_IsStatusSet( Adc* pAdc, uint32_t dwFlag ) ; |
| 150 | extern uint32_t ADC_IsChannelInterruptStatusSet( uint32_t adc_sr, uint32_t dwChannel ) ; |
| 151 | extern uint32_t ADC_ReadBuffer( Adc* pADC, int16_t *pwBuffer, uint32_t dwSize ) ; |
| 152 | |
| 153 | #ifdef __cplusplus |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | #endif /* #ifndef _ADC_ */ |