Skip to content

Commit 31b844b

Browse files
committed
Add debug(const char *format, ...) function
This remove direct use of printf and allow to save more than several Kb of memory. By default, debug function does nothing. If __DEBUG is enabled this will call printf. Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 97719e3 commit 31b844b

File tree

7 files changed

+54
-23
lines changed

7 files changed

+54
-23
lines changed

cores/arduino/debug.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef _DEBUG_H
2+
#ifdef __DEBUG
3+
#include <stdio.h>
4+
#include <stdarg.h>
5+
#endif /* __DEBUG */
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
/** Output a debug message
12+
*
13+
* @param format printf-style format string, followed by variables
14+
* Note: By using the printf function of the library C this inflates the size of
15+
* the code, use a lot of stack. An alternative, will be to implement a tiny
16+
* and limited functionality implementation of printf.
17+
*/
18+
static inline void debug(const char *format, ...) {
19+
#ifdef __DEBUG
20+
va_list args;
21+
va_start(args, format);
22+
vfprintf(stderr, format, args);
23+
va_end(args);
24+
#else
25+
(void)(format);
26+
#endif /* __DEBUG */
27+
}
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif /* _DEBUG_H */

cores/arduino/stm32/spi_com.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
/** @addtogroup STM32F4xx_System_Private_Includes
4747
* @{
4848
*/
49+
#include "debug.h"
4950
#include "stm32_def.h"
5051
#include "spi_com.h"
5152
#include "PinAF_STM32F1.h"
@@ -149,7 +150,7 @@ uint32_t spi_getClkFreqInst(SPI_TypeDef * spi_inst)
149150
break;
150151
#endif
151152
default:
152-
printf("CLK: SPI instance not set");
153+
debug("CLK: SPI instance not set");
153154
break;
154155
}
155156
}
@@ -203,7 +204,7 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
203204

204205
/* Pins MOSI/MISO/SCLK must not be NP. ssel can be NP. */
205206
if(spi_mosi == NP || spi_miso == NP || spi_sclk == NP) {
206-
printf("ERROR: at least one SPI pin has no peripheral\n");
207+
debug("ERROR: at least one SPI pin has no peripheral\n");
207208
return;
208209
}
209210

@@ -214,7 +215,7 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
214215

215216
// Are all pins connected to the same SPI instance?
216217
if(obj->spi == NP) {
217-
printf("ERROR: SPI pins mismatch\n");
218+
debug("ERROR: SPI pins mismatch\n");
218219
return;
219220
}
220221

cores/arduino/stm32/stm32_def.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "stm32_def.h"
2+
#include "debug.h"
23

34
#ifdef __cplusplus
45
extern "C" {
@@ -9,18 +10,10 @@ extern "C" {
910
* @param None
1011
* @retval None
1112
*/
12-
WEAK void _Error_Handler(const char * msg, int val)
13-
{
13+
WEAK void _Error_Handler(const char * msg, int val) {
1414
/* User can add his own implementation to report the HAL error return state */
15-
/* By using the printf function of the library C this inflates the size of
16-
* the code, use a lot of stack. An alternative, will be to implement a tiny
17-
* and limited functionality implementation of printf.
18-
*/
19-
UNUSED(msg);
20-
UNUSED(val);
21-
/*printf("Error: %s (%i)\n", msg, val);*/
22-
while(1)
23-
{
15+
debug("Error: %s (%i)\n", msg, val);
16+
while(1) {
2417
}
2518
}
2619

cores/arduino/stm32/timer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
/** @addtogroup STM32F4xx_System_Private_Includes
4747
* @{
4848
*/
49+
#include "debug.h"
4950
#include "timer.h"
5051
#include "board.h"
5152

@@ -544,7 +545,7 @@ uint32_t getTimerIrq(TIM_TypeDef* tim)
544545
#endif
545546
break;
546547
default:
547-
printf("TIM: Unknown timer IRQn");
548+
debug("TIM: Unknown timer IRQn");
548549
break;
549550
}
550551
}
@@ -652,7 +653,7 @@ uint8_t getTimerClkSrc(TIM_TypeDef* tim)
652653
clkSrc = 2;
653654
break;
654655
default:
655-
printf("TIM: Unknown timer instance");
656+
debug("TIM: Unknown timer instance");
656657
break;
657658
}
658659
}
@@ -686,7 +687,7 @@ uint32_t getTimerClkFreq(TIM_TypeDef* tim)
686687
#endif
687688
default:
688689
case 0:
689-
printf("TIM: Unknown clock source");
690+
debug("TIM: Unknown clock source");
690691
break;
691692
}
692693
/* When TIMPRE bit of the RCC_DCKCFGR register is reset,

cores/arduino/stm32/twi.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
/** @addtogroup STM32F4xx_System_Private_Includes
4848
* @{
4949
*/
50+
#include "debug.h"
5051
#include "stm32_def.h"
5152
#include "twi.h"
5253
#include "PinAF_STM32F1.h"
@@ -166,14 +167,14 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
166167

