Skip to content

Commit

Permalink
Enabled printf()
Browse files Browse the repository at this point in the history
  • Loading branch information
crsz20 committed Jun 14, 2024
1 parent 1790b5f commit 56e8700
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 5 deletions.
14 changes: 12 additions & 2 deletions Firmware/Shifter_System/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_board.393059222" name="Board" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_board" useByScannerDiscovery="false" value="NUCLEO-F446RE" valueType="string"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.defaults.556346971" name="Defaults" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.defaults" useByScannerDiscovery="false" value="com.st.stm32cube.ide.common.services.build.inputs.revA.1.0.6 || Debug || true || Executable || com.st.stm32cube.ide.mcu.gnu.managedbuild.option.toolchain.value.workspace || NUCLEO-F446RE || 0 || 0 || arm-none-eabi- || ${gnu_tools_for_stm32_compiler_path} || ../Core/Inc | ../Drivers/STM32F4xx_HAL_Driver/Inc | ../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy | ../Drivers/CMSIS/Device/ST/STM32F4xx/Include | ../Drivers/CMSIS/Include || || || USE_HAL_DRIVER | STM32F446xx || || Drivers | Core/Startup | Core || || || ${workspace_loc:/${ProjName}/STM32F446RETX_FLASH.ld} || true || NonSecure || || secure_nsclib.o || || None || || || " valueType="string"/>
<option id="com.st.stm32cube.ide.mcu.debug.option.cpuclock.1821070149" name="Cpu clock frequence" superClass="com.st.stm32cube.ide.mcu.debug.option.cpuclock" useByScannerDiscovery="false" value="84" valueType="string"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.nanoprintffloat.385811399" name="Use float with printf from newlib-nano (-u _printf_float)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.nanoprintffloat" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.targetplatform.336488663" isAbstract="false" osList="all" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.targetplatform"/>
<builder buildPath="${workspace_loc:/Shifter_System}/Debug" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.builder.589651570" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" parallelBuildOn="true" parallelizationNumber="optimal" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.builder"/>
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler.448493510" name="MCU GCC Assembler" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler">
Expand Down Expand Up @@ -87,10 +88,19 @@
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.objcopy.symbolsrec.1431819164" name="MCU Output Converter Motorola S-rec with symbols" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.objcopy.symbolsrec"/>
</toolChain>
</folderInfo>
<fileInfo id="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.285203902.1302876059" name="syscalls.c" rcbsApplicability="disable" resourcePath="Core/Src/syscalls.c" toolsToInvoke="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.2124145833.2145290782">
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.2124145833.2145290782" name="MCU GCC Compiler" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.2124145833"/>
<tool customBuildStep="true" id="org.eclipse.cdt.managedbuilder.ui.rcbs.719623083" name="Resource Custom Build Step">
<inputType id="org.eclipse.cdt.managedbuilder.ui.rcbs.inputtype.1440616304" name="Resource Custom Build Step Input Type">
<additionalInput kind="additionalinputdependency" paths=""/>
</inputType>
<outputType id="org.eclipse.cdt.managedbuilder.ui.rcbs.outputtype.294120858" name="Resource Custom Build Step Output Type"/>
</tool>
</fileInfo>
<sourceEntries>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Core"/>
<entry excluding="Src/syscalls.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Core"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Drivers"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="Program"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Program"/>
</sourceEntries>
</configuration>
</storageModule>
Expand Down
32 changes: 32 additions & 0 deletions Firmware/Shifter_System/Core/Inc/retarget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// All credit to Carmine Noviello for this code
// https://github.com/cnoviello/mastering-stm32/blob/master/nucleo-f030R8/system/include/retarget/retarget.h

#ifndef _RETARGET_H__
#define _RETARGET_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f4xx_hal.h"
#include <sys/stat.h>

void RetargetInit(UART_HandleTypeDef *huart);
int _isatty(int fd);
int _write(int fd, char* ptr, int len);
int _close(int fd);
int _lseek(int fd, int ptr, int dir);
int _read(int fd, char* ptr, int len);
int _fstat(int fd, struct stat* st);

// These three functions must be implemented when enabling printf with foating point values
int _kill(int pid, int sig);
int _getpid();
void _exit(int);

#ifdef __cplusplus
}
#endif


#endif //#ifndef _RETARGET_H__
1 change: 1 addition & 0 deletions Firmware/Shifter_System/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "../../Program/Src/app.hpp"
#include "retarget.h"

/* USER CODE END Includes */

Expand Down
100 changes: 100 additions & 0 deletions Firmware/Shifter_System/Core/Src/retarget.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// All credit to Carmine Noviello for this code
// https://github.com/cnoviello/mastering-stm32/blob/master/nucleo-f030R8/system/src/retarget/retarget.c

#include <_ansi.h>
#include <_syslist.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/times.h>
#include <limits.h>
#include <signal.h>
#include <../Inc/retarget.h>
#include <stdint.h>
#include <stdio.h>

#if !defined(OS_USE_SEMIHOSTING)

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2

UART_HandleTypeDef *gHuart;

void RetargetInit(UART_HandleTypeDef *huart) {
gHuart = huart;

/* Disable I/O buffering for STDOUT stream, so that
* chars are sent out as soon as they are printed. */
setvbuf(stdout, NULL, _IONBF, 0);
}

int _isatty(int fd) {
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
return 1;

errno = EBADF;
return 0;
}

int _write(int fd, char* ptr, int len) {
HAL_StatusTypeDef hstatus;

if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
hstatus = HAL_UART_Transmit(gHuart, (uint8_t *) ptr, len, HAL_MAX_DELAY);
if (hstatus == HAL_OK)
return len;
else
return EIO;
}
errno = EBADF;
return -1;
}

int _close(int fd) {
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
return 0;

errno = EBADF;
return -1;
}

int _lseek(int fd, int ptr, int dir) {
(void) fd;
(void) ptr;
(void) dir;

errno = EBADF;
return -1;
}

int _read(int fd, char* ptr, int len) {
HAL_StatusTypeDef hstatus;

if (fd == STDIN_FILENO) {
hstatus = HAL_UART_Receive(gHuart, (uint8_t *) ptr, 1, HAL_MAX_DELAY);
if (hstatus == HAL_OK)
return 1;
else
return EIO;
}
errno = EBADF;
return -1;
}

int _fstat(int fd, struct stat* st) {
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
st->st_mode = S_IFCHR;
return 0;
}

errno = EBADF;
return 0;
}


int _kill(int pid, int sig) { return 0; }
int _getpid() { return 0; }
// Added a forever loop to supress warning: "the 'noreturn' function does return"
void _exit(int) { for(;;) { } }

#endif //#if !defined(OS_USE_SEMIHOSTING)
9 changes: 6 additions & 3 deletions Firmware/Shifter_System/Program/Src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
// ST HAL Dependencies
#include "gpio.h"

// 3rd Party Libraryes and Frameworks
#include "usart.h"
extern UART_HandleTypeDef huart2;


// DFR Custom Dependencies

#include "../../Core/Inc/retarget.h"


void cppMain() {

// Enable `printf()` using USART
RetargetInit(&huart2);


for(;;){
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
printf("hi\n");
HAL_Delay(1000);
}
}
Expand Down

0 comments on commit 56e8700

Please sign in to comment.