-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPI_LPC17xx.c
108 lines (82 loc) · 4.1 KB
/
SPI_LPC17xx.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
/*----------------------------------------------------------------------------
* RL-ARM - FlashFS
*----------------------------------------------------------------------------
* Name: SPI_LPC17xx.C
* Purpose: Serial Peripheral Interface Driver for LPC17xx
* Rev.: V4.05
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) 2004-2009 KEIL - An ARM Company. All rights reserved.
*---------------------------------------------------------------------------*/
#include <LPC17xx.H> /* LPC17xx definitions */
#include <File_Config.h>
/* SSPxSR - bit definitions. */
#define TFE 0x01
#define TNF 0x02
#define RNE 0x04
#define RFF 0x08
#define BSY 0x10
/*----------------------------------------------------------------------------
* SPI Driver Functions
*----------------------------------------------------------------------------
* Required functions for SPI driver module:
* - void spi_init ()
* - void spi_ss (U32 ss)
* - U8 spi_send (U8 outb)
* - void spi_hi_speed (BOOL on)
*---------------------------------------------------------------------------*/
/*--------------------------- spi_init --------------------------------------*/
void spi_init (void) {
/* Initialize and enable the SSP Interface module. */
LPC_SC->PCONP |= (1 << 21); /* Enable power to SSPI0 block */
/* SSEL is GPIO, output set to high. */
LPC_GPIO1->FIODIR |= (1<<21); /* P1.21 is output */
LPC_GPIO1->FIOPIN |= (1<<21); /* set P1.21 high (SSEL inact.)*/
LPC_PINCON->PINSEL3 &= ~(0<<10); /* P1.21 SSEL (used as GPIO) */
/* P3.26 is SD Card Power Supply Enable Pin */
LPC_GPIO3->FIODIR |= (1<<26); /* P3.26 is output */
LPC_GPIO3->FIOPIN &= ~(1<<26); /* set P3.26 low(enable power) */
/* SCK, MISO, MOSI are SSP pins. */
LPC_PINCON->PINSEL3 &= ~(3UL<<8); /* P1.20 cleared */
LPC_PINCON->PINSEL3 |= (3UL<<8); /* P1.20 SCK0 */
LPC_PINCON->PINSEL3 &= ~((3<<14) | (3<<16)); /* P1.23, P1.24 cleared */
LPC_PINCON->PINSEL3 |= ((3<<14) | (3<<16)); /* P1.23 MISO0, P1.24 MOSI0 */
LPC_SC->PCLKSEL1 &= ~(3<<10); /* PCLKSP0 = CCLK/4 ( 25MHz) */
LPC_SC->PCLKSEL1 |= (1<<10); /* PCLKSP0 = CCLK (100MHz) */
LPC_SSP0->CPSR = 250; /* 100MHz / 250 = 400kBit */
/* maximum of 18MHz is possible*/
LPC_SSP0->CR0 = 0x0007; /* 8Bit, CPOL=0, CPHA=0 */
LPC_SSP0->CR1 = 0x0002; /* SSP0 enable, master */
}
/*--------------------------- spi_hi_speed ----------------------------------*/
void spi_hi_speed (BOOL on) {
/* Set a SPI clock speed to desired value. */
if (on == __TRUE) {
/* Max. 12 MBit used for Data Transfer. */
LPC_SSP0->CPSR = 10; /* 100MHz / 10 = 10MBit */
}
else {
/* Max. 400 kBit used in Card Initialization. */
LPC_SSP0->CPSR = 250; /* 100MHz / 250 = 400kBit */
}
}
/*--------------------------- spi_ss ----------------------------------------*/
void spi_ss (U32 ss) {
/* Enable/Disable SPI Chip Select (drive it high or low). */
if (ss) {
LPC_GPIO1->FIOPIN |= (1<<21); /* SSEL is GPIO, set to high. */
}
else {
LPC_GPIO1->FIOPIN &= ~(1<<21); /* SSEL is GPIO, set to high. */
}
}
/*--------------------------- spi_send --------------------------------------*/
U8 spi_send (U8 outb) {
/* Write and Read a byte on SPI interface. */
LPC_SSP0->DR = outb;
while (LPC_SSP0->SR & BSY); /* Wait for transfer to finish */
return (LPC_SSP0->DR); /* Return received value */
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/