annotate sd_raw.c @ 38:9e888708f33d

sd test code
author Matt Johnston <matt@ucc.asn.au>
date Thu, 27 Jun 2013 11:24:56 +0000
parents 5f9a40d6991b
children 2a47c458d6ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 /*
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * Copyright (c) 2006-2012 by Roland Riegel <[email protected]>
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * This file is free software; you can redistribute it and/or modify
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * it under the terms of either the GNU General Public License version 2
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * or the GNU Lesser General Public License version 2.1, both as
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * published by the Free Software Foundation.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 #include <string.h>
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #include <avr/io.h>
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 #include "sd_raw.h"
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 * \addtogroup sd_raw MMC/SD/SDHC card raw access
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * This module implements read and write access to MMC, SD
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * and SDHC cards. It serves as a low-level driver for the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * higher level modules such as partition and file system
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * access.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * @{
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 * \file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 * MMC/SD/SDHC raw access implementation (license: GPLv2 or LGPLv2.1)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 * \author Roland Riegel
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 * \addtogroup sd_raw_config MMC/SD configuration
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 * Preprocessor defines to configure the MMC/SD support.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 * @}
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 /* commands available in SPI mode */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 /* CMD0: response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #define CMD_GO_IDLE_STATE 0x00
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 /* CMD1: response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 #define CMD_SEND_OP_COND 0x01
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 /* CMD8: response R7 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 #define CMD_SEND_IF_COND 0x08
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 /* CMD9: response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 #define CMD_SEND_CSD 0x09
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 /* CMD10: response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 #define CMD_SEND_CID 0x0a
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 /* CMD12: response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #define CMD_STOP_TRANSMISSION 0x0c
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 /* CMD13: response R2 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 #define CMD_SEND_STATUS 0x0d
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 /* CMD16: arg0[31:0]: block length, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 #define CMD_SET_BLOCKLEN 0x10
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 /* CMD17: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 #define CMD_READ_SINGLE_BLOCK 0x11
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 /* CMD18: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 #define CMD_READ_MULTIPLE_BLOCK 0x12
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 /* CMD24: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 #define CMD_WRITE_SINGLE_BLOCK 0x18
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 /* CMD25: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 #define CMD_WRITE_MULTIPLE_BLOCK 0x19
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 /* CMD27: response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 #define CMD_PROGRAM_CSD 0x1b
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 /* CMD28: arg0[31:0]: data address, response R1b */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #define CMD_SET_WRITE_PROT 0x1c
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 /* CMD29: arg0[31:0]: data address, response R1b */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 #define CMD_CLR_WRITE_PROT 0x1d
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 /* CMD30: arg0[31:0]: write protect data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 #define CMD_SEND_WRITE_PROT 0x1e
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 /* CMD32: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 #define CMD_TAG_SECTOR_START 0x20
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 /* CMD33: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 #define CMD_TAG_SECTOR_END 0x21
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 /* CMD34: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 #define CMD_UNTAG_SECTOR 0x22
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 /* CMD35: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 #define CMD_TAG_ERASE_GROUP_START 0x23
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 /* CMD36: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 #define CMD_TAG_ERASE_GROUP_END 0x24
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 /* CMD37: arg0[31:0]: data address, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 #define CMD_UNTAG_ERASE_GROUP 0x25
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 /* CMD38: arg0[31:0]: stuff bits, response R1b */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 #define CMD_ERASE 0x26
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 /* ACMD41: arg0[31:0]: OCR contents, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 #define CMD_SD_SEND_OP_COND 0x29
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 /* CMD42: arg0[31:0]: stuff bits, response R1b */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 #define CMD_LOCK_UNLOCK 0x2a
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 /* CMD55: arg0[31:0]: stuff bits, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 #define CMD_APP 0x37
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 /* CMD58: arg0[31:0]: stuff bits, response R3 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 #define CMD_READ_OCR 0x3a
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 /* CMD59: arg0[31:1]: stuff bits, arg0[0:0]: crc option, response R1 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 #define CMD_CRC_ON_OFF 0x3b
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 /* command responses */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 /* R1: size 1 byte */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 #define R1_IDLE_STATE 0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 #define R1_ERASE_RESET 1
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 #define R1_ILL_COMMAND 2
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 #define R1_COM_CRC_ERR 3
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 #define R1_ERASE_SEQ_ERR 4
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 #define R1_ADDR_ERR 5
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 #define R1_PARAM_ERR 6
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 /* R1b: equals R1, additional busy bytes */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 /* R2: size 2 bytes */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 #define R2_CARD_LOCKED 0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 #define R2_WP_ERASE_SKIP 1
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 #define R2_ERR 2
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 #define R2_CARD_ERR 3
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 #define R2_CARD_ECC_FAIL 4
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 #define R2_WP_VIOLATION 5
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #define R2_INVAL_ERASE 6
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 #define R2_OUT_OF_RANGE 7
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 #define R2_CSD_OVERWRITE 7
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 #define R2_IDLE_STATE (R1_IDLE_STATE + 8)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 #define R2_ERASE_RESET (R1_ERASE_RESET + 8)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 #define R2_ILL_COMMAND (R1_ILL_COMMAND + 8)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 #define R2_COM_CRC_ERR (R1_COM_CRC_ERR + 8)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #define R2_ERASE_SEQ_ERR (R1_ERASE_SEQ_ERR + 8)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 #define R2_ADDR_ERR (R1_ADDR_ERR + 8)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 #define R2_PARAM_ERR (R1_PARAM_ERR + 8)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 /* R3: size 5 bytes */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 #define R3_OCR_MASK (0xffffffffUL)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 #define R3_IDLE_STATE (R1_IDLE_STATE + 32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 #define R3_ERASE_RESET (R1_ERASE_RESET + 32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 #define R3_ILL_COMMAND (R1_ILL_COMMAND + 32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 #define R3_COM_CRC_ERR (R1_COM_CRC_ERR + 32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 #define R3_ERASE_SEQ_ERR (R1_ERASE_SEQ_ERR + 32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 #define R3_ADDR_ERR (R1_ADDR_ERR + 32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 #define R3_PARAM_ERR (R1_PARAM_ERR + 32)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 /* Data Response: size 1 byte */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 #define DR_STATUS_MASK 0x0e
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 #define DR_STATUS_ACCEPTED 0x05
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 #define DR_STATUS_CRC_ERR 0x0a
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 #define DR_STATUS_WRITE_ERR 0x0c
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 /* status bits for card types */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 #define SD_RAW_SPEC_1 0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 #define SD_RAW_SPEC_2 1
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 #define SD_RAW_SPEC_SDHC 2
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 #if !SD_RAW_SAVE_RAM
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 /* static data buffer for acceleration */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 static uint8_t raw_block[512];
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 /* offset where the data within raw_block lies on the card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 static offset_t raw_block_address;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 #if SD_RAW_WRITE_BUFFERING
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 /* flag to remember if raw_block was written to the card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 static uint8_t raw_block_written;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 /* card type state */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 static uint8_t sd_raw_card_type;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 /* private helper functions */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 static void sd_raw_send_byte(uint8_t b);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 static uint8_t sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 static uint8_t sd_raw_send_command(uint8_t command, uint32_t arg);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 * Initializes memory card communication.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 uint8_t sd_raw_init()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 /* enable inputs for reading card status */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 configure_pin_available();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 configure_pin_locked();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 /* enable outputs for MOSI, SCK, SS, input for MISO */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 configure_pin_mosi();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 configure_pin_sck();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 configure_pin_ss();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 configure_pin_miso();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 /* initialize SPI with lowest frequency; max. 400kHz during identification mode of card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 (1 << SPE) | /* SPI Enable */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 (0 << DORD) | /* Data Order: MSB first */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 (1 << MSTR) | /* Master mode */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 (0 << CPOL) | /* Clock Polarity: SCK low when idle */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 (1 << SPR0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 SPSR &= ~(1 << SPI2X); /* No doubled clock frequency */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 /* initialization procedure */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 sd_raw_card_type = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 if(!sd_raw_available())
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 /* card needs 74 cycles minimum to start up */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 for(uint8_t i = 0; i < 10; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 /* wait 8 clock cycles */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 /* address card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 select_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 /* reset card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 uint8_t response;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 for(uint16_t i = 0; ; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 response = sd_raw_send_command(CMD_GO_IDLE_STATE, 0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 if(response == (1 << R1_IDLE_STATE))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 if(i == 0x1ff)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 #if SD_RAW_SDHC
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 /* check for version of SD card specification */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 response = sd_raw_send_command(CMD_SEND_IF_COND, 0x100 /* 2.7V - 3.6V */ | 0xaa /* test pattern */);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 if((response & (1 << R1_ILL_COMMAND)) == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 if((sd_raw_rec_byte() & 0x01) == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 return 0; /* card operation voltage range doesn't match */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 if(sd_raw_rec_byte() != 0xaa)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 return 0; /* wrong test pattern */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 /* card conforms to SD 2 card specification */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 sd_raw_card_type |= (1 << SD_RAW_SPEC_2);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 /* determine SD/MMC card type */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 sd_raw_send_command(CMD_APP, 0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 response = sd_raw_send_command(CMD_SD_SEND_OP_COND, 0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 if((response & (1 << R1_ILL_COMMAND)) == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 /* card conforms to SD 1 card specification */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 sd_raw_card_type |= (1 << SD_RAW_SPEC_1);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 /* MMC card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 /* wait for card to get ready */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 for(uint16_t i = 0; ; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 if(sd_raw_card_type & ((1 << SD_RAW_SPEC_1) | (1 << SD_RAW_SPEC_2)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 uint32_t arg = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 #if SD_RAW_SDHC
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 arg = 0x40000000;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 sd_raw_send_command(CMD_APP, 0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 response = sd_raw_send_command(CMD_SD_SEND_OP_COND, arg);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 response = sd_raw_send_command(CMD_SEND_OP_COND, 0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 if((response & (1 << R1_IDLE_STATE)) == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 if(i == 0x7fff)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 #if SD_RAW_SDHC
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 if(sd_raw_send_command(CMD_READ_OCR, 0))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 if(sd_raw_rec_byte() & 0x40)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 sd_raw_card_type |= (1 << SD_RAW_SPEC_SDHC);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 /* set block size to 512 bytes */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 if(sd_raw_send_command(CMD_SET_BLOCKLEN, 512))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 /* deaddress card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 /* switch to highest SPI frequency possible */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 SPCR &= ~((1 << SPR1) | (1 << SPR0)); /* Clock Frequency: f_OSC / 4 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318 SPSR |= (1 << SPI2X); /* Doubled Clock Frequency: f_OSC / 2 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 #if !SD_RAW_SAVE_RAM
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 /* the first block is likely to be accessed first, so precache it here */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 raw_block_address = (offset_t) -1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 #if SD_RAW_WRITE_BUFFERING
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 raw_block_written = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 if(!sd_raw_read(0, raw_block, sizeof(raw_block)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332
38
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
333 void
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
334 sd_raw_deinit(void)
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
335 {
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
336 configure_pin_mosi();
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
337 configure_pin_sck();
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
338 configure_pin_ss();
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
339 }
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
340
19
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 * Checks wether a memory card is located in the slot.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 * \returns 1 if the card is available, 0 if it is not.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 uint8_t sd_raw_available()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 return get_pin_available() == 0x00;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 * Checks wether the memory card is locked for write access.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 * \returns 1 if the card is locked, 0 if it is not.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358 uint8_t sd_raw_locked()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 return get_pin_locked() == 0x00;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 * Sends a raw byte to the memory card.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 * \param[in] b The byte to sent.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368 * \see sd_raw_rec_byte
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 void sd_raw_send_byte(uint8_t b)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372 SPDR = b;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373 /* wait for byte to be shifted out */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374 while(!(SPSR & (1 << SPIF)));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375 SPSR &= ~(1 << SPIF);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
380 * Receives a raw byte from the memory card.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 * \returns The byte which should be read.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383 * \see sd_raw_send_byte
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 uint8_t sd_raw_rec_byte()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387 /* send dummy data for receiving some */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 SPDR = 0xff;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 while(!(SPSR & (1 << SPIF)));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390 SPSR &= ~(1 << SPIF);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
392 return SPDR;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 * Send a command to the memory card which responses with a R1 response (and possibly others).
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 * \param[in] command The command to send.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400 * \param[in] arg The argument for command.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 * \returns The command answer.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403 uint8_t sd_raw_send_command(uint8_t command, uint32_t arg)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405 uint8_t response;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 /* wait some clock cycles */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410 /* send command via SPI */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 sd_raw_send_byte(0x40 | command);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 sd_raw_send_byte((arg >> 24) & 0xff);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 sd_raw_send_byte((arg >> 16) & 0xff);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414 sd_raw_send_byte((arg >> 8) & 0xff);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415 sd_raw_send_byte((arg >> 0) & 0xff);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 switch(command)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418 case CMD_GO_IDLE_STATE:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419 sd_raw_send_byte(0x95);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 case CMD_SEND_IF_COND:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422 sd_raw_send_byte(0x87);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 default:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 sd_raw_send_byte(0xff);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429 /* receive response */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430 for(uint8_t i = 0; i < 10; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432 response = sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 if(response != 0xff)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
434 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 return response;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
438 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
441 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442 * Reads raw data from the card.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 * \param[in] offset The offset from which to read.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 * \param[out] buffer The buffer into which to write the data.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 * \param[in] length The number of bytes to read.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448 * \see sd_raw_read_interval, sd_raw_write, sd_raw_write_interval
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 uint8_t sd_raw_read(offset_t offset, uint8_t* buffer, uintptr_t length)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 offset_t block_address;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453 uint16_t block_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454 uint16_t read_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 while(length > 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457 /* determine byte count to read at once */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458 block_offset = offset & 0x01ff;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 block_address = offset - block_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 read_length = 512 - block_offset; /* read up to block border */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 if(read_length > length)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462 read_length = length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 #if !SD_RAW_SAVE_RAM
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465 /* check if the requested data is cached */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
466 if(block_address != raw_block_address)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469 #if SD_RAW_WRITE_BUFFERING
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 if(!sd_raw_sync())
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 /* address card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 select_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
477 /* send single block request */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 #if SD_RAW_SDHC
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, block_address))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 /* wait for data block (start byte 0xfe) */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 while(sd_raw_rec_byte() != 0xfe);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 #if SD_RAW_SAVE_RAM
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492 /* read byte block */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 uint16_t read_to = block_offset + read_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 for(uint16_t i = 0; i < 512; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 uint8_t b = sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 if(i >= block_offset && i < read_to)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 *buffer++ = b;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501 /* read byte block */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 uint8_t* cache = raw_block;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503 for(uint16_t i = 0; i < 512; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 *cache++ = sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 raw_block_address = block_address;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 memcpy(buffer, raw_block + block_offset, read_length);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508 buffer += read_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 /* read crc16 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515 /* deaddress card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 /* let card some time to finish */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 #if !SD_RAW_SAVE_RAM
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 /* use cached data */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 memcpy(buffer, raw_block + block_offset, read_length);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
526 buffer += read_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 length -= read_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 offset += read_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
532 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
537 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
539 * Continuously reads units of \c interval bytes and calls a callback function.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541 * This function starts reading at the specified offset. Every \c interval bytes,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 * it calls the callback function with the associated data buffer.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 * By returning zero, the callback may stop reading.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546 * \note Within the callback function, you can not start another read or
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 * write operation.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 * \note This function only works if the following conditions are met:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 * - (offset - (offset % 512)) % interval == 0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 * - length % interval == 0
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 * \param[in] offset Offset from which to start reading.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 * \param[in] buffer Pointer to a buffer which is at least interval bytes in size.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 * \param[in] interval Number of bytes to read before calling the callback function.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 * \param[in] length Number of bytes to read altogether.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 * \param[in] callback The function to call every interval bytes.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 * \param[in] p An opaque pointer directly passed to the callback function.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 * \returns 0 on failure, 1 on success
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 * \see sd_raw_write_interval, sd_raw_read, sd_raw_write
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 uint8_t sd_raw_read_interval(offset_t offset, uint8_t* buffer, uintptr_t interval, uintptr_t length, sd_raw_read_interval_handler_t callback, void* p)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563 if(!buffer || interval == 0 || length < interval || !callback)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 #if !SD_RAW_SAVE_RAM
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 while(length >= interval)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 /* as reading is now buffered, we directly
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 * hand over the request to sd_raw_read()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 if(!sd_raw_read(offset, buffer, interval))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 if(!callback(buffer, offset, p))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 offset += interval;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 length -= interval;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 /* address card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583 select_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585 uint16_t block_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 uint16_t read_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587 uint8_t* buffer_cur;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 uint8_t finished = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 do
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591 /* determine byte count to read at once */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592 block_offset = offset & 0x01ff;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
593 read_length = 512 - block_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595 /* send single block request */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596 #if SD_RAW_SDHC
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? offset / 512 : offset - block_offset)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, offset - block_offset))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
605
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606 /* wait for data block (start byte 0xfe) */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 while(sd_raw_rec_byte() != 0xfe);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 /* read up to the data of interest */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 for(uint16_t i = 0; i < block_offset; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
613 /* read interval bytes of data and execute the callback */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
614 do
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616 if(read_length < interval || length < interval)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619 buffer_cur = buffer;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620 for(uint16_t i = 0; i < interval; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621 *buffer_cur++ = sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623 if(!callback(buffer, offset + (512 - read_length), p))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 finished = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 read_length -= interval;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 length -= interval;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632 } while(read_length > 0 && length > 0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634 /* read rest of data block */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635 while(read_length-- > 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 /* read crc16 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642 if(length < interval)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645 offset = offset - block_offset + 512;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
647 } while(!finished);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 /* deaddress card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 /* let card some time to finish */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
654
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659 #if DOXYGEN || SD_RAW_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
662 * Writes raw data to the card.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664 * \note If write buffering is enabled, you might have to
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665 * call sd_raw_sync() before disconnecting the card
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 * to ensure all remaining data has been written.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668 * \param[in] offset The offset where to start writing.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669 * \param[in] buffer The buffer containing the data to be written.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670 * \param[in] length The number of bytes to write.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 * \see sd_raw_write_interval, sd_raw_read, sd_raw_read_interval
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
673 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
674 uint8_t sd_raw_write(offset_t offset, const uint8_t* buffer, uintptr_t length)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
676 if(sd_raw_locked())
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
677 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
678
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
679 offset_t block_address;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
680 uint16_t block_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
681 uint16_t write_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
682 while(length > 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
683 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
684 /* determine byte count to write at once */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
685 block_offset = offset & 0x01ff;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686 block_address = offset - block_offset;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687 write_length = 512 - block_offset; /* write up to block border */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688 if(write_length > length)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
689 write_length = length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
690
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
691 /* Merge the data to write with the content of the block.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692 * Use the cached block if available.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
693 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
694 if(block_address != raw_block_address)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
695 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
696 #if SD_RAW_WRITE_BUFFERING
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
697 if(!sd_raw_sync())
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
700
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
701 if(block_offset || write_length < 512)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
702 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
703 if(!sd_raw_read(block_address, raw_block, sizeof(raw_block)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
704 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
705 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
706 raw_block_address = block_address;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
707 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
708
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
709 if(buffer != raw_block)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
710 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
711 memcpy(raw_block + block_offset, buffer, write_length);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
712
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
713 #if SD_RAW_WRITE_BUFFERING
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
714 raw_block_written = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
715
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
716 if(length == write_length)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
717 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
718 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
719 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
720
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
721 /* address card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
722 select_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
723
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
724 /* send single block request */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
725 #if SD_RAW_SDHC
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
726 if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
727 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
728 if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, block_address))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
729 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
730 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
731 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
732 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
733 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
734
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
735 /* send start byte */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
736 sd_raw_send_byte(0xfe);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
737
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
738 /* write byte block */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
739 uint8_t* cache = raw_block;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
740 for(uint16_t i = 0; i < 512; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
741 sd_raw_send_byte(*cache++);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
742
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
743 /* write dummy crc16 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
744 sd_raw_send_byte(0xff);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
745 sd_raw_send_byte(0xff);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
746
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 /* wait while card is busy */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748 while(sd_raw_rec_byte() != 0xff);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
749 sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
750
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
751 /* deaddress card */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
752 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
753
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754 buffer += write_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755 offset += write_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
756 length -= write_length;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
757
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
758 #if SD_RAW_WRITE_BUFFERING
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
759 raw_block_written = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
760 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
761 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
762
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
763 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
764 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
765 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
766
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
767 #if DOXYGEN || SD_RAW_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
768 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
769 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
770 * Writes a continuous data stream obtained from a callback function.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
771 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
772 * This function starts writing at the specified offset. To obtain the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
773 * next bytes to write, it calls the callback function. The callback fills the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
774 * provided data buffer and returns the number of bytes it has put into the buffer.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
775 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
776 * By returning zero, the callback may stop writing.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
777 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
778 * \param[in] offset Offset where to start writing.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
779 * \param[in] buffer Pointer to a buffer which is used for the callback function.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
780 * \param[in] length Number of bytes to write in total. May be zero for endless writes.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
781 * \param[in] callback The function used to obtain the bytes to write.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
782 * \param[in] p An opaque pointer directly passed to the callback function.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
783 * \returns 0 on failure, 1 on success
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
784 * \see sd_raw_read_interval, sd_raw_write, sd_raw_read
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
785 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
786 uint8_t sd_raw_write_interval(offset_t offset, uint8_t* buffer, uintptr_t length, sd_raw_write_interval_handler_t callback, void* p)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
787 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
788 #if SD_RAW_SAVE_RAM
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
789 #error "SD_RAW_WRITE_SUPPORT is not supported together with SD_RAW_SAVE_RAM"
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
790 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
791
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
792 if(!buffer || !callback)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
793 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
794
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
795 uint8_t endless = (length == 0);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
796 while(endless || length > 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
797 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
798 uint16_t bytes_to_write = callback(buffer, offset, p);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
799 if(!bytes_to_write)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
800 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
801 if(!endless && bytes_to_write > length)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
802 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
803
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
804 /* as writing is always buffered, we directly
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
805 * hand over the request to sd_raw_write()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
806 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
807 if(!sd_raw_write(offset, buffer, bytes_to_write))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
808 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
809
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
810 offset += bytes_to_write;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
811 length -= bytes_to_write;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
812 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
813
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
814 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
815 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
816 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
817
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
818 #if DOXYGEN || SD_RAW_WRITE_SUPPORT
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
819 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
820 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
821 * Writes the write buffer's content to the card.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
822 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
823 * \note When write buffering is enabled, you should
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
824 * call this function before disconnecting the
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
825 * card to ensure all remaining data has been
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
826 * written.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
827 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
828 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
829 * \see sd_raw_write
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
830 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
831 uint8_t sd_raw_sync()
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
832 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
833 #if SD_RAW_WRITE_BUFFERING
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
834 if(raw_block_written)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
835 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
836 if(!sd_raw_write(raw_block_address, raw_block, sizeof(raw_block)))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
837 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
838 raw_block_written = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
839 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
840 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
841 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
842 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
843
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
844 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
845 * \ingroup sd_raw
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
846 * Reads informational data from the card.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
847 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
848 * This function reads and returns the card's registers
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
849 * containing manufacturing and status information.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
850 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
851 * \note: The information retrieved by this function is
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
852 * not required in any way to operate on the card,
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
853 * but it might be nice to display some of the data
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
854 * to the user.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
855 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
856 * \param[in] info A pointer to the structure into which to save the information.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
857 * \returns 0 on failure, 1 on success.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
858 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
859 uint8_t sd_raw_get_info(struct sd_raw_info* info)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
860 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
861 if(!info || !sd_raw_available())
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
862 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
863
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
864 memset(info, 0, sizeof(*info));
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
865
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
866 select_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
867
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
868 /* read cid register */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
869 if(sd_raw_send_command(CMD_SEND_CID, 0))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
870 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
871 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
872 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
873 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
874 while(sd_raw_rec_byte() != 0xfe);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
875 for(uint8_t i = 0; i < 18; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
876 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
877 uint8_t b = sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
878
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
879 switch(i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
880 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
881 case 0:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
882 info->manufacturer = b;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
883 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
884 case 1:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
885 case 2:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
886 info->oem[i - 1] = b;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
887 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
888 case 3:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
889 case 4:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
890 case 5:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
891 case 6:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
892 case 7:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
893 info->product[i - 3] = b;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
894 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
895 case 8:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
896 info->revision = b;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
897 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
898 case 9:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
899 case 10:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
900 case 11:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
901 case 12:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
902 info->serial |= (uint32_t) b << ((12 - i) * 8);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
903 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
904 case 13:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
905 info->manufacturing_year = b << 4;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
906 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
907 case 14:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
908 info->manufacturing_year |= b >> 4;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
909 info->manufacturing_month = b & 0x0f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
910 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
911 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
912 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
913
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
914 /* read csd register */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
915 uint8_t csd_read_bl_len = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
916 uint8_t csd_c_size_mult = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
917 #if SD_RAW_SDHC
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
918 uint16_t csd_c_size = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
919 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
920 uint32_t csd_c_size = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
921 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
922 uint8_t csd_structure = 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
923 if(sd_raw_send_command(CMD_SEND_CSD, 0))
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
924 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
925 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
926 return 0;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
927 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
928 while(sd_raw_rec_byte() != 0xfe);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
929 for(uint8_t i = 0; i < 18; ++i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
930 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
931 uint8_t b = sd_raw_rec_byte();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
932
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
933 if(i == 0)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
934 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
935 csd_structure = b >> 6;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
936 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
937 else if(i == 14)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
938 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
939 if(b & 0x40)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
940 info->flag_copy = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
941 if(b & 0x20)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
942 info->flag_write_protect = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
943 if(b & 0x10)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
944 info->flag_write_protect_temp = 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
945 info->format = (b & 0x0c) >> 2;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
946 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
947 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
948 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
949 #if SD_RAW_SDHC
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
950 if(csd_structure == 0x01)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
951 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
952 switch(i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
953 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
954 case 7:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
955 b &= 0x3f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
956 case 8:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
957 case 9:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
958 csd_c_size <<= 8;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
959 csd_c_size |= b;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
960 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
961 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
962 if(i == 9)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
963 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
964 ++csd_c_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
965 info->capacity = (offset_t) csd_c_size * 512 * 1024;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
966 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
967 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
968 else if(csd_structure == 0x00)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
969 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
970 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
971 switch(i)
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
972 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
973 case 5:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
974 csd_read_bl_len = b & 0x0f;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
975 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
976 case 6:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
977 csd_c_size = b & 0x03;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
978 csd_c_size <<= 8;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
979 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
980 case 7:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
981 csd_c_size |= b;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
982 csd_c_size <<= 2;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
983 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
984 case 8:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
985 csd_c_size |= b >> 6;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
986 ++csd_c_size;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
987 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
988 case 9:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
989 csd_c_size_mult = b & 0x03;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
990 csd_c_size_mult <<= 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
991 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
992 case 10:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
993 csd_c_size_mult |= b >> 7;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
994
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
995 info->capacity = (uint32_t) csd_c_size << (csd_c_size_mult + csd_read_bl_len + 2);
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
996 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
997 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
998 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
999 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1000 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1001
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1002 unselect_card();
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1003
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1004 return 1;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1005 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1006