summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bsp/ls1cdev/drivers/drv_spi.c229
-rw-r--r--bsp/ls1cdev/drivers/drv_spi.h55
-rw-r--r--bsp/ls1cdev/drivers/led.c40
-rw-r--r--bsp/ls1cdev/drivers/led.h27
-rw-r--r--bsp/ls1cdev/libraries/ls1c_spi.c141
-rw-r--r--bsp/ls1cdev/libraries/ls1c_spi.h96
-rw-r--r--bsp/ls1cdev/rtconfig.h5
7 files changed, 389 insertions, 204 deletions
diff --git a/bsp/ls1cdev/drivers/drv_spi.c b/bsp/ls1cdev/drivers/drv_spi.c
new file mode 100644
index 0000000000..b26de557db
--- /dev/null
+++ b/bsp/ls1cdev/drivers/drv_spi.c
@@ -0,0 +1,229 @@
+/*
+ * File : drv_spi.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-11-02 ���� first version
+ */
+
+#include <rtthread.h>
+#include <drivers/spi.h>
+#include "drv_spi.h"
+
+
+//#define DEBUG
+
+#ifdef DEBUG
+#define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
+#else
+#define DEBUG_PRINTF(...)
+#endif
+
+
+static rt_err_t configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
+static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message);
+
+
+static struct rt_spi_ops ls1c_spi_ops =
+{
+ .configure = configure,
+ .xfer = xfer
+};
+
+
+static rt_err_t configure(struct rt_spi_device *device,
+ struct rt_spi_configuration *configuration)
+{
+ struct rt_spi_bus *spi_bus = NULL;
+ struct ls1c_spi *ls1c_spi = NULL;
+ unsigned char SPIx = 0;
+ void *spi_base = NULL;
+ unsigned char cpol = 0;
+ unsigned char cpha = 0;
+ unsigned char val = 0;
+
+ RT_ASSERT(NULL != device);
+ RT_ASSERT(NULL != configuration);
+
+ spi_bus = device->bus;
+ ls1c_spi = (struct ls1c_spi *)spi_bus->parent.user_data;
+ SPIx = ls1c_spi->SPIx;
+ spi_base = ls1c_spi_get_base(SPIx);
+
+ {
+ // ʹ��SPI��������masterģʽ���ر��ж�
+ reg_write_8(0x53, spi_base + LS1C_SPI_SPCR_OFFSET);
+
+ // ���״̬�Ĵ���
+ reg_write_8(0xc0, spi_base + LS1C_SPI_SPSR_OFFSET);
+
+ // 1�ֽڲ����жϣ�����(��)�뷢��(д)ʱ��ͬʱ
+ reg_write_8(0x03, spi_base + LS1C_SPI_SPER_OFFSET);
+
+ // �ر�SPI flash
+ val = reg_read_8(spi_base + LS1C_SPI_SFC_PARAM_OFFSET);
+ val &= 0xfe;
+ reg_write_8(val, spi_base + LS1C_SPI_SFC_PARAM_OFFSET);
+
+ // spi flashʱ����ƼĴ���
+ reg_write_8(0x05, spi_base + LS1C_SPI_SFC_TIMING_OFFSET);
+ }
+
+ // baudrate
+ ls1c_spi_set_clock(spi_base, configuration->max_hz);
+
+ // ����ͨ��ģʽ(ʱ�Ӽ��Ժ���λ)
+ if (configuration->mode & RT_SPI_CPOL) // cpol
+ {
+ cpol = SPI_CPOL_1;
+ }
+ else
+ {
+ cpol = SPI_CPOL_0;
+ }
+ if (configuration->mode & RT_SPI_CPHA) // cpha
+ {
+ cpha = SPI_CPHA_1;
+ }
+ else
+ {
+ cpha = SPI_CPHA_0;
+ }
+ ls1c_spi_set_mode(spi_base, cpol, cpha);
+
+ DEBUG_PRINTF("ls1c spi%d configuration\n", SPIx);
+
+ return RT_EOK;
+}
+
+
+static rt_uint32_t xfer(struct rt_spi_device *device,
+ struct rt_spi_message *message)
+{
+ struct rt_spi_bus *spi_bus = NULL;
+ struct ls1c_spi *ls1c_spi = NULL;
+ void *spi_base = NULL;
+ unsigned char SPIx = 0;
+ struct ls1c_spi_cs *ls1c_spi_cs = NULL;
+ unsigned char cs = 0;
+ rt_uint32_t size = 0;
+ const rt_uint8_t *send_ptr = NULL;
+ rt_uint8_t *recv_ptr = NULL;
+ rt_uint8_t data = 0;
+
+ RT_ASSERT(NULL != device);
+ RT_ASSERT(NULL != message);
+
+ spi_bus = device->bus;
+ ls1c_spi = spi_bus->parent.user_data;
+ SPIx = ls1c_spi->SPIx;
+ spi_base = ls1c_spi_get_base(SPIx);
+ ls1c_spi_cs = device->parent.user_data;
+ cs = ls1c_spi_cs->cs;
+ size = message->length;
+
+ DEBUG_PRINTF("[%s] SPIx=%d, cs=%d\n", __FUNCTION__, SPIx, cs);
+
+ // take cs
+ if (message->cs_take)
+ {
+ ls1c_spi_set_cs(spi_base, cs, 0);
+ }
+
+ // �շ�����
+ send_ptr = message->send_buf;
+ recv_ptr = message->recv_buf;
+ while (size--)
+ {
+ data = 0xFF;
+ if (NULL != send_ptr)
+ {
+ data = *send_ptr++;
+ }
+
+ if (NULL != recv_ptr)
+ {
+ *recv_ptr++ = ls1c_spi_txrx_byte(spi_base, data);
+ }
+ else
+ {
+ ls1c_spi_txrx_byte(spi_base, data);
+ }
+ }
+
+ // release cs
+ if (message->cs_release)
+ {
+ ls1c_spi_set_cs(spi_base, cs, 1);
+ }
+
+ return message->length;
+}
+
+
+#ifdef RT_USING_SPI0
+struct ls1c_spi ls1c_spi0 =
+{
+ .SPIx = LS1C_SPI_0,
+};
+
+static struct rt_spi_bus spi0_bus;
+#endif
+
+
+#ifdef RT_USING_SPI1
+struct ls1c_spi ls1c_spi1 =
+{
+ .SPIx = LS1C_SPI_1,
+};
+
+static struct rt_spi_bus spi1_bus;
+#endif
+
+
+/*
+ * ��ʼ����ע����о1c��spi����
+ * @SPI SPI���ߣ�����LS1C_SPI_0�� LS1C_SPI_1
+ * @spi_bus_name ��������
+ * @ret
+ */
+rt_err_t ls1c_spi_bus_register(rt_uint8_t SPI, const char *spi_bus_name)
+{
+ struct rt_spi_bus *spi_bus = NULL;
+
+#ifdef RT_USING_SPI0
+ if (LS1C_SPI_0 == SPI)
+ {
+ spi_bus = &spi0_bus;
+ spi_bus->parent.user_data = &ls1c_spi0;
+ }
+#endif
+
+#ifdef RT_USING_SPI1
+ if (LS1C_SPI_1 == SPI)
+ {
+ spi_bus = &spi1_bus;
+ spi_bus->parent.user_data = &ls1c_spi1;
+ }
+#endif
+
+ return rt_spi_bus_register(spi_bus, spi_bus_name, &ls1c_spi_ops);
+}
+
+
diff --git a/bsp/ls1cdev/drivers/drv_spi.h b/bsp/ls1cdev/drivers/drv_spi.h
new file mode 100644
index 0000000000..94fbb58097
--- /dev/null
+++ b/bsp/ls1cdev/drivers/drv_spi.h
@@ -0,0 +1,55 @@
+/*
+ * File : drv_spi.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-11-02 ���� first version
+ */
+
+#ifndef LS1C_DRV_SPI_H
+#define LS1C_DRV_SPI_H
+
+
+#include "../libraries/ls1c_spi.h"
+
+
+struct ls1c_spi
+{
+ unsigned char SPIx; // LS1C_SPI_0 or LS1C_SPI_1
+};
+
+
+struct ls1c_spi_cs
+{
+ unsigned char cs; // LS1C_SPI_CS_0, LS1C_SPI_CS_1, LS1C_SPI_CS_2 or LS1C_SPI_CS_3
+};
+
+
+
+/*
+ * ��ʼ����ע����о1c��spi����
+ * @SPI SPI���ߣ�����LS1C_SPI_0�� LS1C_SPI_1
+ * @spi_bus_name ��������
+ * @ret
+ */
+rt_err_t ls1c_spi_bus_register(rt_uint8_t SPI, const char *spi_bus_name);
+
+
+#endif
+
diff --git a/bsp/ls1cdev/drivers/led.c b/bsp/ls1cdev/drivers/led.c
deleted file mode 100644
index 6f73ac9892..0000000000
--- a/bsp/ls1cdev/drivers/led.c
+++ /dev/null
@@ -1,40 +0,0 @@
-// led�ӿ�
-// ʹ�õ͵�ƽ����led���ߵ�ƽϨ��led
-
-
-#include "ls1c_gpio.h"
-
-
-// ��ʼ��led
-// @led_gpio led����gpio����
-void led_init(unsigned int led_gpio)
-{
- gpio_init(led_gpio, gpio_mode_output);
- gpio_set(led_gpio, gpio_level_high); // ָʾ��Ĭ��Ϩ��
-
- return ;
-}
-
-
-// ����led
-// @led_gpio led����gpio����
-void led_on(unsigned int led_gpio)
-{
- gpio_set(led_gpio, gpio_level_low);
-
- return ;
-}
-
-
-// Ϩ��led
-// @led_gpio
-void led_off(unsigned int led_gpio)
-{
- gpio_set(led_gpio, gpio_level_high);
-
- return ;
-}
-
-
-
-
diff --git a/bsp/ls1cdev/drivers/led.h b/bsp/ls1cdev/drivers/led.h
deleted file mode 100644
index 76ea905371..0000000000
--- a/bsp/ls1cdev/drivers/led.h
+++ /dev/null
@@ -1,27 +0,0 @@
-// led�ӿ�
-// ʹ�õ͵�ƽ����led���ߵ�ƽϨ��led
-
-#ifndef __OPENLOONGSON_LED_H
-#define __OPENLOONGSON_LED_H
-
-
-// ��ʼ��led
-// @led_gpio led����gpio����
-void led_init(unsigned int led_gpio);
-
-
-// ����led
-// @led_gpio led����gpio����
-void led_on(unsigned int led_gpio);
-
-
-// Ϩ��led
-// @led_gpio
-void led_off(unsigned int led_gpio);
-
-
-
-
-
-#endif
-
diff --git a/bsp/ls1cdev/libraries/ls1c_spi.c b/bsp/ls1cdev/libraries/ls1c_spi.c
index 8547626c30..7da50e1c37 100644
--- a/bsp/ls1cdev/libraries/ls1c_spi.c
+++ b/bsp/ls1cdev/libraries/ls1c_spi.c
@@ -32,53 +32,13 @@
#include "ls1c_spi.h"
-// �Ĵ���ƫ��
-#define LS1C_SPI_SPCR_OFFSET (0) // ���ƼĴ���
-#define LS1C_SPI_SPSR_OFFSET (1) // ״̬�Ĵ���
-#define LS1C_SPI_TxFIFO_OFFSET (2) // ���͵����ݼĴ�������������ݼĴ�����ƫ����ͬ
-#define LS1C_SPI_RxFIFO_OFFSET (2) // ���յ����ݼĴ������뷢�����ݼĴ�����ƫ����ͬ
-#define LS1C_SPI_SPER_OFFSET (3) // �ⲿ�Ĵ���
-#define LS1C_SPI_SFC_PARAM_OFFSET (4) // �������ƼĴ���
-#define LS1C_SPI_SFC_SOFTCS_OFFSET (5) // Ƭѡ���ƼĴ���
-#define LS1C_SPI_SFC_TIMING_OFFSET (6) // ʱ����ƼĴ���
-
-// �Ĵ���SPCR�е�λ��
-#define LS1C_SPI_SPCR_SPIE_BIT (7)
-#define LS1C_SPI_SPCR_SPIE_MASK (0x01 << LS1C_SPI_SPCR_SPIE_BIT)
-#define LS1C_SPI_SPCR_SPE_BIT (6)
-#define LS1C_SPI_SPCR_SPE_MASK (0x01 << LS1C_SPI_SPCR_SPE_BIT)
-#define LS1C_SPI_SPCR_CPOL_BIT (3)
-#define LS1C_SPI_SPCR_CPOL_MASK (0x01 << LS1C_SPI_SPCR_CPOL_BIT)
-#define LS1C_SPI_SPCR_CPHA_BIT (2)
-#define LS1C_SPI_SPCR_CPHA_MASK (0x01 << LS1C_SPI_SPCR_CPHA_BIT)
-#define LS1C_SPI_SPCR_SPR_BIT (0)
-#define LS1C_SPI_SPCR_SPR_MASK (0x03 << LS1C_SPI_SPCR_SPR_BIT)
-
-// �Ĵ���SPSR�е�λ��
-#define LS1C_SPI_SPSR_SPIF_BIT (7)
-#define LS1C_SPI_SPSR_SPIF_MASK (0x01 << LS1C_SPI_SPSR_SPIF_BIT)
-#define LS1C_SPI_SPSR_WCOL_BIT (6)
-#define LS1C_SPI_SPSR_WCOL_MASK (0x01 << LS1C_SPI_SPSR_WCOL_BIT)
-
-// �Ĵ���SPER�е�λ��
-#define LS1C_SPI_SPER_SPRE_BIT (0)
-#define LS1C_SPI_SPER_SPRE_MASK (0x3 << LS1C_SPI_SPER_SPRE_BIT)
-
-// �Ĵ���SFC_SOFTCS��λ��
-#define LS1C_SPI_SFC_SOFTCS_CSN_BIT (4)
-#define LS1C_SPI_SFC_SOFTCS_CSN_MASK (0x0f << LS1C_SPI_SFC_SOFTCS_CSN_BIT)
-#define LS1C_SPI_SFC_SOFTCS_CSEN_BIT (0)
-#define LS1C_SPI_SFC_SOFTCS_CSEN_MASK (0x0f << LS1C_SPI_SFC_SOFTCS_CSEN_BIT)
-
-// ���ͳ�ʱ������ֵ
-#define LS1C_SPI_TX_TIMEOUT (20000)
/*
* ��ȡָ��SPIģ��Ļ���ַ
* @SPIx SPIģ��ı��
*/
-inline void *spi_get_base(ls1c_spi_t SPIx)
+inline void *ls1c_spi_get_base(unsigned char SPIx)
{
void *base = NULL;
@@ -103,15 +63,12 @@ inline void *spi_get_base(ls1c_spi_t SPIx)
/*
* ��ӡָ��SPIģ������мĴ�����ֵ
- * @spi_info_p SPIģ����Ϣ
+ * @spi_base ����ַ
*/
-void spi_print_all_regs_info(ls1c_spi_info_t *spi_info_p)
+void ls1c_spi_print_all_regs_info(void *spi_base)
{
- void *spi_base = spi_get_base(spi_info_p->SPIx);
-
- rt_kprintf("[%s] SPI%d's info:\r\n\
- SPCR=0x%x, SPSR=0x%x, SPER=0x%x, SFC_PARAM=0x%x, SFC_SOFTCS=0x%x, SFC_TIMING=0x%x\r\n",
- __FUNCTION__, spi_info_p->SPIx,
+ rt_kprintf("[%s] SPCR=0x%x, SPSR=0x%x, SPER=0x%x, SFC_PARAM=0x%x, SFC_SOFTCS=0x%x, SFC_TIMING=0x%x\r\n",
+ __FUNCTION__,
reg_read_8(spi_base + LS1C_SPI_SPCR_OFFSET),
reg_read_8(spi_base + LS1C_SPI_SPSR_OFFSET),
reg_read_8(spi_base + LS1C_SPI_SPER_OFFSET),
@@ -128,7 +85,7 @@ void spi_print_all_regs_info(ls1c_spi_info_t *spi_info_p)
* @max_speed_hz SPI���ͨ���ٶ�
* @ret ��Ƶϵ��
*/
-unsigned int spi_get_div(unsigned int max_speed_hz)
+unsigned int ls1c_spi_get_div(unsigned int max_speed_hz)
{
unsigned long clk = 0;
unsigned int div = 0;
@@ -189,16 +146,16 @@ unsigned int spi_get_div(unsigned int max_speed_hz)
/*
* ����ʱ��
- * @spi_info_p SPIģ����Ϣ
+ * @spi_base ����ַ
+ * @max_hz ���Ƶ�ʣ���λhz
*/
-void spi_set_clock(ls1c_spi_info_t *spi_info_p)
+void ls1c_spi_set_clock(void *spi_base, unsigned long max_hz)
{
- void *spi_base = spi_get_base(spi_info_p->SPIx);
unsigned int div = 0;
unsigned char val = 0;
// ��ȡ��Ƶϵ��
- div = spi_get_div(spi_info_p->max_speed_hz);
+ div = ls1c_spi_get_div(max_hz);
// ����spr
val = reg_read_8(spi_base + LS1C_SPI_SPCR_OFFSET);
@@ -218,22 +175,23 @@ void spi_set_clock(ls1c_spi_info_t *spi_info_p)
/*
* ����ͨ��ģʽ(ʱ�Ӽ��Ժ���λ)
- * @spi_info_p SPIģ����Ϣ
+ * @spi_base ����ַ
+ * @cpol ʱ�Ӽ���
+ * @cpha ʱ����λ
*/
-void spi_set_mode(ls1c_spi_info_t *spi_info_p)
+void ls1c_spi_set_mode(void *spi_base, unsigned char cpol, unsigned char cpha)
{
- void *spi_base = spi_get_base(spi_info_p->SPIx);
unsigned char val = 0;
val = reg_read_8(spi_base + LS1C_SPI_SPCR_OFFSET);
// ����ʱ�Ӽ���--cpol
- val &= (~LS1C_SPI_SPCR_CPOL_MASK); // cpol��0
- val |= (spi_info_p->cpol << LS1C_SPI_SPCR_CPOL_BIT); // д���µ�cpol
+ val &= (~LS1C_SPI_SPCR_CPOL_MASK); // cpol��0
+ val |= (cpol << LS1C_SPI_SPCR_CPOL_BIT); // д���µ�cpol
// ����ʱ����λ--cpha
- val &= (~LS1C_SPI_SPCR_CPHA_MASK); // cpha��0
- val |= (spi_info_p->cpha << LS1C_SPI_SPCR_CPHA_BIT); // д���µ�cpha
+ val &= (~LS1C_SPI_SPCR_CPHA_MASK); // cpha��0
+ val |= (cpha << LS1C_SPI_SPCR_CPHA_BIT); // д���µ�cpha
reg_write_8(val, spi_base + LS1C_SPI_SPCR_OFFSET);
@@ -243,13 +201,12 @@ void spi_set_mode(ls1c_spi_info_t *spi_info_p)
/*
* ����ָ��ƬѡΪָ��״̬
- * @spi_info_p SPIģ����Ϣ
+ * @spi_base ����ַ
+ * @cs Ƭѡ
* @new_status Ƭѡ���ŵ���״̬��ȡֵΪ0��1�����ߵ�ƽ��͵�ƽ
*/
-void spi_set_cs(ls1c_spi_info_t *spi_info_p, int new_status)
+void ls1c_spi_set_cs(void *spi_base, unsigned char cs, int new_status)
{
- void *spi_base = spi_get_base(spi_info_p->SPIx);
- unsigned char cs = spi_info_p->cs;
unsigned char val = 0;
val = 0xf0 | (0x01 << cs); // ȫ��csn=1��ָ����csen=1
@@ -268,50 +225,11 @@ void spi_set_cs(ls1c_spi_info_t *spi_info_p, int new_status)
/*
- * ��ʼ��ָ��SPIģ��
- * @spi_info_p SPIģ����Ϣ
- */
-void spi_init(ls1c_spi_info_t *spi_info_p)
-{
- void *spi_base = spi_get_base(spi_info_p->SPIx);
- unsigned char val = 0;
-
- // ʹ��SPI��������masterģʽ���ر��ж�
- reg_write_8(0x53, spi_base + LS1C_SPI_SPCR_OFFSET);
-
- // ���״̬�Ĵ���
- reg_write_8(0xc0, spi_base + LS1C_SPI_SPSR_OFFSET);
-
- // 1�ֽڲ����жϣ�����(��)�뷢��(д)ʱ��ͬʱ
- reg_write_8(0x03, spi_base + LS1C_SPI_SPER_OFFSET);
-
- // �ر�SPI flash
- val = reg_read_8(spi_base + LS1C_SPI_SFC_PARAM_OFFSET);
- val &= 0xfe;
- reg_write_8(val, spi_base + LS1C_SPI_SFC_PARAM_OFFSET);
-
- // spi flashʱ����ƼĴ���
- reg_write_8(0x05, spi_base + LS1C_SPI_SFC_TIMING_OFFSET);
-
- // ����ʱ��
- spi_set_clock(spi_info_p);
-
- // ����ͨ��ģʽ(ʱ�Ӽ��Ժ���λ)
- spi_set_mode(spi_info_p);
-
- // ��ӡ�Ĵ�����Ϣ(���ڵ���)
-// spi_print_all_regs_info(spi_info_p);
-
- return ;
-}
-
-
-/*
* �ȴ��շ����
+ * @spi_base ����ַ
*/
-inline void spi_wait_txrx_done(ls1c_spi_info_t *spi_info_p)
+inline void ls1c_spi_wait_txrx_done(void *spi_base)
{
- void *spi_base = spi_get_base(spi_info_p->SPIx);
int timeout = LS1C_SPI_TX_TIMEOUT;
while (timeout--)
@@ -326,10 +244,10 @@ inline void spi_wait_txrx_done(ls1c_spi_info_t *spi_info_p)
/*
* ���жϺͱ�־λ
+ * @spi_base ����ַ
*/
-inline void spi_clear(ls1c_spi_info_t *spi_info_p)
+inline void ls1c_spi_clear(void *spi_base)
{
- void *spi_base = spi_get_base(spi_info_p->SPIx);
unsigned char val = 0;
// ���ж�
@@ -356,20 +274,19 @@ inline void spi_clear(ls1c_spi_info_t *spi_info_p)
* ע�⣬�ڶ������ϵͳ�У��˺�����Ҫ���⡣
* ����֤�ں�ij�����豸�շ�ij���ֽڵĹ����У����ܱ��л�����������ͬʱ���������ͬһ��SPI�����ϵĴ��豸ͨ��
* ��Ϊ��о1c��ÿ·SPI�Ͽ��ܽ��в�ͬ�Ĵ��豸��ͨ��Ƶ�ʡ�ģʽ�ȿ��ܲ�ͬ
- * @spi_info_p SPI�ӿ�
+ * @spi_base ����ַ
* @tx_ch �����͵�����
* @ret �յ�������
*/
-unsigned char spi_txrx_byte(ls1c_spi_info_t *spi_info_p, unsigned char tx_ch)
+unsigned char ls1c_spi_txrx_byte(void *spi_base, unsigned char tx_ch)
{
- void *spi_base = spi_get_base(spi_info_p->SPIx);
unsigned char rx_ch = 0;
// �շ�����
reg_write_8(tx_ch, spi_base + LS1C_SPI_TxFIFO_OFFSET); // ��ʼ����
- spi_wait_txrx_done(spi_info_p); // �ȴ��շ����
+ ls1c_spi_wait_txrx_done(spi_base); // �ȴ��շ����
rx_ch = reg_read_8(spi_base + LS1C_SPI_RxFIFO_OFFSET); // ��ȡ�յ�������
- spi_clear(spi_info_p); // ���жϺͱ�־λ
+ ls1c_spi_clear(spi_base); // ���жϺͱ�־λ
return rx_ch;
}
diff --git a/bsp/ls1cdev/libraries/ls1c_spi.h b/bsp/ls1cdev/libraries/ls1c_spi.h
index 6b041b19c1..8046127de7 100644
--- a/bsp/ls1cdev/libraries/ls1c_spi.h
+++ b/bsp/ls1cdev/libraries/ls1c_spi.h
@@ -29,15 +29,10 @@
// SPI���
-typedef enum
-{
- LS1C_SPI_0 = 0,
- LS1C_SPI_1,
-}ls1c_spi_t;
-
+#define LS1C_SPI_0 (0)
+#define LS1C_SPI_1 (1)
// Ƭѡ
-#define LS1C_SPI_INVALID_CS (-1)
#define LS1C_SPI_CS_0 (0)
#define LS1C_SPI_CS_1 (1)
#define LS1C_SPI_CS_2 (2)
@@ -50,31 +45,80 @@ typedef enum
#define SPI_CPHA_0 (0)
-// Ӳ��SPI��Ϣ
-typedef struct
-{
- ls1c_spi_t SPIx; // SPI���
- unsigned long max_speed_hz; // ���ͨ���ٶȣ���λhz
- unsigned char cs; // Ƭѡ
- unsigned char cpol; // ʱ�Ӽ���
- unsigned char cpha; // ʱ����λ
-}ls1c_spi_info_t;
+// �Ĵ���ƫ��
+#define LS1C_SPI_SPCR_OFFSET (0) // ���ƼĴ���
+#define LS1C_SPI_SPSR_OFFSET (1) // ״̬�Ĵ���
+#define LS1C_SPI_TxFIFO_OFFSET (2) // ���͵����ݼĴ�������������ݼĴ�����ƫ����ͬ
+#define LS1C_SPI_RxFIFO_OFFSET (2) // ���յ����ݼĴ������뷢�����ݼĴ�����ƫ����ͬ
+#define LS1C_SPI_SPER_OFFSET (3) // �ⲿ�Ĵ���
+#define LS1C_SPI_SFC_PARAM_OFFSET (4) // �������ƼĴ���
+#define LS1C_SPI_SFC_SOFTCS_OFFSET (5) // Ƭѡ���ƼĴ���
+#define LS1C_SPI_SFC_TIMING_OFFSET (6) // ʱ����ƼĴ���
+
+// �Ĵ���SPCR�е�λ��
+#define LS1C_SPI_SPCR_SPIE_BIT (7)
+#define LS1C_SPI_SPCR_SPIE_MASK (0x01 << LS1C_SPI_SPCR_SPIE_BIT)
+#define LS1C_SPI_SPCR_SPE_BIT (6)
+#define LS1C_SPI_SPCR_SPE_MASK (0x01 << LS1C_SPI_SPCR_SPE_BIT)
+#define LS1C_SPI_SPCR_CPOL_BIT (3)
+#define LS1C_SPI_SPCR_CPOL_MASK (0x01 << LS1C_SPI_SPCR_CPOL_BIT)
+#define LS1C_SPI_SPCR_CPHA_BIT (2)
+#define LS1C_SPI_SPCR_CPHA_MASK (0x01 << LS1C_SPI_SPCR_CPHA_BIT)
+#define LS1C_SPI_SPCR_SPR_BIT (0)
+#define LS1C_SPI_SPCR_SPR_MASK (0x03 << LS1C_SPI_SPCR_SPR_BIT)
+
+// �Ĵ���SPSR�е�λ��
+#define LS1C_SPI_SPSR_SPIF_BIT (7)
+#define LS1C_SPI_SPSR_SPIF_MASK (0x01 << LS1C_SPI_SPSR_SPIF_BIT)
+#define LS1C_SPI_SPSR_WCOL_BIT (6)
+#define LS1C_SPI_SPSR_WCOL_MASK (0x01 << LS1C_SPI_SPSR_WCOL_BIT)
+
+// �Ĵ���SPER�е�λ��
+#define LS1C_SPI_SPER_SPRE_BIT (0)
+#define LS1C_SPI_SPER_SPRE_MASK (0x3 << LS1C_SPI_SPER_SPRE_BIT)
+
+// �Ĵ���SFC_SOFTCS��λ��
+#define LS1C_SPI_SFC_SOFTCS_CSN_BIT (4)
+#define LS1C_SPI_SFC_SOFTCS_CSN_MASK (0x0f << LS1C_SPI_SFC_SOFTCS_CSN_BIT)
+#define LS1C_SPI_SFC_SOFTCS_CSEN_BIT (0)
+#define LS1C_SPI_SFC_SOFTCS_CSEN_MASK (0x0f << LS1C_SPI_SFC_SOFTCS_CSEN_BIT)
+
+// ���ͳ�ʱ������ֵ
+#define LS1C_SPI_TX_TIMEOUT (20000)
+
+
+/*
+ * ��ȡָ��SPIģ��Ļ���ַ
+ * @SPIx SPIģ��ı��
+ */
+inline void *ls1c_spi_get_base(unsigned char SPIx);
+
+
+/*
+ * ����ʱ��
+ * @spi_base ����ַ
+ * @max_hz ���Ƶ�ʣ���λhz
+ */
+void ls1c_spi_set_clock(void *spi_base, unsigned long max_hz);
/*
- * ��ʼ��ָ��SPIģ��
- * @spi_info_p SPIģ����Ϣ
+ * ����ͨ��ģʽ(ʱ�Ӽ��Ժ���λ)
+ * @spi_base ����ַ
+ * @cpol ʱ�Ӽ���
+ * @cpha ʱ����λ
*/
-void spi_init(ls1c_spi_info_t *spi_info_p);
+void ls1c_spi_set_mode(void *spi_base, unsigned char cpol, unsigned char cpha);
/*
* ����ָ��ƬѡΪָ��״̬
- * @spi_info_p SPIģ����Ϣ
+ * @spi_base ����ַ
+ * @cs Ƭѡ
* @new_status Ƭѡ���ŵ���״̬��ȡֵΪ0��1�����ߵ�ƽ��͵�ƽ
*/
-void spi_set_cs(ls1c_spi_info_t *spi_info_p, int new_status);
+void ls1c_spi_set_cs(void *spi_base, unsigned char cs, int new_status);
/*
@@ -82,18 +126,20 @@ void spi_set_cs(ls1c_spi_info_t *spi_info_p, int new_status);
* ע�⣬�ڶ������ϵͳ�У��˺�����Ҫ���⡣
* ����֤�ں�ij�����豸�շ�ij���ֽڵĹ����У����ܱ��л�����������ͬʱ���������ͬһ��SPI�����ϵĴ��豸ͨ��
* ��Ϊ��о1c��ÿ·SPI�Ͽ��ܽ��в�ͬ�Ĵ��豸��ͨ��Ƶ�ʡ�ģʽ�ȿ��ܲ�ͬ
- * @spi_info_p SPI�ӿ�
+ * @spi_base ����ַ
* @tx_ch �����͵�����
* @ret �յ�������
*/
-unsigned char spi_txrx_byte(ls1c_spi_info_t *spi_info_p, unsigned char tx_ch);
+unsigned char ls1c_spi_txrx_byte(void *spi_base, unsigned char tx_ch);
/*
* ��ӡָ��SPIģ������мĴ�����ֵ
- * @spi_info_p SPIģ����Ϣ
+ * @spi_base ����ַ
*/
-void spi_print_all_regs_info(ls1c_spi_info_t *spi_info_p);
+void ls1c_spi_print_all_regs_info(void *spi_base);
+
+
#endif
diff --git a/bsp/ls1cdev/rtconfig.h b/bsp/ls1cdev/rtconfig.h
index 168d3a7fda..ae208a485e 100644
--- a/bsp/ls1cdev/rtconfig.h
+++ b/bsp/ls1cdev/rtconfig.h
@@ -227,6 +227,11 @@
#define RTGUI_IMAGE_BMP
// </section>
+#define RT_USING_SPI
+#define RT_USING_SPI0
+#define RT_USING_SPI1
+
+
// </RDTConfigurator>
#endif