mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Fri Sep 11 09:30:09 2015 +0100
Revision:
621:9c82b0f79f3d
Parent:
514:7668256dbe61
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538

Full URL: https://github.com/mbedmicro/mbed/commit/6c1d63e069ab9bd86de92e8296ca783681257538/

ignore target files not supported by the yotta module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 514:7668256dbe61 1 /*******************************************************************************
mbed_official 514:7668256dbe61 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
mbed_official 514:7668256dbe61 3 *
mbed_official 514:7668256dbe61 4 * Permission is hereby granted, free of charge, to any person obtaining a
mbed_official 514:7668256dbe61 5 * copy of this software and associated documentation files (the "Software"),
mbed_official 514:7668256dbe61 6 * to deal in the Software without restriction, including without limitation
mbed_official 514:7668256dbe61 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
mbed_official 514:7668256dbe61 8 * and/or sell copies of the Software, and to permit persons to whom the
mbed_official 514:7668256dbe61 9 * Software is furnished to do so, subject to the following conditions:
mbed_official 514:7668256dbe61 10 *
mbed_official 514:7668256dbe61 11 * The above copyright notice and this permission notice shall be included
mbed_official 514:7668256dbe61 12 * in all copies or substantial portions of the Software.
mbed_official 514:7668256dbe61 13 *
mbed_official 514:7668256dbe61 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
mbed_official 514:7668256dbe61 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
mbed_official 514:7668256dbe61 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
mbed_official 514:7668256dbe61 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
mbed_official 514:7668256dbe61 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
mbed_official 514:7668256dbe61 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
mbed_official 514:7668256dbe61 20 * OTHER DEALINGS IN THE SOFTWARE.
mbed_official 514:7668256dbe61 21 *
mbed_official 514:7668256dbe61 22 * Except as contained in this notice, the name of Maxim Integrated
mbed_official 514:7668256dbe61 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
mbed_official 514:7668256dbe61 24 * Products, Inc. Branding Policy.
mbed_official 514:7668256dbe61 25 *
mbed_official 514:7668256dbe61 26 * The mere transfer of this software does not imply any licenses
mbed_official 514:7668256dbe61 27 * of trade secrets, proprietary technology, copyrights, patents,
mbed_official 514:7668256dbe61 28 * trademarks, maskwork rights, or any other form of intellectual
mbed_official 514:7668256dbe61 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
mbed_official 514:7668256dbe61 30 * ownership rights.
mbed_official 514:7668256dbe61 31 *******************************************************************************
mbed_official 514:7668256dbe61 32 */
mbed_official 514:7668256dbe61 33
mbed_official 514:7668256dbe61 34 #include "mbed_assert.h"
mbed_official 514:7668256dbe61 35 #include "analogout_api.h"
mbed_official 514:7668256dbe61 36 #include "clkman_regs.h"
mbed_official 514:7668256dbe61 37 #include "pwrman_regs.h"
mbed_official 514:7668256dbe61 38 #include "afe_regs.h"
mbed_official 514:7668256dbe61 39 #include "PeripheralPins.h"
mbed_official 514:7668256dbe61 40
mbed_official 514:7668256dbe61 41 //******************************************************************************
mbed_official 514:7668256dbe61 42 void analogout_init(dac_t *obj, PinName pin)
mbed_official 514:7668256dbe61 43 {
mbed_official 514:7668256dbe61 44 // Make sure pin is an analog pin we can use for ADC
mbed_official 514:7668256dbe61 45 DACName dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
mbed_official 514:7668256dbe61 46 MBED_ASSERT((DACName)dac != (DACName)NC);
mbed_official 514:7668256dbe61 47
mbed_official 514:7668256dbe61 48 // Set the object pointer
mbed_official 514:7668256dbe61 49 obj->dac = ((mxc_dac_regs_t*)MXC_DAC_GET_DAC((pin & 0x3)));
mbed_official 514:7668256dbe61 50 obj->dac_fifo = ((mxc_dac_fifo_t*)MXC_DAC_GET_FIFO((pin & 0x3)));
mbed_official 514:7668256dbe61 51 obj->index = (pin & 0x3);
mbed_official 514:7668256dbe61 52
mbed_official 514:7668256dbe61 53 // Set the ADC clock to the system clock frequency
mbed_official 514:7668256dbe61 54 MXC_SET_FIELD(&MXC_CLKMAN->clk_ctrl, MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT,
mbed_official 514:7668256dbe61 55 (MXC_F_CLKMAN_CLK_CTRL_ADC_GATE_N | (MXC_E_CLKMAN_ADC_SOURCE_SELECT_SYSTEM <<
mbed_official 514:7668256dbe61 56 MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT_POS)));
mbed_official 514:7668256dbe61 57
mbed_official 514:7668256dbe61 58
mbed_official 514:7668256dbe61 59 // Setup the OPAMP in follower mode
mbed_official 514:7668256dbe61 60 switch(obj->index) {
mbed_official 514:7668256dbe61 61 case 0:
mbed_official 514:7668256dbe61 62 // Enable DAC clock
mbed_official 514:7668256dbe61 63 MXC_CLKMAN->clk_ctrl_14_dac0 = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
mbed_official 514:7668256dbe61 64
mbed_official 514:7668256dbe61 65 // Enable OPAMP
mbed_official 514:7668256dbe61 66 MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP0;
mbed_official 514:7668256dbe61 67
mbed_official 514:7668256dbe61 68 // Set the positive and negative inputs
mbed_official 514:7668256dbe61 69 MXC_SET_FIELD(&MXC_AFE->ctrl4, (MXC_F_AFE_CTRL4_DAC_SEL_A |
mbed_official 514:7668256dbe61 70 MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP0 | MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP0),
mbed_official 514:7668256dbe61 71 ((0x1 << MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP0_POS) |
mbed_official 514:7668256dbe61 72 (0x1 << MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP0_POS) |
mbed_official 514:7668256dbe61 73 (0x0 << MXC_F_AFE_CTRL4_DAC_SEL_A_POS)));
mbed_official 514:7668256dbe61 74
mbed_official 514:7668256dbe61 75 // Enable N and P channel inputs
mbed_official 514:7668256dbe61 76 MXC_AFE->ctrl3 |= (MXC_F_AFE_CTRL3_EN_PCH_OPAMP0 |
mbed_official 514:7668256dbe61 77 MXC_F_AFE_CTRL3_EN_NCH_OPAMP0);
mbed_official 514:7668256dbe61 78 break;
mbed_official 514:7668256dbe61 79 case 1:
mbed_official 514:7668256dbe61 80 // Enable DAC clock
mbed_official 514:7668256dbe61 81 MXC_CLKMAN->clk_ctrl_15_dac1 = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
mbed_official 514:7668256dbe61 82
mbed_official 514:7668256dbe61 83 // Enable OPAMP
mbed_official 514:7668256dbe61 84 MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP1;
mbed_official 514:7668256dbe61 85
mbed_official 514:7668256dbe61 86 // Set the positive and negative inputs
mbed_official 514:7668256dbe61 87 MXC_SET_FIELD(&MXC_AFE->ctrl4, (MXC_F_AFE_CTRL4_DAC_SEL_B |
mbed_official 514:7668256dbe61 88 MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP1 | MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP1),
mbed_official 514:7668256dbe61 89 ((0x1 << MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP1_POS) |
mbed_official 514:7668256dbe61 90 (0x1 << MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP1_POS) |
mbed_official 514:7668256dbe61 91 (0x1 << MXC_F_AFE_CTRL4_DAC_SEL_B_POS)));
mbed_official 514:7668256dbe61 92
mbed_official 514:7668256dbe61 93 // Enable N and P channel inputs
mbed_official 514:7668256dbe61 94 MXC_AFE->ctrl3 |= (MXC_F_AFE_CTRL3_EN_PCH_OPAMP1 |
mbed_official 514:7668256dbe61 95 MXC_F_AFE_CTRL3_EN_NCH_OPAMP1);
mbed_official 514:7668256dbe61 96
mbed_official 514:7668256dbe61 97 break;
mbed_official 514:7668256dbe61 98 case 2:
mbed_official 514:7668256dbe61 99 // Enable DAC clock
mbed_official 514:7668256dbe61 100 MXC_CLKMAN->clk_ctrl_16_dac2 = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
mbed_official 514:7668256dbe61 101
mbed_official 514:7668256dbe61 102 // Enable OPAMP
mbed_official 514:7668256dbe61 103 MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP2;
mbed_official 514:7668256dbe61 104
mbed_official 514:7668256dbe61 105 // Set the positive and negative inputs
mbed_official 514:7668256dbe61 106 MXC_SET_FIELD(&MXC_AFE->ctrl4, (MXC_F_AFE_CTRL4_DAC_SEL_C |
mbed_official 514:7668256dbe61 107 MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP2 | MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP2),
mbed_official 514:7668256dbe61 108 ((0x1 << MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP2_POS) |
mbed_official 514:7668256dbe61 109 (0x1 << MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP2_POS) |
mbed_official 514:7668256dbe61 110 (0x2 << MXC_F_AFE_CTRL4_DAC_SEL_C_POS)));
mbed_official 514:7668256dbe61 111
mbed_official 514:7668256dbe61 112 // Enable N and P channel inputs
mbed_official 514:7668256dbe61 113 MXC_AFE->ctrl3 |= (MXC_F_AFE_CTRL3_EN_PCH_OPAMP2 |
mbed_official 514:7668256dbe61 114 MXC_F_AFE_CTRL3_EN_NCH_OPAMP2);
mbed_official 514:7668256dbe61 115 break;
mbed_official 514:7668256dbe61 116 case 3:
mbed_official 514:7668256dbe61 117 // Enable DAC clock
mbed_official 514:7668256dbe61 118 MXC_CLKMAN->clk_ctrl_17_dac3 = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
mbed_official 514:7668256dbe61 119
mbed_official 514:7668256dbe61 120 // Enable OPAMP
mbed_official 514:7668256dbe61 121 MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP3;
mbed_official 514:7668256dbe61 122
mbed_official 514:7668256dbe61 123 // Set the positive and negative inputs
mbed_official 514:7668256dbe61 124 MXC_SET_FIELD(&MXC_AFE->ctrl4, (MXC_F_AFE_CTRL4_DAC_SEL_D |
mbed_official 514:7668256dbe61 125 MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP3 | MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP3),
mbed_official 514:7668256dbe61 126 ((0x1 << MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP3_POS) |
mbed_official 514:7668256dbe61 127 (0x1 << MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP3_POS) |
mbed_official 514:7668256dbe61 128 (0x3 << MXC_F_AFE_CTRL4_DAC_SEL_D_POS)));
mbed_official 514:7668256dbe61 129
mbed_official 514:7668256dbe61 130 // Enable N and P channel inputs
mbed_official 514:7668256dbe61 131 MXC_AFE->ctrl3 |= (MXC_F_AFE_CTRL3_EN_PCH_OPAMP3 |
mbed_official 514:7668256dbe61 132 MXC_F_AFE_CTRL3_EN_NCH_OPAMP3);
mbed_official 514:7668256dbe61 133 break;
mbed_official 514:7668256dbe61 134 }
mbed_official 514:7668256dbe61 135
mbed_official 514:7668256dbe61 136 // Enable AFE power
mbed_official 514:7668256dbe61 137 MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED;
mbed_official 514:7668256dbe61 138
mbed_official 514:7668256dbe61 139 // Setup internal voltage references
mbed_official 514:7668256dbe61 140 MXC_SET_FIELD(&MXC_AFE->ctrl1, (MXC_F_AFE_CTRL1_REF_DAC_VOLT_SEL | MXC_F_AFE_CTRL1_REF_ADC_VOLT_SEL),
mbed_official 514:7668256dbe61 141 (MXC_F_AFE_CTRL1_REF_ADC_POWERUP | MXC_F_AFE_CTRL1_REF_BLK_POWERUP |
mbed_official 514:7668256dbe61 142 (MXC_E_AFE_REF_VOLT_SEL_1500 << MXC_F_AFE_CTRL1_REF_ADC_VOLT_SEL_POS)));
mbed_official 514:7668256dbe61 143
mbed_official 514:7668256dbe61 144 // Disable interpolation
mbed_official 514:7668256dbe61 145 obj->dac->ctrl0 &= MXC_F_DAC_CTRL0_INTERP_MODE;
mbed_official 514:7668256dbe61 146 }
mbed_official 514:7668256dbe61 147
mbed_official 514:7668256dbe61 148 //******************************************************************************
mbed_official 514:7668256dbe61 149 void analogout_write(dac_t *obj, float value)
mbed_official 514:7668256dbe61 150 {
mbed_official 514:7668256dbe61 151 analogout_write_u16(obj, (uint16_t)((value/1.0) * 0xFFFF));
mbed_official 514:7668256dbe61 152 }
mbed_official 514:7668256dbe61 153
mbed_official 514:7668256dbe61 154 //******************************************************************************
mbed_official 514:7668256dbe61 155 void analogout_write_u16(dac_t *obj, uint16_t value)
mbed_official 514:7668256dbe61 156 {
mbed_official 514:7668256dbe61 157 // Enable the OPAMP
mbed_official 514:7668256dbe61 158 // Setup the OPAMP in follower mode
mbed_official 514:7668256dbe61 159 switch(obj->index) {
mbed_official 514:7668256dbe61 160 case 0:
mbed_official 514:7668256dbe61 161 MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP0;
mbed_official 514:7668256dbe61 162 break;
mbed_official 514:7668256dbe61 163 case 1:
mbed_official 514:7668256dbe61 164 MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP1;
mbed_official 514:7668256dbe61 165 break;
mbed_official 514:7668256dbe61 166 case 2:
mbed_official 514:7668256dbe61 167 MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP2;
mbed_official 514:7668256dbe61 168 break;
mbed_official 514:7668256dbe61 169 case 3:
mbed_official 514:7668256dbe61 170 MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP3;
mbed_official 514:7668256dbe61 171 break;
mbed_official 514:7668256dbe61 172 }
mbed_official 514:7668256dbe61 173
mbed_official 514:7668256dbe61 174 // Output 1 sample with minimal delay
mbed_official 514:7668256dbe61 175 obj->dac->rate |= 0x1;
mbed_official 514:7668256dbe61 176
mbed_official 514:7668256dbe61 177 // Set the start mode to output once data is in the FIFO
mbed_official 514:7668256dbe61 178 obj->dac->ctrl0 &= ~(MXC_F_DAC_CTRL0_START_MODE | MXC_F_DAC_CTRL0_OP_MODE);
mbed_official 514:7668256dbe61 179
mbed_official 514:7668256dbe61 180 // Enable the DAC
mbed_official 514:7668256dbe61 181 obj->dac->ctrl0 |= (MXC_F_DAC_CTRL0_POWER_MODE_2 |
mbed_official 514:7668256dbe61 182 MXC_F_DAC_CTRL0_POWER_MODE_1_0 | MXC_F_DAC_CTRL0_POWER_ON |
mbed_official 514:7668256dbe61 183 MXC_F_DAC_CTRL0_CLOCK_GATE_EN | MXC_F_DAC_CTRL0_CPU_START);
mbed_official 514:7668256dbe61 184
mbed_official 514:7668256dbe61 185 if(obj->index < 2) {
mbed_official 514:7668256dbe61 186 obj->out = (value);
mbed_official 514:7668256dbe61 187 obj->dac_fifo->output_16 = (obj->out);
mbed_official 514:7668256dbe61 188
mbed_official 514:7668256dbe61 189 } else {
mbed_official 514:7668256dbe61 190 // Convert 16 bits to 8 bits
mbed_official 514:7668256dbe61 191 obj->out = (value >> 8);
mbed_official 514:7668256dbe61 192 obj->dac_fifo->output_8 = (obj->out);
mbed_official 514:7668256dbe61 193 }
mbed_official 514:7668256dbe61 194 }
mbed_official 514:7668256dbe61 195
mbed_official 514:7668256dbe61 196 //******************************************************************************
mbed_official 514:7668256dbe61 197 float analogout_read(dac_t *obj)
mbed_official 514:7668256dbe61 198 {
mbed_official 514:7668256dbe61 199 return (((float)analogout_read_u16(obj) / (float)0xFFFF) * 1.5);
mbed_official 514:7668256dbe61 200 }
mbed_official 514:7668256dbe61 201
mbed_official 514:7668256dbe61 202 //******************************************************************************
mbed_official 514:7668256dbe61 203 uint16_t analogout_read_u16(dac_t *obj)
mbed_official 514:7668256dbe61 204 {
mbed_official 514:7668256dbe61 205 if(obj->index < 2) {
mbed_official 514:7668256dbe61 206 // Convert 12 bits to 16 bits
mbed_official 514:7668256dbe61 207 return (obj->out << 4);
mbed_official 514:7668256dbe61 208 } else {
mbed_official 514:7668256dbe61 209 // Convert 8 bits to 16 bits
mbed_official 514:7668256dbe61 210 return (obj->out << 8);
mbed_official 514:7668256dbe61 211 }
mbed_official 514:7668256dbe61 212 }