test

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
168:e84263d55307
Child:
174:e131a1973e81
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 168:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 168:e84263d55307 2 * Copyright (c) 2013-2016 Realtek Semiconductor Corp.
AnnaBridge 168:e84263d55307 3 *
AnnaBridge 168:e84263d55307 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 168:e84263d55307 5 * you may not use this file except in compliance with the License.
AnnaBridge 168:e84263d55307 6 * You may obtain a copy of the License at
AnnaBridge 168:e84263d55307 7 *
AnnaBridge 168:e84263d55307 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 168:e84263d55307 9 *
AnnaBridge 168:e84263d55307 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 168:e84263d55307 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 168:e84263d55307 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 168:e84263d55307 13 * See the License for the specific language governing permissions and
AnnaBridge 168:e84263d55307 14 * limitations under the License.
AnnaBridge 168:e84263d55307 15 */
AnnaBridge 168:e84263d55307 16
AnnaBridge 168:e84263d55307 17 #include "objects.h"
AnnaBridge 168:e84263d55307 18 #include "PinNames.h"
AnnaBridge 168:e84263d55307 19 #include "hal_adc.h"
AnnaBridge 168:e84263d55307 20 #include "analogin_api.h"
AnnaBridge 168:e84263d55307 21
AnnaBridge 168:e84263d55307 22
AnnaBridge 168:e84263d55307 23
AnnaBridge 168:e84263d55307 24 #if CONFIG_ADC_EN
AnnaBridge 168:e84263d55307 25 #include "pinmap.h"
AnnaBridge 168:e84263d55307 26
AnnaBridge 168:e84263d55307 27
AnnaBridge 168:e84263d55307 28 extern u32 ConfigDebugErr;
AnnaBridge 168:e84263d55307 29 extern u32 ConfigDebuginfo;
AnnaBridge 168:e84263d55307 30
AnnaBridge 168:e84263d55307 31
AnnaBridge 168:e84263d55307 32 void analogin_init (analogin_t *obj, PinName pin)
AnnaBridge 168:e84263d55307 33 {
AnnaBridge 168:e84263d55307 34 uint32_t adc_idx;
AnnaBridge 168:e84263d55307 35 PSAL_ADC_MNGT_ADPT pSalADCMngtAdpt = NULL;
AnnaBridge 168:e84263d55307 36 PSAL_ADC_USERCB_ADPT pSalADCUserCBAdpt = NULL;
AnnaBridge 168:e84263d55307 37 PSAL_ADC_HND pSalADCHND = NULL;
AnnaBridge 168:e84263d55307 38
AnnaBridge 168:e84263d55307 39 HAL_ADC_INIT_DAT HalADCInitDataTmp;
AnnaBridge 168:e84263d55307 40 PHAL_ADC_INIT_DAT pHalADCInitDataTmp = &HalADCInitDataTmp;
AnnaBridge 168:e84263d55307 41 /* To backup user config first */
AnnaBridge 168:e84263d55307 42
AnnaBridge 168:e84263d55307 43 _memset(&(obj->HalADCInitData), 0, sizeof(HAL_ADC_INIT_DAT));
AnnaBridge 168:e84263d55307 44 _memcpy(pHalADCInitDataTmp, &(obj->HalADCInitData), sizeof(HAL_ADC_INIT_DAT));
AnnaBridge 168:e84263d55307 45 _memset(obj, 0x00, sizeof(analogin_t));
AnnaBridge 168:e84263d55307 46
AnnaBridge 168:e84263d55307 47 ConfigDebugErr &= (~(_DBG_ADC_|_DBG_GDMA_));
AnnaBridge 168:e84263d55307 48 ConfigDebugInfo&= (~(_DBG_ADC_|_DBG_GDMA_));
AnnaBridge 168:e84263d55307 49
AnnaBridge 168:e84263d55307 50 adc_idx = pin & 0x0F;
AnnaBridge 168:e84263d55307 51
AnnaBridge 168:e84263d55307 52 /* Get I2C device handler */
AnnaBridge 168:e84263d55307 53 pSalADCMngtAdpt = &(obj->SalADCMngtAdpt);
AnnaBridge 168:e84263d55307 54 pSalADCUserCBAdpt = (PSAL_ADC_USERCB_ADPT)&(obj->SalADCUserCBAdpt);
AnnaBridge 168:e84263d55307 55
AnnaBridge 168:e84263d55307 56 /*To assign the rest pointers*/
AnnaBridge 168:e84263d55307 57 pSalADCMngtAdpt->pSalHndPriv = &(obj->SalADCHndPriv);
AnnaBridge 168:e84263d55307 58 pSalADCMngtAdpt->pSalHndPriv->ppSalADCHnd = (void**)&(pSalADCMngtAdpt->pSalHndPriv);
AnnaBridge 168:e84263d55307 59
AnnaBridge 168:e84263d55307 60 /* To assign the default (ROM) HAL OP initialization function */
AnnaBridge 168:e84263d55307 61 pSalADCMngtAdpt->pHalOpInit = &HalADCOpInit;
AnnaBridge 168:e84263d55307 62
AnnaBridge 168:e84263d55307 63 /* To assign the default (ROM) HAL GDMA OP initialization function */
AnnaBridge 168:e84263d55307 64 pSalADCMngtAdpt->pHalGdmaOpInit = &HalGdmaOpInit;
AnnaBridge 168:e84263d55307 65
AnnaBridge 168:e84263d55307 66 /* To assign the default (ROM) SAL interrupt function */
AnnaBridge 168:e84263d55307 67 pSalADCMngtAdpt->pSalIrqFunc = &ADCISRHandle;
AnnaBridge 168:e84263d55307 68
AnnaBridge 168:e84263d55307 69 /* To assign the default (ROM) SAL DMA TX interrupt function */
AnnaBridge 168:e84263d55307 70 pSalADCMngtAdpt->pSalDMAIrqFunc = &ADCGDMAISRHandle;
AnnaBridge 168:e84263d55307 71
AnnaBridge 168:e84263d55307 72 /* To backup user config first */
AnnaBridge 168:e84263d55307 73 //_memcpy(pHalADCInitDataTmp, &(obj->HalADCInitData), sizeof(HAL_ADC_INIT_DAT));
AnnaBridge 168:e84263d55307 74
AnnaBridge 168:e84263d55307 75 pSalADCMngtAdpt->pHalInitDat = &(obj->HalADCInitData);
AnnaBridge 168:e84263d55307 76 pSalADCMngtAdpt->pHalOp = &(obj->HalADCOp);
AnnaBridge 168:e84263d55307 77 pSalADCMngtAdpt->pIrqHnd = &(obj->ADCIrqHandleDat);
AnnaBridge 168:e84263d55307 78 pSalADCMngtAdpt->pHalGdmaAdp = &(obj->HalADCGdmaAdpt);
AnnaBridge 168:e84263d55307 79 pSalADCMngtAdpt->pHalGdmaOp = &(obj->HalADCGdmaOp);
AnnaBridge 168:e84263d55307 80 pSalADCMngtAdpt->pIrqGdmaHnd = &(obj->ADCGdmaIrqHandleDat);
AnnaBridge 168:e84263d55307 81 pSalADCMngtAdpt->pUserCB = &(obj->SalADCUserCB);
AnnaBridge 168:e84263d55307 82
AnnaBridge 168:e84263d55307 83 /* Assign the private SAL handle to public SAL handle */
AnnaBridge 168:e84263d55307 84 pSalADCHND = &(pSalADCMngtAdpt->pSalHndPriv->SalADCHndPriv);
AnnaBridge 168:e84263d55307 85
AnnaBridge 168:e84263d55307 86 /* Assign the internal HAL initial data pointer to the SAL handle */
AnnaBridge 168:e84263d55307 87 pSalADCHND->pInitDat = pSalADCMngtAdpt->pHalInitDat;
AnnaBridge 168:e84263d55307 88
AnnaBridge 168:e84263d55307 89 /* Assign the internal user callback pointer to the SAL handle */
AnnaBridge 168:e84263d55307 90 pSalADCHND->pUserCB = pSalADCMngtAdpt->pUserCB;
AnnaBridge 168:e84263d55307 91
AnnaBridge 168:e84263d55307 92 /*To assign user callback pointers*/
AnnaBridge 168:e84263d55307 93 pSalADCMngtAdpt->pUserCB->pTXCB = pSalADCUserCBAdpt;
AnnaBridge 168:e84263d55307 94 pSalADCMngtAdpt->pUserCB->pTXCCB = (pSalADCUserCBAdpt+1);
AnnaBridge 168:e84263d55307 95 pSalADCMngtAdpt->pUserCB->pRXCB = (pSalADCUserCBAdpt+2);
AnnaBridge 168:e84263d55307 96 pSalADCMngtAdpt->pUserCB->pRXCCB = (pSalADCUserCBAdpt+3);
AnnaBridge 168:e84263d55307 97 pSalADCMngtAdpt->pUserCB->pRDREQCB = (pSalADCUserCBAdpt+4);
AnnaBridge 168:e84263d55307 98 pSalADCMngtAdpt->pUserCB->pERRCB = (pSalADCUserCBAdpt+5);
AnnaBridge 168:e84263d55307 99 pSalADCMngtAdpt->pUserCB->pDMATXCB = (pSalADCUserCBAdpt+6);
AnnaBridge 168:e84263d55307 100 pSalADCMngtAdpt->pUserCB->pDMATXCCB = (pSalADCUserCBAdpt+7);
AnnaBridge 168:e84263d55307 101 pSalADCMngtAdpt->pUserCB->pDMARXCB = (pSalADCUserCBAdpt+8);
AnnaBridge 168:e84263d55307 102 pSalADCMngtAdpt->pUserCB->pDMARXCCB = (pSalADCUserCBAdpt+9);
AnnaBridge 168:e84263d55307 103
AnnaBridge 168:e84263d55307 104 /* Set ADC Device Number */
AnnaBridge 168:e84263d55307 105 pSalADCHND->DevNum = adc_idx;
AnnaBridge 168:e84263d55307 106
AnnaBridge 168:e84263d55307 107 /* Load ADC default value */
AnnaBridge 168:e84263d55307 108 RtkADCLoadDefault(pSalADCHND);
AnnaBridge 168:e84263d55307 109
AnnaBridge 168:e84263d55307 110 /* Assign ADC Pin Mux */
AnnaBridge 168:e84263d55307 111 pSalADCHND->PinMux = 0;
AnnaBridge 168:e84263d55307 112 pSalADCHND->OpType = ADC_RDREG_TYPE;
AnnaBridge 168:e84263d55307 113
AnnaBridge 168:e84263d55307 114 /* Load user setting */
AnnaBridge 168:e84263d55307 115 if ((pHalADCInitDataTmp->ADCEndian == ADC_DATA_ENDIAN_LITTLE) || (pHalADCInitDataTmp->ADCEndian == ADC_DATA_ENDIAN_BIG)) {
AnnaBridge 168:e84263d55307 116 DBG_8195A("K\n");
AnnaBridge 168:e84263d55307 117 pSalADCHND->pInitDat->ADCEndian = pHalADCInitDataTmp->ADCEndian;
AnnaBridge 168:e84263d55307 118 }
AnnaBridge 168:e84263d55307 119
AnnaBridge 168:e84263d55307 120 if ((pHalADCInitDataTmp->ADCAudioEn != ADC_FEATURE_DISABLED) && (pHalADCInitDataTmp->ADCAudioEn < 2)) {
AnnaBridge 168:e84263d55307 121 DBG_8195A("O\n");
AnnaBridge 168:e84263d55307 122 pSalADCHND->pInitDat->ADCAudioEn = pHalADCInitDataTmp->ADCAudioEn;
AnnaBridge 168:e84263d55307 123 }
AnnaBridge 168:e84263d55307 124
AnnaBridge 168:e84263d55307 125 /* Init ADC now */
AnnaBridge 168:e84263d55307 126 pSalADCHND->pInitDat->ADCBurstSz = 8;
AnnaBridge 168:e84263d55307 127 pSalADCHND->pInitDat->ADCOneShotTD = 8;
AnnaBridge 168:e84263d55307 128 RtkADCInit(pSalADCHND);
AnnaBridge 168:e84263d55307 129 }
AnnaBridge 168:e84263d55307 130
AnnaBridge 168:e84263d55307 131 float analogin_read(analogin_t *obj)
AnnaBridge 168:e84263d55307 132 {
AnnaBridge 168:e84263d55307 133 float value;
AnnaBridge 168:e84263d55307 134 uint32_t AnaloginTmp[2] = {0,0};
AnnaBridge 168:e84263d55307 135 uint32_t AnaloginDatMsk = 0xFFFF;
AnnaBridge 168:e84263d55307 136 uint8_t AnaloginIdx = 0;
AnnaBridge 168:e84263d55307 137 uint32_t AnalogDat = 0;
AnnaBridge 168:e84263d55307 138
AnnaBridge 168:e84263d55307 139 //no auto-calibration implemented yet, uses hard coded calibrate
AnnaBridge 168:e84263d55307 140 uint32_t Offset = 0x2980;
AnnaBridge 168:e84263d55307 141 uint32_t AnalogDatFull = 0xAA00;
AnnaBridge 168:e84263d55307 142
AnnaBridge 168:e84263d55307 143 PSAL_ADC_MNGT_ADPT pSalADCMngtAdpt = NULL;
AnnaBridge 168:e84263d55307 144 PSAL_ADC_HND pSalADCHND = NULL;
AnnaBridge 168:e84263d55307 145
AnnaBridge 168:e84263d55307 146 pSalADCMngtAdpt = &(obj->SalADCMngtAdpt);
AnnaBridge 168:e84263d55307 147 pSalADCHND = &(pSalADCMngtAdpt->pSalHndPriv->SalADCHndPriv);
AnnaBridge 168:e84263d55307 148 AnaloginIdx = pSalADCHND->DevNum;
AnnaBridge 168:e84263d55307 149 RtkADCReceiveBuf(pSalADCHND,&AnaloginTmp[0]);
AnnaBridge 168:e84263d55307 150
AnnaBridge 168:e84263d55307 151 AnaloginDatMsk = (u32)(AnaloginDatMsk<<((u32)(16*(AnaloginIdx&0x01))));
AnnaBridge 168:e84263d55307 152 AnalogDat = AnaloginTmp[(AnaloginIdx/2)];
AnnaBridge 168:e84263d55307 153 AnalogDat = (AnalogDat & AnaloginDatMsk);
AnnaBridge 168:e84263d55307 154 AnalogDat = (AnalogDat>>((u32)(16*(AnaloginIdx&0x01))));
AnnaBridge 168:e84263d55307 155 AnalogDat -= Offset;
AnnaBridge 168:e84263d55307 156
AnnaBridge 168:e84263d55307 157 value = (float)(AnalogDat) / (float)(AnalogDatFull);
AnnaBridge 168:e84263d55307 158 return (float)value;
AnnaBridge 168:e84263d55307 159 }
AnnaBridge 168:e84263d55307 160
AnnaBridge 168:e84263d55307 161 uint16_t analogin_read_u16(analogin_t *obj)
AnnaBridge 168:e84263d55307 162 {
AnnaBridge 168:e84263d55307 163 uint32_t AnaloginTmp[2] = {0,0};
AnnaBridge 168:e84263d55307 164 uint32_t AnaloginDatMsk = 0xFFFF;
AnnaBridge 168:e84263d55307 165 uint8_t AnaloginIdx = 0;
AnnaBridge 168:e84263d55307 166 uint32_t AnalogDat = 0;
AnnaBridge 168:e84263d55307 167
AnnaBridge 168:e84263d55307 168 PSAL_ADC_MNGT_ADPT pSalADCMngtAdpt = NULL;
AnnaBridge 168:e84263d55307 169 PSAL_ADC_HND pSalADCHND = NULL;
AnnaBridge 168:e84263d55307 170
AnnaBridge 168:e84263d55307 171 pSalADCMngtAdpt = &(obj->SalADCMngtAdpt);
AnnaBridge 168:e84263d55307 172 pSalADCHND = &(pSalADCMngtAdpt->pSalHndPriv->SalADCHndPriv);
AnnaBridge 168:e84263d55307 173 AnaloginIdx = pSalADCHND->DevNum;
AnnaBridge 168:e84263d55307 174 RtkADCRxManualRotate(pSalADCHND,&AnaloginTmp[0]);
AnnaBridge 168:e84263d55307 175
AnnaBridge 168:e84263d55307 176 AnaloginDatMsk = (u32)(AnaloginDatMsk<<((u32)(16*(AnaloginIdx&0x01))));
AnnaBridge 168:e84263d55307 177 AnalogDat = AnaloginTmp[(AnaloginIdx/2)];
AnnaBridge 168:e84263d55307 178 AnalogDat = (AnalogDat & AnaloginDatMsk);
AnnaBridge 168:e84263d55307 179 AnalogDat = (AnalogDat>>((u32)(16*(AnaloginIdx&0x01))));
AnnaBridge 168:e84263d55307 180
AnnaBridge 168:e84263d55307 181 return (uint16_t)AnalogDat;
AnnaBridge 168:e84263d55307 182 }
AnnaBridge 168:e84263d55307 183
AnnaBridge 168:e84263d55307 184
AnnaBridge 168:e84263d55307 185 void analogin_deinit(analogin_t *obj)
AnnaBridge 168:e84263d55307 186 {
AnnaBridge 168:e84263d55307 187 PSAL_ADC_MNGT_ADPT pSalADCMngtAdpt = NULL;
AnnaBridge 168:e84263d55307 188 PSAL_ADC_HND pSalADCHND = NULL;
AnnaBridge 168:e84263d55307 189
AnnaBridge 168:e84263d55307 190 pSalADCMngtAdpt = &(obj->SalADCMngtAdpt);
AnnaBridge 168:e84263d55307 191 pSalADCHND = &(pSalADCMngtAdpt->pSalHndPriv->SalADCHndPriv);
AnnaBridge 168:e84263d55307 192
AnnaBridge 168:e84263d55307 193 /* To deinit analogin */
AnnaBridge 168:e84263d55307 194 RtkADCDeInit(pSalADCHND);
AnnaBridge 168:e84263d55307 195 }
AnnaBridge 168:e84263d55307 196 #endif