kubtss / Mbed 2 deprecated BIRD2017

Dependencies:   mbed-rtos mbed

Committer:
shimogamo
Date:
Tue Mar 21 05:50:52 2017 +0000
Revision:
37:d16ff13edcb2
Child:
38:18ac0f8628bf
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 pc.putc('a');
shimogamo 37:d16ff13edcb2 95 vector<uint8_t> write_buf = readSector(sector);
shimogamo 37:d16ff13edcb2 96 pc.putc('b');
shimogamo 37:d16ff13edcb2 97 sectorErase(sector);
shimogamo 37:d16ff13edcb2 98 pc.putc('c');
shimogamo 37:d16ff13edcb2 99
shimogamo 37:d16ff13edcb2 100 // write_bufの書き換え
shimogamo 37:d16ff13edcb2 101 for(int i = 0; i < write_buf.size(); i++) {
shimogamo 37:d16ff13edcb2 102 int data_index = sector * SECTOR_SIZE + i - addr;
shimogamo 37:d16ff13edcb2 103 if(0 <= data_index && data_index < data.size()) {
shimogamo 37:d16ff13edcb2 104 write_buf[i] = data[data_index];
shimogamo 37:d16ff13edcb2 105 }
shimogamo 37:d16ff13edcb2 106 }
shimogamo 37:d16ff13edcb2 107 pc.putc('d');
shimogamo 37:d16ff13edcb2 108
shimogamo 37:d16ff13edcb2 109 // 変更したwrite_bufをページ単位でメモリ書き込み
shimogamo 37:d16ff13edcb2 110 for(int page = 0; page < (write_buf.size() + PAGE_SIZE - 1) / PAGE_SIZE; page++) {
shimogamo 37:d16ff13edcb2 111 pc.printf("%d",page);
shimogamo 37:d16ff13edcb2 112 int length = min(write_buf.size() - page*PAGE_SIZE , PAGE_SIZE);
shimogamo 37:d16ff13edcb2 113 m_mx25r.writeEnable(); // send WREN 1st
shimogamo 37:d16ff13edcb2 114 m_mx25r.programPage(sector * SECTOR_SIZE + page*PAGE_SIZE, &write_buf[page*PAGE_SIZE], length);
shimogamo 37:d16ff13edcb2 115 do {
shimogamo 37:d16ff13edcb2 116 wait_ms(TIME_PP);
shimogamo 37:d16ff13edcb2 117 } while(isBusyInWriting());
shimogamo 37:d16ff13edcb2 118 }
shimogamo 37:d16ff13edcb2 119 }
shimogamo 37:d16ff13edcb2 120 }
shimogamo 37:d16ff13edcb2 121
shimogamo 37:d16ff13edcb2 122
shimogamo 37:d16ff13edcb2 123 void FlashManager::write(int addr, uint8_t data[], int size)
shimogamo 37:d16ff13edcb2 124 {
shimogamo 37:d16ff13edcb2 125 write(addr, vector<uint8_t>(data, data + size));
shimogamo 37:d16ff13edcb2 126 }
shimogamo 37:d16ff13edcb2 127
shimogamo 37:d16ff13edcb2 128
shimogamo 37:d16ff13edcb2 129 void FlashManager::write4byte(int addr, const vector<uint32_t>& data)
shimogamo 37:d16ff13edcb2 130 {
shimogamo 37:d16ff13edcb2 131 write(addr, type32to8(data));
shimogamo 37:d16ff13edcb2 132 }
shimogamo 37:d16ff13edcb2 133
shimogamo 37:d16ff13edcb2 134
shimogamo 37:d16ff13edcb2 135 void FlashManager::sectorErase(int sector)
shimogamo 37:d16ff13edcb2 136 {
shimogamo 37:d16ff13edcb2 137 pc.putc('s');
shimogamo 37:d16ff13edcb2 138 m_mx25r.writeEnable() ; // send WREN 1st
shimogamo 37:d16ff13edcb2 139 m_mx25r.sectorErase(sector * SECTOR_SIZE) ;
shimogamo 37:d16ff13edcb2 140 do {
shimogamo 37:d16ff13edcb2 141 pc.putc('.');
shimogamo 37:d16ff13edcb2 142 wait_ms(TIME_SE);
shimogamo 37:d16ff13edcb2 143 } while(isBusyInWriting()); // end poll
shimogamo 37:d16ff13edcb2 144 }
shimogamo 37:d16ff13edcb2 145
shimogamo 37:d16ff13edcb2 146
shimogamo 37:d16ff13edcb2 147 void FlashManager::chipErase()
shimogamo 37:d16ff13edcb2 148 {
shimogamo 37:d16ff13edcb2 149 m_mx25r.writeEnable() ; // send WREN 1st
shimogamo 37:d16ff13edcb2 150 m_mx25r.chipErase() ;
shimogamo 37:d16ff13edcb2 151 do {
shimogamo 37:d16ff13edcb2 152 wait_ms(TIME_CE);
shimogamo 37:d16ff13edcb2 153 } while(isBusyInWriting()); // end poll
shimogamo 37:d16ff13edcb2 154 }
shimogamo 37:d16ff13edcb2 155
shimogamo 37:d16ff13edcb2 156
shimogamo 37:d16ff13edcb2 157 void FlashManager::reset()
shimogamo 37:d16ff13edcb2 158 {
shimogamo 37:d16ff13edcb2 159 m_mx25r.resetEnable();
shimogamo 37:d16ff13edcb2 160 m_mx25r.noOperation();
shimogamo 37:d16ff13edcb2 161 m_mx25r.reset();
shimogamo 37:d16ff13edcb2 162 }
shimogamo 37:d16ff13edcb2 163
shimogamo 37:d16ff13edcb2 164
shimogamo 37:d16ff13edcb2 165 bool FlashManager::isBusyInWriting()
shimogamo 37:d16ff13edcb2 166 {
shimogamo 37:d16ff13edcb2 167 return m_mx25r.readStatus() & 0x01 == 0x01;
shimogamo 37:d16ff13edcb2 168 }
shimogamo 37:d16ff13edcb2 169
shimogamo 37:d16ff13edcb2 170
shimogamo 37:d16ff13edcb2 171 bool FlashManager::isValidRange(int addr)
shimogamo 37:d16ff13edcb2 172 {
shimogamo 37:d16ff13edcb2 173 return ADDR_MIN <= addr && addr <= ADDR_MAX;
shimogamo 37:d16ff13edcb2 174 }
shimogamo 37:d16ff13edcb2 175
shimogamo 37:d16ff13edcb2 176 //--------------double用に追加した関数--------------//
shimogamo 37:d16ff13edcb2 177 double FlashManager::readdouble(int addr){
shimogamo 37:d16ff13edcb2 178 vector<uint8_t> datavec = read(addr,addr+0x8-1);
shimogamo 37:d16ff13edcb2 179 return *(double*) &datavec[0];
shimogamo 37:d16ff13edcb2 180 }
shimogamo 37:d16ff13edcb2 181
shimogamo 37:d16ff13edcb2 182 vector<double> FlashManager::readdouble(int start_addr, int size){
shimogamo 37:d16ff13edcb2 183 vector<double> datavec;
shimogamo 37:d16ff13edcb2 184 for(int current_addr = start_addr; current_addr <= start_addr + 8*(size-1); current_addr += 8){
shimogamo 37:d16ff13edcb2 185 datavec.push_back(readdouble(current_addr));
shimogamo 37:d16ff13edcb2 186 }
shimogamo 37:d16ff13edcb2 187 return datavec;
shimogamo 37:d16ff13edcb2 188 }
shimogamo 37:d16ff13edcb2 189
shimogamo 37:d16ff13edcb2 190 void FlashManager::writedouble(int addr,double value){
shimogamo 37:d16ff13edcb2 191 vector<uint8_t> datavec(8);
shimogamo 37:d16ff13edcb2 192 *(double*) &datavec[0] = value;
shimogamo 37:d16ff13edcb2 193 write(addr,datavec);
shimogamo 37:d16ff13edcb2 194 }
shimogamo 37:d16ff13edcb2 195
shimogamo 37:d16ff13edcb2 196 void FlashManager::writedouble(int addr, const vector<double>& data){
shimogamo 37:d16ff13edcb2 197 vector<uint8_t> uint8data;
shimogamo 37:d16ff13edcb2 198 uint8_t temp[8];
shimogamo 37:d16ff13edcb2 199 for(int i = 0; i < data.size(); i++){
shimogamo 37:d16ff13edcb2 200 *(double*) temp = data[i];
shimogamo 37:d16ff13edcb2 201 for(int j=0; j < 8; j++){
shimogamo 37:d16ff13edcb2 202 uint8data.push_back(temp[j]);
shimogamo 37:d16ff13edcb2 203 }
shimogamo 37:d16ff13edcb2 204 }
shimogamo 37:d16ff13edcb2 205 write(addr,uint8data);
shimogamo 37:d16ff13edcb2 206 }