#Include #Include: "Spi.H" "Digitalio.H"
#Include #Include: "Spi.H" "Digitalio.H"
h"
#include "DigitalIO.h"
// Hardware
#include <xc.h>
#include <proc/p32mx170f256b.h>
#include <sys/attribs.h> // for ISR macros
#include "DigitalIO.h"//Hardware abstraction layer
#include "Hardware_Configure.h"
//Display Service
#include "DisplayService.h"
/*----------------------------- Module Defines ----------------------------*/
/***************************************************************************
Interrupt Service Routines
***************************************************************************/
void __ISR(_SPI1_VECTOR, IPL7SOFT) SPI_Handler(void){
volatile uint8_t thisByte;//Holds contents of RX buffer
volatile ES_Event_t thisEvent;//Will be posted if an update is required
// printf("\n\r");
// printf("%d ",thisByteArr[0]);
// printf("%d ",thisByteArr[1]);
// volatile uint8_t i = 0;
// for(i = 0; i<DATA_LENGTH; i++){
// printf("%d ", byteArray[i]);
// }
}
****************************************************************************/
void SPI_Init(uint32_t bitRate, uint8_t bitWidth){
/*MAKE SURE YOU CONFIGURE PINS AS INPUT/ OUTPUT BEFORE RUNNING SPI_INIT!*/
/*Calculate baud rate divisor to get our desired clock rate or below.
* Assumes PB clock (FPB) is 20MHz
*FSCK = FPB/(2*(BRG+1) = 20E6/(2*(999+1))*/
/* IF PBCLK is evenly divisible by desired freq*/
if(PBCLK%(2*bitRate)==0) SPI1BRG = PBCLK/(2*bitRate)-1;//Calculate BRG exactly
else SPI1BRG = PBCLK/(2*bitRate);//Step the speed down one notch
/*Calculate baud rate divisor to get our desired clock rate or below.
* Assumes PB clock (FPB) is 20MHz
*FSCK = FPB/(2*(BRG+1) = 20E6/(2*(999+1))*/
/* IF PBCLK is evenly divisible by desired freq*/
if(PBCLK%(2*bitRate)==0) SPI2BRG = PBCLK/(2*bitRate)-1;//Calculate BRG exactly
else SPI2BRG = PBCLK/(2*bitRate);//Step the speed down one notch
/*TURN IT ON!*/
SPI1CONbits.ON = 1;//Turn on SPI1
SPI2CONbits.ON = 1;//Turn on SPI2
/****************************************************************************
Function
SPI_Tx
Parameters
uint8_t data the 8-bit value to be sent out through the SPI
Returns
void
Description
write the data to the SPIxBUF and then wait for it to go out (SPITBF)
Notes
don't forget to read the buffer after the transfer to prevent over-runs
****************************************************************************/
void SPI_Tx(uint8_t data){
while(SPI2STATbits.SPITBF);//Wait while tx buffer is full
SPI2BUF = data;//Write data to SPI xmit buffer
while(!SPI2STATbits.SRMT);//Wait until shift register is empty
SPI2BUF;//Read buffer to clear/prevent over-runs
}
/****************************************************************************
Function
SPI_TxBuffer
Parameters
uint8_t *buffer, a pointer to the buffer to be transmitted
uint8_t length the number of bytes in the buffer to transmit
Returns
void
Description
loop through buffer calling SPI_Tx for each element in the buffer
Notes
****************************************************************************/
void SPI_TxBuffer(uint8_t *buffer, uint8_t length){
uint8_t index = 0;//Determines our position in buffer
while(index < length)//While we are in bounds of buffer array
{
SPI_Tx(*(buffer+index));//Transmit the index(th) element of buffer
index++;//Move on to next element
}
}