test
Fork of mbed-dev by
targets/TARGET_Realtek/TARGET_AMEBA/analogout_api.c@178:d650f5d4c87a, 2017-11-08 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Nov 08 13:50:44 2017 +0000
- Revision:
- 178:d650f5d4c87a
- Parent:
- 168:e84263d55307
This updates the lib to the mbed lib v 155
Who changed what in which revision?
User | Revision | Line number | New 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 | #include "objects.h" |
AnnaBridge | 168:e84263d55307 | 17 | #include "analogout_api.h" |
AnnaBridge | 168:e84263d55307 | 18 | |
AnnaBridge | 168:e84263d55307 | 19 | |
AnnaBridge | 168:e84263d55307 | 20 | #if CONFIG_DAC_EN |
AnnaBridge | 168:e84263d55307 | 21 | |
AnnaBridge | 168:e84263d55307 | 22 | #include "cmsis.h" |
AnnaBridge | 168:e84263d55307 | 23 | #include "pinmap.h" |
AnnaBridge | 168:e84263d55307 | 24 | #include <string.h> |
AnnaBridge | 168:e84263d55307 | 25 | |
AnnaBridge | 168:e84263d55307 | 26 | #define DAC_POSITIVE_FULL_SCALE 0x7E0 |
AnnaBridge | 168:e84263d55307 | 27 | #define DAC_NEGATIVE_FULL_SCALE 0x820 |
AnnaBridge | 168:e84263d55307 | 28 | |
AnnaBridge | 168:e84263d55307 | 29 | |
AnnaBridge | 168:e84263d55307 | 30 | /** \brief analogout_init:\n |
AnnaBridge | 168:e84263d55307 | 31 | * to initialize DAC |
AnnaBridge | 168:e84263d55307 | 32 | * |
AnnaBridge | 168:e84263d55307 | 33 | * This function is mainly to initialize a DAC channel. |
AnnaBridge | 168:e84263d55307 | 34 | * \para dac_t *: obj |
AnnaBridge | 168:e84263d55307 | 35 | * \para PinName: pin |
AnnaBridge | 168:e84263d55307 | 36 | */ |
AnnaBridge | 168:e84263d55307 | 37 | void analogout_init(dac_t *obj, PinName pin) |
AnnaBridge | 168:e84263d55307 | 38 | { |
AnnaBridge | 168:e84263d55307 | 39 | uint32_t dac_idx; |
AnnaBridge | 168:e84263d55307 | 40 | uint32_t DacTemp; |
AnnaBridge | 168:e84263d55307 | 41 | PHAL_DAC_INIT_DAT pHalDacInitData = (PHAL_DAC_INIT_DAT)&(obj->DACpara); |
AnnaBridge | 168:e84263d55307 | 42 | dac_idx = pin & 0x0F; |
AnnaBridge | 168:e84263d55307 | 43 | |
AnnaBridge | 168:e84263d55307 | 44 | /* Assign dac index */ |
AnnaBridge | 168:e84263d55307 | 45 | pHalDacInitData->DACIdx = dac_idx; |
AnnaBridge | 168:e84263d55307 | 46 | |
AnnaBridge | 168:e84263d55307 | 47 | pHalDacInitData->DACEn = DAC_DISABLE; |
AnnaBridge | 168:e84263d55307 | 48 | pHalDacInitData->DACDataRate = DAC_DATA_RATE_250K; |
AnnaBridge | 168:e84263d55307 | 49 | pHalDacInitData->DACEndian = DAC_DATA_ENDIAN_LITTLE; |
AnnaBridge | 168:e84263d55307 | 50 | pHalDacInitData->DACBurstSz = 10; |
AnnaBridge | 168:e84263d55307 | 51 | pHalDacInitData->DACDbgSel = DAC_DBG_SEL_DISABLE; |
AnnaBridge | 168:e84263d55307 | 52 | pHalDacInitData->DACDscDbgSel = DAC_DSC_DBG_SEL_DISABLE; |
AnnaBridge | 168:e84263d55307 | 53 | pHalDacInitData->DACBPDsc = DAC_BYPASS_DSC_SEL_DISABLE; |
AnnaBridge | 168:e84263d55307 | 54 | pHalDacInitData->DACDeltaSig = 0; |
AnnaBridge | 168:e84263d55307 | 55 | pHalDacInitData->DACAnaCtrl0 = 0; |
AnnaBridge | 168:e84263d55307 | 56 | pHalDacInitData->DACAnaCtrl1 = 0; |
AnnaBridge | 168:e84263d55307 | 57 | pHalDacInitData->DACIntrMSK = DAC_FEATURE_DISABLED; |
AnnaBridge | 168:e84263d55307 | 58 | |
AnnaBridge | 168:e84263d55307 | 59 | /* DAC Function and Clock Enable*/ |
AnnaBridge | 168:e84263d55307 | 60 | HalDACPinMuxInit(pHalDacInitData); |
AnnaBridge | 168:e84263d55307 | 61 | |
AnnaBridge | 168:e84263d55307 | 62 | HalDACInit8195a(pHalDacInitData); |
AnnaBridge | 168:e84263d55307 | 63 | |
AnnaBridge | 168:e84263d55307 | 64 | HAL_DAC_WRITE32(pHalDacInitData->DACIdx, REG_DAC_INTR_CTRL, |
AnnaBridge | 168:e84263d55307 | 65 | (BIT_DAC_FIFO_FULL_EN | |
AnnaBridge | 168:e84263d55307 | 66 | BIT_DAC_FIFO_OVERFLOW_EN | |
AnnaBridge | 168:e84263d55307 | 67 | BIT_DAC_FIFO_STOP_EN | |
AnnaBridge | 168:e84263d55307 | 68 | BIT_DAC__WRITE_ERROR_EN | |
AnnaBridge | 168:e84263d55307 | 69 | BIT_DAC_DSC_OVERFLOW0_EN | |
AnnaBridge | 168:e84263d55307 | 70 | BIT_DAC_DSC_OVERFLOW1_EN)); |
AnnaBridge | 168:e84263d55307 | 71 | DBG_DAC_INFO("INTR MSK:%x\n", HAL_DAC_READ32(pHalDacInitData->DACIdx,REG_DAC_INTR_CTRL)); |
AnnaBridge | 168:e84263d55307 | 72 | |
AnnaBridge | 168:e84263d55307 | 73 | DacTemp = HAL_DAC_READ32(pHalDacInitData->DACIdx, REG_DAC_ANAPAR_DA1); |
AnnaBridge | 168:e84263d55307 | 74 | DacTemp |= (BIT31); |
AnnaBridge | 168:e84263d55307 | 75 | HAL_DAC_WRITE32(pHalDacInitData->DACIdx, REG_DAC_ANAPAR_DA1, DacTemp); |
AnnaBridge | 168:e84263d55307 | 76 | DBG_DAC_INFO("REG_DAC_ANAPAR_DA1:%08x\n",HAL_DAC_READ32(pHalDacInitData->DACIdx, REG_DAC_ANAPAR_DA1)); |
AnnaBridge | 168:e84263d55307 | 77 | |
AnnaBridge | 168:e84263d55307 | 78 | DacTemp = HAL_DAC_READ32(pHalDacInitData->DACIdx, REG_DAC_CTRL); |
AnnaBridge | 168:e84263d55307 | 79 | DacTemp |= BIT3; |
AnnaBridge | 168:e84263d55307 | 80 | HAL_DAC_WRITE32(pHalDacInitData->DACIdx, REG_DAC_CTRL, DacTemp); |
AnnaBridge | 168:e84263d55307 | 81 | DBG_DAC_INFO("REG_DAC_CTRL:%08x\n",DacTemp); |
AnnaBridge | 168:e84263d55307 | 82 | |
AnnaBridge | 168:e84263d55307 | 83 | pHalDacInitData->DACEn = DAC_ENABLE; |
AnnaBridge | 168:e84263d55307 | 84 | HalDACEnableRtl8195a(pHalDacInitData); |
AnnaBridge | 168:e84263d55307 | 85 | osDelay(6); //hardware needs some time to get ready |
AnnaBridge | 168:e84263d55307 | 86 | } |
AnnaBridge | 168:e84263d55307 | 87 | |
AnnaBridge | 168:e84263d55307 | 88 | /** \brief analogout_free:\n |
AnnaBridge | 168:e84263d55307 | 89 | * to free DAC |
AnnaBridge | 168:e84263d55307 | 90 | * |
AnnaBridge | 168:e84263d55307 | 91 | * This function is mainly to free a DAC channel. |
AnnaBridge | 168:e84263d55307 | 92 | * \para dac_t *: obj |
AnnaBridge | 168:e84263d55307 | 93 | */ |
AnnaBridge | 168:e84263d55307 | 94 | void analogout_free(dac_t *obj) |
AnnaBridge | 168:e84263d55307 | 95 | { |
AnnaBridge | 168:e84263d55307 | 96 | PHAL_DAC_INIT_DAT pHalDacInitData = (PHAL_DAC_INIT_DAT)&(obj->DACpara); |
AnnaBridge | 168:e84263d55307 | 97 | |
AnnaBridge | 168:e84263d55307 | 98 | HalDACPinMuxDeInit(pHalDacInitData); |
AnnaBridge | 168:e84263d55307 | 99 | |
AnnaBridge | 168:e84263d55307 | 100 | pHalDacInitData->DACEn = DAC_DISABLE; |
AnnaBridge | 168:e84263d55307 | 101 | HalDACEnableRtl8195a(pHalDacInitData); |
AnnaBridge | 168:e84263d55307 | 102 | } |
AnnaBridge | 168:e84263d55307 | 103 | |
AnnaBridge | 168:e84263d55307 | 104 | /** \brief analogout_write:\n |
AnnaBridge | 168:e84263d55307 | 105 | * to execute analogout_write |
AnnaBridge | 168:e84263d55307 | 106 | * |
AnnaBridge | 168:e84263d55307 | 107 | * This function is mainly to execute analog output and the value is a ratio. |
AnnaBridge | 168:e84263d55307 | 108 | * The upper/lower bound of DAC register input value is defined by |
AnnaBridge | 168:e84263d55307 | 109 | * DAC_XXXXX_FULL_SCALE. The parameter "value" of this function should be |
AnnaBridge | 168:e84263d55307 | 110 | * transfered to register value. |
AnnaBridge | 168:e84263d55307 | 111 | * |
AnnaBridge | 168:e84263d55307 | 112 | * \para dac_t * : obj |
AnnaBridge | 168:e84263d55307 | 113 | * \para float : value |
AnnaBridge | 168:e84263d55307 | 114 | */ |
AnnaBridge | 168:e84263d55307 | 115 | void analogout_write(dac_t *obj, float value) |
AnnaBridge | 168:e84263d55307 | 116 | { |
AnnaBridge | 168:e84263d55307 | 117 | uint32_t dactemp; |
AnnaBridge | 168:e84263d55307 | 118 | uint16_t dacnegtemp; |
AnnaBridge | 168:e84263d55307 | 119 | PHAL_DAC_INIT_DAT pHalDacInitData = (PHAL_DAC_INIT_DAT)&(obj->DACpara); |
AnnaBridge | 168:e84263d55307 | 120 | |
AnnaBridge | 168:e84263d55307 | 121 | if (value < 0.0f) { |
AnnaBridge | 168:e84263d55307 | 122 | HAL_DAC_WRITE32(pHalDacInitData->DACIdx, REG_DAC0_FIFO_WR, 0x00000000); |
AnnaBridge | 168:e84263d55307 | 123 | } else if (value > 1.0f) { |
AnnaBridge | 168:e84263d55307 | 124 | dactemp = (DAC_POSITIVE_FULL_SCALE<<16) | DAC_POSITIVE_FULL_SCALE; |
AnnaBridge | 168:e84263d55307 | 125 | HAL_DAC_WRITE32(pHalDacInitData->DACIdx, REG_DAC0_FIFO_WR, dactemp); |
AnnaBridge | 168:e84263d55307 | 126 | } else { |
AnnaBridge | 168:e84263d55307 | 127 | if (value >= 0.5) { |
AnnaBridge | 168:e84263d55307 | 128 | dactemp = (uint32_t)((((value-0.5)/0.5) * (2^12)) * DAC_POSITIVE_FULL_SCALE); |
AnnaBridge | 168:e84263d55307 | 129 | dactemp = dactemp / (2^12); |
AnnaBridge | 168:e84263d55307 | 130 | dactemp = (dactemp<<16) | dactemp; |
AnnaBridge | 168:e84263d55307 | 131 | HAL_DAC_WRITE32(pHalDacInitData->DACIdx, REG_DAC0_FIFO_WR, dactemp); |
AnnaBridge | 168:e84263d55307 | 132 | } else { |
AnnaBridge | 168:e84263d55307 | 133 | dacnegtemp = (DAC_NEGATIVE_FULL_SCALE & 0x7FF); |
AnnaBridge | 168:e84263d55307 | 134 | dacnegtemp = ((~dacnegtemp) + 1) & 0x7FF; |
AnnaBridge | 168:e84263d55307 | 135 | dactemp = (uint32_t)(((0.5-value)/0.5) * (2^12) * dacnegtemp); |
AnnaBridge | 168:e84263d55307 | 136 | dactemp = dactemp / (2^12); |
AnnaBridge | 168:e84263d55307 | 137 | dactemp = 0x1000 - dactemp; //change to 2's complement |
AnnaBridge | 168:e84263d55307 | 138 | dactemp = (dactemp<<16) | dactemp; |
AnnaBridge | 168:e84263d55307 | 139 | HAL_DAC_WRITE32(pHalDacInitData->DACIdx, REG_DAC0_FIFO_WR, dactemp); |
AnnaBridge | 168:e84263d55307 | 140 | } |
AnnaBridge | 168:e84263d55307 | 141 | } |
AnnaBridge | 168:e84263d55307 | 142 | } |
AnnaBridge | 168:e84263d55307 | 143 | |
AnnaBridge | 168:e84263d55307 | 144 | /** \brief analogout_write_u16:\n |
AnnaBridge | 168:e84263d55307 | 145 | * to execute analogout_write_u16 |
AnnaBridge | 168:e84263d55307 | 146 | * |
AnnaBridge | 168:e84263d55307 | 147 | * The register value of DAC input is a format of 2's complement. |
AnnaBridge | 168:e84263d55307 | 148 | * The most maximum value of positive value drives DAC to output a voltage about 3.3V. |
AnnaBridge | 168:e84263d55307 | 149 | * The most mimimum value of negative value drives DAC to output a voltage about 0. |
AnnaBridge | 168:e84263d55307 | 150 | * And the middle value of 0x000 will drive DAC to output a voltage of half of max voltage. |
AnnaBridge | 168:e84263d55307 | 151 | * |
AnnaBridge | 168:e84263d55307 | 152 | * \para dac_t * : obj |
AnnaBridge | 168:e84263d55307 | 153 | * \para float : value |
AnnaBridge | 168:e84263d55307 | 154 | */ |
AnnaBridge | 168:e84263d55307 | 155 | void analogout_write_u16(dac_t *obj, uint16_t value) |
AnnaBridge | 168:e84263d55307 | 156 | { |
AnnaBridge | 168:e84263d55307 | 157 | uint32_t dactemp; |
AnnaBridge | 168:e84263d55307 | 158 | PHAL_DAC_INIT_DAT pHalDacInitData = (PHAL_DAC_INIT_DAT)&(obj->DACpara); |
AnnaBridge | 168:e84263d55307 | 159 | |
AnnaBridge | 168:e84263d55307 | 160 | /* To give a two point data */ |
AnnaBridge | 168:e84263d55307 | 161 | dactemp = (value << 16) | value; |
AnnaBridge | 168:e84263d55307 | 162 | HAL_DAC_WRITE32(pHalDacInitData->DACIdx, REG_DAC0_FIFO_WR, dactemp); |
AnnaBridge | 168:e84263d55307 | 163 | } |
AnnaBridge | 168:e84263d55307 | 164 | |
AnnaBridge | 168:e84263d55307 | 165 | /** \brief analogout_read_u16:\n |
AnnaBridge | 168:e84263d55307 | 166 | * to read back analog output value in float format |
AnnaBridge | 168:e84263d55307 | 167 | * |
AnnaBridge | 168:e84263d55307 | 168 | * This function is NOT available in rtl8195a hardware design. |
AnnaBridge | 168:e84263d55307 | 169 | * It always returns a fixed value of 0.0; |
AnnaBridge | 168:e84263d55307 | 170 | * \para dac_t * : obj |
AnnaBridge | 168:e84263d55307 | 171 | */ |
AnnaBridge | 168:e84263d55307 | 172 | float analogout_read(dac_t *obj) |
AnnaBridge | 168:e84263d55307 | 173 | { |
AnnaBridge | 168:e84263d55307 | 174 | return (float)0.0; |
AnnaBridge | 168:e84263d55307 | 175 | } |
AnnaBridge | 168:e84263d55307 | 176 | |
AnnaBridge | 168:e84263d55307 | 177 | /** \brief analogout_read_u16:\n |
AnnaBridge | 168:e84263d55307 | 178 | * to read back analog output register value |
AnnaBridge | 168:e84263d55307 | 179 | * |
AnnaBridge | 168:e84263d55307 | 180 | * This function is NOT available in rtl8195a hardware design. |
AnnaBridge | 168:e84263d55307 | 181 | * It always returns a fixed value of 0xFFFF; |
AnnaBridge | 168:e84263d55307 | 182 | * \para dac_t * : obj |
AnnaBridge | 168:e84263d55307 | 183 | */ |
AnnaBridge | 168:e84263d55307 | 184 | uint16_t analogout_read_u16(dac_t *obj) |
AnnaBridge | 168:e84263d55307 | 185 | { |
AnnaBridge | 168:e84263d55307 | 186 | return (uint16_t)0xFFFF; |
AnnaBridge | 168:e84263d55307 | 187 | } |
AnnaBridge | 168:e84263d55307 | 188 | |
AnnaBridge | 168:e84263d55307 | 189 | #endif |