Version for FWST0001_v2_00

Fork of 25LCxxx_SPI by Hendrik Lipka

Committer:
hlipka
Date:
Sat Feb 19 18:29:20 2011 +0000
Revision:
2:3a3404dbd3eb
Parent:
1:c29a67c7034d
Child:
3:314812139e9c
added license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 2:3a3404dbd3eb 1 /*
hlipka 2:3a3404dbd3eb 2 * Ser25lcxxx library
hlipka 2:3a3404dbd3eb 3 * Copyright (c) 2010 Hendrik Lipka
hlipka 2:3a3404dbd3eb 4 *
hlipka 2:3a3404dbd3eb 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
hlipka 2:3a3404dbd3eb 6 * of this software and associated documentation files (the "Software"), to deal
hlipka 2:3a3404dbd3eb 7 * in the Software without restriction, including without limitation the rights
hlipka 2:3a3404dbd3eb 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hlipka 2:3a3404dbd3eb 9 * copies of the Software, and to permit persons to whom the Software is
hlipka 2:3a3404dbd3eb 10 * furnished to do so, subject to the following conditions:
hlipka 2:3a3404dbd3eb 11 *
hlipka 2:3a3404dbd3eb 12 * The above copyright notice and this permission notice shall be included in
hlipka 2:3a3404dbd3eb 13 * all copies or substantial portions of the Software.
hlipka 2:3a3404dbd3eb 14 *
hlipka 2:3a3404dbd3eb 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hlipka 2:3a3404dbd3eb 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hlipka 2:3a3404dbd3eb 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hlipka 2:3a3404dbd3eb 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hlipka 2:3a3404dbd3eb 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hlipka 2:3a3404dbd3eb 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hlipka 2:3a3404dbd3eb 21 * THE SOFTWARE.
hlipka 2:3a3404dbd3eb 22 */
hlipka 2:3a3404dbd3eb 23
hlipka 0:238ca4fdef8c 24 #include "Ser25lcxxx.h"
hlipka 0:238ca4fdef8c 25 #include "wait_api.h"
hlipka 0:238ca4fdef8c 26
hlipka 0:238ca4fdef8c 27 #define HIGH(x) ((x&0xff00)>>8)
hlipka 0:238ca4fdef8c 28 #define LOW(x) (x&0xff)
hlipka 0:238ca4fdef8c 29
hlipka 0:238ca4fdef8c 30 Ser25LCxxx::Ser25LCxxx(SPI *spi, PinName enable, int bytes, int pagesize) {
hlipka 0:238ca4fdef8c 31 _spi=spi;
hlipka 0:238ca4fdef8c 32 _enable=new DigitalOut(enable);
hlipka 0:238ca4fdef8c 33 _size=bytes;
hlipka 0:238ca4fdef8c 34 _pageSize=pagesize;
hlipka 0:238ca4fdef8c 35 _enable->write(1);
hlipka 0:238ca4fdef8c 36 }
hlipka 0:238ca4fdef8c 37
hlipka 0:238ca4fdef8c 38 Ser25LCxxx::~Ser25LCxxx() {
hlipka 0:238ca4fdef8c 39 delete _enable;
hlipka 0:238ca4fdef8c 40 }
hlipka 0:238ca4fdef8c 41
hlipka 0:238ca4fdef8c 42 char* Ser25LCxxx::read(unsigned int startAdr, unsigned int len) {
hlipka 0:238ca4fdef8c 43 // assertion
hlipka 0:238ca4fdef8c 44 if (startAdr+len>_size)
hlipka 0:238ca4fdef8c 45 return NULL;
hlipka 0:238ca4fdef8c 46 char* ret=(char*)malloc(len);
hlipka 0:238ca4fdef8c 47 _enable->write(0);
hlipka 0:238ca4fdef8c 48 wait_us(1);
hlipka 0:238ca4fdef8c 49 // send address
hlipka 0:238ca4fdef8c 50 if (_size<512) { // 256 and 128 bytes
hlipka 0:238ca4fdef8c 51 _spi->write(0x03);
hlipka 0:238ca4fdef8c 52 _spi->write(LOW(startAdr));
hlipka 0:238ca4fdef8c 53 } else if (512==_size) { // 4k variant adds 9th address bit to command
hlipka 0:238ca4fdef8c 54 _spi->write(startAdr>255?0xb:0x3);
hlipka 0:238ca4fdef8c 55 _spi->write(LOW(startAdr));
hlipka 0:238ca4fdef8c 56 } else if (_size<131072) { // everything up to 512k
hlipka 0:238ca4fdef8c 57 _spi->write(0x03);
hlipka 0:238ca4fdef8c 58 _spi->write(HIGH(startAdr));
hlipka 0:238ca4fdef8c 59 _spi->write(LOW(startAdr));
hlipka 0:238ca4fdef8c 60 } else { // 25xx1024, needs 3 byte address
hlipka 0:238ca4fdef8c 61 _spi->write(0x03);
hlipka 0:238ca4fdef8c 62 _spi->write(startAdr>>16);
hlipka 0:238ca4fdef8c 63 _spi->write(HIGH(startAdr));
hlipka 0:238ca4fdef8c 64 _spi->write(LOW(startAdr));
hlipka 0:238ca4fdef8c 65 }
hlipka 0:238ca4fdef8c 66 // read data into buffer
hlipka 0:238ca4fdef8c 67 for (int i=0;i<len;i++) {
hlipka 0:238ca4fdef8c 68 ret[i]=_spi->write(0);
hlipka 0:238ca4fdef8c 69 }
hlipka 0:238ca4fdef8c 70 wait_us(1);
hlipka 0:238ca4fdef8c 71 _enable->write(1);
hlipka 0:238ca4fdef8c 72 return ret;
hlipka 0:238ca4fdef8c 73 }
hlipka 0:238ca4fdef8c 74
hlipka 0:238ca4fdef8c 75 bool Ser25LCxxx::write(unsigned int startAdr, unsigned int len, const char* data) {
hlipka 0:238ca4fdef8c 76 if (startAdr+len>_size)
hlipka 0:238ca4fdef8c 77 return -1;
hlipka 0:238ca4fdef8c 78
hlipka 0:238ca4fdef8c 79 int ofs=0;
hlipka 0:238ca4fdef8c 80 while (ofs<len) {
hlipka 0:238ca4fdef8c 81 // calculate amount of data to write into current page
hlipka 0:238ca4fdef8c 82 int pageLen=_pageSize-((startAdr+ofs)%_pageSize);
hlipka 0:238ca4fdef8c 83 if (ofs+pageLen>len)
hlipka 0:238ca4fdef8c 84 pageLen=len-ofs;
hlipka 0:238ca4fdef8c 85 // write single page
hlipka 0:238ca4fdef8c 86 bool b=writePage(startAdr+ofs,pageLen,data+ofs);
hlipka 0:238ca4fdef8c 87 if (!b)
hlipka 0:238ca4fdef8c 88 return false;
hlipka 0:238ca4fdef8c 89 // and switch to next page
hlipka 0:238ca4fdef8c 90 ofs+=pageLen;
hlipka 0:238ca4fdef8c 91 }
hlipka 0:238ca4fdef8c 92 return true;
hlipka 0:238ca4fdef8c 93 }
hlipka 0:238ca4fdef8c 94
hlipka 0:238ca4fdef8c 95 bool Ser25LCxxx::writePage(unsigned int startAdr, unsigned int len, const char* data) {
hlipka 0:238ca4fdef8c 96 enableWrite();
hlipka 0:238ca4fdef8c 97
hlipka 0:238ca4fdef8c 98 _enable->write(0);
hlipka 0:238ca4fdef8c 99 wait_us(1);
hlipka 0:238ca4fdef8c 100
hlipka 0:238ca4fdef8c 101 if (_size<512) { // 256 and 128 bytes
hlipka 0:238ca4fdef8c 102 _spi->write(0x02);
hlipka 0:238ca4fdef8c 103 _spi->write(LOW(startAdr));
hlipka 0:238ca4fdef8c 104 } else if (512==_size) { // 4k variant adds 9th address bit to command
hlipka 0:238ca4fdef8c 105 _spi->write(startAdr>255?0xa:0x2);
hlipka 0:238ca4fdef8c 106 _spi->write(LOW(startAdr));
hlipka 0:238ca4fdef8c 107 } else if (_size<131072) { // everything up to 512k
hlipka 0:238ca4fdef8c 108 _spi->write(0x02);
hlipka 0:238ca4fdef8c 109 _spi->write(HIGH(startAdr));
hlipka 0:238ca4fdef8c 110 _spi->write(LOW(startAdr));
hlipka 0:238ca4fdef8c 111 } else { // 25xx1024, needs 3 byte address
hlipka 0:238ca4fdef8c 112 _spi->write(0x02);
hlipka 0:238ca4fdef8c 113 _spi->write(startAdr>>16);
hlipka 0:238ca4fdef8c 114 _spi->write(HIGH(startAdr));
hlipka 0:238ca4fdef8c 115 _spi->write(LOW(startAdr));
hlipka 0:238ca4fdef8c 116 }
hlipka 0:238ca4fdef8c 117
hlipka 0:238ca4fdef8c 118 // do real write
hlipka 0:238ca4fdef8c 119 for (int i=0;i<len;i++) {
hlipka 0:238ca4fdef8c 120 _spi->write(data[i]);
hlipka 0:238ca4fdef8c 121 }
hlipka 0:238ca4fdef8c 122 wait_us(1);
hlipka 0:238ca4fdef8c 123 // disable to start physical write
hlipka 0:238ca4fdef8c 124 _enable->write(1);
hlipka 0:238ca4fdef8c 125
hlipka 0:238ca4fdef8c 126 waitForWrite();
hlipka 0:238ca4fdef8c 127
hlipka 0:238ca4fdef8c 128 return true;
hlipka 0:238ca4fdef8c 129 }
hlipka 0:238ca4fdef8c 130
hlipka 0:238ca4fdef8c 131 bool Ser25LCxxx::clearPage(unsigned int pageNum) {
hlipka 0:238ca4fdef8c 132 enableWrite();
hlipka 0:238ca4fdef8c 133 if (_size<65535) {
hlipka 0:238ca4fdef8c 134 char* s=(char*)malloc(_pageSize);
hlipka 0:238ca4fdef8c 135 for (int i=0;i<_pageSize;i++) {
hlipka 0:238ca4fdef8c 136 s[i]=0xff;
hlipka 0:238ca4fdef8c 137 }
hlipka 0:238ca4fdef8c 138 bool b=writePage(_pageSize*pageNum,_pageSize,s);
hlipka 0:238ca4fdef8c 139 delete s;
hlipka 0:238ca4fdef8c 140 return b;
hlipka 2:3a3404dbd3eb 141 } else {
hlipka 0:238ca4fdef8c 142 _enable->write(0);
hlipka 0:238ca4fdef8c 143 wait_us(1);
hlipka 0:238ca4fdef8c 144 _spi->write(0x42);
hlipka 0:238ca4fdef8c 145 _spi->write(HIGH(_pageSize*pageNum));
hlipka 0:238ca4fdef8c 146 _spi->write(LOW(_pageSize*pageNum));
hlipka 0:238ca4fdef8c 147 wait_us(1);
hlipka 0:238ca4fdef8c 148 _enable->write(1);
hlipka 0:238ca4fdef8c 149
hlipka 0:238ca4fdef8c 150 waitForWrite();
hlipka 0:238ca4fdef8c 151 }
hlipka 0:238ca4fdef8c 152 return true;
hlipka 0:238ca4fdef8c 153 }
hlipka 0:238ca4fdef8c 154
hlipka 0:238ca4fdef8c 155 void Ser25LCxxx::clearMem() {
hlipka 0:238ca4fdef8c 156 enableWrite();
hlipka 0:238ca4fdef8c 157 if (_size<65535) {
hlipka 0:238ca4fdef8c 158 for (int i=0;i<_size/_pageSize;i++) {
hlipka 0:238ca4fdef8c 159 if (!clearPage(i))
hlipka 0:238ca4fdef8c 160 break;
hlipka 0:238ca4fdef8c 161 }
hlipka 0:238ca4fdef8c 162 }
hlipka 0:238ca4fdef8c 163 else
hlipka 0:238ca4fdef8c 164 {
hlipka 0:238ca4fdef8c 165 _enable->write(0);
hlipka 0:238ca4fdef8c 166 wait_us(1);
hlipka 0:238ca4fdef8c 167 _spi->write(0xc7);
hlipka 0:238ca4fdef8c 168 wait_us(1);
hlipka 0:238ca4fdef8c 169 _enable->write(1);
hlipka 0:238ca4fdef8c 170
hlipka 0:238ca4fdef8c 171 waitForWrite();
hlipka 0:238ca4fdef8c 172 }
hlipka 0:238ca4fdef8c 173 }
hlipka 0:238ca4fdef8c 174
hlipka 0:238ca4fdef8c 175 int Ser25LCxxx::readStatus() {
hlipka 0:238ca4fdef8c 176 _enable->write(0);
hlipka 0:238ca4fdef8c 177 wait_us(1);
hlipka 0:238ca4fdef8c 178 _spi->write(0x5);
hlipka 0:238ca4fdef8c 179 int status=_spi->write(0x00);
hlipka 0:238ca4fdef8c 180 wait_us(1);
hlipka 0:238ca4fdef8c 181 _enable->write(1);
hlipka 0:238ca4fdef8c 182 return status;
hlipka 0:238ca4fdef8c 183 }
hlipka 0:238ca4fdef8c 184
hlipka 0:238ca4fdef8c 185 void Ser25LCxxx::waitForWrite() {
hlipka 0:238ca4fdef8c 186 while (true) {
hlipka 0:238ca4fdef8c 187 if (0==readStatus()&1)
hlipka 0:238ca4fdef8c 188 break;
hlipka 0:238ca4fdef8c 189 wait_us(10);
hlipka 0:238ca4fdef8c 190 }
hlipka 0:238ca4fdef8c 191 }
hlipka 0:238ca4fdef8c 192
hlipka 0:238ca4fdef8c 193 void Ser25LCxxx::enableWrite()
hlipka 0:238ca4fdef8c 194 {
hlipka 0:238ca4fdef8c 195 _enable->write(0);
hlipka 0:238ca4fdef8c 196 wait_us(1);
hlipka 0:238ca4fdef8c 197 _spi->write(0x06);
hlipka 0:238ca4fdef8c 198 wait_us(1);
hlipka 0:238ca4fdef8c 199 _enable->write(1);
hlipka 0:238ca4fdef8c 200 }