Modification of Mbed-dev library for LQFP48 package microcontrollers: STM32F103C8 (STM32F103C8T6) and STM32F103CB (STM32F103CBT6) (Bluepill boards, Maple mini etc. )
Fork of mbed-STM32F103C8_org by
Library for STM32F103C8 (Bluepill boards etc.).
Use this instead of mbed library.
This library allows the size of the code in the FLASH up to 128kB. Therefore, code also runs on microcontrollers STM32F103CB (eg. Maple mini).
But in the case of STM32F103C8, check the size of the resulting code would not exceed 64kB.
To compile a program with this library, use NUCLEO-F103RB as the target name. !
Changes:
- Corrected initialization of the HSE + crystal clock (mbed permanent bug), allowing the use of on-board xtal (8MHz).(1)
- Additionally, it also set USB clock (48Mhz).(2)
- Definitions of pins and peripherals adjusted to LQFP48 case.
- Board led LED1 is now PC_13 (3)
- USER_BUTTON is now PC_14 (4)
Now the library is complete rebuilt based on mbed-dev v160 (and not yet fully tested).
notes
(1) - In case 8MHz xtal on board, CPU frequency is 72MHz. Without xtal is 64MHz.
(2) - Using the USB interface is only possible if STM32 is clocking by on-board 8MHz xtal or external clock signal 8MHz on the OSC_IN pin.
(3) - On Bluepill board led operation is reversed, i.e. 0 - led on, 1 - led off.
(4) - Bluepill board has no real user button
Information
After export to SW4STM (AC6):
- add line
#include "mbed_config.h"
in files Serial.h and RawSerial.h - in project properties change
Optimisation Level
toOptimise for size (-Os)
targets/TARGET_STM/TARGET_STM32F1/analogin_api.c@148:8b0b02bf146f, 2017-04-27 (annotated)
- Committer:
- mega64
- Date:
- Thu Apr 27 23:56:38 2017 +0000
- Revision:
- 148:8b0b02bf146f
- Parent:
- 146:03e976389d16
Remove unnecessary folders
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mega64 | 146:03e976389d16 | 1 | /* mbed Microcontroller Library |
mega64 | 146:03e976389d16 | 2 | * Copyright (c) 2016, STMicroelectronics |
mega64 | 146:03e976389d16 | 3 | * All rights reserved. |
mega64 | 146:03e976389d16 | 4 | * |
mega64 | 146:03e976389d16 | 5 | * Redistribution and use in source and binary forms, with or without |
mega64 | 146:03e976389d16 | 6 | * modification, are permitted provided that the following conditions are met: |
mega64 | 146:03e976389d16 | 7 | * |
mega64 | 146:03e976389d16 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
mega64 | 146:03e976389d16 | 9 | * this list of conditions and the following disclaimer. |
mega64 | 146:03e976389d16 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mega64 | 146:03e976389d16 | 11 | * this list of conditions and the following disclaimer in the documentation |
mega64 | 146:03e976389d16 | 12 | * and/or other materials provided with the distribution. |
mega64 | 146:03e976389d16 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mega64 | 146:03e976389d16 | 14 | * may be used to endorse or promote products derived from this software |
mega64 | 146:03e976389d16 | 15 | * without specific prior written permission. |
mega64 | 146:03e976389d16 | 16 | * |
mega64 | 146:03e976389d16 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mega64 | 146:03e976389d16 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mega64 | 146:03e976389d16 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mega64 | 146:03e976389d16 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mega64 | 146:03e976389d16 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mega64 | 146:03e976389d16 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mega64 | 146:03e976389d16 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mega64 | 146:03e976389d16 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mega64 | 146:03e976389d16 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mega64 | 146:03e976389d16 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mega64 | 146:03e976389d16 | 27 | */ |
mega64 | 146:03e976389d16 | 28 | #include "mbed_assert.h" |
mega64 | 146:03e976389d16 | 29 | #include "analogin_api.h" |
mega64 | 146:03e976389d16 | 30 | |
mega64 | 146:03e976389d16 | 31 | #if DEVICE_ANALOGIN |
mega64 | 146:03e976389d16 | 32 | |
mega64 | 146:03e976389d16 | 33 | #include "mbed_wait_api.h" |
mega64 | 146:03e976389d16 | 34 | #include "cmsis.h" |
mega64 | 146:03e976389d16 | 35 | #include "pinmap.h" |
mega64 | 146:03e976389d16 | 36 | #include "PeripheralPins.h" |
mega64 | 146:03e976389d16 | 37 | |
mega64 | 146:03e976389d16 | 38 | ADC_HandleTypeDef AdcHandle; |
mega64 | 146:03e976389d16 | 39 | |
mega64 | 146:03e976389d16 | 40 | int adc_inited = 0; |
mega64 | 146:03e976389d16 | 41 | |
mega64 | 146:03e976389d16 | 42 | void analogin_init(analogin_t *obj, PinName pin) |
mega64 | 146:03e976389d16 | 43 | { |
mega64 | 146:03e976389d16 | 44 | RCC_PeriphCLKInitTypeDef PeriphClkInit; |
mega64 | 146:03e976389d16 | 45 | |
mega64 | 146:03e976389d16 | 46 | // Get the peripheral name from the pin and assign it to the object |
mega64 | 146:03e976389d16 | 47 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mega64 | 146:03e976389d16 | 48 | MBED_ASSERT(obj->adc != (ADCName)NC); |
mega64 | 146:03e976389d16 | 49 | |
mega64 | 146:03e976389d16 | 50 | // Get the functions (adc channel) from the pin and assign it to the object |
mega64 | 146:03e976389d16 | 51 | uint32_t function = pinmap_function(pin, PinMap_ADC); |
mega64 | 146:03e976389d16 | 52 | MBED_ASSERT(function != (uint32_t)NC); |
mega64 | 146:03e976389d16 | 53 | obj->channel = STM_PIN_CHANNEL(function); |
mega64 | 146:03e976389d16 | 54 | |
mega64 | 146:03e976389d16 | 55 | // Configure GPIO excepted for internal channels (Temperature, Vref, Vbat, ...) |
mega64 | 146:03e976389d16 | 56 | // ADC Internal Channels "pins" are described in PinNames.h and must have a value >= 0xF0 |
mega64 | 146:03e976389d16 | 57 | if (pin < 0xF0) { |
mega64 | 146:03e976389d16 | 58 | pinmap_pinout(pin, PinMap_ADC); |
mega64 | 146:03e976389d16 | 59 | } |
mega64 | 146:03e976389d16 | 60 | |
mega64 | 146:03e976389d16 | 61 | // Save pin number for the read function |
mega64 | 146:03e976389d16 | 62 | obj->pin = pin; |
mega64 | 146:03e976389d16 | 63 | |
mega64 | 146:03e976389d16 | 64 | // The ADC initialization is done once |
mega64 | 146:03e976389d16 | 65 | if (adc_inited == 0) { |
mega64 | 146:03e976389d16 | 66 | adc_inited = 1; |
mega64 | 146:03e976389d16 | 67 | |
mega64 | 146:03e976389d16 | 68 | // Enable ADC clock |
mega64 | 146:03e976389d16 | 69 | __HAL_RCC_ADC1_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 70 | |
mega64 | 146:03e976389d16 | 71 | // Configure ADC clock prescaler |
mega64 | 146:03e976389d16 | 72 | // Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet). |
mega64 | 146:03e976389d16 | 73 | // Therefore, ADC clock prescaler must be configured in function |
mega64 | 146:03e976389d16 | 74 | // of ADC clock source frequency to remain below this maximum frequency. |
mega64 | 146:03e976389d16 | 75 | // with 8 MHz external xtal: PCLK2 = 72 MHz --> ADC clock = 72/6 = 12 MHz |
mega64 | 146:03e976389d16 | 76 | // with internal clock : PCLK2 = 64 MHz --> ADC clock = 64/6 = 10.67 MHz |
mega64 | 146:03e976389d16 | 77 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; |
mega64 | 146:03e976389d16 | 78 | PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; |
mega64 | 146:03e976389d16 | 79 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); |
mega64 | 146:03e976389d16 | 80 | |
mega64 | 146:03e976389d16 | 81 | // Configure ADC |
mega64 | 146:03e976389d16 | 82 | AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); |
mega64 | 146:03e976389d16 | 83 | AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
mega64 | 146:03e976389d16 | 84 | AdcHandle.Init.ScanConvMode = DISABLE; |
mega64 | 146:03e976389d16 | 85 | AdcHandle.Init.ContinuousConvMode = DISABLE; |
mega64 | 146:03e976389d16 | 86 | AdcHandle.Init.NbrOfConversion = 1; |
mega64 | 146:03e976389d16 | 87 | AdcHandle.Init.DiscontinuousConvMode = DISABLE; |
mega64 | 146:03e976389d16 | 88 | AdcHandle.Init.NbrOfDiscConversion = 0; |
mega64 | 146:03e976389d16 | 89 | AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; |
mega64 | 146:03e976389d16 | 90 | HAL_ADC_Init(&AdcHandle); |
mega64 | 146:03e976389d16 | 91 | } |
mega64 | 146:03e976389d16 | 92 | } |
mega64 | 146:03e976389d16 | 93 | |
mega64 | 146:03e976389d16 | 94 | static inline uint16_t adc_read(analogin_t *obj) |
mega64 | 146:03e976389d16 | 95 | { |
mega64 | 146:03e976389d16 | 96 | ADC_ChannelConfTypeDef sConfig; |
mega64 | 146:03e976389d16 | 97 | |
mega64 | 146:03e976389d16 | 98 | AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); |
mega64 | 146:03e976389d16 | 99 | |
mega64 | 146:03e976389d16 | 100 | // Configure ADC channel |
mega64 | 146:03e976389d16 | 101 | sConfig.Rank = 1; |
mega64 | 146:03e976389d16 | 102 | sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5; |
mega64 | 146:03e976389d16 | 103 | |
mega64 | 146:03e976389d16 | 104 | switch (obj->channel) { |
mega64 | 146:03e976389d16 | 105 | case 0: |
mega64 | 146:03e976389d16 | 106 | sConfig.Channel = ADC_CHANNEL_0; |
mega64 | 146:03e976389d16 | 107 | break; |
mega64 | 146:03e976389d16 | 108 | case 1: |
mega64 | 146:03e976389d16 | 109 | sConfig.Channel = ADC_CHANNEL_1; |
mega64 | 146:03e976389d16 | 110 | break; |
mega64 | 146:03e976389d16 | 111 | case 2: |
mega64 | 146:03e976389d16 | 112 | sConfig.Channel = ADC_CHANNEL_2; |
mega64 | 146:03e976389d16 | 113 | break; |
mega64 | 146:03e976389d16 | 114 | case 3: |
mega64 | 146:03e976389d16 | 115 | sConfig.Channel = ADC_CHANNEL_3; |
mega64 | 146:03e976389d16 | 116 | break; |
mega64 | 146:03e976389d16 | 117 | case 4: |
mega64 | 146:03e976389d16 | 118 | sConfig.Channel = ADC_CHANNEL_4; |
mega64 | 146:03e976389d16 | 119 | break; |
mega64 | 146:03e976389d16 | 120 | case 5: |
mega64 | 146:03e976389d16 | 121 | sConfig.Channel = ADC_CHANNEL_5; |
mega64 | 146:03e976389d16 | 122 | break; |
mega64 | 146:03e976389d16 | 123 | case 6: |
mega64 | 146:03e976389d16 | 124 | sConfig.Channel = ADC_CHANNEL_6; |
mega64 | 146:03e976389d16 | 125 | break; |
mega64 | 146:03e976389d16 | 126 | case 7: |
mega64 | 146:03e976389d16 | 127 | sConfig.Channel = ADC_CHANNEL_7; |
mega64 | 146:03e976389d16 | 128 | break; |
mega64 | 146:03e976389d16 | 129 | case 8: |
mega64 | 146:03e976389d16 | 130 | sConfig.Channel = ADC_CHANNEL_8; |
mega64 | 146:03e976389d16 | 131 | break; |
mega64 | 146:03e976389d16 | 132 | case 9: |
mega64 | 146:03e976389d16 | 133 | sConfig.Channel = ADC_CHANNEL_9; |
mega64 | 146:03e976389d16 | 134 | break; |
mega64 | 146:03e976389d16 | 135 | case 10: |
mega64 | 146:03e976389d16 | 136 | sConfig.Channel = ADC_CHANNEL_10; |
mega64 | 146:03e976389d16 | 137 | break; |
mega64 | 146:03e976389d16 | 138 | case 11: |
mega64 | 146:03e976389d16 | 139 | sConfig.Channel = ADC_CHANNEL_11; |
mega64 | 146:03e976389d16 | 140 | break; |
mega64 | 146:03e976389d16 | 141 | case 12: |
mega64 | 146:03e976389d16 | 142 | sConfig.Channel = ADC_CHANNEL_12; |
mega64 | 146:03e976389d16 | 143 | break; |
mega64 | 146:03e976389d16 | 144 | case 13: |
mega64 | 146:03e976389d16 | 145 | sConfig.Channel = ADC_CHANNEL_13; |
mega64 | 146:03e976389d16 | 146 | break; |
mega64 | 146:03e976389d16 | 147 | case 14: |
mega64 | 146:03e976389d16 | 148 | sConfig.Channel = ADC_CHANNEL_14; |
mega64 | 146:03e976389d16 | 149 | break; |
mega64 | 146:03e976389d16 | 150 | case 15: |
mega64 | 146:03e976389d16 | 151 | sConfig.Channel = ADC_CHANNEL_15; |
mega64 | 146:03e976389d16 | 152 | break; |
mega64 | 146:03e976389d16 | 153 | case 16: |
mega64 | 146:03e976389d16 | 154 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
mega64 | 146:03e976389d16 | 155 | break; |
mega64 | 146:03e976389d16 | 156 | case 17: |
mega64 | 146:03e976389d16 | 157 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
mega64 | 146:03e976389d16 | 158 | break; |
mega64 | 146:03e976389d16 | 159 | default: |
mega64 | 146:03e976389d16 | 160 | return 0; |
mega64 | 146:03e976389d16 | 161 | } |
mega64 | 146:03e976389d16 | 162 | |
mega64 | 146:03e976389d16 | 163 | HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); |
mega64 | 146:03e976389d16 | 164 | |
mega64 | 146:03e976389d16 | 165 | HAL_ADC_Start(&AdcHandle); // Start conversion |
mega64 | 146:03e976389d16 | 166 | |
mega64 | 146:03e976389d16 | 167 | // Wait end of conversion and get value |
mega64 | 146:03e976389d16 | 168 | if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) { |
mega64 | 146:03e976389d16 | 169 | return (HAL_ADC_GetValue(&AdcHandle)); |
mega64 | 146:03e976389d16 | 170 | } else { |
mega64 | 146:03e976389d16 | 171 | return 0; |
mega64 | 146:03e976389d16 | 172 | } |
mega64 | 146:03e976389d16 | 173 | } |
mega64 | 146:03e976389d16 | 174 | |
mega64 | 146:03e976389d16 | 175 | uint16_t analogin_read_u16(analogin_t *obj) |
mega64 | 146:03e976389d16 | 176 | { |
mega64 | 146:03e976389d16 | 177 | uint16_t value = adc_read(obj); |
mega64 | 146:03e976389d16 | 178 | // 12-bit to 16-bit conversion |
mega64 | 146:03e976389d16 | 179 | value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); |
mega64 | 146:03e976389d16 | 180 | return value; |
mega64 | 146:03e976389d16 | 181 | } |
mega64 | 146:03e976389d16 | 182 | |
mega64 | 146:03e976389d16 | 183 | float analogin_read(analogin_t *obj) |
mega64 | 146:03e976389d16 | 184 | { |
mega64 | 146:03e976389d16 | 185 | uint16_t value = adc_read(obj); |
mega64 | 146:03e976389d16 | 186 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
mega64 | 146:03e976389d16 | 187 | } |
mega64 | 146:03e976389d16 | 188 | |
mega64 | 146:03e976389d16 | 189 | #endif |