| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- /*
- * Copyright (c) 2021 HPMicro
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
- #if 0
- #include <stdio.h>
- #include "board.h"
- #include "test.h"
- #ifdef TEST_EN
- #include "bsp_V8M_YY_led.h"
- #include "bsp_V8M_YY_pwm.h"
- #include "bsp_V8M_YY_adc.h"
- #include "hard_system.h"
- #include "hard_system_time.h"
- #include "hard_system_delay.h"
- #include "hard_system_timer.h"
- #include "hard_imu_uart3.h"
- #include "hard_rc_sbus.h"
- #include "hard_can.h"
- #include "hard_sbus_out.h"
- #include "hard_flash_gd25q16.h"
- #include "main.h"
- #include "hpm_math.h"
- #endif
- /*
- 1 手册:中断源优先级,有效值为 0 到 7。
- 2 注意:内存缓存问题 catch
- 如果DMA要访问 → 必须用非缓存宏
- 如果多核要共享 → 必须用非缓存宏
- 如果频繁被中断更新 → 建议用非缓存宏
- 其他情况 → 不用修饰,让编译器优化
- 3 注意配置顺序 IO-时钟-外设
- 4 XDMA,作为主设备连接到 AXI 系统总线 HDMA,作为主设备连接到 AHB 外设总线
- 当 XDMA 的 destination 为 DRAM 时,如果 burst 大于等于 16,那 destsize 必须为 64bit。
- DMAMUX 的输出 0∼7 连接到外设总线 DMA 控制器 HDMA,DMAMUX 的输出 8∼15 连接到系统总线 DMA 控制器 XDMA
- 它们都连接在统一的 DMAMUX(DMA 多路复用器)
- DMAMUX将所有外设的 DMA 请求(Request)统一管理,然后根据你的配置分配给 HDMA 或 XDMA 的任意空闲通道
- 5 注意cfg文件和一些前置的工程配置文件,可能导致编译出错 运行出错 仿真不了 重点:链接文件、yaml、板级cfg文件
- */
- uint64_t delta_time;
- #define PI (3.1415926f)
- void start_time(void)
- {
- delta_time = hpm_csr_get_core_mcycle();
- }
- uint32_t get_end_time(void)
- {
- delta_time = hpm_csr_get_core_mcycle() - delta_time;
- return delta_time;
- }
- float theta ;
- float sin_theta;
- static void test_hard(void)
- {
- // v8m_yy_led_test();
- // v8m_yy_motor_pwm_test();
- // v8m_yy_adc_test();
- // timer0_test();
- // cpu_delay_test();
- // timer1_test();
- // can2_test();
- // imu_uart3_test();
- // uart2_sbus_test();
- // system_test();
- // sbus_uart2_out_test();
- test_gd25q16_quick();
- gd25q16_cs_high();
- while(1)
- {
- theta += PI*0.1;
- sin_theta = hpm_dsp_sin_f32(theta);
-
- board_delay_ms(200);
- printf("sin theta is %f\r\n", sin_theta);
- }
-
- }
- int main(void)
- {
- board_init();
- float i = 9.8f;
-
- sin_theta = hpm_dsp_sin_f32(theta);
-
-
- printf("hello world %f\n", i);
- printf("sin theta is %f\r\n", sin_theta);
- test_hard();
- return 0;
- }
- // DMA 最大4k
- #endif
- #if 1
- /*
- * Copyright (c) 2021 HPMicro
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
- #include "board.h"
- #include "hpm_sdmmc_sd.h"
- #include "ff.h"
- #include "diskio.h"
- FATFS s_sd_disk;
- FIL s_file;
- DIR s_dir;
- FRESULT fatfs_result;
- BYTE work[FF_MAX_SS];
- const TCHAR driver_num_buf[3] = { DEV_SD + '0', ':', '/' };
- #define TEST_DIR_NAME "hpmicro_sd_test_dir0"
- void show_menu(void);
- const char *show_error_string(FRESULT fresult);
- static FRESULT sd_mount_fs(void);
- static FRESULT sd_mkfs(void);
- static FRESULT sd_write_file(void);
- static FRESULT sd_read_file(void);
- static FRESULT sd_dir_test(void);
- static FRESULT sd_big_file_test(void);
- int main(void)
- {
- bool need_init_filesystem = true;
- board_init();
- show_menu();
- while (1) {
- char option = getchar();
- /* Before doing FATFS operation, ensure the SD card is present */
- DSTATUS dstatus = disk_status(DEV_SD);
- printf("sd state is %d\r\n", dstatus); // 找到SD卡
- if (dstatus == STA_NODISK) {
- printf("No disk in the SD slot, please insert an SD card...\n");
- do {
- dstatus = disk_status(DEV_SD);
- } while (dstatus == STA_NODISK);
- board_delay_ms(100);
- printf("Detected SD card, re-initialize the filesystem...\n");
- need_init_filesystem = true;
- }
- dstatus = disk_initialize(DEV_SD);
- if (dstatus != RES_OK) {
- printf("Failed to initialize SD disk\n");
- }
- if (need_init_filesystem) {
- fatfs_result = sd_mount_fs();
- if (fatfs_result == FR_NO_FILESYSTEM) {
- printf("There is no File system available, making file system...\n");
- fatfs_result = sd_mkfs();
- if (fatfs_result != FR_OK) {
- printf("Failed to make filesystem, cause:%s\n", show_error_string(fatfs_result));
- } else {
- need_init_filesystem = false;
- }
- }
- }
- switch (option) {
- case '1':
- fatfs_result = sd_mkfs();
- break;
- case '2':
- fatfs_result = sd_write_file();
- break;
- case '3':
- fatfs_result = sd_read_file();
- break;
- case '4':
- fatfs_result = sd_dir_test();
- break;
- case 's':
- fatfs_result = sd_big_file_test();
- break;
- default:
- show_menu();
- break;
- }
- }
- }
- void show_menu(void)
- {
- const char menu_str[] = "SD FATFS demo\n-----------------------------------\n"
- "1 - Format the SD card with FATFS\n"
- "2 - Create hello.txt\n"
- "3 - Read 1st line from hello.txt\n"
- "4 - Directory related test\n"
- "s - Large file write test\n"
- "Others - Show menu\n";
- printf(menu_str);
- }
- static FRESULT sd_mount_fs(void)
- {
- FRESULT fresult = f_mount(&s_sd_disk, driver_num_buf, 1);
- if (fresult == FR_OK) {
- printf("SD card has been mounted successfully\n");
- } else {
- printf("Failed to mount SD card, cause: %s\n", show_error_string(fresult));
- }
- fresult = f_chdrive(driver_num_buf);
- return fresult;
- }
- static FRESULT sd_mkfs(void)
- {
- printf("Formatting the SD card, depending on the SD card capacity, the formatting process may take a long time\n");
- FRESULT fresult = f_mkfs(driver_num_buf, NULL, work, sizeof(work));
- if (fresult != FR_OK) {
- printf("Making File system failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Making file system is successful\n");
- }
- return fresult;
- }
- static FRESULT sd_write_file(void)
- {
- FRESULT fresult = f_open(&s_file, "readme.txt", FA_WRITE | FA_CREATE_ALWAYS);
- if (fresult != FR_OK) {
- printf("Create new file failed, cause: %d\n", show_error_string(fresult));
- } else {
- printf("Create new file successfully, status=%d\n", fresult);
- }
- char hello_str[] = "Hello, this is SD card FATFS demo\n";
- UINT byte_written;
- fresult = f_write(&s_file, hello_str, sizeof(hello_str), &byte_written);
- if (fresult != FR_OK) {
- printf("Write file failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Write file operation is successfully\n");
- }
- f_close(&s_file);
- return fresult;
- }
- static FRESULT sd_read_file(void)
- {
- FRESULT fresult = f_open(&s_file, "readme.txt", FA_READ);
- if (fresult != FR_OK) {
- printf("Open file failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Open file successfully\n");
- }
- if (fresult != FR_OK) {
- return fresult;
- }
- TCHAR str[100];
- f_gets(str, sizeof(str), &s_file);
- printf("%s\n", str);
- f_close(&s_file);
- return fresult;
- }
- static FRESULT sd_big_file_test(void)
- {
- FRESULT fresult = f_open(&s_file, "big_file.bin", FA_WRITE | FA_CREATE_ALWAYS);
- if (fresult != FR_OK) {
- printf("Create new file failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Create new file successfully\n");
- }
- uint32_t write_size = 1024UL * 1024UL * 100UL;
- static uint8_t buf[32768];
- for (uint32_t i = 0; i < sizeof(buf); i++) {
- buf[i] = i & 0xFF;
- }
- while (write_size > 0) {
- UINT byte_written;
- fresult = f_write(&s_file, buf, sizeof(buf), &byte_written);
- if (fresult != FR_OK) {
- printf("Write file failed, cause: %s\n", show_error_string(fresult));
- return fresult;
- }
- write_size -= byte_written;
- }
- printf("Write file operation is successful\n");
- f_close(&s_file);
- return fresult;
- }
- static FRESULT sd_dir_test(void)
- {
- FRESULT fresult = f_mkdir(TEST_DIR_NAME);
- if (fresult != FR_OK) {
- printf("Creating new directory failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Creating new directory succeeded\n");
- }
- fresult = f_rmdir(TEST_DIR_NAME);
- if (fresult != FR_OK) {
- printf("Removing new directory failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Removing new directory succeeded\n");
- }
- return fresult;
- }
- const char *show_error_string(FRESULT fresult)
- {
- const char *result_str;
- switch (fresult) {
- case FR_OK:
- result_str = "succeeded";
- break;
- case FR_DISK_ERR:
- result_str = "A hard error occurred in the low level disk I/O level";
- break;
- case FR_INT_ERR:
- result_str = "Assertion failed";
- break;
- case FR_NOT_READY:
- result_str = "The physical drive cannot work";
- break;
- case FR_NO_FILE:
- result_str = "Could not find the file";
- break;
- case FR_NO_PATH:
- result_str = "Could not find the path";
- break;
- case FR_INVALID_NAME:
- result_str = "Tha path name format is invalid";
- break;
- case FR_DENIED:
- result_str = "Access denied due to prohibited access or directory full";
- break;
- case FR_EXIST:
- result_str = "Access denied due to prohibited access";
- break;
- case FR_INVALID_OBJECT:
- result_str = "The file/directory object is invalid";
- break;
- case FR_WRITE_PROTECTED:
- result_str = "The physical drive is write protected";
- break;
- case FR_INVALID_DRIVE:
- result_str = "The logical driver number is invalid";
- break;
- case FR_NOT_ENABLED:
- result_str = "The volume has no work area";
- break;
- case FR_NO_FILESYSTEM:
- result_str = "There is no valid FAT volume";
- break;
- case FR_MKFS_ABORTED:
- result_str = "THe f_mkfs() aborted due to any problem";
- break;
- case FR_TIMEOUT:
- result_str = "Could not get a grant to access the volume within defined period";
- break;
- case FR_LOCKED:
- result_str = "The operation is rejected according to the file sharing policy";
- break;
- case FR_NOT_ENOUGH_CORE:
- result_str = "LFN working buffer could not be allocated";
- break;
- case FR_TOO_MANY_OPEN_FILES:
- result_str = "Number of open files > FF_FS_LOCK";
- break;
- case FR_INVALID_PARAMETER:
- result_str = "Given parameter is invalid";
- break;
- default:
- result_str = "Unknown error";
- break;
- }
- return result_str;
- }
- #endif
|