t
Fork of mbed-dev by
targets/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/dac/TARGET_SAMD21/dac.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/dac/TARGET_SAMD21/dac.c@18:da299f395b9e
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 15:a81a8d6c1dfe | 1 | /** |
mbed_official | 15:a81a8d6c1dfe | 2 | * \file |
mbed_official | 15:a81a8d6c1dfe | 3 | * |
mbed_official | 15:a81a8d6c1dfe | 4 | * \brief SAM Peripheral Digital-to-Analog Converter Driver |
mbed_official | 15:a81a8d6c1dfe | 5 | * |
mbed_official | 15:a81a8d6c1dfe | 6 | * Copyright (C) 2012-2015 Atmel Corporation. All rights reserved. |
mbed_official | 15:a81a8d6c1dfe | 7 | * |
mbed_official | 15:a81a8d6c1dfe | 8 | * \asf_license_start |
mbed_official | 15:a81a8d6c1dfe | 9 | * |
mbed_official | 15:a81a8d6c1dfe | 10 | * \page License |
mbed_official | 15:a81a8d6c1dfe | 11 | * |
mbed_official | 15:a81a8d6c1dfe | 12 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 15:a81a8d6c1dfe | 13 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 15:a81a8d6c1dfe | 14 | * |
mbed_official | 15:a81a8d6c1dfe | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 15:a81a8d6c1dfe | 16 | * this list of conditions and the following disclaimer. |
mbed_official | 15:a81a8d6c1dfe | 17 | * |
mbed_official | 15:a81a8d6c1dfe | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 15:a81a8d6c1dfe | 19 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 15:a81a8d6c1dfe | 20 | * and/or other materials provided with the distribution. |
mbed_official | 15:a81a8d6c1dfe | 21 | * |
mbed_official | 15:a81a8d6c1dfe | 22 | * 3. The name of Atmel may not be used to endorse or promote products derived |
mbed_official | 15:a81a8d6c1dfe | 23 | * from this software without specific prior written permission. |
mbed_official | 15:a81a8d6c1dfe | 24 | * |
mbed_official | 15:a81a8d6c1dfe | 25 | * 4. This software may only be redistributed and used in connection with an |
mbed_official | 15:a81a8d6c1dfe | 26 | * Atmel microcontroller product. |
mbed_official | 15:a81a8d6c1dfe | 27 | * |
mbed_official | 15:a81a8d6c1dfe | 28 | * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED |
mbed_official | 15:a81a8d6c1dfe | 29 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
mbed_official | 15:a81a8d6c1dfe | 30 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
mbed_official | 15:a81a8d6c1dfe | 31 | * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR |
mbed_official | 15:a81a8d6c1dfe | 32 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 15:a81a8d6c1dfe | 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
mbed_official | 15:a81a8d6c1dfe | 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
mbed_official | 15:a81a8d6c1dfe | 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
mbed_official | 15:a81a8d6c1dfe | 36 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
mbed_official | 15:a81a8d6c1dfe | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 15:a81a8d6c1dfe | 38 | * POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 15:a81a8d6c1dfe | 39 | * |
mbed_official | 15:a81a8d6c1dfe | 40 | * \asf_license_stop |
mbed_official | 15:a81a8d6c1dfe | 41 | * |
mbed_official | 15:a81a8d6c1dfe | 42 | */ |
mbed_official | 15:a81a8d6c1dfe | 43 | /* |
mbed_official | 15:a81a8d6c1dfe | 44 | * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> |
mbed_official | 15:a81a8d6c1dfe | 45 | */ |
mbed_official | 15:a81a8d6c1dfe | 46 | #include "dac.h" |
mbed_official | 15:a81a8d6c1dfe | 47 | #include <system.h> |
mbed_official | 15:a81a8d6c1dfe | 48 | #include <pinmux.h> |
mbed_official | 15:a81a8d6c1dfe | 49 | |
mbed_official | 15:a81a8d6c1dfe | 50 | /** |
mbed_official | 15:a81a8d6c1dfe | 51 | * \internal Writes a DAC configuration to the hardware module. |
mbed_official | 15:a81a8d6c1dfe | 52 | * |
mbed_official | 15:a81a8d6c1dfe | 53 | * Writes out a given configuration to the hardware module. |
mbed_official | 15:a81a8d6c1dfe | 54 | * |
mbed_official | 15:a81a8d6c1dfe | 55 | * \param[out] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 56 | * \param[in] config Pointer to the configuration struct |
mbed_official | 15:a81a8d6c1dfe | 57 | * |
mbed_official | 15:a81a8d6c1dfe | 58 | */ |
mbed_official | 15:a81a8d6c1dfe | 59 | static void _dac_set_config( |
mbed_official | 15:a81a8d6c1dfe | 60 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 61 | struct dac_config *const config) |
mbed_official | 15:a81a8d6c1dfe | 62 | { |
mbed_official | 15:a81a8d6c1dfe | 63 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 64 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 65 | Assert(config); |
mbed_official | 15:a81a8d6c1dfe | 66 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 67 | |
mbed_official | 15:a81a8d6c1dfe | 68 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 69 | |
mbed_official | 15:a81a8d6c1dfe | 70 | /* Set selected DAC output to be enabled when enabling the module */ |
mbed_official | 15:a81a8d6c1dfe | 71 | module_inst->output = config->output; |
mbed_official | 15:a81a8d6c1dfe | 72 | module_inst->start_on_event = false; |
mbed_official | 15:a81a8d6c1dfe | 73 | |
mbed_official | 15:a81a8d6c1dfe | 74 | uint32_t new_ctrla = 0; |
mbed_official | 15:a81a8d6c1dfe | 75 | uint32_t new_ctrlb = 0; |
mbed_official | 15:a81a8d6c1dfe | 76 | |
mbed_official | 15:a81a8d6c1dfe | 77 | /* Enable DAC in standby sleep mode if configured */ |
mbed_official | 15:a81a8d6c1dfe | 78 | if (config->run_in_standby) { |
mbed_official | 15:a81a8d6c1dfe | 79 | new_ctrla |= DAC_CTRLA_RUNSTDBY; |
mbed_official | 15:a81a8d6c1dfe | 80 | } |
mbed_official | 15:a81a8d6c1dfe | 81 | |
mbed_official | 15:a81a8d6c1dfe | 82 | /* Set reference voltage */ |
mbed_official | 15:a81a8d6c1dfe | 83 | new_ctrlb |= config->reference; |
mbed_official | 15:a81a8d6c1dfe | 84 | |
mbed_official | 15:a81a8d6c1dfe | 85 | /* Left adjust data if configured */ |
mbed_official | 15:a81a8d6c1dfe | 86 | if (config->left_adjust) { |
mbed_official | 15:a81a8d6c1dfe | 87 | new_ctrlb |= DAC_CTRLB_LEFTADJ; |
mbed_official | 15:a81a8d6c1dfe | 88 | } |
mbed_official | 15:a81a8d6c1dfe | 89 | |
mbed_official | 15:a81a8d6c1dfe | 90 | #ifdef FEATURE_DAC_DATABUF_WRITE_PROTECTION |
mbed_official | 15:a81a8d6c1dfe | 91 | /* Bypass DATABUF write protection if configured */ |
mbed_official | 15:a81a8d6c1dfe | 92 | if (config->databuf_protection_bypass) { |
mbed_official | 15:a81a8d6c1dfe | 93 | new_ctrlb |= DAC_CTRLB_BDWP; |
mbed_official | 15:a81a8d6c1dfe | 94 | } |
mbed_official | 15:a81a8d6c1dfe | 95 | #endif |
mbed_official | 15:a81a8d6c1dfe | 96 | |
mbed_official | 15:a81a8d6c1dfe | 97 | /* Voltage pump disable if configured */ |
mbed_official | 15:a81a8d6c1dfe | 98 | if (config->voltage_pump_disable) { |
mbed_official | 15:a81a8d6c1dfe | 99 | new_ctrlb |= DAC_CTRLB_VPD; |
mbed_official | 15:a81a8d6c1dfe | 100 | } |
mbed_official | 15:a81a8d6c1dfe | 101 | |
mbed_official | 15:a81a8d6c1dfe | 102 | /* Apply the new configuration to the hardware module */ |
mbed_official | 15:a81a8d6c1dfe | 103 | dac_module->CTRLA.reg = new_ctrla; |
mbed_official | 15:a81a8d6c1dfe | 104 | |
mbed_official | 15:a81a8d6c1dfe | 105 | while (dac_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 106 | /* Wait until the synchronization is complete */ |
mbed_official | 15:a81a8d6c1dfe | 107 | } |
mbed_official | 15:a81a8d6c1dfe | 108 | |
mbed_official | 15:a81a8d6c1dfe | 109 | dac_module->CTRLB.reg = new_ctrlb; |
mbed_official | 15:a81a8d6c1dfe | 110 | } |
mbed_official | 15:a81a8d6c1dfe | 111 | |
mbed_official | 15:a81a8d6c1dfe | 112 | /** |
mbed_official | 15:a81a8d6c1dfe | 113 | * \brief Determines if the hardware module(s) are currently synchronizing to the bus. |
mbed_official | 15:a81a8d6c1dfe | 114 | * |
mbed_official | 15:a81a8d6c1dfe | 115 | * Checks to see if the underlying hardware peripheral module(s) are currently |
mbed_official | 15:a81a8d6c1dfe | 116 | * synchronizing across multiple clock domains to the hardware bus, This |
mbed_official | 15:a81a8d6c1dfe | 117 | * function can be used to delay further operations on a module until such time |
mbed_official | 15:a81a8d6c1dfe | 118 | * that it is ready, to prevent blocking delays for synchronization in the |
mbed_official | 15:a81a8d6c1dfe | 119 | * user application. |
mbed_official | 15:a81a8d6c1dfe | 120 | * |
mbed_official | 15:a81a8d6c1dfe | 121 | * \param[in] dev_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 122 | * |
mbed_official | 15:a81a8d6c1dfe | 123 | * \return Synchronization status of the underlying hardware module(s). |
mbed_official | 15:a81a8d6c1dfe | 124 | * |
mbed_official | 15:a81a8d6c1dfe | 125 | * \retval true If the module synchronization is ongoing |
mbed_official | 15:a81a8d6c1dfe | 126 | * \retval false If the module has completed synchronization |
mbed_official | 15:a81a8d6c1dfe | 127 | */ |
mbed_official | 15:a81a8d6c1dfe | 128 | bool dac_is_syncing( |
mbed_official | 15:a81a8d6c1dfe | 129 | struct dac_module *const dev_inst) |
mbed_official | 15:a81a8d6c1dfe | 130 | { |
mbed_official | 15:a81a8d6c1dfe | 131 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 132 | Assert(dev_inst); |
mbed_official | 15:a81a8d6c1dfe | 133 | |
mbed_official | 15:a81a8d6c1dfe | 134 | Dac *const dac_module = dev_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 135 | |
mbed_official | 15:a81a8d6c1dfe | 136 | #if (SAMC21) |
mbed_official | 15:a81a8d6c1dfe | 137 | if (dac_module->SYNCBUSY.reg) { |
mbed_official | 15:a81a8d6c1dfe | 138 | #else |
mbed_official | 15:a81a8d6c1dfe | 139 | if (dac_module->STATUS.reg & DAC_STATUS_SYNCBUSY) { |
mbed_official | 15:a81a8d6c1dfe | 140 | #endif |
mbed_official | 15:a81a8d6c1dfe | 141 | return true; |
mbed_official | 15:a81a8d6c1dfe | 142 | } |
mbed_official | 15:a81a8d6c1dfe | 143 | |
mbed_official | 15:a81a8d6c1dfe | 144 | return false; |
mbed_official | 15:a81a8d6c1dfe | 145 | } |
mbed_official | 15:a81a8d6c1dfe | 146 | |
mbed_official | 15:a81a8d6c1dfe | 147 | /** |
mbed_official | 15:a81a8d6c1dfe | 148 | * \brief Initializes a DAC configuration structure to defaults. |
mbed_official | 15:a81a8d6c1dfe | 149 | * |
mbed_official | 15:a81a8d6c1dfe | 150 | * Initializes a given DAC configuration structure to a set of |
mbed_official | 15:a81a8d6c1dfe | 151 | * known default values. This function should be called on any new |
mbed_official | 15:a81a8d6c1dfe | 152 | * instance of the configuration structures before being modified by the |
mbed_official | 15:a81a8d6c1dfe | 153 | * user application. |
mbed_official | 15:a81a8d6c1dfe | 154 | * |
mbed_official | 15:a81a8d6c1dfe | 155 | * The default configuration is as follows: |
mbed_official | 15:a81a8d6c1dfe | 156 | * \li 1V from internal bandgap reference |
mbed_official | 15:a81a8d6c1dfe | 157 | * \li Drive the DAC output to the VOUT pin |
mbed_official | 15:a81a8d6c1dfe | 158 | * \li Right adjust data |
mbed_official | 15:a81a8d6c1dfe | 159 | * \li GCLK generator 0 (GCLK main) clock source |
mbed_official | 15:a81a8d6c1dfe | 160 | * \li The output buffer is disabled when the chip enters STANDBY sleep |
mbed_official | 15:a81a8d6c1dfe | 161 | * mode |
mbed_official | 15:a81a8d6c1dfe | 162 | * |
mbed_official | 15:a81a8d6c1dfe | 163 | * \param[out] config Configuration structure to initialize to default values |
mbed_official | 15:a81a8d6c1dfe | 164 | */ |
mbed_official | 15:a81a8d6c1dfe | 165 | void dac_get_config_defaults( |
mbed_official | 15:a81a8d6c1dfe | 166 | struct dac_config *const config) |
mbed_official | 15:a81a8d6c1dfe | 167 | { |
mbed_official | 15:a81a8d6c1dfe | 168 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 169 | Assert(config); |
mbed_official | 15:a81a8d6c1dfe | 170 | |
mbed_official | 15:a81a8d6c1dfe | 171 | /* Default configuration values */ |
mbed_official | 15:a81a8d6c1dfe | 172 | config->reference = DAC_REFERENCE_INT1V; |
mbed_official | 15:a81a8d6c1dfe | 173 | config->output = DAC_OUTPUT_EXTERNAL; |
mbed_official | 15:a81a8d6c1dfe | 174 | config->left_adjust = false; |
mbed_official | 15:a81a8d6c1dfe | 175 | #ifdef FEATURE_DAC_DATABUF_WRITE_PROTECTION |
mbed_official | 15:a81a8d6c1dfe | 176 | config->databuf_protection_bypass = false; |
mbed_official | 15:a81a8d6c1dfe | 177 | #endif |
mbed_official | 15:a81a8d6c1dfe | 178 | config->voltage_pump_disable = false; |
mbed_official | 15:a81a8d6c1dfe | 179 | config->clock_source = GCLK_GENERATOR_0; |
mbed_official | 15:a81a8d6c1dfe | 180 | config->run_in_standby = false; |
mbed_official | 15:a81a8d6c1dfe | 181 | #if (SAMC21) |
mbed_official | 15:a81a8d6c1dfe | 182 | config->dither_mode = false; |
mbed_official | 15:a81a8d6c1dfe | 183 | #endif |
mbed_official | 15:a81a8d6c1dfe | 184 | } |
mbed_official | 15:a81a8d6c1dfe | 185 | |
mbed_official | 15:a81a8d6c1dfe | 186 | /** |
mbed_official | 15:a81a8d6c1dfe | 187 | * \brief Initialize the DAC device struct. |
mbed_official | 15:a81a8d6c1dfe | 188 | * |
mbed_official | 15:a81a8d6c1dfe | 189 | * Use this function to initialize the Digital to Analog Converter. Resets the |
mbed_official | 15:a81a8d6c1dfe | 190 | * underlying hardware module and configures it. |
mbed_official | 15:a81a8d6c1dfe | 191 | * |
mbed_official | 15:a81a8d6c1dfe | 192 | * \note The DAC channel must be configured separately. |
mbed_official | 15:a81a8d6c1dfe | 193 | * |
mbed_official | 15:a81a8d6c1dfe | 194 | * \param[out] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 195 | * \param[in] module Pointer to the DAC module instance |
mbed_official | 15:a81a8d6c1dfe | 196 | * \param[in] config Pointer to the config struct, created by the user |
mbed_official | 15:a81a8d6c1dfe | 197 | * application |
mbed_official | 15:a81a8d6c1dfe | 198 | * |
mbed_official | 15:a81a8d6c1dfe | 199 | * \return Status of initialization. |
mbed_official | 15:a81a8d6c1dfe | 200 | * \retval STATUS_OK Module initiated correctly |
mbed_official | 15:a81a8d6c1dfe | 201 | * \retval STATUS_ERR_DENIED If module is enabled |
mbed_official | 15:a81a8d6c1dfe | 202 | * \retval STATUS_BUSY If module is busy resetting |
mbed_official | 15:a81a8d6c1dfe | 203 | */ |
mbed_official | 15:a81a8d6c1dfe | 204 | enum status_code dac_init( |
mbed_official | 15:a81a8d6c1dfe | 205 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 206 | Dac *const module, |
mbed_official | 15:a81a8d6c1dfe | 207 | struct dac_config *const config) |
mbed_official | 15:a81a8d6c1dfe | 208 | { |
mbed_official | 15:a81a8d6c1dfe | 209 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 210 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 211 | Assert(module); |
mbed_official | 15:a81a8d6c1dfe | 212 | Assert(config); |
mbed_official | 15:a81a8d6c1dfe | 213 | |
mbed_official | 15:a81a8d6c1dfe | 214 | /* Initialize device instance */ |
mbed_official | 15:a81a8d6c1dfe | 215 | module_inst->hw = module; |
mbed_official | 15:a81a8d6c1dfe | 216 | |
mbed_official | 15:a81a8d6c1dfe | 217 | /* Turn on the digital interface clock */ |
mbed_official | 15:a81a8d6c1dfe | 218 | #if (SAMC21) |
mbed_official | 15:a81a8d6c1dfe | 219 | system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_DAC); |
mbed_official | 15:a81a8d6c1dfe | 220 | #else |
mbed_official | 15:a81a8d6c1dfe | 221 | system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, PM_APBCMASK_DAC); |
mbed_official | 15:a81a8d6c1dfe | 222 | #endif |
mbed_official | 15:a81a8d6c1dfe | 223 | |
mbed_official | 15:a81a8d6c1dfe | 224 | /* Check if module is enabled. */ |
mbed_official | 15:a81a8d6c1dfe | 225 | if (module->CTRLA.reg & DAC_CTRLA_ENABLE) { |
mbed_official | 15:a81a8d6c1dfe | 226 | return STATUS_ERR_DENIED; |
mbed_official | 15:a81a8d6c1dfe | 227 | } |
mbed_official | 15:a81a8d6c1dfe | 228 | |
mbed_official | 15:a81a8d6c1dfe | 229 | /* Check if reset is in progress. */ |
mbed_official | 15:a81a8d6c1dfe | 230 | if (module->CTRLA.reg & DAC_CTRLA_SWRST) { |
mbed_official | 15:a81a8d6c1dfe | 231 | return STATUS_BUSY; |
mbed_official | 15:a81a8d6c1dfe | 232 | } |
mbed_official | 15:a81a8d6c1dfe | 233 | |
mbed_official | 15:a81a8d6c1dfe | 234 | /* Configure GCLK channel and enable clock */ |
mbed_official | 15:a81a8d6c1dfe | 235 | struct system_gclk_chan_config gclk_chan_conf; |
mbed_official | 15:a81a8d6c1dfe | 236 | system_gclk_chan_get_config_defaults(&gclk_chan_conf); |
mbed_official | 15:a81a8d6c1dfe | 237 | gclk_chan_conf.source_generator = config->clock_source; |
mbed_official | 15:a81a8d6c1dfe | 238 | system_gclk_chan_set_config(DAC_GCLK_ID, &gclk_chan_conf); |
mbed_official | 15:a81a8d6c1dfe | 239 | system_gclk_chan_enable(DAC_GCLK_ID); |
mbed_official | 15:a81a8d6c1dfe | 240 | |
mbed_official | 15:a81a8d6c1dfe | 241 | /* MUX the DAC VOUT pin */ |
mbed_official | 15:a81a8d6c1dfe | 242 | struct system_pinmux_config pin_conf; |
mbed_official | 15:a81a8d6c1dfe | 243 | system_pinmux_get_config_defaults(&pin_conf); |
mbed_official | 15:a81a8d6c1dfe | 244 | |
mbed_official | 15:a81a8d6c1dfe | 245 | /* Set up the DAC VOUT pin */ |
mbed_official | 15:a81a8d6c1dfe | 246 | pin_conf.mux_position = MUX_PA02B_DAC_VOUT; |
mbed_official | 15:a81a8d6c1dfe | 247 | pin_conf.direction = SYSTEM_PINMUX_PIN_DIR_INPUT; |
mbed_official | 15:a81a8d6c1dfe | 248 | pin_conf.input_pull = SYSTEM_PINMUX_PIN_PULL_NONE; |
mbed_official | 15:a81a8d6c1dfe | 249 | system_pinmux_pin_set_config(PIN_PA02B_DAC_VOUT, &pin_conf); |
mbed_official | 15:a81a8d6c1dfe | 250 | |
mbed_official | 15:a81a8d6c1dfe | 251 | /* Write configuration to module */ |
mbed_official | 15:a81a8d6c1dfe | 252 | _dac_set_config(module_inst, config); |
mbed_official | 15:a81a8d6c1dfe | 253 | |
mbed_official | 15:a81a8d6c1dfe | 254 | /* Store reference selection for later use */ |
mbed_official | 15:a81a8d6c1dfe | 255 | module_inst->reference = config->reference; |
mbed_official | 15:a81a8d6c1dfe | 256 | |
mbed_official | 15:a81a8d6c1dfe | 257 | #if DAC_CALLBACK_MODE == true |
mbed_official | 15:a81a8d6c1dfe | 258 | for (uint8_t i = 0; i < DAC_CALLBACK_N; i++) { |
mbed_official | 15:a81a8d6c1dfe | 259 | module_inst->callback[i] = NULL; |
mbed_official | 15:a81a8d6c1dfe | 260 | }; |
mbed_official | 15:a81a8d6c1dfe | 261 | |
mbed_official | 15:a81a8d6c1dfe | 262 | _dac_instances[0] = module_inst; |
mbed_official | 15:a81a8d6c1dfe | 263 | #endif |
mbed_official | 15:a81a8d6c1dfe | 264 | |
mbed_official | 15:a81a8d6c1dfe | 265 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 266 | } |
mbed_official | 15:a81a8d6c1dfe | 267 | |
mbed_official | 15:a81a8d6c1dfe | 268 | /** |
mbed_official | 15:a81a8d6c1dfe | 269 | * \brief Resets the DAC module. |
mbed_official | 15:a81a8d6c1dfe | 270 | * |
mbed_official | 15:a81a8d6c1dfe | 271 | * This function will reset the DAC module to its power on default values and |
mbed_official | 15:a81a8d6c1dfe | 272 | * disable it. |
mbed_official | 15:a81a8d6c1dfe | 273 | * |
mbed_official | 15:a81a8d6c1dfe | 274 | * \param[in] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 275 | */ |
mbed_official | 15:a81a8d6c1dfe | 276 | void dac_reset( |
mbed_official | 15:a81a8d6c1dfe | 277 | struct dac_module *const module_inst) |
mbed_official | 15:a81a8d6c1dfe | 278 | { |
mbed_official | 15:a81a8d6c1dfe | 279 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 280 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 281 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 282 | |
mbed_official | 15:a81a8d6c1dfe | 283 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 284 | |
mbed_official | 15:a81a8d6c1dfe | 285 | while (dac_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 286 | /* Wait until the synchronization is complete */ |
mbed_official | 15:a81a8d6c1dfe | 287 | } |
mbed_official | 15:a81a8d6c1dfe | 288 | |
mbed_official | 15:a81a8d6c1dfe | 289 | /* Software reset the module */ |
mbed_official | 15:a81a8d6c1dfe | 290 | dac_module->CTRLA.reg |= DAC_CTRLA_SWRST; |
mbed_official | 15:a81a8d6c1dfe | 291 | } |
mbed_official | 15:a81a8d6c1dfe | 292 | |
mbed_official | 15:a81a8d6c1dfe | 293 | /** |
mbed_official | 15:a81a8d6c1dfe | 294 | * \brief Enable the DAC module. |
mbed_official | 15:a81a8d6c1dfe | 295 | * |
mbed_official | 15:a81a8d6c1dfe | 296 | * Enables the DAC interface and the selected output. If any internal reference |
mbed_official | 15:a81a8d6c1dfe | 297 | * is selected it will be enabled. |
mbed_official | 15:a81a8d6c1dfe | 298 | * |
mbed_official | 15:a81a8d6c1dfe | 299 | * \param[in] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 300 | * |
mbed_official | 15:a81a8d6c1dfe | 301 | */ |
mbed_official | 15:a81a8d6c1dfe | 302 | void dac_enable( |
mbed_official | 15:a81a8d6c1dfe | 303 | struct dac_module *const module_inst) |
mbed_official | 15:a81a8d6c1dfe | 304 | { |
mbed_official | 15:a81a8d6c1dfe | 305 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 306 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 307 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 308 | |
mbed_official | 15:a81a8d6c1dfe | 309 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 310 | |
mbed_official | 15:a81a8d6c1dfe | 311 | /* Enable selected output */ |
mbed_official | 15:a81a8d6c1dfe | 312 | dac_module->CTRLB.reg |= module_inst->output; |
mbed_official | 15:a81a8d6c1dfe | 313 | |
mbed_official | 15:a81a8d6c1dfe | 314 | while (dac_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 315 | /* Wait until the synchronization is complete */ |
mbed_official | 15:a81a8d6c1dfe | 316 | } |
mbed_official | 15:a81a8d6c1dfe | 317 | |
mbed_official | 15:a81a8d6c1dfe | 318 | /* Enable the module */ |
mbed_official | 15:a81a8d6c1dfe | 319 | dac_module->CTRLA.reg |= DAC_CTRLA_ENABLE; |
mbed_official | 15:a81a8d6c1dfe | 320 | |
mbed_official | 15:a81a8d6c1dfe | 321 | /* Enable internal bandgap reference if selected in the configuration */ |
mbed_official | 15:a81a8d6c1dfe | 322 | if (module_inst->reference == DAC_REFERENCE_INT1V) { |
mbed_official | 15:a81a8d6c1dfe | 323 | #if (SAMC21) |
mbed_official | 15:a81a8d6c1dfe | 324 | system_voltage_reference_enable(SYSTEM_VOLTAGE_REFERENCE_OUTPUT); |
mbed_official | 15:a81a8d6c1dfe | 325 | } |
mbed_official | 15:a81a8d6c1dfe | 326 | |
mbed_official | 15:a81a8d6c1dfe | 327 | if(dac_module->CTRLA.reg & DAC_CTRLA_ENABLE) { |
mbed_official | 15:a81a8d6c1dfe | 328 | while(! (dac_module->STATUS.reg & DAC_STATUS_READY)) { |
mbed_official | 15:a81a8d6c1dfe | 329 | }; |
mbed_official | 15:a81a8d6c1dfe | 330 | } |
mbed_official | 15:a81a8d6c1dfe | 331 | #else |
mbed_official | 15:a81a8d6c1dfe | 332 | system_voltage_reference_enable(SYSTEM_VOLTAGE_REFERENCE_BANDGAP); |
mbed_official | 15:a81a8d6c1dfe | 333 | } |
mbed_official | 15:a81a8d6c1dfe | 334 | |
mbed_official | 15:a81a8d6c1dfe | 335 | #endif |
mbed_official | 15:a81a8d6c1dfe | 336 | } |
mbed_official | 15:a81a8d6c1dfe | 337 | |
mbed_official | 15:a81a8d6c1dfe | 338 | /** |
mbed_official | 15:a81a8d6c1dfe | 339 | * \brief Disable the DAC module. |
mbed_official | 15:a81a8d6c1dfe | 340 | * |
mbed_official | 15:a81a8d6c1dfe | 341 | * Disables the DAC interface and the output buffer. |
mbed_official | 15:a81a8d6c1dfe | 342 | * |
mbed_official | 15:a81a8d6c1dfe | 343 | * \param[in] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 344 | * |
mbed_official | 15:a81a8d6c1dfe | 345 | */ |
mbed_official | 15:a81a8d6c1dfe | 346 | void dac_disable( |
mbed_official | 15:a81a8d6c1dfe | 347 | struct dac_module *const module_inst) |
mbed_official | 15:a81a8d6c1dfe | 348 | { |
mbed_official | 15:a81a8d6c1dfe | 349 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 350 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 351 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 352 | |
mbed_official | 15:a81a8d6c1dfe | 353 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 354 | |
mbed_official | 15:a81a8d6c1dfe | 355 | /* Wait until the synchronization is complete */ |
mbed_official | 15:a81a8d6c1dfe | 356 | while (dac_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 357 | }; |
mbed_official | 15:a81a8d6c1dfe | 358 | |
mbed_official | 15:a81a8d6c1dfe | 359 | /* Disable DAC */ |
mbed_official | 15:a81a8d6c1dfe | 360 | dac_module->CTRLA.reg &= ~DAC_CTRLA_ENABLE; |
mbed_official | 15:a81a8d6c1dfe | 361 | } |
mbed_official | 15:a81a8d6c1dfe | 362 | |
mbed_official | 15:a81a8d6c1dfe | 363 | /** |
mbed_official | 15:a81a8d6c1dfe | 364 | * \brief Enables a DAC event input or output. |
mbed_official | 15:a81a8d6c1dfe | 365 | * |
mbed_official | 15:a81a8d6c1dfe | 366 | * Enables one or more input or output events to or from the DAC module. See |
mbed_official | 15:a81a8d6c1dfe | 367 | * \ref dac_events "here" for a list of events this module supports. |
mbed_official | 15:a81a8d6c1dfe | 368 | * |
mbed_official | 15:a81a8d6c1dfe | 369 | * \note Events cannot be altered while the module is enabled. |
mbed_official | 15:a81a8d6c1dfe | 370 | * |
mbed_official | 15:a81a8d6c1dfe | 371 | * \param[in] module_inst Software instance for the DAC peripheral |
mbed_official | 15:a81a8d6c1dfe | 372 | * \param[in] events Struct containing flags of events to enable |
mbed_official | 15:a81a8d6c1dfe | 373 | */ |
mbed_official | 15:a81a8d6c1dfe | 374 | void dac_enable_events( |
mbed_official | 15:a81a8d6c1dfe | 375 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 376 | struct dac_events *const events) |
mbed_official | 15:a81a8d6c1dfe | 377 | { |
mbed_official | 15:a81a8d6c1dfe | 378 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 379 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 380 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 381 | Assert(events); |
mbed_official | 15:a81a8d6c1dfe | 382 | |
mbed_official | 15:a81a8d6c1dfe | 383 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 384 | |
mbed_official | 15:a81a8d6c1dfe | 385 | uint32_t event_mask = 0; |
mbed_official | 15:a81a8d6c1dfe | 386 | |
mbed_official | 15:a81a8d6c1dfe | 387 | #if(SAMC21) |
mbed_official | 15:a81a8d6c1dfe | 388 | /* Configure Enable Inversion of input event */ |
mbed_official | 15:a81a8d6c1dfe | 389 | if (events->generate_event_on_chan_falling_edge) { |
mbed_official | 15:a81a8d6c1dfe | 390 | event_mask |= DAC_EVCTRL_INVEI; |
mbed_official | 15:a81a8d6c1dfe | 391 | } |
mbed_official | 15:a81a8d6c1dfe | 392 | #endif |
mbed_official | 15:a81a8d6c1dfe | 393 | |
mbed_official | 15:a81a8d6c1dfe | 394 | /* Configure Buffer Empty event */ |
mbed_official | 15:a81a8d6c1dfe | 395 | if (events->generate_event_on_buffer_empty) { |
mbed_official | 15:a81a8d6c1dfe | 396 | event_mask |= DAC_EVCTRL_EMPTYEO; |
mbed_official | 15:a81a8d6c1dfe | 397 | } |
mbed_official | 15:a81a8d6c1dfe | 398 | |
mbed_official | 15:a81a8d6c1dfe | 399 | /* Configure Conversion Start event */ |
mbed_official | 15:a81a8d6c1dfe | 400 | if (events->on_event_start_conversion) { |
mbed_official | 15:a81a8d6c1dfe | 401 | event_mask |= DAC_EVCTRL_STARTEI; |
mbed_official | 15:a81a8d6c1dfe | 402 | module_inst->start_on_event = true; |
mbed_official | 15:a81a8d6c1dfe | 403 | } |
mbed_official | 15:a81a8d6c1dfe | 404 | |
mbed_official | 15:a81a8d6c1dfe | 405 | dac_module->EVCTRL.reg |= event_mask; |
mbed_official | 15:a81a8d6c1dfe | 406 | } |
mbed_official | 15:a81a8d6c1dfe | 407 | |
mbed_official | 15:a81a8d6c1dfe | 408 | /** |
mbed_official | 15:a81a8d6c1dfe | 409 | * \brief Disables a DAC event input or output. |
mbed_official | 15:a81a8d6c1dfe | 410 | * |
mbed_official | 15:a81a8d6c1dfe | 411 | * Disables one or more input or output events to or from the DAC module. See |
mbed_official | 15:a81a8d6c1dfe | 412 | * \ref dac_events "here" for a list of events this module supports. |
mbed_official | 15:a81a8d6c1dfe | 413 | * |
mbed_official | 15:a81a8d6c1dfe | 414 | * \note Events cannot be altered while the module is enabled. |
mbed_official | 15:a81a8d6c1dfe | 415 | * |
mbed_official | 15:a81a8d6c1dfe | 416 | * \param[in] module_inst Software instance for the DAC peripheral |
mbed_official | 15:a81a8d6c1dfe | 417 | * \param[in] events Struct containing flags of events to disable |
mbed_official | 15:a81a8d6c1dfe | 418 | */ |
mbed_official | 15:a81a8d6c1dfe | 419 | void dac_disable_events( |
mbed_official | 15:a81a8d6c1dfe | 420 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 421 | struct dac_events *const events) |
mbed_official | 15:a81a8d6c1dfe | 422 | { |
mbed_official | 15:a81a8d6c1dfe | 423 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 424 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 425 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 426 | Assert(events); |
mbed_official | 15:a81a8d6c1dfe | 427 | |
mbed_official | 15:a81a8d6c1dfe | 428 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 429 | |
mbed_official | 15:a81a8d6c1dfe | 430 | uint32_t event_mask = 0; |
mbed_official | 15:a81a8d6c1dfe | 431 | |
mbed_official | 15:a81a8d6c1dfe | 432 | /* Configure Buffer Empty event */ |
mbed_official | 15:a81a8d6c1dfe | 433 | if (events->generate_event_on_buffer_empty) { |
mbed_official | 15:a81a8d6c1dfe | 434 | event_mask |= DAC_EVCTRL_EMPTYEO; |
mbed_official | 15:a81a8d6c1dfe | 435 | } |
mbed_official | 15:a81a8d6c1dfe | 436 | |
mbed_official | 15:a81a8d6c1dfe | 437 | /* Configure Conversion Start event */ |
mbed_official | 15:a81a8d6c1dfe | 438 | if (events->on_event_start_conversion) { |
mbed_official | 15:a81a8d6c1dfe | 439 | event_mask |= DAC_EVCTRL_STARTEI; |
mbed_official | 15:a81a8d6c1dfe | 440 | module_inst->start_on_event = false; |
mbed_official | 15:a81a8d6c1dfe | 441 | } |
mbed_official | 15:a81a8d6c1dfe | 442 | |
mbed_official | 15:a81a8d6c1dfe | 443 | dac_module->EVCTRL.reg &= ~event_mask; |
mbed_official | 15:a81a8d6c1dfe | 444 | } |
mbed_official | 15:a81a8d6c1dfe | 445 | |
mbed_official | 15:a81a8d6c1dfe | 446 | /** |
mbed_official | 15:a81a8d6c1dfe | 447 | * \brief Initializes a DAC channel configuration structure to defaults. |
mbed_official | 15:a81a8d6c1dfe | 448 | * |
mbed_official | 15:a81a8d6c1dfe | 449 | * Initializes a given DAC channel configuration structure to a set of |
mbed_official | 15:a81a8d6c1dfe | 450 | * known default values. This function should be called on any new |
mbed_official | 15:a81a8d6c1dfe | 451 | * instance of the configuration structures before being modified by the |
mbed_official | 15:a81a8d6c1dfe | 452 | * user application. |
mbed_official | 15:a81a8d6c1dfe | 453 | * |
mbed_official | 15:a81a8d6c1dfe | 454 | * The default configuration is as follows: |
mbed_official | 15:a81a8d6c1dfe | 455 | * \li Start Conversion Event Input enabled |
mbed_official | 15:a81a8d6c1dfe | 456 | * \li Start Data Buffer Empty Event Output disabled |
mbed_official | 15:a81a8d6c1dfe | 457 | * |
mbed_official | 15:a81a8d6c1dfe | 458 | * \param[out] config Configuration structure to initialize to default values |
mbed_official | 15:a81a8d6c1dfe | 459 | */ |
mbed_official | 15:a81a8d6c1dfe | 460 | void dac_chan_get_config_defaults( |
mbed_official | 15:a81a8d6c1dfe | 461 | struct dac_chan_config *const config) |
mbed_official | 15:a81a8d6c1dfe | 462 | { |
mbed_official | 15:a81a8d6c1dfe | 463 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 464 | Assert(config); |
mbed_official | 15:a81a8d6c1dfe | 465 | } |
mbed_official | 15:a81a8d6c1dfe | 466 | |
mbed_official | 15:a81a8d6c1dfe | 467 | /** |
mbed_official | 15:a81a8d6c1dfe | 468 | * \brief Writes a DAC channel configuration to the hardware module. |
mbed_official | 15:a81a8d6c1dfe | 469 | * |
mbed_official | 15:a81a8d6c1dfe | 470 | * Writes a given channel configuration to the hardware module. |
mbed_official | 15:a81a8d6c1dfe | 471 | * |
mbed_official | 15:a81a8d6c1dfe | 472 | * \note The DAC device instance structure must be initialized before calling |
mbed_official | 15:a81a8d6c1dfe | 473 | * this function. |
mbed_official | 15:a81a8d6c1dfe | 474 | * |
mbed_official | 15:a81a8d6c1dfe | 475 | * \param[in] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 476 | * \param[in] channel Channel to configure |
mbed_official | 15:a81a8d6c1dfe | 477 | * \param[in] config Pointer to the configuration struct |
mbed_official | 15:a81a8d6c1dfe | 478 | * |
mbed_official | 15:a81a8d6c1dfe | 479 | */ |
mbed_official | 15:a81a8d6c1dfe | 480 | void dac_chan_set_config( |
mbed_official | 15:a81a8d6c1dfe | 481 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 482 | const enum dac_channel channel, |
mbed_official | 15:a81a8d6c1dfe | 483 | struct dac_chan_config *const config) |
mbed_official | 15:a81a8d6c1dfe | 484 | { |
mbed_official | 15:a81a8d6c1dfe | 485 | /* No channel support yet */ |
mbed_official | 15:a81a8d6c1dfe | 486 | UNUSED(channel); |
mbed_official | 15:a81a8d6c1dfe | 487 | } |
mbed_official | 15:a81a8d6c1dfe | 488 | |
mbed_official | 15:a81a8d6c1dfe | 489 | /** |
mbed_official | 15:a81a8d6c1dfe | 490 | * \brief Enable a DAC channel. |
mbed_official | 15:a81a8d6c1dfe | 491 | * |
mbed_official | 15:a81a8d6c1dfe | 492 | * Enables the selected DAC channel. |
mbed_official | 15:a81a8d6c1dfe | 493 | * |
mbed_official | 15:a81a8d6c1dfe | 494 | * \param[in] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 495 | * \param[in] channel Channel to enable |
mbed_official | 15:a81a8d6c1dfe | 496 | * |
mbed_official | 15:a81a8d6c1dfe | 497 | */ |
mbed_official | 15:a81a8d6c1dfe | 498 | void dac_chan_enable( |
mbed_official | 15:a81a8d6c1dfe | 499 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 500 | enum dac_channel channel) |
mbed_official | 15:a81a8d6c1dfe | 501 | { |
mbed_official | 15:a81a8d6c1dfe | 502 | /* No channel support yet */ |
mbed_official | 15:a81a8d6c1dfe | 503 | UNUSED(channel); |
mbed_official | 15:a81a8d6c1dfe | 504 | } |
mbed_official | 15:a81a8d6c1dfe | 505 | |
mbed_official | 15:a81a8d6c1dfe | 506 | /** |
mbed_official | 15:a81a8d6c1dfe | 507 | * \brief Disable a DAC channel. |
mbed_official | 15:a81a8d6c1dfe | 508 | * |
mbed_official | 15:a81a8d6c1dfe | 509 | * Disables the selected DAC channel. |
mbed_official | 15:a81a8d6c1dfe | 510 | * |
mbed_official | 15:a81a8d6c1dfe | 511 | * \param[in] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 512 | * \param[in] channel Channel to disable |
mbed_official | 15:a81a8d6c1dfe | 513 | * |
mbed_official | 15:a81a8d6c1dfe | 514 | */ |
mbed_official | 15:a81a8d6c1dfe | 515 | void dac_chan_disable( |
mbed_official | 15:a81a8d6c1dfe | 516 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 517 | enum dac_channel channel) |
mbed_official | 15:a81a8d6c1dfe | 518 | { |
mbed_official | 15:a81a8d6c1dfe | 519 | /* No channel support yet */ |
mbed_official | 15:a81a8d6c1dfe | 520 | UNUSED(channel); |
mbed_official | 15:a81a8d6c1dfe | 521 | } |
mbed_official | 15:a81a8d6c1dfe | 522 | |
mbed_official | 15:a81a8d6c1dfe | 523 | /** |
mbed_official | 15:a81a8d6c1dfe | 524 | * \brief Enable the output buffer. |
mbed_official | 15:a81a8d6c1dfe | 525 | * |
mbed_official | 15:a81a8d6c1dfe | 526 | * Enables the output buffer and drives the DAC output to the VOUT pin. |
mbed_official | 15:a81a8d6c1dfe | 527 | * |
mbed_official | 15:a81a8d6c1dfe | 528 | * \param[in] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 529 | * \param[in] channel DAC channel to alter |
mbed_official | 15:a81a8d6c1dfe | 530 | */ |
mbed_official | 15:a81a8d6c1dfe | 531 | void dac_chan_enable_output_buffer( |
mbed_official | 15:a81a8d6c1dfe | 532 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 533 | enum dac_channel channel) |
mbed_official | 15:a81a8d6c1dfe | 534 | { |
mbed_official | 15:a81a8d6c1dfe | 535 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 536 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 537 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 538 | |
mbed_official | 15:a81a8d6c1dfe | 539 | /* No channel support yet */ |
mbed_official | 15:a81a8d6c1dfe | 540 | UNUSED(channel); |
mbed_official | 15:a81a8d6c1dfe | 541 | |
mbed_official | 15:a81a8d6c1dfe | 542 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 543 | |
mbed_official | 15:a81a8d6c1dfe | 544 | /* Enable output buffer */ |
mbed_official | 15:a81a8d6c1dfe | 545 | dac_module->CTRLB.reg |= DAC_OUTPUT_EXTERNAL; |
mbed_official | 15:a81a8d6c1dfe | 546 | } |
mbed_official | 15:a81a8d6c1dfe | 547 | |
mbed_official | 15:a81a8d6c1dfe | 548 | /** |
mbed_official | 15:a81a8d6c1dfe | 549 | * \brief Disable the output buffer. |
mbed_official | 15:a81a8d6c1dfe | 550 | * |
mbed_official | 15:a81a8d6c1dfe | 551 | * Disables the output buffer. |
mbed_official | 15:a81a8d6c1dfe | 552 | * |
mbed_official | 15:a81a8d6c1dfe | 553 | * \note The output buffer(s) should be disabled when a channel's output is not |
mbed_official | 15:a81a8d6c1dfe | 554 | * currently needed, as it will draw current even if the system is in |
mbed_official | 15:a81a8d6c1dfe | 555 | * sleep mode. |
mbed_official | 15:a81a8d6c1dfe | 556 | * |
mbed_official | 15:a81a8d6c1dfe | 557 | * \param[in] module_inst Pointer to the DAC software instance struct |
mbed_official | 15:a81a8d6c1dfe | 558 | * \param[in] channel DAC channel to alter |
mbed_official | 15:a81a8d6c1dfe | 559 | */ |
mbed_official | 15:a81a8d6c1dfe | 560 | void dac_chan_disable_output_buffer( |
mbed_official | 15:a81a8d6c1dfe | 561 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 562 | enum dac_channel channel) |
mbed_official | 15:a81a8d6c1dfe | 563 | { |
mbed_official | 15:a81a8d6c1dfe | 564 | /* Sanity check arguments*/ |
mbed_official | 15:a81a8d6c1dfe | 565 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 566 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 567 | |
mbed_official | 15:a81a8d6c1dfe | 568 | /* No channel support yet */ |
mbed_official | 15:a81a8d6c1dfe | 569 | UNUSED(channel); |
mbed_official | 15:a81a8d6c1dfe | 570 | |
mbed_official | 15:a81a8d6c1dfe | 571 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 572 | |
mbed_official | 15:a81a8d6c1dfe | 573 | /* Disable output buffer */ |
mbed_official | 15:a81a8d6c1dfe | 574 | dac_module->CTRLB.reg &= ~DAC_OUTPUT_EXTERNAL; |
mbed_official | 15:a81a8d6c1dfe | 575 | } |
mbed_official | 15:a81a8d6c1dfe | 576 | |
mbed_official | 15:a81a8d6c1dfe | 577 | /** |
mbed_official | 15:a81a8d6c1dfe | 578 | * \brief Write to the DAC. |
mbed_official | 15:a81a8d6c1dfe | 579 | * |
mbed_official | 15:a81a8d6c1dfe | 580 | * This function writes to the DATA or DATABUF register. |
mbed_official | 15:a81a8d6c1dfe | 581 | * If the conversion is not event-triggered, the data will be written to |
mbed_official | 15:a81a8d6c1dfe | 582 | * the DATA register and the conversion will start. |
mbed_official | 15:a81a8d6c1dfe | 583 | * If the conversion is event-triggered, the data will be written to DATABUF |
mbed_official | 15:a81a8d6c1dfe | 584 | * and transferred to the DATA register and converted when a Start Conversion |
mbed_official | 15:a81a8d6c1dfe | 585 | * Event is issued. |
mbed_official | 15:a81a8d6c1dfe | 586 | * Conversion data must be right or left adjusted according to configuration |
mbed_official | 15:a81a8d6c1dfe | 587 | * settings. |
mbed_official | 15:a81a8d6c1dfe | 588 | * \note To be event triggered, the enable_start_on_event must be |
mbed_official | 15:a81a8d6c1dfe | 589 | * enabled in the configuration. |
mbed_official | 15:a81a8d6c1dfe | 590 | * |
mbed_official | 15:a81a8d6c1dfe | 591 | * \param[in] module_inst Pointer to the DAC software device struct |
mbed_official | 15:a81a8d6c1dfe | 592 | * \param[in] channel DAC channel to write to |
mbed_official | 15:a81a8d6c1dfe | 593 | * \param[in] data Conversion data |
mbed_official | 15:a81a8d6c1dfe | 594 | * |
mbed_official | 15:a81a8d6c1dfe | 595 | * \return Status of the operation. |
mbed_official | 15:a81a8d6c1dfe | 596 | * \retval STATUS_OK If the data was written |
mbed_official | 15:a81a8d6c1dfe | 597 | */ |
mbed_official | 15:a81a8d6c1dfe | 598 | enum status_code dac_chan_write( |
mbed_official | 15:a81a8d6c1dfe | 599 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 600 | enum dac_channel channel, |
mbed_official | 15:a81a8d6c1dfe | 601 | const uint16_t data) |
mbed_official | 15:a81a8d6c1dfe | 602 | { |
mbed_official | 15:a81a8d6c1dfe | 603 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 604 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 605 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 606 | |
mbed_official | 15:a81a8d6c1dfe | 607 | /* No channel support yet */ |
mbed_official | 15:a81a8d6c1dfe | 608 | UNUSED(channel); |
mbed_official | 15:a81a8d6c1dfe | 609 | |
mbed_official | 15:a81a8d6c1dfe | 610 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 611 | |
mbed_official | 15:a81a8d6c1dfe | 612 | /* Wait until the synchronization is complete */ |
mbed_official | 15:a81a8d6c1dfe | 613 | while (dac_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 614 | }; |
mbed_official | 15:a81a8d6c1dfe | 615 | |
mbed_official | 15:a81a8d6c1dfe | 616 | if (module_inst->start_on_event) { |
mbed_official | 15:a81a8d6c1dfe | 617 | /* Write the new value to the buffered DAC data register */ |
mbed_official | 15:a81a8d6c1dfe | 618 | dac_module->DATABUF.reg = data; |
mbed_official | 15:a81a8d6c1dfe | 619 | } else { |
mbed_official | 15:a81a8d6c1dfe | 620 | /* Write the new value to the DAC data register */ |
mbed_official | 15:a81a8d6c1dfe | 621 | dac_module->DATA.reg = data; |
mbed_official | 15:a81a8d6c1dfe | 622 | } |
mbed_official | 15:a81a8d6c1dfe | 623 | |
mbed_official | 15:a81a8d6c1dfe | 624 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 625 | } |
mbed_official | 15:a81a8d6c1dfe | 626 | |
mbed_official | 15:a81a8d6c1dfe | 627 | /** |
mbed_official | 15:a81a8d6c1dfe | 628 | * \brief Write to the DAC. |
mbed_official | 15:a81a8d6c1dfe | 629 | * |
mbed_official | 15:a81a8d6c1dfe | 630 | * This function converts a specific number of digital data. |
mbed_official | 15:a81a8d6c1dfe | 631 | * The conversion should be event-triggered, the data will be written to DATABUF |
mbed_official | 15:a81a8d6c1dfe | 632 | * and transferred to the DATA register and converted when a Start Conversion |
mbed_official | 15:a81a8d6c1dfe | 633 | * Event is issued. |
mbed_official | 15:a81a8d6c1dfe | 634 | * Conversion data must be right or left adjusted according to configuration |
mbed_official | 15:a81a8d6c1dfe | 635 | * settings. |
mbed_official | 15:a81a8d6c1dfe | 636 | * \note To be event triggered, the enable_start_on_event must be |
mbed_official | 15:a81a8d6c1dfe | 637 | * enabled in the configuration. |
mbed_official | 15:a81a8d6c1dfe | 638 | * |
mbed_official | 15:a81a8d6c1dfe | 639 | * \param[in] module_inst Pointer to the DAC software device struct |
mbed_official | 15:a81a8d6c1dfe | 640 | * \param[in] channel DAC channel to write to |
mbed_official | 15:a81a8d6c1dfe | 641 | * \param[in] buffer Pointer to the digital data write buffer to be converted |
mbed_official | 15:a81a8d6c1dfe | 642 | * \param[in] length Length of the write buffer |
mbed_official | 15:a81a8d6c1dfe | 643 | * |
mbed_official | 15:a81a8d6c1dfe | 644 | * \return Status of the operation. |
mbed_official | 15:a81a8d6c1dfe | 645 | * \retval STATUS_OK If the data was written or no data conversion required |
mbed_official | 15:a81a8d6c1dfe | 646 | * \retval STATUS_ERR_UNSUPPORTED_DEV The DAC is not configured as using event trigger |
mbed_official | 15:a81a8d6c1dfe | 647 | * \retval STATUS_BUSY The DAC is busy to convert |
mbed_official | 15:a81a8d6c1dfe | 648 | */ |
mbed_official | 15:a81a8d6c1dfe | 649 | enum status_code dac_chan_write_buffer_wait( |
mbed_official | 15:a81a8d6c1dfe | 650 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 651 | enum dac_channel channel, |
mbed_official | 15:a81a8d6c1dfe | 652 | uint16_t *buffer, |
mbed_official | 15:a81a8d6c1dfe | 653 | uint32_t length) |
mbed_official | 15:a81a8d6c1dfe | 654 | { |
mbed_official | 15:a81a8d6c1dfe | 655 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 656 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 657 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 658 | |
mbed_official | 15:a81a8d6c1dfe | 659 | /* No channel support yet */ |
mbed_official | 15:a81a8d6c1dfe | 660 | UNUSED(channel); |
mbed_official | 15:a81a8d6c1dfe | 661 | |
mbed_official | 15:a81a8d6c1dfe | 662 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 663 | |
mbed_official | 15:a81a8d6c1dfe | 664 | /* Wait until the synchronization is complete */ |
mbed_official | 15:a81a8d6c1dfe | 665 | while (dac_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 666 | }; |
mbed_official | 15:a81a8d6c1dfe | 667 | |
mbed_official | 15:a81a8d6c1dfe | 668 | /* Zero length request */ |
mbed_official | 15:a81a8d6c1dfe | 669 | if (length == 0) { |
mbed_official | 15:a81a8d6c1dfe | 670 | /* No data to be converted */ |
mbed_official | 15:a81a8d6c1dfe | 671 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 672 | } |
mbed_official | 15:a81a8d6c1dfe | 673 | |
mbed_official | 15:a81a8d6c1dfe | 674 | #if DAC_CALLBACK_MODE == true |
mbed_official | 15:a81a8d6c1dfe | 675 | /* Check if busy */ |
mbed_official | 15:a81a8d6c1dfe | 676 | if (module_inst->job_status == STATUS_BUSY) { |
mbed_official | 15:a81a8d6c1dfe | 677 | return STATUS_BUSY; |
mbed_official | 15:a81a8d6c1dfe | 678 | } |
mbed_official | 15:a81a8d6c1dfe | 679 | #endif |
mbed_official | 15:a81a8d6c1dfe | 680 | |
mbed_official | 15:a81a8d6c1dfe | 681 | /* Only support event triggered conversion */ |
mbed_official | 15:a81a8d6c1dfe | 682 | if (module_inst->start_on_event == false) { |
mbed_official | 15:a81a8d6c1dfe | 683 | return STATUS_ERR_UNSUPPORTED_DEV; |
mbed_official | 15:a81a8d6c1dfe | 684 | } |
mbed_official | 15:a81a8d6c1dfe | 685 | |
mbed_official | 15:a81a8d6c1dfe | 686 | /* Blocks while buffer is being transferred */ |
mbed_official | 15:a81a8d6c1dfe | 687 | while (length--) { |
mbed_official | 15:a81a8d6c1dfe | 688 | /* Convert one data */ |
mbed_official | 15:a81a8d6c1dfe | 689 | dac_chan_write(module_inst, channel, buffer[length]); |
mbed_official | 15:a81a8d6c1dfe | 690 | |
mbed_official | 15:a81a8d6c1dfe | 691 | /* Wait until Transmit is complete or timeout */ |
mbed_official | 15:a81a8d6c1dfe | 692 | for (uint32_t i = 0; i <= DAC_TIMEOUT; i++) { |
mbed_official | 15:a81a8d6c1dfe | 693 | if (dac_module->INTFLAG.reg & DAC_INTFLAG_EMPTY) { |
mbed_official | 15:a81a8d6c1dfe | 694 | break; |
mbed_official | 15:a81a8d6c1dfe | 695 | } else if (i == DAC_TIMEOUT) { |
mbed_official | 15:a81a8d6c1dfe | 696 | return STATUS_ERR_TIMEOUT; |
mbed_official | 15:a81a8d6c1dfe | 697 | } |
mbed_official | 15:a81a8d6c1dfe | 698 | } |
mbed_official | 15:a81a8d6c1dfe | 699 | } |
mbed_official | 15:a81a8d6c1dfe | 700 | |
mbed_official | 15:a81a8d6c1dfe | 701 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 702 | } |
mbed_official | 15:a81a8d6c1dfe | 703 | |
mbed_official | 15:a81a8d6c1dfe | 704 | /** |
mbed_official | 15:a81a8d6c1dfe | 705 | * \brief Retrieves the current module status |
mbed_official | 15:a81a8d6c1dfe | 706 | * |
mbed_official | 15:a81a8d6c1dfe | 707 | * Checks the status of the module and returns it as a bitmask of status |
mbed_official | 15:a81a8d6c1dfe | 708 | * flags. |
mbed_official | 15:a81a8d6c1dfe | 709 | * |
mbed_official | 15:a81a8d6c1dfe | 710 | * \param[in] module_inst Pointer to the DAC software device struct |
mbed_official | 15:a81a8d6c1dfe | 711 | * |
mbed_official | 15:a81a8d6c1dfe | 712 | * \return Bitmask of status flags. |
mbed_official | 15:a81a8d6c1dfe | 713 | * |
mbed_official | 15:a81a8d6c1dfe | 714 | * \retval DAC_STATUS_CHANNEL_0_EMPTY Data has been transferred from DATABUF |
mbed_official | 15:a81a8d6c1dfe | 715 | * to DATA by a start conversion event |
mbed_official | 15:a81a8d6c1dfe | 716 | * and DATABUF is ready for new data |
mbed_official | 15:a81a8d6c1dfe | 717 | * \retval DAC_STATUS_CHANNEL_0_UNDERRUN A start conversion event has occurred |
mbed_official | 15:a81a8d6c1dfe | 718 | * when DATABUF is empty |
mbed_official | 15:a81a8d6c1dfe | 719 | * |
mbed_official | 15:a81a8d6c1dfe | 720 | */ |
mbed_official | 15:a81a8d6c1dfe | 721 | uint32_t dac_get_status( |
mbed_official | 15:a81a8d6c1dfe | 722 | struct dac_module *const module_inst) |
mbed_official | 15:a81a8d6c1dfe | 723 | { |
mbed_official | 15:a81a8d6c1dfe | 724 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 725 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 726 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 727 | |
mbed_official | 15:a81a8d6c1dfe | 728 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 729 | |
mbed_official | 15:a81a8d6c1dfe | 730 | uint8_t intflags = dac_module->INTFLAG.reg; |
mbed_official | 15:a81a8d6c1dfe | 731 | uint32_t status_flags = 0; |
mbed_official | 15:a81a8d6c1dfe | 732 | |
mbed_official | 15:a81a8d6c1dfe | 733 | if (intflags & DAC_INTFLAG_EMPTY) { |
mbed_official | 15:a81a8d6c1dfe | 734 | status_flags |= DAC_STATUS_CHANNEL_0_EMPTY; |
mbed_official | 15:a81a8d6c1dfe | 735 | } |
mbed_official | 15:a81a8d6c1dfe | 736 | |
mbed_official | 15:a81a8d6c1dfe | 737 | if (intflags & DAC_INTFLAG_UNDERRUN) { |
mbed_official | 15:a81a8d6c1dfe | 738 | status_flags |= DAC_STATUS_CHANNEL_0_UNDERRUN; |
mbed_official | 15:a81a8d6c1dfe | 739 | } |
mbed_official | 15:a81a8d6c1dfe | 740 | |
mbed_official | 15:a81a8d6c1dfe | 741 | return status_flags; |
mbed_official | 15:a81a8d6c1dfe | 742 | } |
mbed_official | 15:a81a8d6c1dfe | 743 | |
mbed_official | 15:a81a8d6c1dfe | 744 | /** |
mbed_official | 15:a81a8d6c1dfe | 745 | * \brief Clears a module status flag |
mbed_official | 15:a81a8d6c1dfe | 746 | * |
mbed_official | 15:a81a8d6c1dfe | 747 | * Clears the given status flag of the module. |
mbed_official | 15:a81a8d6c1dfe | 748 | * |
mbed_official | 15:a81a8d6c1dfe | 749 | * \param[in] module_inst Pointer to the DAC software device struct |
mbed_official | 15:a81a8d6c1dfe | 750 | * \param[in] status_flags Bit mask of status flags to clear |
mbed_official | 15:a81a8d6c1dfe | 751 | * |
mbed_official | 15:a81a8d6c1dfe | 752 | */ |
mbed_official | 15:a81a8d6c1dfe | 753 | void dac_clear_status( |
mbed_official | 15:a81a8d6c1dfe | 754 | struct dac_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 755 | uint32_t status_flags) |
mbed_official | 15:a81a8d6c1dfe | 756 | { |
mbed_official | 15:a81a8d6c1dfe | 757 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 758 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 759 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 760 | |
mbed_official | 15:a81a8d6c1dfe | 761 | Dac *const dac_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 762 | |
mbed_official | 15:a81a8d6c1dfe | 763 | uint32_t intflags = 0; |
mbed_official | 15:a81a8d6c1dfe | 764 | |
mbed_official | 15:a81a8d6c1dfe | 765 | if (status_flags & DAC_STATUS_CHANNEL_0_EMPTY) { |
mbed_official | 15:a81a8d6c1dfe | 766 | intflags |= DAC_INTFLAG_EMPTY; |
mbed_official | 15:a81a8d6c1dfe | 767 | } |
mbed_official | 15:a81a8d6c1dfe | 768 | |
mbed_official | 15:a81a8d6c1dfe | 769 | if (status_flags & DAC_STATUS_CHANNEL_0_UNDERRUN) { |
mbed_official | 15:a81a8d6c1dfe | 770 | intflags |= DAC_INTFLAG_UNDERRUN; |
mbed_official | 15:a81a8d6c1dfe | 771 | } |
mbed_official | 15:a81a8d6c1dfe | 772 | |
mbed_official | 15:a81a8d6c1dfe | 773 | dac_module->INTFLAG.reg = intflags; |
mbed_official | 15:a81a8d6c1dfe | 774 | } |