打印

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

[复制链接]
2385|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sunshitao|  楼主 | 2011-9-26 22:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 sunshitao 于 2011-9-26 22:57 编辑

去掉了原程序界面的显示部分、定时器只提供回调函数,
在实际中勉强凑合着用  但是有时候这种架构也不是很方便,感觉还是操作系统好用一点。。。
附上源码  供兄弟姐妹  大叔大婶们 拍砖
 
#include"LPC11xx.h"                                                   /* LPC11xx 外设寄存器定义        */
//#include"uart.h"
//#include"cmd.h"
//#include"study.h"
//#include"en25f16drv.h"
#include"mytime.h"
#include"message.h"
//#include"led.h"
#include"mytime.h"
#include"signal.h"
void LEDproc(void);
void FLASHproc(uint8_t);
void CMDproc(uint8_t infor);
SIGNAL * pledsig;
int main (void)
{
// uint8_t datatest[] = "sunshitao" ; //串口测试信息
uint16_t Msg;
uint8_t  MsgVal;
SystemInit();   /* 系统初始化                   */
MsgVal = MsgVal;
// pledsig = founn_signal();
// led_flicker_times = 3;
//    led_flicker_time_interval = 3;
//-------------------------------------------------------------
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 6);  //使能GPIO模块
// LED1INIT(); //初始化LED1
// SSPPinInit();//初始化SPI引脚                     
// UARTInit(115200); //串口初始化  
// UARTSend(datatest, 10);//测试串口
// timer32B1_init(); //TMR32B1初始化
SysTick_Config(48000);//系统时钟初始化(10MS)。
init_soft_timer();
LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 16); //关闭引脚配置模块
//--------------------------------------------------------------
msg_init();//初始化消息队列

// CMD_Queue.out = 0;              
// CMD_Queue.in  = 0;
MSG_SEND_MSG(MSG_LED ,1);
while (1) {     
  MSG_GET_MSG(&Msg); /*获取消息*/
  switch(MSG_TYPE(Msg)){
   case MSG_LED:
//    LEDproc();
    break;
   case MSG_FLASH:
//    MsgVal = (uint8_t)MSG_VALUE(Msg);
//    FLASHproc(MsgVal);
    break;
   case MSG_CMD:
//    MsgVal = (uint8_t)MSG_VALUE(Msg);
//    CMDproc(MsgVal);
   default :
   break;
  }
}
}
//void LEDproc(void)
//{
//  LED1_flicker();
//}
//void FLASHproc(uint8_t operate)
//{
// switch(operate){
//  case 1:
//   break;
//  case 2:
//   break;
//  default:
//   break;
//  
// }
//}
//void CMDproc(uint8_t infor)
//{
//  
//
//
//
//
//}
/********************************************************************************************************
**  End Of File
********************************************************************************************************/
#ifndef MYTIME_H_
#define MYTIME_H_
typedef void (*pTimerCallBack)(void);
typedef struct {
uint32_t         delay_counter;
pTimerCallBack   TimerProcess;
}MYtimer;
#define TIMER_NUMBER 16
extern void init_soft_timer(void);
extern void TimerService(void);
extern void TimerStart(uint16_t delay,pTimerCallBack pcallback);
#endif


#include "lpc11xx.h"
#include "mytime.h"
//#include "led.h"
static uint16_t TimerID;
static MYtimer TimerArray[TIMER_NUMBER];
void init_soft_timer(void)
{
TimerID = 0;
}
static void destroy_Timer(uint8_t ID)
{
TimerID &=~(1<<ID);
}
static void found_Timer(uint8_t ID)
{
TimerID |= (1<<ID);
}
void TimerService(void)
{
uint8_t i;
uint16_t TimerID_MAP;
TimerID_MAP = TimerID;
i = 0;   
while(TimerID_MAP){
  if((TimerID_MAP & 0x01 )== 1){
   if(!(--TimerArray.delay_counter)){
    (*(TimerArray.TimerProcess))();
    destroy_Timer(i);
   }
  }
  TimerID_MAP >>= 1;
  i++;
}
}
void TimerStart(uint16_t delay,pTimerCallBack pcallback)
{
   uint8_t i;
   uint16_t TimerID_MAP;
   TimerID_MAP = TimerID;
   for(i = 0;i < TIMER_NUMBER;i++){
     if(TimerID_MAP&0x01){
   TimerID_MAP >>= 1;
   continue;
  }  
  found_Timer(i);
  TimerArray.delay_counter = delay;
  TimerArray.TimerProcess  = pcallback;
  break;
   }
   return ;
}



