diff --git a/DecaRanging.coproj b/DecaRanging.coproj index 93b3496942b327f5c87e6dfdcad9e89a136d8fea..daa3c74b5cbfcb8478f604ae95264c2a0407799e 100644 --- a/DecaRanging.coproj +++ b/DecaRanging.coproj @@ -68,25 +68,28 @@ <DebugOption> <Option name="org.coocox.codebugger.gdbjtag.core.adapter" value="ST-Link"/> <Option name="org.coocox.codebugger.gdbjtag.core.debugMode" value="SWD"/> - <Option name="org.coocox.codebugger.gdbjtag.core.clockDiv" value="1M"/> + <Option name="org.coocox.codebugger.gdbjtag.core.clockDiv" value="2M"/> <Option name="org.coocox.codebugger.gdbjtag.corerunToMain" value="1"/> <Option name="org.coocox.codebugger.gdbjtag.core.jlinkgdbserver" value=""/> <Option name="org.coocox.codebugger.gdbjtag.core.userDefineGDBScript" value=""/> <Option name="org.coocox.codebugger.gdbjtag.core.targetEndianess" value="0"/> <Option name="org.coocox.codebugger.gdbjtag.core.jlinkResetMode" value="Type 0: Normal"/> <Option name="org.coocox.codebugger.gdbjtag.core.resetMode" value="SYSRESETREQ"/> - <Option name="org.coocox.codebugger.gdbjtag.core.ifSemihost" value="0"/> + <Option name="org.coocox.codebugger.gdbjtag.core.ifSemihost" value="1"/> <Option name="org.coocox.codebugger.gdbjtag.core.ifCacheRom" value="1"/> <Option name="org.coocox.codebugger.gdbjtag.core.ipAddress" value="127.0.0.1"/> <Option name="org.coocox.codebugger.gdbjtag.core.portNumber" value="2009"/> <Option name="org.coocox.codebugger.gdbjtag.core.autoDownload" value="1"/> <Option name="org.coocox.codebugger.gdbjtag.core.verify" value="1"/> <Option name="org.coocox.codebugger.gdbjtag.core.downloadFuction" value="Erase Effected"/> - <Option name="org.coocox.codebugger.gdbjtag.core.defaultAlgorithm" value="STM32F10x_CL_256.elf"/> + <Option name="org.coocox.codebugger.gdbjtag.core.defaultAlgorithm" value="./stm32f10x_cl_256.elf"/> </DebugOption> <ExcludeFile/> </Target> - <Components path="./"/> + <Components path="./"> + <Component id="31" name="Retarget printf" path="" type="2"/> + <Component id="33" name="Semihosting" path="" type="2"/> + </Components> <Files> <File name="Libraries/STM32_USB_Device_Library/Core/inc/usbd_ioreq.h" path="Libraries/STM32_USB_Device_Library/Core/inc/usbd_ioreq.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_fsmc.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_fsmc.h" type="1"/> @@ -104,20 +107,21 @@ <File name="src/platform/deca_spi.h" path="src/platform/deca_spi.h" type="1"/> <File name="Libraries/STM32_USB_Device_Library/Core/src/usbd_ioreq.c" path="Libraries/STM32_USB_Device_Library/Core/src/usbd_ioreq.c" type="1"/> <File name="Libraries/STM32_USB_Device_Library/Class/cdc/src/usbd_cdc_core.c" path="Libraries/STM32_USB_Device_Library/Class/cdc/src/usbd_cdc_core.c" type="1"/> - <File name="src/usb/usbd_desc.h" path="src/usb/usbd_desc.h" type="1"/> <File name="Libraries/STM32_USB_Device_Library/Class/cdc/inc/usbd_cdc_core.h" path="Libraries/STM32_USB_Device_Library/Class/cdc/inc/usbd_cdc_core.h" type="1"/> + <File name="src/usb/usbd_desc.h" path="src/usb/usbd_desc.h" type="1"/> <File name="src/platform/deca_spi.c" path="src/platform/deca_spi.c" type="1"/> <File name="src/usb" path="" type="2"/> <File name="src/decadriver/deca_version.h" path="src/decadriver/deca_version.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c" type="1"/> - <File name="src/application/instance.c" path="src/application/instance.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_wwdg.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_wwdg.c" type="1"/> + <File name="src/application/instance.c" path="src/application/instance.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_dbgmcu.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_dbgmcu.h" type="1"/> - <File name="src/decadriver/deca_device_api.h" path="src/decadriver/deca_device_api.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_sdio.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_sdio.c" type="1"/> + <File name="src/decadriver/deca_device_api.h" path="src/decadriver/deca_device_api.h" type="1"/> <File name="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x" path="" type="2"/> - <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_bkp.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_bkp.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c" type="1"/> + <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_bkp.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_bkp.h" type="1"/> + <File name="stdio/printf.c" path="stdio/printf.c" type="1"/> <File name="src/compiler/compiler.h" path="src/compiler/compiler.h" type="1"/> <File name="Libraries/STM32_USB_OTG_Driver" path="" type="2"/> <File name="src/platform/deca_mutex.c" path="src/platform/deca_mutex.c" type="1"/> @@ -126,17 +130,18 @@ <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_dma.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_dma.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_crc.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_crc.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_rcc.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_rcc.h" type="1"/> + <File name="stdio" path="" type="2"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_can.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_can.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_sdio.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_sdio.h" type="1"/> <File name="Libraries/STM32_USB_OTG_Driver/src/usb_hcd_int.c" path="Libraries/STM32_USB_OTG_Driver/src/usb_hcd_int.c" type="1"/> <File name="src/platform/port.c" path="src/platform/port.c" type="1"/> - <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_wwdg.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_wwdg.h" type="1"/> <File name="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.h" path="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.h" type="1"/> + <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_wwdg.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_wwdg.h" type="1"/> <File name="src/decadriver/deca_device.c" path="src/decadriver/deca_device.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c" type="1"/> + <File name="Libraries/STM32_USB_Device_Library/Core/inc/usbd_req.h" path="Libraries/STM32_USB_Device_Library/Core/inc/usbd_req.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_adc.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_adc.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rtc.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rtc.c" type="1"/> - <File name="Libraries/STM32_USB_Device_Library/Core/inc/usbd_req.h" path="Libraries/STM32_USB_Device_Library/Core/inc/usbd_req.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_fsmc.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_fsmc.c" type="1"/> <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_otg.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_otg.h" type="1"/> <File name="src/usb/usb_conf.h" path="src/usb/usb_conf.h" type="1"/> @@ -169,10 +174,12 @@ <File name="src/decadriver/deca_param_types.h" path="src/decadriver/deca_param_types.h" type="1"/> <File name="src/usb/deca_usb_bsp_evk1000.c" path="src/usb/deca_usb_bsp_evk1000.c" type="1"/> <File name="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/startup_stm32f10x_cl.S" path="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/startup_stm32f10x_cl.S" type="1"/> - <File name="src/platform/stm32f10x_conf.h" path="src/platform/stm32f10x_conf.h" type="1"/> + <File name="semihosting/sh_cmd.s" path="semihosting/sh_cmd.s" type="1"/> <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_bsp.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_bsp.h" type="1"/> + <File name="src/platform/stm32f10x_conf.h" path="src/platform/stm32f10x_conf.h" type="1"/> <File name="Libraries/CMSIS/CM3" path="" type="2"/> <File name="Libraries" path="" type="2"/> + <File name="semihosting/semihosting.c" path="semihosting/semihosting.c" type="1"/> <File name="src/decadriver/deca_types.h" path="src/decadriver/deca_types.h" type="1"/> <File name="src/usb/deca_usb.h" path="src/usb/deca_usb.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc" path="" type="2"/> @@ -198,6 +205,7 @@ <File name="Libraries/STM32_USB_OTG_Driver/inc" path="" type="2"/> <File name="Libraries/STM32_USB_Device_Library/Core/inc/usbd_core.h" path="Libraries/STM32_USB_Device_Library/Core/inc/usbd_core.h" type="1"/> <File name="Libraries/CMSIS/CM3/DeviceSupport" path="" type="2"/> + <File name="semihosting" path="" type="2"/> <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_dcd_int.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_dcd_int.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dac.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dac.c" type="1"/> <File name="Libraries/CMSIS/CM3/CoreSupport/core_cm3.h" path="Libraries/CMSIS/CM3/CoreSupport/core_cm3.h" type="1"/> @@ -210,8 +218,8 @@ <File name="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup" path="" type="2"/> <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_core.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_core.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_bkp.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_bkp.c" type="1"/> - <File name="src/application/dw_main.c" path="src/application/dw_main.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c" type="1"/> + <File name="src/application/dw_main.c" path="src/application/dw_main.c" type="1"/> <File name="src/platform" path="" type="2"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_tim.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_tim.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_i2c.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_i2c.h" type="1"/> @@ -221,6 +229,7 @@ <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dbgmcu.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dbgmcu.c" type="1"/> <File name="src/decadriver/deca_regs.h" path="src/decadriver/deca_regs.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_pwr.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_pwr.h" type="1"/> + <File name="semihosting/semihosting.h" path="semihosting/semihosting.h" type="1"/> <File name="src/usb/usbd_usr.c" path="src/usb/usbd_usr.c" type="1"/> <File name="src/application/instance.h" path="src/application/instance.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_gpio.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_gpio.h" type="1"/> diff --git a/semihosting/semihosting.c b/semihosting/semihosting.c new file mode 100644 index 0000000000000000000000000000000000000000..cb161561277d41f49f5e93e569413869f36632d8 --- /dev/null +++ b/semihosting/semihosting.c @@ -0,0 +1,72 @@ +/** + ****************************************************************************** + * @file semihosting.c + * @author Coocox + * @version V1.0 + * @date 09/10/2011 + * @brief Semihosting LowLayer GetChar/SendChar Implement. + * + ******************************************************************************* + */ + +#include <sys/types.h> +#include <sys/stat.h> +#include "semihosting.h" + +static char g_buf[16]; +static char g_buf_len = 0; + +/**************************************************************************//** + * @brief Transmit a char on semihosting mode. + * + * @param ch is the char that to send. + * + * @return Character to write. + *****************************************************************************/ +void SH_SendChar(int ch) { + g_buf[g_buf_len++] = ch; + g_buf[g_buf_len] = '\0'; + if (g_buf_len + 1 >= sizeof(g_buf) || ch == '\n' || ch == '\0') { + g_buf_len = 0; + /* Send the char */ + if (SH_DoCommand(0x04, (int) g_buf, NULL) != 0) { + return; + } + } +} + +/**************************************************************************//** + * @brief Transmit a null-terminated string on semihosting mode. + * + * @param str is the string that to send. + * + * @return Character to write. + *****************************************************************************/ +void SH_SendString(const char *str) +{ + int j; + if (SH_DoCommand(0x04, (int)str, NULL) != 0) { + return; + } +} + +/**************************************************************************//** + * @brief Read a char on semihosting mode. + * + * @param None. + * + * @return Character that have read. + *****************************************************************************/ +char SH_GetChar() { + int nRet; + + while (SH_DoCommand(0x101, 0, &nRet) != 0) { + if (nRet != 0) { + SH_DoCommand(0x07, 0, &nRet); + return (char) nRet; + } + } + + return 0; +} + diff --git a/semihosting/semihosting.h b/semihosting/semihosting.h new file mode 100644 index 0000000000000000000000000000000000000000..caa55d1f8c30ba9b68ed5340130b3c6be20a9434 --- /dev/null +++ b/semihosting/semihosting.h @@ -0,0 +1,25 @@ +/** + ****************************************************************************** + * @file semihosting.h + * @author Coocox + * @version V1.0 + * @date 09/10/2011 + * @brief Semihosting Low Layer GetChar/SendChar API. + * + ******************************************************************************* + */ +#ifndef __SIMIHOSTTING_IMPL +#define __SIMIHOSTTING_IMPL + +/********************************************************************************************************//** + * Semihosting functions prototype +************************************************************************************************************/ +extern int SH_DoCommand(int n32In_R0, int n32In_R1, int *pn32Out_R0); +void SH_SendChar(int ch); +void SH_SendString(const char *str); +char SH_GetChar(void); + + +#endif + + diff --git a/semihosting/sh_cmd.s b/semihosting/sh_cmd.s new file mode 100644 index 0000000000000000000000000000000000000000..f82b129933d6e4f8363c527fe798927a6497d105 --- /dev/null +++ b/semihosting/sh_cmd.s @@ -0,0 +1,77 @@ +/** + ****************************************************************************** + * @file semihost_cmd.s + * @author Coocox + * @version V1.0 + * @date 09/10/2011 + * @brief Semihost command support. + * + ******************************************************************************* + */ + + +.text +.global SH_DoCommand +//.global HardFault_Handler +.code 16 +.syntax unified +.type SH_DoCommand, function +//.type HardFault_Handler, function + + +/**************************************************************************//** + * @brief prototype: int SH_DoCommand(int n32In_R0, int n32In_R1, int *pn32Out_R0) + * + * @param n32In_R0 R0 + * @param n32In_R1 R1 + * @param pn32Out_R0 R2 + * + * @retval None + *****************************************************************************/ +SH_DoCommand: + BKPT 0xAB /* Wait ICE or HardFault */ + /* ICE will step over BKPT directly */ + /* HardFault will step BKPT and the next line */ + + B SH_ICE + +SH_HardFault: /* Captured by HardFault */ + MOVS R0, #0 /* Set return value to 0 */ + BX LR /* Return */ + +SH_ICE: /* Captured by ICE */ + /* Save return value */ + CMP R2, #0 + BEQ SH_End + STR R0, [R2] /* Save the return value to *pn32Out_R0 */ +SH_End: + MOVS R0, #1 /* Set return value to 1 */ + BX LR /* Return */ + +/**************************************************************************//** + * @brief HardFault Handler + * + * @param None. + * + * Skip the semihost command in free run mode. + * + * @retval None + *****************************************************************************/ +HardFault_Handler: + LDR R0, [R13, #24] /* Get previous PC */ + LDRH R1, [R0] /* Get instruction */ + LDR R2, =0xBEAB /* The sepcial BKPT instruction */ + CMP R1, R2 /* Test if the instruction at previous PC is BKPT */ + BNE HardFault_Handler_Ret /* Not BKPT */ + + ADDS R0, #4 /* Skip BKPT and next line */ + STR R0, [R13, #24] /* Save previous PC */ + + BX LR + +HardFault_Handler_Ret: + B . + + +.end + diff --git a/src/application/dw_main.c b/src/application/dw_main.c index e1305ebc7fa7055695ca9a903b572a15956e68ad..6e1774c753598a3936ccd592bb22d2dfd9ee6451 100644 --- a/src/application/dw_main.c +++ b/src/application/dw_main.c @@ -385,6 +385,8 @@ int dw_main(void) if(inittestapplication(s1switch) == (uint32)-1) { + + led_on(LED_ALL); //to display error.... dataseq[0] = 0x2 ; //return cursor home writetoLCD( 1, 0, &dataseq[0]); diff --git a/src/application/instance.c b/src/application/instance.c index 46bafdd5c12d27b023b40721bf2fb529924cad66..d098d0a9cf5aadcfeb366b82056710d2d1e97c5f 100644 --- a/src/application/instance.c +++ b/src/application/instance.c @@ -20,7 +20,14 @@ #include "lib.h" #include "instance.h" -// #include "deca_usb.h" +#include <stdio.h> + + + +extern void usb_run(void); +extern int usb_init(void); +extern void usb_printconfig(int, uint8*, int); +extern void send_usbmessage(uint8*, int); //NOTE: my added USB debug values/functions #define USB_DEBUG_BUFF_LEN (100) @@ -173,14 +180,15 @@ char* get_msg_fcode_string(int fcode) void send_statetousb(instance_data_t *inst) { - // usbrxdebugdata[0] = 0x2 ; //return cursor home + int usbdebugdata_size = sprintf((char*)&usbdebugdata[0], "%s , %s , %s , %s", get_inst_states_string(inst->testAppState), get_inst_states_string(inst->previousState), get_inst_states_string(inst->nextState), get_instanceModes_string(inst->mode)); - memcpy(usbdebugdata, usbdebugdata, usbdebugdata_size); - if (strncmp(usbdebugdataprev, usbdebugdata, usbdebugdata_size) != 0 || usbdebugdata_size != usbdebugdataprev_size) + // memcpy(usbdebugdata, usbdebugdata, usbdebugdata_size); + // if (strncmp(usbdebugdataprev, usbdebugdata, usbdebugdata_size) != 0 || usbdebugdata_size != usbdebugdataprev_size) + if (memcmp(usbdebugdataprev, usbdebugdata, usbdebugdata_size) != 0 || usbdebugdata_size != usbdebugdataprev_size) { - // send_usbmessage("start", 5); + // usb_run(); send_usbmessage(&usbdebugdata[0], usbdebugdata_size); usb_run(); @@ -198,8 +206,9 @@ void send_rxmsgtousb(char *data) // usbrxdebugdata[0] = 0x2 ; //return cursor home int usbrxdebugdata_size = sprintf((char*)&usbrxdebugdata[0], "%s", data); - memcpy(usbrxdebugdata, usbrxdebugdata, usbrxdebugdata_size); - if (strncmp(usbrxdebugdataprev, usbrxdebugdata, usbrxdebugdata_size) != 0 || usbrxdebugdata_size != usbrxdebugdataprev_size) + //memcpy(usbrxdebugdata, usbrxdebugdata, usbrxdebugdata_size); + // if (strncmp(usbrxdebugdataprev, usbrxdebugdata, usbrxdebugdata_size) != 0 || usbrxdebugdata_size != usbrxdebugdataprev_size) + if (memcmp(usbrxdebugdataprev, usbrxdebugdata, usbrxdebugdata_size) != 0 || usbrxdebugdata_size != usbrxdebugdataprev_size) { // n = sprintf((char*)&usbtmpmsg[0], "prev: %s curr: %s", (char*)&usbrxdebugdata, (char*)&usbrxdebugdataprev); // send_usbmessage(&usbtmpmsg[0], n); @@ -241,6 +250,8 @@ void send_txmsgtousb(char *data) int testapprun(instance_data_t *inst, int message) { int done = INST_NOT_DONE_YET; + + printf("Test 123"); // uint8 debug_msg[100]; // int n = sprintf((char*)&debug_msg[0], "Test app run"); @@ -464,7 +475,7 @@ int testapprun(instance_data_t *inst, int message) } #endif //DW1000 gone to sleep - report the received range - if(inst->tof > 0) //if ToF == 0 - then no new range to report + if(inst->tof[inst->tagToRangeWith] > 0) //if ToF == 0 - then no new range to report { if(reportTOF(inst)==0) { @@ -487,8 +498,7 @@ int testapprun(instance_data_t *inst, int message) // { send_statetousb(inst); // } - //send_txmsgtousb("BLINK"); - + int flength = (BLINK_FRAME_CRTL_AND_ADDRESS + FRAME_CRC); //blink frames with IEEE EUI-64 tag ID @@ -505,7 +515,17 @@ int testapprun(instance_data_t *inst, int message) //set the delayed rx on time (the ranging init will be sent after this delay) dwt_setrxaftertxdelay((uint32)inst->rnginitW4Rdelay_sy); //units are 1.0256us - wait for wait4respTIM before RX on (delay RX) - dwt_starttx(DWT_START_TX_IMMEDIATE | inst->wait4ack); //always using immediate TX and enable dealyed RX + int tx_stat = dwt_starttx(DWT_START_TX_IMMEDIATE | inst->wait4ack); //always using immediate TX and enable dealyed RX + + if(tx_stat == 0) + { + send_txmsgtousb("BLINK-SUCCESS"); + } + else + { + send_txmsgtousb("BLINK-ERROR"); + } + inst->goToSleep = 1; //go to Sleep after this blink inst->testAppState = TA_TX_WAIT_CONF ; // wait confirmation @@ -717,13 +737,14 @@ int testapprun(instance_data_t *inst, int message) { if(dw_event->type == DWT_SIG_RX_TIMEOUT) //got RX timeout - i.e. did not get the response (e.g. ACK) { + // printf("RX timeout in TA_TX_WAIT_CONF (%d)\n", inst->previousState); //we need to wait for SIG_TX_DONE and then process the timeout and re-send the frame if needed inst->gotTO = 1; } - done = INST_DONE_WAIT_FOR_NEXT_EVENT; send_rxmsgtousb("RX process: (1st) got TO in TA_TX_WAIT_CONF"); + done = INST_DONE_WAIT_FOR_NEXT_EVENT; break; } @@ -745,8 +766,8 @@ int testapprun(instance_data_t *inst, int message) } else if (inst->gotTO) //timeout { - //printf("got TO in TA_TX_WAIT_CONF\n"); - send_rxmsgtousb("RX process: (2nd) got TO in TA_TX_WAIT_CONF"); + // printf("got TO in TA_TX_WAIT_CONF\n"); + // send_rxmsgtousb("RX process: (2nd) got TO in TA_TX_WAIT_CONF"); inst_processrxtimeout(inst); inst->gotTO = 0; inst->wait4ack = 0 ; //clear this @@ -1147,9 +1168,9 @@ int testapprun(instance_data_t *inst, int message) inst->canPrintInfo = 2; - inst->tof = 0; + inst->tof[inst->tagToRangeWith] = 0; //copy previously calculated ToF - memcpy(&inst->tof, &(messageData[TOFR]), 5); + memcpy(&inst->tof[inst->tagToRangeWith], &(messageData[TOFR]), 5); inst->newRangeAncAddress = srcAddr[0] + ((uint16) srcAddr[1] << 8); inst->newRangeTagAddress = inst->eui64[0] + ((uint16) inst->eui64[1] << 8); @@ -1204,7 +1225,7 @@ int testapprun(instance_data_t *inst, int message) RayDa = ((double)Ra + (double)Da); //time-of-flight - inst->tof = (int64) ( RaRbxDaDb/(RbyDb + RayDa) ); + inst->tof[inst->tagToRangeWith] = (int64) ( RaRbxDaDb/(RbyDb + RayDa) ); if(reportTOF(inst) == 0) { @@ -1264,10 +1285,10 @@ int testapprun(instance_data_t *inst, int message) break ; //end of DWT_SIG_RX_OKAY case DWT_SIG_RX_TIMEOUT : - if(inst->mode == ANCHOR) - { + // if(inst->mode == ANCHOR) + // { send_rxmsgtousb("RX process: DWT_SIG_RX_TIMEOUT "); - } + // } instance_getevent(17); //get and clear this event //printf("PD_DATA_TIMEOUT %d\n", inst->previousState) ; inst_processrxtimeout(inst); @@ -1291,10 +1312,10 @@ int testapprun(instance_data_t *inst, int message) } break ; // end case TA_RX_WAIT_DATA default: - if (inst->mode == ANCHOR) - { + // if (inst->mode == ANCHOR) + // { send_statetousb(inst); - } + // } //printf("\nERROR - invalid state %d - what is going on??\n", inst->testAppState) ; break; } // end switch on testAppState diff --git a/src/application/instance.h b/src/application/instance.h index 47c71cb3f26f6c2947ba2797a9992c925cd60e40..366abe607b237353325512bb2390d7bedcb457d0 100644 --- a/src/application/instance.h +++ b/src/application/instance.h @@ -10,8 +10,8 @@ * * @author DecaWave */ -#ifndef _INSTANCE_H_ -#define _INSTANCE_H_ +#ifndef INSTANCE_H_ +#define INSTANCE_H_ #ifdef __cplusplus extern "C" { @@ -338,7 +338,7 @@ typedef struct ******************************************************************************************************************* *******************************************************************************************************************/ -#define MAX_EVENT_NUMBER (4) +#define MAX_EVENT_NUMBER (8) //NOTE: Accumulators don't need to be stored as part of the event structure as when reading them only one RX event can happen... //the receiver is singly buffered and will stop after a frame is received @@ -461,8 +461,8 @@ typedef struct //diagnostic counters/data, results and logging - int32 tof32 ; - int64 tof ; + // int32 tof32 ; + int64 tof[TAG_LIST_SIZE] ; double clockOffset ; //counts for debug diff --git a/src/application/instance_common.c b/src/application/instance_common.c index 70daa09e74f15f1dfbbf8527a3f4bf922b317c36..69915eb869eadcbd7b298dd0c2cf83786a765658 100644 --- a/src/application/instance_common.c +++ b/src/application/instance_common.c @@ -17,6 +17,11 @@ #include "instance.h" +extern void usb_run(void); +extern int usb_init(void); +extern void usb_printconfig(int, uint8*, int); +extern void send_usbmessage(uint8*, int); + // ------------------------------------------------------------------------------------------------------------------- // Data Definitions @@ -117,7 +122,7 @@ int reportTOF(instance_data_t *inst) int64 tofi ; // check for negative results and accept them making them proper negative integers - tofi = inst->tof ; // make it signed + tofi = inst->tof[inst->tagToRangeWith] ; // make it signed if (tofi > 0x007FFFFFFFFF) // MP counter is 40 bits, close up TOF may be negative { tofi -= 0x010000000000 ; // subtract fill 40 bit range to make it negative @@ -355,11 +360,6 @@ int instanceanchorwaiting(void) return instance_data[0].canPrintInfo; } -//NOTE quick hack by me -void resetinstanceanchorwaiting(void) -{ - return instance_data[0].canPrintInfo; -} int instancesleeping(void) { @@ -416,7 +416,11 @@ int instance_init(void) instance_data[instance].tofIndex = 0; instance_data[instance].tofCount = 0; - instance_data[instance].tof = 0; + for(uint8 i=0; i<TAG_LIST_SIZE; i++) + { + instance_data[instance].tof[i] = 0; + } + // Reset the IC (might be needed if not getting here from POWER ON) dwt_softreset(); @@ -753,10 +757,13 @@ double instance_get_adist(void) //get average range void inst_processrxtimeout(instance_data_t *inst) { send_statetousb(inst); + +#ifdef USB_SUPPORT //this is set in the port.h file uint8 debug_msg[100]; int n = sprintf((char*)&debug_msg[0], "inst_processrxtimeout(), tagToRangeWith: %i, taglistlen: %i", inst->tagToRangeWith, inst->tagListLen); send_usbmessage(&debug_msg[0], n); usb_run(); +#endif if(inst->mode == ANCHOR) //we did not receive the final/ACK - wait for next poll { @@ -902,7 +909,8 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) } else rxd_event = SIG_RX_UNKNOWN; - break; + + break; //ACK type frame - not supported in this SW - set as unknown (re-enable RX) case 0x02: @@ -1036,7 +1044,7 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) else { tag_index = 255; - instance_data[instance].tagToRangeWith == 255; + instance_data[instance].tagToRangeWith = 255; } } else if(rxd_event == DWT_SIG_RX_BLINK && instance_data[instance].mode == ANCHOR) @@ -1049,7 +1057,7 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) else { tag_index = 255; - instance_data[instance].tagToRangeWith == 255; + instance_data[instance].tagToRangeWith = 255; } } } @@ -1104,9 +1112,9 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) memcpy(&instance_data[instance].msg[tag_index].destAddr[0], &dw_event.msgu.frame[srcAddr_index], ADDR_BYTE_SIZE_S); //remember who to send the reply to (set destination address) #endif // Write calculated TOF into response message - memcpy(&(instance_data[instance].msg[tag_index].messageData[TOFR]), &instance_data[instance].tof, 5); + memcpy(&(instance_data[instance].msg[tag_index].messageData[TOFR]), &instance_data[instance].tof[tag_index], 5); - instance_data[instance].tof = 0; //clear ToF .. + instance_data[instance].tof[tag_index] = 0; //clear ToF .. instance_data[instance].msg[tag_index].seqNum = instance_data[instance].frameSN++; @@ -1171,8 +1179,8 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) //treat as error if(!event_placed) { - // instance_rxerrorcallback(rxd); - instancerxon(&instance_data[instance], 0, 0); //immediate enable + instance_rxerrorcallback(rxd); + //instancerxon(&instance_data[instance], 0, 0); //immediate enable } @@ -1321,6 +1329,8 @@ event_data_t* instance_getevent(int x) int instance = 0; int indexOut = instance_data[instance].dweventIdxOut; + instance_data_t idt = instance_data[instance]; + if(instance_data[instance].dwevent[indexOut].type == 0) //exit with "no event" { dw_event_g.type = 0; @@ -1376,12 +1386,21 @@ int instance_run(void) int done = INST_NOT_DONE_YET; int message = instance_peekevent(); //get any of the received events from ISR - + // send_rxmsgtousb("Instance Run 1"); while(done == INST_NOT_DONE_YET) { //int state = instance_data[instance].testAppState; done = testapprun(&instance_data[instance], message) ; // run the communications application + if(done == INST_DONE_WAIT_FOR_NEXT_EVENT) + { + // uint8 debug_msg[100]; + // int n = sprintf((char*)&debug_msg[0], "done == INST_DONE_WAIT_FOR_NEXT_EVENT"); + // send_usbmessage(&debug_msg[0], n); + // usb_run(); + // send_rxmsgtousb("done == INST_DONE_WAIT_FOR_NEXT_EVENT"); + + } //we've processed message message = 0; } diff --git a/src/decadriver/deca_device_api.h b/src/decadriver/deca_device_api.h index d4de6775da1c70041e783cefed2751e1d5e76e86..3b0257b2af3ac9aae759649514b715f8860e5a6e 100644 --- a/src/decadriver/deca_device_api.h +++ b/src/decadriver/deca_device_api.h @@ -10,8 +10,8 @@ * */ -#ifndef _DECA_DEVICE_API_H_ -#define _DECA_DEVICE_API_H_ +#ifndef DECA_DEVICE_API_H_ +#define DECA_DEVICE_API_H_ #ifdef __cplusplus extern "C" { diff --git a/src/decadriver/deca_param_types.h b/src/decadriver/deca_param_types.h index aa094ab5c9499f13de483a6c5ddcbe15ae80c29f..d4ed0ca2c253f6e8ca57c87b959b4a422899bf9c 100644 --- a/src/decadriver/deca_param_types.h +++ b/src/decadriver/deca_param_types.h @@ -9,8 +9,8 @@ * All rights reserved. * */ -#ifndef _DECA_PARAM_TYPES_H_ -#define _DECA_PARAM_TYPES_H_ +#ifndef DECA_PARAM_TYPES_H_ +#define DECA_PARAM_TYPES_H_ #ifdef __cplusplus extern "C" { diff --git a/src/decadriver/deca_regs.h b/src/decadriver/deca_regs.h index 7574c72bf54fadb7ecbb29372ea8731ca8470478..42c737309de656dcbfa4fff70e75519598874def 100644 --- a/src/decadriver/deca_regs.h +++ b/src/decadriver/deca_regs.h @@ -11,8 +11,8 @@ * */ -#ifndef _DECA_REGS_H_ -#define _DECA_REGS_H_ +#ifndef DECA_REGS_H_ +#define DECA_REGS_H_ #ifdef __cplusplus extern "C" { diff --git a/src/decadriver/deca_types.h b/src/decadriver/deca_types.h index 541863ab57a546ac23b66142858c68dd20a2b6b3..6839bc6a3aa35ba164a29654008da49f11c94d7d 100644 --- a/src/decadriver/deca_types.h +++ b/src/decadriver/deca_types.h @@ -10,8 +10,8 @@ * */ -#ifndef _DECA_TYPES_H_ -#define _DECA_TYPES_H_ +#ifndef DECA_TYPES_H_ +#define DECA_TYPES_H_ #ifdef __cplusplus extern "C" { diff --git a/src/decadriver/deca_version.h b/src/decadriver/deca_version.h index 78695ac4e82ff9307c4d2660ba0649ba939edceb..ae522db8da4128f451842aacc4fb120ceda1296a 100644 --- a/src/decadriver/deca_version.h +++ b/src/decadriver/deca_version.h @@ -10,8 +10,8 @@ * */ -#ifndef _DECA_VERSION_H_ -#define _DECA_VERSION_H_ +#ifndef DECA_VERSION_H_ +#define DECA_VERSION_H_ // // The DW1000 device driver is separately version numbered to any version the application using it may have diff --git a/src/platform/deca_spi.c b/src/platform/deca_spi.c index a758e17e215b217a8ba5dde821e87ddde373f27c..a2b680692b36d03b374b67eb157f6f7fce554d9a 100644 --- a/src/platform/deca_spi.c +++ b/src/platform/deca_spi.c @@ -139,7 +139,7 @@ int readfromspi { SPIx->DR = 0; // Dummy write as we read the message body - while((SPIx->SR & SPI_I2S_FLAG_RXNE) == (uint16_t)RESET); + while((SPIx->SR & SPI_I2S_FLAG_RXNE) == (uint16_t)RESET){}; readBuffer[i] = SPIx->DR ;//port_SPIx_receive_data(); //this clears RXNE bit } diff --git a/src/platform/deca_spi.h b/src/platform/deca_spi.h index 2849c6488e319f5d75728154552a7e0eef7e5c0f..a178390ba51ee167af9529e39dd14ec3650d8a9a 100644 --- a/src/platform/deca_spi.h +++ b/src/platform/deca_spi.h @@ -11,8 +11,8 @@ * @author DecaWave */ -#ifndef _DECA_SPI_H_ -#define _DECA_SPI_H_ +#ifndef DECA_SPI_H_ +#define DECA_SPI_H_ #ifdef __cplusplus extern "C" { diff --git a/src/usb/deca_usb.c b/src/usb/deca_usb.c index 18f29652e2b956891a32577ee53bd875bb6077de..efb9c48a2469819e1966cb704ee00d3d98c6db7b 100644 --- a/src/usb/deca_usb.c +++ b/src/usb/deca_usb.c @@ -18,11 +18,14 @@ #include "deca_types.h" #include "deca_spi.h" -#include "usbd_cdc_core.h" +//#include "usbd_cdc_core.h" #include "usbd_usr.h" #include "usb_conf.h" #include "usbd_desc.h" + + + /** @defgroup USB_VCP_Private_Variables * @{ */ diff --git a/src/usb/deca_usb.h b/src/usb/deca_usb.h index 6b60505749453104c144e164591c8a824cdfd734..77b2099d46e78ba7e5e38a7bd5ed567a06e6b83d 100644 --- a/src/usb/deca_usb.h +++ b/src/usb/deca_usb.h @@ -10,12 +10,15 @@ * * @author DecaWave */ +#ifndef DECA_USB_H_ +#define DECA_USB_H_ /* Includes */ #include "port.h" - +//#include "port.h" +// #include "usbd_cdc_core.h" -#include "usbd_conf.h" +//#include "usbd_conf.h" //local data extern int local_buff_length; @@ -53,3 +56,4 @@ CDC_IF_Prop_TypeDef VCP_fops = DW_VCP_DataRx }; +#endif diff --git a/src/usb/usb_conf.h b/src/usb/usb_conf.h index d3a48c7d37d0d250429dd0bb461b6df66933c1b8..a0ca3b0bd6091f99443ab2e3538df33c615285fb 100644 --- a/src/usb/usb_conf.h +++ b/src/usb/usb_conf.h @@ -26,8 +26,8 @@ */ /* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CONF__H__ -#define __USB_CONF__H__ +#ifndef USB_CONF_H_ +#define USB_CONF_H_ /* Includes ------------------------------------------------------------------*/ #if defined (USE_STM322xG_EVAL) diff --git a/src/usb/usbd_conf.h b/src/usb/usbd_conf.h index c35fbe9e2b7c02b06ee386b07b938c34b558f8c5..69af368ab7c997dea2a2fb44f21b51f9f15ac9e2 100644 --- a/src/usb/usbd_conf.h +++ b/src/usb/usbd_conf.h @@ -26,8 +26,8 @@ */ /* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_CONF__H__ -#define __USBD_CONF__H__ +#ifndef USBD_CONF_H_ +#define USBD_CONF_H_ /* Includes ------------------------------------------------------------------*/ #include "usb_conf.h" diff --git a/src/usb/usbd_desc.h b/src/usb/usbd_desc.h index e28e14ae08061ad62db603fd2b3cc6a46ecbb763..0df73f62af2f2d52c337591da1e5d5ee70de0252 100644 --- a/src/usb/usbd_desc.h +++ b/src/usb/usbd_desc.h @@ -27,8 +27,8 @@ /* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_DESC_H -#define __USB_DESC_H +#ifndef USB_DESC_H_ +#define USB_DESC_H_ /* Includes ------------------------------------------------------------------*/ #include "usbd_def.h" diff --git a/stdio/printf.c b/stdio/printf.c new file mode 100644 index 0000000000000000000000000000000000000000..8ec2e4cdb2b8ab12404bb75c8b2d218fe3727069 --- /dev/null +++ b/stdio/printf.c @@ -0,0 +1,559 @@ +/**************************************************************************//***** + * @file printf.c + * @brief Implementation of several stdio.h methods, such as printf(), + * sprintf() and so on. This reduces the memory footprint of the + * binary when using those methods, compared to the libc implementation. + ********************************************************************************/ +#include <stdio.h> +#include <stdarg.h> + +extern SH_SendChar(int c); + +/** + * @brief Transmit a char, if you want to use printf(), + * you need implement this function + * + * @param pStr Storage string. + * @param c Character to write. + */ +void PrintChar(char c) +{ + SH_SendChar(c); + /* Send a char like: + while(Transfer not completed); + Transmit a char; + */ +} + +/** Maximum string size allowed (in bytes). */ +#define MAX_STRING_SIZE 100 + + +/** Required for proper compilation. */ +struct _reent r = {0, (FILE *) 0, (FILE *) 1, (FILE *) 0}; +//struct _reent *_impure_ptr = &r; + +/** + * @brief Writes a character inside the given string. Returns 1. + * + * @param pStr Storage string. + * @param c Character to write. + */ +signed int PutChar(char *pStr, char c) +{ + *pStr = c; + return 1; +} + + +/** + * @brief Writes a string inside the given string. + * + * @param pStr Storage string. + * @param pSource Source string. + * @return The size of the written + */ +signed int PutString(char *pStr, const char *pSource) +{ + signed int num = 0; + + while (*pSource != 0) { + + *pStr++ = *pSource++; + num++; + } + + return num; +} + + +/** + * @brief Writes an unsigned int inside the given string, using the provided fill & + * width parameters. + * + * @param pStr Storage string. + * @param fill Fill character. + * @param width Minimum integer width. + * @param value Integer value. + */ +signed int PutUnsignedInt( + char *pStr, + char fill, + signed int width, + unsigned int value) +{ + signed int num = 0; + + /* Take current digit into account when calculating width */ + width--; + + /* Recursively write upper digits */ + if ((value / 10) > 0) { + + num = PutUnsignedInt(pStr, fill, width, value / 10); + pStr += num; + } + + /* Write filler characters */ + else { + + while (width > 0) { + + PutChar(pStr, fill); + pStr++; + num++; + width--; + } + } + + /* Write lower digit */ + num += PutChar(pStr, (value % 10) + '0'); + + return num; +} + + +/** + * @brief Writes a signed int inside the given string, using the provided fill & width + * parameters. + * + * @param pStr Storage string. + * @param fill Fill character. + * @param width Minimum integer width. + * @param value Signed integer value. + */ +signed int PutSignedInt( + char *pStr, + char fill, + signed int width, + signed int value) +{ + signed int num = 0; + unsigned int absolute; + + /* Compute absolute value */ + if (value < 0) { + + absolute = -value; + } + else { + + absolute = value; + } + + /* Take current digit into account when calculating width */ + width--; + + /* Recursively write upper digits */ + if ((absolute / 10) > 0) { + + if (value < 0) { + + num = PutSignedInt(pStr, fill, width, -(absolute / 10)); + } + else { + + num = PutSignedInt(pStr, fill, width, absolute / 10); + } + pStr += num; + } + else { + + /* Reserve space for sign */ + if (value < 0) { + + width--; + } + + /* Write filler characters */ + while (width > 0) { + + PutChar(pStr, fill); + pStr++; + num++; + width--; + } + + /* Write sign */ + if (value < 0) { + + num += PutChar(pStr, '-'); + pStr++; + } + } + + /* Write lower digit */ + num += PutChar(pStr, (absolute % 10) + '0'); + + return num; +} + + +/** + * @brief Writes an hexadecimal value into a string, using the given fill, width & + * capital parameters. + * + * @param pStr Storage string. + * @param fill Fill character. + * @param width Minimum integer width. + * @param maj Indicates if the letters must be printed in lower- or upper-case. + * @param value Hexadecimal value. + * + * @return The number of char written + */ +signed int PutHexa( + char *pStr, + char fill, + signed int width, + unsigned char maj, + unsigned int value) +{ + signed int num = 0; + + /* Decrement width */ + width--; + + /* Recursively output upper digits */ + if ((value >> 4) > 0) { + + num += PutHexa(pStr, fill, width, maj, value >> 4); + pStr += num; + } + /* Write filler chars */ + else { + + while (width > 0) { + + PutChar(pStr, fill); + pStr++; + num++; + width--; + } + } + + /* Write current digit */ + if ((value & 0xF) < 10) { + + PutChar(pStr, (value & 0xF) + '0'); + } + else if (maj) { + + PutChar(pStr, (value & 0xF) - 10 + 'A'); + } + else { + + PutChar(pStr, (value & 0xF) - 10 + 'a'); + } + num++; + + return num; +} + + + +/* Global Functions ----------------------------------------------------------- */ + + +/** + * @brief Stores the result of a formatted string into another string. Format + * arguments are given in a va_list instance. + * + * @param pStr Destination string. + * @param length Length of Destination string. + * @param pFormat Format string. + * @param ap Argument list. + * + * @return The number of characters written. + */ +signed int vsnprintf(char *pStr, size_t length, const char *pFormat, va_list ap) +{ + char fill; + unsigned char width; + signed int num = 0; + signed int size = 0; + + /* Clear the string */ + if (pStr) { + + *pStr = 0; + } + + /* Phase string */ + while (*pFormat != 0 && size < length) { + + /* Normal character */ + if (*pFormat != '%') { + + *pStr++ = *pFormat++; + size++; + } + /* Escaped '%' */ + else if (*(pFormat+1) == '%') { + + *pStr++ = '%'; + pFormat += 2; + size++; + } + /* Token delimiter */ + else { + + fill = ' '; + width = 0; + pFormat++; + + /* Parse filler */ + if (*pFormat == '0') { + + fill = '0'; + pFormat++; + } + + /* Parse width */ + while ((*pFormat >= '0') && (*pFormat <= '9')) { + + width = (width*10) + *pFormat-'0'; + pFormat++; + } + + /* Check if there is enough space */ + if (size + width > length) { + + width = length - size; + } + + /* Parse type */ + switch (*pFormat) { + case 'd': + case 'i': num = PutSignedInt(pStr, fill, width, va_arg(ap, signed int)); break; + case 'u': num = PutUnsignedInt(pStr, fill, width, va_arg(ap, unsigned int)); break; + case 'x': num = PutHexa(pStr, fill, width, 0, va_arg(ap, unsigned int)); break; + case 'X': num = PutHexa(pStr, fill, width, 1, va_arg(ap, unsigned int)); break; + case 's': num = PutString(pStr, va_arg(ap, char *)); break; + case 'c': num = PutChar(pStr, va_arg(ap, unsigned int)); break; + default: + return EOF; + } + + pFormat++; + pStr += num; + size += num; + } + } + + /* NULL-terminated (final \0 is not counted) */ + if (size < length) { + + *pStr = 0; + } + else { + + *(--pStr) = 0; + size--; + } + + return size; +} + + +/** + * @brief Stores the result of a formatted string into another string. Format + * arguments are given in a va_list instance. + * + * @param pStr Destination string. + * @param length Length of Destination string. + * @param pFormat Format string. + * @param ... Other arguments + * + * @return The number of characters written. + */ +signed int snprintf(char *pString, size_t length, const char *pFormat, ...) +{ + va_list ap; + signed int rc; + + va_start(ap, pFormat); + rc = vsnprintf(pString, length, pFormat, ap); + va_end(ap); + + return rc; +} + + +/** + * @brief Stores the result of a formatted string into another string. Format + * arguments are given in a va_list instance. + * + * @param pString Destination string. + * @param length Length of Destination string. + * @param pFormat Format string. + * @param ap Argument list. + * + * @return The number of characters written. + */ +signed int vsprintf(char *pString, const char *pFormat, va_list ap) +{ + return vsnprintf(pString, MAX_STRING_SIZE, pFormat, ap); +} + +/** + * @brief Outputs a formatted string on the given stream. Format arguments are given + * in a va_list instance. + * + * @param pStream Output stream. + * @param pFormat Format string + * @param ap Argument list. + */ +signed int vfprintf(FILE *pStream, const char *pFormat, va_list ap) +{ + char pStr[MAX_STRING_SIZE]; + char pError[] = "stdio.c: increase MAX_STRING_SIZE\n\r"; + + /* Write formatted string in buffer */ + if (vsprintf(pStr, pFormat, ap) >= MAX_STRING_SIZE) { + + fputs(pError, stderr); + while (1); /* Increase MAX_STRING_SIZE */ + } + + /* Display string */ + return fputs(pStr, pStream); +} + + +/** + * @brief Outputs a formatted string on the DBGU stream. Format arguments are given + * in a va_list instance. + * + * @param pFormat Format string. + * @param ap Argument list. + */ +signed int vprintf(const char *pFormat, va_list ap) +{ + return vfprintf(stdout, pFormat, ap); +} + + +/** + * @brief Outputs a formatted string on the given stream, using a variable + * number of arguments. + * + * @param pStream Output stream. + * @param pFormat Format string. + */ +signed int fprintf(FILE *pStream, const char *pFormat, ...) +{ + va_list ap; + signed int result; + + /* Forward call to vfprintf */ + va_start(ap, pFormat); + result = vfprintf(pStream, pFormat, ap); + va_end(ap); + + return result; +} + + +/** + * @brief Outputs a formatted string on the DBGU stream, using a variable number of + * arguments. + * + * @param pFormat Format string. + */ +signed int printf(const char *pFormat, ...) +{ + va_list ap; + signed int result; + + /* Forward call to vprintf */ + va_start(ap, pFormat); + result = vprintf(pFormat, ap); + va_end(ap); + + return result; +} + + +/** + * @brief Writes a formatted string inside another string. + * + * @param pStr torage string. + * @param pFormat Format string. + */ +signed int sprintf(char *pStr, const char *pFormat, ...) +{ + va_list ap; + signed int result; + + // Forward call to vsprintf + va_start(ap, pFormat); + result = vsprintf(pStr, pFormat, ap); + va_end(ap); + + return result; +} + + +/** + * @brief Outputs a string on stdout. + * + * @param pStr String to output. + */ +signed int puts(const char *pStr) +{ + return fputs(pStr, stdout); +} + + +/** + * @brief Implementation of fputc using the DBGU as the standard output. Required + * for printf(). + * + * @param c Character to write. + * @param pStream Output stream. + * @param The character written if successful, or -1 if the output stream is + * not stdout or stderr. + */ +signed int fputc(signed int c, FILE *pStream) +{ + if ((pStream == stdout) || (pStream == stderr)) { + + PrintChar(c); + + return c; + } + else { + + return EOF; + } +} + + +/** + * @brief Implementation of fputs using the DBGU as the standard output. Required + * for printf(). + * + * @param pStr String to write. + * @param pStream Output stream. + * + * @return Number of characters written if successful, or -1 if the output + * stream is not stdout or stderr. + */ +signed int fputs(const char *pStr, FILE *pStream) +{ + signed int num = 0; + + while (*pStr != 0) { + + if (fputc(*pStr, pStream) == -1) { + + return -1; + } + num++; + pStr++; + } + + return num; +} + +/* --------------------------------- End Of File ------------------------------ */