!-----------------------------------------------------------------------
!  Copyright (c) 2020 Feasa Enterprises Ltd.
!  Rev 3.0  August 2020
!
!  All Rights Reserved.  Reproduction, adaptation, or translation
!  without prior written permission is prohibited, except as allowed
!  under the copyright laws.
!
!-----------------------------------------------------------------------
!
! Device        : Feasa_20i
! Manufacturer  : Feasa Enterprises Ltd.
! Description   : 20 Channel device to interrogate a LED
! Package       : 20 pin
! Test Platform : 3070
! Safeguard     : cmos_3v3
!
!-----------------------------------------------------------------------
! WARNING:  This is a Led Analyser function referenced by a PDL.
!
!   This test is not intended to be used directly for a Led analyser.
!   Instead, a part description library for the Led analyser should be
!   used where the part description language of that file refers to
!   this file as the part number of one of it's several "sub-components" .
!   Notice also that this test uses signal names rather than ordinal
!   pin numbers.  The referencing part description library should map
!   the device's actual pin numbers to these signal names.
!
!
! Additional Comments:
!
!   This test outputs the Intensity stored under a Fiber Number and Sequence    !   capture.    It uses the synchronous serial bus of the LED Analyser
!
!-----------------------------------------------------------------------
!
!*******************************************************************************
!      Synchronous Serial Model for the Feasa LED Analyser
!
!      The Fiber Number to read is passed in using the Array Fibernum(*)
!      The Sequence Capture Number is passed in the array Data(*)
!      The measured data is passed out to the testplan using the array Data(*)
!
!Note: All commands sent to the Led Analyser use the following format
! To send a command
!       - Set Ser_in to MSB
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Set Ser_in to Bit 6
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Continue until all 8 bits have been clocked into the Led Analyser

! To read a byte
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Read Ser_out to MSB
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Read Ser_out Bit 6
!       - Continue until all 8 bits have been read from the Led Analyser

! - Reset the Synchronous serial communications by
!       - Set Addr_0 to [0]
!       - Set Ser_in     to [1]
!       - Execute Ser_clk [1] then Ser_clk[0] at leaset 8 times
! - Send the command (0X91) to read a single Intensity sequence for a Fiber
! - Send the Fiber number and sequence capture number to be read to the Led Analyser
! - Read back 3 Bytes of Intensity data from the Led Analyser

test digital; Fibernum(*),Data(*)
sequential

test time 2000m

vector cycle  20u
receive delay 19u

set terminators to on
assign Ser_out            to pins "Ser_out"
assign Ser_in             to pins "Ser_in"

assign Ser_clk            to pins "Ser_clk"

assign GND1               to pins "GND1"

assign Reset              to pins "Reset"

assign OE_bar             to pins "OE_bar"
assign Ext_trig           to pins "Ext_trig"

assign PWM                to pins "PWM"

assign Addr_0             to pins "Addr_0"
assign Addr_1             to pins "Addr_1"
assign Addr_2             to pins "Addr_2"
assign Addr_3             to pins "Addr_3"
assign Addr_4             to pins "Addr_4"

assign WE_bar             to pins "WE_bar"
assign Int                to pins "Int"
assign Color              to pins "Color"
assign Sat                to pins "Sat"

assign RyBy_bar           to pins "RyBy_bar"

assign VCC                to pins "VCC"
assign GND                to pins "GND"

power  VCC, GND, GND1

family   CMOS_3V3

inputs   Ser_in, Ser_clk, Addr_0
outputs  Ser_out, RyBy_bar

nondigital Int, Color, Sat, OE_bar, Ext_trig, PWM
nondigital Addr_1, Addr_2, Addr_3, Addr_4, WE_bar, Reset

!********** assignments **********************

variable Fibernum(*) to groups Ser_in

variable Data(*) to groups Ser_in

capture Ser_out pack 8    !8 data bits in each byte read back from Analyser

!*******************************************************************
!*******************      VECTOR SECTION       *********************
!*******************************************************************

vector  Set_values         !set LED Analyser to known state
   set Addr_0              to "0"
   set Ser_clk             to "0"
   set Ser_in              to "0"
end vector

vector Keep_values        !Save input values.
   set Addr_0              to "k"
   set Ser_clk             to "k"
   set Ser_in              to "k"
end vector


vector Addr_0_high
   initialize to Keep_values
   set Addr_0          to "1"
end vector

vector Addr_0_low
   initialize to Keep_values
   set Addr_0          to "0"
end vector

vector Clock
   initialize to Keep_values
   set Ser_clk          to "t"
end vector

vector Clock_1
   initialize to Keep_values
   set Ser_clk          to "1"
end vector

vector Clock_0
   initialize to Keep_values
   set Ser_clk          to "0"
end vector

vector Start_bit
   initialize to Keep_values
   set Ser_in               to "1"
end vector

vector Ser_in_X
   initialize to Keep_values
   set Ser_in          to "x"
end vector

vector Ser_in_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector In_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector Ser_in_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector In_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector Ser_out_x
   initialize to Keep_values
   set Ser_out         to "x"
end vector

vector Ser_out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Ser_out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector

vector Out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector



!*******************************************************************
!*******************    SUBROUTINE SECTION     *********************
!*******************************************************************

! Command to reset or initialize Communications
! Send 11111111 (FF) to LA
sub Reset_Comms
   execute Addr_0_low              !Chip Select = 0
   execute Clock_0
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
end sub


!Command to read Intensity for Fiber@@ Seq Num ###
!Send 10010001 (0x91) to LA
sub Get_Intensity_0x91
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_1                    !MSB = 1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock

end sub

! Read 8 data bits from Ser_out pin
! The pack 8 statement has been used so the 8 data bits will
! be read back as one Byte
sub Read_Capture
   repeat 8 times
     execute Clock
     execute Clock
     execute Ser_out_x capture   !Captures a bit will be put in array using fetch command
   end repeat
end sub



!*******************************************************************
!*********************      UNIT SECTION       *********************
!*******************************************************************


!-------------------------------------------------------------------
!  Initialize all the inputs to a known value
!-------------------------------------------------------------------
unit "Initialize All"
    execute Set_values
end unit
!-------------------------------------------------------------------



!-------------------------------------------------------------------
!  Clear the input buffer of the Led Analyser to
!  ensure that the led analyser has detected no false
!  clock inputs
!-------------------------------------------------------------------
unit "Reset Communications"
        call Reset_Comms                 !Send Reset Comms Code x0FF
        wait 100u
end unit
!-------------------------------------------------------------------
!Send Command 0x91 to get intensity data for fiber@@ and Seq Num ####
!-------------------------------------------------------------------

unit "Get Intensity"
   call Get_Intensity_0x91      !Send Code 0x91
end unit

!--------------------------------------------------------------------
!   Unit to Determine the fiber number to read
!--------------------------------------------------------------------------
!   The Fiber number is passed into the test using the Array Fibernum(*)
!   The MSB is passed in first so the format is as follows
!   Fibernum(0) = MSB (Bit7)
!   Fibernum(1) = Bit6
!   Fibernum(2) = Bit6
!   Fibernum(3) = Bit6
!   Fibernum(4) = Bit6
!   Fibernum(5) = Bit6
!   Fibernum(6) = Bit6
!   Fibernum(7) = LSB (Bit0)
! i.e. to read fiber 1 set Fibernum(7) to 1 and all others to 0
!----------------------------------------------------------------------
unit "Fiber No to read"
   execute Ser_in_x drive Fibernum(0)    !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(1)    !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(2)    !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(3)    !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(4)    !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(5)    !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(6)    !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Fibernum(7)    !Bit0
   execute Clock
   execute Clock
   wait 20m        !need to wait 20ms as the Led Analyser is performing calculations
end unit

unit "Capture No to read"            ! 2 Bytes - up to 400 Captures allowed
   execute Ser_in_x drive Data(0)    !Bit 15 MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(1)    !Bit 14
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(2)    !Bit 13
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(3)    !Bit 12
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(4)    !Bit 11
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(5)    !Bit 10
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(6)    !Bit 9
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(7)    !Bit 8
   execute Clock
   execute Clock

   execute Ser_in_x drive Data(8)    !Bit 7
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(9)    !Bit 6
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(10)    !Bit 5
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(11)    !Bit 4
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(12)    !Bit 3
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(13)    !Bit 2
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(14)    !Bit 1
   execute Clock
   execute Clock
   execute Ser_in_x drive Data(15)    !Bit 0
   execute Clock
   execute Clock

   wait 20m  ! need to wait 20ms as the Led Analyser is performing calculations
end unit

!------------------------------------------------------------
!---------------------------------------------------------------------------
!      Unit to read Int data
!------------------------------------------------------------
! 3 Bytes of data are read back.
! 1 = Int#1
! 2 = Int#2
! 3 = Int#3
! Int = (Int#1 * 65536) + Int#2 * 256 + Int#3
!------------------------------------------------------------
unit "Read Back Int"

   repeat 3 times    !read 3 bytes
     call Read_Capture
   end repeat

fetch Data(*)  ! puts measured values in the array

end unit
!------------------------------------------------------------


! End of Test