#define __MESSAGE_H__
#define MSG_ARRAY_SIZE 8

typedef enum
{
MSG_NULL  = 0x00,     /*have no message*/
MSG_LED   = 0x01,     /*key message*/
MSG_UART  = 0x02,     /*uart message*/
MSG_FLASH  = 0x03,     /*flash operate message*/
MSG_CMD     = 0x04,     /*commend message*/
}MSG;

extern void msg_init(void);
extern void msg_put_in(uint16_t u16);
extern void msg_send_msg(uint8_t MsgType, uint8_t Val);
extern void msg_get_out(uint16_t *pU16);


#define MSG_SEND_MSG(MsgType, Val)  msg_send_msg(MsgType, Val)
#define MSG_SEND_DATA(u16)     msg_put_in(u16)
#define MSG_GET_MSG(pU16)     msg_get_out(pU16)
#define MSG_TYPE(u16)      ((uint8_t *)(&u16))[0]
#define MSG_VALUE(u16)      ((uint8_t *)(&u16))[1]
#define MSG_GET_DATA(pU16)    msg_get_out(pU16)
#endif



#include "lpc11xx.h"
#include "message.h"

static uint8_t u8MsgHead;  /*position that msg will be put in*/
static uint8_t u8MsgTail;  /*position that msg will be get out*/
static uint8_t u8MsgNum;  /*msg number*/
static uint8_t u16MsgArray[MSG_ARRAY_SIZE]; /*msg queue*/
/*==================================================================
* Function : msg_init
* Description : init message
* Input Para : void
* Output Para : void
* Return Value: void
==================================================================*/
void msg_init(void)
{
u8MsgHead = 0;
u8MsgTail = 0;
u8MsgNum = 0;
}

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

if (u8MsgNum >= MSG_ARRAY_SIZE)
{
  return;
}

//first put in data, then increase u8MsgHead
u16MsgArray[u8MsgHead] = u16;
u8MsgHead++;
if (u8MsgHead >= MSG_ARRAY_SIZE)
{
  u8MsgHead = 0;
}
u8MsgNum++;
// INTERRUPT_SET(EA_MAP);
return;
}
void msg_send_msg(uint8_t MsgType, uint8_t Val)
{
if (u8MsgNum >= MSG_ARRAY_SIZE)
{
  return;
}
//first put in data, then increase u8MsgHead
((uint8_t *)(&(u16MsgArray[ u8MsgHead])))[0] = MsgType;
((uint8_t *)(&(u16MsgArray[ u8MsgHead])))[1] = Val;

u8MsgHead++;
if (u8MsgHead >= MSG_ARRAY_SIZE)
{
  u8MsgHead = 0;
}
u8MsgNum++;
return;
}
/*==================================================================
* Function : msg_get_out
* Description : get a U16 data out of the msg queue.
     If the msg queue is empty, get MSG_NULL
* Input Para : void
* Output Para : U16 * pu16Val : pointer to hold data
* Return Value: void
==================================================================*/
void msg_get_out(uint16_t *pU16)
{
if (u8MsgNum == 0)
{
  * pU16 = 0x0000;
  return;
}
//first get out data, then increase u8MsgTail
*pU16 = u16MsgArray[u8MsgTail];
u8MsgTail++;
if (u8MsgTail >= MSG_ARRAY_SIZE)
{
  u8MsgTail = 0;
}
u8MsgNum--;
return;
}



#ifndef __SIGNAL_H_
#define __SIGNAL_H_
typedef struct {
  uint8_t signal_sta;
}SIGNAL;   
#define SIG_USE    1
#define SIG_NUSE   0
#define SIG_VAL    1
#define SIG_UVAL   0
extern SIGNAL* founn_signal(void);

