@@ -0,0 +1,14 @@
|
||||
# for module compiling
|
||||
import os
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
objs = []
|
||||
list = os.listdir(cwd)
|
||||
|
||||
for d in list:
|
||||
path = os.path.join(cwd, d)
|
||||
if os.path.isfile(os.path.join(path, 'SConscript')):
|
||||
objs = objs + SConscript(os.path.join(d, 'SConscript'))
|
||||
|
||||
Return('objs')
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* File : alarm.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
|
||||
* 2012-10-27 heyuanjie87 first version.
|
||||
*/
|
||||
|
||||
#ifndef __ALARM_H__
|
||||
#define __ALARM_H__
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define RT_ALARM_TM_NOW -1 /* set the alarm tm_day,tm_mon,tm_sec,etc.
|
||||
to now.we also call it "don't care" value */
|
||||
|
||||
/* alarm flags */
|
||||
#define RT_ALARM_ONESHOT 0x000 /* only alarm onece */
|
||||
#define RT_ALARM_DAILY 0x100 /* alarm everyday */
|
||||
#define RT_ALARM_WEEKLY 0x200 /* alarm weekly at Monday or Friday etc. */
|
||||
#define RT_ALARM_MONTHLY 0x400 /* alarm monthly at someday */
|
||||
#define RT_ALARM_YAERLY 0x800 /* alarm yearly at a certain date */
|
||||
|
||||
/* alarm control cmd */
|
||||
#define RT_ALARM_CTRL_MODIFY 1 /* modify alarm time or alarm flag */
|
||||
|
||||
typedef struct rt_alarm *rt_alarm_t;
|
||||
typedef void (*rt_alarm_callback_t)(rt_alarm_t alarm, time_t timestamp);
|
||||
|
||||
/* used for low level RTC driver */
|
||||
struct rt_rtc_wkalarm
|
||||
{
|
||||
rt_bool_t enable; /* 0 = alarm disabled, 1 = alarm enabled */
|
||||
rt_int32_t tm_sec; /* alarm at tm_sec */
|
||||
rt_int32_t tm_min; /* alarm at tm_min */
|
||||
rt_int32_t tm_hour; /* alarm at tm_hour */
|
||||
};
|
||||
|
||||
struct rt_alarm
|
||||
{
|
||||
rt_list_t list;
|
||||
rt_uint32_t flag;
|
||||
rt_alarm_callback_t callback;
|
||||
struct tm wktime;
|
||||
};
|
||||
|
||||
struct rt_alarm_setup
|
||||
{
|
||||
rt_uint32_t flag; /* alarm flag */
|
||||
struct tm wktime; /* when will the alarm wake up user */
|
||||
};
|
||||
|
||||
struct rt_alarm_container
|
||||
{
|
||||
rt_list_t head;
|
||||
struct rt_mutex mutex;
|
||||
struct rt_event event;
|
||||
struct rt_alarm *current;
|
||||
};
|
||||
|
||||
rt_alarm_t rt_alarm_create(rt_alarm_callback_t callback,
|
||||
struct rt_alarm_setup *setup);
|
||||
rt_err_t rt_alarm_control(rt_alarm_t alarm, rt_uint8_t cmd, void *arg);
|
||||
void rt_alarm_update(rt_device_t dev, rt_uint32_t event);
|
||||
rt_err_t rt_alarm_delete(rt_alarm_t alarm);
|
||||
rt_err_t rt_alarm_start(rt_alarm_t alarm);
|
||||
rt_err_t rt_alarm_stop(rt_alarm_t alarm);
|
||||
void rt_alarm_system_init(void);
|
||||
|
||||
#endif /* __ALARM_H__ */
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* File : i2c-bit-ops.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
|
||||
* 2012-04-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __I2C_BIT_OPS_H__
|
||||
#define __I2C_BIT_OPS_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct rt_i2c_bit_ops
|
||||
{
|
||||
void *data; /* private data for lowlevel routines */
|
||||
void (*set_sda)(void *data, rt_int32_t state);
|
||||
void (*set_scl)(void *data, rt_int32_t state);
|
||||
rt_int32_t (*get_sda)(void *data);
|
||||
rt_int32_t (*get_scl)(void *data);
|
||||
|
||||
void (*udelay)(rt_uint32_t us);
|
||||
|
||||
rt_uint32_t delay_us; /* scl and sda line delay */
|
||||
rt_uint32_t timeout; /* in tick */
|
||||
};
|
||||
|
||||
rt_err_t rt_i2c_bit_add_bus(struct rt_i2c_bus_device *bus,
|
||||
const char *bus_name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* File : i2c.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
|
||||
* 2012-04-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __I2C_H__
|
||||
#define __I2C_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RT_I2C_WR 0x0000
|
||||
#define RT_I2C_RD (1u << 0)
|
||||
#define RT_I2C_ADDR_10BIT (1u << 2) /* this is a ten bit chip address */
|
||||
#define RT_I2C_NO_START (1u << 4)
|
||||
#define RT_I2C_IGNORE_NACK (1u << 5)
|
||||
#define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
|
||||
|
||||
struct rt_i2c_msg
|
||||
{
|
||||
rt_uint16_t addr;
|
||||
rt_uint16_t flags;
|
||||
rt_uint16_t len;
|
||||
rt_uint8_t *buf;
|
||||
};
|
||||
|
||||
struct rt_i2c_bus_device;
|
||||
|
||||
struct rt_i2c_bus_device_ops
|
||||
{
|
||||
rt_size_t (*master_xfer)(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num);
|
||||
rt_size_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num);
|
||||
rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
|
||||
rt_uint32_t,
|
||||
rt_uint32_t);
|
||||
};
|
||||
|
||||
/*for i2c bus driver*/
|
||||
struct rt_i2c_bus_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
const struct rt_i2c_bus_device_ops *ops;
|
||||
rt_uint16_t flags;
|
||||
rt_uint16_t addr;
|
||||
struct rt_mutex lock;
|
||||
rt_uint32_t timeout;
|
||||
rt_uint32_t retries;
|
||||
void *priv;
|
||||
};
|
||||
|
||||
#ifdef RT_I2C_DEBUG
|
||||
#define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define i2c_dbg(fmt, ...)
|
||||
#endif
|
||||
|
||||
rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
|
||||
const char *bus_name);
|
||||
struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
|
||||
rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num);
|
||||
rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
const rt_uint8_t *buf,
|
||||
rt_uint32_t count);
|
||||
rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t count);
|
||||
int rt_i2c_core_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* File : i2c_dev.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
|
||||
* 2012-04-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __I2C_DEV_H__
|
||||
#define __I2C_DEV_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RT_I2C_DEV_CTRL_10BIT 0x20
|
||||
#define RT_I2C_DEV_CTRL_ADDR 0x21
|
||||
#define RT_I2C_DEV_CTRL_TIMEOUT 0x22
|
||||
#define RT_I2C_DEV_CTRL_RW 0x23
|
||||
|
||||
struct rt_i2c_priv_data
|
||||
{
|
||||
struct rt_i2c_msg *msgs;
|
||||
rt_size_t number;
|
||||
};
|
||||
|
||||
rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
|
||||
const char *name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* File : mmcsd_card.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, 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-07-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __MMCSD_CARD_H__
|
||||
#define __MMCSD_CARD_H__
|
||||
|
||||
#include <drivers/mmcsd_host.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SD_SCR_BUS_WIDTH_1 (1 << 0)
|
||||
#define SD_SCR_BUS_WIDTH_4 (1 << 2)
|
||||
|
||||
struct rt_mmcsd_cid {
|
||||
rt_uint8_t mid; /* ManufacturerID */
|
||||
rt_uint8_t prv; /* Product Revision */
|
||||
rt_uint16_t oid; /* OEM/Application ID */
|
||||
rt_uint32_t psn; /* Product Serial Number */
|
||||
rt_uint8_t pnm[5]; /* Product Name */
|
||||
rt_uint8_t reserved1;/* reserved */
|
||||
rt_uint16_t mdt; /* Manufacturing Date */
|
||||
rt_uint8_t crc; /* CID CRC */
|
||||
rt_uint8_t reserved2;/* not used, always 1 */
|
||||
};
|
||||
|
||||
struct rt_mmcsd_csd {
|
||||
rt_uint8_t csd_structure; /* CSD register version */
|
||||
rt_uint8_t taac;
|
||||
rt_uint8_t nsac;
|
||||
rt_uint8_t tran_speed; /* max data transfer rate */
|
||||
rt_uint16_t card_cmd_class; /* card command classes */
|
||||
rt_uint8_t rd_blk_len; /* max read data block length */
|
||||
rt_uint8_t rd_blk_part;
|
||||
rt_uint8_t wr_blk_misalign;
|
||||
rt_uint8_t rd_blk_misalign;
|
||||
rt_uint8_t dsr_imp; /* DSR implemented */
|
||||
rt_uint8_t c_size_mult; /* CSD 1.0 , device size multiplier */
|
||||
rt_uint32_t c_size; /* device size */
|
||||
rt_uint8_t r2w_factor;
|
||||
rt_uint8_t wr_blk_len; /* max wtire data block length */
|
||||
rt_uint8_t wr_blk_partial;
|
||||
rt_uint8_t csd_crc;
|
||||
|
||||
};
|
||||
|
||||
struct rt_sd_scr {
|
||||
rt_uint8_t sd_version;
|
||||
rt_uint8_t sd_bus_widths;
|
||||
};
|
||||
|
||||
struct rt_sdio_cccr {
|
||||
rt_uint8_t sdio_version;
|
||||
rt_uint8_t sd_version;
|
||||
rt_uint8_t direct_cmd:1, /* Card Supports Direct Commands during data transfer
|
||||
only SD mode, not used for SPI mode */
|
||||
multi_block:1, /* Card Supports Multi-Block */
|
||||
read_wait:1, /* Card Supports Read Wait
|
||||
only SD mode, not used for SPI mode */
|
||||
suspend_resume:1, /* Card supports Suspend/Resume
|
||||
only SD mode, not used for SPI mode */
|
||||
s4mi:1, /* generate interrupts during a 4-bit
|
||||
multi-block data transfer */
|
||||
e4mi:1, /* Enable the multi-block IRQ during
|
||||
4-bit transfer for the SDIO card */
|
||||
low_speed:1, /* Card is a Low-Speed card */
|
||||
low_speed_4:1; /* 4-bit support for Low-Speed cards */
|
||||
|
||||
rt_uint8_t bus_width:1, /* Support SDIO bus width, 1:4bit, 0:1bit */
|
||||
cd_disable:1, /* Connect[0]/Disconnect[1] the 10K-90K ohm pull-up
|
||||
resistor on CD/DAT[3] (pin 1) of the card */
|
||||
power_ctrl:1, /* Support Master Power Control */
|
||||
high_speed:1; /* Support High-Speed */
|
||||
|
||||
|
||||
};
|
||||
|
||||
struct rt_sdio_cis {
|
||||
rt_uint16_t manufacturer;
|
||||
rt_uint16_t product;
|
||||
rt_uint16_t func0_blk_size;
|
||||
rt_uint32_t max_tran_speed;
|
||||
};
|
||||
|
||||
/*
|
||||
* SDIO function CIS tuple (unknown to the core)
|
||||
*/
|
||||
struct rt_sdio_function_tuple {
|
||||
struct rt_sdio_function_tuple *next;
|
||||
rt_uint8_t code;
|
||||
rt_uint8_t size;
|
||||
rt_uint8_t *data;
|
||||
};
|
||||
|
||||
struct rt_sdio_function;
|
||||
typedef void (rt_sdio_irq_handler_t)(struct rt_sdio_function *);
|
||||
|
||||
/*
|
||||
* SDIO function devices
|
||||
*/
|
||||
struct rt_sdio_function {
|
||||
struct rt_mmcsd_card *card; /* the card this device belongs to */
|
||||
rt_sdio_irq_handler_t *irq_handler; /* IRQ callback */
|
||||
rt_uint8_t num; /* function number */
|
||||
|
||||
rt_uint8_t func_code; /* Standard SDIO Function interface code */
|
||||
rt_uint16_t manufacturer; /* manufacturer id */
|
||||
rt_uint16_t product; /* product id */
|
||||
|
||||
rt_uint32_t max_blk_size; /* maximum block size */
|
||||
rt_uint32_t cur_blk_size; /* current block size */
|
||||
|
||||
rt_uint32_t enable_timeout_val; /* max enable timeout in msec */
|
||||
|
||||
struct rt_sdio_function_tuple *tuples;
|
||||
};
|
||||
|
||||
#define SDIO_MAX_FUNCTIONS 7
|
||||
|
||||
|
||||
|
||||
struct rt_mmcsd_card {
|
||||
struct rt_mmcsd_host *host;
|
||||
rt_uint32_t rca; /* card addr */
|
||||
rt_uint32_t resp_cid[4]; /* card CID register */
|
||||
rt_uint32_t resp_csd[4]; /* card CSD register */
|
||||
rt_uint32_t resp_scr[2]; /* card SCR register */
|
||||
|
||||
rt_uint16_t tacc_clks; /* data access time by ns */
|
||||
rt_uint32_t tacc_ns; /* data access time by clk cycles */
|
||||
rt_uint32_t max_data_rate; /* max data transfer rate */
|
||||
rt_uint32_t card_capacity; /* card capacity, unit:KB */
|
||||
rt_uint32_t card_blksize; /* card block size */
|
||||
rt_uint16_t card_type;
|
||||
#define CARD_TYPE_MMC 0 /* MMC card */
|
||||
#define CARD_TYPE_SD 1 /* SD card */
|
||||
#define CARD_TYPE_SDIO 2 /* SDIO card */
|
||||
#define CARD_TYPE_SDIO_COMBO 3 /* SD combo (IO+mem) card */
|
||||
|
||||
rt_uint16_t flags;
|
||||
#define CARD_FLAG_HIGHSPEED (1 << 0) /* SDIO bus speed 50MHz */
|
||||
#define CARD_FLAG_SDHC (1 << 1) /* SDHC card */
|
||||
#define CARD_FLAG_SDXC (1 << 2) /* SDXC card */
|
||||
|
||||
struct rt_sd_scr scr;
|
||||
struct rt_mmcsd_csd csd;
|
||||
rt_uint32_t hs_max_data_rate; /* max data transfer rate in high speed mode */
|
||||
|
||||
rt_uint8_t sdio_function_num; /* totol number of SDIO functions */
|
||||
struct rt_sdio_cccr cccr; /* common card info */
|
||||
struct rt_sdio_cis cis; /* common tuple info */
|
||||
struct rt_sdio_function *sdio_function[SDIO_MAX_FUNCTIONS + 1]; /* SDIO functions (devices) */
|
||||
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* File : mmcsd_cmd.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, 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-07-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __CMD_H__
|
||||
#define __CMD_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* class 1 */
|
||||
#define GO_IDLE_STATE 0 /* bc */
|
||||
#define SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
|
||||
#define ALL_SEND_CID 2 /* bcr R2 */
|
||||
#define SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
|
||||
#define SET_DSR 4 /* bc [31:16] RCA */
|
||||
#define SWITCH 6 /* ac [31:0] See below R1b */
|
||||
#define SELECT_CARD 7 /* ac [31:16] RCA R1 */
|
||||
#define SEND_EXT_CSD 8 /* adtc R1 */
|
||||
#define SEND_CSD 9 /* ac [31:16] RCA R2 */
|
||||
#define SEND_CID 10 /* ac [31:16] RCA R2 */
|
||||
#define READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
|
||||
#define STOP_TRANSMISSION 12 /* ac R1b */
|
||||
#define SEND_STATUS 13 /* ac [31:16] RCA R1 */
|
||||
#define GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
|
||||
#define SPI_READ_OCR 58 /* spi spi_R3 */
|
||||
#define SPI_CRC_ON_OFF 59 /* spi [0:0] flag spi_R1 */
|
||||
|
||||
/* class 2 */
|
||||
#define SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
|
||||
#define READ_SINGLE_BLOCK 17 /* adtc [31:0] data addr R1 */
|
||||
#define READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */
|
||||
|
||||
/* class 3 */
|
||||
#define WRITE_DAT_UNTIL_STOP 20 /* adtc [31:0] data addr R1 */
|
||||
|
||||
/* class 4 */
|
||||
#define SET_BLOCK_COUNT 23 /* adtc [31:0] data addr R1 */
|
||||
#define WRITE_BLOCK 24 /* adtc [31:0] data addr R1 */
|
||||
#define WRITE_MULTIPLE_BLOCK 25 /* adtc R1 */
|
||||
#define PROGRAM_CID 26 /* adtc R1 */
|
||||
#define PROGRAM_CSD 27 /* adtc R1 */
|
||||
|
||||
/* class 6 */
|
||||
#define SET_WRITE_PROT 28 /* ac [31:0] data addr R1b */
|
||||
#define CLR_WRITE_PROT 29 /* ac [31:0] data addr R1b */
|
||||
#define SEND_WRITE_PROT 30 /* adtc [31:0] wpdata addr R1 */
|
||||
|
||||
/* class 5 */
|
||||
#define ERASE_GROUP_START 35 /* ac [31:0] data addr R1 */
|
||||
#define ERASE_GROUP_END 36 /* ac [31:0] data addr R1 */
|
||||
#define ERASE 38 /* ac R1b */
|
||||
|
||||
/* class 9 */
|
||||
#define FAST_IO 39 /* ac <Complex> R4 */
|
||||
#define GO_IRQ_STATE 40 /* bcr R5 */
|
||||
|
||||
/* class 7 */
|
||||
#define LOCK_UNLOCK 42 /* adtc R1b */
|
||||
|
||||
/* class 8 */
|
||||
#define APP_CMD 55 /* ac [31:16] RCA R1 */
|
||||
#define GEN_CMD 56 /* adtc [0] RD/WR R1 */
|
||||
|
||||
|
||||
/* SD commands type argument response */
|
||||
/* class 0 */
|
||||
/* This is basically the same command as for MMC with some quirks. */
|
||||
#define SD_SEND_RELATIVE_ADDR 3 /* bcr R6 */
|
||||
#define SD_SEND_IF_COND 8 /* bcr [11:0] See below R7 */
|
||||
|
||||
/* class 10 */
|
||||
#define SD_SWITCH 6 /* adtc [31:0] See below R1 */
|
||||
|
||||
/* Application commands */
|
||||
#define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */
|
||||
#define SD_APP_SEND_NUM_WR_BLKS 22 /* adtc R1 */
|
||||
#define SD_APP_OP_COND 41 /* bcr [31:0] OCR R3 */
|
||||
#define SD_APP_SEND_SCR 51 /* adtc R1 */
|
||||
|
||||
#define SCR_SPEC_VER_0 0 /* Implements system specification 1.0 - 1.01 */
|
||||
#define SCR_SPEC_VER_1 1 /* Implements system specification 1.10 */
|
||||
#define SCR_SPEC_VER_2 2 /* Implements system specification 2.00 */
|
||||
|
||||
|
||||
/* SDIO commands type argument response */
|
||||
#define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
|
||||
#define SD_IO_RW_DIRECT 52 /* ac [31:0] See below R5 */
|
||||
#define SD_IO_RW_EXTENDED 53 /* adtc [31:0] See below R5 */
|
||||
|
||||
|
||||
/* CMD52 arguments */
|
||||
#define SDIO_ARG_CMD52_READ (0<<31)
|
||||
#define SDIO_ARG_CMD52_WRITE (1u<<31)
|
||||
#define SDIO_ARG_CMD52_FUNC_SHIFT 28
|
||||
#define SDIO_ARG_CMD52_FUNC_MASK 0x7
|
||||
#define SDIO_ARG_CMD52_RAW_FLAG (1u<<27)
|
||||
#define SDIO_ARG_CMD52_REG_SHIFT 9
|
||||
#define SDIO_ARG_CMD52_REG_MASK 0x1ffff
|
||||
#define SDIO_ARG_CMD52_DATA_SHIFT 0
|
||||
#define SDIO_ARG_CMD52_DATA_MASK 0xff
|
||||
#define SDIO_R5_DATA(resp) ((resp)[0] & 0xff)
|
||||
|
||||
/* CMD53 arguments */
|
||||
#define SDIO_ARG_CMD53_READ (0<<31)
|
||||
#define SDIO_ARG_CMD53_WRITE (1u<<31)
|
||||
#define SDIO_ARG_CMD53_FUNC_SHIFT 28
|
||||
#define SDIO_ARG_CMD53_FUNC_MASK 0x7
|
||||
#define SDIO_ARG_CMD53_BLOCK_MODE (1u<<27)
|
||||
#define SDIO_ARG_CMD53_INCREMENT (1u<<26)
|
||||
#define SDIO_ARG_CMD53_REG_SHIFT 9
|
||||
#define SDIO_ARG_CMD53_REG_MASK 0x1ffff
|
||||
#define SDIO_ARG_CMD53_LENGTH_SHIFT 0
|
||||
#define SDIO_ARG_CMD53_LENGTH_MASK 0x1ff
|
||||
#define SDIO_ARG_CMD53_LENGTH_MAX 511
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+257
@@ -0,0 +1,257 @@
|
||||
/*
|
||||
* File : mmcsd_core.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, 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-07-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __CORE_H__
|
||||
#define __CORE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <drivers/mmcsd_host.h>
|
||||
#include <drivers/mmcsd_card.h>
|
||||
#include <drivers/mmcsd_cmd.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef RT_MMCSD_DBG
|
||||
#define mmcsd_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define mmcsd_dbg(fmt, ...)
|
||||
#endif
|
||||
|
||||
struct rt_mmcsd_data {
|
||||
rt_uint32_t blksize;
|
||||
rt_uint32_t blks;
|
||||
rt_uint32_t *buf;
|
||||
rt_int32_t err;
|
||||
rt_uint32_t flags;
|
||||
#define DATA_DIR_WRITE (1 << 0)
|
||||
#define DATA_DIR_READ (1 << 1)
|
||||
#define DATA_STREAM (1 << 2)
|
||||
|
||||
unsigned int bytes_xfered;
|
||||
|
||||
struct rt_mmcsd_cmd *stop; /* stop command */
|
||||
struct rt_mmcsd_req *mrq; /* associated request */
|
||||
|
||||
rt_uint32_t timeout_ns;
|
||||
rt_uint32_t timeout_clks;
|
||||
};
|
||||
|
||||
struct rt_mmcsd_cmd {
|
||||
rt_uint32_t cmd_code;
|
||||
rt_uint32_t arg;
|
||||
rt_uint32_t resp[4];
|
||||
rt_uint32_t flags;
|
||||
/*rsponse types
|
||||
*bits:0~3
|
||||
*/
|
||||
#define RESP_MASK (0xF)
|
||||
#define RESP_NONE (0)
|
||||
#define RESP_R1 (1 << 0)
|
||||
#define RESP_R1B (2 << 0)
|
||||
#define RESP_R2 (3 << 0)
|
||||
#define RESP_R3 (4 << 0)
|
||||
#define RESP_R4 (5 << 0)
|
||||
#define RESP_R6 (6 << 0)
|
||||
#define RESP_R7 (7 << 0)
|
||||
#define RESP_R5 (8 << 0) /*SDIO command response type*/
|
||||
/*command types
|
||||
*bits:4~5
|
||||
*/
|
||||
#define CMD_MASK (3 << 4) /* command type */
|
||||
#define CMD_AC (0 << 4)
|
||||
#define CMD_ADTC (1 << 4)
|
||||
#define CMD_BC (2 << 4)
|
||||
#define CMD_BCR (3 << 4)
|
||||
|
||||
#define resp_type(cmd) ((cmd)->flags & RESP_MASK)
|
||||
|
||||
/*spi rsponse types
|
||||
*bits:6~8
|
||||
*/
|
||||
#define RESP_SPI_MASK (0x7 << 6)
|
||||
#define RESP_SPI_R1 (1 << 6)
|
||||
#define RESP_SPI_R1B (2 << 6)
|
||||
#define RESP_SPI_R2 (3 << 6)
|
||||
#define RESP_SPI_R3 (4 << 6)
|
||||
#define RESP_SPI_R4 (5 << 6)
|
||||
#define RESP_SPI_R5 (6 << 6)
|
||||
#define RESP_SPI_R7 (7 << 6)
|
||||
|
||||
#define spi_resp_type(cmd) ((cmd)->flags & RESP_SPI_MASK)
|
||||
/*
|
||||
* These are the command types.
|
||||
*/
|
||||
#define cmd_type(cmd) ((cmd)->flags & CMD_MASK)
|
||||
|
||||
rt_int32_t retries; /* max number of retries */
|
||||
rt_int32_t err;
|
||||
|
||||
struct rt_mmcsd_data *data;
|
||||
struct rt_mmcsd_req *mrq; /* associated request */
|
||||
};
|
||||
|
||||
struct rt_mmcsd_req {
|
||||
struct rt_mmcsd_data *data;
|
||||
struct rt_mmcsd_cmd *cmd;
|
||||
struct rt_mmcsd_cmd *stop;
|
||||
};
|
||||
|
||||
/*the following is response bit*/
|
||||
#define R1_OUT_OF_RANGE (1 << 31) /* er, c */
|
||||
#define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
|
||||
#define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
|
||||
#define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
|
||||
#define R1_ERASE_PARAM (1 << 27) /* ex, c */
|
||||
#define R1_WP_VIOLATION (1 << 26) /* erx, c */
|
||||
#define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
|
||||
#define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
|
||||
#define R1_COM_CRC_ERROR (1 << 23) /* er, b */
|
||||
#define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
|
||||
#define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
|
||||
#define R1_CC_ERROR (1 << 20) /* erx, c */
|
||||
#define R1_ERROR (1 << 19) /* erx, c */
|
||||
#define R1_UNDERRUN (1 << 18) /* ex, c */
|
||||
#define R1_OVERRUN (1 << 17) /* ex, c */
|
||||
#define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
|
||||
#define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
|
||||
#define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
|
||||
#define R1_ERASE_RESET (1 << 13) /* sr, c */
|
||||
#define R1_STATUS(x) (x & 0xFFFFE000)
|
||||
#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
|
||||
#define R1_READY_FOR_DATA (1 << 8) /* sx, a */
|
||||
#define R1_APP_CMD (1 << 5) /* sr, c */
|
||||
|
||||
|
||||
#define R1_SPI_IDLE (1 << 0)
|
||||
#define R1_SPI_ERASE_RESET (1 << 1)
|
||||
#define R1_SPI_ILLEGAL_COMMAND (1 << 2)
|
||||
#define R1_SPI_COM_CRC (1 << 3)
|
||||
#define R1_SPI_ERASE_SEQ (1 << 4)
|
||||
#define R1_SPI_ADDRESS (1 << 5)
|
||||
#define R1_SPI_PARAMETER (1 << 6)
|
||||
/* R1 bit 7 is always zero */
|
||||
#define R2_SPI_CARD_LOCKED (1 << 8)
|
||||
#define R2_SPI_WP_ERASE_SKIP (1 << 9) /* or lock/unlock fail */
|
||||
#define R2_SPI_LOCK_UNLOCK_FAIL R2_SPI_WP_ERASE_SKIP
|
||||
#define R2_SPI_ERROR (1 << 10)
|
||||
#define R2_SPI_CC_ERROR (1 << 11)
|
||||
#define R2_SPI_CARD_ECC_ERROR (1 << 12)
|
||||
#define R2_SPI_WP_VIOLATION (1 << 13)
|
||||
#define R2_SPI_ERASE_PARAM (1 << 14)
|
||||
#define R2_SPI_OUT_OF_RANGE (1 << 15) /* or CSD overwrite */
|
||||
#define R2_SPI_CSD_OVERWRITE R2_SPI_OUT_OF_RANGE
|
||||
|
||||
#define CARD_BUSY 0x80000000 /* Card Power up status bit */
|
||||
|
||||
/* R5 response bits */
|
||||
#define R5_COM_CRC_ERROR (1 << 15)
|
||||
#define R5_ILLEGAL_COMMAND (1 << 14)
|
||||
#define R5_ERROR (1 << 11)
|
||||
#define R5_FUNCTION_NUMBER (1 << 9)
|
||||
#define R5_OUT_OF_RANGE (1 << 8)
|
||||
#define R5_STATUS(x) (x & 0xCB00)
|
||||
#define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12)
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* fls - find last (most-significant) bit set
|
||||
* @x: the word to search
|
||||
*
|
||||
* This is defined the same way as ffs.
|
||||
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
|
||||
*/
|
||||
|
||||
rt_inline rt_uint32_t fls(rt_uint32_t val)
|
||||
{
|
||||
rt_uint32_t bit = 32;
|
||||
|
||||
if (!val)
|
||||
return 0;
|
||||
if (!(val & 0xffff0000u))
|
||||
{
|
||||
val <<= 16;
|
||||
bit -= 16;
|
||||
}
|
||||
if (!(val & 0xff000000u))
|
||||
{
|
||||
val <<= 8;
|
||||
bit -= 8;
|
||||
}
|
||||
if (!(val & 0xf0000000u))
|
||||
{
|
||||
val <<= 4;
|
||||
bit -= 4;
|
||||
}
|
||||
if (!(val & 0xc0000000u))
|
||||
{
|
||||
val <<= 2;
|
||||
bit -= 2;
|
||||
}
|
||||
if (!(val & 0x80000000u))
|
||||
{
|
||||
val <<= 1;
|
||||
bit -= 1;
|
||||
}
|
||||
|
||||
return bit;
|
||||
}
|
||||
|
||||
void mmcsd_host_lock(struct rt_mmcsd_host *host);
|
||||
void mmcsd_host_unlock(struct rt_mmcsd_host *host);
|
||||
void mmcsd_req_complete(struct rt_mmcsd_host *host);
|
||||
void mmcsd_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req);
|
||||
rt_int32_t mmcsd_send_cmd(struct rt_mmcsd_host *host, struct rt_mmcsd_cmd *cmd, int retries);
|
||||
rt_int32_t mmcsd_go_idle(struct rt_mmcsd_host *host);
|
||||
rt_int32_t mmcsd_spi_read_ocr(struct rt_mmcsd_host *host, rt_int32_t high_capacity, rt_uint32_t *ocr);
|
||||
rt_int32_t mmcsd_all_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid);
|
||||
rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid);
|
||||
rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd);
|
||||
rt_int32_t mmcsd_select_card(struct rt_mmcsd_card *card);
|
||||
rt_int32_t mmcsd_deselect_cards(struct rt_mmcsd_card *host);
|
||||
rt_int32_t mmcsd_spi_use_crc(struct rt_mmcsd_host *host, rt_int32_t use_crc);
|
||||
void mmcsd_set_chip_select(struct rt_mmcsd_host *host, rt_int32_t mode);
|
||||
void mmcsd_set_clock(struct rt_mmcsd_host *host, rt_uint32_t clk);
|
||||
void mmcsd_set_bus_mode(struct rt_mmcsd_host *host, rt_uint32_t mode);
|
||||
void mmcsd_set_bus_width(struct rt_mmcsd_host *host, rt_uint32_t width);
|
||||
void mmcsd_set_data_timeout(struct rt_mmcsd_data *data, const struct rt_mmcsd_card *card);
|
||||
rt_uint32_t mmcsd_select_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr);
|
||||
void mmcsd_change(struct rt_mmcsd_host *host);
|
||||
void mmcsd_detect(void *param);
|
||||
struct rt_mmcsd_host *mmcsd_alloc_host(void);
|
||||
void mmcsd_free_host(struct rt_mmcsd_host *host);
|
||||
void rt_mmcsd_core_init(void);
|
||||
|
||||
void rt_mmcsd_blk_init(void);
|
||||
rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card);
|
||||
void rt_mmcsd_blk_remove(struct rt_mmcsd_card *card);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* File : mmcsd_host.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, 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-07-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __HOST_H__
|
||||
#define __HOST_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct rt_mmcsd_io_cfg {
|
||||
rt_uint32_t clock; /* clock rate */
|
||||
rt_uint16_t vdd;
|
||||
|
||||
/* vdd stores the bit number of the selected voltage range from below. */
|
||||
|
||||
rt_uint8_t bus_mode; /* command output mode */
|
||||
|
||||
#define MMCSD_BUSMODE_OPENDRAIN 1
|
||||
#define MMCSD_BUSMODE_PUSHPULL 2
|
||||
|
||||
rt_uint8_t chip_select; /* SPI chip select */
|
||||
|
||||
#define MMCSD_CS_IGNORE 0
|
||||
#define MMCSD_CS_HIGH 1
|
||||
#define MMCSD_CS_LOW 2
|
||||
|
||||
rt_uint8_t power_mode; /* power supply mode */
|
||||
|
||||
#define MMCSD_POWER_OFF 0
|
||||
#define MMCSD_POWER_UP 1
|
||||
#define MMCSD_POWER_ON 2
|
||||
|
||||
rt_uint8_t bus_width; /* data bus width */
|
||||
|
||||
#define MMCSD_BUS_WIDTH_1 0
|
||||
#define MMCSD_BUS_WIDTH_4 2
|
||||
#define MMCSD_BUS_WIDTH_8 3
|
||||
|
||||
};
|
||||
|
||||
struct rt_mmcsd_host;
|
||||
struct rt_mmcsd_req;
|
||||
|
||||
struct rt_mmcsd_host_ops {
|
||||
void (*request)(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req);
|
||||
void (*set_iocfg)(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg);
|
||||
rt_int32_t (*get_card_status)(struct rt_mmcsd_host *host);
|
||||
void (*enable_sdio_irq)(struct rt_mmcsd_host *host, rt_int32_t en);
|
||||
};
|
||||
|
||||
struct rt_mmcsd_host {
|
||||
struct rt_mmcsd_card *card;
|
||||
const struct rt_mmcsd_host_ops *ops;
|
||||
rt_uint32_t freq_min;
|
||||
rt_uint32_t freq_max;
|
||||
struct rt_mmcsd_io_cfg io_cfg;
|
||||
rt_uint32_t valid_ocr; /* current valid OCR */
|
||||
#define VDD_165_195 (1 << 7) /* VDD voltage 1.65 - 1.95 */
|
||||
#define VDD_20_21 (1 << 8) /* VDD voltage 2.0 ~ 2.1 */
|
||||
#define VDD_21_22 (1 << 9) /* VDD voltage 2.1 ~ 2.2 */
|
||||
#define VDD_22_23 (1 << 10) /* VDD voltage 2.2 ~ 2.3 */
|
||||
#define VDD_23_24 (1 << 11) /* VDD voltage 2.3 ~ 2.4 */
|
||||
#define VDD_24_25 (1 << 12) /* VDD voltage 2.4 ~ 2.5 */
|
||||
#define VDD_25_26 (1 << 13) /* VDD voltage 2.5 ~ 2.6 */
|
||||
#define VDD_26_27 (1 << 14) /* VDD voltage 2.6 ~ 2.7 */
|
||||
#define VDD_27_28 (1 << 15) /* VDD voltage 2.7 ~ 2.8 */
|
||||
#define VDD_28_29 (1 << 16) /* VDD voltage 2.8 ~ 2.9 */
|
||||
#define VDD_29_30 (1 << 17) /* VDD voltage 2.9 ~ 3.0 */
|
||||
#define VDD_30_31 (1 << 18) /* VDD voltage 3.0 ~ 3.1 */
|
||||
#define VDD_31_32 (1 << 19) /* VDD voltage 3.1 ~ 3.2 */
|
||||
#define VDD_32_33 (1 << 20) /* VDD voltage 3.2 ~ 3.3 */
|
||||
#define VDD_33_34 (1 << 21) /* VDD voltage 3.3 ~ 3.4 */
|
||||
#define VDD_34_35 (1 << 22) /* VDD voltage 3.4 ~ 3.5 */
|
||||
#define VDD_35_36 (1 << 23) /* VDD voltage 3.5 ~ 3.6 */
|
||||
rt_uint32_t flags; /* define device capabilities */
|
||||
#define MMCSD_BUSWIDTH_4 (1 << 0)
|
||||
#define MMCSD_BUSWIDTH_8 (1 << 1)
|
||||
#define MMCSD_MUTBLKWRITE (1 << 2)
|
||||
#define MMCSD_HOST_IS_SPI (1 << 3)
|
||||
#define controller_is_spi(host) (host->flags & MMCSD_HOST_IS_SPI)
|
||||
#define MMCSD_SUP_SDIO_IRQ (1 << 4) /* support signal pending SDIO IRQs */
|
||||
#define MMCSD_SUP_HIGHSPEED (1 << 5) /* support high speed */
|
||||
|
||||
rt_uint32_t max_seg_size; /* maximum size of one dma segment */
|
||||
rt_uint32_t max_dma_segs; /* maximum number of dma segments in one request */
|
||||
rt_uint32_t max_blk_size; /* maximum block size */
|
||||
rt_uint32_t max_blk_count; /* maximum block count */
|
||||
|
||||
rt_uint32_t spi_use_crc;
|
||||
struct rt_semaphore bus_lock;
|
||||
struct rt_semaphore sem_ack;
|
||||
|
||||
rt_uint32_t sdio_irq_num;
|
||||
struct rt_semaphore *sdio_irq_sem;
|
||||
struct rt_thread *sdio_irq_thread;
|
||||
|
||||
void *private_data;
|
||||
};
|
||||
|
||||
rt_inline void mmcsd_delay_ms(rt_uint32_t ms)
|
||||
{
|
||||
if (ms < 1000 / RT_TICK_PER_SECOND)
|
||||
{
|
||||
rt_thread_delay(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_thread_delay(ms/(1000 / RT_TICK_PER_SECOND));
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* File : mtd_nand.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
|
||||
* 2011-12-05 Bernard the first version
|
||||
* 2011-04-02 prife add mark_badblock and check_block
|
||||
*/
|
||||
|
||||
/*
|
||||
* COPYRIGHT (C) 2012, Shanghai Real Thread
|
||||
*/
|
||||
|
||||
#ifndef __MTD_NAND_H__
|
||||
#define __MTD_NAND_H__
|
||||
|
||||
#include <rtdevice.h>
|
||||
|
||||
struct rt_mtd_nand_driver_ops;
|
||||
#define RT_MTD_NAND_DEVICE(device) ((struct rt_mtd_nand_device*)(device))
|
||||
|
||||
#define RT_MTD_EOK 0 /* NO error */
|
||||
#define RT_MTD_EECC 1 /* ECC error */
|
||||
#define RT_MTD_EBUSY 2 /* hardware busy */
|
||||
#define RT_MTD_EIO 3 /* generic IO issue */
|
||||
#define RT_MTD_ENOMEM 4 /* out of memory */
|
||||
#define RT_MTD_ESRC 5 /* source issue */
|
||||
|
||||
struct rt_mtd_nand_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
rt_uint16_t page_size; /* The Page size in the flash */
|
||||
rt_uint16_t oob_size; /* Out of bank size */
|
||||
rt_uint16_t oob_free; /* the free area in oob that flash driver not use */
|
||||
rt_uint16_t plane_num; /* the number of plane in the NAND Flash */
|
||||
|
||||
rt_uint32_t pages_per_block; /* The number of page a block */
|
||||
rt_uint16_t block_total;
|
||||
|
||||
rt_uint32_t block_start; /* The start of available block*/
|
||||
rt_uint32_t block_end; /* The end of available block */
|
||||
|
||||
/* operations interface */
|
||||
const struct rt_mtd_nand_driver_ops* ops;
|
||||
};
|
||||
|
||||
struct rt_mtd_nand_driver_ops
|
||||
{
|
||||
rt_err_t (*read_id) (struct rt_mtd_nand_device* device);
|
||||
|
||||
rt_err_t (*read_page)(struct rt_mtd_nand_device* device,
|
||||
rt_off_t page,
|
||||
rt_uint8_t* data, rt_uint32_t data_len,
|
||||
rt_uint8_t * spare, rt_uint32_t spare_len);
|
||||
|
||||
rt_err_t (*write_page)(struct rt_mtd_nand_device * device,
|
||||
rt_off_t page,
|
||||
const rt_uint8_t * data, rt_uint32_t data_len,
|
||||
const rt_uint8_t * spare, rt_uint32_t spare_len);
|
||||
rt_err_t (*move_page) (struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page);
|
||||
|
||||
rt_err_t (*erase_block)(struct rt_mtd_nand_device* device, rt_uint32_t block);
|
||||
rt_err_t (*check_block)(struct rt_mtd_nand_device* device, rt_uint32_t block);
|
||||
rt_err_t (*mark_badblock)(struct rt_mtd_nand_device* device, rt_uint32_t block);
|
||||
};
|
||||
|
||||
rt_err_t rt_mtd_nand_register_device(const char* name, struct rt_mtd_nand_device* device);
|
||||
|
||||
rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device* device)
|
||||
{
|
||||
return device->ops->read_id(device);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_mtd_nand_read(
|
||||
struct rt_mtd_nand_device* device,
|
||||
rt_off_t page,
|
||||
rt_uint8_t* data, rt_uint32_t data_len,
|
||||
rt_uint8_t * spare, rt_uint32_t spare_len)
|
||||
{
|
||||
return device->ops->read_page(device, page, data, data_len, spare, spare_len);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_mtd_nand_write(
|
||||
struct rt_mtd_nand_device* device,
|
||||
rt_off_t page,
|
||||
const rt_uint8_t* data, rt_uint32_t data_len,
|
||||
const rt_uint8_t * spare, rt_uint32_t spare_len)
|
||||
{
|
||||
return device->ops->write_page(device, page, data, data_len, spare, spare_len);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device* device,
|
||||
rt_off_t src_page, rt_off_t dst_page)
|
||||
{
|
||||
return device->ops->move_page(device, src_page, dst_page);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
|
||||
{
|
||||
return device->ops->erase_block(device, block);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
|
||||
{
|
||||
return device->ops->check_block(device, block);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device* device, rt_uint32_t block)
|
||||
{
|
||||
return device->ops->mark_badblock(device, block);
|
||||
}
|
||||
|
||||
#endif /* MTD_NAND_H_ */
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* File : mtd_nor.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2012, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* 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
|
||||
* 2012-5-30 Bernard the first version
|
||||
*/
|
||||
|
||||
#ifndef __MTD_NOR_H__
|
||||
#define __MTD_NOR_H__
|
||||
|
||||
#include <rtdevice.h>
|
||||
|
||||
struct rt_mtd_nor_driver_ops;
|
||||
#define RT_MTD_NOR_DEVICE(device) ((struct rt_mtd_nor_device*)(device))
|
||||
|
||||
struct rt_mtd_nor_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
rt_uint32_t block_size; /* The Block size in the flash */
|
||||
rt_uint32_t block_start; /* The start of available block*/
|
||||
rt_uint32_t block_end; /* The end of available block */
|
||||
|
||||
/* operations interface */
|
||||
const struct rt_mtd_nor_driver_ops* ops;
|
||||
};
|
||||
|
||||
struct rt_mtd_nor_driver_ops
|
||||
{
|
||||
rt_err_t (*read_id) (struct rt_mtd_nor_device* device);
|
||||
|
||||
rt_size_t (*read) (struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint8_t* data, rt_uint32_t length);
|
||||
rt_size_t (*write) (struct rt_mtd_nor_device* device, rt_off_t offset, const rt_uint8_t* data, rt_uint32_t length);
|
||||
|
||||
rt_err_t (*erase_block)(struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint32_t length);
|
||||
};
|
||||
|
||||
rt_err_t rt_mtd_nor_register_device(const char* name, struct rt_mtd_nor_device* device);
|
||||
|
||||
rt_inline rt_uint32_t rt_mtd_nor_read_id(struct rt_mtd_nor_device* device)
|
||||
{
|
||||
return device->ops->read_id(device);
|
||||
}
|
||||
|
||||
rt_inline rt_size_t rt_mtd_nor_read(
|
||||
struct rt_mtd_nor_device* device,
|
||||
rt_off_t offset, rt_uint8_t* data, rt_uint32_t length)
|
||||
{
|
||||
return device->ops->read(device, offset, data, length);
|
||||
}
|
||||
|
||||
rt_inline rt_size_t rt_mtd_nor_write(
|
||||
struct rt_mtd_nor_device* device,
|
||||
rt_off_t offset, const rt_uint8_t* data, rt_uint32_t length)
|
||||
{
|
||||
return device->ops->write(device, offset, data, length);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_mtd_nor_erase_block(struct rt_mtd_nor_device* device, rt_off_t offset, rt_size_t length)
|
||||
{
|
||||
return device->ops->erase_block(device, offset, length);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* File : rtc.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
|
||||
* 2012-10-10 aozima first version.
|
||||
*/
|
||||
|
||||
#ifndef RTC_H_INCLUDED
|
||||
#define RTC_H_INCLUDED
|
||||
|
||||
extern rt_err_t set_date(rt_uint32_t year,
|
||||
rt_uint32_t month,
|
||||
rt_uint32_t day);
|
||||
|
||||
extern rt_err_t set_time(rt_uint32_t hour,
|
||||
rt_uint32_t minute,
|
||||
rt_uint32_t second);
|
||||
|
||||
#endif // RTC_H_INCLUDED
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* File : sd.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, 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-07-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __SD_H__
|
||||
#define __SD_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <drivers/mmcsd_host.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
rt_err_t mmcsd_send_if_cond(struct rt_mmcsd_host *host, rt_uint32_t ocr);
|
||||
rt_err_t mmcsd_send_app_op_cond(struct rt_mmcsd_host *host, rt_uint32_t ocr, rt_uint32_t *rocr);
|
||||
rt_int32_t init_sd(struct rt_mmcsd_host *host, rt_uint32_t ocr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* File : sdio.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, 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
|
||||
* 2012-01-15 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __SDIO_H__
|
||||
#define __SDIO_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <drivers/mmcsd_host.h>
|
||||
#include <drivers/mmcsd_card.h>
|
||||
#include <drivers/sdio_func_ids.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Card Common Control Registers (CCCR)
|
||||
*/
|
||||
|
||||
#define SDIO_REG_CCCR_CCCR_REV 0x00
|
||||
|
||||
#define SDIO_CCCR_REV_1_00 0 /* CCCR/FBR Version 1.00 */
|
||||
#define SDIO_CCCR_REV_1_10 1 /* CCCR/FBR Version 1.10 */
|
||||
#define SDIO_CCCR_REV_1_20 2 /* CCCR/FBR Version 1.20 */
|
||||
|
||||
#define SDIO_SDIO_REV_1_00 0 /* SDIO Spec Version 1.00 */
|
||||
#define SDIO_SDIO_REV_1_10 1 /* SDIO Spec Version 1.10 */
|
||||
#define SDIO_SDIO_REV_1_20 2 /* SDIO Spec Version 1.20 */
|
||||
#define SDIO_SDIO_REV_2_00 3 /* SDIO Spec Version 2.00 */
|
||||
|
||||
#define SDIO_REG_CCCR_SD_REV 0x01
|
||||
|
||||
#define SDIO_SD_REV_1_01 0 /* SD Physical Spec Version 1.01 */
|
||||
#define SDIO_SD_REV_1_10 1 /* SD Physical Spec Version 1.10 */
|
||||
#define SDIO_SD_REV_2_00 2 /* SD Physical Spec Version 2.00 */
|
||||
|
||||
#define SDIO_REG_CCCR_IO_EN 0x02
|
||||
#define SDIO_REG_CCCR_IO_RDY 0x03
|
||||
|
||||
#define SDIO_REG_CCCR_INT_EN 0x04 /* Function/Master Interrupt Enable */
|
||||
#define SDIO_REG_CCCR_INT_PEND 0x05 /* Function Interrupt Pending */
|
||||
|
||||
#define SDIO_REG_CCCR_IO_ABORT 0x06 /* function abort/card reset */
|
||||
|
||||
#define SDIO_REG_CCCR_BUS_IF 0x07 /* bus interface controls */
|
||||
|
||||
#define SDIO_BUS_WIDTH_1BIT 0x00
|
||||
#define SDIO_BUS_WIDTH_4BIT 0x02
|
||||
#define SDIO_BUS_ECSI 0x20 /* Enable continuous SPI interrupt */
|
||||
#define SDIO_BUS_SCSI 0x40 /* Support continuous SPI interrupt */
|
||||
|
||||
#define SDIO_BUS_ASYNC_INT 0x20
|
||||
|
||||
#define SDIO_BUS_CD_DISABLE 0x80 /* disable pull-up on DAT3 (pin 1) */
|
||||
|
||||
#define SDIO_REG_CCCR_CARD_CAPS 0x08
|
||||
|
||||
#define SDIO_CCCR_CAP_SDC 0x01 /* can do CMD52 while data transfer */
|
||||
#define SDIO_CCCR_CAP_SMB 0x02 /* can do multi-block xfers (CMD53) */
|
||||
#define SDIO_CCCR_CAP_SRW 0x04 /* supports read-wait protocol */
|
||||
#define SDIO_CCCR_CAP_SBS 0x08 /* supports suspend/resume */
|
||||
#define SDIO_CCCR_CAP_S4MI 0x10 /* interrupt during 4-bit CMD53 */
|
||||
#define SDIO_CCCR_CAP_E4MI 0x20 /* enable ints during 4-bit CMD53 */
|
||||
#define SDIO_CCCR_CAP_LSC 0x40 /* low speed card */
|
||||
#define SDIO_CCCR_CAP_4BLS 0x80 /* 4 bit low speed card */
|
||||
|
||||
#define SDIO_REG_CCCR_CIS_PTR 0x09 /* common CIS pointer (3 bytes) */
|
||||
|
||||
/* Following 4 regs are valid only if SBS is set */
|
||||
#define SDIO_REG_CCCR_BUS_SUSPEND 0x0c
|
||||
#define SDIO_REG_CCCR_FUNC_SEL 0x0d
|
||||
#define SDIO_REG_CCCR_EXEC_FLAG 0x0e
|
||||
#define SDIO_REG_CCCR_READY_FLAG 0x0f
|
||||
|
||||
#define SDIO_REG_CCCR_FN0_BLKSIZE 0x10 /* 2bytes, 0x10~0x11 */
|
||||
|
||||
#define SDIO_REG_CCCR_POWER_CTRL 0x12
|
||||
|
||||
#define SDIO_POWER_SMPC 0x01 /* Supports Master Power Control */
|
||||
#define SDIO_POWER_EMPC 0x02 /* Enable Master Power Control */
|
||||
|
||||
#define SDIO_REG_CCCR_SPEED 0x13
|
||||
|
||||
#define SDIO_SPEED_SHS 0x01 /* Supports High-Speed mode */
|
||||
#define SDIO_SPEED_EHS 0x02 /* Enable High-Speed mode */
|
||||
|
||||
/*
|
||||
* Function Basic Registers (FBR)
|
||||
*/
|
||||
|
||||
#define SDIO_REG_FBR_BASE(f) ((f) * 0x100) /* base of function f's FBRs */
|
||||
|
||||
#define SDIO_REG_FBR_STD_FUNC_IF 0x00
|
||||
|
||||
#define SDIO_FBR_SUPPORTS_CSA 0x40 /* supports Code Storage Area */
|
||||
#define SDIO_FBR_ENABLE_CSA 0x80 /* enable Code Storage Area */
|
||||
|
||||
#define SDIO_REG_FBR_STD_IF_EXT 0x01
|
||||
|
||||
#define SDIO_REG_FBR_POWER 0x02
|
||||
|
||||
#define SDIO_FBR_POWER_SPS 0x01 /* Supports Power Selection */
|
||||
#define SDIO_FBR_POWER_EPS 0x02 /* Enable (low) Power Selection */
|
||||
|
||||
#define SDIO_REG_FBR_CIS 0x09 /* CIS pointer (3 bytes) */
|
||||
|
||||
|
||||
#define SDIO_REG_FBR_CSA 0x0C /* CSA pointer (3 bytes) */
|
||||
|
||||
#define SDIO_REG_FBR_CSA_DATA 0x0F
|
||||
|
||||
#define SDIO_REG_FBR_BLKSIZE 0x10 /* block size (2 bytes) */
|
||||
|
||||
/* SDIO CIS Tuple code */
|
||||
#define CISTPL_NULL 0x00
|
||||
#define CISTPL_CHECKSUM 0x10
|
||||
#define CISTPL_VERS_1 0x15
|
||||
#define CISTPL_ALTSTR 0x16
|
||||
#define CISTPL_MANFID 0x20
|
||||
#define CISTPL_FUNCID 0x21
|
||||
#define CISTPL_FUNCE 0x22
|
||||
#define CISTPL_SDIO_STD 0x91
|
||||
#define CISTPL_SDIO_EXT 0x92
|
||||
#define CISTPL_END 0xff
|
||||
|
||||
/* SDIO device id */
|
||||
#define SDIO_ANY_FUNC_ID 0xff
|
||||
#define SDIO_ANY_MAN_ID 0xffff
|
||||
#define SDIO_ANY_PROD_ID 0xffff
|
||||
|
||||
struct rt_sdio_device_id
|
||||
{
|
||||
rt_uint8_t func_code;
|
||||
rt_uint16_t manufacturer;
|
||||
rt_uint16_t product;
|
||||
};
|
||||
|
||||
struct rt_sdio_driver
|
||||
{
|
||||
char *name;
|
||||
rt_int32_t (*probe)(struct rt_mmcsd_card *card);
|
||||
rt_int32_t (*remove)(struct rt_mmcsd_card *card);
|
||||
struct rt_sdio_device_id *id;
|
||||
};
|
||||
|
||||
rt_int32_t sdio_io_send_op_cond(struct rt_mmcsd_host *host,
|
||||
rt_uint32_t ocr,
|
||||
rt_uint32_t *cmd5_resp);
|
||||
rt_int32_t sdio_io_rw_direct(struct rt_mmcsd_card *card,
|
||||
rt_int32_t rw,
|
||||
rt_uint32_t fn,
|
||||
rt_uint32_t reg_addr,
|
||||
rt_uint8_t *pdata,
|
||||
rt_uint8_t raw);
|
||||
rt_int32_t sdio_io_rw_extended(struct rt_mmcsd_card *card,
|
||||
rt_int32_t rw,
|
||||
rt_uint32_t fn,
|
||||
rt_uint32_t addr,
|
||||
rt_int32_t op_code,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t blocks,
|
||||
rt_uint32_t blksize);
|
||||
rt_uint8_t sdio_io_readb(struct rt_sdio_function *func,
|
||||
rt_uint32_t reg,
|
||||
rt_int32_t *err);
|
||||
rt_int32_t sdio_io_writeb(struct rt_sdio_function *func,
|
||||
rt_uint32_t reg,
|
||||
rt_uint8_t data);
|
||||
rt_uint16_t sdio_io_readw(struct rt_sdio_function *func,
|
||||
rt_uint32_t addr,
|
||||
rt_int32_t *err);
|
||||
rt_int32_t sdio_io_writew(struct rt_sdio_function *func,
|
||||
rt_uint16_t data,
|
||||
rt_uint32_t addr);
|
||||
rt_uint32_t sdio_io_readl(struct rt_sdio_function *func,
|
||||
rt_uint32_t addr,
|
||||
rt_int32_t *err);
|
||||
rt_int32_t sdio_io_writel(struct rt_sdio_function *func,
|
||||
rt_uint32_t data,
|
||||
rt_uint32_t addr);
|
||||
rt_int32_t sdio_io_read_multi_fifo_b(struct rt_sdio_function *func,
|
||||
rt_uint32_t addr,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t len);
|
||||
rt_int32_t sdio_io_write_multi_fifo_b(struct rt_sdio_function *func,
|
||||
rt_uint32_t addr,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t len);
|
||||
rt_int32_t sdio_io_read_multi_incr_b(struct rt_sdio_function *func,
|
||||
rt_uint32_t addr,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t len);
|
||||
rt_int32_t sdio_io_write_multi_incr_b(struct rt_sdio_function *func,
|
||||
rt_uint32_t addr,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t len);
|
||||
rt_int32_t init_sdio(struct rt_mmcsd_host *host, rt_uint32_t ocr);
|
||||
rt_int32_t sdio_attach_irq(struct rt_sdio_function *func,
|
||||
rt_sdio_irq_handler_t *handler);
|
||||
rt_int32_t sdio_detach_irq(struct rt_sdio_function *func);
|
||||
void sdio_irq_wakeup(struct rt_mmcsd_host *host);
|
||||
rt_int32_t sdio_enable_func(struct rt_sdio_function *func);
|
||||
rt_int32_t sdio_disable_func(struct rt_sdio_function *func);
|
||||
rt_int32_t sdio_set_block_size(struct rt_sdio_function *func,
|
||||
rt_uint32_t blksize);
|
||||
rt_int32_t sdio_register_driver(struct rt_sdio_driver *driver);
|
||||
rt_int32_t sdio_unregister_driver(struct rt_sdio_driver *driver);
|
||||
void rt_sdio_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* File : sdio_func_ids.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, 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
|
||||
* 2012-02-26 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __SDIO_FUNC_IDS_H__
|
||||
#define __SDIO_FUNC_IDS_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Standard SDIO Function Interfaces */
|
||||
|
||||
#define SDIO_FUNC_CODE_NONE 0x00 /* Not a SDIO standard interface */
|
||||
#define SDIO_FUNC_CODE_UART 0x01 /* SDIO Standard UART */
|
||||
#define SDIO_FUNC_CODE_BT_A 0x02 /* SDIO Type-A for Bluetooth standard interface */
|
||||
#define SDIO_FUNC_CODE_BT_B 0x03 /* SDIO Type-B for Bluetooth standard interface */
|
||||
#define SDIO_FUNC_CODE_GPS 0x04 /* SDIO GPS standard interface */
|
||||
#define SDIO_FUNC_CODE_CAMERA 0x05 /* SDIO Camera standard interface */
|
||||
#define SDIO_FUNC_CODE_PHS 0x06 /* SDIO PHS standard interface */
|
||||
#define SDIO_FUNC_CODE_WLAN 0x07 /* SDIO WLAN interface */
|
||||
#define SDIO_FUNC_CODE_ATA 0x08 /* Embedded SDIO-ATA standard interface */
|
||||
|
||||
/* manufacturer id, product io */
|
||||
|
||||
#define SDIO_MANUFACTURER_ID_MARVELL 0x02df
|
||||
#define SDIO_PRODUCT_ID_MARVELL_88W8686 0x9103
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* File : serial.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
|
||||
* 2012-05-15 lgnq first version.
|
||||
* 2012-05-28 bernard change interfaces
|
||||
* 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
|
||||
* the size of ring buffer.
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_H__
|
||||
#define __SERIAL_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define BAUD_RATE_2400 2400
|
||||
#define BAUD_RATE_4800 4800
|
||||
#define BAUD_RATE_9600 9600
|
||||
#define BAUD_RATE_38400 38400
|
||||
#define BAUD_RATE_57600 57600
|
||||
#define BAUD_RATE_115200 115200
|
||||
#define BAUD_RATE_230400 230400
|
||||
#define BAUD_RATE_460800 460800
|
||||
#define BAUD_RATE_921600 921600
|
||||
|
||||
#define DATA_BITS_5 5
|
||||
#define DATA_BITS_6 6
|
||||
#define DATA_BITS_7 7
|
||||
#define DATA_BITS_8 8
|
||||
#define DATA_BITS_9 9
|
||||
|
||||
#define STOP_BITS_1 0
|
||||
#define STOP_BITS_2 1
|
||||
#define STOP_BITS_3 2
|
||||
#define STOP_BITS_4 3
|
||||
|
||||
#define PARITY_NONE 0
|
||||
#define PARITY_ODD 1
|
||||
#define PARITY_EVEN 2
|
||||
|
||||
#define BIT_ORDER_LSB 0
|
||||
#define BIT_ORDER_MSB 1
|
||||
|
||||
#define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
|
||||
#define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
|
||||
|
||||
#ifndef RT_SERIAL_RB_BUFSZ
|
||||
#define RT_SERIAL_RB_BUFSZ 64
|
||||
#endif
|
||||
|
||||
#define RT_DEVICE_CTRL_CONFIG 0x03 /* configure device */
|
||||
#define RT_DEVICE_CTRL_SET_INT 0x10 /* enable serial irq */
|
||||
#define RT_DEVICE_CTRL_CLR_INT 0x11 /* disable serial irq */
|
||||
#define RT_DEVICE_CTRL_GET_INT 0x12
|
||||
#define RT_DEVICE_CTRL_GET_FLAG 0x13
|
||||
|
||||
#define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
|
||||
#define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
|
||||
#define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
|
||||
#define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
|
||||
#define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
|
||||
|
||||
#define RT_SERIAL_DMA_RX 0x01
|
||||
#define RT_SERIAL_DMA_TX 0x02
|
||||
|
||||
#define RT_SERIAL_RX_INT 0x01
|
||||
#define RT_SERIAL_TX_INT 0x02
|
||||
|
||||
#define RT_SERIAL_ERR_OVERRUN 0x01
|
||||
#define RT_SERIAL_ERR_FRAMING 0x02
|
||||
#define RT_SERIAL_ERR_PARITY 0x03
|
||||
|
||||
#define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
|
||||
#define RT_SERIAL_TX_DATAQUEUE_LWM 30
|
||||
|
||||
/* Default config for serial_configure structure */
|
||||
#define RT_SERIAL_CONFIG_DEFAULT \
|
||||
{ \
|
||||
BAUD_RATE_115200, /* 115200 bits/s */ \
|
||||
DATA_BITS_8, /* 8 databits */ \
|
||||
STOP_BITS_1, /* 1 stopbit */ \
|
||||
PARITY_NONE, /* No parity */ \
|
||||
BIT_ORDER_LSB, /* LSB first sent */ \
|
||||
NRZ_NORMAL, /* Normal mode */ \
|
||||
RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
|
||||
0 \
|
||||
}
|
||||
|
||||
struct serial_configure
|
||||
{
|
||||
rt_uint32_t baud_rate;
|
||||
|
||||
rt_uint32_t data_bits :4;
|
||||
rt_uint32_t stop_bits :2;
|
||||
rt_uint32_t parity :2;
|
||||
rt_uint32_t bit_order :1;
|
||||
rt_uint32_t invert :1;
|
||||
rt_uint32_t bufsz :16;
|
||||
rt_uint32_t reserved :4;
|
||||
};
|
||||
|
||||
/*
|
||||
* Serial FIFO mode
|
||||
*/
|
||||
struct rt_serial_rx_fifo
|
||||
{
|
||||
/* software fifo */
|
||||
rt_uint8_t *buffer;
|
||||
|
||||
rt_uint16_t put_index, get_index;
|
||||
};
|
||||
|
||||
struct rt_serial_tx_fifo
|
||||
{
|
||||
struct rt_completion completion;
|
||||
};
|
||||
|
||||
/*
|
||||
* Serial DMA mode
|
||||
*/
|
||||
struct rt_serial_rx_dma
|
||||
{
|
||||
rt_bool_t activated;
|
||||
};
|
||||
|
||||
struct rt_serial_tx_dma
|
||||
{
|
||||
rt_bool_t activated;
|
||||
struct rt_data_queue data_queue;
|
||||
};
|
||||
|
||||
struct rt_serial_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
const struct rt_uart_ops *ops;
|
||||
struct serial_configure config;
|
||||
|
||||
void *serial_rx;
|
||||
void *serial_tx;
|
||||
};
|
||||
typedef struct rt_serial_device rt_serial_t;
|
||||
|
||||
/**
|
||||
* uart operators
|
||||
*/
|
||||
struct rt_uart_ops
|
||||
{
|
||||
rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
|
||||
rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
|
||||
|
||||
int (*putc)(struct rt_serial_device *serial, char c);
|
||||
int (*getc)(struct rt_serial_device *serial);
|
||||
|
||||
rt_size_t (*dma_transmit)(struct rt_serial_device *serial, const rt_uint8_t *buf, rt_size_t size, int direction);
|
||||
};
|
||||
|
||||
void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
|
||||
|
||||
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
|
||||
const char *name,
|
||||
rt_uint32_t flag,
|
||||
void *data);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* File : 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
|
||||
* 2012-11-23 Bernard Add extern "C"
|
||||
*/
|
||||
|
||||
#ifndef __SPI_H__
|
||||
#define __SPI_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
#endif
|
||||
|
||||
#define RT_SPI_CPHA (1<<0) /* bit[0]:CPHA, clock phase */
|
||||
#define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
|
||||
/**
|
||||
* At CPOL=0 the base value of the clock is zero
|
||||
* - For CPHA=0, data are captured on the clock's rising edge (low¡úhigh transition)
|
||||
* and data are propagated on a falling edge (high¡úlow clock transition).
|
||||
* - For CPHA=1, data are captured on the clock's falling edge and data are
|
||||
* propagated on a rising edge.
|
||||
* At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
|
||||
* - For CPHA=0, data are captured on clock's falling edge and data are propagated
|
||||
* on a rising edge.
|
||||
* - For CPHA=1, data are captured on clock's rising edge and data are propagated
|
||||
* on a falling edge.
|
||||
*/
|
||||
#define RT_SPI_LSB (0<<2) /* bit[2]: 0-LSB */
|
||||
#define RT_SPI_MSB (1<<2) /* bit[2]: 1-MSB */
|
||||
|
||||
#define RT_SPI_MASTER (0<<3) /* SPI master device */
|
||||
#define RT_SPI_SLAVE (1<<3) /* SPI slave device */
|
||||
|
||||
#define RT_SPI_MODE_0 (0 | 0) /* CPOL = 0, CPHA = 0 */
|
||||
#define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /* CPOL = 0, CPHA = 1 */
|
||||
#define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */
|
||||
#define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */
|
||||
|
||||
#define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB)
|
||||
|
||||
/**
|
||||
* SPI message structure
|
||||
*/
|
||||
struct rt_spi_message
|
||||
{
|
||||
const void *send_buf;
|
||||
void *recv_buf;
|
||||
rt_size_t length;
|
||||
struct rt_spi_message *next;
|
||||
|
||||
unsigned cs_take : 1;
|
||||
unsigned cs_release : 1;
|
||||
};
|
||||
|
||||
/**
|
||||
* SPI configuration structure
|
||||
*/
|
||||
struct rt_spi_configuration
|
||||
{
|
||||
rt_uint8_t mode;
|
||||
rt_uint8_t data_width;
|
||||
rt_uint16_t reserved;
|
||||
|
||||
rt_uint32_t max_hz;
|
||||
};
|
||||
|
||||
struct rt_spi_ops;
|
||||
struct rt_spi_bus
|
||||
{
|
||||
struct rt_device parent;
|
||||
const struct rt_spi_ops *ops;
|
||||
|
||||
struct rt_mutex lock;
|
||||
struct rt_spi_device *owner;
|
||||
};
|
||||
|
||||
/**
|
||||
* SPI operators
|
||||
*/
|
||||
struct rt_spi_ops
|
||||
{
|
||||
rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
|
||||
rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
|
||||
};
|
||||
|
||||
/**
|
||||
* SPI Virtual BUS, one device must connected to a virtual BUS
|
||||
*/
|
||||
struct rt_spi_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
struct rt_spi_bus *bus;
|
||||
|
||||
struct rt_spi_configuration config;
|
||||
};
|
||||
#define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
|
||||
|
||||
/* register a SPI bus */
|
||||
rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
|
||||
const char *name,
|
||||
const struct rt_spi_ops *ops);
|
||||
|
||||
/* attach a device on SPI bus */
|
||||
rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
|
||||
const char *name,
|
||||
const char *bus_name,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* This function takes SPI bus.
|
||||
*
|
||||
* @param device the SPI device attached to SPI bus
|
||||
*
|
||||
* @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
|
||||
*/
|
||||
rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
|
||||
|
||||
/**
|
||||
* This function releases SPI bus.
|
||||
*
|
||||
* @param device the SPI device attached to SPI bus
|
||||
*
|
||||
* @return RT_EOK on release SPI bus successfully.
|
||||
*/
|
||||
rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
|
||||
|
||||
/**
|
||||
* This function take SPI device (takes CS of SPI device).
|
||||
*
|
||||
* @param device the SPI device attached to SPI bus
|
||||
*
|
||||
* @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
|
||||
*/
|
||||
rt_err_t rt_spi_take(struct rt_spi_device *device);
|
||||
|
||||
/**
|
||||
* This function releases SPI device (releases CS of SPI device).
|
||||
*
|
||||
* @param device the SPI device attached to SPI bus
|
||||
*
|
||||
* @return RT_EOK on release SPI device successfully.
|
||||
*/
|
||||
rt_err_t rt_spi_release(struct rt_spi_device *device);
|
||||
|
||||
/* set configuration on SPI device */
|
||||
rt_err_t rt_spi_configure(struct rt_spi_device *device,
|
||||
struct rt_spi_configuration *cfg);
|
||||
|
||||
/* send data then receive data from SPI device */
|
||||
rt_err_t rt_spi_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 rt_spi_send_then_send(struct rt_spi_device *device,
|
||||
const void *send_buf1,
|
||||
rt_size_t send_length1,
|
||||
const void *send_buf2,
|
||||
rt_size_t send_length2);
|
||||
|
||||
/**
|
||||
* This function transmits data to SPI device.
|
||||
*
|
||||
* @param device the SPI device attached to SPI bus
|
||||
* @param send_buf the buffer to be transmitted to SPI device.
|
||||
* @param recv_buf the buffer to save received data from SPI device.
|
||||
* @param length the length of transmitted data.
|
||||
*
|
||||
* @return the actual length of transmitted.
|
||||
*/
|
||||
rt_size_t rt_spi_transfer(struct rt_spi_device *device,
|
||||
const void *send_buf,
|
||||
void *recv_buf,
|
||||
rt_size_t length);
|
||||
|
||||
/**
|
||||
* This function transfers a message list to the SPI device.
|
||||
*
|
||||
* @param device the SPI device attached to SPI bus
|
||||
* @param message the message list to be transmitted to SPI device
|
||||
*
|
||||
* @return RT_NULL if transmits message list successfully,
|
||||
* SPI message which be transmitted failed.
|
||||
*/
|
||||
struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
|
||||
struct rt_spi_message *message);
|
||||
|
||||
rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
|
||||
void *recv_buf,
|
||||
rt_size_t length)
|
||||
{
|
||||
return rt_spi_transfer(device, RT_NULL, recv_buf, length);
|
||||
}
|
||||
|
||||
rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
|
||||
const void *send_buf,
|
||||
rt_size_t length)
|
||||
{
|
||||
return rt_spi_transfer(device, send_buf, RT_NULL, length);
|
||||
}
|
||||
|
||||
rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device *device,
|
||||
rt_uint8_t data)
|
||||
{
|
||||
rt_uint8_t value;
|
||||
|
||||
rt_spi_send_then_recv(device, &data, 1, &value, 1);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
|
||||
rt_uint16_t data)
|
||||
{
|
||||
rt_uint16_t value;
|
||||
|
||||
rt_spi_send_then_recv(device, &data, 2, &value, 2);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function appends a message to the SPI message list.
|
||||
*
|
||||
* @param list the SPI message list header.
|
||||
* @param message the message pointer to be appended to the message list.
|
||||
*/
|
||||
rt_inline void rt_spi_message_append(struct rt_spi_message *list,
|
||||
struct rt_spi_message *message)
|
||||
{
|
||||
RT_ASSERT(list != RT_NULL);
|
||||
if (message == RT_NULL)
|
||||
return; /* not append */
|
||||
|
||||
while (list->next != RT_NULL)
|
||||
{
|
||||
list = list->next;
|
||||
}
|
||||
|
||||
list->next = message;
|
||||
message->next = RT_NULL;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+433
@@ -0,0 +1,433 @@
|
||||
/*
|
||||
* File : usb_common.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 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
|
||||
* 2012-10-01 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#ifndef __USB_COMMON_H__
|
||||
#define __USB_COMMON_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define RT_DEBUG_USB 0x00
|
||||
#define USB_DYNAMIC 0x00
|
||||
|
||||
#define USB_CLASS_DEVICE 0x00
|
||||
#define USB_CLASS_AUDIO 0x01
|
||||
#define USB_CLASS_CDC 0x02
|
||||
#define USB_CLASS_HID 0x03
|
||||
#define USB_CLASS_PHYSICAL 0x05
|
||||
#define USB_CLASS_IMAGE 0x06
|
||||
#define USB_CLASS_PRINTER 0x07
|
||||
#define USB_CLASS_MASS_STORAGE 0x08
|
||||
#define USB_CLASS_HUB 0x09
|
||||
#define USB_CLASS_CDC_DATA 0x0a
|
||||
#define USB_CLASS_SMART_CARD 0x0b
|
||||
#define USB_CLASS_SECURITY 0x0d
|
||||
#define USB_CLASS_VIDEO 0x0e
|
||||
#define USB_CLASS_HEALTHCARE 0x0f
|
||||
#define USB_CLASS_DIAG_DEVICE 0xdc
|
||||
#define USB_CLASS_WIRELESS 0xe0
|
||||
#define USB_CLASS_MISC 0xef
|
||||
#define USB_CLASS_APP_SPECIFIC 0xfe
|
||||
#define USB_CLASS_VEND_SPECIFIC 0xff
|
||||
|
||||
#define USB_DESC_TYPE_DEVICE 0x01
|
||||
#define USB_DESC_TYPE_CONFIGURATION 0x02
|
||||
#define USB_DESC_TYPE_STRING 0x03
|
||||
#define USB_DESC_TYPE_INTERFACE 0x04
|
||||
#define USB_DESC_TYPE_ENDPOINT 0x05
|
||||
#define USB_DESC_TYPE_DEVICEQUALIFIER 0x06
|
||||
#define USB_DESC_TYPE_OTHERSPEED 0x07
|
||||
#define USB_DESC_TYPE_IAD 0x0b
|
||||
#define USB_DESC_TYPE_HID 0x21
|
||||
#define USB_DESC_TYPE_REPORT 0x22
|
||||
#define USB_DESC_TYPE_PHYSICAL 0x23
|
||||
#define USB_DESC_TYPE_HUB 0x29
|
||||
|
||||
#define USB_DESC_LENGTH_DEVICE 0x12
|
||||
#define USB_DESC_LENGTH_CONFIG 0x9
|
||||
#define USB_DESC_LENGTH_IAD 0x8
|
||||
#define USB_DESC_LENGTH_STRING 0x4
|
||||
#define USB_DESC_LENGTH_INTERFACE 0x9
|
||||
#define USB_DESC_LENGTH_ENDPOINT 0x7
|
||||
|
||||
#define USB_REQ_TYPE_STANDARD 0x00
|
||||
#define USB_REQ_TYPE_CLASS 0x20
|
||||
#define USB_REQ_TYPE_VENDOR 0x40
|
||||
#define USB_REQ_TYPE_MASK 0x60
|
||||
|
||||
#define USB_REQ_TYPE_DIR_OUT 0x00
|
||||
#define USB_REQ_TYPE_DIR_IN 0x80
|
||||
|
||||
#define USB_REQ_TYPE_DEVICE 0x00
|
||||
#define USB_REQ_TYPE_INTERFACE 0x01
|
||||
#define USB_REQ_TYPE_ENDPOINT 0x02
|
||||
#define USB_REQ_TYPE_OTHER 0x03
|
||||
#define USB_REQ_TYPE_RECIPIENT_MASK 0x1f
|
||||
|
||||
#define USB_FEATURE_ENDPOINT_HALT 0x00
|
||||
#define USB_FEATURE_DEV_REMOTE_WAKEUP 0x01
|
||||
#define USB_FEATURE_TEST_MODE 0x02
|
||||
|
||||
#define USB_REQ_GET_STATUS 0x00
|
||||
#define USB_REQ_CLEAR_FEATURE 0x01
|
||||
#define USB_REQ_SET_FEATURE 0x03
|
||||
#define USB_REQ_SET_ADDRESS 0x05
|
||||
#define USB_REQ_GET_DESCRIPTOR 0x06
|
||||
#define USB_REQ_SET_DESCRIPTOR 0x07
|
||||
#define USB_REQ_GET_CONFIGURATION 0x08
|
||||
#define USB_REQ_SET_CONFIGURATION 0x09
|
||||
#define USB_REQ_GET_INTERFACE 0x0A
|
||||
#define USB_REQ_SET_INTERFACE 0x0B
|
||||
#define USB_REQ_SYNCH_FRAME 0x0C
|
||||
#define USB_REQ_SET_ENCRYPTION 0x0D
|
||||
#define USB_REQ_GET_ENCRYPTION 0x0E
|
||||
#define USB_REQ_RPIPE_ABORT 0x0E
|
||||
#define USB_REQ_SET_HANDSHAKE 0x0F
|
||||
#define USB_REQ_RPIPE_RESET 0x0F
|
||||
#define USB_REQ_GET_HANDSHAKE 0x10
|
||||
#define USB_REQ_SET_CONNECTION 0x11
|
||||
#define USB_REQ_SET_SECURITY_DATA 0x12
|
||||
#define USB_REQ_GET_SECURITY_DATA 0x13
|
||||
#define USB_REQ_SET_WUSB_DATA 0x14
|
||||
#define USB_REQ_LOOPBACK_DATA_WRITE 0x15
|
||||
#define USB_REQ_LOOPBACK_DATA_READ 0x16
|
||||
#define USB_REQ_SET_INTERFACE_DS 0x17
|
||||
|
||||
#define USB_STRING_LANGID_INDEX 0x00
|
||||
#define USB_STRING_MANU_INDEX 0x01
|
||||
#define USB_STRING_PRODUCT_INDEX 0x02
|
||||
#define USB_STRING_SERIAL_INDEX 0x03
|
||||
#define USB_STRING_CONFIG_INDEX 0x04
|
||||
#define USB_STRING_INTERFACE_INDEX 0x05
|
||||
|
||||
#define USB_PID_OUT 0x01
|
||||
#define USB_PID_ACK 0x02
|
||||
#define USB_PID_DATA0 0x03
|
||||
#define USB_PID_SOF 0x05
|
||||
#define USB_PID_IN 0x09
|
||||
#define USB_PID_NACK 0x0A
|
||||
#define USB_PID_DATA1 0x0B
|
||||
#define USB_PID_PRE 0x0C
|
||||
#define USB_PID_SETUP 0x0D
|
||||
#define USB_PID_STALL 0x0E
|
||||
|
||||
#define USB_EP_DESC_OUT 0x00
|
||||
#define USB_EP_DESC_IN 0x80
|
||||
#define USB_EP_DESC_NUM_MASK 0x0f
|
||||
|
||||
#define USB_EP_ATTR_CONTROL 0x00
|
||||
#define USB_EP_ATTR_ISOC 0x01
|
||||
#define USB_EP_ATTR_BULK 0x02
|
||||
#define USB_EP_ATTR_INT 0x03
|
||||
#define USB_EP_ATTR_TYPE_MASK 0x03
|
||||
|
||||
#define USB_EPNO_MASK 0x7f
|
||||
#define USB_DIR_OUT 0x00
|
||||
#define USB_DIR_IN 0x80
|
||||
#define USB_DIR_MASK 0x80
|
||||
|
||||
#define RH_GET_PORT_STATUS 0
|
||||
#define RH_SET_PORT_STATUS 1
|
||||
#define RH_CLEAR_PORT_FEATURE 2
|
||||
#define RH_SET_PORT_FEATURE 3
|
||||
|
||||
/*
|
||||
* Port feature numbers
|
||||
*/
|
||||
#define PORT_FEAT_CONNECTION 0
|
||||
#define PORT_FEAT_ENABLE 1
|
||||
#define PORT_FEAT_SUSPEND 2
|
||||
#define PORT_FEAT_OVER_CURRENT 3
|
||||
#define PORT_FEAT_RESET 4
|
||||
#define PORT_FEAT_POWER 8
|
||||
#define PORT_FEAT_LOWSPEED 9
|
||||
#define PORT_FEAT_HIGHSPEED 10
|
||||
#define PORT_FEAT_C_CONNECTION 16
|
||||
#define PORT_FEAT_C_ENABLE 17
|
||||
#define PORT_FEAT_C_SUSPEND 18
|
||||
#define PORT_FEAT_C_OVER_CURRENT 19
|
||||
#define PORT_FEAT_C_RESET 20
|
||||
|
||||
/*
|
||||
The HcRhPortStatus[1:NDP] register is used to control and report port events on a per-port
|
||||
basis. NumberDownstreamPorts represents the number of HcRhPortStatus registers that are
|
||||
implemented in hardware. The lower word is used to reflect the port status, whereas the upper
|
||||
word reflects the status change bits. Some status bits are implemented with special write behavior
|
||||
(see below). If a transaction (token through handshake) is in progress when a write to change
|
||||
port status occurs, the resulting port status change must be postponed until the transaction
|
||||
completes. Reserved bits should always be written '0'.
|
||||
*/
|
||||
#define PORT_CCS 0x00000001UL /* R:CurrentConnectStatus - W:ClearPortEnable */
|
||||
#define PORT_PES 0x00000002UL /* R:PortEnableStatus - W:SetPortEnable */
|
||||
#define PORT_PSS 0x00000004UL /* R:PortSuspendStatus - W:SetPortSuspend */
|
||||
#define PORT_POCI 0x00000008UL /* R:PortOverCurrentIndicator - W:ClearSuspendStatus */
|
||||
#define PORT_PRS 0x00000010UL /* R:PortResetStatus - W: SetPortReset */
|
||||
#define PORT_PPS 0x00000100UL /* R:PortPowerStatus - W: SetPortPower */
|
||||
#define PORT_LSDA 0x00000200UL /* R:LowSpeedDeviceAttached - W:ClearPortPower */
|
||||
#define PORT_CCSC 0x00010000UL
|
||||
#define PORT_PESC 0x00020000UL
|
||||
#define PORT_PSSC 0x00040000UL
|
||||
#define PORT_POCIC 0x00080000UL
|
||||
#define PORT_PRSC 0x00100000UL
|
||||
|
||||
/*
|
||||
*Hub Status & Hub Change bit masks
|
||||
*/
|
||||
#define HUB_STATUS_LOCAL_POWER 0x0001
|
||||
#define HUB_STATUS_OVERCURRENT 0x0002
|
||||
|
||||
#define HUB_CHANGE_LOCAL_POWER 0x0001
|
||||
#define HUB_CHANGE_OVERCURRENT 0x0002
|
||||
|
||||
#define USB_EP_ATTR(attr) (attr & USB_EP_ATTR_TYPE_MASK)
|
||||
#define USB_EP_DESC_NUM(addr) (addr & USB_EP_DESC_NUM_MASK)
|
||||
|
||||
#define uswap_32(x) \
|
||||
((((x) & 0xff000000) >> 24) | \
|
||||
(((x) & 0x00ff0000) >> 8) | \
|
||||
(((x) & 0x0000ff00) << 8) | \
|
||||
(((x) & 0x000000ff) << 24))
|
||||
|
||||
#define uswap_8(x) \
|
||||
(((rt_uint16_t)(*((rt_uint8_t *)(x)))) + \
|
||||
(((rt_uint16_t)(*(((rt_uint8_t *)(x)) + 1))) << 8))
|
||||
|
||||
typedef void (*func_callback)(void *context);
|
||||
typedef enum
|
||||
{
|
||||
USB_STATE_NOTATTACHED = 0,
|
||||
USB_STATE_ATTACHED,
|
||||
USB_STATE_POWERED,
|
||||
USB_STATE_RECONNECTING,
|
||||
USB_STATE_UNAUTHENTICATED,
|
||||
USB_STATE_DEFAULT,
|
||||
USB_STATE_ADDRESS,
|
||||
USB_STATE_CONFIGURED,
|
||||
USB_STATE_SUSPENDED
|
||||
}udevice_state_t;
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
struct usb_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
};
|
||||
typedef struct usb_descriptor* udesc_t;
|
||||
|
||||
struct udevice_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint16_t bcdUSB;
|
||||
rt_uint8_t bDeviceClass;
|
||||
rt_uint8_t bDeviceSubClass;
|
||||
rt_uint8_t bDeviceProtocol;
|
||||
rt_uint8_t bMaxPacketSize0;
|
||||
rt_uint16_t idVendor;
|
||||
rt_uint16_t idProduct;
|
||||
rt_uint16_t bcdDevice;
|
||||
rt_uint8_t iManufacturer;
|
||||
rt_uint8_t iProduct;
|
||||
rt_uint8_t iSerialNumber;
|
||||
rt_uint8_t bNumConfigurations;
|
||||
};
|
||||
typedef struct udevice_descriptor* udev_desc_t;
|
||||
|
||||
struct uconfig_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint16_t wTotalLength;
|
||||
rt_uint8_t bNumInterfaces;
|
||||
rt_uint8_t bConfigurationValue;
|
||||
rt_uint8_t iConfiguration;
|
||||
rt_uint8_t bmAttributes;
|
||||
rt_uint8_t MaxPower;
|
||||
rt_uint8_t data[256];
|
||||
};
|
||||
typedef struct uconfig_descriptor* ucfg_desc_t;
|
||||
|
||||
struct uinterface_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t bInterfaceNumber;
|
||||
rt_uint8_t bAlternateSetting;
|
||||
rt_uint8_t bNumEndpoints;
|
||||
rt_uint8_t bInterfaceClass;
|
||||
rt_uint8_t bInterfaceSubClass;
|
||||
rt_uint8_t bInterfaceProtocol;
|
||||
rt_uint8_t iInterface;
|
||||
};
|
||||
typedef struct uinterface_descriptor* uintf_desc_t;
|
||||
|
||||
/* Interface Association Descriptor (IAD) */
|
||||
struct uiad_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t bDescriptorType;
|
||||
rt_uint8_t bFirstInterface;
|
||||
rt_uint8_t bInterfaceCount;
|
||||
rt_uint8_t bFunctionClass;
|
||||
rt_uint8_t bFunctionSubClass;
|
||||
rt_uint8_t bFunctionProtocol;
|
||||
rt_uint8_t iFunction;
|
||||
};
|
||||
typedef struct uiad_descriptor* uiad_desc_t;
|
||||
|
||||
struct uendpoint_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t bEndpointAddress;
|
||||
rt_uint8_t bmAttributes;
|
||||
rt_uint16_t wMaxPacketSize;
|
||||
rt_uint8_t bInterval;
|
||||
};
|
||||
typedef struct uendpoint_descriptor* uep_desc_t;
|
||||
|
||||
struct ustring_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t String[64];
|
||||
};
|
||||
typedef struct ustring_descriptor* ustr_desc_t;
|
||||
|
||||
struct uhub_descriptor
|
||||
{
|
||||
rt_uint8_t length;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t num_ports;
|
||||
rt_uint16_t characteristics;
|
||||
rt_uint8_t pwron_to_good; /* power on to power good */
|
||||
rt_uint8_t current;
|
||||
rt_uint8_t removable[8];
|
||||
rt_uint8_t pwr_ctl[8];
|
||||
};
|
||||
typedef struct uhub_descriptor* uhub_desc_t;
|
||||
|
||||
struct uhid_descriptor
|
||||
{
|
||||
rt_uint8_t bLength;
|
||||
rt_uint8_t type;
|
||||
rt_uint16_t bcdHID;
|
||||
rt_uint8_t bCountryCode;
|
||||
rt_uint8_t bNumDescriptors;
|
||||
struct hid_descriptor_list
|
||||
{
|
||||
rt_uint8_t type;
|
||||
rt_uint16_t wLength;
|
||||
}Descriptor[1];
|
||||
};
|
||||
typedef struct uhid_descriptor* uhid_desc_t;
|
||||
|
||||
struct ureqest
|
||||
{
|
||||
rt_uint8_t request_type;
|
||||
rt_uint8_t request;
|
||||
rt_uint16_t value;
|
||||
rt_uint16_t index;
|
||||
rt_uint16_t length;
|
||||
};
|
||||
typedef struct ureqest* ureq_t;
|
||||
|
||||
#define MIN(a, b) (a < b ? a : b)
|
||||
#define MAX(a, b) (a > b ? a : b)
|
||||
|
||||
/*
|
||||
* the define related to mass storage
|
||||
*/
|
||||
#define USBREQ_GET_MAX_LUN 0xfe
|
||||
#define USBREQ_MASS_STORAGE_RESET 0xff
|
||||
|
||||
#define SIZEOF_CSW 0x0d
|
||||
#define SIZEOF_CBW 0x1f
|
||||
|
||||
#define CBWFLAGS_DIR_M 0x80
|
||||
#define CBWFLAGS_DIR_IN 0x80
|
||||
#define CBWFLAGS_DIR_OUT 0x00
|
||||
|
||||
#define SCSI_TEST_UNIT_READY 0x00
|
||||
#define SCSI_REQUEST_SENSE 0x03
|
||||
#define SCSI_INQUIRY_CMD 0x12
|
||||
#define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
|
||||
#define SCSI_MODE_SENSE_6 0x1a
|
||||
#define SCSI_START_STOP 0x1b
|
||||
#define SCSI_READ_CAPACITIES 0x23
|
||||
#define SCSI_READ_CAPACITY 0x25
|
||||
#define SCSI_READ_10 0x28
|
||||
#define SCSI_WRITE_10 0x2a
|
||||
#define SCSI_VERIFY_10 0x2f
|
||||
|
||||
#define CBW_SIGNATURE 0x43425355
|
||||
#define CSW_SIGNATURE 0x53425355
|
||||
#define CBW_TAG_VALUE 0x12345678
|
||||
|
||||
struct ustorage_cbw
|
||||
{
|
||||
rt_uint32_t signature;
|
||||
rt_uint32_t tag;
|
||||
rt_uint32_t xfer_len;
|
||||
rt_uint8_t dflags;
|
||||
rt_uint8_t lun;
|
||||
rt_uint8_t cb_len;
|
||||
rt_uint8_t cb[16];
|
||||
};
|
||||
typedef struct ustorage_cbw* ustorage_cbw_t;
|
||||
|
||||
struct ustorage_csw
|
||||
{
|
||||
rt_uint32_t signature;
|
||||
rt_uint32_t tag;
|
||||
rt_uint32_t data_reside;
|
||||
rt_uint8_t status;
|
||||
};
|
||||
typedef struct ustorage_csw* ustorage_csw_t;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
/*
|
||||
* USB device event loop thread configurations
|
||||
*/
|
||||
/* the stack size of USB thread */
|
||||
#ifndef RT_USBD_THREAD_STACK_SZ
|
||||
#define RT_USBD_THREAD_STACK_SZ 2048
|
||||
#endif
|
||||
|
||||
/* the priority of USB thread */
|
||||
#ifndef RT_USBD_THREAD_PRIO
|
||||
#define RT_USBD_THREAD_PRIO 8
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+316
@@ -0,0 +1,316 @@
|
||||
/*
|
||||
* File : usb_device.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 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
|
||||
* 2012-10-01 Yi Qiu first version
|
||||
* 2012-12-12 heyuanjie87 change endpoint and class handler
|
||||
*/
|
||||
|
||||
#ifndef __USB_DEVICE_H__
|
||||
#define __USB_DEVICE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "usb_common.h"
|
||||
|
||||
/* Vendor ID */
|
||||
#ifdef USB_VENDOR_ID
|
||||
#define _VENDOR_ID USB_VENDOR_ID
|
||||
#else
|
||||
#define _VENDOR_ID 0x0EFF
|
||||
#endif
|
||||
/* Product ID */
|
||||
#ifdef USB_PRODUCT_ID
|
||||
#define _PRODUCT_ID USB_PRODUCT_ID
|
||||
#else
|
||||
#define _PRODUCT_ID 0x0001
|
||||
#endif
|
||||
|
||||
#define USB_BCD_DEVICE 0x0200 /* USB Specification Release Number in Binary-Coded Decimal */
|
||||
#define USB_BCD_VERSION 0x0200 /* USB 2.0 */
|
||||
|
||||
struct uclass;
|
||||
struct udevice;
|
||||
struct uendpoint;
|
||||
|
||||
struct udcd_ops
|
||||
{
|
||||
rt_err_t (*set_address)(rt_uint8_t value);
|
||||
rt_err_t (*clear_feature)(rt_uint16_t value, rt_uint16_t index);
|
||||
rt_err_t (*set_feature)(rt_uint16_t value, rt_uint16_t index);
|
||||
rt_err_t (*ep_alloc)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_free)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_stall)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_run)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_stop)(struct uendpoint* ep);
|
||||
rt_err_t (*ep_read)(struct uendpoint* ep, void *buffer, rt_size_t size);
|
||||
rt_size_t (*ep_write)(struct uendpoint* ep, void *buffer, rt_size_t size);
|
||||
rt_err_t (*send_status)(void);
|
||||
};
|
||||
|
||||
struct udcd
|
||||
{
|
||||
struct rt_device parent;
|
||||
struct udcd_ops* ops;
|
||||
struct rt_completion completion;
|
||||
};
|
||||
typedef struct udcd* udcd_t;
|
||||
|
||||
typedef rt_err_t (*udep_handler_t)(struct udevice* device, struct uclass* cls, rt_size_t size);
|
||||
|
||||
struct uendpoint
|
||||
{
|
||||
rt_list_t list;
|
||||
rt_uint8_t* buffer;
|
||||
uep_desc_t ep_desc;
|
||||
udep_handler_t handler;
|
||||
rt_bool_t is_stall;
|
||||
};
|
||||
typedef struct uendpoint* uep_t;
|
||||
|
||||
struct ualtsetting
|
||||
{
|
||||
rt_list_t list;
|
||||
uintf_desc_t intf_desc;
|
||||
void* desc;
|
||||
rt_size_t desc_size;
|
||||
rt_list_t ep_list;
|
||||
};
|
||||
typedef struct ualtsetting* ualtsetting_t;
|
||||
|
||||
typedef rt_err_t (*uintf_handler_t)(struct udevice* device, struct uclass* cls, ureq_t setup);
|
||||
|
||||
struct uinterface
|
||||
{
|
||||
rt_list_t list;
|
||||
rt_uint8_t intf_num;
|
||||
ualtsetting_t curr_setting;
|
||||
rt_list_t setting_list;
|
||||
uintf_handler_t handler;
|
||||
};
|
||||
typedef struct uinterface* uintf_t;
|
||||
|
||||
struct uclass_ops
|
||||
{
|
||||
rt_err_t (*run)(struct udevice* device, struct uclass* cls);
|
||||
rt_err_t (*stop)(struct udevice* device, struct uclass* cls);
|
||||
rt_err_t (*sof_handler)(struct udevice* device, struct uclass* cls);
|
||||
};
|
||||
typedef struct uclass_ops* uclass_ops_t;
|
||||
|
||||
struct uclass
|
||||
{
|
||||
rt_list_t list;
|
||||
uclass_ops_t ops;
|
||||
void* eps;
|
||||
struct udevice* device;
|
||||
udev_desc_t dev_desc;
|
||||
void* user_data;
|
||||
|
||||
rt_list_t intf_list;
|
||||
};
|
||||
typedef struct uclass* uclass_t;
|
||||
|
||||
struct uconfig
|
||||
{
|
||||
rt_list_t list;
|
||||
struct uconfig_descriptor cfg_desc;
|
||||
rt_list_t cls_list;
|
||||
};
|
||||
typedef struct uconfig* uconfig_t;
|
||||
|
||||
struct udevice
|
||||
{
|
||||
rt_list_t list;
|
||||
struct udevice_descriptor dev_desc;
|
||||
const char** str;
|
||||
|
||||
udevice_state_t state;
|
||||
rt_list_t cfg_list;
|
||||
uconfig_t curr_cfg;
|
||||
rt_uint8_t nr_intf;
|
||||
|
||||
udcd_t dcd;
|
||||
};
|
||||
typedef struct udevice* udevice_t;
|
||||
|
||||
enum udev_msg_type
|
||||
{
|
||||
USB_MSG_SETUP_NOTIFY,
|
||||
USB_MSG_DATA_NOTIFY,
|
||||
USB_MSG_SOF,
|
||||
USB_MSG_RESET,
|
||||
/* we don't need to add a "PLUG_IN" event because after the cable is
|
||||
* plugged in(before any SETUP) the classed have nothing to do. If the host
|
||||
* is ready, it will send RESET and we will have USB_MSG_RESET. So, a RESET
|
||||
* should reset and run the class while plug_in is not. */
|
||||
USB_MSG_PLUG_OUT,
|
||||
};
|
||||
typedef enum udev_msg_type udev_msg_type;
|
||||
|
||||
struct udev_msg
|
||||
{
|
||||
udev_msg_type type;
|
||||
udcd_t dcd;
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
rt_size_t size;
|
||||
rt_uint8_t ep_addr;
|
||||
} ep_msg;
|
||||
struct
|
||||
{
|
||||
rt_uint32_t* packet;
|
||||
} setup_msg;
|
||||
} content;
|
||||
};
|
||||
typedef struct udev_msg* udev_msg_t;
|
||||
|
||||
udevice_t rt_usbd_device_create(void);
|
||||
uconfig_t rt_usbd_config_create(void);
|
||||
uclass_t rt_usbd_class_create(udevice_t device,
|
||||
udev_desc_t dev_desc,
|
||||
uclass_ops_t ops);
|
||||
uintf_t rt_usbd_interface_create(udevice_t device, uintf_handler_t handler);
|
||||
uep_t rt_usbd_endpoint_create(uep_desc_t ep_desc, udep_handler_t handler);
|
||||
ualtsetting_t rt_usbd_altsetting_create(rt_size_t desc_size);
|
||||
|
||||
rt_err_t rt_usbd_core_init(void);
|
||||
rt_err_t rt_usb_device_init(const char *udc_name);
|
||||
rt_err_t rt_usbd_post_event(struct udev_msg *msg, rt_size_t size);
|
||||
rt_err_t rt_usbd_free_device(udevice_t device);
|
||||
rt_err_t rt_usbd_device_set_controller(udevice_t device, udcd_t dcd);
|
||||
rt_err_t rt_usbd_device_set_descriptor(udevice_t device, udev_desc_t dev_desc);
|
||||
rt_err_t rt_usbd_device_set_string(udevice_t device, const char** ustring);
|
||||
rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg);
|
||||
rt_err_t rt_usbd_config_add_class(uconfig_t cfg, uclass_t cls);
|
||||
rt_err_t rt_usbd_class_add_interface(uclass_t cls, uintf_t intf);
|
||||
rt_err_t rt_usbd_interface_add_altsetting(uintf_t intf, ualtsetting_t setting);
|
||||
rt_err_t rt_usbd_altsetting_add_endpoint(ualtsetting_t setting, uep_t ep);
|
||||
rt_err_t rt_usbd_altsetting_config_descriptor(ualtsetting_t setting,
|
||||
const void *desc,
|
||||
rt_off_t intf_pos);
|
||||
rt_err_t rt_usbd_set_config(udevice_t device, rt_uint8_t value);
|
||||
rt_err_t rt_usbd_set_altsetting(uintf_t intf, rt_uint8_t value);
|
||||
|
||||
udevice_t rt_usbd_find_device(udcd_t dcd);
|
||||
uconfig_t rt_usbd_find_config(udevice_t device, rt_uint8_t value);
|
||||
uintf_t rt_usbd_find_interface(udevice_t device,
|
||||
rt_uint8_t value,
|
||||
uclass_t *pcls);
|
||||
uep_t rt_usbd_find_endpoint(udevice_t device,
|
||||
uclass_t *pcls,
|
||||
rt_uint8_t ep_addr);
|
||||
|
||||
uclass_t rt_usbd_class_mstorage_create(udevice_t device);
|
||||
uclass_t rt_usbd_class_cdc_create(udevice_t device);
|
||||
uclass_t rt_usbd_class_rndis_create(udevice_t device);
|
||||
uclass_t rt_usbd_class_dap_create(udevice_t device);
|
||||
|
||||
#ifdef RT_USB_DEVICE_COMPOSITE
|
||||
rt_err_t rt_usbd_class_set_iad(uclass_t cls, uiad_desc_t iad_desc);
|
||||
#endif
|
||||
|
||||
rt_inline rt_err_t dcd_set_address(udcd_t dcd, rt_uint8_t value)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->set_address(value);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_clear_feature(udcd_t dcd,
|
||||
rt_uint16_t value,
|
||||
rt_uint16_t index)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->clear_feature(value, index);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_set_feature(udcd_t dcd,
|
||||
rt_uint8_t value,
|
||||
rt_uint16_t index)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->set_feature(value, index);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_stall(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_stall(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_uint8_t dcd_ep_alloc(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_alloc(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_free(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_free(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_run(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_run(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_stop(udcd_t dcd, uep_t ep)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_stop(ep);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_ep_read(udcd_t dcd, uep_t ep, void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_read(ep, buffer, size);
|
||||
}
|
||||
|
||||
rt_inline rt_size_t dcd_ep_write(udcd_t dcd,
|
||||
uep_t ep,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->ep_write(ep, buffer, size);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t dcd_send_status(udcd_t dcd)
|
||||
{
|
||||
RT_ASSERT(dcd != RT_NULL);
|
||||
|
||||
return dcd->ops->send_status();
|
||||
}
|
||||
|
||||
#endif
|
||||
+320
@@ -0,0 +1,320 @@
|
||||
/*
|
||||
* File : usb_host.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2011, 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-3-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#ifndef __RT_USB_HOST_H__
|
||||
#define __RT_USB_HOST_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "usb_common.h"
|
||||
|
||||
#define USB_MAX_DEVICE 0x20
|
||||
#define USB_MAX_INTERFACE 0x08
|
||||
#define USB_HUB_PORT_NUM 0x04
|
||||
#define SIZEOF_USB_REQUEST 0x08
|
||||
|
||||
#define UINST_STATUS_IDLE 0x00
|
||||
#define UINST_STATUS_BUSY 0x01
|
||||
#define UINST_STATUS_ERROR 0x02
|
||||
|
||||
#define UPIPE_STATUS_OK 0x00
|
||||
#define UPIPE_STATUS_STALL 0x01
|
||||
#define UPIPE_STATUS_ERROR 0x02
|
||||
|
||||
struct uhcd;
|
||||
struct uifinst;
|
||||
struct uhubinst;
|
||||
|
||||
struct uclass_driver
|
||||
{
|
||||
rt_list_t list;
|
||||
int class_code;
|
||||
int subclass_code;
|
||||
|
||||
rt_err_t (*run)(void* arg);
|
||||
rt_err_t (*stop)(void* arg);
|
||||
|
||||
void* user_data;
|
||||
};
|
||||
typedef struct uclass_driver* ucd_t;
|
||||
|
||||
struct uprotocal
|
||||
{
|
||||
rt_list_t list;
|
||||
int pro_id;
|
||||
|
||||
rt_err_t (*init)(void* arg);
|
||||
rt_err_t (*callback)(void* arg);
|
||||
};
|
||||
typedef struct uprotocal* uprotocal_t;
|
||||
|
||||
struct uinstance
|
||||
{
|
||||
struct udevice_descriptor dev_desc;
|
||||
ucfg_desc_t cfg_desc;
|
||||
struct uhcd *hcd;
|
||||
|
||||
rt_uint8_t status;
|
||||
rt_uint8_t type;
|
||||
rt_uint8_t index;
|
||||
rt_uint8_t address;
|
||||
rt_uint8_t speed;
|
||||
rt_uint8_t max_packet_size;
|
||||
rt_uint8_t port;
|
||||
|
||||
struct uhubinst* parent;
|
||||
struct uifinst* ifinst[USB_MAX_INTERFACE];
|
||||
};
|
||||
typedef struct uinstance* uinst_t;
|
||||
|
||||
struct uifinst
|
||||
{
|
||||
uinst_t uinst;
|
||||
uintf_desc_t intf_desc;
|
||||
|
||||
ucd_t drv;
|
||||
void *user_data;
|
||||
};
|
||||
typedef struct uifinst* uifinst_t;
|
||||
|
||||
struct upipe
|
||||
{
|
||||
rt_uint32_t status;
|
||||
struct uendpoint_descriptor ep;
|
||||
uifinst_t ifinst;
|
||||
func_callback callback;
|
||||
void* user_data;
|
||||
};
|
||||
typedef struct upipe* upipe_t;
|
||||
|
||||
struct uhubinst
|
||||
{
|
||||
struct uhub_descriptor hub_desc;
|
||||
rt_uint8_t num_ports;
|
||||
rt_uint32_t port_status[USB_HUB_PORT_NUM];
|
||||
struct uinstance* child[USB_HUB_PORT_NUM];
|
||||
|
||||
rt_bool_t is_roothub;
|
||||
upipe_t pipe_in;
|
||||
rt_uint8_t buffer[8];
|
||||
struct uinstance* self;
|
||||
struct uhcd *hcd;
|
||||
};
|
||||
typedef struct uhubinst* uhubinst_t;
|
||||
|
||||
struct uhcd_ops
|
||||
{
|
||||
int (*ctl_xfer)(uinst_t inst, ureq_t setup, void* buffer, int nbytes,
|
||||
int timeout);
|
||||
int (*bulk_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
|
||||
int (*int_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
|
||||
int (*iso_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
|
||||
|
||||
rt_err_t (*alloc_pipe)(struct upipe** pipe, uifinst_t ifinst, uep_desc_t ep,
|
||||
func_callback callback);
|
||||
rt_err_t (*free_pipe)(upipe_t pipe);
|
||||
rt_err_t (*hub_ctrl)(rt_uint16_t port, rt_uint8_t cmd, void *args);
|
||||
};
|
||||
|
||||
struct uhcd
|
||||
{
|
||||
struct rt_device parent;
|
||||
struct uhcd_ops* ops;
|
||||
};
|
||||
typedef struct uhcd* uhcd_t;
|
||||
|
||||
enum uhost_msg_type
|
||||
{
|
||||
USB_MSG_CONNECT_CHANGE,
|
||||
USB_MSG_CALLBACK,
|
||||
};
|
||||
typedef enum uhost_msg_type uhost_msg_type;
|
||||
|
||||
struct uhost_msg
|
||||
{
|
||||
uhost_msg_type type;
|
||||
union
|
||||
{
|
||||
struct uhubinst* uhub;
|
||||
struct
|
||||
{
|
||||
func_callback function;
|
||||
void *context;
|
||||
}cb;
|
||||
}content;
|
||||
};
|
||||
typedef struct uhost_msg* uhost_msg_t;
|
||||
|
||||
/* usb host system interface */
|
||||
void rt_usb_host_init(void);
|
||||
void rt_usb_hub_thread(void);
|
||||
|
||||
/* usb host core interface */
|
||||
uinst_t rt_usb_alloc_instance(void);
|
||||
rt_err_t rt_usb_attatch_instance(uinst_t uinst);
|
||||
rt_err_t rt_usb_detach_instance(uinst_t uinst);
|
||||
rt_err_t rt_usb_get_descriptor(uinst_t uinst,
|
||||
rt_uint8_t type,
|
||||
void *buffer,
|
||||
int nbytes);
|
||||
rt_err_t rt_usb_set_configure(uinst_t uinst, int config);
|
||||
rt_err_t rt_usb_set_address(uinst_t uinst);
|
||||
rt_err_t rt_usb_set_interface(uinst_t uinst, int intf);
|
||||
rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature);
|
||||
rt_err_t rt_usb_get_interface_descriptor(ucfg_desc_t cfg_desc,
|
||||
int num,
|
||||
uintf_desc_t *intf_desc);
|
||||
rt_err_t rt_usb_get_endpoint_descriptor(uintf_desc_t intf_desc,
|
||||
int num,
|
||||
uep_desc_t *ep_desc);
|
||||
|
||||
/* usb class driver interface */
|
||||
rt_err_t rt_usb_class_driver_init(void);
|
||||
rt_err_t rt_usb_class_driver_register(ucd_t drv);
|
||||
rt_err_t rt_usb_class_driver_unregister(ucd_t drv);
|
||||
rt_err_t rt_usb_class_driver_run(ucd_t drv, void *args);
|
||||
rt_err_t rt_usb_class_driver_stop(ucd_t drv, void *args);
|
||||
ucd_t rt_usb_class_driver_find(int class_code, int subclass_code);
|
||||
|
||||
/* usb class driver implement */
|
||||
ucd_t rt_usb_class_driver_hid(void);
|
||||
ucd_t rt_usb_class_driver_hub(void);
|
||||
ucd_t rt_usb_class_driver_storage(void);
|
||||
ucd_t rt_usb_class_driver_adk(void);
|
||||
|
||||
/* usb hid protocal implement */
|
||||
uprotocal_t rt_usb_hid_protocal_kbd(void);
|
||||
uprotocal_t rt_usb_hid_protocal_mouse(void);
|
||||
|
||||
/* usb adk class driver interface */
|
||||
rt_err_t rt_usb_adk_set_string(const char *manufacturer,
|
||||
const char *model,
|
||||
const char *description,
|
||||
const char *version,
|
||||
const char *uri,
|
||||
const char *serial);
|
||||
|
||||
/* usb hub interface */
|
||||
rt_err_t rt_usb_hub_get_descriptor(uinst_t uinst,
|
||||
rt_uint8_t *buffer,
|
||||
rt_size_t size);
|
||||
rt_err_t rt_usb_hub_get_status(uinst_t uinst, rt_uint8_t *buffer);
|
||||
rt_err_t rt_usb_hub_get_port_status(uhubinst_t uhub,
|
||||
rt_uint16_t port,
|
||||
rt_uint8_t *buffer);
|
||||
rt_err_t rt_usb_hub_clear_port_feature(uhubinst_t uhub,
|
||||
rt_uint16_t port,
|
||||
rt_uint16_t feature);
|
||||
rt_err_t rt_usb_hub_set_port_feature(uhubinst_t uhub,
|
||||
rt_uint16_t port,
|
||||
rt_uint16_t feature);
|
||||
rt_err_t rt_usb_hub_reset_port(uhubinst_t uhub, rt_uint16_t port);
|
||||
rt_err_t rt_usb_post_event(struct uhost_msg* msg, rt_size_t size);
|
||||
|
||||
/* usb host controller driver interface */
|
||||
rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd,
|
||||
upipe_t *pipe,
|
||||
uifinst_t ifinst,
|
||||
uep_desc_t ep,
|
||||
func_callback callback)
|
||||
{
|
||||
if (ifinst == RT_NULL)
|
||||
return -RT_EIO;
|
||||
|
||||
return hcd->ops->alloc_pipe(pipe, ifinst, ep, callback);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
|
||||
{
|
||||
RT_ASSERT(pipe != RT_NULL);
|
||||
|
||||
return hcd->ops->free_pipe(pipe);
|
||||
}
|
||||
|
||||
rt_inline int rt_usb_hcd_bulk_xfer(uhcd_t hcd,
|
||||
upipe_t pipe,
|
||||
void *buffer,
|
||||
int nbytes,
|
||||
int timeout)
|
||||
{
|
||||
if (pipe == RT_NULL)
|
||||
return -1;
|
||||
if (pipe->ifinst == RT_NULL)
|
||||
return -1;
|
||||
if (pipe->ifinst->uinst == RT_NULL)
|
||||
return -1;
|
||||
if (pipe->ifinst->uinst->status == UINST_STATUS_IDLE)
|
||||
return -1;
|
||||
|
||||
return hcd->ops->bulk_xfer(pipe, buffer, nbytes, timeout);
|
||||
}
|
||||
|
||||
rt_inline int rt_usb_hcd_control_xfer(uhcd_t hcd,
|
||||
uinst_t uinst,
|
||||
ureq_t setup,
|
||||
void *buffer,
|
||||
int nbytes,
|
||||
int timeout)
|
||||
{
|
||||
if (uinst->status == UINST_STATUS_IDLE)
|
||||
return -1;
|
||||
|
||||
return hcd->ops->ctl_xfer(uinst, setup, buffer, nbytes, timeout);
|
||||
}
|
||||
|
||||
rt_inline int rt_usb_hcd_int_xfer(uhcd_t hcd,
|
||||
upipe_t pipe,
|
||||
void *buffer,
|
||||
int nbytes,
|
||||
int timeout)
|
||||
{
|
||||
if (pipe == RT_NULL)
|
||||
return -1;
|
||||
if (pipe->ifinst == RT_NULL)
|
||||
return -1;
|
||||
if (pipe->ifinst->uinst == RT_NULL)
|
||||
return -1;
|
||||
if (pipe->ifinst->uinst->status == UINST_STATUS_IDLE)
|
||||
return -1;
|
||||
|
||||
return hcd->ops->int_xfer(pipe, buffer, nbytes, timeout);
|
||||
}
|
||||
|
||||
rt_inline rt_err_t rt_usb_hcd_hub_control(uhcd_t hcd,
|
||||
rt_uint16_t port,
|
||||
rt_uint8_t cmd,
|
||||
void *args)
|
||||
{
|
||||
return hcd->ops->hub_ctrl(port, cmd, args);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,361 @@
|
||||
/*
|
||||
* File : rtdevice.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
|
||||
* 2012-01-08 bernard first version.
|
||||
* 2014-07-12 bernard Add workqueue implementation.
|
||||
*/
|
||||
|
||||
#ifndef __RT_DEVICE_H__
|
||||
#define __RT_DEVICE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define RT_DEVICE(device) ((rt_device_t)device)
|
||||
|
||||
/* completion flag */
|
||||
struct rt_completion
|
||||
{
|
||||
rt_uint32_t flag;
|
||||
|
||||
/* suspended list */
|
||||
rt_list_t suspended_list;
|
||||
};
|
||||
|
||||
/* ring buffer */
|
||||
struct rt_ringbuffer
|
||||
{
|
||||
rt_uint8_t *buffer_ptr;
|
||||
/* use the msb of the {read,write}_index as mirror bit. You can see this as
|
||||
* if the buffer adds a virtual mirror and the pointers point either to the
|
||||
* normal or to the mirrored buffer. If the write_index has the same value
|
||||
* with the read_index, but in a different mirror, the buffer is full.
|
||||
* While if the write_index and the read_index are the same and within the
|
||||
* same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
|
||||
*
|
||||
* mirror = 0 mirror = 1
|
||||
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
||||
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
|
||||
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
||||
* read_idx-^ write_idx-^
|
||||
*
|
||||
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
||||
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
|
||||
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
||||
* read_idx-^ ^-write_idx
|
||||
*
|
||||
* The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
|
||||
* But it should be enough for most of the cases.
|
||||
*
|
||||
* Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
|
||||
rt_uint16_t read_mirror : 1;
|
||||
rt_uint16_t read_index : 15;
|
||||
rt_uint16_t write_mirror : 1;
|
||||
rt_uint16_t write_index : 15;
|
||||
/* as we use msb of index as mirror bit, the size should be signed and
|
||||
* could only be positive. */
|
||||
rt_int16_t buffer_size;
|
||||
};
|
||||
|
||||
/* portal device */
|
||||
struct rt_portal_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
struct rt_device *write_dev;
|
||||
struct rt_device *read_dev;
|
||||
};
|
||||
|
||||
/* pipe device */
|
||||
#define PIPE_DEVICE(device) ((struct rt_pipe_device*)(device))
|
||||
enum rt_pipe_flag
|
||||
{
|
||||
/* both read and write won't block */
|
||||
RT_PIPE_FLAG_NONBLOCK_RDWR = 0x00,
|
||||
/* read would block */
|
||||
RT_PIPE_FLAG_BLOCK_RD = 0x01,
|
||||
/* write would block */
|
||||
RT_PIPE_FLAG_BLOCK_WR = 0x02,
|
||||
/* write to this pipe will discard some data when the pipe is full.
|
||||
* When this flag is set, RT_PIPE_FLAG_BLOCK_WR will be ignored since write
|
||||
* operation will always be success. */
|
||||
RT_PIPE_FLAG_FORCE_WR = 0x04,
|
||||
};
|
||||
|
||||
struct rt_pipe_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
/* ring buffer in pipe device */
|
||||
struct rt_ringbuffer ringbuffer;
|
||||
|
||||
enum rt_pipe_flag flag;
|
||||
|
||||
/* suspended list */
|
||||
rt_list_t suspended_read_list;
|
||||
rt_list_t suspended_write_list;
|
||||
|
||||
struct rt_portal_device *write_portal;
|
||||
struct rt_portal_device *read_portal;
|
||||
};
|
||||
|
||||
#define PIPE_CTRL_GET_SPACE 0x14 /**< get the remaining size of a pipe device */
|
||||
|
||||
#define RT_DATAQUEUE_EVENT_UNKNOWN 0x00
|
||||
#define RT_DATAQUEUE_EVENT_POP 0x01
|
||||
#define RT_DATAQUEUE_EVENT_PUSH 0x02
|
||||
#define RT_DATAQUEUE_EVENT_LWM 0x03
|
||||
|
||||
struct rt_data_item;
|
||||
#define RT_DATAQUEUE_SIZE(dq) ((dq)->put_index - (dq)->get_index)
|
||||
#define RT_DATAQUEUE_EMPTY(dq) ((dq)->size - RT_DATAQUEUE_SIZE(dq))
|
||||
/* data queue implementation */
|
||||
struct rt_data_queue
|
||||
{
|
||||
rt_uint16_t size;
|
||||
rt_uint16_t lwm;
|
||||
rt_bool_t waiting_lwm;
|
||||
|
||||
rt_uint16_t get_index;
|
||||
rt_uint16_t put_index;
|
||||
|
||||
struct rt_data_item *queue;
|
||||
|
||||
rt_list_t suspended_push_list;
|
||||
rt_list_t suspended_pop_list;
|
||||
|
||||
/* event notify */
|
||||
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
|
||||
};
|
||||
|
||||
/* workqueue implementation */
|
||||
struct rt_workqueue
|
||||
{
|
||||
rt_list_t work_list;
|
||||
rt_thread_t work_thread;
|
||||
};
|
||||
|
||||
struct rt_work
|
||||
{
|
||||
rt_list_t list;
|
||||
|
||||
void (*work_func)(struct rt_work* work, void* work_data);
|
||||
void *work_data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Completion
|
||||
*/
|
||||
void rt_completion_init(struct rt_completion *completion);
|
||||
rt_err_t rt_completion_wait(struct rt_completion *completion,
|
||||
rt_int32_t timeout);
|
||||
void rt_completion_done(struct rt_completion *completion);
|
||||
|
||||
/**
|
||||
* RingBuffer for DeviceDriver
|
||||
*
|
||||
* Please note that the ring buffer implementation of RT-Thread
|
||||
* has no thread wait or resume feature.
|
||||
*/
|
||||
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
|
||||
rt_uint8_t *pool,
|
||||
rt_int16_t size);
|
||||
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
|
||||
const rt_uint8_t *ptr,
|
||||
rt_uint16_t length);
|
||||
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
|
||||
const rt_uint8_t *ptr,
|
||||
rt_uint16_t length);
|
||||
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb,
|
||||
const rt_uint8_t ch);
|
||||
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb,
|
||||
const rt_uint8_t ch);
|
||||
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
|
||||
rt_uint8_t *ptr,
|
||||
rt_uint16_t length);
|
||||
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
|
||||
|
||||
enum rt_ringbuffer_state
|
||||
{
|
||||
RT_RINGBUFFER_EMPTY,
|
||||
RT_RINGBUFFER_FULL,
|
||||
/* half full is neither full nor empty */
|
||||
RT_RINGBUFFER_HALFFULL,
|
||||
};
|
||||
|
||||
rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
|
||||
{
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
return rb->buffer_size;
|
||||
}
|
||||
|
||||
rt_inline enum rt_ringbuffer_state
|
||||
rt_ringbuffer_status(struct rt_ringbuffer *rb)
|
||||
{
|
||||
if (rb->read_index == rb->write_index)
|
||||
{
|
||||
if (rb->read_mirror == rb->write_mirror)
|
||||
return RT_RINGBUFFER_EMPTY;
|
||||
else
|
||||
return RT_RINGBUFFER_FULL;
|
||||
}
|
||||
return RT_RINGBUFFER_HALFFULL;
|
||||
}
|
||||
|
||||
/** return the size of data in rb */
|
||||
rt_inline rt_uint16_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
|
||||
{
|
||||
switch (rt_ringbuffer_status(rb))
|
||||
{
|
||||
case RT_RINGBUFFER_EMPTY:
|
||||
return 0;
|
||||
case RT_RINGBUFFER_FULL:
|
||||
return rb->buffer_size;
|
||||
case RT_RINGBUFFER_HALFFULL:
|
||||
default:
|
||||
if (rb->write_index > rb->read_index)
|
||||
return rb->write_index - rb->read_index;
|
||||
else
|
||||
return rb->buffer_size - (rb->read_index - rb->write_index);
|
||||
};
|
||||
}
|
||||
|
||||
/** return the size of empty space in rb */
|
||||
#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
|
||||
|
||||
/**
|
||||
* Pipe Device
|
||||
*/
|
||||
rt_err_t rt_pipe_init(struct rt_pipe_device *pipe,
|
||||
const char *name,
|
||||
enum rt_pipe_flag flag,
|
||||
rt_uint8_t *buf,
|
||||
rt_size_t size);
|
||||
rt_err_t rt_pipe_detach(struct rt_pipe_device *pipe);
|
||||
#ifdef RT_USING_HEAP
|
||||
rt_err_t rt_pipe_create(const char *name, enum rt_pipe_flag flag, rt_size_t size);
|
||||
void rt_pipe_destroy(struct rt_pipe_device *pipe);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Portal for DeviceDriver
|
||||
*/
|
||||
|
||||
rt_err_t rt_portal_init(struct rt_portal_device *portal,
|
||||
const char *portal_name,
|
||||
const char *write_dev,
|
||||
const char *read_dev);
|
||||
rt_err_t rt_portal_detach(struct rt_portal_device *portal);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
rt_err_t rt_portal_create(const char *name,
|
||||
const char *write_dev,
|
||||
const char *read_dev);
|
||||
void rt_portal_destroy(struct rt_portal_device *portal);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* DataQueue for DeviceDriver
|
||||
*/
|
||||
rt_err_t rt_data_queue_init(struct rt_data_queue *queue,
|
||||
rt_uint16_t size,
|
||||
rt_uint16_t lwm,
|
||||
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event));
|
||||
rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
|
||||
const void *data_ptr,
|
||||
rt_size_t data_size,
|
||||
rt_int32_t timeout);
|
||||
rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
|
||||
const void **data_ptr,
|
||||
rt_size_t *size,
|
||||
rt_int32_t timeout);
|
||||
rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
|
||||
const void **data_ptr,
|
||||
rt_size_t *size);
|
||||
void rt_data_queue_reset(struct rt_data_queue *queue);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/**
|
||||
* WorkQueue for DeviceDriver
|
||||
*/
|
||||
struct rt_workqueue *rt_workqueue_create(const char* name, rt_uint16_t stack_size, rt_uint8_t priority);
|
||||
rt_err_t rt_workqueue_destroy(struct rt_workqueue* queue);
|
||||
rt_err_t rt_workqueue_dowork(struct rt_workqueue* queue, struct rt_work* work);
|
||||
rt_err_t rt_workqueue_cancel_work(struct rt_workqueue* queue, struct rt_work* work);
|
||||
|
||||
rt_inline void rt_work_init(struct rt_work* work, void (*work_func)(struct rt_work* work, void* work_data),
|
||||
void* work_data)
|
||||
{
|
||||
rt_list_init(&(work->list));
|
||||
work->work_func = work_func;
|
||||
work->work_data = work_data;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_RTC
|
||||
#include "drivers/rtc.h"
|
||||
#ifdef RT_USING_ALARM
|
||||
#include "drivers/alarm.h"
|
||||
#endif
|
||||
#endif /* RT_USING_RTC */
|
||||
|
||||
#ifdef RT_USING_SPI
|
||||
#include "drivers/spi.h"
|
||||
#endif /* RT_USING_SPI */
|
||||
|
||||
#ifdef RT_USING_MTD_NOR
|
||||
#include "drivers/mtd_nor.h"
|
||||
#endif /* RT_USING_MTD_NOR */
|
||||
|
||||
#ifdef RT_USING_MTD_NAND
|
||||
#include "drivers/mtd_nand.h"
|
||||
#endif /* RT_USING_MTD_NAND */
|
||||
|
||||
#ifdef RT_USING_USB_DEVICE
|
||||
#include "drivers/usb_device.h"
|
||||
#endif /* RT_USING_USB_DEVICE */
|
||||
|
||||
#ifdef RT_USING_USB_HOST
|
||||
#include "drivers/usb_host.h"
|
||||
#endif /* RT_USING_USB_HOST */
|
||||
|
||||
#ifdef RT_USING_SERIAL
|
||||
#include "drivers/serial.h"
|
||||
#endif /* RT_USING_SERIAL */
|
||||
|
||||
#ifdef RT_USING_I2C
|
||||
#include "drivers/i2c.h"
|
||||
#include "drivers/i2c_dev.h"
|
||||
|
||||
#ifdef RT_USING_I2C_BITOPS
|
||||
#include "drivers/i2c-bit-ops.h"
|
||||
#endif /* RT_USING_I2C_BITOPS */
|
||||
#endif /* RT_USING_I2C */
|
||||
|
||||
#ifdef RT_USING_SDIO
|
||||
#include "drivers/mmcsd_core.h"
|
||||
#include "drivers/sd.h"
|
||||
#include "drivers/sdio.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_WDT
|
||||
#include "drivers/watchdog.h"
|
||||
#endif
|
||||
|
||||
#endif /* __RT_DEVICE_H__ */
|
||||
@@ -0,0 +1,8 @@
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
CPPPATH = [cwd + '/../include']
|
||||
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_SERIAL'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,615 @@
|
||||
/*
|
||||
* File : serial.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
|
||||
* 2006-03-13 bernard first version
|
||||
* 2012-05-15 lgnq modified according bernard's implementation.
|
||||
* 2012-05-28 bernard code cleanup
|
||||
* 2012-11-23 bernard fix compiler warning.
|
||||
* 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
|
||||
* the size of ring buffer.
|
||||
* 2014-07-10 bernard rewrite serial framework
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
/*
|
||||
* Serial poll routines
|
||||
*/
|
||||
rt_inline int _serial_poll_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
|
||||
{
|
||||
int ch;
|
||||
int size;
|
||||
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
size = length;
|
||||
|
||||
while (length)
|
||||
{
|
||||
ch = serial->ops->getc(serial);
|
||||
*data = ch;
|
||||
data ++; length --;
|
||||
|
||||
if (ch == '\n') break;
|
||||
}
|
||||
|
||||
return size - length;
|
||||
}
|
||||
|
||||
rt_inline int _serial_poll_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
|
||||
{
|
||||
int size;
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
|
||||
size = length;
|
||||
while (length)
|
||||
{
|
||||
/*
|
||||
* to be polite with serial console add a line feed
|
||||
* to the carriage return character
|
||||
*/
|
||||
if (*data == '\n' && (serial->parent.flag & RT_DEVICE_FLAG_STREAM))
|
||||
{
|
||||
serial->ops->putc(serial, '\r');
|
||||
}
|
||||
|
||||
serial->ops->putc(serial, *data);
|
||||
|
||||
++ data;
|
||||
-- length;
|
||||
}
|
||||
|
||||
return size - length;
|
||||
}
|
||||
|
||||
/*
|
||||
* Serial interrupt routines
|
||||
*/
|
||||
rt_inline int _serial_int_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
|
||||
{
|
||||
int size;
|
||||
struct rt_serial_rx_fifo* rx_fifo;
|
||||
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
size = length;
|
||||
|
||||
rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
|
||||
/* read from software FIFO */
|
||||
while (length)
|
||||
{
|
||||
int ch;
|
||||
rt_base_t level;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (rx_fifo->get_index != rx_fifo->put_index)
|
||||
{
|
||||
ch = rx_fifo->buffer[rx_fifo->get_index];
|
||||
rx_fifo->get_index += 1;
|
||||
if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* no data, enable interrupt and break out */
|
||||
rt_hw_interrupt_enable(level);
|
||||
break;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
*data = ch & 0xff;
|
||||
data ++; length --;
|
||||
}
|
||||
|
||||
return size - length;
|
||||
}
|
||||
|
||||
rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
|
||||
{
|
||||
int size;
|
||||
struct rt_serial_tx_fifo *tx;
|
||||
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
|
||||
size = length;
|
||||
tx = (struct rt_serial_tx_fifo*) serial->serial_tx;
|
||||
RT_ASSERT(tx != RT_NULL);
|
||||
|
||||
while (length)
|
||||
{
|
||||
if (serial->ops->putc(serial, *(char*)data) == -1)
|
||||
{
|
||||
rt_completion_wait(&(tx->completion), RT_WAITING_FOREVER);
|
||||
continue;
|
||||
}
|
||||
|
||||
data ++; length --;
|
||||
}
|
||||
|
||||
return size - length;
|
||||
}
|
||||
|
||||
/*
|
||||
* Serial DMA routines
|
||||
*/
|
||||
rt_inline int _serial_dma_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
|
||||
{
|
||||
rt_base_t level;
|
||||
int result = RT_EOK;
|
||||
struct rt_serial_rx_dma *rx_dma;
|
||||
|
||||
RT_ASSERT((serial != RT_NULL) && (data != RT_NULL));
|
||||
rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
|
||||
RT_ASSERT(rx_dma != RT_NULL);
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (rx_dma->activated != RT_TRUE)
|
||||
{
|
||||
rx_dma->activated = RT_TRUE;
|
||||
serial->ops->dma_transmit(serial, data, length, RT_SERIAL_DMA_RX);
|
||||
}
|
||||
else result = -RT_EBUSY;
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
if (result == RT_EOK) return length;
|
||||
|
||||
rt_set_errno(result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
|
||||
{
|
||||
rt_base_t level;
|
||||
rt_err_t result;
|
||||
struct rt_serial_tx_dma *tx_dma;
|
||||
|
||||
tx_dma = (struct rt_serial_tx_dma*)(serial->serial_tx);
|
||||
|
||||
result = rt_data_queue_push(&(tx_dma->data_queue), data, length, RT_WAITING_FOREVER);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (tx_dma->activated != RT_TRUE)
|
||||
{
|
||||
tx_dma->activated = RT_TRUE;
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* make a DMA transfer */
|
||||
serial->ops->dma_transmit(serial, data, length, RT_SERIAL_DMA_TX);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_set_errno(result);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* RT-Thread Device Interface */
|
||||
/*
|
||||
* This function initializes serial device.
|
||||
*/
|
||||
static rt_err_t rt_serial_init(struct rt_device *dev)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
/* initialize rx/tx */
|
||||
serial->serial_rx = RT_NULL;
|
||||
serial->serial_tx = RT_NULL;
|
||||
|
||||
/* apply configuration */
|
||||
if (serial->ops->configure)
|
||||
result = serial->ops->configure(serial, &serial->config);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||
{
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
/* check device flag with the open flag */
|
||||
if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
|
||||
return -RT_EIO;
|
||||
if ((oflag & RT_DEVICE_FLAG_DMA_TX) && !(dev->flag & RT_DEVICE_FLAG_DMA_TX))
|
||||
return -RT_EIO;
|
||||
if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))
|
||||
return -RT_EIO;
|
||||
if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
|
||||
return -RT_EIO;
|
||||
|
||||
/* get open flags */
|
||||
dev->open_flag = oflag & 0xff;
|
||||
|
||||
/* initialize the Rx/Tx structure according to open flag */
|
||||
if (serial->serial_rx == RT_NULL)
|
||||
{
|
||||
if (oflag & RT_DEVICE_FLAG_DMA_RX)
|
||||
{
|
||||
struct rt_serial_rx_dma* rx_dma;
|
||||
|
||||
rx_dma = (struct rt_serial_rx_dma*) rt_malloc (sizeof(struct rt_serial_rx_dma));
|
||||
RT_ASSERT(rx_dma != RT_NULL);
|
||||
rx_dma->activated = RT_FALSE;
|
||||
|
||||
serial->serial_rx = rx_dma;
|
||||
dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
|
||||
}
|
||||
else if (oflag & RT_DEVICE_FLAG_INT_RX)
|
||||
{
|
||||
struct rt_serial_rx_fifo* rx_fifo;
|
||||
|
||||
rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
|
||||
serial->config.bufsz);
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
|
||||
rt_memset(rx_fifo->buffer, 0, RT_SERIAL_RB_BUFSZ);
|
||||
rx_fifo->put_index = 0;
|
||||
rx_fifo->get_index = 0;
|
||||
|
||||
serial->serial_rx = rx_fifo;
|
||||
dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
|
||||
/* configure low level device */
|
||||
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||
}
|
||||
else
|
||||
{
|
||||
serial->serial_rx = RT_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (serial->serial_tx == RT_NULL)
|
||||
{
|
||||
if (oflag & RT_DEVICE_FLAG_DMA_TX)
|
||||
{
|
||||
struct rt_serial_tx_dma* tx_dma;
|
||||
|
||||
tx_dma = (struct rt_serial_tx_dma*) rt_malloc (sizeof(struct rt_serial_tx_dma));
|
||||
RT_ASSERT(tx_dma != RT_NULL);
|
||||
|
||||
rt_data_queue_init(&(tx_dma->data_queue), 8, 4, RT_NULL);
|
||||
serial->serial_tx = tx_dma;
|
||||
|
||||
dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
|
||||
}
|
||||
else if (oflag & RT_DEVICE_FLAG_INT_TX)
|
||||
{
|
||||
struct rt_serial_tx_fifo *tx_fifo;
|
||||
|
||||
tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc(sizeof(struct rt_serial_tx_fifo));
|
||||
RT_ASSERT(tx_fifo != RT_NULL);
|
||||
|
||||
rt_completion_init(&(tx_fifo->completion));
|
||||
serial->serial_tx = tx_fifo;
|
||||
|
||||
dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
|
||||
/* configure low level device */
|
||||
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
|
||||
}
|
||||
else
|
||||
{
|
||||
serial->serial_tx = RT_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_serial_close(struct rt_device *dev)
|
||||
{
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
/* this device has more reference count */
|
||||
if (dev->ref_count > 1) return RT_EOK;
|
||||
|
||||
if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
|
||||
{
|
||||
struct rt_serial_rx_fifo* rx_fifo;
|
||||
|
||||
rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
|
||||
rt_free(rx_fifo);
|
||||
serial->serial_rx = RT_NULL;
|
||||
dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
|
||||
/* configure low level device */
|
||||
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
|
||||
}
|
||||
else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
|
||||
{
|
||||
struct rt_serial_rx_dma* rx_dma;
|
||||
|
||||
rx_dma = (struct rt_serial_rx_dma*)serial->serial_tx;
|
||||
RT_ASSERT(rx_dma != RT_NULL);
|
||||
|
||||
rt_free(rx_dma);
|
||||
serial->serial_rx = RT_NULL;
|
||||
dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX;
|
||||
}
|
||||
|
||||
if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
|
||||
{
|
||||
struct rt_serial_tx_fifo* tx_fifo;
|
||||
|
||||
tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_rx;
|
||||
RT_ASSERT(tx_fifo != RT_NULL);
|
||||
|
||||
rt_free(tx_fifo);
|
||||
serial->serial_tx = RT_NULL;
|
||||
dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
|
||||
/* configure low level device */
|
||||
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
|
||||
}
|
||||
else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
|
||||
{
|
||||
struct rt_serial_tx_dma* tx_dma;
|
||||
|
||||
tx_dma = (struct rt_serial_tx_dma*)serial->serial_tx;
|
||||
RT_ASSERT(tx_dma != RT_NULL);
|
||||
|
||||
rt_free(tx_dma);
|
||||
serial->serial_tx = RT_NULL;
|
||||
dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_size_t rt_serial_read(struct rt_device *dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
if (size == 0) return 0;
|
||||
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
|
||||
{
|
||||
return _serial_int_rx(serial, buffer, size);
|
||||
}
|
||||
else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
|
||||
{
|
||||
return _serial_dma_rx(serial, buffer, size);
|
||||
}
|
||||
|
||||
return _serial_poll_rx(serial, buffer, size);
|
||||
}
|
||||
|
||||
static rt_size_t rt_serial_write(struct rt_device *dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
if (size == 0) return 0;
|
||||
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
|
||||
{
|
||||
return _serial_int_tx(serial, buffer, size);
|
||||
}
|
||||
else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
|
||||
{
|
||||
return _serial_dma_tx(serial, buffer, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
return _serial_poll_tx(serial, buffer, size);
|
||||
}
|
||||
}
|
||||
|
||||
static rt_err_t rt_serial_control(struct rt_device *dev,
|
||||
rt_uint8_t cmd,
|
||||
void *args)
|
||||
{
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_DEVICE_CTRL_SUSPEND:
|
||||
/* suspend device */
|
||||
dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
|
||||
break;
|
||||
|
||||
case RT_DEVICE_CTRL_RESUME:
|
||||
/* resume device */
|
||||
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
|
||||
break;
|
||||
|
||||
case RT_DEVICE_CTRL_CONFIG:
|
||||
/* configure device */
|
||||
serial->ops->configure(serial, (struct serial_configure *)args);
|
||||
break;
|
||||
|
||||
default :
|
||||
/* control device */
|
||||
serial->ops->control(serial, cmd, args);
|
||||
break;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/*
|
||||
* serial register
|
||||
*/
|
||||
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
|
||||
const char *name,
|
||||
rt_uint32_t flag,
|
||||
void *data)
|
||||
{
|
||||
struct rt_device *device;
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
|
||||
device = &(serial->parent);
|
||||
|
||||
device->type = RT_Device_Class_Char;
|
||||
device->rx_indicate = RT_NULL;
|
||||
device->tx_complete = RT_NULL;
|
||||
|
||||
device->init = rt_serial_init;
|
||||
device->open = rt_serial_open;
|
||||
device->close = rt_serial_close;
|
||||
device->read = rt_serial_read;
|
||||
device->write = rt_serial_write;
|
||||
device->control = rt_serial_control;
|
||||
device->user_data = data;
|
||||
|
||||
/* register a character device */
|
||||
return rt_device_register(device, name, flag);
|
||||
}
|
||||
|
||||
/* ISR for serial interrupt */
|
||||
void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
|
||||
{
|
||||
switch (event & 0xff)
|
||||
{
|
||||
case RT_SERIAL_EVENT_RX_IND:
|
||||
{
|
||||
int ch = -1;
|
||||
rt_base_t level;
|
||||
struct rt_serial_rx_fifo* rx_fifo;
|
||||
|
||||
rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
|
||||
/* interrupt mode receive */
|
||||
RT_ASSERT(serial->parent.open_flag & RT_DEVICE_FLAG_INT_RX);
|
||||
|
||||
while (1)
|
||||
{
|
||||
ch = serial->ops->getc(serial);
|
||||
if (ch == -1) break;
|
||||
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
rx_fifo->buffer[rx_fifo->put_index] = ch;
|
||||
rx_fifo->put_index += 1;
|
||||
if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
|
||||
|
||||
/* if the next position is read index, discard this 'read char' */
|
||||
if (rx_fifo->put_index == rx_fifo->get_index)
|
||||
{
|
||||
rx_fifo->get_index += 1;
|
||||
if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
|
||||
/* invoke callback */
|
||||
if (serial->parent.rx_indicate != RT_NULL)
|
||||
{
|
||||
rt_size_t rx_length;
|
||||
|
||||
/* get rx length */
|
||||
level = rt_hw_interrupt_disable();
|
||||
rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
|
||||
(serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
serial->parent.rx_indicate(&serial->parent, rx_length);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RT_SERIAL_EVENT_TX_DONE:
|
||||
{
|
||||
struct rt_serial_tx_fifo* tx_fifo;
|
||||
|
||||
tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
|
||||
rt_completion_done(&(tx_fifo->completion));
|
||||
break;
|
||||
}
|
||||
case RT_SERIAL_EVENT_TX_DMADONE:
|
||||
{
|
||||
const void *data_ptr;
|
||||
rt_size_t data_size;
|
||||
const void *last_data_ptr;
|
||||
struct rt_serial_tx_dma* tx_dma;
|
||||
|
||||
tx_dma = (struct rt_serial_tx_dma*) serial->serial_tx;
|
||||
|
||||
rt_data_queue_pop(&(tx_dma->data_queue), &last_data_ptr, &data_size, 0);
|
||||
if (rt_data_queue_peak(&(tx_dma->data_queue), &data_ptr, &data_size) == RT_EOK)
|
||||
{
|
||||
/* transmit next data node */
|
||||
tx_dma->activated = RT_TRUE;
|
||||
serial->ops->dma_transmit(serial, data_ptr, data_size, RT_SERIAL_DMA_TX);
|
||||
}
|
||||
else
|
||||
{
|
||||
tx_dma->activated = RT_FALSE;
|
||||
}
|
||||
|
||||
/* invoke callback */
|
||||
if (serial->parent.tx_complete != RT_NULL)
|
||||
{
|
||||
serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RT_SERIAL_EVENT_RX_DMADONE:
|
||||
{
|
||||
int length;
|
||||
struct rt_serial_rx_dma* rx_dma;
|
||||
|
||||
rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
|
||||
/* get DMA rx length */
|
||||
length = (event & (~0xff)) >> 8;
|
||||
serial->parent.rx_indicate(&(serial->parent), length);
|
||||
rx_dma->activated = RT_FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
CPPPATH = [cwd + '/../include']
|
||||
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_DEVICE_IPC'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* File : completion.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 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
|
||||
* 2012-09-30 Bernard first version.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#define RT_COMPLETED 1
|
||||
#define RT_UNCOMPLETED 0
|
||||
|
||||
void rt_completion_init(struct rt_completion *completion)
|
||||
{
|
||||
rt_base_t level;
|
||||
RT_ASSERT(completion != RT_NULL);
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
completion->flag = RT_UNCOMPLETED;
|
||||
rt_list_init(&completion->suspended_list);
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
|
||||
rt_err_t rt_completion_wait(struct rt_completion *completion,
|
||||
rt_int32_t timeout)
|
||||
{
|
||||
rt_err_t result;
|
||||
rt_base_t level;
|
||||
rt_thread_t thread;
|
||||
RT_ASSERT(completion != RT_NULL);
|
||||
|
||||
result = RT_EOK;
|
||||
thread = rt_thread_self();
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (completion->flag != RT_COMPLETED)
|
||||
{
|
||||
/* only one thread can suspend on complete */
|
||||
RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));
|
||||
|
||||
if (timeout == 0)
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* reset thread error number */
|
||||
thread->error = RT_EOK;
|
||||
|
||||
/* suspend thread */
|
||||
rt_thread_suspend(thread);
|
||||
/* add to suspended list */
|
||||
rt_list_insert_before(&(completion->suspended_list),
|
||||
&(thread->tlist));
|
||||
|
||||
/* current context checking */
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* start timer */
|
||||
if (timeout > 0)
|
||||
{
|
||||
/* reset the timeout of thread timer and start it */
|
||||
rt_timer_control(&(thread->thread_timer),
|
||||
RT_TIMER_CTRL_SET_TIME,
|
||||
&timeout);
|
||||
rt_timer_start(&(thread->thread_timer));
|
||||
}
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* do schedule */
|
||||
rt_schedule();
|
||||
|
||||
/* thread is waked up */
|
||||
result = thread->error;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
/* clean completed flag */
|
||||
completion->flag = RT_UNCOMPLETED;
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void rt_completion_done(struct rt_completion *completion)
|
||||
{
|
||||
rt_base_t level;
|
||||
RT_ASSERT(completion != RT_NULL);
|
||||
|
||||
if (completion->flag == RT_COMPLETED)
|
||||
return;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
completion->flag = RT_COMPLETED;
|
||||
|
||||
if (!rt_list_isempty(&(completion->suspended_list)))
|
||||
{
|
||||
/* there is one thread in suspended list */
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* get thread entry */
|
||||
thread = rt_list_entry(completion->suspended_list.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
|
||||
/* resume it */
|
||||
rt_thread_resume(thread);
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* perform a schedule */
|
||||
rt_schedule();
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
/*
|
||||
* File : dataqueue.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 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
|
||||
* 2012-09-30 Bernard first version.
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include <rthw.h>
|
||||
|
||||
struct rt_data_item
|
||||
{
|
||||
const void *data_ptr;
|
||||
rt_size_t data_size;
|
||||
};
|
||||
|
||||
rt_err_t
|
||||
rt_data_queue_init(struct rt_data_queue *queue,
|
||||
rt_uint16_t size,
|
||||
rt_uint16_t lwm,
|
||||
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event))
|
||||
{
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
|
||||
queue->evt_notify = evt_notify;
|
||||
|
||||
queue->size = size;
|
||||
queue->lwm = lwm;
|
||||
queue->waiting_lwm = RT_FALSE;
|
||||
|
||||
queue->get_index = 0;
|
||||
queue->put_index = 0;
|
||||
|
||||
rt_list_init(&(queue->suspended_push_list));
|
||||
rt_list_init(&(queue->suspended_pop_list));
|
||||
|
||||
queue->queue = (struct rt_data_item *)rt_malloc(sizeof(struct rt_data_item) * size);
|
||||
if (queue->queue == RT_NULL)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_data_queue_init);
|
||||
|
||||
rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
|
||||
const void *data_ptr,
|
||||
rt_size_t data_size,
|
||||
rt_int32_t timeout)
|
||||
{
|
||||
rt_uint16_t mask;
|
||||
rt_ubase_t level;
|
||||
rt_thread_t thread;
|
||||
rt_err_t result;
|
||||
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
|
||||
result = RT_EOK;
|
||||
thread = rt_thread_self();
|
||||
mask = queue->size - 1;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
while (queue->put_index - queue->get_index == queue->size)
|
||||
{
|
||||
queue->waiting_lwm = RT_TRUE;
|
||||
|
||||
/* queue is full */
|
||||
if (timeout == 0)
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* current context checking */
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* reset thread error number */
|
||||
thread->error = RT_EOK;
|
||||
|
||||
/* suspend thread on the push list */
|
||||
rt_thread_suspend(thread);
|
||||
rt_list_insert_before(&(queue->suspended_push_list), &(thread->tlist));
|
||||
/* start timer */
|
||||
if (timeout > 0)
|
||||
{
|
||||
/* reset the timeout of thread timer and start it */
|
||||
rt_timer_control(&(thread->thread_timer),
|
||||
RT_TIMER_CTRL_SET_TIME,
|
||||
&timeout);
|
||||
rt_timer_start(&(thread->thread_timer));
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* do schedule */
|
||||
rt_schedule();
|
||||
|
||||
/* thread is waked up */
|
||||
result = thread->error;
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (result != RT_EOK) goto __exit;
|
||||
}
|
||||
|
||||
queue->queue[queue->put_index & mask].data_ptr = data_ptr;
|
||||
queue->queue[queue->put_index & mask].data_size = data_size;
|
||||
queue->put_index += 1;
|
||||
|
||||
if (!rt_list_isempty(&(queue->suspended_pop_list)))
|
||||
{
|
||||
/* there is at least one thread in suspended list */
|
||||
|
||||
/* get thread entry */
|
||||
thread = rt_list_entry(queue->suspended_pop_list.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
|
||||
/* resume it */
|
||||
rt_thread_resume(thread);
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* perform a schedule */
|
||||
rt_schedule();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
__exit:
|
||||
rt_hw_interrupt_enable(level);
|
||||
if ((result == RT_EOK) && queue->evt_notify != RT_NULL)
|
||||
{
|
||||
queue->evt_notify(queue, RT_DATAQUEUE_EVENT_PUSH);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(rt_data_queue_push);
|
||||
|
||||
rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
|
||||
const void** data_ptr,
|
||||
rt_size_t *size,
|
||||
rt_int32_t timeout)
|
||||
{
|
||||
rt_ubase_t level;
|
||||
rt_thread_t thread;
|
||||
rt_err_t result;
|
||||
rt_uint16_t mask;
|
||||
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
RT_ASSERT(data_ptr != RT_NULL);
|
||||
RT_ASSERT(size != RT_NULL);
|
||||
|
||||
result = RT_EOK;
|
||||
thread = rt_thread_self();
|
||||
mask = queue->size - 1;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
while (queue->get_index == queue->put_index)
|
||||
{
|
||||
/* queue is empty */
|
||||
if (timeout == 0)
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* current context checking */
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* reset thread error number */
|
||||
thread->error = RT_EOK;
|
||||
|
||||
/* suspend thread on the pop list */
|
||||
rt_thread_suspend(thread);
|
||||
rt_list_insert_before(&(queue->suspended_pop_list), &(thread->tlist));
|
||||
/* start timer */
|
||||
if (timeout > 0)
|
||||
{
|
||||
/* reset the timeout of thread timer and start it */
|
||||
rt_timer_control(&(thread->thread_timer),
|
||||
RT_TIMER_CTRL_SET_TIME,
|
||||
&timeout);
|
||||
rt_timer_start(&(thread->thread_timer));
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* do schedule */
|
||||
rt_schedule();
|
||||
|
||||
/* thread is waked up */
|
||||
result = thread->error;
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (result != RT_EOK)
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
*data_ptr = queue->queue[queue->get_index & mask].data_ptr;
|
||||
*size = queue->queue[queue->get_index & mask].data_size;
|
||||
|
||||
queue->get_index += 1;
|
||||
|
||||
if ((queue->waiting_lwm == RT_TRUE) &&
|
||||
(queue->put_index - queue->get_index) <= queue->lwm)
|
||||
{
|
||||
queue->waiting_lwm = RT_FALSE;
|
||||
|
||||
/*
|
||||
* there is at least one thread in suspended list
|
||||
* and less than low water mark
|
||||
*/
|
||||
if (!rt_list_isempty(&(queue->suspended_push_list)))
|
||||
{
|
||||
/* get thread entry */
|
||||
thread = rt_list_entry(queue->suspended_push_list.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
|
||||
/* resume it */
|
||||
rt_thread_resume(thread);
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* perform a schedule */
|
||||
rt_schedule();
|
||||
}
|
||||
|
||||
if (queue->evt_notify != RT_NULL)
|
||||
queue->evt_notify(queue, RT_DATAQUEUE_EVENT_LWM);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
__exit:
|
||||
rt_hw_interrupt_enable(level);
|
||||
if ((result == RT_EOK) && (queue->evt_notify != RT_NULL))
|
||||
{
|
||||
queue->evt_notify(queue, RT_DATAQUEUE_EVENT_POP);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(rt_data_queue_pop);
|
||||
|
||||
rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
|
||||
const void** data_ptr,
|
||||
rt_size_t *size)
|
||||
{
|
||||
rt_ubase_t level;
|
||||
rt_uint16_t mask;
|
||||
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
|
||||
mask = queue->size - 1;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
if (queue->get_index == queue->put_index)
|
||||
{
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return -RT_EEMPTY;
|
||||
}
|
||||
|
||||
*data_ptr = queue->queue[queue->get_index & mask].data_ptr;
|
||||
*size = queue->queue[queue->get_index & mask].data_size;
|
||||
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_data_queue_peak);
|
||||
|
||||
void rt_data_queue_reset(struct rt_data_queue *queue)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
register rt_ubase_t temp;
|
||||
|
||||
rt_enter_critical();
|
||||
/* wakeup all suspend threads */
|
||||
|
||||
/* resume on pop list */
|
||||
while (!rt_list_isempty(&(queue->suspended_pop_list)))
|
||||
{
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* get next suspend thread */
|
||||
thread = rt_list_entry(queue->suspended_pop_list.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
/* set error code to RT_ERROR */
|
||||
thread->error = -RT_ERROR;
|
||||
|
||||
/*
|
||||
* resume thread
|
||||
* In rt_thread_resume function, it will remove current thread from
|
||||
* suspend list
|
||||
*/
|
||||
rt_thread_resume(thread);
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
|
||||
/* resume on push list */
|
||||
while (!rt_list_isempty(&(queue->suspended_push_list)))
|
||||
{
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* get next suspend thread */
|
||||
thread = rt_list_entry(queue->suspended_push_list.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
/* set error code to RT_ERROR */
|
||||
thread->error = -RT_ERROR;
|
||||
|
||||
/*
|
||||
* resume thread
|
||||
* In rt_thread_resume function, it will remove current thread from
|
||||
* suspend list
|
||||
*/
|
||||
rt_thread_resume(thread);
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
rt_exit_critical();
|
||||
|
||||
rt_schedule();
|
||||
}
|
||||
RTM_EXPORT(rt_data_queue_reset);
|
||||
@@ -0,0 +1,293 @@
|
||||
/*
|
||||
* File : pipe.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 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
|
||||
* 2012-09-30 Bernard first version.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
static void _rt_pipe_resume_writer(struct rt_pipe_device *pipe)
|
||||
{
|
||||
if (!rt_list_isempty(&pipe->suspended_write_list))
|
||||
{
|
||||
rt_thread_t thread;
|
||||
|
||||
RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_WR);
|
||||
|
||||
/* get suspended thread */
|
||||
thread = rt_list_entry(pipe->suspended_write_list.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
|
||||
/* resume the write thread */
|
||||
rt_thread_resume(thread);
|
||||
|
||||
rt_schedule();
|
||||
}
|
||||
}
|
||||
|
||||
static rt_size_t rt_pipe_read(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
rt_uint32_t level;
|
||||
rt_thread_t thread;
|
||||
struct rt_pipe_device *pipe;
|
||||
rt_size_t read_nbytes;
|
||||
|
||||
pipe = PIPE_DEVICE(dev);
|
||||
RT_ASSERT(pipe != RT_NULL);
|
||||
|
||||
if (!(pipe->flag & RT_PIPE_FLAG_BLOCK_RD))
|
||||
{
|
||||
level = rt_hw_interrupt_disable();
|
||||
read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
|
||||
|
||||
/* if the ringbuffer is empty, there won't be any writer waiting */
|
||||
if (read_nbytes)
|
||||
_rt_pipe_resume_writer(pipe);
|
||||
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return read_nbytes;
|
||||
}
|
||||
|
||||
thread = rt_thread_self();
|
||||
|
||||
/* current context checking */
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
do {
|
||||
level = rt_hw_interrupt_disable();
|
||||
read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
|
||||
if (read_nbytes == 0)
|
||||
{
|
||||
rt_thread_suspend(thread);
|
||||
/* waiting on suspended read list */
|
||||
rt_list_insert_before(&(pipe->suspended_read_list),
|
||||
&(thread->tlist));
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_schedule();
|
||||
}
|
||||
else
|
||||
{
|
||||
_rt_pipe_resume_writer(pipe);
|
||||
rt_hw_interrupt_enable(level);
|
||||
break;
|
||||
}
|
||||
} while (read_nbytes == 0);
|
||||
|
||||
return read_nbytes;
|
||||
}
|
||||
|
||||
static void _rt_pipe_resume_reader(struct rt_pipe_device *pipe)
|
||||
{
|
||||
if (pipe->parent.rx_indicate)
|
||||
pipe->parent.rx_indicate(&pipe->parent,
|
||||
rt_ringbuffer_data_len(&pipe->ringbuffer));
|
||||
|
||||
if (!rt_list_isempty(&pipe->suspended_read_list))
|
||||
{
|
||||
rt_thread_t thread;
|
||||
|
||||
RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_RD);
|
||||
|
||||
/* get suspended thread */
|
||||
thread = rt_list_entry(pipe->suspended_read_list.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
|
||||
/* resume the read thread */
|
||||
rt_thread_resume(thread);
|
||||
|
||||
rt_schedule();
|
||||
}
|
||||
}
|
||||
|
||||
static rt_size_t rt_pipe_write(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
rt_uint32_t level;
|
||||
rt_thread_t thread;
|
||||
struct rt_pipe_device *pipe;
|
||||
rt_size_t write_nbytes;
|
||||
|
||||
pipe = PIPE_DEVICE(dev);
|
||||
RT_ASSERT(pipe != RT_NULL);
|
||||
|
||||
if ((pipe->flag & RT_PIPE_FLAG_FORCE_WR) ||
|
||||
!(pipe->flag & RT_PIPE_FLAG_BLOCK_WR))
|
||||
{
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
if (pipe->flag & RT_PIPE_FLAG_FORCE_WR)
|
||||
write_nbytes = rt_ringbuffer_put_force(&(pipe->ringbuffer),
|
||||
buffer, size);
|
||||
else
|
||||
write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer),
|
||||
buffer, size);
|
||||
|
||||
_rt_pipe_resume_reader(pipe);
|
||||
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return write_nbytes;
|
||||
}
|
||||
|
||||
thread = rt_thread_self();
|
||||
|
||||
/* current context checking */
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
do {
|
||||
level = rt_hw_interrupt_disable();
|
||||
write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
|
||||
if (write_nbytes == 0)
|
||||
{
|
||||
/* pipe full, waiting on suspended write list */
|
||||
rt_thread_suspend(thread);
|
||||
/* waiting on suspended read list */
|
||||
rt_list_insert_before(&(pipe->suspended_write_list),
|
||||
&(thread->tlist));
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_schedule();
|
||||
}
|
||||
else
|
||||
{
|
||||
_rt_pipe_resume_reader(pipe);
|
||||
rt_hw_interrupt_enable(level);
|
||||
break;
|
||||
}
|
||||
} while (write_nbytes == 0);
|
||||
|
||||
return write_nbytes;
|
||||
}
|
||||
|
||||
static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
||||
{
|
||||
if (cmd == PIPE_CTRL_GET_SPACE && args)
|
||||
*(rt_size_t*)args = rt_ringbuffer_space_len(&PIPE_DEVICE(dev)->ringbuffer);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will initialize a pipe device and put it under control of
|
||||
* resource management.
|
||||
*
|
||||
* @param pipe the pipe device
|
||||
* @param name the name of pipe device
|
||||
* @param flag the attribute of the pipe device
|
||||
* @param buf the buffer of pipe device
|
||||
* @param size the size of pipe device buffer
|
||||
*
|
||||
* @return the operation status, RT_EOK on successful
|
||||
*/
|
||||
rt_err_t rt_pipe_init(struct rt_pipe_device *pipe,
|
||||
const char *name,
|
||||
enum rt_pipe_flag flag,
|
||||
rt_uint8_t *buf,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(pipe);
|
||||
RT_ASSERT(buf);
|
||||
|
||||
/* initialize suspended list */
|
||||
rt_list_init(&pipe->suspended_read_list);
|
||||
rt_list_init(&pipe->suspended_write_list);
|
||||
|
||||
/* initialize ring buffer */
|
||||
rt_ringbuffer_init(&pipe->ringbuffer, buf, size);
|
||||
|
||||
pipe->flag = flag;
|
||||
|
||||
/* create pipe */
|
||||
pipe->parent.type = RT_Device_Class_Pipe;
|
||||
pipe->parent.init = RT_NULL;
|
||||
pipe->parent.open = RT_NULL;
|
||||
pipe->parent.close = RT_NULL;
|
||||
pipe->parent.read = rt_pipe_read;
|
||||
pipe->parent.write = rt_pipe_write;
|
||||
pipe->parent.control = rt_pipe_control;
|
||||
|
||||
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
|
||||
}
|
||||
RTM_EXPORT(rt_pipe_init);
|
||||
|
||||
/**
|
||||
* This function will detach a pipe device from resource management
|
||||
*
|
||||
* @param pipe the pipe device
|
||||
*
|
||||
* @return the operation status, RT_EOK on successful
|
||||
*/
|
||||
rt_err_t rt_pipe_detach(struct rt_pipe_device *pipe)
|
||||
{
|
||||
return rt_device_unregister(&pipe->parent);
|
||||
}
|
||||
RTM_EXPORT(rt_pipe_detach);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
rt_err_t rt_pipe_create(const char *name, enum rt_pipe_flag flag, rt_size_t size)
|
||||
{
|
||||
rt_uint8_t *rb_memptr = RT_NULL;
|
||||
struct rt_pipe_device *pipe = RT_NULL;
|
||||
|
||||
/* get aligned size */
|
||||
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
||||
pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device));
|
||||
if (pipe == RT_NULL)
|
||||
return -RT_ENOMEM;
|
||||
|
||||
/* create ring buffer of pipe */
|
||||
rb_memptr = rt_malloc(size);
|
||||
if (rb_memptr == RT_NULL)
|
||||
{
|
||||
rt_free(pipe);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
return rt_pipe_init(pipe, name, flag, rb_memptr, size);
|
||||
}
|
||||
RTM_EXPORT(rt_pipe_create);
|
||||
|
||||
void rt_pipe_destroy(struct rt_pipe_device *pipe)
|
||||
{
|
||||
if (pipe == RT_NULL)
|
||||
return;
|
||||
|
||||
/* un-register pipe device */
|
||||
rt_pipe_detach(pipe);
|
||||
|
||||
/* release memory */
|
||||
rt_free(pipe->ringbuffer.buffer_ptr);
|
||||
rt_free(pipe);
|
||||
|
||||
return;
|
||||
}
|
||||
RTM_EXPORT(rt_pipe_destroy);
|
||||
#endif /* RT_USING_HEAP */
|
||||
@@ -0,0 +1,256 @@
|
||||
/*
|
||||
* File : portal.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2013, 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
|
||||
* 2013-08-19 Grissiom initial version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#define PT_WRITE_DEV(pt) (((struct rt_portal_device*)pt)->write_dev)
|
||||
#define PT_READ_DEV(pt) (((struct rt_portal_device*)pt)->read_dev)
|
||||
|
||||
static rt_err_t _portal_init(rt_device_t dev)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct rt_portal_device *portal;
|
||||
|
||||
RT_ASSERT(dev);
|
||||
|
||||
portal = (struct rt_portal_device*)dev;
|
||||
|
||||
err = rt_device_init(portal->write_dev);
|
||||
if (err != RT_EOK)
|
||||
return err;
|
||||
|
||||
err = rt_device_init(portal->read_dev);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t _portal_open(rt_device_t dev, rt_uint16_t oflag)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct rt_portal_device *portal;
|
||||
|
||||
RT_ASSERT(dev);
|
||||
|
||||
if (!oflag)
|
||||
return -RT_ERROR;
|
||||
|
||||
portal = (struct rt_portal_device*)dev;
|
||||
|
||||
if (oflag & RT_DEVICE_OFLAG_RDONLY)
|
||||
{
|
||||
err = rt_device_open(portal->read_dev, RT_DEVICE_OFLAG_RDONLY);
|
||||
if (err != RT_EOK)
|
||||
return err;
|
||||
}
|
||||
|
||||
if (oflag & RT_DEVICE_OFLAG_WRONLY)
|
||||
{
|
||||
err = rt_device_open(portal->write_dev, RT_DEVICE_OFLAG_WRONLY);
|
||||
if (err != RT_EOK)
|
||||
return err;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t _portal_close(rt_device_t dev)
|
||||
{
|
||||
struct rt_portal_device *portal;
|
||||
|
||||
RT_ASSERT(dev);
|
||||
|
||||
portal = (struct rt_portal_device*)dev;
|
||||
|
||||
rt_device_close(portal->write_dev);
|
||||
rt_device_close(portal->read_dev);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_size_t _portal_read(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
return rt_device_read(PT_READ_DEV(dev),
|
||||
pos, buffer, size);
|
||||
}
|
||||
|
||||
static rt_size_t _portal_write(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
return rt_device_write(PT_WRITE_DEV(dev),
|
||||
pos, buffer, size);
|
||||
}
|
||||
|
||||
static rt_err_t _portal_rx_indicate(rt_device_t dev, rt_size_t size)
|
||||
{
|
||||
struct rt_pipe_device *pipe;
|
||||
|
||||
RT_ASSERT(dev && dev->type == RT_Device_Class_Pipe);
|
||||
|
||||
pipe = (struct rt_pipe_device*)dev;
|
||||
|
||||
if (pipe->read_portal->parent.rx_indicate)
|
||||
return pipe->read_portal->parent.rx_indicate(
|
||||
(rt_device_t)pipe->read_portal, size);
|
||||
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
|
||||
static rt_err_t _portal_tx_complete(rt_device_t dev, void *buf)
|
||||
{
|
||||
struct rt_pipe_device *pipe;
|
||||
|
||||
RT_ASSERT(dev && dev->type == RT_Device_Class_Pipe);
|
||||
|
||||
pipe = (struct rt_pipe_device*)dev;
|
||||
|
||||
if (pipe->write_portal->parent.tx_complete)
|
||||
return pipe->write_portal->parent.tx_complete(
|
||||
(rt_device_t)pipe->write_portal, buf);
|
||||
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will initialize a portal device and put it under control of
|
||||
* resource management.
|
||||
*
|
||||
* Portal is a device that connect devices
|
||||
*
|
||||
* Currently, you can only connect pipes in portal. Pipes are unidirectional.
|
||||
* But with portal, you can construct a bidirectional device with two pipes.
|
||||
* The inner connection is just like this:
|
||||
*
|
||||
* portal0 portal1
|
||||
* read || || write
|
||||
* <--<---||<---<---||<---<-- (pipe0)
|
||||
* || ||
|
||||
* -->--->||--->--->||--->--> (pipe1)
|
||||
* write || || read
|
||||
*
|
||||
* You will always construct two portals on two pipes, say, "portal0" and
|
||||
* "portal1". Data written into "portal0" can be retrieved in "portal1" and
|
||||
* vice versa. `rx_indicate` and `tx_complete` events are propagated
|
||||
* accordingly.
|
||||
*
|
||||
* @param portal the portal device
|
||||
* @param portal_name the name of the portal device
|
||||
* @param write_dev the name of the pipe device that this portal write into
|
||||
* @param read_dev the name of the pipe device that this portal read from
|
||||
*
|
||||
* @return the operation status, RT_EOK on successful. -RT_ENOSYS on one pipe
|
||||
* device could not be found.
|
||||
*/
|
||||
rt_err_t rt_portal_init(struct rt_portal_device *portal,
|
||||
const char *portal_name,
|
||||
const char *write_dev,
|
||||
const char *read_dev)
|
||||
{
|
||||
rt_device_t dev;
|
||||
|
||||
RT_ASSERT(portal);
|
||||
|
||||
portal->parent.type = RT_Device_Class_Portal;
|
||||
portal->parent.init = _portal_init;
|
||||
portal->parent.open = _portal_open;
|
||||
portal->parent.close = _portal_close;
|
||||
portal->parent.write = _portal_write;
|
||||
portal->parent.read = _portal_read;
|
||||
/* single control of the two devices makes no sense */
|
||||
portal->parent.control = RT_NULL;
|
||||
|
||||
dev = rt_device_find(write_dev);
|
||||
if (dev == RT_NULL)
|
||||
return -RT_ENOSYS;
|
||||
RT_ASSERT(dev->type == RT_Device_Class_Pipe);
|
||||
portal->write_dev = dev;
|
||||
rt_device_set_tx_complete(&portal->parent, dev->tx_complete);
|
||||
rt_device_set_tx_complete(dev, _portal_tx_complete);
|
||||
((struct rt_pipe_device*)dev)->write_portal = portal;
|
||||
|
||||
dev = rt_device_find(read_dev);
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
rt_device_set_tx_complete(dev, portal->parent.tx_complete);
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
RT_ASSERT(dev->type == RT_Device_Class_Pipe);
|
||||
portal->read_dev = dev;
|
||||
rt_device_set_rx_indicate(&portal->parent, dev->rx_indicate);
|
||||
rt_device_set_rx_indicate(dev, _portal_rx_indicate);
|
||||
((struct rt_pipe_device*)dev)->read_portal = portal;
|
||||
|
||||
return rt_device_register(&(portal->parent),
|
||||
portal_name,
|
||||
RT_DEVICE_FLAG_RDWR);
|
||||
}
|
||||
RTM_EXPORT(rt_portal_init);
|
||||
|
||||
/**
|
||||
* This function will detach a portal device from resource management
|
||||
*
|
||||
* @param portal the portal device
|
||||
*
|
||||
* @return the operation status, RT_EOK on successful
|
||||
*/
|
||||
rt_err_t rt_portal_detach(struct rt_portal_device *portal)
|
||||
{
|
||||
return rt_device_unregister(&portal->parent);
|
||||
}
|
||||
RTM_EXPORT(rt_portal_detach);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
rt_err_t rt_portal_create(const char *name,
|
||||
const char *write_dev,
|
||||
const char *read_dev)
|
||||
{
|
||||
struct rt_portal_device *portal;
|
||||
|
||||
portal = (struct rt_portal_device*)rt_calloc(1, sizeof(*portal));
|
||||
if (portal == RT_NULL)
|
||||
return -RT_ENOMEM;
|
||||
|
||||
return rt_portal_init(portal, name, write_dev, read_dev);
|
||||
}
|
||||
RTM_EXPORT(rt_portal_create);
|
||||
|
||||
void rt_portal_destroy(struct rt_portal_device *portal)
|
||||
{
|
||||
if (portal == RT_NULL)
|
||||
return;
|
||||
|
||||
rt_portal_detach(portal);
|
||||
|
||||
rt_free(portal);
|
||||
|
||||
return;
|
||||
}
|
||||
RTM_EXPORT(rt_portal_destroy);
|
||||
#endif /* RT_USING_HEAP */
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
* File : ringbuffer.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 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
|
||||
* 2012-09-30 Bernard first version.
|
||||
* 2013-05-08 Grissiom reimplement
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include <string.h>
|
||||
|
||||
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
|
||||
rt_uint8_t *pool,
|
||||
rt_int16_t size)
|
||||
{
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
RT_ASSERT(size > 0)
|
||||
|
||||
/* initialize read and write index */
|
||||
rb->read_mirror = rb->read_index = 0;
|
||||
rb->write_mirror = rb->write_index = 0;
|
||||
|
||||
/* set buffer pool and size */
|
||||
rb->buffer_ptr = pool;
|
||||
rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_init);
|
||||
|
||||
/**
|
||||
* put a block of data into ring buffer
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
|
||||
const rt_uint8_t *ptr,
|
||||
rt_uint16_t length)
|
||||
{
|
||||
rt_uint16_t size;
|
||||
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
/* whether has enough space */
|
||||
size = rt_ringbuffer_space_len(rb);
|
||||
|
||||
/* no space */
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
/* drop some data */
|
||||
if (size < length)
|
||||
length = size;
|
||||
|
||||
if (rb->buffer_size - rb->write_index > length)
|
||||
{
|
||||
/* read_index - write_index = empty space */
|
||||
memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
|
||||
/* this should not cause overflow because there is enough space for
|
||||
* length of data in current mirror */
|
||||
rb->write_index += length;
|
||||
return length;
|
||||
}
|
||||
|
||||
memcpy(&rb->buffer_ptr[rb->write_index],
|
||||
&ptr[0],
|
||||
rb->buffer_size - rb->write_index);
|
||||
memcpy(&rb->buffer_ptr[0],
|
||||
&ptr[rb->buffer_size - rb->write_index],
|
||||
length - (rb->buffer_size - rb->write_index));
|
||||
|
||||
/* we are going into the other side of the mirror */
|
||||
rb->write_mirror = ~rb->write_mirror;
|
||||
rb->write_index = length - (rb->buffer_size - rb->write_index);
|
||||
|
||||
return length;
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_put);
|
||||
|
||||
/**
|
||||
* put a block of data into ring buffer
|
||||
*
|
||||
* When the buffer is full, it will discard the old data.
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
|
||||
const rt_uint8_t *ptr,
|
||||
rt_uint16_t length)
|
||||
{
|
||||
enum rt_ringbuffer_state old_state;
|
||||
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
old_state = rt_ringbuffer_status(rb);
|
||||
|
||||
if (length > rb->buffer_size)
|
||||
length = rb->buffer_size;
|
||||
|
||||
if (rb->buffer_size - rb->write_index > length)
|
||||
{
|
||||
/* read_index - write_index = empty space */
|
||||
memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
|
||||
/* this should not cause overflow because there is enough space for
|
||||
* length of data in current mirror */
|
||||
rb->write_index += length;
|
||||
|
||||
if (old_state == RT_RINGBUFFER_FULL)
|
||||
rb->read_index = rb->write_index;
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
memcpy(&rb->buffer_ptr[rb->write_index],
|
||||
&ptr[0],
|
||||
rb->buffer_size - rb->write_index);
|
||||
memcpy(&rb->buffer_ptr[0],
|
||||
&ptr[rb->buffer_size - rb->write_index],
|
||||
length - (rb->buffer_size - rb->write_index));
|
||||
|
||||
/* we are going into the other side of the mirror */
|
||||
rb->write_mirror = ~rb->write_mirror;
|
||||
rb->write_index = length - (rb->buffer_size - rb->write_index);
|
||||
|
||||
if (old_state == RT_RINGBUFFER_FULL)
|
||||
{
|
||||
rb->read_mirror = ~rb->read_mirror;
|
||||
rb->read_index = rb->write_index;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_put_force);
|
||||
|
||||
/**
|
||||
* get data from ring buffer
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
|
||||
rt_uint8_t *ptr,
|
||||
rt_uint16_t length)
|
||||
{
|
||||
rt_size_t size;
|
||||
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
/* whether has enough data */
|
||||
size = rt_ringbuffer_data_len(rb);
|
||||
|
||||
/* no data */
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
/* less data */
|
||||
if (size < length)
|
||||
length = size;
|
||||
|
||||
if (rb->buffer_size - rb->read_index > length)
|
||||
{
|
||||
/* copy all of data */
|
||||
memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
|
||||
/* this should not cause overflow because there is enough space for
|
||||
* length of data in current mirror */
|
||||
rb->read_index += length;
|
||||
return length;
|
||||
}
|
||||
|
||||
memcpy(&ptr[0],
|
||||
&rb->buffer_ptr[rb->read_index],
|
||||
rb->buffer_size - rb->read_index);
|
||||
memcpy(&ptr[rb->buffer_size - rb->read_index],
|
||||
&rb->buffer_ptr[0],
|
||||
length - (rb->buffer_size - rb->read_index));
|
||||
|
||||
/* we are going into the other side of the mirror */
|
||||
rb->read_mirror = ~rb->read_mirror;
|
||||
rb->read_index = length - (rb->buffer_size - rb->read_index);
|
||||
|
||||
return length;
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_get);
|
||||
|
||||
/**
|
||||
* put a character into ring buffer
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
|
||||
{
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
/* whether has enough space */
|
||||
if (!rt_ringbuffer_space_len(rb))
|
||||
return 0;
|
||||
|
||||
rb->buffer_ptr[rb->write_index] = ch;
|
||||
|
||||
/* flip mirror */
|
||||
if (rb->write_index == rb->buffer_size-1)
|
||||
{
|
||||
rb->write_mirror = ~rb->write_mirror;
|
||||
rb->write_index = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
rb->write_index++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_putchar);
|
||||
|
||||
/**
|
||||
* put a character into ring buffer
|
||||
*
|
||||
* When the buffer is full, it will discard one old data.
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch)
|
||||
{
|
||||
enum rt_ringbuffer_state old_state;
|
||||
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
old_state = rt_ringbuffer_status(rb);
|
||||
|
||||
rb->buffer_ptr[rb->write_index] = ch;
|
||||
|
||||
/* flip mirror */
|
||||
if (rb->write_index == rb->buffer_size-1)
|
||||
{
|
||||
rb->write_mirror = ~rb->write_mirror;
|
||||
rb->write_index = 0;
|
||||
if (old_state == RT_RINGBUFFER_FULL)
|
||||
{
|
||||
rb->read_mirror = ~rb->read_mirror;
|
||||
rb->read_index = rb->write_index;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rb->write_index++;
|
||||
if (old_state == RT_RINGBUFFER_FULL)
|
||||
rb->read_index = rb->write_index;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_putchar_force);
|
||||
|
||||
/**
|
||||
* get a character from a ringbuffer
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
|
||||
{
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
/* ringbuffer is empty */
|
||||
if (!rt_ringbuffer_data_len(rb))
|
||||
return 0;
|
||||
|
||||
/* put character */
|
||||
*ch = rb->buffer_ptr[rb->read_index];
|
||||
|
||||
if (rb->read_index == rb->buffer_size-1)
|
||||
{
|
||||
rb->read_mirror = ~rb->read_mirror;
|
||||
rb->read_index = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
rb->read_index++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
RTM_EXPORT(rt_ringbuffer_getchar);
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
static void _workqueue_thread_entry(void* parameter)
|
||||
{
|
||||
struct rt_work* work;
|
||||
struct rt_workqueue* queue;
|
||||
|
||||
queue = (struct rt_workqueue*) parameter;
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (rt_list_isempty(&(queue->work_list)))
|
||||
{
|
||||
/* no software timer exist, suspend self. */
|
||||
rt_thread_suspend(rt_thread_self());
|
||||
rt_schedule();
|
||||
}
|
||||
|
||||
/* we have work to do with. */
|
||||
rt_enter_critical();
|
||||
work = rt_list_entry(queue->work_list.next, struct rt_work, list);
|
||||
rt_list_remove(&(work->list));
|
||||
rt_exit_critical();
|
||||
|
||||
/* do work */
|
||||
work->work_func(work, work->work_data);
|
||||
}
|
||||
}
|
||||
|
||||
struct rt_workqueue *rt_workqueue_create(const char* name, rt_uint16_t stack_size, rt_uint8_t priority)
|
||||
{
|
||||
struct rt_workqueue *queue = RT_NULL;
|
||||
|
||||
queue = (struct rt_workqueue*)RT_KERNEL_MALLOC(sizeof(struct rt_workqueue));
|
||||
if (queue != RT_NULL)
|
||||
{
|
||||
/* initialize work list */
|
||||
rt_list_init(&(queue->work_list));
|
||||
|
||||
/* create the work thread */
|
||||
queue->work_thread = rt_thread_create(name, _workqueue_thread_entry, queue, stack_size, priority, 10);
|
||||
if (queue->work_thread == RT_NULL)
|
||||
{
|
||||
RT_KERNEL_FREE(queue);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
rt_thread_startup(queue->work_thread);
|
||||
}
|
||||
|
||||
return queue;
|
||||
}
|
||||
|
||||
rt_err_t rt_workqueue_destroy(struct rt_workqueue* queue)
|
||||
{
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
|
||||
rt_thread_delete(queue->work_thread);
|
||||
RT_KERNEL_FREE(queue);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_workqueue_dowork(struct rt_workqueue* queue, struct rt_work* work)
|
||||
{
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
RT_ASSERT(work != RT_NULL);
|
||||
|
||||
rt_enter_critical();
|
||||
/* NOTE: the work MUST be initialized firstly */
|
||||
rt_list_remove(&(work->list));
|
||||
|
||||
rt_list_insert_after(queue->work_list.prev, &(work->list));
|
||||
if (queue->work_thread->stat != RT_THREAD_READY)
|
||||
{
|
||||
rt_exit_critical();
|
||||
/* resume work thread */
|
||||
rt_thread_resume(queue->work_thread);
|
||||
rt_schedule();
|
||||
}
|
||||
else rt_exit_critical();
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_workqueue_cancel_work(struct rt_workqueue* queue, struct rt_work* work)
|
||||
{
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
RT_ASSERT(work != RT_NULL);
|
||||
|
||||
rt_enter_critical();
|
||||
rt_list_remove(&(work->list));
|
||||
rt_exit_critical();
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user