Skip to content

Commit 846b97e

Browse files
committed
Enable serial port only when tag reset is externally triggered
- use the reset reason to decide whether to enable UART and log to it - streamline the high-speed/low-speed serial baud rate selection
1 parent 1eaf56d commit 846b97e

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

firmware/nRF51/core/openbeacon/inc/uart.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
#ifdef CONFIG_UART_BAUDRATE
3737
extern void uart_init(void);
38-
extern int uart_enable(int enable);
3938
extern BOOL uart_tx(uint8_t data);
4039
extern int uart_rx(void);
4140
#endif

firmware/nRF51/core/openbeacon/src/uart.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static volatile uint16_t g_uart_buffer_count;
3434
/* allow to override default putchar output from serial to something else */
3535
BOOL default_putchar (uint8_t data) ALIAS(uart_tx);
3636

37+
3738
void uart_init(void)
3839
{
3940
g_uart_buffer_count = 0;
@@ -74,21 +75,17 @@ void uart_init(void)
7475
(UART_INTENSET_TXDRDY_Enabled << UART_INTENSET_TXDRDY_Pos);
7576

7677
/* start UART */
77-
#ifdef CONFIG_UART_RXD_PIN
78+
79+
#if CONFIG_UART_FORCE_POWERED | CONFIG_UART_RXD_PIN
7880
NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
79-
NRF_UART0->TASKS_STARTRX = 1;
80-
NRF_UART0->EVENTS_RXDRDY = 0;
8181
#else
8282
NRF_UART0->ENABLE = 0;
83-
#endif /*CONFIG_UART_RXD_PIN*/
84-
}
83+
#endif
8584

86-
inline int uart_enable(int enable)
87-
{
88-
#if CONFIG_UART_FORCE_POWERED
89-
NRF_UART0->ENABLE = (enable) ? (NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos) : 0;
85+
#ifdef CONFIG_UART_RXD_PIN
86+
NRF_UART0->TASKS_STARTRX = 1;
87+
NRF_UART0->EVENTS_RXDRDY = 0;
9088
#endif
91-
return enable;
9289
}
9390

9491
#ifdef CONFIG_UART_TXD_PIN
@@ -111,7 +108,9 @@ BOOL uart_tx(uint8_t data)
111108
else
112109
{
113110
/* enable UART for sending out first byte */
111+
#if !CONFIG_UART_FORCE_POWERED
114112
NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
113+
#endif
115114
NRF_UART0->TASKS_STARTTX = 1;
116115
NRF_UART0->TXD = data;
117116
}

firmware/nRF51/tag-proximity/src/main.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <openbeacon.h>
28+
#include <uart.h>
2829
#include <acc.h>
2930
#include <flash.h>
3031
#include <radio.h>
@@ -38,6 +39,7 @@ uint8_t hibernate = 0;
3839
uint16_t status_flags = 0;
3940
uint8_t boot_count;
4041
uint32_t reset_reason;
42+
uint8_t uart_enabled = 0;
4143

4244

4345
void blink(uint8_t times)
@@ -99,10 +101,14 @@ void main_entry(void)
99101
reset_reason = NRF_POWER->RESETREAS;
100102
NRF_POWER->RESETREAS = 0;
101103

102-
/* if reset was externally triggered, boot into hibernation mode */
104+
/* if reset was externally triggered,
105+
boot into hibernation mode and enable serial port */
103106
if ( (reset_reason & POWER_RESETREAS_RESETPIN_Msk) ||
104107
(reset_reason & POWER_RESETREAS_SREQ_Msk) )
108+
{
105109
hibernate = 1;
110+
uart_enabled = 1;
111+
}
106112

107113
/* get/update boot counter -- register is retained
108114
across resets as long as the device is powered */
@@ -121,7 +127,6 @@ void main_entry(void)
121127

122128
/* initialize UART */
123129
uart_init();
124-
uart_enable(1);
125130

126131
/* start timer */
127132
timer_init();
@@ -142,13 +147,12 @@ void main_entry(void)
142147
halt(3);
143148

144149
/* start radio */
145-
debug_printf("\n\rInitializing Tag[%08X] v" PROGRAM_VERSION " @24%02iMHz ...\n\r",
150+
if (uart_enabled)
151+
debug_printf("\n\rInitializing Tag[%08X] v" PROGRAM_VERSION " @24%02iMHz ...\n\r",
146152
tag_id,
147153
CONFIG_TRACKER_CHANNEL);
148154
radio_init(tag_id);
149155

150-
uart_enable(0);
151-
152156
/* enter main loop */
153157
blink_fast(5);
154158

@@ -172,16 +176,14 @@ void main_entry(void)
172176
keypress_duration = blink_wait_release();
173177

174178
/* long key press while tag is hibernating triggers log dump */
175-
if (hibernate && (keypress_duration > 2000))
179+
if (hibernate && uart_enabled && (keypress_duration > 2000))
176180
{
177181
#if CONFIG_FLASH_LOGGING
178182
blink_fast(10);
179183

180184
/* dump log data & status to serial */
181-
uart_enable(1);
182185
flash_log_dump();
183186
flash_log_status();
184-
uart_enable(0);
185187
#endif /* CONFIG_FLASH_LOGGING */
186188
} else if (keypress_duration > 500)
187189
{
@@ -195,9 +197,8 @@ void main_entry(void)
195197
#endif /* CONFIG_FLASH_LOGGING */
196198

197199
blink_fast(hibernate ? 3 : 6);
198-
uart_enable(1);
199-
debug_printf("\n\rhibernate -> %i", hibernate);
200-
uart_enable(0);
200+
if (uart_enabled)
201+
debug_printf("\n\rhibernate -> %i", hibernate);
201202
}
202203
}
203204

0 commit comments

Comments
 (0)