SRD Standelone programmation box: spi1.c File Reference

| SRD Standelone programmation box 2.0 (16/11/15 10:00:00) Documentation of the SRD standelone programmation box

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Enumerations Enumerator Macros Pages
Functions | Variables
spi1.c File Reference

Source file for SPI1 driver. More...

`#include "spi1.h"`
Include dependency graph for spi1.c:

Go to the source code of this file.

## Functions

void | Spi1Init (unsigned long sclkFreq)
| Function used to initialize the SPI1 module. More...
unsigned long | Spi1GetBaudrate (void)
signed short | Spi1Send (unsigned char *data, unsigned short dataLength)
| Function used to send data over the SPI module 1. More...
signed short | Spi1Read (unsigned char *data, unsigned short dataLength)
| Function used to read data over the SPI module 1. More...

## Variables

unsigned long | Spi1Baudrate

## Detailed Description

Source file for SPI1 driver.

Author
  Thibaut LALUQUE 3D-Ouest

Date
  26/09/2014

Version
  1.0

supported device : MKL16Z256VLH4

All Right Reserved.

Definition in file spi1.c.

## Function Documentation

unsigned long Spi1GetBaudrate | ( | void |  | ) |

Definition at line 146 of file spi1.c.

References Spi1Baudrate.

146 {
147 return Spi1Baudrate;
148 }
Spi1Baudrate
unsigned long Spi1Baudrate
Definition: spi1.c:54

void Spi1Init | ( | unsigned long | sclkFreq | ) |

Function used to initialize the SPI1 module.

Parameters

  [in] | sclkFreq | (unsigned long) : baudrate of the SPI clock.

Definition at line 80 of file spi1.c.

References SPI1_CLOCK_IDLE_POL, SPI1_CLOCK_PHASE, SPI1_CLOCK_SOURCE, SPI1_MODE_16BIT, SPI1_STOP_IN_WAIT_MODE, Spi1Baudrate, SPI1ClockGate(), and SPI1PinMappingPeripheral().

Referenced by FM25V20Init().

