char uart_usb_getchar(void) { register char data_rx; while( !uart_usb_test_hit() ); data_rx=Usb_read_endpoint_data(RX_EP, 8); if( 0==Usb_byte_count(RX_EP) ) { Usb_ack_out_received_free(RX_EP); b_rx_new = TRUE; } return data_rx; }
void USB_CCID_send_INT_Message (void) { unsigned short Message_u16; int i, i1; /* todo while (Is_usb_endpoint_stall_requested(EP_CCID_IN)) { if (Is_usb_setup_received()) usb_process_request(); } while (Is_usb_endpoint_stall_requested(EP_CCID_OUT)) { if (Is_usb_setup_received()) usb_process_request(); } // MSC Compliance - Free BAD out receive during SCSI command while( Is_usb_out_received(EP_CCID_OUT) ) { Usb_ack_out_received_free(EP_CCID_OUT); } */ while (!Is_usb_in_ready (EP_CCID_INT)); Usb_reset_endpoint_fifo_access (EP_CCID_INT); Message_u16 = 0x5003; i = Usb_byte_count (EP_CCID_INT); Usb_write_endpoint_data (EP_CCID_INT, 16, Message_u16); i1 = Usb_byte_count (EP_CCID_INT); Usb_ack_in_ready_send (EP_CCID_INT); // MSC Compliance - Wait end of all transmitions on USB line for (i = 0; i < 100; i++) { if (0 != Usb_nb_busy_bank (EP_CCID_INT)) { if (Is_usb_setup_received ()) usb_process_request (); } else { break; } } }
static void USB_CCID_GetDataFromUSB (void) { int i; int USB_Datalen_s32; // Bool cbw_error; Usb_reset_endpoint_fifo_access (EP_CCID_OUT); // Get all data from USB USB_CCID_data_st.CCID_datalen = 0; while (Is_usb_out_received (EP_CCID_OUT)) { Usb_reset_endpoint_fifo_access (EP_CCID_OUT); USB_Datalen_s32 = Usb_byte_count (EP_CCID_OUT); USB_Log_st.CCID_WriteCalls_u32++; USB_Log_st.CCID_BytesWrite_u32 += USB_Datalen_s32; // CI_TickLocalPrintf ("Get CCID USB block %3d byte - %3d\n",USB_Datalen_s32,USB_CCID_data_st.CCID_datalen); if (CCID_MAX_XFER_LENGTH <= USB_Datalen_s32 + USB_CCID_data_st.CCID_datalen) // Check for oversize { CI_LocalPrintf ("*** CCID buffer to small %d ***\n", CCID_MAX_XFER_LENGTH); Usb_ack_out_received_free (EP_CCID_OUT); return; } for (i = 0; i < USB_Datalen_s32; i++) { USB_CCID_data_st.USB_data[USB_CCID_data_st.CCID_datalen] = Usb_read_endpoint_data (EP_CCID_OUT, 8); USB_CCID_data_st.CCID_datalen++; } Usb_ack_out_received_free (EP_CCID_OUT); DelayMs (1); } LED_RedOn (); // USB_CCID_Datalen_s32 = USB_CCID_data_st.CCID_datalen; USB_to_CRD_DispatchUSBMessage_v (&USB_CCID_data_st); // memset (USB_CCID_data_st.USB_data,0,USB_Datalen_s32); LED_RedOff (); // Usb_ack_out_received_free(EP_CCID_OUT); }
Bool uart_usb_test_hit(void) { if( Is_usb_out_received(RX_EP) ) { if( 0!=Usb_byte_count(RX_EP) ) { if( b_rx_new ) { Usb_reset_endpoint_fifo_access(RX_EP); b_rx_new = FALSE; } return TRUE; } Usb_ack_out_received_free(RX_EP); b_rx_new = TRUE; } return FALSE; }
bool uart_usb_test_hit(void) { if( Is_usb_out_received(RX_EP) ) { if( 0!=Usb_byte_count(RX_EP) ) { if( b_rx_new ) { Usb_reset_endpoint_fifo_access(RX_EP); b_rx_new = false; } return true; } Usb_ack_out_received_free(RX_EP); b_rx_new = true; } return false; }
void cdc_receiving(void) { // return: Anzahl empfangener Bytes im Fifo int bytes_rx, bytes_left; #if USB_DEVICE_FEATURE == ENABLED && USB_HOST_FEATURE == ENABLED usb_task(); #elif USB_DEVICE_FEATURE == ENABLED usb_device_task(); #elif USB_HOST_FEATURE == ENABLED usb_host_task(); #endif if ( usb_connected && Is_usb_out_received(RX_EP) ) { bytes_rx = Usb_byte_count(RX_EP); if ((bytes_rx > 0)&&(bytes_rx < cdc_rxleft)) { bytes_left = CDC_RXBUFFERSIZE-cdc_wrpos; Usb_reset_endpoint_fifo_access(RX_EP); if (bytes_rx <= bytes_left) { // Normalfall: kein Wrap usb_read_ep_rxpacket(RX_EP, (void *)cdc_rxbuffer+cdc_wrpos, bytes_rx, NULL); } else { usb_read_ep_rxpacket(RX_EP, (void *)cdc_rxbuffer+cdc_wrpos, bytes_left, NULL); usb_read_ep_rxpacket(RX_EP, (void *)cdc_rxbuffer, bytes_rx-bytes_left, NULL); } cdc_wrpos = (cdc_wrpos+bytes_rx)%CDC_RXBUFFERSIZE; // End copy in own fifo cdc_rxidle_pos = cdc_sof_counter + cdc_timeoutval; cdc_timeout_enabled = (cdc_timeoutval>0)&&(cdc_timeout_fct!=NULL); } // fi copy received Usb_ack_out_received_free(RX_EP); // moved outside of if-con } else if (cdc_timeout_enabled) { if ((int)(cdc_sof_counter - cdc_rxidle_pos) >= 0) { cdc_timeout_fct(cdc_rxlen); cdc_timeout_enabled = FALSE; } } // esle fi to // chain-tx (>64 byte): if (cdc_txbuffer_len > 0) { cdc_transmit(cdc_txbuffer, cdc_txbuffer_len); } // fi }
//! //! @brief This function takes the stream coming from the selected USB pipe and sends //! it to the DAC driver. Moreover, it ensures that both input and output stream //! keep synchronized by adding or deleting samples. //! //! @param side USB_STREAM_HOST for USB host, USB_STREAM_DEVICE for device. //! @param pipe_in Number of the addressed pipe/endpoint //! @param pFifoCount (return parameter) NULL or pointer to the number of used buffers at this time //! //! @return status: (USB_STREAM_STATUS_OK, USB_STREAM_STATUS_NOT_SYNCHRONIZED, //! USB_STREAM_STATUS_SPEED_UP, USB_STREAM_STATUS_SLOW_DOWN, USB_STREAM_STATUS_BUFFER_OVERFLOW) //! int usb_stream_input(usb_stream_side_t side, uint8_t pipe_in, uint32_t* pFifoCount) { uint16_t fifo_used_cnt; uint16_t byte_count=0; uint32_t i; UnionPtr pswap; UnionPtr buffer; // We comes here since we have received something. Let's increase the internal // activity counter. usb_stream_cnt++; fifo_used_cnt=usb_stream_fifo_get_used_room(); if (pFifoCount) *pFifoCount = fifo_used_cnt; // usb_stream_fifo_get_free_room() if( USB_STREAM_BUFFER_NUMBER-fifo_used_cnt==0 ) { // Fatal error: even with the synchro mechanism acting, we are in a case in which the // buffers are full. usb_stream_context->synchronized = false; usb_stream_context->status = USB_STREAM_ERROR_NOT_SYNCHRONIZED; return usb_stream_context->status; } pswap.s8ptr = buffer.s8ptr = usb_stream_fifo_get_buffer(usb_stream_context->wr_id); #if USB_HOST_FEATURE == true if( side==USB_STREAM_HOST ) { byte_count=Host_byte_count(pipe_in); } #endif #if USB_DEVICE_FEATURE == true if( side==USB_STREAM_DEVICE ) { byte_count=Usb_byte_count(pipe_in); } #endif if( byte_count==0 ) { if( cpu_is_timeout(&broken_stream_timer) ) { usb_stream_context->status = USB_STREAM_ERROR_BROKEN_STREAM; } else { usb_stream_context->status = USB_STREAM_ERROR_NO_DATA; } return usb_stream_context->status; } else { // reset time out detection cpu_set_timeout(cpu_ms_2_cy(BROKEN_STREAM_TIMER, FCPU_HZ), &broken_stream_timer); } #if USB_HOST_FEATURE == true if( side==USB_STREAM_HOST ) { Host_reset_pipe_fifo_access(pipe_in); host_read_p_rxpacket(pipe_in, (void*)buffer.s8ptr, byte_count, NULL); } #endif #if USB_DEVICE_FEATURE == true if( side==USB_STREAM_DEVICE ) { Usb_reset_endpoint_fifo_access(pipe_in); usb_read_ep_rxpacket(pipe_in, (void*)buffer.s8ptr, byte_count, NULL); } #endif usb_stream_context->status = USB_STREAM_ERROR_NONE; if( byte_count > USB_STREAM_REAL_BUFFER_SIZE ) { byte_count = USB_STREAM_REAL_BUFFER_SIZE; usb_stream_context->status = USB_STREAM_ERROR_OVERFLOW; } // Swap samples since they are coming from the USB world. if( usb_stream_context->bits_per_sample==16 ) for( i=0 ; i<byte_count/(16/8) ; i++ ) pswap.s16ptr[i] = swap16(pswap.s16ptr[i]); else if( usb_stream_context->bits_per_sample==32 ) for( i=0 ; i<byte_count/(32/8) ; i++ ) pswap.s32ptr[i] = swap32(pswap.s32ptr[i]); //for( i=0 ; i<byte_count/2 ; i++ ) // printf("0x%04hx ", pswap[i]); //printf("\r\n"); usb_stream_fifo_push(byte_count); fifo_used_cnt++; if( !usb_stream_context->synchronized ) { usb_stream_context->status = USB_STREAM_ERROR_NOT_SYNCHRONIZED; if( fifo_used_cnt>=(USB_STREAM_BUFFER_NUMBER/2) ) { // We have enough buffers to start the playback. void* buffer; uint16_t size; // CS2200 cs2200_freq_clk_out(_32_BITS_RATIO(usb_stream_resync_frequency, CS2200_FREF)); usb_stream_resync_step = PPM(usb_stream_resync_frequency, USB_STREAM_RESYNC_PPM_STEPS); usb_stream_resync_freq_ofst = usb_stream_resync_frequency; usb_stream_resync_ppm_ofst = 0; usb_stream_resync_last_room = fifo_used_cnt; #define TIMER_USB_RESYNC_CORRECTION 320 cpu_set_timeout( cpu_ms_2_cy(TIMER_USB_RESYNC_CORRECTION, FCPU_HZ), &usb_resync_timer ); usb_stream_context->synchronized=true; usb_stream_fifo_get(&buffer, &size); audio_mixer_dacs_output_direct(buffer, size/(usb_stream_context->channel_count*usb_stream_context->bits_per_sample/8)); // Fill also the reload stage of the PDCA. usb_stream_fifo_pull(); usb_stream_fifo_get(&buffer, &size); audio_mixer_dacs_output_direct(buffer, size/(usb_stream_context->channel_count*usb_stream_context->bits_per_sample/8)); } } return usb_stream_context->status; }
//! usb_read_ep_rxpacket //! //! This function reads the selected endpoint FIFO to the buffer pointed to by //! rxbuf, using as few accesses as possible. //! //! @param ep Number of the addressed endpoint //! @param rxbuf Address of buffer to write //! @param data_length Number of bytes to read //! @param prxbuf NULL or pointer to the buffer address to update //! //! @return Number of non-read bytes //! //! @note The selected endpoint FIFO may be read in several steps by calling //! usb_read_ep_rxpacket several times. //! //! @warning Invoke Usb_reset_endpoint_fifo_access before this function when at //! FIFO beginning whether or not the FIFO is to be read in several steps. //! //! @warning Do not mix calls to this function with calls to indexed macros. //! U32 usb_read_ep_rxpacket(U8 ep, void *rxbuf, U32 data_length, void **prxbuf) { // Use aggregated pointers to have several alignments available for a same address UnionCVPtr ep_fifo; UnionPtr rxbuf_cur; #if (!defined __OPTIMIZE_SIZE__) || !__OPTIMIZE_SIZE__ // Auto-generated when GCC's -Os command option is used StructCPtr rxbuf_end; #else StructCPtr rxbuf_end; #endif // !__OPTIMIZE_SIZE__ // Initialize pointers for copy loops and limit the number of bytes to copy ep_fifo.u8ptr = pep_fifo[ep].u8ptr; rxbuf_cur.u8ptr = rxbuf; rxbuf_end.u8ptr = rxbuf_cur.u8ptr + min(data_length, Usb_byte_count(ep)); #if (!defined __OPTIMIZE_SIZE__) || !__OPTIMIZE_SIZE__ // Auto-generated when GCC's -Os command option is used rxbuf_end.u16ptr = (U16 *)Align_down((U32)rxbuf_end.u8ptr, sizeof(U16)); rxbuf_end.u32ptr = (U32 *)Align_down((U32)rxbuf_end.u16ptr, sizeof(U32)); rxbuf_end.u64ptr = (U64 *)Align_down((U32)rxbuf_end.u32ptr, sizeof(U64)); // If all addresses are aligned the same way with respect to 16-bit boundaries if (Get_align((U32)rxbuf_cur.u8ptr, sizeof(U16)) == Get_align((U32)ep_fifo.u8ptr, sizeof(U16))) { // If pointer to reception buffer is not 16-bit aligned if (!Test_align((U32)rxbuf_cur.u8ptr, sizeof(U16))) { // Copy 8-bit data to reach 16-bit alignment if (rxbuf_cur.u8ptr < rxbuf_end.u8ptr) { // 8-bit accesses to FIFO data registers do require pointer post-increment *rxbuf_cur.u8ptr++ = *ep_fifo.u8ptr++; } } // If all addresses are aligned the same way with respect to 32-bit boundaries if (Get_align((U32)rxbuf_cur.u16ptr, sizeof(U32)) == Get_align((U32)ep_fifo.u16ptr, sizeof(U32))) { // If pointer to reception buffer is not 32-bit aligned if (!Test_align((U32)rxbuf_cur.u16ptr, sizeof(U32))) { // Copy 16-bit data to reach 32-bit alignment if (rxbuf_cur.u16ptr < rxbuf_end.u16ptr) { // 16-bit accesses to FIFO data registers do require pointer post-increment *rxbuf_cur.u16ptr++ = *ep_fifo.u16ptr++; } } // If pointer to reception buffer is not 64-bit aligned if (!Test_align((U32)rxbuf_cur.u32ptr, sizeof(U64))) { // Copy 32-bit data to reach 64-bit alignment if (rxbuf_cur.u32ptr < rxbuf_end.u32ptr) { // 32-bit accesses to FIFO data registers do not require pointer post-increment *rxbuf_cur.u32ptr++ = *ep_fifo.u32ptr; } } // Copy 64-bit-aligned data while (rxbuf_cur.u64ptr < rxbuf_end.u64ptr) { // 64-bit accesses to FIFO data registers do not require pointer post-increment *rxbuf_cur.u64ptr++ = *ep_fifo.u64ptr; } // Copy 32-bit-aligned data if (rxbuf_cur.u32ptr < rxbuf_end.u32ptr) { // 32-bit accesses to FIFO data registers do not require pointer post-increment *rxbuf_cur.u32ptr++ = *ep_fifo.u32ptr; } } // Copy remaining 16-bit data if some while (rxbuf_cur.u16ptr < rxbuf_end.u16ptr) { // 16-bit accesses to FIFO data registers do require pointer post-increment *rxbuf_cur.u16ptr++ = *ep_fifo.u16ptr++; } } #endif // !__OPTIMIZE_SIZE__ // Copy remaining 8-bit data if some while (rxbuf_cur.u8ptr < rxbuf_end.u8ptr) { // 8-bit accesses to FIFO data registers do require pointer post-increment *rxbuf_cur.u8ptr++ = *ep_fifo.u8ptr++; } // Save current position in FIFO data register pep_fifo[ep].u8ptr = (volatile U8 *)ep_fifo.u8ptr; // Return the updated buffer address and the number of non-copied bytes if (prxbuf) *prxbuf = rxbuf_cur.u8ptr; return data_length - (rxbuf_cur.u8ptr - (U8 *)rxbuf); }
//! usb_set_ep_txpacket //! //! This function fills the selected endpoint FIFO with a constant byte, using //! as few accesses as possible. //! //! @param ep Number of the addressed endpoint //! @param txbyte Byte to fill the endpoint with //! @param data_length Number of bytes to write //! //! @return Number of non-written bytes //! //! @note The selected endpoint FIFO may be filled in several steps by calling //! usb_set_ep_txpacket several times. //! //! @warning Invoke Usb_reset_endpoint_fifo_access before this function when at //! FIFO beginning whether or not the FIFO is to be filled in several steps. //! //! @warning Do not mix calls to this function with calls to indexed macros. //! U32 usb_set_ep_txpacket(U8 ep, U8 txbyte, U32 data_length) { // Use aggregated pointers to have several alignments available for a same address UnionVPtr ep_fifo_cur; #if (!defined __OPTIMIZE_SIZE__) || !__OPTIMIZE_SIZE__ // Auto-generated when GCC's -Os command option is used StructCVPtr ep_fifo_end; Union64 txval; #else UnionCVPtr ep_fifo_end; union { U8 u8[1]; } txval; #endif // !__OPTIMIZE_SIZE__ // Initialize pointers for write loops and limit the number of bytes to write ep_fifo_cur.u8ptr = pep_fifo[ep].u8ptr; ep_fifo_end.u8ptr = ep_fifo_cur.u8ptr + min(data_length, Usb_get_endpoint_size(ep) - Usb_byte_count(ep)); #if (!defined __OPTIMIZE_SIZE__) || !__OPTIMIZE_SIZE__ // Auto-generated when GCC's -Os command option is used ep_fifo_end.u16ptr = (U16 *)Align_down((U32)ep_fifo_end.u8ptr, sizeof(U16)); ep_fifo_end.u32ptr = (U32 *)Align_down((U32)ep_fifo_end.u16ptr, sizeof(U32)); ep_fifo_end.u64ptr = (U64 *)Align_down((U32)ep_fifo_end.u32ptr, sizeof(U64)); #endif // !__OPTIMIZE_SIZE__ txval.u8[0] = txbyte; #if (!defined __OPTIMIZE_SIZE__) || !__OPTIMIZE_SIZE__ // Auto-generated when GCC's -Os command option is used txval.u8[1] = txval.u8[0]; txval.u16[1] = txval.u16[0]; txval.u32[1] = txval.u32[0]; // If pointer to FIFO data register is not 16-bit aligned if (!Test_align((U32)ep_fifo_cur.u8ptr, sizeof(U16))) { // Write 8-bit data to reach 16-bit alignment if (ep_fifo_cur.u8ptr < ep_fifo_end.u8ptr) { *ep_fifo_cur.u8ptr++ = txval.u8[0]; } } // If pointer to FIFO data register is not 32-bit aligned if (!Test_align((U32)ep_fifo_cur.u16ptr, sizeof(U32))) { // Write 16-bit data to reach 32-bit alignment if (ep_fifo_cur.u16ptr < ep_fifo_end.u16ptr) { *ep_fifo_cur.u16ptr++ = txval.u16[0]; } } // If pointer to FIFO data register is not 64-bit aligned if (!Test_align((U32)ep_fifo_cur.u32ptr, sizeof(U64))) { // Write 32-bit data to reach 64-bit alignment if (ep_fifo_cur.u32ptr < ep_fifo_end.u32ptr) { *ep_fifo_cur.u32ptr++ = txval.u32[0]; } } // Write 64-bit-aligned data while (ep_fifo_cur.u64ptr < ep_fifo_end.u64ptr) { *ep_fifo_cur.u64ptr++ = txval.u64; } // Write remaining 32-bit data if some if (ep_fifo_cur.u32ptr < ep_fifo_end.u32ptr) { *ep_fifo_cur.u32ptr++ = txval.u32[0]; } // Write remaining 16-bit data if some if (ep_fifo_cur.u16ptr < ep_fifo_end.u16ptr) { *ep_fifo_cur.u16ptr++ = txval.u16[0]; } // Write remaining 8-bit data if some if (ep_fifo_cur.u8ptr < ep_fifo_end.u8ptr) { *ep_fifo_cur.u8ptr++ = txval.u8[0]; } #else // Write remaining 8-bit data if some while (ep_fifo_cur.u8ptr < ep_fifo_end.u8ptr) { *ep_fifo_cur.u8ptr++ = txval.u8[0]; } #endif // !__OPTIMIZE_SIZE__ // Compute the number of non-written bytes data_length -= ep_fifo_cur.u8ptr - pep_fifo[ep].u8ptr; // Save current position in FIFO data register pep_fifo[ep].u8ptr = ep_fifo_cur.u8ptr; // Return the number of non-written bytes return data_length; }
void device_template_task(void) #endif { static U8 buf[EP_SIZE_TEMP2]; #ifdef FREERTOS_USED portTickType xLastWakeTime; xLastWakeTime = xTaskGetTickCount(); while (true) { vTaskDelayUntil(&xLastWakeTime, configTSK_USB_DTP_PERIOD); // First, check the device enumeration state if (!Is_device_enumerated()) continue; #else // First, check the device enumeration state if (!Is_device_enumerated()) return; #endif // FREERTOS_USED // HERE STARTS THE USB DEVICE APPLICATIVE CODE // The example below just performs a loopback transmission/reception. // All data received with the OUT endpoint is stored in a RAM buffer and // sent back to the IN endpoint. #if BOARD == EVK1100 // For example, display Start-of-Frame counter on LEDs LED_Display_Field(LED_MONO0_GREEN | LED_MONO1_GREEN | LED_MONO2_GREEN | LED_MONO3_GREEN, sof_cnt >> 5); #elif BOARD == EVK1101 || BOARD == UC3C_EK || BOARD == EVK1104 || BOARD == EVK1105 // For example, display Start-of-Frame counter on LEDs LED_Display_Field(LED0 | LED1, sof_cnt >> 5); #else #error The display of the SOFs must be defined here. #endif // If we receive something in the OUT endpoint, just store it in the RAM buffer if (Is_usb_out_received(EP_TEMP_OUT)) { LED_On(LED_APPLI_1); Usb_reset_endpoint_fifo_access(EP_TEMP_OUT); data_length = Usb_byte_count(EP_TEMP_OUT); usb_read_ep_rxpacket(EP_TEMP_OUT, buf, data_length, NULL); Usb_ack_out_received_free(EP_TEMP_OUT); LED_Off(LED_APPLI_1); } // Load the IN endpoint with the contents of the RAM buffer if (data_length && Is_usb_in_ready(EP_TEMP_IN)) { LED_On(LED_APPLI_0); Usb_reset_endpoint_fifo_access(EP_TEMP_IN); usb_write_ep_txpacket(EP_TEMP_IN, buf, data_length, NULL); data_length = 0; Usb_ack_in_ready_send(EP_TEMP_IN); LED_Off(LED_APPLI_0); } #ifdef FREERTOS_USED } #endif }