Skip to content

Commit e08e555

Browse files
committed
SPI : add a skeleton
1 parent 3a6e942 commit e08e555

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

debug/spi/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPI
2+
3+
spi_transfer数据结构
4+
5+
struct spi_transfer - a read/write buffer pair
6+
@tx_buf: data to be written (dma-safe memory), or NULL
7+
@rx_buf: data to be read (dma-safe memory), or NULL
8+
@len: size of rx and tx buffers (in bytes)
9+
@cs_change: affects chipselect after this transfer completes

debug/spi/spi2uart.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#include <mach/gpio.h>
2+
#include <mach/iomux.h>
3+
#include <linux/init.h>
4+
#include <linux/module.h>
5+
#include <linux/kernel.h>
6+
#include <linux/delay.h>
7+
#include <linux/err.h>
8+
#include <linux/spi/spi.h>
9+
#include <linux/platform_device.h>
10+
#include "wk2xxx.h"
11+
12+
/* global mutex lock */
13+
static DEFINE_MUTEX(wk2xxxs_reg_lock);
14+
15+
#define GPIO_DRIVER_CS_SUCK
16+
#ifdef GPIO_DRIVER_CS_SUCK
17+
#define CS_HIGH 1
18+
#define CS_LOW 0
19+
void chip_select_enable(int enable)
20+
{
21+
iomux_set(GPIO0_D7);
22+
gpio_set_value(RK30_PIN0_PD7, enable);
23+
iomux_set(SPI1_CS0);
24+
}
25+
#endif
26+
27+
static int wk2xxx_write_reg(struct spi_device *chip, uint8_t port, uint8_t reg, uint8_t data)
28+
{
29+
struct spi_message msg;
30+
uint8_t buffer_w[2];
31+
int status;
32+
struct spi_transfer index_xfer = {
33+
.len = 2,
34+
.cs_change = 1,
35+
};
36+
int sub_number;
37+
38+
sub_number = ((port - 1) << 4);
39+
40+
mutex_lock(&wk2xxxs_reg_lock);
41+
42+
spi_message_init(&msg);
43+
44+
/* 构造控制字节和数据, 参考wk2xxx手册 */
45+
buffer_w[0] = sub_number | reg;
46+
buffer_w[1] = data;
47+
48+
/* 设置发送缓冲区 */
49+
index_xfer.tx_buf = buffer_w;
50+
51+
spi_message_add_tail(&index_xfer, &msg);
52+
#ifdef GPIO_DRIVER_CS_SUCK
53+
chip_select_enable(CS_LOW);
54+
#endif
55+
status = spi_sync(chip, &msg);
56+
if(status)
57+
{
58+
printk("spi sync error\n");
59+
return status;
60+
}
61+
udelay(3);
62+
mutex_unlock(&wk2xxxs_reg_lock);
63+
64+
#ifdef GPIO_DRIVER_CS_SUCK
65+
chip_select_enable(CS_HIGH);
66+
#endif
67+
68+
return status;
69+
}
70+
71+
static int wk2xxx_read_reg(struct spi_device *chip, uint8_t port, uint8_t reg, uint8_t *data)
72+
{
73+
struct spi_message msg;
74+
uint8_t buffer_w[2];
75+
uint8_t buffer_r[2];
76+
int status = 0;
77+
int sub_number;
78+
79+
struct spi_transfer index_xfer = {
80+
.len = 2,
81+
.cs_change = 1,
82+
};
83+
84+
sub_number = ((port - 1) << 4);
85+
86+
mutex_lock(&wk2xxxs_reg_lock);
87+
spi_message_init(&msg);
88+
buffer_w[0] = 0x40 | sub_number | reg;
89+
buffer_w[1] = 0x00;
90+
buffer_r[0] = 0x00;
91+
buffer_r[1] = 0x00;
92+
93+
index_xfer.tx_buf = buffer_w;
94+
index_xfer.rx_buf = buffer_r;
95+
96+
spi_message_add_tail(&index_xfer, &msg);
97+
98+
#ifdef GPIO_DRIVER_CS_SUCK
99+
chip_select_enable(CS_LOW);
100+
#endif
101+
status = spi_sync(chip, &msg);
102+
if(status)
103+
{
104+
printk("spi sync error\n");
105+
return status;
106+
}
107+
udelay(3);
108+
mutex_unlock(&wk2xxxs_reg_lock);
109+
110+
/* 读出的数据 */
111+
*data = buffer_r[1];
112+
113+
#ifdef GPIO_DRIVER_CS_SUCK
114+
chip_select_enable(CS_HIGH);
115+
#endif
116+
117+
return 0;
118+
}
119+
120+
static int wk2xxx_remove(struct spi_device *chip)
121+
{
122+
printk("%s, %d\n", __FUNCTION__, __LINE__);
123+
124+
return 0;
125+
}
126+
127+
static int wk2xxx_probe(struct spi_device *chip)
128+
{
129+
int i;
130+
unsigned char val = 0;
131+
132+
printk("%s, %d\n", __FUNCTION__, __LINE__);
133+
134+
for (i = 0; i < 10; i++)
135+
{
136+
/* do 10 time read write */
137+
wk2xxx_read_reg(chip, WK2XXX_GPORT, WK2XXX_GENA, &val);
138+
printk("rval = 0x%x, time %d\n", val, i);
139+
if ((val & 0xF0) == 0x30)
140+
printk("Nice, :) SPI OK %d time %d\n", val, i);
141+
wk2xxx_write_reg(chip, WK2XXX_GPORT, WK2XXX_GENA, i);
142+
}
143+
144+
return 0;
145+
}
146+
147+
static struct spi_driver wk2xxx_driver = {
148+
.driver = {
149+
.name = "wk2xxxspi",
150+
.bus = &spi_bus_type,
151+
.owner = THIS_MODULE,
152+
},
153+
154+
.probe = wk2xxx_probe,
155+
.remove = wk2xxx_remove,
156+
};
157+
158+
static int wk2xxx_init(void)
159+
{
160+
int retval;
161+
retval = spi_register_driver(&wk2xxx_driver);
162+
printk("%s, %d\n", __FUNCTION__, __LINE__);
163+
return retval;
164+
}
165+
166+
static void wk2xxx_exit(void)
167+
{
168+
printk("%s, %d\n", __FUNCTION__, __LINE__);
169+
spi_unregister_driver(&wk2xxx_driver);
170+
}
171+
172+
module_init(wk2xxx_init);
173+
module_exit(wk2xxx_exit);
174+
175+
MODULE_AUTHOR("zeroway");
176+
MODULE_DESCRIPTION("wk2xxx spi2uart");
177+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)