![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/drivers/fsl_dac.c@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2015, Freescale Semiconductor, Inc. |
kadonotakashi | 0:8fdf9a60065b | 3 | * All rights reserved. |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Redistribution and use in source and binary forms, with or without modification, |
kadonotakashi | 0:8fdf9a60065b | 6 | * are permitted provided that the following conditions are met: |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * o Redistributions of source code must retain the above copyright notice, this list |
kadonotakashi | 0:8fdf9a60065b | 9 | * of conditions and the following disclaimer. |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * o Redistributions in binary form must reproduce the above copyright notice, this |
kadonotakashi | 0:8fdf9a60065b | 12 | * list of conditions and the following disclaimer in the documentation and/or |
kadonotakashi | 0:8fdf9a60065b | 13 | * other materials provided with the distribution. |
kadonotakashi | 0:8fdf9a60065b | 14 | * |
kadonotakashi | 0:8fdf9a60065b | 15 | * o Neither the name of Freescale Semiconductor, Inc. nor the names of its |
kadonotakashi | 0:8fdf9a60065b | 16 | * contributors may be used to endorse or promote products derived from this |
kadonotakashi | 0:8fdf9a60065b | 17 | * software without specific prior written permission. |
kadonotakashi | 0:8fdf9a60065b | 18 | * |
kadonotakashi | 0:8fdf9a60065b | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
kadonotakashi | 0:8fdf9a60065b | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
kadonotakashi | 0:8fdf9a60065b | 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
kadonotakashi | 0:8fdf9a60065b | 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
kadonotakashi | 0:8fdf9a60065b | 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
kadonotakashi | 0:8fdf9a60065b | 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
kadonotakashi | 0:8fdf9a60065b | 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
kadonotakashi | 0:8fdf9a60065b | 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
kadonotakashi | 0:8fdf9a60065b | 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
kadonotakashi | 0:8fdf9a60065b | 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kadonotakashi | 0:8fdf9a60065b | 29 | */ |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | #include "fsl_dac.h" |
kadonotakashi | 0:8fdf9a60065b | 32 | |
kadonotakashi | 0:8fdf9a60065b | 33 | /******************************************************************************* |
kadonotakashi | 0:8fdf9a60065b | 34 | * Prototypes |
kadonotakashi | 0:8fdf9a60065b | 35 | ******************************************************************************/ |
kadonotakashi | 0:8fdf9a60065b | 36 | /*! |
kadonotakashi | 0:8fdf9a60065b | 37 | * @brief Get instance number for DAC module. |
kadonotakashi | 0:8fdf9a60065b | 38 | * |
kadonotakashi | 0:8fdf9a60065b | 39 | * @param base DAC peripheral base address |
kadonotakashi | 0:8fdf9a60065b | 40 | */ |
kadonotakashi | 0:8fdf9a60065b | 41 | static uint32_t DAC_GetInstance(DAC_Type *base); |
kadonotakashi | 0:8fdf9a60065b | 42 | |
kadonotakashi | 0:8fdf9a60065b | 43 | /******************************************************************************* |
kadonotakashi | 0:8fdf9a60065b | 44 | * Variables |
kadonotakashi | 0:8fdf9a60065b | 45 | ******************************************************************************/ |
kadonotakashi | 0:8fdf9a60065b | 46 | /*! @brief Pointers to DAC bases for each instance. */ |
kadonotakashi | 0:8fdf9a60065b | 47 | static DAC_Type *const s_dacBases[] = DAC_BASE_PTRS; |
kadonotakashi | 0:8fdf9a60065b | 48 | #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) |
kadonotakashi | 0:8fdf9a60065b | 49 | /*! @brief Pointers to DAC clocks for each instance. */ |
kadonotakashi | 0:8fdf9a60065b | 50 | static const clock_ip_name_t s_dacClocks[] = DAC_CLOCKS; |
kadonotakashi | 0:8fdf9a60065b | 51 | #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ |
kadonotakashi | 0:8fdf9a60065b | 52 | |
kadonotakashi | 0:8fdf9a60065b | 53 | /******************************************************************************* |
kadonotakashi | 0:8fdf9a60065b | 54 | * Codes |
kadonotakashi | 0:8fdf9a60065b | 55 | ******************************************************************************/ |
kadonotakashi | 0:8fdf9a60065b | 56 | static uint32_t DAC_GetInstance(DAC_Type *base) |
kadonotakashi | 0:8fdf9a60065b | 57 | { |
kadonotakashi | 0:8fdf9a60065b | 58 | uint32_t instance; |
kadonotakashi | 0:8fdf9a60065b | 59 | |
kadonotakashi | 0:8fdf9a60065b | 60 | /* Find the instance index from base address mappings. */ |
kadonotakashi | 0:8fdf9a60065b | 61 | for (instance = 0; instance < FSL_FEATURE_SOC_DAC_COUNT; instance++) |
kadonotakashi | 0:8fdf9a60065b | 62 | { |
kadonotakashi | 0:8fdf9a60065b | 63 | if (s_dacBases[instance] == base) |
kadonotakashi | 0:8fdf9a60065b | 64 | { |
kadonotakashi | 0:8fdf9a60065b | 65 | break; |
kadonotakashi | 0:8fdf9a60065b | 66 | } |
kadonotakashi | 0:8fdf9a60065b | 67 | } |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | assert(instance < FSL_FEATURE_SOC_DAC_COUNT); |
kadonotakashi | 0:8fdf9a60065b | 70 | |
kadonotakashi | 0:8fdf9a60065b | 71 | return instance; |
kadonotakashi | 0:8fdf9a60065b | 72 | } |
kadonotakashi | 0:8fdf9a60065b | 73 | |
kadonotakashi | 0:8fdf9a60065b | 74 | void DAC_Init(DAC_Type *base, const dac_config_t *config) |
kadonotakashi | 0:8fdf9a60065b | 75 | { |
kadonotakashi | 0:8fdf9a60065b | 76 | assert(NULL != config); |
kadonotakashi | 0:8fdf9a60065b | 77 | |
kadonotakashi | 0:8fdf9a60065b | 78 | uint8_t tmp8; |
kadonotakashi | 0:8fdf9a60065b | 79 | |
kadonotakashi | 0:8fdf9a60065b | 80 | #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) |
kadonotakashi | 0:8fdf9a60065b | 81 | /* Enable the clock. */ |
kadonotakashi | 0:8fdf9a60065b | 82 | CLOCK_EnableClock(s_dacClocks[DAC_GetInstance(base)]); |
kadonotakashi | 0:8fdf9a60065b | 83 | #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ |
kadonotakashi | 0:8fdf9a60065b | 84 | |
kadonotakashi | 0:8fdf9a60065b | 85 | /* Configure. */ |
kadonotakashi | 0:8fdf9a60065b | 86 | /* DACx_C0. */ |
kadonotakashi | 0:8fdf9a60065b | 87 | tmp8 = base->C0 & ~(DAC_C0_DACRFS_MASK | DAC_C0_LPEN_MASK); |
kadonotakashi | 0:8fdf9a60065b | 88 | if (kDAC_ReferenceVoltageSourceVref2 == config->referenceVoltageSource) |
kadonotakashi | 0:8fdf9a60065b | 89 | { |
kadonotakashi | 0:8fdf9a60065b | 90 | tmp8 |= DAC_C0_DACRFS_MASK; |
kadonotakashi | 0:8fdf9a60065b | 91 | } |
kadonotakashi | 0:8fdf9a60065b | 92 | if (config->enableLowPowerMode) |
kadonotakashi | 0:8fdf9a60065b | 93 | { |
kadonotakashi | 0:8fdf9a60065b | 94 | tmp8 |= DAC_C0_LPEN_MASK; |
kadonotakashi | 0:8fdf9a60065b | 95 | } |
kadonotakashi | 0:8fdf9a60065b | 96 | base->C0 = tmp8; |
kadonotakashi | 0:8fdf9a60065b | 97 | |
kadonotakashi | 0:8fdf9a60065b | 98 | /* DAC_Enable(base, true); */ |
kadonotakashi | 0:8fdf9a60065b | 99 | /* Tip: The DAC output can be enabled till then after user sets their own available data in application. */ |
kadonotakashi | 0:8fdf9a60065b | 100 | } |
kadonotakashi | 0:8fdf9a60065b | 101 | |
kadonotakashi | 0:8fdf9a60065b | 102 | void DAC_Deinit(DAC_Type *base) |
kadonotakashi | 0:8fdf9a60065b | 103 | { |
kadonotakashi | 0:8fdf9a60065b | 104 | DAC_Enable(base, false); |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) |
kadonotakashi | 0:8fdf9a60065b | 107 | /* Disable the clock. */ |
kadonotakashi | 0:8fdf9a60065b | 108 | CLOCK_DisableClock(s_dacClocks[DAC_GetInstance(base)]); |
kadonotakashi | 0:8fdf9a60065b | 109 | #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ |
kadonotakashi | 0:8fdf9a60065b | 110 | } |
kadonotakashi | 0:8fdf9a60065b | 111 | |
kadonotakashi | 0:8fdf9a60065b | 112 | void DAC_GetDefaultConfig(dac_config_t *config) |
kadonotakashi | 0:8fdf9a60065b | 113 | { |
kadonotakashi | 0:8fdf9a60065b | 114 | assert(NULL != config); |
kadonotakashi | 0:8fdf9a60065b | 115 | |
kadonotakashi | 0:8fdf9a60065b | 116 | config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2; |
kadonotakashi | 0:8fdf9a60065b | 117 | config->enableLowPowerMode = false; |
kadonotakashi | 0:8fdf9a60065b | 118 | } |
kadonotakashi | 0:8fdf9a60065b | 119 | |
kadonotakashi | 0:8fdf9a60065b | 120 | void DAC_SetBufferConfig(DAC_Type *base, const dac_buffer_config_t *config) |
kadonotakashi | 0:8fdf9a60065b | 121 | { |
kadonotakashi | 0:8fdf9a60065b | 122 | assert(NULL != config); |
kadonotakashi | 0:8fdf9a60065b | 123 | |
kadonotakashi | 0:8fdf9a60065b | 124 | uint8_t tmp8; |
kadonotakashi | 0:8fdf9a60065b | 125 | |
kadonotakashi | 0:8fdf9a60065b | 126 | /* DACx_C0. */ |
kadonotakashi | 0:8fdf9a60065b | 127 | tmp8 = base->C0 & ~(DAC_C0_DACTRGSEL_MASK); |
kadonotakashi | 0:8fdf9a60065b | 128 | if (kDAC_BufferTriggerBySoftwareMode == config->triggerMode) |
kadonotakashi | 0:8fdf9a60065b | 129 | { |
kadonotakashi | 0:8fdf9a60065b | 130 | tmp8 |= DAC_C0_DACTRGSEL_MASK; |
kadonotakashi | 0:8fdf9a60065b | 131 | } |
kadonotakashi | 0:8fdf9a60065b | 132 | base->C0 = tmp8; |
kadonotakashi | 0:8fdf9a60065b | 133 | |
kadonotakashi | 0:8fdf9a60065b | 134 | /* DACx_C1. */ |
kadonotakashi | 0:8fdf9a60065b | 135 | tmp8 = base->C1 & |
kadonotakashi | 0:8fdf9a60065b | 136 | ~( |
kadonotakashi | 0:8fdf9a60065b | 137 | #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION |
kadonotakashi | 0:8fdf9a60065b | 138 | DAC_C1_DACBFWM_MASK | |
kadonotakashi | 0:8fdf9a60065b | 139 | #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */ |
kadonotakashi | 0:8fdf9a60065b | 140 | DAC_C1_DACBFMD_MASK); |
kadonotakashi | 0:8fdf9a60065b | 141 | #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION |
kadonotakashi | 0:8fdf9a60065b | 142 | tmp8 |= DAC_C1_DACBFWM(config->watermark); |
kadonotakashi | 0:8fdf9a60065b | 143 | #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */ |
kadonotakashi | 0:8fdf9a60065b | 144 | tmp8 |= DAC_C1_DACBFMD(config->workMode); |
kadonotakashi | 0:8fdf9a60065b | 145 | base->C1 = tmp8; |
kadonotakashi | 0:8fdf9a60065b | 146 | |
kadonotakashi | 0:8fdf9a60065b | 147 | /* DACx_C2. */ |
kadonotakashi | 0:8fdf9a60065b | 148 | tmp8 = base->C2 & ~DAC_C2_DACBFUP_MASK; |
kadonotakashi | 0:8fdf9a60065b | 149 | tmp8 |= DAC_C2_DACBFUP(config->upperLimit); |
kadonotakashi | 0:8fdf9a60065b | 150 | base->C2 = tmp8; |
kadonotakashi | 0:8fdf9a60065b | 151 | } |
kadonotakashi | 0:8fdf9a60065b | 152 | |
kadonotakashi | 0:8fdf9a60065b | 153 | void DAC_GetDefaultBufferConfig(dac_buffer_config_t *config) |
kadonotakashi | 0:8fdf9a60065b | 154 | { |
kadonotakashi | 0:8fdf9a60065b | 155 | assert(NULL != config); |
kadonotakashi | 0:8fdf9a60065b | 156 | |
kadonotakashi | 0:8fdf9a60065b | 157 | config->triggerMode = kDAC_BufferTriggerBySoftwareMode; |
kadonotakashi | 0:8fdf9a60065b | 158 | #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION |
kadonotakashi | 0:8fdf9a60065b | 159 | config->watermark = kDAC_BufferWatermark1Word; |
kadonotakashi | 0:8fdf9a60065b | 160 | #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */ |
kadonotakashi | 0:8fdf9a60065b | 161 | config->workMode = kDAC_BufferWorkAsNormalMode; |
kadonotakashi | 0:8fdf9a60065b | 162 | config->upperLimit = DAC_DATL_COUNT - 1U; |
kadonotakashi | 0:8fdf9a60065b | 163 | } |
kadonotakashi | 0:8fdf9a60065b | 164 | |
kadonotakashi | 0:8fdf9a60065b | 165 | void DAC_SetBufferValue(DAC_Type *base, uint8_t index, uint16_t value) |
kadonotakashi | 0:8fdf9a60065b | 166 | { |
kadonotakashi | 0:8fdf9a60065b | 167 | assert(index < DAC_DATL_COUNT); |
kadonotakashi | 0:8fdf9a60065b | 168 | |
kadonotakashi | 0:8fdf9a60065b | 169 | base->DAT[index].DATL = (uint8_t)(0xFFU & value); /* Low 8-bit. */ |
kadonotakashi | 0:8fdf9a60065b | 170 | base->DAT[index].DATH = (uint8_t)((0xF00U & value) >> 8); /* High 4-bit. */ |
kadonotakashi | 0:8fdf9a60065b | 171 | } |
kadonotakashi | 0:8fdf9a60065b | 172 | |
kadonotakashi | 0:8fdf9a60065b | 173 | void DAC_SetBufferReadPointer(DAC_Type *base, uint8_t index) |
kadonotakashi | 0:8fdf9a60065b | 174 | { |
kadonotakashi | 0:8fdf9a60065b | 175 | assert(index < DAC_DATL_COUNT); |
kadonotakashi | 0:8fdf9a60065b | 176 | |
kadonotakashi | 0:8fdf9a60065b | 177 | uint8_t tmp8 = base->C2 & ~DAC_C2_DACBFRP_MASK; |
kadonotakashi | 0:8fdf9a60065b | 178 | |
kadonotakashi | 0:8fdf9a60065b | 179 | tmp8 |= DAC_C2_DACBFRP(index); |
kadonotakashi | 0:8fdf9a60065b | 180 | base->C2 = tmp8; |
kadonotakashi | 0:8fdf9a60065b | 181 | } |
kadonotakashi | 0:8fdf9a60065b | 182 | |
kadonotakashi | 0:8fdf9a60065b | 183 | void DAC_EnableBufferInterrupts(DAC_Type *base, uint32_t mask) |
kadonotakashi | 0:8fdf9a60065b | 184 | { |
kadonotakashi | 0:8fdf9a60065b | 185 | mask &= ( |
kadonotakashi | 0:8fdf9a60065b | 186 | #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION |
kadonotakashi | 0:8fdf9a60065b | 187 | DAC_C0_DACBWIEN_MASK | |
kadonotakashi | 0:8fdf9a60065b | 188 | #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */ |
kadonotakashi | 0:8fdf9a60065b | 189 | DAC_C0_DACBTIEN_MASK | DAC_C0_DACBBIEN_MASK); |
kadonotakashi | 0:8fdf9a60065b | 190 | base->C0 |= ((uint8_t)mask); /* Write 1 to enable. */ |
kadonotakashi | 0:8fdf9a60065b | 191 | } |
kadonotakashi | 0:8fdf9a60065b | 192 | |
kadonotakashi | 0:8fdf9a60065b | 193 | void DAC_DisableBufferInterrupts(DAC_Type *base, uint32_t mask) |
kadonotakashi | 0:8fdf9a60065b | 194 | { |
kadonotakashi | 0:8fdf9a60065b | 195 | mask &= ( |
kadonotakashi | 0:8fdf9a60065b | 196 | #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION |
kadonotakashi | 0:8fdf9a60065b | 197 | DAC_C0_DACBWIEN_MASK | |
kadonotakashi | 0:8fdf9a60065b | 198 | #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */ |
kadonotakashi | 0:8fdf9a60065b | 199 | DAC_C0_DACBTIEN_MASK | DAC_C0_DACBBIEN_MASK); |
kadonotakashi | 0:8fdf9a60065b | 200 | base->C0 &= (uint8_t)(~((uint8_t)mask)); /* Write 0 to disable. */ |
kadonotakashi | 0:8fdf9a60065b | 201 | } |
kadonotakashi | 0:8fdf9a60065b | 202 | |
kadonotakashi | 0:8fdf9a60065b | 203 | uint32_t DAC_GetBufferStatusFlags(DAC_Type *base) |
kadonotakashi | 0:8fdf9a60065b | 204 | { |
kadonotakashi | 0:8fdf9a60065b | 205 | return (uint32_t)(base->SR & ( |
kadonotakashi | 0:8fdf9a60065b | 206 | #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION |
kadonotakashi | 0:8fdf9a60065b | 207 | DAC_SR_DACBFWMF_MASK | |
kadonotakashi | 0:8fdf9a60065b | 208 | #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */ |
kadonotakashi | 0:8fdf9a60065b | 209 | DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFRPBF_MASK)); |
kadonotakashi | 0:8fdf9a60065b | 210 | } |
kadonotakashi | 0:8fdf9a60065b | 211 | |
kadonotakashi | 0:8fdf9a60065b | 212 | void DAC_ClearBufferStatusFlags(DAC_Type *base, uint32_t mask) |
kadonotakashi | 0:8fdf9a60065b | 213 | { |
kadonotakashi | 0:8fdf9a60065b | 214 | mask &= ( |
kadonotakashi | 0:8fdf9a60065b | 215 | #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION |
kadonotakashi | 0:8fdf9a60065b | 216 | DAC_SR_DACBFWMF_MASK | |
kadonotakashi | 0:8fdf9a60065b | 217 | #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */ |
kadonotakashi | 0:8fdf9a60065b | 218 | DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFRPBF_MASK); |
kadonotakashi | 0:8fdf9a60065b | 219 | base->SR &= (uint8_t)(~((uint8_t)mask)); /* Write 0 to clear flags. */ |
kadonotakashi | 0:8fdf9a60065b | 220 | } |