annotate sd_raw.c @ 49:12236bceb6b0 default tip

snapshot export of the schematic
author Matt Johnston <matt@ucc.asn.au>
date Wed, 25 Jun 2014 21:29:52 +0800
parents 2a47c458d6ed
children
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>
44
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
13 #include <util/delay.h>
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
14 #include <avr/pgmspace.h>
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
15 #include <stdio.h>
19
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #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
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 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * \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
20 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * 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
22 * 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
23 * 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
24 * access.
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 * @{
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 */
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 * \file
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 * 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
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 * \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
33 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
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 * \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
37 * 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
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 * @}
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
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 /* 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
45
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 /* 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
47 #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
48 /* 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
49 #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
50 /* 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
51 #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
52 /* 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
53 #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
54 /* 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
55 #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
56 /* 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
57 #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
58 /* 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
59 #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
60 /* 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
61 #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
62 /* 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
63 #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
64 /* 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
65 #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
66 /* 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
67 #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
68 /* 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
69 #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
70 /* 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
71 #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
72 /* 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
73 #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
74 /* 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
75 #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
76 /* 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
77 #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
78 /* 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
79 #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
80 /* 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
81 #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
82 /* 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
83 #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
84 /* 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
85 #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
86 /* 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
87 #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
88 /* 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
89 #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
90 /* 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
91 #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
92 /* 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
93 #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
94 /* 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
95 #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
96 /* 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
97 #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
98 /* 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
99 #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
100 /* 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
101 #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
102
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 /* 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
104 /* 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
105 #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
106 #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
107 #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
108 #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
109 #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
110 #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
111 #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
112 /* 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
113 /* 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
114 #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
115 #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
116 #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
117 #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
118 #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
119 #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
120 #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
121 #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
122 #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
123 #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
124 #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
125 #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
126 #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
127 #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
128 #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
129 #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
130 /* 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
131 #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
132 #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
133 #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
134 #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
135 #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
136 #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
137 #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
138 #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
139 /* 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
140 #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
141 #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
142 #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
143 #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
144
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 /* 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
146 #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
147 #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
148 #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
149
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 #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
151 /* 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
152 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
153 /* 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
154 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
155 #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
156 /* 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
157 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
158 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 #endif
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 /* 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
162 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
163
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 /* 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
165 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
166 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
167 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
168
44
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
169 static uint8_t orig_pb;
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
170
19
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 * \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
173 * 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
174 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 * \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
176 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 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
178 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 /* 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
180 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
181 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
182
44
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
183 orig_pb = PORTB;
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
184
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
185 printf_P(PSTR("startup portb %hhx mosi %d, sck %d, ss, miso %d\n"),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
186 PORTB,
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
187 orig_pb & _BV(PB3),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
188 orig_pb & _BV(PB5),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
189 orig_pb & _BV(PB2),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
190 orig_pb & _BV(PB4));
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
191
19
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 /* 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
193 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
194 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
195 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
196 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
197
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 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
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 /* 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
201 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
202 (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
203 (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
204 (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
205 (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
206 (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
207 (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
208 (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
209 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
210
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 /* 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
212 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
213
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 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
215 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
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 /* 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
218 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
219 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 /* 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
221 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
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
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 /* 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
225 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
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 /* 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
228 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
229 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
230 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 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
232 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
233 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234
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(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
236 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 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
238 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
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 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 #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
243 /* 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
244 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
245 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
246 {
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_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
248 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
249 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
250 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
251 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
252 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
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 /* 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
255 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
256 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 #endif
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 /* 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
261 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
262 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
263 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
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 /* 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
266 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
267 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 /* 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
271 }
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
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 /* 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
275 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
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 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
278 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 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
280 #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
281 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
282 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
283 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 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
285 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
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 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 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
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
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 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
293 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 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
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 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
298 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
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 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 #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
303 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
304 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 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
306 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 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
308 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
309 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 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
312 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
313
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 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
315 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
316 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
317 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318 #endif
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 /* 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
321 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
322 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 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
324 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
325 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 /* 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
328 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
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 /* 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
331 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
332 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
333
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 #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
335 /* 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
336 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
337 #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
338 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
339 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 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
341 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
342 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 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
345 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346
38
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
347 void
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
348 sd_raw_deinit(void)
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
349 {
44
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
350
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
351 printf_P(PSTR("deinit mosi %d, sck %d, ss, miso %d\n"),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
352 PORTB & _BV(PB3),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
353 PORTB & _BV(PB5),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
354 PORTB & _BV(PB2),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
355 PORTB & _BV(PB4));
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
356
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
357 // set to slave mode
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
358 SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
359 (1 << SPE) | /* SPI Enable */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
360 (0 << DORD) | /* Data Order: MSB first */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
361 (0 << MSTR) | /* Master mode */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
362 (0 << CPOL) | /* Clock Polarity: SCK low when idle */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
363 (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
364 (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
365 (1 << SPR0);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
366
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
367 _delay_ms(15);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
368
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
369 // disable it
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
370 SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
371 (0 << SPE) | /* SPI Enable */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
372 (0 << DORD) | /* Data Order: MSB first */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
373 (0 << MSTR) | /* Master mode */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
374 (0 << CPOL) | /* Clock Polarity: SCK low when idle */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
375 (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
376 (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
377 (1 << SPR0);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
378
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
379 _delay_ms(15);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
380
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
381 uint8_t pb_mask = _BV(PB3) | _BV(PB5) | _BV(PB2);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
382 printf_P(PSTR("PORTB was %hhx\n"),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
383 PORTB);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
384 PORTB = (PORTB & ~pb_mask) | (orig_pb & pb_mask);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
385 printf_P(PSTR("PORTB now %hhx\n"),
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
386 PORTB);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
387
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
388 _delay_ms(15);
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
389
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
390 deconfigure_pin_mosi();
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
391 deconfigure_pin_sck();
2a47c458d6ed a bit of debugging tht didn't work
Matt Johnston <matt@ucc.asn.au>
parents: 38
diff changeset
392 deconfigure_pin_ss();
38
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
393 }
9e888708f33d sd test code
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
394
19
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 * 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
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 * \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
400 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 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
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 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
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
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 * \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
408 * 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
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 * \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
411 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 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
413 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414 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
415 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416
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 * \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
419 * 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
420 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 * \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
422 * \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
423 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 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
425 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 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
427 /* 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
428 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
429 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
430 }
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 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 * \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
434 * 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
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 * \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
437 * \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
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 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
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 /* 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
442 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
443 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
444 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
445
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 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
447 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448
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 * \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
451 * 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
452 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453 * \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
454 * \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
455 * \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
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 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
458 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 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
460
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 /* 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
462 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
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 /* 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
465 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
466 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
467 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
468 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
469 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
470 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
471 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 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
473 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
474 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 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
476 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
477 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 default:
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 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
480 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 /* 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
484 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
485 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 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
487 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
488 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 }
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 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
492 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 * \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
496 * 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
497 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 * \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
499 * \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
500 * \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
501 * \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
502 * \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
503 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 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
505 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 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
507 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
508 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
509 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
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 /* 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
512 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
513 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
514 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
515 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
516 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
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 #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
519 /* 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
520 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
521 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 #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
524 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
525 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
526 #endif
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 /* 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
529 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
530
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 /* 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
532 #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
533 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
534 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 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
536 #endif
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 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
539 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
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
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 /* 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
543 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
544
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 #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
546 /* 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
547 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
548 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
549 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 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
551 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
552 *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
553 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 /* 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
556 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
557 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
558 *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
559 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
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 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
562 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
563 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 /* 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
566 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
567 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
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 /* 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
570 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
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 /* 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
573 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
574 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 #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
576 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 /* 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
579 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
580 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
581 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584 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
585 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
586 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 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
589 }
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 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592 * \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
593 * 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
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 * 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
596 * 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
597 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598 * 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
599 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600 * \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
601 * 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
602 * \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
603 * - (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
604 * - 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
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 * \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
607 * \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
608 * \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
609 * \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
610 * \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
611 * \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
612 * \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
613 * \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
614 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615 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
616 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617 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
618 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
619
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620 #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
621 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
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 /* 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
624 * 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
625 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 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
627 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
628 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
629 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 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
631 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
632 }
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 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
635 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 /* 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
637 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
638
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639 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
640 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
641 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
642 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
643 do
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 /* 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
646 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
647 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
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 /* 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
650 #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
651 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
652 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653 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
654 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 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
657 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
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
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 /* 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
661 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
662
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663 /* 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
664 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
665 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
666
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667 /* 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
668 do
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670 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
671 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
673 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
674 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
675 *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
676
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
677 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
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 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
680 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
681 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
682
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
683 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
684 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
685
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686 } 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
687
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688 /* 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
689 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
690 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
691
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692 /* 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
693 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
694 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
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(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
697 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 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
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 } 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
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 /* 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
704 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
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 /* 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
707 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
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 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
710 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
711 }
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 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
714 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
715 * \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
716 * 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
717 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
718 * \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
719 * 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
720 * 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
721 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
722 * \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
723 * \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
724 * \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
725 * \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
726 * \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
727 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
728 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
729 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
730 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
731 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
732
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
733 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
734 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
735 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
736 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
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 /* 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
739 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
740 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
741 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
742 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
743 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
744
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
745 /* 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
746 * 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
747 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748 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
749 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
750 #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
751 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
752 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
753 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755 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
756 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
757 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
758 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
759 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
760 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
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 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
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 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
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 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
768 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
769
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
770 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
771 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
772 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
773 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
774
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
775 /* 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
776 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
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 /* 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
779 #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
780 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
781 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
782 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
783 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
784 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
785 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
786 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
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
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
789 /* 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
790 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
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 /* 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
793 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
794 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
795 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
796
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
797 /* 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
798 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
799 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
800
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
801 /* 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
802 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
803 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
804
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
805 /* 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
806 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
807
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
808 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
809 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
810 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
811
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
812 #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
813 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
814 #endif
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
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
817 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
818 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
819 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
820
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
821 #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
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 * \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
824 * 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
825 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
826 * 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
827 * 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
828 * 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
829 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
830 * 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
831 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
832 * \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
833 * \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
834 * \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
835 * \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
836 * \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
837 * \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
838 * \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
839 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
840 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
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 #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
843 #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
844 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
845
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
846 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
847 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
848
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
849 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
850 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
851 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
852 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
853 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
854 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
855 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
856 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
857
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
858 /* 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
859 * 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
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(!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
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 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
865 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
866 }
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 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
869 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
870 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
871
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
872 #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
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 * \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
875 * 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
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 * \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
878 * 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
879 * 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
880 * written.
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
881 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
882 * \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
883 * \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
884 */
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
885 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
886 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
887 #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
888 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
889 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
890 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
891 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
892 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
893 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
894 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
895 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
896 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
897
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
898 /**
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
899 * \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
900 * 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
901 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
902 * 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
903 * 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
904 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
905 * \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
906 * 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
907 * 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
908 * 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
909 *
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
910 * \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
911 * \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
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 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
914 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
915 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
916 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
917
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
918 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
919
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
920 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
921
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
922 /* 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
923 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
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 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
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 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
936 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
937 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
938 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
939 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
940 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
941 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
942 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
943 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
944 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
945 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
946 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
947 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
948 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
949 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
950 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
951 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
952 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
953 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
954 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
955 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
956 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
957 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
958 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
959 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
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 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
962 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
963 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
964 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
965 }
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 /* 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
969 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
970 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
971 #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
972 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
973 #else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
974 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
975 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
976 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
977 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
978 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
979 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
980 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
981 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
982 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
983 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
984 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
985 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
986
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
987 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
988 {
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_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
990 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
991 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
992 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
993 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
994 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
995 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
996 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
997 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
998 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
999 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
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 else
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1002 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1003 #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
1004 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
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 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
1007 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1008 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
1009 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
1010 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
1011 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
1012 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
1013 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
1014 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1015 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1016 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
1017 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1018 ++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
1019 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
1020 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1021 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1022 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
1023 #endif
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1024 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1025 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
1026 {
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1027 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
1028 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
1029 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1030 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
1031 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
1032 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
1033 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1034 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
1035 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
1036 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
1037 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1038 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
1039 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
1040 ++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
1041 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1042 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
1043 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
1044 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
1045 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1046 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
1047 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
1048
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1049 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
1050 break;
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1051 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1052 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1053 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1054 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1055
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1056 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
1057
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1058 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
1059 }
5f9a40d6991b Import SD handling from http://www.roland-riegel.de/sd-reader/index.html
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1060