-- Title: tstRF12main.jal  Test program for communication via radio link using
--                                                                   HOPE RF12B
-- Author H.v.Broekhuyzen (henk@henkzelf.nl)
-- Compiler: >=2.4m
--
-- This file is part of jallib  (http://jallib.googlecode.com)
-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php)
--
-- Description: This files forms, together with tstRF12com.jal, tstRFsub.jal
--              and T0comInIsr.jal the test- and experiment- framework for
--              the files: spi_rf12b.jal and spi_sw_master.jal
--              For Hardware etc. see: tstRF12B.doc
--

--*********************************************************************
--               General library includes and pragma part            --
--*********************************************************************
include 16f876
;include 16f73

pragma target clock 20_000_000                  -- xtal frequency
pragma target OSC        hs
; pragma target   LVP disabled
pragma target   WDT disabled
enable_digital_io()
-- setup serial (see echo.jal for more details) ;@jallib section serial
const serial_hw_baudrate = 115_200
const USART_HW_Serial     = true     ;true = RS232, false = SPI
include serial_hardware
serial_hw_init()
include format
include print
include delay

--******************************************************************************
--* CONFIFURATION CONSTANDS (used by compiler to generate the required version)
--******************************************************************************
const byte CFG_ControllerID = "a" ; Fist character in a command.
                                  ; Must be different for each module
const byte CFG_ProgramVersion = 0 ; 0: The Echo test version is generated
                                  ;    WITH CRC generation and check in
                                  ;    spi_rf12b.jal
                                  ; 1: WITHOUT CRC generation and check
const byte CFG_VerboseStatus  = 1 ; 0: Commands are not echoed to the
                                  ;    master-controller (if computer)
                                  ; 1; Commands are echoed
                                  ;    But not while another command executes.
                                  ;    Might be usefull when you type commands
-- define Command_Auto_start == command executed after power-up (H = none )
--                                               and the corresponding value
if CFG_ControllerID == "a" then
  const byte Command_Auto_start = "e"   ; master echo-test
  const sword Value_Auto_start  = 0
elsif CFG_ControllerID == "b"  then
  const byte Command_Auto_start = "E"   ; slave echo-test
  const sword Value_Auto_start  = 0
else
  const byte Command_Auto_start = "H"
  const sword Value_Auto_start  = 0
end if

--*****************************************************************************
--*                     IO definition and In/out settings                     *
--*****************************************************************************
;                                   ; MasterClearNot/THV (yellow)      ; pin 1
alias LED1            is pin_a0  pin_a0_direction = output             ; pin 2
alias LED2            is pin_a1  pin_a1_direction = output             ; pin 3
alias LED3            is pin_a2  pin_a2_direction = output             ; pin 4
alias LED4            is pin_a3  pin_a3_direction = output             ; pin 5
;                     is pin_a4  pin_a4_direction =                    ; pin 6
;                     is pin_a5  pin_a5_direction =                    ; pin 7
;                                                            VSS       ; pin 8
;                                                            OSC1      ; pin 9
;                                                            OSC2      ; pin 10
;                     is pin_c0  pin_c0_direction =                    ; pin 11
;                     is pin_c1  pin_c1_direction =                    ; pin 12
alias SPI_RF12B_CS_n  is pin_c2  pin_c2_direction = output ;chip-sel_not pin 13
alias SSPI_SCK        is pin_c3  pin_c3_direction = output ;SPIclock   ; pin 14
alias SSPI_DI         is pin_c4  pin_c4_direction = input  ;SPIdata-in ; pin 15
alias SSPI_DO         is pin_c5  pin_c5_direction = output ;SPIdata-out; pin 16
;           TX_BUS    is pin_c6                            ; TX_BUS    ; pin 17
;           RX        is pin_c7                            ; RX        ; pin 18
;                                                          ; VSS       ; pin 19
;                                                          ; VDD       ; pin 20
alias Echotest_Normal is pin_b0 pin_b0_direction = input ; for Com. e+E; pin 21
;                     is pin_b1 pin_b1_direction =                     ; pin 22
;                     is pin_b2 pin_b2_direction =                     ; pin 23
;                     is pin_b3               ; Program (white)        ; pin 24
;                     is pin_b4  pin_b4_direction =                    ; pin 25
;                     is pin_b5  pin_b5_direction =                    ; pin 26
;                     is pin_b6             ; Program clock (green)    ; pin 27
;                     is pin_b7             ; Program data (blue)      ; pin 28
--************************************************************************
-- Initialise IO (if not OK by reset)
SPI_RF12B_CS_n = 1
-- ***********************************************************************
-- *                Variables read and writen by routines                *
-- ***********************************************************************
const byte MaxPayloadSize = 32
var byte RF12BpacketToTransmit[MaxPayloadSize]
var byte RF12BpacketReceived[MaxPayloadSize] at RF12BpacketToTransmit
var word Reply_delay = 5     ; delay (ms) in command E (change with command W)
var word Loop_delay  = 1000  ; delay in command e and T (change with command w)
var word TimeOutTicks = 2500 ; T0 ticks (of 1/5 ms) before loop in command e
                             ; that calles ReceivePackage(xx) stops
                             ; Changable via command t. (t has units of 1 ms)
