根据21IC的一个程序架构 在cortex M0上的小移植

[复制链接]
 楼主| sunshitao 发表于 2011-9-26 22:56 | 显示全部楼层 |阅读模式
本帖最后由 sunshitao 于 2011-9-26 22:57 编辑

去掉了原程序界面的显示部分、定时器只提供回调函数,
在实际中勉强凑合着用  但是有时候这种架构也不是很方便,感觉还是操作系统好用一点。。。
附上源码  供兄弟姐妹  大叔大婶们 拍砖

  1. #include"LPC11xx.h"                                                   /* LPC11xx 外设寄存器定义        */
  2. //#include"uart.h"
  3. //#include"cmd.h"
  4. //#include"study.h"
  5. //#include"en25f16drv.h"
  6. #include"mytime.h"
  7. #include"message.h"
  8. //#include"led.h"
  9. #include"mytime.h"
  10. #include"signal.h"
  11. void LEDproc(void);
  12. void FLASHproc(uint8_t);
  13. void CMDproc(uint8_t infor);
  14. SIGNAL * pledsig;
  15. int main (void)
  16. {
  17. // uint8_t datatest[] = "sunshitao" ; //串口测试信息
  18. uint16_t Msg;
  19. uint8_t  MsgVal;
  20. SystemInit();   /* 系统初始化                   */
  21. MsgVal = MsgVal;
  22. // pledsig = founn_signal();
  23. // led_flicker_times = 3;
  24. //    led_flicker_time_interval = 3;
  25. //-------------------------------------------------------------
  26. LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 6);  //使能GPIO模块
  27. // LED1INIT(); //初始化LED1
  28. // SSPPinInit();//初始化SPI引脚                     
  29. // UARTInit(115200); //串口初始化  
  30. // UARTSend(datatest, 10);//测试串口
  31. // timer32B1_init(); //TMR32B1初始化
  32. SysTick_Config(48000);//系统时钟初始化(10MS)。
  33. init_soft_timer();
  34. LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 16); //关闭引脚配置模块
  35. //--------------------------------------------------------------
  36. msg_init();//初始化消息队列

  37. // CMD_Queue.out = 0;              
  38. // CMD_Queue.in  = 0;
  39. MSG_SEND_MSG(MSG_LED ,1);
  40. while (1) {     
  41.   MSG_GET_MSG(&Msg); /*获取消息*/
  42.   switch(MSG_TYPE(Msg)){
  43.    case MSG_LED:
  44. //    LEDproc();
  45.     break;
  46.    case MSG_FLASH:
  47. //    MsgVal = (uint8_t)MSG_VALUE(Msg);
  48. //    FLASHproc(MsgVal);
  49.     break;
  50.    case MSG_CMD:
  51. //    MsgVal = (uint8_t)MSG_VALUE(Msg);
  52. //    CMDproc(MsgVal);
  53.    default :
  54.    break;
  55.   }
  56. }
  57. }
  58. //void LEDproc(void)
  59. //{
  60. //  LED1_flicker();
  61. //}
  62. //void FLASHproc(uint8_t operate)
  63. //{
  64. // switch(operate){
  65. //  case 1:
  66. //   break;
  67. //  case 2:
  68. //   break;
  69. //  default:
  70. //   break;
  71. //  
  72. // }
  73. //}
  74. //void CMDproc(uint8_t infor)
  75. //{
  76. //  
  77. //
  78. //
  79. //
  80. //
  81. //}
  82. /********************************************************************************************************
  83. **  End Of File
  84. ********************************************************************************************************/
  85. #ifndef MYTIME_H_
  86. #define MYTIME_H_
  87. typedef void (*pTimerCallBack)(void);
  88. typedef struct {
  89. uint32_t         delay_counter;
  90. pTimerCallBack   TimerProcess;
  91. }MYtimer;
  92. #define TIMER_NUMBER 16
  93. extern void init_soft_timer(void);
  94. extern void TimerService(void);
  95. extern void TimerStart(uint16_t delay,pTimerCallBack pcallback);
  96. #endif


  97. #include "lpc11xx.h"
  98. #include "mytime.h"
  99. //#include "led.h"
  100. static uint16_t TimerID;
  101. static MYtimer TimerArray[TIMER_NUMBER];
  102. void init_soft_timer(void)
  103. {
  104. TimerID = 0;
  105. }
  106. static void destroy_Timer(uint8_t ID)
  107. {
  108. TimerID &=~(1<<ID);
  109. }
  110. static void found_Timer(uint8_t ID)
  111. {
  112. TimerID |= (1<<ID);
  113. }
  114. void TimerService(void)
  115. {
  116. uint8_t i;
  117. uint16_t TimerID_MAP;
  118. TimerID_MAP = TimerID;
  119. i = 0;   
  120. while(TimerID_MAP){
  121.   if((TimerID_MAP & 0x01 )== 1){
  122.    if(!(--TimerArray.delay_counter)){
  123.     (*(TimerArray.TimerProcess))();
  124.     destroy_Timer(i);
  125.    }
  126.   }
  127.   TimerID_MAP >>= 1;
  128.   i++;
  129. }
  130. }
  131. void TimerStart(uint16_t delay,pTimerCallBack pcallback)
  132. {
  133.    uint8_t i;
  134.    uint16_t TimerID_MAP;
  135.    TimerID_MAP = TimerID;
  136.    for(i = 0;i < TIMER_NUMBER;i++){
  137.      if(TimerID_MAP&0x01){
  138.    TimerID_MAP >>= 1;
  139.    continue;
  140.   }  
  141.   found_Timer(i);
  142.   TimerArray.delay_counter = delay;
  143.   TimerArray.TimerProcess  = pcallback;
  144.   break;
  145.    }
  146.    return ;
  147. }



  148. #define __MESSAGE_H__
  149. #define MSG_ARRAY_SIZE 8

  150. typedef enum
  151. {
  152. MSG_NULL  = 0x00,     /*have no message*/
  153. MSG_LED   = 0x01,     /*key message*/
  154. MSG_UART  = 0x02,     /*uart message*/
  155. MSG_FLASH  = 0x03,     /*flash operate message*/
  156. MSG_CMD     = 0x04,     /*commend message*/
  157. }MSG;

  158. extern void msg_init(void);
  159. extern void msg_put_in(uint16_t u16);
  160. extern void msg_send_msg(uint8_t MsgType, uint8_t Val);
  161. extern void msg_get_out(uint16_t *pU16);


  162. #define MSG_SEND_MSG(MsgType, Val)  msg_send_msg(MsgType, Val)
  163. #define MSG_SEND_DATA(u16)     msg_put_in(u16)
  164. #define MSG_GET_MSG(pU16)     msg_get_out(pU16)
  165. #define MSG_TYPE(u16)      ((uint8_t *)(&u16))[0]
  166. #define MSG_VALUE(u16)      ((uint8_t *)(&u16))[1]
  167. #define MSG_GET_DATA(pU16)    msg_get_out(pU16)
  168. #endif



  169. #include "lpc11xx.h"
  170. #include "message.h"

  171. static uint8_t u8MsgHead;  /*position that msg will be put in*/
  172. static uint8_t u8MsgTail;  /*position that msg will be get out*/
  173. static uint8_t u8MsgNum;  /*msg number*/
  174. static uint8_t u16MsgArray[MSG_ARRAY_SIZE]; /*msg queue*/
  175. /*==================================================================
  176. * Function : msg_init
  177. * Description : init message
  178. * Input Para : void
  179. * Output Para : void
  180. * Return Value: void
  181. ==================================================================*/
  182. void msg_init(void)
  183. {
  184. u8MsgHead = 0;
  185. u8MsgTail = 0;
  186. u8MsgNum = 0;
  187. }

  188. /*==================================================================
  189. * Function : msg_put_in
  190. * Description : Put in a U16 data in msg queue.
  191.      Because there is no return value to indicate success or fail,
  192.      make sure msg queue is large enough, that is MSG_ARRAY_SIZE is big enough!!!
  193.      You can get a suitable MSG_ARRAY_SIZE with the debug message.
  194. * Input Para : U16 u16Val : data to be put in
  195. * Output Para : void
  196. * Return Value: void
  197. ==================================================================*/
  198. void msg_put_in(uint16_t u16)
  199. {

  200. if (u8MsgNum >= MSG_ARRAY_SIZE)
  201. {
  202.   return;
  203. }

  204. //first put in data, then increase u8MsgHead
  205. u16MsgArray[u8MsgHead] = u16;
  206. u8MsgHead++;
  207. if (u8MsgHead >= MSG_ARRAY_SIZE)
  208. {
  209.   u8MsgHead = 0;
  210. }
  211. u8MsgNum++;
  212. // INTERRUPT_SET(EA_MAP);
  213. return;
  214. }
  215. void msg_send_msg(uint8_t MsgType, uint8_t Val)
  216. {
  217. if (u8MsgNum >= MSG_ARRAY_SIZE)
  218. {
  219.   return;
  220. }
  221. //first put in data, then increase u8MsgHead
  222. ((uint8_t *)(&(u16MsgArray[ u8MsgHead])))[0] = MsgType;
  223. ((uint8_t *)(&(u16MsgArray[ u8MsgHead])))[1] = Val;

  224. u8MsgHead++;
  225. if (u8MsgHead >= MSG_ARRAY_SIZE)
  226. {
  227.   u8MsgHead = 0;
  228. }
  229. u8MsgNum++;
  230. return;
  231. }
  232. /*==================================================================
  233. * Function : msg_get_out
  234. * Description : get a U16 data out of the msg queue.
  235.      If the msg queue is empty, get MSG_NULL
  236. * Input Para : void
  237. * Output Para : U16 * pu16Val : pointer to hold data
  238. * Return Value: void
  239. ==================================================================*/
  240. void msg_get_out(uint16_t *pU16)
  241. {
  242. if (u8MsgNum == 0)
  243. {
  244.   * pU16 = 0x0000;
  245.   return;
  246. }
  247. //first get out data, then increase u8MsgTail
  248. *pU16 = u16MsgArray[u8MsgTail];
  249. u8MsgTail++;
  250. if (u8MsgTail >= MSG_ARRAY_SIZE)
  251. {
  252.   u8MsgTail = 0;
  253. }
  254. u8MsgNum--;
  255. return;
  256. }



  257. #ifndef __SIGNAL_H_
  258. #define __SIGNAL_H_
  259. typedef struct {
  260.   uint8_t signal_sta;
  261. }SIGNAL;   
  262. #define SIG_USE    1
  263. #define SIG_NUSE   0
  264. #define SIG_VAL    1
  265. #define SIG_UVAL   0
  266. extern SIGNAL* founn_signal(void);

  267. extern uint8_t apply_signal(SIGNAL* psig);

  268. extern void release_signal(SIGNAL* psig);

  269. extern void destroy_signal(SIGNAL *psig);

  270. #endif



  271. #include"lpc11xx.h"
  272. #include"signal.h"
  273. SIGNAL* founn_signal(void)
  274. {
  275.       SIGNAL* psig;
  276. static  SIGNAL signal;
  277.      psig = &signal ;
  278.      psig->signal_sta = SIG_NUSE;
  279. return psig;
  280. }
  281. uint8_t apply_signal(SIGNAL* psig)
  282. {
  283. if(psig->signal_sta == SIG_USE){
  284.   return SIG_UVAL;
  285. } else {
  286.   psig->signal_sta = SIG_USE;
  287.   return SIG_VAL;
  288. }
  289. }
  290. void release_signal(SIGNAL* psig)
  291. {
  292. psig->signal_sta = SIG_NUSE;
  293. }
  294. void destroy_signal(SIGNAL *psig)
  295. {
  296. psig = psig;
  297. psig = 0;
  298. }



  299. #ifndef SYSTICK_H_
  300. #define SYSTICK_H_
  301. extern volatile uint32_t TimeTick;
  302. extern volatile uint32_t TMRInt4ms标志寄存器 ;
  303. extern void sys_process(void);
  304. #endif



  305. #include"LPC11xx.h"
  306. #include"systick.h"
  307. //#include"uart.h"
  308. //#include"cmd.h"
  309. #include"message.h"
  310. //#include"led.h"
  311. #include"mytime.h"
  312. volatile uint32_t TimeTick = 0;
  313. volatile uint32_t TMRInt4ms标志寄存器 = 0;
  314. void SysTick_Handler(void)
  315. {  
  316. TimerService();
  317.   sys_process();
  318. }
  319. void sys_process(void)
  320. {

  321. }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
