/** 
  * @brief This function transmits a ram buffer content to the USB.
  * This function is mode efficient in term of USB bandwith transfer.
  * 
  * @param U8 *buffer : the pointer to the RAM buffer to be sent 
  * @param data_to_send : the number of data to be sent
  */
void uart_usb_send_buffer(U8 *buffer, U8 nb_data)
{
  U8 zlp;

  if(!Is_device_enumerated())
    return;
   
  // Compute if zlp required
  if(nb_data%TX_EP_SIZE) 
    zlp=FALSE;
  else 
    zlp=TRUE;
   
  Usb_select_endpoint(TX_EP);
  while (nb_data)
  {
    while(Is_usb_write_enabled()==FALSE); // Wait Endpoint ready
    while(Is_usb_write_enabled() && nb_data)
    {
      Usb_write_byte(*buffer);
      buffer++;
      nb_data--;
    }
    Usb_ack_in_ready();
  }
  if(zlp)
  {
    while(Is_usb_write_enabled()==FALSE); // Wait Endpoint ready
    Usb_ack_in_ready();
  }
}
void uart_usb_flush (void)
{
  bool zlp=FALSE;

  if(!Is_usb_write_enabled(TX_EP))              // Endpoint full, need ZLP
     zlp=TRUE;

  Usb_ack_in_ready_send(TX_EP);
  b_tx_new = TRUE;
  if( zlp==TRUE )
  {
     while( !Is_usb_write_enabled(TX_EP) );     // Wait Endpoint ready...
     Usb_ack_in_ready_send(TX_EP);              // ...and Send ZLP
  }
}
Example #3
0
/**
  * @brief This function fills the USB transmit buffer with the new data. This buffer
  * is sent if complete. To flush this buffer before waiting full, launch
  * the uart_usb_flush() function.
  *
  * @param data_to_send Data to send
  *
  * @return data_to_send Data that was sent
  */
int uart_usb_putchar(int data_to_send)
{
	// Preserve the currently selected endpoint
	uint8_t uenum = UENUM;

	USB_CDC_ACM_HOOK_TX_START(data_to_send);

	Usb_select_endpoint(VCP_TX_EP);

	if(usb_endpoint_wait_for_write_enabled()!=0) {
		data_to_send=-1;
		goto bail;
	}

	Usb_write_byte(data_to_send);
	tx_counter++;

	//If Endpoint full -> flush
	if(!Is_usb_write_enabled())
		uart_usb_flush();

	USB_CDC_ACM_HOOK_TX_END(data_to_send);

bail:
	// Restore previously selected endpoint
	UENUM = uenum;

	return data_to_send;
}
//! @brief Send data report to Host
//!
void hid_report_in(void)
{

  int joy   =0;
  char* ptr_cram=txbuf;
  int data_to_transfer=0;

  if(!Is_usb_write_enabled(EP_HID_GENERIC_IN))
      return;                                // Not ready to send report

  // Build the Joystick report
  if(is_joystick_up()|| is_joystick_down() || is_joystick_right() || is_joystick_left() )                     //! Check for UP event
  {
    joy=0x01;
  }
  if(joy==last_joy) {
     return;                                // Same report then no send report
  }

  last_joy=joy;
  memset(txbuf, 0, RXBUF_SIZE);
  txbuf[0] = joy;

  // Send report
  data_to_transfer = sizeof(txbuf);
  while (data_to_transfer)
  {
       while (!Is_usb_in_ready(EP_HID_GENERIC_IN));

       Usb_reset_endpoint_fifo_access(EP_HID_GENERIC_IN);
       data_to_transfer = usb_write_ep_txpacket(EP_HID_GENERIC_IN, ptr_cram, data_to_transfer, (const void**)&ptr_cram);
       Usb_ack_in_ready_send(EP_HID_GENERIC_IN);
   }
}
Example #5
0
//! Task which links mouse events with the USB HID mouse device
//!
void mouse_task(void)
{
   if(Is_usb_vbus_low())
   {
      Setup_power_down_mode();
      Sleep_instruction();
   }

   if(!Is_device_enumerated())
      return;  // Device not ready

#if (USB_LOW_SPEED_DEVICE==DISABLE)
   // The SOF is used to schedule the task at the same frequency that Endpoint Interrupt frequency
   // This check allow to win a CPU time
   if(g_u8_cpt_sof<NB_IDLE_POLLING_SOF)
      return;  // Wait a delay
   g_u8_cpt_sof=0;
#endif

   if(!g_b_send_report)
   {
      // No report sending on going, then check mouse event to eventualy fill a new report
      if(is_mouse_event())
      {
         // Enable sending of report
         g_b_send_report      = TRUE;
      }
   }

   if((!g_b_send_report)&&(!g_b_send_ack_report))
      return;  // No report and ack to send

   //** A report or ack must be send
   Usb_select_endpoint(EP_MOUSE_IN);
   if(!Is_usb_write_enabled())
      return;  // Endpoint no free

   Led0_on();
   if( g_b_send_report )
   {
      g_b_send_report      = FALSE;
      // Send an ack after a "clic" report only
      g_b_send_ack_report = (0!=g_hid_mouse_report[0]);
   }
   else
   {
      Hid_mouse_report_reset();     // Reset report to have a ack report
      g_b_send_ack_report  = FALSE;
   }
   // Send report
   Usb_write_byte(g_hid_mouse_report[0]);
   Usb_write_byte(g_hid_mouse_report[1]);
   Usb_write_byte(g_hid_mouse_report[2]);
   Usb_write_byte(g_hid_mouse_report[3]);
   Usb_ack_in_ready();
   Led0_off();
}
//! @brief Send data report to Host
//!
void hid_report_in(void)
{
   Usb_select_endpoint(EP_HID_IN);
   if(!Is_usb_write_enabled())
      return;                                // Not ready to send report

   setINreport();

}
Example #7
0
void cdc_transmit(const char *data, int len) {
  if (usb_connected) {
    if (Is_usb_write_enabled(TX_EP)) {
      Usb_reset_endpoint_fifo_access(TX_EP);
      cdc_txbuffer_len = usb_write_ep_txpacket(TX_EP, data, len, (const void **)&cdc_txbuffer);
      Usb_ack_in_ready_send(TX_EP);
    } // fi TX_EP empty
  }
}
int uart_usb_putchar(int data_to_send)
{
  while( !uart_usb_tx_ready() ); // Wait Endpoint ready
  Usb_write_endpoint_data(TX_EP, 8, data_to_send);
  if( !Is_usb_write_enabled(TX_EP) ) // If Endpoint full -> flush
  {
     uart_usb_flush();
  }
  return data_to_send;
}
/** 
  * @brief This function checks if the USB emission buffer is ready to accept at
  * at least 1 byte
  * 
  * @return Boolean. TRUE if the firmware can write a new byte to transmit.
  */
