| 
									
										
										
										
											2023-03-17 16:12:49 +07:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  |  * The MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-05-14 11:48:05 +07:00
										 |  |  |  * Copyright (c) 2019 Ha Thach (tinyusb.org) | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:44:05 +07:00
										 |  |  | #include "bsp/board_api.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  | #include "tusb.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 18:10:06 +07:00
										 |  |  | /* This example demonstrate HID Generic raw Input & Output.
 | 
					
						
							|  |  |  |  * It will receive data from Host (In endpoint) and echo back (Out endpoint). | 
					
						
							|  |  |  |  * HID Report descriptor use vendor for usage page (using template TUD_HID_REPORT_DESC_GENERIC_INOUT) | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-05-29 15:58:01 +07:00
										 |  |  |  * There are 2 ways to test the sketch | 
					
						
							|  |  |  |  * 1. Using nodejs | 
					
						
							| 
									
										
										
										
											2021-08-09 22:57:11 +07:00
										 |  |  |  * - Install nodejs and npm to your PC | 
					
						
							| 
									
										
										
										
											2019-05-29 15:58:01 +07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-08-09 22:57:11 +07:00
										 |  |  |  * - Install excellent node-hid (https://github.com/node-hid/node-hid) by
 | 
					
						
							|  |  |  |  *   $ npm install node-hid | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - Run provided hid test script | 
					
						
							|  |  |  |  *   $ node hid_test.js | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Using python | 
					
						
							|  |  |  |  * - Install `hid` package (https://pypi.org/project/hid/) by
 | 
					
						
							|  |  |  |  *   $ pip install hid | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - hid package replies on hidapi (https://github.com/libusb/hidapi) for backend,
 | 
					
						
							|  |  |  |  *   which already available in Linux. However on windows, you may need to download its dlls from their release page and | 
					
						
							|  |  |  |  *   copy it over to folder where python is installed. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - Run provided hid test script to send and receive data to this device. | 
					
						
							|  |  |  |  *   $ python3 hid_test.py | 
					
						
							| 
									
										
										
										
											2019-05-02 18:10:06 +07:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MACRO CONSTANT TYPEDEF PROTYPES
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Blink pattern
 | 
					
						
							|  |  |  |  * - 250 ms  : device not mounted | 
					
						
							|  |  |  |  * - 1000 ms : device mounted | 
					
						
							|  |  |  |  * - 2500 ms : device is suspended | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | enum  { | 
					
						
							|  |  |  |   BLINK_NOT_MOUNTED = 250, | 
					
						
							|  |  |  |   BLINK_MOUNTED = 1000, | 
					
						
							|  |  |  |   BLINK_SUSPENDED = 2500, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void led_blinking_task(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*------------- MAIN -------------*/ | 
					
						
							|  |  |  | int main(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   board_init(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-06 22:41:04 +07:00
										 |  |  |   // init device stack on configured roothub port
 | 
					
						
							|  |  |  |   tud_init(BOARD_TUD_RHPORT); | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-31 19:09:40 +07:00
										 |  |  |   if (board_init_after_tusb) { | 
					
						
							|  |  |  |     board_init_after_tusb(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  |   while (1) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2019-07-11 23:03:02 +07:00
										 |  |  |     tud_task(); // tinyusb device task
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  |     led_blinking_task(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Device callbacks
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when device is mounted
 | 
					
						
							|  |  |  | void tud_mount_cb(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   blink_interval_ms = BLINK_MOUNTED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when device is unmounted
 | 
					
						
							|  |  |  | void tud_umount_cb(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   blink_interval_ms = BLINK_NOT_MOUNTED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when usb bus is suspended
 | 
					
						
							|  |  |  | // remote_wakeup_en : if host allow us  to perform remote wakeup
 | 
					
						
							|  |  |  | // Within 7ms, device must draw an average of current less than 2.5 mA from bus
 | 
					
						
							|  |  |  | void tud_suspend_cb(bool remote_wakeup_en) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) remote_wakeup_en; | 
					
						
							|  |  |  |   blink_interval_ms = BLINK_SUSPENDED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when usb bus is resumed
 | 
					
						
							|  |  |  | void tud_resume_cb(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 12:04:22 +00:00
										 |  |  |   blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED; | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // USB HID
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 14:41:21 +07:00
										 |  |  | // Invoked when received GET_REPORT control request
 | 
					
						
							|  |  |  | // Application must fill buffer report's content and return its length.
 | 
					
						
							|  |  |  | // Return zero will cause the stack to STALL request
 | 
					
						
							| 
									
										
										
										
											2021-02-24 14:27:20 +07:00
										 |  |  | uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  | { | 
					
						
							|  |  |  |   // TODO not Implemented
 | 
					
						
							| 
									
										
										
										
											2021-02-24 14:27:20 +07:00
										 |  |  |   (void) itf; | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  |   (void) report_id; | 
					
						
							|  |  |  |   (void) report_type; | 
					
						
							|  |  |  |   (void) buffer; | 
					
						
							|  |  |  |   (void) reqlen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 14:41:21 +07:00
										 |  |  | // Invoked when received SET_REPORT control request or
 | 
					
						
							|  |  |  | // received data on OUT endpoint ( Report ID = 0, Type = 0 )
 | 
					
						
							| 
									
										
										
										
											2021-02-24 14:27:20 +07:00
										 |  |  | void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-02 14:45:44 +07:00
										 |  |  |   // This example doesn't use multiple report and report ID
 | 
					
						
							| 
									
										
										
										
											2021-02-24 14:27:20 +07:00
										 |  |  |   (void) itf; | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  |   (void) report_id; | 
					
						
							|  |  |  |   (void) report_type; | 
					
						
							| 
									
										
										
										
											2019-05-02 14:45:44 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // echo back anything we received from host
 | 
					
						
							|  |  |  |   tud_hid_report(0, buffer, bufsize); | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // BLINKING TASK
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | void led_blinking_task(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   static uint32_t start_ms = 0; | 
					
						
							|  |  |  |   static bool led_state = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 00:52:25 +07:00
										 |  |  |   // Blink every interval ms
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:31:02 +07:00
										 |  |  |   if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
 | 
					
						
							| 
									
										
										
										
											2019-05-02 13:27:00 +07:00
										 |  |  |   start_ms += blink_interval_ms; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   board_led_write(led_state); | 
					
						
							|  |  |  |   led_state = 1 - led_state; // toggle
 | 
					
						
							|  |  |  | } |