uMIDI
The swiss army knife for quick and easy developement of MIDI applications.
Functions | Variables
usb.c File Reference

USB CDC device driver implementation. More...

#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include "lufa/LUFA/Drivers/USB/USB.h"
#include "leds.h"
#include "usb.h"
#include "usb_descriptors.h"
#include "system.h"
Include dependency graph for usb.c:

Functions

void EVENT_USB_Device_Connect (void)
 Event handler for the library USB Connection event. More...
 
void EVENT_USB_Device_Disconnect (void)
 Event handler for the library USB Disconnection event. More...
 
void EVENT_USB_Device_ConfigurationChanged (void)
 Event handler for the library USB Configuration Changed event. More...
 
void EVENT_USB_Device_ControlRequest (void)
 Event handler for the library USB Control Request reception event. More...
 
void EVENT_CDC_Device_ControLineStateChanged (USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo)
 Callback for control line state changes. More...
 
static void flush (void)
 Flushes the USB send buffer. More...
 
static void send_string (const char *const string)
 Writes the given string to the USB send buffer and flushes it. More...
 
void init_usb_module (void)
 Initializes the USB CDC module. More...
 
uint16_t usb_bytes_received (void)
 Returns the number of unread bytes currently residing in the USB receive buffer. More...
 
int16_t usb_getc (void)
 Reads one byte or character from the USB receive buffer. More...
 
void usb_main_task (void)
 Main background task for USB CDC operation. More...
 
void usb_printf (const char *const format,...)
 
void usb_printf_S (const char *const format,...)
 Does exactly what you would expect. ;-) More...
 
void usb_putc (char c)
 Sends the given character over USB. More...
 
void usb_puts (const char *const string)
 
void usb_puts_S (const char *const string)
 Sends the given string and a newline sequence over USB. More...
 

Variables

static USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface
 LUFA CDC Class driver interface configuration and state information. More...
 
static bool ok_to_send = 0
 This flag indicates if the USB port is ready to send data. More...
 

Detailed Description

USB CDC device driver implementation.

Function Documentation

◆ EVENT_CDC_Device_ControLineStateChanged()

void EVENT_CDC_Device_ControLineStateChanged ( USB_ClassInfo_CDC_Device_t *const  CDCInterfaceInfo)

Callback for control line state changes.

Sets / clears the ok_to_send flag.

◆ EVENT_USB_Device_ConfigurationChanged()

void EVENT_USB_Device_ConfigurationChanged ( void  )

Event handler for the library USB Configuration Changed event.

Configures the endpoints or enters panic mode if something goes wrong.

◆ EVENT_USB_Device_Connect()

void EVENT_USB_Device_Connect ( void  )

Event handler for the library USB Connection event.

Indicates device connection by flashing the red on-board LED.

◆ EVENT_USB_Device_ControlRequest()

void EVENT_USB_Device_ControlRequest ( void  )

Event handler for the library USB Control Request reception event.

◆ EVENT_USB_Device_Disconnect()

void EVENT_USB_Device_Disconnect ( void  )

Event handler for the library USB Disconnection event.

Indicates device disconnection by flashing the red on-board LED.

◆ flush()

static void flush ( void  )
static

Flushes the USB send buffer.

Panics if something goes wrong!

◆ init_usb_module()

void init_usb_module ( void  )

Initializes the USB CDC module.

Calls LUFA's USB_Init() function and enables interrupts.

◆ send_string()

static void send_string ( const char *const  string)
static

Writes the given string to the USB send buffer and flushes it.

Panics if something goes wrong!

◆ usb_bytes_received()

uint16_t usb_bytes_received ( void  )

Returns the number of unread bytes currently residing in the USB receive buffer.

Returns
the number of pending bytes

◆ usb_getc()

int16_t usb_getc ( void  )

Reads one byte or character from the USB receive buffer.

Returns
the character read cast to an int16_t or EOF

◆ usb_main_task()

void usb_main_task ( void  )

Main background task for USB CDC operation.

Executes relevant LUFA tasks. This task should be included in the mid frequency background task queue. Depending on your application and requirements, it might also be reasonable to move this to either the slow or the fast queue - yielding lower resp. higher data transfer rates.

◆ usb_printf()

void usb_printf ( const char *const  format,
  ... 
)

◆ usb_printf_S()

void usb_printf_S ( const char *  format,
  ... 
)

Does exactly what you would expect. ;-)

Unlike the "real" printf, this function does not return the number of written bytes, because it can not be guaranteed that the bytes really got sent over the bus. After writing the string to the send buffer, it is flushed. Also, strings formatted and printed with this function may not exceed a certain length, defined by USB_STRING_MAX_LENGTH. You should only rarely need this function. A possible use case is printing the contents of a modifiable string buffer without appending a newline sequence.

See also
man 3 printf
USB_STRING_MAX_LENGTH

◆ usb_putc()

void usb_putc ( char  c)

Sends the given character over USB.

Flushes the USB send buffer.

◆ usb_puts()

void usb_puts ( const char *const  string)

◆ usb_puts_S()

void usb_puts_S ( const char *  string)

Sends the given string and a newline sequence over USB.

Flushes the USB send buffer. You should only rarely need this function - namely when you have to modify or dynamically generate strings. Whenever you want to print a constant string you should place it in program space and use usb_puts instead.

See also
USB_NEWLINE
usb_puts

Variable Documentation

◆ ok_to_send

bool ok_to_send = 0
static

This flag indicates if the USB port is ready to send data.

The flag is set / cleared by a LUFA event callback.

See also
EVENT_CDC_Device_ControLineStateChanged

◆ VirtualSerial_CDC_Interface

USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface
static
Initial value:
= {
.Config = {
.ControlInterfaceNumber = 0,
.DataINEndpoint = {
.Address = (ENDPOINT_DIR_IN | 3) ,
.Size = 16 ,
.Banks = 1,
},
.DataOUTEndpoint = {
.Address = (ENDPOINT_DIR_OUT | 4) ,
.Size = 16 ,
.Banks = 1,
},
.NotificationEndpoint = {
.Address = (ENDPOINT_DIR_IN | 2) ,
.Size = 8 ,
.Banks = 1,
},
},
}

LUFA CDC Class driver interface configuration and state information.

This structure is passed to all CDC Class driver functions, so that multiple instances of the same class within a device can be differentiated from one another.