----------------------------------------------------
--Pheripheral Pin Select (PPS) configuration Library
----------------------------------------------------


------------------------------------------------------------------------
--Enable PPS register writes
--input: bit (PPS_enabled / PPS_disabled)
------------------------------------------------------------------------
; assuming CONFIG3H IOL1WAY = 0, else
; once PPSCON_IOLOCK = unlock it can be locked untill RESET
const bit lock = 1
const bit unlock = 0

-- disable interrupts if necessary before unlock
-- enable interrupts after locking
procedure PPS_write_control (bit in control) is
; this routine should be fast, Microchip suggest assembler,
; but works as is on PIC18F44J11 at 20/40MHz
pragma inline
asm bank PPSCON ; PPSCON is in bank14
EECON2    = 0x55      -- \ required sequence
EECON2    = 0xAA      -- /
PPSCON_IOLOCK = control
end procedure

procedure _PPS_write_control(bit in control) is

   asm bank PPSCON                              -- set bank of PPSCON
   if (control == enabled) then
      EECON2 = 0x55                             -- ) write ..
      EECON2 = 0xAA                             -- ) .. enable
      PPSCON_IOLOCK = 0                  -- allow writes
   else
      EECON2 = 0x55                             -- ) write ..
      EECON2 = 0xAA                             -- ) .. enable
      PPSCON_IOLOCK = 1                 -- deny writes
   end if

end procedure



-----------------------------------------------------------------------
--Selectable input sources
-----------------------------------------------------------------------
;INT1  = RPINR1
;INT2  = RPINR2
;INT3  = RPINR3
;T0CKI = RPINR4
;T3CKI = RPINR6
;CCP1  = RPINR7
;CCP2  = RPINR8
;T1G   = RPINR12
;T3G   = RPINR13
;RX_DT2= RPINR16
;CK2   = RPINR17
;SDI2  = RPINR21
;SCK2IN= RPINR22
;SS2IN = RPINR23
;FLT0  = RPINR24

-----------------------------------------------------------------------
--Selectable output sources
-----------------------------------------------------------------------
const byte NULL = 0      ; asigned to all RPn output at RESET, disables RPn output function
const byte C1OUT = 1     ; comparator 1 output
const byte C2OUT = 2     ; comparator 2 output
const byte TX2_CK2 = 5   ; EUSART2 asynchronous (TX or clock output)
const byte DT2 = 6       ; EUSART2 synchronous transmit
const byte SDO2 = 9      ; SPI2 data output
const byte SCK2 = 10     ; SPI2 clock output
const byte SSDMA = 12    ; SPI DMA slave select
const byte ULPOUT = 13   ; ultra low power wake up event
const byte CCP1_P1A = 14 ; ECCP1 compare or PWM output channel A
const byte P1B = 15      ; ECCP1 compare or PWM output channel B
const byte P1C = 16      ; ECCP1 compare or PWM output channel C
const byte P1D = 17      ; ECCP1 compare or PWM output channel D
const byte CCP2_P2A = 18 ; ECCP2 compare or PWM output channel A
const byte P2B = 19      ; ECCP2 compare or PWM output channel B
const byte P2C = 20      ; ECCP2 compare or PWM output channel C
const byte P2D = 21      ; ECCP2 compare or PWM output channel D

-----------------------------------------------------------------------
--Reconfigurable pins
-----------------------------------------------------------------------
const byte RP0 = 0
const byte RP1 = 1
const byte RP2 = 2
const byte RP3 = 3
const byte RP4 = 4
const byte RP5 = 5
const byte RP6 = 6
const byte RP7 = 7
const byte RP8 = 8
const byte RP9 = 9
const byte RP10 = 10
const byte RP11 = 11
const byte RP12 = 12
const byte RP13 = 13
const byte RP14 = 14
const byte RP15 = 15
const byte RP16 = 16
const byte RP17 = 17
const byte RP18 = 18
const byte RP19 = 19
const byte RP20 = 20
const byte RP21 = 21
const byte RP22 = 22
const byte RP23 = 23
const byte RP24 = 24


-----------------------------------------------------------------------
-- example of use, remove those comments ;
-----------------------------------------------------------------------
;include 18f44j11 ; target PICmicro
;include pps      ; pin assignation library


;PPS_write_control (unlock) ; unlock
--assigning an output pin:
;RPOR20 = CCP1/P1A     ; PWM output channel A routed to pin_RP20
--assigning an input pin:
;RPINR16 = RP0         ; RX/DT2 input routed to pin_RP0
;PPS_write_control (lock) ; lock

;pragma target clock 40_000_000     -- oscillator frequency
-- configuration memory settings (fuses)
;pragma target OSC  HS_PLL          -- HS crystal PLL enabled
;OSCTUNE_PLLEN = 1                  -- turn on the PLL, 10MHz x 4 = 40MHz
;pragma target WDT  disabled        -- no watchdog


;enable_digital_io()                -- make all pins digital I/O


