STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sd_spi.h
Go to the documentation of this file.
1#ifndef STAR_SD_SPI_H
2#define STAR_SD_SPI_H
3
4/*
5 * Portable pieces of the SD-card SPI protocol (SD Physical Layer Simplified
6 * Specification, SPI mode), used to probe the ZDSD01GLGEAG SD NAND (U10).
7 * No HAL: the board layer moves the bytes, these build and check them.
8 */
9
10#include <stdbool.h>
11#include <stdint.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17#define SD_CMD0_GO_IDLE 0U
18#define SD_CMD8_SEND_IF_COND 8U
19#define SD_CMD10_SEND_CID 10U
20#define SD_CMD55_APP_CMD 55U
21#define SD_CMD58_READ_OCR 58U
22#define SD_ACMD41_SD_SEND_OP_COND 41U
23
24#define SD_CMD8_ARG 0x000001AAU /* 2.7-3.6 V, check pattern 0xAA */
25#define SD_ACMD41_HCS 0x40000000U /* host supports high capacity */
26
27#define SD_R1_IDLE 0x01U
28#define SD_R1_ILLEGAL_COMMAND 0x04U
29#define SD_DATA_TOKEN 0xFEU
30#define SD_OCR_CCS 0x40000000U /* card capacity status: SDHC/SDXC */
31
32/* CRC7 (x^7 + x^3 + 1) over len bytes, as used by commands and the CID. */
33uint8_t sd_crc7(const uint8_t* data, uint8_t len);
34
35/* 6-byte command frame: 01 index, 32-bit argument, CRC7, end bit. */
36void sd_command_frame(uint8_t out[6], uint8_t index, uint32_t argument);
37
38/* R1 is the first byte with bit 7 clear; 0xFF means "not yet". */
39static inline bool sd_is_r1(uint8_t byte) {
40 return (byte & 0x80U) == 0U;
41}
42
43/* CMD8 R7 trailer (4 bytes after R1): voltage accepted and pattern echoed. */
44bool sd_r7_ok(const uint8_t r7[4]);
45
46/* CID register (16 bytes): byte 15 must be CRC7(bytes 0..14) << 1 | 1, and
47 * the contents must not be uniform 0xFF/0x00 (a dead bus). */
48bool sd_cid_ok(const uint8_t cid[16]);
49
50#ifdef __cplusplus
51}
52#endif
53#endif
bool sd_r7_ok(const uint8_t r7[4])
Definition sd_spi.c:26
void sd_command_frame(uint8_t out[6], uint8_t index, uint32_t argument)
Definition sd_spi.c:17
static bool sd_is_r1(uint8_t byte)
Definition sd_spi.h:39
uint8_t sd_crc7(const uint8_t *data, uint8_t len)
Definition sd_spi.c:3
bool sd_cid_ok(const uint8_t cid[16])
Definition sd_spi.c:31