no-OS
Loading...
Searching...
No Matches
sd.c File Reference

Implementation of SD card interface over SPI. More...

#include "sd.h"
#include "no_os_delay.h"
#include "no_os_error.h"
#include "no_os_alloc.h"
Include dependency graph for sd.c:

Macros

#define BIT_CCS   (1u<<30)
 
#define BIT_APPLICATION_CMD   (1u<<7)
 
#define CMD(x)
 
#define ACMD(x)
 
#define CMD0_RETRY_NUMBER   (5u)
 
#define WAIT_RESP_TIMEOUT   (1000u)
 
#define R1_READY_STATE   (0x00u)
 
#define R1_IDLE_STATE   (0x01u)
 
#define R1_ILLEGAL_COMMAND   (0x04u)
 
#define R1_LEN   (1u)
 
#define R2_LEN   (2u)
 
#define R3_LEN   (5u)
 
#define R7_LEN   (5u)
 
#define CSD_LEN   (18u)
 
#define CRC_LEN   (2u)
 
#define CMD_LEN   (8u)
 
#define STUFF_ARG   (0x00000000u)
 
#define CMD8_ARG   (0x000001AAu)
 
#define ACMD41_ARG   (0x40000000u)
 
#define DATA_BLOCK_BITS   (9u)
 
#define MASK_ADDR_IN_BLOCK   (DATA_BLOCK_LEN - 1u)
 
#define MASK_BLOCK_NUMBER   (~(uint64_t)MASK_ADDR_IN_BLOCK)
 
#define START_1_BLOCK_TOKEN   (0xFEu)
 
#define START_N_BLOCK_TOKEN   (0xFCu)
 
#define STOP_TRANSMISSION_TOKEN   (0xFDu)
 
#define MASK_RESPONSE_TOKEN   (0x0Eu)
 
#define MASK_ERROR_TOKEN   (0xF0u)
 

Functions

int32_t sd_read (struct sd_desc *sd_desc, uint8_t *data, uint64_t address, uint64_t len)
 
int32_t sd_write (struct sd_desc *sd_desc, uint8_t *data, uint64_t address, uint64_t len)
 
int32_t sd_init (struct sd_desc **sd_desc, const struct sd_init_param *param)
 
int32_t sd_remove (struct sd_desc *desc)
 

Detailed Description

Implementation of SD card interface over SPI.

Author
Mihail Chindris (mihai.nosp@m.l.ch.nosp@m.indri.nosp@m.s@an.nosp@m.alog..nosp@m.com)

Copyright 2019(c) Analog Devices, Inc.

SPDX-License-Identifier: BSD-3-Clause

Macro Definition Documentation

◆ ACMD

#define ACMD ( x)
Value:
#define BIT_APPLICATION_CMD
Definition sd.c:19
#define CMD(x)
Definition sd.c:21

◆ ACMD41_ARG

#define ACMD41_ARG   (0x40000000u)

◆ BIT_APPLICATION_CMD

#define BIT_APPLICATION_CMD   (1u<<7)

◆ BIT_CCS

#define BIT_CCS   (1u<<30)

◆ CMD

#define CMD ( x)
Value:
((x) + 64)

◆ CMD0_RETRY_NUMBER

#define CMD0_RETRY_NUMBER   (5u)

◆ CMD8_ARG

#define CMD8_ARG   (0x000001AAu)

◆ CMD_LEN

#define CMD_LEN   (8u)

◆ CRC_LEN

#define CRC_LEN   (2u)

◆ CSD_LEN

#define CSD_LEN   (18u)

◆ DATA_BLOCK_BITS

#define DATA_BLOCK_BITS   (9u)

◆ MASK_ADDR_IN_BLOCK

#define MASK_ADDR_IN_BLOCK   (DATA_BLOCK_LEN - 1u)

◆ MASK_BLOCK_NUMBER

#define MASK_BLOCK_NUMBER   (~(uint64_t)MASK_ADDR_IN_BLOCK)

◆ MASK_ERROR_TOKEN

#define MASK_ERROR_TOKEN   (0xF0u)

◆ MASK_RESPONSE_TOKEN

#define MASK_RESPONSE_TOKEN   (0x0Eu)

◆ R1_IDLE_STATE

#define R1_IDLE_STATE   (0x01u)

◆ R1_ILLEGAL_COMMAND

#define R1_ILLEGAL_COMMAND   (0x04u)

◆ R1_LEN

#define R1_LEN   (1u)

◆ R1_READY_STATE

#define R1_READY_STATE   (0x00u)

◆ R2_LEN

#define R2_LEN   (2u)

◆ R3_LEN

#define R3_LEN   (5u)

◆ R7_LEN

#define R7_LEN   (5u)

◆ START_1_BLOCK_TOKEN

#define START_1_BLOCK_TOKEN   (0xFEu)

◆ START_N_BLOCK_TOKEN

#define START_N_BLOCK_TOKEN   (0xFCu)

◆ STOP_TRANSMISSION_TOKEN

#define STOP_TRANSMISSION_TOKEN   (0xFDu)

◆ STUFF_ARG

#define STUFF_ARG   (0x00000000u)

◆ WAIT_RESP_TIMEOUT

#define WAIT_RESP_TIMEOUT   (1000u)

Function Documentation

◆ sd_init()

int32_t sd_init ( struct sd_desc ** sd_desc,
const struct sd_init_param * param )

Initialize an instance of SD card and stores it to the parameter desc

Parameters
sd_desc- Pointer where to store the instance of the SD
param- Contains an initialized SPI descriptor
Returns
0 in case of success, -1 otherwise.

◆ sd_read()

int32_t sd_read ( struct sd_desc * sd_desc,
uint8_t * data,
uint64_t address,
uint64_t len )

Read data of size len from the specified address and store it in data. This operation returns only when the read is complete

Parameters
sd_desc- Instance of the SD card
data- Where data will be read
address- Address in memory from where data will be read
len- Length in bytes of data to be read
Returns
0 in case of success, -1 otherwise.

◆ sd_remove()

int32_t sd_remove ( struct sd_desc * desc)

Remove the initialize instance of SD card.

Parameters
desc- Instance of the SD card
Returns
0 in case of success, -1 otherwise.

◆ sd_write()

int32_t sd_write ( struct sd_desc * sd_desc,
uint8_t * data,
uint64_t address,
uint64_t len )

Write data of size len to the specified address This operation returns only when the write is complete

Parameters
sd_desc- Instance of the SD card
data- Data to write
address- Address in memory where data will be written
len- Length of data in bytes
Returns
0 in case of success, -1 otherwise.