More usbserial utility functions

This commit is contained in:
Stefan `Sec` Zehl 2011-07-27 18:22:53 +02:00
parent ce6b63046f
commit 9e4f27ad12
4 changed files with 75 additions and 52 deletions

View File

@ -7,73 +7,31 @@
#include "funk/nrf24l01p.h"
#include "core/usbcdc/usb.h"
#include "core/usbcdc/usbcore.h"
#include "core/usbcdc/usbhw.h"
#include "core/usbcdc/cdcuser.h"
#include "core/usbcdc/cdc_buf.h"
#include "usbcdc/usb.h"
#include "usbcdc/usbcore.h"
#include "usbcdc/usbhw.h"
#include "usbcdc/cdcuser.h"
#include "usbcdc/cdc_buf.h"
#include <string.h>
#if CFG_USBMSC
#error "MSC is defined
#error "MSC is defined"
#endif
#if !CFG_USBCDC
#error "CDC is not defined
#error "CDC is not defined"
#endif
/**************************************************************************/
volatile unsigned int lastTick;
int puts(const char * str)
{
// There must be at least 1ms between USB frames (of up to 64 bytes)
// This buffers all data and writes it out from the buffer one frame
// and one millisecond at a time
if (USB_Configuration)
{
while(*str)
cdcBufferWrite(*str++);
// Check if we can flush the buffer now or if we need to wait
unsigned int currentTick = systickGetTicks();
if (currentTick != lastTick)
{
uint8_t frame[64];
uint32_t bytesRead = 0;
while (cdcBufferDataPending())
{
// Read up to 64 bytes as long as possible
bytesRead = cdcBufferReadLen(frame, 64);
USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
systickDelay(1);
}
lastTick = currentTick;
}
}
return 0;
}
void f_ser(void) {
//lastTick = systickGetTicks(); // Used to control output/printf timing
CDC_Init(); // Initialise VCOM
USB_Init(); // USB Initialization
lcdPrintln("preconnect");
USB_Connect(TRUE); // USB Connect
lcdPrintln("postconnect");
// Wait until USB is configured or timeout occurs
uint32_t usbTimeout = 0;
// while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 ) {
// if (USB_Configuration) break;
// delayms(10); // Wait 10ms
// usbTimeout++;
// }
lcdPrintln("fini");
usbCDCInit();
};
void f_disconnect(void) {
USB_Connect(FALSE);
usbCDCOff();
};
#define LEN 10
@ -113,4 +71,3 @@ void f_echo(){
void f_say(){
puts("hello world\r\n");
};

View File

@ -9,6 +9,7 @@ OBJS += usbcore.o
OBJS += usbdesc.o
OBJS += usbhw.o
OBJS += usbuser.o
OBJS += util.o
LIBNAME=usbcdc

62
firmware/usbcdc/util.c Normal file
View File

@ -0,0 +1,62 @@
#include <sysinit.h>
#include "usbcdc/usb.h"
#include "usbcdc/usbcore.h"
#include "usbcdc/cdc_buf.h"
#include "usbcdc/usbhw.h"
#include "usbcdc/cdcuser.h"
#include "basic/basic.h"
volatile unsigned int lastTick;
// There must be at least 1ms between USB frames (of up to 64 bytes)
// This buffers all data and writes it out from the buffer one frame
// and one millisecond at a time
int puts(const char * str){
if(!USB_Configuration)
return -1;
while(*str)
cdcBufferWrite(*str++);
//XXX: This assumes systick is 1ms, which it isn't for us.
// this makes usbserial unnecessary slow. Ah well....
// Check if we can flush the buffer now or if we need to wait
unsigned int currentTick = systickGetTicks();
if (currentTick != lastTick){
uint8_t frame[64];
uint32_t bytesRead = 0;
while (cdcBufferDataPending()){
// Read up to 64 bytes as long as possible
bytesRead = cdcBufferReadLen(frame, 64);
USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
systickDelay(1);
}
lastTick = currentTick;
}
return 0;
}
void usbCDCInit(){
lastTick = systickGetTicks(); // Used to control output/printf timing
CDC_Init(); // Initialise VCOM
USB_Init(); // USB Initialization
USB_Connect(TRUE); // USB Connect
/* You could wait until serial is connected. */
#if 0 // We don't
uint32_t usbTimeout = 0;
while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 ) {
if (USB_Configuration) break;
delayms(10); // Wait 10ms
usbTimeout++;
}
#endif
}
usbCDCOff(void){
USB_Connect(FALSE);
}

3
firmware/usbcdc/util.h Normal file
View File

@ -0,0 +1,3 @@
int puts(const char * str);
void usbCDCInit(void);
void usbCDCOff(void);