xyz769 发表于 2011-9-27 08:30 | 显示全部楼层
  不错,21IC上很少看到好代码。。
 楼主| sunshitao 发表于 2011-9-28 07:08 | 显示全部楼层
怎么木有人来聊聊?给点建议?指点指点啊?
lixiaoxu2meng 发表于 2011-9-28 11:05 | 显示全部楼层
顶 下来看看
原野之狼 发表于 2011-9-28 14:50 | 显示全部楼层
这东西简单应用还行 玩点复杂的东西就力不从心了
比如说 把文件系统整进去 把USB驱动整进去 你这个框架就玩不转了
如果资源够用 玩OS才是正道
alicedodo 发表于 2011-9-28 14:54 | 显示全部楼层
大体看了一下贴出来的程序架构,基本上是一个消息驱动的模式   
LZ的代码风格比较规范
冒昧的提几点建议:
1、主循环的消息处理结构如果做成一个状态机就更好了,消息驱动+状态机从来都是黄金搭档
2、 消息发送者是否有可能是ISR?如果是的话,需要将消息功能的接口函数做成可重入的,而且函数中操作关键全局变量的代码部分要做成临界段
3、消息的结构只是一个无符号16变量,除了消息类型标识,这样的消息结构不能传递太多的消息参数,是不是过于简单了?我觉得像M0这样资源比较丰富的片子,不用太吝惜RAM的
4、最好将消息相关的数据结构封装成一个结构体,把存储队列和全局管理变量当做结构体的成员,降低数据结构之间的离散性
 楼主| sunshitao 发表于 2011-9-28 15:35 | 显示全部楼层
5# 原野之狼
OS正在写  过几天传上来。麻烦给指点一下
 楼主| sunshitao 发表于 2011-9-28 15:37 | 显示全部楼层
6# alicedodo
谢谢您的建议 ,我在研究一下。。这几条建议,够我好好想想的 了  呵呵
zhanyanqiang 发表于 2012-12-12 13:59 | 显示全部楼层
alicedodo 发表于 2011-9-28 14:54
大体看了一下贴出来的程序架构,基本上是一个消息驱动的模式   
LZ的代码风格比较规范
冒昧的提几点建议:

大侠啊,你给改一个吧~~~~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

111

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部