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_I2C_H
mega64 146:03e976389d16 17 #define MBED_I2C_H
mega64 146:03e976389d16 18
mega64 146:03e976389d16 19 #include "platform/platform.h"
mega64 146:03e976389d16 20
mega64 146:03e976389d16 21 #if DEVICE_I2C
mega64 146:03e976389d16 22
mega64 146:03e976389d16 23 #include "hal/i2c_api.h"
mega64 146:03e976389d16 24 #include "platform/SingletonPtr.h"
mega64 146:03e976389d16 25 #include "platform/PlatformMutex.h"
mega64 146:03e976389d16 26
mega64 146:03e976389d16 27 #if DEVICE_I2C_ASYNCH
mega64 146:03e976389d16 28 #include "platform/CThunk.h"
mega64 146:03e976389d16 29 #include "hal/dma_api.h"
mega64 146:03e976389d16 30 #include "platform/FunctionPointer.h"
mega64 146:03e976389d16 31 #endif
mega64 146:03e976389d16 32
mega64 146:03e976389d16 33 namespace mbed {
mega64 146:03e976389d16 34 /** \addtogroup drivers */
mega64 146:03e976389d16 35 /** @{*/
mega64 146:03e976389d16 36
mega64 146:03e976389d16 37 /** An I2C Master, used for communicating with I2C slave devices
mega64 146:03e976389d16 38 *
mega64 146:03e976389d16 39 * @Note Synchronization level: Thread safe
mega64 146:03e976389d16 40 *
mega64 146:03e976389d16 41 * Example:
mega64 146:03e976389d16 42 * @code
mega64 146:03e976389d16 43 * // Read from I2C slave at address 0x62
mega64 146:03e976389d16 44 *
mega64 146:03e976389d16 45 * #include "mbed.h"
mega64 146:03e976389d16 46 *
mega64 146:03e976389d16 47 * I2C i2c(p28, p27);
mega64 146:03e976389d16 48 *
mega64 146:03e976389d16 49 * int main() {
mega64 146:03e976389d16 50 * int address = 0x62;
mega64 146:03e976389d16 51 * char data[2];
mega64 146:03e976389d16 52 * i2c.read(address, data, 2);
mega64 146:03e976389d16 53 * }
mega64 146:03e976389d16 54 * @endcode
mega64 146:03e976389d16 55 */
mega64 146:03e976389d16 56 class I2C {
mega64 146:03e976389d16 57
mega64 146:03e976389d16 58 public:
mega64 146:03e976389d16 59 enum RxStatus {
mega64 146:03e976389d16 60 NoData,
mega64 146:03e976389d16 61 MasterGeneralCall,
mega64 146:03e976389d16 62 MasterWrite,
mega64 146:03e976389d16 63 MasterRead
mega64 146:03e976389d16 64 };
mega64 146:03e976389d16 65
mega64 146:03e976389d16 66 enum Acknowledge {
mega64 146:03e976389d16 67 NoACK = 0,
mega64 146:03e976389d16 68 ACK = 1
mega64 146:03e976389d16 69 };
mega64 146:03e976389d16 70
mega64 146:03e976389d16 71 /** Create an I2C Master interface, connected to the specified pins
mega64 146:03e976389d16 72 *
mega64 146:03e976389d16 73 * @param sda I2C data line pin
mega64 146:03e976389d16 74 * @param scl I2C clock line pin
mega64 146:03e976389d16 75 */
mega64 146:03e976389d16 76 I2C(PinName sda, PinName scl);
mega64 146:03e976389d16 77
mega64 146:03e976389d16 78 /** Set the frequency of the I2C interface
mega64 146:03e976389d16 79 *
mega64 146:03e976389d16 80 * @param hz The bus frequency in hertz
mega64 146:03e976389d16 81 */
mega64 146:03e976389d16 82 void frequency(int hz);
mega64 146:03e976389d16 83
mega64 146:03e976389d16 84 /** Read from an I2C slave
mega64 146:03e976389d16 85 *
mega64 146:03e976389d16 86 * Performs a complete read transaction. The bottom bit of
mega64 146:03e976389d16 87 * the address is forced to 1 to indicate a read.
mega64 146:03e976389d16 88 *
mega64 146:03e976389d16 89 * @param address 8-bit I2C slave address [ addr | 1 ]
mega64 146:03e976389d16 90 * @param data Pointer to the byte-array to read data in to
mega64 146:03e976389d16 91 * @param length Number of bytes to read
mega64 146:03e976389d16 92 * @param repeated Repeated start, true - don't send stop at end
mega64 146:03e976389d16 93 *
mega64 146:03e976389d16 94 * @returns
mega64 146:03e976389d16 95 * 0 on success (ack),
mega64 146:03e976389d16 96 * non-0 on failure (nack)
mega64 146:03e976389d16 97 */
mega64 146:03e976389d16 98 int read(int address, char *data, int length, bool repeated = false);
mega64 146:03e976389d16 99
mega64 146:03e976389d16 100 /** Read a single byte from the I2C bus
mega64 146:03e976389d16 101 *
mega64 146:03e976389d16 102 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
mega64 146:03e976389d16 103 *
mega64 146:03e976389d16 104 * @returns
mega64 146:03e976389d16 105 * the byte read
mega64 146:03e976389d16 106 */
mega64 146:03e976389d16 107 int read(int ack);
mega64 146:03e976389d16 108
mega64 146:03e976389d16 109 /** Write to an I2C slave
mega64 146:03e976389d16 110 *
mega64 146:03e976389d16 111 * Performs a complete write transaction. The bottom bit of
mega64 146:03e976389d16 112 * the address is forced to 0 to indicate a write.
mega64 146:03e976389d16 113 *
mega64 146:03e976389d16 114 * @param address 8-bit I2C slave address [ addr | 0 ]
mega64 146:03e976389d16 115 * @param data Pointer to the byte-array data to send
mega64 146:03e976389d16 116 * @param length Number of bytes to send
mega64 146:03e976389d16 117 * @param repeated Repeated start, true - do not send stop at end
mega64 146:03e976389d16 118 *
mega64 146:03e976389d16 119 * @returns
mega64 146:03e976389d16 120 * 0 or non-zero - written number of bytes,
mega64 146:03e976389d16 121 * negative - I2C_ERROR_XXX status
mega64 146:03e976389d16 122 */
mega64 146:03e976389d16 123 int write(int address, const char *data, int length, bool repeated = false);
mega64 146:03e976389d16 124
mega64 146:03e976389d16 125 /** Write single byte out on the I2C bus
mega64 146:03e976389d16 126 *
mega64 146:03e976389d16 127 * @param data data to write out on bus
mega64 146:03e976389d16 128 *
mega64 146:03e976389d16 129 * @returns
mega64 146:03e976389d16 130 * '0' - NAK was received
mega64 146:03e976389d16 131 * '1' - ACK was received,
mega64 146:03e976389d16 132 * '2' - timeout
mega64 146:03e976389d16 133 */
mega64 146:03e976389d16 134 int write(int data);
mega64 146:03e976389d16 135
mega64 146:03e976389d16 136 /** Creates a start condition on the I2C bus
mega64 146:03e976389d16 137 */
mega64 146:03e976389d16 138
mega64 146:03e976389d16 139 void start(void);
mega64 146:03e976389d16 140
mega64 146:03e976389d16 141 /** Creates a stop condition on the I2C bus
mega64 146:03e976389d16 142 */
mega64 146:03e976389d16 143 void stop(void);
mega64 146:03e976389d16 144
mega64 146:03e976389d16 145 /** Acquire exclusive access to this I2C bus
mega64 146:03e976389d16 146 */
mega64 146:03e976389d16 147 virtual void lock(void);
mega64 146:03e976389d16 148
mega64 146:03e976389d16 149 /** Release exclusive access to this I2C bus
mega64 146:03e976389d16 150 */
mega64 146:03e976389d16 151 virtual void unlock(void);
mega64 146:03e976389d16 152
mega64 146:03e976389d16 153 virtual ~I2C() {
mega64 146:03e976389d16 154 // Do nothing
mega64 146:03e976389d16 155 }
mega64 146:03e976389d16 156
mega64 146:03e976389d16 157 #if DEVICE_I2C_ASYNCH
mega64 146:03e976389d16 158
mega64 146:03e976389d16 159 /** Start non-blocking I2C transfer.
mega64 146:03e976389d16 160 *
mega64 146:03e976389d16 161 * @param address 8/10 bit I2c slave address
mega64 146:03e976389d16 162 * @param tx_buffer The TX buffer with data to be transfered
mega64 146:03e976389d16 163 * @param tx_length The length of TX buffer in bytes
mega64 146:03e976389d16 164 * @param rx_buffer The RX buffer which is used for received data
mega64 146:03e976389d16 165 * @param rx_length The length of RX buffer in bytes
mega64 146:03e976389d16 166 * @param event The logical OR of events to modify
mega64 146:03e976389d16 167 * @param callback The event callback function
mega64 146:03e976389d16 168 * @param repeated Repeated start, true - do not send stop at end
mega64 146:03e976389d16 169 * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
mega64 146:03e976389d16 170 */
mega64 146:03e976389d16 171 int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
mega64 146:03e976389d16 172
mega64 146:03e976389d16 173 /** Abort the on-going I2C transfer
mega64 146:03e976389d16 174 */
mega64 146:03e976389d16 175 void abort_transfer();
mega64 146:03e976389d16 176 protected:
mega64 146:03e976389d16 177 void irq_handler_asynch(void);
mega64 146:03e976389d16 178 event_callback_t _callback;
mega64 146:03e976389d16 179 CThunk<I2C> _irq;
mega64 146:03e976389d16 180 DMAUsage _usage;
mega64 146:03e976389d16 181 #endif
mega64 146:03e976389d16 182
mega64 146:03e976389d16 183 protected:
mega64 146:03e976389d16 184 void aquire();
mega64 146:03e976389d16 185
mega64 146:03e976389d16 186 i2c_t _i2c;
mega64 146:03e976389d16 187 static I2C *_owner;
mega64 146:03e976389d16 188 int _hz;
mega64 146:03e976389d16 189 static SingletonPtr<PlatformMutex> _mutex;
mega64 146:03e976389d16 190 };
mega64 146:03e976389d16 191
mega64 146:03e976389d16 192 } // namespace mbed
mega64 146:03e976389d16 193
mega64 146:03e976389d16 194 #endif
mega64 146:03e976389d16 195
mega64 146:03e976389d16 196 #endif
mega64 146:03e976389d16 197
mega64 146:03e976389d16 198 /** @}*/