Skip to content

Commit fb851dd

Browse files
authored
PicoSDK and mbed support (#4)
* #1 get logging working on mbed and PicoSDK
1 parent b6b3fcd commit fb851dd

6 files changed

Lines changed: 66 additions & 19 deletions

File tree

cmake/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_library(TcMenuLog
2+
../src/IoLogging.cpp
3+
../src/TextUtilities.cpp
4+
)
5+
6+
target_compile_definitions(TcMenuLog
7+
PUBLIC BUILD_FOR_PICO_CMAKE=1 BUILD_PICO_FORCE_UART=1 IO_LOGGING_DEBUG=1
8+
)
9+
10+
target_include_directories(TcMenuLog PUBLIC
11+
${PROJECT_SOURCE_DIR}/lib/TcMenuLog/src
12+
)
13+
14+
target_link_libraries(TcMenuLog PUBLIC pico_stdlib pico_sync)

src/IoLogging.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ const char* prettyLevel(SerLoggingLevel level) {
5454

5555
#ifdef BUILD_FOR_PICO_CMAKE
5656
PrintfLogger LoggingPort;
57+
unsigned long millis() {
58+
return to_ms_since_boot(get_absolute_time());
59+
}
60+
61+
unsigned long micros() {
62+
return to_us_since_boot(get_absolute_time());
63+
}
64+
#endif
65+
66+
#ifdef LOGGING_USES_MBED
67+
volatile bool timingStarted = false;
68+
Timer ioaTimer;
69+
unsigned long millis() {
70+
if(!timingStarted) {
71+
timingStarted = true;
72+
ioaTimer.start();
73+
}
74+
return ioaTimer.read_ms();
75+
}
76+
77+
unsigned long micros() {
78+
if(!timingStarted) {
79+
timingStarted = true;
80+
ioaTimer.start();
81+
}
82+
return (unsigned long) ioaTimer.read_high_resolution_us();
83+
}
5784
#endif
5885

5986
#endif

src/IoLogging.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,7 @@ enum SerLoggingLevel {
4646

4747
#if defined(__MBED__) && !defined(BUILD_FOR_PICO_CMAKE) && !defined(ARDUINO_ARCH_MBED)
4848
#define LOGGING_USES_MBED
49-
#elif !defined(BUILD_FOR_PICO_CMAKE)
50-
#include <Arduino.h>
51-
#endif
52-
53-
#ifdef IO_LOGGING_DEBUG
54-
55-
56-
/** This uint16_t stores the enabled logging levels, don't use directly */
57-
extern unsigned int enabledLevels;
58-
59-
#ifdef LOGGING_USES_MBED
49+
#include <mbed.h>
6050

6151
#include "PrintCompat.h"
6252
#include <FileHandle.h>
@@ -87,10 +77,14 @@ unsigned long millis();
8777
#define F(x) x
8878
#define IOLOG_MBED_PORT_IF_NEEDED(tx, rx) BufferedSerial serPort(tx, rx);MBedLogger LoggingPort(serPort);
8979
#define IOLOG_START_SERIAL serPort.set_baud(115200);
80+
unsigned long millis();
81+
unsigned long micros();
9082
#elif defined(BUILD_FOR_PICO_CMAKE)
9183
#include "PrintCompat.h"
92-
#include <cstring>
84+
#include <string.h>
85+
#include <ctype.h>
9386
#include <pico/time.h>
87+
#include <hardware/uart.h>
9488
#ifdef BUILD_PICO_FORCE_UART
9589
class PrintfLogger : public Print {
9690
private:
@@ -123,8 +117,9 @@ class PrintfLogger : public Print {
123117
};
124118
#define IOLOG_START_SERIAL stdio_init_all();
125119
#endif
120+
unsigned long millis();
121+
unsigned long micros();
126122
extern PrintfLogger LoggingPort;
127-
unsigned long millis(); // available from task manager
128123
#define F(x) x
129124
#define IOLOG_MBED_PORT_IF_NEEDED(tx, rx)
130125
#else
@@ -138,6 +133,12 @@ unsigned long millis(); // available from task manager
138133
#define IOLOG_MBED_PORT_IF_NEEDED(tx, rx)
139134
#endif
140135

136+
#ifdef IO_LOGGING_DEBUG
137+
138+
139+
/** This uint16_t stores the enabled logging levels, don't use directly */
140+
extern unsigned int enabledLevels;
141+
141142
const char* prettyLevel(SerLoggingLevel level);
142143
#define logTimeAndLevel(title, lvl) LoggingPort.print(millis());LoggingPort.print('-');LoggingPort.print(prettyLevel(lvl));LoggingPort.print(':');LoggingPort.print(title)
143144

@@ -216,9 +217,6 @@ inline void serdebugHexDump(const char *title, const void* data, size_t len) { s
216217

217218
#define startTaskManagerLogDelegate()
218219

219-
#define IOLOG_START_SERIAL
220-
#define IOLOG_MBED_PORT_IF_NEEDED(tx, rx)
221-
222220
#define serEnableLevel(l, a)
223221
#define serLevelEnabled(l) false
224222
#endif // IO_LOGGING_DEBUG

src/TextUtilities.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
44
*/
55

6+
#ifdef __AVR__
7+
#include <Arduino.h>
8+
#else
9+
#include <cstdint>
10+
#include <cctype>
11+
#include <valarray>
12+
#endif
613
#include "TextUtilities.h"
714

815
void appendChar(char* str, char val, int len) {

src/TextUtilities.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef IOABSTRACTION_TEXTUTILITIES_H
77
#define IOABSTRACTION_TEXTUTILITIES_H
88

9-
#include <IoLogging.h>
9+
#include <stdint.h>
1010

1111
/**
1212
* @file TextUtilities.h
@@ -135,8 +135,8 @@ inline float tcFltAbs(float f1) {
135135
#define lowByte(x) ((x) & 0xff)
136136
#define ltoa(a,b,c) itoa(a,b,c)
137137
# ifndef min
138-
# define min(x, y) (((x) < (y))?(x):(y))
139-
# define max(x, y) (((x) > (y))?(x):(y))
138+
template<class T> void min(T x, T y) { return (((x) < (y))?(x):(y)); }
139+
template<class T> void max(T x, T y) { return (((x) > (y))?(x):(y)); }
140140
# endif //TCMENU_MBED_NO_MINMAX
141141
#endif // IOA_USE_MBED
142142

test/test_text_utils/test_text_utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#include <unity.h>
3+
#include <string.h>
34
#include <TextUtilities.h>
45

56
void testTcUtilIntegerConversions() {

0 commit comments

Comments
 (0)