@@ -0,0 +1,45 @@
|
||||
Kernel Design & Implementation
|
||||
- Bernard Xiong <bernard.xiong@gmail.com>
|
||||
|
||||
LwIP 1.3.0/1.3.1/1.3.2/1.4.0
|
||||
- Porting
|
||||
Qiu Yi
|
||||
Mbbill
|
||||
- Testing
|
||||
Bernard Xiong
|
||||
|
||||
Filesystem
|
||||
- Porting and Add Virtual Filesystem
|
||||
- Testing
|
||||
Qiu Yi
|
||||
prife
|
||||
|
||||
RTGUI
|
||||
- Design and Implemenation
|
||||
Bernard Xiong
|
||||
Grissiom
|
||||
|
||||
BSP
|
||||
Bernard Xiong
|
||||
- ATMEL AT91SAM7S64 & AT91SAM7X256 Porting
|
||||
- STM32 Porting
|
||||
- S3C4510 Porting
|
||||
|
||||
Mbbill
|
||||
- ATMEL AT91SAM7X256
|
||||
|
||||
Xulong Cao
|
||||
- QEMU/x86
|
||||
|
||||
Aozima
|
||||
- LPC 2148 Porting
|
||||
- STM32 Porting
|
||||
|
||||
Jing Lee
|
||||
- LPC 2478 Porting
|
||||
|
||||
Qiu Yi
|
||||
- S3C2410 & S3C2440 Porting
|
||||
- TI LM3S
|
||||
|
||||
others...
|
||||
@@ -0,0 +1,340 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
||||
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Library General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Library General
|
||||
Public License instead of this License.
|
||||
@@ -0,0 +1,17 @@
|
||||
# for module compiling
|
||||
import os
|
||||
Import('remove_components')
|
||||
from building import *
|
||||
|
||||
objs = []
|
||||
cwd = GetCurrentDir()
|
||||
list = os.listdir(cwd)
|
||||
|
||||
for item in list:
|
||||
if item in remove_components:
|
||||
continue
|
||||
|
||||
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
|
||||
objs = objs + SConscript(os.path.join(item, 'SConscript'))
|
||||
|
||||
Return('objs')
|
||||
@@ -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
|
||||
@@ -0,0 +1,45 @@
|
||||
Import('rtconfig')
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Split('''
|
||||
shell.c
|
||||
symbol.c
|
||||
cmd.c
|
||||
''')
|
||||
|
||||
fsh_src = Split('''
|
||||
finsh_compiler.c
|
||||
finsh_error.c
|
||||
finsh_heap.c
|
||||
finsh_init.c
|
||||
finsh_node.c
|
||||
finsh_ops.c
|
||||
finsh_parser.c
|
||||
finsh_var.c
|
||||
finsh_vm.c
|
||||
finsh_token.c
|
||||
''')
|
||||
|
||||
msh_src = Split('''
|
||||
msh_cmd.c
|
||||
msh.c
|
||||
''')
|
||||
|
||||
CPPPATH = [cwd]
|
||||
if rtconfig.CROSS_TOOL == 'keil':
|
||||
LINKFLAGS = ' --keep __fsym_*'
|
||||
|
||||
if not GetDepend('FINSH_USING_MSH_ONLY'):
|
||||
LINKFLAGS = LINKFLAGS + ' --keep __vsym_* '
|
||||
else:
|
||||
LINKFLAGS = ''
|
||||
|
||||
if GetDepend('FINSH_USING_MSH'):
|
||||
src = src + msh_src
|
||||
if not GetDepend('FINSH_USING_MSH_ONLY'):
|
||||
src = src + fsh_src
|
||||
|
||||
group = DefineGroup('finsh', src, depend = ['RT_USING_FINSH'], CPPPATH = CPPPATH, LINKFLAGS = LINKFLAGS)
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,893 @@
|
||||
/*
|
||||
* RT-Thread finsh shell commands
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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-04-30 Bernard first implementation
|
||||
* 2006-05-04 Bernard add list_thread,
|
||||
* list_sem,
|
||||
* list_timer
|
||||
* 2006-05-20 Bernard add list_mutex,
|
||||
* list_mailbox,
|
||||
* list_msgqueue,
|
||||
* list_event,
|
||||
* list_fevent,
|
||||
* list_mempool
|
||||
* 2006-06-03 Bernard display stack information in list_thread
|
||||
* 2006-08-10 Bernard change version to invoke rt_show_version
|
||||
* 2008-09-10 Bernard update the list function for finsh syscall
|
||||
* list and sysvar list
|
||||
* 2009-05-30 Bernard add list_device
|
||||
* 2010-04-21 yi.qiu add list_module
|
||||
* 2012-04-29 goprife improve the command line auto-complete feature.
|
||||
* 2012-06-02 lgnq add list_memheap
|
||||
* 2012-10-22 Bernard add MS VC++ patch.
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "finsh.h"
|
||||
|
||||
rt_inline unsigned int rt_list_len(const rt_list_t *l)
|
||||
{
|
||||
unsigned int len = 0;
|
||||
const rt_list_t *p = l;
|
||||
while (p->next != l)
|
||||
{
|
||||
p = p->next;
|
||||
len ++;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
long hello(void)
|
||||
{
|
||||
rt_kprintf("Hello RT-Thread!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(hello, say hello world);
|
||||
|
||||
extern void rt_show_version(void);
|
||||
long version(void)
|
||||
{
|
||||
rt_show_version();
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
|
||||
MSH_CMD_EXPORT(version, show RT-Thread version information);
|
||||
|
||||
extern struct rt_object_information rt_object_container[];
|
||||
|
||||
static long _list_thread(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
struct rt_list_node *node;
|
||||
rt_uint8_t *ptr;
|
||||
|
||||
rt_kprintf(" thread pri status sp stack size max used left tick error\n");
|
||||
rt_kprintf("-------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
thread = rt_list_entry(node, struct rt_thread, list);
|
||||
rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority);
|
||||
|
||||
if (thread->stat == RT_THREAD_READY) rt_kprintf(" ready ");
|
||||
else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
|
||||
else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init ");
|
||||
else if (thread->stat == RT_THREAD_CLOSE) rt_kprintf(" close ");
|
||||
|
||||
ptr = (rt_uint8_t*)thread->stack_addr;
|
||||
while (*ptr == '#')ptr ++;
|
||||
|
||||
rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
|
||||
thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
|
||||
thread->stack_size,
|
||||
thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
|
||||
thread->remaining_tick,
|
||||
thread->error);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_thread(void)
|
||||
{
|
||||
return _list_thread(&rt_object_container[RT_Object_Class_Thread].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_thread, list thread);
|
||||
MSH_CMD_EXPORT(list_thread, list thread);
|
||||
|
||||
static void show_wait_queue(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
struct rt_list_node *node;
|
||||
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
thread = rt_list_entry(node, struct rt_thread, tlist);
|
||||
rt_kprintf("%s", thread->name);
|
||||
|
||||
if (node->next != list)
|
||||
rt_kprintf("/");
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef RT_USING_SEMAPHORE
|
||||
static long _list_sem(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_semaphore *sem;
|
||||
struct rt_list_node *node;
|
||||
|
||||
rt_kprintf("semaphore v suspend thread\n");
|
||||
rt_kprintf("-------- --- --------------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list));
|
||||
if (!rt_list_isempty(&sem->parent.suspend_thread))
|
||||
{
|
||||
rt_kprintf("%-8.*s %03d %d:",
|
||||
RT_NAME_MAX,
|
||||
sem->parent.parent.name,
|
||||
sem->value,
|
||||
rt_list_len(&sem->parent.suspend_thread));
|
||||
show_wait_queue(&(sem->parent.suspend_thread));
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%-8.*s %03d %d\n",
|
||||
RT_NAME_MAX,
|
||||
sem->parent.parent.name,
|
||||
sem->value,
|
||||
rt_list_len(&sem->parent.suspend_thread));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_sem(void)
|
||||
{
|
||||
return _list_sem(&rt_object_container[RT_Object_Class_Semaphore].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system);
|
||||
MSH_CMD_EXPORT(list_sem, list semaphore in system);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_EVENT
|
||||
static long _list_event(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_event *e;
|
||||
struct rt_list_node *node;
|
||||
|
||||
rt_kprintf("event set suspend thread\n");
|
||||
rt_kprintf("-------- ---------- --------------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list));
|
||||
if (!rt_list_isempty(&e->parent.suspend_thread))
|
||||
{
|
||||
rt_kprintf("%-8.*s 0x%08x %03d:",
|
||||
RT_NAME_MAX,
|
||||
e->parent.parent.name,
|
||||
e->set,
|
||||
rt_list_len(&e->parent.suspend_thread));
|
||||
show_wait_queue(&(e->parent.suspend_thread));
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%-8.*s 0x%08x 0\n",
|
||||
RT_NAME_MAX, e->parent.parent.name, e->set);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_event(void)
|
||||
{
|
||||
return _list_event(&rt_object_container[RT_Object_Class_Event].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_event, list event in system);
|
||||
MSH_CMD_EXPORT(list_event, list event in system);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MUTEX
|
||||
static long _list_mutex(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_mutex *m;
|
||||
struct rt_list_node *node;
|
||||
|
||||
rt_kprintf("mutex owner hold suspend thread\n");
|
||||
rt_kprintf("-------- -------- ---- --------------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list));
|
||||
rt_kprintf("%-8.*s %-8.*s %04d %d\n",
|
||||
RT_NAME_MAX,
|
||||
m->parent.parent.name,
|
||||
RT_NAME_MAX,
|
||||
m->owner->name,
|
||||
m->hold,
|
||||
rt_list_len(&m->parent.suspend_thread));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_mutex(void)
|
||||
{
|
||||
return _list_mutex(&rt_object_container[RT_Object_Class_Mutex].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system);
|
||||
MSH_CMD_EXPORT(list_mutex, list mutex in system);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MAILBOX
|
||||
static long _list_mailbox(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_mailbox *m;
|
||||
struct rt_list_node *node;
|
||||
|
||||
rt_kprintf("mailbox entry size suspend thread\n");
|
||||
rt_kprintf("-------- ---- ---- --------------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list));
|
||||
if (!rt_list_isempty(&m->parent.suspend_thread))
|
||||
{
|
||||
rt_kprintf("%-8.*s %04d %04d %d:",
|
||||
RT_NAME_MAX,
|
||||
m->parent.parent.name,
|
||||
m->entry,
|
||||
m->size,
|
||||
rt_list_len(&m->parent.suspend_thread));
|
||||
show_wait_queue(&(m->parent.suspend_thread));
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%-8.*s %04d %04d %d\n",
|
||||
RT_NAME_MAX,
|
||||
m->parent.parent.name,
|
||||
m->entry,
|
||||
m->size,
|
||||
rt_list_len(&m->parent.suspend_thread));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_mailbox(void)
|
||||
{
|
||||
return _list_mailbox(&rt_object_container[RT_Object_Class_MailBox].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system);
|
||||
MSH_CMD_EXPORT(list_mailbox, list mail box in system);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MESSAGEQUEUE
|
||||
static long _list_msgqueue(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_messagequeue *m;
|
||||
struct rt_list_node *node;
|
||||
|
||||
rt_kprintf("msgqueue entry suspend thread\n");
|
||||
rt_kprintf("-------- ---- --------------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
|
||||
if (!rt_list_isempty(&m->parent.suspend_thread))
|
||||
{
|
||||
rt_kprintf("%-8.*s %04d %d:",
|
||||
RT_NAME_MAX,
|
||||
m->parent.parent.name,
|
||||
m->entry,
|
||||
rt_list_len(&m->parent.suspend_thread));
|
||||
show_wait_queue(&(m->parent.suspend_thread));
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%-8.*s %04d %d\n",
|
||||
RT_NAME_MAX,
|
||||
m->parent.parent.name,
|
||||
m->entry,
|
||||
rt_list_len(&m->parent.suspend_thread));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_msgqueue(void)
|
||||
{
|
||||
return _list_msgqueue(&rt_object_container[RT_Object_Class_MessageQueue].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system);
|
||||
MSH_CMD_EXPORT(list_msgqueue, list message queue in system);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MEMHEAP
|
||||
static long _list_memheap(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_memheap *mh;
|
||||
struct rt_list_node *node;
|
||||
|
||||
rt_kprintf("memheap pool size max used size available size\n");
|
||||
rt_kprintf("-------- ---------- ------------- --------------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);
|
||||
|
||||
rt_kprintf("%-8.*s %-010d %-013d %-05d\n",
|
||||
RT_NAME_MAX,
|
||||
mh->parent.name,
|
||||
mh->pool_size,
|
||||
mh->max_used_size,
|
||||
mh->available_size);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_memheap(void)
|
||||
{
|
||||
return _list_memheap(&rt_object_container[RT_Object_Class_MemHeap].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system);
|
||||
MSH_CMD_EXPORT(list_memheap, list memory heap in system);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
static long _list_mempool(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_mempool *mp;
|
||||
struct rt_list_node *node;
|
||||
|
||||
rt_kprintf("mempool block total free suspend thread\n");
|
||||
rt_kprintf("-------- ---- ---- ---- --------------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list);
|
||||
if (mp->suspend_thread_count > 0)
|
||||
{
|
||||
rt_kprintf("%-8.*s %04d %04d %04d %d:",
|
||||
RT_NAME_MAX,
|
||||
mp->parent.name,
|
||||
mp->block_size,
|
||||
mp->block_total_count,
|
||||
mp->block_free_count,
|
||||
mp->suspend_thread_count);
|
||||
show_wait_queue(&(mp->suspend_thread));
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%-8.*s %04d %04d %04d %d\n",
|
||||
RT_NAME_MAX,
|
||||
mp->parent.name,
|
||||
mp->block_size,
|
||||
mp->block_total_count,
|
||||
mp->block_free_count,
|
||||
mp->suspend_thread_count);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_mempool(void)
|
||||
{
|
||||
return _list_mempool(&rt_object_container[RT_Object_Class_MemPool].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
|
||||
MSH_CMD_EXPORT(list_mempool, list memory pool in system);
|
||||
#endif
|
||||
|
||||
static long _list_timer(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_timer *timer;
|
||||
struct rt_list_node *node;
|
||||
|
||||
rt_kprintf("timer periodic timeout flag\n");
|
||||
rt_kprintf("-------- ---------- ---------- -----------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list));
|
||||
rt_kprintf("%-8.*s 0x%08x 0x%08x ",
|
||||
RT_NAME_MAX,
|
||||
timer->parent.name,
|
||||
timer->init_tick,
|
||||
timer->timeout_tick);
|
||||
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
|
||||
rt_kprintf("activated\n");
|
||||
else
|
||||
rt_kprintf("deactivated\n");
|
||||
}
|
||||
|
||||
rt_kprintf("current tick:0x%08x\n", rt_tick_get());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_timer(void)
|
||||
{
|
||||
return _list_timer(&rt_object_container[RT_Object_Class_Timer].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_timer, list timer in system);
|
||||
MSH_CMD_EXPORT(list_timer, list timer in system);
|
||||
|
||||
#ifdef RT_USING_DEVICE
|
||||
static long _list_device(struct rt_list_node *list)
|
||||
{
|
||||
struct rt_device *device;
|
||||
struct rt_list_node *node;
|
||||
char * const device_type_str[] =
|
||||
{
|
||||
"Character Device",
|
||||
"Block Device",
|
||||
"Network Interface",
|
||||
"MTD Device",
|
||||
"CAN Device",
|
||||
"RTC",
|
||||
"Sound Device",
|
||||
"Graphic Device",
|
||||
"I2C Bus",
|
||||
"USB Slave Device",
|
||||
"USB Host Bus",
|
||||
"SPI Bus",
|
||||
"SPI Device",
|
||||
"SDIO Bus",
|
||||
"PM Pseudo Device",
|
||||
"Pipe",
|
||||
"Portal Device",
|
||||
"Miscellaneous Device",
|
||||
"Unknown"
|
||||
};
|
||||
|
||||
rt_kprintf("device type \n");
|
||||
rt_kprintf("-------- ---------- \n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
|
||||
rt_kprintf("%-8.*s %-8s \n",
|
||||
RT_NAME_MAX,
|
||||
device->parent.name,
|
||||
(device->type <= RT_Device_Class_Unknown) ?
|
||||
device_type_str[device->type] :
|
||||
device_type_str[RT_Device_Class_Unknown]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long list_device(void)
|
||||
{
|
||||
return _list_device(&rt_object_container[RT_Object_Class_Device].object_list);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_device, list device in system);
|
||||
MSH_CMD_EXPORT(list_device, list device in system);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
#include <rtm.h>
|
||||
|
||||
int list_module(void)
|
||||
{
|
||||
struct rt_module *module;
|
||||
struct rt_list_node *list, *node;
|
||||
|
||||
list = &rt_object_container[RT_Object_Class_Module].object_list;
|
||||
|
||||
rt_kprintf("module name ref\n");
|
||||
rt_kprintf("------------ --------\n");
|
||||
for (node = list->next; node != list; node = node->next)
|
||||
{
|
||||
module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
|
||||
rt_kprintf("%-16.*s %-04d\n",
|
||||
RT_NAME_MAX, module->parent.name, module->nref);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_module, list module in system);
|
||||
MSH_CMD_EXPORT(list_module, list module in system);
|
||||
|
||||
int list_mod_detail(const char *name)
|
||||
{
|
||||
int i;
|
||||
struct rt_module *module;
|
||||
|
||||
/* find module */
|
||||
if ((module = rt_module_find(name)) != RT_NULL)
|
||||
{
|
||||
/* module has entry point */
|
||||
if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
struct rt_list_node *tlist;
|
||||
rt_uint8_t *ptr;
|
||||
|
||||
/* list main thread in module */
|
||||
if (module->module_thread != RT_NULL)
|
||||
{
|
||||
rt_kprintf("main thread pri status sp stack size max used left tick error\n");
|
||||
rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
|
||||
thread = module->module_thread;
|
||||
rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority);
|
||||
|
||||
if (thread->stat == RT_THREAD_READY) rt_kprintf(" ready ");
|
||||
else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
|
||||
else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init ");
|
||||
|
||||
ptr = (rt_uint8_t*)thread->stack_addr;
|
||||
while (*ptr == '#')ptr ++;
|
||||
|
||||
rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
|
||||
thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
|
||||
thread->stack_size,
|
||||
thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
|
||||
thread->remaining_tick,
|
||||
thread->error);
|
||||
}
|
||||
|
||||
/* list sub thread in module */
|
||||
tlist = &module->module_object[RT_Object_Class_Thread].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_thread(tlist);
|
||||
#ifdef RT_USING_SEMAPHORE
|
||||
/* list semaphored in module */
|
||||
tlist = &module->module_object[RT_Object_Class_Semaphore].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_sem(tlist);
|
||||
#endif
|
||||
#ifdef RT_USING_MUTEX
|
||||
/* list mutex in module */
|
||||
tlist = &module->module_object[RT_Object_Class_Mutex].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_mutex(tlist);
|
||||
#endif
|
||||
#ifdef RT_USING_EVENT
|
||||
/* list event in module */
|
||||
tlist = &module->module_object[RT_Object_Class_Event].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_event(tlist);
|
||||
#endif
|
||||
#ifdef RT_USING_MAILBOX
|
||||
/* list mailbox in module */
|
||||
tlist = &module->module_object[RT_Object_Class_MailBox].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_mailbox(tlist);
|
||||
#endif
|
||||
#ifdef RT_USING_MESSAGEQUEUE
|
||||
/* list message queue in module */
|
||||
tlist = &module->module_object[RT_Object_Class_MessageQueue].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_msgqueue(tlist);
|
||||
#endif
|
||||
#ifdef RT_USING_MEMHEAP
|
||||
/* list memory heap in module */
|
||||
tlist = &module->module_object[RT_Object_Class_MemHeap].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_memheap(tlist);
|
||||
#endif
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
/* list memory pool in module */
|
||||
tlist = &module->module_object[RT_Object_Class_MemPool].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_mempool(tlist);
|
||||
#endif
|
||||
#ifdef RT_USING_DEVICE
|
||||
/* list device in module */
|
||||
tlist = &module->module_object[RT_Object_Class_Device].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_device(tlist);
|
||||
#endif
|
||||
/* list timer in module */
|
||||
tlist = &module->module_object[RT_Object_Class_Timer].object_list;
|
||||
if (!rt_list_isempty(tlist)) _list_timer(tlist);
|
||||
}
|
||||
|
||||
if (module->nsym > 0)
|
||||
{
|
||||
rt_kprintf("symbol address \n");
|
||||
rt_kprintf("-------- ----------\n");
|
||||
|
||||
/* list module export symbols */
|
||||
for (i=0; i<module->nsym; i++)
|
||||
{
|
||||
rt_kprintf("%s 0x%x\n",
|
||||
module->symtab[i].name, module->symtab[i].addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_mod_detail, list module objects in system)
|
||||
MSH_CMD_EXPORT(list_mod_detail, list module objects in system)
|
||||
#endif
|
||||
|
||||
long list(void)
|
||||
{
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
struct finsh_syscall_item *syscall_item;
|
||||
struct finsh_sysvar_item *sysvar_item;
|
||||
#endif
|
||||
|
||||
rt_kprintf("--Function List:\n");
|
||||
{
|
||||
struct finsh_syscall *index;
|
||||
for (index = _syscall_table_begin;
|
||||
index < _syscall_table_end;
|
||||
FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
/* skip the internal command */
|
||||
if (strncmp((char*)index->name, "__", 2) == 0) continue;
|
||||
|
||||
#ifdef FINSH_USING_DESCRIPTION
|
||||
rt_kprintf("%-16s -- %s\n", index->name, index->desc);
|
||||
#else
|
||||
rt_kprintf("%s\n", index->name);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
/* list syscall list */
|
||||
syscall_item = global_syscall_list;
|
||||
while (syscall_item != NULL)
|
||||
{
|
||||
rt_kprintf("[l] %s\n", syscall_item->syscall.name);
|
||||
syscall_item = syscall_item->next;
|
||||
}
|
||||
|
||||
rt_kprintf("--Variable List:\n");
|
||||
{
|
||||
struct finsh_sysvar *index;
|
||||
for (index = _sysvar_table_begin;
|
||||
index < _sysvar_table_end;
|
||||
FINSH_NEXT_SYSVAR(index))
|
||||
{
|
||||
#ifdef FINSH_USING_DESCRIPTION
|
||||
rt_kprintf("%-16s -- %s\n", index->name, index->desc);
|
||||
#else
|
||||
rt_kprintf("%s\n", index->name);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
sysvar_item = global_sysvar_list;
|
||||
while (sysvar_item != NULL)
|
||||
{
|
||||
rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
|
||||
sysvar_item = sysvar_item->next;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list, list all symbol in system)
|
||||
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
static int str_is_prefix(const char *prefix, const char *str)
|
||||
{
|
||||
while ((*prefix) && (*prefix == *str))
|
||||
{
|
||||
prefix ++;
|
||||
str ++;
|
||||
}
|
||||
|
||||
if (*prefix == 0)
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int str_common(const char *str1, const char *str2)
|
||||
{
|
||||
const char *str = str1;
|
||||
|
||||
while ((*str != 0) && (*str2 != 0) && (*str == *str2))
|
||||
{
|
||||
str ++;
|
||||
str2 ++;
|
||||
}
|
||||
|
||||
return (str - str1);
|
||||
}
|
||||
|
||||
void list_prefix(char *prefix)
|
||||
{
|
||||
struct finsh_syscall_item *syscall_item;
|
||||
struct finsh_sysvar_item *sysvar_item;
|
||||
rt_uint16_t func_cnt, var_cnt;
|
||||
int length, min_length;
|
||||
const char *name_ptr;
|
||||
|
||||
func_cnt = 0;
|
||||
var_cnt = 0;
|
||||
min_length = 0;
|
||||
name_ptr = RT_NULL;
|
||||
|
||||
/* checks in system function call */
|
||||
{
|
||||
struct finsh_syscall *index;
|
||||
for (index = _syscall_table_begin;
|
||||
index < _syscall_table_end;
|
||||
FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
/* skip internal command */
|
||||
if (str_is_prefix("__", index->name) == 0) continue;
|
||||
|
||||
if (str_is_prefix(prefix, index->name) == 0)
|
||||
{
|
||||
if (func_cnt == 0)
|
||||
{
|
||||
rt_kprintf("--function:\n");
|
||||
|
||||
if (*prefix != 0)
|
||||
{
|
||||
/* set name_ptr */
|
||||
name_ptr = index->name;
|
||||
|
||||
/* set initial length */
|
||||
min_length = strlen(name_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
func_cnt ++;
|
||||
|
||||
if (*prefix != 0)
|
||||
{
|
||||
length = str_common(name_ptr, index->name);
|
||||
if (length < min_length)
|
||||
min_length = length;
|
||||
}
|
||||
|
||||
#ifdef FINSH_USING_DESCRIPTION
|
||||
rt_kprintf("%-16s -- %s\n", index->name, index->desc);
|
||||
#else
|
||||
rt_kprintf("%s\n", index->name);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* checks in dynamic system function call */
|
||||
syscall_item = global_syscall_list;
|
||||
while (syscall_item != NULL)
|
||||
{
|
||||
if (str_is_prefix(prefix, syscall_item->syscall.name) == 0)
|
||||
{
|
||||
if (func_cnt == 0)
|
||||
{
|
||||
rt_kprintf("--function:\n");
|
||||
if (*prefix != 0 && name_ptr == NULL)
|
||||
{
|
||||
/* set name_ptr */
|
||||
name_ptr = syscall_item->syscall.name;
|
||||
|
||||
/* set initial length */
|
||||
min_length = strlen(name_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
func_cnt ++;
|
||||
|
||||
if (*prefix != 0)
|
||||
{
|
||||
length = str_common(name_ptr, syscall_item->syscall.name);
|
||||
if (length < min_length)
|
||||
min_length = length;
|
||||
}
|
||||
|
||||
rt_kprintf("[l] %s\n", syscall_item->syscall.name);
|
||||
}
|
||||
syscall_item = syscall_item->next;
|
||||
}
|
||||
|
||||
/* checks in system variable */
|
||||
{
|
||||
struct finsh_sysvar* index;
|
||||
for (index = _sysvar_table_begin;
|
||||
index < _sysvar_table_end;
|
||||
FINSH_NEXT_SYSVAR(index))
|
||||
{
|
||||
if (str_is_prefix(prefix, index->name) == 0)
|
||||
{
|
||||
if (var_cnt == 0)
|
||||
{
|
||||
rt_kprintf("--variable:\n");
|
||||
|
||||
if (*prefix != 0 && name_ptr == NULL)
|
||||
{
|
||||
/* set name_ptr */
|
||||
name_ptr = index->name;
|
||||
|
||||
/* set initial length */
|
||||
min_length = strlen(name_ptr);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
var_cnt ++;
|
||||
|
||||
if (*prefix != 0)
|
||||
{
|
||||
length = str_common(name_ptr, index->name);
|
||||
if (length < min_length)
|
||||
min_length = length;
|
||||
}
|
||||
|
||||
#ifdef FINSH_USING_DESCRIPTION
|
||||
rt_kprintf("%-16s -- %s\n", index->name, index->desc);
|
||||
#else
|
||||
rt_kprintf("%s\n", index->name);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* checks in dynamic system variable */
|
||||
sysvar_item = global_sysvar_list;
|
||||
while (sysvar_item != NULL)
|
||||
{
|
||||
if (str_is_prefix(prefix, sysvar_item->sysvar.name) == 0)
|
||||
{
|
||||
if (var_cnt == 0)
|
||||
{
|
||||
rt_kprintf("--variable:\n");
|
||||
if (*prefix != 0 && name_ptr == NULL)
|
||||
{
|
||||
/* set name_ptr */
|
||||
name_ptr = sysvar_item->sysvar.name;
|
||||
|
||||
/* set initial length */
|
||||
min_length = strlen(name_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
var_cnt ++;
|
||||
|
||||
if (*prefix != 0)
|
||||
{
|
||||
length = str_common(name_ptr, sysvar_item->sysvar.name);
|
||||
if (length < min_length)
|
||||
min_length = length;
|
||||
}
|
||||
|
||||
rt_kprintf("[v] %s\n", sysvar_item->sysvar.name);
|
||||
}
|
||||
sysvar_item = sysvar_item->next;
|
||||
}
|
||||
|
||||
/* only one matched */
|
||||
if (name_ptr != NULL)
|
||||
{
|
||||
rt_strncpy(prefix, name_ptr, min_length);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(FINSH_USING_SYMTAB) && !defined(FINSH_USING_MSH_ONLY)
|
||||
static int dummy = 0;
|
||||
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
|
||||
#endif
|
||||
@@ -0,0 +1,479 @@
|
||||
/*
|
||||
* File : finsh.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_H__
|
||||
#define __FINSH_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma section("FSymTab$f",read)
|
||||
#pragma section("VSymTab",read)
|
||||
#endif
|
||||
|
||||
/* -- the beginning of option -- */
|
||||
#define FINSH_NAME_MAX 16 /* max length of identifier */
|
||||
#define FINSH_NODE_MAX 16 /* max number of node */
|
||||
|
||||
#define FINSH_HEAP_MAX 128 /* max length of heap */
|
||||
#define FINSH_STRING_MAX 128 /* max length of string */
|
||||
#define FINSH_VARIABLE_MAX 8 /* max number of variable */
|
||||
|
||||
#define FINSH_STACK_MAX 64 /* max stack size */
|
||||
#define FINSH_TEXT_MAX 128 /* max text segment size */
|
||||
|
||||
#define HEAP_ALIGNMENT 4 /* heap alignment */
|
||||
|
||||
#define FINSH_GET16(x) (*(x)) | (*((x)+1) << 8)
|
||||
#define FINSH_GET32(x) (rt_uint32_t)(*(x)) | ((rt_uint32_t)*((x)+1) << 8) | \
|
||||
((rt_uint32_t)*((x)+2) << 16) | ((rt_uint32_t)*((x)+3) << 24)
|
||||
|
||||
#define FINSH_SET16(x, v) \
|
||||
do \
|
||||
{ \
|
||||
*(x) = (v) & 0x00ff; \
|
||||
(*((x)+1)) = (v) >> 8; \
|
||||
} while ( 0 )
|
||||
|
||||
#define FINSH_SET32(x, v) \
|
||||
do \
|
||||
{ \
|
||||
*(x) = (rt_uint32_t)(v) & 0x000000ff; \
|
||||
(*((x)+1)) = ((rt_uint32_t)(v) >> 8) & 0x000000ff; \
|
||||
(*((x)+2)) = ((rt_uint32_t)(v) >> 16) & 0x000000ff; \
|
||||
(*((x)+3)) = ((rt_uint32_t)(v) >> 24); \
|
||||
} while ( 0 )
|
||||
|
||||
/* -- the end of option -- */
|
||||
|
||||
#if defined(RT_USING_NEWLIB) || defined (RT_USING_MINILIBC)
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#else
|
||||
typedef unsigned char u_char;
|
||||
typedef unsigned short u_short;
|
||||
typedef unsigned long u_long;
|
||||
|
||||
#if !defined(__CC_ARM) && \
|
||||
!defined(__IAR_SYSTEMS_ICC__) && \
|
||||
!defined(__ADSPBLACKFIN__) && \
|
||||
!defined(_MSC_VER)
|
||||
|
||||
/* only for GNU GCC */
|
||||
|
||||
#if !(defined(__GNUC__) && defined(__x86_64__))
|
||||
typedef unsigned int size_t;
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL RT_NULL
|
||||
#endif
|
||||
|
||||
#else
|
||||
/* use libc of armcc */
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define FINSH_VERSION_MAJOR 1
|
||||
#define FINSH_VERSION_MINOR 0
|
||||
|
||||
/**
|
||||
* @addtogroup finsh
|
||||
*/
|
||||
/*@{*/
|
||||
#define FINSH_ERROR_OK 0 /**< No error */
|
||||
#define FINSH_ERROR_INVALID_TOKEN 1 /**< Invalid token */
|
||||
#define FINSH_ERROR_EXPECT_TYPE 2 /**< Expect a type */
|
||||
#define FINSH_ERROR_UNKNOWN_TYPE 3 /**< Unknown type */
|
||||
#define FINSH_ERROR_VARIABLE_EXIST 4 /**< Variable exist */
|
||||
#define FINSH_ERROR_EXPECT_OPERATOR 5 /**< Expect a operator */
|
||||
#define FINSH_ERROR_MEMORY_FULL 6 /**< Memory full */
|
||||
#define FINSH_ERROR_UNKNOWN_OP 7 /**< Unknown operator */
|
||||
#define FINSH_ERROR_UNKNOWN_NODE 8 /**< Unknown node */
|
||||
#define FINSH_ERROR_EXPECT_CHAR 9 /**< Expect a character */
|
||||
#define FINSH_ERROR_UNEXPECT_END 10 /**< Unexpect end */
|
||||
#define FINSH_ERROR_UNKNOWN_TOKEN 11 /**< Unknown token */
|
||||
#define FINSH_ERROR_NO_FLOAT 12 /**< Float not supported */
|
||||
#define FINSH_ERROR_UNKNOWN_SYMBOL 13 /**< Unknown symbol */
|
||||
#define FINSH_ERROR_NULL_NODE 14 /**< Null node */
|
||||
/*@}*/
|
||||
|
||||
typedef long (*syscall_func)();
|
||||
|
||||
/* system call table */
|
||||
struct finsh_syscall
|
||||
{
|
||||
const char* name; /* the name of system call */
|
||||
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
|
||||
const char* desc; /* description of system call */
|
||||
#endif
|
||||
syscall_func func; /* the function address of system call */
|
||||
};
|
||||
|
||||
/* system call item */
|
||||
struct finsh_syscall_item
|
||||
{
|
||||
struct finsh_syscall_item* next; /* next item */
|
||||
struct finsh_syscall syscall; /* syscall */
|
||||
};
|
||||
extern struct finsh_syscall *_syscall_table_begin, *_syscall_table_end;
|
||||
extern struct finsh_syscall_item *global_syscall_list;
|
||||
|
||||
/* find out system call, which should be implemented in user program */
|
||||
struct finsh_syscall* finsh_syscall_lookup(const char* name);
|
||||
|
||||
/* system variable table */
|
||||
struct finsh_sysvar
|
||||
{
|
||||
const char* name; /* the name of variable */
|
||||
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
|
||||
const char* desc; /* description of system variable */
|
||||
#endif
|
||||
u_char type; /* the type of variable */
|
||||
void* var ; /* the address of variable */
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
|
||||
struct finsh_syscall* finsh_syscall_next(struct finsh_syscall* call);
|
||||
struct finsh_sysvar* finsh_sysvar_next(struct finsh_sysvar* call);
|
||||
#define FINSH_NEXT_SYSCALL(index) index=finsh_syscall_next(index)
|
||||
#define FINSH_NEXT_SYSVAR(index) index=finsh_sysvar_next(index)
|
||||
#else
|
||||
#define FINSH_NEXT_SYSCALL(index) index++
|
||||
#define FINSH_NEXT_SYSVAR(index) index++
|
||||
#endif
|
||||
|
||||
/* system variable item */
|
||||
struct finsh_sysvar_item
|
||||
{
|
||||
struct finsh_sysvar_item *next; /* next item */
|
||||
struct finsh_sysvar sysvar; /* system variable */
|
||||
};
|
||||
extern struct finsh_sysvar *_sysvar_table_begin, *_sysvar_table_end;
|
||||
extern struct finsh_sysvar_item* global_sysvar_list;
|
||||
|
||||
/* find out system variable, which should be implemented in user program */
|
||||
struct finsh_sysvar* finsh_sysvar_lookup(const char* name);
|
||||
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
|
||||
#ifdef __TI_COMPILER_VERSION__
|
||||
#define __TI_FINSH_EXPORT_FUNCTION(f) PRAGMA(DATA_SECTION(f,"FSymTab"))
|
||||
#define __TI_FINSH_EXPORT_VAR(v) PRAGMA(DATA_SECTION(v,"VSymTab"))
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_DESCRIPTION
|
||||
#ifdef _MSC_VER
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const char __fsym_##cmd##_desc[] = #desc; \
|
||||
__declspec(allocate("FSymTab$f")) \
|
||||
const struct finsh_syscall __fsym_##cmd = \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
__fsym_##cmd##_desc, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
#pragma comment(linker, "/merge:FSymTab=mytext")
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const char __vsym_##name##_desc[] = #desc; \
|
||||
__declspec(allocate("VSymTab")) \
|
||||
const struct finsh_sysvar __vsym_##name = \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
__vsym_##name##_desc, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#elif defined(__TI_COMPILER_VERSION__)
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
__TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const char __fsym_##cmd##_desc[] = #desc; \
|
||||
const struct finsh_syscall __fsym_##cmd = \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
__fsym_##cmd##_desc, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
__TI_FINSH_EXPORT_VAR(__vsym_##name); \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const char __vsym_##name##_desc[] = #desc; \
|
||||
const struct finsh_sysvar __vsym_##name = \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
__vsym_##name##_desc, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#else
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const char __fsym_##cmd##_desc[] = #desc; \
|
||||
const struct finsh_syscall __fsym_##cmd SECTION("FSymTab")= \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
__fsym_##cmd##_desc, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const char __vsym_##name##_desc[] = #desc; \
|
||||
const struct finsh_sysvar __vsym_##name SECTION("VSymTab")= \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
__vsym_##name##_desc, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#endif
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
__declspec(allocate("FSymTab$f")) \
|
||||
const struct finsh_syscall __fsym_##cmd = \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
#pragma comment(linker, "/merge:FSymTab=mytext")
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
__declspec(allocate("VSymTab")) const struct finsh_sysvar __vsym_##name = \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#elif defined(__TI_COMPILER_VERSION__)
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
__TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const struct finsh_syscall __fsym_##cmd = \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
__TI_FINSH_EXPORT_VAR(__vsym_##name); \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const struct finsh_sysvar __vsym_##name = \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#else
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const struct finsh_syscall __fsym_##cmd SECTION("FSymTab")= \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const struct finsh_sysvar __vsym_##name SECTION("VSymTab")= \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif /* end of FINSH_USING_DESCRIPTION */
|
||||
#endif /* end of FINSH_USING_SYMTAB */
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This macro exports a system function to finsh shell.
|
||||
*
|
||||
* @param name the name of function.
|
||||
* @param desc the description of function, which will show in help.
|
||||
*/
|
||||
#define FINSH_FUNCTION_EXPORT(name, desc) \
|
||||
FINSH_FUNCTION_EXPORT_CMD(name, name, desc)
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This macro exports a system function with an alias name to finsh shell.
|
||||
*
|
||||
* @param name the name of function.
|
||||
* @param alias the alias name of function.
|
||||
* @param desc the description of function, which will show in help.
|
||||
*/
|
||||
#define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc) \
|
||||
FINSH_FUNCTION_EXPORT_CMD(name, alias, desc)
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This macro exports a command to module shell.
|
||||
*
|
||||
* @param command the name of command.
|
||||
* @param desc the description of command, which will show in help.
|
||||
*/
|
||||
#ifdef FINSH_USING_MSH
|
||||
#define MSH_CMD_EXPORT(command, desc) \
|
||||
FINSH_FUNCTION_EXPORT_CMD(command, __cmd_##command, desc)
|
||||
#else
|
||||
#define MSH_CMD_EXPORT(command, desc)
|
||||
#endif
|
||||
|
||||
struct finsh_token
|
||||
{
|
||||
char eof;
|
||||
char replay;
|
||||
|
||||
int position;
|
||||
u_char current_token;
|
||||
|
||||
union {
|
||||
char char_value;
|
||||
int int_value;
|
||||
long long_value;
|
||||
} value;
|
||||
u_char string[128];
|
||||
|
||||
u_char* line;
|
||||
};
|
||||
|
||||
#define FINSH_IDTYPE_VAR 0x01
|
||||
#define FINSH_IDTYPE_SYSVAR 0x02
|
||||
#define FINSH_IDTYPE_SYSCALL 0x04
|
||||
#define FINSH_IDTYPE_ADDRESS 0x08
|
||||
struct finsh_node
|
||||
{
|
||||
u_char node_type; /* node node_type */
|
||||
u_char data_type; /* node data node_type */
|
||||
u_char idtype; /* id node information */
|
||||
|
||||
union { /* value node */
|
||||
char char_value;
|
||||
short short_value;
|
||||
int int_value;
|
||||
long long_value;
|
||||
void* ptr;
|
||||
} value;
|
||||
union
|
||||
{
|
||||
/* point to variable identifier or function identifier */
|
||||
struct finsh_var *var;
|
||||
struct finsh_sysvar *sysvar;
|
||||
struct finsh_syscall*syscall;
|
||||
}id;
|
||||
|
||||
/* sibling and child node */
|
||||
struct finsh_node *sibling, *child;
|
||||
};
|
||||
|
||||
struct finsh_parser
|
||||
{
|
||||
u_char* parser_string;
|
||||
|
||||
struct finsh_token token;
|
||||
struct finsh_node* root;
|
||||
};
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* The basic data type in finsh shell
|
||||
*/
|
||||
enum finsh_type {
|
||||
finsh_type_unknown = 0, /**< unknown data type */
|
||||
finsh_type_void, /**< void */
|
||||
finsh_type_voidp, /**< void pointer */
|
||||
finsh_type_char, /**< char */
|
||||
finsh_type_uchar, /**< unsigned char */
|
||||
finsh_type_charp, /**< char pointer */
|
||||
finsh_type_short, /**< short */
|
||||
finsh_type_ushort, /**< unsigned short */
|
||||
finsh_type_shortp, /**< short pointer */
|
||||
finsh_type_int, /**< int */
|
||||
finsh_type_uint, /**< unsigned int */
|
||||
finsh_type_intp, /**< int pointer */
|
||||
finsh_type_long, /**< long */
|
||||
finsh_type_ulong, /**< unsigned long */
|
||||
finsh_type_longp /**< long pointer */
|
||||
};
|
||||
|
||||
/* init finsh environment */
|
||||
int finsh_init(struct finsh_parser* parser);
|
||||
/* flush finsh node, text segment */
|
||||
int finsh_flush(struct finsh_parser* parser);
|
||||
/* reset all of finsh */
|
||||
int finsh_reset(struct finsh_parser* parser);
|
||||
#ifdef RT_USING_DEVICE
|
||||
void finsh_set_device(const char* device_name);
|
||||
#endif
|
||||
|
||||
/* run finsh parser to generate abstract synatx tree */
|
||||
void finsh_parser_run (struct finsh_parser* parser, const unsigned char* string);
|
||||
/* run compiler to compile abstract syntax tree */
|
||||
int finsh_compiler_run(struct finsh_node* node);
|
||||
/* run finsh virtual machine */
|
||||
void finsh_vm_run(void);
|
||||
|
||||
/* get variable value */
|
||||
struct finsh_var* finsh_var_lookup(const char* name);
|
||||
/* get bottom value of stack */
|
||||
long finsh_stack_bottom(void);
|
||||
|
||||
/* get error number of finsh */
|
||||
u_char finsh_errno(void);
|
||||
/* get error string */
|
||||
const char* finsh_error_string(u_char type);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function appends a system call to finsh runtime environment
|
||||
* @param name the name of system call
|
||||
* @param func the function pointer of system call
|
||||
*/
|
||||
void finsh_syscall_append(const char* name, syscall_func func);
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function appends a system variable to finsh runtime environment
|
||||
* @param name the name of system variable
|
||||
* @param type the data type of system variable
|
||||
* @param addr the address of system variable
|
||||
*/
|
||||
void finsh_sysvar_append(const char* name, u_char type, void* addr);
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,937 @@
|
||||
/*
|
||||
* RT-Thread finsh shell compiler
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include <finsh.h>
|
||||
|
||||
#include "finsh_node.h"
|
||||
#include "finsh_error.h"
|
||||
#include "finsh_var.h"
|
||||
#include "finsh_ops.h"
|
||||
|
||||
union finsh_value* finsh_compile_sp; /* stack pointer */
|
||||
u_char* finsh_compile_pc; /* PC */
|
||||
|
||||
#define finsh_code_byte(x) do { *finsh_compile_pc = (x); finsh_compile_pc ++; } while(0)
|
||||
#define finsh_code_word(x) do { FINSH_SET16(finsh_compile_pc, x); finsh_compile_pc +=2; } while(0)
|
||||
#define finsh_code_dword(x) do { FINSH_SET32(finsh_compile_pc, x); finsh_compile_pc +=4; } while(0)
|
||||
|
||||
static int finsh_compile(struct finsh_node* node)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
/* compile child node */
|
||||
if (finsh_node_child(node) != NULL)
|
||||
finsh_compile(finsh_node_child(node));
|
||||
|
||||
/* compile current node */
|
||||
switch (node->node_type)
|
||||
{
|
||||
case FINSH_NODE_ID:
|
||||
{
|
||||
/* identifier::syscall */
|
||||
if (node->idtype & FINSH_IDTYPE_SYSCALL)
|
||||
{
|
||||
/* load address */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword((long)node->id.syscall->func);
|
||||
}
|
||||
/* identifier::sysvar */
|
||||
else if (node->idtype & FINSH_IDTYPE_SYSVAR)
|
||||
{
|
||||
struct finsh_sysvar* sysvar;
|
||||
|
||||
sysvar = node->id.sysvar;
|
||||
if (sysvar != NULL)
|
||||
{
|
||||
switch (sysvar->type)
|
||||
{
|
||||
case finsh_type_char:
|
||||
case finsh_type_uchar:
|
||||
if (node->idtype & FINSH_IDTYPE_ADDRESS)
|
||||
{
|
||||
/* load address */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* load value */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE);
|
||||
}
|
||||
|
||||
finsh_code_dword((long)(sysvar->var));
|
||||
break;
|
||||
|
||||
case finsh_type_short:
|
||||
case finsh_type_ushort:
|
||||
if (node->idtype & FINSH_IDTYPE_ADDRESS)
|
||||
{
|
||||
/* load address */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* load value */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD);
|
||||
}
|
||||
|
||||
finsh_code_dword((long)(sysvar->var));
|
||||
break;
|
||||
|
||||
case finsh_type_int:
|
||||
case finsh_type_uint:
|
||||
case finsh_type_long:
|
||||
case finsh_type_ulong:
|
||||
case finsh_type_charp:
|
||||
case finsh_type_shortp:
|
||||
case finsh_type_intp:
|
||||
case finsh_type_longp:
|
||||
if (node->idtype & FINSH_IDTYPE_ADDRESS)
|
||||
{
|
||||
/* load address */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* load value */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD);
|
||||
}
|
||||
|
||||
finsh_code_dword((long)(sysvar->var));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* identifier::var */
|
||||
else
|
||||
{
|
||||
struct finsh_var* var;
|
||||
|
||||
var = node->id.var;
|
||||
if (var != NULL)
|
||||
{
|
||||
switch (var->type)
|
||||
{
|
||||
case finsh_type_char:
|
||||
case finsh_type_uchar:
|
||||
if (node->idtype & FINSH_IDTYPE_ADDRESS)
|
||||
{
|
||||
/* load address */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* load value */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE);
|
||||
}
|
||||
|
||||
finsh_code_dword((long)&(var->value.char_value));
|
||||
break;
|
||||
|
||||
case finsh_type_short:
|
||||
case finsh_type_ushort:
|
||||
if (node->idtype & FINSH_IDTYPE_ADDRESS)
|
||||
{
|
||||
/* load address */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* load value */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD);
|
||||
}
|
||||
|
||||
finsh_code_dword((long)&(var->value.short_value));
|
||||
break;
|
||||
|
||||
case finsh_type_int:
|
||||
case finsh_type_uint:
|
||||
case finsh_type_long:
|
||||
case finsh_type_ulong:
|
||||
case finsh_type_charp:
|
||||
case finsh_type_shortp:
|
||||
case finsh_type_intp:
|
||||
case finsh_type_longp:
|
||||
if (node->idtype & FINSH_IDTYPE_ADDRESS)
|
||||
{
|
||||
/* load address */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* load value */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD);
|
||||
}
|
||||
|
||||
finsh_code_dword((long)&(var->value.long_value));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* load const */
|
||||
case FINSH_NODE_VALUE_CHAR:
|
||||
finsh_code_byte(FINSH_OP_LD_BYTE);
|
||||
finsh_code_byte(node->value.char_value);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_VALUE_INT:
|
||||
case FINSH_NODE_VALUE_LONG:
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword(node->value.long_value);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_VALUE_NULL:
|
||||
case FINSH_NODE_VALUE_STRING:
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword((u_long)node->value.ptr);
|
||||
break;
|
||||
|
||||
/* arithmetic operation */
|
||||
case FINSH_NODE_SYS_ADD:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_ADD_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_ADD_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_ADD_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_SUB:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SUB_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SUB_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SUB_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_MUL:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_MUL_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_MUL_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_MUL_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_DIV:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_DIV_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_DIV_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_DIV_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_MOD:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_MOD_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_MOD_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_MOD_DWORD);
|
||||
break;
|
||||
|
||||
/* bit operation */
|
||||
case FINSH_NODE_SYS_AND:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_AND_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_AND_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_AND_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_OR:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_OR_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_OR_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_OR_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_XOR:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_XOR_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_XOR_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_XOR_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_BITWISE:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_BITWISE_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_BITWISE_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_BITWISE_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_SHL:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SHL_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SHL_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SHL_DWORD);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_SHR:
|
||||
if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SHR_BYTE);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SHR_WORD);
|
||||
else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SHR_DWORD);
|
||||
break;
|
||||
|
||||
/* syscall */
|
||||
case FINSH_NODE_SYS_FUNC:
|
||||
{
|
||||
int parameters;
|
||||
struct finsh_node* sibling;
|
||||
|
||||
parameters = 0;
|
||||
if (finsh_node_child(node) != NULL)
|
||||
{
|
||||
sibling = finsh_node_sibling(finsh_node_child(node));
|
||||
while (sibling != NULL)
|
||||
{
|
||||
parameters ++;
|
||||
sibling = finsh_node_sibling(sibling);
|
||||
}
|
||||
|
||||
/* load address of function */
|
||||
// finsh_code_dword((long)&(node->var->value.ptr));
|
||||
|
||||
/* syscall parameters */
|
||||
finsh_code_byte(FINSH_OP_SYSCALL);
|
||||
finsh_code_byte(parameters);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* assign expression */
|
||||
case FINSH_NODE_SYS_ASSIGN:
|
||||
if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID)
|
||||
{
|
||||
switch (finsh_node_child(node)->data_type)
|
||||
{
|
||||
case FINSH_DATA_TYPE_BYTE:
|
||||
finsh_code_byte(FINSH_OP_ST_BYTE);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK);
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_WORD:
|
||||
finsh_code_byte(FINSH_OP_ST_WORD);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK);
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_DWORD:
|
||||
finsh_code_byte(FINSH_OP_ST_DWORD);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
break;
|
||||
|
||||
default:
|
||||
finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE);
|
||||
}
|
||||
}
|
||||
else if (finsh_node_child(node)->node_type == FINSH_NODE_SYS_GETVALUE)
|
||||
{
|
||||
switch ((finsh_node_child(node)->data_type) & 0x0F)
|
||||
{
|
||||
case FINSH_DATA_TYPE_BYTE:
|
||||
finsh_code_byte(FINSH_OP_ST_BYTE);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK);
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_WORD:
|
||||
finsh_code_byte(FINSH_OP_ST_WORD);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK);
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_DWORD:
|
||||
finsh_code_byte(FINSH_OP_ST_DWORD);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
break;
|
||||
|
||||
default:
|
||||
finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* pre-increase */
|
||||
case FINSH_NODE_SYS_PREINC:
|
||||
if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID)
|
||||
{
|
||||
struct finsh_var* var;
|
||||
var = finsh_node_child(node)->id.var;
|
||||
|
||||
/* ld_dword &id */
|
||||
// finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
|
||||
switch (node->data_type)
|
||||
{
|
||||
case FINSH_DATA_TYPE_BYTE:
|
||||
/* address */
|
||||
// finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_value_byte &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE);
|
||||
finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_byte 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_BYTE);
|
||||
finsh_code_byte(1);
|
||||
|
||||
/* add_byte */
|
||||
finsh_code_byte(FINSH_OP_ADD_BYTE);
|
||||
/* st_byte */
|
||||
finsh_code_byte(FINSH_OP_ST_BYTE);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_WORD:
|
||||
/* address */
|
||||
// finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_value_word &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD);
|
||||
finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_word 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_WORD);
|
||||
finsh_code_word(1);
|
||||
|
||||
/* add_word */
|
||||
finsh_code_byte(FINSH_OP_ADD_WORD);
|
||||
/* st_word */
|
||||
finsh_code_byte(FINSH_OP_ST_WORD);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_DWORD:
|
||||
/* address */
|
||||
// finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD);
|
||||
finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_dword 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword(1);
|
||||
|
||||
/* add_dword */
|
||||
finsh_code_byte(FINSH_OP_ADD_DWORD);
|
||||
/* st_dword */
|
||||
finsh_code_byte(FINSH_OP_ST_DWORD);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* pre-decrease */
|
||||
case FINSH_NODE_SYS_PREDEC:
|
||||
if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID)
|
||||
{
|
||||
struct finsh_var* var;
|
||||
var = finsh_node_child(node)->id.var;
|
||||
|
||||
/* ld_dword &id */
|
||||
// finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
|
||||
switch (node->data_type)
|
||||
{
|
||||
case FINSH_DATA_TYPE_BYTE:
|
||||
/* address */
|
||||
// finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_value_byte &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE);
|
||||
finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_byte 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_BYTE);
|
||||
finsh_code_byte(1);
|
||||
|
||||
/* add_byte */
|
||||
finsh_code_byte(FINSH_OP_SUB_BYTE);
|
||||
/* st_byte */
|
||||
finsh_code_byte(FINSH_OP_ST_BYTE);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_WORD:
|
||||
/* address */
|
||||
// finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_value_word &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD);
|
||||
finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_word 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_WORD);
|
||||
finsh_code_word(1);
|
||||
|
||||
/* add_word */
|
||||
finsh_code_byte(FINSH_OP_SUB_WORD);
|
||||
/* st_word */
|
||||
finsh_code_byte(FINSH_OP_ST_WORD);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_DWORD:
|
||||
/* address */
|
||||
// finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD);
|
||||
finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_dword 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword(1);
|
||||
|
||||
/* add_dword */
|
||||
finsh_code_byte(FINSH_OP_SUB_DWORD);
|
||||
/* st_dword */
|
||||
finsh_code_byte(FINSH_OP_ST_DWORD);
|
||||
|
||||
/* load value again */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* increase */
|
||||
case FINSH_NODE_SYS_INC:
|
||||
if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID)
|
||||
{
|
||||
struct finsh_var* var;
|
||||
var = finsh_node_child(node)->id.var;
|
||||
|
||||
switch (node->data_type)
|
||||
{
|
||||
case FINSH_DATA_TYPE_BYTE:
|
||||
/* ld_value_byte &id */
|
||||
// finsh_code_byte(FINSH_OP_LD_VALUE_BYTE);
|
||||
// finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_value_byte &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE);
|
||||
finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_byte 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_BYTE);
|
||||
finsh_code_byte(1);
|
||||
|
||||
/* add_byte */
|
||||
finsh_code_byte(FINSH_OP_ADD_BYTE);
|
||||
/* get byte */
|
||||
finsh_code_byte(FINSH_OP_ST_BYTE);
|
||||
|
||||
/* pop */
|
||||
finsh_code_byte(FINSH_OP_POP);
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_WORD:
|
||||
/* ld_value_word &id */
|
||||
// finsh_code_byte(FINSH_OP_LD_VALUE_WORD);
|
||||
// finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_value_word &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD);
|
||||
finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_word 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_WORD);
|
||||
finsh_code_word(1);
|
||||
|
||||
/* add_byte */
|
||||
finsh_code_byte(FINSH_OP_ADD_WORD);
|
||||
/* get byte */
|
||||
finsh_code_byte(FINSH_OP_ST_WORD);
|
||||
|
||||
/* pop */
|
||||
finsh_code_byte(FINSH_OP_POP);
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_DWORD:
|
||||
/* ld_value_dword &id */
|
||||
// finsh_code_byte(FINSH_OP_LD_VALUE_DWORD);
|
||||
// finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_value_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD);
|
||||
finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_dword 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword(1);
|
||||
|
||||
/* add_byte */
|
||||
finsh_code_byte(FINSH_OP_ADD_DWORD);
|
||||
/* get byte */
|
||||
finsh_code_byte(FINSH_OP_ST_DWORD);
|
||||
|
||||
/* pop */
|
||||
finsh_code_byte(FINSH_OP_POP);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* decrease */
|
||||
case FINSH_NODE_SYS_DEC:
|
||||
if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID)
|
||||
{
|
||||
struct finsh_var* var;
|
||||
var = finsh_node_child(node)->id.var;
|
||||
|
||||
switch (node->data_type)
|
||||
{
|
||||
case FINSH_DATA_TYPE_BYTE:
|
||||
/* ld_value_byte &id */
|
||||
// finsh_code_byte(FINSH_OP_LD_VALUE_BYTE);
|
||||
// finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_value_byte &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE);
|
||||
finsh_code_dword((long)&(var->value.char_value));
|
||||
|
||||
/* ld_byte 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_BYTE);
|
||||
finsh_code_byte(1);
|
||||
|
||||
/* add_byte */
|
||||
finsh_code_byte(FINSH_OP_SUB_BYTE);
|
||||
/* get byte */
|
||||
finsh_code_byte(FINSH_OP_ST_BYTE);
|
||||
|
||||
/* pop */
|
||||
finsh_code_byte(FINSH_OP_POP);
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_WORD:
|
||||
/* ld_value_word &id */
|
||||
// finsh_code_byte(FINSH_OP_LD_VALUE_WORD);
|
||||
// finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_value_word &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD);
|
||||
finsh_code_dword((long)&(var->value.short_value));
|
||||
|
||||
/* ld_word 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_WORD);
|
||||
finsh_code_word(1);
|
||||
|
||||
/* add_byte */
|
||||
finsh_code_byte(FINSH_OP_SUB_WORD);
|
||||
/* get byte */
|
||||
finsh_code_byte(FINSH_OP_ST_WORD);
|
||||
|
||||
/* pop */
|
||||
finsh_code_byte(FINSH_OP_POP);
|
||||
break;
|
||||
|
||||
case FINSH_DATA_TYPE_DWORD:
|
||||
/* ld_value_dword &id */
|
||||
// finsh_code_byte(FINSH_OP_LD_VALUE_DWORD);
|
||||
// finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_value_dword &id */
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD);
|
||||
finsh_code_dword((long)&(var->value.long_value));
|
||||
|
||||
/* ld_dword 1 */
|
||||
finsh_code_byte(FINSH_OP_LD_DWORD);
|
||||
finsh_code_dword(1);
|
||||
|
||||
/* add_byte */
|
||||
finsh_code_byte(FINSH_OP_SUB_DWORD);
|
||||
/* get byte */
|
||||
finsh_code_byte(FINSH_OP_ST_DWORD);
|
||||
|
||||
/* pop */
|
||||
finsh_code_byte(FINSH_OP_POP);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_NULL:
|
||||
finsh_code_dword(0);
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_GETVALUE:
|
||||
if (node->idtype & FINSH_IDTYPE_ADDRESS)
|
||||
{
|
||||
/* nothing will be generated */
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (node->data_type)
|
||||
{
|
||||
case FINSH_DATA_TYPE_BYTE:
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK);
|
||||
break;
|
||||
case FINSH_DATA_TYPE_WORD:
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK);
|
||||
break;
|
||||
case FINSH_DATA_TYPE_DWORD:
|
||||
finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case FINSH_NODE_SYS_GETADDR:
|
||||
/* nothing will be generated */
|
||||
break;
|
||||
|
||||
default:
|
||||
finsh_error_set(FINSH_ERROR_UNKNOWN_NODE);
|
||||
break;
|
||||
}
|
||||
|
||||
/* compile sibling node */
|
||||
if (finsh_node_sibling(node) != NULL)
|
||||
finsh_compile(finsh_node_sibling(node));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int finsh_type_check(struct finsh_node* node, u_char is_addr)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
/* address & value */
|
||||
if (node->node_type == FINSH_NODE_SYS_ASSIGN ||
|
||||
node->node_type == FINSH_NODE_SYS_PREINC ||
|
||||
node->node_type == FINSH_NODE_SYS_PREDEC ||
|
||||
node->node_type == FINSH_NODE_SYS_GETADDR)
|
||||
{
|
||||
/* address */
|
||||
finsh_type_check(finsh_node_child(node), FINSH_IDTYPE_ADDRESS);
|
||||
}
|
||||
else if (node->node_type == FINSH_NODE_SYS_GETVALUE && is_addr)
|
||||
{
|
||||
/* change the attribute of getvalue in left expr */
|
||||
finsh_type_check(finsh_node_child(node), 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* transfer 'av' to child node */
|
||||
finsh_type_check(finsh_node_child(node), is_addr);
|
||||
}
|
||||
|
||||
/* always does not load address in sibling */
|
||||
finsh_type_check(finsh_node_sibling(node), FINSH_NODE_VALUE);
|
||||
|
||||
/** set attribute of current node */
|
||||
|
||||
/* make sure the current node is address or value */
|
||||
if (node->idtype != FINSH_IDTYPE_SYSCALL) node->idtype |= is_addr;
|
||||
|
||||
if (finsh_node_child(node) != NULL)
|
||||
{
|
||||
node->data_type = finsh_node_child(node)->data_type;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (node->node_type == FINSH_NODE_ID)
|
||||
{
|
||||
if (node->idtype & FINSH_IDTYPE_VAR)
|
||||
{
|
||||
struct finsh_var* var;
|
||||
|
||||
var = node->id.var;
|
||||
if (var != NULL)
|
||||
{
|
||||
switch (var->type)
|
||||
{
|
||||
case finsh_type_void:
|
||||
node->data_type = FINSH_DATA_TYPE_VOID;
|
||||
break;
|
||||
|
||||
case finsh_type_char:
|
||||
case finsh_type_uchar:
|
||||
node->data_type = FINSH_DATA_TYPE_BYTE;
|
||||
break;
|
||||
|
||||
case finsh_type_short:
|
||||
case finsh_type_ushort:
|
||||
node->data_type = FINSH_DATA_TYPE_WORD;
|
||||
break;
|
||||
|
||||
case finsh_type_int:
|
||||
case finsh_type_uint:
|
||||
case finsh_type_long:
|
||||
case finsh_type_ulong:
|
||||
node->data_type = FINSH_DATA_TYPE_DWORD;
|
||||
break;
|
||||
|
||||
case finsh_type_charp:
|
||||
case finsh_type_voidp:
|
||||
case finsh_type_shortp:
|
||||
case finsh_type_intp:
|
||||
case finsh_type_longp:
|
||||
node->data_type = FINSH_DATA_TYPE_DWORD;
|
||||
break;
|
||||
|
||||
default:
|
||||
finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (node->idtype & FINSH_IDTYPE_SYSVAR)
|
||||
{
|
||||
struct finsh_sysvar *sysvar;
|
||||
|
||||
sysvar = node->id.sysvar;
|
||||
if (sysvar != NULL)
|
||||
{
|
||||
switch (sysvar->type)
|
||||
{
|
||||
case finsh_type_void:
|
||||
node->data_type = FINSH_DATA_TYPE_VOID;
|
||||
break;
|
||||
|
||||
case finsh_type_char:
|
||||
case finsh_type_uchar:
|
||||
node->data_type = FINSH_DATA_TYPE_BYTE;
|
||||
break;
|
||||
|
||||
case finsh_type_short:
|
||||
case finsh_type_ushort:
|
||||
node->data_type = FINSH_DATA_TYPE_WORD;
|
||||
break;
|
||||
|
||||
case finsh_type_int:
|
||||
case finsh_type_uint:
|
||||
case finsh_type_long:
|
||||
case finsh_type_ulong:
|
||||
node->data_type = FINSH_DATA_TYPE_DWORD;
|
||||
break;
|
||||
|
||||
case finsh_type_charp:
|
||||
case finsh_type_voidp:
|
||||
case finsh_type_shortp:
|
||||
case finsh_type_intp:
|
||||
case finsh_type_longp:
|
||||
node->data_type = FINSH_DATA_TYPE_DWORD;
|
||||
break;
|
||||
|
||||
default:
|
||||
finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (node->node_type == FINSH_NODE_VALUE_CHAR)
|
||||
{
|
||||
node->data_type = FINSH_DATA_TYPE_BYTE;
|
||||
}
|
||||
else if (node->node_type == FINSH_NODE_VALUE_INT ||
|
||||
node->node_type == FINSH_NODE_VALUE_LONG ||
|
||||
node->node_type == FINSH_NODE_VALUE_STRING ||
|
||||
node->node_type == FINSH_NODE_VALUE_NULL)
|
||||
{
|
||||
node->data_type = FINSH_DATA_TYPE_DWORD;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int finsh_compiler_run(struct finsh_node* node)
|
||||
{
|
||||
struct finsh_node* sibling;
|
||||
|
||||
/* type check */
|
||||
finsh_type_check(node, FINSH_NODE_VALUE);
|
||||
|
||||
/* clean text segment and vm stack */
|
||||
memset(&text_segment[0], 0, sizeof(text_segment));
|
||||
memset(&finsh_vm_stack[0], 0, sizeof(finsh_vm_stack[0]));
|
||||
|
||||
/* reset compile stack pointer and pc */
|
||||
finsh_compile_sp = &finsh_vm_stack[0];
|
||||
finsh_compile_pc = &text_segment[0];
|
||||
|
||||
/* compile node */
|
||||
sibling = node;
|
||||
while (sibling != NULL)
|
||||
{
|
||||
struct finsh_node* current_node;
|
||||
current_node = sibling;
|
||||
|
||||
/* get sibling node */
|
||||
sibling = current_node->sibling;
|
||||
|
||||
/* clean sibling node */
|
||||
current_node->sibling = NULL;
|
||||
finsh_compile(current_node);
|
||||
|
||||
/* pop current value */
|
||||
if (sibling != NULL) finsh_code_byte(FINSH_OP_POP);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* error number for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include "finsh_error.h"
|
||||
|
||||
u_char global_errno;
|
||||
|
||||
static const char * finsh_error_string_table[] =
|
||||
{
|
||||
"No error",
|
||||
"Invalid token",
|
||||
"Expect a type",
|
||||
"Unknown type",
|
||||
"Variable exist",
|
||||
"Expect a operater",
|
||||
"Memory full",
|
||||
"Unknown operator",
|
||||
"Unknown node",
|
||||
"Expect a character",
|
||||
"Unexpect end",
|
||||
"Unknown token",
|
||||
"Float not supported",
|
||||
"Unknown symbol",
|
||||
"Null node"
|
||||
};
|
||||
|
||||
int finsh_error_init()
|
||||
{
|
||||
global_errno = FINSH_ERROR_OK;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int finsh_error_set(u_char type)
|
||||
{
|
||||
global_errno = type;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u_char finsh_errno()
|
||||
{
|
||||
return global_errno;
|
||||
}
|
||||
|
||||
const char* finsh_error_string(u_char type)
|
||||
{
|
||||
return finsh_error_string_table[type];
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* error number for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_ERROR_H__
|
||||
#define __FINSH_ERROR_H__
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
int finsh_error_init(void);
|
||||
|
||||
/* get error number */
|
||||
u_char finsh_errno(void);
|
||||
|
||||
int finsh_error_set(u_char type);
|
||||
const char* finsh_error_string(u_char type);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
* heap management in finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include <finsh.h>
|
||||
|
||||
#include "finsh_var.h"
|
||||
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
u_char finsh_heap[FINSH_HEAP_MAX];
|
||||
struct finsh_block_header
|
||||
{
|
||||
u_long length;
|
||||
struct finsh_block_header* next;
|
||||
};
|
||||
#define BLOCK_HEADER(x) (struct finsh_block_header*)(x)
|
||||
#define finsh_block_get_header(data) (struct finsh_block_header*)((u_char*)data - sizeof(struct finsh_block_header))
|
||||
#define finsh_block_get_data(header) (u_char*)((struct finsh_block_header*)header + 1)
|
||||
#define HEAP_ALIGN_SIZE(size) (((size) + HEAP_ALIGNMENT - 1) & ~(HEAP_ALIGNMENT-1))
|
||||
|
||||
static struct finsh_block_header* free_list;
|
||||
static struct finsh_block_header* allocate_list;
|
||||
|
||||
static void finsh_heap_gc(void);
|
||||
|
||||
static void finsh_block_insert(struct finsh_block_header** list, struct finsh_block_header* header);
|
||||
static void finsh_block_remove(struct finsh_block_header** list, struct finsh_block_header* header);
|
||||
static void finsh_block_split(struct finsh_block_header* header, size_t size);
|
||||
static void finsh_block_merge(struct finsh_block_header** list, struct finsh_block_header* header);
|
||||
|
||||
int finsh_heap_init(void)
|
||||
{
|
||||
/* clear heap to zero */
|
||||
memset(&finsh_heap[0], 0, sizeof(finsh_heap));
|
||||
|
||||
/* init free and alloc list */
|
||||
free_list = BLOCK_HEADER(&finsh_heap[0]);
|
||||
free_list->length = FINSH_HEAP_MAX - sizeof(struct finsh_block_header);
|
||||
free_list->next = NULL;
|
||||
|
||||
allocate_list = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* allocate a block from heap
|
||||
*/
|
||||
void* finsh_heap_allocate(size_t size)
|
||||
{
|
||||
struct finsh_block_header* header;
|
||||
|
||||
size = HEAP_ALIGN_SIZE(size);
|
||||
|
||||
/* find the first fit block */
|
||||
for (header = free_list;
|
||||
((header != NULL) && (header->length <= size + sizeof(struct finsh_block_header)));
|
||||
header = header->next) ;
|
||||
|
||||
if (header == NULL)
|
||||
{
|
||||
finsh_heap_gc();
|
||||
|
||||
/* find the first fit block */
|
||||
for (header = free_list;
|
||||
((header != NULL) && (header->length < size + sizeof(struct finsh_block_header)));
|
||||
header = header->next) ;
|
||||
|
||||
/* there is no memory */
|
||||
if (header == NULL) return NULL;
|
||||
}
|
||||
|
||||
/* split block */
|
||||
finsh_block_split(header, size);
|
||||
|
||||
/* remove from free list */
|
||||
finsh_block_remove(&free_list, header);
|
||||
header->next = NULL;
|
||||
|
||||
/* insert to allocate list */
|
||||
finsh_block_insert(&allocate_list, header);
|
||||
|
||||
memset(finsh_block_get_data(header), 0, size);
|
||||
|
||||
return finsh_block_get_data(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* release the allocated block
|
||||
*/
|
||||
void finsh_heap_free(void*ptr)
|
||||
{
|
||||
struct finsh_block_header* header;
|
||||
|
||||
/* get block header */
|
||||
header = finsh_block_get_header(ptr);
|
||||
|
||||
/* remove from allocate list */
|
||||
finsh_block_remove(&allocate_list, header);
|
||||
|
||||
/* insert to free list */
|
||||
finsh_block_insert(&free_list, header);
|
||||
finsh_block_merge(&free_list, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* garbage collector
|
||||
*/
|
||||
static void finsh_heap_gc(void)
|
||||
{
|
||||
int i;
|
||||
struct finsh_block_header *header, *temp;
|
||||
|
||||
temp = NULL;
|
||||
|
||||
/* find the first fit block */
|
||||
for (header = allocate_list; header != NULL; )
|
||||
{
|
||||
for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
|
||||
{
|
||||
if (global_variable[i].type != finsh_type_unknown)
|
||||
{
|
||||
if (global_variable[i].value.ptr == finsh_block_get_data(header))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
temp = header;
|
||||
header = header->next;
|
||||
|
||||
/* this block is an unused block, release it */
|
||||
if (i == FINSH_VARIABLE_MAX)
|
||||
{
|
||||
finsh_heap_free(finsh_block_get_data(temp));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* insert a block to list
|
||||
*/
|
||||
void finsh_block_insert(struct finsh_block_header** list, struct finsh_block_header* header)
|
||||
{
|
||||
struct finsh_block_header* node;
|
||||
|
||||
if (*list == NULL)
|
||||
{
|
||||
*list = header;
|
||||
return;
|
||||
}
|
||||
|
||||
/* find out insert point */
|
||||
node = *list;
|
||||
|
||||
if (node > header)
|
||||
{
|
||||
/* insert node in the header of list */
|
||||
header->next = node;
|
||||
*list = header;
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (node = *list; node; node = node->next)
|
||||
{
|
||||
if (node->next > header) break;
|
||||
|
||||
if (node->next == NULL) break;
|
||||
}
|
||||
}
|
||||
|
||||
/* insert node */
|
||||
if (node->next != NULL) header->next = node->next;
|
||||
node->next = header;
|
||||
}
|
||||
|
||||
/**
|
||||
* remove block from list
|
||||
*/
|
||||
void finsh_block_remove(struct finsh_block_header** list, struct finsh_block_header* header)
|
||||
{
|
||||
struct finsh_block_header* node;
|
||||
|
||||
node = *list;
|
||||
if (node == header)
|
||||
{
|
||||
/* remove list header */
|
||||
*list = header->next;
|
||||
header->next = NULL;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (node = *list; node != NULL; node = node->next)
|
||||
{
|
||||
if (node->next == header)
|
||||
{
|
||||
node->next = header->next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* split block
|
||||
*/
|
||||
void finsh_block_split(struct finsh_block_header* header, size_t size)
|
||||
{
|
||||
struct finsh_block_header* next;
|
||||
|
||||
/*
|
||||
* split header into two node:
|
||||
* header->next->...
|
||||
*/
|
||||
next = BLOCK_HEADER((u_char*)header + sizeof(struct finsh_block_header) + size);
|
||||
next->length = header->length - sizeof(struct finsh_block_header) - size;
|
||||
header->length = size;
|
||||
next->next = header->next;
|
||||
|
||||
header->next = next;
|
||||
}
|
||||
|
||||
void finsh_block_merge(struct finsh_block_header** list, struct finsh_block_header* header)
|
||||
{
|
||||
struct finsh_block_header* prev_node;
|
||||
struct finsh_block_header* next_node;
|
||||
|
||||
next_node = header->next;
|
||||
|
||||
if (*list == header) prev_node = NULL;
|
||||
else
|
||||
{
|
||||
/* find out the previous header */
|
||||
for (prev_node = *list; prev_node; prev_node =prev_node->next)
|
||||
{
|
||||
if (prev_node->next == header)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* try merge node */
|
||||
|
||||
/* merge to previous node */
|
||||
if (prev_node != NULL &&
|
||||
((u_char*)prev_node + prev_node->length + sizeof(struct finsh_block_header)
|
||||
== (u_char*)header))
|
||||
{
|
||||
/* is it close to next node? */
|
||||
if ((next_node != NULL) &&
|
||||
((u_char*)header + header->length + sizeof(struct finsh_block_header)
|
||||
== (u_char*)next_node))
|
||||
{
|
||||
/* merge three node */
|
||||
prev_node->length += header->length + next_node->length +
|
||||
2 * sizeof(struct finsh_block_header);
|
||||
|
||||
prev_node->next = next_node->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev_node->length += header->length + sizeof(struct finsh_block_header);
|
||||
prev_node->next = header->next;
|
||||
}
|
||||
}
|
||||
else /* merge to last node */
|
||||
if ( (next_node != NULL) &&
|
||||
((u_char*)header + header->length + sizeof(struct finsh_block_header)
|
||||
== (u_char*)next_node))
|
||||
{
|
||||
header->length += next_node->length + sizeof(struct finsh_block_header);
|
||||
header->next = next_node->next;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* heap management in finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include <finsh.h>
|
||||
|
||||
#ifndef __FINSH_HEAP_H__
|
||||
#define __FINSH_HEAP_H__
|
||||
|
||||
int finsh_heap_init(void);
|
||||
void* finsh_heap_allocate(size_t size);
|
||||
void finsh_heap_free(void*ptr);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Initialization procedure for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include <finsh.h>
|
||||
|
||||
#include "finsh_node.h"
|
||||
#include "finsh_vm.h"
|
||||
#include "finsh_parser.h"
|
||||
#include "finsh_var.h"
|
||||
#include "finsh_error.h"
|
||||
#include "finsh_heap.h"
|
||||
|
||||
int finsh_init(struct finsh_parser* parser)
|
||||
{
|
||||
finsh_parser_init(parser);
|
||||
|
||||
/* finsh init */
|
||||
finsh_node_init();
|
||||
finsh_var_init();
|
||||
finsh_error_init();
|
||||
finsh_heap_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
long finsh_stack_bottom()
|
||||
{
|
||||
return finsh_vm_stack[0].long_value;
|
||||
}
|
||||
|
||||
int finsh_flush(struct finsh_parser* parser)
|
||||
{
|
||||
finsh_parser_init(parser);
|
||||
|
||||
/* finsh init */
|
||||
finsh_node_init();
|
||||
finsh_error_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int finsh_reset(struct finsh_parser* parser)
|
||||
{
|
||||
/* finsh init */
|
||||
finsh_node_init();
|
||||
finsh_var_init();
|
||||
finsh_error_init();
|
||||
finsh_heap_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* node routines for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include <finsh.h>
|
||||
|
||||
#include "finsh_node.h"
|
||||
#include "finsh_error.h"
|
||||
#include "finsh_var.h"
|
||||
#include "finsh_heap.h"
|
||||
|
||||
struct finsh_node global_node_table[FINSH_NODE_MAX];
|
||||
|
||||
int finsh_node_init()
|
||||
{
|
||||
memset(global_node_table, 0, sizeof(global_node_table));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct finsh_node* finsh_node_allocate(u_char type)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* find an empty entry */
|
||||
for (i = 0; i < FINSH_NODE_MAX; i ++)
|
||||
{
|
||||
if (global_node_table[i].node_type == FINSH_NODE_UNKNOWN) break;
|
||||
}
|
||||
|
||||
if (i == FINSH_NODE_MAX) return NULL;
|
||||
|
||||
/* fill type field */
|
||||
global_node_table[i].node_type = type;
|
||||
|
||||
/* return this allocated node */
|
||||
return &global_node_table[i];
|
||||
}
|
||||
|
||||
struct finsh_node* finsh_node_new_id(char* id)
|
||||
{
|
||||
struct finsh_node* node;
|
||||
void* symbol;
|
||||
unsigned char type;
|
||||
|
||||
symbol = NULL;
|
||||
type = 0;
|
||||
node = NULL;
|
||||
|
||||
/* lookup variable firstly */
|
||||
symbol = (void*)finsh_var_lookup(id);
|
||||
if (symbol == NULL)
|
||||
{
|
||||
/* then lookup system variable */
|
||||
symbol = (void*)finsh_sysvar_lookup(id);
|
||||
if (symbol == NULL)
|
||||
{
|
||||
/* then lookup system call */
|
||||
symbol = (void*)finsh_syscall_lookup(id);
|
||||
if (symbol != NULL) type = FINSH_IDTYPE_SYSCALL;
|
||||
}
|
||||
else type = FINSH_IDTYPE_SYSVAR;
|
||||
}
|
||||
else type = FINSH_IDTYPE_VAR;
|
||||
|
||||
if (symbol != NULL)
|
||||
{
|
||||
/* allocate a new node */
|
||||
node = finsh_node_allocate(FINSH_NODE_ID);
|
||||
|
||||
/* allocate node error */
|
||||
if (node == NULL)
|
||||
{
|
||||
finsh_error_set(FINSH_ERROR_MEMORY_FULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* fill node value according type */
|
||||
switch (type)
|
||||
{
|
||||
case FINSH_IDTYPE_VAR:
|
||||
node->id.var = (struct finsh_var*)symbol;
|
||||
break;
|
||||
|
||||
case FINSH_IDTYPE_SYSVAR:
|
||||
node->id.sysvar = (struct finsh_sysvar*)symbol;
|
||||
break;
|
||||
|
||||
case FINSH_IDTYPE_SYSCALL:
|
||||
node->id.syscall = (struct finsh_syscall*)symbol;
|
||||
break;
|
||||
}
|
||||
/* fill identifier type */
|
||||
node->idtype = type;
|
||||
}
|
||||
else finsh_error_set(FINSH_ERROR_UNKNOWN_SYMBOL);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
struct finsh_node* finsh_node_new_char(char c)
|
||||
{
|
||||
struct finsh_node* node;
|
||||
|
||||
node = finsh_node_allocate(FINSH_NODE_VALUE_CHAR);
|
||||
if (node == NULL)
|
||||
{
|
||||
finsh_error_set(FINSH_ERROR_MEMORY_FULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->value.char_value = c;
|
||||
return node;
|
||||
}
|
||||
|
||||
struct finsh_node* finsh_node_new_int(int i)
|
||||
{
|
||||
struct finsh_node* node;
|
||||
|
||||
node = finsh_node_allocate(FINSH_NODE_VALUE_INT);
|
||||
if (node == NULL)
|
||||
{
|
||||
finsh_error_set(FINSH_ERROR_MEMORY_FULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->value.int_value = i;
|
||||
return node;
|
||||
}
|
||||
|
||||
struct finsh_node* finsh_node_new_long(long l)
|
||||
{
|
||||
struct finsh_node* node;
|
||||
|
||||
node = finsh_node_allocate(FINSH_NODE_VALUE_LONG);
|
||||
if (node == NULL)
|
||||
{
|
||||
finsh_error_set(FINSH_ERROR_MEMORY_FULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->value.long_value = l;
|
||||
return node;
|
||||
}
|
||||
|
||||
struct finsh_node* finsh_node_new_string(char* s)
|
||||
{
|
||||
struct finsh_node* node;
|
||||
|
||||
node = finsh_node_allocate(FINSH_NODE_VALUE_STRING);
|
||||
if (node == NULL)
|
||||
{
|
||||
finsh_error_set(FINSH_ERROR_MEMORY_FULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* make string */
|
||||
node->value.ptr = finsh_heap_allocate(strlen(s) + 1);
|
||||
strncpy(node->value.ptr, s, strlen(s));
|
||||
((u_char*)node->value.ptr)[strlen(s)] = '\0';
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
struct finsh_node* finsh_node_new_ptr(void* ptr)
|
||||
{
|
||||
struct finsh_node* node;
|
||||
|
||||
node = finsh_node_allocate(FINSH_NODE_VALUE_NULL);
|
||||
if (node == NULL)
|
||||
{
|
||||
finsh_error_set(FINSH_ERROR_MEMORY_FULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->value.ptr = ptr;
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* node routines for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_NODE_H__
|
||||
#define __FINSH_NODE_H__
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
#define FINSH_NODE_UNKNOWN 0
|
||||
#define FINSH_NODE_ID 1
|
||||
|
||||
#define FINSH_NODE_VALUE_CHAR 2
|
||||
#define FINSH_NODE_VALUE_INT 3
|
||||
#define FINSH_NODE_VALUE_LONG 4
|
||||
#define FINSH_NODE_VALUE_STRING 5
|
||||
#define FINSH_NODE_VALUE_NULL 6
|
||||
|
||||
#define FINSH_NODE_SYS_ADD 7
|
||||
#define FINSH_NODE_SYS_SUB 8
|
||||
#define FINSH_NODE_SYS_MUL 9
|
||||
#define FINSH_NODE_SYS_DIV 10
|
||||
#define FINSH_NODE_SYS_MOD 11
|
||||
#define FINSH_NODE_SYS_AND 12
|
||||
#define FINSH_NODE_SYS_OR 13
|
||||
#define FINSH_NODE_SYS_XOR 14
|
||||
#define FINSH_NODE_SYS_BITWISE 15
|
||||
#define FINSH_NODE_SYS_SHL 16
|
||||
#define FINSH_NODE_SYS_SHR 17
|
||||
#define FINSH_NODE_SYS_FUNC 18
|
||||
#define FINSH_NODE_SYS_ASSIGN 19
|
||||
#define FINSH_NODE_SYS_CAST 20
|
||||
#define FINSH_NODE_SYS_PREINC 21
|
||||
#define FINSH_NODE_SYS_PREDEC 22
|
||||
#define FINSH_NODE_SYS_INC 23
|
||||
#define FINSH_NODE_SYS_DEC 24
|
||||
#define FINSH_NODE_SYS_GETVALUE 25
|
||||
#define FINSH_NODE_SYS_GETADDR 26
|
||||
#define FINSH_NODE_SYS_NULL 27
|
||||
|
||||
#define FINSH_DATA_TYPE_VOID 0x00
|
||||
#define FINSH_DATA_TYPE_BYTE 0x01
|
||||
#define FINSH_DATA_TYPE_WORD 0x02
|
||||
#define FINSH_DATA_TYPE_DWORD 0x03
|
||||
#define FINSH_DATA_TYPE_PTR 0x10
|
||||
|
||||
#define FINSH_NODE_VALUE 0
|
||||
#define FINSH_NODE_ADDRESS 1
|
||||
#define FINSH_NODE_FUNCTION 2
|
||||
|
||||
int finsh_node_init(void);
|
||||
|
||||
struct finsh_node* finsh_node_allocate(u_char type);
|
||||
struct finsh_node* finsh_node_new_id(char* id);
|
||||
struct finsh_node* finsh_node_new_char(char c);
|
||||
struct finsh_node* finsh_node_new_int(int i);
|
||||
struct finsh_node* finsh_node_new_long(long l);
|
||||
struct finsh_node* finsh_node_new_string(char* s);
|
||||
struct finsh_node* finsh_node_new_ptr(void* ptr);
|
||||
|
||||
#define finsh_node_sibling(node) ((node)->sibling)
|
||||
#define finsh_node_child(node) ((node)->child)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,622 @@
|
||||
/*
|
||||
* operations for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include "finsh_ops.h"
|
||||
#include "finsh_vm.h"
|
||||
|
||||
#define OP_BIN_BYTE(x) do {\
|
||||
(finsh_sp - 2)->char_value = (finsh_sp - 2)->char_value x (finsh_sp - 1)->char_value; \
|
||||
finsh_sp--; \
|
||||
}while (0)
|
||||
|
||||
#define OP_BIN_WORD(x) do {\
|
||||
(finsh_sp - 2)->short_value = (finsh_sp - 2)->short_value x (finsh_sp - 1)->short_value; \
|
||||
finsh_sp--; \
|
||||
}while (0)
|
||||
|
||||
#define OP_BIN_DWORD(x) do {\
|
||||
(finsh_sp - 2)->long_value = (finsh_sp - 2)->long_value x (finsh_sp - 1)->long_value; \
|
||||
finsh_sp--; \
|
||||
}while (0)
|
||||
|
||||
/* --- noop --- */
|
||||
void OP_no_op()
|
||||
{
|
||||
/* none */
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- add --- */
|
||||
void OP_add_byte()
|
||||
{
|
||||
OP_BIN_BYTE(+);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_add_word()
|
||||
{
|
||||
OP_BIN_WORD(+);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_add_dword()
|
||||
{
|
||||
OP_BIN_DWORD(+);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- sub --- */
|
||||
void OP_sub_byte()
|
||||
{
|
||||
OP_BIN_BYTE(-);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_sub_word()
|
||||
{
|
||||
OP_BIN_WORD(-);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_sub_dword()
|
||||
{
|
||||
OP_BIN_DWORD(-);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- div --- */
|
||||
void OP_div_byte()
|
||||
{
|
||||
OP_BIN_BYTE(/);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_div_word()
|
||||
{
|
||||
OP_BIN_WORD(/);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_div_dword()
|
||||
{
|
||||
OP_BIN_DWORD(/);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- mod --- */
|
||||
void OP_mod_byte()
|
||||
{
|
||||
OP_BIN_BYTE(%);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_mod_word()
|
||||
{
|
||||
OP_BIN_WORD(%);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_mod_dword()
|
||||
{
|
||||
OP_BIN_DWORD(%);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- mul --- */
|
||||
void OP_mul_byte()
|
||||
{
|
||||
OP_BIN_BYTE(*);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_mul_word()
|
||||
{
|
||||
OP_BIN_WORD(*);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_mul_dword()
|
||||
{
|
||||
OP_BIN_DWORD(*);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- and --- */
|
||||
void OP_and_byte()
|
||||
{
|
||||
OP_BIN_BYTE(&);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_and_word()
|
||||
{
|
||||
OP_BIN_WORD(&);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_and_dword()
|
||||
{
|
||||
OP_BIN_DWORD(&);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- or --- */
|
||||
void OP_or_byte()
|
||||
{
|
||||
OP_BIN_BYTE(|);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_or_word()
|
||||
{
|
||||
OP_BIN_WORD(|);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_or_dword()
|
||||
{
|
||||
OP_BIN_DWORD(|);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- xor --- */
|
||||
void OP_xor_byte()
|
||||
{
|
||||
OP_BIN_BYTE(^);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_xor_word()
|
||||
{
|
||||
OP_BIN_WORD(^);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_xor_dword()
|
||||
{
|
||||
OP_BIN_DWORD(^);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- bw --- */
|
||||
void OP_bw_byte()
|
||||
{
|
||||
(finsh_sp - 1)->char_value = ~ ((finsh_sp - 1)->char_value);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_bw_word()
|
||||
{
|
||||
(finsh_sp - 1)->short_value = ~ ((finsh_sp - 1)->short_value);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_bw_dword()
|
||||
{
|
||||
(finsh_sp - 1)->long_value = ~ ((finsh_sp - 1)->long_value);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- shl --- */
|
||||
void OP_shl_byte()
|
||||
{
|
||||
OP_BIN_BYTE(<<);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_shl_word()
|
||||
{
|
||||
OP_BIN_WORD(<<);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_shl_dword()
|
||||
{
|
||||
OP_BIN_DWORD(<<);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- shr --- */
|
||||
void OP_shr_byte()
|
||||
{
|
||||
OP_BIN_BYTE(>>);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_shr_word()
|
||||
{
|
||||
OP_BIN_WORD(>>);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_shr_dword()
|
||||
{
|
||||
OP_BIN_DWORD(>>);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- ld --- */
|
||||
void OP_ld_byte()
|
||||
{
|
||||
finsh_sp->char_value = *finsh_pc;
|
||||
|
||||
finsh_sp++;
|
||||
finsh_pc++;
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_ld_word()
|
||||
{
|
||||
finsh_sp->short_value = FINSH_GET16(finsh_pc);
|
||||
|
||||
finsh_sp ++;
|
||||
finsh_pc += 2;
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_ld_dword()
|
||||
{
|
||||
finsh_sp->long_value = FINSH_GET32(finsh_pc);
|
||||
|
||||
finsh_sp ++;
|
||||
finsh_pc += 4;
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void OP_ld_value_byte()
|
||||
{
|
||||
char* c;
|
||||
|
||||
c = (char*) (FINSH_GET32(finsh_pc));
|
||||
|
||||
finsh_sp->char_value = *c;
|
||||
|
||||
finsh_sp ++;
|
||||
finsh_pc += 4;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void OP_ld_value_byte_stack()
|
||||
{
|
||||
char* c;
|
||||
|
||||
c = (char *)(finsh_sp - 1)->long_value;
|
||||
(finsh_sp - 1)->char_value = *c;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void OP_ld_value_word()
|
||||
{
|
||||
short* s;
|
||||
|
||||
s = (short*) (FINSH_GET32(finsh_pc));
|
||||
|
||||
finsh_sp->short_value = *s;
|
||||
|
||||
finsh_sp ++;
|
||||
finsh_pc += 4;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void OP_ld_value_word_stack()
|
||||
{
|
||||
short* s;
|
||||
|
||||
s = (short *)(finsh_sp - 1)->long_value;
|
||||
(finsh_sp - 1)->short_value = *s;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void OP_ld_value_dword()
|
||||
{
|
||||
long* l;
|
||||
|
||||
l = (long*) (FINSH_GET32(finsh_pc));
|
||||
|
||||
finsh_sp->long_value = *l;
|
||||
|
||||
finsh_sp ++;
|
||||
finsh_pc += 4;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void OP_ld_value_dword_stack()
|
||||
{
|
||||
long* l;
|
||||
|
||||
l = (long *)(finsh_sp - 1)->long_value;
|
||||
(finsh_sp - 1)->long_value = *l;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* --- st --- */
|
||||
/*
|
||||
* 2006-4-16 bernard
|
||||
* fixed the sp move bug
|
||||
*/
|
||||
void OP_st_byte()
|
||||
{
|
||||
*(char*)((finsh_sp - 2)->long_value) = (finsh_sp - 1)->char_value;
|
||||
finsh_sp --;
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/*
|
||||
* 2006-4-16 bernard
|
||||
* fixed the sp move bug
|
||||
*/
|
||||
void OP_st_word()
|
||||
{
|
||||
*(short*)((finsh_sp - 2)->long_value) = (finsh_sp - 1)->short_value;
|
||||
finsh_sp --;
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/*
|
||||
* 2006-4-16 bernard
|
||||
* fixed the sp move bug
|
||||
*/
|
||||
void OP_st_dword()
|
||||
{
|
||||
*(long*)((finsh_sp - 2)->long_value) = (finsh_sp - 1)->long_value;
|
||||
finsh_sp --;
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- pop --- */
|
||||
void OP_pop()
|
||||
{
|
||||
finsh_sp --;
|
||||
return ;
|
||||
}
|
||||
|
||||
/* --- call --- */
|
||||
void OP_call()
|
||||
{
|
||||
/* the max number of arg*/
|
||||
unsigned long parameterv[16];
|
||||
unsigned int parameters, i;
|
||||
|
||||
typedef unsigned long var_t;
|
||||
typedef var_t (*op_func)();
|
||||
op_func f;
|
||||
var_t r;
|
||||
|
||||
parameters = *finsh_pc ++;
|
||||
|
||||
i = 0; finsh_sp --;
|
||||
while (i < parameters)
|
||||
{
|
||||
parameterv[parameters - 1 - i] = finsh_sp->long_value;
|
||||
finsh_sp --;
|
||||
i++;
|
||||
}
|
||||
|
||||
f = (op_func)(finsh_sp->long_value);
|
||||
switch (parameters)
|
||||
{
|
||||
case 0:
|
||||
r = f(0);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
r = f(parameterv[0]);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
r = f(parameterv[0], parameterv[1]);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2]);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3]);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4]);
|
||||
break;
|
||||
|
||||
case 6:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5]);
|
||||
break;
|
||||
|
||||
case 7:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6]);
|
||||
break;
|
||||
|
||||
case 8:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7]);
|
||||
break;
|
||||
|
||||
case 9:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7],
|
||||
parameterv[8]);
|
||||
break;
|
||||
|
||||
case 10:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7],
|
||||
parameterv[8], parameterv[9]);
|
||||
break;
|
||||
|
||||
case 11:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7],
|
||||
parameterv[8], parameterv[9], parameterv[10]);
|
||||
break;
|
||||
|
||||
case 12:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7],
|
||||
parameterv[8], parameterv[9], parameterv[10], parameterv[11]);
|
||||
break;
|
||||
|
||||
case 13:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7],
|
||||
parameterv[8], parameterv[9], parameterv[10], parameterv[11],
|
||||
parameterv[12]);
|
||||
break;
|
||||
|
||||
case 14:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7],
|
||||
parameterv[8], parameterv[9], parameterv[10], parameterv[11],
|
||||
parameterv[12], parameterv[13]);
|
||||
break;
|
||||
|
||||
case 15:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7],
|
||||
parameterv[8], parameterv[9], parameterv[10], parameterv[11],
|
||||
parameterv[12], parameterv[13], parameterv[14]);
|
||||
break;
|
||||
|
||||
case 16:
|
||||
r = f(parameterv[0], parameterv[1], parameterv[2], parameterv[3],
|
||||
parameterv[4], parameterv[5], parameterv[6], parameterv[7],
|
||||
parameterv[8], parameterv[9], parameterv[10], parameterv[11],
|
||||
parameterv[12], parameterv[13], parameterv[14], parameterv[15]);
|
||||
break;
|
||||
|
||||
default:
|
||||
r = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
finsh_sp->long_value = r;
|
||||
finsh_sp ++;
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
const op_func op_table[] =
|
||||
{
|
||||
/* 00 */ OP_no_op,
|
||||
/* 01 */ OP_add_byte,
|
||||
/* 02 */ OP_add_word,
|
||||
/* 03 */ OP_add_dword,
|
||||
/* 04 */ OP_sub_byte,
|
||||
/* 05 */ OP_sub_word,
|
||||
/* 06 */ OP_sub_dword,
|
||||
/* 07 */ OP_div_byte,
|
||||
/* 08 */ OP_div_word,
|
||||
/* 09 */ OP_div_dword,
|
||||
/* 10 */ OP_mod_byte,
|
||||
/* 11 */ OP_mod_word,
|
||||
/* 12 */ OP_mod_dword,
|
||||
/* 13 */ OP_mul_byte,
|
||||
/* 14 */ OP_mul_word,
|
||||
/* 15 */ OP_mul_dword,
|
||||
/* 16 */ OP_and_byte,
|
||||
/* 17 */ OP_and_word,
|
||||
/* 18 */ OP_and_dword,
|
||||
/* 19 */ OP_or_byte,
|
||||
/* 20 */ OP_or_word,
|
||||
/* 21 */ OP_or_dword,
|
||||
/* 22 */ OP_xor_byte,
|
||||
/* 23 */ OP_xor_word,
|
||||
/* 24 */ OP_xor_dword,
|
||||
/* 25 */ OP_bw_byte,
|
||||
/* 26 */ OP_bw_word,
|
||||
/* 27 */ OP_bw_dword,
|
||||
/* 28 */ OP_shl_byte,
|
||||
/* 29 */ OP_shl_word,
|
||||
/* 30 */ OP_shl_dword,
|
||||
/* 31 */ OP_shr_byte,
|
||||
/* 32 */ OP_shr_word,
|
||||
/* 33 */ OP_shr_dword,
|
||||
/* 34 */ OP_ld_byte,
|
||||
/* 35 */ OP_ld_word,
|
||||
/* 36 */ OP_ld_dword,
|
||||
/* 37 */ OP_ld_value_byte,
|
||||
/* 38 */ OP_ld_value_word,
|
||||
/* 39 */ OP_ld_value_dword,
|
||||
/* 40 */ OP_st_byte,
|
||||
/* 41 */ OP_st_word,
|
||||
/* 42 */ OP_st_dword,
|
||||
/* 43 */ OP_pop,
|
||||
/* 44 */ OP_call,
|
||||
/* 45 */ OP_ld_value_byte_stack,
|
||||
/* 46 */ OP_ld_value_word_stack,
|
||||
/* 47 */ OP_ld_value_dword_stack,
|
||||
NULL
|
||||
};
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* operations for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_OP_H__
|
||||
#define __FINSH_OP_H__
|
||||
|
||||
#include "finsh_vm.h"
|
||||
|
||||
/*
|
||||
* FinC VM specification
|
||||
* Memory
|
||||
* .VAR
|
||||
*
|
||||
* .STACK
|
||||
*
|
||||
* .HEAP
|
||||
*
|
||||
* .TEXT
|
||||
* OP [op1]
|
||||
*/
|
||||
|
||||
#define FINSH_OP_NOOP 0x00
|
||||
|
||||
/* add @ r1 = r2 + r3 */
|
||||
#define FINSH_OP_ADD_BYTE 0x01
|
||||
#define FINSH_OP_ADD_WORD 0x02
|
||||
#define FINSH_OP_ADD_DWORD 0x03
|
||||
|
||||
/* sub @ r1 = r2 - r3 */
|
||||
#define FINSH_OP_SUB_BYTE 0x04
|
||||
#define FINSH_OP_SUB_WORD 0x05
|
||||
#define FINSH_OP_SUB_DWORD 0x06
|
||||
|
||||
/* div @ r1 = r2 / r3 */
|
||||
#define FINSH_OP_DIV_BYTE 0x07
|
||||
#define FINSH_OP_DIV_WORD 0x08
|
||||
#define FINSH_OP_DIV_DWORD 0x09
|
||||
|
||||
/* mod @ r1 = r2 % r3 */
|
||||
#define FINSH_OP_MOD_BYTE 0x0A
|
||||
#define FINSH_OP_MOD_WORD 0x0B
|
||||
#define FINSH_OP_MOD_DWORD 0x0C
|
||||
|
||||
/* mul @ r1 = r2 * r3 */
|
||||
#define FINSH_OP_MUL_BYTE 0x0D
|
||||
#define FINSH_OP_MUL_WORD 0x0E
|
||||
#define FINSH_OP_MUL_DWORD 0x0F
|
||||
|
||||
/* and @ r1 = r2 & r3 */
|
||||
#define FINSH_OP_AND_BYTE 0x10
|
||||
#define FINSH_OP_AND_WORD 0x11
|
||||
#define FINSH_OP_AND_DWORD 0x12
|
||||
|
||||
/* or @ r1 = r2 | r3 */
|
||||
#define FINSH_OP_OR_BYTE 0x13
|
||||
#define FINSH_OP_OR_WORD 0x14
|
||||
#define FINSH_OP_OR_DWORD 0x15
|
||||
|
||||
/* xor @ r1 = r2 ^ r3 */
|
||||
#define FINSH_OP_XOR_BYTE 0x16
|
||||
#define FINSH_OP_XOR_WORD 0x17
|
||||
#define FINSH_OP_XOR_DWORD 0x18
|
||||
|
||||
/* bw @ r1 = ~r2 */
|
||||
#define FINSH_OP_BITWISE_BYTE 0x19
|
||||
#define FINSH_OP_BITWISE_WORD 0x1A
|
||||
#define FINSH_OP_BITWISE_DWORD 0x1B
|
||||
|
||||
/* shl @ r1 = r2 << r3 */
|
||||
#define FINSH_OP_SHL_BYTE 0x1C
|
||||
#define FINSH_OP_SHL_WORD 0x1D
|
||||
#define FINSH_OP_SHL_DWORD 0x1E
|
||||
|
||||
/* shr @ r1 = r2 >> r3 */
|
||||
#define FINSH_OP_SHR_BYTE 0x1F
|
||||
#define FINSH_OP_SHR_WORD 0x20
|
||||
#define FINSH_OP_SHR_DWORD 0x21
|
||||
|
||||
/* ld @ r1 = [r2] */
|
||||
#define FINSH_OP_LD_BYTE 0x22
|
||||
#define FINSH_OP_LD_WORD 0x23
|
||||
#define FINSH_OP_LD_DWORD 0x24
|
||||
|
||||
#define FINSH_OP_LD_VALUE_BYTE 0x25
|
||||
#define FINSH_OP_LD_VALUE_WORD 0x26
|
||||
#define FINSH_OP_LD_VALUE_DWORD 0x27
|
||||
|
||||
/* st @ [r2] = r1 */
|
||||
#define FINSH_OP_ST_BYTE 0x28
|
||||
#define FINSH_OP_ST_WORD 0x29
|
||||
#define FINSH_OP_ST_DWORD 0x2A
|
||||
|
||||
/* pop */
|
||||
#define FINSH_OP_POP 0x2B
|
||||
|
||||
/* call r1 @ [r1](stack) */
|
||||
#define FINSH_OP_SYSCALL 0x2C
|
||||
|
||||
/* load value from stack */
|
||||
#define FINSH_OP_LD_VALUE_BYTE_STACK 0x2D
|
||||
#define FINSH_OP_LD_VALUE_WORD_STACK 0x2E
|
||||
#define FINSH_OP_LD_VALUE_DWORD_STACK 0x2F
|
||||
|
||||
/* halt */
|
||||
#define FINSH_OP_HALT 0xFF
|
||||
|
||||
typedef void (*op_func)();
|
||||
extern const op_func op_table[];
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* script parser for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_PARSER_H__
|
||||
#define __FINSH_PARSER_H__
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
int finsh_parser_init(struct finsh_parser* self);
|
||||
void finsh_parser_run(struct finsh_parser* self, const u_char* string);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,603 @@
|
||||
/*
|
||||
* token lex for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
* 2013-04-03 Bernard strip more characters.
|
||||
*/
|
||||
#include <finsh.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "finsh_token.h"
|
||||
#include "finsh_error.h"
|
||||
|
||||
#define is_alpha(ch) ((ch | 0x20) - 'a') < 26u
|
||||
#define is_digit(ch) ((ch) >= '0' && (ch) <= '9')
|
||||
#define is_separator(ch) !(((ch) >= 'a' && (ch) <= 'z') \
|
||||
|| ((ch) >= 'A' && (ch) <= 'Z') || ((ch) >= '0' && (ch) <= '9') || ((ch) == '_'))
|
||||
#define is_eof(self) (self)->eof
|
||||
|
||||
struct name_table
|
||||
{
|
||||
char* name;
|
||||
enum finsh_token_type type;
|
||||
};
|
||||
|
||||
/* keyword */
|
||||
static const struct name_table finsh_name_table[] =
|
||||
{
|
||||
{"void", finsh_token_type_void},
|
||||
{"char", finsh_token_type_char},
|
||||
{"short", finsh_token_type_short},
|
||||
{"int", finsh_token_type_int},
|
||||
{"long", finsh_token_type_long},
|
||||
{"unsigned", finsh_token_type_unsigned},
|
||||
|
||||
{"NULL", finsh_token_type_value_null},
|
||||
{"null", finsh_token_type_value_null}
|
||||
};
|
||||
|
||||
static char token_next_char(struct finsh_token* self);
|
||||
static void token_prev_char(struct finsh_token* self);
|
||||
static long token_spec_number(char* string, int length, int b);
|
||||
static void token_run(struct finsh_token* self);
|
||||
static int token_match_name(struct finsh_token* self, const char* str);
|
||||
static void token_proc_number(struct finsh_token* self);
|
||||
static u_char* token_proc_string(struct finsh_token* self);
|
||||
static void token_trim_space(struct finsh_token* self);
|
||||
static char token_proc_char(struct finsh_token* self);
|
||||
static int token_proc_escape(struct finsh_token* self);
|
||||
|
||||
void finsh_token_init(struct finsh_token* self, u_char* line)
|
||||
{
|
||||
memset(self, 0, sizeof(struct finsh_token));
|
||||
|
||||
self->line = line;
|
||||
}
|
||||
|
||||
enum finsh_token_type finsh_token_token(struct finsh_token* self)
|
||||
{
|
||||
if ( self->replay ) self->replay = 0;
|
||||
else token_run(self);
|
||||
|
||||
return (enum finsh_token_type)self->current_token;
|
||||
}
|
||||
|
||||
void finsh_token_get_token(struct finsh_token* self, u_char* token)
|
||||
{
|
||||
strncpy((char*)token, (char*)self->string, FINSH_NAME_MAX);
|
||||
}
|
||||
|
||||
int token_get_string(struct finsh_token* self, u_char* str)
|
||||
{
|
||||
unsigned char *p=str;
|
||||
char ch;
|
||||
|
||||
ch = token_next_char(self);
|
||||
if (is_eof(self)) return -1;
|
||||
|
||||
str[0] = '\0';
|
||||
|
||||
if ( is_digit(ch) )/*the first character of identifier is not a digit.*/
|
||||
{
|
||||
token_prev_char(self);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (!is_separator(ch) && !is_eof(self))
|
||||
{
|
||||
*p++ = ch;
|
||||
|
||||
ch = token_next_char(self);
|
||||
}
|
||||
self->eof = 0;
|
||||
|
||||
token_prev_char(self);
|
||||
*p = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
get next character.
|
||||
*/
|
||||
static char token_next_char(struct finsh_token* self)
|
||||
{
|
||||
if (self->eof) return '\0';
|
||||
|
||||
if (self->position == (int)strlen((char*)self->line) || self->line[self->position] =='\n')
|
||||
{
|
||||
self->eof = 1;
|
||||
self->position = 0;
|
||||
return '\0';
|
||||
}
|
||||
|
||||
return self->line[self->position++];
|
||||
}
|
||||
|
||||
static void token_prev_char(struct finsh_token* self)
|
||||
{
|
||||
if ( self->eof ) return;
|
||||
|
||||
if ( self->position == 0 ) return;
|
||||
else self->position--;
|
||||
}
|
||||
|
||||
static void token_run(struct finsh_token* self)
|
||||
{
|
||||
char ch;
|
||||
|
||||
token_trim_space(self); /* first trim space and tab. */
|
||||
token_get_string(self, &(self->string[0]));
|
||||
|
||||
if ( is_eof(self) ) /*if it is eof, break;*/
|
||||
{
|
||||
self->current_token = finsh_token_type_eof;
|
||||
return ;
|
||||
}
|
||||
|
||||
if (self->string[0] != '\0') /*It is a key word or a identifier.*/
|
||||
{
|
||||
if ( !token_match_name(self, (char*)self->string) )
|
||||
{
|
||||
self->current_token = finsh_token_type_identifier;
|
||||
}
|
||||
}
|
||||
else/*It is a operator character.*/
|
||||
{
|
||||
ch = token_next_char(self);
|
||||
|
||||
switch ( ch )
|
||||
{
|
||||
case '(':
|
||||
self->current_token = finsh_token_type_left_paren;
|
||||
break;
|
||||
|
||||
case ')':
|
||||
self->current_token = finsh_token_type_right_paren;
|
||||
break;
|
||||
|
||||
case ',':
|
||||
self->current_token = finsh_token_type_comma;
|
||||
break;
|
||||
|
||||
case ';':
|
||||
self->current_token = finsh_token_type_semicolon;
|
||||
break;
|
||||
|
||||
case '&':
|
||||
self->current_token = finsh_token_type_and;
|
||||
break;
|
||||
|
||||
case '*':
|
||||
self->current_token = finsh_token_type_mul;
|
||||
break;
|
||||
|
||||
case '+':
|
||||
ch = token_next_char(self);
|
||||
|
||||
if ( ch == '+' )
|
||||
{
|
||||
self->current_token = finsh_token_type_inc;
|
||||
}
|
||||
else
|
||||
{
|
||||
token_prev_char(self);
|
||||
self->current_token = finsh_token_type_add;
|
||||
}
|
||||
break;
|
||||
|
||||
case '-':
|
||||
ch = token_next_char(self);
|
||||
|
||||
if ( ch == '-' )
|
||||
{
|
||||
self->current_token = finsh_token_type_dec;
|
||||
}
|
||||
else
|
||||
{
|
||||
token_prev_char(self);
|
||||
self->current_token = finsh_token_type_sub;
|
||||
}
|
||||
break;
|
||||
|
||||
case '/':
|
||||
ch = token_next_char(self);
|
||||
if (ch == '/')
|
||||
{
|
||||
/* line comments, set to end of file */
|
||||
self->current_token = finsh_token_type_eof;
|
||||
}
|
||||
else
|
||||
{
|
||||
token_prev_char(self);
|
||||
self->current_token = finsh_token_type_div;
|
||||
}
|
||||
break;
|
||||
|
||||
case '<':
|
||||
ch = token_next_char(self);
|
||||
|
||||
if ( ch == '<' )
|
||||
{
|
||||
self->current_token = finsh_token_type_shl;
|
||||
}
|
||||
else
|
||||
{
|
||||
token_prev_char(self);
|
||||
self->current_token = finsh_token_type_bad;
|
||||
}
|
||||
break;
|
||||
|
||||
case '>':
|
||||
ch = token_next_char(self);
|
||||
|
||||
if ( ch == '>' )
|
||||
{
|
||||
self->current_token = finsh_token_type_shr;
|
||||
}
|
||||
else
|
||||
{
|
||||
token_prev_char(self);
|
||||
self->current_token = finsh_token_type_bad;
|
||||
}
|
||||
break;
|
||||
|
||||
case '|':
|
||||
self->current_token = finsh_token_type_or;
|
||||
break;
|
||||
|
||||
case '%':
|
||||
self->current_token = finsh_token_type_mod;
|
||||
break;
|
||||
|
||||
case '~':
|
||||
self->current_token = finsh_token_type_bitwise;
|
||||
break;
|
||||
|
||||
case '^':
|
||||
self->current_token = finsh_token_type_xor;
|
||||
break;
|
||||
|
||||
case '=':
|
||||
self->current_token = finsh_token_type_assign;
|
||||
break;
|
||||
|
||||
case '\'':
|
||||
self->value.char_value = token_proc_char(self);
|
||||
self->current_token = finsh_token_type_value_char;
|
||||
break;
|
||||
|
||||
case '"':
|
||||
token_proc_string(self);
|
||||
self->current_token = finsh_token_type_value_string;
|
||||
break;
|
||||
|
||||
default:
|
||||
if ( is_digit(ch) )
|
||||
{
|
||||
token_prev_char(self);
|
||||
token_proc_number(self);
|
||||
break;
|
||||
}
|
||||
|
||||
finsh_error_set(FINSH_ERROR_UNKNOWN_TOKEN);
|
||||
self->current_token = finsh_token_type_bad;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int token_match_name(struct finsh_token* self, const char* str)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(finsh_name_table)/sizeof(struct name_table); i++)
|
||||
{
|
||||
if ( strcmp(finsh_name_table[i].name, str)==0 )
|
||||
{
|
||||
self->current_token = finsh_name_table[i].type;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void token_trim_space(struct finsh_token* self)
|
||||
{
|
||||
char ch;
|
||||
while ( (ch = token_next_char(self)) ==' ' ||
|
||||
ch == '\t' ||
|
||||
ch == '\r');
|
||||
|
||||
token_prev_char(self);
|
||||
}
|
||||
|
||||
static char token_proc_char(struct finsh_token* self)
|
||||
{
|
||||
char ch;
|
||||
char buf[4], *p;
|
||||
|
||||
p = buf;
|
||||
ch = token_next_char(self);
|
||||
|
||||
if ( ch == '\\' )
|
||||
{
|
||||
ch = token_next_char(self);
|
||||
switch ( ch )
|
||||
{
|
||||
case 'n': ch = '\n'; break;
|
||||
case 't': ch = '\t'; break;
|
||||
case 'v': ch = '\v'; break;
|
||||
case 'b': ch = '\b'; break;
|
||||
case 'r': ch = '\r'; break;
|
||||
case '\\': ch = '\\'; break;
|
||||
case '\'': ch = '\''; break;
|
||||
default :
|
||||
while ( is_digit(ch) )/*for '\113' char*/
|
||||
{
|
||||
ch = token_next_char(self);
|
||||
*p++ = ch;
|
||||
}
|
||||
|
||||
token_prev_char(self);
|
||||
*p = '\0';
|
||||
ch = atoi(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( token_next_char(self) != '\'' )
|
||||
{
|
||||
token_prev_char(self);
|
||||
finsh_error_set(FINSH_ERROR_EXPECT_CHAR);
|
||||
return ch;
|
||||
}
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
static u_char* token_proc_string(struct finsh_token* self)
|
||||
{
|
||||
u_char* p;
|
||||
|
||||
for ( p = &self->string[0]; p - &(self->string[0]) < FINSH_STRING_MAX; )
|
||||
{
|
||||
char ch = token_next_char(self);
|
||||
|
||||
if ( is_eof(self) )
|
||||
{
|
||||
finsh_error_set(FINSH_ERROR_UNEXPECT_END);
|
||||
return NULL;;
|
||||
}
|
||||
if ( ch == '\\' )
|
||||
{
|
||||
ch = token_proc_escape(self);
|
||||
}
|
||||
else if ( ch == '"' )/*end of string.*/
|
||||
{
|
||||
*p = '\0';
|
||||
return self->string;
|
||||
}
|
||||
|
||||
*p++ = ch;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int token_proc_escape(struct finsh_token* self)
|
||||
{
|
||||
char ch;
|
||||
int result=0;
|
||||
|
||||
ch = token_next_char(self);
|
||||
switch (ch)
|
||||
{
|
||||
case 'n':
|
||||
result = '\n';
|
||||
break;
|
||||
case 't':
|
||||
result = '\t';
|
||||
break;
|
||||
case 'v':
|
||||
result = '\v';
|
||||
break;
|
||||
case 'b':
|
||||
result = '\b';
|
||||
break;
|
||||
case 'r':
|
||||
result = '\r';
|
||||
break;
|
||||
case 'f':
|
||||
result = '\f';
|
||||
break;
|
||||
case 'a':
|
||||
result = '\007';
|
||||
break;
|
||||
case 'x':
|
||||
result = 0;
|
||||
ch = token_next_char(self);
|
||||
while ( (ch - '0')<16u )
|
||||
{
|
||||
result = result*16 + ch - '0';
|
||||
ch = token_next_char(self);
|
||||
}
|
||||
token_prev_char(self);
|
||||
break;
|
||||
default:
|
||||
if ( (ch - '0') < 8u)
|
||||
{
|
||||
result = 0;
|
||||
while ( (ch - '0') < 8u )
|
||||
{
|
||||
result = result*8 + ch - '0';
|
||||
ch = token_next_char(self);
|
||||
}
|
||||
|
||||
token_prev_char(self);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
(0|0x|0X|0b|0B)number+(l|L)
|
||||
*/
|
||||
static void token_proc_number(struct finsh_token* self)
|
||||
{
|
||||
char ch;
|
||||
char *p, buf[128];
|
||||
long value;
|
||||
|
||||
value = 0;
|
||||
p = buf;
|
||||
|
||||
ch = token_next_char(self);
|
||||
if ( ch == '0' )
|
||||
{
|
||||
int b;
|
||||
ch = token_next_char(self);
|
||||
if ( ch == 'x' || ch == 'X' )/*it's a hex number*/
|
||||
{
|
||||
b = 16;
|
||||
ch = token_next_char(self);
|
||||
while ( is_digit(ch) || is_alpha(ch) )
|
||||
{
|
||||
*p++ = ch;
|
||||
ch = token_next_char(self);
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
}
|
||||
else if ( ch == 'b' || ch == 'B' )
|
||||
{
|
||||
b = 2;
|
||||
ch = token_next_char(self);
|
||||
while ( (ch=='0')||(ch=='1') )
|
||||
{
|
||||
*p++ = ch;
|
||||
ch = token_next_char(self);
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
}
|
||||
else
|
||||
{
|
||||
b = 8;
|
||||
while ( is_digit(ch) )
|
||||
{
|
||||
*p++ = ch;
|
||||
ch = token_next_char(self);
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
self->value.int_value = token_spec_number(buf, strlen(buf), b);
|
||||
self->current_token = finsh_token_type_value_int;
|
||||
}
|
||||
else
|
||||
{
|
||||
while ( is_digit(ch) )
|
||||
{
|
||||
value = value*10 + ( ch - '0' );
|
||||
ch = token_next_char(self);
|
||||
}
|
||||
|
||||
self->value.int_value = value;
|
||||
self->current_token = finsh_token_type_value_int;
|
||||
}
|
||||
|
||||
switch ( ch )
|
||||
{
|
||||
case 'l':
|
||||
case 'L':
|
||||
self->current_token = finsh_token_type_value_long;
|
||||
break;
|
||||
|
||||
default:
|
||||
token_prev_char(self);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*use 64 bit number*/
|
||||
#define BN_SIZE 2
|
||||
|
||||
static long token_spec_number(char* string, int length, int b)
|
||||
{
|
||||
char* p;
|
||||
int t;
|
||||
int i, j, shift=1;
|
||||
unsigned int bn[BN_SIZE], v;
|
||||
long d;
|
||||
|
||||
p = string;
|
||||
i = 0;
|
||||
|
||||
switch ( b )
|
||||
{
|
||||
case 16: shift = 4;
|
||||
break;
|
||||
case 8: shift = 3;
|
||||
break;
|
||||
case 2: shift = 1;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
for ( j=0; j<BN_SIZE ; j++) bn[j] = 0;
|
||||
|
||||
while ( i<length )
|
||||
{
|
||||
t = *p++;
|
||||
if ( t>='a' && t <='f' )
|
||||
{
|
||||
t = t - 'a' +10;
|
||||
}
|
||||
else if ( t >='A' && t <='F' )
|
||||
{
|
||||
t = t - 'A' +10;
|
||||
}
|
||||
else t = t - '0';
|
||||
|
||||
for ( j=0; j<BN_SIZE ; j++)
|
||||
{
|
||||
v = bn[j];
|
||||
bn[j] = (v<<shift) | t;
|
||||
t = v >> (32 - shift);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
d = (long)bn[0];
|
||||
|
||||
return d;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* token lex for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_TOKEN_H__
|
||||
#define __FINSH_TOKEN_H__
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
enum finsh_token_type
|
||||
{
|
||||
finsh_token_type_left_paren = 1, /* ( */
|
||||
finsh_token_type_right_paren , /* ) */
|
||||
finsh_token_type_comma , /* , */
|
||||
finsh_token_type_semicolon , /* ; */
|
||||
finsh_token_type_mul , /* * */
|
||||
finsh_token_type_add , /* + */
|
||||
finsh_token_type_inc , /* ++ */
|
||||
finsh_token_type_sub , /* - */
|
||||
finsh_token_type_dec , /* -- */
|
||||
finsh_token_type_div , /* / */
|
||||
finsh_token_type_mod , /* % */
|
||||
finsh_token_type_assign , /* = */
|
||||
finsh_token_type_and, /* & */
|
||||
finsh_token_type_or, /* | */
|
||||
finsh_token_type_xor, /* ^ */
|
||||
finsh_token_type_bitwise, /* ~ */
|
||||
finsh_token_type_shl, /* << */
|
||||
finsh_token_type_shr, /* >> */
|
||||
finsh_token_type_comments, /* // */
|
||||
/*-- data type --*/
|
||||
finsh_token_type_void, /* void */
|
||||
finsh_token_type_char, /* char */
|
||||
finsh_token_type_short, /* short */
|
||||
finsh_token_type_int, /* int */
|
||||
finsh_token_type_long, /* long */
|
||||
finsh_token_type_unsigned, /* unsigned */
|
||||
/* data value type */
|
||||
finsh_token_type_value_char, /* v:char */
|
||||
finsh_token_type_value_int, /* v:int */
|
||||
finsh_token_type_value_long, /* v:long */
|
||||
finsh_token_type_value_string, /* v:string */
|
||||
finsh_token_type_value_null, /* NULL */
|
||||
/*-- others --*/
|
||||
finsh_token_type_identifier, /* ID */
|
||||
finsh_token_type_bad, /* bad token */
|
||||
finsh_token_type_eof
|
||||
};
|
||||
|
||||
#define finsh_token_position(self) (self)->position
|
||||
#define finsh_token_replay(self) (self)->replay = 1
|
||||
|
||||
void finsh_token_init(struct finsh_token* self, u_char* script);
|
||||
|
||||
enum finsh_token_type finsh_token_token(struct finsh_token* self);
|
||||
void finsh_token_get_token(struct finsh_token* self, u_char* token);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Variable implementation in finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
* 2012-04-27 Bernard fixed finsh_var_delete issue which
|
||||
* is found by Grissiom.
|
||||
*/
|
||||
#include <finsh.h>
|
||||
#include "finsh_var.h"
|
||||
|
||||
struct finsh_var global_variable[FINSH_VARIABLE_MAX];
|
||||
struct finsh_sysvar_item* global_sysvar_list;
|
||||
|
||||
int finsh_var_init()
|
||||
{
|
||||
memset(global_variable, 0, sizeof(global_variable));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int finsh_var_insert(const char* name, int type)
|
||||
{
|
||||
int i, empty;
|
||||
|
||||
empty = -1;
|
||||
for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
|
||||
{
|
||||
/* there is a same name variable exist. */
|
||||
if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
|
||||
return -1;
|
||||
|
||||
if (global_variable[i].type == finsh_type_unknown && empty == -1)
|
||||
{
|
||||
empty = i;
|
||||
}
|
||||
}
|
||||
|
||||
/* there is no empty entry */
|
||||
if (empty == -1) return -1;
|
||||
|
||||
/* insert entry */
|
||||
strncpy(global_variable[empty].name, name, FINSH_NAME_MAX);
|
||||
global_variable[empty].type = type;
|
||||
|
||||
/* return the offset */
|
||||
return empty;
|
||||
}
|
||||
|
||||
int finsh_var_delete(const char* name)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
|
||||
{
|
||||
if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
/* can't find variable */
|
||||
if (i == FINSH_VARIABLE_MAX) return -1;
|
||||
|
||||
memset(&global_variable[i], 0, sizeof(struct finsh_var));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct finsh_var* finsh_var_lookup(const char* name)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
|
||||
{
|
||||
if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
/* can't find variable */
|
||||
if (i == FINSH_VARIABLE_MAX) return NULL;
|
||||
|
||||
return &global_variable[i];
|
||||
}
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
void finsh_sysvar_append(const char* name, u_char type, void* var_addr)
|
||||
{
|
||||
/* create a sysvar */
|
||||
struct finsh_sysvar_item* item;
|
||||
|
||||
item = (struct finsh_sysvar_item*) rt_malloc (sizeof(struct finsh_sysvar_item));
|
||||
if (item != NULL)
|
||||
{
|
||||
item->next = NULL;
|
||||
item->sysvar.name = rt_strdup(name);
|
||||
item->sysvar.type = type;
|
||||
item->sysvar.var = var_addr;
|
||||
|
||||
if (global_sysvar_list == NULL)
|
||||
{
|
||||
global_sysvar_list = item;
|
||||
}
|
||||
else
|
||||
{
|
||||
item->next = global_sysvar_list;
|
||||
global_sysvar_list = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
struct finsh_sysvar* finsh_sysvar_lookup(const char* name)
|
||||
{
|
||||
struct finsh_sysvar* index;
|
||||
struct finsh_sysvar_item* item;
|
||||
|
||||
for (index = _sysvar_table_begin;
|
||||
index < _sysvar_table_end;
|
||||
FINSH_NEXT_SYSVAR(index))
|
||||
{
|
||||
if (strcmp(index->name, name) == 0)
|
||||
return index;
|
||||
}
|
||||
|
||||
/* find in sysvar list */
|
||||
item = global_sysvar_list;
|
||||
while (item != NULL)
|
||||
{
|
||||
if (strncmp(item->sysvar.name, name, strlen(name)) == 0)
|
||||
{
|
||||
return &(item->sysvar);
|
||||
}
|
||||
|
||||
/* move to next item */
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
/* can't find variable */
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Variable implementation in finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_VAR_H__
|
||||
#define __FINSH_VAR_H__
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
/*
|
||||
* The variable in finsh is put in data segment as a global variable.
|
||||
* The 'finsh_var' structure presents the structure of variable in data segment.
|
||||
*/
|
||||
struct finsh_var
|
||||
{
|
||||
char name[FINSH_NAME_MAX + 1]; /* the name of variable */
|
||||
|
||||
u_char type; /* the type of variable */
|
||||
|
||||
/* variable value */
|
||||
union {
|
||||
char char_value;
|
||||
short short_value;
|
||||
int int_value;
|
||||
long long_value;
|
||||
void* ptr;
|
||||
}value;
|
||||
};
|
||||
extern struct finsh_var global_variable[];
|
||||
|
||||
int finsh_var_init(void);
|
||||
int finsh_var_insert(const char* name, int type);
|
||||
int finsh_var_delete(const char* name);
|
||||
struct finsh_var* finsh_var_lookup(const char* name);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,404 @@
|
||||
/*
|
||||
* Virtual machine of finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include <finsh.h>
|
||||
|
||||
#include "finsh_vm.h"
|
||||
#include "finsh_ops.h"
|
||||
#include "finsh_var.h"
|
||||
|
||||
/* stack */
|
||||
union finsh_value finsh_vm_stack[FINSH_STACK_MAX];
|
||||
/* text segment */
|
||||
u_char text_segment[FINSH_TEXT_MAX];
|
||||
|
||||
union finsh_value* finsh_sp; /* stack pointer */
|
||||
u_char* finsh_pc; /* PC */
|
||||
|
||||
/* syscall list, for dynamic system call register */
|
||||
struct finsh_syscall_item* global_syscall_list = NULL;
|
||||
|
||||
// #define FINSH_VM_DISASSEMBLE
|
||||
void finsh_vm_run()
|
||||
{
|
||||
u_char op;
|
||||
|
||||
/* if you want to disassemble the byte code, please define FINSH_VM_DISASSEMBLE */
|
||||
#ifdef FINSH_VM_DISASSEMBLE
|
||||
void finsh_disassemble();
|
||||
finsh_disassemble();
|
||||
#endif
|
||||
|
||||
/* set sp(stack pointer) to the beginning of stack */
|
||||
finsh_sp = &finsh_vm_stack[0];
|
||||
|
||||
/* set pc to the beginning of text segment */
|
||||
finsh_pc = &text_segment[0];
|
||||
|
||||
while ((finsh_pc - &text_segment[0] >= 0) &&
|
||||
(finsh_pc - &text_segment[0] < FINSH_TEXT_MAX))
|
||||
{
|
||||
/* get op */
|
||||
op = *finsh_pc++;
|
||||
|
||||
/* call op function */
|
||||
op_table[op]();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
void finsh_syscall_append(const char* name, syscall_func func)
|
||||
{
|
||||
/* create the syscall */
|
||||
struct finsh_syscall_item* item;
|
||||
|
||||
item = (struct finsh_syscall_item*)rt_malloc(sizeof(struct finsh_syscall_item));
|
||||
if (item != RT_NULL)
|
||||
{
|
||||
item->next = NULL;
|
||||
item->syscall.name = rt_strdup(name);
|
||||
item->syscall.func = func;
|
||||
|
||||
if (global_syscall_list == NULL)
|
||||
{
|
||||
global_syscall_list = item;
|
||||
}
|
||||
else
|
||||
{
|
||||
item->next = global_syscall_list;
|
||||
global_syscall_list = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
|
||||
struct finsh_syscall* finsh_syscall_next(struct finsh_syscall* call)
|
||||
{
|
||||
unsigned int *ptr;
|
||||
ptr = (unsigned int*) (call + 1);
|
||||
while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _syscall_table_end))
|
||||
ptr ++;
|
||||
|
||||
return (struct finsh_syscall*)ptr;
|
||||
}
|
||||
|
||||
struct finsh_sysvar* finsh_sysvar_next(struct finsh_sysvar* call)
|
||||
{
|
||||
unsigned int *ptr;
|
||||
ptr = (unsigned int*) (call + 1);
|
||||
while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _sysvar_table_end))
|
||||
ptr ++;
|
||||
|
||||
return (struct finsh_sysvar*)ptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct finsh_syscall* finsh_syscall_lookup(const char* name)
|
||||
{
|
||||
struct finsh_syscall* index;
|
||||
struct finsh_syscall_item* item;
|
||||
|
||||
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
if (strcmp(index->name, name) == 0)
|
||||
return index;
|
||||
}
|
||||
|
||||
/* find on syscall list */
|
||||
item = global_syscall_list;
|
||||
while (item != NULL)
|
||||
{
|
||||
if (strncmp(item->syscall.name, name, strlen(name)) == 0)
|
||||
{
|
||||
return &(item->syscall);
|
||||
}
|
||||
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef FINSH_VM_DISASSEMBLE
|
||||
void finsh_disassemble()
|
||||
{
|
||||
u_char *pc, op;
|
||||
|
||||
pc = &text_segment[0];
|
||||
while (*pc != 0)
|
||||
{
|
||||
op = *pc;
|
||||
switch (op)
|
||||
{
|
||||
case FINSH_OP_ADD_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("addb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SUB_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("subb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_DIV_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("divb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_MOD_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("modb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_MUL_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("mulb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_AND_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("andb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_OR_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("orb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_XOR_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("xorb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_BITWISE_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("bwb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SHL_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("shlb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SHR_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("shrb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("ldb %d\n", *pc++);
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_VALUE_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("ldb [0x%x]\n", FINSH_GET32(pc));
|
||||
pc += 4;
|
||||
break;
|
||||
|
||||
case FINSH_OP_ST_BYTE:
|
||||
pc ++;
|
||||
rt_kprintf("stb\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_ADD_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("addw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SUB_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("subw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_DIV_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("divw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_MOD_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("modw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_MUL_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("mulw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_AND_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("andw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_OR_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("orw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_XOR_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("xorw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_BITWISE_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("bww\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SHL_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("shlw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SHR_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("shrw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("ldw %d\n", FINSH_GET16(pc));
|
||||
pc += 2;
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_VALUE_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("ldw [0x%x]\n", FINSH_GET32(pc));
|
||||
pc += 4;
|
||||
break;
|
||||
|
||||
case FINSH_OP_ST_WORD:
|
||||
pc ++;
|
||||
rt_kprintf("stw\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_ADD_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("addd\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SUB_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("subd\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_DIV_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("divd\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_MOD_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("modd\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_MUL_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("muld\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_AND_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("andd\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_OR_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("ord\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_XOR_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("xord\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_BITWISE_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("bwd\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SHL_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("shld\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_SHR_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("shrd\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("ldd 0x%x\n", FINSH_GET32(pc));
|
||||
pc += 4;
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_VALUE_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("ldd [0x%x]\n", FINSH_GET32(pc));
|
||||
pc += 4;
|
||||
break;
|
||||
|
||||
case FINSH_OP_ST_DWORD:
|
||||
pc ++;
|
||||
rt_kprintf("std\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_POP:
|
||||
rt_kprintf("pop\n");
|
||||
pc ++;
|
||||
break;
|
||||
|
||||
case FINSH_OP_SYSCALL:
|
||||
pc ++;
|
||||
rt_kprintf("syscall %d\n", *pc++);
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_VALUE_BYTE_STACK:
|
||||
pc ++;
|
||||
rt_kprintf("ldb [sp]\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_VALUE_WORD_STACK:
|
||||
pc ++;
|
||||
rt_kprintf("ldw [sp]\n");
|
||||
break;
|
||||
|
||||
case FINSH_OP_LD_VALUE_DWORD_STACK:
|
||||
pc ++;
|
||||
rt_kprintf("ldd [sp]\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Virtual machine finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_VM_H__
|
||||
#define __FINSH_VM_H__
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
#include "finsh_var.h"
|
||||
|
||||
union finsh_value {
|
||||
char char_value;
|
||||
short short_value;
|
||||
long long_value;
|
||||
void* ptr;
|
||||
};
|
||||
|
||||
extern union finsh_value* finsh_sp; /* stack pointer */
|
||||
extern u_char* finsh_pc; /* PC */
|
||||
|
||||
/* stack */
|
||||
extern union finsh_value finsh_vm_stack[FINSH_STACK_MAX];
|
||||
/* text segment */
|
||||
extern u_char text_segment[FINSH_TEXT_MAX];
|
||||
|
||||
void finsh_vm_run(void);
|
||||
//void finsh_disassemble(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,547 @@
|
||||
/*
|
||||
* RT-Thread module shell implementation.
|
||||
*
|
||||
* COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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-03-30 Bernard the first verion for finsh
|
||||
* 2014-01-03 Bernard msh can execute module.
|
||||
*/
|
||||
|
||||
#include "msh.h"
|
||||
#include <finsh.h>
|
||||
#include <shell.h>
|
||||
|
||||
#ifdef RT_USING_DFS
|
||||
#include <dfs_posix.h>
|
||||
#endif
|
||||
|
||||
#define RT_FINSH_ARG_MAX 10
|
||||
typedef int (*cmd_function_t)(int argc, char** argv);
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
#ifdef FINSH_USING_MSH_ONLY
|
||||
rt_bool_t msh_is_used(void)
|
||||
{
|
||||
return RT_TRUE;
|
||||
}
|
||||
#else
|
||||
#ifdef FINSH_USING_MSH_DEFAULT
|
||||
static rt_bool_t __msh_state = RT_TRUE;
|
||||
#else
|
||||
static rt_bool_t __msh_state = RT_FALSE;
|
||||
#endif
|
||||
rt_bool_t msh_is_used(void)
|
||||
{
|
||||
return __msh_state;
|
||||
}
|
||||
|
||||
static int msh_exit(int argc, char** argv)
|
||||
{
|
||||
/* return to finsh shell mode */
|
||||
__msh_state = RT_FALSE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(msh_exit, __cmd_exit, return to RT-Thread shell mode.);
|
||||
|
||||
static int msh_enter(void)
|
||||
{
|
||||
/* enter module shell mode */
|
||||
__msh_state = RT_TRUE;
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(msh_enter, msh, use module shell);
|
||||
#endif
|
||||
|
||||
int msh_help(int argc, char** argv)
|
||||
{
|
||||
rt_kprintf("RT-Thread shell commands:\n");
|
||||
{
|
||||
struct finsh_syscall *index;
|
||||
|
||||
for (index = _syscall_table_begin;
|
||||
index < _syscall_table_end;
|
||||
FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
if (strncmp(index->name, "__cmd_", 6) != 0) continue;
|
||||
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
|
||||
rt_kprintf("%-16s - %s\n", &index->name[6], index->desc);
|
||||
#else
|
||||
rt_kprintf("%s ", &index->name[6]);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(msh_help, __cmd_help, RT-Thread shell help.);
|
||||
|
||||
static int msh_split(char* cmd, rt_size_t length, char* argv[RT_FINSH_ARG_MAX])
|
||||
{
|
||||
char *ptr;
|
||||
rt_size_t position;
|
||||
rt_size_t argc;
|
||||
|
||||
ptr = cmd;
|
||||
position = 0; argc = 0;
|
||||
|
||||
while (position < length)
|
||||
{
|
||||
/* strip bank and tab */
|
||||
while ((*ptr == ' ' || *ptr == '\t') && position < length)
|
||||
{
|
||||
*ptr = '\0';
|
||||
ptr ++; position ++;
|
||||
}
|
||||
if (position >= length) break;
|
||||
|
||||
/* handle string */
|
||||
if (*ptr == '"')
|
||||
{
|
||||
ptr ++; position ++;
|
||||
argv[argc] = ptr; argc ++;
|
||||
|
||||
/* skip this string */
|
||||
while (*ptr != '"' && position < length)
|
||||
{
|
||||
if (*ptr == '\\')
|
||||
{
|
||||
if (*(ptr + 1) == '"')
|
||||
{
|
||||
ptr ++; position ++;
|
||||
}
|
||||
}
|
||||
ptr ++; position ++;
|
||||
}
|
||||
if (position >= length) break;
|
||||
|
||||
/* skip '"' */
|
||||
*ptr = '\0'; ptr ++; position ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
argv[argc] = ptr;
|
||||
argc ++;
|
||||
while ((*ptr != ' ' && *ptr != '\t') && position < length)
|
||||
{
|
||||
ptr ++; position ++;
|
||||
}
|
||||
if (position >= length) break;
|
||||
}
|
||||
}
|
||||
|
||||
return argc;
|
||||
}
|
||||
|
||||
static cmd_function_t msh_get_cmd(char *cmd, int size)
|
||||
{
|
||||
struct finsh_syscall *index;
|
||||
cmd_function_t cmd_func = RT_NULL;
|
||||
|
||||
for (index = _syscall_table_begin;
|
||||
index < _syscall_table_end;
|
||||
FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
if (strncmp(index->name, "__cmd_", 6) != 0) continue;
|
||||
|
||||
if (strncmp(&index->name[6], cmd, size) == 0 &&
|
||||
index->name[6 + size] == '\0')
|
||||
{
|
||||
cmd_func = (cmd_function_t)index->func;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return cmd_func;
|
||||
}
|
||||
|
||||
#if defined(RT_USING_MODULE) && defined(RT_USING_DFS)
|
||||
/* Return 0 on module executed. Other value indicate error.
|
||||
*/
|
||||
int msh_exec_module(const char* cmd_line, int size)
|
||||
{
|
||||
int ret;
|
||||
int fd = -1;
|
||||
char *pg_name;
|
||||
int length, cmd_length = 0;
|
||||
|
||||
if (size == 0)
|
||||
return -RT_ERROR;
|
||||
/* get the length of command0 */
|
||||
while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
|
||||
cmd_length ++;
|
||||
|
||||
/* get name length */
|
||||
length = cmd_length + 32;
|
||||
|
||||
/* allocate program name memory */
|
||||
pg_name = (char*) rt_malloc(length);
|
||||
if (pg_name == RT_NULL)
|
||||
return -RT_ENOMEM;
|
||||
|
||||
/* copy command0 */
|
||||
memcpy(pg_name, cmd_line, cmd_length);
|
||||
pg_name[cmd_length] = '\0';
|
||||
|
||||
if (strstr(pg_name, ".mo") != RT_NULL || strstr(pg_name, ".MO") != RT_NULL)
|
||||
{
|
||||
/* try to open program */
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
|
||||
/* search in /bin path */
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* add .mo and open program */
|
||||
|
||||
/* try to open program */
|
||||
strcat(pg_name, ".mo");
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
|
||||
/* search in /bin path */
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_snprintf(pg_name, length - 1, "/bin/%.*s.mo", cmd_length, cmd_line);
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (fd >= 0)
|
||||
{
|
||||
/* found program */
|
||||
close(fd);
|
||||
rt_module_exec_cmd(pg_name, cmd_line, size);
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
rt_free(pg_name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int system(const char *command)
|
||||
{
|
||||
return msh_exec_module(command, rt_strlen(command));
|
||||
}
|
||||
#endif
|
||||
|
||||
static int _msh_exec_cmd(char* cmd, rt_size_t length, int *retp)
|
||||
{
|
||||
int argc;
|
||||
int cmd0_size = 0;
|
||||
cmd_function_t cmd_func;
|
||||
char *argv[RT_FINSH_ARG_MAX];
|
||||
|
||||
RT_ASSERT(cmd);
|
||||
RT_ASSERT(retp);
|
||||
|
||||
/* find the size of first command */
|
||||
while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
|
||||
cmd0_size ++;
|
||||
if (cmd0_size == 0)
|
||||
return -RT_ERROR;
|
||||
|
||||
cmd_func = msh_get_cmd(cmd, cmd0_size);
|
||||
if (cmd_func == RT_NULL)
|
||||
return -RT_ERROR;
|
||||
|
||||
/* split arguments */
|
||||
memset(argv, 0x00, sizeof(argv));
|
||||
argc = msh_split(cmd, length, argv);
|
||||
if (argc == 0)
|
||||
return -RT_ERROR;
|
||||
|
||||
/* exec this command */
|
||||
*retp = cmd_func(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int msh_exec(char* cmd, rt_size_t length)
|
||||
{
|
||||
int cmd_ret;
|
||||
|
||||
/* strim the beginning of command */
|
||||
while(*cmd == ' ' || *cmd == '\t')
|
||||
{
|
||||
cmd++;
|
||||
length--;
|
||||
}
|
||||
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
||||
/* Exec sequence:
|
||||
* 1. built-in command
|
||||
* 2. module(if enabled)
|
||||
* 3. chdir to the directry(if possible)
|
||||
*/
|
||||
if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
|
||||
{
|
||||
return cmd_ret;
|
||||
}
|
||||
#ifdef RT_USING_MODULE
|
||||
if (msh_exec_module(cmd, length) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#ifdef DFS_USING_WORKDIR
|
||||
if (chdir(cmd) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/* truncate the cmd at the first space. */
|
||||
{
|
||||
char *tcmd;
|
||||
tcmd = cmd;
|
||||
while(*tcmd != ' ' && *tcmd != '\0')
|
||||
{
|
||||
tcmd++;
|
||||
}
|
||||
*tcmd = '\0';
|
||||
}
|
||||
rt_kprintf("%s: command not found.\n", cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int str_common(const char *str1, const char *str2)
|
||||
{
|
||||
const char *str = str1;
|
||||
|
||||
while ((*str != 0) && (*str2 != 0) && (*str == *str2))
|
||||
{
|
||||
str ++;
|
||||
str2 ++;
|
||||
}
|
||||
|
||||
return (str - str1);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DFS
|
||||
void msh_auto_complete_path(char *path)
|
||||
{
|
||||
DIR* dir = RT_NULL;
|
||||
struct dirent *dirent = RT_NULL;
|
||||
char *full_path, *ptr, *index;
|
||||
|
||||
full_path = (char*)rt_malloc(256);
|
||||
if (full_path == RT_NULL) return; /* out of memory */
|
||||
|
||||
ptr = full_path;
|
||||
if (*path != '/')
|
||||
{
|
||||
getcwd(full_path, 256);
|
||||
if (full_path[rt_strlen(full_path) - 1] != '/')
|
||||
strcat(full_path, "/");
|
||||
}
|
||||
else *full_path = '\0';
|
||||
|
||||
index = RT_NULL; ptr = path;
|
||||
for (;;)
|
||||
{
|
||||
if (*ptr == '/') index = ptr + 1; if (!*ptr) break; ptr ++;
|
||||
}
|
||||
if (index == RT_NULL) index = path;
|
||||
|
||||
if (index != RT_NULL)
|
||||
{
|
||||
char *dest = index;
|
||||
|
||||
/* fill the parent path */
|
||||
ptr = full_path;
|
||||
while (*ptr) ptr ++;
|
||||
|
||||
for (index = path; index != dest;)
|
||||
*ptr++ = *index++;
|
||||
*ptr = '\0';
|
||||
|
||||
dir = opendir(full_path);
|
||||
if (dir == RT_NULL) /* open directory failed! */
|
||||
{
|
||||
rt_free(full_path);
|
||||
return;
|
||||
}
|
||||
|
||||
/* restore the index position */
|
||||
index = dest;
|
||||
}
|
||||
|
||||
/* auto complete the file or directory name */
|
||||
if (*index == '\0') /* display all of files and directories */
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
|
||||
rt_kprintf("%s\n", dirent->d_name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int length, min_length;
|
||||
|
||||
min_length = 0;
|
||||
for (;;)
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
|
||||
/* matched the prefix string */
|
||||
if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
|
||||
{
|
||||
if (min_length == 0)
|
||||
{
|
||||
min_length = rt_strlen(dirent->d_name);
|
||||
/* save dirent name */
|
||||
strcpy(full_path, dirent->d_name);
|
||||
}
|
||||
|
||||
length = str_common(dirent->d_name, full_path);
|
||||
|
||||
if (length < min_length)
|
||||
{
|
||||
min_length = length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (min_length)
|
||||
{
|
||||
if (min_length < rt_strlen(full_path))
|
||||
{
|
||||
/* list the candidate */
|
||||
rewinddir(dir);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
|
||||
if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
|
||||
rt_kprintf("%s\n", dirent->d_name);
|
||||
}
|
||||
}
|
||||
|
||||
length = index - path;
|
||||
memcpy(index, full_path, min_length);
|
||||
path[length + min_length] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
rt_free(full_path);
|
||||
}
|
||||
#endif
|
||||
|
||||
void msh_auto_complete(char *prefix)
|
||||
{
|
||||
int length, min_length;
|
||||
const char *name_ptr, *cmd_name;
|
||||
struct finsh_syscall *index;
|
||||
|
||||
min_length = 0;
|
||||
name_ptr = RT_NULL;
|
||||
|
||||
if (*prefix == '\0')
|
||||
{
|
||||
msh_help(0, RT_NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DFS
|
||||
/* check whether a spare in the command */
|
||||
{
|
||||
char *ptr;
|
||||
|
||||
ptr = prefix + rt_strlen(prefix);
|
||||
while (ptr != prefix)
|
||||
{
|
||||
if (*ptr == ' ')
|
||||
{
|
||||
msh_auto_complete_path(ptr + 1);
|
||||
break;
|
||||
}
|
||||
|
||||
ptr --;
|
||||
}
|
||||
#ifdef RT_USING_MODULE
|
||||
/* There is a chance that the user want to run the module directly. So
|
||||
* try to complete the file names. If the completed path is not a
|
||||
* module, the system won't crash anyway. */
|
||||
if (ptr == prefix)
|
||||
{
|
||||
msh_auto_complete_path(ptr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
/* checks in internal command */
|
||||
{
|
||||
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
/* skip finsh shell function */
|
||||
if (strncmp(index->name, "__cmd_", 6) != 0) continue;
|
||||
|
||||
cmd_name = (const char*) &index->name[6];
|
||||
if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
|
||||
{
|
||||
if (min_length == 0)
|
||||
{
|
||||
/* set name_ptr */
|
||||
name_ptr = cmd_name;
|
||||
/* set initial length */
|
||||
min_length = strlen(name_ptr);
|
||||
}
|
||||
|
||||
length = str_common(name_ptr, cmd_name);
|
||||
if (length < min_length)
|
||||
min_length = length;
|
||||
|
||||
rt_kprintf("%s\n", cmd_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* auto complete string */
|
||||
if (name_ptr != NULL)
|
||||
{
|
||||
rt_strncpy(prefix, name_ptr, min_length);
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* RT-Thread module shell implementation.
|
||||
*
|
||||
* COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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-03-30 Bernard the first verion for FinSH
|
||||
*/
|
||||
|
||||
#ifndef __M_SHELL__
|
||||
#define __M_SHELL__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
rt_bool_t msh_is_used(void);
|
||||
int msh_exec(char* cmd, rt_size_t length);
|
||||
void msh_auto_complete(char *prefix);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
* internal commands for RT-Thread module shell
|
||||
*
|
||||
* COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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-03-30 Bernard the first verion for FinSH
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <finsh.h>
|
||||
|
||||
#include "msh.h"
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
#ifdef RT_USING_DFS
|
||||
#include <dfs_posix.h>
|
||||
|
||||
#ifdef DFS_USING_WORKDIR
|
||||
extern char working_directory[];
|
||||
#endif
|
||||
|
||||
int cmd_ls(int argc, char** argv)
|
||||
{
|
||||
extern void ls(const char *pathname);
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
#ifdef DFS_USING_WORKDIR
|
||||
ls(working_directory);
|
||||
#else
|
||||
ls("/");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
ls(argv[1]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ls, __cmd_ls, List information about the FILEs.);
|
||||
|
||||
int cmd_cp(int argc, char** argv)
|
||||
{
|
||||
void copy(const char *src, const char *dst);
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
rt_kprintf("Usage: cp SOURCE DEST\n");
|
||||
rt_kprintf("Copy SOURCE to DEST.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
copy(argv[1], argv[2]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cp, __cmd_cp, Copy SOURCE to DEST.);
|
||||
|
||||
int cmd_mv(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
rt_kprintf("Usage: mv SOURCE DEST\n");
|
||||
rt_kprintf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%s => %s\n", argv[1], argv[2]);
|
||||
rename(argv[1], argv[2]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mv, __cmd_mv, Rename SOURCE to DEST.);
|
||||
|
||||
int cmd_cat(int argc, char** argv)
|
||||
{
|
||||
int index;
|
||||
extern void cat(const char* filename);
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
rt_kprintf("Usage: cat [FILE]...\n");
|
||||
rt_kprintf("Concatenate FILE(s)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (index = 1; index < argc; index ++)
|
||||
{
|
||||
cat(argv[index]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s));
|
||||
|
||||
int cmd_rm(int argc, char** argv)
|
||||
{
|
||||
int index;
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
rt_kprintf("Usage: rm FILE...\n");
|
||||
rt_kprintf("Remove (unlink) the FILE(s).\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (index = 1; index < argc; index ++)
|
||||
{
|
||||
unlink(argv[index]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove (unlink) the FILE(s).);
|
||||
|
||||
#ifdef DFS_USING_WORKDIR
|
||||
int cmd_cd(int argc, char** argv)
|
||||
{
|
||||
if (argc == 1)
|
||||
{
|
||||
rt_kprintf("%s\n", working_directory);
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
chdir(argv[1]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cd, __cmd_cd, Change the shell working directory.);
|
||||
|
||||
int cmd_pwd(int argc, char** argv)
|
||||
{
|
||||
rt_kprintf("%s\n", working_directory);
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_pwd, __cmd_pwd, Print the name of the current working directory.);
|
||||
#endif
|
||||
|
||||
int cmd_mkdir(int argc, char** argv)
|
||||
{
|
||||
if (argc == 1)
|
||||
{
|
||||
rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n");
|
||||
rt_kprintf("Create the DIRECTORY, if they do not already exist.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
mkdir(argv[1], 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkdir, __cmd_mkdir, Create the DIRECTORY.);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_LWIP
|
||||
int cmd_ifconfig(int argc, char** argv)
|
||||
{
|
||||
extern void list_if(void);
|
||||
|
||||
list_if();
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ifconfig, __cmd_ifconfig, list the information of network interfaces);
|
||||
|
||||
#endif
|
||||
|
||||
int cmd_ps(int argc, char** argv)
|
||||
{
|
||||
extern long list_thread(void);
|
||||
|
||||
list_thread();
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system.);
|
||||
|
||||
int cmd_time(int argc, char** argv)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_time, __cmd_time, Execute command with time.);
|
||||
|
||||
int cmd_free(int argc, char** argv)
|
||||
{
|
||||
extern void list_mem(void);
|
||||
|
||||
list_mem();
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_free, __cmd_free, Show the memory usage in the system.);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,662 @@
|
||||
/*
|
||||
* shell implementation for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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-04-30 Bernard the first version for FinSH
|
||||
* 2006-05-08 Bernard change finsh thread stack to 2048
|
||||
* 2006-06-03 Bernard add support for skyeye
|
||||
* 2006-09-24 Bernard remove the code related with hardware
|
||||
* 2010-01-18 Bernard fix down then up key bug.
|
||||
* 2010-03-19 Bernard fix backspace issue and fix device read in shell.
|
||||
* 2010-04-01 Bernard add prompt output when start and remove the empty history
|
||||
* 2011-02-23 Bernard fix variable section end issue of finsh shell
|
||||
* initialization when use GNU GCC compiler.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
|
||||
#include "finsh.h"
|
||||
#include "shell.h"
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
#include "msh.h"
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <stdio.h> /* for putchar */
|
||||
#endif
|
||||
|
||||
/* finsh thread */
|
||||
static struct rt_thread finsh_thread;
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
|
||||
struct finsh_shell* shell;
|
||||
|
||||
#if defined(FINSH_USING_MSH) || (defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR))
|
||||
#if defined(RT_USING_DFS)
|
||||
#include <dfs_posix.h>
|
||||
#endif
|
||||
const char* finsh_get_prompt()
|
||||
{
|
||||
#define _MSH_PROMPT "msh "
|
||||
#define _PROMPT "finsh "
|
||||
static char finsh_prompt[RT_CONSOLEBUF_SIZE + 1] = {0};
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
if (msh_is_used()) strcpy(finsh_prompt, _MSH_PROMPT);
|
||||
else
|
||||
#endif
|
||||
strcpy(finsh_prompt, _PROMPT);
|
||||
|
||||
#ifdef DFS_USING_WORKDIR
|
||||
/* get current working directory */
|
||||
getcwd(&finsh_prompt[rt_strlen(finsh_prompt)], RT_CONSOLEBUF_SIZE - rt_strlen(finsh_prompt));
|
||||
#endif
|
||||
strcat(finsh_prompt, ">");
|
||||
|
||||
return finsh_prompt;
|
||||
}
|
||||
#endif
|
||||
|
||||
static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
|
||||
/* release semaphore to let finsh thread rx data */
|
||||
rt_sem_release(&shell->rx_sem);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function sets the input device of finsh shell.
|
||||
*
|
||||
* @param device_name the name of new input device.
|
||||
*/
|
||||
void finsh_set_device(const char* device_name)
|
||||
{
|
||||
rt_device_t dev = RT_NULL;
|
||||
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
dev = rt_device_find(device_name);
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
rt_kprintf("finsh: can not find device: %s\n", device_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* check whether it's a same device */
|
||||
if (dev == shell->device) return;
|
||||
/* open this device and set the new device in finsh shell */
|
||||
if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX) == RT_EOK)
|
||||
{
|
||||
if (shell->device != RT_NULL)
|
||||
{
|
||||
/* close old finsh device */
|
||||
rt_device_close(shell->device);
|
||||
rt_device_set_rx_indicate(dev, RT_NULL);
|
||||
}
|
||||
|
||||
shell->device = dev;
|
||||
rt_device_set_rx_indicate(dev, finsh_rx_ind);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function returns current finsh shell input device.
|
||||
*
|
||||
* @return the finsh shell input device name is returned.
|
||||
*/
|
||||
const char* finsh_get_device()
|
||||
{
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
return shell->device->parent.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function set the echo mode of finsh shell.
|
||||
*
|
||||
* FINSH_OPTION_ECHO=0x01 is echo mode, other values are none-echo mode.
|
||||
*
|
||||
* @param echo the echo mode
|
||||
*/
|
||||
void finsh_set_echo(rt_uint32_t echo)
|
||||
{
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
shell->echo_mode = (rt_uint8_t)echo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function gets the echo mode of finsh shell.
|
||||
*
|
||||
* @return the echo mode
|
||||
*/
|
||||
rt_uint32_t finsh_get_echo()
|
||||
{
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
|
||||
return shell->echo_mode;
|
||||
}
|
||||
|
||||
static void shell_auto_complete(char* prefix)
|
||||
{
|
||||
|
||||
rt_kprintf("\n");
|
||||
#ifdef FINSH_USING_MSH
|
||||
if (msh_is_used() == RT_TRUE)
|
||||
{
|
||||
msh_auto_complete(prefix);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
extern void list_prefix(char* prefix);
|
||||
list_prefix(prefix);
|
||||
#endif
|
||||
}
|
||||
|
||||
rt_kprintf("%s%s", FINSH_PROMPT, prefix);
|
||||
}
|
||||
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
void finsh_run_line(struct finsh_parser* parser, const char *line)
|
||||
{
|
||||
const char* err_str;
|
||||
|
||||
rt_kprintf("\n");
|
||||
finsh_parser_run(parser, (unsigned char*)line);
|
||||
|
||||
/* compile node root */
|
||||
if (finsh_errno() == 0)
|
||||
{
|
||||
finsh_compiler_run(parser->root);
|
||||
}
|
||||
else
|
||||
{
|
||||
err_str = finsh_error_string(finsh_errno());
|
||||
rt_kprintf("%s\n", err_str);
|
||||
}
|
||||
|
||||
/* run virtual machine */
|
||||
if (finsh_errno() == 0)
|
||||
{
|
||||
char ch;
|
||||
finsh_vm_run();
|
||||
|
||||
ch = (unsigned char)finsh_stack_bottom();
|
||||
if (ch > 0x20 && ch < 0x7e)
|
||||
{
|
||||
rt_kprintf("\t'%c', %d, 0x%08x\n",
|
||||
(unsigned char)finsh_stack_bottom(),
|
||||
(unsigned int)finsh_stack_bottom(),
|
||||
(unsigned int)finsh_stack_bottom());
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("\t%d, 0x%08x\n",
|
||||
(unsigned int)finsh_stack_bottom(),
|
||||
(unsigned int)finsh_stack_bottom());
|
||||
}
|
||||
}
|
||||
|
||||
finsh_flush(parser);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
static rt_bool_t shell_handle_history(struct finsh_shell* shell)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
int i;
|
||||
rt_kprintf("\r");
|
||||
|
||||
for(i=0; i<= 60; i++)
|
||||
putchar(' ');
|
||||
rt_kprintf("\r");
|
||||
|
||||
#else
|
||||
rt_kprintf("\033[2K\r");
|
||||
#endif
|
||||
rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
static void shell_push_history(struct finsh_shell* shell)
|
||||
{
|
||||
if (shell->line_position != 0)
|
||||
{
|
||||
/* push history */
|
||||
if (shell->history_count >= FINSH_HISTORY_LINES)
|
||||
{
|
||||
/* move history */
|
||||
int index;
|
||||
for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
|
||||
{
|
||||
memcpy(&shell->cmd_history[index][0],
|
||||
&shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
|
||||
}
|
||||
memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
|
||||
memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);
|
||||
|
||||
/* it's the maximum history */
|
||||
shell->history_count = FINSH_HISTORY_LINES;
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
|
||||
memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);
|
||||
|
||||
/* increase count and set current history position */
|
||||
shell->history_count ++;
|
||||
}
|
||||
}
|
||||
shell->current_history = shell->history_count;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef RT_USING_HEAP
|
||||
struct finsh_shell _shell;
|
||||
#endif
|
||||
void finsh_thread_entry(void* parameter)
|
||||
{
|
||||
char ch;
|
||||
|
||||
/* normal is echo mode */
|
||||
shell->echo_mode = 1;
|
||||
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
finsh_init(&shell->parser);
|
||||
#endif
|
||||
rt_kprintf(FINSH_PROMPT);
|
||||
|
||||
/* set console device as shell device */
|
||||
if (shell->device == RT_NULL)
|
||||
{
|
||||
#ifdef RT_USING_CONSOLE
|
||||
shell->device = rt_console_get_device();
|
||||
RT_ASSERT(shell->device);
|
||||
rt_device_set_rx_indicate(shell->device, finsh_rx_ind);
|
||||
rt_device_open(shell->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
|
||||
#else
|
||||
RT_ASSERT(shell->device);
|
||||
#endif
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* wait receive */
|
||||
if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue;
|
||||
|
||||
/* read one character from device */
|
||||
while (rt_device_read(shell->device, 0, &ch, 1) == 1)
|
||||
{
|
||||
/*
|
||||
* handle control key
|
||||
* up key : 0x1b 0x5b 0x41
|
||||
* down key: 0x1b 0x5b 0x42
|
||||
* right key:0x1b 0x5b 0x43
|
||||
* left key: 0x1b 0x5b 0x44
|
||||
*/
|
||||
if (ch == 0x1b)
|
||||
{
|
||||
shell->stat = WAIT_SPEC_KEY;
|
||||
continue;
|
||||
}
|
||||
else if (shell->stat == WAIT_SPEC_KEY)
|
||||
{
|
||||
if (ch == 0x5b)
|
||||
{
|
||||
shell->stat = WAIT_FUNC_KEY;
|
||||
continue;
|
||||
}
|
||||
|
||||
shell->stat = WAIT_NORMAL;
|
||||
}
|
||||
else if (shell->stat == WAIT_FUNC_KEY)
|
||||
{
|
||||
shell->stat = WAIT_NORMAL;
|
||||
|
||||
if (ch == 0x41) /* up key */
|
||||
{
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
/* prev history */
|
||||
if (shell->current_history > 0)
|
||||
shell->current_history --;
|
||||
else
|
||||
{
|
||||
shell->current_history = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* copy the history command */
|
||||
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
|
||||
FINSH_CMD_SIZE);
|
||||
shell->line_curpos = shell->line_position = strlen(shell->line);
|
||||
shell_handle_history(shell);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
else if (ch == 0x42) /* down key */
|
||||
{
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
/* next history */
|
||||
if (shell->current_history < shell->history_count - 1)
|
||||
shell->current_history ++;
|
||||
else
|
||||
{
|
||||
/* set to the end of history */
|
||||
if (shell->history_count != 0)
|
||||
shell->current_history = shell->history_count - 1;
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
|
||||
FINSH_CMD_SIZE);
|
||||
shell->line_curpos = shell->line_position = strlen(shell->line);
|
||||
shell_handle_history(shell);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
else if (ch == 0x44) /* left key */
|
||||
{
|
||||
if (shell->line_curpos)
|
||||
{
|
||||
rt_kprintf("\b");
|
||||
shell->line_curpos --;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
else if (ch == 0x43) /* right key */
|
||||
{
|
||||
if (shell->line_curpos < shell->line_position)
|
||||
{
|
||||
rt_kprintf("%c", shell->line[shell->line_curpos]);
|
||||
shell->line_curpos ++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* handle CR key */
|
||||
if (ch == '\r')
|
||||
{
|
||||
char next;
|
||||
|
||||
if (rt_device_read(shell->device, 0, &next, 1) == 1)
|
||||
ch = next;
|
||||
else ch = '\r';
|
||||
}
|
||||
/* handle tab key */
|
||||
else if (ch == '\t')
|
||||
{
|
||||
int i;
|
||||
/* move the cursor to the beginning of line */
|
||||
for (i = 0; i < shell->line_curpos; i++)
|
||||
rt_kprintf("\b");
|
||||
|
||||
/* auto complete */
|
||||
shell_auto_complete(&shell->line[0]);
|
||||
/* re-calculate position */
|
||||
shell->line_curpos = shell->line_position = strlen(shell->line);
|
||||
|
||||
continue;
|
||||
}
|
||||
/* handle backspace key */
|
||||
else if (ch == 0x7f || ch == 0x08)
|
||||
{
|
||||
/* note that shell->line_curpos >= 0 */
|
||||
if (shell->line_curpos == 0)
|
||||
continue;
|
||||
|
||||
shell->line_position--;
|
||||
shell->line_curpos--;
|
||||
|
||||
if (shell->line_position > shell->line_curpos)
|
||||
{
|
||||
int i;
|
||||
|
||||
rt_memmove(&shell->line[shell->line_curpos],
|
||||
&shell->line[shell->line_curpos + 1],
|
||||
shell->line_position - shell->line_curpos);
|
||||
shell->line[shell->line_position] = 0;
|
||||
|
||||
rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
|
||||
|
||||
/* move the cursor to the origin position */
|
||||
for (i = shell->line_curpos; i <= shell->line_position; i++)
|
||||
rt_kprintf("\b");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("\b \b");
|
||||
shell->line[shell->line_position] = 0;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
/* handle end of line, break */
|
||||
if (ch == '\r' || ch == '\n')
|
||||
{
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
shell_push_history(shell);
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
if (msh_is_used() == RT_TRUE)
|
||||
{
|
||||
rt_kprintf("\n");
|
||||
msh_exec(shell->line, shell->line_position);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
/* add ';' and run the command line */
|
||||
shell->line[shell->line_position] = ';';
|
||||
|
||||
if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
|
||||
else rt_kprintf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
rt_kprintf(FINSH_PROMPT);
|
||||
memset(shell->line, 0, sizeof(shell->line));
|
||||
shell->line_curpos = shell->line_position = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
/* it's a large line, discard it */
|
||||
if (shell->line_position >= FINSH_CMD_SIZE)
|
||||
shell->line_position = 0;
|
||||
|
||||
/* normal character */
|
||||
if (shell->line_curpos < shell->line_position)
|
||||
{
|
||||
int i;
|
||||
|
||||
rt_memmove(&shell->line[shell->line_curpos + 1],
|
||||
&shell->line[shell->line_curpos],
|
||||
shell->line_position - shell->line_curpos);
|
||||
shell->line[shell->line_curpos] = ch;
|
||||
if (shell->echo_mode)
|
||||
rt_kprintf("%s", &shell->line[shell->line_curpos]);
|
||||
|
||||
/* move the cursor to new position */
|
||||
for (i = shell->line_curpos; i < shell->line_position; i++)
|
||||
rt_kprintf("\b");
|
||||
}
|
||||
else
|
||||
{
|
||||
shell->line[shell->line_position] = ch;
|
||||
rt_kprintf("%c", ch);
|
||||
}
|
||||
|
||||
ch = 0;
|
||||
shell->line_position ++;
|
||||
shell->line_curpos++;
|
||||
if (shell->line_position >= 80)
|
||||
{
|
||||
/* clear command line */
|
||||
shell->line_position = 0;
|
||||
shell->line_curpos = 0;
|
||||
}
|
||||
} /* end of device read */
|
||||
}
|
||||
}
|
||||
|
||||
void finsh_system_function_init(const void* begin, const void* end)
|
||||
{
|
||||
_syscall_table_begin = (struct finsh_syscall*) begin;
|
||||
_syscall_table_end = (struct finsh_syscall*) end;
|
||||
}
|
||||
|
||||
void finsh_system_var_init(const void* begin, const void* end)
|
||||
{
|
||||
_sysvar_table_begin = (struct finsh_sysvar*) begin;
|
||||
_sysvar_table_end = (struct finsh_sysvar*) end;
|
||||
}
|
||||
|
||||
#if defined(__ICCARM__) /* for IAR compiler */
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
#pragma section="FSymTab"
|
||||
#pragma section="VSymTab"
|
||||
#endif
|
||||
#elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
extern "asm" int __fsymtab_start;
|
||||
extern "asm" int __fsymtab_end;
|
||||
extern "asm" int __vsymtab_start;
|
||||
extern "asm" int __vsymtab_end;
|
||||
#endif
|
||||
#elif defined(_MSC_VER)
|
||||
#pragma section("FSymTab$a", read)
|
||||
const char __fsym_begin_name[] = "__start";
|
||||
const char __fsym_begin_desc[] = "begin of finsh";
|
||||
__declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
|
||||
{
|
||||
__fsym_begin_name,
|
||||
__fsym_begin_desc,
|
||||
NULL
|
||||
};
|
||||
|
||||
#pragma section("FSymTab$z", read)
|
||||
const char __fsym_end_name[] = "__end";
|
||||
const char __fsym_end_desc[] = "end of finsh";
|
||||
__declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
|
||||
{
|
||||
__fsym_end_name,
|
||||
__fsym_end_desc,
|
||||
NULL
|
||||
};
|
||||
#endif
|
||||
|
||||
/*
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function will initialize finsh shell
|
||||
*/
|
||||
int finsh_system_init(void)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
#ifdef __CC_ARM /* ARM C Compiler */
|
||||
extern const int FSymTab$$Base;
|
||||
extern const int FSymTab$$Limit;
|
||||
extern const int VSymTab$$Base;
|
||||
extern const int VSymTab$$Limit;
|
||||
finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
|
||||
#endif
|
||||
#elif defined (__ICCARM__) /* for IAR Compiler */
|
||||
finsh_system_function_init(__section_begin("FSymTab"),
|
||||
__section_end("FSymTab"));
|
||||
finsh_system_var_init(__section_begin("VSymTab"),
|
||||
__section_end("VSymTab"));
|
||||
#elif defined (__GNUC__) || defined(__TI_COMPILER_VERSION__)
|
||||
/* GNU GCC Compiler and TI CCS */
|
||||
extern const int __fsymtab_start;
|
||||
extern const int __fsymtab_end;
|
||||
extern const int __vsymtab_start;
|
||||
extern const int __vsymtab_end;
|
||||
finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
|
||||
finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
|
||||
#elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
|
||||
finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
|
||||
finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
|
||||
#elif defined(_MSC_VER)
|
||||
unsigned int *ptr_begin, *ptr_end;
|
||||
|
||||
ptr_begin = (unsigned int*)&__fsym_begin; ptr_begin += (sizeof(struct finsh_syscall)/sizeof(unsigned int));
|
||||
while (*ptr_begin == 0) ptr_begin ++;
|
||||
|
||||
ptr_end = (unsigned int*) &__fsym_end; ptr_end --;
|
||||
while (*ptr_end == 0) ptr_end --;
|
||||
|
||||
finsh_system_function_init(ptr_begin, ptr_end);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* create or set shell structure */
|
||||
#ifdef RT_USING_HEAP
|
||||
shell = (struct finsh_shell*)rt_malloc(sizeof(struct finsh_shell));
|
||||
if (shell == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory for shell\n");
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
shell = &_shell;
|
||||
#endif
|
||||
|
||||
memset(shell, 0, sizeof(struct finsh_shell));
|
||||
|
||||
rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
|
||||
result = rt_thread_init(&finsh_thread,
|
||||
"tshell",
|
||||
finsh_thread_entry, RT_NULL,
|
||||
&finsh_thread_stack[0], sizeof(finsh_thread_stack),
|
||||
FINSH_THREAD_PRIORITY, 10);
|
||||
|
||||
if (result == RT_EOK)
|
||||
rt_thread_startup(&finsh_thread);
|
||||
return 0;
|
||||
}
|
||||
INIT_COMPONENT_EXPORT(finsh_system_init);
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* shell implementation for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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-06-02 Bernard Add finsh_get_prompt function declaration
|
||||
*/
|
||||
|
||||
#ifndef __SHELL_H__
|
||||
#define __SHELL_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "finsh.h"
|
||||
|
||||
#define FINSH_USING_HISTORY
|
||||
#ifndef FINSH_THREAD_PRIORITY
|
||||
#define FINSH_THREAD_PRIORITY 20
|
||||
#endif
|
||||
#ifndef FINSH_THREAD_STACK_SIZE
|
||||
#define FINSH_THREAD_STACK_SIZE 2048
|
||||
#endif
|
||||
#define FINSH_CMD_SIZE 80
|
||||
|
||||
#define FINSH_OPTION_ECHO 0x01
|
||||
#if defined(FINSH_USING_MSH) || (defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR))
|
||||
#define FINSH_PROMPT finsh_get_prompt()
|
||||
const char* finsh_get_prompt(void);
|
||||
#else
|
||||
#define FINSH_PROMPT "finsh>>"
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
#ifndef FINSH_HISTORY_LINES
|
||||
#define FINSH_HISTORY_LINES 5
|
||||
#endif
|
||||
#endif
|
||||
|
||||
enum input_stat
|
||||
{
|
||||
WAIT_NORMAL,
|
||||
WAIT_SPEC_KEY,
|
||||
WAIT_FUNC_KEY,
|
||||
};
|
||||
struct finsh_shell
|
||||
{
|
||||
struct rt_semaphore rx_sem;
|
||||
|
||||
enum input_stat stat;
|
||||
|
||||
rt_uint8_t echo_mode:1;
|
||||
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
rt_uint16_t current_history;
|
||||
rt_uint16_t history_count;
|
||||
|
||||
char cmd_history[FINSH_HISTORY_LINES][FINSH_CMD_SIZE];
|
||||
#endif
|
||||
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
struct finsh_parser parser;
|
||||
#endif
|
||||
|
||||
char line[FINSH_CMD_SIZE];
|
||||
rt_uint8_t line_position;
|
||||
rt_uint8_t line_curpos;
|
||||
|
||||
rt_device_t device;
|
||||
};
|
||||
|
||||
void finsh_set_echo(rt_uint32_t echo);
|
||||
rt_uint32_t finsh_get_echo(void);
|
||||
|
||||
int finsh_system_init(void);
|
||||
void finsh_set_device(const char* device_name);
|
||||
const char* finsh_get_device(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* symbols in finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include "finsh.h"
|
||||
|
||||
long hello(void);
|
||||
long version(void);
|
||||
long list(void);
|
||||
long list_thread(void);
|
||||
long list_sem(void);
|
||||
long list_mutex(void);
|
||||
long list_fevent(void);
|
||||
long list_event(void);
|
||||
long list_mailbox(void);
|
||||
long list_msgqueue(void);
|
||||
long list_mempool(void);
|
||||
long list_timer(void);
|
||||
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
struct finsh_syscall *_syscall_table_begin = NULL;
|
||||
struct finsh_syscall *_syscall_table_end = NULL;
|
||||
struct finsh_sysvar *_sysvar_table_begin = NULL;
|
||||
struct finsh_sysvar *_sysvar_table_end = NULL;
|
||||
#else
|
||||
struct finsh_syscall _syscall_table[] =
|
||||
{
|
||||
{"hello", hello},
|
||||
{"version", version},
|
||||
{"list", list},
|
||||
{"list_thread", list_thread},
|
||||
#ifdef RT_USING_SEMAPHORE
|
||||
{"list_sem", list_sem},
|
||||
#endif
|
||||
#ifdef RT_USING_MUTEX
|
||||
{"list_mutex", list_mutex},
|
||||
#endif
|
||||
#ifdef RT_USING_FEVENT
|
||||
{"list_fevent", list_fevent},
|
||||
#endif
|
||||
#ifdef RT_USING_EVENT
|
||||
{"list_event", list_event},
|
||||
#endif
|
||||
#ifdef RT_USING_MAILBOX
|
||||
{"list_mb", list_mailbox},
|
||||
#endif
|
||||
#ifdef RT_USING_MESSAGEQUEUE
|
||||
{"list_mq", list_msgqueue},
|
||||
#endif
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
{"list_memp", list_mempool},
|
||||
#endif
|
||||
{"list_timer", list_timer},
|
||||
};
|
||||
struct finsh_syscall *_syscall_table_begin = &_syscall_table[0];
|
||||
struct finsh_syscall *_syscall_table_end = &_syscall_table[sizeof(_syscall_table) / sizeof(struct finsh_syscall)];
|
||||
|
||||
struct finsh_sysvar *_sysvar_table_begin = NULL;
|
||||
struct finsh_sysvar *_sysvar_table_end = NULL;
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef CPUUSAGE_H
|
||||
#define CPUUSAGE_H
|
||||
|
||||
|
||||
static void cpu_usage_idle_hook(void);
|
||||
void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor);
|
||||
void cpu_usage_init(void);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* File : rtdebug.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.
|
||||
*/
|
||||
|
||||
#ifndef __RTDEBUG_H__
|
||||
#define __RTDEBUG_H__
|
||||
|
||||
#include <rtconfig.h>
|
||||
|
||||
/* Using this macro to control all kernel debug features. */
|
||||
#ifdef RT_DEBUG
|
||||
|
||||
/* Turn on some of these (set to non-zero) to debug kernel */
|
||||
#ifndef RT_DEBUG_MEM
|
||||
#define RT_DEBUG_MEM 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_MEMHEAP
|
||||
#define RT_DEBUG_MEMHEAP 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_MODULE
|
||||
#define RT_DEBUG_MODULE 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_SCHEDULER
|
||||
#define RT_DEBUG_SCHEDULER 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_SLAB
|
||||
#define RT_DEBUG_SLAB 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_THREAD
|
||||
#define RT_DEBUG_THREAD 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_TIMER
|
||||
#define RT_DEBUG_TIMER 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_IRQ
|
||||
#define RT_DEBUG_IRQ 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_IPC
|
||||
#define RT_DEBUG_IPC 0
|
||||
#endif
|
||||
|
||||
#ifndef RT_DEBUG_INIT
|
||||
#define RT_DEBUG_INIT 0
|
||||
#endif
|
||||
|
||||
/* Turn on this to enable context check */
|
||||
#ifndef RT_DEBUG_CONTEXT_CHECK
|
||||
#define RT_DEBUG_CONTEXT_CHECK 1
|
||||
#endif
|
||||
|
||||
#define RT_DEBUG_LOG(type, message) \
|
||||
do \
|
||||
{ \
|
||||
if (type) \
|
||||
rt_kprintf message; \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
#define RT_ASSERT(EX) \
|
||||
if (!(EX)) \
|
||||
{ \
|
||||
volatile char dummy = 0; \
|
||||
rt_kprintf("(%s) assert failed at %s:%d \n", #EX, __FUNCTION__, __LINE__);\
|
||||
while (dummy == 0); \
|
||||
}
|
||||
|
||||
/* Macro to check current context */
|
||||
#if RT_DEBUG_CONTEXT_CHECK
|
||||
#define RT_DEBUG_NOT_IN_INTERRUPT \
|
||||
do \
|
||||
{ \
|
||||
rt_base_t level; \
|
||||
level = rt_hw_interrupt_disable(); \
|
||||
if (rt_interrupt_get_nest() != 0) \
|
||||
{ \
|
||||
rt_kprintf("Function[%s] shall not used in ISR\n", __FUNCTION__); \
|
||||
RT_ASSERT(0) \
|
||||
} \
|
||||
rt_hw_interrupt_enable(level); \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
/* "In thread context" means:
|
||||
* 1) the scheduler has been started
|
||||
* 2) not in interrupt context.
|
||||
*/
|
||||
#define RT_DEBUG_IN_THREAD_CONTEXT \
|
||||
do \
|
||||
{ \
|
||||
rt_base_t level; \
|
||||
level = rt_hw_interrupt_disable(); \
|
||||
if (rt_thread_self() == RT_NULL) \
|
||||
{ \
|
||||
rt_kprintf("Function[%s] shall not be used before scheduler start\n", \
|
||||
__FUNCTION__); \
|
||||
RT_ASSERT(0) \
|
||||
} \
|
||||
RT_DEBUG_NOT_IN_INTERRUPT; \
|
||||
rt_hw_interrupt_enable(level); \
|
||||
} \
|
||||
while (0)
|
||||
#else
|
||||
#define RT_DEBUG_NOT_IN_INTERRUPT
|
||||
#define RT_DEBUG_IN_THREAD_CONTEXT
|
||||
#endif
|
||||
|
||||
#else /* RT_DEBUG */
|
||||
|
||||
#define RT_ASSERT(EX)
|
||||
#define RT_DEBUG_LOG(type, message)
|
||||
#define RT_DEBUG_NOT_IN_INTERRUPT
|
||||
#define RT_DEBUG_IN_THREAD_CONTEXT
|
||||
|
||||
#endif /* RT_DEBUG */
|
||||
|
||||
#endif /* __RTDEBUG_H__ */
|
||||
@@ -0,0 +1,986 @@
|
||||
/*
|
||||
* File : rtdef.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
|
||||
* 2007-01-10 Bernard the first version
|
||||
* 2008-07-12 Bernard remove all rt_int8, rt_uint32_t etc typedef
|
||||
* 2010-10-26 yi.qiu add module support
|
||||
* 2010-11-10 Bernard add cleanup callback function in thread exit.
|
||||
* 2011-05-09 Bernard use builtin va_arg in GCC 4.x
|
||||
* 2012-11-16 Bernard change RT_NULL from ((void*)0) to 0.
|
||||
* 2012-12-29 Bernard change the RT_USING_MEMPOOL location and add
|
||||
* RT_USING_MEMHEAP condition.
|
||||
* 2012-12-30 Bernard add more control command for graphic.
|
||||
* 2013-01-09 Bernard change version number.
|
||||
*/
|
||||
|
||||
#ifndef __RT_DEF_H__
|
||||
#define __RT_DEF_H__
|
||||
|
||||
/* include rtconfig header to import configuration */
|
||||
#include <rtconfig.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @addtogroup BasicDef
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/* RT-Thread version information */
|
||||
#define RT_VERSION 1L /**< major version number */
|
||||
#define RT_SUBVERSION 2L /**< minor version number */
|
||||
#define RT_REVISION 2L /**< revise version number */
|
||||
|
||||
/* RT-Thread version */
|
||||
#define RTTHREAD_VERSION ((RT_VERSION * 10000) + \
|
||||
(RT_SUBVERSION * 100) + RT_REVISION)
|
||||
|
||||
/* RT-Thread basic data type definitions */
|
||||
typedef signed char rt_int8_t; /**< 8bit integer type */
|
||||
typedef signed short rt_int16_t; /**< 16bit integer type */
|
||||
typedef signed long rt_int32_t; /**< 32bit integer type */
|
||||
typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */
|
||||
typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */
|
||||
typedef unsigned long rt_uint32_t; /**< 32bit unsigned integer type */
|
||||
typedef int rt_bool_t; /**< boolean type */
|
||||
|
||||
/* 32bit CPU */
|
||||
typedef long rt_base_t; /**< Nbit CPU related date type */
|
||||
typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */
|
||||
|
||||
typedef rt_base_t rt_err_t; /**< Type for error number */
|
||||
typedef rt_uint32_t rt_time_t; /**< Type for time stamp */
|
||||
typedef rt_uint32_t rt_tick_t; /**< Type for tick count */
|
||||
typedef rt_base_t rt_flag_t; /**< Type for flags */
|
||||
typedef rt_ubase_t rt_size_t; /**< Type for size number */
|
||||
typedef rt_ubase_t rt_dev_t; /**< Type for device */
|
||||
typedef rt_base_t rt_off_t; /**< Type for offset */
|
||||
|
||||
/* boolean type definitions */
|
||||
#define RT_TRUE 1 /**< boolean true */
|
||||
#define RT_FALSE 0 /**< boolean fails */
|
||||
|
||||
/*@}*/
|
||||
|
||||
/* maximum value of base type */
|
||||
#define RT_UINT8_MAX 0xff /**< Maxium number of UINT8 */
|
||||
#define RT_UINT16_MAX 0xffff /**< Maxium number of UINT16 */
|
||||
#define RT_UINT32_MAX 0xffffffff /**< Maxium number of UINT32 */
|
||||
#define RT_TICK_MAX RT_UINT32_MAX /**< Maxium number of tick */
|
||||
|
||||
/* Compiler Related Definitions */
|
||||
#ifdef __CC_ARM /* ARM Compiler */
|
||||
#include <stdarg.h>
|
||||
#define SECTION(x) __attribute__((section(x)))
|
||||
#define UNUSED __attribute__((unused))
|
||||
#define USED __attribute__((used))
|
||||
#define ALIGN(n) __attribute__((aligned(n)))
|
||||
#define rt_inline static __inline
|
||||
/* module compiling */
|
||||
#ifdef RT_USING_MODULE
|
||||
#define RTT_API __declspec(dllimport)
|
||||
#else
|
||||
#define RTT_API __declspec(dllexport)
|
||||
#endif
|
||||
|
||||
#elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
|
||||
#include <stdarg.h>
|
||||
#define SECTION(x) @ x
|
||||
#define UNUSED
|
||||
#define USED
|
||||
#define PRAGMA(x) _Pragma(#x)
|
||||
#define ALIGN(n) PRAGMA(data_alignment=n)
|
||||
#define rt_inline static inline
|
||||
#define RTT_API
|
||||
|
||||
#elif defined (__GNUC__) /* GNU GCC Compiler */
|
||||
#ifdef RT_USING_NEWLIB
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
/* the version of GNU GCC must be greater than 4.x */
|
||||
typedef __builtin_va_list __gnuc_va_list;
|
||||
typedef __gnuc_va_list va_list;
|
||||
#define va_start(v,l) __builtin_va_start(v,l)
|
||||
#define va_end(v) __builtin_va_end(v)
|
||||
#define va_arg(v,l) __builtin_va_arg(v,l)
|
||||
#endif
|
||||
|
||||
#define SECTION(x) __attribute__((section(x)))
|
||||
#define UNUSED __attribute__((unused))
|
||||
#define USED __attribute__((used))
|
||||
#define ALIGN(n) __attribute__((aligned(n)))
|
||||
#define rt_inline static __inline
|
||||
#define RTT_API
|
||||
#elif defined (__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
|
||||
#include <stdarg.h>
|
||||
#define SECTION(x) __attribute__((section(x)))
|
||||
#define UNUSED __attribute__((unused))
|
||||
#define USED __attribute__((used))
|
||||
#define ALIGN(n) __attribute__((aligned(n)))
|
||||
#define rt_inline static inline
|
||||
#define RTT_API
|
||||
#elif defined (_MSC_VER)
|
||||
#include <stdarg.h>
|
||||
#define SECTION(x)
|
||||
#define UNUSED
|
||||
#define USED
|
||||
#define ALIGN(n) __declspec(align(n))
|
||||
#define rt_inline static __inline
|
||||
#define RTT_API
|
||||
#elif defined (__TI_COMPILER_VERSION__)
|
||||
#include <stdarg.h>
|
||||
/* The way that TI compiler set section is different from other(at least
|
||||
* GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more
|
||||
* details. */
|
||||
#define SECTION(x)
|
||||
#define UNUSED
|
||||
#define USED
|
||||
#define PRAGMA(x) _Pragma(#x)
|
||||
#define ALIGN(n)
|
||||
#define rt_inline static inline
|
||||
#define RTT_API
|
||||
#else
|
||||
#error not supported tool chain
|
||||
#endif
|
||||
|
||||
/* initialization export */
|
||||
#ifdef RT_USING_COMPONENTS_INIT
|
||||
typedef int (*init_fn_t)(void);
|
||||
#ifdef _MSC_VER /* we do not support MS VC++ compiler */
|
||||
#define INIT_EXPORT(fn, level)
|
||||
#else
|
||||
#if RT_DEBUG_INIT
|
||||
struct rt_init_desc
|
||||
{
|
||||
const char* fn_name;
|
||||
const init_fn_t fn;
|
||||
};
|
||||
#define INIT_EXPORT(fn, level) \
|
||||
const char __rti_##fn##_name[] = #fn; \
|
||||
const struct rt_init_desc __rt_init_desc_##fn SECTION(".rti_fn."level) = \
|
||||
{ __rti_##fn##_name, fn};
|
||||
#else
|
||||
#define INIT_EXPORT(fn, level) \
|
||||
const init_fn_t __rt_init_##fn SECTION(".rti_fn."level) = fn
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
#define INIT_EXPORT(fn, level)
|
||||
#endif
|
||||
|
||||
/* board init routines will be called in board_init() function */
|
||||
#define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1")
|
||||
/* device/component/fs/app init routines will be called in init_thread */
|
||||
/* device initialization */
|
||||
#define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, "2")
|
||||
/* components initialization (dfs, lwip, ...) */
|
||||
#define INIT_COMPONENT_EXPORT(fn) INIT_EXPORT(fn, "3")
|
||||
/* file system initialization (dfs-elm, dfs-rom, ...) */
|
||||
#define INIT_FS_EXPORT(fn) INIT_EXPORT(fn, "4")
|
||||
/* environment initialization (mount disk, ...) */
|
||||
#define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "5")
|
||||
/* appliation initialization (rtgui application etc ...) */
|
||||
#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6")
|
||||
|
||||
#if !defined(RT_USING_FINSH)
|
||||
/* define these to empty, even if not include finsh.h file */
|
||||
#define FINSH_FUNCTION_EXPORT(name, desc)
|
||||
#define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
|
||||
#define FINSH_VAR_EXPORT(name, type, desc)
|
||||
|
||||
#define MSH_CMD_EXPORT(command, desc)
|
||||
#elif !defined(FINSH_USING_SYMTAB)
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc)
|
||||
#endif
|
||||
|
||||
/* event length */
|
||||
#define RT_EVENT_LENGTH 32
|
||||
|
||||
/* memory management option */
|
||||
#define RT_MM_PAGE_SIZE 4096
|
||||
#define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1)
|
||||
#define RT_MM_PAGE_BITS 12
|
||||
|
||||
/* kernel malloc definitions */
|
||||
#ifndef RT_KERNEL_MALLOC
|
||||
#define RT_KERNEL_MALLOC(sz) rt_malloc(sz)
|
||||
#endif
|
||||
|
||||
#ifndef RT_KERNEL_FREE
|
||||
#define RT_KERNEL_FREE(ptr) rt_free(ptr)
|
||||
#endif
|
||||
|
||||
#ifndef RT_KERNEL_REALLOC
|
||||
#define RT_KERNEL_REALLOC(ptr, size) rt_realloc(ptr, size)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @addtogroup Error
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/* RT-Thread error code definitions */
|
||||
#define RT_EOK 0 /**< There is no error */
|
||||
#define RT_ERROR 1 /**< A generic error happens */
|
||||
#define RT_ETIMEOUT 2 /**< Timed out */
|
||||
#define RT_EFULL 3 /**< The resource is full */
|
||||
#define RT_EEMPTY 4 /**< The resource is empty */
|
||||
#define RT_ENOMEM 5 /**< No memory */
|
||||
#define RT_ENOSYS 6 /**< No system */
|
||||
#define RT_EBUSY 7 /**< Busy */
|
||||
#define RT_EIO 8 /**< IO error */
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @ingroup BasicDef
|
||||
*
|
||||
* @def RT_ALIGN(size, align)
|
||||
* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
|
||||
* would return 16.
|
||||
*/
|
||||
#define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
|
||||
|
||||
/**
|
||||
* @ingroup BasicDef
|
||||
*
|
||||
* @def RT_ALIGN_DOWN(size, align)
|
||||
* Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
|
||||
* would return 12.
|
||||
*/
|
||||
#define RT_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
|
||||
|
||||
/**
|
||||
* @ingroup BasicDef
|
||||
*
|
||||
* @def RT_NULL
|
||||
* Similar as the \c NULL in C library.
|
||||
*/
|
||||
#define RT_NULL (0)
|
||||
|
||||
struct rt_list_node
|
||||
{
|
||||
struct rt_list_node *next; /**< point to next node. */
|
||||
struct rt_list_node *prev; /**< point to prev node. */
|
||||
};
|
||||
typedef struct rt_list_node rt_list_t; /**< Type for lists. */
|
||||
|
||||
/**
|
||||
* @addtogroup KernelObject
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* kernel object macros
|
||||
*/
|
||||
#define RT_OBJECT_FLAG_MODULE 0x80 /**< is module object. */
|
||||
|
||||
/**
|
||||
* Base structure of Kernel object
|
||||
*/
|
||||
struct rt_object
|
||||
{
|
||||
char name[RT_NAME_MAX]; /**< name of kernel object */
|
||||
rt_uint8_t type; /**< type of kernel object */
|
||||
rt_uint8_t flag; /**< flag of kernel object */
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
void *module_id; /**< id of application module */
|
||||
#endif
|
||||
rt_list_t list; /**< list node of kernel object */
|
||||
};
|
||||
typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */
|
||||
|
||||
/**
|
||||
* The object type can be one of the follows with specific
|
||||
* macros enabled:
|
||||
* - Thread
|
||||
* - Semaphore
|
||||
* - Mutex
|
||||
* - Event
|
||||
* - MailBox
|
||||
* - MessageQueue
|
||||
* - MemHeap
|
||||
* - MemPool
|
||||
* - Device
|
||||
* - Timer
|
||||
* - Module
|
||||
* - Unknown
|
||||
* - Static
|
||||
*/
|
||||
enum rt_object_class_type
|
||||
{
|
||||
RT_Object_Class_Thread = 0, /**< The object is a thread. */
|
||||
#ifdef RT_USING_SEMAPHORE
|
||||
RT_Object_Class_Semaphore, /**< The object is a semaphore. */
|
||||
#endif
|
||||
#ifdef RT_USING_MUTEX
|
||||
RT_Object_Class_Mutex, /**< The object is a mutex. */
|
||||
#endif
|
||||
#ifdef RT_USING_EVENT
|
||||
RT_Object_Class_Event, /**< The object is a event. */
|
||||
#endif
|
||||
#ifdef RT_USING_MAILBOX
|
||||
RT_Object_Class_MailBox, /**< The object is a mail box. */
|
||||
#endif
|
||||
#ifdef RT_USING_MESSAGEQUEUE
|
||||
RT_Object_Class_MessageQueue, /**< The object is a message queue. */
|
||||
#endif
|
||||
#ifdef RT_USING_MEMHEAP
|
||||
RT_Object_Class_MemHeap, /**< The object is a memory heap */
|
||||
#endif
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
RT_Object_Class_MemPool, /**< The object is a memory pool. */
|
||||
#endif
|
||||
#ifdef RT_USING_DEVICE
|
||||
RT_Object_Class_Device, /**< The object is a device */
|
||||
#endif
|
||||
RT_Object_Class_Timer, /**< The object is a timer. */
|
||||
#ifdef RT_USING_MODULE
|
||||
RT_Object_Class_Module, /**< The object is a module. */
|
||||
#endif
|
||||
RT_Object_Class_Unknown, /**< The object is unknown. */
|
||||
RT_Object_Class_Static = 0x80 /**< The object is a static object. */
|
||||
};
|
||||
|
||||
/**
|
||||
* The information of the kernel object
|
||||
*/
|
||||
struct rt_object_information
|
||||
{
|
||||
enum rt_object_class_type type; /**< object class type */
|
||||
rt_list_t object_list; /**< object list */
|
||||
rt_size_t object_size; /**< object size */
|
||||
};
|
||||
|
||||
/**
|
||||
* The hook function call macro
|
||||
*/
|
||||
#ifdef RT_USING_HOOK
|
||||
#define RT_OBJECT_HOOK_CALL(func, argv) \
|
||||
do { if ((func) != RT_NULL) func argv; } while (0)
|
||||
#else
|
||||
#define RT_OBJECT_HOOK_CALL(func, argv)
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @addtogroup Clock
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* clock & timer macros
|
||||
*/
|
||||
#define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */
|
||||
#define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */
|
||||
#define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */
|
||||
#define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */
|
||||
|
||||
#define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */
|
||||
#define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */
|
||||
|
||||
#define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */
|
||||
#define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */
|
||||
#define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */
|
||||
#define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */
|
||||
|
||||
#ifndef RT_TIMER_SKIP_LIST_LEVEL
|
||||
#define RT_TIMER_SKIP_LIST_LEVEL 1
|
||||
#endif
|
||||
|
||||
/* 1 or 3 */
|
||||
#ifndef RT_TIMER_SKIP_LIST_MASK
|
||||
#define RT_TIMER_SKIP_LIST_MASK 0x3
|
||||
#endif
|
||||
|
||||
/**
|
||||
* timer structure
|
||||
*/
|
||||
struct rt_timer
|
||||
{
|
||||
struct rt_object parent; /**< inherit from rt_object */
|
||||
|
||||
rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];
|
||||
|
||||
void (*timeout_func)(void *parameter); /**< timeout function */
|
||||
void *parameter; /**< timeout function's parameter */
|
||||
|
||||
rt_tick_t init_tick; /**< timer timeout tick */
|
||||
rt_tick_t timeout_tick; /**< timeout tick */
|
||||
};
|
||||
typedef struct rt_timer *rt_timer_t;
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @addtogroup Thread
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* Thread
|
||||
*/
|
||||
|
||||
/*
|
||||
* thread state definitions
|
||||
*/
|
||||
#define RT_THREAD_INIT 0x00 /**< Initialized status */
|
||||
#define RT_THREAD_READY 0x01 /**< Ready status */
|
||||
#define RT_THREAD_SUSPEND 0x02 /**< Suspend status */
|
||||
#define RT_THREAD_RUNNING 0x03 /**< Running status */
|
||||
#define RT_THREAD_BLOCK RT_THREAD_SUSPEND /**< Blocked status */
|
||||
#define RT_THREAD_CLOSE 0x04 /**< Closed status */
|
||||
|
||||
/**
|
||||
* thread control command definitions
|
||||
*/
|
||||
#define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */
|
||||
#define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */
|
||||
#define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */
|
||||
#define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */
|
||||
|
||||
/**
|
||||
* Thread structure
|
||||
*/
|
||||
struct rt_thread
|
||||
{
|
||||
/* rt object */
|
||||
char name[RT_NAME_MAX]; /**< the name of thread */
|
||||
rt_uint8_t type; /**< type of object */
|
||||
rt_uint8_t flags; /**< thread's flags */
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
void *module_id; /**< id of application module */
|
||||
#endif
|
||||
|
||||
rt_list_t list; /**< the object list */
|
||||
rt_list_t tlist; /**< the thread list */
|
||||
|
||||
/* stack point and entry */
|
||||
void *sp; /**< stack point */
|
||||
void *entry; /**< entry */
|
||||
void *parameter; /**< parameter */
|
||||
void *stack_addr; /**< stack address */
|
||||
rt_uint16_t stack_size; /**< stack size */
|
||||
|
||||
/* error code */
|
||||
rt_err_t error; /**< error code */
|
||||
|
||||
rt_uint8_t stat; /**< thread stat */
|
||||
|
||||
/* priority */
|
||||
rt_uint8_t current_priority; /**< current priority */
|
||||
rt_uint8_t init_priority; /**< initialized priority */
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
rt_uint8_t number;
|
||||
rt_uint8_t high_mask;
|
||||
#endif
|
||||
rt_uint32_t number_mask;
|
||||
|
||||
#if defined(RT_USING_EVENT)
|
||||
/* thread event */
|
||||
rt_uint32_t event_set;
|
||||
rt_uint8_t event_info;
|
||||
#endif
|
||||
|
||||
rt_ubase_t init_tick; /**< thread's initialized tick */
|
||||
rt_ubase_t remaining_tick; /**< remaining tick */
|
||||
|
||||
struct rt_timer thread_timer; /**< built-in thread timer */
|
||||
|
||||
void (*cleanup)(struct rt_thread *tid); /**< cleanup function when thread exit */
|
||||
|
||||
rt_uint32_t user_data; /**< private user data beyond this thread */
|
||||
};
|
||||
typedef struct rt_thread *rt_thread_t;
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @addtogroup IPC
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* IPC flags and control command definitions
|
||||
*/
|
||||
#define RT_IPC_FLAG_FIFO 0x00 /**< FIFOed IPC. @ref IPC. */
|
||||
#define RT_IPC_FLAG_PRIO 0x01 /**< PRIOed IPC. @ref IPC. */
|
||||
|
||||
#define RT_IPC_CMD_UNKNOWN 0x00 /**< unknown IPC command */
|
||||
#define RT_IPC_CMD_RESET 0x01 /**< reset IPC object */
|
||||
|
||||
#define RT_WAITING_FOREVER -1 /**< Block forever until get resource. */
|
||||
#define RT_WAITING_NO 0 /**< Non-block. */
|
||||
|
||||
/**
|
||||
* Base structure of IPC object
|
||||
*/
|
||||
struct rt_ipc_object
|
||||
{
|
||||
struct rt_object parent; /**< inherit from rt_object */
|
||||
|
||||
rt_list_t suspend_thread; /**< threads pended on this resource */
|
||||
};
|
||||
|
||||
#ifdef RT_USING_SEMAPHORE
|
||||
/**
|
||||
* Semaphore structure
|
||||
*/
|
||||
struct rt_semaphore
|
||||
{
|
||||
struct rt_ipc_object parent; /**< inherit from ipc_object */
|
||||
|
||||
rt_uint16_t value; /**< value of semaphore. */
|
||||
};
|
||||
typedef struct rt_semaphore *rt_sem_t;
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MUTEX
|
||||
/**
|
||||
* Mutual exclusion (mutex) structure
|
||||
*/
|
||||
struct rt_mutex
|
||||
{
|
||||
struct rt_ipc_object parent; /**< inherit from ipc_object */
|
||||
|
||||
rt_uint16_t value; /**< value of mutex */
|
||||
|
||||
rt_uint8_t original_priority; /**< priority of last thread hold the mutex */
|
||||
rt_uint8_t hold; /**< numbers of thread hold the mutex */
|
||||
|
||||
struct rt_thread *owner; /**< current owner of mutex */
|
||||
};
|
||||
typedef struct rt_mutex *rt_mutex_t;
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_EVENT
|
||||
/**
|
||||
* flag defintions in event
|
||||
*/
|
||||
#define RT_EVENT_FLAG_AND 0x01 /**< logic and */
|
||||
#define RT_EVENT_FLAG_OR 0x02 /**< logic or */
|
||||
#define RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */
|
||||
|
||||
/*
|
||||
* event structure
|
||||
*/
|
||||
struct rt_event
|
||||
{
|
||||
struct rt_ipc_object parent; /**< inherit from ipc_object */
|
||||
|
||||
rt_uint32_t set; /**< event set */
|
||||
};
|
||||
typedef struct rt_event *rt_event_t;
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MAILBOX
|
||||
/**
|
||||
* mailbox structure
|
||||
*/
|
||||
struct rt_mailbox
|
||||
{
|
||||
struct rt_ipc_object parent; /**< inherit from ipc_object */
|
||||
|
||||
rt_uint32_t *msg_pool; /**< start address of message buffer */
|
||||
|
||||
rt_uint16_t size; /**< size of message pool */
|
||||
|
||||
rt_uint16_t entry; /**< index of messages in msg_pool */
|
||||
rt_uint16_t in_offset; /**< input offset of the message buffer */
|
||||
rt_uint16_t out_offset; /**< output offset of the message buffer */
|
||||
|
||||
rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */
|
||||
};
|
||||
typedef struct rt_mailbox *rt_mailbox_t;
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MESSAGEQUEUE
|
||||
/**
|
||||
* message queue structure
|
||||
*/
|
||||
struct rt_messagequeue
|
||||
{
|
||||
struct rt_ipc_object parent; /**< inherit from ipc_object */
|
||||
|
||||
void *msg_pool; /**< start address of message queue */
|
||||
|
||||
rt_uint16_t msg_size; /**< message size of each message */
|
||||
rt_uint16_t max_msgs; /**< max number of messages */
|
||||
|
||||
rt_uint16_t entry; /**< index of messages in the queue */
|
||||
|
||||
void *msg_queue_head; /**< list head */
|
||||
void *msg_queue_tail; /**< list tail */
|
||||
void *msg_queue_free; /**< pointer indicated the free node of queue */
|
||||
};
|
||||
typedef struct rt_messagequeue *rt_mq_t;
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @addtogroup MM
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* memory management
|
||||
* heap & partition
|
||||
*/
|
||||
|
||||
#ifdef RT_USING_MEMHEAP
|
||||
/**
|
||||
* memory item on the heap
|
||||
*/
|
||||
struct rt_memheap_item
|
||||
{
|
||||
rt_uint32_t magic; /**< magic number for memheap */
|
||||
struct rt_memheap *pool_ptr; /**< point of pool */
|
||||
|
||||
struct rt_memheap_item *next; /**< next memheap item */
|
||||
struct rt_memheap_item *prev; /**< prev memheap item */
|
||||
|
||||
struct rt_memheap_item *next_free; /**< next free memheap item */
|
||||
struct rt_memheap_item *prev_free; /**< prev free memheap item */
|
||||
};
|
||||
|
||||
/**
|
||||
* Base structure of memory heap object
|
||||
*/
|
||||
struct rt_memheap
|
||||
{
|
||||
struct rt_object parent; /**< inherit from rt_object */
|
||||
|
||||
void *start_addr; /**< pool start address and size */
|
||||
|
||||
rt_uint32_t pool_size; /**< pool size */
|
||||
rt_uint32_t available_size; /**< available size */
|
||||
rt_uint32_t max_used_size; /**< maximum allocated size */
|
||||
|
||||
struct rt_memheap_item *block_list; /**< used block list */
|
||||
|
||||
struct rt_memheap_item *free_list; /**< free block list */
|
||||
struct rt_memheap_item free_header; /**< free block list header */
|
||||
|
||||
struct rt_semaphore lock; /**< semaphore lock */
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
/**
|
||||
* Base structure of Memory pool object
|
||||
*/
|
||||
struct rt_mempool
|
||||
{
|
||||
struct rt_object parent; /**< inherit from rt_object */
|
||||
|
||||
void *start_address; /**< memory pool start */
|
||||
rt_size_t size; /**< size of memory pool */
|
||||
|
||||
rt_size_t block_size; /**< size of memory blocks */
|
||||
rt_uint8_t *block_list; /**< memory blocks list */
|
||||
|
||||
rt_size_t block_total_count; /**< numbers of memory block */
|
||||
rt_size_t block_free_count; /**< numbers of free memory block */
|
||||
|
||||
rt_list_t suspend_thread; /**< threads pended on this resource */
|
||||
rt_size_t suspend_thread_count; /**< numbers of thread pended on this resource */
|
||||
};
|
||||
typedef struct rt_mempool *rt_mp_t;
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
#ifdef RT_USING_DEVICE
|
||||
/**
|
||||
* @addtogroup Device
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* device (I/O) class type
|
||||
*/
|
||||
enum rt_device_class_type
|
||||
{
|
||||
RT_Device_Class_Char = 0, /**< character device */
|
||||
RT_Device_Class_Block, /**< block device */
|
||||
RT_Device_Class_NetIf, /**< net interface */
|
||||
RT_Device_Class_MTD, /**< memory device */
|
||||
RT_Device_Class_CAN, /**< CAN device */
|
||||
RT_Device_Class_RTC, /**< RTC device */
|
||||
RT_Device_Class_Sound, /**< Sound device */
|
||||
RT_Device_Class_Graphic, /**< Graphic device */
|
||||
RT_Device_Class_I2CBUS, /**< I2C bus device */
|
||||
RT_Device_Class_USBDevice, /**< USB slave device */
|
||||
RT_Device_Class_USBHost, /**< USB host bus */
|
||||
RT_Device_Class_SPIBUS, /**< SPI bus device */
|
||||
RT_Device_Class_SPIDevice, /**< SPI device */
|
||||
RT_Device_Class_SDIO, /**< SDIO bus device */
|
||||
RT_Device_Class_PM, /**< PM pseudo device */
|
||||
RT_Device_Class_Pipe, /**< Pipe device */
|
||||
RT_Device_Class_Portal, /**< Portal device */
|
||||
RT_Device_Class_Miscellaneous, /**< Miscellaneous device */
|
||||
RT_Device_Class_Unknown /**< unknown device */
|
||||
};
|
||||
|
||||
/**
|
||||
* device flags defitions
|
||||
*/
|
||||
#define RT_DEVICE_FLAG_DEACTIVATE 0x000 /**< device is not not initialized */
|
||||
|
||||
#define RT_DEVICE_FLAG_RDONLY 0x001 /**< read only */
|
||||
#define RT_DEVICE_FLAG_WRONLY 0x002 /**< write only */
|
||||
#define RT_DEVICE_FLAG_RDWR 0x003 /**< read and write */
|
||||
|
||||
#define RT_DEVICE_FLAG_REMOVABLE 0x004 /**< removable device */
|
||||
#define RT_DEVICE_FLAG_STANDALONE 0x008 /**< standalone device */
|
||||
#define RT_DEVICE_FLAG_ACTIVATED 0x010 /**< device is activated */
|
||||
#define RT_DEVICE_FLAG_SUSPENDED 0x020 /**< device is suspended */
|
||||
#define RT_DEVICE_FLAG_STREAM 0x040 /**< stream mode */
|
||||
|
||||
#define RT_DEVICE_FLAG_INT_RX 0x100 /**< INT mode on Rx */
|
||||
#define RT_DEVICE_FLAG_DMA_RX 0x200 /**< DMA mode on Rx */
|
||||
#define RT_DEVICE_FLAG_INT_TX 0x400 /**< INT mode on Tx */
|
||||
#define RT_DEVICE_FLAG_DMA_TX 0x800 /**< DMA mode on Tx */
|
||||
|
||||
#define RT_DEVICE_OFLAG_CLOSE 0x000 /**< device is closed */
|
||||
#define RT_DEVICE_OFLAG_RDONLY 0x001 /**< read only access */
|
||||
#define RT_DEVICE_OFLAG_WRONLY 0x002 /**< write only access */
|
||||
#define RT_DEVICE_OFLAG_RDWR 0x003 /**< read and write */
|
||||
#define RT_DEVICE_OFLAG_OPEN 0x008 /**< device is opened */
|
||||
|
||||
/**
|
||||
* general device commands
|
||||
*/
|
||||
#define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */
|
||||
#define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */
|
||||
|
||||
/**
|
||||
* special device commands
|
||||
*/
|
||||
#define RT_DEVICE_CTRL_CHAR_STREAM 0x10 /**< stream mode on char device */
|
||||
#define RT_DEVICE_CTRL_BLK_GETGEOME 0x10 /**< get geometry information */
|
||||
#define RT_DEVICE_CTRL_BLK_SYNC 0x11 /**< flush data to block device */
|
||||
#define RT_DEVICE_CTRL_BLK_ERASE 0x12 /**< erase block on block device */
|
||||
#define RT_DEVICE_CTRL_NETIF_GETMAC 0x10 /**< get mac address */
|
||||
#define RT_DEVICE_CTRL_MTD_FORMAT 0x10 /**< format a MTD device */
|
||||
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get time */
|
||||
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set time */
|
||||
#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x12 /**< get alarm */
|
||||
#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x13 /**< set alarm */
|
||||
|
||||
typedef struct rt_device *rt_device_t;
|
||||
/**
|
||||
* Device structure
|
||||
*/
|
||||
struct rt_device
|
||||
{
|
||||
struct rt_object parent; /**< inherit from rt_object */
|
||||
|
||||
enum rt_device_class_type type; /**< device type */
|
||||
rt_uint16_t flag; /**< device flag */
|
||||
rt_uint16_t open_flag; /**< device open flag */
|
||||
|
||||
rt_uint8_t ref_count; /**< reference count */
|
||||
rt_uint8_t device_id; /**< 0 - 255 */
|
||||
|
||||
/* device call back */
|
||||
rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
|
||||
rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);
|
||||
|
||||
/* common device interface */
|
||||
rt_err_t (*init) (rt_device_t dev);
|
||||
rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
|
||||
rt_err_t (*close) (rt_device_t dev);
|
||||
rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
|
||||
rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
|
||||
rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void *args);
|
||||
|
||||
void *user_data; /**< device private data */
|
||||
};
|
||||
|
||||
/**
|
||||
* block device geometry structure
|
||||
*/
|
||||
struct rt_device_blk_geometry
|
||||
{
|
||||
rt_uint32_t sector_count; /**< count of sectors */
|
||||
rt_uint32_t bytes_per_sector; /**< number of bytes per sector */
|
||||
rt_uint32_t block_size; /**< size to erase one block */
|
||||
};
|
||||
|
||||
/**
|
||||
* sector arrange struct on block device
|
||||
*/
|
||||
struct rt_device_blk_sectors
|
||||
{
|
||||
rt_uint32_t sector_begin; /**< begin sector */
|
||||
rt_uint32_t sector_end; /**< end sector */
|
||||
};
|
||||
|
||||
/**
|
||||
* cursor control command
|
||||
*/
|
||||
#define RT_DEVICE_CTRL_CURSOR_SET_POSITION 0x10
|
||||
#define RT_DEVICE_CTRL_CURSOR_SET_TYPE 0x11
|
||||
|
||||
/**
|
||||
* graphic device control command
|
||||
*/
|
||||
#define RTGRAPHIC_CTRL_RECT_UPDATE 0
|
||||
#define RTGRAPHIC_CTRL_POWERON 1
|
||||
#define RTGRAPHIC_CTRL_POWEROFF 2
|
||||
#define RTGRAPHIC_CTRL_GET_INFO 3
|
||||
#define RTGRAPHIC_CTRL_SET_MODE 4
|
||||
#define RTGRAPHIC_CTRL_GET_EXT 5
|
||||
|
||||
/* graphic deice */
|
||||
enum
|
||||
{
|
||||
RTGRAPHIC_PIXEL_FORMAT_MONO = 0,
|
||||
RTGRAPHIC_PIXEL_FORMAT_GRAY4,
|
||||
RTGRAPHIC_PIXEL_FORMAT_GRAY16,
|
||||
RTGRAPHIC_PIXEL_FORMAT_RGB332,
|
||||
RTGRAPHIC_PIXEL_FORMAT_RGB444,
|
||||
RTGRAPHIC_PIXEL_FORMAT_RGB565,
|
||||
RTGRAPHIC_PIXEL_FORMAT_RGB565P,
|
||||
RTGRAPHIC_PIXEL_FORMAT_BGR565 = RTGRAPHIC_PIXEL_FORMAT_RGB565P,
|
||||
RTGRAPHIC_PIXEL_FORMAT_RGB666,
|
||||
RTGRAPHIC_PIXEL_FORMAT_RGB888,
|
||||
RTGRAPHIC_PIXEL_FORMAT_ARGB888
|
||||
};
|
||||
|
||||
/**
|
||||
* build a pixel position according to (x, y) coordinates.
|
||||
*/
|
||||
#define RTGRAPHIC_PIXEL_POSITION(x, y) ((x << 16) | y)
|
||||
|
||||
/**
|
||||
* graphic device information structure
|
||||
*/
|
||||
struct rt_device_graphic_info
|
||||
{
|
||||
rt_uint8_t pixel_format; /**< graphic format */
|
||||
rt_uint8_t bits_per_pixel; /**< bits per pixel */
|
||||
rt_uint16_t reserved; /**< reserved field */
|
||||
|
||||
rt_uint16_t width; /**< width of graphic device */
|
||||
rt_uint16_t height; /**< height of graphic device */
|
||||
|
||||
rt_uint8_t *framebuffer; /**< frame buffer */
|
||||
};
|
||||
|
||||
/**
|
||||
* rectangle information structure
|
||||
*/
|
||||
struct rt_device_rect_info
|
||||
{
|
||||
rt_uint16_t x; /**< x coordinate */
|
||||
rt_uint16_t y; /**< y coordinate */
|
||||
rt_uint16_t width; /**< width */
|
||||
rt_uint16_t height; /**< height */
|
||||
};
|
||||
|
||||
/**
|
||||
* graphic operations
|
||||
*/
|
||||
struct rt_device_graphic_ops
|
||||
{
|
||||
void (*set_pixel) (const char *pixel, int x, int y);
|
||||
void (*get_pixel) (char *pixel, int x, int y);
|
||||
|
||||
void (*draw_hline)(const char *pixel, int x1, int x2, int y);
|
||||
void (*draw_vline)(const char *pixel, int x, int y1, int y2);
|
||||
|
||||
void (*blit_line) (const char *pixel, int x, int y, rt_size_t size);
|
||||
};
|
||||
#define rt_graphix_ops(device) ((struct rt_device_graphic_ops *)(device->user_data))
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
/**
|
||||
* @addtogroup Module
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* module system
|
||||
*/
|
||||
|
||||
#define RT_MODULE_FLAG_WITHENTRY 0x00 /**< with entry point */
|
||||
#define RT_MODULE_FLAG_WITHOUTENTRY 0x01 /**< without entry point */
|
||||
|
||||
/**
|
||||
* Application Module structure
|
||||
*/
|
||||
struct rt_module
|
||||
{
|
||||
struct rt_object parent; /**< inherit from object */
|
||||
|
||||
rt_uint8_t *module_space; /**< module memory space */
|
||||
|
||||
void *module_entry; /**< the entry address of module */
|
||||
rt_thread_t module_thread; /**< the main thread of module */
|
||||
|
||||
rt_uint8_t* module_cmd_line; /**< module command line */
|
||||
rt_uint32_t module_cmd_size; /**< the size of module command line */
|
||||
|
||||
#ifdef RT_USING_SLAB
|
||||
/* module memory allocator */
|
||||
void *mem_list; /**< module's free memory list */
|
||||
void *page_array; /**< module's using pages */
|
||||
rt_uint32_t page_cnt; /**< module's using pages count */
|
||||
#endif
|
||||
|
||||
rt_uint16_t nref; /**< reference count */
|
||||
|
||||
rt_uint16_t nsym; /**< number of symbol in the module */
|
||||
struct rt_module_symtab *symtab; /**< module symbol table */
|
||||
|
||||
/* object in this module, module object is the last basic object type */
|
||||
struct rt_object_information module_object[RT_Object_Class_Unknown];
|
||||
};
|
||||
typedef struct rt_module *rt_module_t;
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* File : rthw.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
|
||||
* 2006-03-18 Bernard the first version
|
||||
* 2006-04-25 Bernard add rt_hw_context_switch_interrupt declaration
|
||||
* 2006-09-24 Bernard add rt_hw_context_switch_to declaration
|
||||
* 2012-12-29 Bernard add rt_hw_exception_install declaration
|
||||
*/
|
||||
|
||||
#ifndef __RT_HW_H__
|
||||
#define __RT_HW_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* CPU interfaces
|
||||
*/
|
||||
void rt_hw_cpu_icache_enable(void);
|
||||
void rt_hw_cpu_icache_disable(void);
|
||||
rt_base_t rt_hw_cpu_icache_status(void);
|
||||
void rt_hw_cpu_dcache_enable(void);
|
||||
void rt_hw_cpu_dcache_disable(void);
|
||||
rt_base_t rt_hw_cpu_dcache_status(void);
|
||||
void rt_hw_cpu_reset(void);
|
||||
void rt_hw_cpu_shutdown(void);
|
||||
|
||||
rt_uint8_t *rt_hw_stack_init(void *entry,
|
||||
void *parameter,
|
||||
rt_uint8_t *stack_addr,
|
||||
void *exit);
|
||||
|
||||
/*
|
||||
* Interrupt handler definition
|
||||
*/
|
||||
typedef void (*rt_isr_handler_t)(int vector, void *param);
|
||||
|
||||
struct rt_irq_desc
|
||||
{
|
||||
rt_isr_handler_t handler;
|
||||
void *param;
|
||||
|
||||
#ifdef RT_USING_INTERRUPT_INFO
|
||||
char name[RT_NAME_MAX];
|
||||
rt_uint32_t counter;
|
||||
#endif
|
||||
};
|
||||
|
||||
/*
|
||||
* Interrupt interfaces
|
||||
*/
|
||||
void rt_hw_interrupt_init(void);
|
||||
void rt_hw_interrupt_mask(int vector);
|
||||
void rt_hw_interrupt_umask(int vector);
|
||||
rt_isr_handler_t rt_hw_interrupt_install(int vector,
|
||||
rt_isr_handler_t handler,
|
||||
void *param,
|
||||
char *name);
|
||||
|
||||
rt_base_t rt_hw_interrupt_disable(void);
|
||||
void rt_hw_interrupt_enable(rt_base_t level);
|
||||
|
||||
/*
|
||||
* Context interfaces
|
||||
*/
|
||||
void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to);
|
||||
void rt_hw_context_switch_to(rt_uint32_t to);
|
||||
void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to);
|
||||
|
||||
void rt_hw_console_output(const char *str);
|
||||
|
||||
void rt_hw_backtrace(rt_uint32_t *fp, rt_uint32_t thread_entry);
|
||||
void rt_hw_show_memory(rt_uint32_t addr, rt_uint32_t size);
|
||||
|
||||
/*
|
||||
* Exception interfaces
|
||||
*/
|
||||
void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* File : rtm.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.
|
||||
*/
|
||||
|
||||
#ifndef __RTM_H__
|
||||
#define __RTM_H__
|
||||
|
||||
#include <rtdef.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
struct rt_module_symtab
|
||||
{
|
||||
void *addr;
|
||||
const char *name;
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma section("RTMSymTab$f",read)
|
||||
#define RTM_EXPORT(symbol) \
|
||||
__declspec(allocate("RTMSymTab$f"))const char __rtmsym_##symbol##_name[] = "__vs_rtm_"#symbol;
|
||||
#pragma comment(linker, "/merge:RTMSymTab=mytext")
|
||||
|
||||
#elif defined(__MINGW32__)
|
||||
#define RTM_EXPORT(symbol)
|
||||
|
||||
#else
|
||||
#define RTM_EXPORT(symbol) \
|
||||
const char __rtmsym_##symbol##_name[] = #symbol; \
|
||||
const struct rt_module_symtab __rtmsym_##symbol SECTION("RTMSymTab")= \
|
||||
{ \
|
||||
(void *)&symbol, \
|
||||
__rtmsym_##symbol##_name \
|
||||
};
|
||||
#endif
|
||||
|
||||
#else
|
||||
#define RTM_EXPORT(symbol)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* File : rtservice.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
|
||||
* 2006-03-16 Bernard the first version
|
||||
* 2006-09-07 Bernard move the kservice APIs to rtthread.h
|
||||
* 2007-06-27 Bernard fix the rt_list_remove bug
|
||||
* 2012-03-22 Bernard rename kservice.h to rtservice.h
|
||||
*/
|
||||
|
||||
#ifndef __RT_SERVICE_H__
|
||||
#define __RT_SERVICE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @addtogroup KernelService
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* @brief initialize a list object
|
||||
*/
|
||||
#define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }
|
||||
|
||||
/**
|
||||
* @brief initialize a list
|
||||
*
|
||||
* @param l list to be initialized
|
||||
*/
|
||||
rt_inline void rt_list_init(rt_list_t *l)
|
||||
{
|
||||
l->next = l->prev = l;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief insert a node after a list
|
||||
*
|
||||
* @param l list to insert it
|
||||
* @param n new node to be inserted
|
||||
*/
|
||||
rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
|
||||
{
|
||||
l->next->prev = n;
|
||||
n->next = l->next;
|
||||
|
||||
l->next = n;
|
||||
n->prev = l;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief insert a node before a list
|
||||
*
|
||||
* @param n new node to be inserted
|
||||
* @param l list to insert it
|
||||
*/
|
||||
rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
|
||||
{
|
||||
l->prev->next = n;
|
||||
n->prev = l->prev;
|
||||
|
||||
l->prev = n;
|
||||
n->next = l;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief remove node from list.
|
||||
* @param n the node to remove from the list.
|
||||
*/
|
||||
rt_inline void rt_list_remove(rt_list_t *n)
|
||||
{
|
||||
n->next->prev = n->prev;
|
||||
n->prev->next = n->next;
|
||||
|
||||
n->next = n->prev = n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tests whether a list is empty
|
||||
* @param l the list to test.
|
||||
*/
|
||||
rt_inline int rt_list_isempty(const rt_list_t *l)
|
||||
{
|
||||
return l->next == l;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get the struct for this entry
|
||||
* @param node the entry point
|
||||
* @param type the type of structure
|
||||
* @param member the name of list in structure
|
||||
*/
|
||||
#define rt_list_entry(node, type, member) \
|
||||
((type *)((char *)(node) - (unsigned long)(&((type *)0)->member)))
|
||||
|
||||
/*@}*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,519 @@
|
||||
/*
|
||||
* File : rtthread.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
|
||||
* 2006-03-18 Bernard the first version
|
||||
* 2006-04-26 Bernard add semaphore APIs
|
||||
* 2006-08-10 Bernard add version information
|
||||
* 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static
|
||||
* 2007-03-03 Bernard clean up the definitions to rtdef.h
|
||||
* 2010-04-11 yi.qiu add module feature
|
||||
* 2013-06-24 Bernard add rt_kprintf re-define when not use RT_USING_CONSOLE.
|
||||
*/
|
||||
|
||||
#ifndef __RT_THREAD_H__
|
||||
#define __RT_THREAD_H__
|
||||
|
||||
#include <rtconfig.h>
|
||||
#include <rtdebug.h>
|
||||
#include <rtdef.h>
|
||||
#include <rtservice.h>
|
||||
#include <rtm.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @addtogroup KernelObject
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* kernel object interface
|
||||
*/
|
||||
void rt_system_object_init(void);
|
||||
struct rt_object_information *
|
||||
rt_object_get_information(enum rt_object_class_type type);
|
||||
void rt_object_init(struct rt_object *object,
|
||||
enum rt_object_class_type type,
|
||||
const char *name);
|
||||
void rt_object_detach(rt_object_t object);
|
||||
rt_object_t rt_object_allocate(enum rt_object_class_type type,
|
||||
const char *name);
|
||||
void rt_object_delete(rt_object_t object);
|
||||
rt_bool_t rt_object_is_systemobject(rt_object_t object);
|
||||
rt_object_t rt_object_find(const char *name, rt_uint8_t type);
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
void rt_object_attach_sethook(void (*hook)(struct rt_object *object));
|
||||
void rt_object_detach_sethook(void (*hook)(struct rt_object *object));
|
||||
void rt_object_trytake_sethook(void (*hook)(struct rt_object *object));
|
||||
void rt_object_take_sethook(void (*hook)(struct rt_object *object));
|
||||
void rt_object_put_sethook(void (*hook)(struct rt_object *object));
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @addtogroup Clock
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* clock & timer interface
|
||||
*/
|
||||
void rt_system_tick_init(void);
|
||||
rt_tick_t rt_tick_get(void);
|
||||
void rt_tick_set(rt_tick_t tick);
|
||||
void rt_tick_increase(void);
|
||||
rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms);
|
||||
|
||||
void rt_system_timer_init(void);
|
||||
void rt_system_timer_thread_init(void);
|
||||
|
||||
void rt_timer_init(rt_timer_t timer,
|
||||
const char *name,
|
||||
void (*timeout)(void *parameter),
|
||||
void *parameter,
|
||||
rt_tick_t time,
|
||||
rt_uint8_t flag);
|
||||
rt_err_t rt_timer_detach(rt_timer_t timer);
|
||||
rt_timer_t rt_timer_create(const char *name,
|
||||
void (*timeout)(void *parameter),
|
||||
void *parameter,
|
||||
rt_tick_t time,
|
||||
rt_uint8_t flag);
|
||||
rt_err_t rt_timer_delete(rt_timer_t timer);
|
||||
rt_err_t rt_timer_start(rt_timer_t timer);
|
||||
rt_err_t rt_timer_stop(rt_timer_t timer);
|
||||
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg);
|
||||
|
||||
rt_tick_t rt_timer_next_timeout_tick(void);
|
||||
void rt_timer_check(void);
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer));
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @addtogroup Thread
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* thread interface
|
||||
*/
|
||||
rt_err_t rt_thread_init(struct rt_thread *thread,
|
||||
const char *name,
|
||||
void (*entry)(void *parameter),
|
||||
void *parameter,
|
||||
void *stack_start,
|
||||
rt_uint32_t stack_size,
|
||||
rt_uint8_t priority,
|
||||
rt_uint32_t tick);
|
||||
rt_err_t rt_thread_detach(rt_thread_t thread);
|
||||
rt_thread_t rt_thread_create(const char *name,
|
||||
void (*entry)(void *parameter),
|
||||
void *parameter,
|
||||
rt_uint32_t stack_size,
|
||||
rt_uint8_t priority,
|
||||
rt_uint32_t tick);
|
||||
rt_thread_t rt_thread_self(void);
|
||||
rt_thread_t rt_thread_find(char *name);
|
||||
rt_err_t rt_thread_startup(rt_thread_t thread);
|
||||
rt_err_t rt_thread_delete(rt_thread_t thread);
|
||||
|
||||
rt_err_t rt_thread_yield(void);
|
||||
rt_err_t rt_thread_delay(rt_tick_t tick);
|
||||
rt_err_t rt_thread_control(rt_thread_t thread, rt_uint8_t cmd, void *arg);
|
||||
rt_err_t rt_thread_suspend(rt_thread_t thread);
|
||||
rt_err_t rt_thread_resume(rt_thread_t thread);
|
||||
void rt_thread_timeout(void *parameter);
|
||||
|
||||
/*
|
||||
* idle thread interface
|
||||
*/
|
||||
void rt_thread_idle_init(void);
|
||||
#ifdef RT_USING_HOOK
|
||||
void rt_thread_idle_sethook(void (*hook)(void));
|
||||
#endif
|
||||
void rt_thread_idle_excute(void);
|
||||
|
||||
/*
|
||||
* schedule service
|
||||
*/
|
||||
void rt_system_scheduler_init(void);
|
||||
void rt_system_scheduler_start(void);
|
||||
|
||||
void rt_schedule(void);
|
||||
void rt_schedule_insert_thread(struct rt_thread *thread);
|
||||
void rt_schedule_remove_thread(struct rt_thread *thread);
|
||||
|
||||
void rt_enter_critical(void);
|
||||
void rt_exit_critical(void);
|
||||
rt_uint16_t rt_critical_level(void);
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
void rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to));
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @addtogroup MM
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* memory management interface
|
||||
*/
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
/*
|
||||
* memory pool interface
|
||||
*/
|
||||
rt_err_t rt_mp_init(struct rt_mempool *mp,
|
||||
const char *name,
|
||||
void *start,
|
||||
rt_size_t size,
|
||||
rt_size_t block_size);
|
||||
rt_err_t rt_mp_detach(struct rt_mempool *mp);
|
||||
rt_mp_t rt_mp_create(const char *name,
|
||||
rt_size_t block_count,
|
||||
rt_size_t block_size);
|
||||
rt_err_t rt_mp_delete(rt_mp_t mp);
|
||||
|
||||
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time);
|
||||
void rt_mp_free(void *block);
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block));
|
||||
void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block));
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/*
|
||||
* heap memory interface
|
||||
*/
|
||||
void rt_system_heap_init(void *begin_addr, void *end_addr);
|
||||
|
||||
void *rt_malloc(rt_size_t nbytes);
|
||||
void rt_free(void *ptr);
|
||||
void *rt_realloc(void *ptr, rt_size_t nbytes);
|
||||
void *rt_calloc(rt_size_t count, rt_size_t size);
|
||||
void *rt_malloc_align(rt_size_t size, rt_size_t align);
|
||||
void rt_free_align(void *ptr);
|
||||
|
||||
void rt_memory_info(rt_uint32_t *total,
|
||||
rt_uint32_t *used,
|
||||
rt_uint32_t *max_used);
|
||||
|
||||
#ifdef RT_USING_SLAB
|
||||
void *rt_page_alloc(rt_size_t npages);
|
||||
void rt_page_free(void *addr, rt_size_t npages);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
void rt_malloc_sethook(void (*hook)(void *ptr, rt_uint32_t size));
|
||||
void rt_free_sethook(void (*hook)(void *ptr));
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MEMHEAP
|
||||
/**
|
||||
* memory heap object interface
|
||||
*/
|
||||
rt_err_t rt_memheap_init(struct rt_memheap *memheap,
|
||||
const char *name,
|
||||
void *start_addr,
|
||||
rt_uint32_t size);
|
||||
rt_err_t rt_memheap_detach(struct rt_memheap *heap);
|
||||
void* rt_memheap_alloc(struct rt_memheap *heap, rt_uint32_t size);
|
||||
void *rt_memheap_realloc(struct rt_memheap* heap, void* ptr, rt_size_t newsize);
|
||||
void rt_memheap_free(void *ptr);
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
* @addtogroup IPC
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
#ifdef RT_USING_SEMAPHORE
|
||||
/*
|
||||
* semaphore interface
|
||||
*/
|
||||
rt_err_t rt_sem_init(rt_sem_t sem,
|
||||
const char *name,
|
||||
rt_uint32_t value,
|
||||
rt_uint8_t flag);
|
||||
rt_err_t rt_sem_detach(rt_sem_t sem);
|
||||
rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag);
|
||||
rt_err_t rt_sem_delete(rt_sem_t sem);
|
||||
|
||||
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time);
|
||||
rt_err_t rt_sem_trytake(rt_sem_t sem);
|
||||
rt_err_t rt_sem_release(rt_sem_t sem);
|
||||
rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void *arg);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MUTEX
|
||||
/*
|
||||
* mutex interface
|
||||
*/
|
||||
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag);
|
||||
rt_err_t rt_mutex_detach(rt_mutex_t mutex);
|
||||
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag);
|
||||
rt_err_t rt_mutex_delete(rt_mutex_t mutex);
|
||||
|
||||
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time);
|
||||
rt_err_t rt_mutex_release(rt_mutex_t mutex);
|
||||
rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void *arg);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_EVENT
|
||||
/*
|
||||
* event interface
|
||||
*/
|
||||
rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag);
|
||||
rt_err_t rt_event_detach(rt_event_t event);
|
||||
rt_event_t rt_event_create(const char *name, rt_uint8_t flag);
|
||||
rt_err_t rt_event_delete(rt_event_t event);
|
||||
|
||||
rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set);
|
||||
rt_err_t rt_event_recv(rt_event_t event,
|
||||
rt_uint32_t set,
|
||||
rt_uint8_t opt,
|
||||
rt_int32_t timeout,
|
||||
rt_uint32_t *recved);
|
||||
rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void *arg);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MAILBOX
|
||||
/*
|
||||
* mailbox interface
|
||||
*/
|
||||
rt_err_t rt_mb_init(rt_mailbox_t mb,
|
||||
const char *name,
|
||||
void *msgpool,
|
||||
rt_size_t size,
|
||||
rt_uint8_t flag);
|
||||
rt_err_t rt_mb_detach(rt_mailbox_t mb);
|
||||
rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag);
|
||||
rt_err_t rt_mb_delete(rt_mailbox_t mb);
|
||||
|
||||
rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value);
|
||||
rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
|
||||
rt_uint32_t value,
|
||||
rt_int32_t timeout);
|
||||
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout);
|
||||
rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void *arg);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MESSAGEQUEUE
|
||||
/*
|
||||
* message queue interface
|
||||
*/
|
||||
rt_err_t rt_mq_init(rt_mq_t mq,
|
||||
const char *name,
|
||||
void *msgpool,
|
||||
rt_size_t msg_size,
|
||||
rt_size_t pool_size,
|
||||
rt_uint8_t flag);
|
||||
rt_err_t rt_mq_detach(rt_mq_t mq);
|
||||
rt_mq_t rt_mq_create(const char *name,
|
||||
rt_size_t msg_size,
|
||||
rt_size_t max_msgs,
|
||||
rt_uint8_t flag);
|
||||
rt_err_t rt_mq_delete(rt_mq_t mq);
|
||||
|
||||
rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size);
|
||||
rt_err_t rt_mq_urgent(rt_mq_t mq, void *buffer, rt_size_t size);
|
||||
rt_err_t rt_mq_recv(rt_mq_t mq,
|
||||
void *buffer,
|
||||
rt_size_t size,
|
||||
rt_int32_t timeout);
|
||||
rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg);
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
#ifdef RT_USING_DEVICE
|
||||
/**
|
||||
* @addtogroup Device
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* device (I/O) system interface
|
||||
*/
|
||||
rt_device_t rt_device_find(const char *name);
|
||||
|
||||
rt_err_t rt_device_register(rt_device_t dev,
|
||||
const char *name,
|
||||
rt_uint16_t flags);
|
||||
rt_err_t rt_device_unregister(rt_device_t dev);
|
||||
rt_err_t rt_device_init_all(void);
|
||||
|
||||
rt_err_t
|
||||
rt_device_set_rx_indicate(rt_device_t dev,
|
||||
rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size));
|
||||
rt_err_t
|
||||
rt_device_set_tx_complete(rt_device_t dev,
|
||||
rt_err_t (*tx_done)(rt_device_t dev, void *buffer));
|
||||
|
||||
rt_err_t rt_device_init (rt_device_t dev);
|
||||
rt_err_t rt_device_open (rt_device_t dev, rt_uint16_t oflag);
|
||||
rt_err_t rt_device_close(rt_device_t dev);
|
||||
rt_size_t rt_device_read (rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size);
|
||||
rt_size_t rt_device_write(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size);
|
||||
rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg);
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
/**
|
||||
* @addtogroup Module
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* module interface
|
||||
*/
|
||||
rt_module_t rt_module_load(const char *name, void *module_ptr);
|
||||
rt_err_t rt_module_unload(rt_module_t module);
|
||||
#ifdef RT_USING_DFS
|
||||
rt_module_t rt_module_open(const char *filename);
|
||||
rt_module_t rt_module_exec_cmd(const char *path, const char* cmd_line, int size);
|
||||
#endif
|
||||
void *rt_module_malloc(rt_size_t size);
|
||||
void *rt_module_realloc(void *ptr, rt_size_t size);
|
||||
void rt_module_free(rt_module_t module, void *addr);
|
||||
rt_module_t rt_module_self(void);
|
||||
rt_module_t rt_module_find(const char *name);
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
void rt_module_load_sethook(void (*hook)(rt_module_t module));
|
||||
void rt_module_unload_sethook(void (*hook)(rt_module_t module));
|
||||
#endif
|
||||
|
||||
void rt_module_init_object_container(struct rt_module *module);
|
||||
rt_err_t rt_module_destroy(rt_module_t module);
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
/*
|
||||
* interrupt service
|
||||
*/
|
||||
|
||||
/*
|
||||
* rt_interrupt_enter and rt_interrupt_leave only can be called by BSP
|
||||
*/
|
||||
void rt_interrupt_enter(void);
|
||||
void rt_interrupt_leave(void);
|
||||
|
||||
/*
|
||||
* the number of nested interrupts.
|
||||
*/
|
||||
rt_uint8_t rt_interrupt_get_nest(void);
|
||||
|
||||
/**
|
||||
* application module
|
||||
*/
|
||||
int rt_system_module_init(void);
|
||||
|
||||
/**
|
||||
* @addtogroup KernelService
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/*
|
||||
* general kernel service
|
||||
*/
|
||||
#ifndef RT_USING_CONSOLE
|
||||
#define rt_kprintf(...)
|
||||
#else
|
||||
void rt_kprintf(const char *fmt, ...);
|
||||
#endif
|
||||
rt_int32_t rt_vsprintf(char *dest, const char *format, va_list arg_ptr);
|
||||
rt_int32_t rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args);
|
||||
rt_int32_t rt_sprintf(char *buf ,const char *format, ...);
|
||||
rt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *format, ...);
|
||||
|
||||
#if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
|
||||
rt_device_t rt_console_set_device(const char *name);
|
||||
rt_device_t rt_console_get_device(void);
|
||||
#endif
|
||||
|
||||
rt_err_t rt_get_errno(void);
|
||||
void rt_set_errno(rt_err_t no);
|
||||
int *_rt_errno(void);
|
||||
#ifndef RT_USING_NEWLIB
|
||||
#ifndef errno
|
||||
#define errno *_rt_errno()
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void *rt_memset(void *src, int c, rt_ubase_t n);
|
||||
void *rt_memcpy(void *dest, const void *src, rt_ubase_t n);
|
||||
|
||||
rt_ubase_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count);
|
||||
rt_ubase_t rt_strcmp (const char *cs, const char *ct);
|
||||
rt_ubase_t rt_strlen (const char *src);
|
||||
char *rt_strdup(const char *s);
|
||||
|
||||
char *rt_strstr(const char *str1, const char *str2);
|
||||
rt_int32_t rt_sscanf(const char *buf, const char *fmt, ...);
|
||||
char *rt_strncpy(char *dest, const char *src, rt_ubase_t n);
|
||||
void *rt_memmove(void *dest, const void *src, rt_ubase_t n);
|
||||
rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_ubase_t count);
|
||||
rt_uint32_t rt_strcasecmp(const char *a, const char *b);
|
||||
|
||||
void rt_show_version(void);
|
||||
|
||||
/*@}*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
Import('RTT_ROOT')
|
||||
Import('rtconfig')
|
||||
from building import *
|
||||
|
||||
comm = rtconfig.ARCH + '/common'
|
||||
path = rtconfig.ARCH + '/' + rtconfig.CPU
|
||||
|
||||
# The set of source files associated with this SConscript file.
|
||||
if rtconfig.PLATFORM == 'armcc':
|
||||
src = Glob(path + '/*.c') + Glob(path + '/*_rvds.S') + Glob(comm + '/*.c')
|
||||
|
||||
if rtconfig.PLATFORM == 'gcc':
|
||||
src = Glob(path + '/*.c') + Glob(path + '/*_gcc.S') + Glob(comm + '/*.c') + Glob(path + '/*_init.S')
|
||||
|
||||
if rtconfig.PLATFORM == 'iar':
|
||||
src = Glob(path + '/*.c') + Glob(path + '/*_iar.S') + Glob(comm + '/*.c')
|
||||
|
||||
if rtconfig.PLATFORM == 'cl':
|
||||
src = Glob(path + '/*.c')
|
||||
|
||||
if rtconfig.PLATFORM == 'mingw':
|
||||
src = Glob(path + '/*.c')
|
||||
|
||||
CPPPATH = [RTT_ROOT + '/libcpu/' + rtconfig.ARCH + '/' + rtconfig.CPU, RTT_ROOT + '/libcpu/' + rtconfig.ARCH + '/common']
|
||||
group = DefineGroup(rtconfig.CPU.upper(), src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* File : context_gcc.S
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2009, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2009-10-11 Bernard first version
|
||||
* 2012-01-01 aozima support context switch load/store FPU register.
|
||||
* 2013-06-18 aozima add restore MSP feature.
|
||||
* 2013-06-23 aozima support lazy stack optimized.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup cortex-m4
|
||||
*/
|
||||
/*@{*/
|
||||
|
||||
.cpu cortex-m4
|
||||
.syntax unified
|
||||
.thumb
|
||||
.text
|
||||
|
||||
.equ SCB_VTOR, 0xE000ED08 /* Vector Table Offset Register */
|
||||
.equ NVIC_INT_CTRL, 0xE000ED04 /* interrupt control state register */
|
||||
.equ NVIC_SYSPRI2, 0xE000ED20 /* system priority register (2) */
|
||||
.equ NVIC_PENDSV_PRI, 0x00FF0000 /* PendSV priority value (lowest) */
|
||||
.equ NVIC_PENDSVSET, 0x10000000 /* value to trigger PendSV exception */
|
||||
|
||||
/*
|
||||
* rt_base_t rt_hw_interrupt_disable();
|
||||
*/
|
||||
.global rt_hw_interrupt_disable
|
||||
.type rt_hw_interrupt_disable, %function
|
||||
rt_hw_interrupt_disable:
|
||||
MRS r0, PRIMASK
|
||||
CPSID I
|
||||
BX LR
|
||||
|
||||
/*
|
||||
* void rt_hw_interrupt_enable(rt_base_t level);
|
||||
*/
|
||||
.global rt_hw_interrupt_enable
|
||||
.type rt_hw_interrupt_enable, %function
|
||||
rt_hw_interrupt_enable:
|
||||
MSR PRIMASK, r0
|
||||
BX LR
|
||||
|
||||
/*
|
||||
* void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
|
||||
* r0 --> from
|
||||
* r1 --> to
|
||||
*/
|
||||
.global rt_hw_context_switch_interrupt
|
||||
.type rt_hw_context_switch_interrupt, %function
|
||||
.global rt_hw_context_switch
|
||||
.type rt_hw_context_switch, %function
|
||||
|
||||
rt_hw_context_switch_interrupt:
|
||||
rt_hw_context_switch:
|
||||
/* set rt_thread_switch_interrupt_flag to 1 */
|
||||
LDR r2, =rt_thread_switch_interrupt_flag
|
||||
LDR r3, [r2]
|
||||
CMP r3, #1
|
||||
BEQ _reswitch
|
||||
MOV r3, #1
|
||||
STR r3, [r2]
|
||||
|
||||
LDR r2, =rt_interrupt_from_thread /* set rt_interrupt_from_thread */
|
||||
STR r0, [r2]
|
||||
|
||||
_reswitch:
|
||||
LDR r2, =rt_interrupt_to_thread /* set rt_interrupt_to_thread */
|
||||
STR r1, [r2]
|
||||
|
||||
LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
|
||||
LDR r1, =NVIC_PENDSVSET
|
||||
STR r1, [r0]
|
||||
BX LR
|
||||
|
||||
/* r0 --> swith from thread stack
|
||||
* r1 --> swith to thread stack
|
||||
* psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
|
||||
*/
|
||||
.global PendSV_Handler
|
||||
.type PendSV_Handler, %function
|
||||
PendSV_Handler:
|
||||
/* disable interrupt to protect context switch */
|
||||
MRS r2, PRIMASK
|
||||
CPSID I
|
||||
|
||||
/* get rt_thread_switch_interrupt_flag */
|
||||
LDR r0, =rt_thread_switch_interrupt_flag
|
||||
LDR r1, [r0]
|
||||
CBZ r1, pendsv_exit /* pendsv already handled */
|
||||
|
||||
/* clear rt_thread_switch_interrupt_flag to 0 */
|
||||
MOV r1, #0x00
|
||||
STR r1, [r0]
|
||||
|
||||
LDR r0, =rt_interrupt_from_thread
|
||||
LDR r1, [r0]
|
||||
CBZ r1, swtich_to_thread /* skip register save at the first time */
|
||||
|
||||
MRS r1, psp /* get from thread stack pointer */
|
||||
|
||||
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
|
||||
TST lr, #0x10 /* if(!EXC_RETURN[4]) */
|
||||
VSTMDBEQ r1!, {d8 - d15} /* push FPU register s16~s31 */
|
||||
#endif
|
||||
|
||||
STMFD r1!, {r4 - r11} /* push r4 - r11 register */
|
||||
|
||||
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
|
||||
MOV r4, #0x00 /* flag = 0 */
|
||||
|
||||
TST lr, #0x10 /* if(!EXC_RETURN[4]) */
|
||||
MOVEQ r4, #0x01 /* flag = 1 */
|
||||
|
||||
STMFD r1!, {r4} /* push flag */
|
||||
#endif
|
||||
|
||||
LDR r0, [r0]
|
||||
STR r1, [r0] /* update from thread stack pointer */
|
||||
|
||||
swtich_to_thread:
|
||||
LDR r1, =rt_interrupt_to_thread
|
||||
LDR r1, [r1]
|
||||
LDR r1, [r1] /* load thread stack pointer */
|
||||
|
||||
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
|
||||
LDMFD r1!, {r3} /* pop flag */
|
||||
#endif
|
||||
|
||||
LDMFD r1!, {r4 - r11} /* pop r4 - r11 register */
|
||||
|
||||
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
|
||||
CMP r3, #0 /* if(flag_r3 != 0) */
|
||||
VLDMIANE r1!, {d8 - d15} /* pop FPU register s16~s31 */
|
||||
#endif
|
||||
|
||||
MSR psp, r1 /* update stack pointer */
|
||||
|
||||
pendsv_exit:
|
||||
/* restore interrupt */
|
||||
MSR PRIMASK, r2
|
||||
|
||||
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
|
||||
ORR lr, lr, #0x10 /* lr |= (1 << 4), clean FPCA. */
|
||||
CMP r3, #0 /* if(flag_r3 != 0) */
|
||||
BICNE lr, lr, #0x10 /* lr &= ~(1 << 4), set FPCA. */
|
||||
#endif
|
||||
|
||||
ORR lr, lr, #0x04
|
||||
BX lr
|
||||
|
||||
/*
|
||||
* void rt_hw_context_switch_to(rt_uint32 to);
|
||||
* r0 --> to
|
||||
*/
|
||||
.global rt_hw_context_switch_to
|
||||
.type rt_hw_context_switch_to, %function
|
||||
rt_hw_context_switch_to:
|
||||
LDR r1, =rt_interrupt_to_thread
|
||||
STR r0, [r1]
|
||||
|
||||
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
|
||||
/* CLEAR CONTROL.FPCA */
|
||||
MRS r2, CONTROL /* read */
|
||||
BIC r2, #0x04 /* modify */
|
||||
MSR CONTROL, r2 /* write-back */
|
||||
#endif
|
||||
|
||||
/* set from thread to 0 */
|
||||
LDR r1, =rt_interrupt_from_thread
|
||||
MOV r0, #0x0
|
||||
STR r0, [r1]
|
||||
|
||||
/* set interrupt flag to 1 */
|
||||
LDR r1, =rt_thread_switch_interrupt_flag
|
||||
MOV r0, #1
|
||||
STR r0, [r1]
|
||||
|
||||
/* set the PendSV exception priority */
|
||||
LDR r0, =NVIC_SYSPRI2
|
||||
LDR r1, =NVIC_PENDSV_PRI
|
||||
LDR.W r2, [r0,#0x00] /* read */
|
||||
ORR r1,r1,r2 /* modify */
|
||||
STR r1, [r0] /* write-back */
|
||||
|
||||
LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
|
||||
LDR r1, =NVIC_PENDSVSET
|
||||
STR r1, [r0]
|
||||
|
||||
/* restore MSP */
|
||||
LDR r0, =SCB_VTOR
|
||||
LDR r0, [r0]
|
||||
LDR r0, [r0]
|
||||
NOP
|
||||
MSR msp, r0
|
||||
|
||||
CPSIE I /* enable interrupts at processor level */
|
||||
|
||||
/* never reach here! */
|
||||
|
||||
/* compatible with old version */
|
||||
.global rt_hw_interrupt_thread_switch
|
||||
.type rt_hw_interrupt_thread_switch, %function
|
||||
rt_hw_interrupt_thread_switch:
|
||||
BX lr
|
||||
NOP
|
||||
|
||||
.global HardFault_Handler
|
||||
.type HardFault_Handler, %function
|
||||
HardFault_Handler:
|
||||
/* get current context */
|
||||
MRS r0, psp /* get fault thread stack pointer */
|
||||
PUSH {lr}
|
||||
BL rt_hw_hard_fault_exception
|
||||
POP {lr}
|
||||
|
||||
ORR lr, lr, #0x04
|
||||
BX lr
|
||||
@@ -0,0 +1,232 @@
|
||||
;/*
|
||||
; * File : context_iar.S
|
||||
; * This file is part of RT-Thread RTOS
|
||||
; * COPYRIGHT (C) 2009, RT-Thread Development Team
|
||||
; *
|
||||
; * The license and distribution terms for this file may be
|
||||
; * found in the file LICENSE in this distribution or at
|
||||
; * http://www.rt-thread.org/license/LICENSE
|
||||
; *
|
||||
; * Change Logs:
|
||||
; * Date Author Notes
|
||||
; * 2009-01-17 Bernard first version
|
||||
; * 2009-09-27 Bernard add protect when contex switch occurs
|
||||
; * 2012-01-01 aozima support context switch load/store FPU register.
|
||||
; * 2013-06-18 aozima add restore MSP feature.
|
||||
; * 2013-06-23 aozima support lazy stack optimized.
|
||||
; */
|
||||
|
||||
;/**
|
||||
; * @addtogroup cortex-m4
|
||||
; */
|
||||
;/*@{*/
|
||||
|
||||
SCB_VTOR EQU 0xE000ED08 ; Vector Table Offset Register
|
||||
NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
|
||||
NVIC_SYSPRI2 EQU 0xE000ED20 ; system priority register (2)
|
||||
NVIC_PENDSV_PRI EQU 0x00FF0000 ; PendSV priority value (lowest)
|
||||
NVIC_PENDSVSET EQU 0x10000000 ; value to trigger PendSV exception
|
||||
|
||||
SECTION .text:CODE(2)
|
||||
THUMB
|
||||
REQUIRE8
|
||||
PRESERVE8
|
||||
|
||||
IMPORT rt_thread_switch_interrupt_flag
|
||||
IMPORT rt_interrupt_from_thread
|
||||
IMPORT rt_interrupt_to_thread
|
||||
|
||||
;/*
|
||||
; * rt_base_t rt_hw_interrupt_disable();
|
||||
; */
|
||||
EXPORT rt_hw_interrupt_disable
|
||||
rt_hw_interrupt_disable:
|
||||
MRS r0, PRIMASK
|
||||
CPSID I
|
||||
BX LR
|
||||
|
||||
;/*
|
||||
; * void rt_hw_interrupt_enable(rt_base_t level);
|
||||
; */
|
||||
EXPORT rt_hw_interrupt_enable
|
||||
rt_hw_interrupt_enable:
|
||||
MSR PRIMASK, r0
|
||||
BX LR
|
||||
|
||||
;/*
|
||||
; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
|
||||
; * r0 --> from
|
||||
; * r1 --> to
|
||||
; */
|
||||
EXPORT rt_hw_context_switch_interrupt
|
||||
EXPORT rt_hw_context_switch
|
||||
rt_hw_context_switch_interrupt:
|
||||
rt_hw_context_switch:
|
||||
; set rt_thread_switch_interrupt_flag to 1
|
||||
LDR r2, =rt_thread_switch_interrupt_flag
|
||||
LDR r3, [r2]
|
||||
CMP r3, #1
|
||||
BEQ _reswitch
|
||||
MOV r3, #1
|
||||
STR r3, [r2]
|
||||
|
||||
LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
|
||||
STR r0, [r2]
|
||||
|
||||
_reswitch
|
||||
LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
|
||||
STR r1, [r2]
|
||||
|
||||
LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
|
||||
LDR r1, =NVIC_PENDSVSET
|
||||
STR r1, [r0]
|
||||
BX LR
|
||||
|
||||
; r0 --> swith from thread stack
|
||||
; r1 --> swith to thread stack
|
||||
; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
|
||||
EXPORT PendSV_Handler
|
||||
PendSV_Handler:
|
||||
|
||||
; disable interrupt to protect context switch
|
||||
MRS r2, PRIMASK
|
||||
CPSID I
|
||||
|
||||
; get rt_thread_switch_interrupt_flag
|
||||
LDR r0, =rt_thread_switch_interrupt_flag
|
||||
LDR r1, [r0]
|
||||
CBZ r1, pendsv_exit ; pendsv already handled
|
||||
|
||||
; clear rt_thread_switch_interrupt_flag to 0
|
||||
MOV r1, #0x00
|
||||
STR r1, [r0]
|
||||
|
||||
LDR r0, =rt_interrupt_from_thread
|
||||
LDR r1, [r0]
|
||||
CBZ r1, swtich_to_thread ; skip register save at the first time
|
||||
|
||||
MRS r1, psp ; get from thread stack pointer
|
||||
|
||||
#if defined ( __ARMVFP__ )
|
||||
TST lr, #0x10 ; if(!EXC_RETURN[4])
|
||||
BNE skip_push_fpu
|
||||
VSTMDB r1!, {d8 - d15} ; push FPU register s16~s31
|
||||
skip_push_fpu
|
||||
#endif
|
||||
|
||||
STMFD r1!, {r4 - r11} ; push r4 - r11 register
|
||||
|
||||
#if defined ( __ARMVFP__ )
|
||||
MOV r4, #0x00 ; flag = 0
|
||||
TST lr, #0x10 ; if(!EXC_RETURN[4])
|
||||
BNE push_flag
|
||||
MOV r4, #0x01 ; flag = 1
|
||||
push_flag
|
||||
;STMFD r1!, {r4} ; push flag
|
||||
SUB r1, r1, #0x04
|
||||
STR r4, [r1]
|
||||
#endif
|
||||
|
||||
LDR r0, [r0]
|
||||
STR r1, [r0] ; update from thread stack pointer
|
||||
|
||||
swtich_to_thread
|
||||
LDR r1, =rt_interrupt_to_thread
|
||||
LDR r1, [r1]
|
||||
LDR r1, [r1] ; load thread stack pointer
|
||||
|
||||
#if defined ( __ARMVFP__ )
|
||||
LDMFD r1!, {r3} ; pop flag
|
||||
#endif
|
||||
|
||||
LDMFD r1!, {r4 - r11} ; pop r4 - r11 register
|
||||
|
||||
#if defined ( __ARMVFP__ )
|
||||
CBZ r3, skip_pop_fpu
|
||||
VLDMIA r1!, {d8 - d15} ; pop FPU register s16~s31
|
||||
skip_pop_fpu
|
||||
#endif
|
||||
|
||||
MSR psp, r1 ; update stack pointer
|
||||
|
||||
pendsv_exit
|
||||
; restore interrupt
|
||||
MSR PRIMASK, r2
|
||||
|
||||
#if defined ( __ARMVFP__ )
|
||||
ORR lr, lr, #0x10 ; lr |= (1 << 4), clean FPCA.
|
||||
CBZ r3, return_without_fpu ; if(flag_r3 != 0)
|
||||
BIC lr, lr, #0x10 ; lr &= ~(1 << 4), set FPCA.
|
||||
return_without_fpu
|
||||
#endif
|
||||
|
||||
ORR lr, lr, #0x04
|
||||
BX lr
|
||||
|
||||
;/*
|
||||
; * void rt_hw_context_switch_to(rt_uint32 to);
|
||||
; * r0 --> to
|
||||
; */
|
||||
EXPORT rt_hw_context_switch_to
|
||||
rt_hw_context_switch_to:
|
||||
LDR r1, =rt_interrupt_to_thread
|
||||
STR r0, [r1]
|
||||
|
||||
#if defined ( __ARMVFP__ )
|
||||
; CLEAR CONTROL.FPCA
|
||||
MRS r2, CONTROL ; read
|
||||
BIC r2, r2, #0x04 ; modify
|
||||
MSR CONTROL, r2 ; write-back
|
||||
#endif
|
||||
|
||||
; set from thread to 0
|
||||
LDR r1, =rt_interrupt_from_thread
|
||||
MOV r0, #0x0
|
||||
STR r0, [r1]
|
||||
|
||||
; set interrupt flag to 1
|
||||
LDR r1, =rt_thread_switch_interrupt_flag
|
||||
MOV r0, #1
|
||||
STR r0, [r1]
|
||||
|
||||
; set the PendSV exception priority
|
||||
LDR r0, =NVIC_SYSPRI2
|
||||
LDR r1, =NVIC_PENDSV_PRI
|
||||
LDR.W r2, [r0,#0x00] ; read
|
||||
ORR r1,r1,r2 ; modify
|
||||
STR r1, [r0] ; write-back
|
||||
|
||||
LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
|
||||
LDR r1, =NVIC_PENDSVSET
|
||||
STR r1, [r0]
|
||||
|
||||
; restore MSP
|
||||
LDR r0, =SCB_VTOR
|
||||
LDR r0, [r0]
|
||||
LDR r0, [r0]
|
||||
NOP
|
||||
MSR msp, r0
|
||||
|
||||
CPSIE I ; enable interrupts at processor level
|
||||
|
||||
; never reach here!
|
||||
|
||||
; compatible with old version
|
||||
EXPORT rt_hw_interrupt_thread_switch
|
||||
rt_hw_interrupt_thread_switch:
|
||||
BX lr
|
||||
|
||||
IMPORT rt_hw_hard_fault_exception
|
||||
EXPORT HardFault_Handler
|
||||
HardFault_Handler:
|
||||
|
||||
; get current context
|
||||
MRS r0, psp ; get fault thread stack pointer
|
||||
PUSH {lr}
|
||||
BL rt_hw_hard_fault_exception
|
||||
POP {lr}
|
||||
|
||||
ORR lr, lr, #0x04
|
||||
BX lr
|
||||
|
||||
END
|
||||
@@ -0,0 +1,238 @@
|
||||
;/*
|
||||
; * File : context_rvds.S
|
||||
; * This file is part of RT-Thread RTOS
|
||||
; * COPYRIGHT (C) 2009, RT-Thread Development Team
|
||||
; *
|
||||
; * The license and distribution terms for this file may be
|
||||
; * found in the file LICENSE in this distribution or at
|
||||
; * http://www.rt-thread.org/license/LICENSE
|
||||
; *
|
||||
; * Change Logs:
|
||||
; * Date Author Notes
|
||||
; * 2009-01-17 Bernard first version.
|
||||
; * 2012-01-01 aozima support context switch load/store FPU register.
|
||||
; * 2013-06-18 aozima add restore MSP feature.
|
||||
; * 2013-06-23 aozima support lazy stack optimized.
|
||||
; */
|
||||
|
||||
;/**
|
||||
; * @addtogroup cortex-m4
|
||||
; */
|
||||
;/*@{*/
|
||||
|
||||
SCB_VTOR EQU 0xE000ED08 ; Vector Table Offset Register
|
||||
NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
|
||||
NVIC_SYSPRI2 EQU 0xE000ED20 ; system priority register (2)
|
||||
NVIC_PENDSV_PRI EQU 0x00FF0000 ; PendSV priority value (lowest)
|
||||
NVIC_PENDSVSET EQU 0x10000000 ; value to trigger PendSV exception
|
||||
|
||||
AREA |.text|, CODE, READONLY, ALIGN=2
|
||||
THUMB
|
||||
REQUIRE8
|
||||
PRESERVE8
|
||||
|
||||
IMPORT rt_thread_switch_interrupt_flag
|
||||
IMPORT rt_interrupt_from_thread
|
||||
IMPORT rt_interrupt_to_thread
|
||||
|
||||
;/*
|
||||
; * rt_base_t rt_hw_interrupt_disable();
|
||||
; */
|
||||
rt_hw_interrupt_disable PROC
|
||||
EXPORT rt_hw_interrupt_disable
|
||||
MRS r0, PRIMASK
|
||||
CPSID I
|
||||
BX LR
|
||||
ENDP
|
||||
|
||||
;/*
|
||||
; * void rt_hw_interrupt_enable(rt_base_t level);
|
||||
; */
|
||||
rt_hw_interrupt_enable PROC
|
||||
EXPORT rt_hw_interrupt_enable
|
||||
MSR PRIMASK, r0
|
||||
BX LR
|
||||
ENDP
|
||||
|
||||
;/*
|
||||
; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
|
||||
; * r0 --> from
|
||||
; * r1 --> to
|
||||
; */
|
||||
rt_hw_context_switch_interrupt
|
||||
EXPORT rt_hw_context_switch_interrupt
|
||||
rt_hw_context_switch PROC
|
||||
EXPORT rt_hw_context_switch
|
||||
|
||||
; set rt_thread_switch_interrupt_flag to 1
|
||||
LDR r2, =rt_thread_switch_interrupt_flag
|
||||
LDR r3, [r2]
|
||||
CMP r3, #1
|
||||
BEQ _reswitch
|
||||
MOV r3, #1
|
||||
STR r3, [r2]
|
||||
|
||||
LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
|
||||
STR r0, [r2]
|
||||
|
||||
_reswitch
|
||||
LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
|
||||
STR r1, [r2]
|
||||
|
||||
LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
|
||||
LDR r1, =NVIC_PENDSVSET
|
||||
STR r1, [r0]
|
||||
BX LR
|
||||
ENDP
|
||||
|
||||
; r0 --> swith from thread stack
|
||||
; r1 --> swith to thread stack
|
||||
; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
|
||||
PendSV_Handler PROC
|
||||
EXPORT PendSV_Handler
|
||||
|
||||
; disable interrupt to protect context switch
|
||||
MRS r2, PRIMASK
|
||||
CPSID I
|
||||
|
||||
; get rt_thread_switch_interrupt_flag
|
||||
LDR r0, =rt_thread_switch_interrupt_flag
|
||||
LDR r1, [r0]
|
||||
CBZ r1, pendsv_exit ; pendsv already handled
|
||||
|
||||
; clear rt_thread_switch_interrupt_flag to 0
|
||||
MOV r1, #0x00
|
||||
STR r1, [r0]
|
||||
|
||||
LDR r0, =rt_interrupt_from_thread
|
||||
LDR r1, [r0]
|
||||
CBZ r1, swtich_to_thread ; skip register save at the first time
|
||||
|
||||
MRS r1, psp ; get from thread stack pointer
|
||||
|
||||
IF {FPU} != "SoftVFP"
|
||||
TST lr, #0x10 ; if(!EXC_RETURN[4])
|
||||
VSTMFDEQ r1!, {d8 - d15} ; push FPU register s16~s31
|
||||
ENDIF
|
||||
|
||||
STMFD r1!, {r4 - r11} ; push r4 - r11 register
|
||||
|
||||
IF {FPU} != "SoftVFP"
|
||||
MOV r4, #0x00 ; flag = 0
|
||||
|
||||
TST lr, #0x10 ; if(!EXC_RETURN[4])
|
||||
MOVEQ r4, #0x01 ; flag = 1
|
||||
|
||||
STMFD r1!, {r4} ; push flag
|
||||
ENDIF
|
||||
|
||||
LDR r0, [r0]
|
||||
STR r1, [r0] ; update from thread stack pointer
|
||||
|
||||
swtich_to_thread
|
||||
LDR r1, =rt_interrupt_to_thread
|
||||
LDR r1, [r1]
|
||||
LDR r1, [r1] ; load thread stack pointer
|
||||
|
||||
IF {FPU} != "SoftVFP"
|
||||
LDMFD r1!, {r3} ; pop flag
|
||||
ENDIF
|
||||
|
||||
LDMFD r1!, {r4 - r11} ; pop r4 - r11 register
|
||||
|
||||
IF {FPU} != "SoftVFP"
|
||||
CMP r3, #0 ; if(flag_r3 != 0)
|
||||
VLDMFDNE r1!, {d8 - d15} ; pop FPU register s16~s31
|
||||
ENDIF
|
||||
|
||||
MSR psp, r1 ; update stack pointer
|
||||
|
||||
pendsv_exit
|
||||
; restore interrupt
|
||||
MSR PRIMASK, r2
|
||||
|
||||
IF {FPU} != "SoftVFP"
|
||||
ORR lr, lr, #0x10 ; lr |= (1 << 4), clean FPCA.
|
||||
CMP r3, #0 ; if(flag_r3 != 0)
|
||||
BICNE lr, lr, #0x10 ; lr &= ~(1 << 4), set FPCA.
|
||||
ENDIF
|
||||
|
||||
ORR lr, lr, #0x04
|
||||
BX lr
|
||||
ENDP
|
||||
|
||||
;/*
|
||||
; * void rt_hw_context_switch_to(rt_uint32 to);
|
||||
; * r0 --> to
|
||||
; * this fucntion is used to perform the first thread switch
|
||||
; */
|
||||
rt_hw_context_switch_to PROC
|
||||
EXPORT rt_hw_context_switch_to
|
||||
; set to thread
|
||||
LDR r1, =rt_interrupt_to_thread
|
||||
STR r0, [r1]
|
||||
|
||||
IF {FPU} != "SoftVFP"
|
||||
; CLEAR CONTROL.FPCA
|
||||
MRS r2, CONTROL ; read
|
||||
BIC r2, #0x04 ; modify
|
||||
MSR CONTROL, r2 ; write-back
|
||||
ENDIF
|
||||
|
||||
; set from thread to 0
|
||||
LDR r1, =rt_interrupt_from_thread
|
||||
MOV r0, #0x0
|
||||
STR r0, [r1]
|
||||
|
||||
; set interrupt flag to 1
|
||||
LDR r1, =rt_thread_switch_interrupt_flag
|
||||
MOV r0, #1
|
||||
STR r0, [r1]
|
||||
|
||||
; set the PendSV exception priority
|
||||
LDR r0, =NVIC_SYSPRI2
|
||||
LDR r1, =NVIC_PENDSV_PRI
|
||||
LDR.W r2, [r0,#0x00] ; read
|
||||
ORR r1,r1,r2 ; modify
|
||||
STR r1, [r0] ; write-back
|
||||
|
||||
; trigger the PendSV exception (causes context switch)
|
||||
LDR r0, =NVIC_INT_CTRL
|
||||
LDR r1, =NVIC_PENDSVSET
|
||||
STR r1, [r0]
|
||||
|
||||
; restore MSP
|
||||
LDR r0, =SCB_VTOR
|
||||
LDR r0, [r0]
|
||||
LDR r0, [r0]
|
||||
NOP
|
||||
MSR msp, r0
|
||||
|
||||
; enable interrupts at processor level
|
||||
CPSIE I
|
||||
|
||||
; never reach here!
|
||||
ENDP
|
||||
|
||||
; compatible with old version
|
||||
rt_hw_interrupt_thread_switch PROC
|
||||
EXPORT rt_hw_interrupt_thread_switch
|
||||
BX lr
|
||||
NOP
|
||||
ENDP
|
||||
|
||||
IMPORT rt_hw_hard_fault_exception
|
||||
EXPORT HardFault_Handler
|
||||
HardFault_Handler PROC
|
||||
|
||||
; get current context
|
||||
MRS r0, psp ; get fault thread stack pointer
|
||||
PUSH {lr}
|
||||
BL rt_hw_hard_fault_exception
|
||||
POP {lr}
|
||||
|
||||
ORR lr, lr, #0x04
|
||||
BX lr
|
||||
ENDP
|
||||
|
||||
END
|
||||
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* File : cpuport.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-10-21 Bernard the first version.
|
||||
* 2011-10-27 aozima update for cortex-M4 FPU.
|
||||
* 2011-12-31 aozima fixed stack align issues.
|
||||
* 2012-01-01 aozima support context switch load/store FPU register.
|
||||
* 2012-12-11 lgnq fixed the coding style.
|
||||
* 2012-12-23 aozima stack addr align to 8byte.
|
||||
* 2012-12-29 Bernard Add exception hook.
|
||||
* 2013-06-23 aozima support lazy stack optimized.
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define USE_FPU /* ARMCC */ ( (defined ( __CC_ARM ) && defined ( __TARGET_FPU_VFP )) \
|
||||
/* IAR */ || (defined ( __ICCARM__ ) && defined ( __ARMVFP__ )) \
|
||||
/* GNU */ || (defined ( __GNUC__ ) && defined ( __VFP_FP__ ) && !defined(__SOFTFP__)) )
|
||||
|
||||
/* exception and interrupt handler table */
|
||||
rt_uint32_t rt_interrupt_from_thread;
|
||||
rt_uint32_t rt_interrupt_to_thread;
|
||||
rt_uint32_t rt_thread_switch_interrupt_flag;
|
||||
/* exception hook */
|
||||
static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
|
||||
|
||||
struct exception_stack_frame
|
||||
{
|
||||
rt_uint32_t r0;
|
||||
rt_uint32_t r1;
|
||||
rt_uint32_t r2;
|
||||
rt_uint32_t r3;
|
||||
rt_uint32_t r12;
|
||||
rt_uint32_t lr;
|
||||
rt_uint32_t pc;
|
||||
rt_uint32_t psr;
|
||||
};
|
||||
|
||||
struct stack_frame
|
||||
{
|
||||
#if USE_FPU
|
||||
rt_uint32_t flag;
|
||||
#endif /* USE_FPU */
|
||||
|
||||
/* r4 ~ r11 register */
|
||||
rt_uint32_t r4;
|
||||
rt_uint32_t r5;
|
||||
rt_uint32_t r6;
|
||||
rt_uint32_t r7;
|
||||
rt_uint32_t r8;
|
||||
rt_uint32_t r9;
|
||||
rt_uint32_t r10;
|
||||
rt_uint32_t r11;
|
||||
|
||||
struct exception_stack_frame exception_stack_frame;
|
||||
};
|
||||
|
||||
struct exception_stack_frame_fpu
|
||||
{
|
||||
rt_uint32_t r0;
|
||||
rt_uint32_t r1;
|
||||
rt_uint32_t r2;
|
||||
rt_uint32_t r3;
|
||||
rt_uint32_t r12;
|
||||
rt_uint32_t lr;
|
||||
rt_uint32_t pc;
|
||||
rt_uint32_t psr;
|
||||
|
||||
#if USE_FPU
|
||||
/* FPU register */
|
||||
rt_uint32_t S0;
|
||||
rt_uint32_t S1;
|
||||
rt_uint32_t S2;
|
||||
rt_uint32_t S3;
|
||||
rt_uint32_t S4;
|
||||
rt_uint32_t S5;
|
||||
rt_uint32_t S6;
|
||||
rt_uint32_t S7;
|
||||
rt_uint32_t S8;
|
||||
rt_uint32_t S9;
|
||||
rt_uint32_t S10;
|
||||
rt_uint32_t S11;
|
||||
rt_uint32_t S12;
|
||||
rt_uint32_t S13;
|
||||
rt_uint32_t S14;
|
||||
rt_uint32_t S15;
|
||||
rt_uint32_t FPSCR;
|
||||
rt_uint32_t NO_NAME;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct stack_frame_fpu
|
||||
{
|
||||
rt_uint32_t flag;
|
||||
|
||||
/* r4 ~ r11 register */
|
||||
rt_uint32_t r4;
|
||||
rt_uint32_t r5;
|
||||
rt_uint32_t r6;
|
||||
rt_uint32_t r7;
|
||||
rt_uint32_t r8;
|
||||
rt_uint32_t r9;
|
||||
rt_uint32_t r10;
|
||||
rt_uint32_t r11;
|
||||
|
||||
#if USE_FPU
|
||||
/* FPU register s16 ~ s31 */
|
||||
rt_uint32_t s16;
|
||||
rt_uint32_t s17;
|
||||
rt_uint32_t s18;
|
||||
rt_uint32_t s19;
|
||||
rt_uint32_t s20;
|
||||
rt_uint32_t s21;
|
||||
rt_uint32_t s22;
|
||||
rt_uint32_t s23;
|
||||
rt_uint32_t s24;
|
||||
rt_uint32_t s25;
|
||||
rt_uint32_t s26;
|
||||
rt_uint32_t s27;
|
||||
rt_uint32_t s28;
|
||||
rt_uint32_t s29;
|
||||
rt_uint32_t s30;
|
||||
rt_uint32_t s31;
|
||||
#endif
|
||||
|
||||
struct exception_stack_frame_fpu exception_stack_frame;
|
||||
};
|
||||
|
||||
rt_uint8_t *rt_hw_stack_init(void *tentry,
|
||||
void *parameter,
|
||||
rt_uint8_t *stack_addr,
|
||||
void *texit)
|
||||
{
|
||||
struct stack_frame *stack_frame;
|
||||
rt_uint8_t *stk;
|
||||
unsigned long i;
|
||||
|
||||
stk = stack_addr + sizeof(rt_uint32_t);
|
||||
stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stk, 8);
|
||||
stk -= sizeof(struct stack_frame);
|
||||
|
||||
stack_frame = (struct stack_frame *)stk;
|
||||
|
||||
/* init all register */
|
||||
for (i = 0; i < sizeof(struct stack_frame) / sizeof(rt_uint32_t); i ++)
|
||||
{
|
||||
((rt_uint32_t *)stack_frame)[i] = 0xdeadbeef;
|
||||
}
|
||||
|
||||
stack_frame->exception_stack_frame.r0 = (unsigned long)parameter; /* r0 : argument */
|
||||
stack_frame->exception_stack_frame.r1 = 0; /* r1 */
|
||||
stack_frame->exception_stack_frame.r2 = 0; /* r2 */
|
||||
stack_frame->exception_stack_frame.r3 = 0; /* r3 */
|
||||
stack_frame->exception_stack_frame.r12 = 0; /* r12 */
|
||||
stack_frame->exception_stack_frame.lr = (unsigned long)texit; /* lr */
|
||||
stack_frame->exception_stack_frame.pc = (unsigned long)tentry; /* entry point, pc */
|
||||
stack_frame->exception_stack_frame.psr = 0x01000000L; /* PSR */
|
||||
|
||||
#if USE_FPU
|
||||
stack_frame->flag = 0;
|
||||
#endif /* USE_FPU */
|
||||
|
||||
/* return task's current stack address */
|
||||
return stk;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function set the hook, which is invoked on fault exception handling.
|
||||
*
|
||||
* @param exception_handle the exception handling hook function.
|
||||
*/
|
||||
void rt_hw_exception_install(rt_err_t (*exception_handle)(void* context))
|
||||
{
|
||||
rt_exception_hook = exception_handle;
|
||||
}
|
||||
|
||||
void rt_hw_hard_fault_exception(struct exception_stack_frame *exception_stack)
|
||||
{
|
||||
extern long list_thread(void);
|
||||
|
||||
if (rt_exception_hook != RT_NULL)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
result = rt_exception_hook(exception_stack);
|
||||
if (result == RT_EOK) return;
|
||||
}
|
||||
|
||||
rt_kprintf("psr: 0x%08x\n", exception_stack->psr);
|
||||
rt_kprintf(" pc: 0x%08x\n", exception_stack->pc);
|
||||
rt_kprintf(" lr: 0x%08x\n", exception_stack->lr);
|
||||
rt_kprintf("r12: 0x%08x\n", exception_stack->r12);
|
||||
rt_kprintf("r03: 0x%08x\n", exception_stack->r3);
|
||||
rt_kprintf("r02: 0x%08x\n", exception_stack->r2);
|
||||
rt_kprintf("r01: 0x%08x\n", exception_stack->r1);
|
||||
rt_kprintf("r00: 0x%08x\n", exception_stack->r0);
|
||||
|
||||
rt_kprintf("hard fault on thread: %s\n", rt_thread_self()->name);
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
list_thread();
|
||||
#endif
|
||||
|
||||
while (1);
|
||||
}
|
||||
|
||||
void rt_hw_cpu_shutdown(void)
|
||||
{
|
||||
rt_kprintf("shutdown...\n");
|
||||
|
||||
RT_ASSERT(0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
Import('RTT_ROOT')
|
||||
Import('rtconfig')
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
|
||||
CPPPATH = [RTT_ROOT + '/include']
|
||||
if rtconfig.CROSS_TOOL == 'keil' and GetDepend('RT_USING_MODULE') == True:
|
||||
LINKFLAGS = ' --keep __rtmsym_* '
|
||||
else:
|
||||
LINKFLAGS = ''
|
||||
|
||||
if GetDepend('RT_USING_MODULE') == False:
|
||||
SrcRemove(src, ['module.c'])
|
||||
|
||||
if GetDepend('RT_USING_HEAP') == False or GetDepend('RT_USING_SMALL_MEM') == False:
|
||||
SrcRemove(src, ['mem.c'])
|
||||
|
||||
if GetDepend('RT_USING_HEAP') == False or GetDepend('RT_USING_SLAB') == False:
|
||||
SrcRemove(src, ['slab.c'])
|
||||
|
||||
if GetDepend('RT_USING_MEMPOOL') == False:
|
||||
SrcRemove(src, ['mempool.c'])
|
||||
|
||||
if GetDepend('RT_USING_MEMHEAP') == False:
|
||||
SrcRemove(src, ['memheap.c'])
|
||||
if GetDepend('RT_USING_MEMHEAP_AS_HEAP'):
|
||||
SrcRemove(src, ['mem.c'])
|
||||
|
||||
if GetDepend('RT_USING_DEVICE') == False:
|
||||
SrcRemove(src, ['device.c'])
|
||||
|
||||
group = DefineGroup('Kernel', src, depend = [''], CPPPATH = CPPPATH, LINKFLAGS = LINKFLAGS)
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* File : clock.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-12 Bernard first version
|
||||
* 2006-05-27 Bernard add support for same priority thread schedule
|
||||
* 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase
|
||||
* 2010-03-08 Bernard remove rt_passed_second
|
||||
* 2010-05-20 Bernard fix the tick exceeds the maximum limits
|
||||
* 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
|
||||
* 2011-06-26 Bernard add rt_tick_set function.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
static rt_tick_t rt_tick = 0;
|
||||
|
||||
extern void rt_timer_check(void);
|
||||
|
||||
/**
|
||||
* This function will init system tick and set it to zero.
|
||||
* @ingroup SystemInit
|
||||
*
|
||||
* @deprecated since 1.1.0, this function does not need to be invoked
|
||||
* in the system initialization.
|
||||
*/
|
||||
void rt_system_tick_init(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup Clock
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will return current tick from operating system startup
|
||||
*
|
||||
* @return current tick
|
||||
*/
|
||||
rt_tick_t rt_tick_get(void)
|
||||
{
|
||||
/* return the global tick */
|
||||
return rt_tick;
|
||||
}
|
||||
RTM_EXPORT(rt_tick_get);
|
||||
|
||||
/**
|
||||
* This function will set current tick
|
||||
*/
|
||||
void rt_tick_set(rt_tick_t tick)
|
||||
{
|
||||
rt_base_t level;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_tick = tick;
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will notify kernel there is one tick passed. Normally,
|
||||
* this function is invoked by clock ISR.
|
||||
*/
|
||||
void rt_tick_increase(void)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* increase the global tick */
|
||||
++ rt_tick;
|
||||
|
||||
/* check time slice */
|
||||
thread = rt_thread_self();
|
||||
|
||||
-- thread->remaining_tick;
|
||||
if (thread->remaining_tick == 0)
|
||||
{
|
||||
/* change to initialized tick */
|
||||
thread->remaining_tick = thread->init_tick;
|
||||
|
||||
/* yield */
|
||||
rt_thread_yield();
|
||||
}
|
||||
|
||||
/* check timer */
|
||||
rt_timer_check();
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will calculate the tick from millisecond.
|
||||
*
|
||||
* @param ms the specified millisecond
|
||||
*
|
||||
* @return the calculated tick
|
||||
*/
|
||||
rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms)
|
||||
{
|
||||
/* return the calculated tick */
|
||||
return (RT_TICK_PER_SECOND * ms + 999) / 1000;
|
||||
}
|
||||
RTM_EXPORT(rt_tick_from_millisecond);
|
||||
|
||||
/*@}*/
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
|
||||
#define CPU_USAGE_CALC_TICK 10
|
||||
#define CPU_USAGE_LOOP 100
|
||||
|
||||
static rt_uint8_t cpu_usage_major = 0, cpu_usage_minor= 0;
|
||||
static rt_uint32_t total_count = 0;
|
||||
|
||||
static void cpu_usage_idle_hook()
|
||||
{
|
||||
rt_tick_t tick;
|
||||
rt_uint32_t count;
|
||||
volatile rt_uint32_t loop;
|
||||
|
||||
if (total_count == 0)
|
||||
{
|
||||
/* get total count */
|
||||
rt_enter_critical();
|
||||
tick = rt_tick_get();
|
||||
while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
|
||||
{
|
||||
total_count ++;
|
||||
loop = 0;
|
||||
while (loop < CPU_USAGE_LOOP) loop ++;
|
||||
}
|
||||
rt_exit_critical();
|
||||
}
|
||||
|
||||
count = 0;
|
||||
/* get CPU usage */
|
||||
tick = rt_tick_get();
|
||||
while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
|
||||
{
|
||||
count ++;
|
||||
loop = 0;
|
||||
while (loop < CPU_USAGE_LOOP) loop ++;
|
||||
}
|
||||
|
||||
/* calculate major and minor */
|
||||
if (count < total_count)
|
||||
{
|
||||
count = total_count - count;
|
||||
cpu_usage_major = (count * 100) / total_count;
|
||||
cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;
|
||||
}
|
||||
else
|
||||
{
|
||||
total_count = count;
|
||||
|
||||
/* no CPU usage */
|
||||
cpu_usage_major = 0;
|
||||
cpu_usage_minor = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor)
|
||||
{
|
||||
RT_ASSERT(major != RT_NULL);
|
||||
RT_ASSERT(minor != RT_NULL);
|
||||
|
||||
*major = cpu_usage_major;
|
||||
*minor = cpu_usage_minor;
|
||||
}
|
||||
|
||||
void cpu_usage_init()
|
||||
{
|
||||
/* set idle thread hook */
|
||||
rt_thread_idle_sethook(cpu_usage_idle_hook);
|
||||
}
|
||||
@@ -0,0 +1,439 @@
|
||||
/*
|
||||
* File : device.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 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
|
||||
* 2007-01-21 Bernard the first version
|
||||
* 2010-05-04 Bernard add rt_device_init implementation
|
||||
* 2012-10-20 Bernard add device check in register function,
|
||||
* provided by Rob <rdent@iinet.net.au>
|
||||
* 2012-12-25 Bernard return RT_EOK if the device interface not exist.
|
||||
* 2013-07-09 Grissiom add ref_count support
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef RT_USING_DEVICE
|
||||
|
||||
/**
|
||||
* This function registers a device driver with specified name.
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
* @param name the device driver's name
|
||||
* @param flags the flag of device
|
||||
*
|
||||
* @return the error code, RT_EOK on initialization successfully.
|
||||
*/
|
||||
rt_err_t rt_device_register(rt_device_t dev,
|
||||
const char *name,
|
||||
rt_uint16_t flags)
|
||||
{
|
||||
if (dev == RT_NULL)
|
||||
return -RT_ERROR;
|
||||
|
||||
if (rt_device_find(name) != RT_NULL)
|
||||
return -RT_ERROR;
|
||||
|
||||
rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
|
||||
dev->flag = flags;
|
||||
dev->ref_count = 0;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_register);
|
||||
|
||||
/**
|
||||
* This function removes a previously registered device driver
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_device_unregister(rt_device_t dev)
|
||||
{
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
rt_object_detach(&(dev->parent));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_unregister);
|
||||
|
||||
/**
|
||||
* This function initializes all registered device driver
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_device_init_all(void)
|
||||
{
|
||||
struct rt_device *device;
|
||||
struct rt_list_node *node;
|
||||
struct rt_object_information *information;
|
||||
register rt_err_t result;
|
||||
|
||||
extern struct rt_object_information rt_object_container[];
|
||||
|
||||
information = &rt_object_container[RT_Object_Class_Device];
|
||||
|
||||
/* for each device */
|
||||
for (node = information->object_list.next;
|
||||
node != &(information->object_list);
|
||||
node = node->next)
|
||||
{
|
||||
rt_err_t (*init)(rt_device_t dev);
|
||||
device = (struct rt_device *)rt_list_entry(node,
|
||||
struct rt_object,
|
||||
list);
|
||||
|
||||
/* get device init handler */
|
||||
init = device->init;
|
||||
if (init != RT_NULL && !(device->flag & RT_DEVICE_FLAG_ACTIVATED))
|
||||
{
|
||||
result = init(device);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf("To initialize device:%s failed. The error code is %d\n",
|
||||
device->parent.name, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
device->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function finds a device driver by specified name.
|
||||
*
|
||||
* @param name the device driver's name
|
||||
*
|
||||
* @return the registered device driver on successful, or RT_NULL on failure.
|
||||
*/
|
||||
rt_device_t rt_device_find(const char *name)
|
||||
{
|
||||
struct rt_object *object;
|
||||
struct rt_list_node *node;
|
||||
struct rt_object_information *information;
|
||||
|
||||
extern struct rt_object_information rt_object_container[];
|
||||
|
||||
/* enter critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_enter_critical();
|
||||
|
||||
/* try to find device object */
|
||||
information = &rt_object_container[RT_Object_Class_Device];
|
||||
for (node = information->object_list.next;
|
||||
node != &(information->object_list);
|
||||
node = node->next)
|
||||
{
|
||||
object = rt_list_entry(node, struct rt_object, list);
|
||||
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
|
||||
{
|
||||
/* leave critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_exit_critical();
|
||||
|
||||
return (rt_device_t)object;
|
||||
}
|
||||
}
|
||||
|
||||
/* leave critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_exit_critical();
|
||||
|
||||
/* not found */
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(rt_device_find);
|
||||
|
||||
/**
|
||||
* This function will initialize the specified device
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
*
|
||||
* @return the result
|
||||
*/
|
||||
rt_err_t rt_device_init(rt_device_t dev)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
/* get device init handler */
|
||||
if (dev->init != RT_NULL)
|
||||
{
|
||||
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
|
||||
{
|
||||
result = dev->init(dev);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf("To initialize device:%s failed. The error code is %d\n",
|
||||
dev->parent.name, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will open a device
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
* @param oflag the flags for device open
|
||||
*
|
||||
* @return the result
|
||||
*/
|
||||
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
/* if device is not initialized, initialize it. */
|
||||
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
|
||||
{
|
||||
if (dev->init != RT_NULL)
|
||||
{
|
||||
result = dev->init(dev);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf("To initialize device:%s failed. The error code is %d\n",
|
||||
dev->parent.name, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
||||
}
|
||||
|
||||
/* device is a stand alone device and opened */
|
||||
if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
|
||||
(dev->open_flag & RT_DEVICE_OFLAG_OPEN))
|
||||
{
|
||||
return -RT_EBUSY;
|
||||
}
|
||||
|
||||
/* call device open interface */
|
||||
if (dev->open != RT_NULL)
|
||||
{
|
||||
result = dev->open(dev, oflag);
|
||||
}
|
||||
|
||||
/* set open flag */
|
||||
if (result == RT_EOK || result == -RT_ENOSYS)
|
||||
{
|
||||
dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
|
||||
|
||||
dev->ref_count++;
|
||||
/* don't let bad things happen silently. If you are bitten by this assert,
|
||||
* please set the ref_count to a bigger type. */
|
||||
RT_ASSERT(dev->ref_count != 0);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(rt_device_open);
|
||||
|
||||
/**
|
||||
* This function will close a device
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
*
|
||||
* @return the result
|
||||
*/
|
||||
rt_err_t rt_device_close(rt_device_t dev)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
if (dev->ref_count == 0)
|
||||
return -RT_ERROR;
|
||||
|
||||
dev->ref_count--;
|
||||
|
||||
if (dev->ref_count != 0)
|
||||
return RT_EOK;
|
||||
|
||||
/* call device close interface */
|
||||
if (dev->close != RT_NULL)
|
||||
{
|
||||
result = dev->close(dev);
|
||||
}
|
||||
|
||||
/* set open flag */
|
||||
if (result == RT_EOK || result == -RT_ENOSYS)
|
||||
dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(rt_device_close);
|
||||
|
||||
/**
|
||||
* This function will read some data from a device.
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
* @param pos the position of reading
|
||||
* @param buffer the data buffer to save read data
|
||||
* @param size the size of buffer
|
||||
*
|
||||
* @return the actually read size on successful, otherwise negative returned.
|
||||
*
|
||||
* @note since 0.4.0, the unit of size/pos is a block for block device.
|
||||
*/
|
||||
rt_size_t rt_device_read(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
if (dev->ref_count == 0)
|
||||
{
|
||||
rt_set_errno(-RT_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* call device read interface */
|
||||
if (dev->read != RT_NULL)
|
||||
{
|
||||
return dev->read(dev, pos, buffer, size);
|
||||
}
|
||||
|
||||
/* set error code */
|
||||
rt_set_errno(-RT_ENOSYS);
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(rt_device_read);
|
||||
|
||||
/**
|
||||
* This function will write some data to a device.
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
* @param pos the position of written
|
||||
* @param buffer the data buffer to be written to device
|
||||
* @param size the size of buffer
|
||||
*
|
||||
* @return the actually written size on successful, otherwise negative returned.
|
||||
*
|
||||
* @note since 0.4.0, the unit of size/pos is a block for block device.
|
||||
*/
|
||||
rt_size_t rt_device_write(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
if (dev->ref_count == 0)
|
||||
{
|
||||
rt_set_errno(-RT_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* call device write interface */
|
||||
if (dev->write != RT_NULL)
|
||||
{
|
||||
return dev->write(dev, pos, buffer, size);
|
||||
}
|
||||
|
||||
/* set error code */
|
||||
rt_set_errno(-RT_ENOSYS);
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(rt_device_write);
|
||||
|
||||
/**
|
||||
* This function will perform a variety of control functions on devices.
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
* @param cmd the command sent to device
|
||||
* @param arg the argument of command
|
||||
*
|
||||
* @return the result
|
||||
*/
|
||||
rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
|
||||
{
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
/* call device write interface */
|
||||
if (dev->control != RT_NULL)
|
||||
{
|
||||
return dev->control(dev, cmd, arg);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_control);
|
||||
|
||||
/**
|
||||
* This function will set the indication callback function when device receives
|
||||
* data.
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
* @param rx_ind the indication callback function
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t
|
||||
rt_device_set_rx_indicate(rt_device_t dev,
|
||||
rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
|
||||
{
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
dev->rx_indicate = rx_ind;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_set_rx_indicate);
|
||||
|
||||
/**
|
||||
* This function will set the indication callback function when device has
|
||||
* written data to physical hardware.
|
||||
*
|
||||
* @param dev the pointer of device driver structure
|
||||
* @param tx_done the indication callback function
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t
|
||||
rt_device_set_tx_complete(rt_device_t dev,
|
||||
rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
|
||||
{
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
dev->tx_complete = tx_done;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_set_tx_complete);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
* File : idle.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-23 Bernard the first version
|
||||
* 2010-11-10 Bernard add cleanup callback function in thread exit.
|
||||
* 2012-12-29 Bernard fix compiling warning.
|
||||
* 2013-12-21 Grissiom let rt_thread_idle_excute loop until there is no
|
||||
* dead thread.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifndef IDLE_THREAD_STACK_SIZE
|
||||
#if defined (RT_USING_HOOK) || defined(RT_USING_HEAP)
|
||||
#define IDLE_THREAD_STACK_SIZE 256
|
||||
#else
|
||||
#define IDLE_THREAD_STACK_SIZE 128
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static struct rt_thread idle;
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
|
||||
|
||||
extern rt_list_t rt_thread_defunct;
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
/**
|
||||
* @addtogroup Hook
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
static void (*rt_thread_idle_hook)();
|
||||
|
||||
/**
|
||||
* This function will set a hook function to idle thread loop.
|
||||
*
|
||||
* @param hook the specified hook function
|
||||
*
|
||||
* @note the hook function must be simple and never be blocked or suspend.
|
||||
*/
|
||||
void rt_thread_idle_sethook(void (*hook)(void))
|
||||
{
|
||||
rt_thread_idle_hook = hook;
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @ingroup Thread
|
||||
*
|
||||
* This function will perform system background job when system idle.
|
||||
*/
|
||||
void rt_thread_idle_excute(void)
|
||||
{
|
||||
/* Loop until there is no dead thread. So one call to rt_thread_idle_excute
|
||||
* will do all the cleanups. */
|
||||
while (!rt_list_isempty(&rt_thread_defunct))
|
||||
{
|
||||
rt_base_t lock;
|
||||
rt_thread_t thread;
|
||||
#ifdef RT_USING_MODULE
|
||||
rt_module_t module = RT_NULL;
|
||||
#endif
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* disable interrupt */
|
||||
lock = rt_hw_interrupt_disable();
|
||||
|
||||
/* re-check whether list is empty */
|
||||
if (!rt_list_isempty(&rt_thread_defunct))
|
||||
{
|
||||
/* get defunct thread */
|
||||
thread = rt_list_entry(rt_thread_defunct.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
#ifdef RT_USING_MODULE
|
||||
/* get thread's parent module */
|
||||
module = (rt_module_t)thread->module_id;
|
||||
|
||||
/* if the thread is module's main thread */
|
||||
if (module != RT_NULL && module->module_thread == thread)
|
||||
{
|
||||
/* detach module's main thread */
|
||||
module->module_thread = RT_NULL;
|
||||
}
|
||||
#endif
|
||||
/* remove defunct thread */
|
||||
rt_list_remove(&(thread->tlist));
|
||||
/* invoke thread cleanup */
|
||||
if (thread->cleanup != RT_NULL)
|
||||
thread->cleanup(thread);
|
||||
|
||||
/* if it's a system object, not delete it */
|
||||
if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
|
||||
{
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(lock);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(lock);
|
||||
|
||||
/* may the defunct thread list is removed by others, just return */
|
||||
return;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(lock);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
|
||||
/* the thread belongs to an application module */
|
||||
if (thread->flags & RT_OBJECT_FLAG_MODULE)
|
||||
rt_module_free((rt_module_t)thread->module_id, thread->stack_addr);
|
||||
else
|
||||
#endif
|
||||
/* release thread's stack */
|
||||
RT_KERNEL_FREE(thread->stack_addr);
|
||||
/* delete thread object */
|
||||
rt_object_delete((rt_object_t)thread);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
if (module != RT_NULL)
|
||||
{
|
||||
extern rt_err_t rt_module_destroy(rt_module_t module);
|
||||
|
||||
/* if sub thread list and main thread are all empty */
|
||||
if ((module->module_thread == RT_NULL) &&
|
||||
rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list))
|
||||
{
|
||||
module->nref --;
|
||||
}
|
||||
|
||||
/* destroy module */
|
||||
if (module->nref == 0)
|
||||
rt_module_destroy(module);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static void rt_thread_idle_entry(void *parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
#ifdef RT_USING_HOOK
|
||||
if (rt_thread_idle_hook != RT_NULL)
|
||||
rt_thread_idle_hook();
|
||||
#endif
|
||||
|
||||
rt_thread_idle_excute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup SymstemInit
|
||||
*
|
||||
* This function will initialize idle thread, then start it.
|
||||
*
|
||||
* @note this function must be invoked when system init.
|
||||
*/
|
||||
void rt_thread_idle_init(void)
|
||||
{
|
||||
/* initialize thread */
|
||||
rt_thread_init(&idle,
|
||||
"tidle",
|
||||
rt_thread_idle_entry,
|
||||
RT_NULL,
|
||||
&rt_thread_stack[0],
|
||||
sizeof(rt_thread_stack),
|
||||
RT_THREAD_PRIORITY_MAX - 1,
|
||||
32);
|
||||
|
||||
/* startup */
|
||||
rt_thread_startup(&idle);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* File : irq.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-02-24 Bernard first version
|
||||
* 2006-05-03 Bernard add IRQ_DEBUG
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
/* #define IRQ_DEBUG */
|
||||
|
||||
/**
|
||||
* @addtogroup Kernel
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
volatile rt_uint8_t rt_interrupt_nest;
|
||||
|
||||
/**
|
||||
* This function will be invoked by BSP, when enter interrupt service routine
|
||||
*
|
||||
* @note please don't invoke this routine in application
|
||||
*
|
||||
* @see rt_interrupt_leave
|
||||
*/
|
||||
void rt_interrupt_enter(void)
|
||||
{
|
||||
rt_base_t level;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq coming..., irq nest:%d\n",
|
||||
rt_interrupt_nest));
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_interrupt_nest ++;
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
RTM_EXPORT(rt_interrupt_enter);
|
||||
|
||||
/**
|
||||
* This function will be invoked by BSP, when leave interrupt service routine
|
||||
*
|
||||
* @note please don't invoke this routine in application
|
||||
*
|
||||
* @see rt_interrupt_enter
|
||||
*/
|
||||
void rt_interrupt_leave(void)
|
||||
{
|
||||
rt_base_t level;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq leave, irq nest:%d\n",
|
||||
rt_interrupt_nest));
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_interrupt_nest --;
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
RTM_EXPORT(rt_interrupt_leave);
|
||||
|
||||
/**
|
||||
* This function will return the nest of interrupt.
|
||||
*
|
||||
* User application can invoke this function to get whether current
|
||||
* context is interrupt context.
|
||||
*
|
||||
* @return the number of nested interrupts.
|
||||
*/
|
||||
rt_uint8_t rt_interrupt_get_nest(void)
|
||||
{
|
||||
return rt_interrupt_nest;
|
||||
}
|
||||
RTM_EXPORT(rt_interrupt_get_nest);
|
||||
|
||||
RTM_EXPORT(rt_hw_interrupt_disable);
|
||||
RTM_EXPORT(rt_hw_interrupt_enable);
|
||||
|
||||
/*@}*/
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,591 @@
|
||||
/*
|
||||
* File : mem.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2008 - 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
|
||||
* 2008-7-12 Bernard the first version
|
||||
* 2010-06-09 Bernard fix the end stub of heap
|
||||
* fix memory check in rt_realloc function
|
||||
* 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
|
||||
* 2010-10-14 Bernard fix rt_realloc issue when realloc a NULL pointer.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
* Simon Goldschmidt
|
||||
*
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifndef RT_USING_MEMHEAP_AS_HEAP
|
||||
|
||||
/* #define RT_MEM_DEBUG */
|
||||
#define RT_MEM_STATS
|
||||
|
||||
#if defined (RT_USING_HEAP) && defined (RT_USING_SMALL_MEM)
|
||||
#ifdef RT_USING_HOOK
|
||||
static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
|
||||
static void (*rt_free_hook)(void *ptr);
|
||||
|
||||
/**
|
||||
* @addtogroup Hook
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when a memory
|
||||
* block is allocated from heap memory.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
|
||||
{
|
||||
rt_malloc_hook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when a memory
|
||||
* block is released to heap memory.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_free_sethook(void (*hook)(void *ptr))
|
||||
{
|
||||
rt_free_hook = hook;
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
|
||||
#endif
|
||||
|
||||
#define HEAP_MAGIC 0x1ea0
|
||||
struct heap_mem
|
||||
{
|
||||
/* magic and used flag */
|
||||
rt_uint16_t magic;
|
||||
rt_uint16_t used;
|
||||
|
||||
rt_size_t next, prev;
|
||||
};
|
||||
|
||||
/** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */
|
||||
static rt_uint8_t *heap_ptr;
|
||||
|
||||
/** the last entry, always unused! */
|
||||
static struct heap_mem *heap_end;
|
||||
|
||||
#define MIN_SIZE 12
|
||||
#define MIN_SIZE_ALIGNED RT_ALIGN(MIN_SIZE, RT_ALIGN_SIZE)
|
||||
#define SIZEOF_STRUCT_MEM RT_ALIGN(sizeof(struct heap_mem), RT_ALIGN_SIZE)
|
||||
|
||||
static struct heap_mem *lfree; /* pointer to the lowest free block */
|
||||
|
||||
static struct rt_semaphore heap_sem;
|
||||
static rt_size_t mem_size_aligned;
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
static rt_size_t used_mem, max_mem;
|
||||
#endif
|
||||
|
||||
static void plug_holes(struct heap_mem *mem)
|
||||
{
|
||||
struct heap_mem *nmem;
|
||||
struct heap_mem *pmem;
|
||||
|
||||
RT_ASSERT((rt_uint8_t *)mem >= heap_ptr);
|
||||
RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end);
|
||||
RT_ASSERT(mem->used == 0);
|
||||
|
||||
/* plug hole forward */
|
||||
nmem = (struct heap_mem *)&heap_ptr[mem->next];
|
||||
if (mem != nmem &&
|
||||
nmem->used == 0 &&
|
||||
(rt_uint8_t *)nmem != (rt_uint8_t *)heap_end)
|
||||
{
|
||||
/* if mem->next is unused and not end of heap_ptr,
|
||||
* combine mem and mem->next
|
||||
*/
|
||||
if (lfree == nmem)
|
||||
{
|
||||
lfree = mem;
|
||||
}
|
||||
mem->next = nmem->next;
|
||||
((struct heap_mem *)&heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - heap_ptr;
|
||||
}
|
||||
|
||||
/* plug hole backward */
|
||||
pmem = (struct heap_mem *)&heap_ptr[mem->prev];
|
||||
if (pmem != mem && pmem->used == 0)
|
||||
{
|
||||
/* if mem->prev is unused, combine mem and mem->prev */
|
||||
if (lfree == mem)
|
||||
{
|
||||
lfree = pmem;
|
||||
}
|
||||
pmem->next = mem->next;
|
||||
((struct heap_mem *)&heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - heap_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup SystemInit
|
||||
*
|
||||
* This function will init system heap
|
||||
*
|
||||
* @param begin_addr the beginning address of system page
|
||||
* @param end_addr the end address of system page
|
||||
*/
|
||||
void rt_system_heap_init(void *begin_addr, void *end_addr)
|
||||
{
|
||||
struct heap_mem *mem;
|
||||
rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);
|
||||
rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE);
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* alignment addr */
|
||||
if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
|
||||
((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))
|
||||
{
|
||||
/* calculate the aligned memory size */
|
||||
mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",
|
||||
(rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* point to begin address of heap */
|
||||
heap_ptr = (rt_uint8_t *)begin_align;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
|
||||
(rt_uint32_t)heap_ptr, mem_size_aligned));
|
||||
|
||||
/* initialize the start of the heap */
|
||||
mem = (struct heap_mem *)heap_ptr;
|
||||
mem->magic = HEAP_MAGIC;
|
||||
mem->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
mem->prev = 0;
|
||||
mem->used = 0;
|
||||
|
||||
/* initialize the end of the heap */
|
||||
heap_end = (struct heap_mem *)&heap_ptr[mem->next];
|
||||
heap_end->magic = HEAP_MAGIC;
|
||||
heap_end->used = 1;
|
||||
heap_end->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
heap_end->prev = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
|
||||
rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);
|
||||
|
||||
/* initialize the lowest-free pointer to the start of the heap */
|
||||
lfree = (struct heap_mem *)heap_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup MM
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* Allocate a block of memory with a minimum of 'size' bytes.
|
||||
*
|
||||
* @param size is the minimum size of the requested block in bytes.
|
||||
*
|
||||
* @return pointer to allocated memory or NULL if no free memory was found.
|
||||
*/
|
||||
void *rt_malloc(rt_size_t size)
|
||||
{
|
||||
rt_size_t ptr, ptr2;
|
||||
struct heap_mem *mem, *mem2;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
if (size == 0)
|
||||
return RT_NULL;
|
||||
|
||||
if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",
|
||||
size, RT_ALIGN(size, RT_ALIGN_SIZE)));
|
||||
else
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d\n", size));
|
||||
|
||||
/* alignment size */
|
||||
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
||||
|
||||
if (size > mem_size_aligned)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/* every data block must be at least MIN_SIZE_ALIGNED long */
|
||||
if (size < MIN_SIZE_ALIGNED)
|
||||
size = MIN_SIZE_ALIGNED;
|
||||
|
||||
/* take memory semaphore */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
for (ptr = (rt_uint8_t *)lfree - heap_ptr;
|
||||
ptr < mem_size_aligned - size;
|
||||
ptr = ((struct heap_mem *)&heap_ptr[ptr])->next)
|
||||
{
|
||||
mem = (struct heap_mem *)&heap_ptr[ptr];
|
||||
|
||||
if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
|
||||
{
|
||||
/* mem is not used and at least perfect fit is possible:
|
||||
* mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
|
||||
|
||||
if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=
|
||||
(size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
|
||||
{
|
||||
/* (in addition to the above, we test if another struct heap_mem (SIZEOF_STRUCT_MEM) containing
|
||||
* at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
|
||||
* -> split large block, create empty remainder,
|
||||
* remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
|
||||
* mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
|
||||
* struct heap_mem would fit in but no data between mem2 and mem2->next
|
||||
* @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
|
||||
* region that couldn't hold data, but when mem->next gets freed,
|
||||
* the 2 regions would be combined, resulting in more free memory
|
||||
*/
|
||||
ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
|
||||
|
||||
/* create mem2 struct */
|
||||
mem2 = (struct heap_mem *)&heap_ptr[ptr2];
|
||||
mem2->used = 0;
|
||||
mem2->next = mem->next;
|
||||
mem2->prev = ptr;
|
||||
|
||||
/* and insert it between mem and mem->next */
|
||||
mem->next = ptr2;
|
||||
mem->used = 1;
|
||||
|
||||
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
|
||||
{
|
||||
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
|
||||
}
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem += (size + SIZEOF_STRUCT_MEM);
|
||||
if (max_mem < used_mem)
|
||||
max_mem = used_mem;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
/* (a mem2 struct does no fit into the user data space of mem and mem->next will always
|
||||
* be used at this point: if not we have 2 unused structs in a row, plug_holes should have
|
||||
* take care of this).
|
||||
* -> near fit or excact fit: do not split, no mem2 creation
|
||||
* also can't move mem->next directly behind mem, since mem->next
|
||||
* will always be used at this point!
|
||||
*/
|
||||
mem->used = 1;
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);
|
||||
if (max_mem < used_mem)
|
||||
max_mem = used_mem;
|
||||
#endif
|
||||
}
|
||||
/* set memory block magic */
|
||||
mem->magic = HEAP_MAGIC;
|
||||
|
||||
if (mem == lfree)
|
||||
{
|
||||
/* Find next free block after mem and update lowest free pointer */
|
||||
while (lfree->used && lfree != heap_end)
|
||||
lfree = (struct heap_mem *)&heap_ptr[lfree->next];
|
||||
|
||||
RT_ASSERT(((lfree == heap_end) || (!lfree->used)));
|
||||
}
|
||||
|
||||
rt_sem_release(&heap_sem);
|
||||
RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);
|
||||
RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
|
||||
RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM,
|
||||
("allocate memory at 0x%x, size: %d\n",
|
||||
(rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
|
||||
(rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_malloc_hook,
|
||||
(((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));
|
||||
|
||||
/* return the memory data except mem struct */
|
||||
return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(rt_malloc);
|
||||
|
||||
/**
|
||||
* This function will change the previously allocated memory block.
|
||||
*
|
||||
* @param rmem pointer to memory allocated by rt_malloc
|
||||
* @param newsize the required new size
|
||||
*
|
||||
* @return the changed memory block address
|
||||
*/
|
||||
void *rt_realloc(void *rmem, rt_size_t newsize)
|
||||
{
|
||||
rt_size_t size;
|
||||
rt_size_t ptr, ptr2;
|
||||
struct heap_mem *mem, *mem2;
|
||||
void *nmem;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* alignment size */
|
||||
newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
|
||||
if (newsize > mem_size_aligned)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/* allocate a new memory block */
|
||||
if (rmem == RT_NULL)
|
||||
return rt_malloc(newsize);
|
||||
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
|
||||
(rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
|
||||
{
|
||||
/* illegal memory */
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return rmem;
|
||||
}
|
||||
|
||||
mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
|
||||
|
||||
ptr = (rt_uint8_t *)mem - heap_ptr;
|
||||
size = mem->next - ptr - SIZEOF_STRUCT_MEM;
|
||||
if (size == newsize)
|
||||
{
|
||||
/* the size is the same as */
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return rmem;
|
||||
}
|
||||
|
||||
if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
|
||||
{
|
||||
/* split memory block */
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem -= (size - newsize);
|
||||
#endif
|
||||
|
||||
ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
|
||||
mem2 = (struct heap_mem *)&heap_ptr[ptr2];
|
||||
mem2->magic= HEAP_MAGIC;
|
||||
mem2->used = 0;
|
||||
mem2->next = mem->next;
|
||||
mem2->prev = ptr;
|
||||
mem->next = ptr2;
|
||||
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
|
||||
{
|
||||
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
|
||||
}
|
||||
|
||||
plug_holes(mem2);
|
||||
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return rmem;
|
||||
}
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
/* expand memory */
|
||||
nmem = rt_malloc(newsize);
|
||||
if (nmem != RT_NULL) /* check memory */
|
||||
{
|
||||
rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
|
||||
rt_free(rmem);
|
||||
}
|
||||
|
||||
return nmem;
|
||||
}
|
||||
RTM_EXPORT(rt_realloc);
|
||||
|
||||
/**
|
||||
* This function will contiguously allocate enough space for count objects
|
||||
* that are size bytes of memory each and returns a pointer to the allocated
|
||||
* memory.
|
||||
*
|
||||
* The allocated memory is filled with bytes of value zero.
|
||||
*
|
||||
* @param count number of objects to allocate
|
||||
* @param size size of the objects to allocate
|
||||
*
|
||||
* @return pointer to allocated memory / NULL pointer if there is an error
|
||||
*/
|
||||
void *rt_calloc(rt_size_t count, rt_size_t size)
|
||||
{
|
||||
void *p;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* allocate 'count' objects of size 'size' */
|
||||
p = rt_malloc(count * size);
|
||||
|
||||
/* zero the memory */
|
||||
if (p)
|
||||
rt_memset(p, 0, count * size);
|
||||
|
||||
return p;
|
||||
}
|
||||
RTM_EXPORT(rt_calloc);
|
||||
|
||||
/**
|
||||
* This function will release the previously allocated memory block by
|
||||
* rt_malloc. The released memory block is taken back to system heap.
|
||||
*
|
||||
* @param rmem the address of memory which will be released
|
||||
*/
|
||||
void rt_free(void *rmem)
|
||||
{
|
||||
struct heap_mem *mem;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
if (rmem == RT_NULL)
|
||||
return;
|
||||
RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE-1)) == 0);
|
||||
RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr &&
|
||||
(rt_uint8_t *)rmem < (rt_uint8_t *)heap_end);
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_free_hook, (rmem));
|
||||
|
||||
if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
|
||||
(rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n"));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get the corresponding struct heap_mem ... */
|
||||
mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM,
|
||||
("release memory 0x%x, size: %d\n",
|
||||
(rt_uint32_t)rmem,
|
||||
(rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
|
||||
|
||||
|
||||
/* protect the heap from concurrent access */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
/* ... which has to be in a used state ... */
|
||||
RT_ASSERT(mem->used);
|
||||
RT_ASSERT(mem->magic == HEAP_MAGIC);
|
||||
/* ... and is now unused. */
|
||||
mem->used = 0;
|
||||
mem->magic = 0;
|
||||
|
||||
if (mem < lfree)
|
||||
{
|
||||
/* the newly freed struct is now the lowest */
|
||||
lfree = mem;
|
||||
}
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem -= (mem->next - ((rt_uint8_t*)mem - heap_ptr));
|
||||
#endif
|
||||
|
||||
/* finally, see if prev or next are free also */
|
||||
plug_holes(mem);
|
||||
rt_sem_release(&heap_sem);
|
||||
}
|
||||
RTM_EXPORT(rt_free);
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
void rt_memory_info(rt_uint32_t *total,
|
||||
rt_uint32_t *used,
|
||||
rt_uint32_t *max_used)
|
||||
{
|
||||
if (total != RT_NULL)
|
||||
*total = mem_size_aligned;
|
||||
if (used != RT_NULL)
|
||||
*used = used_mem;
|
||||
if (max_used != RT_NULL)
|
||||
*max_used = max_mem;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
|
||||
void list_mem(void)
|
||||
{
|
||||
rt_kprintf("total memory: %d\n", mem_size_aligned);
|
||||
rt_kprintf("used memory : %d\n", used_mem);
|
||||
rt_kprintf("maximum allocated memory: %d\n", max_mem);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
#endif /* end of RT_USING_HEAP */
|
||||
#endif /* end of RT_USING_MEMHEAP_AS_HEAP */
|
||||
|
||||
@@ -0,0 +1,704 @@
|
||||
/*
|
||||
* File : memheap.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-04-10 Bernard first implementation
|
||||
* 2012-10-16 Bernard add the mutex lock for heap object.
|
||||
* 2012-12-29 Bernard memheap can be used as system heap.
|
||||
* change mutex lock to semaphore lock.
|
||||
* 2013-04-10 Bernard add rt_memheap_realloc function.
|
||||
* 2013-05-24 Bernard fix the rt_memheap_realloc issue.
|
||||
* 2013-07-11 Grissiom fix the memory block splitting issue.
|
||||
* 2013-07-15 Grissiom optimize rt_memheap_realloc
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef RT_USING_MEMHEAP
|
||||
|
||||
/* dynamic pool magic and mask */
|
||||
#define RT_MEMHEAP_MAGIC 0x1ea01ea0
|
||||
#define RT_MEMHEAP_MASK 0xfffffffe
|
||||
#define RT_MEMHEAP_USED 0x01
|
||||
#define RT_MEMHEAP_FREED 0x00
|
||||
|
||||
#define RT_MEMHEAP_IS_USED(i) ((i)->magic & RT_MEMHEAP_USED)
|
||||
#define RT_MEMHEAP_MINIALLOC 12
|
||||
|
||||
#define RT_MEMHEAP_SIZE RT_ALIGN(sizeof(struct rt_memheap_item), RT_ALIGN_SIZE)
|
||||
#define MEMITEM_SIZE(item) ((rt_uint32_t)item->next - (rt_uint32_t)item - RT_MEMHEAP_SIZE)
|
||||
|
||||
/*
|
||||
* The initialized memory pool will be:
|
||||
* +-----------------------------------+--------------------------+
|
||||
* | whole freed memory block | Used Memory Block Tailer |
|
||||
* +-----------------------------------+--------------------------+
|
||||
*
|
||||
* block_list --> whole freed memory block
|
||||
*
|
||||
* The length of Used Memory Block Tailer is 0,
|
||||
* which is prevents block merging across list
|
||||
*/
|
||||
rt_err_t rt_memheap_init(struct rt_memheap *memheap,
|
||||
const char *name,
|
||||
void *start_addr,
|
||||
rt_uint32_t size)
|
||||
{
|
||||
struct rt_memheap_item *item;
|
||||
|
||||
RT_ASSERT(memheap != RT_NULL);
|
||||
|
||||
/* initialize pool object */
|
||||
rt_object_init(&(memheap->parent), RT_Object_Class_MemHeap, name);
|
||||
|
||||
memheap->start_addr = start_addr;
|
||||
memheap->pool_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
||||
memheap->available_size = memheap->pool_size - (2 * RT_MEMHEAP_SIZE);
|
||||
memheap->max_used_size = memheap->pool_size - memheap->available_size;
|
||||
|
||||
/* initialize the free list header */
|
||||
item = &(memheap->free_header);
|
||||
item->magic = RT_MEMHEAP_MAGIC;
|
||||
item->pool_ptr = memheap;
|
||||
item->next = RT_NULL;
|
||||
item->prev = RT_NULL;
|
||||
item->next_free = item;
|
||||
item->prev_free = item;
|
||||
|
||||
/* set the free list to free list header */
|
||||
memheap->free_list = item;
|
||||
|
||||
/* initialize the first big memory block */
|
||||
item = (struct rt_memheap_item *)start_addr;
|
||||
item->magic = RT_MEMHEAP_MAGIC;
|
||||
item->pool_ptr = memheap;
|
||||
item->next = RT_NULL;
|
||||
item->prev = RT_NULL;
|
||||
item->next_free = item;
|
||||
item->prev_free = item;
|
||||
|
||||
item->next = (struct rt_memheap_item *)
|
||||
((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE);
|
||||
item->prev = item->next;
|
||||
|
||||
/* block list header */
|
||||
memheap->block_list = item;
|
||||
|
||||
/* place the big memory block to free list */
|
||||
item->next_free = memheap->free_list->next_free;
|
||||
item->prev_free = memheap->free_list;
|
||||
memheap->free_list->next_free->prev_free = item;
|
||||
memheap->free_list->next_free = item;
|
||||
|
||||
/* move to the end of memory pool to build a small tailer block,
|
||||
* which prevents block merging
|
||||
*/
|
||||
item = item->next;
|
||||
/* it's a used memory block */
|
||||
item->magic = RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED;
|
||||
item->pool_ptr = memheap;
|
||||
item->next = (struct rt_memheap_item *)start_addr;
|
||||
item->prev = (struct rt_memheap_item *)start_addr;
|
||||
/* not in free list */
|
||||
item->next_free = item->prev_free = RT_NULL;
|
||||
|
||||
/* initialize semaphore lock */
|
||||
rt_sem_init(&(memheap->lock), name, 1, RT_IPC_FLAG_FIFO);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("memory heap: start addr 0x%08x, size %d, free list header 0x%08x\n",
|
||||
start_addr, size, &(memheap->free_header)));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_memheap_init);
|
||||
|
||||
rt_err_t rt_memheap_detach(struct rt_memheap *heap)
|
||||
{
|
||||
RT_ASSERT(heap);
|
||||
|
||||
rt_object_detach(&(heap->lock.parent.parent));
|
||||
rt_object_detach(&(heap->parent));
|
||||
|
||||
/* Return a successful completion. */
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_memheap_detach);
|
||||
|
||||
void *rt_memheap_alloc(struct rt_memheap *heap, rt_uint32_t size)
|
||||
{
|
||||
rt_err_t result;
|
||||
rt_uint32_t free_size;
|
||||
struct rt_memheap_item *header_ptr;
|
||||
|
||||
RT_ASSERT(heap != RT_NULL);
|
||||
|
||||
/* align allocated size */
|
||||
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
||||
if (size < RT_MEMHEAP_MINIALLOC)
|
||||
size = RT_MEMHEAP_MINIALLOC;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d on heap:%8.*s",
|
||||
size, RT_NAME_MAX, heap->parent.name));
|
||||
|
||||
if (size < heap->available_size)
|
||||
{
|
||||
/* search on free list */
|
||||
free_size = 0;
|
||||
|
||||
/* lock memheap */
|
||||
result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_set_errno(result);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/* get the first free memory block */
|
||||
header_ptr = heap->free_list->next_free;
|
||||
while (header_ptr != heap->free_list && free_size < size)
|
||||
{
|
||||
/* get current freed memory block size */
|
||||
free_size = MEMITEM_SIZE(header_ptr);
|
||||
if (free_size < size)
|
||||
{
|
||||
/* move to next free memory block */
|
||||
header_ptr = header_ptr->next_free;
|
||||
}
|
||||
}
|
||||
|
||||
/* determine if the memory is available. */
|
||||
if (free_size >= size)
|
||||
{
|
||||
/* a block that satisfies the request has been found. */
|
||||
|
||||
/* determine if the block needs to be split. */
|
||||
if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC))
|
||||
{
|
||||
struct rt_memheap_item *new_ptr;
|
||||
|
||||
/* split the block. */
|
||||
new_ptr = (struct rt_memheap_item *)
|
||||
(((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
|
||||
header_ptr,
|
||||
header_ptr->next,
|
||||
header_ptr->prev,
|
||||
new_ptr));
|
||||
|
||||
/* mark the new block as a memory block and freed. */
|
||||
new_ptr->magic = RT_MEMHEAP_MAGIC;
|
||||
|
||||
/* put the pool pointer into the new block. */
|
||||
new_ptr->pool_ptr = heap;
|
||||
|
||||
/* break down the block list */
|
||||
new_ptr->prev = header_ptr;
|
||||
new_ptr->next = header_ptr->next;
|
||||
header_ptr->next->prev = new_ptr;
|
||||
header_ptr->next = new_ptr;
|
||||
|
||||
/* remove header ptr from free list */
|
||||
header_ptr->next_free->prev_free = header_ptr->prev_free;
|
||||
header_ptr->prev_free->next_free = header_ptr->next_free;
|
||||
header_ptr->next_free = RT_NULL;
|
||||
header_ptr->prev_free = RT_NULL;
|
||||
|
||||
/* insert new_ptr to free list */
|
||||
new_ptr->next_free = heap->free_list->next_free;
|
||||
new_ptr->prev_free = heap->free_list;
|
||||
heap->free_list->next_free->prev_free = new_ptr;
|
||||
heap->free_list->next_free = new_ptr;
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
|
||||
new_ptr->next_free,
|
||||
new_ptr->prev_free));
|
||||
|
||||
/* decrement the available byte count. */
|
||||
heap->available_size = heap->available_size -
|
||||
size -
|
||||
RT_MEMHEAP_SIZE;
|
||||
if (heap->pool_size - heap->available_size > heap->max_used_size)
|
||||
heap->max_used_size = heap->pool_size - heap->available_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* decrement the entire free size from the available bytes count. */
|
||||
heap->available_size = heap->available_size - free_size;
|
||||
if (heap->pool_size - heap->available_size > heap->max_used_size)
|
||||
heap->max_used_size = heap->pool_size - heap->available_size;
|
||||
|
||||
/* remove header_ptr from free list */
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x\n",
|
||||
header_ptr,
|
||||
header_ptr->next_free,
|
||||
header_ptr->prev_free));
|
||||
|
||||
header_ptr->next_free->prev_free = header_ptr->prev_free;
|
||||
header_ptr->prev_free->next_free = header_ptr->next_free;
|
||||
header_ptr->next_free = RT_NULL;
|
||||
header_ptr->prev_free = RT_NULL;
|
||||
}
|
||||
|
||||
/* Mark the allocated block as not available. */
|
||||
header_ptr->magic |= RT_MEMHEAP_USED;
|
||||
|
||||
/* release lock */
|
||||
rt_sem_release(&(heap->lock));
|
||||
|
||||
/* Return a memory address to the caller. */
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("alloc mem: memory[0x%08x], heap[0x%08x], size: %d\n",
|
||||
(void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
|
||||
header_ptr,
|
||||
size));
|
||||
|
||||
return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE);
|
||||
}
|
||||
|
||||
/* release lock */
|
||||
rt_sem_release(&(heap->lock));
|
||||
}
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate memory: failed\n"));
|
||||
|
||||
/* Return the completion status. */
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(rt_memheap_alloc);
|
||||
|
||||
void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
|
||||
{
|
||||
rt_err_t result;
|
||||
rt_size_t oldsize;
|
||||
struct rt_memheap_item *header_ptr;
|
||||
struct rt_memheap_item *new_ptr;
|
||||
|
||||
if (newsize == 0)
|
||||
{
|
||||
rt_memheap_free(ptr);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
/* align allocated size */
|
||||
newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
|
||||
if (newsize < RT_MEMHEAP_MINIALLOC)
|
||||
newsize = RT_MEMHEAP_MINIALLOC;
|
||||
|
||||
if (ptr == RT_NULL)
|
||||
{
|
||||
return rt_memheap_alloc(heap, newsize);
|
||||
}
|
||||
|
||||
/* get memory block header and get the size of memory block */
|
||||
header_ptr = (struct rt_memheap_item *)
|
||||
((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
|
||||
oldsize = MEMITEM_SIZE(header_ptr);
|
||||
/* re-allocate memory */
|
||||
if (newsize > oldsize)
|
||||
{
|
||||
void* new_ptr;
|
||||
struct rt_memheap_item *next_ptr;
|
||||
|
||||
/* lock memheap */
|
||||
result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_set_errno(result);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
next_ptr = header_ptr->next;
|
||||
|
||||
/* header_ptr should not be the tail */
|
||||
RT_ASSERT(next_ptr > header_ptr);
|
||||
|
||||
/* check whether the following free space is enough to expand */
|
||||
if (!RT_MEMHEAP_IS_USED(next_ptr))
|
||||
{
|
||||
rt_int32_t nextsize;
|
||||
|
||||
nextsize = MEMITEM_SIZE(next_ptr);
|
||||
RT_ASSERT(next_ptr > 0);
|
||||
|
||||
/* Here is the ASCII art of the situation that we can make use of
|
||||
* the next free node without alloc/memcpy, |*| is the control
|
||||
* block:
|
||||
*
|
||||
* oldsize free node
|
||||
* |*|-----------|*|----------------------|*|
|
||||
* newsize >= minialloc
|
||||
* |*|----------------|*|-----------------|*|
|
||||
*/
|
||||
if (nextsize + oldsize > newsize + RT_MEMHEAP_MINIALLOC)
|
||||
{
|
||||
/* decrement the entire free size from the available bytes count. */
|
||||
heap->available_size = heap->available_size - (newsize - oldsize);
|
||||
if (heap->pool_size - heap->available_size > heap->max_used_size)
|
||||
heap->max_used_size = heap->pool_size - heap->available_size;
|
||||
|
||||
/* remove next_ptr from free list */
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("remove block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x",
|
||||
next_ptr,
|
||||
next_ptr->next_free,
|
||||
next_ptr->prev_free));
|
||||
|
||||
next_ptr->next_free->prev_free = next_ptr->prev_free;
|
||||
next_ptr->prev_free->next_free = next_ptr->next_free;
|
||||
next_ptr->next->prev = next_ptr->prev;
|
||||
next_ptr->prev->next = next_ptr->next;
|
||||
|
||||
/* build a new one on the right place */
|
||||
next_ptr = (struct rt_memheap_item*)((char*)ptr + newsize);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("new free block: block[0x%08x] nextm[0x%08x] prevm[0x%08x]",
|
||||
next_ptr,
|
||||
next_ptr->next,
|
||||
next_ptr->prev));
|
||||
|
||||
/* mark the new block as a memory block and freed. */
|
||||
next_ptr->magic = RT_MEMHEAP_MAGIC;
|
||||
|
||||
/* put the pool pointer into the new block. */
|
||||
next_ptr->pool_ptr = heap;
|
||||
|
||||
next_ptr->prev = header_ptr;
|
||||
next_ptr->next = header_ptr->next;
|
||||
header_ptr->next->prev = next_ptr;
|
||||
header_ptr->next = next_ptr;
|
||||
|
||||
/* insert next_ptr to free list */
|
||||
next_ptr->next_free = heap->free_list->next_free;
|
||||
next_ptr->prev_free = heap->free_list;
|
||||
heap->free_list->next_free->prev_free = next_ptr;
|
||||
heap->free_list->next_free = next_ptr;
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x",
|
||||
next_ptr->next_free,
|
||||
next_ptr->prev_free));
|
||||
|
||||
/* release lock */
|
||||
rt_sem_release(&(heap->lock));
|
||||
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
|
||||
/* release lock */
|
||||
rt_sem_release(&(heap->lock));
|
||||
|
||||
/* re-allocate a memory block */
|
||||
new_ptr = (void*)rt_memheap_alloc(heap, newsize);
|
||||
if (new_ptr != RT_NULL)
|
||||
{
|
||||
rt_memcpy(new_ptr, ptr, oldsize < newsize ? oldsize : newsize);
|
||||
rt_memheap_free(ptr);
|
||||
}
|
||||
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
/* don't split when there is less than one node space left */
|
||||
if (newsize + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC >= oldsize)
|
||||
return ptr;
|
||||
|
||||
/* lock memheap */
|
||||
result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_set_errno(result);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/* split the block. */
|
||||
new_ptr = (struct rt_memheap_item *)
|
||||
(((rt_uint8_t *)header_ptr) + newsize + RT_MEMHEAP_SIZE);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
|
||||
header_ptr,
|
||||
header_ptr->next,
|
||||
header_ptr->prev,
|
||||
new_ptr));
|
||||
|
||||
/* mark the new block as a memory block and freed. */
|
||||
new_ptr->magic = RT_MEMHEAP_MAGIC;
|
||||
/* put the pool pointer into the new block. */
|
||||
new_ptr->pool_ptr = heap;
|
||||
|
||||
/* break down the block list */
|
||||
new_ptr->prev = header_ptr;
|
||||
new_ptr->next = header_ptr->next;
|
||||
header_ptr->next->prev = new_ptr;
|
||||
header_ptr->next = new_ptr;
|
||||
|
||||
/* determine if the block can be merged with the next neighbor. */
|
||||
if (!RT_MEMHEAP_IS_USED(new_ptr->next))
|
||||
{
|
||||
struct rt_memheap_item *free_ptr;
|
||||
|
||||
/* merge block with next neighbor. */
|
||||
free_ptr = new_ptr->next;
|
||||
heap->available_size = heap->available_size - MEMITEM_SIZE(free_ptr);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
|
||||
header_ptr, header_ptr->next_free, header_ptr->prev_free));
|
||||
|
||||
free_ptr->next->prev = new_ptr;
|
||||
new_ptr->next = free_ptr->next;
|
||||
|
||||
/* remove free ptr from free list */
|
||||
free_ptr->next_free->prev_free = free_ptr->prev_free;
|
||||
free_ptr->prev_free->next_free = free_ptr->next_free;
|
||||
}
|
||||
|
||||
/* insert the split block to free list */
|
||||
new_ptr->next_free = heap->free_list->next_free;
|
||||
new_ptr->prev_free = heap->free_list;
|
||||
heap->free_list->next_free->prev_free = new_ptr;
|
||||
heap->free_list->next_free = new_ptr;
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new free ptr: next_free 0x%08x, prev_free 0x%08x\n",
|
||||
new_ptr->next_free,
|
||||
new_ptr->prev_free));
|
||||
|
||||
/* increment the available byte count. */
|
||||
heap->available_size = heap->available_size + MEMITEM_SIZE(new_ptr);
|
||||
|
||||
/* release lock */
|
||||
rt_sem_release(&(heap->lock));
|
||||
|
||||
/* return the old memory block */
|
||||
return ptr;
|
||||
}
|
||||
RTM_EXPORT(rt_memheap_realloc);
|
||||
|
||||
void rt_memheap_free(void *ptr)
|
||||
{
|
||||
rt_err_t result;
|
||||
struct rt_memheap *heap;
|
||||
struct rt_memheap_item *header_ptr, *new_ptr;
|
||||
rt_uint32_t insert_header;
|
||||
|
||||
/* NULL check */
|
||||
if (ptr == RT_NULL) return;
|
||||
|
||||
/* set initial status as OK */
|
||||
insert_header = 1;
|
||||
new_ptr = RT_NULL;
|
||||
header_ptr = (struct rt_memheap_item *)
|
||||
((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
|
||||
ptr, header_ptr));
|
||||
|
||||
/* check magic */
|
||||
RT_ASSERT((header_ptr->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
|
||||
|
||||
/* get pool ptr */
|
||||
heap = header_ptr->pool_ptr;
|
||||
|
||||
/* lock memheap */
|
||||
result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_set_errno(result);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/* Mark the memory as available. */
|
||||
header_ptr->magic &= ~RT_MEMHEAP_USED;
|
||||
/* Adjust the available number of bytes. */
|
||||
heap->available_size = heap->available_size + MEMITEM_SIZE(header_ptr);
|
||||
|
||||
/* Determine if the block can be merged with the previous neighbor. */
|
||||
if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
|
||||
header_ptr->prev));
|
||||
|
||||
/* adjust the available number of bytes. */
|
||||
heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
|
||||
|
||||
/* yes, merge block with previous neighbor. */
|
||||
(header_ptr->prev)->next = header_ptr->next;
|
||||
(header_ptr->next)->prev = header_ptr->prev;
|
||||
|
||||
/* move header pointer to previous. */
|
||||
header_ptr = header_ptr->prev;
|
||||
/* don't insert header to free list */
|
||||
insert_header = 0;
|
||||
}
|
||||
|
||||
/* determine if the block can be merged with the next neighbor. */
|
||||
if (!RT_MEMHEAP_IS_USED(header_ptr->next))
|
||||
{
|
||||
/* adjust the available number of bytes. */
|
||||
heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
|
||||
|
||||
/* merge block with next neighbor. */
|
||||
new_ptr = header_ptr->next;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
|
||||
new_ptr, new_ptr->next_free, new_ptr->prev_free));
|
||||
|
||||
new_ptr->next->prev = header_ptr;
|
||||
header_ptr->next = new_ptr->next;
|
||||
|
||||
/* remove new ptr from free list */
|
||||
new_ptr->next_free->prev_free = new_ptr->prev_free;
|
||||
new_ptr->prev_free->next_free = new_ptr->next_free;
|
||||
}
|
||||
|
||||
if (insert_header)
|
||||
{
|
||||
/* no left merge, insert to free list */
|
||||
header_ptr->next_free = heap->free_list->next_free;
|
||||
header_ptr->prev_free = heap->free_list;
|
||||
heap->free_list->next_free->prev_free = header_ptr;
|
||||
heap->free_list->next_free = header_ptr;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
|
||||
("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
|
||||
header_ptr->next_free, header_ptr->prev_free));
|
||||
}
|
||||
|
||||
/* release lock */
|
||||
rt_sem_release(&(heap->lock));
|
||||
}
|
||||
RTM_EXPORT(rt_memheap_free);
|
||||
|
||||
#ifdef RT_USING_MEMHEAP_AS_HEAP
|
||||
static struct rt_memheap _heap;
|
||||
|
||||
void rt_system_heap_init(void *begin_addr, void *end_addr)
|
||||
{
|
||||
/* initialize a default heap in the system */
|
||||
rt_memheap_init(&_heap,
|
||||
"heap",
|
||||
begin_addr,
|
||||
(rt_uint32_t)end_addr - (rt_uint32_t)begin_addr);
|
||||
}
|
||||
|
||||
void *rt_malloc(rt_size_t size)
|
||||
{
|
||||
void* ptr;
|
||||
|
||||
/* try to allocate in system heap */
|
||||
ptr = rt_memheap_alloc(&_heap, size);
|
||||
if (ptr == RT_NULL)
|
||||
{
|
||||
struct rt_object *object;
|
||||
struct rt_list_node *node;
|
||||
struct rt_memheap *heap;
|
||||
struct rt_object_information *information;
|
||||
extern struct rt_object_information rt_object_container[];
|
||||
|
||||
/* try to allocate on other memory heap */
|
||||
information = &rt_object_container[RT_Object_Class_MemHeap];
|
||||
for (node = information->object_list.next;
|
||||
node != &(information->object_list);
|
||||
node = node->next)
|
||||
{
|
||||
object = rt_list_entry(node, struct rt_object, list);
|
||||
heap = (struct rt_memheap *)object;
|
||||
|
||||
/* not allocate in the default system heap */
|
||||
if (heap == &_heap)
|
||||
continue;
|
||||
|
||||
ptr = rt_memheap_alloc(heap, size);
|
||||
if (ptr != RT_NULL)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
RTM_EXPORT(rt_malloc);
|
||||
|
||||
void rt_free(void *rmem)
|
||||
{
|
||||
rt_memheap_free(rmem);
|
||||
}
|
||||
RTM_EXPORT(rt_free);
|
||||
|
||||
void *rt_realloc(void *rmem, rt_size_t newsize)
|
||||
{
|
||||
void *new_ptr;
|
||||
struct rt_memheap_item *header_ptr;
|
||||
|
||||
if (rmem == RT_NULL)
|
||||
return rt_malloc(newsize);
|
||||
|
||||
/* get old memory item */
|
||||
header_ptr = (struct rt_memheap_item *)
|
||||
((rt_uint8_t *)rmem - RT_MEMHEAP_SIZE);
|
||||
|
||||
new_ptr = rt_memheap_realloc(header_ptr->pool_ptr, rmem, newsize);
|
||||
if (new_ptr == RT_NULL && newsize != 0)
|
||||
{
|
||||
/* allocate memory block from other memheap */
|
||||
new_ptr = rt_malloc(newsize);
|
||||
if (new_ptr != RT_NULL && rmem != RT_NULL)
|
||||
{
|
||||
rt_size_t oldsize;
|
||||
|
||||
/* get the size of old memory block */
|
||||
oldsize = MEMITEM_SIZE(header_ptr);
|
||||
if (newsize > oldsize)
|
||||
rt_memcpy(new_ptr, rmem, oldsize);
|
||||
else
|
||||
rt_memcpy(new_ptr, rmem, newsize);
|
||||
}
|
||||
}
|
||||
|
||||
return new_ptr;
|
||||
}
|
||||
RTM_EXPORT(rt_realloc);
|
||||
|
||||
void *rt_calloc(rt_size_t count, rt_size_t size)
|
||||
{
|
||||
void *ptr;
|
||||
rt_size_t total_size;
|
||||
|
||||
total_size = count * size;
|
||||
ptr = rt_malloc(total_size);
|
||||
if (ptr != RT_NULL)
|
||||
{
|
||||
/* clean memory */
|
||||
rt_memset(ptr, 0, total_size);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
RTM_EXPORT(rt_calloc);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,470 @@
|
||||
/*
|
||||
* File : mempool.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-05-27 Bernard implement memory pool
|
||||
* 2006-06-03 Bernard fix the thread timer init bug
|
||||
* 2006-06-30 Bernard fix the allocate/free block bug
|
||||
* 2006-08-04 Bernard add hook support
|
||||
* 2006-08-10 Bernard fix interrupt bug in rt_mp_alloc
|
||||
* 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
|
||||
* 2010-10-26 yi.qiu add module support in rt_mp_delete
|
||||
* 2011-01-24 Bernard add object allocation check.
|
||||
* 2012-03-22 Bernard fix align issue in rt_mp_init and rt_mp_create.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
static void (*rt_mp_alloc_hook)(struct rt_mempool *mp, void *block);
|
||||
static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block);
|
||||
|
||||
/**
|
||||
* @addtogroup Hook
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when a memory
|
||||
* block is allocated from memory pool.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
|
||||
{
|
||||
rt_mp_alloc_hook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when a memory
|
||||
* block is released to memory pool.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
|
||||
{
|
||||
rt_mp_free_hook = hook;
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @addtogroup MM
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will initialize a memory pool object, normally which is used
|
||||
* for static object.
|
||||
*
|
||||
* @param mp the memory pool object
|
||||
* @param name the name of memory pool
|
||||
* @param start the star address of memory pool
|
||||
* @param size the total size of memory pool
|
||||
* @param block_size the size for each block
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t rt_mp_init(struct rt_mempool *mp,
|
||||
const char *name,
|
||||
void *start,
|
||||
rt_size_t size,
|
||||
rt_size_t block_size)
|
||||
{
|
||||
rt_uint8_t *block_ptr;
|
||||
register rt_base_t offset;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(mp != RT_NULL);
|
||||
|
||||
/* initialize object */
|
||||
rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name);
|
||||
|
||||
/* initialize memory pool */
|
||||
mp->start_address = start;
|
||||
mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
||||
|
||||
/* align the block size */
|
||||
block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
|
||||
mp->block_size = block_size;
|
||||
|
||||
/* align to align size byte */
|
||||
mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *));
|
||||
mp->block_free_count = mp->block_total_count;
|
||||
|
||||
/* initialize suspended thread list */
|
||||
rt_list_init(&(mp->suspend_thread));
|
||||
mp->suspend_thread_count = 0;
|
||||
|
||||
/* initialize free block list */
|
||||
block_ptr = (rt_uint8_t *)mp->start_address;
|
||||
for (offset = 0; offset < mp->block_total_count; offset ++)
|
||||
{
|
||||
*(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *))) =
|
||||
(rt_uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *)));
|
||||
}
|
||||
|
||||
*(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) =
|
||||
RT_NULL;
|
||||
|
||||
mp->block_list = block_ptr;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_mp_init);
|
||||
|
||||
/**
|
||||
* This function will detach a memory pool from system object management.
|
||||
*
|
||||
* @param mp the memory pool object
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t rt_mp_detach(struct rt_mempool *mp)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
register rt_ubase_t temp;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(mp != RT_NULL);
|
||||
|
||||
/* wake up all suspended threads */
|
||||
while (!rt_list_isempty(&(mp->suspend_thread)))
|
||||
{
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* get next suspend thread */
|
||||
thread = rt_list_entry(mp->suspend_thread.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);
|
||||
|
||||
/* decrease suspended thread count */
|
||||
mp->suspend_thread_count --;
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
|
||||
/* detach object */
|
||||
rt_object_detach(&(mp->parent));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_mp_detach);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/**
|
||||
* This function will create a mempool object and allocate the memory pool from
|
||||
* heap.
|
||||
*
|
||||
* @param name the name of memory pool
|
||||
* @param block_count the count of blocks in memory pool
|
||||
* @param block_size the size for each block
|
||||
*
|
||||
* @return the created mempool object
|
||||
*/
|
||||
rt_mp_t rt_mp_create(const char *name,
|
||||
rt_size_t block_count,
|
||||
rt_size_t block_size)
|
||||
{
|
||||
rt_uint8_t *block_ptr;
|
||||
struct rt_mempool *mp;
|
||||
register rt_base_t offset;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* allocate object */
|
||||
mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
|
||||
/* allocate object failed */
|
||||
if (mp == RT_NULL)
|
||||
return RT_NULL;
|
||||
|
||||
/* initialize memory pool */
|
||||
block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
|
||||
mp->block_size = block_size;
|
||||
mp->size = (block_size + sizeof(rt_uint8_t *)) * block_count;
|
||||
|
||||
/* allocate memory */
|
||||
mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) *
|
||||
block_count);
|
||||
if (mp->start_address == RT_NULL)
|
||||
{
|
||||
/* no memory, delete memory pool object */
|
||||
rt_object_delete(&(mp->parent));
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
mp->block_total_count = block_count;
|
||||
mp->block_free_count = mp->block_total_count;
|
||||
|
||||
/* initialize suspended thread list */
|
||||
rt_list_init(&(mp->suspend_thread));
|
||||
mp->suspend_thread_count = 0;
|
||||
|
||||
/* initialize free block list */
|
||||
block_ptr = (rt_uint8_t *)mp->start_address;
|
||||
for (offset = 0; offset < mp->block_total_count; offset ++)
|
||||
{
|
||||
*(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
|
||||
= block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *));
|
||||
}
|
||||
|
||||
*(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *)))
|
||||
= RT_NULL;
|
||||
|
||||
mp->block_list = block_ptr;
|
||||
|
||||
return mp;
|
||||
}
|
||||
RTM_EXPORT(rt_mp_create);
|
||||
|
||||
/**
|
||||
* This function will delete a memory pool and release the object memory.
|
||||
*
|
||||
* @param mp the memory pool object
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t rt_mp_delete(rt_mp_t mp)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
register rt_ubase_t temp;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(mp != RT_NULL);
|
||||
|
||||
/* wake up all suspended threads */
|
||||
while (!rt_list_isempty(&(mp->suspend_thread)))
|
||||
{
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* get next suspend thread */
|
||||
thread = rt_list_entry(mp->suspend_thread.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);
|
||||
|
||||
/* decrease suspended thread count */
|
||||
mp->suspend_thread_count --;
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
|
||||
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
|
||||
/* the mp object belongs to an application module */
|
||||
if (mp->parent.flag & RT_OBJECT_FLAG_MODULE)
|
||||
rt_module_free(mp->parent.module_id, mp->start_address);
|
||||
else
|
||||
#endif
|
||||
|
||||
/* release allocated room */
|
||||
rt_free(mp->start_address);
|
||||
|
||||
/* detach object */
|
||||
rt_object_delete(&(mp->parent));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_mp_delete);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will allocate a block from memory pool
|
||||
*
|
||||
* @param mp the memory pool object
|
||||
* @param time the waiting time
|
||||
*
|
||||
* @return the allocated memory block or RT_NULL on allocated failed
|
||||
*/
|
||||
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
|
||||
{
|
||||
rt_uint8_t *block_ptr;
|
||||
register rt_base_t level;
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
if (mp->block_free_count)
|
||||
{
|
||||
/* memory block is available. decrease the free block counter */
|
||||
mp->block_free_count --;
|
||||
|
||||
/* get block from block list */
|
||||
block_ptr = mp->block_list;
|
||||
mp->block_list = *(rt_uint8_t **)block_ptr;
|
||||
|
||||
/* point to memory pool */
|
||||
*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* memory block is unavailable. */
|
||||
if (time == 0)
|
||||
{
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* get current thread */
|
||||
thread = rt_thread_self();
|
||||
|
||||
thread->error = RT_EOK;
|
||||
|
||||
/* need suspend thread */
|
||||
rt_thread_suspend(thread);
|
||||
rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
|
||||
mp->suspend_thread_count ++;
|
||||
|
||||
if (time > 0)
|
||||
{
|
||||
/* init thread timer and start it */
|
||||
rt_timer_control(&(thread->thread_timer),
|
||||
RT_TIMER_CTRL_SET_TIME,
|
||||
&time);
|
||||
rt_timer_start(&(thread->thread_timer));
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* do a schedule */
|
||||
rt_schedule();
|
||||
|
||||
if (thread->error != RT_EOK)
|
||||
return RT_NULL;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/* decrease free block */
|
||||
mp->block_free_count --;
|
||||
|
||||
/* get block from block list */
|
||||
block_ptr = mp->block_list;
|
||||
mp->block_list = *(rt_uint8_t **)block_ptr;
|
||||
|
||||
/* point to memory pool */
|
||||
*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
|
||||
}
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook,
|
||||
(mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));
|
||||
|
||||
return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
|
||||
}
|
||||
RTM_EXPORT(rt_mp_alloc);
|
||||
|
||||
/**
|
||||
* This function will release a memory block
|
||||
*
|
||||
* @param block the address of memory block to be released
|
||||
*/
|
||||
void rt_mp_free(void *block)
|
||||
{
|
||||
rt_uint8_t **block_ptr;
|
||||
struct rt_mempool *mp;
|
||||
struct rt_thread *thread;
|
||||
register rt_base_t level;
|
||||
|
||||
/* get the control block of pool which the block belongs to */
|
||||
block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *));
|
||||
mp = (struct rt_mempool *)*block_ptr;
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block));
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/* increase the free block count */
|
||||
mp->block_free_count ++;
|
||||
|
||||
/* link the block into the block list */
|
||||
*block_ptr = mp->block_list;
|
||||
mp->block_list = (rt_uint8_t *)block_ptr;
|
||||
|
||||
if (mp->suspend_thread_count > 0)
|
||||
{
|
||||
/* get the suspended thread */
|
||||
thread = rt_list_entry(mp->suspend_thread.next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
|
||||
/* set error */
|
||||
thread->error = RT_EOK;
|
||||
|
||||
/* resume thread */
|
||||
rt_thread_resume(thread);
|
||||
|
||||
/* decrease suspended thread count */
|
||||
mp->suspend_thread_count --;
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* do a schedule */
|
||||
rt_schedule();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
RTM_EXPORT(rt_mp_free);
|
||||
|
||||
/*@}*/
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,258 @@
|
||||
/*
|
||||
* File : module.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
|
||||
* 2010-01-09 Bernard first version
|
||||
* 2010-04-09 yi.qiu implement based on first version
|
||||
*/
|
||||
|
||||
#ifndef __MODULE_H__
|
||||
#define __MODULE_H__
|
||||
|
||||
#include <rtdef.h>
|
||||
|
||||
typedef rt_uint8_t Elf_Byte;
|
||||
|
||||
typedef rt_uint32_t Elf32_Addr; /* Unsigned program address */
|
||||
typedef rt_uint32_t Elf32_Off; /* Unsigned file offset */
|
||||
typedef rt_int32_t Elf32_Sword; /* Signed large integer */
|
||||
typedef rt_uint32_t Elf32_Word; /* Unsigned large integer */
|
||||
typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */
|
||||
|
||||
/* e_ident[] magic number */
|
||||
#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
|
||||
#define ELFMAG1 'E' /* e_ident[EI_MAG1] */
|
||||
#define ELFMAG2 'L' /* e_ident[EI_MAG2] */
|
||||
#define ELFMAG3 'F' /* e_ident[EI_MAG3] */
|
||||
#define RTMMAG "\177RTM" /* magic */
|
||||
#define ELFMAG "\177ELF" /* magic */
|
||||
#define SELFMAG 4 /* size of magic */
|
||||
|
||||
#define EI_CLASS 4 /* file class */
|
||||
#define EI_NIDENT 16 /* Size of e_ident[] */
|
||||
|
||||
/* e_ident[] file class */
|
||||
#define ELFCLASSNONE 0 /* invalid */
|
||||
#define ELFCLASS32 1 /* 32-bit objs */
|
||||
#define ELFCLASS64 2 /* 64-bit objs */
|
||||
#define ELFCLASSNUM 3 /* number of classes */
|
||||
|
||||
/* e_ident[] data encoding */
|
||||
#define ELFDATANONE 0 /* invalid */
|
||||
#define ELFDATA2LSB 1 /* Little-Endian */
|
||||
#define ELFDATA2MSB 2 /* Big-Endian */
|
||||
#define ELFDATANUM 3 /* number of data encode defines */
|
||||
|
||||
/* e_ident */
|
||||
#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
|
||||
(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
|
||||
(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
|
||||
(ehdr).e_ident[EI_MAG3] == ELFMAG3)
|
||||
|
||||
#define ET_NONE 0 /* No file type */
|
||||
#define ET_REL 1 /* Relocatable file */
|
||||
#define ET_EXEC 2 /* Executable file */
|
||||
#define ET_DYN 3 /* Shared object file */
|
||||
#define ET_CORE 4 /* Core file */
|
||||
|
||||
/* ELF Header */
|
||||
typedef struct elfhdr
|
||||
{
|
||||
unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
|
||||
Elf32_Half e_type; /* object file type */
|
||||
Elf32_Half e_machine; /* machine */
|
||||
Elf32_Word e_version; /* object file version */
|
||||
Elf32_Addr e_entry; /* virtual entry point */
|
||||
Elf32_Off e_phoff; /* program header table offset */
|
||||
Elf32_Off e_shoff; /* section header table offset */
|
||||
Elf32_Word e_flags; /* processor-specific flags */
|
||||
Elf32_Half e_ehsize; /* ELF header size */
|
||||
Elf32_Half e_phentsize; /* program header entry size */
|
||||
Elf32_Half e_phnum; /* number of program header entries */
|
||||
Elf32_Half e_shentsize; /* section header entry size */
|
||||
Elf32_Half e_shnum; /* number of section header entries */
|
||||
Elf32_Half e_shstrndx; /* section header table's "section
|
||||
header string table" entry offset */
|
||||
} Elf32_Ehdr;
|
||||
|
||||
/* Section Header */
|
||||
typedef struct
|
||||
{
|
||||
Elf32_Word sh_name; /* name - index into section header
|
||||
string table section */
|
||||
Elf32_Word sh_type; /* type */
|
||||
Elf32_Word sh_flags; /* flags */
|
||||
Elf32_Addr sh_addr; /* address */
|
||||
Elf32_Off sh_offset; /* file offset */
|
||||
Elf32_Word sh_size; /* section size */
|
||||
Elf32_Word sh_link; /* section header table index link */
|
||||
Elf32_Word sh_info; /* extra information */
|
||||
Elf32_Word sh_addralign; /* address alignment */
|
||||
Elf32_Word sh_entsize; /* section entry size */
|
||||
} Elf32_Shdr;
|
||||
|
||||
/* Section names */
|
||||
#define ELF_BSS ".bss" /* uninitialized data */
|
||||
#define ELF_DATA ".data" /* initialized data */
|
||||
#define ELF_DEBUG ".debug" /* debug */
|
||||
#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
|
||||
#define ELF_DYNSTR ".dynstr" /* dynamic string table */
|
||||
#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
|
||||
#define ELF_FINI ".fini" /* termination code */
|
||||
#define ELF_GOT ".got" /* global offset table */
|
||||
#define ELF_HASH ".hash" /* symbol hash table */
|
||||
#define ELF_INIT ".init" /* initialization code */
|
||||
#define ELF_REL_DATA ".rel.data" /* relocation data */
|
||||
#define ELF_REL_FINI ".rel.fini" /* relocation termination code */
|
||||
#define ELF_REL_INIT ".rel.init" /* relocation initialization code */
|
||||
#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
|
||||
#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
|
||||
#define ELF_REL_TEXT ".rel.text" /* relocation code */
|
||||
#define ELF_RODATA ".rodata" /* read-only data */
|
||||
#define ELF_SHSTRTAB ".shstrtab" /* section header string table */
|
||||
#define ELF_STRTAB ".strtab" /* string table */
|
||||
#define ELF_SYMTAB ".symtab" /* symbol table */
|
||||
#define ELF_TEXT ".text" /* code */
|
||||
#define ELF_RTMSYMTAB "RTMSymTab"
|
||||
|
||||
/* Symbol Table Entry */
|
||||
typedef struct elf32_sym
|
||||
{
|
||||
Elf32_Word st_name; /* name - index into string table */
|
||||
Elf32_Addr st_value; /* symbol value */
|
||||
Elf32_Word st_size; /* symbol size */
|
||||
unsigned char st_info; /* type and binding */
|
||||
unsigned char st_other; /* 0 - no defined meaning */
|
||||
Elf32_Half st_shndx; /* section header index */
|
||||
} Elf32_Sym;
|
||||
|
||||
#define STB_LOCAL 0 /* BIND */
|
||||
#define STB_GLOBAL 1
|
||||
#define STB_WEAK 2
|
||||
#define STB_NUM 3
|
||||
|
||||
#define STB_LOPROC 13 /* processor specific range */
|
||||
#define STB_HIPROC 15
|
||||
|
||||
#define STT_NOTYPE 0 /* symbol type is unspecified */
|
||||
#define STT_OBJECT 1 /* data object */
|
||||
#define STT_FUNC 2 /* code object */
|
||||
#define STT_SECTION 3 /* symbol identifies an ELF section */
|
||||
#define STT_FILE 4 /* symbol's name is file name */
|
||||
#define STT_COMMON 5 /* common data object */
|
||||
#define STT_TLS 6 /* thread-local data object */
|
||||
#define STT_NUM 7 /* # defined types in generic range */
|
||||
#define STT_LOOS 10 /* OS specific range */
|
||||
#define STT_HIOS 12
|
||||
#define STT_LOPROC 13 /* processor specific range */
|
||||
#define STT_HIPROC 15
|
||||
|
||||
#define STN_UNDEF 0 /* undefined */
|
||||
|
||||
#define ELF_ST_BIND(info) ((info) >> 4)
|
||||
#define ELF_ST_TYPE(info) ((info) & 0xf)
|
||||
#define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf))
|
||||
|
||||
/* Relocation entry with implicit addend */
|
||||
typedef struct
|
||||
{
|
||||
Elf32_Addr r_offset; /* offset of relocation */
|
||||
Elf32_Word r_info; /* symbol table index and type */
|
||||
} Elf32_Rel;
|
||||
|
||||
/* Relocation entry with explicit addend */
|
||||
typedef struct
|
||||
{
|
||||
Elf32_Addr r_offset; /* offset of relocation */
|
||||
Elf32_Word r_info; /* symbol table index and type */
|
||||
Elf32_Sword r_addend;
|
||||
} Elf32_Rela;
|
||||
|
||||
/* Extract relocation info - r_info */
|
||||
#define ELF32_R_SYM(i) ((i) >> 8)
|
||||
#define ELF32_R_TYPE(i) ((unsigned char) (i))
|
||||
#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
|
||||
|
||||
/*
|
||||
* Relocation type for arm
|
||||
*/
|
||||
#define R_ARM_NONE 0
|
||||
#define R_ARM_PC24 1
|
||||
#define R_ARM_ABS32 2
|
||||
#define R_ARM_REL32 3
|
||||
#define R_ARM_THM_CALL 10
|
||||
#define R_ARM_GLOB_DAT 21
|
||||
#define R_ARM_JUMP_SLOT 22
|
||||
#define R_ARM_RELATIVE 23
|
||||
#define R_ARM_GOT_BREL 26
|
||||
#define R_ARM_PLT32 27
|
||||
#define R_ARM_CALL 28
|
||||
#define R_ARM_JUMP24 29
|
||||
#define R_ARM_THM_JUMP24 30
|
||||
#define R_ARM_V4BX 40
|
||||
|
||||
/* Program Header */
|
||||
typedef struct
|
||||
{
|
||||
Elf32_Word p_type; /* segment type */
|
||||
Elf32_Off p_offset; /* segment offset */
|
||||
Elf32_Addr p_vaddr; /* virtual address of segment */
|
||||
Elf32_Addr p_paddr; /* physical address - ignored? */
|
||||
Elf32_Word p_filesz; /* number of bytes in file for seg. */
|
||||
Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
|
||||
Elf32_Word p_flags; /* flags */
|
||||
Elf32_Word p_align; /* memory alignment */
|
||||
} Elf32_Phdr;
|
||||
|
||||
/* p_type */
|
||||
#define PT_LOAD 1
|
||||
|
||||
/* p_flags */
|
||||
#define PF_X 1
|
||||
#define PF_W 2
|
||||
#define PF_R 4
|
||||
|
||||
/* sh_type */
|
||||
#define SHT_NULL 0 /* inactive */
|
||||
#define SHT_PROGBITS 1 /* program defined information */
|
||||
#define SHT_SYMTAB 2 /* symbol table section */
|
||||
#define SHT_STRTAB 3 /* string table section */
|
||||
#define SHT_RELA 4 /* relocation section with addends*/
|
||||
#define SHT_HASH 5 /* symbol hash table section */
|
||||
#define SHT_DYNAMIC 6 /* dynamic section */
|
||||
#define SHT_NOTE 7 /* note section */
|
||||
#define SHT_NOBITS 8 /* no space section */
|
||||
#define SHT_REL 9 /* relation section without addends */
|
||||
#define SHT_SHLIB 10 /* reserved - purpose unknown */
|
||||
#define SHT_DYNSYM 11 /* dynamic symbol table section */
|
||||
#define SHT_NUM 12 /* number of section types */
|
||||
#define SHT_LOPROC 0x70000000 /* reserved range for processor */
|
||||
#define SHT_HIPROC 0x7fffffff /* specific section header types */
|
||||
#define SHT_LOUSER 0x80000000 /* reserved range for application */
|
||||
#define SHT_HIUSER 0xffffffff /* specific indexes */
|
||||
|
||||
/* Section Attribute Flags - sh_flags */
|
||||
#define SHF_WRITE 0x1 /* Writable */
|
||||
#define SHF_ALLOC 0x2 /* occupies memory */
|
||||
#define SHF_EXECINSTR 0x4 /* executable */
|
||||
#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
|
||||
/* specific section attributes */
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,442 @@
|
||||
/*
|
||||
* File : object.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-14 Bernard the first version
|
||||
* 2006-04-21 Bernard change the scheduler lock to interrupt lock
|
||||
* 2006-05-18 Bernard fix the object init bug
|
||||
* 2006-08-03 Bernard add hook support
|
||||
* 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static
|
||||
* 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
|
||||
#define _OBJ_CONTAINER_LIST_INIT(c) \
|
||||
{&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
|
||||
struct rt_object_information rt_object_container[RT_Object_Class_Unknown] =
|
||||
{
|
||||
/* initialize object container - thread */
|
||||
{RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Thread), sizeof(struct rt_thread)},
|
||||
#ifdef RT_USING_SEMAPHORE
|
||||
/* initialize object container - semaphore */
|
||||
{RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Semaphore), sizeof(struct rt_semaphore)},
|
||||
#endif
|
||||
#ifdef RT_USING_MUTEX
|
||||
/* initialize object container - mutex */
|
||||
{RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Mutex), sizeof(struct rt_mutex)},
|
||||
#endif
|
||||
#ifdef RT_USING_EVENT
|
||||
/* initialize object container - event */
|
||||
{RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Event), sizeof(struct rt_event)},
|
||||
#endif
|
||||
#ifdef RT_USING_MAILBOX
|
||||
/* initialize object container - mailbox */
|
||||
{RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MailBox), sizeof(struct rt_mailbox)},
|
||||
#endif
|
||||
#ifdef RT_USING_MESSAGEQUEUE
|
||||
/* initialize object container - message queue */
|
||||
{RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MessageQueue), sizeof(struct rt_messagequeue)},
|
||||
#endif
|
||||
#ifdef RT_USING_MEMHEAP
|
||||
/* initialize object container - memory heap */
|
||||
{RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemHeap), sizeof(struct rt_memheap)},
|
||||
#endif
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
/* initialize object container - memory pool */
|
||||
{RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemPool), sizeof(struct rt_mempool)},
|
||||
#endif
|
||||
#ifdef RT_USING_DEVICE
|
||||
/* initialize object container - device */
|
||||
{RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Device), sizeof(struct rt_device)},
|
||||
#endif
|
||||
/* initialize object container - timer */
|
||||
{RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Timer), sizeof(struct rt_timer)},
|
||||
#ifdef RT_USING_MODULE
|
||||
/* initialize object container - module */
|
||||
{RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Module), sizeof(struct rt_module)},
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
static void (*rt_object_attach_hook)(struct rt_object *object);
|
||||
static void (*rt_object_detach_hook)(struct rt_object *object);
|
||||
void (*rt_object_trytake_hook)(struct rt_object *object);
|
||||
void (*rt_object_take_hook)(struct rt_object *object);
|
||||
void (*rt_object_put_hook)(struct rt_object *object);
|
||||
|
||||
/**
|
||||
* @addtogroup Hook
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when object
|
||||
* attaches to kernel object system.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
|
||||
{
|
||||
rt_object_attach_hook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when object
|
||||
* detaches from kernel object system.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
|
||||
{
|
||||
rt_object_detach_hook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when object
|
||||
* is taken from kernel object system.
|
||||
*
|
||||
* The object is taken means:
|
||||
* semaphore - semaphore is taken by thread
|
||||
* mutex - mutex is taken by thread
|
||||
* event - event is received by thread
|
||||
* mailbox - mail is received by thread
|
||||
* message queue - message is received by thread
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
|
||||
{
|
||||
rt_object_trytake_hook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when object
|
||||
* have been taken from kernel object system.
|
||||
*
|
||||
* The object have been taken means:
|
||||
* semaphore - semaphore have been taken by thread
|
||||
* mutex - mutex have been taken by thread
|
||||
* event - event have been received by thread
|
||||
* mailbox - mail have been received by thread
|
||||
* message queue - message have been received by thread
|
||||
* timer - timer is started
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_object_take_sethook(void (*hook)(struct rt_object *object))
|
||||
{
|
||||
rt_object_take_hook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when object
|
||||
* is put to kernel object system.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_object_put_sethook(void (*hook)(struct rt_object *object))
|
||||
{
|
||||
rt_object_put_hook = hook;
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @ingroup SystemInit
|
||||
*
|
||||
* This function will initialize system object management.
|
||||
*
|
||||
* @deprecated since 0.3.0, this function does not need to be invoked
|
||||
* in the system initialization.
|
||||
*/
|
||||
void rt_system_object_init(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup KernelObject
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will return the specified type of object information.
|
||||
*
|
||||
* @param type the type of object
|
||||
* @return the object type information or RT_NULL
|
||||
*/
|
||||
struct rt_object_information *
|
||||
rt_object_get_information(enum rt_object_class_type type)
|
||||
{
|
||||
return &rt_object_container[type];
|
||||
}
|
||||
RTM_EXPORT(rt_object_get_information);
|
||||
|
||||
/**
|
||||
* This function will initialize an object and add it to object system
|
||||
* management.
|
||||
*
|
||||
* @param object the specified object to be initialized.
|
||||
* @param type the object type.
|
||||
* @param name the object name. In system, the object's name must be unique.
|
||||
*/
|
||||
void rt_object_init(struct rt_object *object,
|
||||
enum rt_object_class_type type,
|
||||
const char *name)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
struct rt_object_information *information;
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
/* get module object information */
|
||||
information = (rt_module_self() != RT_NULL) ?
|
||||
&rt_module_self()->module_object[type] : &rt_object_container[type];
|
||||
#else
|
||||
/* get object information */
|
||||
information = &rt_object_container[type];
|
||||
#endif
|
||||
|
||||
/* initialize object's parameters */
|
||||
|
||||
/* set object type to static */
|
||||
object->type = type | RT_Object_Class_Static;
|
||||
|
||||
/* copy name */
|
||||
rt_strncpy(object->name, name, RT_NAME_MAX);
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
|
||||
|
||||
/* lock interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* insert object into information object list */
|
||||
rt_list_insert_after(&(information->object_list), &(object->list));
|
||||
|
||||
/* unlock interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will detach a static object from object system,
|
||||
* and the memory of static object is not freed.
|
||||
*
|
||||
* @param object the specified object to be detached.
|
||||
*/
|
||||
void rt_object_detach(rt_object_t object)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
|
||||
/* object check */
|
||||
RT_ASSERT(object != RT_NULL);
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
|
||||
|
||||
/* lock interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* remove from old list */
|
||||
rt_list_remove(&(object->list));
|
||||
|
||||
/* unlock interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/**
|
||||
* This function will allocate an object from object system
|
||||
*
|
||||
* @param type the type of object
|
||||
* @param name the object name. In system, the object's name must be unique.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
|
||||
{
|
||||
struct rt_object *object;
|
||||
register rt_base_t temp;
|
||||
struct rt_object_information *information;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
/*
|
||||
* get module object information,
|
||||
* module object should be managed by kernel object container
|
||||
*/
|
||||
information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module)) ?
|
||||
&rt_module_self()->module_object[type] : &rt_object_container[type];
|
||||
#else
|
||||
/* get object information */
|
||||
information = &rt_object_container[type];
|
||||
#endif
|
||||
|
||||
object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
|
||||
if (object == RT_NULL)
|
||||
{
|
||||
/* no memory can be allocated */
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/* initialize object's parameters */
|
||||
|
||||
/* set object type */
|
||||
object->type = type;
|
||||
|
||||
/* set object flag */
|
||||
object->flag = 0;
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
if (rt_module_self() != RT_NULL)
|
||||
{
|
||||
object->flag |= RT_OBJECT_FLAG_MODULE;
|
||||
}
|
||||
object->module_id = (void *)rt_module_self();
|
||||
#endif
|
||||
|
||||
/* copy name */
|
||||
rt_strncpy(object->name, name, RT_NAME_MAX);
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
|
||||
|
||||
/* lock interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* insert object into information object list */
|
||||
rt_list_insert_after(&(information->object_list), &(object->list));
|
||||
|
||||
/* unlock interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
|
||||
/* return object */
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will delete an object and release object memory.
|
||||
*
|
||||
* @param object the specified object to be deleted.
|
||||
*/
|
||||
void rt_object_delete(rt_object_t object)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
|
||||
/* object check */
|
||||
RT_ASSERT(object != RT_NULL);
|
||||
RT_ASSERT(!(object->type & RT_Object_Class_Static));
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
|
||||
|
||||
/* lock interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* remove from old list */
|
||||
rt_list_remove(&(object->list));
|
||||
|
||||
/* unlock interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
|
||||
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
|
||||
if (object->flag & RT_OBJECT_FLAG_MODULE)
|
||||
rt_module_free((rt_module_t)object->module_id, object);
|
||||
else
|
||||
#endif
|
||||
|
||||
/* free the memory of object */
|
||||
RT_KERNEL_FREE(object);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will judge the object is system object or not.
|
||||
* Normally, the system object is a static object and the type
|
||||
* of object set to RT_Object_Class_Static.
|
||||
*
|
||||
* @param object the specified object to be judged.
|
||||
*
|
||||
* @return RT_TRUE if a system object, RT_FALSE for others.
|
||||
*/
|
||||
rt_bool_t rt_object_is_systemobject(rt_object_t object)
|
||||
{
|
||||
/* object check */
|
||||
RT_ASSERT(object != RT_NULL);
|
||||
|
||||
if (object->type & RT_Object_Class_Static)
|
||||
return RT_TRUE;
|
||||
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will find specified name object from object
|
||||
* container.
|
||||
*
|
||||
* @param name the specified name of object.
|
||||
* @param type the type of object
|
||||
*
|
||||
* @return the found object or RT_NULL if there is no this object
|
||||
* in object container.
|
||||
*
|
||||
* @note this function shall not be invoked in interrupt status.
|
||||
*/
|
||||
rt_object_t rt_object_find(const char *name, rt_uint8_t type)
|
||||
{
|
||||
struct rt_object *object;
|
||||
struct rt_list_node *node;
|
||||
struct rt_object_information *information;
|
||||
extern volatile rt_uint8_t rt_interrupt_nest;
|
||||
|
||||
/* parameter check */
|
||||
if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
|
||||
return RT_NULL;
|
||||
|
||||
/* which is invoke in interrupt status */
|
||||
if (rt_interrupt_nest != 0)
|
||||
RT_ASSERT(0);
|
||||
|
||||
/* enter critical */
|
||||
rt_enter_critical();
|
||||
|
||||
/* try to find object */
|
||||
information = &rt_object_container[type];
|
||||
for (node = information->object_list.next;
|
||||
node != &(information->object_list);
|
||||
node = node->next)
|
||||
{
|
||||
object = rt_list_entry(node, struct rt_object, list);
|
||||
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
|
||||
{
|
||||
/* leave critical */
|
||||
rt_exit_critical();
|
||||
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
/* leave critical */
|
||||
rt_exit_critical();
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
@@ -0,0 +1,412 @@
|
||||
/*
|
||||
* File : scheduler.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-17 Bernard the first version
|
||||
* 2006-04-28 Bernard fix the scheduler algorthm
|
||||
* 2006-04-30 Bernard add SCHEDULER_DEBUG
|
||||
* 2006-05-27 Bernard fix the scheduler algorthm for same priority
|
||||
* thread schedule
|
||||
* 2006-06-04 Bernard rewrite the scheduler algorithm
|
||||
* 2006-08-03 Bernard add hook support
|
||||
* 2006-09-05 Bernard add 32 priority level support
|
||||
* 2006-09-24 Bernard add rt_system_scheduler_start function
|
||||
* 2009-09-16 Bernard fix _rt_scheduler_stack_check
|
||||
* 2010-04-11 yi.qiu add module feature
|
||||
* 2010-07-13 Bernard fix the maximal number of rt_scheduler_lock_nest
|
||||
* issue found by kuronca
|
||||
* 2010-12-13 Bernard add defunct list initialization even if not use heap.
|
||||
* 2011-05-10 Bernard clean scheduler debug log.
|
||||
* 2013-12-21 Grissiom add rt_critical_level
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
|
||||
static rt_int16_t rt_scheduler_lock_nest;
|
||||
extern volatile rt_uint8_t rt_interrupt_nest;
|
||||
extern int __rt_ffs(int value);
|
||||
|
||||
rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
|
||||
struct rt_thread *rt_current_thread;
|
||||
|
||||
rt_uint8_t rt_current_priority;
|
||||
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
/* Maximum priority level, 256 */
|
||||
rt_uint32_t rt_thread_ready_priority_group;
|
||||
rt_uint8_t rt_thread_ready_table[32];
|
||||
#else
|
||||
/* Maximum priority level, 32 */
|
||||
rt_uint32_t rt_thread_ready_priority_group;
|
||||
#endif
|
||||
|
||||
rt_list_t rt_thread_defunct;
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
|
||||
|
||||
/**
|
||||
* @addtogroup Hook
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when thread
|
||||
* switch happens.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void
|
||||
rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
|
||||
{
|
||||
rt_scheduler_hook = hook;
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_OVERFLOW_CHECK
|
||||
static void _rt_scheduler_stack_check(struct rt_thread *thread)
|
||||
{
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
if ((rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr ||
|
||||
(rt_uint32_t)thread->sp >
|
||||
(rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size)
|
||||
{
|
||||
rt_uint32_t level;
|
||||
|
||||
rt_kprintf("thread:%s stack overflow\n", thread->name);
|
||||
#ifdef RT_USING_FINSH
|
||||
{
|
||||
extern long list_thread(void);
|
||||
list_thread();
|
||||
}
|
||||
#endif
|
||||
level = rt_hw_interrupt_disable();
|
||||
while (level);
|
||||
}
|
||||
else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32))
|
||||
{
|
||||
rt_kprintf("warning: %s stack is close to end of stack address.\n",
|
||||
thread->name);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @ingroup SystemInit
|
||||
* This function will initialize the system scheduler
|
||||
*/
|
||||
void rt_system_scheduler_init(void)
|
||||
{
|
||||
register rt_base_t offset;
|
||||
|
||||
rt_scheduler_lock_nest = 0;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
|
||||
RT_THREAD_PRIORITY_MAX));
|
||||
|
||||
for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
|
||||
{
|
||||
rt_list_init(&rt_thread_priority_table[offset]);
|
||||
}
|
||||
|
||||
rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;
|
||||
rt_current_thread = RT_NULL;
|
||||
|
||||
/* initialize ready priority group */
|
||||
rt_thread_ready_priority_group = 0;
|
||||
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
/* initialize ready table */
|
||||
rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
|
||||
#endif
|
||||
|
||||
/* initialize thread defunct */
|
||||
rt_list_init(&rt_thread_defunct);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup SystemInit
|
||||
* This function will startup scheduler. It will select one thread
|
||||
* with the highest priority level, then switch to it.
|
||||
*/
|
||||
void rt_system_scheduler_start(void)
|
||||
{
|
||||
register struct rt_thread *to_thread;
|
||||
register rt_ubase_t highest_ready_priority;
|
||||
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
register rt_ubase_t number;
|
||||
|
||||
number = __rt_ffs(rt_thread_ready_priority_group) - 1;
|
||||
highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
|
||||
#else
|
||||
highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
|
||||
#endif
|
||||
|
||||
/* get switch to thread */
|
||||
to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
|
||||
rt_current_thread = to_thread;
|
||||
|
||||
/* switch to new thread */
|
||||
rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
|
||||
|
||||
/* never come back */
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup Thread
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will perform one schedule. It will select one thread
|
||||
* with the highest priority level, then switch to it.
|
||||
*/
|
||||
void rt_schedule(void)
|
||||
{
|
||||
rt_base_t level;
|
||||
struct rt_thread *to_thread;
|
||||
struct rt_thread *from_thread;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/* check the scheduler is enabled or not */
|
||||
if (rt_scheduler_lock_nest == 0)
|
||||
{
|
||||
register rt_ubase_t highest_ready_priority;
|
||||
|
||||
#if RT_THREAD_PRIORITY_MAX <= 32
|
||||
highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
|
||||
#else
|
||||
register rt_ubase_t number;
|
||||
|
||||
number = __rt_ffs(rt_thread_ready_priority_group) - 1;
|
||||
highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
|
||||
#endif
|
||||
|
||||
/* get switch to thread */
|
||||
to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
|
||||
struct rt_thread,
|
||||
tlist);
|
||||
|
||||
/* if the destination thread is not the same as current thread */
|
||||
if (to_thread != rt_current_thread)
|
||||
{
|
||||
rt_current_priority = (rt_uint8_t)highest_ready_priority;
|
||||
from_thread = rt_current_thread;
|
||||
rt_current_thread = to_thread;
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
|
||||
|
||||
/* switch to new thread */
|
||||
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
|
||||
("[%d]switch to priority#%d "
|
||||
"thread:%.*s(sp:0x%p), "
|
||||
"from thread:%.*s(sp: 0x%p)\n",
|
||||
rt_interrupt_nest, highest_ready_priority,
|
||||
RT_NAME_MAX, to_thread->name, to_thread->sp,
|
||||
RT_NAME_MAX, from_thread->name, from_thread->sp));
|
||||
|
||||
#ifdef RT_USING_OVERFLOW_CHECK
|
||||
_rt_scheduler_stack_check(to_thread);
|
||||
#endif
|
||||
|
||||
if (rt_interrupt_nest == 0)
|
||||
{
|
||||
rt_hw_context_switch((rt_uint32_t)&from_thread->sp,
|
||||
(rt_uint32_t)&to_thread->sp);
|
||||
}
|
||||
else
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
|
||||
|
||||
rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,
|
||||
(rt_uint32_t)&to_thread->sp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function will insert a thread to system ready queue. The state of
|
||||
* thread will be set as READY and remove from suspend queue.
|
||||
*
|
||||
* @param thread the thread to be inserted
|
||||
* @note Please do not invoke this function in user application.
|
||||
*/
|
||||
void rt_schedule_insert_thread(struct rt_thread *thread)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* change stat */
|
||||
thread->stat = RT_THREAD_READY;
|
||||
|
||||
/* insert thread to ready list */
|
||||
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
|
||||
&(thread->tlist));
|
||||
|
||||
/* set priority mask */
|
||||
#if RT_THREAD_PRIORITY_MAX <= 32
|
||||
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
|
||||
RT_NAME_MAX, thread->name, thread->current_priority));
|
||||
#else
|
||||
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
|
||||
("insert thread[%.*s], the priority: %d 0x%x %d\n",
|
||||
RT_NAME_MAX,
|
||||
thread->name,
|
||||
thread->number,
|
||||
thread->number_mask,
|
||||
thread->high_mask));
|
||||
#endif
|
||||
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
rt_thread_ready_table[thread->number] |= thread->high_mask;
|
||||
#endif
|
||||
rt_thread_ready_priority_group |= thread->number_mask;
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function will remove a thread from system ready queue.
|
||||
*
|
||||
* @param thread the thread to be removed
|
||||
*
|
||||
* @note Please do not invoke this function in user application.
|
||||
*/
|
||||
void rt_schedule_remove_thread(struct rt_thread *thread)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
#if RT_THREAD_PRIORITY_MAX <= 32
|
||||
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
|
||||
RT_NAME_MAX, thread->name,
|
||||
thread->current_priority));
|
||||
#else
|
||||
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
|
||||
("remove thread[%.*s], the priority: %d 0x%x %d\n",
|
||||
RT_NAME_MAX,
|
||||
thread->name,
|
||||
thread->number,
|
||||
thread->number_mask,
|
||||
thread->high_mask));
|
||||
#endif
|
||||
|
||||
/* remove thread from ready list */
|
||||
rt_list_remove(&(thread->tlist));
|
||||
if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
|
||||
{
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
rt_thread_ready_table[thread->number] &= ~thread->high_mask;
|
||||
if (rt_thread_ready_table[thread->number] == 0)
|
||||
{
|
||||
rt_thread_ready_priority_group &= ~thread->number_mask;
|
||||
}
|
||||
#else
|
||||
rt_thread_ready_priority_group &= ~thread->number_mask;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will lock the thread scheduler.
|
||||
*/
|
||||
void rt_enter_critical(void)
|
||||
{
|
||||
register rt_base_t level;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/*
|
||||
* the maximal number of nest is RT_UINT16_MAX, which is big
|
||||
* enough and does not check here
|
||||
*/
|
||||
rt_scheduler_lock_nest ++;
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will unlock the thread scheduler.
|
||||
*/
|
||||
void rt_exit_critical(void)
|
||||
{
|
||||
register rt_base_t level;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
rt_scheduler_lock_nest --;
|
||||
|
||||
if (rt_scheduler_lock_nest <= 0)
|
||||
{
|
||||
rt_scheduler_lock_nest = 0;
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_schedule();
|
||||
}
|
||||
else
|
||||
{
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the scheduler lock level
|
||||
*
|
||||
* @return the level of the scheduler lock. 0 means unlocked.
|
||||
*/
|
||||
rt_uint16_t rt_critical_level(void)
|
||||
{
|
||||
return rt_scheduler_lock_nest;
|
||||
}
|
||||
/*@}*/
|
||||
|
||||
@@ -0,0 +1,968 @@
|
||||
/*
|
||||
* File : slab.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2008 - 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
|
||||
* 2008-07-12 Bernard the first version
|
||||
* 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
|
||||
* 2010-10-23 yi.qiu add module memory allocator
|
||||
* 2010-12-18 yi.qiu fix zone release bug
|
||||
*/
|
||||
|
||||
/*
|
||||
* KERN_SLABALLOC.C - Kernel SLAB memory allocator
|
||||
*
|
||||
* Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The DragonFly Project
|
||||
* by Matthew Dillon <dillon@backplane.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name of The DragonFly Project nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific, prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#define RT_MEM_STATS
|
||||
|
||||
#if defined (RT_USING_HEAP) && defined (RT_USING_SLAB)
|
||||
/* some statistical variable */
|
||||
#ifdef RT_MEM_STATS
|
||||
static rt_size_t used_mem, max_mem;
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
|
||||
static void (*rt_free_hook)(void *ptr);
|
||||
|
||||
/**
|
||||
* @addtogroup Hook
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when a memory
|
||||
* block is allocated from heap memory.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
|
||||
{
|
||||
rt_malloc_hook = hook;
|
||||
}
|
||||
RTM_EXPORT(rt_malloc_sethook);
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when a memory
|
||||
* block is released to heap memory.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_free_sethook(void (*hook)(void *ptr))
|
||||
{
|
||||
rt_free_hook = hook;
|
||||
}
|
||||
RTM_EXPORT(rt_free_sethook);
|
||||
|
||||
/*@}*/
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* slab allocator implementation
|
||||
*
|
||||
* A slab allocator reserves a ZONE for each chunk size, then lays the
|
||||
* chunks out in an array within the zone. Allocation and deallocation
|
||||
* is nearly instantanious, and fragmentation/overhead losses are limited
|
||||
* to a fixed worst-case amount.
|
||||
*
|
||||
* The downside of this slab implementation is in the chunk size
|
||||
* multiplied by the number of zones. ~80 zones * 128K = 10MB of VM per cpu.
|
||||
* In a kernel implementation all this memory will be physical so
|
||||
* the zone size is adjusted downward on machines with less physical
|
||||
* memory. The upside is that overhead is bounded... this is the *worst*
|
||||
* case overhead.
|
||||
*
|
||||
* Slab management is done on a per-cpu basis and no locking or mutexes
|
||||
* are required, only a critical section. When one cpu frees memory
|
||||
* belonging to another cpu's slab manager an asynchronous IPI message
|
||||
* will be queued to execute the operation. In addition, both the
|
||||
* high level slab allocator and the low level zone allocator optimize
|
||||
* M_ZERO requests, and the slab allocator does not have to pre initialize
|
||||
* the linked list of chunks.
|
||||
*
|
||||
* XXX Balancing is needed between cpus. Balance will be handled through
|
||||
* asynchronous IPIs primarily by reassigning the z_Cpu ownership of chunks.
|
||||
*
|
||||
* XXX If we have to allocate a new zone and M_USE_RESERVE is set, use of
|
||||
* the new zone should be restricted to M_USE_RESERVE requests only.
|
||||
*
|
||||
* Alloc Size Chunking Number of zones
|
||||
* 0-127 8 16
|
||||
* 128-255 16 8
|
||||
* 256-511 32 8
|
||||
* 512-1023 64 8
|
||||
* 1024-2047 128 8
|
||||
* 2048-4095 256 8
|
||||
* 4096-8191 512 8
|
||||
* 8192-16383 1024 8
|
||||
* 16384-32767 2048 8
|
||||
* (if RT_MM_PAGE_SIZE is 4K the maximum zone allocation is 16383)
|
||||
*
|
||||
* Allocations >= zone_limit go directly to kmem.
|
||||
*
|
||||
* API REQUIREMENTS AND SIDE EFFECTS
|
||||
*
|
||||
* To operate as a drop-in replacement to the FreeBSD-4.x malloc() we
|
||||
* have remained compatible with the following API requirements:
|
||||
*
|
||||
* + small power-of-2 sized allocations are power-of-2 aligned (kern_tty)
|
||||
* + all power-of-2 sized allocations are power-of-2 aligned (twe)
|
||||
* + malloc(0) is allowed and returns non-RT_NULL (ahc driver)
|
||||
* + ability to allocate arbitrarily large chunks of memory
|
||||
*/
|
||||
|
||||
/*
|
||||
* Chunk structure for free elements
|
||||
*/
|
||||
typedef struct slab_chunk
|
||||
{
|
||||
struct slab_chunk *c_next;
|
||||
} slab_chunk;
|
||||
|
||||
/*
|
||||
* The IN-BAND zone header is placed at the beginning of each zone.
|
||||
*/
|
||||
typedef struct slab_zone
|
||||
{
|
||||
rt_int32_t z_magic; /* magic number for sanity check */
|
||||
rt_int32_t z_nfree; /* total free chunks / ualloc space in zone */
|
||||
rt_int32_t z_nmax; /* maximum free chunks */
|
||||
|
||||
struct slab_zone *z_next; /* zoneary[] link if z_nfree non-zero */
|
||||
rt_uint8_t *z_baseptr; /* pointer to start of chunk array */
|
||||
|
||||
rt_int32_t z_uindex; /* current initial allocation index */
|
||||
rt_int32_t z_chunksize; /* chunk size for validation */
|
||||
|
||||
rt_int32_t z_zoneindex; /* zone index */
|
||||
slab_chunk *z_freechunk; /* free chunk list */
|
||||
} slab_zone;
|
||||
|
||||
#define ZALLOC_SLAB_MAGIC 0x51ab51ab
|
||||
#define ZALLOC_ZONE_LIMIT (16 * 1024) /* max slab-managed alloc */
|
||||
#define ZALLOC_MIN_ZONE_SIZE (32 * 1024) /* minimum zone size */
|
||||
#define ZALLOC_MAX_ZONE_SIZE (128 * 1024) /* maximum zone size */
|
||||
#define NZONES 72 /* number of zones */
|
||||
#define ZONE_RELEASE_THRESH 2 /* threshold number of zones */
|
||||
|
||||
static slab_zone *zone_array[NZONES]; /* linked list of zones NFree > 0 */
|
||||
static slab_zone *zone_free; /* whole zones that have become free */
|
||||
|
||||
static int zone_free_cnt;
|
||||
static int zone_size;
|
||||
static int zone_limit;
|
||||
static int zone_page_cnt;
|
||||
|
||||
/*
|
||||
* Misc constants. Note that allocations that are exact multiples of
|
||||
* RT_MM_PAGE_SIZE, or exceed the zone limit, fall through to the kmem module.
|
||||
*/
|
||||
#define MIN_CHUNK_SIZE 8 /* in bytes */
|
||||
#define MIN_CHUNK_MASK (MIN_CHUNK_SIZE - 1)
|
||||
|
||||
/*
|
||||
* Array of descriptors that describe the contents of each page
|
||||
*/
|
||||
#define PAGE_TYPE_FREE 0x00
|
||||
#define PAGE_TYPE_SMALL 0x01
|
||||
#define PAGE_TYPE_LARGE 0x02
|
||||
struct memusage
|
||||
{
|
||||
rt_uint32_t type:2 ; /* page type */
|
||||
rt_uint32_t size:30; /* pages allocated or offset from zone */
|
||||
};
|
||||
static struct memusage *memusage = RT_NULL;
|
||||
#define btokup(addr) \
|
||||
(&memusage[((rt_uint32_t)(addr) - heap_start) >> RT_MM_PAGE_BITS])
|
||||
|
||||
static rt_uint32_t heap_start, heap_end;
|
||||
|
||||
/* page allocator */
|
||||
struct rt_page_head
|
||||
{
|
||||
struct rt_page_head *next; /* next valid page */
|
||||
rt_size_t page; /* number of page */
|
||||
|
||||
/* dummy */
|
||||
char dummy[RT_MM_PAGE_SIZE - (sizeof(struct rt_page_head*) + sizeof (rt_size_t))];
|
||||
};
|
||||
static struct rt_page_head *rt_page_list;
|
||||
static struct rt_semaphore heap_sem;
|
||||
|
||||
void *rt_page_alloc(rt_size_t npages)
|
||||
{
|
||||
struct rt_page_head *b, *n;
|
||||
struct rt_page_head **prev;
|
||||
|
||||
if(npages == 0)
|
||||
return RT_NULL;
|
||||
|
||||
/* lock heap */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
for (prev = &rt_page_list; (b = *prev) != RT_NULL; prev = &(b->next))
|
||||
{
|
||||
if (b->page > npages)
|
||||
{
|
||||
/* splite pages */
|
||||
n = b + npages;
|
||||
n->next = b->next;
|
||||
n->page = b->page - npages;
|
||||
*prev = n;
|
||||
break;
|
||||
}
|
||||
|
||||
if (b->page == npages)
|
||||
{
|
||||
/* this node fit, remove this node */
|
||||
*prev = b->next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* unlock heap */
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
void rt_page_free(void *addr, rt_size_t npages)
|
||||
{
|
||||
struct rt_page_head *b, *n;
|
||||
struct rt_page_head **prev;
|
||||
|
||||
RT_ASSERT(addr != RT_NULL);
|
||||
RT_ASSERT((rt_uint32_t)addr % RT_MM_PAGE_SIZE == 0);
|
||||
RT_ASSERT(npages != 0);
|
||||
|
||||
n = (struct rt_page_head *)addr;
|
||||
|
||||
/* lock heap */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
for (prev = &rt_page_list; (b = *prev) != RT_NULL; prev = &(b->next))
|
||||
{
|
||||
RT_ASSERT(b->page > 0);
|
||||
RT_ASSERT(b > n || b + b->page <= n);
|
||||
|
||||
if (b + b->page == n)
|
||||
{
|
||||
if (b + (b->page += npages) == b->next)
|
||||
{
|
||||
b->page += b->next->page;
|
||||
b->next = b->next->next;
|
||||
}
|
||||
|
||||
goto _return;
|
||||
}
|
||||
|
||||
if (b == n + npages)
|
||||
{
|
||||
n->page = b->page + npages;
|
||||
n->next = b->next;
|
||||
*prev = n;
|
||||
|
||||
goto _return;
|
||||
}
|
||||
|
||||
if (b > n + npages)
|
||||
break;
|
||||
}
|
||||
|
||||
n->page = npages;
|
||||
n->next = b;
|
||||
*prev = n;
|
||||
|
||||
_return:
|
||||
/* unlock heap */
|
||||
rt_sem_release(&heap_sem);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the page allocator
|
||||
*/
|
||||
static void rt_page_init(void *addr, rt_size_t npages)
|
||||
{
|
||||
RT_ASSERT(addr != RT_NULL);
|
||||
RT_ASSERT(npages != 0);
|
||||
|
||||
rt_page_list = RT_NULL;
|
||||
rt_page_free(addr, npages);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup SystemInit
|
||||
*
|
||||
* This function will init system heap
|
||||
*
|
||||
* @param begin_addr the beginning address of system page
|
||||
* @param end_addr the end address of system page
|
||||
*/
|
||||
void rt_system_heap_init(void *begin_addr, void *end_addr)
|
||||
{
|
||||
rt_uint32_t limsize, npages;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* align begin and end addr to page */
|
||||
heap_start = RT_ALIGN((rt_uint32_t)begin_addr, RT_MM_PAGE_SIZE);
|
||||
heap_end = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_MM_PAGE_SIZE);
|
||||
|
||||
if (heap_start >= heap_end)
|
||||
{
|
||||
rt_kprintf("rt_system_heap_init, wrong address[0x%x - 0x%x]\n",
|
||||
(rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
limsize = heap_end - heap_start;
|
||||
npages = limsize / RT_MM_PAGE_SIZE;
|
||||
|
||||
/* initialize heap semaphore */
|
||||
rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB, ("heap[0x%x - 0x%x], size 0x%x, 0x%x pages\n",
|
||||
heap_start, heap_end, limsize, npages));
|
||||
|
||||
/* init pages */
|
||||
rt_page_init((void *)heap_start, npages);
|
||||
|
||||
/* calculate zone size */
|
||||
zone_size = ZALLOC_MIN_ZONE_SIZE;
|
||||
while (zone_size < ZALLOC_MAX_ZONE_SIZE && (zone_size << 1) < (limsize/1024))
|
||||
zone_size <<= 1;
|
||||
|
||||
zone_limit = zone_size / 4;
|
||||
if (zone_limit > ZALLOC_ZONE_LIMIT)
|
||||
zone_limit = ZALLOC_ZONE_LIMIT;
|
||||
|
||||
zone_page_cnt = zone_size / RT_MM_PAGE_SIZE;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB, ("zone size 0x%x, zone page count 0x%x\n",
|
||||
zone_size, zone_page_cnt));
|
||||
|
||||
/* allocate memusage array */
|
||||
limsize = npages * sizeof(struct memusage);
|
||||
limsize = RT_ALIGN(limsize, RT_MM_PAGE_SIZE);
|
||||
memusage = rt_page_alloc(limsize/RT_MM_PAGE_SIZE);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB, ("memusage 0x%x, size 0x%x\n",
|
||||
(rt_uint32_t)memusage, limsize));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate the zone index for the allocation request size and set the
|
||||
* allocation request size to that particular zone's chunk size.
|
||||
*/
|
||||
rt_inline int zoneindex(rt_uint32_t *bytes)
|
||||
{
|
||||
/* unsigned for shift opt */
|
||||
rt_uint32_t n = (rt_uint32_t)*bytes;
|
||||
|
||||
if (n < 128)
|
||||
{
|
||||
*bytes = n = (n + 7) & ~7;
|
||||
|
||||
/* 8 byte chunks, 16 zones */
|
||||
return(n / 8 - 1);
|
||||
}
|
||||
if (n < 256)
|
||||
{
|
||||
*bytes = n = (n + 15) & ~15;
|
||||
|
||||
return(n / 16 + 7);
|
||||
}
|
||||
if (n < 8192)
|
||||
{
|
||||
if (n < 512)
|
||||
{
|
||||
*bytes = n = (n + 31) & ~31;
|
||||
|
||||
return(n / 32 + 15);
|
||||
}
|
||||
if (n < 1024)
|
||||
{
|
||||
*bytes = n = (n + 63) & ~63;
|
||||
|
||||
return(n / 64 + 23);
|
||||
}
|
||||
if (n < 2048)
|
||||
{
|
||||
*bytes = n = (n + 127) & ~127;
|
||||
|
||||
return(n / 128 + 31);
|
||||
}
|
||||
if (n < 4096)
|
||||
{
|
||||
*bytes = n = (n + 255) & ~255;
|
||||
|
||||
return(n / 256 + 39);
|
||||
}
|
||||
*bytes = n = (n + 511) & ~511;
|
||||
|
||||
return(n / 512 + 47);
|
||||
}
|
||||
if (n < 16384)
|
||||
{
|
||||
*bytes = n = (n + 1023) & ~1023;
|
||||
|
||||
return(n / 1024 + 55);
|
||||
}
|
||||
|
||||
rt_kprintf("Unexpected byte count %d", n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup MM
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will allocate a block from system heap memory.
|
||||
* - If the nbytes is less than zero,
|
||||
* or
|
||||
* - If there is no nbytes sized memory valid in system,
|
||||
* the RT_NULL is returned.
|
||||
*
|
||||
* @param size the size of memory to be allocated
|
||||
*
|
||||
* @return the allocated memory
|
||||
*/
|
||||
void *rt_malloc(rt_size_t size)
|
||||
{
|
||||
slab_zone *z;
|
||||
rt_int32_t zi;
|
||||
slab_chunk *chunk;
|
||||
struct memusage *kup;
|
||||
|
||||
/* zero size, return RT_NULL */
|
||||
if (size == 0)
|
||||
return RT_NULL;
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
if (rt_module_self() != RT_NULL)
|
||||
return rt_module_malloc(size);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Handle large allocations directly. There should not be very many of
|
||||
* these so performance is not a big issue.
|
||||
*/
|
||||
if (size >= zone_limit)
|
||||
{
|
||||
size = RT_ALIGN(size, RT_MM_PAGE_SIZE);
|
||||
|
||||
chunk = rt_page_alloc(size >> RT_MM_PAGE_BITS);
|
||||
if (chunk == RT_NULL)
|
||||
return RT_NULL;
|
||||
|
||||
/* set kup */
|
||||
kup = btokup(chunk);
|
||||
kup->type = PAGE_TYPE_LARGE;
|
||||
kup->size = size >> RT_MM_PAGE_BITS;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB,
|
||||
("malloc a large memory 0x%x, page cnt %d, kup %d\n",
|
||||
size,
|
||||
size >> RT_MM_PAGE_BITS,
|
||||
((rt_uint32_t)chunk - heap_start) >> RT_MM_PAGE_BITS));
|
||||
|
||||
/* lock heap */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem += size;
|
||||
if (used_mem > max_mem)
|
||||
max_mem = used_mem;
|
||||
#endif
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* lock heap */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
/*
|
||||
* Attempt to allocate out of an existing zone. First try the free list,
|
||||
* then allocate out of unallocated space. If we find a good zone move
|
||||
* it to the head of the list so later allocations find it quickly
|
||||
* (we might have thousands of zones in the list).
|
||||
*
|
||||
* Note: zoneindex() will panic of size is too large.
|
||||
*/
|
||||
zi = zoneindex(&size);
|
||||
RT_ASSERT(zi < NZONES);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB, ("try to malloc 0x%x on zone: %d\n", size, zi));
|
||||
|
||||
if ((z = zone_array[zi]) != RT_NULL)
|
||||
{
|
||||
RT_ASSERT(z->z_nfree > 0);
|
||||
|
||||
/* Remove us from the zone_array[] when we become empty */
|
||||
if (--z->z_nfree == 0)
|
||||
{
|
||||
zone_array[zi] = z->z_next;
|
||||
z->z_next = RT_NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* No chunks are available but nfree said we had some memory, so
|
||||
* it must be available in the never-before-used-memory area
|
||||
* governed by uindex. The consequences are very serious if our zone
|
||||
* got corrupted so we use an explicit rt_kprintf rather then a KASSERT.
|
||||
*/
|
||||
if (z->z_uindex + 1 != z->z_nmax)
|
||||
{
|
||||
z->z_uindex = z->z_uindex + 1;
|
||||
chunk = (slab_chunk *)(z->z_baseptr + z->z_uindex * size);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* find on free chunk list */
|
||||
chunk = z->z_freechunk;
|
||||
|
||||
/* remove this chunk from list */
|
||||
z->z_freechunk = z->z_freechunk->c_next;
|
||||
}
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem += z->z_chunksize;
|
||||
if (used_mem > max_mem)
|
||||
max_mem = used_mem;
|
||||
#endif
|
||||
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*
|
||||
* If all zones are exhausted we need to allocate a new zone for this
|
||||
* index.
|
||||
*
|
||||
* At least one subsystem, the tty code (see CROUND) expects power-of-2
|
||||
* allocations to be power-of-2 aligned. We maintain compatibility by
|
||||
* adjusting the base offset below.
|
||||
*/
|
||||
{
|
||||
rt_int32_t off;
|
||||
|
||||
if ((z = zone_free) != RT_NULL)
|
||||
{
|
||||
/* remove zone from free zone list */
|
||||
zone_free = z->z_next;
|
||||
-- zone_free_cnt;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* unlock heap, since page allocator will think about lock */
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
/* allocate a zone from page */
|
||||
z = rt_page_alloc(zone_size / RT_MM_PAGE_SIZE);
|
||||
if (z == RT_NULL)
|
||||
goto fail;
|
||||
|
||||
/* lock heap */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB, ("alloc a new zone: 0x%x\n",
|
||||
(rt_uint32_t)z));
|
||||
|
||||
/* set message usage */
|
||||
for (off = 0, kup = btokup(z); off < zone_page_cnt; off ++)
|
||||
{
|
||||
kup->type = PAGE_TYPE_SMALL;
|
||||
kup->size = off;
|
||||
|
||||
kup ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* clear to zero */
|
||||
rt_memset(z, 0, sizeof(slab_zone));
|
||||
|
||||
/* offset of slab zone struct in zone */
|
||||
off = sizeof(slab_zone);
|
||||
|
||||
/*
|
||||
* Guarentee power-of-2 alignment for power-of-2-sized chunks.
|
||||
* Otherwise just 8-byte align the data.
|
||||
*/
|
||||
if ((size | (size - 1)) + 1 == (size << 1))
|
||||
off = (off + size - 1) & ~(size - 1);
|
||||
else
|
||||
off = (off + MIN_CHUNK_MASK) & ~MIN_CHUNK_MASK;
|
||||
|
||||
z->z_magic = ZALLOC_SLAB_MAGIC;
|
||||
z->z_zoneindex = zi;
|
||||
z->z_nmax = (zone_size - off) / size;
|
||||
z->z_nfree = z->z_nmax - 1;
|
||||
z->z_baseptr = (rt_uint8_t *)z + off;
|
||||
z->z_uindex = 0;
|
||||
z->z_chunksize = size;
|
||||
|
||||
chunk = (slab_chunk *)(z->z_baseptr + z->z_uindex * size);
|
||||
|
||||
/* link to zone array */
|
||||
z->z_next = zone_array[zi];
|
||||
zone_array[zi] = z;
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem += z->z_chunksize;
|
||||
if (used_mem > max_mem)
|
||||
max_mem = used_mem;
|
||||
#endif
|
||||
}
|
||||
|
||||
done:
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_malloc_hook, ((char *)chunk, size));
|
||||
|
||||
return chunk;
|
||||
|
||||
fail:
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(rt_malloc);
|
||||
|
||||
/**
|
||||
* This function will change the size of previously allocated memory block.
|
||||
*
|
||||
* @param ptr the previously allocated memory block
|
||||
* @param size the new size of memory block
|
||||
*
|
||||
* @return the allocated memory
|
||||
*/
|
||||
void *rt_realloc(void *ptr, rt_size_t size)
|
||||
{
|
||||
void *nptr;
|
||||
slab_zone *z;
|
||||
struct memusage *kup;
|
||||
|
||||
if (ptr == RT_NULL)
|
||||
return rt_malloc(size);
|
||||
if (size == 0)
|
||||
{
|
||||
rt_free(ptr);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
if (rt_module_self() != RT_NULL)
|
||||
return rt_module_realloc(ptr, size);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Get the original allocation's zone. If the new request winds up
|
||||
* using the same chunk size we do not have to do anything.
|
||||
*/
|
||||
kup = btokup((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK);
|
||||
if (kup->type == PAGE_TYPE_LARGE)
|
||||
{
|
||||
rt_size_t osize;
|
||||
|
||||
osize = kup->size << RT_MM_PAGE_BITS;
|
||||
if ((nptr = rt_malloc(size)) == RT_NULL)
|
||||
return RT_NULL;
|
||||
rt_memcpy(nptr, ptr, size > osize ? osize : size);
|
||||
rt_free(ptr);
|
||||
|
||||
return nptr;
|
||||
}
|
||||
else if (kup->type == PAGE_TYPE_SMALL)
|
||||
{
|
||||
z = (slab_zone *)(((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK) -
|
||||
kup->size * RT_MM_PAGE_SIZE);
|
||||
RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC);
|
||||
|
||||
zoneindex(&size);
|
||||
if (z->z_chunksize == size)
|
||||
return(ptr); /* same chunk */
|
||||
|
||||
/*
|
||||
* Allocate memory for the new request size. Note that zoneindex has
|
||||
* already adjusted the request size to the appropriate chunk size, which
|
||||
* should optimize our bcopy(). Then copy and return the new pointer.
|
||||
*/
|
||||
if ((nptr = rt_malloc(size)) == RT_NULL)
|
||||
return RT_NULL;
|
||||
|
||||
rt_memcpy(nptr, ptr, size > z->z_chunksize ? z->z_chunksize : size);
|
||||
rt_free(ptr);
|
||||
|
||||
return nptr;
|
||||
}
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(rt_realloc);
|
||||
|
||||
/**
|
||||
* This function will contiguously allocate enough space for count objects
|
||||
* that are size bytes of memory each and returns a pointer to the allocated
|
||||
* memory.
|
||||
*
|
||||
* The allocated memory is filled with bytes of value zero.
|
||||
*
|
||||
* @param count number of objects to allocate
|
||||
* @param size size of the objects to allocate
|
||||
*
|
||||
* @return pointer to allocated memory / NULL pointer if there is an error
|
||||
*/
|
||||
void *rt_calloc(rt_size_t count, rt_size_t size)
|
||||
{
|
||||
void *p;
|
||||
|
||||
/* allocate 'count' objects of size 'size' */
|
||||
p = rt_malloc(count * size);
|
||||
|
||||
/* zero the memory */
|
||||
if (p)
|
||||
rt_memset(p, 0, count * size);
|
||||
|
||||
return p;
|
||||
}
|
||||
RTM_EXPORT(rt_calloc);
|
||||
|
||||
/**
|
||||
* This function will release the previous allocated memory block by rt_malloc.
|
||||
* The released memory block is taken back to system heap.
|
||||
*
|
||||
* @param ptr the address of memory which will be released
|
||||
*/
|
||||
void rt_free(void *ptr)
|
||||
{
|
||||
slab_zone *z;
|
||||
slab_chunk *chunk;
|
||||
struct memusage *kup;
|
||||
|
||||
/* free a RT_NULL pointer */
|
||||
if (ptr == RT_NULL)
|
||||
return ;
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_free_hook, (ptr));
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
if(rt_module_self() != RT_NULL)
|
||||
{
|
||||
rt_module_free(rt_module_self(), ptr);
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* get memory usage */
|
||||
#if RT_DEBUG_SLAB
|
||||
{
|
||||
rt_uint32_t addr = ((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK);
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB,
|
||||
("free a memory 0x%x and align to 0x%x, kup index %d\n",
|
||||
(rt_uint32_t)ptr,
|
||||
(rt_uint32_t)addr,
|
||||
((rt_uint32_t)(addr) - heap_start) >> RT_MM_PAGE_BITS));
|
||||
}
|
||||
#endif
|
||||
|
||||
kup = btokup((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK);
|
||||
/* release large allocation */
|
||||
if (kup->type == PAGE_TYPE_LARGE)
|
||||
{
|
||||
rt_uint32_t size;
|
||||
|
||||
/* lock heap */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
/* clear page counter */
|
||||
size = kup->size;
|
||||
kup->size = 0;
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem -= size * RT_MM_PAGE_SIZE;
|
||||
#endif
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB,
|
||||
("free large memory block 0x%x, page count %d\n",
|
||||
(rt_uint32_t)ptr, size));
|
||||
|
||||
/* free this page */
|
||||
rt_page_free(ptr, size);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* lock heap */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
/* zone case. get out zone. */
|
||||
z = (slab_zone *)(((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK) -
|
||||
kup->size * RT_MM_PAGE_SIZE);
|
||||
RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC);
|
||||
|
||||
chunk = (slab_chunk *)ptr;
|
||||
chunk->c_next = z->z_freechunk;
|
||||
z->z_freechunk = chunk;
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem -= z->z_chunksize;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Bump the number of free chunks. If it becomes non-zero the zone
|
||||
* must be added back onto the appropriate list.
|
||||
*/
|
||||
if (z->z_nfree++ == 0)
|
||||
{
|
||||
z->z_next = zone_array[z->z_zoneindex];
|
||||
zone_array[z->z_zoneindex] = z;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the zone becomes totally free, and there are other zones we
|
||||
* can allocate from, move this zone to the FreeZones list. Since
|
||||
* this code can be called from an IPI callback, do *NOT* try to mess
|
||||
* with kernel_map here. Hysteresis will be performed at malloc() time.
|
||||
*/
|
||||
if (z->z_nfree == z->z_nmax &&
|
||||
(z->z_next || zone_array[z->z_zoneindex] != z))
|
||||
{
|
||||
slab_zone **pz;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_SLAB, ("free zone 0x%x\n",
|
||||
(rt_uint32_t)z, z->z_zoneindex));
|
||||
|
||||
/* remove zone from zone array list */
|
||||
for (pz = &zone_array[z->z_zoneindex]; z != *pz; pz = &(*pz)->z_next)
|
||||
;
|
||||
*pz = z->z_next;
|
||||
|
||||
/* reset zone */
|
||||
z->z_magic = -1;
|
||||
|
||||
/* insert to free zone list */
|
||||
z->z_next = zone_free;
|
||||
zone_free = z;
|
||||
|
||||
++ zone_free_cnt;
|
||||
|
||||
/* release zone to page allocator */
|
||||
if (zone_free_cnt > ZONE_RELEASE_THRESH)
|
||||
{
|
||||
register rt_base_t i;
|
||||
|
||||
z = zone_free;
|
||||
zone_free = z->z_next;
|
||||
-- zone_free_cnt;
|
||||
|
||||
/* set message usage */
|
||||
for (i = 0, kup = btokup(z); i < zone_page_cnt; i ++)
|
||||
{
|
||||
kup->type = PAGE_TYPE_FREE;
|
||||
kup->size = 0;
|
||||
kup ++;
|
||||
}
|
||||
|
||||
/* unlock heap */
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
/* release pages */
|
||||
rt_page_free(z, zone_size / RT_MM_PAGE_SIZE);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* unlock heap */
|
||||
rt_sem_release(&heap_sem);
|
||||
}
|
||||
RTM_EXPORT(rt_free);
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
void rt_memory_info(rt_uint32_t *total,
|
||||
rt_uint32_t *used,
|
||||
rt_uint32_t *max_used)
|
||||
{
|
||||
if (total != RT_NULL)
|
||||
*total = heap_end - heap_start;
|
||||
|
||||
if (used != RT_NULL)
|
||||
*used = used_mem;
|
||||
|
||||
if (max_used != RT_NULL)
|
||||
*max_used = max_mem;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
|
||||
void list_mem(void)
|
||||
{
|
||||
rt_kprintf("total memory: %d\n", heap_end - heap_start);
|
||||
rt_kprintf("used memory : %d\n", used_mem);
|
||||
rt_kprintf("maximum allocated memory: %d\n", max_mem);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*@}*/
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,707 @@
|
||||
/*
|
||||
* File : thread.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-28 Bernard first version
|
||||
* 2006-04-29 Bernard implement thread timer
|
||||
* 2006-04-30 Bernard added THREAD_DEBUG
|
||||
* 2006-05-27 Bernard fixed the rt_thread_yield bug
|
||||
* 2006-06-03 Bernard fixed the thread timer init bug
|
||||
* 2006-08-10 Bernard fixed the timer bug in thread_sleep
|
||||
* 2006-09-03 Bernard changed rt_timer_delete to rt_timer_detach
|
||||
* 2006-09-03 Bernard implement rt_thread_detach
|
||||
* 2008-02-16 Bernard fixed the rt_thread_timeout bug
|
||||
* 2010-03-21 Bernard change the errno of rt_thread_delay/sleep to
|
||||
* RT_EOK.
|
||||
* 2010-11-10 Bernard add cleanup callback function in thread exit.
|
||||
* 2011-09-01 Bernard fixed rt_thread_exit issue when the current
|
||||
* thread preempted, which reported by Jiaxing Lee.
|
||||
* 2011-09-08 Bernard fixed the scheduling issue in rt_thread_startup.
|
||||
* 2012-12-29 Bernard fixed compiling warning.
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
|
||||
extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
|
||||
extern struct rt_thread *rt_current_thread;
|
||||
extern rt_list_t rt_thread_defunct;
|
||||
|
||||
static void rt_thread_exit(void)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
register rt_base_t level;
|
||||
|
||||
/* get current thread */
|
||||
thread = rt_current_thread;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/* remove from schedule */
|
||||
rt_schedule_remove_thread(thread);
|
||||
/* change stat */
|
||||
thread->stat = RT_THREAD_CLOSE;
|
||||
|
||||
/* remove it from timer list */
|
||||
rt_timer_detach(&thread->thread_timer);
|
||||
|
||||
if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
|
||||
thread->cleanup == RT_NULL)
|
||||
{
|
||||
rt_object_detach((rt_object_t)thread);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* insert to defunct thread list */
|
||||
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* switch to next task */
|
||||
rt_schedule();
|
||||
}
|
||||
|
||||
static rt_err_t _rt_thread_init(struct rt_thread *thread,
|
||||
const char *name,
|
||||
void (*entry)(void *parameter),
|
||||
void *parameter,
|
||||
void *stack_start,
|
||||
rt_uint32_t stack_size,
|
||||
rt_uint8_t priority,
|
||||
rt_uint32_t tick)
|
||||
{
|
||||
/* init thread list */
|
||||
rt_list_init(&(thread->tlist));
|
||||
|
||||
thread->entry = (void *)entry;
|
||||
thread->parameter = parameter;
|
||||
|
||||
/* stack init */
|
||||
thread->stack_addr = stack_start;
|
||||
thread->stack_size = (rt_uint16_t)stack_size;
|
||||
|
||||
/* init thread stack */
|
||||
rt_memset(thread->stack_addr, '#', thread->stack_size);
|
||||
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
|
||||
(void *)((char *)thread->stack_addr + thread->stack_size - 4),
|
||||
(void *)rt_thread_exit);
|
||||
|
||||
/* priority init */
|
||||
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
|
||||
thread->init_priority = priority;
|
||||
thread->current_priority = priority;
|
||||
|
||||
/* tick init */
|
||||
thread->init_tick = tick;
|
||||
thread->remaining_tick = tick;
|
||||
|
||||
/* error and flags */
|
||||
thread->error = RT_EOK;
|
||||
thread->stat = RT_THREAD_INIT;
|
||||
|
||||
/* initialize cleanup function and user data */
|
||||
thread->cleanup = 0;
|
||||
thread->user_data = 0;
|
||||
|
||||
/* init thread timer */
|
||||
rt_timer_init(&(thread->thread_timer),
|
||||
thread->name,
|
||||
rt_thread_timeout,
|
||||
thread,
|
||||
0,
|
||||
RT_TIMER_FLAG_ONE_SHOT);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup Thread
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will initialize a thread, normally it's used to initialize a
|
||||
* static thread object.
|
||||
*
|
||||
* @param thread the static thread object
|
||||
* @param name the name of thread, which shall be unique
|
||||
* @param entry the entry function of thread
|
||||
* @param parameter the parameter of thread enter function
|
||||
* @param stack_start the start address of thread stack
|
||||
* @param stack_size the size of thread stack
|
||||
* @param priority the priority of thread
|
||||
* @param tick the time slice if there are same priority thread
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_thread_init(struct rt_thread *thread,
|
||||
const char *name,
|
||||
void (*entry)(void *parameter),
|
||||
void *parameter,
|
||||
void *stack_start,
|
||||
rt_uint32_t stack_size,
|
||||
rt_uint8_t priority,
|
||||
rt_uint32_t tick)
|
||||
{
|
||||
/* thread check */
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
RT_ASSERT(stack_start != RT_NULL);
|
||||
|
||||
/* init thread object */
|
||||
rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
|
||||
|
||||
return _rt_thread_init(thread,
|
||||
name,
|
||||
entry,
|
||||
parameter,
|
||||
stack_start,
|
||||
stack_size,
|
||||
priority,
|
||||
tick);
|
||||
}
|
||||
RTM_EXPORT(rt_thread_init);
|
||||
|
||||
/**
|
||||
* This function will return self thread object
|
||||
*
|
||||
* @return the self thread object
|
||||
*/
|
||||
rt_thread_t rt_thread_self(void)
|
||||
{
|
||||
return rt_current_thread;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_self);
|
||||
|
||||
/**
|
||||
* This function will start a thread and put it to system ready queue
|
||||
*
|
||||
* @param thread the thread to be started
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_thread_startup(rt_thread_t thread)
|
||||
{
|
||||
/* thread check */
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
RT_ASSERT(thread->stat == RT_THREAD_INIT);
|
||||
|
||||
/* set current priority to init priority */
|
||||
thread->current_priority = thread->init_priority;
|
||||
|
||||
/* calculate priority attribute */
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
thread->number = thread->current_priority >> 3; /* 5bit */
|
||||
thread->number_mask = 1L << thread->number;
|
||||
thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
|
||||
#else
|
||||
thread->number_mask = 1L << thread->current_priority;
|
||||
#endif
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
|
||||
thread->name, thread->init_priority));
|
||||
/* change thread stat */
|
||||
thread->stat = RT_THREAD_SUSPEND;
|
||||
/* then resume it */
|
||||
rt_thread_resume(thread);
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
{
|
||||
/* do a scheduling */
|
||||
rt_schedule();
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_startup);
|
||||
|
||||
/**
|
||||
* This function will detach a thread. The thread object will be removed from
|
||||
* thread queue and detached/deleted from system object management.
|
||||
*
|
||||
* @param thread the thread to be deleted
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_thread_detach(rt_thread_t thread)
|
||||
{
|
||||
rt_base_t lock;
|
||||
|
||||
/* thread check */
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
/* remove from schedule */
|
||||
rt_schedule_remove_thread(thread);
|
||||
|
||||
/* release thread timer */
|
||||
rt_timer_detach(&(thread->thread_timer));
|
||||
|
||||
/* change stat */
|
||||
thread->stat = RT_THREAD_CLOSE;
|
||||
|
||||
/* detach object */
|
||||
rt_object_detach((rt_object_t)thread);
|
||||
|
||||
if (thread->cleanup != RT_NULL)
|
||||
{
|
||||
/* disable interrupt */
|
||||
lock = rt_hw_interrupt_disable();
|
||||
|
||||
/* insert to defunct thread list */
|
||||
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(lock);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_detach);
|
||||
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/**
|
||||
* This function will create a thread object and allocate thread object memory
|
||||
* and stack.
|
||||
*
|
||||
* @param name the name of thread, which shall be unique
|
||||
* @param entry the entry function of thread
|
||||
* @param parameter the parameter of thread enter function
|
||||
* @param stack_size the size of thread stack
|
||||
* @param priority the priority of thread
|
||||
* @param tick the time slice if there are same priority thread
|
||||
*
|
||||
* @return the created thread object
|
||||
*/
|
||||
rt_thread_t rt_thread_create(const char *name,
|
||||
void (*entry)(void *parameter),
|
||||
void *parameter,
|
||||
rt_uint32_t stack_size,
|
||||
rt_uint8_t priority,
|
||||
rt_uint32_t tick)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
void *stack_start;
|
||||
|
||||
thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
|
||||
name);
|
||||
if (thread == RT_NULL)
|
||||
return RT_NULL;
|
||||
|
||||
stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
|
||||
if (stack_start == RT_NULL)
|
||||
{
|
||||
/* allocate stack failure */
|
||||
rt_object_delete((rt_object_t)thread);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
_rt_thread_init(thread,
|
||||
name,
|
||||
entry,
|
||||
parameter,
|
||||
stack_start,
|
||||
stack_size,
|
||||
priority,
|
||||
tick);
|
||||
|
||||
return thread;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_create);
|
||||
|
||||
/**
|
||||
* This function will delete a thread. The thread object will be removed from
|
||||
* thread queue and detached/deleted from system object management.
|
||||
*
|
||||
* @param thread the thread to be deleted
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_thread_delete(rt_thread_t thread)
|
||||
{
|
||||
rt_base_t lock;
|
||||
|
||||
/* thread check */
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
/* remove from schedule */
|
||||
rt_schedule_remove_thread(thread);
|
||||
|
||||
/* release thread timer */
|
||||
rt_timer_detach(&(thread->thread_timer));
|
||||
|
||||
/* change stat */
|
||||
thread->stat = RT_THREAD_CLOSE;
|
||||
|
||||
/* disable interrupt */
|
||||
lock = rt_hw_interrupt_disable();
|
||||
|
||||
/* insert to defunct thread list */
|
||||
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(lock);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_delete);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will let current thread yield processor, and scheduler will
|
||||
* choose a highest thread to run. After yield processor, the current thread
|
||||
* is still in READY state.
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t rt_thread_yield(void)
|
||||
{
|
||||
register rt_base_t level;
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/* set to current thread */
|
||||
thread = rt_current_thread;
|
||||
|
||||
/* if the thread stat is READY and on ready queue list */
|
||||
if (thread->stat == RT_THREAD_READY &&
|
||||
thread->tlist.next != thread->tlist.prev)
|
||||
{
|
||||
/* remove thread from thread list */
|
||||
rt_list_remove(&(thread->tlist));
|
||||
|
||||
/* put thread to end of ready queue */
|
||||
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
|
||||
&(thread->tlist));
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_schedule();
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_yield);
|
||||
|
||||
/**
|
||||
* This function will let current thread sleep for some ticks.
|
||||
*
|
||||
* @param tick the sleep ticks
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t rt_thread_sleep(rt_tick_t tick)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
/* set to current thread */
|
||||
thread = rt_current_thread;
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
/* suspend thread */
|
||||
rt_thread_suspend(thread);
|
||||
|
||||
/* reset the timeout of thread timer and start it */
|
||||
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
|
||||
rt_timer_start(&(thread->thread_timer));
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
|
||||
rt_schedule();
|
||||
|
||||
/* clear error number of this thread to RT_EOK */
|
||||
if (thread->error == -RT_ETIMEOUT)
|
||||
thread->error = RT_EOK;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will let current thread delay for some ticks.
|
||||
*
|
||||
* @param tick the delay ticks
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t rt_thread_delay(rt_tick_t tick)
|
||||
{
|
||||
return rt_thread_sleep(tick);
|
||||
}
|
||||
RTM_EXPORT(rt_thread_delay);
|
||||
|
||||
/**
|
||||
* This function will control thread behaviors according to control command.
|
||||
*
|
||||
* @param thread the specified thread to be controlled
|
||||
* @param cmd the control command, which includes
|
||||
* RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
|
||||
* RT_THREAD_CTRL_STARTUP for starting a thread;
|
||||
* RT_THREAD_CTRL_CLOSE for delete a thread.
|
||||
* @param arg the argument of control command
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t rt_thread_control(rt_thread_t thread, rt_uint8_t cmd, void *arg)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
|
||||
/* thread check */
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_THREAD_CTRL_CHANGE_PRIORITY:
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* for ready thread, change queue */
|
||||
if (thread->stat == RT_THREAD_READY)
|
||||
{
|
||||
/* remove thread from schedule queue first */
|
||||
rt_schedule_remove_thread(thread);
|
||||
|
||||
/* change thread priority */
|
||||
thread->current_priority = *(rt_uint8_t *)arg;
|
||||
|
||||
/* recalculate priority attribute */
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
thread->number = thread->current_priority >> 3; /* 5bit */
|
||||
thread->number_mask = 1 << thread->number;
|
||||
thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
|
||||
#else
|
||||
thread->number_mask = 1 << thread->current_priority;
|
||||
#endif
|
||||
|
||||
/* insert thread to schedule queue again */
|
||||
rt_schedule_insert_thread(thread);
|
||||
}
|
||||
else
|
||||
{
|
||||
thread->current_priority = *(rt_uint8_t *)arg;
|
||||
|
||||
/* recalculate priority attribute */
|
||||
#if RT_THREAD_PRIORITY_MAX > 32
|
||||
thread->number = thread->current_priority >> 3; /* 5bit */
|
||||
thread->number_mask = 1 << thread->number;
|
||||
thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
|
||||
#else
|
||||
thread->number_mask = 1 << thread->current_priority;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
break;
|
||||
|
||||
case RT_THREAD_CTRL_STARTUP:
|
||||
return rt_thread_startup(thread);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
case RT_THREAD_CTRL_CLOSE:
|
||||
return rt_thread_delete(thread);
|
||||
#endif
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_control);
|
||||
|
||||
/**
|
||||
* This function will suspend the specified thread.
|
||||
*
|
||||
* @param thread the thread to be suspended
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
||||
*
|
||||
* @note if suspend self thread, after this function call, the
|
||||
* rt_schedule() must be invoked.
|
||||
*/
|
||||
rt_err_t rt_thread_suspend(rt_thread_t thread)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
|
||||
/* thread check */
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
|
||||
|
||||
if (thread->stat != RT_THREAD_READY)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, %d\n",
|
||||
thread->stat));
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* change thread stat */
|
||||
thread->stat = RT_THREAD_SUSPEND;
|
||||
rt_schedule_remove_thread(thread);
|
||||
|
||||
/* stop thread timer anyway */
|
||||
rt_timer_stop(&(thread->thread_timer));
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_suspend);
|
||||
|
||||
/**
|
||||
* This function will resume a thread and put it to system ready queue.
|
||||
*
|
||||
* @param thread the thread to be resumed
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_thread_resume(rt_thread_t thread)
|
||||
{
|
||||
register rt_base_t temp;
|
||||
|
||||
/* thread check */
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
|
||||
|
||||
if (thread->stat != RT_THREAD_SUSPEND)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
|
||||
thread->stat));
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* remove from suspend list */
|
||||
rt_list_remove(&(thread->tlist));
|
||||
|
||||
rt_timer_stop(&thread->thread_timer);
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
|
||||
/* insert to schedule ready list */
|
||||
rt_schedule_insert_thread(thread);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_resume);
|
||||
|
||||
/**
|
||||
* This function is the timeout function for thread, normally which is invoked
|
||||
* when thread is timeout to wait some resource.
|
||||
*
|
||||
* @param parameter the parameter of thread timeout function
|
||||
*/
|
||||
void rt_thread_timeout(void *parameter)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
|
||||
thread = (struct rt_thread *)parameter;
|
||||
|
||||
/* thread check */
|
||||
RT_ASSERT(thread != RT_NULL);
|
||||
RT_ASSERT(thread->stat == RT_THREAD_SUSPEND);
|
||||
|
||||
/* set error number */
|
||||
thread->error = -RT_ETIMEOUT;
|
||||
|
||||
/* remove from suspend list */
|
||||
rt_list_remove(&(thread->tlist));
|
||||
|
||||
/* insert to schedule ready list */
|
||||
rt_schedule_insert_thread(thread);
|
||||
|
||||
/* do schedule */
|
||||
rt_schedule();
|
||||
}
|
||||
RTM_EXPORT(rt_thread_timeout);
|
||||
|
||||
/**
|
||||
* This function will find the specified thread.
|
||||
*
|
||||
* @param name the name of thread finding
|
||||
*
|
||||
* @return the found thread
|
||||
*
|
||||
* @note please don't invoke this function in interrupt status.
|
||||
*/
|
||||
rt_thread_t rt_thread_find(char *name)
|
||||
{
|
||||
struct rt_object_information *information;
|
||||
struct rt_object *object;
|
||||
struct rt_list_node *node;
|
||||
|
||||
extern struct rt_object_information rt_object_container[];
|
||||
|
||||
/* enter critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_enter_critical();
|
||||
|
||||
/* try to find device object */
|
||||
information = &rt_object_container[RT_Object_Class_Thread];
|
||||
for (node = information->object_list.next;
|
||||
node != &(information->object_list);
|
||||
node = node->next)
|
||||
{
|
||||
object = rt_list_entry(node, struct rt_object, list);
|
||||
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
|
||||
{
|
||||
/* leave critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_exit_critical();
|
||||
|
||||
return (rt_thread_t)object;
|
||||
}
|
||||
}
|
||||
|
||||
/* leave critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_exit_critical();
|
||||
|
||||
/* not found */
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_find);
|
||||
|
||||
/*@}*/
|
||||
@@ -0,0 +1,704 @@
|
||||
/*
|
||||
* File : timer.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-12 Bernard first version
|
||||
* 2006-04-29 Bernard implement thread timer
|
||||
* 2006-06-04 Bernard implement rt_timer_control
|
||||
* 2006-08-10 Bernard fix the periodic timer bug
|
||||
* 2006-09-03 Bernard implement rt_timer_detach
|
||||
* 2009-11-11 LiJin add soft timer
|
||||
* 2010-05-12 Bernard fix the timer check bug.
|
||||
* 2010-11-02 Charlie re-implement tick overflow issue
|
||||
* 2012-12-15 Bernard fix the next timeout issue in soft timer
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
|
||||
/* hard timer list */
|
||||
static rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
|
||||
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
#ifndef RT_TIMER_THREAD_STACK_SIZE
|
||||
#define RT_TIMER_THREAD_STACK_SIZE 512
|
||||
#endif
|
||||
|
||||
#ifndef RT_TIMER_THREAD_PRIO
|
||||
#define RT_TIMER_THREAD_PRIO 0
|
||||
#endif
|
||||
|
||||
/* soft timer list */
|
||||
static rt_list_t rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
|
||||
static struct rt_thread timer_thread;
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
extern void (*rt_object_take_hook)(struct rt_object *object);
|
||||
extern void (*rt_object_put_hook)(struct rt_object *object);
|
||||
static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
|
||||
|
||||
/**
|
||||
* @addtogroup Hook
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when timer
|
||||
* is timeout.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
|
||||
{
|
||||
rt_timer_timeout_hook = hook;
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
#endif
|
||||
|
||||
static void _rt_timer_init(rt_timer_t timer,
|
||||
void (*timeout)(void *parameter),
|
||||
void *parameter,
|
||||
rt_tick_t time,
|
||||
rt_uint8_t flag)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* set flag */
|
||||
timer->parent.flag = flag;
|
||||
|
||||
/* set deactivated */
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
|
||||
timer->timeout_func = timeout;
|
||||
timer->parameter = parameter;
|
||||
|
||||
timer->timeout_tick = 0;
|
||||
timer->init_tick = time;
|
||||
|
||||
/* initialize timer list */
|
||||
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
|
||||
{
|
||||
rt_list_init(&(timer->row[i]));
|
||||
}
|
||||
}
|
||||
|
||||
/* the fist timer always in the last row */
|
||||
static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])
|
||||
{
|
||||
struct rt_timer *timer;
|
||||
|
||||
if (rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
|
||||
return RT_TICK_MAX;
|
||||
|
||||
timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
|
||||
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
|
||||
|
||||
return timer->timeout_tick;
|
||||
}
|
||||
|
||||
rt_inline void _rt_timer_remove(rt_timer_t timer)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
|
||||
{
|
||||
rt_list_remove(&timer->row[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#if RT_DEBUG_TIMER
|
||||
static int rt_timer_count_height(struct rt_timer *timer)
|
||||
{
|
||||
int i, cnt = 0;
|
||||
|
||||
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
|
||||
{
|
||||
if (!rt_list_isempty(&timer->row[i]))
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
void rt_timer_dump(rt_list_t timer_heads[])
|
||||
{
|
||||
rt_list_t *list;
|
||||
|
||||
for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL-1].next;
|
||||
list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL-1];
|
||||
list = list->next)
|
||||
{
|
||||
struct rt_timer *timer = rt_list_entry(list,
|
||||
struct rt_timer,
|
||||
row[RT_TIMER_SKIP_LIST_LEVEL-1]);
|
||||
rt_kprintf("%d", rt_timer_count_height(timer));
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @addtogroup Clock
|
||||
*/
|
||||
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
* This function will initialize a timer, normally this function is used to
|
||||
* initialize a static timer object.
|
||||
*
|
||||
* @param timer the static timer object
|
||||
* @param name the name of timer
|
||||
* @param timeout the timeout function
|
||||
* @param parameter the parameter of timeout function
|
||||
* @param time the tick of timer
|
||||
* @param flag the flag of timer
|
||||
*/
|
||||
void rt_timer_init(rt_timer_t timer,
|
||||
const char *name,
|
||||
void (*timeout)(void *parameter),
|
||||
void *parameter,
|
||||
rt_tick_t time,
|
||||
rt_uint8_t flag)
|
||||
{
|
||||
/* timer check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
|
||||
/* timer object initialization */
|
||||
rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
|
||||
|
||||
_rt_timer_init(timer, timeout, parameter, time, flag);
|
||||
}
|
||||
RTM_EXPORT(rt_timer_init);
|
||||
|
||||
/**
|
||||
* This function will detach a timer from timer management.
|
||||
*
|
||||
* @param timer the static timer object
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK; RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_timer_detach(rt_timer_t timer)
|
||||
{
|
||||
register rt_base_t level;
|
||||
|
||||
/* timer check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
_rt_timer_remove(timer);
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_object_detach((rt_object_t)timer);
|
||||
|
||||
return -RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_timer_detach);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/**
|
||||
* This function will create a timer
|
||||
*
|
||||
* @param name the name of timer
|
||||
* @param timeout the timeout function
|
||||
* @param parameter the parameter of timeout function
|
||||
* @param time the tick of timer
|
||||
* @param flag the flag of timer
|
||||
*
|
||||
* @return the created timer object
|
||||
*/
|
||||
rt_timer_t rt_timer_create(const char *name,
|
||||
void (*timeout)(void *parameter),
|
||||
void *parameter,
|
||||
rt_tick_t time,
|
||||
rt_uint8_t flag)
|
||||
{
|
||||
struct rt_timer *timer;
|
||||
|
||||
/* allocate a object */
|
||||
timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
|
||||
if (timer == RT_NULL)
|
||||
{
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
_rt_timer_init(timer, timeout, parameter, time, flag);
|
||||
|
||||
return timer;
|
||||
}
|
||||
RTM_EXPORT(rt_timer_create);
|
||||
|
||||
/**
|
||||
* This function will delete a timer and release timer memory
|
||||
*
|
||||
* @param timer the timer to be deleted
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK; RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_timer_delete(rt_timer_t timer)
|
||||
{
|
||||
register rt_base_t level;
|
||||
|
||||
/* timer check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
_rt_timer_remove(timer);
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_object_delete((rt_object_t)timer);
|
||||
|
||||
return -RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_timer_delete);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will start the timer
|
||||
*
|
||||
* @param timer the timer to be started
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_timer_start(rt_timer_t timer)
|
||||
{
|
||||
int row_lvl;
|
||||
rt_list_t *timer_list;
|
||||
register rt_base_t level;
|
||||
rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
|
||||
unsigned int tst_nr;
|
||||
static unsigned int random_nr;
|
||||
|
||||
/* timer check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
|
||||
/* stop timer firstly */
|
||||
level = rt_hw_interrupt_disable();
|
||||
/* remove timer from list */
|
||||
_rt_timer_remove(timer);
|
||||
/* change status of timer */
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
|
||||
|
||||
/*
|
||||
* get timeout tick,
|
||||
* the max timeout tick shall not great than RT_TICK_MAX/2
|
||||
*/
|
||||
RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
|
||||
timer->timeout_tick = rt_tick_get() + timer->init_tick;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
|
||||
{
|
||||
/* insert timer to soft timer list */
|
||||
timer_list = rt_soft_timer_list;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* insert timer to system timer list */
|
||||
timer_list = rt_timer_list;
|
||||
}
|
||||
|
||||
row_head[0] = &timer_list[0];
|
||||
for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
|
||||
{
|
||||
for (;row_head[row_lvl] != timer_list[row_lvl].prev;
|
||||
row_head[row_lvl] = row_head[row_lvl]->next)
|
||||
{
|
||||
struct rt_timer *t;
|
||||
rt_list_t *p = row_head[row_lvl]->next;
|
||||
|
||||
/* fix up the entry pointer */
|
||||
t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
|
||||
|
||||
/* If we have two timers that timeout at the same time, it's
|
||||
* preferred that the timer inserted early get called early.
|
||||
* So insert the new timer to the end the the some-timeout timer
|
||||
* list.
|
||||
*/
|
||||
if ((t->timeout_tick - timer->timeout_tick) == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
|
||||
row_head[row_lvl+1] = row_head[row_lvl]+1;
|
||||
}
|
||||
|
||||
/* Interestingly, this super simple timer insert counter works very very
|
||||
* well on distributing the list height uniformly. By means of "very very
|
||||
* well", I mean it beats the randomness of timer->timeout_tick very easily
|
||||
* (actually, the timeout_tick is not random and easy to be attacked). */
|
||||
random_nr++;
|
||||
tst_nr = random_nr;
|
||||
|
||||
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL-1],
|
||||
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL-1]));
|
||||
for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
|
||||
{
|
||||
if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
|
||||
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
|
||||
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
|
||||
else
|
||||
break;
|
||||
/* Shift over the bits we have tested. Works well with 1 bit and 2
|
||||
* bits. */
|
||||
tst_nr >>= (RT_TIMER_SKIP_LIST_MASK+1)>>1;
|
||||
}
|
||||
|
||||
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
|
||||
{
|
||||
/* check whether timer thread is ready */
|
||||
if (timer_thread.stat != RT_THREAD_READY)
|
||||
{
|
||||
/* resume timer thread to check soft timer */
|
||||
rt_thread_resume(&timer_thread);
|
||||
rt_schedule();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return -RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_timer_start);
|
||||
|
||||
/**
|
||||
* This function will stop the timer
|
||||
*
|
||||
* @param timer the timer to be stopped
|
||||
*
|
||||
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
||||
*/
|
||||
rt_err_t rt_timer_stop(rt_timer_t timer)
|
||||
{
|
||||
register rt_base_t level;
|
||||
|
||||
/* timer check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
||||
return -RT_ERROR;
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
_rt_timer_remove(timer);
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
/* change stat */
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_timer_stop);
|
||||
|
||||
/**
|
||||
* This function will get or set some options of the timer
|
||||
*
|
||||
* @param timer the timer to be get or set
|
||||
* @param cmd the control command
|
||||
* @param arg the argument
|
||||
*
|
||||
* @return RT_EOK
|
||||
*/
|
||||
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg)
|
||||
{
|
||||
/* timer check */
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_TIMER_CTRL_GET_TIME:
|
||||
*(rt_tick_t *)arg = timer->init_tick;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_SET_TIME:
|
||||
timer->init_tick = *(rt_tick_t *)arg;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_SET_ONESHOT:
|
||||
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
|
||||
break;
|
||||
|
||||
case RT_TIMER_CTRL_SET_PERIODIC:
|
||||
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
|
||||
break;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_timer_control);
|
||||
|
||||
/**
|
||||
* This function will check timer list, if a timeout event happens, the
|
||||
* corresponding timeout function will be invoked.
|
||||
*
|
||||
* @note this function shall be invoked in operating system timer interrupt.
|
||||
*/
|
||||
void rt_timer_check(void)
|
||||
{
|
||||
struct rt_timer *t;
|
||||
rt_tick_t current_tick;
|
||||
register rt_base_t level;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
|
||||
|
||||
current_tick = rt_tick_get();
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL-1]))
|
||||
{
|
||||
t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
|
||||
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
|
||||
|
||||
/*
|
||||
* It supposes that the new tick shall less than the half duration of
|
||||
* tick max.
|
||||
*/
|
||||
if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
|
||||
{
|
||||
RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
|
||||
|
||||
/* remove timer from timer list firstly */
|
||||
_rt_timer_remove(t);
|
||||
|
||||
/* call timeout function */
|
||||
t->timeout_func(t->parameter);
|
||||
|
||||
/* re-get tick */
|
||||
current_tick = rt_tick_get();
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
|
||||
|
||||
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
||||
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
||||
{
|
||||
/* start it */
|
||||
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
rt_timer_start(t);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* stop timer */
|
||||
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
}
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will return the next timeout tick in the system.
|
||||
*
|
||||
* @return the next timeout tick in the system
|
||||
*/
|
||||
rt_tick_t rt_timer_next_timeout_tick(void)
|
||||
{
|
||||
return rt_timer_list_next_timeout(rt_timer_list);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
/**
|
||||
* This function will check timer list, if a timeout event happens, the
|
||||
* corresponding timeout function will be invoked.
|
||||
*/
|
||||
void rt_soft_timer_check(void)
|
||||
{
|
||||
rt_tick_t current_tick;
|
||||
rt_list_t *n;
|
||||
struct rt_timer *t;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
|
||||
|
||||
current_tick = rt_tick_get();
|
||||
|
||||
for (n = rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL-1].next;
|
||||
n != &(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL-1]);)
|
||||
{
|
||||
t = rt_list_entry(n, struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL-1]);
|
||||
|
||||
/*
|
||||
* It supposes that the new tick shall less than the half duration of
|
||||
* tick max.
|
||||
*/
|
||||
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
|
||||
{
|
||||
RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
|
||||
|
||||
/* move node to the next */
|
||||
n = n->next;
|
||||
|
||||
/* remove timer from timer list firstly */
|
||||
_rt_timer_remove(t);
|
||||
|
||||
/* call timeout function */
|
||||
t->timeout_func(t->parameter);
|
||||
|
||||
/* re-get tick */
|
||||
current_tick = rt_tick_get();
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
|
||||
|
||||
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
||||
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
||||
{
|
||||
/* start it */
|
||||
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
rt_timer_start(t);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* stop timer */
|
||||
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
}
|
||||
}
|
||||
else break; /* not check anymore */
|
||||
}
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
|
||||
}
|
||||
|
||||
/* system timer thread entry */
|
||||
static void rt_thread_timer_entry(void *parameter)
|
||||
{
|
||||
rt_tick_t next_timeout;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* get the next timeout tick */
|
||||
next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
|
||||
if (next_timeout == RT_TICK_MAX)
|
||||
{
|
||||
/* no software timer exist, suspend self. */
|
||||
rt_thread_suspend(rt_thread_self());
|
||||
rt_schedule();
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_tick_t current_tick;
|
||||
|
||||
/* get current tick */
|
||||
current_tick = rt_tick_get();
|
||||
|
||||
if ((next_timeout - current_tick) < RT_TICK_MAX/2)
|
||||
{
|
||||
/* get the delta timeout tick */
|
||||
next_timeout = next_timeout - current_tick;
|
||||
rt_thread_delay(next_timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/* lock scheduler */
|
||||
rt_enter_critical();
|
||||
/* check software timer */
|
||||
rt_soft_timer_check();
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @ingroup SystemInit
|
||||
*
|
||||
* This function will initialize system timer
|
||||
*/
|
||||
void rt_system_timer_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(rt_timer_list)/sizeof(rt_timer_list[0]); i++)
|
||||
{
|
||||
rt_list_init(rt_timer_list+i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup SystemInit
|
||||
*
|
||||
* This function will initialize system timer thread
|
||||
*/
|
||||
void rt_system_timer_thread_init(void)
|
||||
{
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
int i;
|
||||
|
||||
for (i = 0;
|
||||
i < sizeof(rt_soft_timer_list)/sizeof(rt_soft_timer_list[0]);
|
||||
i++)
|
||||
{
|
||||
rt_list_init(rt_soft_timer_list+i);
|
||||
}
|
||||
|
||||
/* start software timer thread */
|
||||
rt_thread_init(&timer_thread,
|
||||
"timer",
|
||||
rt_thread_timer_entry,
|
||||
RT_NULL,
|
||||
&timer_thread_stack[0],
|
||||
sizeof(timer_thread_stack),
|
||||
RT_TIMER_THREAD_PRIO,
|
||||
10);
|
||||
|
||||
/* startup */
|
||||
rt_thread_startup(&timer_thread);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
Reference in New Issue
Block a user