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 * Copyright (c) 2006-2016 ARM Limited
mega64 146:03e976389d16 3 *
mega64 146:03e976389d16 4 * Licensed under the Apache License, Version 2.0 (the "License");
mega64 146:03e976389d16 5 * you may not use this file except in compliance with the License.
mega64 146:03e976389d16 6 * You may obtain a copy of the License at
mega64 146:03e976389d16 7 *
mega64 146:03e976389d16 8 * http://www.apache.org/licenses/LICENSE-2.0
mega64 146:03e976389d16 9 *
mega64 146:03e976389d16 10 * Unless required by applicable law or agreed to in writing, software
mega64 146:03e976389d16 11 * distributed under the License is distributed on an "AS IS" BASIS,
mega64 146:03e976389d16 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mega64 146:03e976389d16 13 * See the License for the specific language governing permissions and
mega64 146:03e976389d16 14 * limitations under the License.
mega64 146:03e976389d16 15 */
mega64 146:03e976389d16 16 #include <stddef.h>
mega64 146:03e976389d16 17 #include "us_ticker_api.h"
mega64 146:03e976389d16 18 #include "PeripheralNames.h"
mega64 146:03e976389d16 19 #include "hal_tick.h"
mega64 146:03e976389d16 20
mega64 146:03e976389d16 21 // A 16-bit timer is used
mega64 146:03e976389d16 22 #if TIM_MST_16BIT
mega64 146:03e976389d16 23
mega64 146:03e976389d16 24 TIM_HandleTypeDef TimMasterHandle;
mega64 146:03e976389d16 25
mega64 146:03e976389d16 26 volatile uint32_t SlaveCounter = 0;
mega64 146:03e976389d16 27 volatile uint32_t oc_int_part = 0;
mega64 146:03e976389d16 28 volatile uint16_t oc_rem_part = 0;
mega64 146:03e976389d16 29 volatile uint8_t tim_it_update; // TIM_IT_UPDATE event flag set in timer_irq_handler()
mega64 146:03e976389d16 30 volatile uint32_t tim_it_counter = 0; // Time stamp to be updated by timer_irq_handler()
mega64 146:03e976389d16 31
mega64 146:03e976389d16 32 static int us_ticker_inited = 0;
mega64 146:03e976389d16 33
mega64 146:03e976389d16 34 void set_compare(uint16_t count)
mega64 146:03e976389d16 35 {
mega64 146:03e976389d16 36 TimMasterHandle.Instance = TIM_MST;
mega64 146:03e976389d16 37 // Set new output compare value
mega64 146:03e976389d16 38 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
mega64 146:03e976389d16 39 // Enable IT
mega64 146:03e976389d16 40 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
mega64 146:03e976389d16 41 }
mega64 146:03e976389d16 42
mega64 146:03e976389d16 43 void us_ticker_init(void)
mega64 146:03e976389d16 44 {
mega64 146:03e976389d16 45 if (us_ticker_inited) return;
mega64 146:03e976389d16 46 us_ticker_inited = 1;
mega64 146:03e976389d16 47
mega64 146:03e976389d16 48 TimMasterHandle.Instance = TIM_MST;
mega64 146:03e976389d16 49
mega64 146:03e976389d16 50 HAL_InitTick(0); // The passed value is not used
mega64 146:03e976389d16 51 }
mega64 146:03e976389d16 52
mega64 146:03e976389d16 53 uint32_t us_ticker_read()
mega64 146:03e976389d16 54 {
mega64 146:03e976389d16 55 uint32_t counter;
mega64 146:03e976389d16 56
mega64 146:03e976389d16 57 TimMasterHandle.Instance = TIM_MST;
mega64 146:03e976389d16 58
mega64 146:03e976389d16 59 if (!us_ticker_inited) us_ticker_init();
mega64 146:03e976389d16 60
mega64 146:03e976389d16 61 #if defined(TARGET_STM32L0)
mega64 146:03e976389d16 62 uint16_t cntH_old, cntH, cntL;
mega64 146:03e976389d16 63 do {
mega64 146:03e976389d16 64 // For some reason on L0xx series we need to read and clear the
mega64 146:03e976389d16 65 // overflow flag which give extra time to propelry handle possible
mega64 146:03e976389d16 66 // hiccup after ~60s
mega64 146:03e976389d16 67 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF) == SET) {
mega64 146:03e976389d16 68 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF);
mega64 146:03e976389d16 69 }
mega64 146:03e976389d16 70 cntH_old = SlaveCounter;
mega64 146:03e976389d16 71 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
mega64 146:03e976389d16 72 cntH_old += 1;
mega64 146:03e976389d16 73 }
mega64 146:03e976389d16 74 cntL = TIM_MST->CNT;
mega64 146:03e976389d16 75 cntH = SlaveCounter;
mega64 146:03e976389d16 76 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
mega64 146:03e976389d16 77 cntH += 1;
mega64 146:03e976389d16 78 }
mega64 146:03e976389d16 79 } while(cntH_old != cntH);
mega64 146:03e976389d16 80 // Glue the upper and lower part together to get a 32 bit timer
mega64 146:03e976389d16 81 return (uint32_t)(cntH << 16 | cntL);
mega64 146:03e976389d16 82 #else
mega64 146:03e976389d16 83 tim_it_update = 0; // Clear TIM_IT_UPDATE event flag
mega64 146:03e976389d16 84 counter = TIM_MST->CNT + (uint32_t)(SlaveCounter << 16); // Calculate new time stamp
mega64 146:03e976389d16 85 if (tim_it_update == 1) {
mega64 146:03e976389d16 86 return tim_it_counter; // In case of TIM_IT_UPDATE return the time stamp that was calculated in timer_irq_handler()
mega64 146:03e976389d16 87 }
mega64 146:03e976389d16 88 else {
mega64 146:03e976389d16 89 return counter; // Otherwise return the time stamp calculated here
mega64 146:03e976389d16 90 }
mega64 146:03e976389d16 91 #endif
mega64 146:03e976389d16 92 }
mega64 146:03e976389d16 93
mega64 146:03e976389d16 94 void us_ticker_set_interrupt(timestamp_t timestamp)
mega64 146:03e976389d16 95 {
mega64 146:03e976389d16 96 int delta = (int)((uint32_t)timestamp - us_ticker_read());
mega64 146:03e976389d16 97
mega64 146:03e976389d16 98 uint16_t cval = TIM_MST->CNT;
mega64 146:03e976389d16 99
mega64 146:03e976389d16 100 if (delta <= 0) { // This event was in the past
mega64 146:03e976389d16 101 us_ticker_irq_handler();
mega64 146:03e976389d16 102 } else {
mega64 146:03e976389d16 103 oc_int_part = (uint32_t)(delta >> 16);
mega64 146:03e976389d16 104 oc_rem_part = (uint16_t)(delta & 0xFFFF);
mega64 146:03e976389d16 105 if (oc_rem_part <= (0xFFFF - cval)) {
mega64 146:03e976389d16 106 set_compare(cval + oc_rem_part);
mega64 146:03e976389d16 107 oc_rem_part = 0;
mega64 146:03e976389d16 108 } else {
mega64 146:03e976389d16 109 set_compare(0xFFFF);
mega64 146:03e976389d16 110 oc_rem_part = oc_rem_part - (0xFFFF - cval);
mega64 146:03e976389d16 111 }
mega64 146:03e976389d16 112 }
mega64 146:03e976389d16 113 }
mega64 146:03e976389d16 114
mega64 146:03e976389d16 115 void us_ticker_disable_interrupt(void)
mega64 146:03e976389d16 116 {
mega64 146:03e976389d16 117 TimMasterHandle.Instance = TIM_MST;
mega64 146:03e976389d16 118 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
mega64 146:03e976389d16 119 }
mega64 146:03e976389d16 120
mega64 146:03e976389d16 121 void us_ticker_clear_interrupt(void)
mega64 146:03e976389d16 122 {
mega64 146:03e976389d16 123 TimMasterHandle.Instance = TIM_MST;
mega64 146:03e976389d16 124 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
mega64 146:03e976389d16 125 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
mega64 146:03e976389d16 126 }
mega64 146:03e976389d16 127 }
mega64 146:03e976389d16 128
mega64 146:03e976389d16 129 #endif // TIM_MST_16BIT