PDA

View Full Version : Vdrive2



Bill Legge
- 31st May 2012, 00:22
Some code to talk to the Vdrive2 device from Vinculum.
Thought this code may be of interest to someone:

1. DEBUG on G1/G2.
2. Vdrive2 on portB.
3. Vdrive2 CTS to ground, RTS not used.




' -----[ Program Description ]---------------------------------------
'
' Name: WVL Vdrive_03 File write test
' Program stores two 8-bit readings to USB thumb drive using a Vdrive2
'
' Vdrive jumper in UART MODE (UART/SPI pulled up datasheet V.99 is correct)
' Pin Name Description
'
' 1 GND 5V common
' 2 RTS Request to Send
' 3 Vdd Connects to +5V
' 4 RXD Receive Data
' 5 TXD Transmit Data
' 6 CTS Clear to Send (connect to common or make low)
' 7 NC No Connection
' 8 RI Ring Indicator (not used)
'
' PIC18F8722 on BIGPIC5
' DEBUG used for PC comms and SERIN/SEROUT for VDrive2 comms all at 9600 Baud
'
' -----[ DEFINES ]---------------------------------------------------
'
clear
DEFINE OSC 40 ' Compile as HS
DEFINE LCD_DREG PORTD ' Define PIC port used for LCD Data lines
DEFINE LCD_DBIT 4 ' Define first pin of data nibble
DEFINE LCD_RSREG PORTD ' Define PIC port used for R/S line
DEFINE LCD_RSBIT 2 ' Define Portb pin used for R/S
DEFINE LCD_EREG PORTD ' Define PIC prot used for E line
DEFINE LCD_EBIT 3 ' Define PortB pin used for E connection
DEFINE LCD_BITS 4 ' Define the 4 bit communication mode
DEFINE LCD_LINES 4 ' Define using a 2 line LCD
DEFINE LCD_COMMANDUS 1500 ' Define delay time between sending commands
DEFINE LCD_DATAUS 45 ' Define delay time between data sent
lcdout $fe,$1
pause 2000
FLAGS=0
INCLUDE "modedefs.bas" ' Include serial modes
DEFINE DEBUG_REG PORTG ' Note: UART2
DEFINE DEBUG_BIT 1
DEFINE DEBUG_BAUD 9600 ' VDrive2 default speed
DEFINE DEBUG_MODE 0
DEFINE DEBUGIN_REG PORTG
DEFINE DEBUGIN_BIT 2
DEFINE DEBUGIN_BAUD 9600
DEFINE DEBUGIN_MODE 0
define ADC_BITS 8
'
' -----[ VARIABLES ]-----------------------------------------------------------
'
X VAR byte
Y var byte
Z var byte
A var byte
B var byte
C var byte
D var byte
E var byte
F var byte
G var word
DataIn var byte[50]
Echo var byte
Rx VAR PORTB.1 ' Data in from VDrive [Yellow]
Tx VAR PORTB.0 ' Data out to VDrive [Orange]
Heartbeat var PORTD.0
Number VAR BYTE[10]
'
' -----[ INITIALISE ]----------------------------------------------------------
'
ADCON1 = %00001111 ' All digital
TRISA = %00000011 ' A0 and A1 inputs
TRISB = %00000000
TRISC = %00000001 ' C0 is data in, C1 is data out
TRISD = %00000000
TRISE = %00000000
TRISF = %00000000
TRISG = %00000000
TRISH = %00000000
TRISJ = %00000000
'
' -----[ IDENTIFY ]------------------------------------------------------------
'
lcdout $fe,$80, "WVL Vdrive_03"
lcdout $fe,$c0, "PIC18F8722"
pause 3000
lcdout $fe,1
' ************************************************** ***************************
' * *
' * ESTABLISH COMMS WITH VDRIVE *
' * *
' ************************************************** ***************************
Begin:
Debug "VDrive2 Starting",13
Pause 200
Debug "Attempting synchronisation:"

Syncro:
Pause 200
toggle Heartbeat
X=X+1
debug " ",dec3 X
SerOut2 TX,84,["E",13] ' Send sync command folowed by CR
SerIn2 RX,84,200,Toolong,[Echo] ' Wait 200mS for reply 'E'
if Echo = "E" then Comms_Good

Toolong:
GoTo Syncro

