#include "common.h"
#include "spi.h"

/**
 * Configure SPI0 for 8-bit, 400KHz SCK, Master mode, polled operation, data
 * sampled on 1st SCK rising edge.
 * 
 */
void spi_init(){
  /*
   * data sampled on rising edge, clk active low,
   * 8-bit data words, master mode;
   */
  SPI0CFG = 0x70;
  
  // 4-wire mode; SPI enabled; flags cleared
  SPI0CN = 0x0F;
  
  // SPI clock = 49M / (2 * (1 + 1)) = 12.25M
  SPI0CKR = 1;
}

void spi_init_slave(){
  /*
   * data sampled on rising edge, clk active low,
   * 8-bit data words, slave mode;
   */
  SPI0CFG = 0x30;

  // 4-wire slave mode; flags cleared
  SPI0CN = 0x04;
  
  SPIEN = 1; // SPI enabled

  ESPI0 = 1;
  PSPI0 = 1;
}

void spi_send_8clock(){
  SPI0DAT = 0xFF;
  while(!SPIF);
  SPIF = 0;
}

/**
 * Function sends one byte to spi and reads ony byte from spi
 * it will be written with SYSCLK as clk
 * 
 * @param byte value to write
 * @return SPI byte
 */
unsigned char spi_write_read_spi(unsigned char byte){
  SPI0DAT = byte;
  while(!SPIF);                      
  SPIF = 0;
  return SPI0DAT;
}

void spi_read(unsigned char * pchar, unsigned int length){
  while(length--){
    SPI0DAT = 0xFF;
    while(!SPIF);
    SPIF = 0;
    *(pchar++) = SPI0DAT;
  }
}

void spi_write(unsigned char * pchar, unsigned int length){
  while(length--){
    SPI0DAT = *(pchar++);
    while(!SPIF);
    SPIF = 0;
  }
}

