-
Notifications
You must be signed in to change notification settings - Fork 4
/
TI_CC_spi.c
251 lines (224 loc) · 10.4 KB
/
TI_CC_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* --COPYRIGHT--,BSD
* Copyright (c) 2011, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* --/COPYRIGHT--*/
//------------------------------------------------------------------------------
// Description: This file contains functions that allow the MSP430 device to
// access the SPI interface of the CC1100/CC2500. There are multiple
// instances of each function; the one to be compiled is selected by the
// system variable TI_CC_RF_SER_INTF, defined in "TI_CC_hardware_board.h".
//
// MSP430/CC1100-2500 Interface Code Library v1.1
//
// W. Goh
// Texas Instruments, Inc.
// December 2009
// IAR Embedded Workbench v4.20
//------------------------------------------------------------------------------
// Change Log:
//------------------------------------------------------------------------------
//
// Version: 1.00
// Comments: Based on code from TI. Edited by Lars Kristian Roland
//------------------------------------------------------------------------------
#include "include.h"
//#include "TI_CC_spi.h"
//------------------------------------------------------------------------------
// void TI_CC_SPISetup(void)
//
// DESCRIPTION:
// Configures the assigned interface to function as a SPI port and
// initializes it.
//------------------------------------------------------------------------------
// void TI_CC_SPIWriteReg(char addr, char value)
//
// DESCRIPTION:
// Writes "value" to a single configuration register at address "addr".
//------------------------------------------------------------------------------
// void TI_CC_SPIWriteBurstReg(char addr, char *buffer, char count)
//
// DESCRIPTION:
// Writes values to multiple configuration registers, the first register being
// at address "addr". First data byte is at "buffer", and both addr and
// buffer are incremented sequentially (within the CCxxxx and MSP430,
// respectively) until "count" writes have been performed.
//------------------------------------------------------------------------------
// char TI_CC_SPIReadReg(char addr)
//
// DESCRIPTION:
// Reads a single configuration register at address "addr" and returns the
// value read.
//------------------------------------------------------------------------------
// void TI_CC_SPIReadBurstReg(char addr, char *buffer, char count)
//
// DESCRIPTION:
// Reads multiple configuration registers, the first register being at address
// "addr". Values read are deposited sequentially starting at address
// "buffer", until "count" registers have been read.
//------------------------------------------------------------------------------
// char TI_CC_SPIReadStatus(char addr)
//
// DESCRIPTION:
// Special read function for reading status registers. Reads status register
// at register "addr" and returns the value read.
//------------------------------------------------------------------------------
// void TI_CC_SPIStrobe(char strobe)
//
// DESCRIPTION:
// Special write function for writing to command strobe registers. Writes
// to the strobe at address "addr".
//------------------------------------------------------------------------------
// Delay function. # of CPU cycles delayed is similar to "cycles". Specifically,
// it's ((cycles-15) % 6) + 15. Not exact, but gives a sense of the real-time
// delay. Also, if MCLK ~1MHz, "cycles" is similar to # of useconds delayed.
void TI_CC_Wait(unsigned int cycles)
{
while(cycles>15) // 15 cycles consumed by overhead
cycles = cycles - 6; // 6 cycles consumed each iteration
}
//******************************************************************************
// Support for USCI_B0
//******************************************************************************
void TI_CC_SPISetup(void)
{
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN;
TI_CC_CSn_PxDIR |= TI_CC_CSn_PIN; // /CS disable
P2SEL = 0x00; // Make sure CSn works instead of crystal
UCB0CTL1 |= UCSWRST; // **Disable USCI state machine**
UCB0CTL0 |= UCMST+UCCKPH+UCMSB+UCSYNC; // 3-pin, 8-bit SPI master
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 = 0x02; // UCLK/2
UCB0BR1 = 0;
TI_CC_SPI_USCIB0_PxSEL |= TI_CC_SPI_USCIB0_SIMO
| TI_CC_SPI_USCIB0_SOMI
| TI_CC_SPI_USCIB0_UCLK;
TI_CC_SPI_USCIB0_PxSEL2 |= TI_CC_SPI_USCIB0_SIMO
| TI_CC_SPI_USCIB0_SOMI
| TI_CC_SPI_USCIB0_UCLK;
// SPI option select
TI_CC_SPI_USCIB0_PxDIR |= TI_CC_SPI_USCIB0_SIMO | TI_CC_SPI_USCIB0_UCLK;
// SPI TXD out direction
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
void TI_CC_SPIWriteReg(char addr, char value)
{
TI_CC_CSn_PxOUT &= ~TI_CC_CSn_PIN; // /CS enable
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = addr; // Send address
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = value; // Send data
while (UCB0STAT & UCBUSY); // Wait for TX to complete
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN; // /CS disable
}
void TI_CC_SPIWriteBurstReg(char addr, char *buffer, char count)
{
unsigned int i;
TI_CC_CSn_PxOUT &= ~TI_CC_CSn_PIN; // /CS enable
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = addr | TI_CCxxx0_WRITE_BURST; // Send address
for (i = 0; i < count; i++)
{
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = buffer[i]; // Send data
}
while (UCB0STAT & UCBUSY); // Wait for TX to complete
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN; // /CS disable
}
char TI_CC_SPIReadReg(char addr)
{
char x;
TI_CC_CSn_PxOUT &= ~TI_CC_CSn_PIN; // /CS enable
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = (addr | TI_CCxxx0_READ_SINGLE);// Send address
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = 0; // Dummy write so we can read data
while (UCB0STAT & UCBUSY); // Wait for TX to complete
x = UCB0RXBUF; // Read data
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN; // /CS disable
return x;
}
void TI_CC_SPIReadBurstReg(char addr, char *buffer, char count)
{
char i;
TI_CC_CSn_PxOUT &= ~TI_CC_CSn_PIN; // /CS enable
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = (addr | TI_CCxxx0_READ_BURST);// Send address
while (UCB0STAT & UCBUSY); // Wait for TX to complete
UCB0TXBUF = 0; // Dummy write to read 1st data byte
// Addr byte is now being TX'ed, with dummy byte to follow immediately after
IFG2 &= ~UCB0RXIFG; // Clear flag
while (!(IFG2&UCB0RXIFG)); // Wait for end of 1st data byte TX
// First data byte now in RXBUF
for (i = 0; i < (count-1); i++)
{
UCB0TXBUF = 0; //Initiate next data RX, meanwhile..
buffer[i] = UCB0RXBUF; // Store data from last data RX
while (!(IFG2&UCB0RXIFG)); // Wait for RX to finish
}
buffer[count-1] = UCB0RXBUF; // Store last RX byte in buffer
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN; // /CS disable
}
char TI_CC_SPIReadStatus(char addr)
{
char status;
TI_CC_CSn_PxOUT &= ~TI_CC_CSn_PIN; // /CS enable
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = (addr | TI_CCxxx0_READ_BURST);// Send address
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = 0; // Dummy write so we can read data
while (UCB0STAT & UCBUSY); // Wait for TX to complete
status = UCB0RXBUF; // Read data
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN; // /CS disable
return status;
}
void TI_CC_SPIStrobe(char strobe)
{
TI_CC_CSn_PxOUT &= ~TI_CC_CSn_PIN; // /CS enable
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = strobe; // Send strobe
// Strobe addr is now being TX'ed
while (UCB0STAT & UCBUSY); // Wait for TX to complete
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN; // /CS disable
}
void TI_CC_PowerupResetCCxxxx(void)
{
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN;
TI_CC_Wait(30);
TI_CC_CSn_PxOUT &= ~TI_CC_CSn_PIN;
TI_CC_Wait(30);
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN;
TI_CC_Wait(45);
TI_CC_CSn_PxOUT &= ~TI_CC_CSn_PIN; // /CS enable
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = TI_CCxxx0_SRES; // Send strobe
// Strobe addr is now being TX'ed
while (UCB0STAT & UCBUSY); // Wait for TX to complete
TI_CC_CSn_PxOUT |= TI_CC_CSn_PIN; // /CS disable
}