iSDIO Library for TOSHIBA FlashAir. include HTTP or HTTPS Client.
Dependents: FlashAir_Twitter Neon_F303K8_04
Fork of HTTPClient by
iSDIO.cpp@20:51abf34bcc06, 2014-12-15 (annotated)
- Committer:
- ban4jp
- Date:
- Mon Dec 15 12:23:22 2014 +0000
- Revision:
- 20:51abf34bcc06
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ban4jp | 20:51abf34bcc06 | 1 | #include "mbed.h" |
ban4jp | 20:51abf34bcc06 | 2 | #include "iSDIO.h" |
ban4jp | 20:51abf34bcc06 | 3 | |
ban4jp | 20:51abf34bcc06 | 4 | #define CMD48 0x30 |
ban4jp | 20:51abf34bcc06 | 5 | #define CMD49 0x31 |
ban4jp | 20:51abf34bcc06 | 6 | |
ban4jp | 20:51abf34bcc06 | 7 | SD_iSDIO::SD_iSDIO(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) : |
ban4jp | 20:51abf34bcc06 | 8 | SDFileSystem(mosi, miso, sclk, cs, name) |
ban4jp | 20:51abf34bcc06 | 9 | { |
ban4jp | 20:51abf34bcc06 | 10 | //disk_initialize(); |
ban4jp | 20:51abf34bcc06 | 11 | instance = this; |
ban4jp | 20:51abf34bcc06 | 12 | sequenceId = 0; |
ban4jp | 20:51abf34bcc06 | 13 | } |
ban4jp | 20:51abf34bcc06 | 14 | |
ban4jp | 20:51abf34bcc06 | 15 | SD_iSDIO* SD_iSDIO::instance = 0; |
ban4jp | 20:51abf34bcc06 | 16 | |
ban4jp | 20:51abf34bcc06 | 17 | SD_iSDIO* SD_iSDIO::getInstance() |
ban4jp | 20:51abf34bcc06 | 18 | { |
ban4jp | 20:51abf34bcc06 | 19 | return SD_iSDIO::instance; |
ban4jp | 20:51abf34bcc06 | 20 | } |
ban4jp | 20:51abf34bcc06 | 21 | |
ban4jp | 20:51abf34bcc06 | 22 | uint32_t SD_iSDIO::getSequenceId() |
ban4jp | 20:51abf34bcc06 | 23 | { |
ban4jp | 20:51abf34bcc06 | 24 | return sequenceId++; |
ban4jp | 20:51abf34bcc06 | 25 | } |
ban4jp | 20:51abf34bcc06 | 26 | |
ban4jp | 20:51abf34bcc06 | 27 | uint8_t SD_iSDIO::readExtDataPort(uint8_t mio, uint8_t func, uint16_t addr, uint8_t* dst) |
ban4jp | 20:51abf34bcc06 | 28 | { |
ban4jp | 20:51abf34bcc06 | 29 | uint32_t arg = |
ban4jp | 20:51abf34bcc06 | 30 | (((uint32_t)mio & 0x1) << 31) | |
ban4jp | 20:51abf34bcc06 | 31 | (mio ? (((uint32_t)func & 0x7) << 28) : (((uint32_t)func & 0xF) << 27)) | |
ban4jp | 20:51abf34bcc06 | 32 | (((uint32_t)addr & 0x1FE00) << 9); |
ban4jp | 20:51abf34bcc06 | 33 | return readExt(arg, dst, 512); |
ban4jp | 20:51abf34bcc06 | 34 | } |
ban4jp | 20:51abf34bcc06 | 35 | |
ban4jp | 20:51abf34bcc06 | 36 | uint8_t SD_iSDIO::readExtMemory(uint8_t mio, uint8_t func, uint32_t addr, uint16_t count, uint8_t* dst) |
ban4jp | 20:51abf34bcc06 | 37 | { |
ban4jp | 20:51abf34bcc06 | 38 | uint32_t offset = addr & 0x1FF; |
ban4jp | 20:51abf34bcc06 | 39 | if (offset + count > 512) count = 512 - offset; |
ban4jp | 20:51abf34bcc06 | 40 | if (count == 0) return true; |
ban4jp | 20:51abf34bcc06 | 41 | uint32_t arg = |
ban4jp | 20:51abf34bcc06 | 42 | (((uint32_t)mio & 0x1) << 31) | |
ban4jp | 20:51abf34bcc06 | 43 | (mio ? (((uint32_t)func & 0x7) << 28) : (((uint32_t)func & 0xF) << 27)) | |
ban4jp | 20:51abf34bcc06 | 44 | ((addr & 0x1FFFF) << 9) | |
ban4jp | 20:51abf34bcc06 | 45 | ((count - 1) & 0x1FF); |
ban4jp | 20:51abf34bcc06 | 46 | return readExt(arg, dst, count); |
ban4jp | 20:51abf34bcc06 | 47 | } |
ban4jp | 20:51abf34bcc06 | 48 | |
ban4jp | 20:51abf34bcc06 | 49 | uint8_t SD_iSDIO::writeExtDataPort(uint8_t mio, uint8_t func, uint16_t addr, const uint8_t* src) |
ban4jp | 20:51abf34bcc06 | 50 | { |
ban4jp | 20:51abf34bcc06 | 51 | uint32_t arg = |
ban4jp | 20:51abf34bcc06 | 52 | (((uint32_t)mio & 0x1) << 31) | |
ban4jp | 20:51abf34bcc06 | 53 | (mio ? (((uint32_t)func & 0x7) << 28) : (((uint32_t)func & 0xF) << 27)) | |
ban4jp | 20:51abf34bcc06 | 54 | (((uint32_t)addr & 0x1FE00) << 9); |
ban4jp | 20:51abf34bcc06 | 55 | return writeExt(arg, src, 512); |
ban4jp | 20:51abf34bcc06 | 56 | } |
ban4jp | 20:51abf34bcc06 | 57 | |
ban4jp | 20:51abf34bcc06 | 58 | uint8_t SD_iSDIO::writeExtMemory(uint8_t mio, uint8_t func, uint32_t addr, uint16_t count, const uint8_t* src) |
ban4jp | 20:51abf34bcc06 | 59 | { |
ban4jp | 20:51abf34bcc06 | 60 | uint32_t arg = |
ban4jp | 20:51abf34bcc06 | 61 | (((uint32_t)mio & 0x1) << 31) | |
ban4jp | 20:51abf34bcc06 | 62 | (mio ? (((uint32_t)func & 0x7) << 28) : (((uint32_t)func & 0xF) << 27)) | |
ban4jp | 20:51abf34bcc06 | 63 | ((addr & 0x1FFFF) << 9) | |
ban4jp | 20:51abf34bcc06 | 64 | ((count - 1) & 0x1FF); |
ban4jp | 20:51abf34bcc06 | 65 | return writeExt(arg, src, count); |
ban4jp | 20:51abf34bcc06 | 66 | } |
ban4jp | 20:51abf34bcc06 | 67 | |
ban4jp | 20:51abf34bcc06 | 68 | uint8_t SD_iSDIO::writeExtMask(uint8_t mio, uint8_t func, uint32_t addr, uint8_t mask, const uint8_t* src) |
ban4jp | 20:51abf34bcc06 | 69 | { |
ban4jp | 20:51abf34bcc06 | 70 | uint32_t arg = |
ban4jp | 20:51abf34bcc06 | 71 | (((uint32_t)mio & 0x1) << 31) | |
ban4jp | 20:51abf34bcc06 | 72 | (mio ? (((uint32_t)func & 0x7) << 28) : (((uint32_t)func & 0xF) << 27)) | |
ban4jp | 20:51abf34bcc06 | 73 | (0x1 << 26) | |
ban4jp | 20:51abf34bcc06 | 74 | ((addr & 0x1FFFF) << 9) | |
ban4jp | 20:51abf34bcc06 | 75 | mask; |
ban4jp | 20:51abf34bcc06 | 76 | return writeExt(arg, src, 1); |
ban4jp | 20:51abf34bcc06 | 77 | } |
ban4jp | 20:51abf34bcc06 | 78 | |
ban4jp | 20:51abf34bcc06 | 79 | uint8_t SD_iSDIO::waitResponse(uint32_t sequenceId) |
ban4jp | 20:51abf34bcc06 | 80 | { |
ban4jp | 20:51abf34bcc06 | 81 | uint8_t buffer[0x14]; |
ban4jp | 20:51abf34bcc06 | 82 | |
ban4jp | 20:51abf34bcc06 | 83 | //printf("\nWaiting response "); |
ban4jp | 20:51abf34bcc06 | 84 | uint8_t prev = 0xFF; |
ban4jp | 20:51abf34bcc06 | 85 | for (int i = 0; i < 20; ++i) { |
ban4jp | 20:51abf34bcc06 | 86 | memset(buffer, 0, 0x14); |
ban4jp | 20:51abf34bcc06 | 87 | // Read command response status. |
ban4jp | 20:51abf34bcc06 | 88 | if (!readExtMemory(1, 1, 0x440, 0x14, buffer)) { |
ban4jp | 20:51abf34bcc06 | 89 | return false; |
ban4jp | 20:51abf34bcc06 | 90 | } |
ban4jp | 20:51abf34bcc06 | 91 | uint8_t resp = get_u8(buffer + 8); |
ban4jp | 20:51abf34bcc06 | 92 | if (sequenceId == get_u32(buffer + 4)) { |
ban4jp | 20:51abf34bcc06 | 93 | if (prev != resp) { |
ban4jp | 20:51abf34bcc06 | 94 | switch (resp) { |
ban4jp | 20:51abf34bcc06 | 95 | case 0x00: |
ban4jp | 20:51abf34bcc06 | 96 | //printf("\n Initial"); |
ban4jp | 20:51abf34bcc06 | 97 | break; |
ban4jp | 20:51abf34bcc06 | 98 | case 0x01: |
ban4jp | 20:51abf34bcc06 | 99 | //printf("\n Command Processing"); |
ban4jp | 20:51abf34bcc06 | 100 | break; |
ban4jp | 20:51abf34bcc06 | 101 | case 0x02: |
ban4jp | 20:51abf34bcc06 | 102 | //printf("\n Command Rejected"); |
ban4jp | 20:51abf34bcc06 | 103 | return false; |
ban4jp | 20:51abf34bcc06 | 104 | case 0x03: |
ban4jp | 20:51abf34bcc06 | 105 | //printf("\n Process Succeeded"); |
ban4jp | 20:51abf34bcc06 | 106 | return true; |
ban4jp | 20:51abf34bcc06 | 107 | case 0x04: |
ban4jp | 20:51abf34bcc06 | 108 | //printf("\n Process Terminated"); |
ban4jp | 20:51abf34bcc06 | 109 | return false; |
ban4jp | 20:51abf34bcc06 | 110 | default: |
ban4jp | 20:51abf34bcc06 | 111 | //printf("\n Process Failed "); |
ban4jp | 20:51abf34bcc06 | 112 | //printf("%x", resp); |
ban4jp | 20:51abf34bcc06 | 113 | return false; |
ban4jp | 20:51abf34bcc06 | 114 | } |
ban4jp | 20:51abf34bcc06 | 115 | prev = resp; |
ban4jp | 20:51abf34bcc06 | 116 | } |
ban4jp | 20:51abf34bcc06 | 117 | } |
ban4jp | 20:51abf34bcc06 | 118 | //printf("."); |
ban4jp | 20:51abf34bcc06 | 119 | wait_ms(1000); |
ban4jp | 20:51abf34bcc06 | 120 | } |
ban4jp | 20:51abf34bcc06 | 121 | return false; |
ban4jp | 20:51abf34bcc06 | 122 | } |
ban4jp | 20:51abf34bcc06 | 123 | |
ban4jp | 20:51abf34bcc06 | 124 | /** Perform Extention Read. */ |
ban4jp | 20:51abf34bcc06 | 125 | uint8_t SD_iSDIO::readExt(uint32_t arg, uint8_t* dst, uint16_t count) |
ban4jp | 20:51abf34bcc06 | 126 | { |
ban4jp | 20:51abf34bcc06 | 127 | uint16_t i; |
ban4jp | 20:51abf34bcc06 | 128 | // send command and argument. |
ban4jp | 20:51abf34bcc06 | 129 | if (_cmd(CMD48, arg)) { |
ban4jp | 20:51abf34bcc06 | 130 | error("SD_CARD_ERROR_CMD48"); |
ban4jp | 20:51abf34bcc06 | 131 | _cs = 1; |
ban4jp | 20:51abf34bcc06 | 132 | return false; |
ban4jp | 20:51abf34bcc06 | 133 | } |
ban4jp | 20:51abf34bcc06 | 134 | |
ban4jp | 20:51abf34bcc06 | 135 | _cs = 0; |
ban4jp | 20:51abf34bcc06 | 136 | |
ban4jp | 20:51abf34bcc06 | 137 | /* |
ban4jp | 20:51abf34bcc06 | 138 | // wait for start block token. |
ban4jp | 20:51abf34bcc06 | 139 | if (!waitStartBlock()) { |
ban4jp | 20:51abf34bcc06 | 140 | _cs = 1; |
ban4jp | 20:51abf34bcc06 | 141 | return false; |
ban4jp | 20:51abf34bcc06 | 142 | } |
ban4jp | 20:51abf34bcc06 | 143 | */ |
ban4jp | 20:51abf34bcc06 | 144 | while (_spi.write(0xFF) == 0xFF); |
ban4jp | 20:51abf34bcc06 | 145 | |
ban4jp | 20:51abf34bcc06 | 146 | // receive data |
ban4jp | 20:51abf34bcc06 | 147 | for (i = 0; i < count; ++i) { |
ban4jp | 20:51abf34bcc06 | 148 | dst[i] = _spi.write(0xFF); |
ban4jp | 20:51abf34bcc06 | 149 | } |
ban4jp | 20:51abf34bcc06 | 150 | // skip dummy bytes and 16-bit crc. |
ban4jp | 20:51abf34bcc06 | 151 | for (; i < 514; ++i) { |
ban4jp | 20:51abf34bcc06 | 152 | _spi.write(0xFF); |
ban4jp | 20:51abf34bcc06 | 153 | } |
ban4jp | 20:51abf34bcc06 | 154 | _cs = 1; |
ban4jp | 20:51abf34bcc06 | 155 | _spi.write(0xFF); // dummy clock to force FlashAir finish the command. |
ban4jp | 20:51abf34bcc06 | 156 | return true; |
ban4jp | 20:51abf34bcc06 | 157 | } |
ban4jp | 20:51abf34bcc06 | 158 | |
ban4jp | 20:51abf34bcc06 | 159 | /** Perform Extention Write. */ |
ban4jp | 20:51abf34bcc06 | 160 | uint8_t SD_iSDIO::writeExt(uint32_t arg, const uint8_t* src, uint16_t count) |
ban4jp | 20:51abf34bcc06 | 161 | { |
ban4jp | 20:51abf34bcc06 | 162 | uint16_t i; |
ban4jp | 20:51abf34bcc06 | 163 | uint8_t status; |
ban4jp | 20:51abf34bcc06 | 164 | // send command and argument. |
ban4jp | 20:51abf34bcc06 | 165 | if (_cmd(CMD49, arg)) { |
ban4jp | 20:51abf34bcc06 | 166 | error("SD_CARD_ERROR_CMD49"); |
ban4jp | 20:51abf34bcc06 | 167 | _cs = 1; |
ban4jp | 20:51abf34bcc06 | 168 | return false; |
ban4jp | 20:51abf34bcc06 | 169 | } |
ban4jp | 20:51abf34bcc06 | 170 | |
ban4jp | 20:51abf34bcc06 | 171 | _cs = 0; |
ban4jp | 20:51abf34bcc06 | 172 | |
ban4jp | 20:51abf34bcc06 | 173 | // send start block token. |
ban4jp | 20:51abf34bcc06 | 174 | //_spi.write(DATA_START_BLOCK); |
ban4jp | 20:51abf34bcc06 | 175 | _spi.write(0xFE); |
ban4jp | 20:51abf34bcc06 | 176 | // send data |
ban4jp | 20:51abf34bcc06 | 177 | for (i = 0; i < count; ++i) { |
ban4jp | 20:51abf34bcc06 | 178 | _spi.write(src[i]); |
ban4jp | 20:51abf34bcc06 | 179 | } |
ban4jp | 20:51abf34bcc06 | 180 | // send dummy bytes until 512 bytes. |
ban4jp | 20:51abf34bcc06 | 181 | for (; i < 512; ++i) { |
ban4jp | 20:51abf34bcc06 | 182 | _spi.write(0xFF); |
ban4jp | 20:51abf34bcc06 | 183 | } |
ban4jp | 20:51abf34bcc06 | 184 | // dummy 16-bit crc |
ban4jp | 20:51abf34bcc06 | 185 | _spi.write(0xFF); |
ban4jp | 20:51abf34bcc06 | 186 | _spi.write(0xFF); |
ban4jp | 20:51abf34bcc06 | 187 | // wait a data response token |
ban4jp | 20:51abf34bcc06 | 188 | status = _spi.write(0xFF); |
ban4jp | 20:51abf34bcc06 | 189 | //if ((status & DATA_RES_MASK) != DATA_RES_ACCEPTED) { |
ban4jp | 20:51abf34bcc06 | 190 | if ((status & 0x1F) != 0x05) { |
ban4jp | 20:51abf34bcc06 | 191 | error("SD_CARD_ERROR_WRITE"); |
ban4jp | 20:51abf34bcc06 | 192 | _cs = 1; |
ban4jp | 20:51abf34bcc06 | 193 | return false; |
ban4jp | 20:51abf34bcc06 | 194 | } |
ban4jp | 20:51abf34bcc06 | 195 | // wait for flash programming to complete |
ban4jp | 20:51abf34bcc06 | 196 | /* |
ban4jp | 20:51abf34bcc06 | 197 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) { |
ban4jp | 20:51abf34bcc06 | 198 | error("SD_CARD_ERROR_WRITE_TIMEOUT"); |
ban4jp | 20:51abf34bcc06 | 199 | _cs = 1; |
ban4jp | 20:51abf34bcc06 | 200 | return false; |
ban4jp | 20:51abf34bcc06 | 201 | } |
ban4jp | 20:51abf34bcc06 | 202 | */ |
ban4jp | 20:51abf34bcc06 | 203 | while (_spi.write(0xFF) == 0); |
ban4jp | 20:51abf34bcc06 | 204 | |
ban4jp | 20:51abf34bcc06 | 205 | _cs = 1; |
ban4jp | 20:51abf34bcc06 | 206 | return true; |
ban4jp | 20:51abf34bcc06 | 207 | } |