mbed library sources. Supersedes mbed-src. removed serial

Fork of mbed-dev by mbed official

Committer:
mrcrsch
Date:
Mon Feb 13 14:26:36 2017 +0000
Revision:
158:aa577577efe0
Parent:
149:156823d33999
removed stdio and serial to reduce size

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /*******************************************************************************
<> 149:156823d33999 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 149:156823d33999 3 *
<> 149:156823d33999 4 * Permission is hereby granted, free of charge, to any person obtaining a
<> 149:156823d33999 5 * copy of this software and associated documentation files (the "Software"),
<> 149:156823d33999 6 * to deal in the Software without restriction, including without limitation
<> 149:156823d33999 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 149:156823d33999 8 * and/or sell copies of the Software, and to permit persons to whom the
<> 149:156823d33999 9 * Software is furnished to do so, subject to the following conditions:
<> 149:156823d33999 10 *
<> 149:156823d33999 11 * The above copyright notice and this permission notice shall be included
<> 149:156823d33999 12 * in all copies or substantial portions of the Software.
<> 149:156823d33999 13 *
<> 149:156823d33999 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 149:156823d33999 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 149:156823d33999 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 149:156823d33999 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 149:156823d33999 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 149:156823d33999 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 149:156823d33999 20 * OTHER DEALINGS IN THE SOFTWARE.
<> 149:156823d33999 21 *
<> 149:156823d33999 22 * Except as contained in this notice, the name of Maxim Integrated
<> 149:156823d33999 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 149:156823d33999 24 * Products, Inc. Branding Policy.
<> 149:156823d33999 25 *
<> 149:156823d33999 26 * The mere transfer of this software does not imply any licenses
<> 149:156823d33999 27 * of trade secrets, proprietary technology, copyrights, patents,
<> 149:156823d33999 28 * trademarks, maskwork rights, or any other form of intellectual
<> 149:156823d33999 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 149:156823d33999 30 * ownership rights.
<> 149:156823d33999 31 *******************************************************************************
<> 149:156823d33999 32 */
<> 149:156823d33999 33
<> 149:156823d33999 34 #include "mbed_assert.h"
<> 149:156823d33999 35 #include "analogin_api.h"
<> 149:156823d33999 36 #include "clkman_regs.h"
<> 149:156823d33999 37 #include "pwrman_regs.h"
<> 149:156823d33999 38 #include "trim_regs.h"
<> 149:156823d33999 39 #include "PeripheralPins.h"
<> 149:156823d33999 40
<> 149:156823d33999 41 #define PGA_TRK_CNT 0x1F
<> 149:156823d33999 42 #define ADC_ACT_CNT 0x1
<> 149:156823d33999 43 #define ADC_PGA_CNT 0x1
<> 149:156823d33999 44 #define ADC_ACQ_CNT 0x1
<> 149:156823d33999 45 #define ADC_SLP_CNT 0x1
<> 149:156823d33999 46
<> 149:156823d33999 47 #define ADC_FULL_SCALE 0x3FF
<> 149:156823d33999 48 #define ADC_EXTERNAL_LAST_INPUT 3
<> 149:156823d33999 49
<> 149:156823d33999 50 // Only allow initialization once
<> 149:156823d33999 51 static int initialized = 0;
<> 149:156823d33999 52
<> 149:156823d33999 53 //******************************************************************************
<> 149:156823d33999 54 void analogin_init(analogin_t *obj, PinName pin)
<> 149:156823d33999 55 {
<> 149:156823d33999 56 // Make sure pin is an analog pin we can use for ADC
<> 149:156823d33999 57 MBED_ASSERT((ADCName)pinmap_peripheral(pin, PinMap_ADC) != (ADCName)NC);
<> 149:156823d33999 58
<> 149:156823d33999 59 // Set the object pointer
<> 149:156823d33999 60 obj->adc = MXC_ADC;
<> 149:156823d33999 61 obj->adc_pin = pin;
<> 149:156823d33999 62
<> 149:156823d33999 63 if (initialized == 0) {
<> 149:156823d33999 64 // Enable AFE power
<> 149:156823d33999 65 MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED;
<> 149:156823d33999 66
<> 149:156823d33999 67 // Enable the clock
<> 149:156823d33999 68 MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_ADC_CLOCK_ENABLE;
<> 149:156823d33999 69
<> 149:156823d33999 70 // Enable clock gate
<> 149:156823d33999 71 MXC_CLKMAN->clk_gate_ctrl2 |= MXC_F_CLKMAN_CLK_GATE_CTRL2_ADC_CLK_GATER;
<> 149:156823d33999 72
<> 149:156823d33999 73 // Enable interface clock
<> 149:156823d33999 74 obj->adc->ctrl |= MXC_F_ADC_CTRL_ADC_CLK_EN;
<> 149:156823d33999 75
<> 149:156823d33999 76 if ((MXC_TRIM->reg11_adc_trim0 == 0xFFFFFFFF) && (MXC_TRIM->reg12_adc_trim1 == 0xFFFFFFFF)) {
<> 149:156823d33999 77 // Set default trim for untrimmed parts.
<> 149:156823d33999 78 MXC_TRIM->reg11_adc_trim0 = 0x02000200;
<> 149:156823d33999 79 MXC_TRIM->reg12_adc_trim1 = 0x02000200;
<> 149:156823d33999 80 }
<> 149:156823d33999 81
<> 149:156823d33999 82 // Clear ADC ready interrupt (wite 1 to clr)
<> 149:156823d33999 83 obj->adc->intr = (obj->adc->intr & 0xFFFF) | MXC_F_ADC_INTR_ADC_REF_READY_IF;
<> 149:156823d33999 84
<> 149:156823d33999 85 // Using internal reference of 1.20V
<> 149:156823d33999 86
<> 149:156823d33999 87 // Enable ADC power bypass the buffer
<> 149:156823d33999 88 obj->adc->ctrl |= (MXC_F_ADC_CTRL_ADC_PU | MXC_F_ADC_CTRL_ADC_REFBUF_PU |
<> 149:156823d33999 89 MXC_F_ADC_CTRL_ADC_CHGPUMP_PU | MXC_F_ADC_CTRL_BUF_BYPASS);
<> 149:156823d33999 90
<> 149:156823d33999 91 // Wait for ADC ready
<> 149:156823d33999 92 while (!(obj->adc->intr & MXC_F_ADC_INTR_ADC_REF_READY_IF));
<> 149:156823d33999 93
<> 149:156823d33999 94 initialized = 1;
<> 149:156823d33999 95 }
<> 149:156823d33999 96 }
<> 149:156823d33999 97
<> 149:156823d33999 98 //******************************************************************************
<> 149:156823d33999 99 float analogin_read(analogin_t *obj)
<> 149:156823d33999 100 {
<> 149:156823d33999 101 // Convert integer to float
<> 149:156823d33999 102 return (((float)analogin_read_u16(obj)/(float)ADC_FULL_SCALE));
<> 149:156823d33999 103 }
<> 149:156823d33999 104
<> 149:156823d33999 105 //******************************************************************************
<> 149:156823d33999 106 uint16_t analogin_read_u16(analogin_t *obj)
<> 149:156823d33999 107 {
<> 149:156823d33999 108 // Set the pin to take readings from
<> 149:156823d33999 109 uint32_t adc_input = PINNAME_TO_PIN(obj->adc_pin);
<> 149:156823d33999 110
<> 149:156823d33999 111 // Select the channel
<> 149:156823d33999 112 obj->adc->ctrl &= ~MXC_F_ADC_CTRL_ADC_CHSEL;
<> 149:156823d33999 113 obj->adc->ctrl |= (adc_input << MXC_F_ADC_CTRL_ADC_CHSEL_POS) & MXC_F_ADC_CTRL_ADC_CHSEL;
<> 149:156823d33999 114
<> 149:156823d33999 115 // We want unity gain, to get full 0-Vref range
<> 149:156823d33999 116 // So, both ref and adc input scale should be enabled
<> 149:156823d33999 117 obj->adc->ctrl |= MXC_F_ADC_CTRL_ADC_SCALE | MXC_F_ADC_CTRL_ADC_REFSCL;
<> 149:156823d33999 118
<> 149:156823d33999 119 // Not using internal buffer, disable anyway
<> 149:156823d33999 120 obj->adc->ctrl &= ~MXC_F_ADC_CTRL_BUF_PU;
<> 149:156823d33999 121 obj->adc->ctrl |= MXC_F_ADC_CTRL_BUF_BYPASS;
<> 149:156823d33999 122
<> 149:156823d33999 123 // Normal LSB justified data alignment
<> 149:156823d33999 124
<> 149:156823d33999 125 // Not using limits
<> 149:156823d33999 126
<> 149:156823d33999 127 // Clear ADC done flag (wite 1 to clr)
<> 149:156823d33999 128 obj->adc->intr = (obj->adc->intr & 0xFFFF) | MXC_F_ADC_INTR_ADC_DONE_IF;
<> 149:156823d33999 129
<> 149:156823d33999 130 // Start the conversion
<> 149:156823d33999 131 obj->adc->ctrl |= MXC_F_ADC_CTRL_CPU_ADC_START;
<> 149:156823d33999 132
<> 149:156823d33999 133 // Wait for ADC done
<> 149:156823d33999 134 while (!(obj->adc->intr & MXC_F_ADC_INTR_ADC_DONE_IF));
<> 149:156823d33999 135
<> 149:156823d33999 136 // Get sample from the fifo
<> 149:156823d33999 137 uint16_t sample = obj->adc->data;
<> 149:156823d33999 138
<> 149:156823d33999 139 // Check for overflow, hardware will report overflow as 0
<> 149:156823d33999 140 if (obj->adc->status & MXC_F_ADC_STATUS_ADC_OVERFLOW) {
<> 149:156823d33999 141 sample = (uint16_t)ADC_FULL_SCALE;
<> 149:156823d33999 142 }
<> 149:156823d33999 143
<> 149:156823d33999 144 return sample;
<> 149:156823d33999 145 }