Page 1 of 3
FatFS, F_Write and integer
Posted: Tue Jan 08, 2013 9:05 pm
by Ramazzukk
Hello there
I know its a fairly minor challenge for you guys, but how do I write an integer with f_write? The code underneath can not work, so far as I know, the integer has to be castet to byte or char. But how do I do that in a simple way? Its just basics, but it would be much appreciated if someone could point out some directional advice!
Code: Select all
uint16_t adc1_avg;
rc=f_write (&Fil , adc1_avg, sizeof(adc1_avg), &bw);
Re: FatFS, F_Write and integer
Posted: Tue Jan 08, 2013 9:20 pm
by Tectu
Personally, I'd use something like sprintf() but this needs a hell mass of memory.
You can use smaller functions like itoa to cast an integer. You can find many different implementations of this function by using google. If you only have an unsigned integer, here's how you can do it:
https://github.com/Tectu/ChibiOS-GFX/bl ... main.c#L33~ Tectu
Re: FatFS, F_Write and integer
Posted: Tue Jan 08, 2013 9:38 pm
by Giovanni
IMO you have to pass to f_write() the pointer to your integer, not the integer itself.
Giovanni
Re: FatFS, F_Write and integer
Posted: Wed Jan 09, 2013 10:07 am
by Ramazzukk
@Tectu : Thanks for the excellent sample code. I already tried with sprintf and got errors, as well other code I've found with google. Yours works like a charm! Next step: float!
@Giovanni: I tried that as well, but got only rubbish on the SD-Card. I think it has to be a char-var, otherwise it wouldn't work..
Thanks again for the help!
Re: FatFS, F_Write and integer
Posted: Wed Jan 09, 2013 10:14 am
by Tectu
I am quite sure that it does not work with just passing the pointer to your integer. You're supposed to just get garbage

