大家好,我希望有一些指导,我目前正在尝试通过 SPI 与外部 SD 读卡器通信,安装了4GB 卡,在这种情况下 SDHC 会适用(我认为这是正确的)。
我已经下载了所需的 FATF32库、并将其包含在我的代码中、同时还下载了适用于大多数电路板的第三方 MSP430库、并为 SPI 配置正确选择了引脚 I/O。 Im 在执行某些 FATF 功能时感到很棘手,并想知道是否有人可以帮助我:)。
我在下面链接了我的代码、之后我上传了我遇到的错误。
使用 IAR Embedded Workbench IDE
#include "msp430.h" #include "msp430_spi.h" // Include your SPI library #include "ff.h" // Include FatFs library for SD card access #include "diskio.h" // Include low-level disk I/O functions #include "userconfig.h" #include "stdio.h" #define BUFFER_SIZE 512 // Define buffer size for reading data from SD card #define SPI_CS BIT3 // Chip select pin #define CMD0 0x40 #define CMD8 0x48 #define ACMD41 (41 | 0x80) #define R1_IDLE_STATE 0x01 #define CMD58 0x3A // Function prototypes uint8_t send_command(uint8_t cmd, uint32_t arg); uint8_t read_response(uint8_t *response, uint8_t num_bytes); // Function prototypes void sd_card_init(); int main(void) { FRESULT result; // FatFs function result FATFS fs; // File system object FIL file; // File object BYTE buffer[BUFFER_SIZE]; // Data buffer for reading from file // Disable watchdog timer WDTCTL = WDTPW + WDTHOLD; // Initialize MSP430 SPI spi_init(); // Initialize SD card sd_card_init(); // Mount the SD card result = f_mount(&fs, "", 0); if (result != FR_OK) { // Error handling return 1; } // Open the text file for reading result = f_open(&file, "Test.txt", FA_READ); if (result != FR_OK) { // Error handling return 1; } // Read data from the file UINT bytesRead; result = f_read(&file, buffer, BUFFER_SIZE, &bytesRead); if (result != FR_OK) { // Error handling f_close(&file); return 1; } // Close the file f_close(&file); // Process the data read from the file // For example, you can print it to the console int i; for (i = 0; i < bytesRead; i++) { putchar(buffer[i]); // Assuming UART is used for output } // End of program return 0; } // Initialize SD card void sd_card_init() { // Chip select low to select SD card P1OUT &= ~SPI_CS; // Delay for at least 74 clock cycles (about 1 ms) __delay_cycles(1000); // Send CMD0 (GO_IDLE_STATE) command send_command(CMD0, 0); // Send CMD8 (SEND_IF_COND) command send_command(CMD8, 0x000001AA); // CMD8 with argument 0x1AA (voltage range 2.7-3.6V) // Read R7 response to CMD8 uint8_t response[5]; if (read_response(response, 5) != 0) { // Error handling return; } // Verify that SD card is version 2.x and voltage range is supported if ((response[2] != 0x01) || (response[3] != 0xAA)) { // Error handling return; } // Send ACMD41 (SD_SEND_OP_COND) command until card is ready // (loop until card returns R1_IDLE_STATE indicating initialization complete) while (send_command(ACMD41, 0) != R1_IDLE_STATE); // Send CMD58 (READ_OCR) command to read OCR register send_command(CMD58, 0); // Read R3 response to CMD58 if (read_response(response, 5) != 0) { // Error handling return; } // Verify SDHC or SDXC card (check CCS bit in OCR) if (response[1] & 0x40) { // SDHC or SDXC card detected } else { // Standard capacity SD card detected } // Chip select high to deselect SD card P1OUT |= SPI_CS; }
Error[e46]: Undefined external "f_read" referred in main (C:\Users\*****\Documents\SPI_Code\Debug\Obj\main.r43).
对于所有函数'f_open f_close f_read 等、都会发生此错误。