80 {
81
82 /*=== Enable clock on the peripheral ===*/
83 SPI1ClockGate(1);
84
85 /*=== Make sure SPI is disable ===*/
86 SPI1->C1 = 0;
87
88 /*=== MAP IO ===*/
89 SPI1PinMappingPeripheral();
90
91 /*=== compute baudrate divisor ===*/
92 unsigned char baudrateDivisor;
93 if( (SPI1_CLOCK_SOURCE>>1) >= sclkFreq && (SPI1_CLOCK_SOURCE>>4) < sclkFreq) {
94 baudrateDivisor = 0; //Use baudrate divisor 1:2
95 } else if ( (SPI1_CLOCK_SOURCE>>2) >= sclkFreq && (SPI1_CLOCK_SOURCE>>5) < sclkFreq) {
96 baudrateDivisor = 1; //Use baudrate divisor 1:4
97 } else if ( (SPI1_CLOCK_SOURCE>>3) >= sclkFreq && (SPI1_CLOCK_SOURCE>>6) < sclkFreq) {
98 baudrateDivisor = 2; //Use baudrate divisor 1:8
99 } else if ( (SPI1_CLOCK_SOURCE>>4) >= sclkFreq && (SPI1_CLOCK_SOURCE>>7) < sclkFreq) {
100 baudrateDivisor = 3; //Use baudrate divisor 1:16
101 } else if ( (SPI1_CLOCK_SOURCE>>5) >= sclkFreq && (SPI1_CLOCK_SOURCE>>8) < sclkFreq) {
102 baudrateDivisor = 4; //Use baudrate divisor 1:32
103 } else if ( (SPI1_CLOCK_SOURCE>>6) >= sclkFreq && (SPI1_CLOCK_SOURCE>>9) < sclkFreq) {
104 baudrateDivisor = 5; //Use baudrate divisor 1:64
105 } else if ( (SPI1_CLOCK_SOURCE>>7) >= sclkFreq && (SPI1_CLOCK_SOURCE>>10) < sclkFreq) {
106 baudrateDivisor = 6; //Use baudrate divisor 1:128
107 } else if ( (SPI1_CLOCK_SOURCE>>8) >= sclkFreq && (SPI1_CLOCK_SOURCE>>11) < sclkFreq) {
108 baudrateDivisor = 7; //Use baudrate divisor 1:256
109 } else if ( (SPI1_CLOCK_SOURCE>>9) >= sclkFreq && (SPI1_CLOCK_SOURCE>>12) < sclkFreq) {
110 baudrateDivisor = 8; //Use baudrate divisor 1:512
111 } else {
112 baudrateDivisor = 0;
113 Spi1Baudrate = 0;
114 return;
115 }
116
117 /*=== compute baudrate prescaler ===*/
118 unsigned char baudratePrescaler = (unsigned char)( (SPI1_CLOCK_SOURCE>>(baudrateDivisor+1)) / sclkFreq );
119
120 /*=== Compute real baudrate and update register ===*/
121 Spi1Baudrate = (SPI1_CLOCK_SOURCE>>(baudrateDivisor+1)) / baudratePrescaler;
122 SPI1->BR = baudrateDivisor<<4 | baudratePrescaler;
123
124 SPI1->C2 = SPI1_MODE_16BIT<<6 | SPI1_STOP_IN_WAIT_MODE<<1; //No DMA tranfert / No master mode fault / set data length size / set Wait mode state
125
126 /*=== SPI global conf ===*/
127 #ifdef SPI1_MASTER_MODE
128 SPI1->C1 = SPI_C1_MSTR_MASK | (SPI1_CLOCK_IDLE_POL<<3) | (SPI1_CLOCK_PHASE<<2);
129 #else
130 SPI1->C1 = (SPI1_CLOCK_IDLE_POL<<3) | (SPI1_CLOCK_PHASE<<2);
131 #endif
132
133 /*=== Spi endian mode ===*/
134 #ifdef SPI1_SEND_LSB_FIRST
135 SPI1->C1 |= 1;
136 #endif
137
138 #ifdef SPI1_USE_PCS
139 SPI1->C1 |= SPI_C1_SSOE_MASK;
140 SPI1->C2 |= SPI_C2_MODFEN_MASK;
141 #endif
142
143 //SPI1->C3 = 0; //Don't use buffer mode / No interrupt
144 }
Spi1Baudrate
unsigned long Spi1Baudrate
Definition: spi1.c:54
SPI1_MODE_16BIT
#define SPI1_MODE_16BIT
Set to 1 to enable the 16 bits mode.
Definition: spi1.h:37
SPI1_CLOCK_IDLE_POL
#define SPI1_CLOCK_IDLE_POL
Define the clock polarity.
Definition: spi1.h:61
SPI1ClockGate
void SPI1ClockGate(unsigned char bool)
Definition: DSP_mkl16z256vlh4.c:125
SPI1_STOP_IN_WAIT_MODE
#define SPI1_STOP_IN_WAIT_MODE
Set to 1 to continue operation when the processor enter in wait mode.
Definition: spi1.h:45
SPI1_CLOCK_SOURCE
#define SPI1_CLOCK_SOURCE
Define clock source for peripehrals.
Definition: DSP_mkl16z256vlh4.h:325
SPI1_CLOCK_PHASE
#define SPI1_CLOCK_PHASE
Define the clock phase.
Definition: spi1.h:69
SPI1PinMappingPeripheral
void SPI1PinMappingPeripheral(void)
Definition: DSP_mkl16z256vlh4.c:365

Here is the call graph for this function:

Here is the caller graph for this function:

signed short Spi1Read | ( | unsigned char * | data,
|  | unsigned short | dataLength
| ) |  |

Function used to read data over the SPI module 1.

Parameters

  [out] | data | (unsigned char* or unsigned short*) : Pointer on the data read buffer.
  [in] | dataLength | (unsigned short) : length of the data to read.

Returns
  (signed short) return 0 if data was correctly sended else return -1.

