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 Mar 16 06:15:53 2017 +0000
Revision:
146:03e976389d16
fully rebuild, now based on mbed-dev v160

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 "hal_tick.h"
mega64 146:03e976389d16 17
mega64 146:03e976389d16 18 // A 32-bit timer is used
mega64 146:03e976389d16 19 #if !TIM_MST_16BIT
mega64 146:03e976389d16 20
mega64 146:03e976389d16 21 #define DEBUG_TICK 0 // Set to 1 to toggle a pin (see below which pin) at each tick
mega64 146:03e976389d16 22
mega64 146:03e976389d16 23 extern TIM_HandleTypeDef TimMasterHandle;
mega64 146:03e976389d16 24
mega64 146:03e976389d16 25 extern void HAL_IncTick(void);
mega64 146:03e976389d16 26
mega64 146:03e976389d16 27 volatile uint32_t PreviousVal = 0;
mega64 146:03e976389d16 28
mega64 146:03e976389d16 29 void us_ticker_irq_handler(void);
mega64 146:03e976389d16 30
mega64 146:03e976389d16 31 void timer_irq_handler(void)
mega64 146:03e976389d16 32 {
mega64 146:03e976389d16 33 // Channel 1 for mbed timeout
mega64 146:03e976389d16 34 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
mega64 146:03e976389d16 35 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
mega64 146:03e976389d16 36 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
mega64 146:03e976389d16 37 us_ticker_irq_handler();
mega64 146:03e976389d16 38 }
mega64 146:03e976389d16 39 }
mega64 146:03e976389d16 40
mega64 146:03e976389d16 41 // Channel 2 for HAL tick
mega64 146:03e976389d16 42 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
mega64 146:03e976389d16 43 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
mega64 146:03e976389d16 44 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
mega64 146:03e976389d16 45 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
mega64 146:03e976389d16 46 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
mega64 146:03e976389d16 47 // Increment HAL variable
mega64 146:03e976389d16 48 HAL_IncTick();
mega64 146:03e976389d16 49 // Prepare next interrupt
mega64 146:03e976389d16 50 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
mega64 146:03e976389d16 51 PreviousVal = val;
mega64 146:03e976389d16 52 #if DEBUG_TICK > 0
mega64 146:03e976389d16 53 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
mega64 146:03e976389d16 54 #endif
mega64 146:03e976389d16 55 }
mega64 146:03e976389d16 56 }
mega64 146:03e976389d16 57 }
mega64 146:03e976389d16 58 }
mega64 146:03e976389d16 59
mega64 146:03e976389d16 60 // Reconfigure the HAL tick using a standard timer instead of systick.
mega64 146:03e976389d16 61 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
mega64 146:03e976389d16 62 {
mega64 146:03e976389d16 63 RCC_ClkInitTypeDef RCC_ClkInitStruct;
mega64 146:03e976389d16 64 uint32_t PclkFreq;
mega64 146:03e976389d16 65
mega64 146:03e976389d16 66 // Get clock configuration
mega64 146:03e976389d16 67 // Note: PclkFreq contains here the Latency (not used after)
mega64 146:03e976389d16 68 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
mega64 146:03e976389d16 69
mega64 146:03e976389d16 70 // Get timer clock value
mega64 146:03e976389d16 71 #if TIM_MST_PCLK == 1
mega64 146:03e976389d16 72 PclkFreq = HAL_RCC_GetPCLK1Freq();
mega64 146:03e976389d16 73 #else
mega64 146:03e976389d16 74 PclkFreq = HAL_RCC_GetPCLK2Freq();
mega64 146:03e976389d16 75 #endif
mega64 146:03e976389d16 76
mega64 146:03e976389d16 77 // Enable timer clock
mega64 146:03e976389d16 78 TIM_MST_RCC;
mega64 146:03e976389d16 79
mega64 146:03e976389d16 80 // Reset timer
mega64 146:03e976389d16 81 TIM_MST_RESET_ON;
mega64 146:03e976389d16 82 TIM_MST_RESET_OFF;
mega64 146:03e976389d16 83
mega64 146:03e976389d16 84 // Configure time base
mega64 146:03e976389d16 85 TimMasterHandle.Instance = TIM_MST;
mega64 146:03e976389d16 86 TimMasterHandle.Init.Period = 0xFFFFFFFF;
mega64 146:03e976389d16 87
mega64 146:03e976389d16 88 // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
mega64 146:03e976389d16 89 #if TIM_MST_PCLK == 1
mega64 146:03e976389d16 90 if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) {
mega64 146:03e976389d16 91 #else
mega64 146:03e976389d16 92 if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) {
mega64 146:03e976389d16 93 #endif
mega64 146:03e976389d16 94 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
mega64 146:03e976389d16 95 } else {
mega64 146:03e976389d16 96 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
mega64 146:03e976389d16 97 }
mega64 146:03e976389d16 98
mega64 146:03e976389d16 99 TimMasterHandle.Init.ClockDivision = 0;
mega64 146:03e976389d16 100 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
mega64 146:03e976389d16 101 #if !TARGET_STM32L1
mega64 146:03e976389d16 102 TimMasterHandle.Init.RepetitionCounter = 0;
mega64 146:03e976389d16 103 #endif
mega64 146:03e976389d16 104 #ifdef TARGET_STM32F0
mega64 146:03e976389d16 105 TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
mega64 146:03e976389d16 106 #endif
mega64 146:03e976389d16 107 HAL_TIM_OC_Init(&TimMasterHandle);
mega64 146:03e976389d16 108
mega64 146:03e976389d16 109 NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
mega64 146:03e976389d16 110 NVIC_EnableIRQ(TIM_MST_IRQ);
mega64 146:03e976389d16 111
mega64 146:03e976389d16 112 // Channel 1 for mbed timeout
mega64 146:03e976389d16 113 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
mega64 146:03e976389d16 114
mega64 146:03e976389d16 115 // Channel 2 for HAL tick
mega64 146:03e976389d16 116 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
mega64 146:03e976389d16 117 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
mega64 146:03e976389d16 118 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
mega64 146:03e976389d16 119 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
mega64 146:03e976389d16 120
mega64 146:03e976389d16 121 #if DEBUG_TICK > 0
mega64 146:03e976389d16 122 __GPIOB_CLK_ENABLE();
mega64 146:03e976389d16 123 GPIO_InitTypeDef GPIO_InitStruct;
mega64 146:03e976389d16 124 GPIO_InitStruct.Pin = GPIO_PIN_6;
mega64 146:03e976389d16 125 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
mega64 146:03e976389d16 126 GPIO_InitStruct.Pull = GPIO_PULLUP;
mega64 146:03e976389d16 127 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
mega64 146:03e976389d16 128 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
mega64 146:03e976389d16 129 #endif
mega64 146:03e976389d16 130
mega64 146:03e976389d16 131 return HAL_OK;
mega64 146:03e976389d16 132 }
mega64 146:03e976389d16 133
mega64 146:03e976389d16 134 void HAL_SuspendTick(void)
mega64 146:03e976389d16 135 {
mega64 146:03e976389d16 136 TimMasterHandle.Instance = TIM_MST;
mega64 146:03e976389d16 137 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
mega64 146:03e976389d16 138 }
mega64 146:03e976389d16 139
mega64 146:03e976389d16 140 void HAL_ResumeTick(void)
mega64 146:03e976389d16 141 {
mega64 146:03e976389d16 142 TimMasterHandle.Instance = TIM_MST;
mega64 146:03e976389d16 143 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
mega64 146:03e976389d16 144 }
mega64 146:03e976389d16 145
mega64 146:03e976389d16 146 #endif // !TIM_MST_16BIT