SPI - UWCubeSat/DubSat1 GitHub Wiki
DubSat1 SPI Package
This is the University of Washington CubeSat Club's SPI Library for the MSP430FR5994.
Description
The SPI Library operates by first initializing the SPI bus, then calling spiTranscieve with a write buffer (an array) and a read buffer (they can be the same buffer.) Calling spiTranscieve will send bytes in the buffer one at a time, and record the response bytes (if any) into the receive buffer. To receive information, keep sending zeros after your message and the bytes will go into the receive buffer, up to the length you give.
API
methods:
Type | Method and Description |
---|---|
void | spiInit: Initializes the SPI bus |
void | spiTransceive: reads/writes to/from SPI bus using buffers |
spiInit
void spiInit(uint8_t csPins)
Description:
This method initializes the SPI bus using eUSCI B1 using pins: Pins:
- P5.0: UCB1SIMO
- P5.1: UCB1SOMI
- P5.2: UCB1CLK
- P5.3, P4.4, P4.5: CS1, CS2, CS3 respectively
Parameters:
uint8_t* csPins: The CS pins you want to enable. CS_1 (or 0x01) for CS1, CS_2 (or 0x02) for CS2, and CS_3 (or 0x04) for CS3, or a bitwise OR combination for any combination of them.
spiTransceive
void spiTransceive(uint8_t *pTxBuf, uint8_t *pRxBuf, size_t num, uint8_t csPin)
Description:
This method writes num bytes from a buffer pTxBuf to the SPI bus using 4 Wire SPI Mode. It will store the bytes received during the write sequence to another client provided buffer, pRxBuf. The pRxBuf must be at least num bytes, and it will fill num bytes of data. Bits are transmitted on the rising edge of the CLK clock.
Clock pin frequency: 1MHz
Delay between bytes: 17μs (Subject to change)
Parameters:
uint8_t* pTxBuf: A pointer to the transmit buffer. This can actually be the same buffer as the receive buffer, which can be used if you no longer need the data in the tx buffer. IMPORTANT: THE pTxBuf MUST BE AT LEAST num BYTES
uint8_t* pRxBuf: A pointer to the receive buffer. This can actually be the same buffer as the transmit buffer, which can be used if you no longer need the data in the tx buffer. IMPORTANT: THE pRxBuf MUST BE AT LEAST num BYTES
size_t num: Size of the transmit buffer in bytes.
uint8_t* csPins: The CS pin you want to go low for transmit. CS_1 (or 0x01) for CS1, CS_2 (or 0x02) for CS2, and CS_3 (or 0x04) for CS3.
Code example
This is example code that transmits 2 bytes, 0xFA and 0xCE from the transmit buffer, and receives them at testRXData
// Stop watchdog timer
WDTCTL = WDTPW | WDTHOLD;
// Transmit Buffer: MUST BE AT LEAST num BYTES
uint8_t txData[2] = {0xFA, 0xCE};
// Recieve Buffer: MUST BE AT LEAST num BYTES
uint8_t rxData[2];
//Disable high impedence mode, whatever that is... Now I know!
PM5CTL0 &= ~LOCKLPM5;
//Initialize the SPI Library
spiInit(CS_3);
//Transmit 2 bytes, fill rxData with the stuff it gets back.
spiTransceive(txData, rxData, 2, CS_3);