Taksiepacze
Taksiepacze

Reputation: 29

Undefined external "f_open" reffered in main , FATF library not recognised?

Hi all im trying to set up an SPI interface between an MSP430 FR4133 and an external SD Card Reader. I am coming across these errors

Error[e46]: Undefined external "f_open" referred in main (C:\Users\****\Documents\SPI_Code\Debug\Obj\main.r43).

However i am unsure as to why these are being undefined as i have included all the FatF libraries needed within my workspace on IAR and have included them in my main, i know this is a simple problem but im struggling to figure out why these errors are occuring.

Here is my implementation of the functions and the headers.

#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;
}

Upvotes: 2

Views: 56

Answers (0)

Related Questions