GlobLib
HAL and API libraries for MCUs and hardware.

This module contains low level functions for USB interaction. More...

Files

file  stm32f103cb_usb.h
 Header file for stm32f103cb USB.
 

Functions

void USB_setup (void)
 Initialize the USB device. More...
 
void USB_update (void)
 Update the USB peripheral. More...
 
void USB_put (uint8_t byte)
 Send a single byte on USB. More...
 
void USB_setGet (void(*data_available)(uint8_t))
 Set the function called when data is received on the USB. More...
 
void USB_disconnect (void)
 Dissconnect the USB device from the HOST. More...
 

Detailed Description

This module contains low level functions for USB interaction.

The USB stack is taken from a modified version of OPENCM3 lib.

The USB module has a maximum data rate of 64kb / second.

When using the USB, USB_update must be called at at least 4KHz. Higher frequencies can be used, however this just causes unneccesary CPU usage.

The clock frequency for the MCU must be either internal 48MHz or external 72MHz for the USB to function.

Author
Stuart Ianna
Version
0.1
Date
June 2018
Warning
Using the USB requires the USB device to be reset in the host OS every time the MCU is flashed. This is handled automatically in recipe "make flash". This requires ROOT access. OR the the the usb can be disconnected / reconnected.
Bug:
When using the USB, the reset button on the MCU doesn't reset the USB peripheral. This means the only way to reset is to cycle power on the device.
Todo:
Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Minimum working example
#include <string11.h>
//This gets called when new data is available
void data_available(uint8_t byte);
int main(void){
//Clock speed needs to be 48MHz or greater
//Setup the USB
//Target received data to function
USB_setGet(&data_available);
//USB MUST be refreshed via a timer at at least 4KHz (higher rate = more CPU)
//Set the string output to USB
STRING11_setOutput(&USB_put);
//Print message
printl("USB test. Echo characters to terminal.");
//Done
while(1);
return 0;
}
void data_available(uint8_t byte){
print((char)byte);
}

Function Documentation

void USB_disconnect ( void  )

Dissconnect the USB device from the HOST.

This is also called internally at the start of USB_setup(). This allows a software reset to register with the host and the USB to be reconfigured.

Definition at line 271 of file stm32f103cb_usb.c.

void USB_put ( uint8_t  byte)

Send a single byte on USB.

This function actually loads the byte into the internal software buffer. The byte is send internally in USB_update().

Parameters
byteThe byte to send (add to buffer).

Definition at line 340 of file stm32f103cb_usb.c.

void USB_setGet ( void(*)(uint8_t)  data_available)

Set the function called when data is received on the USB.

Parameters
data_availableFuntion pointer to the function to be called

Definition at line 309 of file stm32f103cb_usb.c.

void USB_setup ( void  )

Initialize the USB device.

This must be called before any other API feature is used.

The clock must be > 48Mhz This function has a internal delay for the USB to setup (approx 0.3s);

Definition at line 287 of file stm32f103cb_usb.c.

void USB_update ( void  )

Update the USB peripheral.

This must be call at at least 4kHz.

This function handles control requests from the host and sends any data waiting in the internal software buffer.

Definition at line 318 of file stm32f103cb_usb.c.