max32630fthr quad spi , unexpected spi behavior

Committer:
boonshen
Date:
Tue Mar 13 21:12:00 2018 +0000
Revision:
0:a35c40f49345
MAX32630FTHR QuadSPI test

Who changed what in which revision?

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