Modification of Mbed-dev library for LQFP48 package microcontrollers: STM32F103C8 (STM32F103C8T6) and STM32F103CB (STM32F103CBT6) (Bluepill boards, Maple mini etc. )

Fork of mbed-dev by mbed official

Warning!

This library is unable to work due to huge changes in the mbed toolchain. Do not use with mbed online!

New wersion:

Import librarymbed-STM32F103C8

Modification of Mbed-dev library for LQFP48 package microcontrollers: STM32F103C8 (STM32F103C8T6) and STM32F103CB (STM32F103CBT6) (Bluepill boards, Maple mini etc. )




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 clock (mbed 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)

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

Committer:
mega64
Date:
Mon Aug 29 01:00:12 2016 +0000
Revision:
145:54b3c5994df6
Parent:
119:3921aeca8633
Change peripherals and pins definitions from LQFP64 to LQFP48 case.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:9b334a45a8ff 1 /* General C++ Object Thunking class
bogdanm 0:9b334a45a8ff 2 *
bogdanm 0:9b334a45a8ff 3 * - allows direct callbacks to non-static C++ class functions
bogdanm 0:9b334a45a8ff 4 * - keeps track for the corresponding class instance
bogdanm 0:9b334a45a8ff 5 * - supports an optional context parameter for the called function
bogdanm 0:9b334a45a8ff 6 * - ideally suited for class object receiving interrupts (NVIC_SetVector)
bogdanm 0:9b334a45a8ff 7 *
bogdanm 0:9b334a45a8ff 8 * Copyright (c) 2014-2015 ARM Limited
bogdanm 0:9b334a45a8ff 9 *
bogdanm 0:9b334a45a8ff 10 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 0:9b334a45a8ff 11 * you may not use this file except in compliance with the License.
bogdanm 0:9b334a45a8ff 12 * You may obtain a copy of the License at
bogdanm 0:9b334a45a8ff 13 *
bogdanm 0:9b334a45a8ff 14 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 0:9b334a45a8ff 15 *
bogdanm 0:9b334a45a8ff 16 * Unless required by applicable law or agreed to in writing, software
bogdanm 0:9b334a45a8ff 17 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 0:9b334a45a8ff 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 0:9b334a45a8ff 19 * See the License for the specific language governing permissions and
bogdanm 0:9b334a45a8ff 20 * limitations under the License.
bogdanm 0:9b334a45a8ff 21 */
bogdanm 0:9b334a45a8ff 22 #ifndef __CTHUNK_H__
bogdanm 0:9b334a45a8ff 23 #define __CTHUNK_H__
bogdanm 0:9b334a45a8ff 24
bogdanm 0:9b334a45a8ff 25 #define CTHUNK_ADDRESS 1
bogdanm 0:9b334a45a8ff 26
mbed_official 119:3921aeca8633 27 #if (defined(__CORTEX_M3) || defined(__CORTEX_M4) || defined(__thumb2__)) && ! defined(__CORTEX_A9)
bogdanm 0:9b334a45a8ff 28 #define CTHUNK_VARIABLES volatile uint32_t code[1]
bogdanm 0:9b334a45a8ff 29 /**
bogdanm 0:9b334a45a8ff 30 * CTHUNK disassembly for Cortex-M3/M4 (thumb2):
bogdanm 0:9b334a45a8ff 31 * * ldm.w pc,{r0,r1,r2,pc}
bogdanm 0:9b334a45a8ff 32 *
bogdanm 0:9b334a45a8ff 33 * This instruction loads the arguments for the static thunking function to r0-r2, and
bogdanm 0:9b334a45a8ff 34 * branches to that function by loading its address into PC.
bogdanm 0:9b334a45a8ff 35 *
bogdanm 0:9b334a45a8ff 36 * This is safe for both regular calling and interrupt calling, since it only touches scratch registers
bogdanm 0:9b334a45a8ff 37 * which should be saved by the caller, and are automatically saved as part of the IRQ context switch.
bogdanm 0:9b334a45a8ff 38 */
bogdanm 0:9b334a45a8ff 39 #define CTHUNK_ASSIGMENT m_thunk.code[0] = 0x8007E89F
bogdanm 0:9b334a45a8ff 40
mbed_official 119:3921aeca8633 41 #elif defined(__CORTEX_M0PLUS) || defined(__CORTEX_M0) || defined(__CORTEX_A9)
bogdanm 0:9b334a45a8ff 42 /*
bogdanm 0:9b334a45a8ff 43 * CTHUNK disassembly for Cortex M0 (thumb):
bogdanm 0:9b334a45a8ff 44 * * push {r0,r1,r2,r3,r4,lr} save touched registers and return address
bogdanm 0:9b334a45a8ff 45 * * movs r4,#4 set up address to load arguments from (immediately following this code block) (1)
bogdanm 0:9b334a45a8ff 46 * * add r4,pc set up address to load arguments from (immediately following this code block) (2)
bogdanm 0:9b334a45a8ff 47 * * ldm r4!,{r0,r1,r2,r3} load arguments for static thunk function
bogdanm 0:9b334a45a8ff 48 * * blx r3 call static thunk function
bogdanm 0:9b334a45a8ff 49 * * pop {r0,r1,r2,r3,r4,pc} restore scratch registers and return from function
bogdanm 0:9b334a45a8ff 50 */
bogdanm 0:9b334a45a8ff 51 #define CTHUNK_VARIABLES volatile uint32_t code[3]
bogdanm 0:9b334a45a8ff 52 #define CTHUNK_ASSIGMENT do { \
bogdanm 0:9b334a45a8ff 53 m_thunk.code[0] = 0x2404B51F; \
bogdanm 0:9b334a45a8ff 54 m_thunk.code[1] = 0xCC0F447C; \
bogdanm 0:9b334a45a8ff 55 m_thunk.code[2] = 0xBD1F4798; \
bogdanm 0:9b334a45a8ff 56 } while (0)
bogdanm 0:9b334a45a8ff 57
bogdanm 0:9b334a45a8ff 58 #else
bogdanm 0:9b334a45a8ff 59 #error "Target is not currently suported."
bogdanm 0:9b334a45a8ff 60 #endif
bogdanm 0:9b334a45a8ff 61
bogdanm 0:9b334a45a8ff 62 /* IRQ/Exception compatible thunk entry function */
bogdanm 0:9b334a45a8ff 63 typedef void (*CThunkEntry)(void);
bogdanm 0:9b334a45a8ff 64
bogdanm 0:9b334a45a8ff 65 template<class T>
bogdanm 0:9b334a45a8ff 66 class CThunk
bogdanm 0:9b334a45a8ff 67 {
bogdanm 0:9b334a45a8ff 68 public:
bogdanm 0:9b334a45a8ff 69 typedef void (T::*CCallbackSimple)(void);
bogdanm 0:9b334a45a8ff 70 typedef void (T::*CCallback)(void* context);
bogdanm 0:9b334a45a8ff 71
bogdanm 0:9b334a45a8ff 72 inline CThunk(T *instance)
bogdanm 0:9b334a45a8ff 73 {
bogdanm 0:9b334a45a8ff 74 init(instance, NULL, NULL);
bogdanm 0:9b334a45a8ff 75 }
bogdanm 0:9b334a45a8ff 76
bogdanm 0:9b334a45a8ff 77 inline CThunk(T *instance, CCallback callback)
bogdanm 0:9b334a45a8ff 78 {
bogdanm 0:9b334a45a8ff 79 init(instance, callback, NULL);
bogdanm 0:9b334a45a8ff 80 }
bogdanm 0:9b334a45a8ff 81
bogdanm 0:9b334a45a8ff 82 ~CThunk() {
bogdanm 0:9b334a45a8ff 83
bogdanm 0:9b334a45a8ff 84 }
bogdanm 0:9b334a45a8ff 85
bogdanm 0:9b334a45a8ff 86 inline CThunk(T *instance, CCallbackSimple callback)
bogdanm 0:9b334a45a8ff 87 {
bogdanm 0:9b334a45a8ff 88 init(instance, (CCallback)callback, NULL);
bogdanm 0:9b334a45a8ff 89 }
bogdanm 0:9b334a45a8ff 90
bogdanm 0:9b334a45a8ff 91 inline CThunk(T &instance, CCallback callback)
bogdanm 0:9b334a45a8ff 92 {
bogdanm 0:9b334a45a8ff 93 init(instance, callback, NULL);
bogdanm 0:9b334a45a8ff 94 }
bogdanm 0:9b334a45a8ff 95
bogdanm 0:9b334a45a8ff 96 inline CThunk(T &instance, CCallbackSimple callback)
bogdanm 0:9b334a45a8ff 97 {
bogdanm 0:9b334a45a8ff 98 init(instance, (CCallback)callback, NULL);
bogdanm 0:9b334a45a8ff 99 }
bogdanm 0:9b334a45a8ff 100
bogdanm 0:9b334a45a8ff 101 inline CThunk(T &instance, CCallback callback, void* context)
bogdanm 0:9b334a45a8ff 102 {
bogdanm 0:9b334a45a8ff 103 init(instance, callback, context);
bogdanm 0:9b334a45a8ff 104 }
bogdanm 0:9b334a45a8ff 105
bogdanm 0:9b334a45a8ff 106 inline void callback(CCallback callback)
bogdanm 0:9b334a45a8ff 107 {
bogdanm 0:9b334a45a8ff 108 m_callback = callback;
bogdanm 0:9b334a45a8ff 109 }
bogdanm 0:9b334a45a8ff 110
bogdanm 0:9b334a45a8ff 111 inline void callback(CCallbackSimple callback)
bogdanm 0:9b334a45a8ff 112 {
bogdanm 0:9b334a45a8ff 113 m_callback = (CCallback)callback;
bogdanm 0:9b334a45a8ff 114 }
bogdanm 0:9b334a45a8ff 115
bogdanm 0:9b334a45a8ff 116 inline void context(void* context)
bogdanm 0:9b334a45a8ff 117 {
bogdanm 0:9b334a45a8ff 118 m_thunk.context = (uint32_t)context;
bogdanm 0:9b334a45a8ff 119 }
bogdanm 0:9b334a45a8ff 120
bogdanm 0:9b334a45a8ff 121 inline void context(uint32_t context)
bogdanm 0:9b334a45a8ff 122 {
bogdanm 0:9b334a45a8ff 123 m_thunk.context = context;
bogdanm 0:9b334a45a8ff 124 }
bogdanm 0:9b334a45a8ff 125
bogdanm 0:9b334a45a8ff 126 inline uint32_t entry(void)
bogdanm 0:9b334a45a8ff 127 {
bogdanm 0:9b334a45a8ff 128 return (((uint32_t)&m_thunk)|CTHUNK_ADDRESS);
bogdanm 0:9b334a45a8ff 129 }
bogdanm 0:9b334a45a8ff 130
bogdanm 0:9b334a45a8ff 131 /* get thunk entry point for connecting rhunk to an IRQ table */
bogdanm 0:9b334a45a8ff 132 inline operator CThunkEntry(void)
bogdanm 0:9b334a45a8ff 133 {
bogdanm 0:9b334a45a8ff 134 return (CThunkEntry)entry();
bogdanm 0:9b334a45a8ff 135 }
bogdanm 0:9b334a45a8ff 136
bogdanm 0:9b334a45a8ff 137 /* get thunk entry point for connecting rhunk to an IRQ table */
bogdanm 0:9b334a45a8ff 138 inline operator uint32_t(void)
bogdanm 0:9b334a45a8ff 139 {
bogdanm 0:9b334a45a8ff 140 return entry();
bogdanm 0:9b334a45a8ff 141 }
bogdanm 0:9b334a45a8ff 142
bogdanm 0:9b334a45a8ff 143 /* simple test function */
bogdanm 0:9b334a45a8ff 144 inline void call(void)
bogdanm 0:9b334a45a8ff 145 {
bogdanm 0:9b334a45a8ff 146 (((CThunkEntry)(entry()))());
bogdanm 0:9b334a45a8ff 147 }
bogdanm 0:9b334a45a8ff 148
bogdanm 0:9b334a45a8ff 149 private:
bogdanm 0:9b334a45a8ff 150 T* m_instance;
bogdanm 0:9b334a45a8ff 151 volatile CCallback m_callback;
bogdanm 0:9b334a45a8ff 152
bogdanm 0:9b334a45a8ff 153 // TODO: this needs proper fix, to refactor toolchain header file and all its use
bogdanm 0:9b334a45a8ff 154 // PACKED there is not defined properly for IAR
bogdanm 0:9b334a45a8ff 155 #if defined (__ICCARM__)
bogdanm 0:9b334a45a8ff 156 typedef __packed struct
bogdanm 0:9b334a45a8ff 157 {
bogdanm 0:9b334a45a8ff 158 CTHUNK_VARIABLES;
bogdanm 0:9b334a45a8ff 159 volatile uint32_t instance;
bogdanm 0:9b334a45a8ff 160 volatile uint32_t context;
bogdanm 0:9b334a45a8ff 161 volatile uint32_t callback;
bogdanm 0:9b334a45a8ff 162 volatile uint32_t trampoline;
bogdanm 0:9b334a45a8ff 163 } CThunkTrampoline;
bogdanm 0:9b334a45a8ff 164 #else
bogdanm 0:9b334a45a8ff 165 typedef struct
bogdanm 0:9b334a45a8ff 166 {
bogdanm 0:9b334a45a8ff 167 CTHUNK_VARIABLES;
bogdanm 0:9b334a45a8ff 168 volatile uint32_t instance;
bogdanm 0:9b334a45a8ff 169 volatile uint32_t context;
bogdanm 0:9b334a45a8ff 170 volatile uint32_t callback;
bogdanm 0:9b334a45a8ff 171 volatile uint32_t trampoline;
bogdanm 0:9b334a45a8ff 172 } __attribute__((__packed__)) CThunkTrampoline;
bogdanm 0:9b334a45a8ff 173 #endif
bogdanm 0:9b334a45a8ff 174
bogdanm 0:9b334a45a8ff 175 static void trampoline(T* instance, void* context, CCallback* callback)
bogdanm 0:9b334a45a8ff 176 {
bogdanm 0:9b334a45a8ff 177 if(instance && *callback) {
bogdanm 0:9b334a45a8ff 178 (static_cast<T*>(instance)->**callback)(context);
bogdanm 0:9b334a45a8ff 179 }
bogdanm 0:9b334a45a8ff 180 }
bogdanm 0:9b334a45a8ff 181
bogdanm 0:9b334a45a8ff 182 volatile CThunkTrampoline m_thunk;
bogdanm 0:9b334a45a8ff 183
bogdanm 0:9b334a45a8ff 184 inline void init(T *instance, CCallback callback, void* context)
bogdanm 0:9b334a45a8ff 185 {
bogdanm 0:9b334a45a8ff 186 /* remember callback - need to add this level of redirection
bogdanm 0:9b334a45a8ff 187 as pointer size for member functions differs between platforms */
bogdanm 0:9b334a45a8ff 188 m_callback = callback;
bogdanm 0:9b334a45a8ff 189
bogdanm 0:9b334a45a8ff 190 /* populate thunking trampoline */
bogdanm 0:9b334a45a8ff 191 CTHUNK_ASSIGMENT;
bogdanm 0:9b334a45a8ff 192 m_thunk.context = (uint32_t)context;
bogdanm 0:9b334a45a8ff 193 m_thunk.instance = (uint32_t)instance;
bogdanm 0:9b334a45a8ff 194 m_thunk.callback = (uint32_t)&m_callback;
bogdanm 0:9b334a45a8ff 195 m_thunk.trampoline = (uint32_t)&trampoline;
bogdanm 0:9b334a45a8ff 196
mbed_official 119:3921aeca8633 197 #if defined(__CORTEX_A9)
mbed_official 119:3921aeca8633 198 /* Data cache clean */
mbed_official 119:3921aeca8633 199 /* Cache control */
mbed_official 119:3921aeca8633 200 {
mbed_official 119:3921aeca8633 201 uint32_t start_addr = (uint32_t)&m_thunk & 0xFFFFFFE0;
mbed_official 119:3921aeca8633 202 uint32_t end_addr = (uint32_t)&m_thunk + sizeof(m_thunk);
mbed_official 119:3921aeca8633 203 uint32_t addr;
mbed_official 119:3921aeca8633 204
mbed_official 119:3921aeca8633 205 /* Data cache clean and invalid */
mbed_official 119:3921aeca8633 206 for (addr = start_addr; addr < end_addr; addr += 0x20) {
mbed_official 119:3921aeca8633 207 __v7_clean_inv_dcache_mva((void *)addr);
mbed_official 119:3921aeca8633 208 }
mbed_official 119:3921aeca8633 209 /* Instruction cache invalid */
mbed_official 119:3921aeca8633 210 __v7_inv_icache_all();
mbed_official 119:3921aeca8633 211 __ca9u_inv_tlb_all();
mbed_official 119:3921aeca8633 212 __v7_inv_btac();
mbed_official 119:3921aeca8633 213 }
mbed_official 119:3921aeca8633 214 #endif
bogdanm 0:9b334a45a8ff 215 __ISB();
bogdanm 0:9b334a45a8ff 216 __DSB();
bogdanm 0:9b334a45a8ff 217 }
bogdanm 0:9b334a45a8ff 218 };
bogdanm 0:9b334a45a8ff 219
bogdanm 0:9b334a45a8ff 220 #endif/*__CTHUNK_H__*/