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 Nothing Special

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 to Optimise for size (-Os)
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?

UserRevisionLine numberNew contents of line
mega64 146:03e976389d16 1 /* mbed Microcontroller Library
mega64 146:03e976389d16 2 *******************************************************************************
mega64 146:03e976389d16 3 * Copyright (c) 2017, STMicroelectronics
mega64 146:03e976389d16 4 * All rights reserved.
mega64 146:03e976389d16 5 *
mega64 146:03e976389d16 6 * Redistribution and use in source and binary forms, with or without
mega64 146:03e976389d16 7 * modification, are permitted provided that the following conditions are met:
mega64 146:03e976389d16 8 *
mega64 146:03e976389d16 9 * 1. Redistributions of source code must retain the above copyright notice,
mega64 146:03e976389d16 10 * this list of conditions and the following disclaimer.
mega64 146:03e976389d16 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mega64 146:03e976389d16 12 * this list of conditions and the following disclaimer in the documentation
mega64 146:03e976389d16 13 * and/or other materials provided with the distribution.
mega64 146:03e976389d16 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mega64 146:03e976389d16 15 * may be used to endorse or promote products derived from this software
mega64 146:03e976389d16 16 * without specific prior written permission.
mega64 146:03e976389d16 17 *
mega64 146:03e976389d16 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mega64 146:03e976389d16 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mega64 146:03e976389d16 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mega64 146:03e976389d16 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mega64 146:03e976389d16 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mega64 146:03e976389d16 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mega64 146:03e976389d16 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mega64 146:03e976389d16 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mega64 146:03e976389d16 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mega64 146:03e976389d16 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mega64 146:03e976389d16 28 *******************************************************************************
mega64 146:03e976389d16 29 */
mega64 146:03e976389d16 30 #include "mbed_assert.h"
mega64 146:03e976389d16 31 #include "pinmap.h"
mega64 146:03e976389d16 32 #include "PortNames.h"
mega64 146:03e976389d16 33 #include "mbed_error.h"
mega64 146:03e976389d16 34 #include "pin_device.h"
mega64 146:03e976389d16 35
mega64 146:03e976389d16 36 extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
mega64 146:03e976389d16 37
mega64 146:03e976389d16 38 const uint32_t ll_pin_defines[16] = {
mega64 146:03e976389d16 39 LL_GPIO_PIN_0,
mega64 146:03e976389d16 40 LL_GPIO_PIN_1,
mega64 146:03e976389d16 41 LL_GPIO_PIN_2,
mega64 146:03e976389d16 42 LL_GPIO_PIN_3,
mega64 146:03e976389d16 43 LL_GPIO_PIN_4,
mega64 146:03e976389d16 44 LL_GPIO_PIN_5,
mega64 146:03e976389d16 45 LL_GPIO_PIN_6,
mega64 146:03e976389d16 46 LL_GPIO_PIN_7,
mega64 146:03e976389d16 47 LL_GPIO_PIN_8,
mega64 146:03e976389d16 48 LL_GPIO_PIN_9,
mega64 146:03e976389d16 49 LL_GPIO_PIN_10,
mega64 146:03e976389d16 50 LL_GPIO_PIN_11,
mega64 146:03e976389d16 51 LL_GPIO_PIN_12,
mega64 146:03e976389d16 52 LL_GPIO_PIN_13,
mega64 146:03e976389d16 53 LL_GPIO_PIN_14,
mega64 146:03e976389d16 54 LL_GPIO_PIN_15
mega64 146:03e976389d16 55 };
mega64 146:03e976389d16 56
mega64 146:03e976389d16 57 /**
mega64 146:03e976389d16 58 * Configure pin (mode, speed, output type and pull-up/pull-down)
mega64 146:03e976389d16 59 */
mega64 146:03e976389d16 60 void pin_function(PinName pin, int data)
mega64 146:03e976389d16 61 {
mega64 146:03e976389d16 62 MBED_ASSERT(pin != (PinName)NC);
mega64 146:03e976389d16 63
mega64 146:03e976389d16 64 // Get the pin informations
mega64 146:03e976389d16 65 uint32_t mode = STM_PIN_FUNCTION(data);
mega64 146:03e976389d16 66 uint32_t afnum = STM_PIN_AFNUM(data);
mega64 146:03e976389d16 67 uint32_t port = STM_PORT(pin);
mega64 146:03e976389d16 68 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
mega64 146:03e976389d16 69 uint32_t ll_mode = 0;
mega64 146:03e976389d16 70
mega64 146:03e976389d16 71 // Enable GPIO clock
mega64 146:03e976389d16 72 GPIO_TypeDef *gpio = Set_GPIO_Clock(port);
mega64 146:03e976389d16 73
mega64 146:03e976389d16 74 /* Set default speed to high.
mega64 146:03e976389d16 75 * This is done before other settings on purpose:
mega64 146:03e976389d16 76 * For most families there are dedicated registers so it is
mega64 146:03e976389d16 77 * not so important, register can be set at any time.
mega64 146:03e976389d16 78 * But for families like F1, speed only applies to output. so we set
mega64 146:03e976389d16 79 * it here, and then if input is selected, this setting might be
mega64 146:03e976389d16 80 * overriden by the input one.
mega64 146:03e976389d16 81 */
mega64 146:03e976389d16 82 LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
mega64 146:03e976389d16 83
mega64 146:03e976389d16 84 switch (mode) {
mega64 146:03e976389d16 85 case STM_PIN_INPUT:
mega64 146:03e976389d16 86 ll_mode = LL_GPIO_MODE_INPUT;
mega64 146:03e976389d16 87 break;
mega64 146:03e976389d16 88 case STM_PIN_OUTPUT:
mega64 146:03e976389d16 89 ll_mode = LL_GPIO_MODE_OUTPUT;
mega64 146:03e976389d16 90 break;
mega64 146:03e976389d16 91 case STM_PIN_ALTERNATE:
mega64 146:03e976389d16 92 ll_mode = LL_GPIO_MODE_ALTERNATE;
mega64 146:03e976389d16 93 // In case of ALT function, also set he afnum
mega64 146:03e976389d16 94 stm_pin_SetAFPin(gpio, pin, afnum);
mega64 146:03e976389d16 95 break;
mega64 146:03e976389d16 96 case STM_PIN_ANALOG:
mega64 146:03e976389d16 97 ll_mode = LL_GPIO_MODE_ANALOG;
mega64 146:03e976389d16 98 break;
mega64 146:03e976389d16 99 default:
mega64 146:03e976389d16 100 MBED_ASSERT(0);
mega64 146:03e976389d16 101 break;
mega64 146:03e976389d16 102 }
mega64 146:03e976389d16 103 LL_GPIO_SetPinMode(gpio, ll_pin, ll_mode);
mega64 146:03e976389d16 104
mega64 146:03e976389d16 105 #if defined(GPIO_ASCR_ASC0)
mega64 146:03e976389d16 106 /* For families where Analog Control ASC0 register is present */
mega64 146:03e976389d16 107 if (STM_PIN_ANALOG_CONTROL(data)) {
mega64 146:03e976389d16 108 LL_GPIO_EnablePinAnalogControl(gpio, ll_pin);
mega64 146:03e976389d16 109 } else {
mega64 146:03e976389d16 110 LL_GPIO_DisablePinAnalogControl(gpio, ll_pin);
mega64 146:03e976389d16 111 }
mega64 146:03e976389d16 112 #endif
mega64 146:03e976389d16 113
mega64 146:03e976389d16 114 /* For now by default use Speed HIGH for output or alt modes */
mega64 146:03e976389d16 115 if ((mode == STM_PIN_OUTPUT) ||(mode == STM_PIN_ALTERNATE)) {
mega64 146:03e976389d16 116 if (STM_PIN_OD(data)) {
mega64 146:03e976389d16 117 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
mega64 146:03e976389d16 118 } else {
mega64 146:03e976389d16 119 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
mega64 146:03e976389d16 120 }
mega64 146:03e976389d16 121 }
mega64 146:03e976389d16 122
mega64 146:03e976389d16 123 stm_pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(data));
mega64 146:03e976389d16 124
mega64 146:03e976389d16 125 stm_pin_DisconnectDebug(pin);
mega64 146:03e976389d16 126 }
mega64 146:03e976389d16 127
mega64 146:03e976389d16 128 /**
mega64 146:03e976389d16 129 * Configure pin pull-up/pull-down
mega64 146:03e976389d16 130 */
mega64 146:03e976389d16 131 void pin_mode(PinName pin, PinMode mode)
mega64 146:03e976389d16 132 {
mega64 146:03e976389d16 133 MBED_ASSERT(pin != (PinName)NC);
mega64 146:03e976389d16 134
mega64 146:03e976389d16 135 uint32_t port_index = STM_PORT(pin);
mega64 146:03e976389d16 136 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
mega64 146:03e976389d16 137 // Enable GPIO clock
mega64 146:03e976389d16 138 GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
mega64 146:03e976389d16 139 uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
mega64 146:03e976389d16 140
mega64 146:03e976389d16 141 if ((function == LL_GPIO_MODE_OUTPUT) || (function == LL_GPIO_MODE_ALTERNATE))
mega64 146:03e976389d16 142 {
mega64 146:03e976389d16 143 if ((mode == OpenDrainNoPull) || (mode == OpenDrainPullUp) || (mode == OpenDrainPullDown)) {
mega64 146:03e976389d16 144 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
mega64 146:03e976389d16 145 } else {
mega64 146:03e976389d16 146 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
mega64 146:03e976389d16 147 }
mega64 146:03e976389d16 148 }
mega64 146:03e976389d16 149
mega64 146:03e976389d16 150 if ((mode == OpenDrainPullUp) || (mode == PullUp)) {
mega64 146:03e976389d16 151 stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLUP);
mega64 146:03e976389d16 152 } else if ((mode == OpenDrainPullDown) || (mode == PullDown)) {
mega64 146:03e976389d16 153 stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLDOWN);
mega64 146:03e976389d16 154 } else {
mega64 146:03e976389d16 155 stm_pin_PullConfig(gpio, ll_pin, GPIO_NOPULL);
mega64 146:03e976389d16 156 }
mega64 146:03e976389d16 157 }