Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to read and write the sd card using renesas rx62n . i tried using the sample code of rl78 using rx62n library bt it didnt works. can any one provide me a sample program. Thanks in advance

What I have tried:

#include "lib/r_bsp/platform.h"
#include "lib/r_sdc/r_sdc_sd_rx_config.h"

#include "lib/r_cmt_rx/r_cmt_rx_if.h"
#include "lib/r_sys_time_rx/r_sys_time_rx_if.h"
#include "lib/r_sdc/r_sdc_sdmem_rx/r_sdc_sd_rx_if.h"
#include "lib/r_pincfg/r_sdhi_rx_pinset.h"
#include "lib/r_tfat_driver_rx/r_tfat_driver_rx_config.h"
#include "lib/r_smc/r_smc_entry.h"

#define PERFORMANCE_MESUREMENT_DATA_SIZE 1024 * 20 /* 20KB */

typedef enum{
    NO_INITIALIZED = 0,
    IDLE,
    ON_PROCESS, /* Prevent from processing another task */
    REQUEST_SDC_DETECTION_TASK,
    REQUEST_SDC_INSERTION_TASK,
    REQUEST_SDC_REMOVAL_TASK,
}app_status_t;

uint8_t g_drv_tbl[TFAT_SDHI_DRIVE_NUM ];
uint32_t g_sdc_sd_work[TFAT_SDHI_DRIVE_NUM ][50];
FATFS g_fatfs[TFAT_SDHI_DRIVE_NUM ];
FIL g_file[TFAT_SDHI_DRIVE_NUM ];
app_status_t g_app_status = NO_INITIALIZED;
uint8_t g_oneshot_timer_flg = 0;
const uint8_t data_to_write[PERFORMANCE_MESUREMENT_DATA_SIZE] =
{
    0x52, 0x65, 0x6E, 0x65, 0x73, 0x61, 0x73, 0x0a,
};
uint8_t data_to_read[PERFORMANCE_MESUREMENT_DATA_SIZE];

void main(void);
static void initialize_sdc_demo (void);
sdc_sd_status_t r_sdc_sd_cd_callback (int32_t cd);
void set_status_sdc_detection(void);
void card_detection(void);
void process_on_sdc_insertion (void);
void process_on_sdc_removal (void);
void initialize_sdc_on_insertion (uint32_t sdc_no);
void deinitialize_sdc_on_insertion (uint32_t sdc_no);
sdc_sd_status_t r_sdc_sd_callback (int32_t channel);
void tfat_sample (void);
sdc_sd_status_t r_sdc_sdmem_demo_power_init(uint32_t card_no);
sdc_sd_status_t r_sdc_sdmem_demo_power_on(uint32_t card_no);
sdc_sd_status_t r_sdc_sdmem_demo_power_off(uint32_t card_no);
sys_time_err_t wait_milliseconds (uint32_t interval_milliseconds);
void set_oneshot_timer_flg (void);
static void error_trap_r_sdc_sd (uint32_t sdc_sd_card_no);
void blink_LED(void);
void cmt_1us_callback(void *arg);
void _1us_timer_reset(void);
void _1us_timer_stop(void);
void _1us_timer_start(void);
uint32_t _1us_timer_get(void);

uint32_t _1us_timer;
volatile uint32_t _1us_timer_flag;

void main (void)
{
	uint32_t cmt_channel;

    /* Initialize for this demo program */
    initialize_sdc_demo();
    R_CMT_CreatePeriodic(1000000, cmt_1us_callback, &cmt_channel);

    /* Process tasks by a current status */
    while (1)
    {
        switch (g_app_status)
        {
        case NO_INITIALIZED:
            return;
        case IDLE:
            break;
        case ON_PROCESS:
            break;
        case REQUEST_SDC_DETECTION_TASK:
            card_detection();
            break;
        case REQUEST_SDC_INSERTION_TASK:
            process_on_sdc_insertion();
            break;
        case REQUEST_SDC_REMOVAL_TASK:
            process_on_sdc_removal();
            break;
        default:
            return;
        }
    }
	
}

