示例#1
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;
}
示例#2
0
文件: cdc_task.c 项目: engie/bikeadc
void cdc_task(void)
{
    if(Is_device_enumerated()) //Enumeration processs OK ?
    {
        if(cpt_sof>=NB_MS_BEFORE_FLUSH && tx_counter!=0 )  //Flush buffer in Timeout
        {
            cpt_sof=0;
            uart_usb_flush();
        }

        if ( waiting_to_start && uart_usb_test_hit())   // Something received from the USB ?
        {
            waiting_to_start = 0;
            timer_init();
            adc_init();
        }

        if( !waiting_to_start )
        {
            READING r;
            if( cb_pop( &r ) )
            {
                uart_usb_putchar( (r.voltage >> 8) & 0xFF );
                uart_usb_putchar( r.voltage & 0xFF );
                uart_usb_putchar( (r.current >> 8) & 0xFF  );
                uart_usb_putchar( r.current & 0xFF );

                uint16 checksum = r.voltage ^ r.current;
                uart_usb_putchar( (checksum >> 8) & 0xFF  );
                uart_usb_putchar( checksum & 0xFF );
            }
        }
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 Communication Data Class (CDC) Process
 *
 *   This is the link between USB and the "good stuff". In this routine data
 *   is received and processed by CDC-ACM Class
 */
PROCESS_THREAD(cdc_process, ev, data_proc)
{

	PROCESS_BEGIN();
	uart_usb_init();

	while(1) {
      

	    // turn off LED's if necessary
		if (led3_timer) led3_timer--;
		else			Led3_off();

 		if(Is_device_enumerated() && (usb_mode == rndis_debug) && rndis_state && (!usb_busy)) {

			if (justenumerated) {

				//If we have serial port, set it as output
			    if (usb_mode == rndis_debug) {
					uart_usb_set_stdout();
					menu_print();
				}
				justenumerated = 0;
			}

			//Flush buffer if timeout
	        if(timer >= 4 && tx_counter!=0 ){
	            timer = 0;
	            uart_usb_flush();
	        } else {
				timer++;
			}

			while (uart_usb_test_hit()){
  		  	   menu_process(uart_usb_getchar());   // See what they want
            }


		}//if (Is_device_enumerated())


		if (usb_mode == rndis_debug) {
			etimer_set(&et, CLOCK_SECOND/80);
		} else {
			etimer_set(&et, CLOCK_SECOND);
		}

		PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));	
		
	} // while(1)

	PROCESS_END();
}
示例#5
0
int usb_stdout_putchar(char c, FILE *stream)
{
    // No need to preserve the currently
    // selected endpoint here because this
    // function doesn't directly touch any
    // USB registers.
	if(c=='\n')
		uart_usb_putchar('\r');

	if(c!='\r')
		uart_usb_putchar(c);

	// Flush on newline.
	if(c=='\n')
		uart_usb_flush();
		
	return 0;
}
示例#6
0
/**
 * \brief Communication Data Class (CDC) Process
 *
 *   This is the link between USB and the "good stuff". In this routine data
 *   is received and processed by CDC-ACM Class
 */
PROCESS_THREAD(cdc_process, ev, data_proc)
{
	PROCESS_BEGIN();

#if USB_CONF_RS232
	static FILE *rs232_stdout,*usb_stdout;
	rs232_stdout=stdout;
#endif

	while(1) {
	    // turn off LED's if necessary
		if (led3_timer) led3_timer--;
		else			Led3_off();
		
 		if(Is_device_enumerated()) {
			// If the configuration is different than the last time we checked...
			if((uart_usb_get_control_line_state()&1)!=previous_uart_usb_control_line_state) {
				previous_uart_usb_control_line_state = uart_usb_get_control_line_state()&1;
				static FILE* previous_stdout;
				
				if(previous_uart_usb_control_line_state&1) {
					previous_stdout = stdout;
					uart_usb_init();
					uart_usb_set_stdout();
				//	menu_print(); do this later
				} else {
					stdout = previous_stdout;
				}
#if USB_CONF_RS232
				usb_stdout=stdout;
#endif
			}

			//Flush buffer if timeout
	        if(timer >= 4 && tx_counter!=0 ){
	            timer = 0;
	            uart_usb_flush();
	        } else {
				timer++;
			}

#if USB_CONF_RS232
			stdout=usb_stdout;
#endif
			while (uart_usb_test_hit()){
  		  	   menu_process(uart_usb_getchar());   // See what they want
            }
#if USB_CONF_RS232
            if (usbstick_mode.debugOn) {
			  stdout=rs232_stdout;
			} else {
			  stdout=NULL;
			}
#endif
		}//if (Is_device_enumerated())



		if (USB_CONFIG_HAS_DEBUG_PORT(usb_configuration_nb)) {
			etimer_set(&et, CLOCK_SECOND/80);
		} else {
			etimer_set(&et, CLOCK_SECOND);
		}

		PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));	
		
	} // while(1)

	PROCESS_END();
}