Definition at line 187 of file spi1.c.

References Spi1GetEnable, and Spi1Read().

Referenced by FM25V20ReadData(), FM25V20ReadDeviceId(), FM25V20ReadStatusReg(), and Spi1Read().

187 {
188 unsigned char read;
189 #else
190 signed short Spi1Read (unsigned short* data, unsigned short dataLength) {
191 unsigned short read;
192 #endif
193
194 unsigned short id = 0;
195 unsigned char dummy= 1;
196
197 if(Spi1GetEnable()) { //Check if module is enable
198
199 while(dataLength != 0) {
200
201 while((SPI1->S & 0x20) != 0x20); //Wait for SPI becomes ready
202
203 #if SPI1_MODE_16BIT == 0
204 SPI1->DL = 0x00; //Write data
205 #else
206 SPI1->DH = 0x00;
207 SPI1->DL = 0x00;
208 #endif
209
210 while((SPI1->S & 0x80) != 0x80); //Wait for transfert complete
211
212 #if SPI1_MODE_16BIT == 0
213 read = SPI1->DL;
214 #else
215 read = (((unsigned short)SPI1->DH) <<8) | SPI1->DL;
216 #endif
217
218
219 if(dummy) {
220 dummy = 0;
221 } else {
222
223 data[id++] = read; //Read data
224 dataLength--;
225 }
226 }
227
228 return 0;
229
230 } else {
231 return -1;
232 }
233 }
Spi1GetEnable
#define Spi1GetEnable()
Set/Clear/Read the SPI enable status.
Definition: spi1.h:134
Spi1Read
signed short Spi1Read(unsigned char *data, unsigned short dataLength)
Function used to read data over the SPI module 1.
Definition: spi1.c:187

Here is the call graph for this function:

Here is the caller graph for this function:

signed short Spi1Send | ( | unsigned char * | data,
|  | unsigned short | dataLength
| ) |  |

Function used to send data over the SPI module 1.

Parameters

  [in] | data | (unsigned char* or unsigned short*) : Pointer on the data to send.
  [in] | dataLength | (unsigned short) : length of the data to send.

Returns
  (signed short) return 0 if data was correctly sended else return -1.

Definition at line 151 of file spi1.c.

References Spi1GetEnable, and Spi1Send().

Referenced by FM25V20_SetWriteEnable(), FM25V20LPM(), FM25V20ReadData(), FM25V20ReadDeviceId(), FM25V20ReadStatusReg(), FM25V20SetStatusReg(), FM25V20WriteData(), and Spi1Send().

151 {
152 #else
153 signed short Spi1Send(unsigned short* data, unsigned short dataLength) {
154 #endif
155
156 unsigned short id = 0;
157
158 if(Spi1GetEnable()) { //Check if module is enable
159
160 while(dataLength != 0) { //While they are already data to send
161
162 while((SPI1->S & 0x20) != 0x20); //Wait for Tx empty flag
163 #if SPI1_MODE_16BIT == 0
164 SPI1->DL =data[id++]; //Write data
165 #else
166 SPI1->DH =(data[id] >> 8) & 0xFF;
167 SPI1->DL =data[id++] & 0xFF;
168 #endif
169
170 dataLength--;
171 while((SPI1->S & 0x20) != 0x20); //Wait for transfert complete
172
173 SPI1->DL; //Clear Rx buffer
174 SPI1->DH;
175
176 }
177
178 return 0;
179
180 } else {
181 return -1; //Return error -> module is not enable
182 }
183 }
Spi1GetEnable
#define Spi1GetEnable()
Set/Clear/Read the SPI enable status.
Definition: spi1.h:134
Spi1Send
signed short Spi1Send(unsigned char *data, unsigned short dataLength)
Function used to send data over the SPI module 1.
Definition: spi1.c:151

Here is the call graph for this function:

Here is the caller graph for this function:

## Variable Documentation

unsigned long Spi1Baudrate

Definition at line 54 of file spi1.c.

Referenced by Spi1GetBaudrate(), and Spi1Init().