1 /* 2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #pragma once 8 9 #include <stdint.h> 10 #include <stddef.h> 11 #include "esp_err.h" 12 #include "freertos/FreeRTOS.h" 13 #include "freertos/queue.h" 14 #include "driver/sdspi_host.h" 15 16 /// Control tokens used to frame data transfers 17 /// (see section 7.3.3 of SD simplified spec) 18 19 /// Token sent before single/multi block reads and single block writes 20 #define TOKEN_BLOCK_START 0b11111110 21 /// Token sent before multi block writes 22 #define TOKEN_BLOCK_START_WRITE_MULTI 0b11111100 23 /// Token used to stop multi block write (for reads, CMD12 is used instead) 24 #define TOKEN_BLOCK_STOP_WRITE_MULTI 0b11111101 25 26 /// Data response tokens 27 28 /// Mask (high 3 bits are undefined for data response tokens) 29 #define TOKEN_RSP_MASK 0b11111 30 /// Data accepted 31 #define TOKEN_RSP_OK 0b00101 32 /// Data rejected due to CRC error 33 #define TOKEN_RSP_CRC_ERR 0b01011 34 /// Data rejected due to write error 35 #define TOKEN_RSP_WRITE_ERR 0b01101 36 37 38 /// Data error tokens have format 0b0000xyzw where xyzw are signle bit flags. 39 /// MASK and VAL are used to check if a token is an error token 40 #define TOKEN_ERR_MASK 0b11110000 41 #define TOKEN_ERR_VAL 0b00000000 42 43 /// Argument is out of range 44 #define TOKEN_ERR_RANGE BIT(3) 45 /// Card internal ECC error 46 #define TOKEN_ERR_CARD_ECC BIT(2) 47 /// Card controller error 48 #define TOKEN_ERR_INTERNAL BIT(1) 49 /// Card is locked 50 #define TOKEN_ERR_LOCKED BIT(0) 51 52 53 /// Transfer format in SPI mode. See section 7.3.1.1 of SD simplified spec. 54 typedef struct { 55 // These fields form the command sent from host to the card (6 bytes) 56 uint8_t cmd_index : 6; 57 uint8_t transmission_bit : 1; 58 uint8_t start_bit : 1; 59 uint8_t arguments[4]; 60 uint8_t stop_bit : 1; 61 uint8_t crc7 : 7; 62 /// Ncr is the dead time between command and response; should be 0xff 63 uint8_t ncr; 64 /// Response data, should be set by host to 0xff for read operations 65 uint8_t r1; 66 /// Up to 16 bytes of response. Luckily, this is aligned on 4 byte boundary. 67 uint32_t response[4]; 68 /// response timeout, in milliseconds 69 int timeout_ms; 70 } sdspi_hw_cmd_t; 71 72 #define SDSPI_CMD_SIZE 6 73 #define SDSPI_NCR_MIN_SIZE 1 74 #define SDSPI_NCR_MAX_SIZE 8 75 76 //the size here contains 6 bytes of CMD, 1 bytes of dummy and the actual response 77 #define SDSPI_CMD_NORESP_SIZE (SDSPI_CMD_SIZE+0) //!< Size of the command without any response 78 #define SDSPI_CMD_R1_SIZE (SDSPI_CMD_SIZE+SDSPI_NCR_MIN_SIZE+1) //!< Size of the command with R1 response 79 #define SDSPI_CMD_R2_SIZE (SDSPI_CMD_SIZE+SDSPI_NCR_MIN_SIZE+2) //!< Size of the command with R1b response 80 #define SDSPI_CMD_R3_SIZE (SDSPI_CMD_SIZE+SDSPI_NCR_MIN_SIZE+5) //!< Size of the command with R3 response 81 #define SDSPI_CMD_R4_SIZE (SDSPI_CMD_SIZE+SDSPI_NCR_MIN_SIZE+5) //!< Size of the command with R4 response 82 #define SDSPI_CMD_R5_SIZE (SDSPI_CMD_SIZE+SDSPI_NCR_MIN_SIZE+2) //!< Size of the command with R5 response 83 #define SDSPI_CMD_R7_SIZE (SDSPI_CMD_SIZE+SDSPI_NCR_MIN_SIZE+5) //!< Size of the command with R7 response 84 85 #define SDSPI_CMD_FLAG_DATA BIT(0) //!< Command has data transfer 86 #define SDSPI_CMD_FLAG_WRITE BIT(1) //!< Data is written to the card 87 #define SDSPI_CMD_FLAG_RSP_R1 BIT(2) //!< Response format R1 (1 byte) 88 #define SDSPI_CMD_FLAG_RSP_R2 BIT(3) //!< Response format R2 (2 bytes) 89 #define SDSPI_CMD_FLAG_RSP_R3 BIT(4) //!< Response format R3 (5 bytes) 90 #define SDSPI_CMD_FLAG_RSP_R4 BIT(5) //!< Response format R4 (5 bytes) 91 #define SDSPI_CMD_FLAG_RSP_R5 BIT(6) //!< Response format R5 (2 bytes) 92 #define SDSPI_CMD_FLAG_RSP_R7 BIT(7) //!< Response format R7 (5 bytes) 93 #define SDSPI_CMD_FLAG_NORSP BIT(8) //!< Don't expect response (used when sending CMD0 first time). 94 #define SDSPI_CMD_FLAG_MULTI_BLK BIT(9) //!< For the write multiblock commands, the start token should be different 95 96 #define SDSPI_MAX_DATA_LEN 512 //!< Max size of single block transfer 97 98 void make_hw_cmd(uint32_t opcode, uint32_t arg, int timeout_ms, sdspi_hw_cmd_t *hw_cmd); 99 100 esp_err_t sdspi_host_start_command(sdspi_dev_handle_t handle, sdspi_hw_cmd_t *cmd, 101 void *data, uint32_t data_size, int flags); 102