77
88static uint8_t gpioState = 0x00 ;
99
10+ static void MakeReport (uint8_t * buffer )
11+ {
12+ /* TODO: Read the actual states of the GPIO input hardware pins. */
13+ buffer [0 ] = 0x00 ;
14+ buffer [1 ] = gpioState ;
15+ buffer [2 ] = 0x00 ;
16+ buffer [3 ] = 0x00 ;
17+ }
18+
19+ static void SendReport (void )
20+ {
21+ uint8_t reportBuffer [USB_HID_INOUT_REPORT_LEN ];
22+ MakeReport (reportBuffer );
23+
24+ tud_hid_report (0 , reportBuffer , sizeof (reportBuffer ));
25+ }
26+
1027static void ControlPTT (uint8_t gpio )
1128{
1229 /* PTT1 on GPIO 3, PTT2 on GPIO4 */
@@ -28,10 +45,9 @@ uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t
2845
2946 switch (report_type ) {
3047 case HID_REPORT_TYPE_INPUT :
31- buffer [0 ] = 0x00 ;
32- buffer [1 ] = gpioState ;
33- buffer [2 ] = 0x00 ;
34- buffer [3 ] = 0x00 ;
48+ TU_ASSERT (reqlen >= USB_HID_INOUT_REPORT_LEN , 0 );
49+
50+ MakeReport (buffer );
3551 return USB_HID_INOUT_REPORT_LEN ;
3652
3753 case HID_REPORT_TYPE_FEATURE :
@@ -58,8 +74,22 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t rep
5874
5975 /* Output report, emulate CM108 behaviour */
6076 if ((buffer [0 ] & 0xC0 ) == 0 ) {
77+ uint8_t gpioChange = gpioState ^ buffer [1 ];
6178 gpioState = buffer [1 ];
6279 ControlPTT (gpioState );
80+
81+ /* The CM108 sends an input report via the HID interrupt endpoint to the host,
82+ * whenever something changes (e.g. GPIO). Currently, this is only the case, whenever
83+ * the host changes the GPIO state (since the GPIOs are hardwired as output).
84+ * In the future, we might also support GPIO as input for other purposes. In that
85+ * case, we might need some kind of interrupt mechanism to detect changes on the GPIOs.
86+ * However some GPIOs might be shared (e.g. with UART), so we need to find a way to
87+ * only enable the external GPIO interrupts, if the host actually has opened the HID
88+ * interrupt pipe.
89+ */
90+ if (gpioChange ) {
91+ SendReport ();
92+ }
6393 }
6494 break ;
6595
0 commit comments