var sword LengthToSend = MaxPayloadSize /2  ; Numers of characters send by
                             ; command e via RF12BpacketToTransmit[]
                             ; This can be changed by command l (lowercase L)
                             ; If this value <= 0 it is a 'signal' (only this
                             ; length is transmitted.
                             ; Markt that SPI_RF12B_receive_packet() can
                             ; cause signal -1 and -2, and the testprogram
                             ; can cause -3
var byte  PrintInEchotestMask = 0xFF; Controls what is printed during the
                             ; Echo test (command e transmit to command E
                             ; on the other PIC.
                             ; This value can be changed via command p
  ; if the bit with value 1 is set, command E will print
     ; the  number of bytes in the received package (or if negative the signal)
  ; if the bit with value 2 is set, command E will print the
                             ; payload in the received package.
  ; if the bit with value 16 is set, command e will print the
     ; the  number of bytes in the echoed package (or if negative the signal)
  ; if the bit with value 32 is set, command e will print the
                             ; time (t1) spent to transmit the package
  ; if the bit with value 64 is set, command e will print the
      ; time (t2) from the end of transmit to the end of receiving the echo
  ; if the bit with value 128 is set, command e will print the
      ; time (t3) from the beginning of transmit to the end of receiving.
  ; bits with value 4 and 8 do not care for the moment.
  ; So when PrintInEchotestMasK = 0xFF (255) everything is printed but t2
                                              ;   includes some printing time
                     ; Rrinting of the payload by command E will also delay
                     ; the transmission of the echoed package.
  ; For evaluation of the timing: set PrintInEchotestMasK to 96 (= 32 + 64)
  ; Mark that during the echotest success and failing are also indicated by
  ;                  ; LED2 and LED3

-- Next 3 var's are filled by interrupt routine
var byte  CommandIn = Command_Auto_start
var sword ComValIn  = Value_Auto_start
var byte  ComFlag   =  1


--*************************************************************************
--*                 More Includes and Initialisation                      *
--*************************************************************************
include SPI_sw_master
spi_master_init(1,0)

-- defines to control how the compiler will generate code from SPI_RF12B.jal
const bit spi_rf12b_debug   = true
if CFG_ProgramVersion == 0 then
   const bit spi_rf12b_use_CRC = true
end if
include SPI_RF12B
include T0comInISR
TMRs_init()
include tstRF12sub


--*************************************************************************
--*                         Main program                                  *
--*************************************************************************

-- blink a little to tell the world we're up and all LEDs working.
-- In the meantime the RF12B powers up (needs > 100 ms)
for 5  loop
   LED1 = on
   LED2 = on
   LED3 = on
   LED4 = on
   delay_1ms(150)
   LED1 = off
   LED2 = off
   LED3 = off
   LED4 = off
   delay_1ms(150)
end loop

   LED2 = on
   LED3 = on
SPI_RF12B_Init()  ; not earlier, at least 100 ms after Power_Up.
   LED2 = off
   LED3 = off
; Next 3 statements you can use if you want your own settings.
; The values set by SPI_RF12B_Init() are shown.
; SPI_RF12B_set_Frequency(1600)
; SPI_RF12B_set_Bandwidth(4)
; SPI_RF12B_set_Datarate(71)
----------------------------------------forever loop ------------------
forever loop
  if ComFlag != 0 then
     LED1 = 1
     if CFG_VerboseStatus != 0 then
        if CommandIn != "H" then
          print_crlf(Serial_HW_data)
          serial_hw_write(CFG_ControllerID)
          serial_hw_write(CommandIn)
          format_sword_dec(Serial_HW_data ,ComValIn,6,0 )
          print_crlf(Serial_HW_data)
       end if
     end if
     include tstRF12com
     LED1 = 0
  end if
end loop

