Skip to content

Commit

Permalink
messages debug
Browse files Browse the repository at this point in the history
  • Loading branch information
y-salnikov committed Apr 28, 2016
1 parent 56b63cc commit cd972dc
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 100 deletions.
6 changes: 3 additions & 3 deletions host/default_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ common:\n\
{\n\
vid=0x04b4; #vid/pid\n\
pid=0x8613;\n\
usb_buf_size=16384;\n\
usb_transfers=15;\n\
usb_timeout=200;\n\
usb_freq=12000000;\n\
framebuffer_size=1024; #framebuffer size is 1024x1024\n\
clk_inverted=true;\n\
sync_bit_mask=0x10; # which bit is sync\n\
Expand Down Expand Up @@ -41,6 +39,7 @@ machines:\n\
{\n\
name=\"MS-0511\";\n\
inv_bits=0x0f;\n\
usb_freq=12500000;\n\
area={ x0=0.07; y0=0.01; x1=0.7; y1=0.3; }\n\
},\n\
{\n\
Expand Down Expand Up @@ -71,6 +70,7 @@ machines:\n\
\n\
clk_inverted=true; \n\
inv_bits=0x00; # will be xored with input data\n\
usb_freq=8000000;\n\
area={ x0=0.048; y0=0.035; x1=0.34; y1=0.245; }\n\
\n\
}\n\
Expand Down
46 changes: 39 additions & 7 deletions host/machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,50 @@ int bin2int(const char *bin_str)

}

static unsigned int to_bytes_per_ms(unsigned int samplerate)
{
return samplerate / 1000;
}

int get_macine_config(machine_type *mac, config_setting_t *machine)
{
int err=0;
int i,c,m,bw;
const char *binstr;
config_setting_t *colors, *color, *area;
size_t s;
size_t total_size;
unsigned int timeout;

config_setting_lookup_bool(machine, "clk_inverted",&mac->clk_inverted );
config_setting_lookup_int(machine, "inv_bits",&mac->inv_bits);
config_setting_lookup_int(machine, "sync_bit_mask",&mac->sync_bit_mask);
config_setting_lookup_int(machine,"pixel_bits_mask",&mac->pixel_bits_mask );
config_setting_lookup_int(machine,"vid",&mac->vid );
config_setting_lookup_int(machine,"pid",&mac->pid );
config_setting_lookup_int(machine,"usb_buf_size",&mac->USB_BUF_SIZE );
config_setting_lookup_int(machine,"usb_transfers",&mac->N_OF_TRANSFERS );
config_setting_lookup_int(machine,"usb_timeout",&mac->usb_timeout );
config_setting_lookup_int(machine,"usb_freq",&mac->freq );

/*
* The buffer should be large enough to hold 10ms of data and
* a multiple of 512. (from sigrok)
*/

s = 10 * to_bytes_per_ms(mac->freq);
mac->USB_BUF_SIZE=(s + 511) & ~511;

/* Total buffer size should be able to hold about 500ms of data. */
mac->N_OF_TRANSFERS = (500 * to_bytes_per_ms(mac->freq)) / mac->USB_BUF_SIZE;

if ( mac->N_OF_TRANSFERS > 32)
mac->N_OF_TRANSFERS=32;



total_size = mac->USB_BUF_SIZE * mac->N_OF_TRANSFERS;
timeout = total_size / to_bytes_per_ms(mac->freq);
mac->usb_timeout=(timeout + timeout / 4); /* Leave a headroom of 25% percent. */


colors=config_setting_get_member(machine,"colors");
if (colors!=NULL)
{
Expand Down Expand Up @@ -151,6 +180,9 @@ int get_macine_config(machine_type *mac, config_setting_t *machine)
return err;
}




machine_type *machine_init(uint8_t command, const char* machine_name, const char* config_file_path)
{
char *endptr=NULL;
Expand Down Expand Up @@ -178,10 +210,10 @@ machine_type *machine_init(uint8_t command, const char* machine_name, const char
mac->pixel_bits_mask=0x0f;
mac->vid=0x04b4;
mac->pid=0x8613;
mac->USB_BUF_SIZE=16384;
mac->N_OF_TRANSFERS=15;
mac->usb_timeout=200;

// mac->USB_BUF_SIZE=16384;
// mac->N_OF_TRANSFERS=15;
// mac->usb_timeout=200;
mac->freq=12000000;
if(command & COMMAND_DUMP) return mac;
if(command & COMMAND_SELECT) // -m
{
Expand Down
4 changes: 2 additions & 2 deletions host/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ int main(int argc, char **argv)

if(test)
{
usb_test(utc);
// usb_test(utc);
exit(0);
}
rc=render_init(mac,pcont);
Expand Down Expand Up @@ -193,7 +193,7 @@ int main(int argc, char **argv)
{
rc->no_device_flag=0;
rc->no_signal_flag=1;
signal_present=80;
signal_present=90;
}
}
else if(usb_get_thread_state(utc))
Expand Down
2 changes: 2 additions & 0 deletions host/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct machine_struct
int USB_BUF_SIZE;
int N_OF_TRANSFERS;
int usb_timeout;
int freq;
}machine_type;


Expand Down Expand Up @@ -68,6 +69,7 @@ typedef struct usb_transfer_context_struct
uint8_t usb_stop_flag;
uint8_t *control_buffer;
process_context_type *process_context;
uint8_t *tmp_buffer;

}usb_transfer_context_type;

Expand Down
159 changes: 71 additions & 88 deletions host/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include "command.h"
#include "compat.h"
#include "types.h"
#include "process.h"
#include "usb.h"
#include "profile.h"

#define MAX_EMPTY_TRANSFERS 64


LIBUSB_CALL void callbackUSBTransferComplete(struct libusb_transfer *xfr);

int WriteRAM(usb_transfer_context_type *utc, size_t addr,const unsigned char *data, size_t nbytes)
Expand Down Expand Up @@ -164,12 +168,13 @@ usb_transfer_context_type* usb_init(const char **firmware, void *proc_cont)
utc=malloc(sizeof(usb_transfer_context_type));
utc->active_transfers=0;
utc->usb_transfer_cb_served=0;
utc->usb_stop_flag=2; // 0 -running, 1-stoppes, 2-not inited
utc->usb_stop_flag=2; // 0 -running, 1-stoppes, 2-not inited, 3- no device
utc->process_context=proc_cont;
utc->tmp_buffer=malloc(utc->process_context->machine_context->USB_BUF_SIZE);
utc->device_h=libusb_open_device_with_vid_pid(NULL,utc->process_context->machine_context->vid,utc->process_context->machine_context->pid);
if(utc->device_h==NULL)
{
fprintf(stderr,"No device found or device already configured.\n");
// fprintf(stderr,"No device found or device already configured.\n");
}
else
{
Expand Down Expand Up @@ -236,7 +241,7 @@ usb_transfer_context_type* usb_init(const char **firmware, void *proc_cont)
utc->device_h=libusb_open_device_with_vid_pid(NULL,0xffff,0x2048);
if(utc->device_h==NULL)
{
fprintf(stderr,"No reconfigured device found\n");
// fprintf(stderr,"No reconfigured device found\n");
free(utc);
return NULL;
}
Expand Down Expand Up @@ -314,46 +319,6 @@ void usb_send_start_cmd(usb_transfer_context_type *utc)
}
}

#define N 1024*1024
void usb_test(usb_transfer_context_type *utc)
{
int rv,i;
struct version_info ver;
uint8_t *buf;
int transfered=0;
FILE *f;

rv=libusb_control_transfer(utc->device_h,0xc0,CMD_GET_FW_VERSION,
/*addr=*/0,0,
/*buf=*/(unsigned char*)&ver,/*size=*/2,
/*timeout=*/1000/*msec*/);
if(rv<0)
{
fprintf(stderr,"Vendor request error\n");
return;
}
printf("Version %d.%d\n",ver.major,ver.minor);
buf=malloc(N);
profile_start();
usb_send_start_cmd(utc);
profile_check("usb_send_start_cmd");
rv=libusb_bulk_transfer(utc->device_h,utc->endpoint,(unsigned char *)buf,N,&transfered,200);
profile_check("bulk_transfer");
if(rv<0)
{
fprintf(stderr,"Bulk transfer error\n");
return;
}
printf("transfered %d bytes\n", transfered);
f=fopen("usb_log.dat","w");
for(i=0;i<transfered;i++)
{
fprintf(f,"%c\n",buf[i]);
}
fclose(f);

}


void usb_start_transfer (usb_transfer_context_type *utc)
{
Expand Down Expand Up @@ -384,10 +349,14 @@ void usb_start_transfer (usb_transfer_context_type *utc)
LIBUSB_CALL void callbackUSBTransferComplete(struct libusb_transfer *xfr)
{
uint8_t err=0;
uint8_t fatal=0;
usb_transfer_context_type *utc;

static int et_count;
size_t lngth;

if(xfr->user_data==NULL) // control transfer
{
et_count=0;
if(xfr->status!=LIBUSB_TRANSFER_COMPLETED)
{
fprintf(stderr,"USB controll transfer error\n");
Expand All @@ -398,63 +367,78 @@ LIBUSB_CALL void callbackUSBTransferComplete(struct libusb_transfer *xfr)
utc=xfr->user_data;
switch(xfr->status)
{
case LIBUSB_TRANSFER_COMPLETED:
// Success here, data transfered are inside
// xfr->buffer
// and the length is
// xfr->actual_length
parse_data(utc->process_context,(void *)xfr->buffer, xfr->actual_length);
if(libusb_submit_transfer(xfr) < 0)
{
// Error
libusb_free_transfer(xfr);
free(xfr->buffer);
fprintf(stderr,"USB resubmit transfer error\n");
utc->active_transfers--;
err=1;
}
break;
case LIBUSB_TRANSFER_CANCELLED:
fprintf(stderr,"USB transfer error: canceled\n");
err=1;
break;
case LIBUSB_TRANSFER_NO_DEVICE:
if(utc->usb_stop_flag!=1) fprintf(stderr,"USB transfer error: no device\n");
err=1;
case LIBUSB_TRANSFER_NO_DEVICE:
fatal=1;
break;
case LIBUSB_TRANSFER_COMPLETED:
case LIBUSB_TRANSFER_TIMED_OUT:
fprintf(stderr,"USB transfer error: time out\n");
err=1;
break;
case LIBUSB_TRANSFER_ERROR:
fprintf(stderr,"USB transfer error\n");
err=1;
break;
case LIBUSB_TRANSFER_STALL:
fprintf(stderr,"USB transfer error: stall\n");
err=1;
break;
case LIBUSB_TRANSFER_OVERFLOW:
fprintf(stderr,"USB transfer error: overflow\n");
err=1;
break;

default:
err=1;
break;
}
if (err && utc->usb_stop_flag==0)
if(!fatal)
{
fprintf(stderr,"%d active transfers left\n", --utc->active_transfers);
if(xfr->actual_length==0 || err)
{
et_count++;
if(et_count>=MAX_EMPTY_TRANSFERS)
{
// fprintf(stderr,"%d empty transfers\n",MAX_EMPTY_TRANSFERS);
fatal=1;
}
else
{
if(libusb_submit_transfer(xfr) < 0)
{
// Error
libusb_free_transfer(xfr);
free(xfr->buffer);
fprintf(stderr,"USB resubmit transfer error\n");
utc->active_transfers--;
err=1;
}
}
}
else
{
et_count=0;
lngth=xfr->actual_length;
memcpy(utc->tmp_buffer,xfr->buffer,lngth);
if(libusb_submit_transfer(xfr) < 0)
{
// Error
libusb_free_transfer(xfr);
free(xfr->buffer);
fprintf(stderr,"USB resubmit transfer error\n");
utc->active_transfers--;
err=1;
}
parse_data(utc->process_context,utc->tmp_buffer, lngth);
}
}
if (fatal)
{
libusb_free_transfer(xfr);
free(xfr->buffer);
utc->active_transfers--;
if(utc->active_transfers==0) utc->usb_stop_flag=1;
return;
}
if (utc->active_transfers<2) utc->usb_stop_flag=1;

utc->usb_transfer_cb_served=1;
}
}


void usb_poll(void) // need to be periodicaly called
{
if(libusb_handle_events(NULL) != LIBUSB_SUCCESS)
struct timeval tv;
tv.tv_sec = tv.tv_usec = 0;
if( libusb_handle_events_timeout(NULL, &tv)!= LIBUSB_SUCCESS)
{
fprintf(stderr,"USB error event\n");
fprintf(stderr,"USB error event\n");
}
}

Expand All @@ -465,7 +449,7 @@ int usb_thread_function(void *utc_ptr)
if(utc==NULL) return 0;
usb_start_transfer(utc);
utc->usb_stop_flag=0;
while(!utc->usb_stop_flag)
while(utc->usb_stop_flag<2)
{
usb_poll();
SLEEP(1);
Expand All @@ -482,7 +466,6 @@ uint8_t usb_get_thread_state(usb_transfer_context_type *utc)
utc=NULL;
return 3;
}

return utc->usb_stop_flag;
}

Expand Down

0 comments on commit cd972dc

Please sign in to comment.