/*
   * testspi.c
   * PC le 15/05/2003
   *
   * Ce programme teste la liaison SPI.
   * Chaque caractere frappe est envoye sur la liaison SPI et l'UART
   *
   */

  #include <pic.h>
  #include <stdio.h>
  #include "delay.h"
  #include "kbd.h"
  #include "sci.h"
  #include "spi.h"




  __CONFIG( XT & UNPROTECT & BORDIS & WDTDIS & PWRTDIS );

  #define XTAL        6000000     // crystal frequency - 6MHz

  /*  Calculate T2PRELOAD value for Timer2
   *  Setup the frequency you wish to get. Chose Prescaler and a postscaler then 
   *  calculate Preload. Check That Preload is not upper than 0xFF
   *  and not too low. Change prescale as possible.
   *  ATTENTION!
   *  Postscaller is used only for interrupt flag. Leage at 1 if you use Timer2
   *  to drive SSP.
   */

  #define FRQTMR2       40000     // Timer2 output frequence 40KHz for SPI at 20KHz
  #define T2PRESCALE        0     // prescale by 1
  //#define   T2PRESCALE        1     // prescale by 4
  //#define   T2PRESCALE        2     // prescale by 16
  #define T2POSTSCALE       0     // postscale by 1
  //#define   T2POSTSCALE       1     // postscale by 2
  //#define   T2POSTSCALE       2     // postscale by 3
  //#define   T2POSTSCALE       3     // postscale by 4
  //#define   T2POSTSCALE       4     // postscale by 5
  //#define   T2POSTSCALE       5     // postscale by 6
  //#define   T2POSTSCALE       6     // postscale by 7
  //#define   T2POSTSCALE       7     // postscale by 8
  //#define   T2POSTSCALE       8     // postscale by 9
  //#define   T2POSTSCALE       9     // postscale by 10
  //#define   T2POSTSCALE       10    // postscale by 11
  //#define   T2POSTSCALE       11    // postscale by 12
  //#define   T2POSTSCALE       12    // postscale by 13
  //#define   T2POSTSCALE       13    // postscale by 14
  //#define   T2POSTSCALE       14    // postscale by 15
  //#define   T2POSTSCALE       15    // postscale by 16

  #define T2PRELOAD   ((XTAL>>(2+(T2PRESCALE*2)))/FRQTMR2)

  // works only for 0,...F values.
  unsigned char convHex(unsigned char abyte) {
    if (abyte < 0x0a) {
      return abyte + 0x30;
    } else {
      return abyte + 0x37;
    }
  }

  void putHex(unsigned char ch) {
    putch(convHex(ch>>4));
    putch(convHex(ch&0xf));
  }


  void main(void) {
      char k;

      TRISA=0;        /* debug */
      PORTA=255;

      TRISC=0b10010000;   // C0:  SCL (i2c)
                          // C1:  SDA (i2c)
                          // C2:  clkf
                          // C3:  SCK (SPI)
                          // C4:  SDI (SPI)
                          // C5:  SDO (SPI)
                          // C6:  Tx  (UART)
                          // C7:  Rx  (UART)

      //
      // initialize Timer2
      //
      // Setup_Timer_2 (T2_DIV_BY_1,SPI_PERIODE,1);   // génère la fréquence 40KHz pour SPI 20KHz
      // Setup the 8-bit period register
      PR2 = T2PRELOAD;
      // Setup postscaler, enable the timer and setup the prescaler
      T2CON = (T2POSTSCALE<<3) |(1<<2) | T2PRESCALE;

      init_uart();

      spi_init(SPI_MASTER | SPI_L_TO_H | SPI_CLK_T2, SPI_SAMPLE_AT_END | SPI_XMIT_L_TO_H);

      while (1) {
          k = kbd_Getc();
          if(k != 0) {
              PORTA = k;
              spi_write(k);
              putch(k);
              putHex(spi_read());
          }
      }
  }