Skip to content

Commit c6f2aec

Browse files
committed
Add debug messages over serial
1 parent b31717d commit c6f2aec

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ else()
2626
message("Building for PSLab v6")
2727
endif(LEGACY_HARDWARE)
2828

29+
if (ENABLE_DEBUG)
30+
message("Debug messages enabled")
31+
add_compile_definitions(PSLAB_DEBUG)
32+
endif(ENABLE_DEBUG)
33+
2934
bin2hex(pslab-firmware.elf)

src/commands.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "bus/uart/uart.h"
44
#include "bus/spi/spi.h"
55
#include "helpers/buffer.h"
6+
#include "helpers/debug.h"
67
#include "helpers/device.h"
78
#include "helpers/interval.h"
89
#include "helpers/light.h"
@@ -247,8 +248,8 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
247248
{ // 11 COMMON
248249
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
249250
Undefined, MULTIMETER_GetCTMUVolts, MULTIMETER_GetCapacitance, Unimplemented,
250-
// 4 GET_INDUCTANCE 5 GET_VERSION 6 GET_FW_VERSION 7
251-
Unimplemented, DEVICE_GetVersion, DEVICE_get_fw_version, Undefined,
251+
// 4 GET_INDUCTANCE 5 GET_VERSION 6 GET_FW_VERSION 7 DEBUG_IS_ENABLED
252+
Unimplemented, DEVICE_GetVersion, DEVICE_get_fw_version, DEBUG_is_enabled,
252253
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SET_RGB1
253254
BUFFER_Retrieve, Unimplemented, BUFFER_Clear, Removed,
254255
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS

src/helpers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
target_sources(pslab-firmware.elf PRIVATE
22
buffer.c
3+
debug.c
34
delay.c
45
device.c
56
interval.c

src/helpers/debug.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdbool.h>
2+
3+
#include "../bus/uart/uart.h"
4+
#include "../commands.h"
5+
#include "debug.h"
6+
7+
response_t DEBUG_is_enabled(void) {
8+
#ifdef PSLAB_DEBUG
9+
bool const IS_DEBUG_ENABLED = true;
10+
#else
11+
bool const IS_DEBUG_ENABLED = false;
12+
#endif // PSLAB_DEBUG
13+
14+
UART1_Write(IS_DEBUG_ENABLED);
15+
return DO_NOT_BOTHER;
16+
}

src/helpers/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+
#define _DEBUG_H
3+
4+
#include "../commands.h"
5+
6+
// Enable debug messages over serial with -DENABLE_DEBUG CMake flag.
7+
8+
#ifdef SERIAL_DEBUG
9+
#define DEBUG_write_u8(byte) UART1_Write(byte)
10+
#define DEBUG_write_u16(word) UART1_WriteInt(word)
11+
#define DEBUG_write_u32(dword) UART1_write_u32(dword)
12+
#else
13+
#define DEBUG_write_u8(byte) ((void)byte)
14+
#define DEBUG_write_u16(word) ((void)word)
15+
#define DEBUG_write_u32(dword) ((void)dword)
16+
#endif // SERIAL_DEBUG
17+
18+
/**
19+
* @brief Tell host whether debug messages are enabled.
20+
*
21+
* @details If the firmware was built with -DENABLE_DEBUG, extra messages
22+
* containing information that may be useful in debugging are sent over
23+
* serial, in addition to the messages which are normally sent. The host
24+
* must take this into consideration when communicating with the PSLab, so
25+
* that the correct number of bytes are read for each transaction.
26+
*
27+
* @return is_debug_enabled bool
28+
*
29+
* @return DO_NOT_BOTHER response_t
30+
*/
31+
response_t DEBUG_is_enabled(void);
32+
33+
#endif // _DEBUG_H

0 commit comments

Comments
 (0)