-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi.c
122 lines (113 loc) · 2.36 KB
/
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
/*
* spi.c
*
* Created on: 25/07/2019
* Author: Evandro Teixeira
*/
#include "spi.h"
/**
* @brief
* @param spi
* @param type
* @param alt
* @param pre
* @param div
* @return
*/
bool spi_init(SPI_MemMapPtr spi,spi_type_t type,spi_alt_pin_t alt, uint8_t pre, uint8_t div)
{
bool ret = true;
switch(alt)
{
case SPI_Alt0:
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; // Enable PORT
//PORTA_PCR5 = PORT_PCR_MUX(0x3); // Set PTA5 to mux 3 [SPI0_SS ]
PORTA_PCR6 = PORT_PCR_MUX(0x3); // Set PTA6 to mux 3 [SPI0_MISO]
PORTA_PCR7 = PORT_PCR_MUX(0x3); // Set PTA7 to mux 3 [SPI0_MOSI]
PORTB_PCR0 = PORT_PCR_MUX(0x3); // Set PTB0 to mux 3 [SPI0_SCK ]
break;
case SPI_Alt1:
SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK; // Enable PORT
//PORTA_PCR19 = PORT_PCR_MUX(0x3); // Set PTA19 to mux 3 [SPI0_SS ]
PORTB_PCR15 = PORT_PCR_MUX(0x3); // Set PTB15 to mux 3 [SPI0_MISO]
PORTB_PCR16 = PORT_PCR_MUX(0x3); // Set PTB16 to mux 3 [SPI0_MOSI]
PORTB_PCR17 = PORT_PCR_MUX(0x3); // Set PTB17 to mux 3 [SPI0_SCK ]
break;
default:
ret = false;
break;
}
if(type == SPI_Master)
{
// enable clock gate for spi module
SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK;
spi->C1 &= ~SPI_C1_SPE_MASK; // disable SPI
// Set SPI to Master
spi->C1 = SPI_C1_MSTR_MASK;
// Configure SPI Register C2
spi->C2 = SPI_C2_MODFEN_MASK; //Master SS pin acts as slave select output
// Set baud rate prescale divisor to 6 & set baud rate divisor to 4 for baud rate of 1 Mhz
spi->BR = (SPI_BR_SPPR(pre) | SPI_BR_SPR(div)); // Mhz
// Enable SPI
spi->C1 |= SPI_C1_SPE_MASK;
}
else if(type == SPI_Slave)
{
ret = false;
}
else
{
ret = false;
}
return ret;
}
/**
* @brief
* @param spi
* @param data
*/
void spi_write(SPI_MemMapPtr spi, uint8_t data)
{
while(!(SPI_S_SPTEF_MASK & spi->S))
{
__asm("nop");
}
spi->D = data;
}
/**
* @brief
* @param spi
*/
uint8_t spi_read(SPI_MemMapPtr spi)
{
while (!(spi->S & SPI_S_SPTEF_MASK));
{
__asm("nop");
}
spi->D = 0;
while(!(spi->S & SPI_S_SPRF_MASK))
{
__asm("nop");
}
return spi->D;
}
/**
* @brief
* @param gpio
* @param pin
* @param st
*/
void spi_write_CS(GPIO_MemMapPtr gpio,uint32_t pin,spi_cs_t st)
{
gpio_write(gpio,pin,st);
}
/**
* @brief
* @param gpio
* @param pin
*/
void spi_init_CS(GPIO_MemMapPtr gpio,uint32_t pin)
{
gpio_init(gpio,pin,GPIO_OUTPUT);
gpio_write(gpio,pin,SPI_CS_Disable);
}