Skip to content
Snippets Groups Projects
Commit 5834a379 authored by Marco Tinner's avatar Marco Tinner
Browse files

corrected a bug with the descriptor values.

still problem with crashes... when displaying properties in device manager
parent 6bbb1104
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,6 @@
* Author: tinner
*/
#include "config.h"
#include "virtual_usb.h"
#include "uart.h"
#include "pll.h"
#include "usb_common.h"
......@@ -20,7 +19,8 @@ void init(void) {
CLKPR = 0x00; // set the system clock prescaler to 0
wdt_disable();
uart_init();
usb_common_init();
}
......@@ -29,14 +29,13 @@ int main(void) {
sei();
init();
U8 msg[] = "start\n\r";
uart_write_array(msg,sizeof(msg));
uart_write_string("start\n\r");
uart_trigger_send();
for(;;) {
//fillnrOfBytesInBuffer(&outBuffer,msg,sizeof(msg));
uart_write_string("heartbeat\n\r");
uart_trigger_send();
_delay_ms(1000);
}
return 0;
}
......@@ -23,6 +23,10 @@
// Start the USB PLL with clockfactor (can be PLLx24, PLLx12, PLLx08, PLLx06, PLLx04, PLLx03)
#define Start_pll(clockfactor) (PLLCSR = ( clockfactor | (1<<PLLE) ))
#define Config_pll(clockfactor) (PLLCSR |= clockfactor)
#define Enable_pll() (PLLCSR |= (1<<PLLE))
// Test PLL lock, return 1 when PLL locked
#define Is_pll_ready() (PLLCSR & (1<<PLOCK) )
......
......@@ -58,6 +58,8 @@ void uart_write(U8 c) {
}
}
void uart_writeNumber(U16 nr) {
U16 nrOfDigits = 0;
U16 power10 = 0;
......@@ -85,6 +87,15 @@ void uart_write_array(U8 *data, U8 length) {
}
}
void uart_write_string(U8 *string){
U8 i = 0;
while (string[i] != '\0'){
uart_write(string[i]);
i++;
}
}
// returns data from the in buffer
// if no data is availabe -1 will be returned
U8 uart_read(void) {
......
......@@ -3,7 +3,7 @@
#include "config.h"
#define BAUDRATE 9600
#define BAUDRATE 38400
#define UBRR_VAL (F_CPU / (16L * BAUDRATE)) - 1
#define UART_BUFSIZE_IN 0x80
......@@ -22,5 +22,5 @@ void uart_write(U8 c);
void uart_write_array(U8 *data, U8 length);
void uart_writeNumber(U16 nr);
U8 uart_read(void);
void uart_write_string(U8 *string);
#endif /*UART_H_*/
......@@ -14,11 +14,6 @@ U8 usb_config_ep(U8 config0, U8 config1)
UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
Usb_allocate_memory();
Usb_enable_in_ready_interrupt();
//Usb_enable_nak_in_interrupt();
Usb_enable_receive_out_interrupt();
Usb_enable_receive_setup_interrupt();
return (Is_endpoint_configured());
}
......@@ -86,57 +81,32 @@ void usb_common_init(void) {
Usb_force_device_mode();
Usb_enable();
Usb_select_device();
initBuffer(&inBuffer);
initBuffer(&outBuffer);
Usb_disable_wake_up_interrupt();
Usb_disable_reset_interrupt();
Usb_disable_sof_interrupt();
Usb_disable_suspend_interrupt();
Usb_disable_resume_interrupt();
UDINT = 0;
Usb_enable_vbus_interrupt();
}
U8 usb_init_device (void)
{
Usb_select_device();
if(Is_usb_id_device())
{
Usb_select_endpoint(EP_CONTROL);
if(!Is_usb_endpoint_enabled())
{
return usb_configure_endpoint(EP_CONTROL, \
USB_EP_TYPE_CONTROL, \
USB_EP_DIR_OUT, \
EP_SIZE_64, \
EP_ONE_BANK, \
EP_NYET_DISABLED);
}
}
Usb_select_device();
if(Is_usb_id_device()){
Usb_select_endpoint(EP_CONTROL);
if(!Is_usb_endpoint_enabled()){
usb_configure_endpoint(EP_CONTROL,USB_EP_TYPE_CONTROL,USB_EP_DIR_OUT,EP_SIZE_64,EP_ONE_BANK,EP_NYET_DISABLED);
Usb_enable_receive_setup_interrupt();
return TRUE;
}
}
return FALSE;
}
void usb_start_device (void)
{
Usb_enable_pad_regulator();
Pll_start_auto();
Wait_pll_ready();
Usb_unfreeze_clock();
Usb_enable_reset_interrupt();
usb_init_device(); // configure the USB controller EP0
// Endpoint 1
Usb_reset_endpoint(USB_EP1_NUM);
Usb_select_endpoint(USB_EP1_NUM);
Usb_enable_endpoint();
usb_configure_endpoint(USB_EP1_NUM, USB_EP1_TYPE, USB_EP1_DIR,USB_EP1_SIZE,EP_ONE_BANK,EP_NYET_ENABLED);
// Endpoint 2
Usb_reset_endpoint(USB_EP2_NUM);
Usb_select_endpoint(USB_EP2_NUM);
Usb_enable_endpoint();
usb_configure_endpoint(USB_EP2_NUM, USB_EP2_TYPE, USB_EP2_DIR, USB_EP2_SIZE, EP_ONE_BANK, EP_NYET_ENABLED);
// Endpoint 3
Usb_reset_endpoint(USB_EP3_NUM);
Usb_select_endpoint(USB_EP3_NUM);
Usb_enable_endpoint();
usb_configure_endpoint(USB_EP3_NUM, USB_EP3_TYPE, USB_EP3_DIR, USB_EP3_SIZE, EP_ONE_BANK, EP_NYET_ENABLED);
Usb_attach();
}
extern void vbus_off_action(void)
{
......@@ -165,15 +135,31 @@ U8 get_endpoint_nr_from_irq(){
void handleEPControlIRQ(){
if (Is_usb_receive_setup())
{
uart_write_string("Setup ");
usb_process_request();
Usb_ack_receive_setup();
}
}
if(Is_usb_in_ready())
{
uart_write_string("In");
Usb_ack_in_ready();
}
if(Is_usb_receive_out())
{
uart_write_string("Out");
Usb_ack_receive_out();
}
}
void handelEP1IRQ(){
if(Is_usb_in_ready())
{
Usb_send_control_in();
uart_write_string("In");
Usb_ack_in_ready();
//Usb_send_control_in();
/*
uint16_t bytesToSend = getAvailableBytes(&outBuffer);
if(bytesToSend>0){
uint16_t i = 0;
......@@ -184,6 +170,7 @@ void handelEP1IRQ(){
i++;
}
}
*/
while(Is_usb_read_enabled()){
}
Usb_ack_fifocon();
......@@ -193,15 +180,16 @@ void handelEP1IRQ(){
void handelEP2IRQ(){
if(Is_usb_receive_out())
{
uart_write_string("Out");
Usb_ack_receive_out();
uint16_t bytesToReceive = Usb_frame_number();
if(bytesToReceive>0){
uint16_t i = 0;
while(i < bytesToReceive){
fillBuffer(&inBuffer,Usb_read_byte());
i++;
}
}
// uint16_t bytesToReceive = Usb_frame_number();
// if(bytesToReceive>0){
// uint16_t i = 0;
// while(i < bytesToReceive){
// fillBuffer(&inBuffer,Usb_read_byte());
// i++;
// }
// }
}
}
......@@ -219,15 +207,21 @@ void handelEPIRQ(U8 ep){
ISR(USB_COM_vect){
U8 ep = 0;
uart_write_string("Controller Interrupt: ");
while(UEINT != 0){
ep = get_endpoint_nr_from_irq();
Usb_select_endpoint(ep);
switch(ep){
case EP_CONTROL:
handleEPControlIRQ();
UEINTX = 0;
//UEINTX = 0;
break;
case EP_1:
case EP_1:
uart_write_string("EP: ");
uart_writeNumber(ep);
uart_write_string(" ");
handelEP1IRQ();
break;
case EP_2:
......@@ -248,34 +242,53 @@ ISR(USB_COM_vect){
case EP_7:
handelEPIRQ(ep);
break;
}
}
//UEINTX = 0;
uart_write_string(", ");
}
uart_write_string("\n\r");
}
void handleVbusTransition(){
uart_write_string("VBUS plug-in detection, ");
if (Is_usb_vbus_high()){
Usb_enable_pad_regulator();
Config_pll(PLLx06);
Enable_pll();
Wait_pll_ready();
Usb_enable_reset_interrupt();
Usb_unfreeze_clock();
Usb_attach();
}else{
Usb_detach();
}
Usb_ack_vbus_transition();
}
ISR (USB_GEN_vect)
{
if (Is_usb_vbus_transition()){
if (Is_usb_vbus_high()){
usb_start_device();
Usb_attach();
}else{
Usb_detach();
}
Usb_ack_vbus_transition();
}
uart_write_string("Processing Interrupt: ");
if (Is_usb_vbus_transition()){
handleVbusTransition();
}
if(Is_usb_id_transition())
{
if(Is_usb_id_transition()){
//This shouldn't happen
uart_write_string("USB ID Pad change detection, ");
Usb_ack_id_transition();
}
if (Is_usb_sof()){
Usb_sof_action();
uart_write_string("Start of frame, ");
Usb_ack_sof();
}
if (Is_usb_suspend()){
Usb_enable_wake_up_interrupt();
uart_write_string("Suspend detected after 3 ms of inactivity, ");
//Usb_enable_wake_up_interrupt();
Usb_ack_wake_up(); // clear wake up to detect next event
Usb_freeze_clock();
Usb_suspend_action();
......@@ -283,25 +296,56 @@ ISR (USB_GEN_vect)
}
if (Is_usb_wake_up()){
Usb_unfreeze_clock();
uart_write_string("Wake up, ");
//Usb_unfreeze_clock();
Usb_ack_wake_up();
Usb_disable_wake_up_interrupt();
Usb_wake_up_action();
}
if (Is_usb_resume()){
Usb_disable_wake_up_interrupt();
Usb_disable_resume_interrupt();
Usb_resume_action();
Usb_ack_resume();
}
if (Is_usb_resume()){
uart_write_string("End of resume, ");
Usb_disable_wake_up_interrupt();
Usb_disable_resume_interrupt();
Usb_resume_action();
Usb_ack_resume();
}
if (Is_usb_reset()){
usb_init_device();
Usb_reset_action();
Usb_reset_endpoint(0);
usb_configuration_nb=0;
Usb_ack_reset();
}
if (Is_usb_reset()){
uart_write_string("End of reset, ");
usb_configuration_nb=0;
Usb_reset_endpoint(EP_CONTROL);
Usb_select_endpoint(EP_CONTROL);
Usb_enable_endpoint();
usb_configure_endpoint(EP_CONTROL,USB_EP_TYPE_CONTROL,USB_EP_DIR_OUT,EP_SIZE_64,EP_ONE_BANK,EP_NYET_DISABLED);
Usb_enable_receive_setup_interrupt();
// Endpoint 1
Usb_reset_endpoint(USB_EP1_NUM);
Usb_select_endpoint(USB_EP1_NUM);
Usb_enable_endpoint();
usb_configure_endpoint(USB_EP1_NUM, USB_EP1_TYPE, USB_EP1_DIR,USB_EP1_SIZE,EP_ONE_BANK,EP_NYET_ENABLED);
Usb_enable_in_ready_interrupt();
// Endpoint 2
Usb_reset_endpoint(USB_EP2_NUM);
Usb_select_endpoint(USB_EP2_NUM);
Usb_enable_endpoint();
usb_configure_endpoint(USB_EP2_NUM, USB_EP2_TYPE, USB_EP2_DIR, USB_EP2_SIZE, EP_ONE_BANK, EP_NYET_ENABLED);
Usb_enable_receive_out_interrupt();
// Endpoint 3
Usb_reset_endpoint(USB_EP3_NUM);
Usb_select_endpoint(USB_EP3_NUM);
Usb_enable_endpoint();
usb_configure_endpoint(USB_EP3_NUM, USB_EP3_TYPE, USB_EP3_DIR, USB_EP3_SIZE, EP_ONE_BANK, EP_NYET_ENABLED);
Usb_ack_reset();
}
uart_write_string("\n\r");
}
\ No newline at end of file
......@@ -357,8 +357,6 @@
U8 usb_config_ep(U8, U8);
void usb_common_init(void);
void usb_start_device (void);
void usb_task(void);
//********** Variables **********//
struct RingBuffer inBuffer, outBuffer;
......
......@@ -21,7 +21,7 @@
//********** USB sub types **********//
#define USB_ST_HEADER 0x00
#define USB_ST_CALL_MANG 0x02
#define USB_ST_CALL_MANG 0x01
#define USB_ST_ABSTR_CTR 0x02
#define USB_ST_UNION 0x06
......
......@@ -25,13 +25,13 @@ void send_fake_data(void){
}
Usb_send_control_in();
if(Is_usb_receive_out()) {
Usb_ack_receive_out();
return; // abort from Host
}
if(Is_usb_receive_out()) {
Usb_ack_receive_out();
return; // abort from Host
}
//while(!Is_usb_receive_out());
//Usb_ack_receive_out();
while(!Is_usb_receive_out());
Usb_ack_receive_out();
}
void fake_handle(){
......@@ -64,38 +64,47 @@ void fake_handle(){
* sent by the standard usb read request function.
*/
U8 usb_device_read_request(U8 requestType, U8 request) {
switch(requestType){
case WRITE_TMS:
case WRITE_REQUEST:
switch(request){
case SET_DATA:
fake_handle();
return TRUE;
break;
case MODEM_CONTROL:
fake_handle();
return TRUE;
break;
case USB_RESET:
fake_handle();
return TRUE;
break;
case SET_FLOW_CONTROL:
fake_handle();
return TRUE;
break;
case SET_BAUDE_RATE:
fake_handle();
return TRUE;
break;
case SET_LATENCY_TIMER:
fake_handle();
return TRUE;
break;
default:
return FALSE;
}
break;
case HW_INFO_REQUEST:
case READ_REQUEST:
send_fake_data();
return FALSE;
break;
default:
return FALSE;
}
return TRUE;
return FALSE;
}
/*
......@@ -104,11 +113,11 @@ U8 usb_device_read_request(U8 requestType, U8 request) {
void usb_device_endpoint_init(U8 conf_nb) {
// Endpoint 1
usb_configure_endpoint(USB_EP1_NUM, USB_EP1_TYPE, USB_EP1_DIR,USB_EP1_SIZE,EP_ONE_BANK,EP_NYET_ENABLED);
//usb_configure_endpoint(USB_EP1_NUM, USB_EP1_TYPE, USB_EP1_DIR,USB_EP1_SIZE,EP_ONE_BANK,EP_NYET_ENABLED);
// Endpoint 2
usb_configure_endpoint(USB_EP2_NUM, USB_EP2_TYPE, USB_EP2_DIR, USB_EP2_SIZE, EP_ONE_BANK, EP_NYET_ENABLED);
//usb_configure_endpoint(USB_EP2_NUM, USB_EP2_TYPE, USB_EP2_DIR, USB_EP2_SIZE, EP_ONE_BANK, EP_NYET_ENABLED);
// Endpoint 3
usb_configure_endpoint(USB_EP3_NUM, USB_EP3_TYPE, USB_EP3_DIR, USB_EP3_SIZE, EP_ONE_BANK, EP_NYET_ENABLED);
//usb_configure_endpoint(USB_EP3_NUM, USB_EP3_TYPE, USB_EP3_DIR, USB_EP3_SIZE, EP_ONE_BANK, EP_NYET_ENABLED);
}
/*
......
......@@ -14,8 +14,8 @@
//********** request types **********//
#define WRITE_TMS 0x40
#define HW_INFO_REQUEST 0xC0
#define WRITE_REQUEST 0x40
#define READ_REQUEST 0xC0
//********** request **********//
......
......@@ -61,6 +61,7 @@ void usb_process_request(void) {
switch (bmRequest) {
case GET_DESCRIPTOR:
if (0x80 == bmRequestType) {
uart_write_string("get descriptor ");
usb_get_descriptor();
} else {
usb_device_read_request(bmRequestType, bmRequest);
......@@ -68,13 +69,16 @@ void usb_process_request(void) {
break;
case GET_CONFIGURATION:
if (0x80 == bmRequestType) {
uart_write_string("get configuration ");
usb_get_configuration();
} else {
usb_device_read_request(bmRequestType, bmRequest);
}
break;
case SET_ADDRESS:
if (0x00 == bmRequestType) {
uart_write_string("set address ");
usb_set_address();
} else {
usb_device_read_request(bmRequestType, bmRequest);
......@@ -82,6 +86,7 @@ void usb_process_request(void) {
break;
case SET_CONFIGURATION:
if (0x00 == bmRequestType) {
uart_write_string("set configuration ");
usb_set_configuration();
} else {
......@@ -90,6 +95,7 @@ void usb_process_request(void) {
break;
case CLEAR_FEATURE:
if (0x02 == bmRequestType) {
uart_write_string("clear feature ");
usb_clear_feature();
} else {
usb_device_read_request(bmRequestType, bmRequest);
......@@ -98,12 +104,14 @@ void usb_process_request(void) {
case SET_FEATURE:
if (0x02 == bmRequestType) {
usb_set_feature();
uart_write_string("set feature ");
} else {
usb_device_read_request(bmRequestType, bmRequest);
}
break;
case GET_STATUS:
if ((0x7F < bmRequestType) & (0x82 >= bmRequestType)) {
uart_write_string("get status ");
usb_get_status();
} else {
usb_device_read_request(bmRequestType, bmRequest);
......@@ -111,6 +119,7 @@ void usb_process_request(void) {
break;
case GET_INTERFACE:
if (bmRequestType == 0x81) {
uart_write_string("get interface ");
usb_get_interface();
} else {
usb_device_read_request(bmRequestType, bmRequest);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment