I have to read a file 100kB big using FatFs in a microcontroller to send it via an AT TCP-IP modem. An obvious method would be doing f_open() calls, but I am curious how to use the f_forward function. The example code looks like this:

/* Sample code of data transfer function to be called back from f_forward */

UINT out_stream (   /* Returns number of bytes sent or stream status */
    const BYTE *p,  /* Pointer to the data block to be sent */
    UINT btf        /* >0: Transfer call (Number of bytes to be sent). 0: Sense call */
    UINT cnt = 0;

    if (btf == 0) {     /* Sense call */
        /* Return stream status (0: Busy, 1: Ready) */
        /* When once it returned ready to sense call, it must accept a byte at least */
        /* at subsequent transfer call, or f_forward will fail with FR_INT_ERR. */
        if (FIFO_READY) cnt = 1;
    else {              /* Transfer call */
    do {    /* Repeat while there is any data to be sent and the stream is ready */
            FIFO_PORT = *p++;
        } while (cnt < btf && FIFO_READY);

    return cnt;

/* Sample code using f_forward function                                   */

FRESULT play_file (
    char *fn        /* Pointer to the audio file name to be played */
    FRESULT rc;
    FIL fil;
    UINT dmy;

    /* Open the audio file in read only mode */
    rc = f_open(&fil, fn, FA_READ);
    if (rc) return rc;

    /* Repeat until the file pointer reaches end of the file */
    while (rc == FR_OK && !f_eof(&fil)) {

        /* some processes... */

        /* Fill output stream periodicaly or on-demand */
        rc = f_forward(&fil, out_stream, 1000, &dmy);

    /* Close the file and return */
    return rc;

I cant understand how to use this example. What is FIFO_READY and FIFO_PORT ? How do I extract the data I need and print it using printf(..)? Could somebody explain in more detail how to use this example ?

  • \$\begingroup\$ Your teacher could not help you? Obviously this device has a hardware based FiFO for both transmit and receive, so you will need to get the datasheet to that device. \$\endgroup\$ – Sparky256 Feb 6 at 4:14
  • \$\begingroup\$ I do not have a teacher (¿?). The MCU is a atxmega128A4u. I think I know understand, the FIFO device could be, for example the UART. Is this correct ? \$\endgroup\$ – Tomás Arturo Herrera Castro Feb 6 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.