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)

drivers/FlashIAP.cpp

Committer:
mega64
Date:
2017-04-27
Revision:
148:8b0b02bf146f
Parent:
146:03e976389d16

File content as of revision 148:8b0b02bf146f:

/* mbed Microcontroller Library
 * Copyright (c) 2017 ARM Limited
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <string.h>
#include "FlashIAP.h"
#include "mbed_assert.h"


#ifdef DEVICE_FLASH

namespace mbed {

SingletonPtr<PlatformMutex> FlashIAP::_mutex;

static inline bool is_aligned(uint32_t number, uint32_t alignment)
{
    if ((number % alignment) != 0) {
        return false;
    } else {
        return true;
    }
}

FlashIAP::FlashIAP()
{

}

FlashIAP::~FlashIAP()
{

}

int FlashIAP::init()
{
    int ret = 0;
    _mutex->lock();
    if (flash_init(&_flash)) {
        ret = -1; 
    }
    _mutex->unlock();
    return ret;
}

int FlashIAP::deinit()
{
    int ret = 0;
    _mutex->lock();
    if (flash_free(&_flash)) {
        ret = -1; 
    }
    _mutex->unlock();
    return ret;
}


int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size)
{
    _mutex->lock();
    memcpy(buffer, (const void *)addr, size);
    _mutex->unlock();
    return 0;
}

int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)
{
    uint32_t page_size = get_page_size();
    uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
    // addr and size should be aligned to page size, and multiple of page size
    // page program should not cross sector boundaries
    if (!is_aligned(addr, page_size) ||
        !is_aligned(size, page_size) ||
        (size < page_size) ||
        (((addr % current_sector_size) + size) > current_sector_size)) {
        return -1;
    }

    int ret = 0;
    _mutex->lock();
    if (flash_program_page(&_flash, addr, (const uint8_t *)buffer, size)) {
        ret = -1;
    }
    _mutex->unlock();
    return ret;
}

bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size)
{
    uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
    if (!is_aligned(size, current_sector_size) ||
        !is_aligned(addr, current_sector_size)) {
        return false;
    } else {
        return true;
    }
}

int FlashIAP::erase(uint32_t addr, uint32_t size)
{
    uint32_t current_sector_size = 0UL;

    if (!is_aligned_to_sector(addr, size)) {
        return -1;
    }

    int32_t ret = 0;
    _mutex->lock();
    while (size) {
        ret = flash_erase_sector(&_flash, addr);
        if (ret != 0) {
            ret = -1;
            break;
        }
        current_sector_size = flash_get_sector_size(&_flash, addr);
        if (!is_aligned_to_sector(addr, size)) {
            ret = -1;
            break;
        }
        size -= current_sector_size;
        addr += current_sector_size;
    }
    _mutex->unlock();
    return ret;
}

uint32_t FlashIAP::get_page_size() const
{
    return flash_get_page_size(&_flash);
}

uint32_t FlashIAP::get_sector_size(uint32_t addr) const
{
    return flash_get_sector_size(&_flash, addr);
}

uint32_t FlashIAP::get_flash_start() const
{
    return flash_get_start_address(&_flash);
}

uint32_t FlashIAP::get_flash_size() const
{
    return flash_get_size(&_flash);
}

}

#endif