#define SD_CARD_DEBUG_PRINT 1 //デバッグ用 Printfの有効化
#define SD_CARD_DEV_NO 0 //SDカード デバイス番号
#define SD_CARD_DRV_LETTER "0:" //SDカード ドライブ番号(FatFs)
#define SD_POWER_PORT PORT1.PODR.BIT.B0 //SDカード電源制御ポート
FATFS SD_CARD_FATFS; //SDカード ファイルシステム
uint32_t SD_CARD_WORK_MEM[50]; //SDカード ワークエリア(200Byte必要)
unsigned short SD_CARD_POWER_WAIT; //SDカード 電源投入タイマ
unsigned short SD_CARD_ERR_STATUS = 0; //SDカード エラーステータス
unsigned short SD_CARD_READY = 0; //SDカード 準備テータス
//SDカード 電源初期化
sdc_sd_status_t sd_card_power_init(uint32_t card_no){
if (SDC_SD_CARD_NO0 == card_no){ //User must create processing for device number 1 or later.
SD_POWER_PORT = 0; //SD-POW
}
return SDC_SD_SUCCESS;
}
//SDカード 電源 ON
sdc_sd_status_t sd_card_power_on(uint32_t card_no){
if (SDC_SD_CARD_NO0 == card_no){ //User must create processing for device number 1 or later.
SD_POWER_PORT = 1; //SD-POW
}
//R_BSP_SoftwareDelay(10, BSP_DELAY_MILLISECS); //Supplies the Power to the SD Card and waits.
SD_CARD_POWER_WAIT = 10;
return SDC_SD_SUCCESS;
}
//SDカード 電源 OFF
sdc_sd_status_t sd_card_power_off(uint32_t card_no){
if (SDC_SD_CARD_NO0 == card_no){ //User must create processing for device number 1 or later.
SD_POWER_PORT = 0; //SD-POW
}
//R_BSP_SoftwareDelay(1, BSP_DELAY_MILLISECS); //Supplies the Power to the SD Card and waits.
return SDC_SD_SUCCESS;
}
//カード検出(CD)コールバック
sdc_sd_status_t sd_card_cd_callback (int32_t cd){
uint32_t card_no;
R_SDC_SD_CdInt(SD_CARD_DEV_NO, SDC_SD_CD_INT_DISABLE, 0);
return SDC_SD_SUCCESS;
}
//カード状態変化コールバック
sdc_sd_status_t sd_card_status_callback (int32_t channel){
return SDC_SD_SUCCESS;
}
//エラー検出
void sd_card_error_trap (uint32_t sdc_sd_card_no){
sdc_sd_status_t err_code;
err_code = R_SDC_SD_GetErrCode(sdc_sd_card_no);
#ifdef SD_CARD_DEBUG_PRINT
printf("ERROR: error code (sdc_sd_status_t) is %d.\n", err_code);
#endif
SD_CARD_ERR_STATUS |= 0x0020;
}
//タイマ処理
void sd_card_1ms_interval(void){
if(SD_CARD_POWER_WAIT) SD_CARD_POWER_WAIT--;
r_sdc_sd_1msInterval();
}
//SDカードの初期化
void sd_card_init(void){
sdc_sd_status_t sdc_sd_status;
uint32_t cmt_channel;
bool cmt_ret;
//1mS周期割り込みの登録
//cmt_ret = R_CMT_CreatePeriodic(1000,(void(*)(void*))R_SDC_SD_1msInterval, &cmt_channel);
cmt_ret = R_CMT_CreatePeriodic(1000,(void(*)(void*))sd_card_1ms_interval, &cmt_channel);
if(cmt_ret != true){
SD_CARD_ERR_STATUS |= 0x0001;
}
//SDHI インタフェースの初期化
R_SDHI_PinSetInit(); //SDHI 端子の初期設定
sd_card_power_init(SD_CARD_DEV_NO); //カード電源初期化
//SD メモリカードドライバの初期化
sdc_sd_status = R_SDC_SD_Open(SD_CARD_DEV_NO, SD_CARD_DEV_NO, &SD_CARD_WORK_MEM);
if(sdc_sd_status != SDC_SD_SUCCESS){
#ifdef SD_CARD_DEBUG_PRINT
printf("ERROR: R_SDC_SD_Open. Error code (sdc_sd_status_t) is %d.\n", sdc_sd_status);
#endif
SD_CARD_ERR_STATUS |= 0x0010;
}
sdc_sd_status = R_SDC_SD_CdInt(SD_CARD_DEV_NO, SDC_SD_CD_INT_ENABLE, sd_card_cd_callback);
if (SDC_SD_SUCCESS != sdc_sd_status){
sd_card_error_trap(SD_CARD_DEV_NO);
}
//sdc_sd_status = R_SDC_SD_IntCallback(SD_CARD_DEV_NO, sd_card_status_callback);
sdc_sd_status = R_SDC_SD_IntCallback(SD_CARD_DEV_NO, NULL);
if (SDC_SD_SUCCESS != sdc_sd_status){
sd_card_error_trap(SD_CARD_DEV_NO);
}
}
//SDカード検出処理
void sd_card_task(void){
static short init_status = 0; //初期化状態フラグ
static sdc_sd_status_t pre_detection_status = SDC_SD_SUCCESS; //カード検出前回値
sdc_sd_status_t detection_status;
sdc_sd_status_t sdc_sd_status;
sdc_sd_cfg_t sdc_sd_config;
FRESULT f_res;
//カード検出状態の確認
detection_status = R_SDC_SD_GetCardDetection(SD_CARD_DEV_NO);
if(pre_detection_status != detection_status){
pre_detection_status = detection_status;
if(detection_status == SDC_SD_SUCCESS){
//カード検出時
#ifdef SD_CARD_DEBUG_PRINT
printf("Detected attached SD card.\n");
#endif
sd_card_power_on(SD_CARD_DEV_NO); //カード電源投入
init_status = 1;
}else{
//カード未検出時
#ifdef SD_CARD_DEBUG_PRINT
printf("Detected detached SD card.\n");
#endif
sdc_sd_status = R_SDC_SD_End(SD_CARD_DEV_NO, SDC_SD_MODE_MEM);
if (SDC_SD_SUCCESS != sdc_sd_status){
sd_card_error_trap(SD_CARD_DEV_NO);
}
init_status = 0;
sd_card_power_off(SD_CARD_DEV_NO);
SD_CARD_READY = 0;
}
}
//カード初期化処理
if(init_status == 1){
if(SD_CARD_POWER_WAIT == 0){ //電源投入後安定化待ち
R_SDHI_PinSetTransfer(); //ホストを転送可能状態にする
init_status++;
}
}else if(init_status){
sdc_sd_config.mode = SDC_SD_CFG_DRIVER_MODE;
sdc_sd_config.voltage = SDC_SD_VOLT_3_3;
sdc_sd_status = R_SDC_SD_Initialize(SD_CARD_DEV_NO, &sdc_sd_config, SDC_SD_MODE_MEM);
if (SDC_SD_SUCCESS == sdc_sd_status){
init_status = 0;
f_res = f_mount(&SD_CARD_FATFS, SD_CARD_DRV_LETTER, 0);
if(f_res == FR_OK){
SD_CARD_READY = 1;
}else{
SD_CARD_ERR_STATUS |= 0x0400;
}
}else{
sdc_sd_status = R_SDC_SD_End(SD_CARD_DEV_NO, SDC_SD_MODE_MEM);
if (SDC_SD_SUCCESS != sdc_sd_status){
sd_card_error_trap(SD_CARD_DEV_NO);
}
if(init_status++ > 10000){
init_status = 0;
SD_CARD_ERR_STATUS |= 0x0100;
SD_CARD_READY = 0;
}
}
}
}