Hacker-Jeopardy/Mainboard_/Mainboard/usb.c

81 lines
2.6 KiB
C
Raw Permalink Normal View History

2013-11-04 18:45:46 +01:00
/*
* usb.c
*
* Created: 06.10.2013 21:42:35
* Author: netz
*/
#include "config.h"
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "usb_user_config.h" // USB configuration
#include "usb_user.h" // USB functions
#include "usb.h"
#ifdef USERDEFCONTROLS // see usbconfig.h
//-----------------------------------------------------------------------------
// Handle user defined control requests
// "ucr" contains the data of the control request
// function has to return "true", if request is handled (otherwise "false")
bool usb_controlrequest(struct usb_control_request *ucr) {
#define USER_REQUESTTYPE 0xC3 // a user defined request type (means, no standard request)
#define USER_REQUESTVERSION 0x01 // a self defined request: get version info
bool Result;
const char VersionInfo[ENDPOINT0_SIZE] = {"Version 1.0"};
Result=false;
if (ucr->bmRequestType == USER_REQUESTTYPE) {
// handle the requests (only one defined in this example)
switch (ucr->bRequest) {
case USER_REQUESTVERSION: // request for version info)
usb_txdata_control((uint8_t *)VersionInfo,sizeof(VersionInfo));
Result=true;
break;
}
}
return Result; // true means, request is handled
}
#endif
//-----------------------------------------------------------------------------
// handle of endpoint interrupt for incoming data
// "datasize" contains the number of bytes in the USB FIFO buffer
void usb_ep(uint8_t endpoint, uint16_t datasize) {
#define RXENDPOINT 0x01 // endpoint, used for data transfer host->device (defined in "usb_user_config.h")
#define TXENDPOINT 0x82 // endpoint, used for data transfer device->host
#define BUFSIZE 32 // USB FIFO buffer size (see usb_user_config.h at "endpoints")
char buf[BUFSIZE]; // buffer for received data from host and data to transmit to host
uint8_t n;
if (endpoint == RXENDPOINT) { // data from host by our defined data receive endpoint
n=usb_rxdata(endpoint, (uint8_t *)buf, sizeof(buf)); // read data into buffer
if (n) { // data read into local buffer
buf[BUFSIZE-1]=0; // for security: terminate string
//resi(buf);
strupr(buf); // upper case string
// response to host with converted string
usb_txdata(TXENDPOINT, (uint8_t *)buf, sizeof(buf)); // transfer data to host*/
}
}
}
void init_usb() {
usb_init();
}
void usb_send(char* text) {
#define RXENDPOINT 0x01
#define TXENDPOINT 0x82
#define ITENDPOINT 0x83
usb_txdata(TXENDPOINT, (uint8_t *)text, strlen(text));
}
uint8_t usb_getstatus() {
return usb_status();
}
uint8_t usb_ready() {
return usb_is_ready();
}