From 29add8a8806f1780af6aaf80f33587e558ebe13d Mon Sep 17 00:00:00 2001
From: Andrew Tridgell <andrew@tridgell.net>
Date: Sat, 3 Feb 2018 09:02:01 +1100
Subject: [PATCH] HAL_ChibiOS: fixed build without FATFS enabled

---
 libraries/AP_HAL_ChibiOS/hwdef/common/chibios_board.mk | 5 +++--
 libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c         | 2 ++
 libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c          | 2 ++
 libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h          | 2 ++
 4 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/chibios_board.mk b/libraries/AP_HAL_ChibiOS/hwdef/common/chibios_board.mk
index ec91d0a5df..50589311c2 100644
--- a/libraries/AP_HAL_ChibiOS/hwdef/common/chibios_board.mk
+++ b/libraries/AP_HAL_ChibiOS/hwdef/common/chibios_board.mk
@@ -51,6 +51,7 @@ endif
 
 ifeq ($(USE_FATFS),)
   USE_FATFS = yes
+  FATFS_FLAGS=-DUSE_FATFS
 endif
 
 #
@@ -102,9 +103,9 @@ include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk
 # Other files (optional).
 #include $(CHIBIOS)/test/rt/test.mk
 include $(CHIBIOS)/os/hal/lib/streams/streams.mk
-include $(CHIBIOS)/os/various/cpp_wrappers/chcpp.mk
 
 ifeq ($(USE_FATFS),yes)
+include $(CHIBIOS)/os/various/cpp_wrappers/chcpp.mk
 include $(CHIBIOS)/os/various/fatfs_bindings/fatfs.mk
 endif
 
@@ -218,7 +219,7 @@ CPPWARN = -Wall -Wextra -Wundef
 #
 
 # List all user C define here, like -D_DEBUG=1
-UDEFS =
+UDEFS = $(FATFS_FLAGS)
 
 # Define ASM defines here
 UADEFS =
diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c b/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c
index 9433ed86b9..ae692e41e5 100644
--- a/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c
+++ b/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c
@@ -52,6 +52,8 @@ void *malloc_ccm(size_t size)
     if (p != NULL) {
         memset(p, 0, size);
     }
+#else
+    (void)size;
 #endif
     return p;
 }
diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c b/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c
index 0abc5c1293..55ec54116c 100644
--- a/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c
+++ b/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.c
@@ -227,6 +227,7 @@ atob(uint32_t *vp, char *p, int base)
 }
 
 
+#if HAL_OS_FATFS_IO
 /*
  *  vsscanf(buf,fmt,ap)
  */
@@ -343,3 +344,4 @@ static int vfscanf (FILE *stream, const char *fmt, va_list ap)
     count = vsscanf (buf, fmt, ap);
     return (count);
 }
+#endif // HAL_OS_FATFS_IO
diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h b/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h
index 352fcd8123..5664d03fcd 100644
--- a/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h
+++ b/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h
@@ -27,7 +27,9 @@ int vasprintf(char **strp, const char *fmt, va_list ap);
 int asprintf(char **strp, const char *fmt, ...);
 int vprintf(const char *fmt, va_list arg);
 int printf(const char *fmt, ...);
+#if defined(USE_FATFS) || (defined(HAL_OS_FATFS_IO) && HAL_OS_FATFS_IO)
 int fscanf ( FILE * stream, const char * format, ... );
+#endif
 
 int scanf (const char *fmt, ...);
 int sscanf (const char *buf, const char *fmt, ...);