extern uint8_t apply_signal(SIGNAL* psig);

extern void release_signal(SIGNAL* psig);

extern void destroy_signal(SIGNAL *psig);

#endif



#include"lpc11xx.h"
#include"signal.h"
SIGNAL* founn_signal(void)
{
      SIGNAL* psig;
static  SIGNAL signal;
     psig = &signal ;
     psig->signal_sta = SIG_NUSE;
return psig;
}
uint8_t apply_signal(SIGNAL* psig)
{
if(psig->signal_sta == SIG_USE){
  return SIG_UVAL;
} else {
  psig->signal_sta = SIG_USE;
  return SIG_VAL;
}
}
void release_signal(SIGNAL* psig)
{
psig->signal_sta = SIG_NUSE;
}
void destroy_signal(SIGNAL *psig)
{
psig = psig;
psig = 0;
}



#ifndef SYSTICK_H_
#define SYSTICK_H_
extern volatile uint32_t TimeTick;
extern volatile uint32_t TMRInt4ms标志寄存器 ;
extern void sys_process(void);
#endif



#include"LPC11xx.h"
#include"systick.h"
//#include"uart.h"
//#include"cmd.h"
#include"message.h"
//#include"led.h"
#include"mytime.h"
volatile uint32_t TimeTick = 0;
volatile uint32_t TMRInt4ms标志寄存器 = 0;
void SysTick_Handler(void)
{  
TimerService();
  sys_process();
}
void sys_process(void)
{

}

V2.02.rar

323.58 KB

相关帖子

沙发
xyz769| | 2011-9-27 08:30 | 只看该作者
  不错,21IC上很少看到好代码。。

使用特权

评论回复
板凳
sunshitao|  楼主 | 2011-9-28 07:08 | 只看该作者
怎么木有人来聊聊?给点建议?指点指点啊?

使用特权

评论回复
地板
lixiaoxu2meng| | 2011-9-28 11:05 | 只看该作者
顶 下来看看

使用特权

评论回复
5
原野之狼| | 2011-9-28 14:50 | 只看该作者
这东西简单应用还行 玩点复杂的东西就力不从心了
比如说 把文件系统整进去 把USB驱动整进去 你这个框架就玩不转了
如果资源够用 玩OS才是正道

使用特权

评论回复
6
alicedodo| | 2011-9-28 14:54 | 只看该作者
大体看了一下贴出来的程序架构,基本上是一个消息驱动的模式   
LZ的代码风格比较规范
冒昧的提几点建议:
1、主循环的消息处理结构如果做成一个状态机就更好了,消息驱动+状态机从来都是黄金搭档
2、 消息发送者是否有可能是ISR?如果是的话,需要将消息功能的接口函数做成可重入的,而且函数中操作关键全局变量的代码部分要做成临界段
3、消息的结构只是一个无符号16变量,除了消息类型标识,这样的消息结构不能传递太多的消息参数,是不是过于简单了?我觉得像M0这样资源比较丰富的片子,不用太吝惜RAM的
4、最好将消息相关的数据结构封装成一个结构体,把存储队列和全局管理变量当做结构体的成员,降低数据结构之间的离散性

使用特权

评论回复
7
sunshitao|  楼主 | 2011-9-28 15:35 | 只看该作者
5# 原野之狼
OS正在写  过几天传上来。麻烦给指点一下

使用特权

评论回复
8
sunshitao|  楼主 | 2011-9-28 15:37 | 只看该作者
6# alicedodo
谢谢您的建议 ,我在研究一下。。这几条建议,够我好好想想的 了  呵呵

使用特权

评论回复
9
zhanyanqiang| | 2012-12-12 13:59 | 只看该作者
alicedodo 发表于 2011-9-28 14:54
大体看了一下贴出来的程序架构,基本上是一个消息驱动的模式   
LZ的代码风格比较规范
冒昧的提几点建议:

大侠啊,你给改一个吧~~~~~

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

111

帖子

1

粉丝