1 #include <stdio.h> // ###
6 /*************************************************************************
8 * Used with ICCARM and AARM.
10 * (c) Copyright IAR Systems 2003
12 * File name : mmc_ll.h
13 * Description : define MMC module
16 * 1. Data : July 1, 2005
17 * Author : Stanimir Bonev
18 * Description : Create
19 * 2. Data : July 1, 2005
20 * Author : Stanimir Bonev
21 * Description : Modify
22 * Fix a lock problem in spiReceiveBlock
25 * (C) Joel Winarske, 2006,2007
26 **************************************************************************/
28 /*************************************************************************
29 * Function Name: spiChipSelect
30 * Parameters: Boolean Select
33 * Description: MMC Chip select control
34 * Select = true - Chip is enabled
35 * Select = false - Chip is disabled
37 *************************************************************************/
38 void spiChipSelect (BOOL select)
41 GPIO0_IOCLR = GPIO_IO_P20;
44 GPIO0_IOSET = GPIO_IO_P20;
46 while (!(SSP_SR & SSP_SR_TNF))
52 // Wait until TX fifo and TX shift buffer are empty
54 while (SSP_SR & SSP_SR_BSY)
56 while (!(SSP_SR & SSP_SR_RNE))
63 while (SSP_SR & SSP_SR_RNE);
67 /*************************************************************************
68 * Function Name: spiPresent
70 * Return: Boolean - true card present
71 * - false card not present
73 * Description: MMC present check
75 *************************************************************************/
76 inline BOOL spiPresent (void)
81 /*************************************************************************
82 * Function Name: spiWriteProtect
84 * Return: Boolean - true card is protected
85 * - false card not protected
87 * Description: MMC Write protect check
89 *************************************************************************/
90 inline BOOL spiWriteProtect (void)
95 /*************************************************************************
96 * Function Name: spiSetClockFreq
97 * Parameters: Int32U Frequency
100 * Description: Set SPI ckl frequency
102 *************************************************************************/
103 U32 spiSetClockFreq (U32 frequency)
105 U32 Div = SystemPeripherialClock / frequency;
116 return (SystemPeripherialClock / Div);
119 /*************************************************************************
120 * Function Name: spiInit
124 * 1 - speed is to high
126 * Description: Init SPI, Cart Present, Write Protect and Chip select pins
128 *************************************************************************/
137 GPIO0_IOSET = GPIO_IO_P20;
138 GPIO0_IODIR |= GPIO_IO_P20;
143 SCB_PCONP |= SCB_PCONP_PCSPI1;
145 SSP_CR0 = SSP_CR0_DSS_8 | SSP_CR0_FRF_SPI;
150 // Clock Freq. Identification Mode < 400kHz
152 spiSetClockFreq (400000);
154 PCB_PINSEL1 |= PCB_PINSEL1_P017_SCK1;
155 PCB_PINSEL1 |= PCB_PINSEL1_P018_MISO1;
156 PCB_PINSEL1 |= PCB_PINSEL1_P019_MOSI1;
157 PCB_PINSEL1 &= ~PCB_PINSEL1_P020_MASK;
162 SSP_CR1 |= SSP_CR1_SSE;
164 for (i = 0; i < 8; i++)
168 /*************************************************************************
169 * Function Name: spiTransferByte
173 * Description: Read byte from SPI
175 *************************************************************************/
176 U8 spiTransferByte (U8 c)
178 while (!(SSP_SR & SSP_SR_TNF))
183 while (!(SSP_SR & SSP_SR_RNE))
189 /*-----------------------------------------------------------------------*/
190 /* Wait for card ready */
191 /*-----------------------------------------------------------------------*/
192 U8 spiWaitReady (void)
196 do /* TODO: add in 500ms timeout */
200 while (!(SSP_SR & SSP_SR_RNE))
210 /*************************************************************************
211 * Function Name: spiSendBlock
212 * Parameters: pInt8U pData, Int32U Size
216 * Description: Read byte from SPI
218 *************************************************************************/
219 void spiSendBlock (U8 *pData, U32 size)
226 while ((SSP_SR & SSP_SR_TNF) && outCount)
233 while ((SSP_SR & SSP_SR_RNE) || !(SSP_SR & SSP_SR_TFE))
237 /*************************************************************************
238 * Function Name: spiReceiveBlock
239 * Parameters: pInt8U pData, Int32U Size
243 * Description: Read byte from SPI
245 *************************************************************************/
246 void spiReceiveBlock (U8 *pData, U32 size)
250 while (size || delta)
252 while ((SSP_SR & SSP_SR_TNF) && (delta < SSP_FIFO_DEPTH) && size)
259 while (SSP_SR & SSP_SR_RNE)
267 /*************************************************************************
268 * Function Name: spiDly_1ms
269 * Parameters: Int32U Delay
272 * Description: Delay [msec]
274 *************************************************************************/
275 void spiDelay1ms (U32 delay)
279 for (;delay; --delay)
281 for (i = MMC_DLY_1MSEC; i; --i)