Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 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 | /* After device is enumerated, run following command |
| 27 | * |
| 28 | * $ dfu-util -l |
| 29 | * |
| 30 | * It should be able to list our device as in Runtime mode. Then run |
| 31 | * |
| 32 | * $ dfu-util -e |
| 33 | * |
| 34 | * This will send DETTACH command to put device into bootloader. Since this example |
| 35 | * is minimal, it doesn't actually go into DFU mode but rather change the LED blinking |
| 36 | * pattern to fast rate as indicator. |
| 37 | */ |
| 38 | |
| 39 | #include <stdlib.h> |
| 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | |
| 43 | #include "bsp/board.h" |
| 44 | #include "tusb.h" |
| 45 | |
| 46 | //--------------------------------------------------------------------+ |
| 47 | // MACRO CONSTANT TYPEDEF PROTYPES |
| 48 | //--------------------------------------------------------------------+ |
| 49 | |
| 50 | /* Blink pattern |
| 51 | * - 1000 ms : device should reboot |
| 52 | * - 250 ms : device not mounted |
| 53 | * - 0 ms : device mounted |
| 54 | * - 2500 ms : device is suspended |
| 55 | */ |
| 56 | enum { |
| 57 | BLINK_DFU_MODE = 100, |
| 58 | BLINK_NOT_MOUNTED = 250, |
| 59 | BLINK_MOUNTED = 1000, |
| 60 | BLINK_SUSPENDED = 2500, |
| 61 | }; |
| 62 | |
| 63 | static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; |
| 64 | |
| 65 | void led_blinking_task(void); |
| 66 | |
| 67 | /*------------- MAIN -------------*/ |
| 68 | int main(void) |
| 69 | { |
| 70 | board_init(); |
| 71 | |
| 72 | tusb_init(); |
| 73 | |
| 74 | while (1) |
| 75 | { |
| 76 | tud_task(); // tinyusb device task |
| 77 | led_blinking_task(); |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | //--------------------------------------------------------------------+ |
| 84 | // Device callbacks |
| 85 | //--------------------------------------------------------------------+ |
| 86 | |
| 87 | // Invoked when device is mounted |
| 88 | void tud_mount_cb(void) |
| 89 | { |
| 90 | blink_interval_ms = BLINK_MOUNTED; |
| 91 | } |
| 92 | |
| 93 | // Invoked when device is unmounted |
| 94 | void tud_umount_cb(void) |
| 95 | { |
| 96 | blink_interval_ms = BLINK_NOT_MOUNTED; |
| 97 | } |
| 98 | |
| 99 | // Invoked when usb bus is suspended |
| 100 | // remote_wakeup_en : if host allow us to perform remote wakeup |
| 101 | // Within 7ms, device must draw an average of current less than 2.5 mA from bus |
| 102 | void tud_suspend_cb(bool remote_wakeup_en) |
| 103 | { |
| 104 | (void) remote_wakeup_en; |
| 105 | blink_interval_ms = BLINK_SUSPENDED; |
| 106 | } |
| 107 | |
| 108 | // Invoked when usb bus is resumed |
| 109 | void tud_resume_cb(void) |
| 110 | { |
| 111 | blink_interval_ms = BLINK_MOUNTED; |
| 112 | } |
| 113 | |
| 114 | // Invoked on DFU_DETACH request to reboot to the bootloader |
| 115 | void tud_dfu_runtime_reboot_to_dfu_cb(void) |
| 116 | { |
| 117 | blink_interval_ms = BLINK_DFU_MODE; |
| 118 | } |
| 119 | |
| 120 | //--------------------------------------------------------------------+ |
| 121 | // BLINKING TASK + Indicator pulse |
| 122 | //--------------------------------------------------------------------+ |
| 123 | |
| 124 | void led_blinking_task(void) |
| 125 | { |
| 126 | static uint32_t start_ms = 0; |
| 127 | static bool led_state = false; |
| 128 | |
| 129 | // Blink every interval ms |
| 130 | if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time |
| 131 | start_ms += blink_interval_ms; |
| 132 | |
| 133 | board_led_write(led_state); |
| 134 | led_state = 1 - led_state; // toggle |
| 135 | } |