forked from particle-iot/device-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspark_wiring_spi.cpp
193 lines (156 loc) · 4.62 KB
/
spark_wiring_spi.cpp
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
/**
******************************************************************************
* @file spark_wiring_spi.cpp
* @author Satish Nair
* @version V1.0.0
* @date 13-March-2013
* @brief Wrapper for wiring SPI module
******************************************************************************
Copyright (c) 2013 Spark Labs, Inc. All rights reserved.
Copyright (c) 2010 by Cristian Maglie <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
#include "spark_wiring_spi.h"
SPIClass SPI;
SPI_InitTypeDef SPIClass::SPI_InitStructure;
bool SPIClass::SPI_Bit_Order_Set = false;
bool SPIClass::SPI_Data_Mode_Set = false;
bool SPIClass::SPI_Clock_Divider_Set = false;
bool SPIClass::SPI_Enabled = false;
void SPIClass::begin() {
begin(SS);
}
void SPIClass::begin(uint16_t ss_pin) {
if (ss_pin >= TOTAL_PINS )
{
return;
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
pinMode(SCK, AF_OUTPUT_PUSHPULL);
pinMode(MOSI, AF_OUTPUT_PUSHPULL);
pinMode(MISO, INPUT);
pinMode(ss_pin, OUTPUT);
digitalWrite(ss_pin, HIGH);
/* SPI configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
if(SPI_Data_Mode_Set != true)
{
//Default: SPI_MODE3
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
}
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
if(SPI_Clock_Divider_Set != true)
{
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
}
if(SPI_Bit_Order_Set != true)
{
//Default: MSBFIRST
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
}
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
SPI_Enabled = true;
}
void SPIClass::end() {
if(SPI_Enabled != false)
{
SPI_Cmd(SPI1, DISABLE);
SPI_Enabled = false;
}
}
void SPIClass::setBitOrder(uint8_t bitOrder)
{
if(bitOrder == LSBFIRST)
{
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
}
else
{
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
}
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Bit_Order_Set = true;
}
void SPIClass::setDataMode(uint8_t mode)
{
if(SPI_Enabled != false)
{
SPI_Cmd(SPI1, DISABLE);
}
switch(mode)
{
case SPI_MODE0:
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
break;
case SPI_MODE1:
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
break;
case SPI_MODE2:
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
break;
case SPI_MODE3:
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
break;
}
SPI_Init(SPI1, &SPI_InitStructure);
if(SPI_Enabled != false)
{
SPI_Cmd(SPI1, ENABLE);
}
SPI_Data_Mode_Set = true;
}
void SPIClass::setClockDivider(uint8_t rate)
{
SPI_InitStructure.SPI_BaudRatePrescaler = rate;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Clock_Divider_Set = true;
}
byte SPIClass::transfer(byte _data) {
/* Wait for SPI1 Tx buffer empty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI1 data */
SPI_I2S_SendData(SPI1, _data);
/* Wait for SPI1 data reception */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Read and return SPI1 received data */
return SPI_I2S_ReceiveData(SPI1);
}
void SPIClass::attachInterrupt() {
//To Do
}
void SPIClass::detachInterrupt() {
//To Do
}
bool SPIClass::isEnabled() {
return SPI_Enabled;
}
/*******************************************************************************
* Function Name : Wiring_SPI1_Interrupt_Handler (Declared as weak in stm32_it.cpp)
* Description : This function handles SPI1 global interrupt request.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void Wiring_SPI1_Interrupt_Handler(void)
{
//To Do
}