Jonne Valola / PokittoLib Featured

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Wed Jun 27 13:14:54 2018 +0000
Revision:
50:ca94812a17b6
Parent:
0:e8b8f36b4505
Lockup at startup due to missing EEPROM values fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 0:e8b8f36b4505 1 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 2 /* PFF - Low level disk control module for ATtiny85 (C)ChaN, 2009 */
Pokitto 0:e8b8f36b4505 3 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 4
Pokitto 0:e8b8f36b4505 5 #define _WRITE_FUNC 1
Pokitto 0:e8b8f36b4505 6
Pokitto 0:e8b8f36b4505 7 #include "diskio.h"
Pokitto 0:e8b8f36b4505 8 #include "mbed.h"
Pokitto 0:e8b8f36b4505 9 #include "connect.h"
Pokitto 0:e8b8f36b4505 10 #include "PokittoDisk.h"
Pokitto 0:e8b8f36b4505 11
Pokitto 0:e8b8f36b4505 12 /* Definitions for MMC/SDC command */
Pokitto 0:e8b8f36b4505 13 #define CMD0 (0x40+0) /* GO_IDLE_STATE */
Pokitto 0:e8b8f36b4505 14 #define CMD1 (0x40+1) /* SEND_OP_COND (MMC) */
Pokitto 0:e8b8f36b4505 15 #define ACMD41 (0xC0+41) /* SEND_OP_COND (SDC) */
Pokitto 0:e8b8f36b4505 16 #define CMD8 (0x40+8) /* SEND_IF_COND */
Pokitto 0:e8b8f36b4505 17 #define CMD16 (0x40+16) /* SET_BLOCKLEN */
Pokitto 0:e8b8f36b4505 18 #define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */
Pokitto 0:e8b8f36b4505 19 #define CMD24 (0x40+24) /* WRITE_BLOCK */
Pokitto 0:e8b8f36b4505 20 #define CMD55 (0x40+55) /* APP_CMD */
Pokitto 0:e8b8f36b4505 21 #define CMD58 (0x40+58) /* READ_OCR */
Pokitto 0:e8b8f36b4505 22
Pokitto 0:e8b8f36b4505 23
Pokitto 0:e8b8f36b4505 24
Pokitto 0:e8b8f36b4505 25 /* Port Controls (Platform dependent) */
Pokitto 0:e8b8f36b4505 26 #define SELECT() CLR_SD_CS //mmccs.write(0) /* MMC CS = L */
Pokitto 0:e8b8f36b4505 27 #define DESELECT() SET_SD_CS // mmccs.write(1) /* MMC CS = H */
Pokitto 0:e8b8f36b4505 28 #define MMC_SEL !GET_SD_CS // !mmccs.read() /* MMC CS status (true:selected) */
Pokitto 0:e8b8f36b4505 29
Pokitto 0:e8b8f36b4505 30 void xmit_spi (BYTE c) {
Pokitto 0:e8b8f36b4505 31 device.write(c); /* Send a byte */
Pokitto 0:e8b8f36b4505 32 }
Pokitto 0:e8b8f36b4505 33
Pokitto 0:e8b8f36b4505 34 BYTE rcv_spi (void) {
Pokitto 0:e8b8f36b4505 35 return device.write(0xff);
Pokitto 0:e8b8f36b4505 36 }
Pokitto 0:e8b8f36b4505 37
Pokitto 0:e8b8f36b4505 38 /*--------------------------------------------------------------------------
Pokitto 0:e8b8f36b4505 39
Pokitto 0:e8b8f36b4505 40 Module Private Functions
Pokitto 0:e8b8f36b4505 41
Pokitto 0:e8b8f36b4505 42 ---------------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 43
Pokitto 0:e8b8f36b4505 44 static
Pokitto 0:e8b8f36b4505 45 BYTE CardType;
Pokitto 0:e8b8f36b4505 46
Pokitto 0:e8b8f36b4505 47
Pokitto 0:e8b8f36b4505 48 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 49 /* Deselect the card and release SPI bus */
Pokitto 0:e8b8f36b4505 50 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 51
Pokitto 0:e8b8f36b4505 52 static
Pokitto 0:e8b8f36b4505 53 void release_spi (void) {
Pokitto 0:e8b8f36b4505 54 DESELECT();
Pokitto 0:e8b8f36b4505 55 rcv_spi();
Pokitto 0:e8b8f36b4505 56 }
Pokitto 0:e8b8f36b4505 57
Pokitto 0:e8b8f36b4505 58
Pokitto 0:e8b8f36b4505 59 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 60 /* Send a command packet to MMC */
Pokitto 0:e8b8f36b4505 61 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 62
Pokitto 0:e8b8f36b4505 63 static
Pokitto 0:e8b8f36b4505 64 BYTE send_cmd (
Pokitto 0:e8b8f36b4505 65 BYTE cmd, /* Command byte */
Pokitto 0:e8b8f36b4505 66 DWORD arg /* Argument */
Pokitto 0:e8b8f36b4505 67 ) {
Pokitto 0:e8b8f36b4505 68 BYTE n, res;
Pokitto 0:e8b8f36b4505 69
Pokitto 0:e8b8f36b4505 70
Pokitto 0:e8b8f36b4505 71 if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
Pokitto 0:e8b8f36b4505 72 cmd &= 0x7F;
Pokitto 0:e8b8f36b4505 73 res = send_cmd(CMD55, 0);
Pokitto 0:e8b8f36b4505 74 if (res > 1) return res;
Pokitto 0:e8b8f36b4505 75 }
Pokitto 0:e8b8f36b4505 76
Pokitto 0:e8b8f36b4505 77 /* Select the card */
Pokitto 0:e8b8f36b4505 78 DESELECT();
Pokitto 0:e8b8f36b4505 79 rcv_spi();
Pokitto 0:e8b8f36b4505 80 SELECT();
Pokitto 0:e8b8f36b4505 81 rcv_spi();
Pokitto 0:e8b8f36b4505 82
Pokitto 0:e8b8f36b4505 83 /* Send a command packet */
Pokitto 0:e8b8f36b4505 84 xmit_spi(cmd); /* Start + Command index */
Pokitto 0:e8b8f36b4505 85 xmit_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
Pokitto 0:e8b8f36b4505 86 xmit_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
Pokitto 0:e8b8f36b4505 87 xmit_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
Pokitto 0:e8b8f36b4505 88 xmit_spi((BYTE)arg); /* Argument[7..0] */
Pokitto 0:e8b8f36b4505 89 n = 0x01; /* Dummy CRC + Stop */
Pokitto 0:e8b8f36b4505 90 if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
Pokitto 0:e8b8f36b4505 91 if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
Pokitto 0:e8b8f36b4505 92 xmit_spi(n);
Pokitto 0:e8b8f36b4505 93
Pokitto 0:e8b8f36b4505 94 /* Receive a command response */
Pokitto 0:e8b8f36b4505 95 n = 10; /* Wait for a valid response in timeout of 10 attempts */
Pokitto 0:e8b8f36b4505 96 do {
Pokitto 0:e8b8f36b4505 97 res = rcv_spi();
Pokitto 0:e8b8f36b4505 98 } while ((res & 0x80) && --n);
Pokitto 0:e8b8f36b4505 99
Pokitto 0:e8b8f36b4505 100 return res; /* Return with the response value */
Pokitto 0:e8b8f36b4505 101 }
Pokitto 0:e8b8f36b4505 102
Pokitto 0:e8b8f36b4505 103
Pokitto 0:e8b8f36b4505 104
Pokitto 0:e8b8f36b4505 105 /*--------------------------------------------------------------------------
Pokitto 0:e8b8f36b4505 106
Pokitto 0:e8b8f36b4505 107 Public Functions
Pokitto 0:e8b8f36b4505 108
Pokitto 0:e8b8f36b4505 109 ---------------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 110
Pokitto 0:e8b8f36b4505 111
Pokitto 0:e8b8f36b4505 112 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 113 /* Initialize Disk Drive */
Pokitto 0:e8b8f36b4505 114 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 115
Pokitto 0:e8b8f36b4505 116 //__attribute__((section(".SD_Code")))
Pokitto 0:e8b8f36b4505 117 DSTATUS disk_initialize (void) {
Pokitto 0:e8b8f36b4505 118 BYTE n, cmd, ty, ocr[4];
Pokitto 0:e8b8f36b4505 119 WORD tmr;
Pokitto 0:e8b8f36b4505 120
Pokitto 0:e8b8f36b4505 121 DESELECT();
Pokitto 0:e8b8f36b4505 122 device.frequency(SPI_FREQ);
Pokitto 0:e8b8f36b4505 123 device.format(8);
Pokitto 0:e8b8f36b4505 124 // device.frequency(SPI_FREQ); // hangs if this is after format
Pokitto 0:e8b8f36b4505 125
Pokitto 0:e8b8f36b4505 126 #if _WRITE_FUNC
Pokitto 0:e8b8f36b4505 127 if (MMC_SEL) disk_writep(0, 0); /* Finalize write process if it is in progress */
Pokitto 0:e8b8f36b4505 128 #endif
Pokitto 0:e8b8f36b4505 129 for (n = 100; n; n--) rcv_spi(); /* Dummy clocks */
Pokitto 0:e8b8f36b4505 130
Pokitto 0:e8b8f36b4505 131 ty = 0;
Pokitto 0:e8b8f36b4505 132 if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
Pokitto 0:e8b8f36b4505 133 if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2 */
Pokitto 0:e8b8f36b4505 134 for (n = 0; n < 4; n++) ocr[n] = rcv_spi(); /* Get trailing return value of R7 resp */
Pokitto 0:e8b8f36b4505 135 if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
Pokitto 0:e8b8f36b4505 136 for (tmr = 12000; tmr && send_cmd(ACMD41, 1UL << 30); tmr--) ; /* Wait for leaving idle state (ACMD41 with HCS bit) */
Pokitto 0:e8b8f36b4505 137 if (tmr && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
Pokitto 0:e8b8f36b4505 138 for (n = 0; n < 4; n++) ocr[n] = rcv_spi();
Pokitto 0:e8b8f36b4505 139 ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 (HC or SC) */
Pokitto 0:e8b8f36b4505 140 }
Pokitto 0:e8b8f36b4505 141 }
Pokitto 0:e8b8f36b4505 142 } else { /* SDv1 or MMCv3 */
Pokitto 0:e8b8f36b4505 143 if (send_cmd(ACMD41, 0) <= 1) {
Pokitto 0:e8b8f36b4505 144 ty = CT_SD1;
Pokitto 0:e8b8f36b4505 145 cmd = ACMD41; /* SDv1 */
Pokitto 0:e8b8f36b4505 146 } else {
Pokitto 0:e8b8f36b4505 147 ty = CT_MMC;
Pokitto 0:e8b8f36b4505 148 cmd = CMD1; /* MMCv3 */
Pokitto 0:e8b8f36b4505 149 }
Pokitto 0:e8b8f36b4505 150 for (tmr = 25000; tmr && send_cmd(cmd, 0); tmr--) ; /* Wait for leaving idle state */
Pokitto 0:e8b8f36b4505 151 if (!tmr || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
Pokitto 0:e8b8f36b4505 152 ty = 0;
Pokitto 0:e8b8f36b4505 153 }
Pokitto 0:e8b8f36b4505 154 }
Pokitto 0:e8b8f36b4505 155 CardType = ty;
Pokitto 0:e8b8f36b4505 156 release_spi();
Pokitto 0:e8b8f36b4505 157
Pokitto 0:e8b8f36b4505 158 return ty ? 0 : STA_NOINIT;
Pokitto 0:e8b8f36b4505 159 }
Pokitto 0:e8b8f36b4505 160
Pokitto 0:e8b8f36b4505 161
Pokitto 0:e8b8f36b4505 162
Pokitto 0:e8b8f36b4505 163 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 164 /* Read partial sector */
Pokitto 0:e8b8f36b4505 165 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 166
Pokitto 0:e8b8f36b4505 167 DRESULT disk_readp (
Pokitto 0:e8b8f36b4505 168 BYTE *buff, /* Pointer to the read buffer (NULL:Read bytes are forwarded to the stream) */
Pokitto 0:e8b8f36b4505 169 DWORD lba, /* Sector number (LBA) */
Pokitto 0:e8b8f36b4505 170 WORD ofs, /* Byte offset to read from (0..511) */
Pokitto 0:e8b8f36b4505 171 WORD cnt /* Number of bytes to read (ofs + cnt mus be <= 512) */
Pokitto 0:e8b8f36b4505 172 ) {
Pokitto 0:e8b8f36b4505 173 DRESULT res;
Pokitto 0:e8b8f36b4505 174 BYTE rc;
Pokitto 0:e8b8f36b4505 175 WORD bc;
Pokitto 0:e8b8f36b4505 176
Pokitto 0:e8b8f36b4505 177
Pokitto 0:e8b8f36b4505 178 if (!(CardType & CT_BLOCK)) lba *= 512; /* Convert to byte address if needed */
Pokitto 0:e8b8f36b4505 179
Pokitto 0:e8b8f36b4505 180 res = RES_ERROR;
Pokitto 0:e8b8f36b4505 181 if (send_cmd(CMD17, lba) == 0) { /* READ_SINGLE_BLOCK */
Pokitto 0:e8b8f36b4505 182
Pokitto 0:e8b8f36b4505 183 bc = 30000;
Pokitto 0:e8b8f36b4505 184 do { /* Wait for data packet in timeout of 100ms */
Pokitto 0:e8b8f36b4505 185 rc = rcv_spi();
Pokitto 0:e8b8f36b4505 186 } while (rc == 0xFF && --bc);
Pokitto 0:e8b8f36b4505 187
Pokitto 0:e8b8f36b4505 188 if (rc == 0xFE) { /* A data packet arrived */
Pokitto 0:e8b8f36b4505 189 bc = 514 - ofs - cnt;
Pokitto 0:e8b8f36b4505 190
Pokitto 0:e8b8f36b4505 191 /* Skip leading bytes */
Pokitto 0:e8b8f36b4505 192 if (ofs) {
Pokitto 0:e8b8f36b4505 193 do {
Pokitto 0:e8b8f36b4505 194 rcv_spi();
Pokitto 0:e8b8f36b4505 195 } while (--ofs);
Pokitto 0:e8b8f36b4505 196 }
Pokitto 0:e8b8f36b4505 197
Pokitto 0:e8b8f36b4505 198 /* Receive a part of the sector */
Pokitto 0:e8b8f36b4505 199 if (buff) { /* Store data to the memory */
Pokitto 0:e8b8f36b4505 200 do {
Pokitto 0:e8b8f36b4505 201 *buff++ = rcv_spi();
Pokitto 0:e8b8f36b4505 202 } while (--cnt);
Pokitto 0:e8b8f36b4505 203 } else { /* Forward data to the outgoing stream (depends on the project) */
Pokitto 0:e8b8f36b4505 204 do {
Pokitto 0:e8b8f36b4505 205 rcv_spi();
Pokitto 0:e8b8f36b4505 206 } while (--cnt);
Pokitto 0:e8b8f36b4505 207 }
Pokitto 0:e8b8f36b4505 208
Pokitto 0:e8b8f36b4505 209 /* Skip trailing bytes and CRC */
Pokitto 0:e8b8f36b4505 210 do rcv_spi();
Pokitto 0:e8b8f36b4505 211 while (--bc);
Pokitto 0:e8b8f36b4505 212
Pokitto 0:e8b8f36b4505 213 res = RES_OK;
Pokitto 0:e8b8f36b4505 214 }
Pokitto 0:e8b8f36b4505 215 }
Pokitto 0:e8b8f36b4505 216
Pokitto 0:e8b8f36b4505 217 release_spi();
Pokitto 0:e8b8f36b4505 218
Pokitto 0:e8b8f36b4505 219 return res;
Pokitto 0:e8b8f36b4505 220 }
Pokitto 0:e8b8f36b4505 221
Pokitto 0:e8b8f36b4505 222
Pokitto 0:e8b8f36b4505 223
Pokitto 0:e8b8f36b4505 224 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 225 /* Write partial sector */
Pokitto 0:e8b8f36b4505 226 /*-----------------------------------------------------------------------*/
Pokitto 0:e8b8f36b4505 227 #if _WRITE_FUNC
Pokitto 0:e8b8f36b4505 228
Pokitto 0:e8b8f36b4505 229 DRESULT disk_writep (
Pokitto 0:e8b8f36b4505 230 const BYTE *buff, /* Pointer to the bytes to be written (NULL:Initiate/Finalize sector write) */
Pokitto 0:e8b8f36b4505 231 DWORD sa /* Number of bytes to send, Sector number (LBA) or zero */
Pokitto 0:e8b8f36b4505 232 ) {
Pokitto 0:e8b8f36b4505 233 DRESULT res;
Pokitto 0:e8b8f36b4505 234 WORD bc;
Pokitto 0:e8b8f36b4505 235 static WORD wc;
Pokitto 0:e8b8f36b4505 236
Pokitto 0:e8b8f36b4505 237
Pokitto 0:e8b8f36b4505 238 res = RES_ERROR;
Pokitto 0:e8b8f36b4505 239
Pokitto 0:e8b8f36b4505 240 if (buff) { /* Send data bytes */
Pokitto 0:e8b8f36b4505 241 bc = (WORD)sa;
Pokitto 0:e8b8f36b4505 242 while (bc && wc) { /* Send data bytes to the card */
Pokitto 0:e8b8f36b4505 243 xmit_spi(*buff++);
Pokitto 0:e8b8f36b4505 244 wc--;
Pokitto 0:e8b8f36b4505 245 bc--;
Pokitto 0:e8b8f36b4505 246 }
Pokitto 0:e8b8f36b4505 247 res = RES_OK;
Pokitto 0:e8b8f36b4505 248 } else {
Pokitto 0:e8b8f36b4505 249 if (sa) { /* Initiate sector write process */
Pokitto 0:e8b8f36b4505 250 if (!(CardType & CT_BLOCK)) sa *= 512; /* Convert to byte address if needed */
Pokitto 0:e8b8f36b4505 251 if (send_cmd(CMD24, sa) == 0) { /* WRITE_SINGLE_BLOCK */
Pokitto 0:e8b8f36b4505 252 xmit_spi(0xFF);
Pokitto 0:e8b8f36b4505 253 xmit_spi(0xFE); /* Data block header */
Pokitto 0:e8b8f36b4505 254 wc = 512; /* Set byte counter */
Pokitto 0:e8b8f36b4505 255 res = RES_OK;
Pokitto 0:e8b8f36b4505 256 }
Pokitto 0:e8b8f36b4505 257 } else { /* Finalize sector write process */
Pokitto 0:e8b8f36b4505 258 bc = wc + 2;
Pokitto 0:e8b8f36b4505 259 while (bc--) xmit_spi(0); /* Fill left bytes and CRC with zeros */
Pokitto 0:e8b8f36b4505 260 if ((rcv_spi() & 0x1F) == 0x05) { /* Receive data resp and wait for end of write process in timeout of 300ms */
Pokitto 0:e8b8f36b4505 261 for (bc = 65000; rcv_spi() != 0xFF && bc; bc--) ; /* Wait ready */
Pokitto 0:e8b8f36b4505 262 if (bc) res = RES_OK;
Pokitto 0:e8b8f36b4505 263 }
Pokitto 0:e8b8f36b4505 264 release_spi();
Pokitto 0:e8b8f36b4505 265 }
Pokitto 0:e8b8f36b4505 266 }
Pokitto 0:e8b8f36b4505 267
Pokitto 0:e8b8f36b4505 268 return res;
Pokitto 0:e8b8f36b4505 269 }
Pokitto 0:e8b8f36b4505 270 #endif
Pokitto 0:e8b8f36b4505 271