bit uart_usb_tx_ready(void)
{
  if(!Is_device_enumerated())
     return FALSE;

  if (!Is_usb_write_enabled())
  {
    return FALSE;
  }
  return TRUE;
}
// this function should be continuously called
void my_uart_usb_send_to_endpoint(void){
  static uint8_t zlp=FALSE; /* zero load packet */
  static uint8_t local_buffer[TX_EP_SIZE];
  uint16_t num_read;
  bit ret;
  if(!Is_device_enumerated())
    return;


  // make atomic, this will be fixed by interrupts
  Usb_select_endpoint(TX_EP);
  if(Is_usb_write_enabled()==FALSE) // Only if endpoint ready
    return;

  if(line_status.DTR == 0){
    Usb_ack_in_ready();
    return;
  }


  // we send in chunks of TX_EP_SIZE 
  num_read = TX_EP_SIZE;
  ret = circ_buffer_get_nbytes(&my_uart_usb_tx_buffer, local_buffer, &num_read);
  if(num_read == 0 && zlp == TRUE){
    Usb_ack_in_ready();
    zlp = FALSE;
    Usb_disable_in_ready_interrupt(); // Nothing left to send, disable this until next time
    return;
  }
  // if we got exactly TX_EP_SIZE bytes, we should send zlp the next time if we send 0
  if(ret == PASS){
    zlp = TRUE;
    //Usb_enable_in_ready_interrupt(); // We don't need to do this since this is inside an interrupt.
  }
  else{ // this is the second pass and we didn't get a full TX_EP_SIZE bytes so the buffer is not full
    zlp = FALSE;
  }

  // write to the endpoint
  uint16_t nb_data = 0;
  Usb_select_endpoint(TX_EP);
  while(nb_data < num_read)
  {
    Usb_write_byte(local_buffer[nb_data]);
    nb_data++;
  }
  Usb_ack_in_ready();

  cdc_update_serial_state(); // I really do not know what this does, it could have been an old artifact from usart usb

//  if(usb_request_break_generation == TRUE)
//   usb_request_break_generation = FALSE;
}
Example #11
0
uint8_t
ecm_send(uint8_t * senddata, uint16_t sendlen, uint8_t led) {
	U8 byte_in_packet = 0;

	//Send Data
	Usb_select_endpoint(TX_EP);

	if(usb_endpoint_wait_for_write_enabled()!=0) {
		USB_ETH_HOOK_TX_ERROR("Timeout: write enabled");
		return 0;
	}

#ifdef USB_ETH_HOOK_TX_START
	USB_ETH_HOOK_TX_START();
#endif

	//Send packet
	while(sendlen) {
		Usb_write_byte(*senddata);
		senddata++;
		sendlen--;
		byte_in_packet++;
		
		//If endpoint is full, send data in
		//And then wait for data to transfer
		if (!Is_usb_write_enabled()) {
			Usb_ack_in_ready();

			if(usb_endpoint_wait_for_write_enabled()!=0) {
				USB_ETH_HOOK_TX_ERROR("Timeout: write enabled");
				return 0;
			}
			byte_in_packet=0;
		}

	}

	//Send last data in - also handles sending a ZLP if needed
	Usb_ack_in_ready();

#ifdef USB_ETH_HOOK_TX_END
	USB_ETH_HOOK_TX_END();
#endif

    //Wait for ready
	if(usb_endpoint_wait_for_IN_ready()!=0) {
		USB_ETH_HOOK_TX_ERROR("Timeout: IN ready");
		return 0;
	}

	return 1;
}
// Functions that manage characters output through USB
//
Bool uart_usb_tx_ready(void)
{
  if( Is_usb_write_enabled(TX_EP) )
  {
      if( b_tx_new )
      {
         Usb_reset_endpoint_fifo_access(TX_EP);
         b_tx_new = FALSE;
      }
      return TRUE;
  }
  return FALSE;
}
Example #13
0
// Functions that manage characters output through USB
//
bool uart_usb_tx_ready(void)
{
  if( Is_usb_write_enabled(TX_EP) )
  {
      if( b_tx_new )
      {
         Usb_reset_endpoint_fifo_access(TX_EP);
         b_tx_new = false;
      }
      return true;
  }
  return false;
}
Example #14
0
//! usb_send_packet.
//!
//! This function moves the data pointed by tbuf to the selected endpoint fifo
//! and sends it through the USB.
//!
//!
//! @param ep_num       number of the addressed endpoint
//! @param *tbuf        address of the first data to send
//! @param data_length  number of bytes to send
//!
//! @return address of the next U8 to send.
//!
//! Example:
//! usb_send_packet(3,&first_data,0x20);    // send packet on the endpoint #3
//! while(!(Usb_tx_complete));              // wait packet ACK'ed by the Host
//! Usb_clear_tx_complete();                     // acknowledge the transmit
//!
//! Note:
//! tbuf is incremented of 'data_length'.
//!
U8 usb_send_packet(U8 ep_num, U8* tbuf, U8 data_length)
{
U8 remaining_length;

   remaining_length = data_length;
   Usb_select_endpoint(ep_num);
   while(Is_usb_write_enabled() && (0 != remaining_length))
   {
      Usb_write_byte(*tbuf);
      remaining_length--;
      tbuf++;
   }
   return remaining_length;
}
Example #15
0
//! usb_send_packet.
//!
//! This function moves the data pointed by tbuf to the selected endpoint fifo
//! and sends it through the USB.
//!
//! @warning Code:xx bytes (function code length)
//!
//! @param ep_num       number of the addressed endpoint
//! @param *tbuf        address of the first data to send
//! @param data_length  number of bytes to send
//!
//! @return address of the next uint8_t to send.
//!
//! Example:
//! usb_send_packet(3,&first_data,0x20);    // send packet on the endpoint #3
//! while(!(Usb_tx_complete));              // wait packet ACK'ed by the Host
//! Usb_clear_tx_complete();                     // acknowledge the transmit
//!
//! Note:
//! tbuf is incremented of 'data_length'.
//!
uint8_t usb_send_packet(uint8_t ep_num, uint8_t* tbuf, uint8_t data_length)
{
uint8_t remaining_length;

   remaining_length = data_length;
   Usb_select_endpoint(ep_num);
   while(Is_usb_write_enabled() && (0 != remaining_length))
   {
      Usb_write_byte(*tbuf);
      remaining_length--;
      tbuf++;
   }
   return remaining_length;
}
Example #16
0
void adc_sample_sent_cb(void)
{
  uint32_t i;
  uint8_t  old_mic_buf_id=mic_buf_id;

  if (!b_microphone_pause && Is_usb_write_enabled(EP_AUDIO_IN) && audio_mixer_dacs_input(NULL, 0) )  // Endpoint buffer free ?
  {
    mic_frame_id++;
    if( mic_frame_id>=10 )
    {
      mic_frame_id=0;
      microphone[mic_buf_id].size = 45;
    }
    else
      microphone[mic_buf_id].size = 44;

    Usb_reset_endpoint_fifo_access(EP_AUDIO_IN);
    if(!mute)
    {
      for(i = 0; i < microphone[old_mic_buf_id].size; i++)
      {
          Usb_write_endpoint_data(EP_AUDIO_IN, 8, LSB(microphone[old_mic_buf_id].buffer[2 * i]));
          Usb_write_endpoint_data(EP_AUDIO_IN, 8, MSB(microphone[old_mic_buf_id].buffer[2 * i]));
      }
    }
    else
    {
      for(i = 0; i < 2*microphone[old_mic_buf_id].size; i++)
      {
          Usb_write_endpoint_data(EP_AUDIO_IN, 8, 0);
      }
    }
    Usb_ack_in_ready_send(EP_AUDIO_IN);

    audio_mixer_dacs_input(microphone[mic_buf_id].buffer, 45/*microphone[mic_buf_id].size*/);
    // Let's switch to the next buffer.
    mic_buf_id^=1;
  }
  else
    b_microphone_started=false;
}
Example #17
0
void device_audio_task(void)
#endif
{
#if( BOARD!=EVK1105) || (DEFAULT_DACS!=AUDIO_MIXER_DAC_AIC23B)
  uint32_t  i;
#endif
  uint32_t  fifo_cnt;
  int  stream_status;

#ifdef FREERTOS_USED
  portTickType xLastWakeTime;
  xLastWakeTime = xTaskGetTickCount();
  LED_On(LED0);

  while (true)
  {
    vTaskDelayUntil(&xLastWakeTime, configTSK_USB_DAUDIO_PERIOD);

    // First, check the device enumeration state
    if (!Is_device_enumerated()) { mmi_state=0; continue; }
#else
    // First, check the device enumeration state
    if (!Is_device_enumerated()) { mmi_state=0; return; }
#endif  // FREERTOS_USED

    mmi_display();

#if( BOARD!=EVK1105) || (DEFAULT_DACS!=AUDIO_MIXER_DAC_AIC23B)
    // Microphone emulation
    //
    if ( is_joystick_pressed() )
    {
       if (Is_usb_write_enabled(EP_AUDIO_IN))   // Endpoint buffer free ?
       {
          Usb_reset_endpoint_fifo_access(EP_AUDIO_IN);
          for( i=0 ; i<EP_SIZE_IN ; i++ )   // Fill endpoint with sample raw
          {
             if(mute==false)
             {
                uint8_t sample;
                sample = sample_sound[dat_sample_index++];
                LED_Set_Intensity(LED0, sample);
                Usb_write_endpoint_data(EP_AUDIO_IN, 8, sample);
                if (dat_sample_index >= SAMPLE_SOUND_LEN)
                {  dat_sample_index=0; }
             }
             else
             {
                LED_Set_Intensity(LED0, 0);
                Usb_write_endpoint_data(EP_AUDIO_IN, 8, 0x00);
             }
          }
          Usb_ack_in_ready_send(EP_AUDIO_IN);
       }
    }
#else
    // Handle input stream from microphone
    if( !b_microphone_started && (Is_usb_write_enabled(EP_AUDIO_IN)) && audio_mixer_dacs_input(NULL, 0) )
    {
      // Start ADC conversion. This will launch the IRL in background...
      mic_buf_id = 0;
      mic_frame_id=2;
      microphone[mic_buf_id].size=45;
      audio_mixer_dacs_input(microphone[0].buffer, 45 /*microphone[mic_buf_id].size*/);
      audio_mixer_dacs_input(microphone[1].buffer, 45 /*microphone[mic_buf_id].size*/);
      b_microphone_started=true;
    }
#endif



    // Handle incoming audio samples
    //
    if((Is_usb_out_received(EP_AUDIO_OUT) )
    /*&& (Is_usb_stall(EP_AUDIO_OUT)==false)*/)
    {
       stream_status = usb_stream_input(USB_STREAM_DEVICE, EP_AUDIO_OUT, &fifo_cnt);
       Usb_ack_out_received_free(EP_AUDIO_OUT);
       mmi_activity_display(false, fifo_cnt);
    }
#if USB_RESYNC_AUDIO_STREAM && (USB_RESYNC_AUDIO_STREAM==true)
    usb_stream_resync();
#endif

#ifdef FREERTOS_USED
  }
#endif
}
Example #18
0
/**
  * @brief This function checks if the USB emission buffer is ready to accept at
  * at least 1 byte
  *
  * @retval TRUE if the firmware can write a new byte to transmit.
  * @retval FALSE otherwise
  */
bit uart_usb_tx_ready(void)
{
  Usb_select_endpoint(VCP_TX_EP);
  return !!Is_usb_write_enabled();
}