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-2015 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 #ifndef MBED_SPI_H
mega64 146:03e976389d16 17 #define MBED_SPI_H
mega64 146:03e976389d16 18
mega64 146:03e976389d16 19 #include "platform/platform.h"
mega64 146:03e976389d16 20
mega64 146:03e976389d16 21 #if DEVICE_SPI
mega64 146:03e976389d16 22
mega64 146:03e976389d16 23 #include "platform/PlatformMutex.h"
mega64 146:03e976389d16 24 #include "hal/spi_api.h"
mega64 146:03e976389d16 25 #include "platform/SingletonPtr.h"
mega64 146:03e976389d16 26
mega64 146:03e976389d16 27 #if DEVICE_SPI_ASYNCH
mega64 146:03e976389d16 28 #include "platform/CThunk.h"
mega64 146:03e976389d16 29 #include "hal/dma_api.h"
mega64 146:03e976389d16 30 #include "platform/CircularBuffer.h"
mega64 146:03e976389d16 31 #include "platform/FunctionPointer.h"
mega64 146:03e976389d16 32 #include "platform/Transaction.h"
mega64 146:03e976389d16 33 #endif
mega64 146:03e976389d16 34
mega64 146:03e976389d16 35 namespace mbed {
mega64 146:03e976389d16 36 /** \addtogroup drivers */
mega64 146:03e976389d16 37 /** @{*/
mega64 146:03e976389d16 38
mega64 146:03e976389d16 39 /** A SPI Master, used for communicating with SPI slave devices
mega64 146:03e976389d16 40 *
mega64 146:03e976389d16 41 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
mega64 146:03e976389d16 42 *
mega64 146:03e976389d16 43 * Most SPI devices will also require Chip Select and Reset signals. These
mega64 146:03e976389d16 44 * can be controlled using <DigitalOut> pins
mega64 146:03e976389d16 45 *
mega64 146:03e976389d16 46 * @Note Synchronization level: Thread safe
mega64 146:03e976389d16 47 *
mega64 146:03e976389d16 48 * Example:
mega64 146:03e976389d16 49 * @code
mega64 146:03e976389d16 50 * // Send a byte to a SPI slave, and record the response
mega64 146:03e976389d16 51 *
mega64 146:03e976389d16 52 * #include "mbed.h"
mega64 146:03e976389d16 53 *
mega64 146:03e976389d16 54 * // hardware ssel (where applicable)
mega64 146:03e976389d16 55 * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
mega64 146:03e976389d16 56 *
mega64 146:03e976389d16 57 * // software ssel
mega64 146:03e976389d16 58 * SPI device(p5, p6, p7); // mosi, miso, sclk
mega64 146:03e976389d16 59 * DigitalOut cs(p8); // ssel
mega64 146:03e976389d16 60 *
mega64 146:03e976389d16 61 * int main() {
mega64 146:03e976389d16 62 * // hardware ssel (where applicable)
mega64 146:03e976389d16 63 * //int response = device.write(0xFF);
mega64 146:03e976389d16 64 *
mega64 146:03e976389d16 65 * device.lock();
mega64 146:03e976389d16 66 * // software ssel
mega64 146:03e976389d16 67 * cs = 0;
mega64 146:03e976389d16 68 * int response = device.write(0xFF);
mega64 146:03e976389d16 69 * cs = 1;
mega64 146:03e976389d16 70 * device.unlock();
mega64 146:03e976389d16 71 *
mega64 146:03e976389d16 72 * }
mega64 146:03e976389d16 73 * @endcode
mega64 146:03e976389d16 74 */
mega64 146:03e976389d16 75 class SPI {
mega64 146:03e976389d16 76
mega64 146:03e976389d16 77 public:
mega64 146:03e976389d16 78
mega64 146:03e976389d16 79 /** Create a SPI master connected to the specified pins
mega64 146:03e976389d16 80 *
mega64 146:03e976389d16 81 * mosi or miso can be specfied as NC if not used
mega64 146:03e976389d16 82 *
mega64 146:03e976389d16 83 * @param mosi SPI Master Out, Slave In pin
mega64 146:03e976389d16 84 * @param miso SPI Master In, Slave Out pin
mega64 146:03e976389d16 85 * @param sclk SPI Clock pin
mega64 146:03e976389d16 86 * @param ssel SPI chip select pin
mega64 146:03e976389d16 87 */
mega64 146:03e976389d16 88 SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC);
mega64 146:03e976389d16 89
mega64 146:03e976389d16 90 /** Configure the data transmission format
mega64 146:03e976389d16 91 *
mega64 146:03e976389d16 92 * @param bits Number of bits per SPI frame (4 - 16)
mega64 146:03e976389d16 93 * @param mode Clock polarity and phase mode (0 - 3)
mega64 146:03e976389d16 94 *
mega64 146:03e976389d16 95 * @code
mega64 146:03e976389d16 96 * mode | POL PHA
mega64 146:03e976389d16 97 * -----+--------
mega64 146:03e976389d16 98 * 0 | 0 0
mega64 146:03e976389d16 99 * 1 | 0 1
mega64 146:03e976389d16 100 * 2 | 1 0
mega64 146:03e976389d16 101 * 3 | 1 1
mega64 146:03e976389d16 102 * @endcode
mega64 146:03e976389d16 103 */
mega64 146:03e976389d16 104 void format(int bits, int mode = 0);
mega64 146:03e976389d16 105
mega64 146:03e976389d16 106 /** Set the spi bus clock frequency
mega64 146:03e976389d16 107 *
mega64 146:03e976389d16 108 * @param hz SCLK frequency in hz (default = 1MHz)
mega64 146:03e976389d16 109 */
mega64 146:03e976389d16 110 void frequency(int hz = 1000000);
mega64 146:03e976389d16 111
mega64 146:03e976389d16 112 /** Write to the SPI Slave and return the response
mega64 146:03e976389d16 113 *
mega64 146:03e976389d16 114 * @param value Data to be sent to the SPI slave
mega64 146:03e976389d16 115 *
mega64 146:03e976389d16 116 * @returns
mega64 146:03e976389d16 117 * Response from the SPI slave
mega64 146:03e976389d16 118 */
mega64 146:03e976389d16 119 virtual int write(int value);
mega64 146:03e976389d16 120
mega64 146:03e976389d16 121 /** Acquire exclusive access to this SPI bus
mega64 146:03e976389d16 122 */
mega64 146:03e976389d16 123 virtual void lock(void);
mega64 146:03e976389d16 124
mega64 146:03e976389d16 125 /** Release exclusive access to this SPI bus
mega64 146:03e976389d16 126 */
mega64 146:03e976389d16 127 virtual void unlock(void);
mega64 146:03e976389d16 128
mega64 146:03e976389d16 129 #if DEVICE_SPI_ASYNCH
mega64 146:03e976389d16 130
mega64 146:03e976389d16 131 /** Start non-blocking SPI transfer using 8bit buffers.
mega64 146:03e976389d16 132 *
mega64 146:03e976389d16 133 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mega64 146:03e976389d16 134 * the default SPI value is sent
mega64 146:03e976389d16 135 * @param tx_length The length of TX buffer in bytes
mega64 146:03e976389d16 136 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mega64 146:03e976389d16 137 * received data are ignored
mega64 146:03e976389d16 138 * @param rx_length The length of RX buffer in bytes
mega64 146:03e976389d16 139 * @param callback The event callback function
mega64 146:03e976389d16 140 * @param event The logical OR of events to modify. Look at spi hal header file for SPI events.
mega64 146:03e976389d16 141 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
mega64 146:03e976389d16 142 */
mega64 146:03e976389d16 143 template<typename Type>
mega64 146:03e976389d16 144 int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) {
mega64 146:03e976389d16 145 if (spi_active(&_spi)) {
mega64 146:03e976389d16 146 return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
mega64 146:03e976389d16 147 }
mega64 146:03e976389d16 148 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
mega64 146:03e976389d16 149 return 0;
mega64 146:03e976389d16 150 }
mega64 146:03e976389d16 151
mega64 146:03e976389d16 152 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
mega64 146:03e976389d16 153 */
mega64 146:03e976389d16 154 void abort_transfer();
mega64 146:03e976389d16 155
mega64 146:03e976389d16 156 /** Clear the transaction buffer
mega64 146:03e976389d16 157 */
mega64 146:03e976389d16 158 void clear_transfer_buffer();
mega64 146:03e976389d16 159
mega64 146:03e976389d16 160 /** Clear the transaction buffer and abort on-going transfer.
mega64 146:03e976389d16 161 */
mega64 146:03e976389d16 162 void abort_all_transfers();
mega64 146:03e976389d16 163
mega64 146:03e976389d16 164 /** Configure DMA usage suggestion for non-blocking transfers
mega64 146:03e976389d16 165 *
mega64 146:03e976389d16 166 * @param usage The usage DMA hint for peripheral
mega64 146:03e976389d16 167 * @return Zero if the usage was set, -1 if a transaction is on-going
mega64 146:03e976389d16 168 */
mega64 146:03e976389d16 169 int set_dma_usage(DMAUsage usage);
mega64 146:03e976389d16 170
mega64 146:03e976389d16 171 protected:
mega64 146:03e976389d16 172 /** SPI IRQ handler
mega64 146:03e976389d16 173 *
mega64 146:03e976389d16 174 */
mega64 146:03e976389d16 175 void irq_handler_asynch(void);
mega64 146:03e976389d16 176
mega64 146:03e976389d16 177 /** Common transfer method
mega64 146:03e976389d16 178 *
mega64 146:03e976389d16 179 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mega64 146:03e976389d16 180 * the default SPI value is sent
mega64 146:03e976389d16 181 * @param tx_length The length of TX buffer in bytes
mega64 146:03e976389d16 182 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mega64 146:03e976389d16 183 * received data are ignored
mega64 146:03e976389d16 184 * @param rx_length The length of RX buffer in bytes
mega64 146:03e976389d16 185 * @param bit_width The buffers element width
mega64 146:03e976389d16 186 * @param callback The event callback function
mega64 146:03e976389d16 187 * @param event The logical OR of events to modify
mega64 146:03e976389d16 188 * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
mega64 146:03e976389d16 189 */
mega64 146:03e976389d16 190 int transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
mega64 146:03e976389d16 191
mega64 146:03e976389d16 192 /**
mega64 146:03e976389d16 193 *
mega64 146:03e976389d16 194 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mega64 146:03e976389d16 195 * the default SPI value is sent
mega64 146:03e976389d16 196 * @param tx_length The length of TX buffer in bytes
mega64 146:03e976389d16 197 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mega64 146:03e976389d16 198 * received data are ignored
mega64 146:03e976389d16 199 * @param rx_length The length of RX buffer in bytes
mega64 146:03e976389d16 200 * @param bit_width The buffers element width
mega64 146:03e976389d16 201 * @param callback The event callback function
mega64 146:03e976389d16 202 * @param event The logical OR of events to modify
mega64 146:03e976389d16 203 * @return Zero if a transfer was added to the queue, or -1 if the queue is full
mega64 146:03e976389d16 204 */
mega64 146:03e976389d16 205 int queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
mega64 146:03e976389d16 206
mega64 146:03e976389d16 207 /** Configures a callback, spi peripheral and initiate a new transfer
mega64 146:03e976389d16 208 *
mega64 146:03e976389d16 209 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mega64 146:03e976389d16 210 * the default SPI value is sent
mega64 146:03e976389d16 211 * @param tx_length The length of TX buffer in bytes
mega64 146:03e976389d16 212 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mega64 146:03e976389d16 213 * received data are ignored
mega64 146:03e976389d16 214 * @param rx_length The length of RX buffer in bytes
mega64 146:03e976389d16 215 * @param bit_width The buffers element width
mega64 146:03e976389d16 216 * @param callback The event callback function
mega64 146:03e976389d16 217 * @param event The logical OR of events to modify
mega64 146:03e976389d16 218 */
mega64 146:03e976389d16 219 void start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
mega64 146:03e976389d16 220
mega64 146:03e976389d16 221 #if TRANSACTION_QUEUE_SIZE_SPI
mega64 146:03e976389d16 222
mega64 146:03e976389d16 223 /** Start a new transaction
mega64 146:03e976389d16 224 *
mega64 146:03e976389d16 225 * @param data Transaction data
mega64 146:03e976389d16 226 */
mega64 146:03e976389d16 227 void start_transaction(transaction_t *data);
mega64 146:03e976389d16 228
mega64 146:03e976389d16 229 /** Dequeue a transaction
mega64 146:03e976389d16 230 *
mega64 146:03e976389d16 231 */
mega64 146:03e976389d16 232 void dequeue_transaction();
mega64 146:03e976389d16 233 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
mega64 146:03e976389d16 234 #endif
mega64 146:03e976389d16 235
mega64 146:03e976389d16 236 #endif
mega64 146:03e976389d16 237
mega64 146:03e976389d16 238 public:
mega64 146:03e976389d16 239 virtual ~SPI() {
mega64 146:03e976389d16 240 }
mega64 146:03e976389d16 241
mega64 146:03e976389d16 242 protected:
mega64 146:03e976389d16 243 spi_t _spi;
mega64 146:03e976389d16 244
mega64 146:03e976389d16 245 #if DEVICE_SPI_ASYNCH
mega64 146:03e976389d16 246 CThunk<SPI> _irq;
mega64 146:03e976389d16 247 event_callback_t _callback;
mega64 146:03e976389d16 248 DMAUsage _usage;
mega64 146:03e976389d16 249 #endif
mega64 146:03e976389d16 250
mega64 146:03e976389d16 251 void aquire(void);
mega64 146:03e976389d16 252 static SPI *_owner;
mega64 146:03e976389d16 253 static SingletonPtr<PlatformMutex> _mutex;
mega64 146:03e976389d16 254 int _bits;
mega64 146:03e976389d16 255 int _mode;
mega64 146:03e976389d16 256 int _hz;
mega64 146:03e976389d16 257 };
mega64 146:03e976389d16 258
mega64 146:03e976389d16 259 } // namespace mbed
mega64 146:03e976389d16 260
mega64 146:03e976389d16 261 #endif
mega64 146:03e976389d16 262
mega64 146:03e976389d16 263 #endif
mega64 146:03e976389d16 264
mega64 146:03e976389d16 265 /** @}*/