167168
//Pins SDA/SCL must not be NP
168169
if(i2c_sda == NP || i2c_scl == NP) {
169-
printf("ERROR: at least one I2C pin has no peripheral\n");
170+
debug("ERROR: at least one I2C pin has no peripheral\n");
170171
return;
171172
}
172173

173174
obj->i2c = pinmap_merge_peripheral(i2c_sda, i2c_scl);
174175

175176
if(obj->i2c == NP) {
176-
printf("ERROR: I2C pins mismatch\n");
177+
debug("ERROR: I2C pins mismatch\n");
177178
return;
178179
}
179180

@@ -596,7 +597,7 @@ void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
596597
if(obj->slaveRxNbData < I2C_TXRX_BUFFER_SIZE) {
597598
obj->slaveRxNbData++;
598599
} else {
599-
printf("ERROR: I2C Slave RX overflow\n");
600+
debug("ERROR: I2C Slave RX overflow\n");
600601
}
601602
/* Restart interrupt mode for next Byte */
602603
if(obj->slaveMode == SLAVE_MODE_RECEIVE) {

cores/arduino/stm32/uart.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*
3434
******************************************************************************
3535
*/
36+
#include "debug.h"
3637
#include "uart.h"
3738
#include "Arduino.h"
3839
#include "PinAF_STM32F1.h"
@@ -101,7 +102,7 @@ void uart_init(serial_t *obj)
101102

102103
/* Pins Rx/Tx must not be NP */
103104
if(uart_rx == NP || uart_tx == NP) {
104-
printf("ERROR: at least one UART pin has no peripheral\n");
105+
debug("ERROR: at least one UART pin has no peripheral\n");
105106
return;
106107
}
107108

@@ -112,7 +113,7 @@ void uart_init(serial_t *obj)
112113
obj->uart = pinmap_merge_peripheral(uart_tx, uart_rx);
113114

114115
if(obj->uart == NP) {
115-
printf("ERROR: U(S)ART pins mismatch\n");
116+
debug("ERROR: U(S)ART pins mismatch\n");
116117
return;
117118
}
118119

cores/arduino/stm32/uart_emul.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
/** @addtogroup STM32F4xx_System_Private_Includes
4747
* @{
4848
*/
49+
#include "debug.h"
4950
#include "uart_emul.h"
5051
#include "digital_io.h"
5152
#include "interrupt.h"
@@ -402,7 +403,7 @@ void HAL_UART_Emul_RxCpltCallback(UART_Emul_HandleTypeDef *huart)
402403

403404
/*void HAL_UART_Emul_ErrorCallback(UART_Emul_HandleTypeDef *huart)
404405
{
405-
printf("UART EMUL RX ERROR\n");
406+
debug("UART EMUL RX ERROR\n");
406407
}*/
407408

408409
/**

0 commit comments

Comments
 (0)