Skip to content

Commit 9848997

Browse files
Merge branch 'develop'
2 parents 3b4cb6a + 9125d53 commit 9848997

File tree

6 files changed

+54
-21
lines changed

6 files changed

+54
-21
lines changed

examples/nucleo-l053r8/SimplePublish/Src/dn_time.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ uint32_t dn_time_ms(void)
2424

2525
void dn_sleep_ms(uint32_t milliseconds)
2626
{
27+
/* A simple delay is used for simplicity in this example.
28+
* To save power, we could instead have initialized a timer to fire an
29+
* interrupt after the set number of milliseconds, followed by entering
30+
* a low-power sleep mode. Upon wake up, we would have to check that we
31+
* were indeed woken by said interrupt (and e.g. not an USART interrupt)
32+
* to decide if we should go back to sleep or not. */
2733
HAL_Delay(milliseconds);
2834
}
2935

examples/nucleo-l053r8/SimplePublish/Src/dn_watchdog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Port of the watchdog module to the NUCLEO-L053R8.
1717

1818
void dn_watchdog_feed(void)
1919
{
20-
// Nothing to do since we have not implemented watchdog on Raspberry Pi
20+
// Nothing to do since we have not implemented a watchdog in this example
2121
}
2222

2323
//=========================== private =========================================

examples/nucleo-l053r8/SimplePublish/Src/main.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ int main(void)
7676
uint8_t payload[3];
7777
uint8_t inboxBuf[DN_DEFAULT_PAYLOAD_SIZE_LIMIT];
7878
uint8_t bytesRead;
79+
uint8_t i;
7980
/* USER CODE END 1 */
8081

8182
/* MCU Configuration----------------------------------------------------------*/
@@ -94,6 +95,13 @@ int main(void)
9495
/* USER CODE BEGIN 2 */
9596
log_info("Initializing...");
9697
dn_qsl_init();
98+
99+
// Flash green LED to indicate start-up complete
100+
for (i = 0; i < 10; i++)
101+
{
102+
HAL_GPIO_TogglePin(GPIOA, LD2_Pin);
103+
dn_sleep_ms(50);
104+
}
97105
/* USER CODE END 2 */
98106

99107
/* Infinite loop */
@@ -108,6 +116,7 @@ int main(void)
108116
{
109117
uint16_t val = randomWalk();
110118
static uint8_t count = 0;
119+
HAL_GPIO_WritePin(GPIOA, LD2_Pin, GPIO_PIN_RESET); // Turn LED off during send/read
111120

112121
dn_write_uint16_t(payload, val);
113122
payload[2] = count;
@@ -127,15 +136,16 @@ int main(void)
127136
parsePayload(inboxBuf, bytesRead);
128137
} while (bytesRead > 0);
129138