Comms_Good:
Debug 13,"Disk synchronised",10,13 ' Show SUD has been sent
pause 500
' ************************************************** ***************************
' * *
' * M A I N C O D E *
' * *
' ************************************************** ***************************
gosub Check_disk
gosub Get_version
gosub File_list
gosub File_name_size ' File size & name before FOR...NEXT loop
gosub Free_space
gosub File_delete
pause 100
gosub File_open ' Create the file "TestFile.csv" (XL type)
' When read becomes "TESTFILE.CSV"
for X=1 to 17
toggle Heartbeat
debug "Writing to file X: ",dec3 X,13
SerOut2 TX,84,["WRF ",0,0,0,4,13] ' 32 bit number of bytes
serout2 Tx,84,[dec3 x,10,13] ' Number of bytes must be exact
next X ' Additional CR puts XL numbers into a column

gosub File_close

gosub File_size ' File size after FOR...NEXT loop
' Read the file size and send first byte to LCD
serout2 Tx,84,["IPA",13] ' Switch to ASCII characters for display of HEX digits
pause 100 ' Essential to wait after o/p switch
serout2 Tx,84,["DIR TestFile.csv",13] ' Issue the command
serin2 Rx,84,[Wait ("TESTFILE.CSV $"),_
hex2 A,skip 2,hex2 B,skip 2,hex2 C,skip 2,hex2 D]
lcdout $fe,$80, "$: ",hex2 A," ",HEX2 B," ",HEX2 C," ",HEX2 D
pause 100

debug "All done",10,13
end

' ************************************************** ***************************
' * *
' * S U B R O U T I N E S *
' * *
' ************************************************** ***************************
Check_disk:
for X=0 to 49 : DataIn[X]=0 : NEXT X ' Clear the array
serout2 tx,84,[13] ' Issue the command. "D"= Disk Ok, "N"=no disk
serin2 Rx,84,[str DataIn\1] ' Read 1 character
If DataIn[0] = "N" then
debug "No disk detected "
pause 500
goto Check_disk
else
debug "Disk found OK",10,13
endif
pause 100 ' These pauses are essential
return
Get_version:
debug "Firmware version:"
for X=0 to 49 : DataIn[X]=0 : NEXT X ' Clear the array
serout2 tx,84,["FWV",13] ' Issue the command
serin2 Rx,84,[str DataIn\50\">"] ' Read characters until ">" is found
debug str DataIn\50 ' Send data to the PC
pause 100 ' These pauses are essential
return

File_list:
debug 13,10,10,"List all files:"
for X=0 to 49 : DataIn[X]=0 : NEXT X ' Clear the array
serout2 Tx,84,["DIR",13] ' Issue the command
serin2 Rx,84,[str DataIn\50\">"] ' Read up to max of 50 chrs
debug str DataIn\50
pause 100
return

File_name_size:
debug 13,10,10,"File name & size:"
serout2 Tx,84,["IPA",13] ' Switch to ASCII characters for display of HEX digits
pause 10 ' Essential to wait after o/p switch
for X=0 to 49 : DataIn[X]=0 : NEXT X ' Clear the array
serout2 Tx,84,["DIR TestFile.csv",13] ' Issue the command
serin2 Rx,84,2000,Jump2,[str DataIn\50\">"]' Read up to max of 50 chrs
Jump2: ' Needed in case there is no file on the drive
debug str DataIn\50
pause 100
return

File_size:
debug 13,10,10,"File size:",13
serout2 Tx,84,["IPA",13] ' Switch to ASCII characters for display of HEX digits
pause 10 ' Essential to wait after o/p switch
for X=0 to 49 : DataIn[X]=0 : NEXT X ' Clear the array
serout2 Tx,84,["DIR TestFile.csv",13] ' Issue the command
serin2 Rx,84,2000,Jump3,[Wait ("TESTFILE.CSV "), str DataIn\50\">"]
' Read up to max of 50 chrs
Jump3: ' Needed in case there is no file on the drive
debug str DataIn\50,10,13
serout2 Tx,84,["IPH",13] ' Switch to HEX characters
pause 100
return


Free_space
debug 13,10,10,"Free space:",13
serout2 Tx,84,["IPA",13] ' Switch to ASCII characters for display of HEX digits
pause 10 ' Essential to wait after o/p switch
for X=0 to 49 : DataIn[X]=0 : NEXT X ' Clear the array
serout2 Tx,84,["FS",13] ' Return 4 bytes
serin2 Rx,84,[str DataIn\50\">"] ' Read up to max of 50 chrs
debug str DataIn\50
serout2 Tx,84,["IPH",13] ' Switch to HEX characters
pause 100
return

File_open:
debug "Opening file",10,13
serout2 Tx,84,["OPW TestFile.csv",13] ' Issue the command
return
File_close:
debug 10,"Closing file",10,13
SerOut2 TX,84,["CLF TestFile.csv",13] ' Issue the command
return

File_delete:
debug 13,10,10,"Deleting file",10,13
serout2 tx,84,["DLF TestFile.csv",13] ' Issue the command
return