|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Ennebi Elettronica (https://ennebielettronica.com) |
| 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_api.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 | + */ |
| 42 | +enum { |
| 43 | + BLINK_NOT_MOUNTED = 250, |
| 44 | + BLINK_MOUNTED = 1000, |
| 45 | + BLINK_SUSPENDED = 2500, |
| 46 | +}; |
| 47 | + |
| 48 | +static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; |
| 49 | + |
| 50 | +void led_blinking_task(void); |
| 51 | + |
| 52 | +/*------------- MAIN -------------*/ |
| 53 | +int main(void) { |
| 54 | + board_init(); |
| 55 | + |
| 56 | + // init device stack on configured roothub port |
| 57 | + tusb_rhport_init_t dev_init = { |
| 58 | + .role = TUSB_ROLE_DEVICE, |
| 59 | + .speed = TUSB_SPEED_AUTO |
| 60 | + }; |
| 61 | + tusb_init(BOARD_TUD_RHPORT, &dev_init); |
| 62 | + |
| 63 | + if (board_init_after_tusb) { |
| 64 | + board_init_after_tusb(); |
| 65 | + } |
| 66 | + |
| 67 | + while (1) { |
| 68 | + tud_task(); // tinyusb device task |
| 69 | + led_blinking_task(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +//--------------------------------------------------------------------+ |
| 74 | +// Device callbacks |
| 75 | +//--------------------------------------------------------------------+ |
| 76 | + |
| 77 | +// Invoked when device is mounted |
| 78 | +void tud_mount_cb(void) { |
| 79 | + blink_interval_ms = BLINK_MOUNTED; |
| 80 | +} |
| 81 | + |
| 82 | +// Invoked when device is unmounted |
| 83 | +void tud_umount_cb(void) { |
| 84 | + blink_interval_ms = BLINK_NOT_MOUNTED; |
| 85 | +} |
| 86 | + |
| 87 | +// Invoked when usb bus is suspended |
| 88 | +// remote_wakeup_en : if host allow us to perform remote wakeup |
| 89 | +// Within 7ms, device must draw an average of current less than 2.5 mA from bus |
| 90 | +void tud_suspend_cb(bool remote_wakeup_en) { |
| 91 | + (void) remote_wakeup_en; |
| 92 | + blink_interval_ms = BLINK_SUSPENDED; |
| 93 | +} |
| 94 | + |
| 95 | +// Invoked when usb bus is resumed |
| 96 | +void tud_resume_cb(void) { |
| 97 | + blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED; |
| 98 | +} |
| 99 | + |
| 100 | +//--------------------------------------------------------------------+ |
| 101 | +// BLINKING TASK |
| 102 | +//--------------------------------------------------------------------+ |
| 103 | +void led_blinking_task(void) { |
| 104 | + static uint32_t start_ms = 0; |
| 105 | + static bool led_state = false; |
| 106 | + |
| 107 | + // Blink every interval ms |
| 108 | + if (board_millis() - start_ms < blink_interval_ms) return; // not enough time |
| 109 | + start_ms += blink_interval_ms; |
| 110 | + |
| 111 | + board_led_write(led_state); |
| 112 | + led_state = 1 - led_state; // toggle |
| 113 | +} |
0 commit comments