summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsundm75 <[email protected]>2018-01-14 19:41:22 +0800
committersundm75 <[email protected]>2018-01-14 19:47:49 +0800
commit37aa65db48c4e0c499855a48eae01342ef666268 (patch)
tree05455663a2e8ac91b7fa85bb6dd06e8c8fdf9152
parenta730f40363bb08c941e12e42994901cd2e226dd9 (diff)
[bsp] Add display,sd,touch,hw_i2c driver
-rw-r--r--bsp/ls1cdev/drivers/display_controller.c233
-rw-r--r--bsp/ls1cdev/drivers/display_controller.h59
-rw-r--r--bsp/ls1cdev/drivers/hw_i2c.c147
-rw-r--r--bsp/ls1cdev/drivers/hw_i2c.h32
-rw-r--r--bsp/ls1cdev/drivers/msd.c1693
-rw-r--r--bsp/ls1cdev/drivers/msd.h132
-rw-r--r--bsp/ls1cdev/drivers/touch.c607
-rw-r--r--bsp/ls1cdev/drivers/touch.h43
8 files changed, 2946 insertions, 0 deletions
diff --git a/bsp/ls1cdev/drivers/display_controller.c b/bsp/ls1cdev/drivers/display_controller.c
new file mode 100644
index 0000000000..84bf99cd5c
--- /dev/null
+++ b/bsp/ls1cdev/drivers/display_controller.c
@@ -0,0 +1,233 @@
+/*
+ * File :display_controller.c
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2011-08-09 lgnq first version for LS1B DC
+ * 2015-07-06 chinesebear modified for loongson 1c
+ * 2018-01-06 sundm75 modified for smartloong
+ */
+
+#include <rtthread.h>
+#include "display_controller.h"
+#include "../../libraries/ls1c_pwm.h"
+#include "../../libraries/ls1c_public.h"
+#include "../../libraries/ls1c_gpio.h"
+
+struct vga_struct vga_mode[] =
+{
+ {/*"480x272_60.00"*/ 111000, 480, 482, 523, 525, 272, 274, 284, 286, },
+ {/*"640x480_70.00"*/ 28560, 640, 664, 728, 816, 480, 481, 484, 500, },
+ {/*"640x640_60.00"*/ 33100, 640, 672, 736, 832, 640, 641, 644, 663, },
+ {/*"640x768_60.00"*/ 39690, 640, 672, 736, 832, 768, 769, 772, 795, },
+ {/*"640x800_60.00"*/ 42130, 640, 680, 744, 848, 800, 801, 804, 828, },
+ {/*"800x480_70.00"*/ 35840, 800, 832, 912, 1024, 480, 481, 484, 500, },
+ {/*"800x600_60.00"*/ 38220, 800, 832, 912, 1024, 600, 601, 604, 622, },
+ {/*"800x640_60.00"*/ 40730, 800, 832, 912, 1024, 640, 641, 644, 663, },
+ {/*"832x600_60.00"*/ 40010, 832, 864, 952, 1072, 600, 601, 604, 622, },
+ {/*"832x608_60.00"*/ 40520, 832, 864, 952, 1072, 608, 609, 612, 630, },
+ {/*"1024x480_60.00"*/ 38170, 1024, 1048, 1152, 1280, 480, 481, 484, 497, },
+ {/*"1024x600_60.00"*/ 48960, 1024, 1064, 1168, 1312, 600, 601, 604, 622, },
+ {/*"1024x640_60.00"*/ 52830, 1024, 1072, 1176, 1328, 640, 641, 644, 663, },
+ {/*"1024x768_60.00"*/ 64110, 1024, 1080, 1184, 1344, 768, 769, 772, 795, },
+ {/*"1152x764_60.00"*/ 71380, 1152, 1208, 1328, 1504, 764, 765, 768, 791, },
+ {/*"1280x800_60.00"*/ 83460, 1280, 1344, 1480, 1680, 800, 801, 804, 828, },
+ {/*"1280x1024_55.00"*/ 98600, 1280, 1352, 1488, 1696, 1024, 1025, 1028, 1057, },
+ {/*"1440x800_60.00"*/ 93800, 1440, 1512, 1664, 1888, 800, 801, 804, 828, },
+ {/*"1440x900_67.00"*/ 120280, 1440, 1528, 1680, 1920, 900, 901, 904, 935, },
+};
+
+ALIGN(16)
+volatile rt_uint16_t _rt_framebuffer[FB_YSIZE][FB_XSIZE];
+static struct rt_device_graphic_info _dc_info;
+
+static void pwminit(void)
+{
+ pwm_info_t pwm_info;
+ pwm_info.gpio = LS1C_PWM0_GPIO06; // pwm引脚位gpio06
+ pwm_info.mode = PWM_MODE_NORMAL; // 正常模式--连续输出pwm波形
+ pwm_info.duty = 0.85; // pwm占空比 85%
+ pwm_info.period_ns = 5*1000*1000; // pwm周期5ms
+
+ /*pwm初始化,初始化后立即产生pwm波形*/
+ pwm_init(&pwm_info);
+
+ /* 使能pwm */
+ pwm_enable(&pwm_info);
+}
+int caclulate_freq(rt_uint32_t XIN, rt_uint32_t PCLK)
+{
+ rt_uint32_t divider_int;
+ rt_uint32_t needed_pixclk;
+ rt_uint32_t pll_clk, pix_div;
+ rt_uint32_t regval;
+
+
+ pll_clk = PLL_FREQ; // 读CPU的 PLL及SDRAM 分频系数
+ pll_clk =( pll_clk>>8 )& 0xff;
+ pll_clk = XIN * pll_clk / 4 ;
+ pix_div = PLL_DIV_PARAM;//读CPU的 CPU/CAMERA/DC 分频系数
+ pix_div = (pix_div>>24)&0xff;
+ rt_kprintf("old pll_clk=%d, pix_div=%d\n", pll_clk, pix_div);
+
+ divider_int = pll_clk/(1000000) *PCLK/1000;
+ if(divider_int%1000>=500)
+ divider_int = divider_int/1000+1;
+ else
+ divider_int = divider_int/1000;
+ rt_kprintf("divider_int = %d\n", divider_int);
+
+ /* check whether divisor is too small. */
+ if (divider_int < 1) {
+ rt_kprintf("Warning: clock source is too slow.Try smaller resolution\n");
+ divider_int = 1;
+ }
+ else if(divider_int > 100) {
+ rt_kprintf("Warning: clock source is too fast.Try smaller resolution\n");
+ divider_int = 100;
+ }
+ /* 配置分频寄存器 */
+ {
+ rt_uint32_t regval = 0;
+ regval = PLL_DIV_PARAM;
+ /*首先需要把分频使能位清零 */
+ regval &= ~0x80000030; //PIX_DIV_VALID PIX_SEL 置0
+ regval &= ~(0x3f<<24); //PIX_DIV 清零
+ regval |= divider_int << 24;
+ PLL_DIV_PARAM = regval;
+ regval |= 0x80000030; //PIX_DIV_VALID PIX_SEL 置1
+ PLL_DIV_PARAM = regval;
+ }
+ rt_kprintf("new PLL_FREQ=0x%x, PLL_DIV_PARAM=0x%x\n", PLL_FREQ, PLL_DIV_PARAM);
+ rt_thread_delay(10);
+ return 0;
+}
+
+static rt_err_t rt_dc_init(rt_device_t dev)
+{
+ int i, out, mode=-1;
+ int val;
+
+ rt_kprintf("PWM initied\n");
+ /* Set the back light PWM. */
+ pwminit();
+
+ for (i=0; i<sizeof(vga_mode)/sizeof(struct vga_struct); i++)
+ {
+ if (vga_mode[i].hr == FB_XSIZE && vga_mode[i].vr == FB_YSIZE)
+ {
+ mode=i;
+ /* 计算时钟 配置频率*/
+ caclulate_freq(OSC, vga_mode[i].pclk);
+ break;
+ }
+ }
+
+ if (mode<0)
+ {
+ rt_kprintf("\n\n\nunsupported framebuffer resolution\n\n\n");
+ return;
+ }
+
+ DC_FB_CONFIG = 0x0;
+ DC_FB_CONFIG = 0x3; // // framebuffer configuration RGB565
+ DC_FB_BUFFER_ADDR0 = (rt_uint32_t)_rt_framebuffer - 0x80000000;
+ DC_FB_BUFFER_ADDR1 = (rt_uint32_t)_rt_framebuffer - 0x80000000;
+ DC_DITHER_CONFIG = 0x0; //颜色抖动配置寄存器
+ DC_DITHER_TABLE_LOW = 0x0; //颜色抖动查找表低位寄存器
+ DC_DITHER_TABLE_HIGH = 0x0; //颜色抖动查找表高位寄存器
+ DC_PANEL_CONFIG = 0x80001311; //液晶面板配置寄存器
+ DC_PANEL_TIMING = 0x0;
+
+ DC_HDISPLAY = (vga_mode[mode].hfl<<16) | vga_mode[mode].hr;
+ DC_HSYNC = 0x40000000 | (vga_mode[mode].hse<<16) | vga_mode[mode].hss;
+ DC_VDISPLAY = (vga_mode[mode].vfl<<16) | vga_mode[mode].vr;
+ DC_VSYNC = 0x40000000 | (vga_mode[mode].vse<<16) | vga_mode[mode].vss;
+
+#if defined(CONFIG_VIDEO_32BPP)
+ DC_FB_CONFIG = 0x00100105;
+ DC_FB_BUFFER_STRIDE = FB_XSIZE*4;
+#elif defined(CONFIG_VIDEO_24BPP)
+ DC_FB_CONFIG = 0x00100104;
+ DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255);
+#elif defined(CONFIG_VIDEO_16BPP)// 使用这个选项
+ DC_FB_CONFIG = 0x00100103;
+ DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255);
+#elif defined(CONFIG_VIDEO_15BPP)
+ DC_FB_CONFIG = 0x00100102;
+ DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255);
+#elif defined(CONFIG_VIDEO_12BPP)
+ DC_FB_CONFIG = 0x00100101;
+ DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255);
+#else
+ DC_FB_CONFIG = 0x00100104;
+ DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255);
+#endif
+ return RT_EOK;
+}
+
+static rt_err_t rt_dc_control(rt_device_t dev, int cmd, void *args)
+{
+ switch (cmd)
+ {
+ case RTGRAPHIC_CTRL_RECT_UPDATE:
+ break;
+ case RTGRAPHIC_CTRL_POWERON:
+ break;
+ case RTGRAPHIC_CTRL_POWEROFF:
+ break;
+ case RTGRAPHIC_CTRL_GET_INFO:
+ rt_memcpy(args, &_dc_info, sizeof(_dc_info));
+ break;
+ case RTGRAPHIC_CTRL_SET_MODE:
+ break;
+ }
+
+ return RT_EOK;
+}
+
+void rt_hw_dc_init(void)
+{
+ rt_device_t dc = rt_malloc(sizeof(struct rt_device));
+ if (dc == RT_NULL)
+ {
+ rt_kprintf("dc == RT_NULL\n");
+ return; /* no memory yet */
+ }
+
+ _dc_info.bits_per_pixel = 16;
+ _dc_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
+ _dc_info.framebuffer = (rt_uint8_t*)HW_FB_ADDR;
+ _dc_info.width = FB_XSIZE;
+ _dc_info.height = FB_YSIZE;
+
+ /* init device structure */
+ dc->type = RT_Device_Class_Graphic;
+ dc->init = rt_dc_init;
+ dc->open = RT_NULL;
+ dc->close = RT_NULL;
+ dc->control = rt_dc_control;
+ dc->user_data = (void*)&_dc_info;
+
+ /* register Display Controller device to RT-Thread */
+ rt_device_register(dc, "dc", RT_DEVICE_FLAG_RDWR);
+
+ rt_device_init(dc);
+}
+
diff --git a/bsp/ls1cdev/drivers/display_controller.h b/bsp/ls1cdev/drivers/display_controller.h
new file mode 100644
index 0000000000..ad252a9c2f
--- /dev/null
+++ b/bsp/ls1cdev/drivers/display_controller.h
@@ -0,0 +1,59 @@
+/*
+ * File : display_controller.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006-2012, RT-Thread Develop Team
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rt-thread.org/license/LICENSE
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2011-08-08 lgnq first version for LS1B
+ * 2015-07-06 chinesebear modified for loongson 1c
+ * 2018-01-06 sundm75 modified for smartloong
+*/
+
+#ifndef __DISPLAY_CONTROLLER_H__
+#define __DISPLAY_CONTROLLER_H__
+
+#include <rtthread.h>
+#include "ls1c.h"
+
+#define DC_BASE 0xBC301240 //Display Controller
+
+/* Frame Buffer registers */
+#define DC_FB_CONFIG __REG32(DC_BASE + 0x000)
+#define DC_FB_BUFFER_ADDR0 __REG32(DC_BASE + 0x020)
+#define DC_FB_BUFFER_STRIDE __REG32(DC_BASE + 0x040)
+#define DC_FB_BUFFER_ORIGIN __REG32(DC_BASE + 0x060)
+#define DC_DITHER_CONFIG __REG32(DC_BASE + 0x120)
+#define DC_DITHER_TABLE_LOW __REG32(DC_BASE + 0x140)
+#define DC_DITHER_TABLE_HIGH __REG32(DC_BASE + 0x160)
+#define DC_PANEL_CONFIG __REG32(DC_BASE + 0x180)
+#define DC_PANEL_TIMING __REG32(DC_BASE + 0x1A0)
+#define DC_HDISPLAY __REG32(DC_BASE + 0x1C0)
+#define DC_HSYNC __REG32(DC_BASE + 0x1E0)
+#define DC_VDISPLAY __REG32(DC_BASE + 0x240)
+#define DC_VSYNC __REG32(DC_BASE + 0x260)
+#define DC_FB_BUFFER_ADDR1 __REG32(DC_BASE + 0x340)
+
+/* Display Controller driver for 1024x768 16bit */
+#define FB_XSIZE 480
+#define FB_YSIZE 272
+#define CONFIG_VIDEO_16BPP
+
+#define OSC 24000000 /* Hz */
+
+#define K1BASE 0xA0000000
+#define KSEG1(addr) ((void *)(K1BASE | (rt_uint32_t)(addr)))
+#define HW_FB_ADDR KSEG1(_rt_framebuffer)
+
+struct vga_struct
+{
+ long pclk;
+ int hr,hss,hse,hfl;
+ int vr,vss,vse,vfl;
+};
+
+#endif
diff --git a/bsp/ls1cdev/drivers/hw_i2c.c b/bsp/ls1cdev/drivers/hw_i2c.c
new file mode 100644
index 0000000000..15d84997c6
--- /dev/null
+++ b/bsp/ls1cdev/drivers/hw_i2c.c
@@ -0,0 +1,147 @@
+/*
+ * File :hw_i2c.c
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-01-04 Sundm75 the first version
+ */
+
+#include <rtthread.h>
+#include <rtdevice.h>
+#include "ls1c_i2c.h"
+
+struct ls1c_i2c_bus
+{
+ struct rt_i2c_bus_device parent;
+ rt_uint32_t u32Module;
+};
+
+rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus,
+ struct rt_i2c_msg *msgs,
+ rt_uint32_t num)
+{
+ struct ls1c_i2c_bus * i2c_bus = (struct ls1c_i2c_bus *)bus;
+ ls1c_i2c_info_t i2c_info;
+ struct rt_i2c_msg *msg;
+ int i;
+ rt_int32_t ret = RT_EOK;
+ i2c_info.clock = 50000; // 50kb/s
+ i2c_info.I2Cx = i2c_bus->u32Module;
+ i2c_init(&i2c_info);
+
+ for (i = 0; i < num; i++)
+ {
+ msg = &msgs[i];
+ if (msg->flags == RT_I2C_RD)
+ {
+ i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_READ);
+ i2c_receive_ack(&i2c_info);
+ i2c_receive_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len);
+ i2c_send_stop(&i2c_info);
+ }
+ else if(msg->flags == RT_I2C_WR)
+ {
+ i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_WRITE);
+ i2c_receive_ack(&i2c_info);
+ i2c_send_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len);
+ i2c_send_stop(&i2c_info);
+ }
+ ret++;
+ }
+ return ret;
+}
+
+rt_err_t rt_i2c_bus_control(struct rt_i2c_bus_device *bus,
+ rt_uint32_t cmd,
+ rt_uint32_t arg)
+{
+ struct ls1c_i2c_bus * i2c_bus = (struct ls1c_i2c_bus *)bus;
+
+ RT_ASSERT(bus != RT_NULL);
+ i2c_bus = (struct ls1c_i2c_bus *)bus->parent.user_data;
+
+ RT_ASSERT(i2c_bus != RT_NULL);
+
+ switch (cmd)
+ {
+ case RT_DEVICE_CTRL_CONFIG :
+ break;
+ }
+
+ return RT_EOK;
+}
+
+static const struct rt_i2c_bus_device_ops ls1c_i2c_ops =
+{
+ rt_i2c_master_xfer,
+ RT_NULL,
+ rt_i2c_bus_control
+};
+
+
+#ifdef RT_USING_I2C0
+static struct ls1c_i2c_bus ls1c_i2c_bus_0 =
+{
+ {1},
+ LS1C_I2C_0,
+};
+#endif
+
+#ifdef RT_USING_I2C1
+static struct ls1c_i2c_bus ls1c_i2c_bus_1 =
+{
+ {1},
+ LS1C_I2C_1,
+};
+#endif
+
+#ifdef RT_USING_I2C2
+static struct ls1c_i2c_bus ls1c_i2c_bus_2 =
+{
+ {1},
+ LS1C_I2C_2,
+};
+#endif
+
+int rt_i2c_init(void)
+{
+ struct ls1c_i2c_bus* ls1c_i2c;
+
+#ifdef RT_USING_I2C0
+ ls1c_i2c = &ls1c_i2c_bus_0;
+ ls1c_i2c->parent.ops = &ls1c_i2c_ops;
+ rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c0");
+ rt_kprintf("i2c0_init!\n");
+#endif
+#ifdef RT_USING_I2C1
+ ls1c_i2c = &ls1c_i2c_bus_1;
+ ls1c_i2c->parent.ops = &ls1c_i2c_ops;
+ rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c1");
+ rt_kprintf("i2c1_init!\n");
+#endif
+
+#ifdef RT_USING_I2C2
+ ls1c_i2c = &ls1c_i2c_bus_2;
+ ls1c_i2c->parent.ops = &ls1c_i2c_ops;
+ rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c2");
+ rt_kprintf("i2c2_init!\n");
+#endif
+
+ return RT_EOK;
+}
diff --git a/bsp/ls1cdev/drivers/hw_i2c.h b/bsp/ls1cdev/drivers/hw_i2c.h
new file mode 100644
index 0000000000..1bd4e991ed
--- /dev/null
+++ b/bsp/ls1cdev/drivers/hw_i2c.h
@@ -0,0 +1,32 @@
+/*
+ * File : hw_i2c.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-01-04 Sundm75 the first version
+ */
+
+#ifndef LS1C_I2C_H
+#define LS1C_I2C_H
+
+#include <rtthread.h>
+
+int rt_i2c_init(void);
+
+#endif
diff --git a/bsp/ls1cdev/drivers/msd.c b/bsp/ls1cdev/drivers/msd.c
new file mode 100644
index 0000000000..844302cb3f
--- /dev/null
+++ b/bsp/ls1cdev/drivers/msd.c
@@ -0,0 +1,1693 @@
+/*
+ * File : msd.c
+ * SPI mode SD Card Driver
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rt-thread.org/license/LICENSE
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2009-04-17 Bernard first version.
+ * 2010-07-15 aozima Modify read/write according new block driver interface.
+ * 2012-02-01 aozima use new RT-Thread SPI drivers.
+ * 2012-04-11 aozima get max. data transfer rate from CSD[TRAN_SPEED].
+ * 2012-05-21 aozima update MMC card support.
+ */
+
+#include <string.h>
+#include "msd.h"
+
+//#define MSD_TRACE
+
+#ifdef MSD_TRACE
+#define MSD_DEBUG(...) rt_kprintf("[MSD] %d ", rt_tick_get()); rt_kprintf(__VA_ARGS__);
+#else
+#define MSD_DEBUG(...)
+#endif /* #ifdef MSD_TRACE */
+
+#define DUMMY 0xFF
+
+#define CARD_NCR_MAX 8
+
+#define CARD_NRC 1
+#define CARD_NCR 1
+
+static struct msd_device _msd_device;
+
+/* function define */
+static rt_bool_t rt_tick_timeout(rt_tick_t tick_start, rt_tick_t tick_long);
+
+static rt_err_t MSD_take_owner(struct rt_spi_device* spi_device);
+static void MSD_take_cs(struct rt_spi_device* device);
+static void MSD_release_cs(struct rt_spi_device* device);
+
+static rt_err_t _wait_token(struct rt_spi_device* device, uint8_t token);
+static rt_err_t _wait_ready(struct rt_spi_device* device);
+static rt_size_t rt_msd_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size);
+static rt_size_t rt_msd_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size);
+static rt_size_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size);
+static rt_size_t rt_msd_sdhc_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size);
+
+static rt_err_t MSD_take_owner(struct rt_spi_device* spi_device)
+{
+ rt_err_t result;
+
+ result = rt_mutex_take(&(spi_device->bus->lock), RT_WAITING_FOREVER);
+ if(result == RT_EOK)
+ {
+ if (spi_device->bus->owner != spi_device)
+ {
+ /* not the same owner as current, re-configure SPI bus */
+ result = spi_device->bus->ops->configure(spi_device, &spi_device->config);
+ if (result == RT_EOK)
+ {
+ /* set SPI bus owner */
+ spi_device->bus->owner = spi_device;
+ }
+ }
+ }
+
+ return result;
+}
+
+static void MSD_take_cs(struct rt_spi_device* device)
+{
+ struct rt_spi_message message;
+
+ /* initial message */
+ message.send_buf = RT_NULL;
+ message.recv_buf = RT_NULL;
+ message.length = 0;
+ message.cs_take = 1;
+ message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+}
+
+static void MSD_release_cs(struct rt_spi_device* device)
+{
+ struct rt_spi_message message;
+
+ /* initial message */
+ message.send_buf = RT_NULL;
+ message.recv_buf = RT_NULL;
+ message.length = 0;
+ message.cs_take = 0;
+ message.cs_release = 1;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+}
+
+static rt_bool_t rt_tick_timeout(rt_tick_t tick_start, rt_tick_t tick_long)
+{
+ rt_tick_t tick_end = tick_start + tick_long;
+ rt_tick_t tick_now = rt_tick_get();
+ rt_bool_t result = RT_FALSE;
+
+ if(tick_end >= tick_start)
+ {
+ if (tick_now >= tick_end)
+ {
+ result = RT_TRUE;
+ }
+ else
+ {
+ result = RT_FALSE;
+ }
+ }
+ else
+ {
+ if ((tick_now < tick_start ) && (tick_now >= tick_end) )
+ {
+ result = RT_TRUE;
+ }
+ else
+ {
+ result = RT_FALSE;
+ }
+ }
+
+ return result;
+}
+
+static uint8_t crc7(const uint8_t *buf, int len)
+{
+ unsigned char i, j, crc, ch, ch2, ch3;
+
+ crc = 0;
+
+ for (i = 0; i < len; i ++)
+ {
+ ch = buf[i];
+
+ for (j = 0; j < 8; j ++, ch <<= 1)
+ {
+ ch2 = (crc & 0x40) ? 1 : 0;
+ ch3 = (ch & 0x80) ? 1 : 0;
+
+ if (ch2 ^ ch3)
+ {
+ crc ^= 0x04;
+ crc <<= 1;
+ crc |= 0x01;
+ }
+ else
+ {
+ crc <<= 1;
+ }
+ }
+ }
+
+ return crc;
+}
+
+static rt_err_t _send_cmd(
+ struct rt_spi_device* device,
+ uint8_t cmd,
+ uint32_t arg,
+ uint8_t crc,
+ response_type type,
+ uint8_t * response
+)
+{
+ struct rt_spi_message message;
+ uint8_t cmd_buffer[8];
+ uint8_t recv_buffer[sizeof(cmd_buffer)];
+ uint32_t i;
+
+ cmd_buffer[0] = DUMMY;
+ cmd_buffer[1] = (cmd | 0x40);
+ cmd_buffer[2] = (uint8_t)(arg >> 24);
+ cmd_buffer[3] = (uint8_t)(arg >> 16);
+ cmd_buffer[4] = (uint8_t)(arg >> 8);
+ cmd_buffer[5] = (uint8_t)(arg);
+
+ if(crc == 0x00)
+ {
+ crc = crc7(&cmd_buffer[1], 5);
+ crc = (crc<<1) | 0x01;
+ }
+ cmd_buffer[6] = (crc);
+
+ cmd_buffer[7] = DUMMY;
+
+ /* initial message */
+ message.send_buf = cmd_buffer;
+ message.recv_buf = recv_buffer;
+ message.length = sizeof(cmd_buffer);
+ message.cs_take = message.cs_release = 0;
+
+ _wait_ready(device);
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+
+ for(i=CARD_NCR; i<(CARD_NCR_MAX+1); i++)
+ {
+ uint8_t send = DUMMY;
+
+ /* initial message */
+ message.send_buf = &send;
+ message.recv_buf = response;
+ message.length = 1;
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+
+ if(0 == (response[0] & 0x80))
+ {
+ break;
+ }
+ } /* wait response */
+
+ if((CARD_NCR_MAX+1) == i)
+ {
+ return RT_ERROR;//fail
+ }
+
+ //recieve other byte
+ if(type == response_r1)
+ {
+ return RT_EOK;
+ }
+ else if(type == response_r1b)
+ {
+ rt_tick_t tick_start = rt_tick_get();
+ uint8_t recv;
+
+ while(1)
+ {
+ /* initial message */
+ message.send_buf = RT_NULL;
+ message.recv_buf = &recv;
+ message.length = 1;
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+
+ if(recv == DUMMY)
+ {
+ return RT_EOK;
+ }
+
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(2000)))
+ {
+ return RT_ETIMEOUT;
+ }
+ }
+ }
+ else if(type == response_r2)
+ {
+ /* initial message */
+ message.send_buf = RT_NULL;
+ message.recv_buf = response+1;
+ message.length = 1;
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+ }
+ else if((type == response_r3) || (type == response_r7))
+ {
+ /* initial message */
+ message.send_buf = RT_NULL;
+ message.recv_buf = response+1;
+ message.length = 4;
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+ }
+ else
+ {
+ return RT_ERROR; // unknow type?
+ }
+
+ return RT_EOK;
+}
+
+static rt_err_t _wait_token(struct rt_spi_device* device, uint8_t token)
+{
+ struct rt_spi_message message;
+ rt_tick_t tick_start;
+ uint8_t send, recv;
+
+ tick_start = rt_tick_get();
+
+ /* wati token */
+ /* initial message */
+ send = DUMMY;
+ message.send_buf = &send;
+ message.recv_buf = &recv;
+ message.length = 1;
+ message.cs_take = message.cs_release = 0;
+
+ while(1)
+ {
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+
+ if(recv == token)
+ {
+ return RT_EOK;
+ }
+
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_WAIT_TOKEN_TIMES)))
+ {
+ MSD_DEBUG("[err] wait data start token timeout!\r\n");
+ return RT_ETIMEOUT;
+ }
+ } /* wati token */
+}
+
+static rt_err_t _wait_ready(struct rt_spi_device* device)
+{
+ struct rt_spi_message message;
+ rt_tick_t tick_start;
+ uint8_t send, recv;
+
+ tick_start = rt_tick_get();
+
+ send = DUMMY;
+ /* initial message */
+ message.send_buf = &send;
+ message.recv_buf = &recv;
+ message.length = 1;
+ message.cs_take = message.cs_release = 0;
+
+ while(1)
+ {
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+
+ if(recv == DUMMY)
+ {
+ return RT_EOK;
+ }
+
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(1000)))
+ {
+ MSD_DEBUG("[err] wait ready timeout!\r\n");
+ return RT_ETIMEOUT;
+ }
+ }
+}
+
+static rt_err_t _read_block(struct rt_spi_device* device, void * buffer, uint32_t block_size)
+{
+ struct rt_spi_message message;
+ rt_err_t result;
+
+ /* wati token */
+ result = _wait_token(device, MSD_TOKEN_READ_START);
+ if(result != RT_EOK)
+ {
+ return result;
+ }
+
+ /* read data */
+ {
+ /* initial message */
+ message.send_buf = RT_NULL;
+ message.recv_buf = buffer;
+ message.length = block_size;
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+ } /* read data */
+
+ /* get crc */
+ {
+ uint8_t recv_buffer[2];
+
+ /* initial message */
+ message.send_buf = RT_NULL;
+ message.recv_buf = recv_buffer;
+ message.length = 2;
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+ } /* get crc */
+
+ return RT_EOK;
+}
+
+static rt_err_t _write_block(struct rt_spi_device* device, const void * buffer, uint32_t block_size, uint8_t token)
+{
+ struct rt_spi_message message;
+ uint8_t send_buffer[16];
+
+ rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
+ send_buffer[sizeof(send_buffer) - 1] = token;
+
+ /* send start block token */
+ {
+ /* initial message */
+ message.send_buf = send_buffer;
+ message.recv_buf = RT_NULL;
+ message.length = sizeof(send_buffer);
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+ }
+
+ /* send data */
+ {
+ /* initial message */
+ message.send_buf = buffer;
+ message.recv_buf = RT_NULL;
+ message.length = block_size;
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+ }
+
+ /* put crc and get data response */
+ {
+ uint8_t recv_buffer[3];
+ uint8_t response;
+
+ /* initial message */
+ message.send_buf = send_buffer;
+ message.recv_buf = recv_buffer;
+ message.length = sizeof(recv_buffer);
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ device->bus->ops->xfer(device, &message);
+
+// response = 0x0E & recv_buffer[2];
+ response = MSD_GET_DATA_RESPONSE(recv_buffer[2]);
+ if(response != MSD_DATA_OK)
+ {
+ MSD_DEBUG("[err] write block fail! data response : 0x%02X\r\n", response);
+ return RT_ERROR;
+ }
+ }
+
+ /* wati ready */
+ return _wait_ready(device);
+}
+
+/* RT-Thread Device Driver Interface */
+static rt_err_t rt_msd_init(rt_device_t dev)
+{
+ struct msd_device * msd = (struct msd_device *)dev;
+ uint8_t response[MSD_RESPONSE_MAX_LEN];
+ rt_err_t result = RT_EOK;
+ rt_tick_t tick_start;
+ uint32_t OCR;
+
+ if(msd->spi_device == RT_NULL)
+ {
+ MSD_DEBUG("[err] the SPI SD device has no SPI!\r\n");
+ return RT_EIO;
+ }
+
+ /* config spi */
+ {
+ struct rt_spi_configuration cfg;
+ cfg.data_width = 8;
+ cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
+ cfg.max_hz = 1000*400; /* 400kbit/s */
+ rt_spi_configure(msd->spi_device, &cfg);
+ } /* config spi */
+
+ /* init SD card */
+ {
+ struct rt_spi_message message;
+
+ result = MSD_take_owner(msd->spi_device);
+
+ if (result != RT_EOK)
+ {
+ goto _exit;
+ }
+
+ MSD_release_cs(msd->spi_device);
+
+ /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
+ start to supply at least 74 SD clocks to the SD card with keeping CMD line to high.
+ In case of SPI mode, CS shall be held to high during 74 clock cycles. */
+ {
+ uint8_t send_buffer[100]; /* 100byte > 74 clock */
+
+ /* initial message */
+ rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
+ message.send_buf = send_buffer;
+ message.recv_buf = RT_NULL;
+ message.length = sizeof(send_buffer);
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
+ } /* send 74 clock */
+
+ /* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
+ {
+ tick_start = rt_tick_get();
+
+ while(1)
+ {
+ MSD_take_cs(msd->spi_device);
+ result = _send_cmd(msd->spi_device, GO_IDLE_STATE, 0x00, 0x95, response_r1, response);
+ MSD_release_cs(msd->spi_device);
+
+ if((result == RT_EOK) && (response[0] == MSD_IN_IDLE_STATE))
+ {
+ break;
+ }
+
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
+ {
+ MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
+ result = RT_ETIMEOUT;
+ goto _exit;
+ }
+ }
+
+ MSD_DEBUG("[info] SD card goto IDLE mode OK!\r\n");
+ } /* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
+
+ /* CMD8 */
+ {
+ tick_start = rt_tick_get();
+
+ do
+ {
+ MSD_take_cs(msd->spi_device);
+ result = _send_cmd(msd->spi_device, SEND_IF_COND, 0x01AA, 0x87, response_r7, response);
+ MSD_release_cs(msd->spi_device);
+
+ if(result == RT_EOK)
+ {
+ MSD_DEBUG("[info] CMD8 response : 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\r\n",
+ response[0], response[1], response[2], response[3], response[4]);
+
+ if(response[0] & (1<<2))
+ {
+ /* illegal command, SD V1.x or MMC card */
+ MSD_DEBUG("[info] CMD8 is illegal command.\r\n");
+ MSD_DEBUG("[info] maybe Ver1.X SD Memory Card or MMC card!\r\n");
+ msd->card_type = MSD_CARD_TYPE_SD_V1_X;
+ break;
+ }
+ else
+ {
+ /* SD V2.0 or later or SDHC or SDXC memory card! */
+ MSD_DEBUG("[info] Ver2.00 or later or SDHC or SDXC memory card!\r\n");
+ msd->card_type = MSD_CARD_TYPE_SD_V2_X;
+ }
+
+ if((0xAA == response[4]) && (0x00 == response[3]))
+ {
+ /* SD2.0 not support current voltage */
+ MSD_DEBUG("[err] VCA = 0, SD2.0 not surpport current operation voltage range\r\n");
+ result = RT_ERROR;
+ goto _exit;
+ }
+ }
+ else
+ {
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(200)))
+ {
+ MSD_DEBUG("[err] CMD8 SEND_IF_COND timeout!\r\n");
+ result = RT_ETIMEOUT;
+ goto _exit;
+ }
+ }
+ }
+ while(0xAA != response[4]);
+ } /* CMD8 */
+
+ /* Ver1.X SD Memory Card or MMC card */
+ if(msd->card_type == MSD_CARD_TYPE_SD_V1_X)
+ {
+ rt_bool_t is_sd_v1_x = RT_FALSE;
+ rt_tick_t tick_start;
+
+ /* try SD Ver1.x */
+ while(1)
+ {
+ MSD_take_cs(msd->spi_device);
+
+ result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
+ if(result != RT_EOK)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[info] It maybe SD1.x or MMC But it is Not response to CMD58!\r\n");
+ goto _exit;
+ }
+
+ if(0 != (response[0]&0xFE))
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[info] It look CMD58 as illegal command so it is not SD card!\r\n");
+ break;
+ }
+ MSD_release_cs(msd->spi_device);
+
+ OCR = response[1];
+ OCR = (OCR<<8) + response[2];
+ OCR = (OCR<<8) + response[3];
+ OCR = (OCR<<8) + response[4];
+ MSD_DEBUG("[info] OCR is 0x%08X\r\n", OCR);
+
+ if( 0 == (OCR & (0x1 << 15)))
+ {
+ MSD_DEBUG(("[err] SD 1.x But not surpport current voltage\r\n"));
+ result = RT_ERROR;
+ goto _exit;
+ }
+
+ /* --Send ACMD41 to make card ready */
+ tick_start = rt_tick_get();
+
+ /* try CMD55 + ACMD41 */
+ while(1)
+ {
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES_ACMD41)))
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[info] try CMD55 + ACMD41 timeout! mabey MMC card!\r\n");
+ break;
+ }
+
+ MSD_take_cs(msd->spi_device);
+
+ /* CMD55 APP_CMD */
+ result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
+ if(result != RT_EOK)
+ {
+ MSD_release_cs(msd->spi_device);
+ continue;
+ }
+
+ if(0 != (response[0]&0xFE))
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[info] Not SD card2 , may be MMC\r\n");
+ break;
+ }
+
+ /* ACMD41 SD_SEND_OP_COND */
+ result = _send_cmd(msd->spi_device, SD_SEND_OP_COND, 0x00, 0x00, response_r1, response);
+ if(result != RT_EOK)
+ {
+ MSD_release_cs(msd->spi_device);
+ continue;
+ }
+
+ if(0 != (response[0]&0xFE))
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[info] Not SD card4 , may be MMC\r\n");
+ break;
+ }
+
+ if(0 == (response[0]&0xFF))
+ {
+ MSD_release_cs(msd->spi_device);
+ is_sd_v1_x = RT_TRUE;
+ MSD_DEBUG("[info] It is Ver1.X SD Memory Card!!!\r\n");
+ break;
+ }
+ } /* try CMD55 + ACMD41 */
+
+ break;
+ } /* try SD Ver1.x */
+
+ /* try MMC */
+ if(is_sd_v1_x != RT_TRUE)
+ {
+ uint32_t i;
+
+ MSD_DEBUG("[info] try MMC card!\r\n");
+ MSD_release_cs(msd->spi_device);
+
+ /* send dummy clock */
+ {
+ uint8_t send_buffer[100];
+
+ /* initial message */
+ memset(send_buffer, DUMMY, sizeof(send_buffer));
+ message.send_buf = send_buffer;
+ message.recv_buf = RT_NULL;
+ message.length = sizeof(send_buffer);
+ message.cs_take = message.cs_release = 0;
+
+ for(i=0; i<10; i++)
+ {
+ /* transfer message */
+ msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
+ }
+ } /* send dummy clock */
+
+ /* send CMD0 goto IDLE state */
+ tick_start = rt_tick_get();
+ while(1)
+ {
+ MSD_take_cs(msd->spi_device);
+ result = _send_cmd(msd->spi_device, GO_IDLE_STATE, 0x00, 0x95, response_r1, response);
+ MSD_release_cs(msd->spi_device);
+
+ if((result == RT_EOK) && (response[0] == MSD_IN_IDLE_STATE))
+ {
+ break;
+ }
+
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
+ {
+ MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
+ result = RT_ETIMEOUT;
+ goto _exit;
+ }
+ } /* send CMD0 goto IDLE stat */
+
+ /* send CMD1 */
+ tick_start = rt_tick_get();
+ while(1)
+ {
+ MSD_take_cs(msd->spi_device);
+ result = _send_cmd(msd->spi_device, SEND_OP_COND, 0x00, 0x00, response_r1, response);
+ MSD_release_cs(msd->spi_device);
+
+ if((result == RT_EOK) && (response[0] == MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[info] It is MMC card!!!\r\n");
+ msd->card_type = MSD_CARD_TYPE_MMC;
+ break;
+ }
+
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
+ {
+ MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
+ result = RT_ETIMEOUT;
+ goto _exit;
+ }
+ } /* send CMD1 */
+ } /* try MMC */
+ }
+ else if(msd->card_type == MSD_CARD_TYPE_SD_V2_X)
+ {
+ MSD_take_cs(msd->spi_device);
+
+ result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
+ if(result != RT_EOK)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] It maybe SD2.0 But it is Not response to CMD58!\r\n");
+ goto _exit;
+ }
+
+ if((response[0] & 0xFE) != 0)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] It look CMD58 as illegal command so it is not SD card!\r\n");
+ result = RT_ERROR;
+ goto _exit;
+ }
+
+ MSD_release_cs(msd->spi_device);
+
+ OCR = response[1];
+ OCR = (OCR<<8) + response[2];
+ OCR = (OCR<<8) + response[3];
+ OCR = (OCR<<8) + response[4];
+ MSD_DEBUG("[info] OCR is 0x%08X\r\n", OCR);
+
+ if( 0 == (OCR & (0x1 << 15)))
+ {
+ MSD_DEBUG(("[err] SD 1.x But not surpport current voltage\r\n"));
+ result = RT_ERROR;
+ goto _exit;
+ }
+
+ /* --Send ACMD41 to make card ready */
+ tick_start = rt_tick_get();
+
+ /* try CMD55 + ACMD41 */
+ do
+ {
+ MSD_take_cs(msd->spi_device);
+ if(rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES_ACMD41)))
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] SD Ver2.x or later try CMD55 + ACMD41 timeout!\r\n");
+ result = RT_ERROR;
+ goto _exit;
+ }
+
+ /* CMD55 APP_CMD */
+ result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x65, response_r1, response);
+// if((result != RT_EOK) || (response[0] == 0x01))
+ if(result != RT_EOK)
+ {
+ MSD_release_cs(msd->spi_device);
+ continue;
+ }
+
+ if((response[0] & 0xFE) != 0)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] Not SD ready!\r\n");
+ result = RT_ERROR;
+ goto _exit;
+ }
+
+ /* ACMD41 SD_SEND_OP_COND */
+ result = _send_cmd(msd->spi_device, SD_SEND_OP_COND, 0x40000000, 0x77, response_r1, response);
+ if(result != RT_EOK)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] ACMD41 fail!\r\n");
+ result = RT_ERROR;
+ goto _exit;
+ }
+
+ if((response[0] & 0xFE) != 0)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[info] Not SD card4 , response : 0x%02X\r\n", response[0]);
+// break;
+ }
+ }
+ while(response[0] != MSD_RESPONSE_NO_ERROR);
+ MSD_release_cs(msd->spi_device);
+ /* try CMD55 + ACMD41 */
+
+ /* --Read OCR again */
+ MSD_take_cs(msd->spi_device);
+ result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
+ if(result != RT_EOK)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] It maybe SD2.0 But it is Not response to 2nd CMD58!\r\n");
+ goto _exit;
+ }
+
+ if((response[0] & 0xFE) != 0)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] It look 2nd CMD58 as illegal command so it is not SD card!\r\n");
+ result = RT_ERROR;
+ goto _exit;
+ }
+ MSD_release_cs(msd->spi_device);
+
+ OCR = response[1];
+ OCR = (OCR<<8) + response[2];
+ OCR = (OCR<<8) + response[3];
+ OCR = (OCR<<8) + response[4];
+ MSD_DEBUG("[info] OCR 2nd read is 0x%08X\r\n", OCR);
+
+ if((OCR & 0x40000000) != 0)
+ {
+ MSD_DEBUG("[info] It is SD2.0 SDHC Card!!!\r\n");
+ msd->card_type = MSD_CARD_TYPE_SD_SDHC;
+ }
+ else
+ {
+ MSD_DEBUG("[info] It is SD2.0 standard capacity Card!!!\r\n");
+ }
+ } /* MSD_CARD_TYPE_SD_V2_X */
+ else
+ {
+ MSD_DEBUG("[err] SD card type unkonw!\r\n");
+ result = RT_ERROR;
+ goto _exit;
+ }
+ } /* init SD card */
+
+ if(msd->card_type == MSD_CARD_TYPE_SD_SDHC)
+ {
+ dev->read = rt_msd_sdhc_read;
+ dev->write = rt_msd_sdhc_write;
+ }
+ else
+ {
+ dev->read = rt_msd_read;
+ dev->write = rt_msd_write;
+ }
+
+ /* set CRC */
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_take_cs(msd->spi_device);
+#ifdef MSD_USE_CRC
+ result = _send_cmd(msd->spi_device, CRC_ON_OFF, 0x01, 0x83, response_r1, response);
+#else
+ result = _send_cmd(msd->spi_device, CRC_ON_OFF, 0x00, 0x91, response_r1, response);
+#endif
+ MSD_release_cs(msd->spi_device);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] CMD59 CRC_ON_OFF fail! response : 0x%02X\r\n", response[0]);
+ result = RT_ERROR;
+ goto _exit;
+ }
+ } /* set CRC */
+
+ /* CMD16 SET_BLOCKLEN */
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_take_cs(msd->spi_device);
+ result = _send_cmd(msd->spi_device, SET_BLOCKLEN, SECTOR_SIZE, 0x00, response_r1, response);
+ MSD_release_cs(msd->spi_device);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] CMD16 SET_BLOCKLEN fail! response : 0x%02X\r\n", response[0]);
+ result = RT_ERROR;
+ goto _exit;
+ }
+ msd->geometry.block_size = SECTOR_SIZE;
+ msd->geometry.bytes_per_sector = SECTOR_SIZE;
+ }
+
+ /* read CSD */
+ {
+ uint8_t CSD_buffer[MSD_CSD_LEN];
+
+ MSD_take_cs(msd->spi_device);
+// result = _send_cmd(msd->spi_device, SEND_CSD, 0x00, 0xAF, response_r1, response);
+ result = _send_cmd(msd->spi_device, SEND_CSD, 0x00, 0x00, response_r1, response);
+
+ if(result != RT_EOK)
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] CMD9 SEND_CSD timeout!\r\n");
+ goto _exit;
+ }
+
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_release_cs(msd->spi_device);
+ MSD_DEBUG("[err] CMD9 SEND_CSD fail! response : 0x%02X\r\n", response[0]);
+ result = RT_ERROR;
+ goto _exit;
+ }
+
+ result = _read_block(msd->spi_device, CSD_buffer, MSD_CSD_LEN);
+ MSD_release_cs(msd->spi_device);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] read CSD fail!\r\n");
+ goto _exit;
+ }
+
+ /* Analyze CSD */
+ {
+ uint8_t CSD_STRUCTURE;
+ uint32_t C_SIZE;
+ uint32_t card_capacity;
+
+ uint8_t tmp8;
+ uint16_t tmp16;
+ uint32_t tmp32;
+
+ /* get CSD_STRUCTURE */
+ tmp8 = CSD_buffer[0] & 0xC0; /* 0b11000000 */
+ CSD_STRUCTURE = tmp8 >> 6;
+
+ /* MMC CSD Analyze. */
+ if(msd->card_type == MSD_CARD_TYPE_MMC)
+ {
+ uint8_t C_SIZE_MULT;
+ uint8_t READ_BL_LEN;
+
+ if(CSD_STRUCTURE > 2)
+ {
+ MSD_DEBUG("[err] bad CSD Version : %d\r\n", CSD_STRUCTURE);
+ result = RT_ERROR;
+ goto _exit;
+ }
+
+ if(CSD_STRUCTURE == 0)
+ {
+ MSD_DEBUG("[info] CSD version No. 1.0\r\n");
+ }
+ else if(CSD_STRUCTURE == 1)
+ {
+ MSD_DEBUG("[info] CSD version No. 1.1\r\n");
+ }
+ else if(CSD_STRUCTURE == 2)
+ {
+ MSD_DEBUG("[info] CSD version No. 1.2\r\n");
+ }
+
+ /* get TRAN_SPEED 8bit [103:96] */
+ tmp8 = CSD_buffer[3];
+ tmp8 &= 0x03; /* [2:0] transfer rate unit.*/
+ if(tmp8 == 0)
+ {
+ msd->max_clock = 100 * 1000; /* 0=100kbit/s. */
+ }
+ else if(tmp8 == 1)
+ {
+ msd->max_clock = 1 * 1000 * 1000; /* 1=1Mbit/s. */
+ }
+ else if(tmp8 == 2)
+ {
+ msd->max_clock = 10 * 1000 * 1000; /* 2=10Mbit/s. */
+ }
+ else if(tmp8 == 3)
+ {
+ msd->max_clock = 100 * 1000 * 1000; /* 3=100Mbit/s. */
+ }
+ if(tmp8 == 0)
+ {
+ MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dkbit/s.\r\n", tmp8, msd->max_clock/1000);
+ }
+ else
+ {
+ MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock/1000/1000);
+ }
+
+ /* get READ_BL_LEN 4bit [83:80] */
+ tmp8 = CSD_buffer[5] & 0x0F; /* 0b00001111; */
+ READ_BL_LEN = tmp8; /* 4 bit */
+ MSD_DEBUG("[info] CSD : READ_BL_LEN : %d %dbyte\r\n", READ_BL_LEN, (1 << READ_BL_LEN));
+
+ /* get C_SIZE 12bit [73:62] */
+ tmp16 = CSD_buffer[6] & 0x03; /* get [73:72] 0b00000011 */
+ tmp16 = tmp16<<8;
+ tmp16 += CSD_buffer[7]; /* get [71:64] */
+ tmp16 = tmp16<<2;
+ tmp8 = CSD_buffer[8] & 0xC0; /* get [63:62] 0b11000000 */
+ tmp8 = tmp8>>6;
+ tmp16 = tmp16 + tmp8;
+ C_SIZE = tmp16; //12 bit
+ MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
+
+ /* get C_SIZE_MULT 3bit [49:47] */
+ tmp8 = CSD_buffer[9] & 0x03;//0b00000011;
+ tmp8 = tmp8<<1;
+ tmp8 = tmp8 + ((CSD_buffer[10] & 0x80/*0b10000000*/)>>7);
+ C_SIZE_MULT = tmp8; // 3 bit
+ MSD_DEBUG("[info] CSD : C_SIZE_MULT : %d\r\n", C_SIZE_MULT);
+
+ /* memory capacity = BLOCKNR * BLOCK_LEN */
+ /* BLOCKNR = (C_SIZE+1) * MULT */
+ /* MULT = 2^(C_SIZE_MULT+2) */
+ /* BLOCK_LEN = 2^READ_BL_LEN */
+ card_capacity = (1 << READ_BL_LEN) * ((C_SIZE + 1) * (1 << (C_SIZE_MULT+2)));
+ msd->geometry.sector_count = card_capacity / msd->geometry.bytes_per_sector;
+ MSD_DEBUG("[info] card capacity : %d Mbyte\r\n", card_capacity/(1024*1024));
+ }
+ else /* SD CSD Analyze. */
+ {
+ if(CSD_STRUCTURE == 0)
+ {
+ uint8_t C_SIZE_MULT;
+ uint8_t READ_BL_LEN;
+
+ MSD_DEBUG("[info] CSD Version 1.0\r\n");
+
+ /* get TRAN_SPEED 8bit [103:96] */
+ tmp8 = CSD_buffer[3];
+ if(tmp8 == 0x32)
+ {
+ msd->max_clock = 1000 * 1000 * 10; /* 10Mbit/s. */
+ }
+ else if(tmp8 == 0x5A)
+ {
+ msd->max_clock = 1000 * 1000 * 50; /* 50Mbit/s. */
+ }
+ else
+ {
+ msd->max_clock = 1000 * 1000 * 1; /* 1Mbit/s default. */
+ }
+ MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock/1000/1000);
+
+ /* get READ_BL_LEN 4bit [83:80] */
+ tmp8 = CSD_buffer[5] & 0x0F; /* 0b00001111; */
+ READ_BL_LEN = tmp8; /* 4 bit */
+ MSD_DEBUG("[info] CSD : READ_BL_LEN : %d %dbyte\r\n", READ_BL_LEN, (1 << READ_BL_LEN));
+
+ /* get C_SIZE 12bit [73:62] */
+ tmp16 = CSD_buffer[6] & 0x03; /* get [73:72] 0b00000011 */
+ tmp16 = tmp16<<8;
+ tmp16 += CSD_buffer[7]; /* get [71:64] */
+ tmp16 = tmp16<<2;
+ tmp8 = CSD_buffer[8] & 0xC0; /* get [63:62] 0b11000000 */
+ tmp8 = tmp8>>6;
+ tmp16 = tmp16 + tmp8;
+ C_SIZE = tmp16; //12 bit
+ MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
+
+ /* get C_SIZE_MULT 3bit [49:47] */
+ tmp8 = CSD_buffer[9] & 0x03;//0b00000011;
+ tmp8 = tmp8<<1;
+ tmp8 = tmp8 + ((CSD_buffer[10] & 0x80/*0b10000000*/)>>7);
+ C_SIZE_MULT = tmp8; // 3 bit
+ MSD_DEBUG("[info] CSD : C_SIZE_MULT : %d\r\n", C_SIZE_MULT);
+
+ /* memory capacity = BLOCKNR * BLOCK_LEN */
+ /* BLOCKNR = (C_SIZE+1) * MULT */
+ /* MULT = 2^(C_SIZE_MULT+2) */
+ /* BLOCK_LEN = 2^READ_BL_LEN */
+ card_capacity = (1 << READ_BL_LEN) * ((C_SIZE + 1) * (1 << (C_SIZE_MULT+2)));
+ msd->geometry.sector_count = card_capacity / msd->geometry.bytes_per_sector;
+ MSD_DEBUG("[info] card capacity : %d Mbyte\r\n", card_capacity/(1024*1024));
+ }
+ else if(CSD_STRUCTURE == 1)
+ {
+ MSD_DEBUG("[info] CSD Version 2.0\r\n");
+
+ /* get TRAN_SPEED 8bit [103:96] */
+ tmp8 = CSD_buffer[3];
+ if(tmp8 == 0x32)
+ {
+ msd->max_clock = 1000 * 1000 * 10; /* 10Mbit/s. */
+ }
+ else if(tmp8 == 0x5A)
+ {
+ msd->max_clock = 1000 * 1000 * 50; /* 50Mbit/s. */
+ }
+ else if(tmp8 == 0x0B)
+ {
+ msd->max_clock = 1000 * 1000 * 100; /* 100Mbit/s. */
+ /* UHS50 Card sets TRAN_SPEED to 0Bh (100Mbit/sec), */
+ /* for both SDR50 and DDR50 modes. */
+ }
+ else if(tmp8 == 0x2B)
+ {
+ msd->max_clock = 1000 * 1000 * 200; /* 200Mbit/s. */
+ /* UHS104 Card sets TRAN_SPEED to 2Bh (200Mbit/sec). */
+ }
+ else
+ {
+ msd->max_clock = 1000 * 1000 * 1; /* 1Mbit/s default. */
+ }
+ MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock/1000/1000);
+
+ /* get C_SIZE 22bit [69:48] */
+ tmp32 = CSD_buffer[7] & 0x3F; /* 0b00111111 */
+ tmp32 = tmp32<<8;
+ tmp32 += CSD_buffer[8];
+ tmp32 = tmp32<<8;
+ tmp32 += CSD_buffer[9];
+ C_SIZE = tmp32;
+ MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
+
+ /* memory capacity = (C_SIZE+1) * 512K byte */
+ card_capacity = (C_SIZE + 1) / 2; /* unit : Mbyte */
+ msd->geometry.sector_count = card_capacity * 1024; /* 1 Mbyte = 512 byte X 2048 */
+ MSD_DEBUG("[info] card capacity : %d.%d Gbyte\r\n", card_capacity/1024, (card_capacity%1024)*100/1024);
+ }
+ else
+ {
+ MSD_DEBUG("[err] bad CSD Version : %d\r\n", CSD_STRUCTURE);
+ result = RT_ERROR;
+ goto _exit;
+ }
+ } /* SD CSD Analyze. */
+ } /* Analyze CSD */
+
+ } /* read CSD */
+
+ /* config spi to high speed */
+ {
+ struct rt_spi_configuration cfg;
+ cfg.data_width = 8;
+ cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
+ cfg.max_hz = msd->max_clock;
+
+ rt_spi_configure(msd->spi_device, &cfg);
+ } /* config spi */
+
+_exit:
+ MSD_release_cs(msd->spi_device);
+ rt_mutex_release(&(msd->spi_device->bus->lock));
+ return result;
+}
+
+static rt_err_t rt_msd_open(rt_device_t dev, rt_uint16_t oflag)
+{
+// struct msd_device * msd = (struct msd_device *)dev;
+ return RT_EOK;
+}
+
+static rt_err_t rt_msd_close(rt_device_t dev)
+{
+// struct msd_device * msd = (struct msd_device *)dev;
+ return RT_EOK;
+}
+
+static rt_size_t rt_msd_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
+{
+ struct msd_device * msd = (struct msd_device *)dev;
+ uint8_t response[MSD_RESPONSE_MAX_LEN];
+ rt_err_t result = RT_EOK;
+
+ result = MSD_take_owner(msd->spi_device);
+
+ if (result != RT_EOK)
+ {
+ goto _exit;
+ }
+
+ /* config spi to high speed */
+ {
+ struct rt_spi_configuration cfg;
+ cfg.data_width = 8;
+ cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
+ cfg.max_hz = msd->max_clock;
+
+ rt_spi_configure(msd->spi_device, &cfg);
+ } /* config spi */
+
+ /* SINGLE_BLOCK? */
+ if(size == 1)
+ {
+ MSD_take_cs(msd->spi_device);
+
+ result = _send_cmd(msd->spi_device, READ_SINGLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
+ size = 0;
+ goto _exit;
+ }
+
+ result = _read_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
+ size = 0;
+ }
+ }
+ else if(size > 1)
+ {
+ uint32_t i;
+
+ MSD_take_cs(msd->spi_device);
+
+ result = _send_cmd(msd->spi_device, READ_MULTIPLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
+ size = 0;
+ goto _exit;
+ }
+
+ for(i=0; i<size; i++)
+ {
+ result = _read_block(msd->spi_device,
+ (uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
+ msd->geometry.bytes_per_sector);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
+ size = i;
+ break;
+ }
+ }
+
+ /* send CMD12 stop transfer */
+ result = _send_cmd(msd->spi_device, STOP_TRANSMISSION, 0x00, 0x00, response_r1b, response);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK, send stop token fail!\r\n");
+ }
+ } /* READ_MULTIPLE_BLOCK */
+
+_exit:
+ /* release and exit */
+ MSD_release_cs(msd->spi_device);
+ rt_mutex_release(&(msd->spi_device->bus->lock));
+
+ return size;
+}
+
+static rt_size_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
+{
+ struct msd_device * msd = (struct msd_device *)dev;
+ uint8_t response[MSD_RESPONSE_MAX_LEN];
+ rt_err_t result = RT_EOK;
+
+ result = MSD_take_owner(msd->spi_device);
+
+ if (result != RT_EOK)
+ {
+ goto _exit;
+ }
+
+ /* config spi to high speed */
+ {
+ struct rt_spi_configuration cfg;
+ cfg.data_width = 8;
+ cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
+ cfg.max_hz = msd->max_clock;
+
+ rt_spi_configure(msd->spi_device, &cfg);
+ } /* config spi */
+
+ /* SINGLE_BLOCK? */
+ if(size == 1)
+ {
+ MSD_take_cs(msd->spi_device);
+
+ result = _send_cmd(msd->spi_device, READ_SINGLE_BLOCK, pos, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
+ size = 0;
+ goto _exit;
+ }
+
+ result = _read_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
+ size = 0;
+ }
+ }
+ else if(size > 1)
+ {
+ uint32_t i;
+
+ MSD_take_cs(msd->spi_device);
+
+ result = _send_cmd(msd->spi_device, READ_MULTIPLE_BLOCK, pos, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
+ size = 0;
+ goto _exit;
+ }
+
+ for(i=0; i<size; i++)
+ {
+ result = _read_block(msd->spi_device,
+ (uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
+ msd->geometry.bytes_per_sector);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
+ size = i;
+ break;
+ }
+ }
+
+ /* send CMD12 stop transfer */
+ result = _send_cmd(msd->spi_device, STOP_TRANSMISSION, 0x00, 0x00, response_r1b, response);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK, send stop token fail!\r\n");
+ }
+ } /* READ_MULTIPLE_BLOCK */
+
+_exit:
+ /* release and exit */
+ MSD_release_cs(msd->spi_device);
+ rt_mutex_release(&(msd->spi_device->bus->lock));
+
+ return size;
+}
+
+static rt_size_t rt_msd_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
+{
+ struct msd_device * msd = (struct msd_device *)dev;
+ uint8_t response[MSD_RESPONSE_MAX_LEN];
+ rt_err_t result;
+
+ result = MSD_take_owner(msd->spi_device);
+
+ if (result != RT_EOK)
+ {
+ MSD_DEBUG("[err] get SPI owner fail!\r\n");
+ goto _exit;
+ }
+
+ /* config spi to high speed */
+ {
+ struct rt_spi_configuration cfg;
+ cfg.data_width = 8;
+ cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
+ cfg.max_hz = msd->max_clock;
+
+ rt_spi_configure(msd->spi_device, &cfg);
+ } /* config spi */
+
+ /* SINGLE_BLOCK? */
+ if(size == 1)
+ {
+ MSD_take_cs(msd->spi_device);
+ result = _send_cmd(msd->spi_device, WRITE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] CMD WRITE_BLOCK fail!\r\n");
+ size = 0;
+ goto _exit;
+ }
+
+ result = _write_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector, MSD_TOKEN_WRITE_SINGLE_START);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
+ size = 0;
+ }
+ }
+ else if(size > 1)
+ {
+ struct rt_spi_message message;
+ uint32_t i;
+
+ MSD_take_cs(msd->spi_device);
+
+#ifdef MSD_USE_PRE_ERASED
+ if(msd->card_type != MSD_CARD_TYPE_MMC)
+ {
+ /* CMD55 APP_CMD */
+ result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] CMD55 APP_CMD fail!\r\n");
+ size = 0;
+ goto _exit;
+ }
+
+ /* ACMD23 Pre-erased */
+ result = _send_cmd(msd->spi_device, SET_WR_BLK_ERASE_COUNT, size, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] ACMD23 SET_BLOCK_COUNT fail!\r\n");
+ size = 0;
+ goto _exit;
+ }
+ }
+#endif
+
+ result = _send_cmd(msd->spi_device, WRITE_MULTIPLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] CMD WRITE_MULTIPLE_BLOCK fail!\r\n");
+ size = 0;
+ goto _exit;
+ }
+
+ /* write all block */
+ for(i=0; i<size; i++)
+ {
+ result = _write_block(msd->spi_device,
+ (const uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
+ msd->geometry.bytes_per_sector,
+ MSD_TOKEN_WRITE_MULTIPLE_START);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
+ size = i;
+ break;
+ }
+ } /* write all block */
+
+ /* send stop token */
+ {
+ uint8_t send_buffer[18];
+
+ rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
+ send_buffer[sizeof(send_buffer) - 1] = MSD_TOKEN_WRITE_MULTIPLE_STOP;
+
+ /* initial message */
+ message.send_buf = send_buffer;
+ message.recv_buf = RT_NULL;
+ message.length = sizeof(send_buffer);
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
+ }
+
+ /* wait ready */
+ result = _wait_ready(msd->spi_device);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[warning] wait WRITE_MULTIPLE_BLOCK stop token ready timeout!\r\n");
+ }
+ } /* size > 1 */
+
+_exit:
+ /* release and exit */
+ MSD_release_cs(msd->spi_device);
+ rt_mutex_release(&(msd->spi_device->bus->lock));
+
+ return size;
+}
+
+static rt_size_t rt_msd_sdhc_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
+{
+ struct msd_device * msd = (struct msd_device *)dev;
+ uint8_t response[MSD_RESPONSE_MAX_LEN];
+ rt_err_t result;
+
+ result = MSD_take_owner(msd->spi_device);
+
+ if (result != RT_EOK)
+ {
+ goto _exit;
+ }
+
+ /* config spi to high speed */
+ {
+ struct rt_spi_configuration cfg;
+ cfg.data_width = 8;
+ cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
+ cfg.max_hz = msd->max_clock;
+
+ rt_spi_configure(msd->spi_device, &cfg);
+ } /* config spi */
+
+ /* SINGLE_BLOCK? */
+ if(size == 1)
+ {
+ MSD_take_cs(msd->spi_device);
+ result = _send_cmd(msd->spi_device, WRITE_BLOCK, pos, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] CMD WRITE_BLOCK fail!\r\n");
+ size = 0;
+ goto _exit;
+ }
+
+ result = _write_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector, MSD_TOKEN_WRITE_SINGLE_START);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
+ size = 0;
+ }
+ }
+ else if(size > 1)
+ {
+ struct rt_spi_message message;
+ uint32_t i;
+
+ MSD_take_cs(msd->spi_device);
+
+#ifdef MSD_USE_PRE_ERASED
+ /* CMD55 APP_CMD */
+ result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] CMD55 APP_CMD fail!\r\n");
+ size = 0;
+ goto _exit;
+ }
+
+ /* ACMD23 Pre-erased */
+ result = _send_cmd(msd->spi_device, SET_WR_BLK_ERASE_COUNT, size, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] ACMD23 SET_BLOCK_COUNT fail!\r\n");
+ size = 0;
+ goto _exit;
+ }
+#endif
+
+ result = _send_cmd(msd->spi_device, WRITE_MULTIPLE_BLOCK, pos, 0x00, response_r1, response);
+ if((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
+ {
+ MSD_DEBUG("[err] CMD WRITE_MULTIPLE_BLOCK fail!\r\n");
+ size = 0;
+ goto _exit;
+ }
+
+ /* write all block */
+ for(i=0; i<size; i++)
+ {
+ result = _write_block(msd->spi_device,
+ (const uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
+ msd->geometry.bytes_per_sector,
+ MSD_TOKEN_WRITE_MULTIPLE_START);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[err] write MULTIPLE_BLOCK #%d fail!\r\n", pos);
+ size = i;
+ break;
+ }
+ } /* write all block */
+
+ /* send stop token */
+ {
+ uint8_t send_buffer[18];
+
+ rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
+ send_buffer[sizeof(send_buffer) - 1] = MSD_TOKEN_WRITE_MULTIPLE_STOP;
+
+ /* initial message */
+ message.send_buf = send_buffer;
+ message.recv_buf = RT_NULL;
+ message.length = sizeof(send_buffer);
+ message.cs_take = message.cs_release = 0;
+
+ /* transfer message */
+ msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
+ }
+
+ result = _wait_ready(msd->spi_device);
+ if(result != RT_EOK)
+ {
+ MSD_DEBUG("[warning] wait WRITE_MULTIPLE_BLOCK stop token ready timeout!\r\n");
+ }
+ } /* size > 1 */
+
+_exit:
+ /* release and exit */
+ MSD_release_cs(msd->spi_device);
+ rt_mutex_release(&(msd->spi_device->bus->lock));
+
+ return size;
+}
+
+static rt_err_t rt_msd_control(rt_device_t dev, int cmd, void *args)
+{
+ struct msd_device * msd = (struct msd_device *)dev;
+
+ RT_ASSERT(dev != RT_NULL);
+
+ if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
+ {
+ struct rt_device_blk_geometry *geometry;
+
+ geometry = (struct rt_device_blk_geometry *)args;
+ if (geometry == RT_NULL) return -RT_ERROR;
+
+ geometry->bytes_per_sector = msd->geometry.bytes_per_sector;
+ geometry->block_size = msd->geometry.block_size;
+ geometry->sector_count = msd->geometry.sector_count;
+ }
+
+ return RT_EOK;
+}
+
+rt_err_t msd_init(const char * sd_device_name, const char * spi_device_name)
+{
+ rt_err_t result = RT_EOK;
+ struct rt_spi_device * spi_device;
+
+ spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
+ if(spi_device == RT_NULL)
+ {
+ MSD_DEBUG("spi device %s not found!\r\n", spi_device_name);
+ return -RT_ENOSYS;
+ }
+
+ rt_memset(&_msd_device, 0, sizeof(_msd_device));
+ _msd_device.spi_device = spi_device;
+
+ /* register sdcard device */
+ _msd_device.parent.type = RT_Device_Class_Block;
+
+ _msd_device.geometry.bytes_per_sector = 0;
+ _msd_device.geometry.sector_count = 0;
+ _msd_device.geometry.block_size = 0;
+
+ _msd_device.parent.init = rt_msd_init;
+ _msd_device.parent.open = rt_msd_open;
+ _msd_device.parent.close = rt_msd_close;
+ _msd_device.parent.read = RT_NULL;
+ _msd_device.parent.write = RT_NULL;
+ _msd_device.parent.control = rt_msd_control;
+
+ /* no private, no callback */
+ _msd_device.parent.user_data = RT_NULL;
+ _msd_device.parent.rx_indicate = RT_NULL;
+ _msd_device.parent.tx_complete = RT_NULL;
+
+ result = rt_device_register(&_msd_device.parent, sd_device_name,
+ RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
+
+
+ return result;
+}
diff --git a/bsp/ls1cdev/drivers/msd.h b/bsp/ls1cdev/drivers/msd.h
new file mode 100644
index 0000000000..e004944332
--- /dev/null
+++ b/bsp/ls1cdev/drivers/msd.h
@@ -0,0 +1,132 @@
+/*
+ * File : msd.h
+ * SPI mode SD Card Driver
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rt-thread.org/license/LICENSE
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2009-04-17 Bernard first version.
+ */
+
+#ifndef MSD_H_INCLUDED
+#define MSD_H_INCLUDED
+
+#include <stdint.h>
+#include <drivers/spi.h>
+
+/* SD command (SPI mode) */
+#define GO_IDLE_STATE 0 /* CMD0 R1 */
+#define SEND_OP_COND 1 /* CMD1 R1 */
+#define SWITCH_FUNC 6 /* CMD6 R1 */
+#define SEND_IF_COND 8 /* CMD8 R7 */
+#define SEND_CSD 9 /* CMD9 R1 */
+#define SEND_CID 10 /* CMD10 R1 */
+#define STOP_TRANSMISSION 12 /* CMD12 R1B */
+#define SEND_STATUS 13 /* CMD13 R2 */
+#define SET_BLOCKLEN 16 /* CMD16 R1 */
+#define READ_SINGLE_BLOCK 17 /* CMD17 R1 */
+#define READ_MULTIPLE_BLOCK 18 /* CMD18 R1 */
+#define WRITE_BLOCK 24 /* CMD24 R1 */
+#define WRITE_MULTIPLE_BLOCK 25 /* CMD25 R1 */
+#define PROGRAM_CSD 27 /* CMD27 R1 */
+#define SET_WRITE_PROT 28 /* CMD28 R1B */
+#define CLR_WRITE_PROT 29 /* CMD29 R1B */
+#define SEND_WRITE_PROT 30 /* CMD30 R1 */
+#define ERASE_WR_BLK_START_ADDR 32 /* CMD32 R1 */
+#define ERASE_WR_BLK_END_ADDR 33 /* CMD33 R1 */
+#define ERASE 38 /* CMD38 R1B */
+#define LOCK_UNLOCK 42 /* CMD42 R1 */
+#define APP_CMD 55 /* CMD55 R1 */
+#define GEN_CMD 56 /* CMD56 R1 */
+#define READ_OCR 58 /* CMD58 R3 */
+#define CRC_ON_OFF 59 /* CMD59 R1 */
+
+/* Application-Specific Command */
+#define SD_STATUS 13 /* ACMD13 R2 */
+#define SEND_NUM_WR_BLOCKS 22 /* ACMD22 R1 */
+#define SET_WR_BLK_ERASE_COUNT 23 /* ACMD23 R1 */
+#define SD_SEND_OP_COND 41 /* ACMD41 R1 */
+#define SET_CLR_CARD_DETECT 42 /* ACMD42 R1 */
+#define SEND_SCR 51 /* ACMD51 R1 */
+
+/* Start Data tokens */
+/* Tokens (necessary because at nop/idle (and CS active) only 0xff is on the data/command line) */
+#define MSD_TOKEN_READ_START 0xFE /* Data token start byte, Start Single Block Read */
+#define MSD_TOKEN_WRITE_SINGLE_START 0xFE /* Data token start byte, Start Single Block Write */
+
+#define MSD_TOKEN_WRITE_MULTIPLE_START 0xFC /* Data token start byte, Start Multiple Block Write */
+#define MSD_TOKEN_WRITE_MULTIPLE_STOP 0xFD /* Data toke stop byte, Stop Multiple Block Write */
+
+/* MSD reponses and error flags */
+#define MSD_RESPONSE_NO_ERROR 0x00
+#define MSD_IN_IDLE_STATE 0x01
+#define MSD_ERASE_RESET 0x02
+#define MSD_ILLEGAL_COMMAND 0x04
+#define MSD_COM_CRC_ERROR 0x08
+#define MSD_ERASE_SEQUENCE_ERROR 0x10
+#define MSD_ADDRESS_ERROR 0x20
+#define MSD_PARAMETER_ERROR 0x40
+#define MSD_RESPONSE_FAILURE 0xFF
+
+/* Data response error */
+#define MSD_DATA_OK 0x05
+#define MSD_DATA_CRC_ERROR 0x0B
+#define MSD_DATA_WRITE_ERROR 0x0D
+#define MSD_DATA_OTHER_ERROR 0xFF
+#define MSD_DATA_RESPONSE_MASK 0x1F
+#define MSD_GET_DATA_RESPONSE(res) (res & MSD_DATA_RESPONSE_MASK)
+
+#define MSD_CMD_LEN 6 /**< command, arg and crc. */
+#define MSD_RESPONSE_MAX_LEN 5 /**< response max len */
+#define MSD_CSD_LEN 16 /**< SD crad CSD register len */
+#define SECTOR_SIZE 512 /**< sector size, default 512byte */
+
+/* card try timeout, unit: ms */
+#define CARD_TRY_TIMES 3000
+#define CARD_TRY_TIMES_ACMD41 800
+#define CARD_WAIT_TOKEN_TIMES 800
+
+#define MSD_USE_PRE_ERASED /**< id define MSD_USE_PRE_ERASED, before CMD25, send ACMD23 */
+
+/**
+ * SD/MMC card type
+ */
+typedef enum
+{
+ MSD_CARD_TYPE_UNKNOWN = 0, /**< unknown */
+ MSD_CARD_TYPE_MMC, /**< MultiMedia Card */
+ MSD_CARD_TYPE_SD_V1_X, /**< Ver 1.X Standard Capacity SD Memory Card */
+ MSD_CARD_TYPE_SD_V2_X, /**< Ver 2.00 or later Standard Capacity SD Memory Card */
+ MSD_CARD_TYPE_SD_SDHC, /**< High Capacity SD Memory Card */
+ MSD_CARD_TYPE_SD_SDXC, /**< later Extended Capacity SD Memory Card */
+}msd_card_type;
+
+typedef enum
+{
+ response_type_unknown = 0,
+ response_r1,
+ response_r1b,
+ response_r2,
+ response_r3,
+ response_r4,
+ response_r5,
+ response_r7,
+}response_type;
+
+struct msd_device
+{
+ struct rt_device parent; /**< RT-Thread device struct */
+ struct rt_device_blk_geometry geometry; /**< sector size, sector count */
+ struct rt_spi_device * spi_device; /**< SPI interface */
+ msd_card_type card_type; /**< card type: MMC SD1.x SD2.0 SDHC SDXC */
+ uint32_t max_clock; /**< MAX SPI clock */
+};
+
+extern rt_err_t msd_init(const char * sd_device_name, const char * spi_device_name);
+
+#endif // MSD_H_INCLUDED
diff --git a/bsp/ls1cdev/drivers/touch.c b/bsp/ls1cdev/drivers/touch.c
new file mode 100644
index 0000000000..5172b7a359
--- /dev/null
+++ b/bsp/ls1cdev/drivers/touch.c
@@ -0,0 +1,607 @@
+/*
+ * File : touch.c
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2017-12-30 Sundm75 first version
+ */
+
+#include <stdbool.h>
+#include <drivers/spi.h>
+#include "ls1c.h"
+#include "ls1c_gpio.h"
+#include "ls1c_spi.h"
+#include "drv_spi.h"
+#include "touch.h"
+
+#include <rtgui/calibration.h>
+#include <rtthread.h>
+#include <rtdevice.h>
+#include <rtgui/event.h>
+#include <rtgui/kbddef.h>
+#include <rtgui/rtgui_server.h>
+#include <rtgui/rtgui_system.h>
+
+//����Ļ ����Ҫ _ILI_HORIZONTAL_DIRECTION_
+//����Ļ ��Ҫ _ILI_HORIZONTAL_DIRECTION_
+
+//#define _ILI_HORIZONTAL_DIRECTION_
+
+#if defined(_ILI_HORIZONTAL_DIRECTION_)
+#define X_WIDTH 272
+#define Y_WIDTH 480
+#else
+#define X_WIDTH 480
+#define Y_WIDTH 272
+#endif
+/*
+TOUCH INT: 84
+*/
+#define IS_TOUCH_UP() gpio_get(TOUCH_INT_PIN)
+
+#define led_gpio 52 // led1ָʾ
+
+#define DUMMY 0x00
+
+/*
+7 6 - 4 3 2 1-0
+s A2-A0 MODE SER/DFR PD1-PD0
+*/
+/* bit[1:0] power-down */
+#define POWER_MODE0 (0) /* Power-Down Between Conversions. When */
+ /* each conversion is finished, the converter */
+ /* enters a low-power mode. At the start of the */
+ /* next conversion, the device instantly powers up */
+ /* to full power. There is no need for additional */
+ /* delays to ensure full operation, and the very first */
+ /* conversion is valid. The Y? switch is on when in */
+ /* power-down.*/
+#define POWER_MODE1 (1) /* Reference is off and ADC is on. */
+#define POWER_MODE2 (2) /* Reference is on and ADC is off. */
+#define POWER_MODE3 (3) /* Device is always powered. Reference is on and */
+ /* ADC is on. */
+/* bit[2] SER/DFR */
+#define DIFFERENTIAL (0<<2)
+#define SINGLE_ENDED (1<<2)
+/* bit[3] mode */
+#define MODE_12BIT (0<<3)
+#define MODE_8BIT (1<<3)
+/* bit[6:4] differential mode */
+#define MEASURE_X (((1<<2) | (0<<1) | (1<<0))<<4)
+#define MEASURE_Y (((0<<2) | (0<<1) | (1<<0))<<4)
+#define MEASURE_Z1 (((0<<2) | (1<<1) | (1<<0))<<4)
+#define MEASURE_Z2 (((1<<2) | (0<<1) | (0<<0))<<4)
+/* bit[7] start */
+#define START (1<<7)
+
+/* X Y change. */
+#define TOUCH_MSR_X (START | MEASURE_X | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)
+#define TOUCH_MSR_Y (START | MEASURE_Y | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)
+
+
+/* ���¶���XPT2046 �Ĵ�����λ��*/
+#if defined(_ILI_HORIZONTAL_DIRECTION_)
+#define MIN_X_DEFAULT 2047
+#define MAX_X_DEFAULT 47
+#define MIN_Y_DEFAULT 102
+#define MAX_Y_DEFAULT 1939
+#else
+#define MIN_X_DEFAULT 47
+#define MAX_X_DEFAULT 2047
+#define MIN_Y_DEFAULT 1939
+#define MAX_Y_DEFAULT 102
+#endif
+
+
+
+#define SAMP_CNT 8 //the adc array size
+#define SAMP_CNT_DIV2 4 //the middle of the adc array
+#define SH 10 // Valve value
+
+
+/*�궨�� */
+#define TOUCH_SPI_X SPI1
+#define TOUCH_INT_PIN 84
+#define TOUCH_CS_PIN 49
+#define TOUCH_SCK_PIN 46
+#define TOUCH_MISO_PIN 47
+#define TOUCH_MOSI_PIN 48
+
+
+/*�����ṹ�彫��Ҫ�õ��Ķ������д��*/
+struct rtgui_touch_device
+{
+ struct rt_device parent; /* ����ע���豸*/
+
+ rt_uint16_t x, y; /* ��¼��ȡ����λ��ֵ */
+
+ rt_bool_t calibrating; /* ����У׼��־ */
+ rt_touch_calibration_func_t calibration_func;/* �������� ����ָ�� */
+
+ rt_uint16_t min_x, max_x; /* У׼�� X ������С ���ֵ */
+ rt_uint16_t min_y, max_y; /* У׼�� Y ������С ���ֵ */
+
+ struct rt_spi_device * spi_device; /* SPI �豸 ����ͨ�� */
+ struct rt_event event; /* �¼�ͬ�������ڡ����жϡ� */
+};
+static struct rtgui_touch_device *touch = RT_NULL;
+
+static rt_err_t touch_send_then_recv(struct rt_spi_device *device,
+ const void *send_buf,
+ rt_size_t send_length,
+ void *recv_buf,
+ rt_size_t recv_length)
+{
+ rt_err_t result;
+ struct rt_spi_message message;
+ rt_uint8_t dummy[128] ;
+
+ rt_memset(dummy, DUMMY, sizeof(dummy));
+
+ RT_ASSERT(device != RT_NULL);
+ RT_ASSERT(device->bus != RT_NULL);
+
+ result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
+ if (result == RT_EOK)
+ {
+ if (device->bus->owner != device)
+ {
+ /* not the same owner as current, re-configure SPI bus */
+ result = device->bus->ops->configure(device, &device->config);
+ if (result == RT_EOK)
+ {
+ /* set SPI bus owner */
+ device->bus->owner = device;
+ }
+ else
+ {
+ /* configure SPI bus failed */
+ result = -RT_EIO;
+ goto __exit;
+ }
+ }
+
+ /* send data */
+ message.send_buf = send_buf;
+ message.recv_buf = RT_NULL;
+ message.length = send_length;
+ message.cs_take = 1;
+ message.cs_release = 0;
+ message.next = RT_NULL;
+
+ result = device->bus->ops->xfer(device, &message);
+ if (result == 0)
+ {
+ result = -RT_EIO;
+ goto __exit;
+ }
+
+ /* recv data */
+ message.send_buf = dummy;
+ message.recv_buf = recv_buf;
+ message.length = recv_length;
+ message.cs_take = 0;
+ message.cs_release = 1;
+ message.next = RT_NULL;
+
+ result = device->bus->ops->xfer(device, &message);
+ if (result == 0)
+ {
+ result = -RT_EIO;
+ goto __exit;
+ }
+
+ result = RT_EOK;
+ }
+ else
+ {
+ return -RT_EIO;
+ }
+
+__exit:
+ rt_mutex_release(&(device->bus->lock));
+
+ return result;
+}
+
+
+static void rtgui_touch_calculate(void)
+{
+ if (touch != RT_NULL)
+ {
+ /* read touch */
+ {
+ rt_uint8_t i, j, k, min;
+ rt_uint16_t temp;
+ rt_uint16_t tmpxy[2][SAMP_CNT];
+ rt_uint8_t send_buffer[1];
+ rt_uint8_t recv_buffer[2];
+ for(i=0; i<SAMP_CNT; i++)
+ {
+ send_buffer[0] = TOUCH_MSR_X;
+ touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
+ rt_kprintf("touch x: %d ",(recv_buffer[0]*256|recv_buffer[1])>>4);
+#if defined(_ILI_HORIZONTAL_DIRECTION_)
+ tmpxy[1][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
+ tmpxy[1][i] >>= 4;
+#else
+ tmpxy[0][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
+ tmpxy[0][i] >>=4;
+
+#endif
+ send_buffer[0] = TOUCH_MSR_Y;
+ touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
+ rt_kprintf("touch y: %d \n",(recv_buffer[0]*256|recv_buffer[1])>>4);
+
+#if defined(_ILI_HORIZONTAL_DIRECTION_)
+ tmpxy[0][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
+ tmpxy[0][i] >>= 4;
+#else
+ tmpxy[1][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
+ tmpxy[1][i] >>= 4;
+#endif
+ }
+ /*�ٴδ򿪴����ж�*/
+ send_buffer[0] = 1 << 7;
+ touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
+ touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
+ /* calculate average */
+ {
+ rt_uint32_t total_x = 0;
+ rt_uint32_t total_y = 0;
+ for(k=0; k<2; k++)
+ {
+ // sorting the ADC value
+ for(i=0; i<SAMP_CNT-1; i++)
+ {
+ min=i;
+ for (j=i+1; j<SAMP_CNT; j++)
+ {
+ if (tmpxy[k][min] > tmpxy[k][j])
+ min=j;
+ }
+ temp = tmpxy[k][i];
+ tmpxy[k][i] = tmpxy[k][min];
+ tmpxy[k][min] = temp;
+ }
+ //check value for Valve value
+ if((tmpxy[k][SAMP_CNT_DIV2+1]-tmpxy[k][SAMP_CNT_DIV2-2]) > SH)
+ {
+ return;
+ }
+ }
+ total_x=tmpxy[0][SAMP_CNT_DIV2-2]+tmpxy[0][SAMP_CNT_DIV2-1]+tmpxy[0][SAMP_CNT_DIV2]+tmpxy[0][SAMP_CNT_DIV2+1];
+ total_y=tmpxy[1][SAMP_CNT_DIV2-2]+tmpxy[1][SAMP_CNT_DIV2-1]+tmpxy[1][SAMP_CNT_DIV2]+tmpxy[1][SAMP_CNT_DIV2+1];
+ //calculate average value
+ touch->x=total_x>>2;
+ touch->y=total_y>>2;
+ rt_kprintf("touch->x:%d touch->y:%d\r\n", touch->x, touch->y);
+ } /* calculate average */
+ } /* read touch */
+
+ /* if it's not in calibration status */
+ /*����ֵ����*/
+ if (touch->calibrating != RT_TRUE)
+ {
+ if (touch->max_x > touch->min_x)
+ {
+ touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x);
+ }
+ else
+ {
+ touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x);
+ }
+
+ if (touch->max_y > touch->min_y)
+ {
+ touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y);
+ }
+ else
+ {
+ touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y);
+ }
+ }
+ }
+}
+#include "ls1c_regs.h"
+#define TOUCH_INT_EN __REG32(LS1C_INT4_EN)
+rt_inline void touch_int_cmd(rt_bool_t NewState)
+{
+ if(NewState == RT_TRUE)
+ {
+ //TOUCH_INT_EN |= (1<<(TOUCH_INT_PIN-64));
+ reg_set_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
+ }
+ else
+ {
+ //TOUCH_INT_EN &=(~ (1<<(TOUCH_INT_PIN-64)));
+ reg_clr_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
+ }
+
+}
+
+void ls1c_touch_irqhandler(void) /* TouchScreen */
+{
+ if(gpio_get(TOUCH_INT_PIN)==0)
+ {
+ /* ���������º���� */
+ if (gpio_level_low == gpio_get(led_gpio))
+ gpio_set(led_gpio, gpio_level_high);
+ else
+ gpio_set(led_gpio, gpio_level_low);
+ touch_int_cmd(RT_FALSE);
+ rt_event_send(&touch->event, 1);
+ }
+}
+
+/*�ܽų�ʼ���������жϴ�SPI1 CS0 �豸*/
+rt_inline void touch_init(void)
+{
+ unsigned int touch_int_gpio = TOUCH_INT_PIN; // �������ж�
+ int touch_irq = LS1C_GPIO_TO_IRQ(touch_int_gpio);
+
+ // ��ʼ�������ж�
+ gpio_set_irq_type(touch_int_gpio, IRQ_TYPE_EDGE_FALLING);
+ rt_hw_interrupt_install(touch_irq, ls1c_touch_irqhandler, RT_NULL, "touch");
+ rt_hw_interrupt_umask(touch_irq);
+ gpio_init(touch_int_gpio, gpio_mode_input);
+
+ // ��ʼ��led
+ gpio_init(led_gpio, gpio_mode_output);
+ gpio_set(led_gpio, gpio_level_high);
+}
+
+
+/* RT-Thread Device Interface */
+static rt_err_t rtgui_touch_init (rt_device_t dev)
+{
+ rt_uint8_t send;
+ rt_uint8_t recv_buffer[2];
+ struct rtgui_touch_device * touch_device = (struct rtgui_touch_device *)dev;
+
+ touch_init();
+ rt_kprintf("touch_init ...\n");
+ send = START | DIFFERENTIAL | POWER_MODE0;
+ touch_send_then_recv(touch->spi_device, &send, 1, recv_buffer, 2);
+
+ return RT_EOK;
+}
+
+static rt_err_t rtgui_touch_control (rt_device_t dev, int cmd, void *args)
+{
+ switch (cmd)
+ {
+ case RT_TOUCH_CALIBRATION:
+ touch->calibrating = RT_TRUE;
+ touch->calibration_func = (rt_touch_calibration_func_t)args;
+ break;
+
+ case RT_TOUCH_NORMAL:
+ touch->calibrating = RT_FALSE;
+ break;
+
+ case RT_TOUCH_CALIBRATION_DATA:
+ {
+ struct calibration_data* data;
+ data = (struct calibration_data*) args;
+
+ //update
+ touch->min_x = data->min_x;
+ touch->max_x = data->max_x;
+ touch->min_y = data->min_y;
+ touch->max_y = data->max_y;
+ }
+ break;
+ }
+
+ return RT_EOK;
+}
+
+void _set_mouse_position(rt_uint32_t X, rt_uint32_t Y)
+{}
+static void touch_thread_entry(void *parameter)
+{
+ rt_bool_t touch_down = RT_FALSE;
+ rt_uint32_t event_value;
+ struct rtgui_event_mouse emouse;
+ static struct _touch_previous
+ {
+ rt_uint32_t x;
+ rt_uint32_t y;
+ } touch_previous;
+
+ RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
+ emouse.wid = RT_NULL;
+
+ while(1)
+ {
+ /* ���յ������ж��¼� */
+ if(rt_event_recv(&touch->event,
+ 1,
+ RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
+ 100,
+ &event_value)
+ == RT_EOK)
+ {
+ while(1)
+ {
+ if (IS_TOUCH_UP())
+ {
+ /* ������̧�� */
+ /* touch up */
+ emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP);
+
+ /* use old value */
+ emouse.x = touch->x;
+ emouse.y = touch->y;
+
+ if(touch_down != RT_TRUE)
+ {
+ touch_int_cmd(RT_TRUE);
+ break;
+ }
+
+ if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
+ {
+ /* ����У׼���� */
+ /* callback function */
+ touch->calibration_func(emouse.x, emouse.y);
+
+ }
+ else
+ {
+ /* ��ui���ʹ������� */
+ rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
+ }
+ rt_kprintf("touch up: (%d, %d)\n", emouse.x, emouse.y);
+
+ /* clean */
+ touch_previous.x = touch_previous.y = 0;
+ touch_down = RT_FALSE;
+
+ touch_int_cmd(RT_TRUE);
+ break;
+ } /* touch up */
+ else /* touch down or move */
+ {
+ if(touch_down == RT_FALSE)
+ {
+ rt_thread_delay(RT_TICK_PER_SECOND / 10);
+ }
+ else
+ {
+ rt_thread_delay(5);
+ }
+
+ if(IS_TOUCH_UP()) continue;
+
+ /* calculation */
+ rtgui_touch_calculate();
+
+ /* send mouse event */
+ emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
+ emouse.parent.sender = RT_NULL;
+
+ emouse.x = touch->x;
+ emouse.y = touch->y;
+ _set_mouse_position(emouse.x, emouse.y);
+ /* ������ */
+ /* init mouse button */
+ emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN);
+
+ /* send event to server */
+ if (touch->calibrating != RT_TRUE)
+ {
+#define previous_keep 8
+ /* filter. */
+ if((touch_previous.x > touch->x + previous_keep)
+ || (touch_previous.x < touch->x - previous_keep)
+ || (touch_previous.y > touch->y + previous_keep)
+ || (touch_previous.y < touch->y - previous_keep))
+ {
+ touch_previous.x = touch->x;
+ touch_previous.y = touch->y;
+ /* ��ui���ʹ������� */
+ rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
+ if(touch_down == RT_FALSE)
+ {
+ touch_down = RT_TRUE;
+ rt_kprintf("touch down: (%d, %d)\n", emouse.x, emouse.y);
+ }
+ else
+ {
+ rt_kprintf("touch motion: (%d, %d)\n", emouse.x, emouse.y);
+ }
+ }
+ }
+ else
+ {
+ touch_down = RT_TRUE;
+ }
+ } /* touch down or move */
+ } /* read touch */
+ } /* event recv */
+ } /* thread while(1) */
+}
+
+
+rt_err_t rtgui_touch_hw_init(const char * spi_device_name)
+{
+ rt_uint32_t arg[2];
+ struct rt_device * spi_device;
+ struct rt_thread * touch_thread;
+ rt_err_t err;
+
+ rt_kprintf("spi1 cs0 start...\n");
+ spi_device = rt_device_find("spi10");
+ if(spi_device == RT_NULL)
+ {
+ rt_kprintf("Did not find spi1, exit thread....\n");
+ return;
+ }
+ err = rt_device_open(spi_device, RT_DEVICE_OFLAG_RDWR);
+ if(err != RT_EOK)
+ {
+ rt_kprintf("Open spi1 failed %08X, exit thread....\n", err);
+ return;
+ }
+
+ /* config spi */
+ {
+ struct rt_spi_configuration cfg;
+ cfg.data_width = 8;
+ cfg.mode = RT_SPI_MODE_0;
+ cfg.max_hz = 200 * 1000; /* 200K */
+ rt_spi_configure((struct rt_spi_device *)spi_device, &cfg);
+ }
+
+ touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device));
+ if (touch == RT_NULL) return RT_ENOMEM; /* no memory yet */
+
+ /* clear device structure */
+ rt_memset(&(touch->parent), 0, sizeof(struct rt_device));
+
+ rt_event_init(&touch->event, "touch", RT_IPC_FLAG_FIFO);
+
+ touch->spi_device = (struct rt_spi_device *)spi_device;
+ touch->calibrating = false;
+
+ touch->min_x = MIN_X_DEFAULT;
+ touch->max_x = MAX_X_DEFAULT;
+ touch->min_y = MIN_Y_DEFAULT;
+ touch->max_y = MAX_Y_DEFAULT;
+
+ /* init device structure */
+ touch->parent.type = RT_Device_Class_Miscellaneous;
+ touch->parent.init = rtgui_touch_init;
+ touch->parent.control = rtgui_touch_control;
+ touch->parent.user_data = RT_NULL;
+
+ /* register touch device to RT-Thread */
+ rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR);
+
+
+ touch_thread = rt_thread_create("touch_thread",
+ touch_thread_entry, RT_NULL,
+ 4096, RTGUI_SVR_THREAD_PRIORITY-1, 1);
+ if (touch_thread != RT_NULL) rt_thread_startup(touch_thread);
+
+ rt_device_init((rt_device_t)touch);
+ return RT_EOK;
+}
diff --git a/bsp/ls1cdev/drivers/touch.h b/bsp/ls1cdev/drivers/touch.h
new file mode 100644
index 0000000000..0892d2858f
--- /dev/null
+++ b/bsp/ls1cdev/drivers/touch.h
@@ -0,0 +1,43 @@
+/*
+ * File : touch.h
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2017-12-30 Sundm75 first version
+ */
+ #ifndef __TOUCH_H__
+#define __TOUCH_H__
+
+#define RT_TOUCH_NORMAL 0
+#define RT_TOUCH_CALIBRATION_DATA 1
+#define RT_TOUCH_CALIBRATION 2
+
+//#define SAVE_CALIBRATION
+
+
+rt_uint16_t touch_read_x(void);
+rt_uint16_t touch_read_y(void);
+void touch_config(void);
+
+
+rt_err_t rtgui_touch_hw_init(const char * spi_device_name);
+
+#endif
+
+