static void initialize_sdc_demo (void)
{
    uint32_t cmt_channel;
    SYS_TIME sys_time;
    sdc_sd_status_t sdc_sd_status = SDC_SD_SUCCESS;

    /* Initialize global variables for this demo */
    g_app_status = NO_INITIALIZED;
    g_oneshot_timer_flg = 0;

    /* Initialize pin settings for SDHI.
     * This function is generated by SDHI FIT's pin settings of the Smart Configurator */
    R_SDHI_PinSetInit();

    /* System timer settings */
    R_SYS_TIME_Open();
    sys_time.year = 2022;
    sys_time.month = 05;
    sys_time.day = 06;
    sys_time.hour = 0;
    sys_time.min = 0;
    sys_time.sec = 0;
    R_SYS_TIME_SetCurrentTime( &sys_time);

    /* SD card driver Initialization */
    r_sdc_sdmem_demo_power_init(SDC_SD_CARD_NO0);
    sdc_sd_status = R_SDC_SD_Open(SDC_SD_CARD_NO0, SDHI_CH0, &g_sdc_sd_work[SDC_SD_CARD_NO0]);
    if (SDC_SD_SUCCESS != sdc_sd_status)
    {
        /* Function error_trap_r_sdc_sd() can not called because R_SDC_SD_GetErrCode initially needs R_SDC_SD_Open */
        printf("ERROR: R_SDC_SD_Open. Error code (sdc_sd_status_t) is %d.\n", sdc_sd_status);
        while (1)
        {
            R_BSP_NOP();
        }
    }

    /* Register callback when SD card is inserted/removed */
    sdc_sd_status = R_SDC_SD_CdInt(SDC_SD_CARD_NO0, SDC_SD_CD_INT_ENABLE, r_sdc_sd_cd_callback);
    if (SDC_SD_SUCCESS != sdc_sd_status)
    {
        error_trap_r_sdc_sd(SDC_SD_CARD_NO0);
    }

    /* Setting of internal timer of SD card driver */
    R_CMT_CreatePeriodic(1000, (void (*) (void *)) R_SDC_SD_1msInterval, &cmt_channel);

    /* Table between SD card number of SD card driver and dirve number of TFAT */
    g_drv_tbl[SDC_SD_CARD_NO0] = TFAT_DRIVE_NUM_0;

    /* Set status to check the SD card insertion/removal every 10 ms,
     * then process file system tasks when occurring SD card's insertion */
    if (SYS_TIME_SUCCESS == R_SYS_TIME_RegisterPeriodicCallback(set_status_sdc_detection, 1))
    {
        printf("!!! Ready for this demo. Attach SD card. !!!\n");
        g_app_status = IDLE;
    }
}
sdc_sd_status_t r_sdc_sd_cd_callback (int32_t cd)
{
    sdc_sd_status_t sdc_sd_status = SDC_SD_SUCCESS;

    sdc_sd_status = R_SDC_SD_CdInt(SDC_SD_CARD_NO0, SDC_SD_CD_INT_DISABLE, 0);
    if (SDC_SD_SUCCESS != sdc_sd_status)
    {
        error_trap_r_sdc_sd(SDC_SD_CARD_NO0);
    }
    return SDC_SD_SUCCESS;
}

void set_status_sdc_detection (void)
{
    if (IDLE == g_app_status)
    {
        g_app_status = REQUEST_SDC_DETECTION_TASK;
    }
}
void card_detection (void)
{
    static sdc_sd_status_t sdc_sd_card_detection = SDC_SD_ERR;
    static sdc_sd_status_t previous_sdc_sd_card_detection = SDC_SD_ERR;

    g_app_status = ON_PROCESS;

    sdc_sd_card_detection = R_SDC_SD_GetCardDetection(SDC_SD_CARD_NO0);
    if (previous_sdc_sd_card_detection != sdc_sd_card_detection)
    {
        previous_sdc_sd_card_detection = sdc_sd_card_detection;

        if (SDC_SD_SUCCESS == sdc_sd_card_detection)
        {
            /* Detected attached SD card */
            printf("Detected attached SD card.\n");
            g_app_status = REQUEST_SDC_INSERTION_TASK;
        }
        else
        {
            /* Detected detached SD card */
            printf("Detected detached SD card.\n");
            g_app_status = REQUEST_SDC_REMOVAL_TASK;

        }
    }
    else /* if (previous_sdc_sd_card_detection != sdc_sd_card_detection) */
    {
        g_app_status = IDLE;
    }
}
void process_on_sdc_insertion (void)
{
    g_app_status = ON_PROCESS;

    /* SD card initialization */
    initialize_sdc_on_insertion(SDC_SD_CARD_NO0);

    /* Start of blinking LED per 500 ms */
    R_SYS_TIME_RegisterPeriodicCallback(blink_LED, 50);

    /* Process file system tasks */
    if (TFAT_DRIVE_NUM_0 == g_drv_tbl[SDC_SD_CARD_NO0])
    {
        tfat_sample();
    }

    /* SD card de-initialization */
    deinitialize_sdc_on_insertion(SDC_SD_CARD_NO0);

    printf("!!! Detach SD card !!!.\n");
    g_app_status = IDLE;
}

