SRD Standelone programmation box: spi0.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
spi0.c File Reference

Source file for SPI0 driver. More...

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

Go to the source code of this file.

## Functions

void | Spi0Init (unsigned long sclkFreq)
| Function used to initialize the SPI0 module. More...
unsigned long | Spi0GetBaudrate (void)
signed short | Spi0Send (unsigned char *data, unsigned short dataLength)
| Function used to send data over the SPI module 0. More...
signed short | Spi0Read (unsigned char *data, unsigned short dataLength)
| Function used to read data over the SPI module 0. More...

## Variables

unsigned long | Spi0Baudrate

## Detailed Description

Source file for SPI0 driver.

Author
  Thibaut LALUQUE 3D-Ouest

Date
  26/09/2014

Version
  1.0

supported device : MKL16Z256VLH4

All Right Reserved.

Definition in file spi0.c.

## Function Documentation

unsigned long Spi0GetBaudrate | ( | void |  | ) |

Definition at line 147 of file spi0.c.

References Spi0Baudrate.

147 {
148 return Spi0Baudrate;
149 }
Spi0Baudrate
unsigned long Spi0Baudrate
Definition: spi0.c:54

void Spi0Init | ( | unsigned long | sclkFreq | ) |

Function used to initialize the SPI0 module.

Parameters

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

Definition at line 80 of file spi0.c.

References SPI0_CLOCK_IDLE_POL, SPI0_CLOCK_PHASE, SPI0_CLOCK_SOURCE, SPI0_MODE_16BIT, SPI0_STOP_IN_WAIT_MODE, Spi0Baudrate, SPI0ClockGate(), and SPI0PinMappingPeripheral().

Referenced by DisplayInit().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Function used to read data over the SPI module 0.

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 spi0.c.

References Spi0GetEnable, and Spi0Read().

Referenced by Spi0Read().

187 {
188 #else
189 signed short Spi0Read (unsigned short* data, unsigned short dataLength) {
190 #endif
191
192 unsigned short id = 0;
193
194 if(Spi0GetEnable()) { //Check if module is enable
195
196 while(dataLength != 0) {
197
198 while((SPI0->S & 0x20) != 0x20); //Wait for SPI becomes ready
199 #if SPI0_MODE_16BIT == 0
200 SPI0->DL =0x00; //Write data
201 #else
202 SPI0->DH =0x00;
203 SPI0->DL =0x00;
204 #endif
205
206 dataLength--;
207 while((SPI0->S & 0x20) != 0x20); //Wait for transfert complete
208
209 data[id++] = SPI0->DL | SPI0->DH; //Read data
210 }
211
212 } else {
213 return -1;
214 }
215 }
Spi0GetEnable
#define Spi0GetEnable()
Set/Clear/Read the SPI enable status.
Definition: spi0.h:134
Spi0Read
signed short Spi0Read(unsigned char *data, unsigned short dataLength)
Function used to read data over the SPI module 0.
Definition: spi0.c:187

Here is the call graph for this function:

Here is the caller graph for this function:

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

Function used to send data over the SPI module 0.

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 152 of file spi0.c.

References Spi0GetEnable, and Spi0Send().

Referenced by DisplaySetDataDisplayed(), and Spi0Send().

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

Here is the call graph for this function:

Here is the caller graph for this function:

## Variable Documentation

unsigned long Spi0Baudrate

Definition at line 54 of file spi0.c.

Referenced by Spi0GetBaudrate(), and Spi0Init().