Skip to content

Commit 2a19aec

Browse files
committed
example/net: add led blink task
Signed-off-by: HiFiPhile <[email protected]>
1 parent b9b90e2 commit 2a19aec

File tree

1 file changed

+51
-0
lines changed
  • examples/device/net_lwip_webserver/src

1 file changed

+51
-0
lines changed

examples/device/net_lwip_webserver/src/main.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00
6767
#define INIT_IP4(a, b, c, d) \
6868
{ PP_HTONL(LWIP_MAKEU32(a, b, c, d)) }
6969

70+
/* Blink pattern
71+
* - 250 ms : device not mounted
72+
* - 1000 ms : device mounted
73+
* - 2500 ms : device is suspended
74+
*/
75+
enum {
76+
BLINK_NOT_MOUNTED = 250,
77+
BLINK_MOUNTED = 1000,
78+
BLINK_SUSPENDED = 2500,
79+
};
80+
81+
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
82+
7083
/* lwip context */
7184
static struct netif netif_data;
7285

@@ -218,6 +231,20 @@ uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
218231
return pbuf_copy_partial(p, dst, p->tot_len, 0);
219232
}
220233

234+
static void led_blinking_task(void) {
235+
static uint32_t start_ms = 0;
236+
static bool led_state = false;
237+
238+
// Blink every interval ms
239+
if (board_millis() - start_ms < blink_interval_ms) {
240+
return; // not enough time
241+
}
242+
start_ms += blink_interval_ms;
243+
244+
board_led_write(led_state);
245+
led_state = 1 - led_state; // toggle
246+
}
247+
221248
static void handle_link_state_switch(void) {
222249
/* Check for button press to toggle link state */
223250
static bool last_link_state = true;
@@ -275,11 +302,35 @@ int main(void) {
275302
tud_task();
276303
sys_check_timeouts(); // service lwip
277304
handle_link_state_switch();
305+
led_blinking_task();
278306
}
279307

280308
return 0;
281309
}
282310

311+
// Invoked when device is mounted
312+
void tud_mount_cb(void) {
313+
blink_interval_ms = BLINK_MOUNTED;
314+
}
315+
316+
// Invoked when device is unmounted
317+
void tud_umount_cb(void) {
318+
blink_interval_ms = BLINK_NOT_MOUNTED;
319+
}
320+
321+
// Invoked when usb bus is suspended
322+
// remote_wakeup_en : if host allow us to perform remote wakeup
323+
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
324+
void tud_suspend_cb(bool remote_wakeup_en) {
325+
(void) remote_wakeup_en;
326+
blink_interval_ms = BLINK_SUSPENDED;
327+
}
328+
329+
// Invoked when usb bus is resumed
330+
void tud_resume_cb(void) {
331+
blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
332+
}
333+
283334
/* lwip has provision for using a mutex, when applicable */
284335
/* This implementation is for single-threaded use only */
285336
sys_prot_t sys_arch_protect(void) {

0 commit comments

Comments
 (0)