void process_on_sdc_removal (void)
{
    g_app_status = ON_PROCESS;

    r_sdc_sdmem_demo_power_off(SDC_SD_CARD_NO0);

    /* End of blinking LED */
    R_SYS_TIME_UnregisterPeriodicCallback(blink_LED);
    PORT2.DDR.BIT.B0 = 0; /* LED off */

    g_app_status = IDLE;
}

void initialize_sdc_on_insertion (uint32_t sdc_no)
{
    sdc_sd_status_t sdc_sd_status = SDC_SD_SUCCESS;
    sdc_sd_cfg_t sdc_sd_config;

    r_sdc_sdmem_demo_power_on(sdc_no);
    R_SDHI_PinSetTransfer();
    R_SDC_SD_IntCallback(sdc_no, r_sdc_sd_callback);
    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(sdc_no, &sdc_sd_config,
            SDC_SD_MODE_MEM);
    if (SDC_SD_SUCCESS != sdc_sd_status)
    {
        error_trap_r_sdc_sd(sdc_no);
    }
}

void deinitialize_sdc_on_insertion (uint32_t sdc_no)
{
    sdc_sd_status_t sdc_sd_status = SDC_SD_SUCCESS;

    sdc_sd_status = R_SDC_SD_End(sdc_no, SDC_SD_MODE_MEM);
    if (SDC_SD_SUCCESS != sdc_sd_status)
    {
        error_trap_r_sdc_sd(sdc_no);
    }
}

sdc_sd_status_t r_sdc_sd_callback (int32_t channel)
{
    return SDC_SD_SUCCESS;
}

void tfat_sample (void)
{
    const char *drv0 = "0:";
    const char *path_fld = "0:FLD";
    const char *path_txt = "0:FLD/TEXT.TXT";
    uint8_t drv_no = TFAT_DRIVE_NUM_0;
    FRESULT rst;
     uint32_t file_rw_cnt;

    printf("Start TFAT sample.\n");

    /* Mount the file system (Note delayed mounting) */
    rst = R_tfat_f_mount( &g_fatfs[drv_no], drv0, 0);
    if (TFAT_FR_OK != rst)
    {
        printf("TFAT Error: Drive mount.\n");
    }

    /* Create the directory */
    rst = R_tfat_f_mkdir(path_fld);
    if (TFAT_FR_EXIST == rst)
    {
        printf("TFAT Error: Directory \"FLD\" is already existing.\n");
    }
    else if (TFAT_FR_OK != rst)
    {
        printf("TFAT Error: Directory creation.\n");
    }

    /* Create the file to be written */
    rst = R_tfat_f_open( &g_file[drv_no], path_txt, TFAT_FA_CREATE_ALWAYS | TFAT_FA_WRITE);
    if (TFAT_FR_OK != rst)
    {
        printf("TFAT Error: File creation and open.\n");
    }
    /* Complete file open */

	_1us_timer_reset();
	_1us_timer_start();
    /* Copy the contents to the newly created file */
    rst = R_tfat_f_write( &g_file[drv_no], (void *) data_to_write, sizeof(data_to_write), &file_rw_cnt);
    if (rst != TFAT_FR_OK || file_rw_cnt < sizeof(data_to_write))
    {
        printf("TFAT Error: File writing operation.\n");
    }
    /* file write complete */
	_1us_timer_stop();
	printf("file write %d bytes takes %d us, throughput = %f Mbps\n", sizeof(data_to_write), _1us_timer_get(), (float)((sizeof(data_to_write) * 8) / (float)((float)_1us_timer_get() / (1000000))/1000000));

    /* Close the file */
    rst = R_tfat_f_close( &g_file[drv_no]);
    if (TFAT_FR_OK != rst)
    {
        printf("TFAT Error: File close.\n");
    }

    /* Create the file to be written */
    rst = R_tfat_f_open( &g_file[drv_no], path_txt, TFAT_FA_READ);
    if (TFAT_FR_OK != rst)
    {
        printf("TFAT Error: File creation and open.\n");
    }
    /* Complete file open */

	_1us_timer_reset();
	_1us_timer_start();
    /* Read the contents from the file */
    rst = R_tfat_f_read( &g_file[drv_no], (void *) data_to_read, sizeof(data_to_read), &file_rw_cnt);
    if (rst != TFAT_FR_OK || file_rw_cnt < sizeof(data_to_read))
    {
        printf("TFAT Error: File reading operation.\n");
    }
    /* file write complete */
	_1us_timer_stop();
	printf("file read %d bytes takes %d us, throughput = %f Mbps\n", sizeof(data_to_write), _1us_timer_get(), (float)((sizeof(data_to_write) * 8) / (float)((float)_1us_timer_get() / (1000000))/1000000));

    /* Close the file */
    rst = R_tfat_f_close( &g_file[drv_no]);
    if (TFAT_FR_OK != rst)
    {
        printf("TFAT Error: File close.\n");
    }

    printf("End TFAT sample.\n");
}

