mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

This mbed LIB has additional peripherals for ST F401 board

  • UART2 : PA_3 rx, PA_2 tx
  • UART3 : PC_7 rx, PC_6 tx
  • I2C2 : PB_3 SDA, PB_10 SCL
  • I2C3 : PB_4 SDA, PA_8 SCL
Committer:
mbed_official
Date:
Mon Jan 27 14:30:07 2014 +0000
Revision:
76:aeb1df146756
Synchronized with git revision a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd

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

Add NUCLEO_L152RE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 76:aeb1df146756 1 /* mbed Microcontroller Library
mbed_official 76:aeb1df146756 2 * Copyright (c) 2014, STMicroelectronics
mbed_official 76:aeb1df146756 3 * All rights reserved.
mbed_official 76:aeb1df146756 4 *
mbed_official 76:aeb1df146756 5 * Redistribution and use in source and binary forms, with or without
mbed_official 76:aeb1df146756 6 * modification, are permitted provided that the following conditions are met:
mbed_official 76:aeb1df146756 7 *
mbed_official 76:aeb1df146756 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 76:aeb1df146756 9 * this list of conditions and the following disclaimer.
mbed_official 76:aeb1df146756 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 76:aeb1df146756 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 76:aeb1df146756 12 * and/or other materials provided with the distribution.
mbed_official 76:aeb1df146756 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 76:aeb1df146756 14 * may be used to endorse or promote products derived from this software
mbed_official 76:aeb1df146756 15 * without specific prior written permission.
mbed_official 76:aeb1df146756 16 *
mbed_official 76:aeb1df146756 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 76:aeb1df146756 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 76:aeb1df146756 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 76:aeb1df146756 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 76:aeb1df146756 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 76:aeb1df146756 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 76:aeb1df146756 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 76:aeb1df146756 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 76:aeb1df146756 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 76:aeb1df146756 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 76:aeb1df146756 27 */
mbed_official 76:aeb1df146756 28 #include "analogin_api.h"
mbed_official 76:aeb1df146756 29 #include "wait_api.h"
mbed_official 76:aeb1df146756 30
mbed_official 76:aeb1df146756 31 #if DEVICE_ANALOGIN
mbed_official 76:aeb1df146756 32
mbed_official 76:aeb1df146756 33 #include "cmsis.h"
mbed_official 76:aeb1df146756 34 #include "pinmap.h"
mbed_official 76:aeb1df146756 35 #include "error.h"
mbed_official 76:aeb1df146756 36
mbed_official 76:aeb1df146756 37 static const PinMap PinMap_ADC[] = {
mbed_official 76:aeb1df146756 38 {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0
mbed_official 76:aeb1df146756 39 {PA_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1
mbed_official 76:aeb1df146756 40 {PA_4, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN4
mbed_official 76:aeb1df146756 41 {PB_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN8
mbed_official 76:aeb1df146756 42 {PC_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN11
mbed_official 76:aeb1df146756 43 {PC_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN10
mbed_official 76:aeb1df146756 44 {NC, NC, 0}
mbed_official 76:aeb1df146756 45 };
mbed_official 76:aeb1df146756 46
mbed_official 76:aeb1df146756 47 int adc_inited = 0;
mbed_official 76:aeb1df146756 48
mbed_official 76:aeb1df146756 49 void analogin_init(analogin_t *obj, PinName pin) {
mbed_official 76:aeb1df146756 50
mbed_official 76:aeb1df146756 51 ADC_TypeDef *adc;
mbed_official 76:aeb1df146756 52 ADC_InitTypeDef ADC_InitStructure;
mbed_official 76:aeb1df146756 53
mbed_official 76:aeb1df146756 54 // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object
mbed_official 76:aeb1df146756 55 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
mbed_official 76:aeb1df146756 56
mbed_official 76:aeb1df146756 57 if (obj->adc == (ADCName)NC) {
mbed_official 76:aeb1df146756 58 error("ADC pin mapping failed");
mbed_official 76:aeb1df146756 59 }
mbed_official 76:aeb1df146756 60
mbed_official 76:aeb1df146756 61 // Configure GPIO
mbed_official 76:aeb1df146756 62 pinmap_pinout(pin, PinMap_ADC);
mbed_official 76:aeb1df146756 63
mbed_official 76:aeb1df146756 64 // Save pin number for the read function
mbed_official 76:aeb1df146756 65 obj->pin = pin;
mbed_official 76:aeb1df146756 66
mbed_official 76:aeb1df146756 67 // The ADC initialization is done once
mbed_official 76:aeb1df146756 68 if (adc_inited == 0) {
mbed_official 76:aeb1df146756 69 adc_inited = 1;
mbed_official 76:aeb1df146756 70
mbed_official 76:aeb1df146756 71 // Get ADC registers structure address
mbed_official 76:aeb1df146756 72 adc = (ADC_TypeDef *)(obj->adc);
mbed_official 76:aeb1df146756 73
mbed_official 76:aeb1df146756 74 // Enable ADC clock
mbed_official 76:aeb1df146756 75 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
mbed_official 76:aeb1df146756 76
mbed_official 76:aeb1df146756 77 // Configure ADC
mbed_official 76:aeb1df146756 78 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
mbed_official 76:aeb1df146756 79 ADC_InitStructure.ADC_ScanConvMode = DISABLE;
mbed_official 76:aeb1df146756 80 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
mbed_official 76:aeb1df146756 81 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
mbed_official 76:aeb1df146756 82 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
mbed_official 76:aeb1df146756 83 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
mbed_official 76:aeb1df146756 84 ADC_InitStructure.ADC_NbrOfConversion = 1;
mbed_official 76:aeb1df146756 85 ADC_Init(adc, &ADC_InitStructure);
mbed_official 76:aeb1df146756 86
mbed_official 76:aeb1df146756 87 // Enable ADC
mbed_official 76:aeb1df146756 88 ADC_Cmd(adc, ENABLE);
mbed_official 76:aeb1df146756 89 }
mbed_official 76:aeb1df146756 90 }
mbed_official 76:aeb1df146756 91
mbed_official 76:aeb1df146756 92 static inline uint16_t adc_read(analogin_t *obj) {
mbed_official 76:aeb1df146756 93 // Get ADC registers structure address
mbed_official 76:aeb1df146756 94 ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc);
mbed_official 76:aeb1df146756 95
mbed_official 76:aeb1df146756 96 // Configure ADC channel
mbed_official 76:aeb1df146756 97 switch (obj->pin) {
mbed_official 76:aeb1df146756 98 case PA_0:
mbed_official 76:aeb1df146756 99 ADC_RegularChannelConfig(adc, ADC_Channel_0, 1, ADC_SampleTime_4Cycles);
mbed_official 76:aeb1df146756 100 break;
mbed_official 76:aeb1df146756 101 case PA_1:
mbed_official 76:aeb1df146756 102 ADC_RegularChannelConfig(adc, ADC_Channel_1, 1, ADC_SampleTime_4Cycles);
mbed_official 76:aeb1df146756 103 break;
mbed_official 76:aeb1df146756 104 case PA_4:
mbed_official 76:aeb1df146756 105 ADC_RegularChannelConfig(adc, ADC_Channel_4, 1, ADC_SampleTime_4Cycles);
mbed_official 76:aeb1df146756 106 break;
mbed_official 76:aeb1df146756 107 case PB_0:
mbed_official 76:aeb1df146756 108 ADC_RegularChannelConfig(adc, ADC_Channel_8, 1, ADC_SampleTime_4Cycles);
mbed_official 76:aeb1df146756 109 break;
mbed_official 76:aeb1df146756 110 case PC_1:
mbed_official 76:aeb1df146756 111 ADC_RegularChannelConfig(adc, ADC_Channel_11, 1, ADC_SampleTime_4Cycles);
mbed_official 76:aeb1df146756 112 break;
mbed_official 76:aeb1df146756 113 case PC_0:
mbed_official 76:aeb1df146756 114 ADC_RegularChannelConfig(adc, ADC_Channel_10, 1, ADC_SampleTime_4Cycles);
mbed_official 76:aeb1df146756 115 break;
mbed_official 76:aeb1df146756 116 default:
mbed_official 76:aeb1df146756 117 return 0;
mbed_official 76:aeb1df146756 118 }
mbed_official 76:aeb1df146756 119
mbed_official 76:aeb1df146756 120 ADC_SoftwareStartConv(adc); // Start conversion
mbed_official 76:aeb1df146756 121
mbed_official 76:aeb1df146756 122 while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion
mbed_official 76:aeb1df146756 123
mbed_official 76:aeb1df146756 124 return(ADC_GetConversionValue(adc)); // Get conversion value
mbed_official 76:aeb1df146756 125 }
mbed_official 76:aeb1df146756 126
mbed_official 76:aeb1df146756 127 uint16_t analogin_read_u16(analogin_t *obj) {
mbed_official 76:aeb1df146756 128 return(adc_read(obj));
mbed_official 76:aeb1df146756 129 }
mbed_official 76:aeb1df146756 130
mbed_official 76:aeb1df146756 131 float analogin_read(analogin_t *obj) {
mbed_official 76:aeb1df146756 132 uint16_t value = adc_read(obj);
mbed_official 76:aeb1df146756 133 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
mbed_official 76:aeb1df146756 134 }
mbed_official 76:aeb1df146756 135
mbed_official 76:aeb1df146756 136 #endif