Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 * Copyright (c) 2017 ARM Limited
lypinator 0:bb348c97df44 3 *
lypinator 0:bb348c97df44 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
lypinator 0:bb348c97df44 5 * of this software and associated documentation files (the "Software"), to deal
lypinator 0:bb348c97df44 6 * in the Software without restriction, including without limitation the rights
lypinator 0:bb348c97df44 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
lypinator 0:bb348c97df44 8 * copies of the Software, and to permit persons to whom the Software is
lypinator 0:bb348c97df44 9 * furnished to do so, subject to the following conditions:
lypinator 0:bb348c97df44 10 *
lypinator 0:bb348c97df44 11 * The above copyright notice and this permission notice shall be included in
lypinator 0:bb348c97df44 12 * all copies or substantial portions of the Software.
lypinator 0:bb348c97df44 13 *
lypinator 0:bb348c97df44 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
lypinator 0:bb348c97df44 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
lypinator 0:bb348c97df44 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
lypinator 0:bb348c97df44 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
lypinator 0:bb348c97df44 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
lypinator 0:bb348c97df44 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
lypinator 0:bb348c97df44 20 * SOFTWARE.
lypinator 0:bb348c97df44 21 */
lypinator 0:bb348c97df44 22
lypinator 0:bb348c97df44 23 #include <stdio.h>
lypinator 0:bb348c97df44 24 #include <string.h>
lypinator 0:bb348c97df44 25 #include <algorithm>
lypinator 0:bb348c97df44 26 #include "FlashIAP.h"
lypinator 0:bb348c97df44 27 #include "mbed_assert.h"
lypinator 0:bb348c97df44 28
lypinator 0:bb348c97df44 29
lypinator 0:bb348c97df44 30 #ifdef DEVICE_FLASH
lypinator 0:bb348c97df44 31
lypinator 0:bb348c97df44 32 namespace mbed {
lypinator 0:bb348c97df44 33
lypinator 0:bb348c97df44 34 SingletonPtr<PlatformMutex> FlashIAP::_mutex;
lypinator 0:bb348c97df44 35
lypinator 0:bb348c97df44 36 static inline bool is_aligned(uint32_t number, uint32_t alignment)
lypinator 0:bb348c97df44 37 {
lypinator 0:bb348c97df44 38 if ((number % alignment) != 0) {
lypinator 0:bb348c97df44 39 return false;
lypinator 0:bb348c97df44 40 } else {
lypinator 0:bb348c97df44 41 return true;
lypinator 0:bb348c97df44 42 }
lypinator 0:bb348c97df44 43 }
lypinator 0:bb348c97df44 44
lypinator 0:bb348c97df44 45 FlashIAP::FlashIAP()
lypinator 0:bb348c97df44 46 {
lypinator 0:bb348c97df44 47
lypinator 0:bb348c97df44 48 }
lypinator 0:bb348c97df44 49
lypinator 0:bb348c97df44 50 FlashIAP::~FlashIAP()
lypinator 0:bb348c97df44 51 {
lypinator 0:bb348c97df44 52
lypinator 0:bb348c97df44 53 }
lypinator 0:bb348c97df44 54
lypinator 0:bb348c97df44 55 int FlashIAP::init()
lypinator 0:bb348c97df44 56 {
lypinator 0:bb348c97df44 57 int ret = 0;
lypinator 0:bb348c97df44 58 _mutex->lock();
lypinator 0:bb348c97df44 59 if (flash_init(&_flash)) {
lypinator 0:bb348c97df44 60 ret = -1;
lypinator 0:bb348c97df44 61 }
lypinator 0:bb348c97df44 62 uint32_t page_size = get_page_size();
lypinator 0:bb348c97df44 63 _page_buf = new uint8_t[page_size];
lypinator 0:bb348c97df44 64
lypinator 0:bb348c97df44 65 _mutex->unlock();
lypinator 0:bb348c97df44 66 return ret;
lypinator 0:bb348c97df44 67 }
lypinator 0:bb348c97df44 68
lypinator 0:bb348c97df44 69 int FlashIAP::deinit()
lypinator 0:bb348c97df44 70 {
lypinator 0:bb348c97df44 71 int ret = 0;
lypinator 0:bb348c97df44 72 _mutex->lock();
lypinator 0:bb348c97df44 73 if (flash_free(&_flash)) {
lypinator 0:bb348c97df44 74 ret = -1;
lypinator 0:bb348c97df44 75 }
lypinator 0:bb348c97df44 76 delete[] _page_buf;
lypinator 0:bb348c97df44 77 _mutex->unlock();
lypinator 0:bb348c97df44 78 return ret;
lypinator 0:bb348c97df44 79 }
lypinator 0:bb348c97df44 80
lypinator 0:bb348c97df44 81
lypinator 0:bb348c97df44 82 int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size)
lypinator 0:bb348c97df44 83 {
lypinator 0:bb348c97df44 84 int32_t ret = -1;
lypinator 0:bb348c97df44 85 _mutex->lock();
lypinator 0:bb348c97df44 86 ret = flash_read(&_flash, addr, (uint8_t *) buffer, size);
lypinator 0:bb348c97df44 87 _mutex->unlock();
lypinator 0:bb348c97df44 88 return ret;
lypinator 0:bb348c97df44 89 }
lypinator 0:bb348c97df44 90
lypinator 0:bb348c97df44 91 int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)
lypinator 0:bb348c97df44 92 {
lypinator 0:bb348c97df44 93 uint32_t page_size = get_page_size();
lypinator 0:bb348c97df44 94 uint32_t flash_size = flash_get_size(&_flash);
lypinator 0:bb348c97df44 95 uint32_t flash_start_addr = flash_get_start_address(&_flash);
lypinator 0:bb348c97df44 96 uint32_t chunk, prog_size;
lypinator 0:bb348c97df44 97 const uint8_t *buf = (uint8_t *) buffer;
lypinator 0:bb348c97df44 98 const uint8_t *prog_buf;
lypinator 0:bb348c97df44 99
lypinator 0:bb348c97df44 100 // addr should be aligned to page size
lypinator 0:bb348c97df44 101 if (!is_aligned(addr, page_size) || (!buffer) ||
lypinator 0:bb348c97df44 102 ((addr + size) > (flash_start_addr + flash_size))) {
lypinator 0:bb348c97df44 103 return -1;
lypinator 0:bb348c97df44 104 }
lypinator 0:bb348c97df44 105
lypinator 0:bb348c97df44 106 int ret = 0;
lypinator 0:bb348c97df44 107 _mutex->lock();
lypinator 0:bb348c97df44 108 while (size) {
lypinator 0:bb348c97df44 109 uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
lypinator 0:bb348c97df44 110 bool unaligned_src = (((size_t) buf / sizeof(uint32_t) * sizeof(uint32_t)) != (size_t) buf);
lypinator 0:bb348c97df44 111 chunk = std::min(current_sector_size - (addr % current_sector_size), size);
lypinator 0:bb348c97df44 112 // Need to use the internal page buffer in any of these two cases:
lypinator 0:bb348c97df44 113 // 1. Size is not page aligned
lypinator 0:bb348c97df44 114 // 2. Source buffer is not aligned to uint32_t. This is not supported by many targets (although
lypinator 0:bb348c97df44 115 // the pointer they accept is of uint8_t).
lypinator 0:bb348c97df44 116 if (unaligned_src || (chunk < page_size)) {
lypinator 0:bb348c97df44 117 chunk = std::min(chunk, page_size);
lypinator 0:bb348c97df44 118 memcpy(_page_buf, buf, chunk);
lypinator 0:bb348c97df44 119 if (chunk < page_size) {
lypinator 0:bb348c97df44 120 memset(_page_buf + chunk, 0xFF, page_size - chunk);
lypinator 0:bb348c97df44 121 }
lypinator 0:bb348c97df44 122 prog_buf = _page_buf;
lypinator 0:bb348c97df44 123 prog_size = page_size;
lypinator 0:bb348c97df44 124 } else {
lypinator 0:bb348c97df44 125 chunk = chunk / page_size * page_size;
lypinator 0:bb348c97df44 126 prog_buf = buf;
lypinator 0:bb348c97df44 127 prog_size = chunk;
lypinator 0:bb348c97df44 128 }
lypinator 0:bb348c97df44 129 if (flash_program_page(&_flash, addr, prog_buf, prog_size)) {
lypinator 0:bb348c97df44 130 ret = -1;
lypinator 0:bb348c97df44 131 break;
lypinator 0:bb348c97df44 132 }
lypinator 0:bb348c97df44 133 size -= chunk;
lypinator 0:bb348c97df44 134 addr += chunk;
lypinator 0:bb348c97df44 135 buf += chunk;
lypinator 0:bb348c97df44 136 }
lypinator 0:bb348c97df44 137 _mutex->unlock();
lypinator 0:bb348c97df44 138
lypinator 0:bb348c97df44 139 return ret;
lypinator 0:bb348c97df44 140 }
lypinator 0:bb348c97df44 141
lypinator 0:bb348c97df44 142 bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size)
lypinator 0:bb348c97df44 143 {
lypinator 0:bb348c97df44 144 uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
lypinator 0:bb348c97df44 145 if (!is_aligned(size, current_sector_size) ||
lypinator 0:bb348c97df44 146 !is_aligned(addr, current_sector_size)) {
lypinator 0:bb348c97df44 147 return false;
lypinator 0:bb348c97df44 148 } else {
lypinator 0:bb348c97df44 149 return true;
lypinator 0:bb348c97df44 150 }
lypinator 0:bb348c97df44 151 }
lypinator 0:bb348c97df44 152
lypinator 0:bb348c97df44 153 int FlashIAP::erase(uint32_t addr, uint32_t size)
lypinator 0:bb348c97df44 154 {
lypinator 0:bb348c97df44 155 uint32_t current_sector_size;
lypinator 0:bb348c97df44 156 uint32_t flash_size = flash_get_size(&_flash);
lypinator 0:bb348c97df44 157 uint32_t flash_start_addr = flash_get_start_address(&_flash);
lypinator 0:bb348c97df44 158 uint32_t flash_end_addr = flash_start_addr + flash_size;
lypinator 0:bb348c97df44 159 uint32_t erase_end_addr = addr + size;
lypinator 0:bb348c97df44 160
lypinator 0:bb348c97df44 161 if (erase_end_addr > flash_end_addr) {
lypinator 0:bb348c97df44 162 return -1;
lypinator 0:bb348c97df44 163 } else if (erase_end_addr < flash_end_addr) {
lypinator 0:bb348c97df44 164 uint32_t following_sector_size = flash_get_sector_size(&_flash, erase_end_addr);
lypinator 0:bb348c97df44 165 if (!is_aligned(erase_end_addr, following_sector_size)) {
lypinator 0:bb348c97df44 166 return -1;
lypinator 0:bb348c97df44 167 }
lypinator 0:bb348c97df44 168 }
lypinator 0:bb348c97df44 169
lypinator 0:bb348c97df44 170 int32_t ret = 0;
lypinator 0:bb348c97df44 171 _mutex->lock();
lypinator 0:bb348c97df44 172 while (size) {
lypinator 0:bb348c97df44 173 ret = flash_erase_sector(&_flash, addr);
lypinator 0:bb348c97df44 174 if (ret != 0) {
lypinator 0:bb348c97df44 175 ret = -1;
lypinator 0:bb348c97df44 176 break;
lypinator 0:bb348c97df44 177 }
lypinator 0:bb348c97df44 178 current_sector_size = flash_get_sector_size(&_flash, addr);
lypinator 0:bb348c97df44 179 size -= current_sector_size;
lypinator 0:bb348c97df44 180 addr += current_sector_size;
lypinator 0:bb348c97df44 181 }
lypinator 0:bb348c97df44 182 _mutex->unlock();
lypinator 0:bb348c97df44 183 return ret;
lypinator 0:bb348c97df44 184 }
lypinator 0:bb348c97df44 185
lypinator 0:bb348c97df44 186 uint32_t FlashIAP::get_page_size() const
lypinator 0:bb348c97df44 187 {
lypinator 0:bb348c97df44 188 return flash_get_page_size(&_flash);
lypinator 0:bb348c97df44 189 }
lypinator 0:bb348c97df44 190
lypinator 0:bb348c97df44 191 uint32_t FlashIAP::get_sector_size(uint32_t addr) const
lypinator 0:bb348c97df44 192 {
lypinator 0:bb348c97df44 193 return flash_get_sector_size(&_flash, addr);
lypinator 0:bb348c97df44 194 }
lypinator 0:bb348c97df44 195
lypinator 0:bb348c97df44 196 uint32_t FlashIAP::get_flash_start() const
lypinator 0:bb348c97df44 197 {
lypinator 0:bb348c97df44 198 return flash_get_start_address(&_flash);
lypinator 0:bb348c97df44 199 }
lypinator 0:bb348c97df44 200
lypinator 0:bb348c97df44 201 uint32_t FlashIAP::get_flash_size() const
lypinator 0:bb348c97df44 202 {
lypinator 0:bb348c97df44 203 return flash_get_size(&_flash);
lypinator 0:bb348c97df44 204 }
lypinator 0:bb348c97df44 205
lypinator 0:bb348c97df44 206 }
lypinator 0:bb348c97df44 207
lypinator 0:bb348c97df44 208 #endif