kubtss / Mbed 2 deprecated BIRD2017

Dependencies:   mbed-rtos mbed

Committer:
naoya1687
Date:
Wed Mar 22 06:59:44 2017 +0000
Revision:
38:18ac0f8628bf
Parent:
37:d16ff13edcb2
FlashMemory???????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimogamo 37:d16ff13edcb2 1 #include "mbed.h"
shimogamo 37:d16ff13edcb2 2 #include "FlashManager.h"
shimogamo 37:d16ff13edcb2 3 extern RawSerial pc;
shimogamo 37:d16ff13edcb2 4
shimogamo 37:d16ff13edcb2 5
shimogamo 37:d16ff13edcb2 6 //FlashManager::FlashManager(SPI spi, PinName cs, Serial* _pc) : m_mx25r(spi, cs), pc(_pc) { }
shimogamo 37:d16ff13edcb2 7
shimogamo 37:d16ff13edcb2 8 FlashManager::FlashManager(SPI spi, PinName cs) : m_mx25r(spi, cs) {
shimogamo 37:d16ff13edcb2 9 //spi.frequency(16000000);
shimogamo 37:d16ff13edcb2 10 }
shimogamo 37:d16ff13edcb2 11
shimogamo 37:d16ff13edcb2 12 //FlashManager::FlashManager(PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi,miso,sclk),m_mx25r(_spi,cs){}
shimogamo 37:d16ff13edcb2 13
shimogamo 37:d16ff13edcb2 14
shimogamo 37:d16ff13edcb2 15 /*FlashManager::~FlashManager()
shimogamo 37:d16ff13edcb2 16 {
shimogamo 37:d16ff13edcb2 17 pc = NULL;
shimogamo 37:d16ff13edcb2 18 }*/
shimogamo 37:d16ff13edcb2 19
shimogamo 37:d16ff13edcb2 20
shimogamo 37:d16ff13edcb2 21 int min(int a, int b)
shimogamo 37:d16ff13edcb2 22 {
shimogamo 37:d16ff13edcb2 23 return a < b ? a : b ;
shimogamo 37:d16ff13edcb2 24 }
shimogamo 37:d16ff13edcb2 25
shimogamo 37:d16ff13edcb2 26
shimogamo 37:d16ff13edcb2 27 vector<uint8_t> type32to8(const vector<uint32_t>& data)
shimogamo 37:d16ff13edcb2 28 {
shimogamo 37:d16ff13edcb2 29 vector<uint8_t> temp;
shimogamo 37:d16ff13edcb2 30 for(int i = 0; i < data.size()*4; i++) {
shimogamo 37:d16ff13edcb2 31 temp.push_back((uint8_t)((data[i/4] >> (24 - (i%4)*8)) & 0xFF));
shimogamo 37:d16ff13edcb2 32 }
shimogamo 37:d16ff13edcb2 33 return temp;
shimogamo 37:d16ff13edcb2 34 }
shimogamo 37:d16ff13edcb2 35
shimogamo 37:d16ff13edcb2 36
shimogamo 37:d16ff13edcb2 37 vector<uint32_t> type8to32(const vector<uint8_t>& data)
shimogamo 37:d16ff13edcb2 38 {
shimogamo 37:d16ff13edcb2 39 vector<uint32_t> temp;
shimogamo 37:d16ff13edcb2 40 for(int i = 0; i < data.size()/4; i++) {
shimogamo 37:d16ff13edcb2 41 uint32_t val = 0;
shimogamo 37:d16ff13edcb2 42 for(int j = 0; j < 4; j++) {
shimogamo 37:d16ff13edcb2 43 val += (uint32_t)data[i*4+j] << (24 - j*8);
shimogamo 37:d16ff13edcb2 44 }
shimogamo 37:d16ff13edcb2 45 temp.push_back(val);
shimogamo 37:d16ff13edcb2 46 }
shimogamo 37:d16ff13edcb2 47 return temp;
shimogamo 37:d16ff13edcb2 48 }
shimogamo 37:d16ff13edcb2 49
shimogamo 37:d16ff13edcb2 50
shimogamo 37:d16ff13edcb2 51 uint8_t FlashManager::read(int addr)
shimogamo 37:d16ff13edcb2 52 {
shimogamo 37:d16ff13edcb2 53 return m_mx25r.read8(addr);
shimogamo 37:d16ff13edcb2 54 }
shimogamo 37:d16ff13edcb2 55
shimogamo 37:d16ff13edcb2 56
shimogamo 37:d16ff13edcb2 57 uint32_t FlashManager::read4byte(int addr)
shimogamo 37:d16ff13edcb2 58 {
shimogamo 37:d16ff13edcb2 59 uint32_t temp = 0;
shimogamo 37:d16ff13edcb2 60 vector<uint8_t> data = m_mx25r.readFREAD(addr, addr+0x3);
shimogamo 37:d16ff13edcb2 61 for(int i = 0; i < 4; i++) {
shimogamo 37:d16ff13edcb2 62 temp += (uint32_t)data[i] << (24 - i*8);
shimogamo 37:d16ff13edcb2 63 }
shimogamo 37:d16ff13edcb2 64 return temp;
shimogamo 37:d16ff13edcb2 65 }
shimogamo 37:d16ff13edcb2 66
shimogamo 37:d16ff13edcb2 67
shimogamo 37:d16ff13edcb2 68 vector<uint8_t> FlashManager::readSector(int sector)
shimogamo 37:d16ff13edcb2 69 {
shimogamo 37:d16ff13edcb2 70 return m_mx25r.readFREAD(sector * SECTOR_SIZE, (sector + 1) * SECTOR_SIZE - 0x01);
shimogamo 37:d16ff13edcb2 71 }
shimogamo 37:d16ff13edcb2 72
shimogamo 37:d16ff13edcb2 73
shimogamo 37:d16ff13edcb2 74 vector<uint8_t> FlashManager::read(int start_addr, int end_addr)
shimogamo 37:d16ff13edcb2 75 {
shimogamo 37:d16ff13edcb2 76 return m_mx25r.readFREAD(start_addr, end_addr);
shimogamo 37:d16ff13edcb2 77 }
shimogamo 37:d16ff13edcb2 78
shimogamo 37:d16ff13edcb2 79
shimogamo 37:d16ff13edcb2 80 vector<uint32_t> FlashManager::read4byte(int start_addr, int end_addr)
shimogamo 37:d16ff13edcb2 81 {
shimogamo 37:d16ff13edcb2 82 return type8to32(m_mx25r.readFREAD(start_addr, end_addr));
shimogamo 37:d16ff13edcb2 83 }
shimogamo 37:d16ff13edcb2 84
shimogamo 37:d16ff13edcb2 85
shimogamo 37:d16ff13edcb2 86 void FlashManager::write(int addr, const vector<uint8_t>& data)
shimogamo 37:d16ff13edcb2 87 {
shimogamo 37:d16ff13edcb2 88 // 書き換えが必要なsectorの計算
shimogamo 37:d16ff13edcb2 89 int start_sector = addr / SECTOR_SIZE;
shimogamo 37:d16ff13edcb2 90 int end_sector = (addr + data.size() - 0x1) / SECTOR_SIZE;
shimogamo 37:d16ff13edcb2 91
shimogamo 37:d16ff13edcb2 92 for(int sector = start_sector; sector <= end_sector; sector++) {
shimogamo 37:d16ff13edcb2 93 // bufferに一時退避させ,sector削除
shimogamo 37:d16ff13edcb2 94 vector<uint8_t> write_buf = readSector(sector);
shimogamo 37:d16ff13edcb2 95 sectorErase(sector);
shimogamo 37:d16ff13edcb2 96
shimogamo 37:d16ff13edcb2 97 // write_bufの書き換え
shimogamo 37:d16ff13edcb2 98 for(int i = 0; i < write_buf.size(); i++) {
shimogamo 37:d16ff13edcb2 99 int data_index = sector * SECTOR_SIZE + i - addr;
shimogamo 37:d16ff13edcb2 100 if(0 <= data_index && data_index < data.size()) {
shimogamo 37:d16ff13edcb2 101 write_buf[i] = data[data_index];
shimogamo 37:d16ff13edcb2 102 }
shimogamo 37:d16ff13edcb2 103 }
shimogamo 37:d16ff13edcb2 104
shimogamo 37:d16ff13edcb2 105 // 変更したwrite_bufをページ単位でメモリ書き込み
shimogamo 37:d16ff13edcb2 106 for(int page = 0; page < (write_buf.size() + PAGE_SIZE - 1) / PAGE_SIZE; page++) {
shimogamo 37:d16ff13edcb2 107 int length = min(write_buf.size() - page*PAGE_SIZE , PAGE_SIZE);
shimogamo 37:d16ff13edcb2 108 m_mx25r.writeEnable(); // send WREN 1st
shimogamo 37:d16ff13edcb2 109 m_mx25r.programPage(sector * SECTOR_SIZE + page*PAGE_SIZE, &write_buf[page*PAGE_SIZE], length);
shimogamo 37:d16ff13edcb2 110 do {
shimogamo 37:d16ff13edcb2 111 wait_ms(TIME_PP);
shimogamo 37:d16ff13edcb2 112 } while(isBusyInWriting());
shimogamo 37:d16ff13edcb2 113 }
shimogamo 37:d16ff13edcb2 114 }
shimogamo 37:d16ff13edcb2 115 }
shimogamo 37:d16ff13edcb2 116
shimogamo 37:d16ff13edcb2 117
shimogamo 37:d16ff13edcb2 118 void FlashManager::write(int addr, uint8_t data[], int size)
shimogamo 37:d16ff13edcb2 119 {
shimogamo 37:d16ff13edcb2 120 write(addr, vector<uint8_t>(data, data + size));
shimogamo 37:d16ff13edcb2 121 }
shimogamo 37:d16ff13edcb2 122
shimogamo 37:d16ff13edcb2 123
shimogamo 37:d16ff13edcb2 124 void FlashManager::write4byte(int addr, const vector<uint32_t>& data)
shimogamo 37:d16ff13edcb2 125 {
shimogamo 37:d16ff13edcb2 126 write(addr, type32to8(data));
shimogamo 37:d16ff13edcb2 127 }
shimogamo 37:d16ff13edcb2 128
shimogamo 37:d16ff13edcb2 129
shimogamo 37:d16ff13edcb2 130 void FlashManager::sectorErase(int sector)
shimogamo 37:d16ff13edcb2 131 {
shimogamo 37:d16ff13edcb2 132 m_mx25r.writeEnable() ; // send WREN 1st
shimogamo 37:d16ff13edcb2 133 m_mx25r.sectorErase(sector * SECTOR_SIZE) ;
shimogamo 37:d16ff13edcb2 134 do {
shimogamo 37:d16ff13edcb2 135 wait_ms(TIME_SE);
shimogamo 37:d16ff13edcb2 136 } while(isBusyInWriting()); // end poll
shimogamo 37:d16ff13edcb2 137 }
shimogamo 37:d16ff13edcb2 138
shimogamo 37:d16ff13edcb2 139
shimogamo 37:d16ff13edcb2 140 void FlashManager::chipErase()
shimogamo 37:d16ff13edcb2 141 {
shimogamo 37:d16ff13edcb2 142 m_mx25r.writeEnable() ; // send WREN 1st
shimogamo 37:d16ff13edcb2 143 m_mx25r.chipErase() ;
shimogamo 37:d16ff13edcb2 144 do {
shimogamo 37:d16ff13edcb2 145 wait_ms(TIME_CE);
shimogamo 37:d16ff13edcb2 146 } while(isBusyInWriting()); // end poll
shimogamo 37:d16ff13edcb2 147 }
shimogamo 37:d16ff13edcb2 148
shimogamo 37:d16ff13edcb2 149
shimogamo 37:d16ff13edcb2 150 void FlashManager::reset()
shimogamo 37:d16ff13edcb2 151 {
shimogamo 37:d16ff13edcb2 152 m_mx25r.resetEnable();
shimogamo 37:d16ff13edcb2 153 m_mx25r.noOperation();
shimogamo 37:d16ff13edcb2 154 m_mx25r.reset();
shimogamo 37:d16ff13edcb2 155 }
shimogamo 37:d16ff13edcb2 156
shimogamo 37:d16ff13edcb2 157
shimogamo 37:d16ff13edcb2 158 bool FlashManager::isBusyInWriting()
shimogamo 37:d16ff13edcb2 159 {
shimogamo 37:d16ff13edcb2 160 return m_mx25r.readStatus() & 0x01 == 0x01;
shimogamo 37:d16ff13edcb2 161 }
shimogamo 37:d16ff13edcb2 162
shimogamo 37:d16ff13edcb2 163
shimogamo 37:d16ff13edcb2 164 bool FlashManager::isValidRange(int addr)
shimogamo 37:d16ff13edcb2 165 {
shimogamo 37:d16ff13edcb2 166 return ADDR_MIN <= addr && addr <= ADDR_MAX;
shimogamo 37:d16ff13edcb2 167 }
shimogamo 37:d16ff13edcb2 168
shimogamo 37:d16ff13edcb2 169 //--------------double用に追加した関数--------------//
shimogamo 37:d16ff13edcb2 170 double FlashManager::readdouble(int addr){
shimogamo 37:d16ff13edcb2 171 vector<uint8_t> datavec = read(addr,addr+0x8-1);
shimogamo 37:d16ff13edcb2 172 return *(double*) &datavec[0];
shimogamo 37:d16ff13edcb2 173 }
shimogamo 37:d16ff13edcb2 174
shimogamo 37:d16ff13edcb2 175 vector<double> FlashManager::readdouble(int start_addr, int size){
shimogamo 37:d16ff13edcb2 176 vector<double> datavec;
shimogamo 37:d16ff13edcb2 177 for(int current_addr = start_addr; current_addr <= start_addr + 8*(size-1); current_addr += 8){
shimogamo 37:d16ff13edcb2 178 datavec.push_back(readdouble(current_addr));
shimogamo 37:d16ff13edcb2 179 }
shimogamo 37:d16ff13edcb2 180 return datavec;
shimogamo 37:d16ff13edcb2 181 }
shimogamo 37:d16ff13edcb2 182
shimogamo 37:d16ff13edcb2 183 void FlashManager::writedouble(int addr,double value){
shimogamo 37:d16ff13edcb2 184 vector<uint8_t> datavec(8);
shimogamo 37:d16ff13edcb2 185 *(double*) &datavec[0] = value;
shimogamo 37:d16ff13edcb2 186 write(addr,datavec);
shimogamo 37:d16ff13edcb2 187 }
shimogamo 37:d16ff13edcb2 188
shimogamo 37:d16ff13edcb2 189 void FlashManager::writedouble(int addr, const vector<double>& data){
shimogamo 37:d16ff13edcb2 190 vector<uint8_t> uint8data;
shimogamo 37:d16ff13edcb2 191 uint8_t temp[8];
shimogamo 37:d16ff13edcb2 192 for(int i = 0; i < data.size(); i++){
shimogamo 37:d16ff13edcb2 193 *(double*) temp = data[i];
shimogamo 37:d16ff13edcb2 194 for(int j=0; j < 8; j++){
shimogamo 37:d16ff13edcb2 195 uint8data.push_back(temp[j]);
shimogamo 37:d16ff13edcb2 196 }
shimogamo 37:d16ff13edcb2 197 }
shimogamo 37:d16ff13edcb2 198 write(addr,uint8data);
shimogamo 37:d16ff13edcb2 199 }