Merge branch 'master' into dcd_notif
Signed-off-by: HiFiPhile <admin@hifiphile.com>
This commit is contained in:
		| @@ -489,7 +489,7 @@ typedef enum | ||||
|   AUDIO_DATA_FORMAT_TYPE_I_IEEE_FLOAT     = (uint32_t) (1 << 2), | ||||
|   AUDIO_DATA_FORMAT_TYPE_I_ALAW           = (uint32_t) (1 << 3), | ||||
|   AUDIO_DATA_FORMAT_TYPE_I_MULAW          = (uint32_t) (1 << 4), | ||||
|   AUDIO_DATA_FORMAT_TYPE_I_RAW_DATA       = 0x80000000, | ||||
|   AUDIO_DATA_FORMAT_TYPE_I_RAW_DATA       = 0x80000000u, | ||||
| } audio_data_format_type_I_t; | ||||
|  | ||||
| /// All remaining definitions are taken from the descriptor descriptions in the UAC2 main specification | ||||
| @@ -640,7 +640,7 @@ typedef enum | ||||
|   AUDIO_CHANNEL_CONFIG_BOTTOM_CENTER              = 0x01000000, | ||||
|   AUDIO_CHANNEL_CONFIG_BACK_LEFT_OF_CENTER        = 0x02000000, | ||||
|   AUDIO_CHANNEL_CONFIG_BACK_RIGHT_OF_CENTER       = 0x04000000, | ||||
|   AUDIO_CHANNEL_CONFIG_RAW_DATA                   = 0x80000000, | ||||
|   AUDIO_CHANNEL_CONFIG_RAW_DATA                   = 0x80000000u, | ||||
| } audio_channel_config_t; | ||||
|  | ||||
| /// AUDIO Channel Cluster Descriptor (4.1) | ||||
|   | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -3,6 +3,7 @@ | ||||
|  * | ||||
|  * Copyright (c) 2020 Ha Thach (tinyusb.org) | ||||
|  * Copyright (c) 2020 Reinhard Panhuber | ||||
|  * Copyright (c) 2023 HiFiPhile | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
| @@ -192,6 +193,7 @@ | ||||
| #endif | ||||
|  | ||||
| // Enable/disable conversion from 16.16 to 10.14 format on full-speed devices. See tud_audio_n_fb_set(). | ||||
| // Can be override by tud_audio_feedback_format_correction_cb() | ||||
| #ifndef CFG_TUD_AUDIO_ENABLE_FEEDBACK_FORMAT_CORRECTION | ||||
| #define CFG_TUD_AUDIO_ENABLE_FEEDBACK_FORMAT_CORRECTION     0                             // 0 or 1 | ||||
| #endif | ||||
| @@ -242,7 +244,8 @@ | ||||
| // Enable encoding/decodings - for these to work, support FIFOs need to be setup in appropriate numbers and size | ||||
| // The actual coding parameters of active AS alternate interface is parsed from the descriptors | ||||
|  | ||||
| // The item size of the FIFO is always fixed to one i.e. bytes! Furthermore, the actively used FIFO depth is reconfigured such that the depth is a multiple of the current sample size in order to avoid samples to get split up in case of a wrap in the FIFO ring buffer (depth = (max_depth / sampe_sz) * sampe_sz)! | ||||
| // The item size of the FIFO is always fixed to one i.e. bytes! Furthermore, the actively used FIFO depth is reconfigured such that the depth is a multiple | ||||
| // of the current sample size in order to avoid samples to get split up in case of a wrap in the FIFO ring buffer (depth = (max_depth / sample_sz) * sample_sz)! | ||||
| // This is important to remind in case you use DMAs! If the sample sizes changes, the DMA MUST BE RECONFIGURED just like the FIFOs for a different depth!!! | ||||
|  | ||||
| // For PCM encoding/decoding | ||||
| @@ -446,63 +449,80 @@ static inline bool tud_audio_int_write                      (const audio_interru | ||||
| bool tud_audio_buffer_and_schedule_control_xfer(uint8_t rhport, tusb_control_request_t const * p_request, void* data, uint16_t len); | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Application Callback API (weak is optional) | ||||
| // Application Callback API | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| #if CFG_TUD_AUDIO_ENABLE_EP_IN | ||||
| TU_ATTR_WEAK bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t func_id, uint8_t ep_in, uint8_t cur_alt_setting); | ||||
| TU_ATTR_WEAK bool tud_audio_tx_done_post_load_cb(uint8_t rhport, uint16_t n_bytes_copied, uint8_t func_id, uint8_t ep_in, uint8_t cur_alt_setting); | ||||
| bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t func_id, uint8_t ep_in, uint8_t cur_alt_setting); | ||||
| bool tud_audio_tx_done_post_load_cb(uint8_t rhport, uint16_t n_bytes_copied, uint8_t func_id, uint8_t ep_in, uint8_t cur_alt_setting); | ||||
| #endif | ||||
|  | ||||
| #if CFG_TUD_AUDIO_ENABLE_EP_OUT | ||||
| TU_ATTR_WEAK bool tud_audio_rx_done_pre_read_cb(uint8_t rhport, uint16_t n_bytes_received, uint8_t func_id, uint8_t ep_out, uint8_t cur_alt_setting); | ||||
| TU_ATTR_WEAK bool tud_audio_rx_done_post_read_cb(uint8_t rhport, uint16_t n_bytes_received, uint8_t func_id, uint8_t ep_out, uint8_t cur_alt_setting); | ||||
| bool tud_audio_rx_done_pre_read_cb(uint8_t rhport, uint16_t n_bytes_received, uint8_t func_id, uint8_t ep_out, uint8_t cur_alt_setting); | ||||
| bool tud_audio_rx_done_post_read_cb(uint8_t rhport, uint16_t n_bytes_received, uint8_t func_id, uint8_t ep_out, uint8_t cur_alt_setting); | ||||
| #endif | ||||
|  | ||||
| #if CFG_TUD_AUDIO_ENABLE_EP_OUT && CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP | ||||
| TU_ATTR_WEAK void tud_audio_fb_done_cb(uint8_t func_id); | ||||
| void tud_audio_fb_done_cb(uint8_t func_id); | ||||
|  | ||||
|  | ||||
| // determined by the user itself and set by use of tud_audio_n_fb_set(). The feedback value may be determined e.g. from some fill status of some FIFO buffer. Advantage: No ISR interrupt is enabled, hence the CPU need not to handle an ISR every 1ms or 125us and thus less CPU load, disadvantage: typically a larger FIFO is needed to compensate for jitter (e.g. 8 frames), i.e. a larger delay is introduced. | ||||
| // Note about feedback calculation | ||||
| // | ||||
| // Option 1 - AUDIO_FEEDBACK_METHOD_FIFO_COUNT | ||||
| // Feedback value is calculated within the audio driver by regulating the FIFO level to half fill. | ||||
| // Advantage: No ISR interrupt is enabled, hence the CPU need not to handle an ISR every 1ms or 125us and thus less CPU load, well tested | ||||
| // (Windows, Linux, OSX) with a reliable result so far. | ||||
| // Disadvantage: A FIFO of minimal 4 frames is needed to compensate for jitter, an average delay of 2 frames is introduced. | ||||
| // | ||||
| // Option 2 - AUDIO_FEEDBACK_METHOD_FREQUENCY_FIXED / AUDIO_FEEDBACK_METHOD_FREQUENCY_FLOAT | ||||
| // Feedback value is calculated within the audio driver by use of SOF interrupt. The driver needs information about the master clock f_m from | ||||
| // which the audio sample frequency f_s is derived, f_s itself, and the cycle count of f_m at time of the SOF interrupt (e.g. by use of a hardware counter). | ||||
| // See tud_audio_set_fb_params() and tud_audio_feedback_update() | ||||
| // Advantage: Reduced jitter in the feedback value computation, hence, the receive FIFO can be smaller and thus a smaller delay is possible. | ||||
| // Disadvantage: higher CPU load due to SOF ISR handling every frame i.e. 1ms or 125us. (The most critical point is the reading of the cycle counter value of f_m. | ||||
| // It is read from within the SOF ISR - see: audiod_sof() -, hence, the ISR must has a high priority such that no software dependent "random" delay i.e. jitter is introduced). | ||||
| // Long-term drift could occur since error is accumulated. | ||||
| // | ||||
| // Option 3 - manual | ||||
| // Determined by the user itself and set by use of tud_audio_n_fb_set(). The feedback value may be determined e.g. from some fill status of some FIFO buffer. | ||||
| // Advantage: No ISR interrupt is enabled, hence the CPU need not to handle an ISR every 1ms or 125us and thus less CPU load. | ||||
| // Disadvantage: typically a larger FIFO is needed to compensate for jitter (e.g. 6 frames), i.e. a larger delay is introduced. | ||||
|  | ||||
| // Feedback value is calculated within the audio driver by use of SOF interrupt. The driver needs information about the master clock f_m from which the audio sample frequency f_s is derived, f_s itself, and the cycle count of f_m at time of the SOF interrupt (e.g. by use of a hardware counter) - see tud_audio_set_fb_params(). Advantage: Reduced jitter in the feedback value computation, hence, the receive FIFO can be smaller (e.g. 2 frames) and thus a smaller delay is possible, disadvantage: higher CPU load due to SOF ISR handling every frame i.e. 1ms or 125us. This option is a great starting point to try the SOF ISR option but depending on your hardware setup (performance of the CPU) it might not work. If so, figure out why and use the next option. (The most critical point is the reading of the cycle counter value of f_m. It is read from within the SOF ISR - see: audiod_sof() -, hence, the ISR must has a high priority such that no software dependent "random" delay i.e. jitter is introduced). | ||||
|  | ||||
| // Feedback value is determined by the user by use of SOF interrupt. The user may use tud_audio_sof_isr() which is called every SOF (of course only invoked when an alternate interface other than zero was set). The number of frames used to determine the feedback value for the currently active alternate setting can be get by tud_audio_get_fb_n_frames(). The feedback value must be set by use of tud_audio_n_fb_set(). | ||||
|  | ||||
| // This function is used to provide data rate feedback from an asynchronous sink. Feedback value will be sent at FB endpoint interval till it's changed. | ||||
| // | ||||
| // The feedback format is specified to be 16.16 for HS and 10.14 for FS devices (see Universal Serial Bus Specification Revision 2.0 5.12.4.2). By default, | ||||
| // the choice of format is left to the caller and feedback argument is sent as-is. If CFG_TUD_AUDIO_ENABLE_FEEDBACK_FORMAT_CORRECTION is set, then tinyusb | ||||
| // expects 16.16 format and handles the conversion to 10.14 on FS. | ||||
| // the choice of format is left to the caller and feedback argument is sent as-is. If CFG_TUD_AUDIO_ENABLE_FEEDBACK_FORMAT_CORRECTION is set or tud_audio_feedback_format_correction_cb() | ||||
| // return true, then tinyusb expects 16.16 format and handles the conversion to 10.14 on FS. | ||||
| // | ||||
| // Note that due to a bug in its USB Audio 2.0 driver, Windows currently requires 16.16 format for _all_ USB 2.0 devices. On Linux and it seems the | ||||
| // driver can work with either format. | ||||
| // | ||||
| // Note that due to a bug in its USB Audio 2.0 driver, Windows currently requires 16.16 format for _all_ USB 2.0 devices. On Linux and macOS it seems the | ||||
| // driver can work with either format. So a good compromise is to keep format correction disabled and stick to 16.16 format. | ||||
|  | ||||
| // Feedback value can be determined from within the SOF ISR of the audio driver. This should reduce jitter. If the feature is used, the user can not set the feedback value. | ||||
|  | ||||
| // | ||||
| // Determine feedback value - The feedback method is described in 5.12.4.2 of the USB 2.0 spec | ||||
| // Boiled down, the feedback value Ff = n_samples / (micro)frame. | ||||
| // Since an accuracy of less than 1 Sample / second is desired, at least n_frames = ceil(2^K * f_s / f_m) frames need to be measured, where K = 10 for full speed and K = 13 for high speed, f_s is the sampling frequency e.g. 48 kHz and f_m is the cpu clock frequency e.g. 100 MHz (or any other master clock whose clock count is available and locked to f_s) | ||||
| // Since an accuracy of less than 1 Sample / second is desired, at least n_frames = ceil(2^K * f_s / f_m) frames need to be measured, where K = 10 for full speed and K = 13 | ||||
| // for high speed, f_s is the sampling frequency e.g. 48 kHz and f_m is the cpu clock frequency e.g. 100 MHz (or any other master clock whose clock count is available and locked to f_s) | ||||
| // The update interval in the (4.10.2.1) Feedback Endpoint Descriptor must be less or equal to 2^(K - P), where P = min( ceil(log2(f_m / f_s)), K) | ||||
| // feedback = n_cycles / n_frames * f_s / f_m in 16.16 format, where n_cycles are the number of main clock cycles within fb_n_frames | ||||
|  | ||||
| bool tud_audio_n_fb_set(uint8_t func_id, uint32_t feedback); | ||||
| static inline bool tud_audio_fb_set(uint32_t feedback); | ||||
|  | ||||
| // Update feedback value with passed cycles since last time this update function is called. | ||||
| // Update feedback value with passed MCLK cycles since last time this update function is called. | ||||
| // Typically called within tud_audio_sof_isr(). Required tud_audio_feedback_params_cb() is implemented | ||||
| // This function will also call tud_audio_feedback_set() | ||||
| // return feedback value in 16.16 for reference (0 for error) | ||||
| // Example : | ||||
| //   binterval=3 (4ms); FS = 48kHz; MCLK = 12.288MHz | ||||
| //   In 4 SOF MCLK counted 49152 cycles | ||||
| uint32_t tud_audio_feedback_update(uint8_t func_id, uint32_t cycles); | ||||
|  | ||||
| enum { | ||||
|   AUDIO_FEEDBACK_METHOD_DISABLED, | ||||
|   AUDIO_FEEDBACK_METHOD_FREQUENCY_FIXED, | ||||
|   AUDIO_FEEDBACK_METHOD_FREQUENCY_FLOAT, | ||||
|   AUDIO_FEEDBACK_METHOD_FREQUENCY_POWER_OF_2, | ||||
|  | ||||
|   // impelemnt later | ||||
|   // AUDIO_FEEDBACK_METHOD_FIFO_COUNT | ||||
|   AUDIO_FEEDBACK_METHOD_FREQUENCY_POWER_OF_2, // For driver internal use only | ||||
|   AUDIO_FEEDBACK_METHOD_FIFO_COUNT | ||||
| }; | ||||
|  | ||||
| typedef struct { | ||||
| @@ -514,52 +534,50 @@ typedef struct { | ||||
|       uint32_t mclk_freq; // Main clock frequency in Hz i.e. master clock to which sample clock is based on | ||||
|     }frequency; | ||||
|  | ||||
| #if 0 // implement later | ||||
|     struct { | ||||
|       uint32_t threshold_bytes; // minimum number of bytes received to be considered as filled/ready | ||||
|     }fifo_count; | ||||
| #endif | ||||
|   }; | ||||
| }audio_feedback_params_t; | ||||
|  | ||||
| // Invoked when needed to set feedback parameters | ||||
| TU_ATTR_WEAK void tud_audio_feedback_params_cb(uint8_t func_id, uint8_t alt_itf, audio_feedback_params_t* feedback_param); | ||||
| void tud_audio_feedback_params_cb(uint8_t func_id, uint8_t alt_itf, audio_feedback_params_t* feedback_param); | ||||
|  | ||||
| // Callback in ISR context, invoked periodically according to feedback endpoint bInterval. | ||||
| // Could be used to compute and update feedback value, should be placed in RAM if possible | ||||
| // frame_number  : current SOF count | ||||
| // interval_shift: number of bit shift i.e log2(interval) from Feedback endpoint descriptor | ||||
| TU_ATTR_WEAK TU_ATTR_FAST_FUNC void tud_audio_feedback_interval_isr(uint8_t func_id, uint32_t frame_number, uint8_t interval_shift); | ||||
| TU_ATTR_FAST_FUNC void tud_audio_feedback_interval_isr(uint8_t func_id, uint32_t frame_number, uint8_t interval_shift); | ||||
|  | ||||
| // (Full-Speed only) Callback to set feedback format correction is applied or not, | ||||
| // default to CFG_TUD_AUDIO_ENABLE_FEEDBACK_FORMAT_CORRECTION if not implemented. | ||||
| bool tud_audio_feedback_format_correction_cb(uint8_t func_id); | ||||
| #endif // CFG_TUD_AUDIO_ENABLE_EP_OUT && CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP | ||||
|  | ||||
| #if CFG_TUD_AUDIO_ENABLE_INTERRUPT_EP | ||||
| TU_ATTR_WEAK void tud_audio_int_done_cb(uint8_t rhport); | ||||
| void tud_audio_int_done_cb(uint8_t rhport); | ||||
| #endif | ||||
|  | ||||
| // Invoked when audio set interface request received | ||||
| TU_ATTR_WEAK bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
| bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
|  | ||||
| // Invoked when audio set interface request received which closes an EP | ||||
| TU_ATTR_WEAK bool tud_audio_set_itf_close_EP_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
| bool tud_audio_set_itf_close_EP_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
|  | ||||
| // Invoked when audio class specific set request received for an EP | ||||
| TU_ATTR_WEAK bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff); | ||||
| bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff); | ||||
|  | ||||
| // Invoked when audio class specific set request received for an interface | ||||
| TU_ATTR_WEAK bool tud_audio_set_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff); | ||||
| bool tud_audio_set_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff); | ||||
|  | ||||
| // Invoked when audio class specific set request received for an entity | ||||
| TU_ATTR_WEAK bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff); | ||||
| bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff); | ||||
|  | ||||
| // Invoked when audio class specific get request received for an EP | ||||
| TU_ATTR_WEAK bool tud_audio_get_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
| bool tud_audio_get_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
|  | ||||
| // Invoked when audio class specific get request received for an interface | ||||
| TU_ATTR_WEAK bool tud_audio_get_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
| bool tud_audio_get_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
|  | ||||
| // Invoked when audio class specific get request received for an entity | ||||
| TU_ATTR_WEAK bool tud_audio_get_req_entity_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
| bool tud_audio_get_req_entity_cb(uint8_t rhport, tusb_control_request_t const * p_request); | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Inline Functions | ||||
|   | ||||
| @@ -42,10 +42,14 @@ typedef struct | ||||
|   uint8_t itf_num; | ||||
|   uint8_t ep_ev; | ||||
|   uint8_t ep_acl_in; | ||||
|   uint16_t ep_acl_in_pkt_sz; | ||||
|   uint8_t ep_acl_out; | ||||
|   uint8_t ep_voice[2];  // Not used yet | ||||
|   uint8_t ep_voice_size[2][CFG_TUD_BTH_ISO_ALT_COUNT]; | ||||
|  | ||||
|   // Previous amount of bytes sent when issuing ZLP | ||||
|   uint32_t prev_xferred_bytes; | ||||
|  | ||||
|   // Endpoint Transfer buffer | ||||
|   CFG_TUSB_MEM_ALIGN bt_hci_cmd_t hci_cmd; | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_BTH_DATA_EPSIZE]; | ||||
| @@ -127,11 +131,25 @@ uint16_t btd_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_ | ||||
|   TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0); | ||||
|   _btd_itf.ep_ev = desc_ep->bEndpointAddress; | ||||
|  | ||||
|   // Open endpoint pair | ||||
|   TU_ASSERT(usbd_open_edpt_pair(rhport, tu_desc_next(desc_ep), 2, TUSB_XFER_BULK, &_btd_itf.ep_acl_out, | ||||
|                                 &_btd_itf.ep_acl_in), 0); | ||||
|   desc_ep = (tusb_desc_endpoint_t const *)tu_desc_next(desc_ep); | ||||
|  | ||||
|   itf_desc = (tusb_desc_interface_t const *)tu_desc_next(tu_desc_next(tu_desc_next(desc_ep))); | ||||
|   // Open endpoint pair | ||||
|   TU_ASSERT(usbd_open_edpt_pair(rhport, (uint8_t const *)desc_ep, 2, | ||||
|                                 TUSB_XFER_BULK, &_btd_itf.ep_acl_out, | ||||
|                                 &_btd_itf.ep_acl_in), | ||||
|             0); | ||||
|  | ||||
|   // Save acl in endpoint max packet size | ||||
|   tusb_desc_endpoint_t const *desc_ep_acl_in = desc_ep; | ||||
|   for (size_t p = 0; p < 2; p++) { | ||||
|     if (tu_edpt_dir(desc_ep_acl_in->bEndpointAddress) == TUSB_DIR_IN) { | ||||
|       _btd_itf.ep_acl_in_pkt_sz = tu_edpt_packet_size(desc_ep_acl_in); | ||||
|       break; | ||||
|     } | ||||
|     desc_ep_acl_in = (tusb_desc_endpoint_t const *)tu_desc_next(desc_ep_acl_in); | ||||
|   } | ||||
|  | ||||
|   itf_desc = (tusb_desc_interface_t const *)tu_desc_next(tu_desc_next(desc_ep)); | ||||
|  | ||||
|   // Prepare for incoming data from host | ||||
|   TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_itf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE), 0); | ||||
| @@ -238,10 +256,8 @@ bool btd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t c | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| bool btd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) | ||||
| { | ||||
|   (void)result; | ||||
|  | ||||
| bool btd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, | ||||
|                  uint32_t xferred_bytes) { | ||||
|   // received new data from host | ||||
|   if (ep_addr == _btd_itf.ep_acl_out) | ||||
|   { | ||||
| @@ -256,7 +272,20 @@ bool btd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t | ||||
|   } | ||||
|   else if (ep_addr == _btd_itf.ep_acl_in) | ||||
|   { | ||||
|     if (tud_bt_acl_data_sent_cb) tud_bt_acl_data_sent_cb((uint16_t)xferred_bytes); | ||||
|     if ((result == XFER_RESULT_SUCCESS) && (xferred_bytes > 0) && | ||||
|         ((xferred_bytes & (_btd_itf.ep_acl_in_pkt_sz - 1)) == 0)) { | ||||
|       // Save number of transferred bytes | ||||
|       _btd_itf.prev_xferred_bytes = xferred_bytes; | ||||
|  | ||||
|       // Send zero-length packet | ||||
|       tud_bt_acl_data_send(NULL, 0); | ||||
|     } else if (tud_bt_acl_data_sent_cb) { | ||||
|       if (xferred_bytes == 0) { | ||||
|         xferred_bytes = _btd_itf.prev_xferred_bytes; | ||||
|         _btd_itf.prev_xferred_bytes = 0; | ||||
|       } | ||||
|       tud_bt_acl_data_sent_cb((uint16_t)xferred_bytes); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   return true; | ||||
|   | ||||
| @@ -45,8 +45,7 @@ | ||||
| //--------------------------------------------------------------------+ | ||||
| #define BULK_PACKET_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) | ||||
|  | ||||
| typedef struct | ||||
| { | ||||
| typedef struct { | ||||
|   uint8_t rhport; | ||||
|   uint8_t itf_num; | ||||
|   uint8_t ep_notif; | ||||
| @@ -57,7 +56,7 @@ typedef struct | ||||
|   uint8_t line_state; | ||||
|  | ||||
|   /*------------- From this point, data is not cleared by bus reset -------------*/ | ||||
|   char    wanted_char; | ||||
|   char wanted_char; | ||||
|   TU_ATTR_ALIGNED(4) cdc_line_coding_t line_coding; | ||||
|  | ||||
|   // FIFO | ||||
| @@ -74,18 +73,21 @@ typedef struct | ||||
|   CFG_TUSB_MEM_ALIGN cdc_notif_serial_state_t serial_state_buf; | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE]; | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EP_BUFSIZE]; | ||||
|  | ||||
| }cdcd_interface_t; | ||||
| } cdcd_interface_t; | ||||
|  | ||||
| #define ITF_MEM_RESET_SIZE   offsetof(cdcd_interface_t, wanted_char) | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // INTERNAL OBJECT & FUNCTION DECLARATION | ||||
| //--------------------------------------------------------------------+ | ||||
| CFG_TUD_MEM_SECTION tu_static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC]; | ||||
| CFG_TUD_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC]; | ||||
| static tud_cdc_configure_fifo_t _cdcd_fifo_cfg; | ||||
|  | ||||
| static bool _prep_out_transaction (cdcd_interface_t* p_cdc) { | ||||
|  | ||||
|   // Skip if usb is not ready yet | ||||
|   TU_VERIFY(tud_ready() && p_cdc->ep_out); | ||||
|  | ||||
| static bool _prep_out_transaction (cdcd_interface_t* p_cdc) | ||||
| { | ||||
|   uint16_t available = tu_fifo_remaining(&p_cdc->rx_ff); | ||||
|  | ||||
|   // Prepare for incoming data but only allow what we can store in the ring buffer. | ||||
| @@ -100,14 +102,11 @@ static bool _prep_out_transaction (cdcd_interface_t* p_cdc) | ||||
|   // fifo can be changed before endpoint is claimed | ||||
|   available = tu_fifo_remaining(&p_cdc->rx_ff); | ||||
|  | ||||
|   if ( available >= sizeof(p_cdc->epout_buf) ) | ||||
|   { | ||||
|   if ( available >= sizeof(p_cdc->epout_buf) ) { | ||||
|     return usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_out, p_cdc->epout_buf, sizeof(p_cdc->epout_buf)); | ||||
|   }else | ||||
|   { | ||||
|   }else { | ||||
|     // Release endpoint since we don't make any transfer | ||||
|     usbd_edpt_release(p_cdc->rhport, p_cdc->ep_out); | ||||
|  | ||||
|     return false; | ||||
|   } | ||||
| } | ||||
| @@ -115,28 +114,35 @@ static bool _prep_out_transaction (cdcd_interface_t* p_cdc) | ||||
| //--------------------------------------------------------------------+ | ||||
| // APPLICATION API | ||||
| //--------------------------------------------------------------------+ | ||||
| bool tud_cdc_n_connected(uint8_t itf) | ||||
| { | ||||
|  | ||||
| bool tud_cdc_configure_fifo(tud_cdc_configure_fifo_t const* cfg) { | ||||
|   TU_VERIFY(cfg); | ||||
|   _cdcd_fifo_cfg = (*cfg); | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| bool tud_cdc_n_ready(uint8_t itf) { | ||||
|   return tud_ready() && _cdcd_itf[itf].ep_in != 0 && _cdcd_itf[itf].ep_out != 0; | ||||
| } | ||||
|  | ||||
| bool tud_cdc_n_connected(uint8_t itf) { | ||||
|   // DTR (bit 0) active  is considered as connected | ||||
|   return tud_ready() && tu_bit_test(_cdcd_itf[itf].line_state, 0); | ||||
| } | ||||
|  | ||||
| uint8_t tud_cdc_n_get_line_state (uint8_t itf) | ||||
| { | ||||
| uint8_t tud_cdc_n_get_line_state(uint8_t itf) { | ||||
|   return _cdcd_itf[itf].line_state; | ||||
| } | ||||
|  | ||||
| void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding) | ||||
| { | ||||
| void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t* coding) { | ||||
|   (*coding) = _cdcd_itf[itf].line_coding; | ||||
| } | ||||
|  | ||||
| bool tud_cdc_n_send_uart_state (uint8_t itf, cdc_uart_state_t state) | ||||
| { | ||||
| bool tud_cdc_n_send_uart_state (uint8_t itf, cdc_uart_state_t state) { | ||||
|   cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; | ||||
|  | ||||
|   // Skip if usb is not ready yet | ||||
|   TU_VERIFY( tud_ready(), 0 ); | ||||
|   TU_VERIFY(tud_ready(), 0); | ||||
|  | ||||
|   // claim endpoint | ||||
|   TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_notif)); | ||||
| @@ -152,34 +158,29 @@ bool tud_cdc_n_send_uart_state (uint8_t itf, cdc_uart_state_t state) | ||||
|   return usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_notif, (uint8_t *)&p_cdc->serial_state_buf, sizeof(p_cdc->serial_state_buf)); | ||||
| } | ||||
|  | ||||
| void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted) | ||||
| { | ||||
| void tud_cdc_n_set_wanted_char(uint8_t itf, char wanted) { | ||||
|   _cdcd_itf[itf].wanted_char = wanted; | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // READ API | ||||
| //--------------------------------------------------------------------+ | ||||
| uint32_t tud_cdc_n_available(uint8_t itf) | ||||
| { | ||||
| uint32_t tud_cdc_n_available(uint8_t itf) { | ||||
|   return tu_fifo_count(&_cdcd_itf[itf].rx_ff); | ||||
| } | ||||
|  | ||||
| uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize) | ||||
| { | ||||
| uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize) { | ||||
|   cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; | ||||
|   uint32_t num_read = tu_fifo_read_n(&p_cdc->rx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX)); | ||||
|   _prep_out_transaction(p_cdc); | ||||
|   return num_read; | ||||
| } | ||||
|  | ||||
| bool tud_cdc_n_peek(uint8_t itf, uint8_t* chr) | ||||
| { | ||||
| bool tud_cdc_n_peek(uint8_t itf, uint8_t* chr) { | ||||
|   return tu_fifo_peek(&_cdcd_itf[itf].rx_ff, chr); | ||||
| } | ||||
|  | ||||
| void tud_cdc_n_read_flush (uint8_t itf) | ||||
| { | ||||
| void tud_cdc_n_read_flush(uint8_t itf) { | ||||
|   cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; | ||||
|   tu_fifo_clear(&p_cdc->rx_ff); | ||||
|   _prep_out_transaction(p_cdc); | ||||
| @@ -188,16 +189,15 @@ void tud_cdc_n_read_flush (uint8_t itf) | ||||
| //--------------------------------------------------------------------+ | ||||
| // WRITE API | ||||
| //--------------------------------------------------------------------+ | ||||
| uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) | ||||
| { | ||||
| uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) { | ||||
|   cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; | ||||
|   uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX)); | ||||
|  | ||||
|   // flush if queue more than packet size | ||||
|   if ( tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE | ||||
|        #if CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE | ||||
|        || tu_fifo_full(&p_cdc->tx_ff) // check full if fifo size is less than packet size | ||||
|        #endif | ||||
|   if (tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE | ||||
|       #if CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE | ||||
|       || tu_fifo_full(&p_cdc->tx_ff) // check full if fifo size is less than packet size | ||||
|       #endif | ||||
|       ) { | ||||
|     tud_cdc_n_write_flush(itf); | ||||
|   } | ||||
| @@ -205,28 +205,25 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| uint32_t tud_cdc_n_write_flush (uint8_t itf) | ||||
| { | ||||
| uint32_t tud_cdc_n_write_flush(uint8_t itf) { | ||||
|   cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; | ||||
|  | ||||
|   // Skip if usb is not ready yet | ||||
|   TU_VERIFY( tud_ready(), 0 ); | ||||
|   TU_VERIFY(tud_ready(), 0); | ||||
|  | ||||
|   // No data to send | ||||
|   if ( !tu_fifo_count(&p_cdc->tx_ff) ) return 0; | ||||
|   if (!tu_fifo_count(&p_cdc->tx_ff)) return 0; | ||||
|  | ||||
|   // Claim the endpoint | ||||
|   TU_VERIFY( usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_in), 0 ); | ||||
|   TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_in), 0); | ||||
|  | ||||
|   // Pull data from FIFO | ||||
|   uint16_t const count = tu_fifo_read_n(&p_cdc->tx_ff, p_cdc->epin_buf, sizeof(p_cdc->epin_buf)); | ||||
|  | ||||
|   if ( count ) | ||||
|   { | ||||
|     TU_ASSERT( usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_in, p_cdc->epin_buf, count), 0 ); | ||||
|   if (count) { | ||||
|     TU_ASSERT(usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_in, p_cdc->epin_buf, count), 0); | ||||
|     return count; | ||||
|   }else | ||||
|   { | ||||
|   } else { | ||||
|     // Release endpoint since we don't make any transfer | ||||
|     // Note: data is dropped if terminal is not connected | ||||
|     usbd_edpt_release(p_cdc->rhport, p_cdc->ep_in); | ||||
| @@ -234,33 +231,30 @@ uint32_t tud_cdc_n_write_flush (uint8_t itf) | ||||
|   } | ||||
| } | ||||
|  | ||||
| uint32_t tud_cdc_n_write_available (uint8_t itf) | ||||
| { | ||||
| uint32_t tud_cdc_n_write_available(uint8_t itf) { | ||||
|   return tu_fifo_remaining(&_cdcd_itf[itf].tx_ff); | ||||
| } | ||||
|  | ||||
| bool tud_cdc_n_write_clear (uint8_t itf) | ||||
| { | ||||
| bool tud_cdc_n_write_clear(uint8_t itf) { | ||||
|   return tu_fifo_clear(&_cdcd_itf[itf].tx_ff); | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // USBD Driver API | ||||
| //--------------------------------------------------------------------+ | ||||
| void cdcd_init(void) | ||||
| { | ||||
| void cdcd_init(void) { | ||||
|   tu_memclr(_cdcd_itf, sizeof(_cdcd_itf)); | ||||
|   tu_memclr(&_cdcd_fifo_cfg, sizeof(_cdcd_fifo_cfg)); | ||||
|  | ||||
|   for(uint8_t i=0; i<CFG_TUD_CDC; i++) | ||||
|   { | ||||
|   for (uint8_t i = 0; i < CFG_TUD_CDC; i++) { | ||||
|     cdcd_interface_t* p_cdc = &_cdcd_itf[i]; | ||||
|  | ||||
|     p_cdc->wanted_char = (char) -1; | ||||
|  | ||||
|     // default line coding is : stop bit = 1, parity = none, data bits = 8 | ||||
|     p_cdc->line_coding.bit_rate  = 115200; | ||||
|     p_cdc->line_coding.bit_rate = 115200; | ||||
|     p_cdc->line_coding.stop_bits = 0; | ||||
|     p_cdc->line_coding.parity    = 0; | ||||
|     p_cdc->line_coding.parity = 0; | ||||
|     p_cdc->line_coding.data_bits = 8; | ||||
|  | ||||
|     // Config RX fifo | ||||
| @@ -304,33 +298,28 @@ bool cdcd_deinit(void) { | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| void cdcd_reset(uint8_t rhport) | ||||
| { | ||||
| void cdcd_reset(uint8_t rhport) { | ||||
|   (void) rhport; | ||||
|  | ||||
|   for(uint8_t i=0; i<CFG_TUD_CDC; i++) | ||||
|   { | ||||
|   for (uint8_t i = 0; i < CFG_TUD_CDC; i++) { | ||||
|     cdcd_interface_t* p_cdc = &_cdcd_itf[i]; | ||||
|  | ||||
|     tu_memclr(p_cdc, ITF_MEM_RESET_SIZE); | ||||
|     tu_fifo_clear(&p_cdc->rx_ff); | ||||
|     tu_fifo_clear(&p_cdc->tx_ff); | ||||
|     if (!_cdcd_fifo_cfg.rx_persistent) tu_fifo_clear(&p_cdc->rx_ff); | ||||
|     if (!_cdcd_fifo_cfg.tx_persistent) tu_fifo_clear(&p_cdc->tx_ff); | ||||
|     tu_fifo_set_overwritable(&p_cdc->tx_ff, true); | ||||
|   } | ||||
| } | ||||
|  | ||||
| uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) | ||||
| { | ||||
| uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) { | ||||
|   // Only support ACM subclass | ||||
|   TU_VERIFY( TUSB_CLASS_CDC                           == itf_desc->bInterfaceClass && | ||||
|              CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass, 0); | ||||
|  | ||||
|   // Find available interface | ||||
|   cdcd_interface_t * p_cdc = NULL; | ||||
|   for(uint8_t cdc_id=0; cdc_id<CFG_TUD_CDC; cdc_id++) | ||||
|   { | ||||
|     if ( _cdcd_itf[cdc_id].ep_in == 0 ) | ||||
|     { | ||||
|   cdcd_interface_t* p_cdc = NULL; | ||||
|   for (uint8_t cdc_id = 0; cdc_id < CFG_TUD_CDC; cdc_id++) { | ||||
|     if (_cdcd_itf[cdc_id].ep_in == 0) { | ||||
|       p_cdc = &_cdcd_itf[cdc_id]; | ||||
|       break; | ||||
|     } | ||||
| @@ -342,39 +331,36 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1 | ||||
|   p_cdc->itf_num = itf_desc->bInterfaceNumber; | ||||
|  | ||||
|   uint16_t drv_len = sizeof(tusb_desc_interface_t); | ||||
|   uint8_t const * p_desc = tu_desc_next( itf_desc ); | ||||
|   uint8_t const* p_desc = tu_desc_next(itf_desc); | ||||
|  | ||||
|   // Communication Functional Descriptors | ||||
|   while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len ) | ||||
|   { | ||||
|   while (TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len) { | ||||
|     drv_len += tu_desc_len(p_desc); | ||||
|     p_desc   = tu_desc_next(p_desc); | ||||
|     p_desc = tu_desc_next(p_desc); | ||||
|   } | ||||
|  | ||||
|   if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) ) | ||||
|   { | ||||
|   if (TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)) { | ||||
|     // notification endpoint | ||||
|     tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc; | ||||
|     tusb_desc_endpoint_t const* desc_ep = (tusb_desc_endpoint_t const*) p_desc; | ||||
|  | ||||
|     TU_ASSERT( usbd_edpt_open(rhport, desc_ep), 0 ); | ||||
|     TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0); | ||||
|     p_cdc->ep_notif = desc_ep->bEndpointAddress; | ||||
|  | ||||
|     drv_len += tu_desc_len(p_desc); | ||||
|     p_desc   = tu_desc_next(p_desc); | ||||
|     p_desc = tu_desc_next(p_desc); | ||||
|   } | ||||
|  | ||||
|   //------------- Data Interface (if any) -------------// | ||||
|   if ( (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && | ||||
|        (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) ) | ||||
|   { | ||||
|   if ((TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && | ||||
|       (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const*) p_desc)->bInterfaceClass)) { | ||||
|     // next to endpoint descriptor | ||||
|     drv_len += tu_desc_len(p_desc); | ||||
|     p_desc   = tu_desc_next(p_desc); | ||||
|     p_desc = tu_desc_next(p_desc); | ||||
|  | ||||
|     // Open endpoint pair | ||||
|     TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in), 0 ); | ||||
|     TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in), 0); | ||||
|  | ||||
|     drv_len += 2*sizeof(tusb_desc_endpoint_t); | ||||
|     drv_len += 2 * sizeof(tusb_desc_endpoint_t); | ||||
|   } | ||||
|  | ||||
|   // Prepare for incoming data | ||||
| @@ -386,8 +372,7 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1 | ||||
| // Invoked when a control transfer occurred on an interface of this class | ||||
| // Driver response accordingly to the request and the transfer stage (setup/data/ack) | ||||
| // return false to stall control endpoint (e.g unsupported request) | ||||
| bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) | ||||
| { | ||||
| bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) { | ||||
|   // Handle class request only | ||||
|   TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS); | ||||
|  | ||||
| @@ -395,42 +380,33 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t | ||||
|   cdcd_interface_t* p_cdc = _cdcd_itf; | ||||
|  | ||||
|   // Identify which interface to use | ||||
|   for ( ; ; itf++, p_cdc++) | ||||
|   { | ||||
|   for (;; itf++, p_cdc++) { | ||||
|     if (itf >= TU_ARRAY_SIZE(_cdcd_itf)) return false; | ||||
|  | ||||
|     if ( p_cdc->itf_num == request->wIndex ) break; | ||||
|     if (p_cdc->itf_num == request->wIndex) break; | ||||
|   } | ||||
|  | ||||
|   switch ( request->bRequest ) | ||||
|   { | ||||
|   switch (request->bRequest) { | ||||
|     case CDC_REQUEST_SET_LINE_CODING: | ||||
|       if (stage == CONTROL_STAGE_SETUP) | ||||
|       { | ||||
|       if (stage == CONTROL_STAGE_SETUP) { | ||||
|         TU_LOG_DRV("  Set Line Coding\r\n"); | ||||
|         tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t)); | ||||
|       } else if (stage == CONTROL_STAGE_ACK) { | ||||
|         if (tud_cdc_line_coding_cb) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding); | ||||
|       } | ||||
|       else if ( stage == CONTROL_STAGE_ACK) | ||||
|       { | ||||
|         if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding); | ||||
|       } | ||||
|     break; | ||||
|       break; | ||||
|  | ||||
|     case CDC_REQUEST_GET_LINE_CODING: | ||||
|       if (stage == CONTROL_STAGE_SETUP) | ||||
|       { | ||||
|       if (stage == CONTROL_STAGE_SETUP) { | ||||
|         TU_LOG_DRV("  Get Line Coding\r\n"); | ||||
|         tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t)); | ||||
|       } | ||||
|     break; | ||||
|       break; | ||||
|  | ||||
|     case CDC_REQUEST_SET_CONTROL_LINE_STATE: | ||||
|       if (stage == CONTROL_STAGE_SETUP) | ||||
|       { | ||||
|       if (stage == CONTROL_STAGE_SETUP) { | ||||
|         tud_control_status(rhport, request); | ||||
|       } | ||||
|       else if (stage == CONTROL_STAGE_ACK) | ||||
|       { | ||||
|       } else if (stage == CONTROL_STAGE_ACK) { | ||||
|         // CDC PSTN v1.2 section 6.3.12 | ||||
|         // Bit 0: Indicates if DTE is present or not. | ||||
|         //        This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR (Data Terminal Ready) | ||||
| @@ -447,61 +423,54 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t | ||||
|         TU_LOG_DRV("  Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts); | ||||
|  | ||||
|         // Invoke callback | ||||
|         if ( tud_cdc_line_state_cb ) tud_cdc_line_state_cb(itf, dtr, rts); | ||||
|         if (tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, dtr, rts); | ||||
|       } | ||||
|     break; | ||||
|     case CDC_REQUEST_SEND_BREAK: | ||||
|       if (stage == CONTROL_STAGE_SETUP) | ||||
|       { | ||||
|         tud_control_status(rhport, request); | ||||
|       } | ||||
|       else if (stage == CONTROL_STAGE_ACK) | ||||
|       { | ||||
|         TU_LOG_DRV("  Send Break\r\n"); | ||||
|         if ( tud_cdc_send_break_cb ) tud_cdc_send_break_cb(itf, request->wValue); | ||||
|       } | ||||
|     break; | ||||
|       break; | ||||
|  | ||||
|     default: return false; // stall unsupported request | ||||
|     case CDC_REQUEST_SEND_BREAK: | ||||
|       if (stage == CONTROL_STAGE_SETUP) { | ||||
|         tud_control_status(rhport, request); | ||||
|       } else if (stage == CONTROL_STAGE_ACK) { | ||||
|         TU_LOG_DRV("  Send Break\r\n"); | ||||
|         if (tud_cdc_send_break_cb) tud_cdc_send_break_cb(itf, request->wValue); | ||||
|       } | ||||
|       break; | ||||
|  | ||||
|     default: | ||||
|       return false; // stall unsupported request | ||||
|   } | ||||
|  | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) | ||||
| { | ||||
| bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { | ||||
|   (void) result; | ||||
|  | ||||
|   uint8_t itf; | ||||
|   cdcd_interface_t* p_cdc; | ||||
|  | ||||
|   // Identify which interface to use | ||||
|   for (itf = 0; itf < CFG_TUD_CDC; itf++) | ||||
|   { | ||||
|   for (itf = 0; itf < CFG_TUD_CDC; itf++) { | ||||
|     p_cdc = &_cdcd_itf[itf]; | ||||
|     if ( ( ep_addr == p_cdc->ep_out ) || ( ep_addr == p_cdc->ep_in ) || ( ep_addr == p_cdc->ep_notif )) break; | ||||
|     if ((ep_addr == p_cdc->ep_out) || (ep_addr == p_cdc->ep_in)|| (ep_addr == p_cdc->ep_notif)) break; | ||||
|   } | ||||
|   TU_ASSERT(itf < CFG_TUD_CDC); | ||||
|  | ||||
|   // Received new data | ||||
|   if ( ep_addr == p_cdc->ep_out ) | ||||
|   { | ||||
|   if (ep_addr == p_cdc->ep_out) { | ||||
|     tu_fifo_write_n(&p_cdc->rx_ff, p_cdc->epout_buf, (uint16_t) xferred_bytes); | ||||
|  | ||||
|     // Check for wanted char and invoke callback if needed | ||||
|     if ( tud_cdc_rx_wanted_cb && (((signed char) p_cdc->wanted_char) != -1) ) | ||||
|     { | ||||
|       for ( uint32_t i = 0; i < xferred_bytes; i++ ) | ||||
|       { | ||||
|         if ( (p_cdc->wanted_char == p_cdc->epout_buf[i]) && !tu_fifo_empty(&p_cdc->rx_ff) ) | ||||
|         { | ||||
|     if (tud_cdc_rx_wanted_cb && (((signed char) p_cdc->wanted_char) != -1)) { | ||||
|       for (uint32_t i = 0; i < xferred_bytes; i++) { | ||||
|         if ((p_cdc->wanted_char == p_cdc->epout_buf[i]) && !tu_fifo_empty(&p_cdc->rx_ff)) { | ||||
|           tud_cdc_rx_wanted_cb(itf, p_cdc->wanted_char); | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     // invoke receive callback (if there is still data) | ||||
|     if (tud_cdc_rx_cb && !tu_fifo_empty(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf); | ||||
|     if (tud_cdc_rx_cb && !tu_fifo_empty(&p_cdc->rx_ff)) tud_cdc_rx_cb(itf); | ||||
|  | ||||
|     // prepare for OUT transaction | ||||
|     _prep_out_transaction(p_cdc); | ||||
| @@ -510,19 +479,15 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ | ||||
|   // Data sent to host, we continue to fetch from tx fifo to send. | ||||
|   // Note: This will cause incorrect baudrate set in line coding. | ||||
|   //       Though maybe the baudrate is not really important !!! | ||||
|   if ( ep_addr == p_cdc->ep_in ) | ||||
|   { | ||||
|   if (ep_addr == p_cdc->ep_in) { | ||||
|     // invoke transmit callback to possibly refill tx fifo | ||||
|     if ( tud_cdc_tx_complete_cb ) tud_cdc_tx_complete_cb(itf); | ||||
|     if (tud_cdc_tx_complete_cb) tud_cdc_tx_complete_cb(itf); | ||||
|  | ||||
|     if ( 0 == tud_cdc_n_write_flush(itf) ) | ||||
|     { | ||||
|     if (0 == tud_cdc_n_write_flush(itf)) { | ||||
|       // If there is no data left, a ZLP should be sent if | ||||
|       // xferred_bytes is multiple of EP Packet size and not zero | ||||
|       if ( !tu_fifo_count(&p_cdc->tx_ff) && xferred_bytes && (0 == (xferred_bytes & (BULK_PACKET_SIZE-1))) ) | ||||
|       { | ||||
|         if ( usbd_edpt_claim(rhport, p_cdc->ep_in) ) | ||||
|         { | ||||
|       if (!tu_fifo_count(&p_cdc->tx_ff) && xferred_bytes && (0 == (xferred_bytes & (BULK_PACKET_SIZE - 1)))) { | ||||
|         if (usbd_edpt_claim(rhport, p_cdc->ep_in)) { | ||||
|           usbd_edpt_xfer(rhport, p_cdc->ep_in, NULL, 0); | ||||
|         } | ||||
|       } | ||||
|   | ||||
| @@ -24,8 +24,8 @@ | ||||
|  * This file is part of the TinyUSB stack. | ||||
|  */ | ||||
|  | ||||
| #ifndef _TUSB_CDC_DEVICE_H_ | ||||
| #define _TUSB_CDC_DEVICE_H_ | ||||
| #ifndef TUSB_CDC_DEVICE_H_ | ||||
| #define TUSB_CDC_DEVICE_H_ | ||||
|  | ||||
| #include "cdc.h" | ||||
|  | ||||
| @@ -45,88 +45,152 @@ | ||||
|  extern "C" { | ||||
| #endif | ||||
|  | ||||
| /** \addtogroup CDC_Serial Serial | ||||
|  *  @{ | ||||
|  *  \defgroup   CDC_Serial_Device Device | ||||
|  *  @{ */ | ||||
| //--------------------------------------------------------------------+ | ||||
| // Driver Configuration | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| typedef struct TU_ATTR_PACKED { | ||||
|   uint8_t rx_persistent : 1; // keep rx fifo on bus reset or disconnect | ||||
|   uint8_t tx_persistent : 1; // keep tx fifo on bus reset or disconnect | ||||
| } tud_cdc_configure_fifo_t; | ||||
|  | ||||
| // Configure CDC FIFOs behavior | ||||
| bool tud_cdc_configure_fifo(tud_cdc_configure_fifo_t const* cfg); | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Application API (Multiple Ports) | ||||
| // CFG_TUD_CDC > 1 | ||||
| // Application API (Multiple Ports) i.e. CFG_TUD_CDC > 1 | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| // Check if interface is ready | ||||
| bool tud_cdc_n_ready(uint8_t itf); | ||||
|  | ||||
| // Check if terminal is connected to this port | ||||
| bool     tud_cdc_n_connected       (uint8_t itf); | ||||
| bool tud_cdc_n_connected(uint8_t itf); | ||||
|  | ||||
| // Get current line state. Bit 0:  DTR (Data Terminal Ready), Bit 1: RTS (Request to Send) | ||||
| uint8_t  tud_cdc_n_get_line_state  (uint8_t itf); | ||||
| uint8_t tud_cdc_n_get_line_state(uint8_t itf); | ||||
|  | ||||
| // Get current line encoding: bit rate, stop bits parity etc .. | ||||
| void     tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding); | ||||
| void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t* coding); | ||||
|  | ||||
| // Send UART status notification: DCD, DSR etc .. | ||||
| bool     tud_cdc_n_send_uart_state (uint8_t itf, cdc_uart_state_t state); | ||||
| bool tud_cdc_n_send_uart_state(uint8_t itf, cdc_uart_state_t state); | ||||
|  | ||||
| // Set special character that will trigger tud_cdc_rx_wanted_cb() callback on receiving | ||||
| void     tud_cdc_n_set_wanted_char (uint8_t itf, char wanted); | ||||
| void tud_cdc_n_set_wanted_char(uint8_t itf, char wanted); | ||||
|  | ||||
| // Get the number of bytes available for reading | ||||
| uint32_t tud_cdc_n_available       (uint8_t itf); | ||||
| uint32_t tud_cdc_n_available(uint8_t itf); | ||||
|  | ||||
| // Read received bytes | ||||
| uint32_t tud_cdc_n_read            (uint8_t itf, void* buffer, uint32_t bufsize); | ||||
| uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize); | ||||
|  | ||||
| // Read a byte, return -1 if there is none | ||||
| static inline | ||||
| int32_t  tud_cdc_n_read_char       (uint8_t itf); | ||||
| TU_ATTR_ALWAYS_INLINE static inline int32_t tud_cdc_n_read_char(uint8_t itf) { | ||||
|   uint8_t ch; | ||||
|   return tud_cdc_n_read(itf, &ch, 1) ? (int32_t) ch : -1; | ||||
| } | ||||
|  | ||||
| // Clear the received FIFO | ||||
| void     tud_cdc_n_read_flush      (uint8_t itf); | ||||
| void tud_cdc_n_read_flush(uint8_t itf); | ||||
|  | ||||
| // Get a byte from FIFO without removing it | ||||
| bool     tud_cdc_n_peek            (uint8_t itf, uint8_t* ui8); | ||||
| bool tud_cdc_n_peek(uint8_t itf, uint8_t* ui8); | ||||
|  | ||||
| // Write bytes to TX FIFO, data may remain in the FIFO for a while | ||||
| uint32_t tud_cdc_n_write           (uint8_t itf, void const* buffer, uint32_t bufsize); | ||||
| uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize); | ||||
|  | ||||
| // Write a byte | ||||
| static inline | ||||
| uint32_t tud_cdc_n_write_char      (uint8_t itf, char ch); | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_n_write_char(uint8_t itf, char ch) { | ||||
|   return tud_cdc_n_write(itf, &ch, 1); | ||||
| } | ||||
|  | ||||
| // Write a null-terminated string | ||||
| static inline | ||||
| uint32_t tud_cdc_n_write_str       (uint8_t itf, char const* str); | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_n_write_str(uint8_t itf, char const* str) { | ||||
|   return tud_cdc_n_write(itf, str, strlen(str)); | ||||
| } | ||||
|  | ||||
| // Force sending data if possible, return number of forced bytes | ||||
| uint32_t tud_cdc_n_write_flush     (uint8_t itf); | ||||
| uint32_t tud_cdc_n_write_flush(uint8_t itf); | ||||
|  | ||||
| // Return the number of bytes (characters) available for writing to TX FIFO buffer in a single n_write operation. | ||||
| uint32_t tud_cdc_n_write_available (uint8_t itf); | ||||
| uint32_t tud_cdc_n_write_available(uint8_t itf); | ||||
|  | ||||
| // Clear the transmit FIFO | ||||
| bool tud_cdc_n_write_clear (uint8_t itf); | ||||
| bool tud_cdc_n_write_clear(uint8_t itf); | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Application API (Single Port) | ||||
| //--------------------------------------------------------------------+ | ||||
| static inline bool     tud_cdc_connected       (void); | ||||
| static inline uint8_t  tud_cdc_get_line_state  (void); | ||||
| static inline void     tud_cdc_get_line_coding (cdc_line_coding_t* coding); | ||||
| static inline bool     tud_cdc_send_uart_state (cdc_uart_state_t state); | ||||
| static inline void     tud_cdc_set_wanted_char (char wanted); | ||||
|  | ||||
| static inline uint32_t tud_cdc_available       (void); | ||||
| static inline int32_t  tud_cdc_read_char       (void); | ||||
| static inline uint32_t tud_cdc_read            (void* buffer, uint32_t bufsize); | ||||
| static inline void     tud_cdc_read_flush      (void); | ||||
| static inline bool     tud_cdc_peek            (uint8_t* ui8); | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_ready(void) { | ||||
|   return tud_cdc_n_ready(0); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_write_char      (char ch); | ||||
| static inline uint32_t tud_cdc_write           (void const* buffer, uint32_t bufsize); | ||||
| static inline uint32_t tud_cdc_write_str       (char const* str); | ||||
| static inline uint32_t tud_cdc_write_flush     (void); | ||||
| static inline uint32_t tud_cdc_write_available (void); | ||||
| static inline bool     tud_cdc_write_clear     (void); | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_connected(void) { | ||||
|   return tud_cdc_n_connected(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint8_t tud_cdc_get_line_state(void) { | ||||
|   return tud_cdc_n_get_line_state(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_get_line_coding(cdc_line_coding_t* coding) { | ||||
|   tud_cdc_n_get_line_coding(0, coding); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_send_uart_state(cdc_uart_state_t state) { | ||||
|   return tud_cdc_n_send_uart_state(0, state); | ||||
| } | ||||
|  | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_set_wanted_char(char wanted) { | ||||
|   tud_cdc_n_set_wanted_char(0, wanted); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_available(void) { | ||||
|   return tud_cdc_n_available(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline int32_t tud_cdc_read_char(void) { | ||||
|   return tud_cdc_n_read_char(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_read(void* buffer, uint32_t bufsize) { | ||||
|   return tud_cdc_n_read(0, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_read_flush(void) { | ||||
|   tud_cdc_n_read_flush(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_peek(uint8_t* ui8) { | ||||
|   return tud_cdc_n_peek(0, ui8); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_char(char ch) { | ||||
|   return tud_cdc_n_write_char(0, ch); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write(void const* buffer, uint32_t bufsize) { | ||||
|   return tud_cdc_n_write(0, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_str(char const* str) { | ||||
|   return tud_cdc_n_write_str(0, str); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_flush(void) { | ||||
|   return tud_cdc_n_write_flush(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_available(void) { | ||||
|   return tud_cdc_n_write_available(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_write_clear(void) { | ||||
|   return tud_cdc_n_write_clear(0); | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Application Callback API (weak is optional) | ||||
| @@ -150,108 +214,6 @@ TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p | ||||
| // Invoked when received send break | ||||
| TU_ATTR_WEAK void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms); | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Inline Functions | ||||
| //--------------------------------------------------------------------+ | ||||
| static inline int32_t tud_cdc_n_read_char (uint8_t itf) | ||||
| { | ||||
|   uint8_t ch; | ||||
|   return tud_cdc_n_read(itf, &ch, 1) ? (int32_t) ch : -1; | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_n_write_char(uint8_t itf, char ch) | ||||
| { | ||||
|   return tud_cdc_n_write(itf, &ch, 1); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str) | ||||
| { | ||||
|   return tud_cdc_n_write(itf, str, strlen(str)); | ||||
| } | ||||
|  | ||||
| static inline bool tud_cdc_connected (void) | ||||
| { | ||||
|   return tud_cdc_n_connected(0); | ||||
| } | ||||
|  | ||||
| static inline uint8_t tud_cdc_get_line_state (void) | ||||
| { | ||||
|   return tud_cdc_n_get_line_state(0); | ||||
| } | ||||
|  | ||||
| static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding) | ||||
| { | ||||
|   tud_cdc_n_get_line_coding(0, coding); | ||||
| } | ||||
|  | ||||
| static inline bool tud_cdc_send_uart_state (cdc_uart_state_t state) | ||||
| { | ||||
|   return tud_cdc_n_send_uart_state(0, state); | ||||
| } | ||||
|  | ||||
| static inline void tud_cdc_set_wanted_char (char wanted) | ||||
| { | ||||
|   tud_cdc_n_set_wanted_char(0, wanted); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_available (void) | ||||
| { | ||||
|   return tud_cdc_n_available(0); | ||||
| } | ||||
|  | ||||
| static inline int32_t tud_cdc_read_char (void) | ||||
| { | ||||
|   return tud_cdc_n_read_char(0); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize) | ||||
| { | ||||
|   return tud_cdc_n_read(0, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| static inline void tud_cdc_read_flush (void) | ||||
| { | ||||
|   tud_cdc_n_read_flush(0); | ||||
| } | ||||
|  | ||||
| static inline bool tud_cdc_peek (uint8_t* ui8) | ||||
| { | ||||
|   return tud_cdc_n_peek(0, ui8); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_write_char (char ch) | ||||
| { | ||||
|   return tud_cdc_n_write_char(0, ch); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize) | ||||
| { | ||||
|   return tud_cdc_n_write(0, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_write_str (char const* str) | ||||
| { | ||||
|   return tud_cdc_n_write_str(0, str); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_write_flush (void) | ||||
| { | ||||
|   return tud_cdc_n_write_flush(0); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_cdc_write_available(void) | ||||
| { | ||||
|   return tud_cdc_n_write_available(0); | ||||
| } | ||||
|  | ||||
| static inline bool tud_cdc_write_clear(void) | ||||
| { | ||||
|   return tud_cdc_n_write_clear(0); | ||||
| } | ||||
|  | ||||
| /** @} */ | ||||
| /** @} */ | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // INTERNAL USBD-CLASS DRIVER API | ||||
| //--------------------------------------------------------------------+ | ||||
|   | ||||
| @@ -341,14 +341,14 @@ uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize) { | ||||
|   cdch_interface_t* p_cdc = get_itf(idx); | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   return tu_edpt_stream_write(&p_cdc->stream.tx, buffer, bufsize); | ||||
|   return tu_edpt_stream_write(p_cdc->daddr, &p_cdc->stream.tx, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| uint32_t tuh_cdc_write_flush(uint8_t idx) { | ||||
|   cdch_interface_t* p_cdc = get_itf(idx); | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   return tu_edpt_stream_write_xfer(&p_cdc->stream.tx); | ||||
|   return tu_edpt_stream_write_xfer(p_cdc->daddr, &p_cdc->stream.tx); | ||||
| } | ||||
|  | ||||
| bool tuh_cdc_write_clear(uint8_t idx) { | ||||
| @@ -362,7 +362,7 @@ uint32_t tuh_cdc_write_available(uint8_t idx) { | ||||
|   cdch_interface_t* p_cdc = get_itf(idx); | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   return tu_edpt_stream_write_available(&p_cdc->stream.tx); | ||||
|   return tu_edpt_stream_write_available(p_cdc->daddr, &p_cdc->stream.tx); | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| @@ -373,7 +373,7 @@ uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize) { | ||||
|   cdch_interface_t* p_cdc = get_itf(idx); | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   return tu_edpt_stream_read(&p_cdc->stream.rx, buffer, bufsize); | ||||
|   return tu_edpt_stream_read(p_cdc->daddr, &p_cdc->stream.rx, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| uint32_t tuh_cdc_read_available(uint8_t idx) { | ||||
| @@ -395,7 +395,7 @@ bool tuh_cdc_read_clear (uint8_t idx) { | ||||
|   TU_VERIFY(p_cdc); | ||||
|  | ||||
|   bool ret = tu_edpt_stream_clear(&p_cdc->stream.rx); | ||||
|   tu_edpt_stream_read_xfer(&p_cdc->stream.rx); | ||||
|   tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx); | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| @@ -677,10 +677,10 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t | ||||
|     // invoke tx complete callback to possibly refill tx fifo | ||||
|     if (tuh_cdc_tx_complete_cb) tuh_cdc_tx_complete_cb(idx); | ||||
|  | ||||
|     if ( 0 == tu_edpt_stream_write_xfer(&p_cdc->stream.tx) ) { | ||||
|     if ( 0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx) ) { | ||||
|       // If there is no data left, a ZLP should be sent if: | ||||
|       // - xferred_bytes is multiple of EP Packet size and not zero | ||||
|       tu_edpt_stream_write_zlp_if_needed(&p_cdc->stream.tx, xferred_bytes); | ||||
|       tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes); | ||||
|     } | ||||
|   } else if ( ep_addr == p_cdc->stream.rx.ep_addr ) { | ||||
|     #if CFG_TUH_CDC_FTDI | ||||
| @@ -698,7 +698,7 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t | ||||
|     if (tuh_cdc_rx_cb) tuh_cdc_rx_cb(idx); | ||||
|  | ||||
|     // prepare for next transfer if needed | ||||
|     tu_edpt_stream_read_xfer(&p_cdc->stream.rx); | ||||
|     tu_edpt_stream_read_xfer(daddr, &p_cdc->stream.rx); | ||||
|   }else if ( ep_addr == p_cdc->ep_notif ) { | ||||
|     // TODO handle notification endpoint | ||||
|   }else { | ||||
| @@ -719,9 +719,9 @@ static bool open_ep_stream_pair(cdch_interface_t* p_cdc, tusb_desc_endpoint_t co | ||||
|     TU_ASSERT(tuh_edpt_open(p_cdc->daddr, desc_ep)); | ||||
|  | ||||
|     if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) { | ||||
|       tu_edpt_stream_open(&p_cdc->stream.rx, p_cdc->daddr, desc_ep); | ||||
|       tu_edpt_stream_open(&p_cdc->stream.rx, desc_ep); | ||||
|     } else { | ||||
|       tu_edpt_stream_open(&p_cdc->stream.tx, p_cdc->daddr, desc_ep); | ||||
|       tu_edpt_stream_open(&p_cdc->stream.tx, desc_ep); | ||||
|     } | ||||
|  | ||||
|     desc_ep = (tusb_desc_endpoint_t const*) tu_desc_next(desc_ep); | ||||
| @@ -763,7 +763,7 @@ static void set_config_complete(cdch_interface_t * p_cdc, uint8_t idx, uint8_t i | ||||
|   if (tuh_cdc_mount_cb) tuh_cdc_mount_cb(idx); | ||||
|  | ||||
|   // Prepare for incoming data | ||||
|   tu_edpt_stream_read_xfer(&p_cdc->stream.rx); | ||||
|   tu_edpt_stream_read_xfer(p_cdc->daddr, &p_cdc->stream.rx); | ||||
|  | ||||
|   // notify usbh that driver enumeration is complete | ||||
|   usbh_driver_set_config_complete(p_cdc->daddr, itf_num); | ||||
|   | ||||
| @@ -366,177 +366,224 @@ typedef enum | ||||
| //--------------------------------------------------------------------+ | ||||
| // HID KEYCODE | ||||
| //--------------------------------------------------------------------+ | ||||
| #define HID_KEY_NONE                      0x00 | ||||
| #define HID_KEY_A                         0x04 | ||||
| #define HID_KEY_B                         0x05 | ||||
| #define HID_KEY_C                         0x06 | ||||
| #define HID_KEY_D                         0x07 | ||||
| #define HID_KEY_E                         0x08 | ||||
| #define HID_KEY_F                         0x09 | ||||
| #define HID_KEY_G                         0x0A | ||||
| #define HID_KEY_H                         0x0B | ||||
| #define HID_KEY_I                         0x0C | ||||
| #define HID_KEY_J                         0x0D | ||||
| #define HID_KEY_K                         0x0E | ||||
| #define HID_KEY_L                         0x0F | ||||
| #define HID_KEY_M                         0x10 | ||||
| #define HID_KEY_N                         0x11 | ||||
| #define HID_KEY_O                         0x12 | ||||
| #define HID_KEY_P                         0x13 | ||||
| #define HID_KEY_Q                         0x14 | ||||
| #define HID_KEY_R                         0x15 | ||||
| #define HID_KEY_S                         0x16 | ||||
| #define HID_KEY_T                         0x17 | ||||
| #define HID_KEY_U                         0x18 | ||||
| #define HID_KEY_V                         0x19 | ||||
| #define HID_KEY_W                         0x1A | ||||
| #define HID_KEY_X                         0x1B | ||||
| #define HID_KEY_Y                         0x1C | ||||
| #define HID_KEY_Z                         0x1D | ||||
| #define HID_KEY_1                         0x1E | ||||
| #define HID_KEY_2                         0x1F | ||||
| #define HID_KEY_3                         0x20 | ||||
| #define HID_KEY_4                         0x21 | ||||
| #define HID_KEY_5                         0x22 | ||||
| #define HID_KEY_6                         0x23 | ||||
| #define HID_KEY_7                         0x24 | ||||
| #define HID_KEY_8                         0x25 | ||||
| #define HID_KEY_9                         0x26 | ||||
| #define HID_KEY_0                         0x27 | ||||
| #define HID_KEY_ENTER                     0x28 | ||||
| #define HID_KEY_ESCAPE                    0x29 | ||||
| #define HID_KEY_BACKSPACE                 0x2A | ||||
| #define HID_KEY_TAB                       0x2B | ||||
| #define HID_KEY_SPACE                     0x2C | ||||
| #define HID_KEY_MINUS                     0x2D | ||||
| #define HID_KEY_EQUAL                     0x2E | ||||
| #define HID_KEY_BRACKET_LEFT              0x2F | ||||
| #define HID_KEY_BRACKET_RIGHT             0x30 | ||||
| #define HID_KEY_BACKSLASH                 0x31 | ||||
| #define HID_KEY_EUROPE_1                  0x32 | ||||
| #define HID_KEY_SEMICOLON                 0x33 | ||||
| #define HID_KEY_APOSTROPHE                0x34 | ||||
| #define HID_KEY_GRAVE                     0x35 | ||||
| #define HID_KEY_COMMA                     0x36 | ||||
| #define HID_KEY_PERIOD                    0x37 | ||||
| #define HID_KEY_SLASH                     0x38 | ||||
| #define HID_KEY_CAPS_LOCK                 0x39 | ||||
| #define HID_KEY_F1                        0x3A | ||||
| #define HID_KEY_F2                        0x3B | ||||
| #define HID_KEY_F3                        0x3C | ||||
| #define HID_KEY_F4                        0x3D | ||||
| #define HID_KEY_F5                        0x3E | ||||
| #define HID_KEY_F6                        0x3F | ||||
| #define HID_KEY_F7                        0x40 | ||||
| #define HID_KEY_F8                        0x41 | ||||
| #define HID_KEY_F9                        0x42 | ||||
| #define HID_KEY_F10                       0x43 | ||||
| #define HID_KEY_F11                       0x44 | ||||
| #define HID_KEY_F12                       0x45 | ||||
| #define HID_KEY_PRINT_SCREEN              0x46 | ||||
| #define HID_KEY_SCROLL_LOCK               0x47 | ||||
| #define HID_KEY_PAUSE                     0x48 | ||||
| #define HID_KEY_INSERT                    0x49 | ||||
| #define HID_KEY_HOME                      0x4A | ||||
| #define HID_KEY_PAGE_UP                   0x4B | ||||
| #define HID_KEY_DELETE                    0x4C | ||||
| #define HID_KEY_END                       0x4D | ||||
| #define HID_KEY_PAGE_DOWN                 0x4E | ||||
| #define HID_KEY_ARROW_RIGHT               0x4F | ||||
| #define HID_KEY_ARROW_LEFT                0x50 | ||||
| #define HID_KEY_ARROW_DOWN                0x51 | ||||
| #define HID_KEY_ARROW_UP                  0x52 | ||||
| #define HID_KEY_NUM_LOCK                  0x53 | ||||
| #define HID_KEY_KEYPAD_DIVIDE             0x54 | ||||
| #define HID_KEY_KEYPAD_MULTIPLY           0x55 | ||||
| #define HID_KEY_KEYPAD_SUBTRACT           0x56 | ||||
| #define HID_KEY_KEYPAD_ADD                0x57 | ||||
| #define HID_KEY_KEYPAD_ENTER              0x58 | ||||
| #define HID_KEY_KEYPAD_1                  0x59 | ||||
| #define HID_KEY_KEYPAD_2                  0x5A | ||||
| #define HID_KEY_KEYPAD_3                  0x5B | ||||
| #define HID_KEY_KEYPAD_4                  0x5C | ||||
| #define HID_KEY_KEYPAD_5                  0x5D | ||||
| #define HID_KEY_KEYPAD_6                  0x5E | ||||
| #define HID_KEY_KEYPAD_7                  0x5F | ||||
| #define HID_KEY_KEYPAD_8                  0x60 | ||||
| #define HID_KEY_KEYPAD_9                  0x61 | ||||
| #define HID_KEY_KEYPAD_0                  0x62 | ||||
| #define HID_KEY_KEYPAD_DECIMAL            0x63 | ||||
| #define HID_KEY_EUROPE_2                  0x64 | ||||
| #define HID_KEY_APPLICATION               0x65 | ||||
| #define HID_KEY_POWER                     0x66 | ||||
| #define HID_KEY_KEYPAD_EQUAL              0x67 | ||||
| #define HID_KEY_F13                       0x68 | ||||
| #define HID_KEY_F14                       0x69 | ||||
| #define HID_KEY_F15                       0x6A | ||||
| #define HID_KEY_F16                       0x6B | ||||
| #define HID_KEY_F17                       0x6C | ||||
| #define HID_KEY_F18                       0x6D | ||||
| #define HID_KEY_F19                       0x6E | ||||
| #define HID_KEY_F20                       0x6F | ||||
| #define HID_KEY_F21                       0x70 | ||||
| #define HID_KEY_F22                       0x71 | ||||
| #define HID_KEY_F23                       0x72 | ||||
| #define HID_KEY_F24                       0x73 | ||||
| #define HID_KEY_EXECUTE                   0x74 | ||||
| #define HID_KEY_HELP                      0x75 | ||||
| #define HID_KEY_MENU                      0x76 | ||||
| #define HID_KEY_SELECT                    0x77 | ||||
| #define HID_KEY_STOP                      0x78 | ||||
| #define HID_KEY_AGAIN                     0x79 | ||||
| #define HID_KEY_UNDO                      0x7A | ||||
| #define HID_KEY_CUT                       0x7B | ||||
| #define HID_KEY_COPY                      0x7C | ||||
| #define HID_KEY_PASTE                     0x7D | ||||
| #define HID_KEY_FIND                      0x7E | ||||
| #define HID_KEY_MUTE                      0x7F | ||||
| #define HID_KEY_VOLUME_UP                 0x80 | ||||
| #define HID_KEY_VOLUME_DOWN               0x81 | ||||
| #define HID_KEY_LOCKING_CAPS_LOCK         0x82 | ||||
| #define HID_KEY_LOCKING_NUM_LOCK          0x83 | ||||
| #define HID_KEY_LOCKING_SCROLL_LOCK       0x84 | ||||
| #define HID_KEY_KEYPAD_COMMA              0x85 | ||||
| #define HID_KEY_KEYPAD_EQUAL_SIGN         0x86 | ||||
| #define HID_KEY_KANJI1                    0x87 | ||||
| #define HID_KEY_KANJI2                    0x88 | ||||
| #define HID_KEY_KANJI3                    0x89 | ||||
| #define HID_KEY_KANJI4                    0x8A | ||||
| #define HID_KEY_KANJI5                    0x8B | ||||
| #define HID_KEY_KANJI6                    0x8C | ||||
| #define HID_KEY_KANJI7                    0x8D | ||||
| #define HID_KEY_KANJI8                    0x8E | ||||
| #define HID_KEY_KANJI9                    0x8F | ||||
| #define HID_KEY_LANG1                     0x90 | ||||
| #define HID_KEY_LANG2                     0x91 | ||||
| #define HID_KEY_LANG3                     0x92 | ||||
| #define HID_KEY_LANG4                     0x93 | ||||
| #define HID_KEY_LANG5                     0x94 | ||||
| #define HID_KEY_LANG6                     0x95 | ||||
| #define HID_KEY_LANG7                     0x96 | ||||
| #define HID_KEY_LANG8                     0x97 | ||||
| #define HID_KEY_LANG9                     0x98 | ||||
| #define HID_KEY_ALTERNATE_ERASE           0x99 | ||||
| #define HID_KEY_SYSREQ_ATTENTION          0x9A | ||||
| #define HID_KEY_CANCEL                    0x9B | ||||
| #define HID_KEY_CLEAR                     0x9C | ||||
| #define HID_KEY_PRIOR                     0x9D | ||||
| #define HID_KEY_RETURN                    0x9E | ||||
| #define HID_KEY_SEPARATOR                 0x9F | ||||
| #define HID_KEY_OUT                       0xA0 | ||||
| #define HID_KEY_OPER                      0xA1 | ||||
| #define HID_KEY_CLEAR_AGAIN               0xA2 | ||||
| #define HID_KEY_CRSEL_PROPS               0xA3 | ||||
| #define HID_KEY_EXSEL                     0xA4 | ||||
| // RESERVED					                      0xA5-DF | ||||
| #define HID_KEY_CONTROL_LEFT              0xE0 | ||||
| #define HID_KEY_SHIFT_LEFT                0xE1 | ||||
| #define HID_KEY_ALT_LEFT                  0xE2 | ||||
| #define HID_KEY_GUI_LEFT                  0xE3 | ||||
| #define HID_KEY_CONTROL_RIGHT             0xE4 | ||||
| #define HID_KEY_SHIFT_RIGHT               0xE5 | ||||
| #define HID_KEY_ALT_RIGHT                 0xE6 | ||||
| #define HID_KEY_GUI_RIGHT                 0xE7 | ||||
| #define HID_KEY_NONE                        0x00 | ||||
| #define HID_KEY_A                           0x04 | ||||
| #define HID_KEY_B                           0x05 | ||||
| #define HID_KEY_C                           0x06 | ||||
| #define HID_KEY_D                           0x07 | ||||
| #define HID_KEY_E                           0x08 | ||||
| #define HID_KEY_F                           0x09 | ||||
| #define HID_KEY_G                           0x0A | ||||
| #define HID_KEY_H                           0x0B | ||||
| #define HID_KEY_I                           0x0C | ||||
| #define HID_KEY_J                           0x0D | ||||
| #define HID_KEY_K                           0x0E | ||||
| #define HID_KEY_L                           0x0F | ||||
| #define HID_KEY_M                           0x10 | ||||
| #define HID_KEY_N                           0x11 | ||||
| #define HID_KEY_O                           0x12 | ||||
| #define HID_KEY_P                           0x13 | ||||
| #define HID_KEY_Q                           0x14 | ||||
| #define HID_KEY_R                           0x15 | ||||
| #define HID_KEY_S                           0x16 | ||||
| #define HID_KEY_T                           0x17 | ||||
| #define HID_KEY_U                           0x18 | ||||
| #define HID_KEY_V                           0x19 | ||||
| #define HID_KEY_W                           0x1A | ||||
| #define HID_KEY_X                           0x1B | ||||
| #define HID_KEY_Y                           0x1C | ||||
| #define HID_KEY_Z                           0x1D | ||||
| #define HID_KEY_1                           0x1E | ||||
| #define HID_KEY_2                           0x1F | ||||
| #define HID_KEY_3                           0x20 | ||||
| #define HID_KEY_4                           0x21 | ||||
| #define HID_KEY_5                           0x22 | ||||
| #define HID_KEY_6                           0x23 | ||||
| #define HID_KEY_7                           0x24 | ||||
| #define HID_KEY_8                           0x25 | ||||
| #define HID_KEY_9                           0x26 | ||||
| #define HID_KEY_0                           0x27 | ||||
| #define HID_KEY_ENTER                       0x28 | ||||
| #define HID_KEY_ESCAPE                      0x29 | ||||
| #define HID_KEY_BACKSPACE                   0x2A | ||||
| #define HID_KEY_TAB                         0x2B | ||||
| #define HID_KEY_SPACE                       0x2C | ||||
| #define HID_KEY_MINUS                       0x2D | ||||
| #define HID_KEY_EQUAL                       0x2E | ||||
| #define HID_KEY_BRACKET_LEFT                0x2F | ||||
| #define HID_KEY_BRACKET_RIGHT               0x30 | ||||
| #define HID_KEY_BACKSLASH                   0x31 | ||||
| #define HID_KEY_EUROPE_1                    0x32 | ||||
| #define HID_KEY_SEMICOLON                   0x33 | ||||
| #define HID_KEY_APOSTROPHE                  0x34 | ||||
| #define HID_KEY_GRAVE                       0x35 | ||||
| #define HID_KEY_COMMA                       0x36 | ||||
| #define HID_KEY_PERIOD                      0x37 | ||||
| #define HID_KEY_SLASH                       0x38 | ||||
| #define HID_KEY_CAPS_LOCK                   0x39 | ||||
| #define HID_KEY_F1                          0x3A | ||||
| #define HID_KEY_F2                          0x3B | ||||
| #define HID_KEY_F3                          0x3C | ||||
| #define HID_KEY_F4                          0x3D | ||||
| #define HID_KEY_F5                          0x3E | ||||
| #define HID_KEY_F6                          0x3F | ||||
| #define HID_KEY_F7                          0x40 | ||||
| #define HID_KEY_F8                          0x41 | ||||
| #define HID_KEY_F9                          0x42 | ||||
| #define HID_KEY_F10                         0x43 | ||||
| #define HID_KEY_F11                         0x44 | ||||
| #define HID_KEY_F12                         0x45 | ||||
| #define HID_KEY_PRINT_SCREEN                0x46 | ||||
| #define HID_KEY_SCROLL_LOCK                 0x47 | ||||
| #define HID_KEY_PAUSE                       0x48 | ||||
| #define HID_KEY_INSERT                      0x49 | ||||
| #define HID_KEY_HOME                        0x4A | ||||
| #define HID_KEY_PAGE_UP                     0x4B | ||||
| #define HID_KEY_DELETE                      0x4C | ||||
| #define HID_KEY_END                         0x4D | ||||
| #define HID_KEY_PAGE_DOWN                   0x4E | ||||
| #define HID_KEY_ARROW_RIGHT                 0x4F | ||||
| #define HID_KEY_ARROW_LEFT                  0x50 | ||||
| #define HID_KEY_ARROW_DOWN                  0x51 | ||||
| #define HID_KEY_ARROW_UP                    0x52 | ||||
| #define HID_KEY_NUM_LOCK                    0x53 | ||||
| #define HID_KEY_KEYPAD_DIVIDE               0x54 | ||||
| #define HID_KEY_KEYPAD_MULTIPLY             0x55 | ||||
| #define HID_KEY_KEYPAD_SUBTRACT             0x56 | ||||
| #define HID_KEY_KEYPAD_ADD                  0x57 | ||||
| #define HID_KEY_KEYPAD_ENTER                0x58 | ||||
| #define HID_KEY_KEYPAD_1                    0x59 | ||||
| #define HID_KEY_KEYPAD_2                    0x5A | ||||
| #define HID_KEY_KEYPAD_3                    0x5B | ||||
| #define HID_KEY_KEYPAD_4                    0x5C | ||||
| #define HID_KEY_KEYPAD_5                    0x5D | ||||
| #define HID_KEY_KEYPAD_6                    0x5E | ||||
| #define HID_KEY_KEYPAD_7                    0x5F | ||||
| #define HID_KEY_KEYPAD_8                    0x60 | ||||
| #define HID_KEY_KEYPAD_9                    0x61 | ||||
| #define HID_KEY_KEYPAD_0                    0x62 | ||||
| #define HID_KEY_KEYPAD_DECIMAL              0x63 | ||||
| #define HID_KEY_EUROPE_2                    0x64 | ||||
| #define HID_KEY_APPLICATION                 0x65 | ||||
| #define HID_KEY_POWER                       0x66 | ||||
| #define HID_KEY_KEYPAD_EQUAL                0x67 | ||||
| #define HID_KEY_F13                         0x68 | ||||
| #define HID_KEY_F14                         0x69 | ||||
| #define HID_KEY_F15                         0x6A | ||||
| #define HID_KEY_F16                         0x6B | ||||
| #define HID_KEY_F17                         0x6C | ||||
| #define HID_KEY_F18                         0x6D | ||||
| #define HID_KEY_F19                         0x6E | ||||
| #define HID_KEY_F20                         0x6F | ||||
| #define HID_KEY_F21                         0x70 | ||||
| #define HID_KEY_F22                         0x71 | ||||
| #define HID_KEY_F23                         0x72 | ||||
| #define HID_KEY_F24                         0x73 | ||||
| #define HID_KEY_EXECUTE                     0x74 | ||||
| #define HID_KEY_HELP                        0x75 | ||||
| #define HID_KEY_MENU                        0x76 | ||||
| #define HID_KEY_SELECT                      0x77 | ||||
| #define HID_KEY_STOP                        0x78 | ||||
| #define HID_KEY_AGAIN                       0x79 | ||||
| #define HID_KEY_UNDO                        0x7A | ||||
| #define HID_KEY_CUT                         0x7B | ||||
| #define HID_KEY_COPY                        0x7C | ||||
| #define HID_KEY_PASTE                       0x7D | ||||
| #define HID_KEY_FIND                        0x7E | ||||
| #define HID_KEY_MUTE                        0x7F | ||||
| #define HID_KEY_VOLUME_UP                   0x80 | ||||
| #define HID_KEY_VOLUME_DOWN                 0x81 | ||||
| #define HID_KEY_LOCKING_CAPS_LOCK           0x82 | ||||
| #define HID_KEY_LOCKING_NUM_LOCK            0x83 | ||||
| #define HID_KEY_LOCKING_SCROLL_LOCK         0x84 | ||||
| #define HID_KEY_KEYPAD_COMMA                0x85 | ||||
| #define HID_KEY_KEYPAD_EQUAL_SIGN           0x86 | ||||
| #define HID_KEY_KANJI1                      0x87 | ||||
| #define HID_KEY_KANJI2                      0x88 | ||||
| #define HID_KEY_KANJI3                      0x89 | ||||
| #define HID_KEY_KANJI4                      0x8A | ||||
| #define HID_KEY_KANJI5                      0x8B | ||||
| #define HID_KEY_KANJI6                      0x8C | ||||
| #define HID_KEY_KANJI7                      0x8D | ||||
| #define HID_KEY_KANJI8                      0x8E | ||||
| #define HID_KEY_KANJI9                      0x8F | ||||
| #define HID_KEY_LANG1                       0x90 | ||||
| #define HID_KEY_LANG2                       0x91 | ||||
| #define HID_KEY_LANG3                       0x92 | ||||
| #define HID_KEY_LANG4                       0x93 | ||||
| #define HID_KEY_LANG5                       0x94 | ||||
| #define HID_KEY_LANG6                       0x95 | ||||
| #define HID_KEY_LANG7                       0x96 | ||||
| #define HID_KEY_LANG8                       0x97 | ||||
| #define HID_KEY_LANG9                       0x98 | ||||
| #define HID_KEY_ALTERNATE_ERASE             0x99 | ||||
| #define HID_KEY_SYSREQ_ATTENTION            0x9A | ||||
| #define HID_KEY_CANCEL                      0x9B | ||||
| #define HID_KEY_CLEAR                       0x9C | ||||
| #define HID_KEY_PRIOR                       0x9D | ||||
| #define HID_KEY_RETURN                      0x9E | ||||
| #define HID_KEY_SEPARATOR                   0x9F | ||||
| #define HID_KEY_OUT                         0xA0 | ||||
| #define HID_KEY_OPER                        0xA1 | ||||
| #define HID_KEY_CLEAR_AGAIN                 0xA2 | ||||
| #define HID_KEY_CRSEL_PROPS                 0xA3 | ||||
| #define HID_KEY_EXSEL                       0xA4 | ||||
| // RESERVED					                        0xA5-AF | ||||
| #define HID_KEY_KEYPAD_00                   0xB0 | ||||
| #define HID_KEY_KEYPAD_000                  0xB1 | ||||
| #define HID_KEY_THOUSANDS_SEPARATOR         0xB2 | ||||
| #define HID_KEY_DECIMAL_SEPARATOR           0xB3 | ||||
| #define HID_KEY_CURRENCY_UNIT               0xB4 | ||||
| #define HID_KEY_CURRENCY_SUBUNIT            0xB5 | ||||
| #define HID_KEY_KEYPAD_LEFT_PARENTHESIS     0xB6 | ||||
| #define HID_KEY_KEYPAD_RIGHT_PARENTHESIS    0xB7 | ||||
| #define HID_KEY_KEYPAD_LEFT_BRACE           0xB8 | ||||
| #define HID_KEY_KEYPAD_RIGHT_BRACE          0xB9 | ||||
| #define HID_KEY_KEYPAD_TAB                  0xBA | ||||
| #define HID_KEY_KEYPAD_BACKSPACE            0xBB | ||||
| #define HID_KEY_KEYPAD_A                    0xBC | ||||
| #define HID_KEY_KEYPAD_B                    0xBD | ||||
| #define HID_KEY_KEYPAD_C                    0xBE | ||||
| #define HID_KEY_KEYPAD_D                    0xBF | ||||
| #define HID_KEY_KEYPAD_E                    0xC0 | ||||
| #define HID_KEY_KEYPAD_F                    0xC1 | ||||
| #define HID_KEY_KEYPAD_XOR                  0xC2 | ||||
| #define HID_KEY_KEYPAD_CARET                0xC3 | ||||
| #define HID_KEY_KEYPAD_PERCENT              0xC4 | ||||
| #define HID_KEY_KEYPAD_LESS_THAN            0xC5 | ||||
| #define HID_KEY_KEYPAD_GREATER_THAN         0xC6 | ||||
| #define HID_KEY_KEYPAD_AMPERSAND            0xC7 | ||||
| #define HID_KEY_KEYPAD_DOUBLE_AMPERSAND     0xC8 | ||||
| #define HID_KEY_KEYPAD_VERTICAL_BAR         0xC9 | ||||
| #define HID_KEY_KEYPAD_DOUBLE_VERTICAL_BAR  0xCA | ||||
| #define HID_KEY_KEYPAD_COLON                0xCB | ||||
| #define HID_KEY_KEYPAD_HASH                 0xCC | ||||
| #define HID_KEY_KEYPAD_SPACE                0xCD | ||||
| #define HID_KEY_KEYPAD_AT                   0xCE | ||||
| #define HID_KEY_KEYPAD_EXCLAMATION          0xCF | ||||
| #define HID_KEY_KEYPAD_MEMORY_STORE         0xD0 | ||||
| #define HID_KEY_KEYPAD_MEMORY_RECALL        0xD1 | ||||
| #define HID_KEY_KEYPAD_MEMORY_CLEAR         0xD2 | ||||
| #define HID_KEY_KEYPAD_MEMORY_ADD           0xD3 | ||||
| #define HID_KEY_KEYPAD_MEMORY_SUBTRACT      0xD4 | ||||
| #define HID_KEY_KEYPAD_MEMORY_MULTIPLY      0xD5 | ||||
| #define HID_KEY_KEYPAD_MEMORY_DIVIDE        0xD6 | ||||
| #define HID_KEY_KEYPAD_PLUS_MINUS           0xD7 | ||||
| #define HID_KEY_KEYPAD_CLEAR                0xD8 | ||||
| #define HID_KEY_KEYPAD_CLEAR_ENTRY          0xD9 | ||||
| #define HID_KEY_KEYPAD_BINARY               0xDA | ||||
| #define HID_KEY_KEYPAD_OCTAL                0xDB | ||||
| #define HID_KEY_KEYPAD_DECIMAL_2            0xDC | ||||
| #define HID_KEY_KEYPAD_HEXADECIMAL          0xDD | ||||
| // RESERVED					                        0xDE-DF | ||||
| #define HID_KEY_CONTROL_LEFT                0xE0 | ||||
| #define HID_KEY_SHIFT_LEFT                  0xE1 | ||||
| #define HID_KEY_ALT_LEFT                    0xE2 | ||||
| #define HID_KEY_GUI_LEFT                    0xE3 | ||||
| #define HID_KEY_CONTROL_RIGHT               0xE4 | ||||
| #define HID_KEY_SHIFT_RIGHT                 0xE5 | ||||
| #define HID_KEY_ALT_RIGHT                   0xE6 | ||||
| #define HID_KEY_GUI_RIGHT                   0xE7 | ||||
|  | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| @@ -697,32 +744,33 @@ enum { | ||||
|  | ||||
| /// HID Usage Table - Table 1: Usage Page Summary | ||||
| enum { | ||||
|   HID_USAGE_PAGE_DESKTOP         = 0x01, | ||||
|   HID_USAGE_PAGE_SIMULATE        = 0x02, | ||||
|   HID_USAGE_PAGE_VIRTUAL_REALITY = 0x03, | ||||
|   HID_USAGE_PAGE_SPORT           = 0x04, | ||||
|   HID_USAGE_PAGE_GAME            = 0x05, | ||||
|   HID_USAGE_PAGE_GENERIC_DEVICE  = 0x06, | ||||
|   HID_USAGE_PAGE_KEYBOARD        = 0x07, | ||||
|   HID_USAGE_PAGE_LED             = 0x08, | ||||
|   HID_USAGE_PAGE_BUTTON          = 0x09, | ||||
|   HID_USAGE_PAGE_ORDINAL         = 0x0a, | ||||
|   HID_USAGE_PAGE_TELEPHONY       = 0x0b, | ||||
|   HID_USAGE_PAGE_CONSUMER        = 0x0c, | ||||
|   HID_USAGE_PAGE_DIGITIZER       = 0x0d, | ||||
|   HID_USAGE_PAGE_PID             = 0x0f, | ||||
|   HID_USAGE_PAGE_UNICODE         = 0x10, | ||||
|   HID_USAGE_PAGE_ALPHA_DISPLAY   = 0x14, | ||||
|   HID_USAGE_PAGE_MEDICAL         = 0x40, | ||||
|   HID_USAGE_PAGE_MONITOR         = 0x80, //0x80 - 0x83 | ||||
|   HID_USAGE_PAGE_POWER           = 0x84, // 0x084 - 0x87 | ||||
|   HID_USAGE_PAGE_BARCODE_SCANNER = 0x8c, | ||||
|   HID_USAGE_PAGE_SCALE           = 0x8d, | ||||
|   HID_USAGE_PAGE_MSR             = 0x8e, | ||||
|   HID_USAGE_PAGE_CAMERA          = 0x90, | ||||
|   HID_USAGE_PAGE_ARCADE          = 0x91, | ||||
|   HID_USAGE_PAGE_FIDO            = 0xF1D0, // FIDO alliance HID usage page | ||||
|   HID_USAGE_PAGE_VENDOR          = 0xFF00 // 0xFF00 - 0xFFFF | ||||
|   HID_USAGE_PAGE_DESKTOP                   = 0x01, | ||||
|   HID_USAGE_PAGE_SIMULATE                  = 0x02, | ||||
|   HID_USAGE_PAGE_VIRTUAL_REALITY           = 0x03, | ||||
|   HID_USAGE_PAGE_SPORT                     = 0x04, | ||||
|   HID_USAGE_PAGE_GAME                      = 0x05, | ||||
|   HID_USAGE_PAGE_GENERIC_DEVICE            = 0x06, | ||||
|   HID_USAGE_PAGE_KEYBOARD                  = 0x07, | ||||
|   HID_USAGE_PAGE_LED                       = 0x08, | ||||
|   HID_USAGE_PAGE_BUTTON                    = 0x09, | ||||
|   HID_USAGE_PAGE_ORDINAL                   = 0x0a, | ||||
|   HID_USAGE_PAGE_TELEPHONY                 = 0x0b, | ||||
|   HID_USAGE_PAGE_CONSUMER                  = 0x0c, | ||||
|   HID_USAGE_PAGE_DIGITIZER                 = 0x0d, | ||||
|   HID_USAGE_PAGE_PID                       = 0x0f, | ||||
|   HID_USAGE_PAGE_UNICODE                   = 0x10, | ||||
|   HID_USAGE_PAGE_ALPHA_DISPLAY             = 0x14, | ||||
|   HID_USAGE_PAGE_MEDICAL                   = 0x40, | ||||
|   HID_USAGE_PAGE_LIGHTING_AND_ILLUMINATION = 0x59, | ||||
|   HID_USAGE_PAGE_MONITOR                   = 0x80, // 0x80 - 0x83 | ||||
|   HID_USAGE_PAGE_POWER                     = 0x84, // 0x084 - 0x87 | ||||
|   HID_USAGE_PAGE_BARCODE_SCANNER           = 0x8c, | ||||
|   HID_USAGE_PAGE_SCALE                     = 0x8d, | ||||
|   HID_USAGE_PAGE_MSR                       = 0x8e, | ||||
|   HID_USAGE_PAGE_CAMERA                    = 0x90, | ||||
|   HID_USAGE_PAGE_ARCADE                    = 0x91, | ||||
|   HID_USAGE_PAGE_FIDO                      = 0xF1D0, // FIDO alliance HID usage page | ||||
|   HID_USAGE_PAGE_VENDOR                    = 0xFF00  // 0xFF00 - 0xFFFF | ||||
| }; | ||||
|  | ||||
| /// HID Usage Table - Table 6: Generic Desktop Page | ||||
| @@ -801,8 +849,7 @@ enum { | ||||
|  | ||||
| /// HID Usage Table: Consumer Page (0x0C) | ||||
| /// Only contains controls that supported by Windows (whole list is too long) | ||||
| enum | ||||
| { | ||||
| enum { | ||||
|   // Generic Control | ||||
|   HID_USAGE_CONSUMER_CONTROL                           = 0x0001, | ||||
|  | ||||
| @@ -858,9 +905,45 @@ enum | ||||
|   HID_USAGE_CONSUMER_AC_PAN                            = 0x0238, | ||||
| }; | ||||
|  | ||||
| /// HID Usage Table - Lighting And Illumination Page (0x59) | ||||
| enum { | ||||
|   HID_USAGE_LIGHTING_LAMP_ARRAY                          = 0x01, | ||||
|   HID_USAGE_LIGHTING_LAMP_ARRAY_ATTRIBUTES_REPORT        = 0x02, | ||||
|   HID_USAGE_LIGHTING_LAMP_COUNT                          = 0x03, | ||||
|   HID_USAGE_LIGHTING_BOUNDING_BOX_WIDTH_IN_MICROMETERS   = 0x04, | ||||
|   HID_USAGE_LIGHTING_BOUNDING_BOX_HEIGHT_IN_MICROMETERS  = 0x05, | ||||
|   HID_USAGE_LIGHTING_BOUNDING_BOX_DEPTH_IN_MICROMETERS   = 0x06, | ||||
|   HID_USAGE_LIGHTING_LAMP_ARRAY_KIND                     = 0x07, | ||||
|   HID_USAGE_LIGHTING_MIN_UPDATE_INTERVAL_IN_MICROSECONDS = 0x08, | ||||
|   HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_REQUEST_REPORT      = 0x20, | ||||
|   HID_USAGE_LIGHTING_LAMP_ID                             = 0x21, | ||||
|   HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_RESPONSE_REPORT     = 0x22, | ||||
|   HID_USAGE_LIGHTING_POSITION_X_IN_MICROMETERS           = 0x23, | ||||
|   HID_USAGE_LIGHTING_POSITION_Y_IN_MICROMETERS           = 0x24, | ||||
|   HID_USAGE_LIGHTING_POSITION_Z_IN_MICROMETERS           = 0x25, | ||||
|   HID_USAGE_LIGHTING_LAMP_PURPOSES                       = 0x26, | ||||
|   HID_USAGE_LIGHTING_UPDATE_LATENCY_IN_MICROSECONDS      = 0x27, | ||||
|   HID_USAGE_LIGHTING_RED_LEVEL_COUNT                     = 0x28, | ||||
|   HID_USAGE_LIGHTING_GREEN_LEVEL_COUNT                   = 0x29, | ||||
|   HID_USAGE_LIGHTING_BLUE_LEVEL_COUNT                    = 0x2A, | ||||
|   HID_USAGE_LIGHTING_INTENSITY_LEVEL_COUNT               = 0x2B, | ||||
|   HID_USAGE_LIGHTING_IS_PROGRAMMABLE                     = 0x2C, | ||||
|   HID_USAGE_LIGHTING_INPUT_BINDING                       = 0x2D, | ||||
|   HID_USAGE_LIGHTING_LAMP_MULTI_UPDATE_REPORT            = 0x50, | ||||
|   HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL                  = 0x51, | ||||
|   HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL                = 0x52, | ||||
|   HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL                 = 0x53, | ||||
|   HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL            = 0x54, | ||||
|   HID_USAGE_LIGHTING_LAMP_UPDATE_FLAGS                   = 0x55, | ||||
|   HID_USAGE_LIGHTING_LAMP_RANGE_UPDATE_REPORT            = 0x60, | ||||
|   HID_USAGE_LIGHTING_LAMP_ID_START                       = 0x61, | ||||
|   HID_USAGE_LIGHTING_LAMP_ID_END                         = 0x62, | ||||
|   HID_USAGE_LIGHTING_LAMP_ARRAY_CONTROL_REPORT           = 0x70, | ||||
|   HID_USAGE_LIGHTING_AUTONOMOUS_MODE                     = 0x71, | ||||
| }; | ||||
|  | ||||
| /// HID Usage Table: FIDO Alliance Page (0xF1D0) | ||||
| enum | ||||
| { | ||||
| enum { | ||||
|   HID_USAGE_FIDO_U2FHID   = 0x01, // U2FHID usage for top-level collection | ||||
|   HID_USAGE_FIDO_DATA_IN  = 0x20, // Raw IN data report | ||||
|   HID_USAGE_FIDO_DATA_OUT = 0x21  // Raw OUT data report | ||||
|   | ||||
| @@ -39,92 +39,109 @@ | ||||
| //--------------------------------------------------------------------+ | ||||
| // MACRO CONSTANT TYPEDEF | ||||
| //--------------------------------------------------------------------+ | ||||
| typedef struct | ||||
| { | ||||
| typedef struct { | ||||
|   uint8_t itf_num; | ||||
|   uint8_t ep_in; | ||||
|   uint8_t ep_out;        // optional Out endpoint | ||||
|   uint8_t itf_protocol;  // Boot mouse or keyboard | ||||
|   uint8_t ep_out;       // optional Out endpoint | ||||
|   uint8_t itf_protocol; // Boot mouse or keyboard | ||||
|  | ||||
|   uint16_t report_desc_len; | ||||
|   uint8_t protocol_mode; // Boot (0) or Report protocol (1) | ||||
|   uint8_t idle_rate;     // up to application to handle idle rate | ||||
|   uint16_t report_desc_len; | ||||
|  | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_HID_EP_BUFSIZE]; | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_HID_EP_BUFSIZE]; | ||||
|  | ||||
|   // TODO save hid descriptor since host can specifically request this after enumeration | ||||
|   // Note: HID descriptor may be not available from application after enumeration | ||||
|   tusb_hid_descriptor_hid_t const * hid_descriptor; | ||||
|   tusb_hid_descriptor_hid_t const *hid_descriptor; | ||||
|  | ||||
|   uint8_t ctrl_buf[CFG_TUD_HID_EP_BUFSIZE]; | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_HID_EP_BUFSIZE]; | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_HID_EP_BUFSIZE]; | ||||
| } hidd_interface_t; | ||||
|  | ||||
| CFG_TUD_MEM_SECTION tu_static hidd_interface_t _hidd_itf[CFG_TUD_HID]; | ||||
|  | ||||
| /*------------- Helpers -------------*/ | ||||
| static inline uint8_t get_index_by_itfnum(uint8_t itf_num) | ||||
| { | ||||
| 	for (uint8_t i=0; i < CFG_TUD_HID; i++ ) | ||||
| 	{ | ||||
| 		if ( itf_num == _hidd_itf[i].itf_num ) return i; | ||||
| 	} | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint8_t get_index_by_itfnum(uint8_t itf_num) { | ||||
|   for (uint8_t i = 0; i < CFG_TUD_HID; i++) { | ||||
|     if (itf_num == _hidd_itf[i].itf_num) { | ||||
|       return i; | ||||
|     } | ||||
|   } | ||||
|   return 0xFF; | ||||
| } | ||||
|  | ||||
| 	return 0xFF; | ||||
| //--------------------------------------------------------------------+ | ||||
| // Weak stubs: invoked if no strong implementation is available | ||||
| //--------------------------------------------------------------------+ | ||||
| TU_ATTR_WEAK void tud_hid_set_protocol_cb(uint8_t instance, uint8_t protocol) { | ||||
|   (void) instance; | ||||
|   (void) protocol; | ||||
| } | ||||
|  | ||||
| TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t instance, uint8_t idle_rate) { | ||||
|   (void) instance; | ||||
|   (void) idle_rate; | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len) { | ||||
|   (void) instance; | ||||
|   (void) report; | ||||
|   (void) len; | ||||
| } | ||||
|  | ||||
| // Invoked when a transfer wasn't successful | ||||
| TU_ATTR_WEAK void tud_hid_report_failed_cb(uint8_t instance, hid_report_type_t report_type, uint8_t const* report, uint16_t xferred_bytes) { | ||||
|   (void) instance; | ||||
|   (void) report_type; | ||||
|   (void) report; | ||||
|   (void) xferred_bytes; | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // APPLICATION API | ||||
| //--------------------------------------------------------------------+ | ||||
| bool tud_hid_n_ready(uint8_t instance) | ||||
| { | ||||
| bool tud_hid_n_ready(uint8_t instance) { | ||||
|   uint8_t const rhport = 0; | ||||
|   uint8_t const ep_in = _hidd_itf[instance].ep_in; | ||||
|   return tud_ready() && (ep_in != 0) && !usbd_edpt_busy(rhport, ep_in); | ||||
| } | ||||
|  | ||||
| bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, uint16_t len) | ||||
| { | ||||
| bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const *report, uint16_t len) { | ||||
|   uint8_t const rhport = 0; | ||||
|   hidd_interface_t * p_hid = &_hidd_itf[instance]; | ||||
|   hidd_interface_t *p_hid = &_hidd_itf[instance]; | ||||
|  | ||||
|   // claim endpoint | ||||
|   TU_VERIFY( usbd_edpt_claim(rhport, p_hid->ep_in) ); | ||||
|   TU_VERIFY(usbd_edpt_claim(rhport, p_hid->ep_in)); | ||||
|  | ||||
|   // prepare data | ||||
|   if (report_id) | ||||
|   { | ||||
|   if (report_id) { | ||||
|     p_hid->epin_buf[0] = report_id; | ||||
|     TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf+1, CFG_TUD_HID_EP_BUFSIZE-1, report, len)); | ||||
|     TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf + 1, CFG_TUD_HID_EP_BUFSIZE - 1, report, len)); | ||||
|     len++; | ||||
|   }else | ||||
|   { | ||||
|   } else { | ||||
|     TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf, CFG_TUD_HID_EP_BUFSIZE, report, len)); | ||||
|   } | ||||
|  | ||||
|   return usbd_edpt_xfer(rhport, p_hid->ep_in, p_hid->epin_buf, len); | ||||
| } | ||||
|  | ||||
| uint8_t tud_hid_n_interface_protocol(uint8_t instance) | ||||
| { | ||||
| uint8_t tud_hid_n_interface_protocol(uint8_t instance) { | ||||
|   return _hidd_itf[instance].itf_protocol; | ||||
| } | ||||
|  | ||||
| uint8_t tud_hid_n_get_protocol(uint8_t instance) | ||||
| { | ||||
| uint8_t tud_hid_n_get_protocol(uint8_t instance) { | ||||
|   return _hidd_itf[instance].protocol_mode; | ||||
| } | ||||
|  | ||||
| bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, uint8_t keycode[6]) | ||||
| { | ||||
| bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, const uint8_t keycode[6]) { | ||||
|   hid_keyboard_report_t report; | ||||
|  | ||||
|   report.modifier = modifier; | ||||
|   report.reserved = 0; | ||||
|  | ||||
|   if ( keycode ) | ||||
|   { | ||||
|   if (keycode) { | ||||
|     memcpy(report.keycode, keycode, sizeof(report.keycode)); | ||||
|   }else | ||||
|   { | ||||
|   } else { | ||||
|     tu_memclr(report.keycode, 6); | ||||
|   } | ||||
|  | ||||
| @@ -132,45 +149,41 @@ bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modi | ||||
| } | ||||
|  | ||||
| bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, | ||||
|                             uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) | ||||
| { | ||||
|   hid_mouse_report_t report = | ||||
|   { | ||||
|                             uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) { | ||||
|   hid_mouse_report_t report = { | ||||
|     .buttons = buttons, | ||||
|     .x       = x, | ||||
|     .y       = y, | ||||
|     .wheel   = vertical, | ||||
|     .pan     = horizontal | ||||
|     .x = x, | ||||
|     .y = y, | ||||
|     .wheel = vertical, | ||||
|     .pan = horizontal | ||||
|   }; | ||||
|  | ||||
|   return tud_hid_n_report(instance, report_id, &report, sizeof(report)); | ||||
| } | ||||
|  | ||||
| bool tud_hid_n_abs_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal) | ||||
| { | ||||
|   hid_abs_mouse_report_t report = | ||||
|   { | ||||
| bool tud_hid_n_abs_mouse_report(uint8_t instance, uint8_t report_id, | ||||
|                                 uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal) { | ||||
|   hid_abs_mouse_report_t report = { | ||||
|     .buttons = buttons, | ||||
|     .x       = x, | ||||
|     .y       = y, | ||||
|     .wheel   = vertical, | ||||
|     .pan     = horizontal | ||||
|     .x = x, | ||||
|     .y = y, | ||||
|     .wheel = vertical, | ||||
|     .pan = horizontal | ||||
|   }; | ||||
|   return tud_hid_n_report(instance, report_id, &report, sizeof(report)); | ||||
| } | ||||
|  | ||||
| bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, | ||||
|                               int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) { | ||||
|   hid_gamepad_report_t report = | ||||
|   { | ||||
|     .x       = x, | ||||
|     .y       = y, | ||||
|     .z       = z, | ||||
|     .rz      = rz, | ||||
|     .rx      = rx, | ||||
|     .ry      = ry, | ||||
|     .hat     = hat, | ||||
|     .buttons = buttons, | ||||
|   hid_gamepad_report_t report = { | ||||
|       .x = x, | ||||
|       .y = y, | ||||
|       .z = z, | ||||
|       .rz = rz, | ||||
|       .rx = rx, | ||||
|       .ry = ry, | ||||
|       .hat = hat, | ||||
|       .buttons = buttons, | ||||
|   }; | ||||
|  | ||||
|   return tud_hid_n_report(instance, report_id, &report, sizeof(report)); | ||||
| @@ -187,59 +200,54 @@ bool hidd_deinit(void) { | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| void hidd_reset(uint8_t rhport) | ||||
| { | ||||
|   (void) rhport; | ||||
| void hidd_reset(uint8_t rhport) { | ||||
|   (void)rhport; | ||||
|   tu_memclr(_hidd_itf, sizeof(_hidd_itf)); | ||||
| } | ||||
|  | ||||
| uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len) | ||||
|  { | ||||
| uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const *desc_itf, uint16_t max_len) { | ||||
|   TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass, 0); | ||||
|  | ||||
|   // len = interface + hid + n*endpoints | ||||
|   uint16_t const drv_len = | ||||
|       (uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + | ||||
|   uint16_t const drv_len = (uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + | ||||
|                                        desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t)); | ||||
|   TU_ASSERT(max_len >= drv_len, 0); | ||||
|  | ||||
|   // Find available interface | ||||
|   hidd_interface_t * p_hid = NULL; | ||||
|   hidd_interface_t *p_hid = NULL; | ||||
|   uint8_t hid_id; | ||||
|   for(hid_id=0; hid_id<CFG_TUD_HID; hid_id++) | ||||
|   { | ||||
|     if ( _hidd_itf[hid_id].ep_in == 0 ) | ||||
|     { | ||||
|   for (hid_id = 0; hid_id < CFG_TUD_HID; hid_id++) { | ||||
|     if (_hidd_itf[hid_id].ep_in == 0) { | ||||
|       p_hid = &_hidd_itf[hid_id]; | ||||
|       break; | ||||
|     } | ||||
|   } | ||||
|   TU_ASSERT(p_hid, 0); | ||||
|  | ||||
|   uint8_t const *p_desc = (uint8_t const *) desc_itf; | ||||
|   uint8_t const *p_desc = (uint8_t const *)desc_itf; | ||||
|  | ||||
|   //------------- HID descriptor -------------// | ||||
|   p_desc = tu_desc_next(p_desc); | ||||
|   TU_ASSERT(HID_DESC_TYPE_HID == tu_desc_type(p_desc), 0); | ||||
|   p_hid->hid_descriptor = (tusb_hid_descriptor_hid_t const *) p_desc; | ||||
|   p_hid->hid_descriptor = (tusb_hid_descriptor_hid_t const *)p_desc; | ||||
|  | ||||
|   //------------- Endpoint Descriptor -------------// | ||||
|   p_desc = tu_desc_next(p_desc); | ||||
|   TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_INTERRUPT, &p_hid->ep_out, &p_hid->ep_in), 0); | ||||
|  | ||||
|   if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->itf_protocol = desc_itf->bInterfaceProtocol; | ||||
|   if (desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT) { | ||||
|     p_hid->itf_protocol = desc_itf->bInterfaceProtocol; | ||||
|   } | ||||
|  | ||||
|   p_hid->protocol_mode = HID_PROTOCOL_REPORT; // Per Specs: default is report mode | ||||
|   p_hid->itf_num       = desc_itf->bInterfaceNumber; | ||||
|   p_hid->itf_num = desc_itf->bInterfaceNumber; | ||||
|  | ||||
|   // Use offsetof to avoid pointer to the odd/misaligned address | ||||
|   p_hid->report_desc_len = tu_unaligned_read16((uint8_t const*) p_hid->hid_descriptor + offsetof(tusb_hid_descriptor_hid_t, wReportLength)); | ||||
|   p_hid->report_desc_len = tu_unaligned_read16((uint8_t const *)p_hid->hid_descriptor + offsetof(tusb_hid_descriptor_hid_t, wReportLength)); | ||||
|  | ||||
|   // Prepare for output endpoint | ||||
|   if (p_hid->ep_out) | ||||
|   { | ||||
|     if ( !usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)) ) | ||||
|     { | ||||
|   if (p_hid->ep_out) { | ||||
|     if (!usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))) { | ||||
|       TU_LOG_FAILED(); | ||||
|       TU_BREAKPOINT(); | ||||
|     } | ||||
| @@ -251,177 +259,146 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint1 | ||||
| // Invoked when a control transfer occurred on an interface of this class | ||||
| // Driver response accordingly to the request and the transfer stage (setup/data/ack) | ||||
| // return false to stall control endpoint (e.g unsupported request) | ||||
| bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) | ||||
| { | ||||
| bool hidd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request) { | ||||
|   TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE); | ||||
|  | ||||
|   uint8_t const hid_itf = get_index_by_itfnum((uint8_t) request->wIndex); | ||||
|   uint8_t const hid_itf = get_index_by_itfnum((uint8_t)request->wIndex); | ||||
|   TU_VERIFY(hid_itf < CFG_TUD_HID); | ||||
|  | ||||
|   hidd_interface_t* p_hid = &_hidd_itf[hid_itf]; | ||||
|   hidd_interface_t *p_hid = &_hidd_itf[hid_itf]; | ||||
|  | ||||
|   if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD) | ||||
|   { | ||||
|   if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD) { | ||||
|     //------------- STD Request -------------// | ||||
|     if ( stage == CONTROL_STAGE_SETUP ) | ||||
|     { | ||||
|       uint8_t const desc_type  = tu_u16_high(request->wValue); | ||||
|       //uint8_t const desc_index = tu_u16_low (request->wValue); | ||||
|     if (stage == CONTROL_STAGE_SETUP) { | ||||
|       uint8_t const desc_type = tu_u16_high(request->wValue); | ||||
|       // uint8_t const desc_index = tu_u16_low (request->wValue); | ||||
|  | ||||
|       if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_HID) | ||||
|       { | ||||
|       if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_HID) { | ||||
|         TU_VERIFY(p_hid->hid_descriptor); | ||||
|         TU_VERIFY(tud_control_xfer(rhport, request, (void*)(uintptr_t) p_hid->hid_descriptor, p_hid->hid_descriptor->bLength)); | ||||
|       } | ||||
|       else if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT) | ||||
|       { | ||||
|         uint8_t const * desc_report = tud_hid_descriptor_report_cb(hid_itf); | ||||
|         tud_control_xfer(rhport, request, (void*)(uintptr_t) desc_report, p_hid->report_desc_len); | ||||
|       } | ||||
|       else | ||||
|       { | ||||
|         TU_VERIFY(tud_control_xfer(rhport, request, (void *)(uintptr_t)p_hid->hid_descriptor, p_hid->hid_descriptor->bLength)); | ||||
|       } else if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT) { | ||||
|         uint8_t const *desc_report = tud_hid_descriptor_report_cb(hid_itf); | ||||
|         tud_control_xfer(rhport, request, (void *)(uintptr_t)desc_report, p_hid->report_desc_len); | ||||
|       } else { | ||||
|         return false; // stall unsupported request | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|   else if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS) | ||||
|   { | ||||
|   } else if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS) { | ||||
|     //------------- Class Specific Request -------------// | ||||
|     switch( request->bRequest ) | ||||
|     { | ||||
|     switch (request->bRequest) { | ||||
|       case HID_REQ_CONTROL_GET_REPORT: | ||||
|         if ( stage == CONTROL_STAGE_SETUP ) | ||||
|         { | ||||
|         if (stage == CONTROL_STAGE_SETUP) { | ||||
|           uint8_t const report_type = tu_u16_high(request->wValue); | ||||
|           uint8_t const report_id   = tu_u16_low(request->wValue); | ||||
|           uint8_t const report_id = tu_u16_low(request->wValue); | ||||
|  | ||||
|           uint8_t* report_buf = p_hid->epin_buf; | ||||
|           uint8_t* report_buf = p_hid->ctrl_buf; | ||||
|           uint16_t req_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE); | ||||
|  | ||||
|           uint16_t xferlen = 0; | ||||
|  | ||||
|           // If host request a specific Report ID, add ID to as 1 byte of response | ||||
|           if ( (report_id != HID_REPORT_TYPE_INVALID) && (req_len > 1) ) | ||||
|           { | ||||
|           if ((report_id != HID_REPORT_TYPE_INVALID) && (req_len > 1)) { | ||||
|             *report_buf++ = report_id; | ||||
|             req_len--; | ||||
|  | ||||
|             xferlen++; | ||||
|           } | ||||
|  | ||||
|           xferlen += tud_hid_get_report_cb(hid_itf, report_id, (hid_report_type_t) report_type, report_buf, req_len); | ||||
|           TU_ASSERT( xferlen > 0 ); | ||||
|           TU_ASSERT(xferlen > 0); | ||||
|  | ||||
|           tud_control_xfer(rhport, request, p_hid->epin_buf, xferlen); | ||||
|           tud_control_xfer(rhport, request, p_hid->ctrl_buf, xferlen); | ||||
|         } | ||||
|       break; | ||||
|         break; | ||||
|  | ||||
|       case  HID_REQ_CONTROL_SET_REPORT: | ||||
|         if ( stage == CONTROL_STAGE_SETUP ) | ||||
|         { | ||||
|           TU_VERIFY(request->wLength <= sizeof(p_hid->epout_buf)); | ||||
|           tud_control_xfer(rhport, request, p_hid->epout_buf, request->wLength); | ||||
|         } | ||||
|         else if ( stage == CONTROL_STAGE_ACK ) | ||||
|         { | ||||
|       case HID_REQ_CONTROL_SET_REPORT: | ||||
|         if (stage == CONTROL_STAGE_SETUP) { | ||||
|           TU_VERIFY(request->wLength <= sizeof(p_hid->ctrl_buf)); | ||||
|           tud_control_xfer(rhport, request, p_hid->ctrl_buf, request->wLength); | ||||
|         } else if (stage == CONTROL_STAGE_ACK) { | ||||
|           uint8_t const report_type = tu_u16_high(request->wValue); | ||||
|           uint8_t const report_id   = tu_u16_low(request->wValue); | ||||
|           uint8_t const report_id = tu_u16_low(request->wValue); | ||||
|  | ||||
|           uint8_t const* report_buf = p_hid->epout_buf; | ||||
|           uint8_t const* report_buf = p_hid->ctrl_buf; | ||||
|           uint16_t report_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE); | ||||
|  | ||||
|           // If host request a specific Report ID, extract report ID in buffer before invoking callback | ||||
|           if ( (report_id != HID_REPORT_TYPE_INVALID) && (report_len > 1) && (report_id == report_buf[0]) ) | ||||
|           { | ||||
|           if ((report_id != HID_REPORT_TYPE_INVALID) && (report_len > 1) && (report_id == report_buf[0])) { | ||||
|             report_buf++; | ||||
|             report_len--; | ||||
|           } | ||||
|  | ||||
|           tud_hid_set_report_cb(hid_itf, report_id, (hid_report_type_t) report_type, report_buf, report_len); | ||||
|         } | ||||
|       break; | ||||
|         break; | ||||
|  | ||||
|       case HID_REQ_CONTROL_SET_IDLE: | ||||
|         if ( stage == CONTROL_STAGE_SETUP ) | ||||
|         { | ||||
|         if (stage == CONTROL_STAGE_SETUP) { | ||||
|           p_hid->idle_rate = tu_u16_high(request->wValue); | ||||
|           if ( tud_hid_set_idle_cb ) | ||||
|           { | ||||
|             // stall request if callback return false | ||||
|             TU_VERIFY( tud_hid_set_idle_cb( hid_itf, p_hid->idle_rate) ); | ||||
|           } | ||||
|  | ||||
|           TU_VERIFY(tud_hid_set_idle_cb(hid_itf, p_hid->idle_rate)); // stall if false | ||||
|           tud_control_status(rhport, request); | ||||
|         } | ||||
|       break; | ||||
|         break; | ||||
|  | ||||
|       case HID_REQ_CONTROL_GET_IDLE: | ||||
|         if ( stage == CONTROL_STAGE_SETUP ) | ||||
|         { | ||||
|         if (stage == CONTROL_STAGE_SETUP) { | ||||
|           // TODO idle rate of report | ||||
|           tud_control_xfer(rhport, request, &p_hid->idle_rate, 1); | ||||
|         } | ||||
|       break; | ||||
|         break; | ||||
|  | ||||
|       case HID_REQ_CONTROL_GET_PROTOCOL: | ||||
|         if ( stage == CONTROL_STAGE_SETUP ) | ||||
|         { | ||||
|         if (stage == CONTROL_STAGE_SETUP) { | ||||
|           tud_control_xfer(rhport, request, &p_hid->protocol_mode, 1); | ||||
|         } | ||||
|       break; | ||||
|         break; | ||||
|  | ||||
|       case HID_REQ_CONTROL_SET_PROTOCOL: | ||||
|         if ( stage == CONTROL_STAGE_SETUP ) | ||||
|         { | ||||
|         if (stage == CONTROL_STAGE_SETUP) { | ||||
|           tud_control_status(rhport, request); | ||||
|         } | ||||
|         else if ( stage == CONTROL_STAGE_ACK ) | ||||
|         { | ||||
|         } else if (stage == CONTROL_STAGE_ACK) { | ||||
|           p_hid->protocol_mode = (uint8_t) request->wValue; | ||||
|           if (tud_hid_set_protocol_cb) | ||||
|           { | ||||
|             tud_hid_set_protocol_cb(hid_itf, p_hid->protocol_mode); | ||||
|           } | ||||
|           tud_hid_set_protocol_cb(hid_itf, p_hid->protocol_mode); | ||||
|         } | ||||
|       break; | ||||
|         break; | ||||
|  | ||||
|       default: return false; // stall unsupported request | ||||
|       default: | ||||
|         return false; // stall unsupported request | ||||
|     } | ||||
|   }else | ||||
|   { | ||||
|   } else { | ||||
|     return false; // stall unsupported request | ||||
|   } | ||||
|  | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) | ||||
| { | ||||
|   (void) result; | ||||
|  | ||||
| bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { | ||||
|   uint8_t instance = 0; | ||||
|   hidd_interface_t * p_hid = _hidd_itf; | ||||
|   hidd_interface_t *p_hid = _hidd_itf; | ||||
|  | ||||
|   // Identify which interface to use | ||||
|   for (instance = 0; instance < CFG_TUD_HID; instance++) | ||||
|   { | ||||
|   for (instance = 0; instance < CFG_TUD_HID; instance++) { | ||||
|     p_hid = &_hidd_itf[instance]; | ||||
|     if ( (ep_addr == p_hid->ep_out) || (ep_addr == p_hid->ep_in) ) break; | ||||
|     if ((ep_addr == p_hid->ep_out) || (ep_addr == p_hid->ep_in)) { | ||||
|       break; | ||||
|     } | ||||
|   } | ||||
|   TU_ASSERT(instance < CFG_TUD_HID); | ||||
|  | ||||
|   // Sent report successfully | ||||
|   if (ep_addr == p_hid->ep_in) | ||||
|   { | ||||
|     if (tud_hid_report_complete_cb) | ||||
|     { | ||||
|   if (ep_addr == p_hid->ep_in) { | ||||
|     // Input report | ||||
|     if (XFER_RESULT_SUCCESS == result) { | ||||
|       tud_hid_report_complete_cb(instance, p_hid->epin_buf, (uint16_t) xferred_bytes); | ||||
|     } else { | ||||
|       tud_hid_report_failed_cb(instance, HID_REPORT_TYPE_INPUT, p_hid->epin_buf, (uint16_t) xferred_bytes); | ||||
|     } | ||||
|   } | ||||
|   // Received report | ||||
|   else if (ep_addr == p_hid->ep_out) | ||||
|   { | ||||
|     tud_hid_set_report_cb(instance, 0, HID_REPORT_TYPE_INVALID, p_hid->epout_buf, (uint16_t) xferred_bytes); | ||||
|   } else { | ||||
|     // Output report | ||||
|     if (XFER_RESULT_SUCCESS == result) { | ||||
|       tud_hid_set_report_cb(instance, 0, HID_REPORT_TYPE_OUTPUT, p_hid->epout_buf, (uint16_t)xferred_bytes); | ||||
|     } else { | ||||
|       tud_hid_report_failed_cb(instance, HID_REPORT_TYPE_OUTPUT, p_hid->epout_buf, (uint16_t) xferred_bytes); | ||||
|     } | ||||
|  | ||||
|     // prepare for new transfer | ||||
|     TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))); | ||||
|   } | ||||
|  | ||||
|   | ||||
| @@ -24,8 +24,8 @@ | ||||
|  * This file is part of the TinyUSB stack. | ||||
|  */ | ||||
|  | ||||
| #ifndef _TUSB_HID_DEVICE_H_ | ||||
| #define _TUSB_HID_DEVICE_H_ | ||||
| #ifndef TUSB_HID_DEVICE_H_ | ||||
| #define TUSB_HID_DEVICE_H_ | ||||
|  | ||||
| #include "hid.h" | ||||
|  | ||||
| @@ -48,8 +48,7 @@ | ||||
| #endif | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Application API (Multiple Instances) | ||||
| // CFG_TUD_HID > 1 | ||||
| // Application API (Multiple Instances) i.e. CFG_TUD_HID > 1 | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| // Check if the interface is ready to use | ||||
| @@ -66,7 +65,7 @@ bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, u | ||||
|  | ||||
| // KEYBOARD: convenient helper to send keyboard report if application | ||||
| // use template layout report as defined by hid_keyboard_report_t | ||||
| bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, uint8_t keycode[6]); | ||||
| bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, const uint8_t keycode[6]); | ||||
|  | ||||
| // MOUSE: convenient helper to send mouse report if application | ||||
| // use template layout report as defined by hid_mouse_report_t | ||||
| @@ -76,12 +75,6 @@ bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons | ||||
| // use template layout report as defined by hid_abs_mouse_report_t | ||||
| bool tud_hid_n_abs_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal); | ||||
|  | ||||
|  | ||||
| static inline bool tud_hid_abs_mouse_report(uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal) | ||||
| { | ||||
|   return tud_hid_n_abs_mouse_report(0, report_id, buttons, x, y, vertical, horizontal); | ||||
| } | ||||
|  | ||||
| // Gamepad: convenient helper to send gamepad report if application | ||||
| // use template layout report TUD_HID_REPORT_DESC_GAMEPAD | ||||
| bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons); | ||||
| @@ -89,16 +82,40 @@ bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int | ||||
| //--------------------------------------------------------------------+ | ||||
| // Application API (Single Port) | ||||
| //--------------------------------------------------------------------+ | ||||
| static inline bool    tud_hid_ready(void); | ||||
| static inline uint8_t tud_hid_interface_protocol(void); | ||||
| static inline uint8_t tud_hid_get_protocol(void); | ||||
| static inline bool    tud_hid_report(uint8_t report_id, void const* report, uint16_t len); | ||||
| static inline bool    tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]); | ||||
| static inline bool    tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); | ||||
| static inline bool    tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons); | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_hid_ready(void) { | ||||
|   return tud_hid_n_ready(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint8_t tud_hid_interface_protocol(void) { | ||||
|   return tud_hid_n_interface_protocol(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint8_t tud_hid_get_protocol(void) { | ||||
|   return tud_hid_n_get_protocol(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_hid_report(uint8_t report_id, void const* report, uint16_t len) { | ||||
|   return tud_hid_n_report(0, report_id, report, len); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, const uint8_t keycode[6]) { | ||||
|   return tud_hid_n_keyboard_report(0, report_id, modifier, keycode); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) { | ||||
|   return tud_hid_n_mouse_report(0, report_id, buttons, x, y, vertical, horizontal); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_hid_abs_mouse_report(uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal) { | ||||
|   return tud_hid_n_abs_mouse_report(0, report_id, buttons, x, y, vertical, horizontal); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) { | ||||
|   return tud_hid_n_gamepad_report(0, report_id, x, y, z, rz, rx, ry, hat, buttons); | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Callbacks (Weak is optional) | ||||
| // Application Callbacks | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| // Invoked when received GET HID REPORT DESCRIPTOR request | ||||
| @@ -111,61 +128,25 @@ uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance); | ||||
| uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); | ||||
|  | ||||
| // Invoked when received SET_REPORT control request or | ||||
| // received data on OUT endpoint ( Report ID = 0, Type = 0 ) | ||||
| // received data on OUT endpoint (Report ID = 0, Type = OUTPUT) | ||||
| void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); | ||||
|  | ||||
| // Invoked when received SET_PROTOCOL request | ||||
| // protocol is either HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1) | ||||
| TU_ATTR_WEAK void tud_hid_set_protocol_cb(uint8_t instance, uint8_t protocol); | ||||
| void tud_hid_set_protocol_cb(uint8_t instance, uint8_t protocol); | ||||
|  | ||||
| // Invoked when received SET_IDLE request. return false will stall the request | ||||
| // - Idle Rate = 0 : only send report if there is changes, i.e skip duplication | ||||
| // - Idle Rate = 0 : only send report if there is changes, i.e. skip duplication | ||||
| // - Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms). | ||||
| TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t instance, uint8_t idle_rate); | ||||
| bool tud_hid_set_idle_cb(uint8_t instance, uint8_t idle_rate); | ||||
|  | ||||
| // Invoked when sent REPORT successfully to host | ||||
| // Application can use this to send the next report | ||||
| // Note: For composite reports, report[0] is report ID | ||||
| TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len); | ||||
| void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len); | ||||
|  | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Inline Functions | ||||
| //--------------------------------------------------------------------+ | ||||
| static inline bool tud_hid_ready(void) | ||||
| { | ||||
|   return tud_hid_n_ready(0); | ||||
| } | ||||
|  | ||||
| static inline uint8_t tud_hid_interface_protocol(void) | ||||
| { | ||||
|   return tud_hid_n_interface_protocol(0); | ||||
| } | ||||
|  | ||||
| static inline uint8_t tud_hid_get_protocol(void) | ||||
| { | ||||
|   return tud_hid_n_get_protocol(0); | ||||
| } | ||||
|  | ||||
| static inline bool tud_hid_report(uint8_t report_id, void const* report, uint16_t len) | ||||
| { | ||||
|   return tud_hid_n_report(0, report_id, report, len); | ||||
| } | ||||
|  | ||||
| static inline bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]) | ||||
| { | ||||
|   return tud_hid_n_keyboard_report(0, report_id, modifier, keycode); | ||||
| } | ||||
|  | ||||
| static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) | ||||
| { | ||||
|   return tud_hid_n_mouse_report(0, report_id, buttons, x, y, vertical, horizontal); | ||||
| } | ||||
|  | ||||
| static inline bool  tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) | ||||
| { | ||||
|   return tud_hid_n_gamepad_report(0, report_id, x, y, z, rz, rx, ry, hat, buttons); | ||||
| } | ||||
| // Invoked when a transfer wasn't successful | ||||
| void tud_hid_report_failed_cb(uint8_t instance, hid_report_type_t report_type, uint8_t const* report, uint16_t xferred_bytes); | ||||
|  | ||||
| /* --------------------------------------------------------------------+ | ||||
|  * HID Report Descriptor Template | ||||
| @@ -461,6 +442,178 @@ static inline bool  tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y | ||||
|       HID_OUTPUT      ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE  ),\ | ||||
|     HID_COLLECTION_END \ | ||||
|  | ||||
| // HID Lighting and Illumination Report Descriptor Template | ||||
| // - 1st parameter is report id (required) | ||||
| //   Creates 6 report ids for lighting HID usages in the following order: | ||||
| //     report_id+0: HID_USAGE_LIGHTING_LAMP_ARRAY_ATTRIBUTES_REPORT | ||||
| //     report_id+1: HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_REQUEST_REPORT | ||||
| //     report_id+2: HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_RESPONSE_REPORT | ||||
| //     report_id+3: HID_USAGE_LIGHTING_LAMP_MULTI_UPDATE_REPORT | ||||
| //     report_id+4: HID_USAGE_LIGHTING_LAMP_RANGE_UPDATE_REPORT | ||||
| //     report_id+5: HID_USAGE_LIGHTING_LAMP_ARRAY_CONTROL_REPORT | ||||
| #define TUD_HID_REPORT_DESC_LIGHTING(report_id) \ | ||||
|   HID_USAGE_PAGE ( HID_USAGE_PAGE_LIGHTING_AND_ILLUMINATION ),\ | ||||
|   HID_USAGE      ( HID_USAGE_LIGHTING_LAMP_ARRAY            ),\ | ||||
|   HID_COLLECTION ( HID_COLLECTION_APPLICATION               ),\ | ||||
|     /* Lamp Array Attributes Report */ \ | ||||
|     HID_REPORT_ID (report_id                                    ) \ | ||||
|     HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ARRAY_ATTRIBUTES_REPORT ),\ | ||||
|     HID_COLLECTION ( HID_COLLECTION_LOGICAL                     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_COUNT                          ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                                      ),\ | ||||
|       HID_LOGICAL_MAX_N ( 65535, 3                                               ),\ | ||||
|       HID_REPORT_SIZE   ( 16                                                     ),\ | ||||
|       HID_REPORT_COUNT  ( 1                                                      ),\ | ||||
|       HID_FEATURE       ( HID_CONSTANT | HID_VARIABLE | HID_ABSOLUTE             ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BOUNDING_BOX_WIDTH_IN_MICROMETERS   ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BOUNDING_BOX_HEIGHT_IN_MICROMETERS  ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BOUNDING_BOX_DEPTH_IN_MICROMETERS   ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_ARRAY_KIND                     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_MIN_UPDATE_INTERVAL_IN_MICROSECONDS ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                                      ),\ | ||||
|       HID_LOGICAL_MAX_N ( 2147483647, 3                                          ),\ | ||||
|       HID_REPORT_SIZE   ( 32                                                     ),\ | ||||
|       HID_REPORT_COUNT  ( 5                                                      ),\ | ||||
|       HID_FEATURE       ( HID_CONSTANT | HID_VARIABLE | HID_ABSOLUTE             ),\ | ||||
|     HID_COLLECTION_END ,\ | ||||
|     /* Lamp Attributes Request Report */ \ | ||||
|     HID_REPORT_ID       ( report_id + 1                                     ) \ | ||||
|     HID_USAGE           ( HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_REQUEST_REPORT ),\ | ||||
|     HID_COLLECTION      ( HID_COLLECTION_LOGICAL                            ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_ID             ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                      ),\ | ||||
|       HID_LOGICAL_MAX_N ( 65535, 3                               ),\ | ||||
|       HID_REPORT_SIZE   ( 16                                     ),\ | ||||
|       HID_REPORT_COUNT  ( 1                                      ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ | ||||
|     HID_COLLECTION_END ,\ | ||||
|     /* Lamp Attributes Response Report */ \ | ||||
|     HID_REPORT_ID       ( report_id + 2                                      ) \ | ||||
|     HID_USAGE           ( HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_RESPONSE_REPORT ),\ | ||||
|     HID_COLLECTION      ( HID_COLLECTION_LOGICAL                             ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_ID                        ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                                 ),\ | ||||
|       HID_LOGICAL_MAX_N ( 65535, 3                                          ),\ | ||||
|       HID_REPORT_SIZE   ( 16                                                ),\ | ||||
|       HID_REPORT_COUNT  ( 1                                                 ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE            ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_POSITION_X_IN_MICROMETERS      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_POSITION_Y_IN_MICROMETERS      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_POSITION_Z_IN_MICROMETERS      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_UPDATE_LATENCY_IN_MICROSECONDS ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_PURPOSES                  ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                                 ),\ | ||||
|       HID_LOGICAL_MAX_N ( 2147483647, 3                                     ),\ | ||||
|       HID_REPORT_SIZE   ( 32                                                ),\ | ||||
|       HID_REPORT_COUNT  ( 5                                                 ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE            ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_LEVEL_COUNT                ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_LEVEL_COUNT              ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_LEVEL_COUNT               ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_LEVEL_COUNT          ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_IS_PROGRAMMABLE                ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INPUT_BINDING                  ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                                 ),\ | ||||
|       HID_LOGICAL_MAX_N ( 255, 2                                            ),\ | ||||
|       HID_REPORT_SIZE   ( 8                                                 ),\ | ||||
|       HID_REPORT_COUNT  ( 6                                                 ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE            ),\ | ||||
|     HID_COLLECTION_END ,\ | ||||
|     /* Lamp Multi-Update Report */ \ | ||||
|     HID_REPORT_ID       ( report_id + 3                               ) \ | ||||
|     HID_USAGE           ( HID_USAGE_LIGHTING_LAMP_MULTI_UPDATE_REPORT ),\ | ||||
|     HID_COLLECTION      ( HID_COLLECTION_LOGICAL                      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_COUNT               ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_UPDATE_FLAGS        ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                           ),\ | ||||
|       HID_LOGICAL_MAX   ( 8                                           ),\ | ||||
|       HID_REPORT_SIZE   ( 8                                           ),\ | ||||
|       HID_REPORT_COUNT  ( 2                                           ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_ID                  ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                           ),\ | ||||
|       HID_LOGICAL_MAX_N ( 65535, 3                                    ),\ | ||||
|       HID_REPORT_SIZE   ( 16                                          ),\ | ||||
|       HID_REPORT_COUNT  ( 8                                           ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                           ),\ | ||||
|       HID_LOGICAL_MAX_N ( 255, 2                                      ),\ | ||||
|       HID_REPORT_SIZE   ( 8                                           ),\ | ||||
|       HID_REPORT_COUNT  ( 32                                          ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE      ),\ | ||||
|     HID_COLLECTION_END ,\ | ||||
|     /* Lamp Range Update Report */ \ | ||||
|     HID_REPORT_ID       ( report_id + 4 ) \ | ||||
|     HID_USAGE           ( HID_USAGE_LIGHTING_LAMP_RANGE_UPDATE_REPORT ),\ | ||||
|     HID_COLLECTION      ( HID_COLLECTION_LOGICAL                      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_UPDATE_FLAGS        ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                           ),\ | ||||
|       HID_LOGICAL_MAX   ( 8                                           ),\ | ||||
|       HID_REPORT_SIZE   ( 8                                           ),\ | ||||
|       HID_REPORT_COUNT  ( 1                                           ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_ID_START            ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_LAMP_ID_END              ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                           ),\ | ||||
|       HID_LOGICAL_MAX_N ( 65535, 3                                    ),\ | ||||
|       HID_REPORT_SIZE   ( 16                                          ),\ | ||||
|       HID_REPORT_COUNT  ( 2                                           ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL       ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL     ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL      ),\ | ||||
|       HID_USAGE         ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ | ||||
|       HID_LOGICAL_MIN   ( 0                                           ),\ | ||||
|       HID_LOGICAL_MAX_N ( 255, 2                                      ),\ | ||||
|       HID_REPORT_SIZE   ( 8                                           ),\ | ||||
|       HID_REPORT_COUNT  ( 4                                           ),\ | ||||
|       HID_FEATURE       ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE      ),\ | ||||
|     HID_COLLECTION_END ,\ | ||||
|     /* Lamp Array Control Report */ \ | ||||
|     HID_REPORT_ID      ( report_id + 5                                ) \ | ||||
|     HID_USAGE          ( HID_USAGE_LIGHTING_LAMP_ARRAY_CONTROL_REPORT ),\ | ||||
|     HID_COLLECTION     ( HID_COLLECTION_LOGICAL                       ),\ | ||||
|       HID_USAGE        ( HID_USAGE_LIGHTING_AUTONOMOUS_MODE     ),\ | ||||
|       HID_LOGICAL_MIN  ( 0                                      ),\ | ||||
|       HID_LOGICAL_MAX  ( 1                                      ),\ | ||||
|       HID_REPORT_SIZE  ( 8                                      ),\ | ||||
|       HID_REPORT_COUNT ( 1                                      ),\ | ||||
|       HID_FEATURE      ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ | ||||
|     HID_COLLECTION_END ,\ | ||||
|   HID_COLLECTION_END \ | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Internal Class Driver API | ||||
| //--------------------------------------------------------------------+ | ||||
| @@ -475,4 +628,4 @@ bool     hidd_xfer_cb         (uint8_t rhport, uint8_t ep_addr, xfer_result_t ev | ||||
|  } | ||||
| #endif | ||||
|  | ||||
| #endif /* _TUSB_HID_DEVICE_H_ */ | ||||
| #endif | ||||
|   | ||||
| @@ -657,7 +657,9 @@ uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* report_info_arr, | ||||
|     uint8_t const data8 = desc_report[0]; | ||||
|  | ||||
|     TU_LOG(3, "tag = %d, type = %d, size = %d, data = ", tag, type, size); | ||||
|     for (uint32_t i = 0; i < size; i++) TU_LOG(3, "%02X ", desc_report[i]); | ||||
|     for (uint32_t i = 0; i < size; i++) { | ||||
|       TU_LOG(3, "%02X ", desc_report[i]); | ||||
|     } | ||||
|     TU_LOG(3, "\r\n"); | ||||
|  | ||||
|     switch (type) { | ||||
|   | ||||
| @@ -689,6 +689,24 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_ | ||||
|       } | ||||
|     break; | ||||
|  | ||||
|     case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: | ||||
|       resplen = 0; | ||||
|  | ||||
|       if (tud_msc_prevent_allow_medium_removal_cb) | ||||
|       { | ||||
|          scsi_prevent_allow_medium_removal_t const * prevent_allow = (scsi_prevent_allow_medium_removal_t const *) scsi_cmd; | ||||
|         if ( !tud_msc_prevent_allow_medium_removal_cb(lun, prevent_allow->prohibit_removal, prevent_allow->control) ) | ||||
|         { | ||||
|           // Failed status response | ||||
|           resplen = - 1; | ||||
|  | ||||
|           // set default sense if not set by callback | ||||
|           if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun); | ||||
|         } | ||||
|       } | ||||
|     break; | ||||
|  | ||||
|  | ||||
|     case SCSI_CMD_READ_CAPACITY_10: | ||||
|     { | ||||
|       uint32_t block_count; | ||||
|   | ||||
| @@ -131,6 +131,9 @@ TU_ATTR_WEAK uint8_t tud_msc_get_maxlun_cb(void); | ||||
| // - Start = 1 : active mode, if load_eject = 1 : load disk storage | ||||
| TU_ATTR_WEAK bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject); | ||||
|  | ||||
| //Invoked when we receive the Prevent / Allow Medium Removal command | ||||
| TU_ATTR_WEAK bool tud_msc_prevent_allow_medium_removal_cb(uint8_t lun, uint8_t prohibit_removal, uint8_t control); | ||||
|  | ||||
| // Invoked when received REQUEST_SENSE | ||||
| TU_ATTR_WEAK int32_t tud_msc_request_sense_cb(uint8_t lun, void* buffer, uint16_t bufsize); | ||||
|  | ||||
|   | ||||
| @@ -2,6 +2,7 @@ | ||||
|  * The MIT License (MIT) | ||||
|  * | ||||
|  * Copyright (c) 2021, Ha Thach (tinyusb.org) | ||||
|  * Copyright (c) 2024, Hardy Griech | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
| @@ -24,22 +25,58 @@ | ||||
|  * This file is part of the TinyUSB stack. | ||||
|  */ | ||||
|  | ||||
|  | ||||
| #ifndef _TUSB_NCM_H_ | ||||
| #define _TUSB_NCM_H_ | ||||
|  | ||||
| #include "common/tusb_common.h" | ||||
|  | ||||
| #ifdef __cplusplus | ||||
|  extern "C" { | ||||
| // NTB buffers size for reception side, must be >> MTU to avoid TCP retransmission (driver issue ?) | ||||
| // Linux use 2048 as minimal size | ||||
| #ifndef CFG_TUD_NCM_OUT_NTB_MAX_SIZE | ||||
|   #define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200 | ||||
| #endif | ||||
|  | ||||
| // Table 4.3 Data Class Interface Protocol Codes | ||||
| typedef enum | ||||
| { | ||||
|   NCM_DATA_PROTOCOL_NETWORK_TRANSFER_BLOCK = 0x01 | ||||
| } ncm_data_interface_protocol_code_t; | ||||
| // NTB buffers size for reception side, must be > MTU | ||||
| // Linux use 2048 as minimal size | ||||
| #ifndef CFG_TUD_NCM_IN_NTB_MAX_SIZE | ||||
|   #define CFG_TUD_NCM_IN_NTB_MAX_SIZE 3200 | ||||
| #endif | ||||
|  | ||||
| // Number of NTB buffers for reception side | ||||
| // Depending on the configuration, this parameter could be increased with the cost of additional RAM requirements | ||||
| // On Full-Speed (RP2040) : | ||||
| //    1  - good performance | ||||
| //    2  - up to 30% more performance with iperf with small packets | ||||
| //    >2 - no performance gain | ||||
| // On High-Speed (STM32F7) : | ||||
| //    No performance gain | ||||
| #ifndef CFG_TUD_NCM_OUT_NTB_N | ||||
|   #define CFG_TUD_NCM_OUT_NTB_N 1 | ||||
| #endif | ||||
|  | ||||
| // Number of NTB buffers for transmission side | ||||
| // Depending on the configuration, this parameter could be increased with the cost of additional RAM requirements | ||||
| // On Full-Speed (RP2040) : | ||||
| //    1 - good performance but SystemView shows lost events (on load test) | ||||
| //    2 - up to 50% more performance with iperf with small packets, "tud_network_can_xmit: request blocked" | ||||
| //        happens from time to time with SystemView | ||||
| //    3 - "tud_network_can_xmit: request blocked" never happens | ||||
| //    >3 - no performance gain | ||||
| // On High-Speed (STM32F7) : | ||||
| //    No performance gain | ||||
| #ifndef CFG_TUD_NCM_IN_NTB_N | ||||
|   #define CFG_TUD_NCM_IN_NTB_N 1 | ||||
| #endif | ||||
|  | ||||
| // How many datagrams it is allowed to put into an NTB for transmission side | ||||
| #ifndef CFG_TUD_NCM_IN_MAX_DATAGRAMS_PER_NTB | ||||
|   #define CFG_TUD_NCM_IN_MAX_DATAGRAMS_PER_NTB 8 | ||||
| #endif | ||||
|  | ||||
| // This tells the host how many datagrams it is allowed to put into an NTB | ||||
| #ifndef CFG_TUD_NCM_OUT_MAX_DATAGRAMS_PER_NTB | ||||
|   #define CFG_TUD_NCM_OUT_MAX_DATAGRAMS_PER_NTB 6 | ||||
| #endif | ||||
|  | ||||
| // Table 6.2 Class-Specific Request Codes for Network Control Model subclass | ||||
| typedef enum | ||||
| @@ -62,8 +99,65 @@ typedef enum | ||||
|   NCM_SET_CRC_MODE                                 = 0x8A, | ||||
| } ncm_request_code_t; | ||||
|  | ||||
| #ifdef __cplusplus | ||||
|  } | ||||
| #endif | ||||
| #define NTH16_SIGNATURE 0x484D434E | ||||
| #define NDP16_SIGNATURE_NCM0 0x304D434E | ||||
| #define NDP16_SIGNATURE_NCM1 0x314D434E | ||||
|  | ||||
| typedef struct TU_ATTR_PACKED { | ||||
|   uint16_t wLength; | ||||
|   uint16_t bmNtbFormatsSupported; | ||||
|   uint32_t dwNtbInMaxSize; | ||||
|   uint16_t wNdbInDivisor; | ||||
|   uint16_t wNdbInPayloadRemainder; | ||||
|   uint16_t wNdbInAlignment; | ||||
|   uint16_t wReserved; | ||||
|   uint32_t dwNtbOutMaxSize; | ||||
|   uint16_t wNdbOutDivisor; | ||||
|   uint16_t wNdbOutPayloadRemainder; | ||||
|   uint16_t wNdbOutAlignment; | ||||
|   uint16_t wNtbOutMaxDatagrams; | ||||
| } ntb_parameters_t; | ||||
|  | ||||
| typedef struct TU_ATTR_PACKED { | ||||
|   uint32_t dwSignature; | ||||
|   uint16_t wHeaderLength; | ||||
|   uint16_t wSequence; | ||||
|   uint16_t wBlockLength; | ||||
|   uint16_t wNdpIndex; | ||||
| } nth16_t; | ||||
|  | ||||
| typedef struct TU_ATTR_PACKED { | ||||
|   uint16_t wDatagramIndex; | ||||
|   uint16_t wDatagramLength; | ||||
| } ndp16_datagram_t; | ||||
|  | ||||
| typedef struct TU_ATTR_PACKED { | ||||
|   uint32_t dwSignature; | ||||
|   uint16_t wLength; | ||||
|   uint16_t wNextNdpIndex; | ||||
|   //ndp16_datagram_t datagram[]; | ||||
| } ndp16_t; | ||||
|  | ||||
| typedef union TU_ATTR_PACKED { | ||||
|   struct { | ||||
|     nth16_t nth; | ||||
|     ndp16_t ndp; | ||||
|     ndp16_datagram_t ndp_datagram[CFG_TUD_NCM_IN_MAX_DATAGRAMS_PER_NTB + 1]; | ||||
|   }; | ||||
|   uint8_t data[CFG_TUD_NCM_IN_NTB_MAX_SIZE]; | ||||
| } xmit_ntb_t; | ||||
|  | ||||
| typedef union TU_ATTR_PACKED { | ||||
|   struct { | ||||
|     nth16_t nth; | ||||
|     // only the header is at a guaranteed position | ||||
|   }; | ||||
|   uint8_t data[CFG_TUD_NCM_OUT_NTB_MAX_SIZE]; | ||||
| } recv_ntb_t; | ||||
|  | ||||
| struct ncm_notify_t { | ||||
|   tusb_control_request_t header; | ||||
|   uint32_t downlink, uplink; | ||||
| }; | ||||
|  | ||||
| #endif | ||||
|   | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -28,14 +28,13 @@ | ||||
| #ifndef _TUSB_NET_DEVICE_H_ | ||||
| #define _TUSB_NET_DEVICE_H_ | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include "class/cdc/cdc.h" | ||||
|  | ||||
| #if CFG_TUD_ECM_RNDIS && CFG_TUD_NCM | ||||
| #error "Cannot enable both ECM_RNDIS and NCM network drivers" | ||||
| #endif | ||||
|  | ||||
| #include "ncm.h" | ||||
|  | ||||
| /* declared here, NOT in usb_descriptors.c, so that the driver can intelligently ZLP as needed */ | ||||
| #define CFG_TUD_NET_ENDPOINT_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) | ||||
|  | ||||
| @@ -44,21 +43,13 @@ | ||||
| #define CFG_TUD_NET_MTU           1514 | ||||
| #endif | ||||
|  | ||||
| #ifndef CFG_TUD_NCM_IN_NTB_MAX_SIZE | ||||
| #define CFG_TUD_NCM_IN_NTB_MAX_SIZE 3200 | ||||
| #endif | ||||
|  | ||||
| #ifndef CFG_TUD_NCM_OUT_NTB_MAX_SIZE | ||||
| #define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200 | ||||
| #endif | ||||
| // Table 4.3 Data Class Interface Protocol Codes | ||||
| typedef enum | ||||
| { | ||||
|   NCM_DATA_PROTOCOL_NETWORK_TRANSFER_BLOCK = 0x01 | ||||
| } ncm_data_interface_protocol_code_t; | ||||
|  | ||||
| #ifndef CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB | ||||
| #define CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB 8 | ||||
| #endif | ||||
|  | ||||
| #ifndef CFG_TUD_NCM_ALIGNMENT | ||||
| #define CFG_TUD_NCM_ALIGNMENT 4 | ||||
| #endif | ||||
|  | ||||
| #ifdef __cplusplus | ||||
|  extern "C" { | ||||
| @@ -96,11 +87,6 @@ void tud_network_init_cb(void); | ||||
| // TODO removed later since it is not part of tinyusb stack | ||||
| extern uint8_t tud_network_mac_address[6]; | ||||
|  | ||||
| //------------- NCM -------------// | ||||
|  | ||||
| // callback to client providing optional indication of internal state of network driver | ||||
| void tud_network_link_state_cb(bool state); | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // INTERNAL USBD-CLASS DRIVER API | ||||
| //--------------------------------------------------------------------+ | ||||
|   | ||||
| @@ -183,6 +183,23 @@ typedef enum { | ||||
|  | ||||
| } usmtmc_request_type_enum; | ||||
|  | ||||
| typedef enum { | ||||
|   // The last and first valid bNotify1 for use by the USBTMC class specification. | ||||
|   USBTMC_bNOTIFY1_USBTMC_FIRST          = 0x00, | ||||
|   USBTMC_bNOTIFY1_USBTMC_LAST           = 0x3F, | ||||
|  | ||||
|   // The last and first valid bNotify1 for use by vendors. | ||||
|   USBTMC_bNOTIFY1_VENDOR_SPECIFIC_FIRST = 0x40, | ||||
|   USBTMC_bNOTIFY1_VENDOR_SPECIFIC_LAST  = 0x7F, | ||||
|  | ||||
|   // The last and first valid bNotify1 for use by USBTMC subclass specifications. | ||||
|   USBTMC_bNOTIFY1_SUBCLASS_FIRST        = 0x80, | ||||
|   USBTMC_bNOTIFY1_SUBCLASS_LAST         = 0xFF, | ||||
|  | ||||
|   // From the USB488 Subclass Specification, Section 3.4. | ||||
|   USB488_bNOTIFY1_SRQ                   = 0x81, | ||||
| } usbtmc_int_in_payload_format; | ||||
|  | ||||
| typedef enum { | ||||
|   USBTMC_STATUS_SUCCESS = 0x01, | ||||
|   USBTMC_STATUS_PENDING = 0x02, | ||||
| @@ -303,6 +320,14 @@ typedef struct TU_ATTR_PACKED | ||||
|  | ||||
| TU_VERIFY_STATIC(sizeof(usbtmc_read_stb_rsp_488_t) == 3u, "struct wrong length"); | ||||
|  | ||||
| typedef struct TU_ATTR_PACKED | ||||
| { | ||||
|   uint8_t bNotify1; // Must be USB488_bNOTIFY1_SRQ | ||||
|   uint8_t StatusByte; | ||||
| } usbtmc_srq_interrupt_488_t; | ||||
|  | ||||
| TU_VERIFY_STATIC(sizeof(usbtmc_srq_interrupt_488_t) == 2u, "struct wrong length"); | ||||
|  | ||||
| typedef struct TU_ATTR_PACKED | ||||
| { | ||||
|   struct TU_ATTR_PACKED | ||||
|   | ||||
| @@ -86,6 +86,11 @@ tu_static char logMsg[150]; | ||||
| // imposes a minimum buffer size of 32 bytes. | ||||
| #define USBTMCD_BUFFER_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) | ||||
|  | ||||
| // Interrupt endpoint buffer size, default to 2 bytes as USB488 specification. | ||||
| #ifndef CFG_TUD_USBTMC_INT_EP_SIZE | ||||
| #define CFG_TUD_USBTMC_INT_EP_SIZE 2 | ||||
| #endif | ||||
|  | ||||
| /* | ||||
|  * The state machine does not allow simultaneous reading and writing. This is | ||||
|  * consistent with USBTMC. | ||||
| @@ -124,13 +129,15 @@ typedef struct | ||||
|   uint8_t ep_bulk_in; | ||||
|   uint8_t ep_bulk_out; | ||||
|   uint8_t ep_int_in; | ||||
|   uint32_t ep_bulk_in_wMaxPacketSize; | ||||
|   uint32_t ep_bulk_out_wMaxPacketSize; | ||||
|   // IN buffer is only used for first packet, not the remainder | ||||
|   // in order to deal with prepending header | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_in_buf[USBTMCD_BUFFER_SIZE]; | ||||
|   uint32_t ep_bulk_in_wMaxPacketSize; | ||||
|   // OUT buffer receives one packet at a time | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_out_buf[USBTMCD_BUFFER_SIZE]; | ||||
|   uint32_t ep_bulk_out_wMaxPacketSize; | ||||
|   // Buffer int msg to ensure alignment and placement correctness | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t ep_int_in_buf[CFG_TUD_USBTMC_INT_EP_SIZE]; | ||||
|  | ||||
|   uint32_t transfer_size_remaining; // also used for requested length for bulk IN. | ||||
|   uint32_t transfer_size_sent;      // To keep track of data bytes that have been queued in FIFO (not header bytes) | ||||
| @@ -240,6 +247,19 @@ bool tud_usbtmc_transmit_dev_msg_data( | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| bool tud_usbtmc_transmit_notification_data(const void * data, size_t len) | ||||
| { | ||||
| #ifndef NDEBUG | ||||
|   TU_ASSERT(len > 0); | ||||
|   TU_ASSERT(usbtmc_state.ep_int_in != 0); | ||||
| #endif | ||||
|   TU_VERIFY(usbd_edpt_busy(usbtmc_state.rhport, usbtmc_state.ep_int_in)); | ||||
|  | ||||
|   TU_VERIFY(tu_memcpy_s(usbtmc_state.ep_int_in_buf, sizeof(usbtmc_state.ep_int_in_buf), data, len) == 0); | ||||
|   TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_int_in, usbtmc_state.ep_int_in_buf, (uint16_t)len)); | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| void usbtmcd_init_cb(void) | ||||
| { | ||||
|   usbtmc_state.capabilities = tud_usbtmc_get_capabilities_cb(); | ||||
| @@ -547,9 +567,10 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint | ||||
|     case STATE_TX_INITIATED: | ||||
|       if(usbtmc_state.transfer_size_remaining >= sizeof(usbtmc_state.ep_bulk_in_buf)) | ||||
|       { | ||||
|         // FIXME! This removes const below! | ||||
|         // Copy buffer to ensure alignment correctness | ||||
|         memcpy(usbtmc_state.ep_bulk_in_buf, usbtmc_state.devInBuffer, sizeof(usbtmc_state.ep_bulk_in_buf)); | ||||
|         TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, | ||||
|             (void*)(uintptr_t) usbtmc_state.devInBuffer, sizeof(usbtmc_state.ep_bulk_in_buf))); | ||||
|             usbtmc_state.ep_bulk_in_buf, sizeof(usbtmc_state.ep_bulk_in_buf))); | ||||
|         usbtmc_state.devInBuffer += sizeof(usbtmc_state.ep_bulk_in_buf); | ||||
|         usbtmc_state.transfer_size_remaining -= sizeof(usbtmc_state.ep_bulk_in_buf); | ||||
|         usbtmc_state.transfer_size_sent += sizeof(usbtmc_state.ep_bulk_in_buf); | ||||
| @@ -585,7 +606,9 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint | ||||
|     } | ||||
|   } | ||||
|   else if (ep_addr == usbtmc_state.ep_int_in) { | ||||
|     // Good? | ||||
|     if (tud_usbtmc_notification_complete_cb) { | ||||
|       TU_VERIFY(tud_usbtmc_notification_complete_cb()); | ||||
|     } | ||||
|     return true; | ||||
|   } | ||||
|   return false; | ||||
|   | ||||
| @@ -73,6 +73,10 @@ bool tud_usbtmc_check_abort_bulk_in_cb(usbtmc_check_abort_bulk_rsp_t *rsp); | ||||
| bool tud_usbtmc_check_abort_bulk_out_cb(usbtmc_check_abort_bulk_rsp_t *rsp); | ||||
| bool tud_usbtmc_check_clear_cb(usbtmc_get_clear_status_rsp_t *rsp); | ||||
|  | ||||
| // The interrupt-IN endpoint buffer was transmitted to the host. Use | ||||
| // tud_usbtmc_transmit_notification_data to send another notification. | ||||
| TU_ATTR_WEAK bool tud_usbtmc_notification_complete_cb(void); | ||||
|  | ||||
| // Indicator pulse should be 0.5 to 1.0 seconds long | ||||
| TU_ATTR_WEAK bool tud_usbtmc_indicator_pulse_cb(tusb_control_request_t const * msg, uint8_t *tmcResult); | ||||
|  | ||||
| @@ -82,17 +86,23 @@ TU_ATTR_WEAK bool tud_usbtmc_msg_trigger_cb(usbtmc_msg_generic_t* msg); | ||||
| //TU_ATTR_WEAK bool tud_usbtmc_app_go_to_local_cb(); | ||||
| #endif | ||||
|  | ||||
| /******************************************* | ||||
|  * Called from app | ||||
|  * | ||||
|  * We keep a reference to the buffer, so it MUST not change until the app is | ||||
|  * notified that the transfer is complete. | ||||
|  ******************************************/ | ||||
|  | ||||
| // Called from app | ||||
| // | ||||
| // We keep a reference to the buffer, so it MUST not change until the app is | ||||
| // notified that the transfer is complete. | ||||
| bool tud_usbtmc_transmit_dev_msg_data( | ||||
|     const void * data, size_t len, | ||||
|     bool endOfMessage, bool usingTermChar); | ||||
|  | ||||
| // Buffers a notification to be sent to the host. The data starts | ||||
| // with the bNotify1 field, see the USBTMC Specification, Table 13. | ||||
| // | ||||
| // If the previous notification data has not yet been sent, this | ||||
| // returns false. | ||||
| // | ||||
| // Requires an interrupt endpoint in the interface. | ||||
| bool tud_usbtmc_transmit_notification_data(const void * data, size_t len); | ||||
|  | ||||
| bool tud_usbtmc_start_bus_read(void); | ||||
|  | ||||
|  | ||||
| @@ -105,9 +115,4 @@ void     usbtmcd_reset_cb(uint8_t rhport); | ||||
| bool     usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); | ||||
| bool     usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); | ||||
|  | ||||
| /************************************************************ | ||||
|  * USBTMC Descriptor Templates | ||||
|  *************************************************************/ | ||||
|  | ||||
|  | ||||
| #endif /* CLASS_USBTMC_USBTMC_DEVICE_H_ */ | ||||
|   | ||||
							
								
								
									
										315
									
								
								src/class/vendor/vendor_device.c
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										315
									
								
								src/class/vendor/vendor_device.c
									
									
									
									
										vendored
									
									
								
							| @@ -36,134 +36,103 @@ | ||||
| //--------------------------------------------------------------------+ | ||||
| // MACRO CONSTANT TYPEDEF | ||||
| //--------------------------------------------------------------------+ | ||||
| typedef struct | ||||
| { | ||||
| typedef struct { | ||||
|   uint8_t itf_num; | ||||
|   uint8_t ep_in; | ||||
|   uint8_t ep_out; | ||||
|  | ||||
|   /*------------- From this point, data is not cleared by bus reset -------------*/ | ||||
|   tu_fifo_t rx_ff; | ||||
|   tu_fifo_t tx_ff; | ||||
|  | ||||
|   uint8_t rx_ff_buf[CFG_TUD_VENDOR_RX_BUFSIZE]; | ||||
|   uint8_t tx_ff_buf[CFG_TUD_VENDOR_TX_BUFSIZE]; | ||||
|  | ||||
|   OSAL_MUTEX_DEF(rx_ff_mutex); | ||||
|   OSAL_MUTEX_DEF(tx_ff_mutex); | ||||
|  | ||||
|   // Endpoint Transfer buffer | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE]; | ||||
|   CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE]; | ||||
|   CFG_TUD_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE]; | ||||
|   CFG_TUD_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE]; | ||||
|  | ||||
|   struct { | ||||
|     tu_edpt_stream_t stream; | ||||
|     #if CFG_TUD_VENDOR_TX_BUFSIZE > 0 | ||||
|     uint8_t ff_buf[CFG_TUD_VENDOR_TX_BUFSIZE]; | ||||
|     #endif | ||||
|   }tx; | ||||
|  | ||||
|   struct { | ||||
|     tu_edpt_stream_t stream; | ||||
|     #if CFG_TUD_VENDOR_RX_BUFSIZE > 0 | ||||
|     uint8_t ff_buf[CFG_TUD_VENDOR_RX_BUFSIZE]; | ||||
|     #endif | ||||
|   } rx; | ||||
|  | ||||
| } vendord_interface_t; | ||||
|  | ||||
| CFG_TUD_MEM_SECTION tu_static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR]; | ||||
| CFG_TUD_MEM_SECTION static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR]; | ||||
|  | ||||
| #define ITF_MEM_RESET_SIZE   offsetof(vendord_interface_t, rx_ff) | ||||
| #define ITF_MEM_RESET_SIZE   (offsetof(vendord_interface_t, itf_num) + sizeof(((vendord_interface_t *)0)->itf_num)) | ||||
|  | ||||
| //-------------------------------------------------------------------- | ||||
| // Application API | ||||
| //-------------------------------------------------------------------- | ||||
|  | ||||
| bool tud_vendor_n_mounted (uint8_t itf) | ||||
| { | ||||
|   return _vendord_itf[itf].ep_in && _vendord_itf[itf].ep_out; | ||||
| } | ||||
|  | ||||
| uint32_t tud_vendor_n_available (uint8_t itf) | ||||
| { | ||||
|   return tu_fifo_count(&_vendord_itf[itf].rx_ff); | ||||
| } | ||||
|  | ||||
| bool tud_vendor_n_peek(uint8_t itf, uint8_t* u8) | ||||
| { | ||||
|   return tu_fifo_peek(&_vendord_itf[itf].rx_ff, u8); | ||||
| bool tud_vendor_n_mounted(uint8_t itf) { | ||||
|   TU_VERIFY(itf < CFG_TUD_VENDOR); | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|   return p_itf->rx.stream.ep_addr || p_itf->tx.stream.ep_addr; | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Read API | ||||
| //--------------------------------------------------------------------+ | ||||
| static void _prep_out_transaction (vendord_interface_t* p_itf) | ||||
| { | ||||
| uint32_t tud_vendor_n_available(uint8_t itf) { | ||||
|   TU_VERIFY(itf < CFG_TUD_VENDOR, 0); | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|  | ||||
|   return tu_edpt_stream_read_available(&p_itf->rx.stream); | ||||
| } | ||||
|  | ||||
| bool tud_vendor_n_peek(uint8_t itf, uint8_t* u8) { | ||||
|   TU_VERIFY(itf < CFG_TUD_VENDOR); | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|  | ||||
|   return tu_edpt_stream_peek(&p_itf->rx.stream, u8); | ||||
| } | ||||
|  | ||||
| uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize) { | ||||
|   TU_VERIFY(itf < CFG_TUD_VENDOR, 0); | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|   uint8_t const rhport = 0; | ||||
|  | ||||
|     // claim endpoint | ||||
|   TU_VERIFY(usbd_edpt_claim(rhport, p_itf->ep_out), ); | ||||
|  | ||||
|   // Prepare for incoming data but only allow what we can store in the ring buffer. | ||||
|   uint16_t max_read = tu_fifo_remaining(&p_itf->rx_ff); | ||||
|   if ( max_read >= CFG_TUD_VENDOR_EPSIZE ) | ||||
|   { | ||||
|     usbd_edpt_xfer(rhport, p_itf->ep_out, p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE); | ||||
|   } | ||||
|   else | ||||
|   { | ||||
|     // Release endpoint since we don't make any transfer | ||||
|     usbd_edpt_release(rhport, p_itf->ep_out); | ||||
|   } | ||||
|   return tu_edpt_stream_read(rhport, &p_itf->rx.stream, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize) | ||||
| { | ||||
| void tud_vendor_n_read_flush (uint8_t itf) { | ||||
|   TU_VERIFY(itf < CFG_TUD_VENDOR, ); | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|   uint32_t num_read = tu_fifo_read_n(&p_itf->rx_ff, buffer, (uint16_t) bufsize); | ||||
|   _prep_out_transaction(p_itf); | ||||
|   return num_read; | ||||
| } | ||||
|   uint8_t const rhport = 0; | ||||
|  | ||||
| void tud_vendor_n_read_flush (uint8_t itf) | ||||
| { | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|   tu_fifo_clear(&p_itf->rx_ff); | ||||
|   _prep_out_transaction(p_itf); | ||||
|   tu_edpt_stream_clear(&p_itf->rx.stream); | ||||
|   tu_edpt_stream_read_xfer(rhport, &p_itf->rx.stream); | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Write API | ||||
| //--------------------------------------------------------------------+ | ||||
| uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize) | ||||
| { | ||||
| uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize) { | ||||
|   TU_VERIFY(itf < CFG_TUD_VENDOR, 0); | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|   uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, (uint16_t) bufsize); | ||||
|  | ||||
|   // flush if queue more than packet size | ||||
|   if (tu_fifo_count(&p_itf->tx_ff) >= CFG_TUD_VENDOR_EPSIZE) { | ||||
|     tud_vendor_n_write_flush(itf); | ||||
|   } | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| uint32_t tud_vendor_n_write_flush (uint8_t itf) | ||||
| { | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|  | ||||
|   // Skip if usb is not ready yet | ||||
|   TU_VERIFY( tud_ready(), 0 ); | ||||
|  | ||||
|   // No data to send | ||||
|   if ( !tu_fifo_count(&p_itf->tx_ff) ) return 0; | ||||
|  | ||||
|   uint8_t const rhport = 0; | ||||
|  | ||||
|   // Claim the endpoint | ||||
|   TU_VERIFY( usbd_edpt_claim(rhport, p_itf->ep_in), 0 ); | ||||
|  | ||||
|   // Pull data from FIFO | ||||
|   uint16_t const count = tu_fifo_read_n(&p_itf->tx_ff, p_itf->epin_buf, sizeof(p_itf->epin_buf)); | ||||
|  | ||||
|   if ( count ) | ||||
|   { | ||||
|     TU_ASSERT( usbd_edpt_xfer(rhport, p_itf->ep_in, p_itf->epin_buf, count), 0 ); | ||||
|     return count; | ||||
|   }else | ||||
|   { | ||||
|     // Release endpoint since we don't make any transfer | ||||
|     // Note: data is dropped if terminal is not connected | ||||
|     usbd_edpt_release(rhport, p_itf->ep_in); | ||||
|     return 0; | ||||
|   } | ||||
|   return tu_edpt_stream_write(rhport, &p_itf->tx.stream, buffer, (uint16_t) bufsize); | ||||
| } | ||||
|  | ||||
| uint32_t tud_vendor_n_write_available (uint8_t itf) | ||||
| { | ||||
|   return tu_fifo_remaining(&_vendord_itf[itf].tx_ff); | ||||
| uint32_t tud_vendor_n_write_flush (uint8_t itf) { | ||||
|   TU_VERIFY(itf < CFG_TUD_VENDOR, 0); | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|   uint8_t const rhport = 0; | ||||
|  | ||||
|   return tu_edpt_stream_write_xfer(rhport, &p_itf->tx.stream); | ||||
| } | ||||
|  | ||||
| uint32_t tud_vendor_n_write_available (uint8_t itf) { | ||||
|   TU_VERIFY(itf < CFG_TUD_VENDOR, 0); | ||||
|   vendord_interface_t* p_itf = &_vendord_itf[itf]; | ||||
|   uint8_t const rhport = 0; | ||||
|  | ||||
|   return tu_edpt_stream_write_available(rhport, &p_itf->tx.stream); | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| @@ -175,70 +144,61 @@ void vendord_init(void) { | ||||
|   for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) { | ||||
|     vendord_interface_t* p_itf = &_vendord_itf[i]; | ||||
|  | ||||
|     // config fifo | ||||
|     tu_fifo_config(&p_itf->rx_ff, p_itf->rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE, 1, false); | ||||
|     tu_fifo_config(&p_itf->tx_ff, p_itf->tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE, 1, false); | ||||
|     uint8_t* rx_ff_buf = | ||||
|                         #if CFG_TUD_VENDOR_RX_BUFSIZE > 0 | ||||
|                           p_itf->rx.ff_buf; | ||||
|                         #else | ||||
|                           NULL; | ||||
|                         #endif | ||||
|  | ||||
|     #if OSAL_MUTEX_REQUIRED | ||||
|     osal_mutex_t mutex_rd = osal_mutex_create(&p_itf->rx_ff_mutex); | ||||
|     osal_mutex_t mutex_wr = osal_mutex_create(&p_itf->tx_ff_mutex); | ||||
|     TU_ASSERT(mutex_rd && mutex_wr,); | ||||
|     tu_edpt_stream_init(&p_itf->rx.stream, false, false, false, | ||||
|                         rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE, | ||||
|                         p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE); | ||||
|  | ||||
|     tu_fifo_config_mutex(&p_itf->rx_ff, NULL, mutex_rd); | ||||
|     tu_fifo_config_mutex(&p_itf->tx_ff, mutex_wr, NULL); | ||||
|     #endif | ||||
|     uint8_t* tx_ff_buf = | ||||
|                         #if CFG_TUD_VENDOR_TX_BUFSIZE > 0 | ||||
|                           p_itf->tx.ff_buf; | ||||
|                         #else | ||||
|                           NULL; | ||||
|                         #endif | ||||
|  | ||||
|     tu_edpt_stream_init(&p_itf->tx.stream, false, true, false, | ||||
|                         tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE, | ||||
|                         p_itf->epin_buf, CFG_TUD_VENDOR_EPSIZE); | ||||
|   } | ||||
| } | ||||
|  | ||||
| bool vendord_deinit(void) { | ||||
|   #if OSAL_MUTEX_REQUIRED | ||||
|   for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) { | ||||
|     vendord_interface_t* p_itf = &_vendord_itf[i]; | ||||
|     osal_mutex_t mutex_rd = p_itf->rx_ff.mutex_rd; | ||||
|     osal_mutex_t mutex_wr = p_itf->tx_ff.mutex_wr; | ||||
|  | ||||
|     if (mutex_rd) { | ||||
|       osal_mutex_delete(mutex_rd); | ||||
|       tu_fifo_config_mutex(&p_itf->rx_ff, NULL, NULL); | ||||
|     } | ||||
|  | ||||
|     if (mutex_wr) { | ||||
|       osal_mutex_delete(mutex_wr); | ||||
|       tu_fifo_config_mutex(&p_itf->tx_ff, NULL, NULL); | ||||
|     } | ||||
|     tu_edpt_stream_deinit(&p_itf->rx.stream); | ||||
|     tu_edpt_stream_deinit(&p_itf->tx.stream); | ||||
|   } | ||||
|   #endif | ||||
|  | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| void vendord_reset(uint8_t rhport) | ||||
| { | ||||
| void vendord_reset(uint8_t rhport) { | ||||
|   (void) rhport; | ||||
|  | ||||
|   for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) | ||||
|   { | ||||
|   for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) { | ||||
|     vendord_interface_t* p_itf = &_vendord_itf[i]; | ||||
|  | ||||
|     tu_memclr(p_itf, ITF_MEM_RESET_SIZE); | ||||
|     tu_fifo_clear(&p_itf->rx_ff); | ||||
|     tu_fifo_clear(&p_itf->tx_ff); | ||||
|     tu_edpt_stream_clear(&p_itf->rx.stream); | ||||
|     tu_edpt_stream_clear(&p_itf->tx.stream); | ||||
|     tu_edpt_stream_close(&p_itf->rx.stream); | ||||
|     tu_edpt_stream_close(&p_itf->tx.stream); | ||||
|   } | ||||
| } | ||||
|  | ||||
| uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len) | ||||
| { | ||||
| uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len) { | ||||
|   TU_VERIFY(TUSB_CLASS_VENDOR_SPECIFIC == desc_itf->bInterfaceClass, 0); | ||||
|  | ||||
|   uint8_t const * p_desc = tu_desc_next(desc_itf); | ||||
|   uint8_t const * desc_end = p_desc + max_len; | ||||
|   const uint8_t* p_desc = tu_desc_next(desc_itf); | ||||
|   const uint8_t* desc_end = p_desc + max_len; | ||||
|  | ||||
|   // Find available interface | ||||
|   vendord_interface_t* p_vendor = NULL; | ||||
|   for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) | ||||
|   { | ||||
|     if ( _vendord_itf[i].ep_in == 0 && _vendord_itf[i].ep_out == 0 ) | ||||
|     { | ||||
|   for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) { | ||||
|     if (!tud_vendor_n_mounted(i)) { | ||||
|       p_vendor = &_vendord_itf[i]; | ||||
|       break; | ||||
|     } | ||||
| @@ -246,61 +206,68 @@ uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, ui | ||||
|   TU_VERIFY(p_vendor, 0); | ||||
|  | ||||
|   p_vendor->itf_num = desc_itf->bInterfaceNumber; | ||||
|   if (desc_itf->bNumEndpoints) | ||||
|   { | ||||
|   uint8_t found_ep = 0; | ||||
|   while (found_ep < desc_itf->bNumEndpoints) { | ||||
|     // skip non-endpoint descriptors | ||||
|     while ( (TUSB_DESC_ENDPOINT != tu_desc_type(p_desc)) && (p_desc < desc_end) ) | ||||
|     { | ||||
|     while ( (TUSB_DESC_ENDPOINT != tu_desc_type(p_desc)) && (p_desc < desc_end) ) { | ||||
|       p_desc = tu_desc_next(p_desc); | ||||
|     } | ||||
|  | ||||
|     // Open endpoint pair with usbd helper | ||||
|     TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_BULK, &p_vendor->ep_out, &p_vendor->ep_in), 0); | ||||
|  | ||||
|     p_desc += desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t); | ||||
|  | ||||
|     // Prepare for incoming data | ||||
|     if ( p_vendor->ep_out ) | ||||
|     { | ||||
|       _prep_out_transaction(p_vendor); | ||||
|     if (p_desc >= desc_end) { | ||||
|       break; | ||||
|     } | ||||
|  | ||||
|     if ( p_vendor->ep_in ) tud_vendor_n_write_flush((uint8_t)(p_vendor - _vendord_itf)); | ||||
|     const tusb_desc_endpoint_t* desc_ep = (const tusb_desc_endpoint_t*) p_desc; | ||||
|     TU_ASSERT(usbd_edpt_open(rhport, desc_ep)); | ||||
|     found_ep++; | ||||
|  | ||||
|     if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) { | ||||
|       tu_edpt_stream_open(&p_vendor->tx.stream, desc_ep); | ||||
|       tud_vendor_n_write_flush((uint8_t)(p_vendor - _vendord_itf)); | ||||
|     } else { | ||||
|       tu_edpt_stream_open(&p_vendor->rx.stream, desc_ep); | ||||
|       TU_ASSERT(tu_edpt_stream_read_xfer(rhport, &p_vendor->rx.stream) > 0, 0); // prepare for incoming data | ||||
|     } | ||||
|  | ||||
|     p_desc = tu_desc_next(p_desc); | ||||
|   } | ||||
|  | ||||
|   return (uint16_t) ((uintptr_t) p_desc - (uintptr_t) desc_itf); | ||||
| } | ||||
|  | ||||
| bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) | ||||
| { | ||||
|   (void) rhport; | ||||
| bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { | ||||
|   (void) result; | ||||
|  | ||||
|   uint8_t itf = 0; | ||||
|   vendord_interface_t* p_itf = _vendord_itf; | ||||
|  | ||||
|   for ( ; ; itf++, p_itf++) | ||||
|   { | ||||
|     if (itf >= TU_ARRAY_SIZE(_vendord_itf)) return false; | ||||
|  | ||||
|     if ( ( ep_addr == p_itf->ep_out ) || ( ep_addr == p_itf->ep_in ) ) break; | ||||
|   for ( ; ; itf++, p_itf++) { | ||||
|     if (itf >= CFG_TUD_VENDOR) return false; | ||||
|     if ((ep_addr == p_itf->rx.stream.ep_addr) || (ep_addr == p_itf->tx.stream.ep_addr)) break; | ||||
|   } | ||||
|  | ||||
|   if ( ep_addr == p_itf->ep_out ) | ||||
|   { | ||||
|     // Receive new data | ||||
|     tu_fifo_write_n(&p_itf->rx_ff, p_itf->epout_buf, (uint16_t) xferred_bytes); | ||||
|   if ( ep_addr == p_itf->rx.stream.ep_addr ) { | ||||
|     // Received new data: put into stream's fifo | ||||
|     tu_edpt_stream_read_xfer_complete(&p_itf->rx.stream, xferred_bytes); | ||||
|  | ||||
|     // Invoked callback if any | ||||
|     if (tud_vendor_rx_cb) tud_vendor_rx_cb(itf); | ||||
|     if (tud_vendor_rx_cb) { | ||||
|       tud_vendor_rx_cb(itf, p_itf->epout_buf, (uint16_t) xferred_bytes); | ||||
|     } | ||||
|  | ||||
|     _prep_out_transaction(p_itf); | ||||
|   } | ||||
|   else if ( ep_addr == p_itf->ep_in ) | ||||
|   { | ||||
|     if (tud_vendor_tx_cb) tud_vendor_tx_cb(itf, (uint16_t) xferred_bytes); | ||||
|     // Send complete, try to send more if possible | ||||
|     tud_vendor_n_write_flush(itf); | ||||
|     tu_edpt_stream_read_xfer(rhport, &p_itf->rx.stream); | ||||
|   } else if ( ep_addr == p_itf->tx.stream.ep_addr ) { | ||||
|     // Send complete | ||||
|     if (tud_vendor_tx_cb) { | ||||
|       tud_vendor_tx_cb(itf, (uint16_t) xferred_bytes); | ||||
|     } | ||||
|  | ||||
|     #if CFG_TUD_VENDOR_TX_BUFSIZE > 0 | ||||
|     // try to send more if possible | ||||
|     if ( 0 == tu_edpt_stream_write_xfer(rhport, &p_itf->tx.stream) ) { | ||||
|       // If there is no data left, a ZLP should be sent if xferred_bytes is multiple of EP Packet size and not zero | ||||
|       tu_edpt_stream_write_zlp_if_needed(rhport, &p_itf->tx.stream, xferred_bytes); | ||||
|     } | ||||
|     #endif | ||||
|   } | ||||
|  | ||||
|   return true; | ||||
|   | ||||
							
								
								
									
										119
									
								
								src/class/vendor/vendor_device.h
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										119
									
								
								src/class/vendor/vendor_device.h
									
									
									
									
										vendored
									
									
								
							| @@ -33,15 +33,24 @@ | ||||
| #define CFG_TUD_VENDOR_EPSIZE     64 | ||||
| #endif | ||||
|  | ||||
| // RX FIFO can be disabled by setting this value to 0 | ||||
| #ifndef CFG_TUD_VENDOR_RX_BUFSIZE | ||||
| #define CFG_TUD_VENDOR_RX_BUFSIZE    64 | ||||
| #endif | ||||
|  | ||||
| // TX FIFO can be disabled by setting this value to 0 | ||||
| #ifndef CFG_TUD_VENDOR_TX_BUFSIZE | ||||
| #define CFG_TUD_VENDOR_TX_BUFSIZE    64 | ||||
| #endif | ||||
|  | ||||
| #ifdef __cplusplus | ||||
|  extern "C" { | ||||
| #endif | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Application API (Multiple Interfaces) | ||||
| // Application API (Multiple Interfaces) i.e CFG_TUD_VENDOR > 1 | ||||
| //--------------------------------------------------------------------+ | ||||
| bool     tud_vendor_n_mounted         (uint8_t itf); | ||||
|  | ||||
| uint32_t tud_vendor_n_available       (uint8_t itf); | ||||
| uint32_t tud_vendor_n_read            (uint8_t itf, void* buffer, uint32_t bufsize); | ||||
| bool     tud_vendor_n_peek            (uint8_t itf, uint8_t* ui8); | ||||
| @@ -51,23 +60,56 @@ uint32_t tud_vendor_n_write           (uint8_t itf, void const* buffer, uint32_t | ||||
| uint32_t tud_vendor_n_write_flush     (uint8_t itf); | ||||
| uint32_t tud_vendor_n_write_available (uint8_t itf); | ||||
|  | ||||
| static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str); | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str); | ||||
|  | ||||
| // backward compatible | ||||
| #define tud_vendor_n_flush(itf) tud_vendor_n_write_flush(itf) | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Application API (Single Port) | ||||
| // Application API (Single Port) i.e CFG_TUD_VENDOR = 1 | ||||
| //--------------------------------------------------------------------+ | ||||
| static inline bool     tud_vendor_mounted         (void); | ||||
| static inline uint32_t tud_vendor_available       (void); | ||||
| static inline uint32_t tud_vendor_read            (void* buffer, uint32_t bufsize); | ||||
| static inline bool     tud_vendor_peek            (uint8_t* ui8); | ||||
| static inline void     tud_vendor_read_flush      (void); | ||||
| static inline uint32_t tud_vendor_write           (void const* buffer, uint32_t bufsize); | ||||
| static inline uint32_t tud_vendor_write_str       (char const* str); | ||||
| static inline uint32_t tud_vendor_write_available (void); | ||||
| static inline uint32_t tud_vendor_write_flush     (void); | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_n_write_str(uint8_t itf, char const* str) { | ||||
|  return tud_vendor_n_write(itf, str, strlen(str)); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_vendor_mounted(void) { | ||||
|  return tud_vendor_n_mounted(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_available(void) { | ||||
|  return tud_vendor_n_available(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_read(void* buffer, uint32_t bufsize) { | ||||
|  return tud_vendor_n_read(0, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline bool tud_vendor_peek(uint8_t* ui8) { | ||||
|  return tud_vendor_n_peek(0, ui8); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline void tud_vendor_read_flush(void) { | ||||
|  tud_vendor_n_read_flush(0); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write(void const* buffer, uint32_t bufsize) { | ||||
|  return tud_vendor_n_write(0, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write_str(char const* str) { | ||||
|  return tud_vendor_n_write_str(0, str); | ||||
| } | ||||
|  | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write_flush(void) { | ||||
|  return tud_vendor_n_write_flush(0); | ||||
| } | ||||
|  | ||||
| #if CFG_TUD_VENDOR_TX_BUFSIZE > 0 | ||||
| TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_vendor_write_available(void) { | ||||
|  return tud_vendor_n_write_available(0); | ||||
| } | ||||
| #endif | ||||
|  | ||||
| // backward compatible | ||||
| #define tud_vendor_flush() tud_vendor_write_flush() | ||||
| @@ -77,7 +119,7 @@ static inline uint32_t tud_vendor_write_flush     (void); | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| // Invoked when received new data | ||||
| TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf); | ||||
| TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf, uint8_t const* buffer, uint16_t bufsize); | ||||
| // Invoked when last rx transfer finished | ||||
| TU_ATTR_WEAK void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes); | ||||
|  | ||||
| @@ -85,55 +127,6 @@ TU_ATTR_WEAK void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes); | ||||
| // Inline Functions | ||||
| //--------------------------------------------------------------------+ | ||||
|  | ||||
| static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str) | ||||
| { | ||||
|   return tud_vendor_n_write(itf, str, strlen(str)); | ||||
| } | ||||
|  | ||||
| static inline bool tud_vendor_mounted (void) | ||||
| { | ||||
|   return tud_vendor_n_mounted(0); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_vendor_available (void) | ||||
| { | ||||
|   return tud_vendor_n_available(0); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize) | ||||
| { | ||||
|   return tud_vendor_n_read(0, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| static inline bool tud_vendor_peek (uint8_t* ui8) | ||||
| { | ||||
|   return tud_vendor_n_peek(0, ui8); | ||||
| } | ||||
|  | ||||
| static inline void tud_vendor_read_flush(void) | ||||
| { | ||||
|     tud_vendor_n_read_flush(0); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize) | ||||
| { | ||||
|   return tud_vendor_n_write(0, buffer, bufsize); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_vendor_write_flush (void) | ||||
| { | ||||
|   return tud_vendor_n_write_flush(0); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_vendor_write_str (char const* str) | ||||
| { | ||||
|   return tud_vendor_n_write_str(0, str); | ||||
| } | ||||
|  | ||||
| static inline uint32_t tud_vendor_write_available (void) | ||||
| { | ||||
|   return tud_vendor_n_write_available(0); | ||||
| } | ||||
|  | ||||
| //--------------------------------------------------------------------+ | ||||
| // Internal Class Driver API | ||||
|   | ||||
| @@ -398,7 +398,7 @@ static inline void const *_find_desc_format(void const *beg, void const *end, ui | ||||
|     if ((fmt == VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED || | ||||
|          fmt == VIDEO_CS_ITF_VS_FORMAT_MJPEG || | ||||
|          fmt == VIDEO_CS_ITF_VS_FORMAT_DV || | ||||
|          fmt == VIDEO_CS_ITF_VS_FRAME_FRAME_BASED) && | ||||
|          fmt == VIDEO_CS_ITF_VS_FORMAT_FRAME_BASED) && | ||||
|         fmtnum == p[3]) { | ||||
|       return cur; | ||||
|     } | ||||
| @@ -707,7 +707,7 @@ static bool _open_vc_itf(uint8_t rhport, videod_interface_t *self, uint_fast8_t | ||||
|  | ||||
|   /* The first descriptor is a video control interface descriptor. */ | ||||
|   uint8_t const *cur = _find_desc_itf(beg, end, _desc_itfnum(beg), altnum); | ||||
|   TU_LOG_DRV("    cur %d\r\n", cur - beg); | ||||
|   TU_LOG_DRV("    cur %" PRId32 "\r\n", (int32_t) (cur - beg)); | ||||
|   TU_VERIFY(cur < end); | ||||
|  | ||||
|   tusb_desc_vc_itf_t const *vc = (tusb_desc_vc_itf_t const *)cur; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 HiFiPhile
					HiFiPhile