#include <cvirte.h>
#include "PtsModule.h"
#include <userint.h>
#include "PtsModule_CVI.h"
#include <utility.h>
#include <formatio.h>
static int p1;
// sudo ./pts -a 0x1000000 -v 1 -w 2 turn LED ON
// sudo ./pts -a 0x1000000 -v 1 -w 1 turn LED OFF
// sudo ./ptsvmusb -a 0x02501000 -v 2 -i a.bit -b 2
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "vme.h"
int verbose;
uint32_t ptaddr=0;
char str[255];
int Pts_write(uint32_t addr, uint32_t data ){
int i;
VME_A32D32_W(ptaddr+addr,data);
return 0;
}
int Pts_Mwrite(uint32_t addr, uint32_t data ){
int i;
VME_MW(VME_A32, VME_D32, ptaddr+addr,data);
return 0;
}
int Pts_read(uint32_t addr, uint32_t *data ){
VME_A32D32_R(ptaddr+addr,data);
return 0;
}
int Pts_erase( int verbose ) {
uint32_t dum;
Pts_write( ADR_CSR1, 0 );
Delay(0.1);
Pts_read( ADR_CSR1, &dum);
if( verbose ) {
sprintf(str
, "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1
,dum
& 0xffff );
SetCtrlVal (p1, P1_RESPONSE, str);
}
Pts_write( ADR_CSR1, CSR1_PROGRAM_ );
Delay(0.1);
Pts_read( ADR_CSR1, &dum);
if( verbose ) {
sprintf( str
, "CSR1(0x%02x) = 0x%04x\n", ADR_CSR1
, dum
& 0xffff );
SetCtrlVal (p1, P1_RESPONSE, str);
}
return 1;
}
int Pts_configure_bit( const char *filename, int mode, int verbose ) {
int c,j;
int dummyword;
FILE *fp;
const long byte_per_dot = BYTE_PER_DOT;
unsigned long nchar = 0;
if( ( fp
= fopen( filename
, "rb" ) ) == NULL
) {
if( verbose ) {
sprintf( str
, "cannot open \"%s\"\n", filename
);
SetCtrlVal (p1, P1_RESPONSE, str);
}
return -1;
}
if(verbose
) sprintf( str
, "file \"%s\" opened.\n", filename
);
/* ------------------------------------------------------------ *\
The data for the configuration start from 0xffff_ffff_aa99_aa66
( cf. xapp138; we don't know the definition of the BIT file )
\* ------------------------------------------------------------ */
dummyword = 0;
do{
if( (c
= getc( fp
)) == EOF
) {
if(verbose) {
sprintf(str
, "EOF detected. Exit.\n");
SetCtrlVal (p1, P1_RESPONSE, str);
}
return -1;
}
(c == 0xff) ? dummyword++ : (dummyword=0);
} while( dummyword < 4 );
if( mode == SLAVESERIAL_MODE ) {
if(verbose) SetCtrlVal(p1,P1_RESPONSE,"slave serial mode");
Pts_write( ADR_MODE, mode );
Pts_erase( verbose );
for( j=0; j<32; j++ ) Pts_write( ADR_CFG, 0x1 );
while( (c
=getc(fp
))!=EOF
){
for( j=0; j<8; j++ ) Pts_write( ADR_CFG, (c>>(7-j))&0x1 );
nchar++;
if( verbose && nchar%byte_per_dot==0 ) {
SetCtrlVal(p1,P1_RESPONSE,str);
}
}
} else if( mode == SELECTMAP_MODE ) {
if( verbose ) SetCtrlVal(p1,P1_RESPONSE,"select map mode\n");
Pts_write( ADR_MODE, SELECTMAP_MODE );
Pts_erase( verbose );
VME_MWRST();
for( j=0; j<4; j++ ) Pts_Mwrite( ADR_CFG, 0xff );
VME_MWEXEC();
VME_MWRST();
while( (c
=getc(fp
))!=EOF
){
int cc = 0;
for(j=0; j<8; j++) cc |= ((c&(1<<j))>>j)<<(7-j);
Pts_Mwrite( ADR_CFG, cc );
nchar++;
if( verbose && nchar%byte_per_dot==0 ){
VME_MWEXEC();
VME_MWRST();
SetCtrlVal(p1,P1_RESPONSE,str);
}
}
VME_MWEXEC();
} else {
if(verbose) {
SetCtrlVal(p1,P1_RESPONSE,str);
}
return -1;
}
if(verbose) {
sprintf(str
,"\ntotal %ld bits\n", nchar
);
SetCtrlVal(p1,P1_RESPONSE,str);
}
return Pts_check_configure( verbose );
}
int Pts_check_configure( int verbose ) {
uint32_t csr1_value;
Pts_read(ADR_CSR1,&csr1_value);
if(verbose) {
sprintf( str
,"CSR1(0x%02x)=0x%04x\n",ADR_CSR1
,csr1_value
&0xffff);
SetCtrlVal(p1,P1_RESPONSE,str);
}
if(csr1_value&CSR1_DONE) {
if(verbose) SetCtrlVal(p1,P1_RESPONSE, "configure complete.\n");
return 1;
} else {
if(verbose) SetCtrlVal(p1,P1_RESPONSE,"configure not complete.");
return -1;
}
}
int Pts_reset( int verbose ) {
Pts_write(ADR_CSR0,1);
if( verbose ) {
sprintf( str
, "CSR0(0x%02x) = 0x01\n", ADR_CSR0
);
SetCtrlVal(p1,P1_RESPONSE,str);
}
return 1;
}
int Pts_write_csr( int verbose, uint32_t value ) {
Pts_write(ADR_CSR0,value);
if( verbose ) {
sprintf( str
,"Pts_write_csr 0x%08x\n", value
);
SetCtrlVal(p1,P1_RESPONSE,str);
}
return 1;
}
void help(char *argv){
sprintf(str
,"Usage: %s -a ptsaddr -v verbose -c .... Pts_check_configure\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Usage: %s -a ptsaddr -v verbose -i filename -b mode (2) ... Pts_configure_bit\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Usage: %s -a ptsaddr -v verbose -e .... Pts_erase\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Usage: %s -a ptsaddr -v verbose -x .... Pts_reset\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Usage: %s -a ptsaddr -v verbose -c .... Pts_check_configure\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Usage: %s -a ptsaddr -v verbose -r .... vme read\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Usage: %s -a ptsaddr -v verbose -w value .... vme write\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Usage: %s -a ptsaddr -v verbose -s value .... Pts_write_csr\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Example: %s --address 0x1000000 --verbose 1 --erase\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Example: %s --address 0x1000000 --verbose 1 --input pts_scaler.bit --load-bit 2\n", argv
);
SetCtrlVal(p1,P1_RESPONSE,str);
sprintf(str
,"Example: %s --address 0x1000004 --write-csr 0x7600\n", argv
);SetCtrlVal
(p1
,P1_RESPONSE
,str
);
}
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
char saddr[255];
if (InitCVIRTE (hInstance, 0, 0) == 0)
return -1; /* out of memory */
if ((p1 = LoadPanel (0, "PtsModule_CVI.uir", P1)) < 0)
return -1;
DisplayPanel (p1);
GetCtrlVal(p1,P1_VERBOSE,&verbose);
GetCtrlVal(p1,P1_ADDRESS,saddr);
RunUserInterface ();
DiscardPanel (p1);
VME_STOP();
return 0;
}
int CVICALLBACK Reset (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Pts_reset(verbose);
break;
}
return 0;
}
int CVICALLBACK Erase (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Pts_erase(verbose);
break;
}
return 0;
}
int CVICALLBACK Download (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char filename[254];
int mode=0;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(p1,P1_FIRMWARE,filename);
GetCtrlVal(p1,P1_MODE,&mode);
if(VME_CONNECTED() >=0 ) {
Pts_configure_bit( filename, mode, verbose );
} else {
MessagePopup ("Warning", "Connect VME!!");
}
break;
}
return 0;
}
int CVICALLBACK Check (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Pts_check_configure( verbose );
break;
}
return 0;
}
int CVICALLBACK Read (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
uint32_t value=0;
char str[0xFF];
char saddr[256];
int offset;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(p1,P1_OFFSET,saddr);
Pts_read( offset , &value );
if( verbose ) {
sprintf(str
, "VME Read addr 0x%0x+0x%0x response 0x%0x\n", ptaddr
, offset
, value
);
SetCtrlVal (p1, P1_RESPONSE, str);
}
break;
}
return 0;
}
int CVICALLBACK Write (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int value;
int offset;
char svalue[256];
char saddr[256];
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(p1,P1_DATA,svalue);
GetCtrlVal(p1,P1_OFFSET,saddr);
Pts_write( offset , value );
if( verbose ) {
sprintf(str
, "VME Write addr 0x%0x+0x%0x data 0x%0x\n", ptaddr
, offset
, value
);
SetCtrlVal (p1, P1_RESPONSE, str);
}
break;
}
return 0;
}
int CVICALLBACK WriteCSR (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int value;
char svalue[256];
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(p1,P1_DATA,svalue);
Pts_write_csr( verbose, value );
break;
}
return 0;
}
int CVICALLBACK Verbose (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int * data;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(panel,control,&verbose);
break;
}
return 0;
}
int CVICALLBACK Help (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
help("PtsModule");
break;
}
return 0;
}
int CVICALLBACK Address (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char saddr[100];
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(p1,P1_ADDRESS,saddr);
break;
}
return 0;
}
int CVICALLBACK Exit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface(0);
break;
}
return 0;
}
int CVICALLBACK FSelect (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int status;
char pathName[MAX_PATHNAME_LEN];
switch (event)
{
case EVENT_COMMIT:
status = FileSelectPopup ("", "*.bit", "*.bit", "Select Firmware file",
VAL_SELECT_BUTTON, 0, 0, 1, 0, pathName);
if (status>0) SetCtrlVal(p1,P1_FIRMWARE,pathName);
break;
}
return 0;
}
int CVICALLBACK LedCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
int value=0;
case EVENT_COMMIT:
GetCtrlVal(p1,P1_LED,&value);
Pts_write( 0 , value+1 );
break;
}
return 0;
}
int CVICALLBACK Execute (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:{
int n,i,nb;
char buf[400];
unsigned int data, addr;
char sdata[255], saddr[255], rw[255];
GetNumTextBoxLines(panel, P1_COMMANDS,&n);
for (i=0;i<n;i++){
GetTextBoxLine(panel, P1_COMMANDS, i, buf);
nb
= sscanf(buf
,"%s%s%s",rw
, saddr
,sdata
);
Pts_write( addr , data );
if( verbose ) {
sprintf(str
, "VME Write addr 0x%0x+0x%0x data 0x%0x\n", ptaddr
, addr
, data
);
SetCtrlVal (p1, P1_RESPONSE, str);
}
} else {
Pts_read( addr , &data );
if( verbose ) {
sprintf(str
, "VME Read addr 0x%0x+0x%0x response 0x%0x\n", ptaddr
, addr
, data
);
SetCtrlVal (p1, P1_RESPONSE, str);
}
}
}
break;
}
}
return 0;
}
int CVICALLBACK Connect (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int interface = 0;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal (p1, P1_VMEINTERFACE, &interface);
VME_START(interface);
break;
}
return 0;
}
int CVICALLBACK Disconnect (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
VME_STOP();
break;
}
return 0;
}