[{"data":1,"prerenderedAt":153},["ShallowReactive",2],{"post-\u002Fblog\u002F2020\u002Fstm32-freertos-and-printf-with-floats":3},{"id":4,"title":5,"body":6,"categories":138,"date":143,"description":46,"extension":144,"image":145,"meta":146,"navigation":148,"path":149,"seo":150,"stem":151,"__hash__":152},"blog\u002Fblog\u002F2020\u002Fstm32-freertos-and-printf-with-floats.md","STM32 FreeRTOS and printf",{"type":7,"value":8,"toc":134},"minimark",[9,38,41,48,51,61,70,73,112,115,120],[10,11,12,13,20,23,24,29,31,32,37],"p",{},"After some more coding, I found some more issues with FreeRTOS and printf, not being solved by my fix below. If you need to get it fixed completely, look at that forums post: ",[14,15,19],"a",{"href":16,"rel":17},"https:\u002F\u002Fcommunity.st.com\u002Fs\u002Fquestion\u002F0D50X0000BB1eL7SQJ\u002Fbug-cubemx-freertos-projects-corrupt-memory",[18],"nofollow","ST Community",[21,22],"br",{},"\nand the website of Dave Nadler: ",[14,25,28],{"href":26,"rel":27},"http:\u002F\u002Fwww.nadler.com\u002Fembedded\u002FnewlibAndFreeRTOS.html",[18],"newlib and FreeRTOS",[21,30],{},"\nIn my current project, I replaced the newlib-nano-printf implementation by adding ",[14,33,36],{"href":34,"rel":35},"https:\u002F\u002Fgithub.com\u002Fmpaland\u002Fprintf",[18],"github:mpaland\u002Fprintf"," as a git submodule to my project and including the printf.h (it overwrites the printf-library function with macro defines) in my topmost header file.",[10,39,40],{},"This will be a very short post. If you experience hard faults when using printf (this happens mostly, when using floats) and you already ticked the appropriate settings in the project's properties...",[10,42,43],{},[44,45],"img",{"alt":46,"src":47},"","\u002Fimages\u002Fblog\u002F2020\u002F06\u002Fimage-5.png",[10,49,50],{},"...don't waste your time digging through assembler instructions with instruction stepping (like I did) just to realize, that memory management is broken when using FreeRTOS. It is simply a bug in CubeMX-generated source files. Locate your _sbrk-function (either in syscalls.c or in sysmem.c) and change it to the following:",[52,53,58],"pre",{"className":54,"code":56,"language":57},[55],"language-text","caddr_t _sbrk(int incr)\n{\n  extern char end asm(\"end\");\n  static char *heap_end;\n  char *prev_heap_end,*min_stack_ptr;\n  if (heap_end == 0) \n    heap_end = &end; prev_heap_end = heap_end; \n  \u002F* Use the NVIC offset register to locate the main stack pointer. *\u002F\n  \u002F* Locate the STACK bottom address *\u002F\n  min_stack_ptr = (char*)(*(unsigned int *)*(unsigned int *)0xE000ED08);\n  min_stack_ptr -= MAX_STACK_SIZE; \n  if (heap_end + incr > min_stack_ptr) {\n    errno = ENOMEM;\n    return (caddr_t) -1;\n  }\n  heap_end += incr; \n  return (caddr_t) prev_heap_end;\n}\n","text",[59,60,56],"code",{"__ignoreMap":46},[10,62,63,64,69],{},"For what _sbrk does, have a look ",[14,65,68],{"href":66,"rel":67},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FSbrk",[18],"here",".",[10,71,72],{},"If you want to digg a bit deeper, here are some websites dealing with this problem:",[74,75,76,83,89,95,101,107],"ul",{},[77,78,79],"li",{},[14,80,81],{"href":81,"rel":82},"https:\u002F\u002Fwww.openstm32.org\u002Fforumthread353#threadId354",[18],[77,84,85],{},[14,86,87],{"href":87,"rel":88},"https:\u002F\u002Fwww.openstm32.org\u002Fforumthread7695",[18],[77,90,91],{},[14,92,93],{"href":93,"rel":94},"https:\u002F\u002Fbitbucket.org\u002Farminoonk\u002Fprintf-float-issue\u002Fsrc\u002Fmaster\u002F",[18],[77,96,97],{},[14,98,99],{"href":99,"rel":100},"https:\u002F\u002Fcommunity.st.com\u002Fs\u002Fquestion\u002F0D50X00009XkW7j\u002Fhardfault-from-printf",[18],[77,102,103],{},[14,104,105],{"href":105,"rel":106},"https:\u002F\u002Fcommunity.st.com\u002Fs\u002Fquestion\u002F0D50X00009sUA8S\u002Fwhy-is-printf-not-working-with-floating-point-on-the-stm32f407vg-iar-8301",[18],[77,108,109],{},[14,110,26],{"href":26,"rel":111},[18],[113,114],"hr",{},[116,117,119],"h2",{"id":118},"kommentare-comments","Kommentare \u002F Comments",[10,121,122,123,128,129,69],{},"Hast du Fragen oder Anmerkungen zu diesem Artikel? ",[14,124,127],{"href":125,"rel":126},"https:\u002F\u002Fgithub.com\u002Fthe78mole-blog\u002Fthe78mole-blog.github.io\u002Fissues\u002Fnew?title=Kommentar+zu%3A+stm32-freertos-and-printf-with-floats&labels=comment",[18],"Erstelle ein GitHub Issue"," oder starte eine ",[14,130,133],{"href":131,"rel":132},"https:\u002F\u002Fgithub.com\u002Fthe78mole-blog\u002Fthe78mole-blog.github.io\u002Fdiscussions",[18],"Diskussion",{"title":46,"searchDepth":135,"depth":135,"links":136},2,[137],{"id":118,"depth":135,"text":119},[139,140,141,142],"ARM","FreeRTOS","STM32","Uncategorized","2020-06-03","md",null,{"tags":147},[],true,"\u002Fblog\u002F2020\u002Fstm32-freertos-and-printf-with-floats",{"title":5,"description":46},"blog\u002F2020\u002Fstm32-freertos-and-printf-with-floats","kRxNHldpnhMn4rlrM-_PPTl9Jo9kTqg1l-McJfuF2vQ",1777286694215]