What for errors do you get with sprintf? Do you get errors about missing stubs? You have to implement these syscalls. Afaik ChibiOS/RT brings some of them with it out of the box, I always use the two files attached. Using sprintf should allow to use floats.
~ Tectu
Re: FatFS, F_Write and integer
Posted: Wed Jan 09, 2013 10:57 am
by Ramazzukk
Thank you very much for the file provided. I included it, but if I use sprintf, I get following Error:
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\thumb\v7m\newlib\libc\reent/../../../../../../../newlib-1.20.0/newlib/libc/reent/sbrkr.c:58: undefined reference to `_sbrk'
May it be the same problem as here :
http://embdev.net/topic/177530 ?
I am not an Pro-Coder, so if I encounter such problems, I try to get the result with a workaround like you kindly have provided (If goolge doesn't help). But if you know a solution about sprintf, please let me know! It seems, the problem a complex Linker-Error. I'm using Eclipse/Yagarto/GCC.
BTW, is there any method how to chprintf() to a float USART like this:
Code: Select all
chprintf((BaseChannel *)&SD2, "%d", adc1_sample);
Unfortunately, theres a note:
Code: Select all
@note Floating point types are not implemented, this function is meant
* as a system utility and not a full implementation
Re: FatFS, F_Write and integer
Posted: Wed Jan 09, 2013 11:12 am
by Tectu
The error you get should disappear when you include the two files properly. I didn't use it myself for months. I can do some testing tonight but I can tell that this worked for 100% sure a few months ago.
chprintf() dosen't come with float support. It would simply blow up the memory footprint. There's a thread about this:
viewtopic.php?f=3&t=298~ Tectu
Re: FatFS, F_Write and integer
Posted: Wed Jan 09, 2013 11:21 am
by Ramazzukk
I already saw the thread mentioned. If I'm able to convert float to char, I can work around that restriction.
As for the include: I included the as follow:
Code: Select all
#include "ch.h"
#include "hal.h"
#include "shell.h"
#include "chprintf.h"
#include <ff.h>
#include <string.h>
#include "mad.h"
#include "stubs.h"
I am searching for a basic routine like yours to convert the stuff.. But with sprintf it would be much easier, of course
Thanks so far for your support! Greetings form Bern!
Re: FatFS, F_Write and integer
Posted: Wed Jan 09, 2013 11:27 am
by Tectu
You don't need to include the stub.h file manually, but did you add the stub.c to the CSRC in your Makefile?
Greetings from the Wallis

~ Tectu
Re: FatFS, F_Write and integer
Posted: Wed Jan 09, 2013 11:47 am
by Ramazzukk
Ahh, now I have (

):
Code: Select all
CSRC = $(PORTSRC) \
$(KERNSRC) \
$(HALSRC) \
$(PLATFORMSRC) \
$(BOARDSRC) \
$(FATFSSRC) \
$(STFSRC) \
$(MP3SRC) \
$(CHIBIOS)/os/various/chprintf.c \
main.c \
stubs.c
Still, the same error. I just want to convert values like 1.943 to a char.. Why is it so hard

.. I try to implement a basic routine like yours for converting int to char now..
Anyhow, here is the full dump of the console (just to complete all information):
Code: Select all
11:39:48 **** Incremental Build of configuration Default for project stm32audio ****
make -j all
arm-none-eabi-gcc -c -mcpu=cortex-m4 -O2 -ggdb -fomit-frame-pointer -falign-functions=16 -ffunction-sections -fdata-sections -fno-common -Wall -Wextra -Wstrict-prototypes -Wa,-alms=build/lst/main.lst -DHAVE_CONFIG_H -DFPM_ARM -DCORTEX_USE_FPU=FALSE -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -MD -MP -MF .dep/main.o.d -mthumb -DTHUMB -I. -I../../os/ports/common/ARMCMx/CMSIS/include -I../../os/ports/common/ARMCMx -I../../os/ports/GCC/ARMCMx -I../../os/ports/GCC/ARMCMx/STM32F4xx -I../../os/kernel/include -I./libmad/ -I../../ext/fatfs/src -I../../os/hal/include -I../../os/hal/platforms/STM32F4xx -I../../os/hal/platforms/STM32 -I../../os/hal/platforms/STM32/GPIOv2 -I../../boards/ST_STM32F4_DISCOVERY -I../../os/various main.c -o build/obj/main.o
main.c: In function 'uitoa':
main.c:97:5: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
main.c:115:5: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
main.c: In function 'getadc':
main.c:214:7: warning: unused variable 'fstr' [-Wunused-variable]
main.c:212:8: warning: variable 'floatval' set but not used [-Wunused-but-set-variable]
In file included from main.c:270:0:
mad.c: In function 'input':
mad.c:71:5: warning: implicit declaration of function 'EVAL_AUDIO_VolumeCtl' [-Wimplicit-function-declaration]
mad.c:96:11: warning: unused variable 'fr' [-Wunused-variable]
mad.c: In function 'header':
mad.c:113:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
mad.c:119:3: warning: implicit declaration of function 'Codec_AudioInterface_Init' [-Wimplicit-function-declaration]
mad.c: In function 'output':
mad.c:202:3: warning: implicit declaration of function 'Audio_MAL_Play' [-Wimplicit-function-declaration]
mad.c:175:7: warning: unused variable 'i' [-Wunused-variable]
mad.c:174:13: warning: variable 'write_buf' set but not used [-Wunused-but-set-variable]
mad.c:166:28: warning: unused parameter 'data' [-Wunused-parameter]
mad.c:167:33: warning: unused parameter 'header' [-Wunused-parameter]
mad.c: In function 'error':
mad.c:217:25: warning: unused parameter 'frame' [-Wunused-parameter]
mad.c: In function 'decode':
mad.c:242:14: warning: unused variable 'i' [-Wunused-variable]
main.c: In function 'Mp3PlayAllFiles':
main.c:292:3: warning: implicit declaration of function 'sprintf' [-Wimplicit-function-declaration]
main.c:292:3: warning: incompatible implicit declaration of built-in function 'sprintf' [enabled by default]
main.c: In function 'main':
main.c:576:11: warning: variable 'rc' set but not used [-Wunused-but-set-variable]
arm-none-eabi-gcc build/obj/crt0.o build/obj/vectors.o build/obj/chcore.o build/obj/chcore_v7m.o build/obj/nvic.o build/obj/chsys.o build/obj/chdebug.o build/obj/chlists.o build/obj/chvt.o build/obj/chschd.o build/obj/chthreads.o build/obj/chdynamic.o build/obj/chregistry.o build/obj/chsem.o build/obj/chmtx.o build/obj/chcond.o build/obj/chevents.o build/obj/chmsg.o build/obj/chmboxes.o build/obj/chqueues.o build/obj/chmemcore.o build/obj/chheap.o build/obj/chmempools.o build/obj/hal.o build/obj/adc.o build/obj/can.o build/obj/ext.o build/obj/gpt.o build/obj/i2c.o build/obj/icu.o build/obj/mac.o build/obj/pal.o build/obj/pwm.o build/obj/sdc.o build/obj/serial.o build/obj/spi.o build/obj/uart.o build/obj/usb.o build/obj/tm.o build/obj/mmc_spi.o build/obj/serial_usb.o build/obj/rtc.o build/obj/stm32_dma.o build/obj/hal_lld.o build/obj/adc_lld.o build/obj/can_lld.o build/obj/ext_lld.o build/obj/gpt_lld.o build/obj/i2c_lld.o build/obj/icu_lld.o build/obj/mac_lld.o build/obj/pwm_lld.o build/obj/serial_lld.o build/obj/spi_lld.o build/obj/uart_lld.o build/obj/pal_lld.o build/obj/board.o build/obj/diskio.o build/obj/ff.o build/obj/syscall.o build/obj/stm32f4_discovery_audio_codec.o build/obj/bit.o build/obj/decoder.o build/obj/fixed.o build/obj/frame.o build/obj/huffman.o build/obj/layer12.o build/obj/layer3.o build/obj/stream.o build/obj/synth.o build/obj/timer.o build/obj/version.o build/obj/chprintf.o build/obj/main.o build/obj/stubs.o -mcpu=cortex-m4 -nostartfiles -T./STM32F407xG.ld -Wl,-Map=build/ch.map,--cref,--no-warn-mismatch,--gc-sections -mno-thumb-interwork -mthumb -o build/ch.elf
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/lib/thumb/v7m\libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\thumb\v7m\newlib\libc\reent/../../../../../../../newlib-1.20.0/newlib/libc/reent/sbrkr.c:58: undefined reference to `_sbrk'
collect2.exe: error: ld returned 1 exit status
make: *** [build/ch.elf] Error 1
11:39:52 Build Finished (took 4s.165ms)