139+
HAL_GPIO_WritePin(GPIOA, LD2_Pin, GPIO_PIN_SET); // Turn on green LED
130140
dn_sleep_ms(DATA_PERIOD_MS);
131141
} else
132142
{
133143
log_info("Connecting...");
134-
HAL_GPIO_WritePin(GPIOA, LD2_Pin, GPIO_PIN_RESET); // Turn off green LED
144+
HAL_GPIO_WritePin(GPIOA, LD2_Pin, GPIO_PIN_RESET); // Not connected; turn off green LED
135145
if (dn_qsl_connect(NETID, JOINKEY, SRC_PORT, BANDWIDTH_MS))
136146
{
137147
log_info("Connected to network");
138-
HAL_GPIO_WritePin(GPIOA, LD2_Pin, GPIO_PIN_SET); // Turn on green LED
148+
HAL_GPIO_WritePin(GPIOA, LD2_Pin, GPIO_PIN_SET); // Connected; turn on green LED
139149
} else
140150
{
141151
log_info("Failed to connect");

examples/samc21_xplained_pro/SimplePublish/src/dn_time.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ uint32_t dn_time_ms(void)
3939

4040
void dn_sleep_ms(uint32_t milliseconds)
4141
{
42-
// TODO: Implement proper sleep?
42+
/* A simple delay is used for simplicity in this example.
43+
* To save power, we could instead have initialized a timer to fire an
44+
* interrupt after the set number of milliseconds, followed by entering
45+
* a low-power sleep mode. Upon wake up, we would have to check that we
46+
* were indeed woken by said interrupt (and e.g. not an USART interrupt)
47+
* to decide if we should go back to sleep or not. */
4348
delay_ms(milliseconds);
4449
}
4550

examples/samc21_xplained_pro/SimplePublish/src/main.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ int main(int argc, char** argv)
3131
uint8_t payload[3];
3232
uint8_t inboxBuf[DN_DEFAULT_PAYLOAD_SIZE_LIMIT];
3333
uint8_t bytesRead;
34+
uint8_t i;
3435

3536
// Initialize system clock and board
3637
system_init();
@@ -49,6 +50,13 @@ int main(int argc, char** argv)
4950
log_info("Initializing...");
5051

5152
dn_qsl_init(); // Always returns TRUE at the moment
53+
54+
// Flash LED to indicate start-up complete
55+
for (i = 0; i < 10; i++)
56+
{
57+
LED_Toggle(LED_0_PIN);
58+
dn_sleep_ms(50);
59+
}
5260

5361
while (TRUE)
5462
{
@@ -57,6 +65,7 @@ int main(int argc, char** argv)
5765
{
5866
uint16_t val = randomWalk();
5967
static uint8_t count = 0;
68+
LED_Off(LED_0_PIN); // Turn LED off during send/read
6069

6170
dn_write_uint16_t(payload, val);
6271
payload[2] = count;
@@ -78,15 +87,16 @@ int main(int argc, char** argv)
7887
} while (bytesRead > 0);
7988

8089
wdt_reset_count();
90+
LED_On(LED_0_PIN);
8191
dn_sleep_ms(DATA_PERIOD_MS);
8292
} else
8393
{
8494
log_info("Connecting...");
85-
LED_Off(LED_0_PIN); // Turn off yellow LED
95+
LED_Off(LED_0_PIN); // Not connected; turn off yellow LED
8696
if (dn_qsl_connect(NETID, JOINKEY, SRC_PORT, BANDWIDTH_MS))
8797
{
8898
log_info("Connected to network");
89-
LED_On(LED_0_PIN); // Turn on yellow LED
99+
LED_On(LED_0_PIN); // Connected; turn on yellow LED
90100
} else
91101
{
92102
log_info("Failed to connect");

sm_qsl/dn_fsm.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bool dn_qsl_init(void)
101101
sizeof (dn_fsm_vars.notifBuf),
102102
dn_ipmt_reply_cb
103103
);
104-
104+
105105
dn_fsm_enterState(DN_FSM_STATE_DISCONNECTED, 0);
106106
return TRUE;
107107
}
@@ -254,7 +254,8 @@ uint8_t dn_qsl_read(uint8_t* readBuffer)
254254
*/
255255
static void dn_fsm_run(void)
256256
{
257-
if (dn_fsm_vars.fsmArmed && (dn_time_ms() - dn_fsm_vars.fsmEventScheduled_ms > dn_fsm_vars.fsmDelay_ms))
257+
uint32_t timePassed_ms = dn_time_ms() - dn_fsm_vars.fsmEventScheduled_ms; // Handle dn_time_ms wrap around
258+
if (dn_fsm_vars.fsmArmed && (timePassed_ms > dn_fsm_vars.fsmDelay_ms))
258259
{
259260
// Scheduled event is due; execute it
260261
dn_fsm_vars.fsmArmed = FALSE;
@@ -305,7 +306,7 @@ static void dn_fsm_setReplyCallback(dn_fsm_reply_cbt cb)
305306
dn_fsm_vars.replyCb = cb;
306307
}
307308

308-
//===== setReplyCallback
309+
//===== enterState
309310

310311
/**
311312
Transition FSM to new state and schedule any default entry events.
@@ -316,8 +317,8 @@ static void dn_fsm_enterState(uint8_t newState, uint16_t spesificDelay)
316317
uint16_t delay = DN_CMD_PERIOD_MS;
317318
static uint32_t lastTransition = 0;
318319
if (lastTransition == 0)
319-
lastTransition = dn_time_ms();
320-
320+
lastTransition = now;
321+
321322
// Use default delay if none given
322323
if (spesificDelay > 0)
323324
delay = spesificDelay;
@@ -361,7 +362,7 @@ static void dn_fsm_enterState(uint8_t newState, uint16_t spesificDelay)
361362
}
362363

363364
debug("FSM state transition: %#.2x --> %#.2x (%u ms)",
364-
dn_fsm_vars.state, newState, now - lastTransition);
365+
dn_fsm_vars.state, newState, (uint32_t)(now - lastTransition));
365366
lastTransition = now;
366367
dn_fsm_vars.state = newState;
367368
}
@@ -374,7 +375,8 @@ static void dn_fsm_enterState(uint8_t newState, uint16_t spesificDelay)
374375
*/
375376
static bool dn_fsm_cmd_timeout(uint32_t cmdStart_ms, uint32_t cmdTimeout_ms)
376377
{
377-
bool timeout = (dn_time_ms() - cmdStart_ms) > cmdTimeout_ms;
378+
uint32_t timePassed_ms = dn_time_ms() - cmdStart_ms; // Handle dn_time_ms wrap around
379+
bool timeout = timePassed_ms > cmdTimeout_ms;
378380
if (timeout)
379381
{
380382
// Cancel any ongoing transmission or scheduled event and reset reply cb
@@ -502,7 +504,7 @@ static void dn_ipmt_notif_cb(uint8_t cmdId, uint8_t subCmdId)
502504
case CMDID_RECEIVE:
503505
notif_receive = (dn_ipmt_receive_nt*)dn_fsm_vars.notifBuf;
504506
debug("Received downstream data");
505-
507+
506508
// Push payload at tail of inbox
507509
memcpy
508510
(
@@ -520,7 +522,7 @@ static void dn_ipmt_notif_cb(uint8_t cmdId, uint8_t subCmdId)
520522
dn_fsm_vars.inbox.unreadPackets++;
521523
}
522524
debug("Inbox capacity at %u / %u", dn_fsm_vars.inbox.unreadPackets, DN_INBOX_SIZE);
523-
525+
524526
break;
525527
case CMDID_MACRX:
526528
// Not implemented
@@ -531,7 +533,7 @@ static void dn_ipmt_notif_cb(uint8_t cmdId, uint8_t subCmdId)
531533
case CMDID_ADVRECEIVED:
532534
notif_advReceived = (dn_ipmt_advReceived_nt*)dn_fsm_vars.notifBuf;
533535
debug("Received network advertisement");
534-
536+
535537
if (dn_fsm_vars.state == DN_FSM_STATE_PROMISCUOUS
536538
&& dn_fsm_vars.networkId == DN_PROMISCUOUS_NET_ID)
537539
{
@@ -540,7 +542,7 @@ static void dn_ipmt_notif_cb(uint8_t cmdId, uint8_t subCmdId)
540542
dn_fsm_vars.networkId = notif_advReceived->netId;
541543
dn_fsm_scheduleEvent(DN_CMD_PERIOD_MS, dn_event_setNetworkId);
542544
}
543-
545+
544546
break;
545547
default:
546548
log_warn("Unknown notification ID");
@@ -993,7 +995,7 @@ static void dn_reply_setNetworkId(void)
993995
static void dn_event_search(void)
994996
{
995997
debug("Search");
996-
998+
997999
// Arm reply callback
9981000
dn_fsm_setReplyCallback(dn_reply_search);
9991001

@@ -1199,7 +1201,7 @@ static void dn_reply_getServiceInfo(void)
11991201
log_warn("Only granted service of %u ms (requested %u ms)", reply->value, dn_fsm_vars.service_ms);
12001202
dn_fsm_enterState(DN_FSM_STATE_DISCONNECTED, 0);
12011203
}
1202-
1204+
12031205
} else
12041206
{
12051207
debug("Service request still pending");
@@ -1304,7 +1306,7 @@ static dn_err_t checkAndSaveNetConfig(uint16_t netID, const uint8_t* joinKey, ui
13041306
{
13051307
memcpy(dn_fsm_vars.joinKey, joinKey, DN_JOIN_KEY_LEN);
13061308
}
1307-
1309+
13081310
if (srcPort == 0)
13091311
{
13101312
debug("No source port given; using default");
@@ -1328,7 +1330,7 @@ static uint8_t getPayloadLimit(uint16_t destPort)
13281330
bool destIsF0Bx = (destPort >= DN_WELL_KNOWN_PORT_1 && destPort <= DN_WELL_KNOWN_PORT_8);
13291331
bool srcIsF0Bx = (dn_fsm_vars.srcPort >= DN_WELL_KNOWN_PORT_1 && dn_fsm_vars.srcPort <= DN_WELL_KNOWN_PORT_8);
13301332
int8_t destIsMng = memcmp(DN_DEST_IP, DN_DEFAULT_DEST_IP, DN_IPv6ADDR_LEN);
1331-
1333+
13321334
if (destIsMng == 0)
13331335
{
13341336
if (destIsF0Bx && srcIsF0Bx)

0 commit comments

Comments
 (0)