blob: 1fce48f45c6324fa7d7a4b69c8340add84d0623c [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 */
25
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29
30#include "bsp/board.h"
31#include "tusb.h"
32#include "usbtmc_app.h"
33//--------------------------------------------------------------------+
34// MACRO CONSTANT TYPEDEF PROTYPES
35//--------------------------------------------------------------------+
36
37/* Blink pattern
38 * - 250 ms : device not mounted
39 * - 0 ms : device mounted
40 * - 2500 ms : device is suspended
41 */
42enum {
43 BLINK_NOT_MOUNTED = 250,
44 BLINK_MOUNTED = 0,
45 BLINK_SUSPENDED = 2500,
46};
47
48static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
49
50void led_blinking_task(void);
51
52/*------------- MAIN -------------*/
53int main(void)
54{
55 board_init();
56
57 tusb_init();
58
59 while (1)
60 {
61 tud_task(); // tinyusb device task
62 led_blinking_task();
63 usbtmc_app_task_iter();
64 }
65
66 return 0;
67}
68
69//--------------------------------------------------------------------+
70// Device callbacks
71//--------------------------------------------------------------------+
72
73// Invoked when device is mounted
74void tud_mount_cb(void)
75{
76 blink_interval_ms = BLINK_MOUNTED;
77}
78
79// Invoked when device is unmounted
80void tud_umount_cb(void)
81{
82 blink_interval_ms = BLINK_NOT_MOUNTED;
83}
84
85// Invoked when usb bus is suspended
86// remote_wakeup_en : if host allow us to perform remote wakeup
87// Within 7ms, device must draw an average of current less than 2.5 mA from bus
88void tud_suspend_cb(bool remote_wakeup_en)
89{
90 (void) remote_wakeup_en;
91 blink_interval_ms = BLINK_SUSPENDED;
92}
93
94// Invoked when usb bus is resumed
95void tud_resume_cb(void)
96{
97 blink_interval_ms = BLINK_MOUNTED;
98}
99
100//--------------------------------------------------------------------+
101// BLINKING TASK + Indicator pulse
102//--------------------------------------------------------------------+
103
104
105volatile uint8_t doPulse = false;
106// called from USB context
107void led_indicator_pulse(void) {
108 doPulse = true;
109}
110
111void led_blinking_task(void)
112{
113 static uint32_t start_ms = 0;
114 static bool led_state = false;
115 if(blink_interval_ms == BLINK_MOUNTED) // Mounted
116 {
117 if(doPulse)
118 {
119 led_state = true;
120 board_led_write(true);
121 start_ms = board_millis();
122 doPulse = false;
123 }
124 else if (led_state == true)
125 {
126 if ( board_millis() - start_ms < 750) //Spec says blink must be between 500 and 1000 ms.
127 {
128 return; // not enough time
129 }
130 led_state = false;
131 board_led_write(false);
132 }
133 }
134 else
135 {
136 // Blink every interval ms
137 if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
138 start_ms += blink_interval_ms;
139
140 board_led_write(led_state);
141 led_state = 1 - led_state; // toggle
142 }
143}