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