sdc_sd_status_t r_sdc_sdmem_demo_power_init (uint32_t card_no)
{
    if (SDC_SD_CARD_NO0 == card_no)
    {
  PORT2.DR.BIT.B1 = 0x01U;
    PORTC.DR.BIT.B1 = 0U;
    PORT4.DR.BIT.B1 = 0U;
    PORT2.DR.BIT.B1 = 1U;
    }
    return SDC_SD_SUCCESS;
}

sdc_sd_status_t r_sdc_sdmem_demo_power_on (uint32_t card_no)
{
    /* ---- Power On ---- */
    if (SDC_SD_CARD_NO0 == card_no)
    {
        PORT4.DR.BIT.B2 = 0x01;     /* SDHI_POWER on */
        if (1 == PORT4.DR.BIT.B2)
        {
            /* Wait for the write completion */
            R_BSP_NOP();
        }
    }

    /* ---- Supplies the Power to the SD Card and waits for 100 ms ---- */
    if(SYS_TIME_SUCCESS != wait_milliseconds(100))
    {
        return SDC_SD_ERR;
    }

    return SDC_SD_SUCCESS;
}


sdc_sd_status_t r_sdc_sdmem_demo_power_off (uint32_t card_no)
{
    /* ---- Power Off ---- */
    if (SDC_SD_CARD_NO0 == card_no)
    {
          PORT2.DR.BIT.B2 = 0U;
  /* SDHI_POWER off */
        if (1 == PORT4.DR.BIT.B2)
        {
            /* Wait for the write completion */
            R_BSP_NOP();
        }
    }

    /* ---- Stops the Power to the SD Card and waits for 100 ms ---- */
    if(SYS_TIME_SUCCESS != wait_milliseconds(100))
    {
        return SDC_SD_ERR;
    }

    return SDC_SD_SUCCESS;
}

sys_time_err_t wait_milliseconds (uint32_t interval_milliseconds)
{
    sys_time_err_t sys_time_err = SYS_TIME_ERR_BAD_INTERVAL;
    uint32_t interval_10milliseconds;

    if (1 == g_oneshot_timer_flg)
    {
        return sys_time_err;
    }
    interval_10milliseconds = interval_milliseconds / 10;
    sys_time_err = R_SYS_TIME_RegisterPeriodicCallback(set_oneshot_timer_flg, interval_10milliseconds);
    if (SYS_TIME_SUCCESS != sys_time_err)
    {
        return sys_time_err;
    }
    while(0 == g_oneshot_timer_flg)
    {
        R_BSP_NOP();
    }
    sys_time_err = R_SYS_TIME_UnregisterPeriodicCallback(set_oneshot_timer_flg);
    g_oneshot_timer_flg = 0;
    return sys_time_err;
}

void set_oneshot_timer_flg (void)
{
    g_oneshot_timer_flg = 1;
}

static void error_trap_r_sdc_sd (uint32_t sdc_sd_card_no)
{
    sdc_sd_status_t err_code;

    err_code = R_SDC_SD_GetErrCode(sdc_sd_card_no);

    R_SDC_SD_Log(0x00000001, 0x0000003f, 0x0001ffff);

    printf("ERROR: error code (sdc_sd_status_t) is %d.\n", err_code);
    while (1)
    {
        R_BSP_NOP();
    }
}

void blink_LED(void)
{
    if(PORT2.DDR.BIT.B1 == 1)
    {
        PORT2.DR.BIT.B0 = 0;
    }
    else
    {
        PORT2.DR.BIT.B0 = 0U;
    }
}

void cmt_1us_callback(void *arg)
{
	if(_1us_timer_flag)
	{
		_1us_timer++;
	}
}

void _1us_timer_reset(void)
{
	_1us_timer = 0;
}

void _1us_timer_stop(void)
{
	_1us_timer_flag = 0;
}

void _1us_timer_start(void)
{
	_1us_timer_flag = 1;
}

uint32_t _1us_timer_get(void)
{
	return _1us_timer;
}
Posted
Updated 30-Jun-22 0:07am
v2
Comments
CHill60 30-Jun-22 6:00am    
"it didnt works" does not provide us with any useful information. Exactly what did or did not happen

1 solution

"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

I'd start by going back to where you got the code from, and asking there: they will know their code better than us, and have a much better idea what the "rx62n library" is capable of, and how to use it. If that is the hardware manufacturers, then their tech support is definitely the first place to start!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900