blob: 5c8464526d1f28b0980e50dd2c056ca04df2ae7b [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 /*
27 * After device is enumerated in dfu mode run the following commands
28 *
29 * To transfer firmware from host to device (best to test with text file)
30 *
31 * $ dfu-util -d cafe -a 0 -D [filename]
32 * $ dfu-util -d cafe -a 1 -D [filename]
33 *
34 * To transfer firmware from device to host:
35 *
36 * $ dfu-util -d cafe -a 0 -U [filename]
37 * $ dfu-util -d cafe -a 1 -U [filename]
38 *
39 */
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <string.h>
44
45#include "bsp/board.h"
46#include "tusb.h"
47
48//--------------------------------------------------------------------+
49// MACRO CONSTANT TYPEDEF PROTYPES
50//--------------------------------------------------------------------+
51const char* upload_image[2]=
52{
53 "Hello world from TinyUSB DFU! - Partition 0",
54 "Hello world from TinyUSB DFU! - Partition 1"
55};
56
57/* Blink pattern
58 * - 250 ms : device not mounted
59 * - 1000 ms : device mounted
60 * - 2500 ms : device is suspended
61 */
62enum {
63 BLINK_NOT_MOUNTED = 250,
64 BLINK_MOUNTED = 1000,
65 BLINK_SUSPENDED = 2500,
66};
67
68static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
69
70void led_blinking_task(void);
71
72/*------------- MAIN -------------*/
73int main(void)
74{
75 board_init();
76
77 tusb_init();
78
79 while (1)
80 {
81 tud_task(); // tinyusb device task
82 led_blinking_task();
83 }
84
85 return 0;
86}
87
88//--------------------------------------------------------------------+
89// Device callbacks
90//--------------------------------------------------------------------+
91
92// Invoked when device is mounted
93void tud_mount_cb(void)
94{
95 blink_interval_ms = BLINK_MOUNTED;
96}
97
98// Invoked when device is unmounted
99void tud_umount_cb(void)
100{
101 blink_interval_ms = BLINK_NOT_MOUNTED;
102}
103
104// Invoked when usb bus is suspended
105// remote_wakeup_en : if host allow us to perform remote wakeup
106// Within 7ms, device must draw an average of current less than 2.5 mA from bus
107void tud_suspend_cb(bool remote_wakeup_en)
108{
109 (void) remote_wakeup_en;
110 blink_interval_ms = BLINK_SUSPENDED;
111}
112
113// Invoked when usb bus is resumed
114void tud_resume_cb(void)
115{
116 blink_interval_ms = BLINK_MOUNTED;
117}
118
119//--------------------------------------------------------------------+
120// DFU callbacks
121// Note: alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
122//--------------------------------------------------------------------+
123
124// Invoked right before tud_dfu_download_cb() (state=DFU_DNBUSY) or tud_dfu_manifest_cb() (state=DFU_MANIFEST)
125// Application return timeout in milliseconds (bwPollTimeout) for the next download/manifest operation.
126// During this period, USB host won't try to communicate with us.
127uint32_t tud_dfu_get_timeout_cb(uint8_t alt, uint8_t state)
128{
129 if ( state == DFU_DNBUSY )
130 {
131 // For this example
132 // - Atl0 Flash is fast : 1 ms
133 // - Alt1 EEPROM is slow: 100 ms
134 return (alt == 0) ? 1 : 100;
135 }
136 else if (state == DFU_MANIFEST)
137 {
138 // since we don't buffer entire image and do any flashing in manifest stage
139 return 0;
140 }
141
142 return 0;
143}
144
145// Invoked when received DFU_DNLOAD (wLength>0) following by DFU_GETSTATUS (state=DFU_DNBUSY) requests
146// This callback could be returned before flashing op is complete (async).
147// Once finished flashing, application must call tud_dfu_finish_flashing()
148void tud_dfu_download_cb(uint8_t alt, uint16_t block_num, uint8_t const* data, uint16_t length)
149{
150 (void) alt;
151 (void) block_num;
152
153 //printf("\r\nReceived Alt %u BlockNum %u of length %u\r\n", alt, wBlockNum, length);
154
155 for(uint16_t i=0; i<length; i++)
156 {
157 printf("%c", data[i]);
158 }
159
160 // flashing op for download complete without error
161 tud_dfu_finish_flashing(DFU_STATUS_OK);
162}
163
164// Invoked when download process is complete, received DFU_DNLOAD (wLength=0) following by DFU_GETSTATUS (state=Manifest)
165// Application can do checksum, or actual flashing if buffered entire image previously.
166// Once finished flashing, application must call tud_dfu_finish_flashing()
167void tud_dfu_manifest_cb(uint8_t alt)
168{
169 (void) alt;
170 printf("Download completed, enter manifestation\r\n");
171
172 // flashing op for manifest is complete without error
173 // Application can perform checksum, should it fail, use appropriate status such as errVERIFY.
174 tud_dfu_finish_flashing(DFU_STATUS_OK);
175}
176
177// Invoked when received DFU_UPLOAD request
178// Application must populate data with up to length bytes and
179// Return the number of written bytes
180uint16_t tud_dfu_upload_cb(uint8_t alt, uint16_t block_num, uint8_t* data, uint16_t length)
181{
182 (void) block_num;
183 (void) length;
184
185 uint16_t const xfer_len = (uint16_t) strlen(upload_image[alt]);
186 memcpy(data, upload_image[alt], xfer_len);
187
188 return xfer_len;
189}
190
191// Invoked when the Host has terminated a download or upload transfer
192void tud_dfu_abort_cb(uint8_t alt)
193{
194 (void) alt;
195 printf("Host aborted transfer\r\n");
196}
197
198// Invoked when a DFU_DETACH request is received
199void tud_dfu_detach_cb(void)
200{
201 printf("Host detach, we should probably reboot\r\n");
202}
203
204//--------------------------------------------------------------------+
205// BLINKING TASK + Indicator pulse
206//--------------------------------------------------------------------+
207
208void led_blinking_task(void)
209{
210 static uint32_t start_ms = 0;
211 static bool led_state = false;
212
213 // Blink every interval ms
214 if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
215 start_ms += blink_interval_ms;
216
217 board_led_write(led_state);
218 led_state = 1 - led_state; // toggle
219}