五、主函数- #include "bitband.h"
- #include "usart.h"
- #include "systick.h"
- #include "led.h"
- #include "timer_base.h"
- #include "timer_gen.h"
- #include "timer_adv.h"
- #include "at24c02.h"
- #include "sdcard.h"
- #include "ff.h"
- #include "diskio.h"
- FATFS fs; /* FatFs文件系统对象 */
- FIL fnew; /* 文件对象 */
- FRESULT res_sd; /* 文件操作结果 */
- UINT fnum; /* 文件成功读写数量 */
- BYTE ReadBuffer[1024]={0}; /* 读缓冲区 */
- BYTE WriteBuffer[] = /* 写缓冲区*/
- "Hello 21IC,Hello GD32f30vct6!\r\n";
- extern sd_card_info_struct SDCardInfo;
- int main(void)
- {
- // sd_error_enum sd_error;
- // u16 i = 5;
-
- enhenced_run_enable();
- nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
- systick_set(120);
- at24c02_set();
- usart_set(9600);
- led_set();
- sd_io_init();
-
- disk_initialize(0);
- res_sd=f_mount(0, &fs);
-
- if(res_sd == FR_NO_FILESYSTEM)
- {
- printf("》SD卡还没有文件系统,即将进行格式化...\r\n");
- /* 格式化 */
- //res_sd=f_mkfs(0,0,0);
- printf("%d\r\n",res_sd);
- if(res_sd == FR_OK)
- {
- printf("》SD卡已成功格式化文件系统。\r\n");
- /* 格式化后,先取消挂载 */
- res_sd = f_mount(0,NULL);
- /* 重新挂载 */
- res_sd = f_mount(0, &fs);
- }
- else
- {
- printf("《《格式化失败。》》\r\n");
- //while(1);
- }
- }
- else if(res_sd!=FR_OK)
- {
- printf("!!SD卡挂载文件系统失败。(%d)\r\n",res_sd);
- printf("!!可能原因:SD卡初始化不成功。\r\n");
- while(1);
- }
- else
- {
- printf("》文件系统挂载成功,可以进行读写测试\r\n");
- }
-
- /*----------------------- 文件系统测试:写测试 -----------------------------*/
- /* 打开文件,如果文件不存在则创建它 */
- printf("\r\n****** 即将进行文件写入测试... ******\r\n");
- res_sd = f_open(&fnew, "0:GD32FatFs读写测试文件.txt",FA_CREATE_ALWAYS | FA_WRITE );
- if ( res_sd == FR_OK )
- {
- printf("》打开/创建FatFs读写测试文件.txt文件成功,向文件写入数据。\r\n");
- /* 将指定存储区内容写入到文件内 */
- res_sd=f_write(&fnew,WriteBuffer,sizeof(WriteBuffer),&fnum);
- if(res_sd==FR_OK)
- {
- printf("》文件写入成功,写入字节数据:%d\n",fnum);
- printf("》向文件写入的数据为:\r\n%s\r\n",WriteBuffer);
- }
- else
- {
- printf("!!文件写入失败:(%d)\n",res_sd);
- }
- /* 不再读写,关闭文件 */
- f_close(&fnew);
- }
- else
- {
- printf("!!打开/创建文件失败。\r\n");
- }
-
- /*------------------- 文件系统测试:读测试 ------------------------------------*/
- printf("****** 即将进行文件读取测试... ******\r\n");
- res_sd = f_open(&fnew, "0:GD32FatFs读写测试文件.txt", FA_OPEN_EXISTING | FA_READ);
- if(res_sd == FR_OK)
- {
- printf("》打开文件成功。\r\n");
- res_sd = f_read(&fnew, ReadBuffer, sizeof(ReadBuffer), &fnum);
- if(res_sd==FR_OK)
- {
- printf("》文件读取成功,读到字节数据:%d\r\n",fnum);
- printf("》读取得的文件数据为:\r\n%s \r\n", ReadBuffer);
- }
- else
- {
- printf("!!文件读取失败:(%d)\n",res_sd);
- }
- }
- else
- {
- printf("!!打开文件失败。\r\n");
- }
- /* 不再读写,关闭文件 */
- f_close(&fnew);
-
- /* 不再使用文件系统,取消挂载文件系统 */
- f_mount(0, &fs);
-
- while(1)
- {
- led_toggle(led_all);
- delay_xs(1);
- }
- }
|