本想用semaphore搞个简单的LCD示例,也就是按一个按钮,将按钮值显示在LCD上。但写的时候突然发现semaphore无法实现这个功能啊。因为要在一个task内显示按钮值,而按钮有5个,显示5个值,这时需要参数传递。但semaphore无法传递参数,其实用一个全局变量也可以实现参数的传递。
照片留念:
![](https://img-blog.csdn.net/20170106234424892?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJhdGVp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
赶紧找还有什么机制是可以传递参数的,邮箱不就是干这事的嘛!现在终于理解了,邮箱其实就是可以传递参数的semaphore。正好邮箱没做过例子,那就用邮箱来实现这个简单功能吧。
说干就干,撸起袖子来,干了几个小时,总是无法显示。最后发现是task栈设小了,默认是512,不够,弄到738立马成功。太坑爹了,几个小时又没了。
上代码:
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Error.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/MailBox.h>
/* TI-RTOS Header files */
#include <ti/drivers/PIN.h>
#include <ti/mw/display/Display.h>
#include <ti/mw/display/DisplayExt.h>
/* Board Header files */
#include "Board.h"
#define TASKSTACKSIZE 768
Task_Struct task0Struct;
Char task0Stack[TASKSTACKSIZE];
Mailbox_Handle mbox;
static PIN_Handle buttonPinHandle;
static PIN_State buttonPinState;
//5个按钮的引脚中断配置表
PIN_Config buttonPinTable[] =
{
Board_KEY_SELECT | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
Board_KEY_UP | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
Board_KEY_DOWN | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
Board_KEY_LEFT | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
Board_KEY_RIGHT | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
PIN_TERMINATE
};
//按钮中断回调函数
void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId)
{
//按键消抖
CPUdelay(8000 * 50);
uint8_t id = pinId; //用于存储按钮编号,并作为参数通过邮箱发送
if (!PIN_getInputValue(pinId))
{
Mailbox_post(mbox, &id, BIOS_WAIT_FOREVER);
}
}
//由邮箱激活的任务,用于根据邮箱传递过来的值显示对应按钮
Void taskFxn(UArg arg0, UArg arg1)
{
uint8_t msg;//邮箱传递参数的接收缓冲
char *str; //显示在LCD的字符串
//LCD初始化
Display_Params params;
Display_Params_init(¶ms);
params.lineClearMode = DISPLAY_CLEAR_BOTH;
Display_Handle hDisplayLcd = Display_open(Display_Type_LCD, ¶ms);
Display_print0(hDisplayLcd, 2, 0, "Please Press Button");
while (1)
{
//等待邮件,并根据参数在LCD显示相应按钮
Mailbox_pend(mbox, &msg, BIOS_WAIT_FOREVER);
switch(msg)
{
case Board_KEY_SELECT:
str = "Board_KEY_SELECT";
break;
case Board_KEY_UP:
str = "Board_KEY_UP";
break;
case Board_KEY_DOWN:
str = "Board_KEY_DOWN";
break;
case Board_KEY_LEFT:
str = "Board_KEY_LEFT";
break;
case Board_KEY_RIGHT:
str = "Board_KEY_RIGHT";
break;
}
Display_print0(hDisplayLcd, 4, 0, "You pressed Button:");
Display_print0(hDisplayLcd, 5, 0, str); //显示按钮所对应的字符串
}
}
/*
* ======== main ========
*/
int main(void)
{
Task_Params taskParams;
Mailbox_Params mboxParams;
/* Call board init functions */
Board_initGeneral();
/* Construct heartBeat Task thread */
Task_Params_init(&taskParams);
taskParams.stackSize = TASKSTACKSIZE;
taskParams.stack = &task0Stack;
Task_construct(&task0Struct, (Task_FuncPtr)taskFxn, &taskParams, NULL);
buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn);
//邮箱初始化
Mailbox_Params_init(&mboxParams);
mbox = Mailbox_create(sizeof(uint8_t), 50, &mboxParams, NULL);
if (mbox == NULL)
{
System_abort("Mailbox create failed");
}
System_printf("ini have finish\n");
System_flush();
/* Start BIOS */
BIOS_start();
return (0);
}
照片留念: