0% found this document useful (0 votes)
111 views6 pages

"Spi.H": Spi1 - Init Spi1 - TX Spi1 - Txbuffer

This module establishes a timer to poll OPAMP, TVS, and ZENER services to transmit ship and aiming data from these services to a display PIC through SPI1. It initializes SPI1 to transmit data at a bit rate of 2MHz with 8-bit width every 50ms. The module constructs a transmission array with data like joystick position, weapon levels, and ship position and transmits it via SPI1 on the timer timeout.

Uploaded by

api-552271981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views6 pages

"Spi.H": Spi1 - Init Spi1 - TX Spi1 - Txbuffer

This module establishes a timer to poll OPAMP, TVS, and ZENER services to transmit ship and aiming data from these services to a display PIC through SPI1. It initializes SPI1 to transmit data at a bit rate of 2MHz with 8-bit width every 50ms. The module constructs a transmission array with data like joystick position, weapon levels, and ship position and transmits it via SPI1 on the timer timeout.

Uploaded by

api-552271981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

/****************************************************************************

Module
SPI

Revision
1.0

Description
* This module establishes a timer and polls the OPAMP and TVS services to
* transmit ship + aiming data to the Display PIC through SPI1.
*
Author:
* Ethan Kurteff 5-20-21

Revised:
* Ethan Kurteff 5-21-21 | Swapped position of HULL and SHIELD in TxArray
******************************************************************************/
// This module
#include "SPI.h"

//OPAMP, TVS, ZENER


#include "OPAMP.h"
#include "TVS.h"
#include "ZENER.h"

// Hardware
#include <xc.h>
#include <proc/p32mx170f256b.h>
#include <sys/attribs.h> // for ISR macros

// Event & Services Framework


#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include "ES_Port.h"

/*----------------------------- Module Defines ----------------------------*/


#define PBCLK (uint32_t)20E6//PIC32 clock frequency

/*SPI Parameters*/
#define SPI_BIT_RATE 2E6
#define SPI_BIT_WIDTH 8
#define SPI_UPDATE_TIME 50//Time in mS between SPI transmissions to Disp. PIC
#define MESSAGE_LENGTH 16 //How long will our SPI Tx packet be in bytes?

/*----------------------------- Module Functions --------------------------*/


void SPI1_Init(uint32_t bitRate, uint8_t bitWidth);
void SPI1_Tx(uint8_t data);
void SPI1_TxBuffer(uint8_t *buffer, uint8_t length);

/*----------------------------- Module Variables --------------------------*/


static uint8_t MyPriority;

/*TxArray Holds contents to send to Display PIC. Structured as follows:


Byte: Contents: Possible Value(s):
0 Joystick X MSB 0-3
1 Joystick X LSB 0-255
2 Joystick Y MSB 0-3
3 Joystick Y LSB 0-255
4 Weapons Range 0-128
5 Hull Capacity 0-100
6 Shield Capacity 0-200
7 Weapons Range 0-128
8 Navigation Power 0-64
9 Ship X MSB (our ship) 0-255
10 Ship X LSB 0-255
11 Ship Y MSB 0-255
12 Ship Y LSB 0-255
*/
static uint8_t TxArray[MESSAGE_LENGTH];

/****************************************************************************
Function
InitSPItx
****************************************************************************/
bool InitSPItx(uint8_t Priority)
{
ES_Event_t ThisEvent;

MyPriority = Priority;

/*Configure SP1 in Master Mode*/


SPI1_Init(SPI_BIT_RATE, SPI_BIT_WIDTH);

// post the initial transition event


ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true){
return true;
}
else{
return false;
}
}

/****************************************************************************
Function
PostSPItx

****************************************************************************/
bool PostSPItx(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunSPItx
****************************************************************************/
ES_Event_t RunSPItx(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
switch (ThisEvent.EventType)
{
case ES_INIT:
{
ES_Timer_InitTimer(SPI_TIMER, SPI_UPDATE_TIME);//Start SPI timer
}
break;
case ES_TIMEOUT:
{
switch (ThisEvent.EventParam)
{
case SPI_TIMER:
{
//Grab graph values from OPAMP service
powerLevels thisPL= getPowerLevels();
uint8_t shieldLevel = getShieldLevel();
uint8_t hullLevel = getHullLevel();

//Grab joystick values from TVS service


Joystick thisJL = getWeaponsAngle();
uint8_t weaponsCapacity = getWeaponsCapacity();
uint8_t weaponsRange = getWeaponsRange();

//Grab our ship's location from ZENER service


ShipCoord_t ourShipPos = getShipLocation();

//
// uint16_t XCoord;
// uint16_t YCoord;

//Construct TxArray
TxArray[0] = 0xFF; //Start delimiter MSB
TxArray[1] = 0xFE; //Start delimiter LSB
TxArray[2] = thisJL.X >> 8; //Joy X MSB
TxArray[3] = thisJL.X; //Joy X LSB
TxArray[4] = thisJL.Y >> 8; //Joy Y MSB
TxArray[5] = thisJL.Y; //Joy Y LSB
TxArray[6] = weaponsRange; //Weapons Range
TxArray[7] = hullLevel; //Hull capacity
TxArray[8] = shieldLevel; //Shield capacity
TxArray[9] = weaponsCapacity; //Weapons capacity
TxArray[10] = thisPL.navigation; //Navigation Power
TxArray[11] = ourShipPos.XCoord >> 8;//Ship X MSB
TxArray[12] = ourShipPos.XCoord; //Ship X LSB
TxArray[13] = ourShipPos.YCoord >> 8;//Ship Y MSB
TxArray[14] = ourShipPos.YCoord; //Ship Y LSB
if(QueryZener() != ZenerDead){ //If our ship is alive:
TxArray[15] = 1; //Set alive bit
}
else{ //Else ship is dead:
TxArray[15] = 0; //Clear alive bit
}

//Send contents thru SPI


SPI1_TxBuffer(TxArray, MESSAGE_LENGTH);
//Re-init the SPI timer
ES_Timer_InitTimer(SPI_TIMER, SPI_UPDATE_TIME);

}break;
}
}
break;
}
return ReturnEvent;
}

/****************************************************************************
Function
SPI_Init
Parameters
bitRate, bitWidth
Returns
void
Description
set up the SPI system for use. Set the clock phase and polarity, master mode
clock source, baud rate, SS control, transfer width (8-bits)
Notes
Author: Ethan Kurteff 5/20/21

****************************************************************************/
void SPI1_Init(uint32_t bitRate, uint8_t bitWidth){

/*SPI ONE OUTPUT-----------------------*/


/*Configure SPI1 for transmitting data*/
/*Disable Analog on SPI pins*/
ANSELBbits.ANSB14 = 0; //(RB4 and RB5 don't have ANSEL bits)

/*Set SPI pins as outputs*/


TRISBbits.TRISB4 = 0; //Output
TRISBbits.TRISB5 = 0; //Output
TRISBbits.TRISB14 = 0; //Output

/*MAP SPI peripherals*/


RPB4R = 0b0011; //Map SS1 to RB4
RPB5R = 0b0011; //Map SDO1 to RB5
//(RB14 is always SCK_1)

/*Reset and the SPI module for protocol-specific settings.*/


SPI1CONbits.ON = 0;//Stop and reset SPI1 module by clearing the ON bit
SPI1BUF = 0;//Clear the receive buffer

/*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

SPI1CONbits.MSTEN = 1;//Set the PIC's SPI1 module to MASTER mode


SPI1CONbits.MSSEN = 1;//Drive SS pin automatically
/*Change the SPI desired settings for the specific protocol*/
SPI1CONbits.FRMPOL = 0;//Slave Select pulse is active low
SPI1CONbits.CKP = 0;//Set clock polarity to active low (1) //active high = 0
SPI1CONbits.CKE = 1;//Start reading data on second clock edge (0)

/*Configure bit width*/


if(bitWidth == 32) SPI1CONbits.MODE32 = 1;//Data width will be 32 bits
else if (bitWidth == 16)
{
SPI1CONbits.MODE32 = 0;//Data width will not be 32 bit
SPI1CONbits.MODE16 = 1;//Set data width to 16 bit
}
else if (bitWidth == 8)
{
SPI1CONbits.MODE32 = 0;//Data width will not be 32 bit
SPI1CONbits.MODE16 = 0;//Set data width to 8 bits
}

/*TURN IT ON!*/
SPI1CONbits.ON = 1;//Turn on SPI1

printf("\n\rSPI 1 Setup COMPLETE!");


}

/****************************************************************************
Function
SPI1_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 SPI1_Tx(uint8_t data){
while(SPI1STATbits.SPITBF);//Wait while tx buffer is full
SPI1BUF = data;//Write data to SPI xmit buffer
while(!SPI1STATbits.SRMT);//Wait until shift register is empty
SPI1BUF;//Read buffer to clear/prevent over-runs
}

/****************************************************************************
Function
SPI1_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 SPI1_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
{
SPI1_Tx(*(buffer+index));//Transmit the index(th) element of buffer
index++;//Move on to next element
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy