mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Oct 09 08:15:07 2014 +0100
Revision:
340:28d1f895c6fe
Child:
344:ed24f9926218
Synchronized with git revision b5a4c8e80393336b2656fb29ab46d405d3068602

Full URL: https://github.com/mbedmicro/mbed/commit/b5a4c8e80393336b2656fb29ab46d405d3068602/

HAL: nrf51822 - Few fixes for PWM and Serial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 340:28d1f895c6fe 1 /* mbed Microcontroller Library
mbed_official 340:28d1f895c6fe 2 * Copyright (c) 2014, STMicroelectronics
mbed_official 340:28d1f895c6fe 3 * All rights reserved.
mbed_official 340:28d1f895c6fe 4 *
mbed_official 340:28d1f895c6fe 5 * Redistribution and use in source and binary forms, with or without
mbed_official 340:28d1f895c6fe 6 * modification, are permitted provided that the following conditions are met:
mbed_official 340:28d1f895c6fe 7 *
mbed_official 340:28d1f895c6fe 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 340:28d1f895c6fe 9 * this list of conditions and the following disclaimer.
mbed_official 340:28d1f895c6fe 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 340:28d1f895c6fe 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 340:28d1f895c6fe 12 * and/or other materials provided with the distribution.
mbed_official 340:28d1f895c6fe 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 340:28d1f895c6fe 14 * may be used to endorse or promote products derived from this software
mbed_official 340:28d1f895c6fe 15 * without specific prior written permission.
mbed_official 340:28d1f895c6fe 16 *
mbed_official 340:28d1f895c6fe 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 340:28d1f895c6fe 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 340:28d1f895c6fe 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 340:28d1f895c6fe 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 340:28d1f895c6fe 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 340:28d1f895c6fe 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 340:28d1f895c6fe 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 340:28d1f895c6fe 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 340:28d1f895c6fe 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 340:28d1f895c6fe 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 340:28d1f895c6fe 27 */
mbed_official 340:28d1f895c6fe 28 #include "mbed_assert.h"
mbed_official 340:28d1f895c6fe 29 #include "analogin_api.h"
mbed_official 340:28d1f895c6fe 30
mbed_official 340:28d1f895c6fe 31 #if DEVICE_ANALOGIN
mbed_official 340:28d1f895c6fe 32
mbed_official 340:28d1f895c6fe 33 #include "wait_api.h"
mbed_official 340:28d1f895c6fe 34 #include "cmsis.h"
mbed_official 340:28d1f895c6fe 35 #include "pinmap.h"
mbed_official 340:28d1f895c6fe 36
mbed_official 340:28d1f895c6fe 37 static const PinMap PinMap_ADC[] = {
mbed_official 340:28d1f895c6fe 38 {PA_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN0
mbed_official 340:28d1f895c6fe 39 {PA_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN1
mbed_official 340:28d1f895c6fe 40 {PA_2, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN2
mbed_official 340:28d1f895c6fe 41 {PA_3, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN3
mbed_official 340:28d1f895c6fe 42 {PA_4, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN4
mbed_official 340:28d1f895c6fe 43 {PA_5, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN5
mbed_official 340:28d1f895c6fe 44 {PA_6, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN6
mbed_official 340:28d1f895c6fe 45 {PA_7, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN7
mbed_official 340:28d1f895c6fe 46 {PB_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN8
mbed_official 340:28d1f895c6fe 47 {PB_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN9
mbed_official 340:28d1f895c6fe 48 {PC_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN10
mbed_official 340:28d1f895c6fe 49 {PC_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN11
mbed_official 340:28d1f895c6fe 50 {PC_2, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN12
mbed_official 340:28d1f895c6fe 51 {PC_3, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN13
mbed_official 340:28d1f895c6fe 52 {PC_4, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN14
mbed_official 340:28d1f895c6fe 53 {PC_5, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN15
mbed_official 340:28d1f895c6fe 54 {NC, NC, 0}
mbed_official 340:28d1f895c6fe 55 };
mbed_official 340:28d1f895c6fe 56
mbed_official 340:28d1f895c6fe 57 ADC_HandleTypeDef AdcHandle;
mbed_official 340:28d1f895c6fe 58
mbed_official 340:28d1f895c6fe 59 int adc_inited = 0;
mbed_official 340:28d1f895c6fe 60
mbed_official 340:28d1f895c6fe 61 void analogin_init(analogin_t *obj, PinName pin)
mbed_official 340:28d1f895c6fe 62 {
mbed_official 340:28d1f895c6fe 63 // Get the peripheral name from the pin and assign it to the object
mbed_official 340:28d1f895c6fe 64 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
mbed_official 340:28d1f895c6fe 65 MBED_ASSERT(obj->adc != (ADCName)NC);
mbed_official 340:28d1f895c6fe 66
mbed_official 340:28d1f895c6fe 67 // Configure GPIO
mbed_official 340:28d1f895c6fe 68 pinmap_pinout(pin, PinMap_ADC);
mbed_official 340:28d1f895c6fe 69
mbed_official 340:28d1f895c6fe 70 // Save pin number for the read function
mbed_official 340:28d1f895c6fe 71 obj->pin = pin;
mbed_official 340:28d1f895c6fe 72
mbed_official 340:28d1f895c6fe 73 // The ADC initialization is done once
mbed_official 340:28d1f895c6fe 74 if (adc_inited == 0) {
mbed_official 340:28d1f895c6fe 75 adc_inited = 1;
mbed_official 340:28d1f895c6fe 76
mbed_official 340:28d1f895c6fe 77 // Enable ADC clock
mbed_official 340:28d1f895c6fe 78 __ADC1_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 79
mbed_official 340:28d1f895c6fe 80 // Configure ADC
mbed_official 340:28d1f895c6fe 81 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
mbed_official 340:28d1f895c6fe 82 AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
mbed_official 340:28d1f895c6fe 83 AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
mbed_official 340:28d1f895c6fe 84 AdcHandle.Init.ScanConvMode = DISABLE;
mbed_official 340:28d1f895c6fe 85 AdcHandle.Init.ContinuousConvMode = DISABLE;
mbed_official 340:28d1f895c6fe 86 AdcHandle.Init.DiscontinuousConvMode = DISABLE;
mbed_official 340:28d1f895c6fe 87 AdcHandle.Init.NbrOfDiscConversion = 0;
mbed_official 340:28d1f895c6fe 88 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
mbed_official 340:28d1f895c6fe 89 AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
mbed_official 340:28d1f895c6fe 90 AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
mbed_official 340:28d1f895c6fe 91 AdcHandle.Init.NbrOfConversion = 1;
mbed_official 340:28d1f895c6fe 92 AdcHandle.Init.DMAContinuousRequests = DISABLE;
mbed_official 340:28d1f895c6fe 93 AdcHandle.Init.EOCSelection = DISABLE;
mbed_official 340:28d1f895c6fe 94 HAL_ADC_Init(&AdcHandle);
mbed_official 340:28d1f895c6fe 95 }
mbed_official 340:28d1f895c6fe 96 }
mbed_official 340:28d1f895c6fe 97
mbed_official 340:28d1f895c6fe 98 static inline uint16_t adc_read(analogin_t *obj)
mbed_official 340:28d1f895c6fe 99 {
mbed_official 340:28d1f895c6fe 100 ADC_ChannelConfTypeDef sConfig;
mbed_official 340:28d1f895c6fe 101
mbed_official 340:28d1f895c6fe 102 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
mbed_official 340:28d1f895c6fe 103
mbed_official 340:28d1f895c6fe 104 // Configure ADC channel
mbed_official 340:28d1f895c6fe 105 sConfig.Rank = 1;
mbed_official 340:28d1f895c6fe 106 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
mbed_official 340:28d1f895c6fe 107 sConfig.Offset = 0;
mbed_official 340:28d1f895c6fe 108
mbed_official 340:28d1f895c6fe 109 switch (obj->pin) {
mbed_official 340:28d1f895c6fe 110 case PA_0:
mbed_official 340:28d1f895c6fe 111 sConfig.Channel = ADC_CHANNEL_0;
mbed_official 340:28d1f895c6fe 112 break;
mbed_official 340:28d1f895c6fe 113 case PA_1:
mbed_official 340:28d1f895c6fe 114 sConfig.Channel = ADC_CHANNEL_1;
mbed_official 340:28d1f895c6fe 115 break;
mbed_official 340:28d1f895c6fe 116 case PA_2:
mbed_official 340:28d1f895c6fe 117 sConfig.Channel = ADC_CHANNEL_2;
mbed_official 340:28d1f895c6fe 118 break;
mbed_official 340:28d1f895c6fe 119 case PA_3:
mbed_official 340:28d1f895c6fe 120 sConfig.Channel = ADC_CHANNEL_3;
mbed_official 340:28d1f895c6fe 121 break;
mbed_official 340:28d1f895c6fe 122 case PA_4:
mbed_official 340:28d1f895c6fe 123 sConfig.Channel = ADC_CHANNEL_4;
mbed_official 340:28d1f895c6fe 124 break;
mbed_official 340:28d1f895c6fe 125 case PA_5:
mbed_official 340:28d1f895c6fe 126 sConfig.Channel = ADC_CHANNEL_5;
mbed_official 340:28d1f895c6fe 127 break;
mbed_official 340:28d1f895c6fe 128 case PA_6:
mbed_official 340:28d1f895c6fe 129 sConfig.Channel = ADC_CHANNEL_6;
mbed_official 340:28d1f895c6fe 130 break;
mbed_official 340:28d1f895c6fe 131 case PA_7:
mbed_official 340:28d1f895c6fe 132 sConfig.Channel = ADC_CHANNEL_7;
mbed_official 340:28d1f895c6fe 133 break;
mbed_official 340:28d1f895c6fe 134 case PB_0:
mbed_official 340:28d1f895c6fe 135 sConfig.Channel = ADC_CHANNEL_8;
mbed_official 340:28d1f895c6fe 136 break;
mbed_official 340:28d1f895c6fe 137 case PB_1:
mbed_official 340:28d1f895c6fe 138 sConfig.Channel = ADC_CHANNEL_9;
mbed_official 340:28d1f895c6fe 139 break;
mbed_official 340:28d1f895c6fe 140 case PC_0:
mbed_official 340:28d1f895c6fe 141 sConfig.Channel = ADC_CHANNEL_10;
mbed_official 340:28d1f895c6fe 142 break;
mbed_official 340:28d1f895c6fe 143 case PC_1:
mbed_official 340:28d1f895c6fe 144 sConfig.Channel = ADC_CHANNEL_11;
mbed_official 340:28d1f895c6fe 145 break;
mbed_official 340:28d1f895c6fe 146 case PC_2:
mbed_official 340:28d1f895c6fe 147 sConfig.Channel = ADC_CHANNEL_12;
mbed_official 340:28d1f895c6fe 148 break;
mbed_official 340:28d1f895c6fe 149 case PC_3:
mbed_official 340:28d1f895c6fe 150 sConfig.Channel = ADC_CHANNEL_13;
mbed_official 340:28d1f895c6fe 151 break;
mbed_official 340:28d1f895c6fe 152 case PC_4:
mbed_official 340:28d1f895c6fe 153 sConfig.Channel = ADC_CHANNEL_14;
mbed_official 340:28d1f895c6fe 154 break;
mbed_official 340:28d1f895c6fe 155 case PC_5:
mbed_official 340:28d1f895c6fe 156 sConfig.Channel = ADC_CHANNEL_15;
mbed_official 340:28d1f895c6fe 157 break;
mbed_official 340:28d1f895c6fe 158 default:
mbed_official 340:28d1f895c6fe 159 return 0;
mbed_official 340:28d1f895c6fe 160 }
mbed_official 340:28d1f895c6fe 161
mbed_official 340:28d1f895c6fe 162 HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
mbed_official 340:28d1f895c6fe 163
mbed_official 340:28d1f895c6fe 164 HAL_ADC_Start(&AdcHandle); // Start conversion
mbed_official 340:28d1f895c6fe 165
mbed_official 340:28d1f895c6fe 166 // Wait end of conversion and get value
mbed_official 340:28d1f895c6fe 167 if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
mbed_official 340:28d1f895c6fe 168 return (HAL_ADC_GetValue(&AdcHandle));
mbed_official 340:28d1f895c6fe 169 } else {
mbed_official 340:28d1f895c6fe 170 return 0;
mbed_official 340:28d1f895c6fe 171 }
mbed_official 340:28d1f895c6fe 172 }
mbed_official 340:28d1f895c6fe 173
mbed_official 340:28d1f895c6fe 174 uint16_t analogin_read_u16(analogin_t *obj)
mbed_official 340:28d1f895c6fe 175 {
mbed_official 340:28d1f895c6fe 176 uint16_t value = adc_read(obj);
mbed_official 340:28d1f895c6fe 177 // 12-bit to 16-bit conversion
mbed_official 340:28d1f895c6fe 178 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
mbed_official 340:28d1f895c6fe 179 return value;
mbed_official 340:28d1f895c6fe 180 }
mbed_official 340:28d1f895c6fe 181
mbed_official 340:28d1f895c6fe 182 float analogin_read(analogin_t *obj)
mbed_official 340:28d1f895c6fe 183 {
mbed_official 340:28d1f895c6fe 184 uint16_t value = adc_read(obj);
mbed_official 340:28d1f895c6fe 185 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
mbed_official 340:28d1f895c6fe 186 }
mbed_official 340:28d1f895c6fe 187
mbed_official 340:28d1f895c6fe 188 #endif