OSDN Git Service

First commitment for the BlackTank LPC1769.
[blacktank/blacktank.git] / drivers / ff / ffspi.c
1 /**
2  * @file ffspi.c
3  * @brief FATファイルシステム用下層SPIドライバの実装。
4  */
5
6 #include "ffspi.h"
7
8 #include <lpc17xx_spi.h>
9 #include <lpc17xx_gpio.h>
10 #include <lpc17xx_pinsel.h>
11 #include <lpc17xx_libcfg.h>
12 #include <kernel.h>
13
14 SPI_CFG_Type SPI_ConfigStruct;
15 PINSEL_CFG_Type PinCfg;
16 SPI_DATA_SETUP_Type xferConfig;
17
18 // PORT number that /CS pin assigned on
19 #define CS_PORT_NUM 1
20 // PIN number that  /CS pin assigned on
21 #define CS_PIN_NUM 29
22
23 void ffspi_cs_init(void)
24 {
25     GPIO_SetDir(CS_PORT_NUM, (1 << CS_PIN_NUM), 1);
26     GPIO_SetValue(CS_PORT_NUM, (1 << CS_PIN_NUM));
27 }
28
29 void ffspi_cs_ena(void)
30 {
31     GPIO_ClearValue(CS_PORT_NUM, (1 << CS_PIN_NUM));
32 }
33
34 void ffspi_cs_dis(void)
35 {
36     GPIO_SetValue(CS_PORT_NUM, (1 << CS_PIN_NUM));
37 }
38
39 /* CS status (true:CS == L) */
40 unsigned char ffspi_cs_stat(void)
41 {
42     unsigned char val = GPIO_ReadValue(CS_PORT_NUM);
43     return ((val & (1 << CS_PIN_NUM)) == 0) ? 1 : 0;
44 }
45
46 void ffspi_init(void)
47 {
48     /*
49      * Initialize SPI pin connect
50      * P0.15 - SCK;
51      * P0.17 - MISO
52      * P0.18 - MOSI
53      */
54     PinCfg.Funcnum = 3;
55     PinCfg.OpenDrain = 0;
56     PinCfg.Pinmode = 0;
57     PinCfg.Portnum = 0;
58     PinCfg.Pinnum = 15;
59     PINSEL_ConfigPin(&PinCfg);
60     PinCfg.Pinnum = 17;
61     PINSEL_ConfigPin(&PinCfg);
62     PinCfg.Pinnum = 18;
63     PINSEL_ConfigPin(&PinCfg);
64
65     // initialize SPI configuration structure to default
66 #if 0
67     SPI_ConfigStructInit(&SPI_ConfigStruct);
68 #else
69     SPI_ConfigStruct.CPHA = SPI_CPHA_FIRST;
70     SPI_ConfigStruct.CPOL = SPI_CPOL_HI;
71     SPI_ConfigStruct.ClockRate = 5000000;
72     SPI_ConfigStruct.DataOrder = SPI_DATA_MSB_FIRST;
73     SPI_ConfigStruct.Databit = SPI_DATABIT_8;
74     SPI_ConfigStruct.Mode = SPI_MASTER_MODE;
75 #endif
76     // Initialize SPI peripheral with parameter given in structure above
77     SPI_Init(LPC_SPI, &SPI_ConfigStruct);
78
79     ffspi_cs_init();
80 }
81
82 void ffspi_tx(unsigned char c)
83 {
84     SPI_SendData(LPC_SPI, c);
85     while (!(SPI_GetStatus(LPC_SPI) & SPI_SPSR_SPIF))
86     {
87     }
88     SPI_ReceiveData(LPC_SPI);
89 }
90
91 unsigned char ffspi_rx(void)
92 {
93     SPI_SendData(LPC_SPI, 0xff);
94     while (!(SPI_GetStatus(LPC_SPI) & SPI_SPSR_SPIF))
95     {
96     }
97     return SPI_ReceiveData(LPC_SPI);
98 }
99
100 void ffspi_dly_ms(int ms)
101 {
102     tslp_tsk(ms);
103 }
104