blob: 131ae6549c00327d3254907f237e3fae0dcbd672 [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
33//--------------------------------------------------------------------+
34// MACRO CONSTANT TYPEDEF PROTYPES
35//--------------------------------------------------------------------+
36
37/* Blink pattern
38 * - 250 ms : device not mounted
39 * - 1000 ms : device mounted
40 * - 2500 ms : device is suspended
41 */
42enum {
43 BLINK_NOT_MOUNTED = 250,
44 BLINK_MOUNTED = 1000,
45 BLINK_SUSPENDED = 2500,
46};
47
48static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
49
50void led_blinking_task(void);
51void cdc_task(void);
52
53/*------------- MAIN -------------*/
54int main(void)
55{
56 board_init();
57 tusb_init();
58
59 while (1)
60 {
61 tud_task(); // tinyusb device task
62 led_blinking_task();
63
64 cdc_task();
65 }
66
67 return 0;
68}
69
70//--------------------------------------------------------------------+
71// Device callbacks
72//--------------------------------------------------------------------+
73
74// Invoked when device is mounted
75void tud_mount_cb(void)
76{
77 blink_interval_ms = BLINK_MOUNTED;
78}
79
80// Invoked when device is unmounted
81void tud_umount_cb(void)
82{
83 blink_interval_ms = BLINK_NOT_MOUNTED;
84}
85
86// Invoked when usb bus is suspended
87// remote_wakeup_en : if host allow us to perform remote wakeup
88// Within 7ms, device must draw an average of current less than 2.5 mA from bus
89void tud_suspend_cb(bool remote_wakeup_en)
90{
91 (void) remote_wakeup_en;
92 blink_interval_ms = BLINK_SUSPENDED;
93}
94
95// Invoked when usb bus is resumed
96void tud_resume_cb(void)
97{
98 blink_interval_ms = BLINK_MOUNTED;
99}
100
101
102//--------------------------------------------------------------------+
103// USB CDC
104//--------------------------------------------------------------------+
105void cdc_task(void)
106{
107 // connected() check for DTR bit
108 // Most but not all terminal client set this when making connection
109 // if ( tud_cdc_connected() )
110 {
111 // connected and there are data available
112 if ( tud_cdc_available() )
113 {
114 // read datas
115 char buf[64];
116 uint32_t count = tud_cdc_read(buf, sizeof(buf));
117 (void) count;
118
119 // Echo back
120 // Note: Skip echo by commenting out write() and write_flush()
121 // for throughput test e.g
122 // $ dd if=/dev/zero of=/dev/ttyACM0 count=10000
123 tud_cdc_write(buf, count);
124 tud_cdc_write_flush();
125 }
126 }
127}
128
129// Invoked when cdc when line state changed e.g connected/disconnected
130void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
131{
132 (void) itf;
133 (void) rts;
134
135 // TODO set some indicator
136 if ( dtr )
137 {
138 // Terminal connected
139 }else
140 {
141 // Terminal disconnected
142 }
143}
144
145// Invoked when CDC interface received data from host
146void tud_cdc_rx_cb(uint8_t itf)
147{
148 (void) itf;
149}
150
151//--------------------------------------------------------------------+
152// BLINKING TASK
153//--------------------------------------------------------------------+
154void led_blinking_task(void)
155{
156 static uint32_t start_ms = 0;
157 static bool led_state = false;
158
159 // Blink every interval ms
160 if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
161 start_ms += blink_interval_ms;
162
163 board_led_write(led_state);
164 led_state = 1 - led_state; // toggle
165}