
'pic32 programmer
'-------
'set variables
'=======


  pgd = 14         'pin no. for data i/o connect to target pin 4
  clk = 21        'pin no. for clock connect to target pin 21
  mclr = 15        'pin for /reset target pin 1

'5 bit MTAP
MTAP_COMMAND$ = "07" 'TDI and TDO connected to MCHP Command Shift register (See Table 19-2)
MTAP_SW_MTAP$ = "04" 'Switch TAP controller to MCHP TAP controller
MTAP_SW_ETAP$ = "05" 'Switch TAP controller to EJTAG TAP controller
MTAP_IDCODE$ =  "01" 'Select Chip Identification Data register

'8 bit MCHP
MCHP_STATUS$ = "00" 'NOP and return Status.
MCHP_ASSERT_RST$ = "D1" 'Requests the reset controller to assert device Reset.
MCHP_DE_ASSERT_RST$  = "D0" 'Removes the request for device Reset, which causes the reset
'controller to de-assert device Reset if there is no other source
'requesting Reset (i.e., MCLR).
MCHP_ERASE$ = "FC" 'Cause the Flash controller to perform a Chip Erase.
MCHP_FLASH_ENABLE1$  = "FE" 'Enables fetches and loads to the Flash (from the processor).
MCHP_FLASH_DISABLE1$ = "FD" 'Disables fetches and loads to the Flash (from the processor

'5 bit ETAP
ETAP_ADDRESS$ = "08" 'Select Address register.
ETAP_DATA$ = "09" 'Select Data register.
ETAP_CONTROL$ = "0A" 'Select EJTAG Control register.
ETAP_EJTAGBOOT$ = "0C" 'Set EjtagBrk, ProbEn and ProbTrap to 1 as the reset value.
ETAP_FASTDATA$ = "0E" 'Selects the Data and Fastdata registers.

  Dim byte, bit, tdi, tms, tdo, indata$, outdata$
  SetPin pgd, dout
  SetPin clk, dout
  SetPin mclr,dout

  Pin(pgd) = 0
  Pin(clk) = 0
  Pin(mclr) = 0: Pause 100
  Pin(mclr) = 1     'reset high then hold low
  Pin(mclr) = 0

'======================================================== enter programme mode
  Print "enter programming mode "
  ' output 32 bit sequence MSB first
  pgm$="01001101010000110100100001010000" 'PGM mode data sequence
  For x = 1 To Len(pgm$)
    bit =  Val(Mid$(pgm$,x,1))
    Pin(pgd)=bit
    Pin(clk)=1
    Pin(clk)=0
    Print bit;
  Next x
    Pin(mclr) = 1     'send reset pin high and hold.
Print: Print " MCLR (reset) to 'high' = (operate))"

'========================================================= Set Mode
  Print "Set Mode ";
  setmode("011111")
  Print outdata$

'========
Print "check device status ";

  SendCommand (5,MTAP_SW_MTAP$)
  SendCommand (5,MTAP_COMMAND$)
  XferData (8,MCHP_STATUS$)

  Print outdata$
' FCBUSY = 0 CFGRDY = 1



'========
End ' temporary end due to bugs
'=========
'=========
Sub setmode(st$)
  Local x, bit
  outdata$ = ""
  For x = Len(st$) To 1 Step - 1

    Pin(pgd) = 0      'TDI=0
    Pin(clk) = 1
    Pin(clk) = 0
    bit = Val(Mid$(st$,x,1)) 'TMS = Bit
    Pin(pgd) = bit
    Pin(clk) = 1
    Pin(clk) = 0
    Pin(clk) = 1   'dummy clock
    Pin(clk) = 0
    SetPin pgd , din
    Pin(clk) = 1   'TDO clock
    outdata$ = outdata$ + Str$(Pin(pgd))
    Pin(clk) = 0
    SetPin pgd, dout
  Next x
End Sub
'========
Sub sendcommand(size,st$)
  outdata$ = ""
  Local x, bit, TMS, binstr$
binstr$ = hextobin$(st$)

tdi = 0: tms = 1: GoSub clock
tdi = 0: tms = 1: GoSub clock
tdi = 0: tms = 0: GoSub clock
tdi = 0: tms = 0: GoSub clock

For x = Len(binstr$) To Len(binstr$) - size + 2 Step - 1
  tdi = Val(Mid$(binstr$,x,1)): tms = 0: GoSub clock
Next x
  tdi = Val(Mid$(binstr$,Len(binstr$) - size + 1,1)):tms = 1: GoSub clock
  tdi = 0: tms = 1: GoSub clock
  tdi = 0: tms = 0: GoSub clock
End Sub

'========

Sub xferdata(size,st$)
Local x
  outdata$=""
  indata$ = hextobin$(st$)
  tdi = 0: tms = 1: GoSub clock
  tdi = 0: tms = 0: GoSub clock
  tdi = 0: tms = 0: GoSub clock
  outdata$ = Str$(tdo) 'read lsb of outdata:? tdo;
  For x = Len(indata$) To (Len(indata$) - size + 2) Step - 1
    tdi = Val(Mid$(indata$,x,1)): tms = 0: GoSub clock
    outdata$ = Str$(tdo) + outdata$
  Next x
  tdi = Val(Mid$(indata$,Len(indata$) - size + 1,1)): tms = 1: GoSub clock
  tdi = 0: tms = 1: GoSub clock
  tdi = 0: tms = 0: GoSub clock
End Sub

'========

  'create 4 phase clock and read data out on pgd line
clock:
  Pin(pgd)=tdi
  Pin(clk)=1
  Pin(clk)=0
  Pin(pgd)=tms
  Pin(clk)=1
  Pin(clk)=0
  Pin(clk)=1  'dummy clock cycle
  Pin(clk)=0
  SetPin pgd,din
  Pin(clk)=1
  tdo=Pin(pgd)
  Pin(clk)=0
  SetPin pgd,dout
Return
'========
'Function hextodec(st$)

'Local x,y,hexbyte,char$
'hexbyte = 0
'For x = 1 To Len(st$)
'  char$=Mid$(st$,x,1)
'  y = Instr(1,"0123456789abcdef",char$)
'  hexbyte = hexbyte Or (y-1)
'  If x < Len(st$) Then hexbyte = hexbyte*16
'Next x
'hextodec = hexbyte
'End Function


'========
Function hextobin$(nibbles$)
Local bit,bin_count,dec_val,nibbles,byte$, n$,h$,z
For z = 1 To Len(nibbles$)
  n$=Mid$(nibbles$,z,1)

byte$=""
bin_count = 8
dec_val = Instr(1,"0123456789abcdef",n$)-1
For bit  = 1 To 4
  If dec_val >= bin_count  Then
    byte$ = Byte$ + "1"
    dec_val = dec_val - bin_count
  Else
    byte$ = byte$ + "0"
  EndIf
    bin_count =bin_count / 2
Next bit
h$ = h$ + byte$
Next z
hextobin$ = h$
End Function
'========                                                                                                                       