-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDRV8301_SPI.c
executable file
·92 lines (70 loc) · 3.36 KB
/
DRV8301_SPI.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//============================================================================
//============================================================================
//
// FILE: DRV8301_SPI.c
//
// TITLE: DRV8301 SPI comm functions
//
// Version: 1.0
//
// Date: 24 May 2011
//
//============================================================================
//============================================================================
#include "PeripheralHeaderIncludes.h"
#include "DRV8301_SPI.h"
/*****************************************************************************/
// Initialize the SPI peripheral
/*****************************************************************************/
void DRV8301_SPI_Init(volatile struct SPI_REGS *s)
{
s->SPICCR.bit.SPISWRESET = 0; // Put SPI in reset state
s->SPICCR.bit.SPICHAR = 0xF; // 16-bit character
s->SPICCR.bit.SPILBK = 0; // loopback off
s->SPICCR.bit.CLKPOLARITY = 0; // Rising edge without delay
s->SPICTL.bit.SPIINTENA = 0; // disable SPI interrupt
s->SPICTL.bit.TALK = 1; // enable transmission
s->SPICTL.bit.MASTER_SLAVE = 1; // master
s->SPICTL.bit.CLK_PHASE = 0; // Rising edge without delay
s->SPICTL.bit.OVERRUNINTENA = 0; // disable reciever overrun interrupt
s->SPIBRR = 0; // SPICLK = LSPCLK / 4 (max SPICLK)
s->SPICCR.bit.SPISWRESET=1; // Enable SPI
}
/*****************************************************************************/
// Read from a DRV8301 Register
/*****************************************************************************/
Uint16 DRV8301_SPI_Read(volatile struct SPI_REGS *s, Uint16 address)
{
union DRV8301_SPI_WRITE_WORD_REG w;
volatile Uint16 dummy;
w.bit.R_W = 1; //we are initiating a read
w.bit.ADDRESS = address; //load the address
w.bit.DATA = 0; //dummy data;
s->SPITXBUF = w.all; //send out the data
while(s->SPISTS.bit.INT_FLAG == 0); //wait for the packet to complete
dummy = s->SPIRXBUF; //dummy read to clear the INT_FLAG bit
w.bit.R_W = 1; //we are initiating a read
w.bit.ADDRESS = address; //load the address
w.bit.DATA = 0; //dummy data;
s->SPITXBUF = w.all; //send out the data
while(s->SPISTS.bit.INT_FLAG == 0); //wait for the packet to complete
dummy = s->SPIRXBUF; //dummy read to clear the INT_FLAG bit
return(dummy);
}
/*****************************************************************************/
// Write to a DRV8301 Register
// SPI writes always clock out the data in Status Register 1.
// Since it's available we'll return the status from this function
/*****************************************************************************/
Uint16 DRV8301_SPI_Write(volatile struct SPI_REGS *s, Uint16 address, Uint16 data)
{
union DRV8301_SPI_WRITE_WORD_REG w;
volatile Uint16 stat_reg1;
w.bit.R_W = 0; //we are initiating a write
w.bit.ADDRESS = address; //load the address
w.bit.DATA = data; //data to be written;
s->SPITXBUF = w.all; //send out the data
while(s->SPISTS.bit.INT_FLAG == 0); //wait for the packet to complete
stat_reg1 = s->SPIRXBUF; //read returned value of Status Register 1 and clear the INT_FLAG bit
return(stat_reg1);
}