!-----------------------------------------------------------------------
!  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
! Test          : Read Sync Serial RGB
!-----------------------------------------------------------------------
! 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 rgb (Red, Green, Blue) data of the LED under test.
!
!-----------------------------------------------------------------------
!
!*******************************************************************************
!      Synchronous Serial Model for the Feasa LED Analyser
!
!Note1: The Fiber Number to read is passed in using the Array Fibernum(*)
!      The measured data is passed out to the testplan using the array Data(*)
!
!Note2: 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

!
!To Read the RGB data do the following
! - Initialise all the inputs
!       - Set Addr_0 to [0]
!       - Set Ser_clk to [0]
!       - Set Ser_in     to [0]
! - 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 Read Red Green Blue (0X30) to the Led Analyser
! - Send the Fiber number to be read to the Led Analyser
! - Read back 6 Bytes of 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

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 change the test exposure
! Send 01000000 to LA
sub Set_exposure
   execute Addr_0_high             !Chip Select = 1
   execute Clock_0
   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_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
end sub

!Command to read RGB and Int
!Send 00110000 (0X30) to LA
sub Read_rgb_int
   execute Addr_0_high             !Chip Select = 1
   execute Clock_0
   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_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_0                    !0
   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

!---------------------------------------------------------------------
! This sends the read Hue Sat and Int Command to the Led Analyser
!---------------------------------------------------------------------
unit "Send Command Read Hue Sat Int"
   call Read_rgb_int      !Send Read HUE Sat and Int - Code 0x30
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 performs calculations
end unit

!---------------------------------------------------------------------------
!      Unit to read RGB and Int data
!-----------------------------------------------------------
! 6 Bytes of data are read back.
! 1 = Red
! 2 = Green
! 3 = Blue
! 4 = Int#1
! 5 = Int#2
! 6 = Int#6
! Int = (Int#1 * 65536) + (Int#2 * 256) + Int#3
!-----------------------------------------------------------
unit "Read RGB and Int"
!   execute Clock
!   execute Clock
   repeat 6 times
    call Read_Capture
   end repeat
fetch Data(*)
end unit

!!-----------------------------------------------------------
!
! End of test
!

