How to use FatFs library in MPLAB XC8 without MPLAB Code Configurater?

Home Forums MPLAB XC8 Tutorial Discussions How to use FatFs library in MPLAB XC8 without MPLAB Code Configurater?

Viewing 15 reply threads
  • Author
    Posts
    • #2499
      Karalan
      Participant

      I want to use FatFs library in MPLAB XC8 without MCC because I’m using PIC18F4620 which is not a MCC supported device. I downloaded the FatFs library and added it in project. Now how do I link my controller’s SPI module to FatFs library (diskio.h and diskio.c)????

    • #2501
      Bitahwa Bindu
      Keymaster

      You can use PIC18F peripheral libraries SPI functions. You need to modify the diskio.h file to use the Peripheral library functions instead of MCC functions like the openSPI and writeSPI functions.

      Please read on this article at the bottom we explained how to use PIC18F peripheral libraries functions: SPI Communication with PIC

    • #2502
      Bitahwa Bindu
      Keymaster

      You can change these functions in diskio.h if you are using PIC18F Peripheral Libraries:

      #define sd_init() SPI1_Initialize() with OpenSPI()

      #define sd_open() SPI1_Open()

      #define sd_tx(d) SPI1_Exchange8bit(d) with WriteSPI()

      #define sd_rx() SPI1_Exchange8bit(0xFF) with ReadSPI()

      What XC8 compiler version are you using?

       

    • #2519
      Karalan
      Participant

      Am using XC8 compiler version 1.42 and I tried with your suggestion but still I couldn’t. I got following 2 error with lot of warning:

       

      CLEAN SUCCESSFUL (total time: 53ms)
      make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
      make[1]: Entering directory ‘C:/Users/CEERI/MPLABXProjects/SD_Card.X’
      make -f nbproject/Makefile-default.mk dist/default/production/SD_Card.X.production.hex
      make[2]: Entering directory ‘C:/Users/CEERI/MPLABXProjects/SD_Card.X’
      “C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe” –pass1 –chip=18F4620 -Q -G –double=24 –float=24 –emi=wordwrite –opt=+asm,+asmfile,-speed,+space,-debug,-local –addrqual=ignore –mode=free -P -N255 –warn=-3 –asmlist -DXPRJ_default=default –summary=default,-psect,-class,+mem,-hex,-file –output=default,-inhx032 –runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib –output=-mcof,+elf:multilocs –stack=compiled:auto:auto:auto “–errformat=%f:%l: error: (%n) %s” “–warnformat=%f:%l: warning: (%n) %s” “–msgformat=%f:%l: advisory: (%n) %s” -obuild/default/production/ffsystem.p1 ffsystem.c
      “C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe” –pass1 –chip=18F4620 -Q -G –double=24 –float=24 –emi=wordwrite –opt=+asm,+asmfile,-speed,+space,-debug,-local –addrqual=ignore –mode=free -P -N255 –warn=-3 –asmlist -DXPRJ_default=default –summary=default,-psect,-class,+mem,-hex,-file –output=default,-inhx032 –runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib –output=-mcof,+elf:multilocs –stack=compiled:auto:auto:auto “–errformat=%f:%l: error: (%n) %s” “–warnformat=%f:%l: warning: (%n) %s” “–msgformat=%f:%l: advisory: (%n) %s” -obuild/default/production/diskio.p1 diskio.c
      “C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe” –pass1 –chip=18F4620 -Q -G –double=24 –float=24 –emi=wordwrite –opt=+asm,+asmfile,-speed,+space,-debug,-local –addrqual=ignore –mode=free -P -N255 –warn=-3 –asmlist -DXPRJ_default=default –summary=default,-psect,-class,+mem,-hex,-file –output=default,-inhx032 –runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib –output=-mcof,+elf:multilocs –stack=compiled:auto:auto:auto “–errformat=%f:%l: error: (%n) %s” “–warnformat=%f:%l: warning: (%n) %s” “–msgformat=%f:%l: advisory: (%n) %s” -obuild/default/production/ff.p1 ff.c
      “C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe” –pass1 –chip=18F4620 -Q -G –double=24 –float=24 –emi=wordwrite –opt=+asm,+asmfile,-speed,+space,-debug,-local –addrqual=ignore –mode=free -P -N255 –warn=-3 –asmlist -DXPRJ_default=default –summary=default,-psect,-class,+mem,-hex,-file –output=default,-inhx032 –runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib –output=-mcof,+elf:multilocs –stack=compiled:auto:auto:auto “–errformat=%f:%l: error: (%n) %s” “–warnformat=%f:%l: warning: (%n) %s” “–msgformat=%f:%l: advisory: (%n) %s” -obuild/default/production/newmain.p1 newmain.c
      “C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe” –pass1 –chip=18F4620 -Q -G –double=24 –float=24 –emi=wordwrite –opt=+asm,+asmfile,-speed,+space,-debug,-local –addrqual=ignore –mode=free -P -N255 –warn=-3 –asmlist -DXPRJ_default=default –summary=default,-psect,-class,+mem,-hex,-file –output=default,-inhx032 –runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib –output=-mcof,+elf:multilocs –stack=compiled:auto:auto:auto “–errformat=%f:%l: error: (%n) %s” “–warnformat=%f:%l: warning: (%n) %s” “–msgformat=%f:%l: advisory: (%n) %s” -obuild/default/production/ffunicode.p1 ffunicode.c
      ffsystem.c:44: warning: (179) nested comments
      diskio.c:31: warning: (361) function declared implicit int
      diskio.c:38: warning: (361) function declared implicit int
      diskio.c:45: warning: (361) function declared implicit int
      diskio.c:69: warning: (361) function declared implicit int
      diskio.c:76: warning: (361) function declared implicit int
      diskio.c:83: warning: (361) function declared implicit int
      diskio.c:112: warning: (361) function declared implicit int
      diskio.c:121: warning: (361) function declared implicit int
      diskio.c:130: warning: (361) function declared implicit int
      diskio.c:160: warning: (361) function declared implicit int
      diskio.c:169: warning: (361) function declared implicit int
      diskio.c:178: warning: (361) function declared implicit int
      ff.c:877: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:880: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:943: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:1002: warning: (373) implicit signed to unsigned conversion
      ff.c:1008: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:1013: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:1076: warning: (373) implicit signed to unsigned conversion
      ff.c:1081: warning: (373) implicit signed to unsigned conversion
      ff.c:1099: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:1508: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:2197: warning: (373) implicit signed to unsigned conversion
      ff.c:2304: warning: (373) implicit signed to unsigned conversion
      ff.c:2564: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:2565: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:2846: warning: (373) implicit signed to unsigned conversion
      ff.c:2934: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3019: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3060: warning: (373) implicit signed to unsigned conversion
      ff.c:3101: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3107: warning: (373) implicit signed to unsigned conversion
      ff.c:3163: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3165: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3166: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3176: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3179: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3180: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3182: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3202: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3204: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3220: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3224: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3225: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3226: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3229: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3232: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3324: warning: (373) implicit signed to unsigned conversion
      ff.c:3342: warning: (373) implicit signed to unsigned conversion
      ff.c:3436: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3498: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3499: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3623: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3707: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:3789: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:4183: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:4235: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:4487: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:4490: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:4543: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:4607: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      ff.c:4814: warning: (359) illegal conversion between pointer types
      pointer to unsigned char -> pointer to const unsigned char
      “C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe” –chip=18F4620 -G -mdist/default/production/SD_Card.X.production.map –double=24 –float=24 –emi=wordwrite –opt=+asm,+asmfile,-speed,+space,-debug,-local –addrqual=ignore –mode=free -P -N255 –warn=-3 –asmlist -DXPRJ_default=default –summary=default,-psect,-class,+mem,-hex,-file –output=default,-inhx032 –runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib –output=-mcof,+elf:multilocs –stack=compiled:auto:auto:auto “–errformat=%f:%l: error: (%n) %s” “–warnformat=%f:%l: warning: (%n) %s” “–msgformat=%f:%l: advisory: (%n) %s” –memorysummary dist/default/production/memoryfile.xml -odist/default/production/SD_Card.X.production.elf build/default/production/newmain.p1 build/default/production/diskio.p1 build/default/production/ff.p1 build/default/production/ffsystem.p1 build/default/production/ffunicode.p1
      Microchip MPLAB XC8 C Compiler (Free Mode) V1.42
      Build date: Apr 12 2017
      Part Support Version: 1.42
      Copyright (C) 2017 Microchip Technology Inc.
      License type: Node Configuration

      :: advisory: (1233) Employing 18F4620 errata work-arounds:
      :: advisory: (1234) * Corrupted fast interrupt shadow registers
      :: advisory: (1234) * Data in RAM location can be corrupted if async. reset occurs during write
      :: warning: (1273) Omniscient Code Generation not available in Free mode
      newmain.c:17: error: (1250) could not find space (559 bytes) for variable _FatFs
      newmain.c:18: error: (1250) could not find space (548 bytes) for variable _Fil
      (908) exit status = 1
      nbproject/Makefile-default.mk:189: recipe for target ‘dist/default/production/SD_Card.X.production.hex’ failed
      make[2]: Leaving directory ‘C:/Users/CEERI/MPLABXProjects/SD_Card.X’
      nbproject/Makefile-default.mk:84: recipe for target ‘.build-conf’ failed
      make[1]: Leaving directory ‘C:/Users/CEERI/MPLABXProjects/SD_Card.X’
      nbproject/Makefile-impl.mk:39: recipe for target ‘.build-impl’ failed
      make[2]: *** [dist/default/production/SD_Card.X.production.hex] Error 1
      make[1]: *** [.build-conf] Error 2
      make: *** [.build-impl] Error 2

      BUILD FAILED (exit value 2, total time: 1s)

    • #2520
      Karalan
      Participant

      Am using XC8 1.42 and I tried with your suggestion but still I couldn’t. I got following two errors with lots of warning:

      :: advisory: (1233) Employing 18F4620 errata work-arounds:
      :: advisory: (1234) * Corrupted fast interrupt shadow registers
      :: advisory: (1234) * Data in RAM location can be corrupted if async. reset occurs during write
      :: warning: (1273) Omniscient Code Generation not available in Free mode
      newmain.c:17: error: (1250) could not find space (559 bytes) for variable _FatFs
      newmain.c:18: error: (1250) could not find space (548 bytes) for variable _Fil
      (908) exit status = 1
      nbproject/Makefile-default.mk:189: recipe for target ‘dist/default/production/SD_Card.X.production.hex’ failed
      make[2]: Leaving directory ‘C:/Users/CEERI/MPLABXProjects/SD_Card.X’
      nbproject/Makefile-default.mk:84: recipe for target ‘.build-conf’ failed
      make[1]: Leaving directory ‘C:/Users/CEERI/MPLABXProjects/SD_Card.X’
      nbproject/Makefile-impl.mk:39: recipe for target ‘.build-impl’ failed
      make[2]: *** [dist/default/production/SD_Card.X.production.hex] Error 1
      make[1]: *** [.build-conf] Error 2
      make: *** [.build-impl] Error 2

    • #2524
      Bitahwa Bindu
      Keymaster

      Hi. What suggestion did you make? Library or direct access of registers?

      Did you use PIC18F Peripheral Libraries? Did you install those libraries separately? Because XC8 compilers form version 1.35 are no longer included, you must download and install them separately. They are now called Legacy Peripheral Libraries.

    • #2525
      Bitahwa Bindu
      Keymaster

      We have resolved the caching issue. Try to comment again.

    • #2526
      Karalan
      Participant

      Yup I installed Peripheral Libraries and I have made following changes:

      #define sd_init() OpenSPI()

      #define sd_open() SPI1_Open()

      #define sd_tx(d) Write_SPI(d)

      #define sd_rx() Read_SPI()

      and my main.c file is:

      #include “newxc8_header.h”
      #include “ff.h”

      void main(void) {

      ADCON1=0x0F;
      OSCCON=0x76;
      // Open the file “test.txt” and Write “Hello world!”
      FATFS FatFs; // FatFs work area needed for each volume
      FIL Fil; // File object needed for each open file
      UINT bw;
      if (f_mount(&FatFs, “”, 1) == FR_OK) { // Mount SD
      if (f_open(&Fil, “test.txt”, FA_OPEN_ALWAYS | FA_READ | FA_WRITE) == FR_OK) { // Open or create a file
      f_write(&Fil, “Hello world!\r\n”, 14, &bw); // Write data to test.txt file
      }
      }
      }

      • #2528
        Bitahwa Bindu
        Keymaster

        Can you upload your code. We will compile it on our side and see.

    • #2527
      Karalan
      Participant

      Thank you very much for your quick response 🙂

    • #2529
      Karalan
      Participant

      I just want to write and read in SD Card. Now I trying with the code which is example code in your tutorial.

      main.c file:

      #include “newxc8_header.h”
      #include “ff.h”

      void main(void) {

      ADCON1=0x0F;
      OSCCON=0x76;
      // Open the file “test.txt” and Write “Hello world!”
      FATFS FatFs; // FatFs work area needed for each volume
      FIL Fil; // File object needed for each open file
      UINT bw;
      if (f_mount(&FatFs, “”, 1) == FR_OK) { // Mount SD
      if (f_open(&Fil, “test.txt”, FA_OPEN_ALWAYS | FA_READ | FA_WRITE) == FR_OK) { // Open or create a file
      f_write(&Fil, “Hello world!\r\n”, 14, &bw); // Write data to test.txt file
      }
      }
      }

      • #2530
        Bitahwa Bindu
        Keymaster

        We asked you to upload your code as an attachment in a comment so that we can compile it and see the errors you are getting.

      • #2531
        Karalan
        Participant

        There is no attachment option here 🙁 How can I?

      • #2533
        Bitahwa Bindu
        Keymaster

        Below there an option to Choose file.

      • #2540
        Karalan
        Participant

        Here is the Code. Thank you very much 🙂

        Attachments:
        You must be logged in to view attached files.
      • #2542
        Bitahwa Bindu
        Keymaster

        In your code we don’t see where you are defining the SPI functions like OpenSPI(), there are parameters you need to specify like frequency, slave or master etc. You must sonfigure the pins used as well for SPI communication: SDI, SDO, CS etc.

        All these were defined in  spi1.h, OpenSPI.h and pin_manager.h when using MCC. You must do this using PIC18F Peripheral Libraries as we explained in this article: SPI Functions with PIC18F Peripheral Libraries

    • #2543
      Karalan
      Participant

      After configure that SPI module also I got same error.  Here is my new code:

      #include “newxc8_header.h”
      #include “ff.h”
      #include “spi.h”
      #define USE_OR_MASKS
      #include <p18cxxx.h>

      void main(void) {

      ADCON1=0x0F;
      OSCCON=0x76;

      CloseSPI();

      OpenSPI(SPI_FOSC_4,MODE_01,SMPMID );

       

      FATFS FatFs; // FatFs work area needed for each volume
      FIL Fil; // File object needed for each open file
      UINT bw;
      if (f_mount(&FatFs, “”, 1) == FR_OK) {
      if (f_open(&Fil, “test.txt”, FA_OPEN_ALWAYS | FA_READ | FA_WRITE) == FR_OK) {
      f_write(&Fil, “Hello world!\r\n”, 14, &bw);
      }
      }

      CloseSPI();

      while(1);
      }

       

      Error:

      make[2]: *** [build/default/production/newmain.p1] Error 1
      make[2]: *** Waiting for unfinished jobs….
      make[1]: *** [.build-conf] Error 2
      make: *** [.build-impl] Error 2
      make[2]: Leaving directory ‘C:/Users/CEERI/MPLABXProjects/SD_Card.X’
      nbproject/Makefile-default.mk:84: recipe for target ‘.build-conf’ failed
      make[1]: Leaving directory ‘C:/Users/CEERI/MPLABXProjects/SD_Card.X’
      nbproject/Makefile-impl.mk:39: recipe for target ‘.build-impl’ failed

      BUILD FAILED (exit value 2, total time: 794ms)

       

      Attachments:
      You must be logged in to view attached files.
    • #2547
      Karalan
      Participant

      Sorry, by mistake I have attached same file 2 times. See any one. Thank you

    • #2552
      Karalan
      Participant

      Is it working??

      • #2553
        Bitahwa Bindu
        Keymaster

        We will look at it again when we have time maybe tomorrow.

         

      • #2555
        Karalan
        Participant

        Thank you very much 🙂

    • #2579
      Karalan
      Participant

      Sorry to ask you again but I need to interface SD Card as soon as possible. If you are busy, tell me the suggestions i’ll do it.

      • #2580
        Bitahwa Bindu
        Keymaster

        Hi. We found some few problems in your code:

        1. The error the compiler is complaining about it’s this: newmain.c:11: error: (141) can’t open include file “spi.h”: No such file or directory. You have this line of code in your project: #include “spi.h”  but you don’t have this “spi.h”  file in your project. You can’t include a file unless you have created/added that file.
        2. There is no need to have this  #include <p18cxxx.h> in your main code as in your “newxc8_header.h” you have included already:  #include <xc.h>. With XC8 compiler #include <xc.h> is sufficient to include the PIC
      • #2581
        Karalan
        Participant

        But I already installed the peripheral library which has “spi.h”

      • #2582
        Bitahwa Bindu
        Keymaster

        Specify the location if it gives you errors: #include <plib/spi.h>

    • #2583
      Karalan
      Participant

      still i couldn’t. But “can’t find SPI. . . ” error has gone.

    • #2585
      Karalan
      Participant

      C:\Program Files (x86)\Microchip\xc8\v1.42\include\plib/spi.h:32: error: (141) can’t open include file “pconfig.h”: No such file or directory

       

      make[1]: *** [.build-conf] Error 2

      • #2610
        Bitahwa Bindu
        Keymaster

        The quickest way is to try to use MCC as with our project with a supported device such as the PIC18F46K20 or even PIC18F45K22 then you can use our code and only change the instances relevent to your PIC18F4620 such as the configuration bits if different.

        Read this thread: The PIC18F45K22  code adjusted to work with PIC18F4550.

Viewing 15 reply threads
  • You must be logged in to reply to this topic.
Select your currency
EUR Euro
USD United States (US) dollar