121 lines
3.5 KiB
C
121 lines
3.5 KiB
C
/* Debug Console */
|
|
#include "fsl_debug_console.h"
|
|
|
|
/* FreeRTOS */
|
|
#include "FreeRTOS.h"
|
|
#include "event_groups.h"
|
|
#include "queue.h"
|
|
#include "semphr.h"
|
|
#include "task.h"
|
|
|
|
/* SDMMC */
|
|
#include "sdmmc_config.h"
|
|
|
|
/* Private */
|
|
#include "app_sdmmc.h"
|
|
|
|
static sd_card_t s_app_sd;
|
|
static SDK_ALIGN(uint8_t s_data_buffer[512], BOARD_SDMMC_DATA_BUFFER_ALIGN_SIZE);
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
uint16_t sd_identifier;
|
|
uint8_t mfg_date[6];
|
|
uint8_t health_status;
|
|
uint16_t feature_revision;
|
|
uint16_t generation_identifier;
|
|
} sandisk_status_t;
|
|
|
|
static void app_sdmmc_task(void *parameters);
|
|
|
|
int app_sdmmc_init(void) {
|
|
if (xTaskCreate(app_sdmmc_task, "SDMMC", 1024, NULL, 4, NULL) != pdPASS) {
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void app_sdmmc_detected(bool inserted, void *user_data) {
|
|
QueueHandle_t cd_q = user_data;
|
|
|
|
BaseType_t higher_prio_woken = pdFALSE;
|
|
xQueueSendFromISR(cd_q, &inserted, &higher_prio_woken);
|
|
|
|
portYIELD_FROM_ISR(higher_prio_woken);
|
|
}
|
|
|
|
static void app_sdmmc_task(void *parameters) {
|
|
QueueHandle_t cd_q = xQueueCreate(1, sizeof(bool));
|
|
if (cd_q == NULL) {
|
|
PRINTF("Failed to create card detection queue.\r\n");
|
|
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
BOARD_SD_Config(&s_app_sd, app_sdmmc_detected, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, cd_q);
|
|
if (SD_HostInit(&s_app_sd) != kStatus_Success) {
|
|
PRINTF("Failed to initialize SD host.\r\n");
|
|
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
for (;;) {
|
|
bool inserted = false;
|
|
if (xQueueReceive(cd_q, &inserted, portMAX_DELAY) != pdFALSE) {
|
|
if (inserted) {
|
|
PRINTF("SD card inserted.\r\n");
|
|
|
|
/* Reset card */
|
|
SD_SetCardPower(&s_app_sd, false);
|
|
SD_SetCardPower(&s_app_sd, true);
|
|
|
|
if (SD_CardInit(&s_app_sd) != kStatus_Success) {
|
|
PRINTF("Failed to initialize SD card.\r\n");
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
PRINTF("Card MID: %02x\r\n", s_app_sd.cid.manufacturerID);
|
|
PRINTF("Card AID: %04x\r\n", s_app_sd.cid.applicationID);
|
|
|
|
/* Sandisk */
|
|
if (s_app_sd.cid.manufacturerID == 3) {
|
|
sdmmchost_transfer_t content = {0};
|
|
sdmmchost_cmd_t command = {0};
|
|
sdmmchost_data_t data = {0};
|
|
|
|
memset(&data, 0x00, sizeof(data));
|
|
|
|
command.index = kSDMMC_GeneralCommand;
|
|
command.argument = 0x01;
|
|
command.responseType = kCARD_ResponseTypeR1;
|
|
|
|
data.blockSize = 512U;
|
|
data.blockCount = 1U;
|
|
data.rxData = (uint32_t *)s_data_buffer;
|
|
|
|
content.command = &command;
|
|
content.data = &data;
|
|
|
|
if (SDMMCHOST_TransferFunction(s_app_sd.host, &content) != kStatus_Success) {
|
|
PRINTF("Failed to send CMD56\r\n");
|
|
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
PRINTF("GEN_CMD sent.\r\n");
|
|
|
|
if (command.response[0] & SDMMC_R1_ALL_ERROR_FLAG) {
|
|
PRINTF("SD card reported error.\r\n");
|
|
}
|
|
|
|
sandisk_status_t *st = (sandisk_status_t *)s_data_buffer;
|
|
|
|
PRINTF("ID: %04x\r\n", st->sd_identifier);
|
|
}
|
|
|
|
} else {
|
|
PRINTF("SD card removed.\r\n");
|
|
}
|
|
}
|
|
}
|
|
} |