summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorJianhui.Zhu <zhujianhui6456066@163.com>2019-09-07 15:58:17 (GMT)
committer Gavin.Bai <gavin_8724@163.com>2019-08-27 06:31:00 (GMT)
commitf88adfdf33a6e64eca24c11601533eb4fa415dc4 (patch)
treed9f4040f7eaff01601164a6cd4098ac46d458d79
parent48608cc9f85a46301056753dc5d25efad58469f1 (diff)
downloadAIC-OS-f88adfdf33a6e64eca24c11601533eb4fa415dc4.zip
Merge 1553B bus & driver.
-rw-r--r--SylixOS/driver/bu61580/bu61580.c621
-rw-r--r--SylixOS/driver/bu61580/bu61580.h67
-rw-r--r--SylixOS/system/device/1553B/bus1553.c1050
-rw-r--r--SylixOS/system/device/1553B/bus1553.h175
-rw-r--r--SylixOS/system/include/s_system.h1
-rw-r--r--libsylixos.mk2
6 files changed, 1916 insertions, 0 deletions
diff --git a/SylixOS/driver/bu61580/bu61580.c b/SylixOS/driver/bu61580/bu61580.c
new file mode 100644
index 0000000..178173a
--- /dev/null
+++ b/SylixOS/driver/bu61580/bu61580.c
@@ -0,0 +1,621 @@
+/*********************************************************************************************************
+**
+** 中国软件开源组织
+**
+** 嵌入式实时操作系统
+**
+** SylixOS(TM) LW : long wing
+**
+** Copyright All Rights Reserved
+**
+**--------------文件信息--------------------------------------------------------------------------------
+**
+** 文 件 名: bu61580.c
+**
+** 创 建 人: Zhu.Jianhui (朱剑辉)
+**
+** 文件创建日期: 2019 年 08 月 05 日
+**
+** 描 述: 1553B 总线驱动支持.
+*********************************************************************************************************/
+#define __SYLIXOS_KERNEL
+#include "SylixOS.h"
+#include "../SylixOS/config/driver/drv_cfg.h"
+#include "bu61580.h"
+
+
+/*********************************************************************************************************
+ internal function declare
+*********************************************************************************************************/
+static INT Bu61580Ioctl(BU61580_CHAN *pbu61580chan, INT cmd, LONG arg);
+static INT Bu61580TxStartup(BU61580_CHAN *pbu61580chan);
+static INT Bu61580CallbackInstall(BU61580_CHAN *pbu61580chan,
+ INT callbackType,
+ INT (*callback)(),
+ VOID *callbackArg);
+/*********************************************************************************************************
+ bu61580 driver functions
+*********************************************************************************************************/
+static DRV_FUNCS_1553 bu61580_drv_funcs = {
+ (INT (*)())Bu61580Ioctl,
+ (INT (*)())Bu61580TxStartup,
+ (INT (*)())Bu61580CallbackInstall
+};
+/*********************************************************************************************************
+** 函数名称: Bu61580Init
+** 功能描述: 初始化 bu61580 驱动程序
+** 输 入 : pbu61580chan bu61580 CHAN
+** 输 出 : ERROR or OK
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+INT Bu61580Init (BU61580_CHAN *pbu61580chan)
+{
+ if(!pbu61580chan) {
+ return -1;
+ }
+
+ if (pbu61580chan->channel == 0) {
+ _ErrorHandle(ENOSYS);
+ return (PX_ERROR);
+ }
+
+ LW_SPIN_INIT(&pbu61580chan->bu61580_splock);
+
+ pbu61580chan->pDrvFuncs = &bu61580_drv_funcs;
+ pbu61580chan->channel = 0;
+ pbu61580chan->busMode = INIT_MOD; //初始化模式,可以通过IOCtrl更改
+
+ return (ERROR_NONE);
+}
+
+/*********************************************************************************************************
+** 函数名称: Bu61580Send
+** 功能描述: bu61580 发送
+** 输 入 : pbu61580chan bu61580 CHAN
+** dataFrame bu61580 消息帧
+** 输 出 : ERROR or OK
+*********************************************************************************************************/
+static INT Bu61580Send (BU61580_CHAN *pbu61580chan, FRAME_1553DATA *dataFrame)
+{
+ INTREG intreg;
+ UINT8 wordCnt = 0;
+ UINT16 ctrlWord = 0;
+ UINT8 index = 0;
+ UINT32 msgBlkPtr = 0;
+ UINT32 msgStackPtr = 0;
+ UINT8 subAddr = 0;
+
+ if(!pbu61580chan || !dataFrame) {
+ return -1;
+ }
+
+ LW_SPIN_LOCK_QUICK(&pbu61580chan->bu61580_splock, &intreg);
+
+ //从数据帧获取信息,组成消息帧发送
+ //注意需要区分BC 和RT 模式,BC还需区分发送和接收
+ if (dataFrame->frameInfo1.CMDWORD1.WCOUNT > 0) {
+ wordCnt = dataFrame->frameInfo1.CMDWORD1.WCOUNT;
+ } else {
+ wordCnt = 32;
+ }
+
+ if(pbu61580chan->busMode == BC_MOD) {
+ msgBlkPtr = 0x108;
+ for(index=0;index<38;index++) { //数据清零
+ pbu61580chan->WriteMem(pbu61580chan,msgBlkPtr+index, 0);
+ }
+ pbu61580chan->WriteMem(pbu61580chan,msgBlkPtr, dataFrame->msgCtrlWord); //控制字
+ pbu61580chan->WriteMem(pbu61580chan,msgBlkPtr+1, dataFrame->frameInfo1.cmdWord1); //命令字
+ switch(dataFrame->msgType) {
+ case BC_RT:
+ case BCST:
+ for(index=0;index<wordCnt;index++) {
+ pbu61580chan->WriteMem(pbu61580chan,msgBlkPtr+2+index, dataFrame->msgData[index]);
+ }
+ break;
+ case RT_BC:
+ case MC_ND:
+ case TX_MC_WD:
+ case MCND_BCST:
+
+ break;
+ case RT_RT:
+ case RT_RTS:
+ pbu61580chan->WriteMem(pbu61580chan,msgBlkPtr+2, dataFrame->frameInfo2.cmdWord2);
+ break;
+ case RX_MC_WD:
+ case MCWD_BCST:
+ pbu61580chan->WriteMem(pbu61580chan,msgBlkPtr+2, dataFrame->msgData[0]);
+ break;
+ default:
+ break;
+ }
+ //填写4个字的消息堆栈信息
+ pbu61580chan->WriteMem(pbu61580chan, msgStackPtr, 0);
+ pbu61580chan->WriteMem(pbu61580chan, msgStackPtr+1, 0);
+ pbu61580chan->WriteMem(pbu61580chan, msgStackPtr+2, dataFrame->msgGapTime);
+ pbu61580chan->WriteMem(pbu61580chan, msgStackPtr+3, msgBlkPtr);
+
+ pbu61580chan->WriteMem(pbu61580chan,0x0101, 0xfffeU); //此处设置只发一条消息
+ pbu61580chan->WriteMem(pbu61580chan,0x0100, 0); //设置堆栈
+ pbu61580chan->WriteReg(pbu61580chan,STR_RST, 0x0022); //启动
+ } else if(pbu61580chan->busMode == RT_MOD) {
+ switch(dataFrame->msgType) {
+ case RT_TO_BC:
+ subAddr = dataFrame->frameInfo1.CMDWORD1.SADDR;
+ //msgBlkPtr = pbu61580chan->rtTxSubAddrInfo[subAddr].rtTxSubAddrStartAddr;
+ msgBlkPtr = 0x0660 + subAddr*32;
+ for(index=0;index<wordCnt;index++) {
+ pbu61580chan->WriteMem(pbu61580chan,msgBlkPtr+index, dataFrame->msgData[index]);
+ }
+ break;
+ case MODE_CODE:
+ //
+ break;
+ default:
+ break;
+ }
+ } else {
+ return -1;
+ }
+
+ LW_SPIN_UNLOCK_QUICK(&pbu61580chan->bu61580_splock, intreg);
+
+
+ return (ERROR_NONE);
+}
+
+/*********************************************************************************************************
+** 函数名称: Bu61580Recv
+** 功能描述: bu61580 接收
+** 输 入 : pbu61580chan bu61580 CHAN
+** dataFrame bu61580 消息帧
+** 输 出 : ERROR or OK
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT Bu61580Recv (BU61580_CHAN *pbu61580chan, FRAME_1553DATA *dataFrame)
+{
+ INTREG intreg =0;
+ UINT32 msgBlkPtr = 0;
+ UINT8 wordCnt = 0;
+ UINT8 rtSubAddr = 0;
+ int i = 0;
+ char buf[40];
+
+ if(!pbu61580chan || !dataFrame) {
+ return -1;
+ }
+
+ LW_SPIN_LOCK_QUICK(&pbu61580chan->bu61580_splock, &intreg);
+
+ if (dataFrame->frameInfo1.CMDWORD1.WCOUNT > 0) {
+ wordCnt = dataFrame->frameInfo1.CMDWORD1.WCOUNT;
+ } else {
+ wordCnt = 32;
+ }
+
+ if(pbu61580chan->busMode == BC_MOD) {
+ msgBlkPtr = 0x0108U;
+ switch (dataFrame->msgType) {
+ case BC_RT:
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + wordCnt + 2) & 0xFFFF;
+ dataFrame->msgStatus1 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + wordCnt + 3) & 0xFFFF;
+ break;
+ case RT_BC:
+ for (i=0; i<wordCnt; i++) {
+ dataFrame->msgData[i] = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + i + 4) & 0xFFFF;
+ }
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 2) & 0xFFFF;
+ dataFrame->msgStatus1 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 3) & 0xFFFF;
+ break;
+ case RT_RT:
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 3) & 0xFFFF;
+ dataFrame->msgStatus1 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 4) & 0xFFFF;
+ for (i=0; i<wordCnt; i++) {
+ dataFrame->msgData[i] = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + i + 5) & 0xFFFF;
+ }
+ dataFrame->msgStatus2 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + wordCnt + 5) & 0xFFFF;
+ break;
+ case BCST:
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + wordCnt + 2) & 0xFFFF;
+ break;
+ case RT_RTS:
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 3) & 0xFFFF;
+ dataFrame->msgStatus1 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 4) & 0xFFFF;
+ for (i=0; i<wordCnt; i++) {
+ dataFrame->msgData[i] = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + i + 5) & 0xFFFF;
+ }
+ break;
+ case MC_ND:
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 2) & 0xFFFF;
+ dataFrame->msgStatus1 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 3) & 0xFFFF;
+ break;
+ case TX_MC_WD:
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 2) & 0xFFFF;
+ dataFrame->msgStatus1 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 3) & 0xFFFF;
+ dataFrame->msgData[0] = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 4) & 0xFFFF;
+ break;
+ case RX_MC_WD:
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 3) & 0xFFFF;
+ dataFrame->msgStatus1 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 4) & 0xFFFF;
+ break;
+ case MCND_BCST:
+ dataFrame->loopBack = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 2) & 0xFFFF;
+ break;
+ case MCWD_BCST:
+ dataFrame->msgStatus1 = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + 3) & 0xFFFF;
+ break;
+ default:
+ break;
+ }
+ } else if(pbu61580chan->busMode == RT_MOD) {
+ rtSubAddr = dataFrame->frameInfo1.CMDWORD1.SADDR;
+ if(dataFrame->frameInfo1.CMDWORD1.RTADDR == 31) { //broadcast
+ //msgBlkPtr = pbu61580chan->broadcastSubAddrInfo[rtSubAddr].broadcastSubAddrStartAddr;
+ msgBlkPtr = 0x0a60 + rtSubAddr*32;
+ for (i = 0; i < wordCnt; i++) {
+ dataFrame->msgData[i] = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + i) & 0xFFFF;
+ }
+ pbu61580chan->WriteMem(pbu61580chan,0x0180 + rtSubAddr, msgBlkPtr); // 查找表指针复位
+ dataFrame->msgType = BROADCAST;
+ } else if((rtSubAddr==0) || (rtSubAddr==31)) { //mode code
+ //只有0x00、0x14、0x15三种命令有数接收,此处不处理
+ dataFrame->msgType = MODE_CODE;
+ } else { //BC_RT
+ //msgBlkPtr = pbu61580chan->rtRxSubAddrInfo[rtSubAddr].rtRxSubAddrStartAddr;
+ msgBlkPtr = 0x0260 + rtSubAddr*32;
+ for (i = 0; i < wordCnt; i++)
+ {
+ dataFrame->msgData[i] = pbu61580chan->ReadMem(pbu61580chan,msgBlkPtr + i) & 0xFFFF;
+ }
+ pbu61580chan->WriteMem(pbu61580chan,0x0140 + rtSubAddr, msgBlkPtr); // 查找表指针复位
+ dataFrame->msgType = BC_TO_RT;
+ }
+ } else {
+ return -1;
+ }
+
+ LW_SPIN_UNLOCK_QUICK(&pbu61580chan->bu61580_splock, intreg);
+
+ return (ERROR_NONE);
+}
+
+static void Bu61580MemClear(BU61580_CHAN *pbu61580chan)
+{
+ UINT index = 0;
+
+ for(index=0;index<0x1000;index++) {
+ pbu61580chan->WriteMem(pbu61580chan, index, 0);
+ }
+}
+
+static void Bu61580RegInit(BU61580_CHAN *pbu61580chan, UINT rtAddr)
+{
+ if(pbu61580chan->busMode == BC_MOD) {
+ pbu61580chan->WriteReg(pbu61580chan, STR_RST, 0x0001);
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG3, 0x8000);
+ pbu61580chan->WriteReg(pbu61580chan, INTRMASK, 0x0001);//end of message interrupt
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG1, 0x1838);
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG2, 0x8418);
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG4, 0x1080);
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG5, 0x0e00);
+ pbu61580chan->WriteReg(pbu61580chan, TIMETAG, 0x0000);
+ } else if(pbu61580chan->busMode == RT_MOD) {
+ pbu61580chan->WriteReg(pbu61580chan, STR_RST, 0x0001);
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG3, 0x8000);
+ pbu61580chan->WriteReg(pbu61580chan, INTRMASK, 0x0003);//end of message & mode code interrupt
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG3, 0x8081);
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG2, 0x8a9f);
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG4, 0x0008);//enable RT address conf
+ pbu61580chan->WriteReg(pbu61580chan, TIMETAG, 0x0000);
+ if((rtAddr >= 0) && (rtAddr< 32)) {
+ pbu61580chan->WriteReg(pbu61580chan,CONFIG5, 0x0600 | (rtAddr<<1));//校验位可不配
+ } else {
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG5, 0x0600);//default
+ }
+ }
+}
+
+static void Bu61580MemInit(BU61580_CHAN *pbu61580chan)
+{
+ UINT16 index = 0;
+
+ if(pbu61580chan->busMode == BC_MOD) {
+ pbu61580chan->WriteMem(pbu61580chan, 0x0100, 0); //stack pointer A
+ pbu61580chan->WriteMem(pbu61580chan, 0x0104, 0); //stack pointer B
+ } else if(pbu61580chan->busMode == RT_MOD) {
+ pbu61580chan->WriteMem(pbu61580chan, 0x0100, 0); //stack pointer
+ pbu61580chan->WriteMem(pbu61580chan, 0x0104, 0x0f00);//stack pointer B
+
+ for (index=0;index<32;index++) {
+ pbu61580chan->WriteMem(pbu61580chan, 0x0140+index, 0x0260 + index*32);//每条消息32个字
+ pbu61580chan->WriteMem(pbu61580chan, 0x0160+index, 0x0660 + index*32);
+ pbu61580chan->WriteMem(pbu61580chan, 0x0180+index, 0x0a60 + index*32);
+
+ pbu61580chan->WriteMem(pbu61580chan, 0x01c0+index, 0x0260 + index*32);//area B
+ pbu61580chan->WriteMem(pbu61580chan, 0x01e0+index, 0x0660 + index*32);
+ pbu61580chan->WriteMem(pbu61580chan, 0x0200+index, 0x0a60 + index*32);
+
+ pbu61580chan->WriteMem(pbu61580chan, 0x01a0+index, 0x4210); //控制字
+ pbu61580chan->WriteMem(pbu61580chan, 0x0220+index, 0x4210);
+ }
+ } else {
+ //do nothing?
+ }
+}
+
+/*********************************************************************************************************
+** 函数名称: Bu61580InitChip
+** 功能描述: bu61580 芯片初始化
+** 输 入 : pbu61580chan bu61580 CHAN
+** 输 出 : 无
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID Bu61580InitChip (BU61580_CHAN *pbu61580chan, UINT rtAddr)
+{
+ Bu61580MemClear(pbu61580chan); //数据清零
+ Bu61580RegInit(pbu61580chan,rtAddr); //初始化配置寄存器
+ Bu61580MemInit(pbu61580chan); //初始化配置存储
+
+ if(pbu61580chan->busMode == RT_MOD) {
+ pbu61580chan->WriteReg(pbu61580chan, CONFIG1, 0x8f80); //启动RT
+ }
+ pbu61580chan->ReadReg(pbu61580chan, INTR_STATUS); //读中断状态寄存器,清除中断状态
+}
+
+/*********************************************************************************************************
+** 函数名称: Bu61580TxStartup
+** 功能描述: SJA1000 芯片启动发送
+** 输 入 : pbu61580chan bu16580 CHAN
+** 输 出 : ERROR or OK
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT Bu61580TxStartup (BU61580_CHAN *pbu61580chan)
+{
+ FRAME_1553DATA dataFrame;
+
+ if(!pbu61580chan) {
+ return -1;
+ }
+
+ if (pbu61580chan->GetTxFrame(pbu61580chan->GetTxFrameArg, &dataFrame) == ERROR_NONE) {
+ Bu61580Send(pbu61580chan, &dataFrame);
+ }
+
+ return (ERROR_NONE);
+}
+/*********************************************************************************************************
+** 函数名称: bu61580CallbackInstall
+** 功能描述: bu61580安装回调
+** 输 入 : pbu61580chan bu61580 CHAN
+** callbackType 回调类型
+** callback 回调函数
+** callbackArg 回调参数
+** 输 出 : ERROR or OK
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT Bu61580CallbackInstall (BU61580_CHAN *pbu61580chan,
+ INT callbackType,
+ INT (*callback)(),
+ VOID *callbackArg)
+{
+ if(!pbu61580chan) {
+ return -1;
+ }
+ switch (callbackType) {
+
+ case _1553_CB_GET_TX_FRAME:
+ pbu61580chan->GetTxFrame = callback;
+ pbu61580chan->GetTxFrameArg = callbackArg;
+ return (ERROR_NONE);
+
+ case _1553_CB_PUT_RX_FRAME:
+ pbu61580chan->PutRxFrame = callback;
+ pbu61580chan->PutRxFrameArg = callbackArg;
+ return (ERROR_NONE);
+
+ case _1553_CB_PUT_BUS_STATE:
+ pbu61580chan->SetBusState = callback;
+ pbu61580chan->SetBusStateArg = callbackArg;
+ return (ERROR_NONE);
+
+ default:
+ _ErrorHandle(ENOSYS);
+ return (PX_ERROR);
+ }
+}
+/*********************************************************************************************************
+** 函数名称: Bu61580Ioctl
+** 功能描述: Bu61580 控制
+** 输 入 : pbu61580chan bu61580 CHAN
+** cmd 控制命令
+** arg 控制参数
+** 输 出 : ERROR or OK
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT Bu61580Ioctl (BU61580_CHAN *pbu61580chan, INT cmd, LONG arg)
+{
+ INTREG intreg;
+ INT tmp;
+
+ if(!pbu61580chan) {
+ return -1;
+ }
+
+ switch (cmd) {
+ case _1553_DEV_OPEN: /* 打开 1553 设备 */
+ pbu61580chan->open(pbu61580chan);
+ break;
+
+ case _1553_DEV_CLOSE: /* 关闭 1553 设备 */
+ pbu61580chan->close(pbu61580chan);
+ break;
+
+ case _1553_DEV_SET_MODE:
+ if(pbu61580chan->busMode != INIT_MOD) //只有且必须在尚未启动运行前设置
+ break;
+ switch(arg) {
+ case BC_MOD:
+ pbu61580chan->busMode = BC_MOD;
+ break;
+ case RT_MOD:
+ pbu61580chan->busMode = RT_MOD;
+ break;
+ case MT_MOD:
+ pbu61580chan->busMode = MT_MOD;
+ break;
+ default:
+ break;
+ }
+ break;
+
+ case _1553_DEV_REST_BUS:
+ case _1553_DEV_STARTUP:
+ pbu61580chan->reset(pbu61580chan);
+ pbu61580chan->SetBusState(pbu61580chan->SetBusStateArg, _1553_DEV_BUS_OK);
+
+ intreg = KN_INT_DISABLE();
+
+ Bu61580InitChip(pbu61580chan,arg);
+
+ KN_INT_ENABLE(intreg);
+
+ Bu61580TxStartup(pbu61580chan);
+
+ break;
+
+ default:
+ errno = ENOSYS;
+ return (PX_ERROR);
+ }
+
+ return (ERROR_NONE);
+}
+/*********************************************************************************************************
+** 函数名称: Bu61580BcIsr
+** 功能描述: bu61580 BC模式中断处理
+** 输 入 : pbu61580chan bu61580 CHAN
+** cmd 控制命令
+** arg 控制参数
+** 输 出 : ERROR or OK
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID Bu61580BcIsr (BU61580_CHAN *pbu61580chan)
+{
+ FRAME_1553DATA dataFrame;
+ UINT32 msgCmdWord = 0;
+ UINT16 msgCtrlWord = 0;
+ UINT8 msgFormat = 0;
+ UINT8 trRxBit = 0;
+
+ if(!pbu61580chan) {
+ return;
+ }
+
+ pbu61580chan->ReadReg(pbu61580chan, INTR_STATUS); //读中断状态寄存器,清除1553中断
+
+ /*
+ *读消息发送数据区获取消息命令字、控制字
+ * 此处需注意:必须保证当前消息处理完才能处理后续消息
+ * 即产生中断的消息数据没有被后续消息刷新
+ * 但是如果这样的话,到底是由用户保证还是驱动保证??? 如何加锁???
+ * 是否可以考虑用全局或静态变量暂存产生当前中断的消息信息???
+ */
+ msgCmdWord = pbu61580chan->ReadMem(pbu61580chan, 0x109);
+ dataFrame.frameInfo1.cmdWord1 = msgCmdWord;
+ trRxBit = (msgCmdWord & CMD_TXRX_BIT_MASK) >> CMD_TXRX_BIT;
+
+ msgCtrlWord = (pbu61580chan->ReadMem(pbu61580chan,0x0108U)) & 0xFFFF;
+ msgFormat = msgCtrlWord & 0x07;
+ dataFrame.msgChannel = msgCtrlWord & (0x01<<7);
+
+ //根据消息控制字的低3位和消息收发标志确定消息类型
+ switch(msgFormat) {
+ case 0:
+ if(trRxBit == 1) {
+ dataFrame.msgType = RT_BC;
+ } else {
+ dataFrame.msgType = BC_RT;
+ }
+ break;
+ case 1:
+ dataFrame.msgType = RT_RT;
+ break;
+ case 2:
+ dataFrame.msgType = BCST;
+ break;
+ case 3:
+ dataFrame.msgType = RT_RTS;
+ break;
+ case 4:
+ if(trRxBit == 1) {
+ dataFrame.msgType = TX_MC_WD;
+ } else {
+ dataFrame.msgType = RX_MC_WD;
+ }
+ break;
+ case 6:
+ break;
+ default:
+ break;
+ }
+
+ //不论发送还是接收,均返回用户消息数据或状态信息
+ //但是注意:对于BC发送数据,只有返回的状态信息有效,数据已不在
+ Bu61580Recv(pbu61580chan, &dataFrame);
+ if (pbu61580chan->PutRxFrame(pbu61580chan->PutRxFrameArg, &dataFrame)) {
+ pbu61580chan->SetBusState(pbu61580chan->SetBusStateArg,_1553_DEV_BUS_ERROR);
+ }
+
+ //继续尝试从发送队列取消息帧发送
+ if (pbu61580chan->GetTxFrame(pbu61580chan->GetTxFrameArg, &dataFrame) == ERROR_NONE) {
+ Bu61580Send(pbu61580chan, &dataFrame);
+ }
+}
+
+/*********************************************************************************************************
+** 函数名称: Bu61580RtIsr
+** 功能描述: bu61580 RT模式中断处理
+** 输 入 : pbu61580chan bu61580 CHAN
+** cmd 控制命令
+** arg 控制参数
+** 输 出 : ERROR or OK
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID Bu61580RtIsr (BU61580_CHAN *pbu61580chan)
+{
+ FRAME_1553DATA dataFrame;
+ union
+ {
+ COMMANDWORD CMDWORD;
+ UINT32 cmdWord;
+ } msgCmdWord;
+
+ if(!pbu61580chan)
+ {
+ return;
+ }
+
+ pbu61580chan->ReadReg(pbu61580chan, INTR_STATUS); //读中断状态寄存器,清除1553中断
+ msgCmdWord.cmdWord = pbu61580chan->ReadReg(pbu61580chan, RT_LAST_CMD_WORD);//获取消息命令字
+
+ dataFrame.frameInfo1.cmdWord1 = msgCmdWord.cmdWord;
+ if(msgCmdWord.CMDWORD.TR == 0) { //接收消息中断
+ Bu61580Recv(pbu61580chan, &dataFrame);
+ if (pbu61580chan->PutRxFrame(pbu61580chan->PutRxFrameArg, &dataFrame)) {
+ pbu61580chan->SetBusState(pbu61580chan->SetBusStateArg,_1553_DEV_BUS_ERROR);
+ }
+ } else { //发送消息中断
+ if (pbu61580chan->GetTxFrame(pbu61580chan->GetTxFrameArg, &dataFrame) == ERROR_NONE) {
+ Bu61580Send(pbu61580chan, &dataFrame);
+ }
+ }
+}
diff --git a/SylixOS/driver/bu61580/bu61580.h b/SylixOS/driver/bu61580/bu61580.h
new file mode 100644
index 0000000..3ab5b2d
--- /dev/null
+++ b/SylixOS/driver/bu61580/bu61580.h
@@ -0,0 +1,67 @@
+/*********************************************************************************************************
+**
+** 中国软件开源组织
+**
+** 嵌入式实时操作系统
+**
+** SylixOS(TM) LW : long wing
+**
+** Copyright All Rights Reserved
+**
+**--------------文件信息--------------------------------------------------------------------------------
+**
+** 文 件 名: bu61580.h
+**
+** 创 建 人: Zhu.Jianhui (朱剑辉)
+**
+** 文件创建日期: 2019 年 08 月 05 日
+**
+** 描 述: 1553B 总线驱动支持.
+*********************************************************************************************************/
+#ifndef __bu61580__
+#define __bu61580__
+
+#include "SylixOS.h"
+
+#define CMD_TXRX_BIT 10
+#define CMD_TXRX_BIT_MASK (1<<10)
+
+typedef struct bu61580_chan BU61580_CHAN;
+
+struct bu61580_chan {
+ DRV_FUNCS_1553 *pDrvFuncs;
+
+ INT channel; //用作启动识别?????????初始化前为1/2,初始化后为0
+ INT busMode; /* BC or RT or MT */
+
+ LW_SPINLOCK_DEFINE (bu61580_splock);
+
+ INT (*SetBusState)();
+ INT (*GetTxFrame)();
+ INT (*PutRxFrame)();
+
+ PVOID SetBusStateArg;
+ PVOID GetTxFrameArg;
+ PVOID PutRxFrameArg;
+
+ void (*WriteMem)(BU61580_CHAN *, UINT memAddr, UINT val); /* write memmory value */
+ UINT (*ReadMem)(BU61580_CHAN *, UINT memAddr); /* read memmory value */
+ void (*WriteReg)(BU61580_CHAN *, UINT regAddr, UINT val); /* write register value */
+ UINT (*ReadReg)(BU61580_CHAN *, UINT regAddr); /* read register value */
+
+ void (*reset)(BU61580_CHAN *);
+ void (*open)(BU61580_CHAN *);
+ void (*close)(BU61580_CHAN *);
+
+ void *priv; /* user can use this save some thing */
+};
+
+/*********************************************************************************************************
+ bu61580 driver functions
+*********************************************************************************************************/
+
+INT Bu61580Init(BU61580_CHAN *pcanchan);
+VOID Bu61580BcIsr(BU61580_CHAN *pcanchan);
+VOID Bu61580RtIsr (BU61580_CHAN *pbu61580chan);
+
+#endif /* __bu61580__ */
diff --git a/SylixOS/system/device/1553B/bus1553.c b/SylixOS/system/device/1553B/bus1553.c
new file mode 100644
index 0000000..caa24be
--- /dev/null
+++ b/SylixOS/system/device/1553B/bus1553.c
@@ -0,0 +1,1050 @@
+/*********************************************************************************************************
+**
+** 中国软件开源组织
+**
+** 嵌入式实时操作系统
+**
+** SylixOS(TM) LW : long wing
+**
+** Copyright All Rights Reserved
+**
+**--------------文件信息--------------------------------------------------------------------------------
+**
+** 文 件 名: bus1553.c
+**
+** 创 建 人: Zhu.Jianhui (朱剑辉)
+**
+** 文件创建日期: 2019 年 08 月 05 日
+**
+** 描 述: 1553B 总线驱动支持.
+*********************************************************************************************************/
+#define __SYLIXOS_KERNEL
+#include "../SylixOS/kernel/include/k_kernel.h"
+#include "../SylixOS/system/include/s_system.h"
+#include "../SylixOS/fs/include/fs_fs.h" /* 需要根文件系统时间 */
+#include "bus1553.h"
+
+
+/*********************************************************************************************************
+ 定义全局变量, 用于保存1553驱动号
+*********************************************************************************************************/
+static INT _G_1553DrvNum = PX_ERROR;
+
+/*********************************************************************************************************
+ 定义 1553 缓冲队列结构体
+*********************************************************************************************************/
+typedef struct {
+ UINT Q_counter; /* 当前缓冲区数据帧个数 */
+ UINT Q_maxFrame; /* 最大数据帧个数 */
+ P1553DATA_FRAME Q_p1553frameIn; /* 入队指针*/
+ P1553DATA_FRAME Q_p1553frameOut; /* 出队指针*/
+ P1553DATA_FRAME Q_p1553frameEnd; /* 队尾 */
+ P1553DATA_FRAME Q_p1553frameBuffer; /* 队头,缓冲区的首地址*/
+} _1553_QUEUE, *P1553_QUEUE;
+
+/*********************************************************************************************************
+ _1553_DEV_WR_STATE
+*********************************************************************************************************/
+typedef struct {
+ BOOL STAT1553_bBufEmpty; /* FIFO 空标志*/
+} _1553_DEV_STATE;
+
+/*********************************************************************************************************
+ 1553 设备结构
+*********************************************************************************************************/
+typedef struct {
+ LW_DEV_HDR _1553_devhdr; /* 设备头*/
+ UINT _1553_channel; /* 1553 通道号 */
+
+ P1553_QUEUE _1553_pRecvQueue; /* 接收队列*/
+ P1553_QUEUE _1553_pSendQueue; /* 发送队列 */
+
+ LW_HANDLE _1553_recvSemB; /* 1553 接收信号*/
+ LW_HANDLE _1553_sendSemB; /* 1553 发送信号*/
+ LW_HANDLE _1553_mutexSemM; /* 互斥信号*/
+
+ _1553_DEV_STATE _1553_readState; /* 读状态 */
+ _1553_DEV_STATE _1553_writeState; /* 写状态 */
+ UINT _1553_busState; /* 总线状态*/
+
+ ULONG _1553_sendTimeout; /* 发送超时时间*/
+ ULONG _1553_recvTimeout; /* 接收超时时间 */
+
+ LW_SEL_WAKEUPLIST _1553_selwulList; /* select() 等待链 */
+
+ LW_SPINLOCK_DEFINE (_1553_spLock); /* 自旋锁 */
+} _1553_DEV;
+
+typedef struct {
+ _1553_DEV PORT_1553Dev; /* 1553 设备 */
+ CHAN_1553 *PORT_1553Chan; /* 1553 设备通道*/
+} _1553_PORT, *P1553_PORT;
+
+/*********************************************************************************************************
+ 1553 LOCK
+*********************************************************************************************************/
+#define PORT1553_LOCK(p1553port) \
+ API_SemaphoreMPend(p1553port->PORT_1553Dev._1553_mutexSemM, LW_OPTION_WAIT_INFINITE)
+#define PORT1553_UNLOCK(p1553port) \
+ API_SemaphoreMPost(p1553port->PORT_1553Dev._1553_mutexSemM)
+
+#define DEV1553_LOCK(p1553dev) \
+ API_SemaphoreMPend(p1553dev->_1553_mutexSemM, LW_OPTION_WAIT_INFINITE)
+#define DEV1553_UNLOCK(p1553dev) \
+ API_SemaphoreMPost(p1553dev->_1553_mutexSemM)
+
+/*********************************************************************************************************
+** 函数名称: __1553InitQueue
+** 功能描述: 缓冲初始化
+** 输 入 : uiMaxFrame 缓冲区 1553帧个数
+** 输 出 : 缓冲区指针
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static P1553_QUEUE __1553InitQueue (UINT uiMaxFrame)
+{
+ size_t stAllocSize;
+ _1553_QUEUE *p1553queue = NULL;
+
+ stAllocSize = (size_t)(sizeof(_1553_QUEUE) + (uiMaxFrame * sizeof(FRAME_1553DATA)));
+ p1553queue = (_1553_QUEUE *)__SHEAP_ALLOC(stAllocSize);
+ if (p1553queue == LW_NULL) {
+ return (LW_NULL);
+ }
+
+ p1553queue->Q_counter = 0;
+ p1553queue->Q_maxFrame = uiMaxFrame;
+ p1553queue->Q_p1553frameBuffer = (P1553DATA_FRAME)(p1553queue + 1);
+ p1553queue->Q_p1553frameIn = p1553queue->Q_p1553frameBuffer;
+ p1553queue->Q_p1553frameOut = p1553queue->Q_p1553frameBuffer;
+ p1553queue->Q_p1553frameEnd = p1553queue->Q_p1553frameBuffer + p1553queue->Q_maxFrame;
+
+ return (p1553queue);
+}
+/*********************************************************************************************************
+** 函数名称: __1553WriteQueue
+** 功能描述: 向队列中写入数据
+** 输 入 :
+** p1553dev 指向设备结构
+** p1553queue 队列指针
+** p1553frame 指向要写入数据的指针
+** iNumber 要写入的个数
+** 输 出 : 实际写入的个数
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553WriteQueue (_1553_DEV *p1553dev,
+ P1553_QUEUE p1553queue,
+ P1553DATA_FRAME p1553frame,
+ INT iNumber)
+{
+ INT i = 0;
+ INTREG iregInterLevel;
+
+ while (iNumber) {
+ /*
+ * 关中断,将关中断放在这里是为了关中断时间可预测
+ */
+ LW_SPIN_LOCK_QUICK(&p1553dev->_1553_spLock, &iregInterLevel);
+ if (p1553queue->Q_counter < p1553queue->Q_maxFrame) { /* 更新接收队列*/
+ p1553queue->Q_counter++;
+ p1553queue->Q_p1553frameIn[0] = *p1553frame;
+ p1553queue->Q_p1553frameIn++;
+ if (p1553queue->Q_p1553frameIn >= p1553queue->Q_p1553frameEnd) {
+ p1553queue->Q_p1553frameIn = p1553queue->Q_p1553frameBuffer;
+ }
+ iNumber--;
+ p1553frame++;
+ i++;
+ } else {
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ break;
+ }
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ }
+
+ return (i);
+}
+/*********************************************************************************************************
+** 函数名称: __1553ReadQueue
+** 功能描述: 向队列中写入数据
+** 输 入 :
+** p1553dev 指向设备结构
+** p1553queue 队列指针
+** p1553frame 指向要读出数据的指针
+** iNumber 要读出的个数
+** 输 出 : 实际读出的个数
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553ReadQueue (_1553_DEV *p1553dev,
+ P1553_QUEUE p1553queue,
+ P1553DATA_FRAME p1553frame,
+ INT iNumber)
+{
+ INT i = 0;
+ INTREG iregInterLevel;
+
+ while (iNumber) {
+ /*
+ * 关中断,将关中断放在这里是为了关中断时间可预测
+ */
+ LW_SPIN_LOCK_QUICK(&p1553dev->_1553_spLock, &iregInterLevel);
+ if (p1553queue->Q_counter > 0) {
+ p1553queue->Q_counter--;
+ *p1553frame = p1553queue->Q_p1553frameOut[0];
+ p1553queue->Q_p1553frameOut++;
+ if (p1553queue->Q_p1553frameOut == p1553queue->Q_p1553frameEnd) {
+ p1553queue->Q_p1553frameOut = p1553queue->Q_p1553frameBuffer;
+ }
+ iNumber--;
+ p1553frame++;
+ i++;
+ } else {
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ break;
+ }
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ }
+
+ return (i);
+}
+/*********************************************************************************************************
+** 函数名称: __1553QFreeNum
+** 功能描述: 获取队列中空闲数量
+** 输 入 : p1553queue 队列指针
+** 输 出 : 空闲数量
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553QFreeNum (P1553_QUEUE p1553queue)
+{
+
+ REGISTER INT iNum;
+
+ iNum = p1553queue->Q_maxFrame - p1553queue->Q_counter;
+
+ return (iNum);
+}
+/*********************************************************************************************************
+** 函数名称: __1553QCount
+** 功能描述: 队列中消息的数量
+** 输 入 :
+** p1553queue 队列指针
+** 输 出 : 队列中有用数据的个数
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553QCount (P1553_QUEUE p1553queue)
+{
+ return (p1553queue->Q_counter);
+}
+/*********************************************************************************************************
+** 函数名称: __1553FlushQueue
+** 功能描述: 清空队列
+** 输 入 :
+** p1553Queue 队列指针
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __1553FlushQueue (P1553_QUEUE p1553queue)
+{
+ p1553queue->Q_counter = 0;
+ p1553queue->Q_p1553frameIn = p1553queue->Q_p1553frameBuffer;
+ p1553queue->Q_p1553frameOut = p1553queue->Q_p1553frameBuffer;
+}
+/*********************************************************************************************************
+** 函数名称: __1553DeleteQueue
+** 功能描述: 删除队列
+** 输 入 :
+** p1553queue 队列指针
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __1553DeleteQueue (P1553_QUEUE p1553queue)
+{
+ __SHEAP_FREE((PVOID)p1553queue);
+}
+
+/*********************************************************************************************************
+** 函数名称: __1553FlushRd
+** 功能描述: 清除1553设备读缓冲区
+** 输 入 : p1553port 1553设备
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __1553FlushRd (_1553_PORT *p1553port)
+{
+ INTREG iregInterLevel;
+
+ PORT1553_LOCK(p1553port); /* 等待设备使用权*/
+
+ LW_SPIN_LOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, &iregInterLevel);
+ __1553FlushQueue(p1553port->PORT_1553Dev._1553_pRecvQueue); /* 清除缓冲区*/
+ LW_SPIN_UNLOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, iregInterLevel);
+
+ API_SemaphoreBClear(p1553port->PORT_1553Dev._1553_recvSemB); /* 清除读同步*/
+
+ PORT1553_UNLOCK(p1553port); /* 释放设备使用权*/
+}
+
+/*********************************************************************************************************
+** 函数名称: __1553FlushWrt
+** 功能描述: 清除1553 设备写缓冲区
+** 输 入 : p1553port 1553 设备
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __1553FlushWrt (_1553_PORT *p1553port)
+{
+ INTREG iregInterLevel;
+
+ PORT1553_LOCK(p1553port); /* 等待设备使用权*/
+
+ LW_SPIN_LOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, &iregInterLevel);
+ __1553FlushQueue(p1553port->PORT_1553Dev._1553_pSendQueue); /* 清除缓冲区*/
+ p1553port->PORT_1553Dev._1553_writeState.STAT1553_bBufEmpty = LW_TRUE; /* 发送队列空*/
+ LW_SPIN_UNLOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, iregInterLevel);
+
+ API_SemaphoreBPost(p1553port->PORT_1553Dev._1553_sendSemB); /* 通知线程可写*/
+
+ PORT1553_UNLOCK(p1553port); /* 释放设备使用权*/
+
+ SEL_WAKE_UP_ALL(&p1553port->PORT_1553Dev._1553_selwulList, SELWRITE); /* 通知 select 线程可写*/
+}
+
+/*********************************************************************************************************
+** 函数名称: __1553GetFrameFromTxQueue
+** 功能描述: 从发送缓冲区中读出一帧数据
+** 输 入 :
+** p1553dev 1553 设备
+** p1553frame 指向待读出的数据
+** 输 出 : ERROR_NONE or PX_ERROR
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553GetFrameFromTxQueue (_1553_DEV *p1553dev, P1553DATA_FRAME p1553frame)
+{
+ INTREG iregInterLevel;
+ INT iTemp = 0;
+
+ if (!p1553dev || !p1553frame) {
+ return (PX_ERROR);
+ }
+
+ iTemp = __1553ReadQueue(p1553dev,
+ p1553dev->_1553_pSendQueue,
+ p1553frame, 1); /* 从发送队列中读取一帧数据*/
+
+ LW_SPIN_LOCK_QUICK(&p1553dev->_1553_spLock, &iregInterLevel);
+ if (iTemp <= 0) {
+ p1553dev->_1553_writeState.STAT1553_bBufEmpty = LW_TRUE; /* 发送队列空*/
+ }
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+
+ API_SemaphoreBPost(p1553dev->_1553_sendSemB); /* 释放信号量*/
+ SEL_WAKE_UP_ALL(&p1553dev->_1553_selwulList, SELWRITE); /* 释放所有等待写的线程*/
+
+ return ((iTemp) ? (ERROR_NONE) : (PX_ERROR));
+}
+/*********************************************************************************************************
+** 函数名称: __1553PutFrameToRxQueue
+** 功能描述: 向接收缓冲区中写入一帧数据
+** 输 入 :
+** p1553dev 1553 设备
+** p1553frame 指向待写入的数据
+** 输 出 : ERROR_NONE or PX_ERROR
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553PutFrameToRxQueue (_1553_DEV *p1553dev, P1553DATA_FRAME p1553frame)
+{
+ INT iTemp = 0;
+
+ if (!p1553dev || !p1553frame) {
+ return (PX_ERROR);
+ }
+
+ iTemp = __1553WriteQueue(p1553dev,
+ p1553dev->_1553_pRecvQueue,
+ p1553frame, 1); /* 往接收队列中写入一帧数据 */
+
+ API_SemaphoreBPost(p1553dev->_1553_recvSemB); /* 释放信号量*/
+ SEL_WAKE_UP_ALL(&p1553dev->_1553_selwulList, SELREAD); /* select() 激活*/
+
+ return ((iTemp) ? (ERROR_NONE) : (PX_ERROR));
+}
+/*********************************************************************************************************
+** 函数名称: __1553TxStartup
+** 功能描述: 启动发送函数
+** 输 入 : p1553port 1553 通道
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __1553TxStartup (_1553_PORT *p1553port)
+{
+ INTREG iregInterLevel;
+
+ if (p1553port->PORT_1553Dev._1553_writeState.STAT1553_bBufEmpty == LW_TRUE) {
+ LW_SPIN_LOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, &iregInterLevel); /* 关闭中断*/
+ if (p1553port->PORT_1553Dev._1553_writeState.STAT1553_bBufEmpty == LW_TRUE) {
+ p1553port->PORT_1553Dev._1553_writeState.STAT1553_bBufEmpty = LW_FALSE;
+ LW_SPIN_UNLOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, iregInterLevel); /* 打开中断*/
+ p1553port->PORT_1553Chan->pDrvFuncs->txStartup(p1553port->PORT_1553Chan);/* 启动发送*/
+ return;
+ }
+ LW_SPIN_UNLOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, iregInterLevel);/* 打开中断*/
+ }
+}
+/*********************************************************************************************************
+** 函数名称: __1553SetBusState
+** 功能描述: 设置 1553 设备的总线状态
+** 输 入 :
+** p1553dev 1553 设备
+** iState 总线状态
+** 输 出 : ERROR_NONE or PX_ERROR
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __1553SetBusState (_1553_DEV *p1553dev, INT iState)
+{
+ INTREG iregInterLevel;
+ _1553_PORT *p1553port = (_1553_PORT *)p1553dev;////////有这个必要吗????下面全是对dev操作
+
+ LW_SPIN_LOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, &iregInterLevel);
+ if (iState) {
+ p1553dev->_1553_busState |= iState;
+ } else {
+ p1553dev->_1553_busState = _1553_DEV_BUS_OK;
+ }
+ LW_SPIN_UNLOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, iregInterLevel);
+
+ if (p1553dev->_1553_busState != _1553_DEV_BUS_OK) { /* 总线异常*/
+ API_SemaphoreBFlush(p1553dev->_1553_sendSemB, LW_NULL); /* 激活写等待任务*/
+ API_SemaphoreBFlush(p1553dev->_1553_recvSemB, LW_NULL); /* 激活读等待任务*/
+ SEL_WAKE_UP_ALL(&p1553dev->_1553_selwulList, SELEXCEPT); /* select() 激活*/
+
+ //////////总线异常需要复位吗?///////////////
+ }
+}
+
+/*********************************************************************************************************
+** 函数名称: __1553DevInit
+** 功能描述: 创建1553 设备
+** 输 入 :
+** p1553dev 1553设备
+** uiRdFrameSize 接收缓冲区大小
+** uiWrtFrameSize 发送缓冲区大小
+** 输 出 : ERROR_NONE or PX_ERROR
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553DevInit (_1553_DEV *p1553dev,
+ UINT uiRdFrameSize,
+ UINT uiWrtFrameSize)
+{
+ REGISTER INT iErrLevel = 0;
+
+ p1553dev->_1553_sendTimeout = LW_OPTION_WAIT_INFINITE; /* 初始化为永久等待 */
+ p1553dev->_1553_recvTimeout = LW_OPTION_WAIT_INFINITE; /* 初始化为永久等待*/
+
+ p1553dev->_1553_pRecvQueue = __1553InitQueue(uiRdFrameSize); /* 创建读缓冲区*/
+ if (p1553dev->_1553_pRecvQueue == LW_NULL) { /* 创建失败*/
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "system low memory.\r\n");
+ _ErrorHandle(ERROR_SYSTEM_LOW_MEMORY);
+ return (PX_ERROR);
+ }
+
+ p1553dev->_1553_pSendQueue = __1553InitQueue(uiWrtFrameSize); /* 创建写缓冲区*/
+ if (p1553dev->_1553_pSendQueue == LW_NULL) { /* 创建失败*/
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "system low memory.\r\n");
+ _ErrorHandle(ERROR_SYSTEM_LOW_MEMORY);
+ iErrLevel = 1;
+ goto __error_handle;
+ }
+ p1553dev->_1553_writeState.STAT1553_bBufEmpty = LW_TRUE; /* 发送队列空*/
+ p1553dev->_1553_busState = _1553_DEV_BUS_OK; /* 发送队列空*/
+ p1553dev->_1553_recvSemB = API_SemaphoreBCreate("1553_rsync",
+ LW_FALSE,
+ LW_OPTION_WAIT_PRIORITY | LW_OPTION_OBJECT_GLOBAL,
+ LW_NULL); /* 读同步*/
+ if (!p1553dev->_1553_recvSemB) {
+ iErrLevel = 2;
+ goto __error_handle;
+ }
+
+ p1553dev->_1553_sendSemB = API_SemaphoreBCreate("1553_wsync",
+ LW_TRUE,
+ LW_OPTION_WAIT_PRIORITY | LW_OPTION_OBJECT_GLOBAL,
+ LW_NULL); /* 写同步*/
+ if (!p1553dev->_1553_sendSemB) {
+ iErrLevel = 3;
+ goto __error_handle;
+ }
+
+ p1553dev->_1553_mutexSemM = API_SemaphoreMCreate("1553_lock",
+ LW_PRIO_DEF_CEILING,
+ LW_OPTION_WAIT_PRIORITY |
+ LW_OPTION_DELETE_SAFE |
+ LW_OPTION_INHERIT_PRIORITY |
+ LW_OPTION_OBJECT_GLOBAL,
+ LW_NULL); /* 互斥访问控制信号量*/
+ if (!p1553dev->_1553_mutexSemM) {
+ iErrLevel = 4;
+ goto __error_handle;
+ }
+
+ SEL_WAKE_UP_LIST_INIT(&p1553dev->_1553_selwulList); /* 初始化 select 等待链*/
+
+ LW_SPIN_INIT(&p1553dev->_1553_spLock); /* 初始化自旋锁*/
+
+ return (ERROR_NONE);
+
+__error_handle:
+ if (iErrLevel > 3) {
+ API_SemaphoreBDelete(&p1553dev->_1553_sendSemB); /* 删除写同步 */
+ }
+ if (iErrLevel > 2) {
+ API_SemaphoreBDelete(&p1553dev->_1553_recvSemB); /* 删除读同步*/
+ }
+ if (iErrLevel > 1) {
+ __1553DeleteQueue(p1553dev->_1553_pSendQueue); /* 删除读缓冲区*/
+ }
+ if (iErrLevel > 0) {
+ __1553DeleteQueue(p1553dev->_1553_pRecvQueue); /* 删除写缓冲区*/
+ }
+ return (PX_ERROR);
+}
+
+/*********************************************************************************************************
+** 函数名称: _1553DevDelete
+** 功能描述: 删除1553 设备资源
+** 输 入 : p1553Dev 1553 设备
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __1553DevDelete (_1553_DEV *p1553Dev)
+{
+ __1553DeleteQueue(p1553Dev->_1553_pRecvQueue);
+ __1553DeleteQueue(p1553Dev->_1553_pSendQueue);
+
+ API_SemaphoreBDelete(&p1553Dev->_1553_recvSemB);
+ API_SemaphoreBDelete(&p1553Dev->_1553_sendSemB);
+ API_SemaphoreMDelete(&p1553Dev->_1553_mutexSemM);
+}
+
+/*********************************************************************************************************
+** 函数名称: __1553Ioctl
+** 功能描述: 1553 设备控制
+** 输 入 :
+** p1553dev 1553 设备
+** iCmd 控制命令
+** lArg 参数
+** 输 出 : ERROR_NONE or PX_ERROR
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553Ioctl (_1553_DEV *p1553dev, INT iCmd, LONG lArg)
+{
+ INTREG iregInterLevel;
+ INT iStatus = ERROR_NONE;
+ struct stat *pstat;
+ PLW_SEL_WAKEUPNODE pselwunNode;
+
+ struct timeval *timevalTemp;
+
+ _1553_PORT *p1553port = (_1553_PORT *)p1553dev;
+ DRV_FUNCS_1553 *p1553DevFuncs = p1553port->PORT_1553Chan->pDrvFuncs;
+
+ DEV1553_LOCK(p1553dev); /* 等待设备使用权*/
+
+ if (p1553DevFuncs->ioctl) {
+ iStatus = p1553DevFuncs->ioctl(p1553port->PORT_1553Chan, iCmd, (PVOID)lArg);
+
+ } else {
+ iStatus = ENOSYS;
+ }
+
+ if ((iStatus == ENOSYS) ||
+ ((iStatus == PX_ERROR) && (errno == ENOSYS))) { /* 驱动程序无法识别的命令*/
+
+ iStatus = ERROR_NONE; /* 清除驱动程序错误*/
+
+ switch (iCmd) {
+
+ case FIONREAD: /* 读缓冲区有效数据数量 */
+ {
+ LONG lNFrame = __1553QCount(p1553dev->_1553_pRecvQueue);
+ *((INT *)lArg) = (INT)(lNFrame * sizeof(FRAME_1553DATA));
+ }
+ break;
+
+ case FIONWRITE:
+ {
+ LONG lNFrame = __1553QCount(p1553dev->_1553_pSendQueue);
+ *((INT *)lArg) = (INT)(lNFrame * sizeof(FRAME_1553DATA));
+ }
+ break;
+
+ case FIOFLUSH: /* 清空设备缓冲区*/
+ __1553FlushRd(p1553port);
+ __1553FlushWrt(p1553port);
+ break;
+
+ case FIOWFLUSH:
+ __1553FlushRd(p1553port); /* 清空写缓冲区*/
+ break;
+
+ case FIORFLUSH:
+ __1553FlushWrt(p1553port); /* 清空读缓冲区*/
+ break;
+
+ case FIOFSTATGET: /* 获得文件属性*/
+ pstat = (struct stat *)lArg;
+ pstat->st_dev = (dev_t)p1553dev;
+ pstat->st_ino = (ino_t)0; /* 相当于唯一节点*/
+ pstat->st_mode = 0666 | S_IFCHR; /* 默认属性*/
+ pstat->st_nlink = 1;
+ pstat->st_uid = 0;
+ pstat->st_gid = 0;
+ pstat->st_rdev = 1;
+ pstat->st_size = 0;
+ pstat->st_blksize = 0;
+ pstat->st_blocks = 0;
+ pstat->st_atime = API_RootFsTime(LW_NULL); /* 默认使用 root fs 基准时间*/
+ pstat->st_mtime = API_RootFsTime(LW_NULL);
+ pstat->st_ctime = API_RootFsTime(LW_NULL);
+ break;
+
+ case FIOSELECT:
+ pselwunNode = (PLW_SEL_WAKEUPNODE)lArg;
+ SEL_WAKE_NODE_ADD(&p1553dev->_1553_selwulList, pselwunNode);
+
+ switch (pselwunNode->SELWUN_seltypType) {
+
+ case SELREAD: /* 等待数据可读*/
+ if (__1553QCount(p1553dev->_1553_pRecvQueue) > 0) {
+ SEL_WAKE_UP(pselwunNode); /* 唤醒节点*/
+ }
+ break;
+
+ case SELWRITE:
+ if (__1553QFreeNum(p1553dev->_1553_pSendQueue) > 0) {
+ SEL_WAKE_UP(pselwunNode); /* 唤醒节点*/
+ }
+ break;
+
+ case SELEXCEPT: /* 总线是否异常*/
+ LW_SPIN_LOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, &iregInterLevel);
+ if (p1553dev->_1553_busState != _1553_DEV_BUS_OK) {
+ LW_SPIN_UNLOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, iregInterLevel);
+ SEL_WAKE_UP(pselwunNode); /* 唤醒节点*/
+ } else {
+ LW_SPIN_UNLOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, iregInterLevel);
+ }
+ break;
+ }
+ break;
+
+ case FIOUNSELECT:
+ SEL_WAKE_NODE_DELETE(&p1553dev->_1553_selwulList, (PLW_SEL_WAKEUPNODE)lArg);
+ break;
+
+ case _1553_DEV_GET_BUS_STATE: /* 获取 1553 控制器状态*/
+ LW_SPIN_LOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, &iregInterLevel);
+ *((LONG *)lArg) = p1553dev->_1553_busState;
+ p1553dev->_1553_busState = _1553_DEV_BUS_OK; /* 读取后清除状态*/
+ LW_SPIN_UNLOCK_QUICK(&p1553port->PORT_1553Dev._1553_spLock, iregInterLevel);
+ break;
+
+ case FIOWTIMEOUT:
+ if (lArg) {
+ timevalTemp = (struct timeval *)lArg;
+ p1553dev->_1553_sendTimeout = __timevalToTick(timevalTemp); /* 转换为系统时钟*/
+ } else {
+ p1553dev->_1553_sendTimeout = LW_OPTION_WAIT_INFINITE;
+ }
+ break;
+
+ case FIORTIMEOUT:
+ if (lArg) {
+ timevalTemp = (struct timeval *)lArg;
+ p1553dev->_1553_recvTimeout = __timevalToTick(timevalTemp); /* 转换为系统时钟*/
+ } else {
+ p1553dev->_1553_recvTimeout = LW_OPTION_WAIT_INFINITE;
+ }
+ break;
+
+ default:
+ _ErrorHandle(ERROR_IO_UNKNOWN_REQUEST);
+ iStatus = PX_ERROR;
+ break;
+ }
+ }
+
+ DEV1553_UNLOCK(p1553dev); /* 释放设备使用权*/
+
+ return (iStatus);
+}
+/*********************************************************************************************************
+** 函数名称: __1553Open
+** 功能描述: 1553 设备打开
+** 输 入 :
+** p1553dev 1553设备
+** pcName 设备名称
+** iFlags 打开设备时使用的标志
+** iMode 打开的方式,保留
+** 输 出 : 1553 设备指针
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static LONG __1553Open (_1553_DEV *p1553dev,
+ PCHAR pcName,
+ INT iFlags,
+ INT iMode)
+{
+ _1553_PORT *p1553port = (_1553_PORT *)p1553dev;
+
+ if (LW_DEV_INC_USE_COUNT(&p1553dev->_1553_devhdr) == 1) {
+ if (p1553port->PORT_1553Chan->pDrvFuncs->ioctl) { /* 打开端口*/
+ p1553port->PORT_1553Chan->pDrvFuncs->ioctl(p1553port->PORT_1553Chan, _1553_DEV_OPEN, LW_NULL);
+ }
+ }
+
+ return ((LONG)p1553dev);
+}
+/*********************************************************************************************************
+** 函数名称: __1553Close
+** 功能描述: 1553 设备关闭
+** 输 入 : p1553dev 1553设备
+** 输 出 : ERROR_NONE or PX_ERROR
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __1553Close (_1553_DEV *p1553dev)
+{
+ _1553_PORT *p1553port = (_1553_PORT *)p1553dev;
+
+ if (LW_DEV_GET_USE_COUNT(&p1553dev->_1553_devhdr)) {
+ if (!LW_DEV_DEC_USE_COUNT(&p1553dev->_1553_devhdr)) {
+ if (p1553port->PORT_1553Chan->pDrvFuncs->ioctl) { /* 挂起端口*/
+ p1553port->PORT_1553Chan->pDrvFuncs->ioctl(p1553port->PORT_1553Chan, _1553_DEV_CLOSE, LW_NULL);
+ }
+ SEL_WAKE_UP_ALL(&p1553dev->_1553_selwulList, SELEXCEPT); /* 激活异常等待*/
+ }
+ }
+
+ return (ERROR_NONE);
+}
+/*********************************************************************************************************
+** 函数名称: __1553Write
+** 功能描述: 写 1553设备
+** 输 入 :
+** p1553dev 1553 设备
+** p1553frame 写缓冲区指针
+** stNBytes 发送缓冲区字节数
+** 输 出 : 返回实际写入的个数
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static ssize_t __1553Write (_1553_DEV *p1553dev,
+ P1553DATA_FRAME p1553frame,
+ size_t stNBytes)
+{
+
+ INTREG iregInterLevel;
+ INT iFrameput;
+ INT i = 0;
+ ULONG ulError;
+ _1553_PORT *p1553port = (_1553_PORT *)p1553dev;
+ size_t stNumber = stNBytes / sizeof(FRAME_1553DATA); /* 转换为数据包个数*/
+
+ while (stNumber > 0) {
+ ulError = API_SemaphoreBPend(p1553dev->_1553_sendSemB,
+ p1553dev->_1553_sendTimeout);
+ if (ulError) {
+ _ErrorHandle(ERROR_IO_DEVICE_TIMEOUT); /* 超时*/
+ return ((ssize_t)(i * sizeof(FRAME_1553DATA)));
+ }
+
+ DEV1553_LOCK(p1553dev); /* 等待设备使用权*/
+
+ LW_SPIN_LOCK_QUICK(&p1553dev->_1553_spLock, &iregInterLevel);
+ if (p1553dev->_1553_busState != _1553_DEV_BUS_OK) { /* 总线错误*/
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ DEV1553_UNLOCK(p1553dev);
+ _ErrorHandle(EIO);
+ return ((ssize_t)(i * sizeof(FRAME_1553DATA)));
+ }
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+
+ iFrameput = __1553WriteQueue(p1553dev,
+ p1553dev->_1553_pSendQueue,
+ p1553frame,
+ (INT)stNumber);
+
+ __1553TxStartup(p1553port); /* 启动一次发送 */
+
+ stNumber -= (size_t)iFrameput; /* 剩余需要发送的数据*/
+ p1553frame += iFrameput; /* 新的缓冲区起始地址*/
+ i += iFrameput;
+
+ LW_SPIN_LOCK_QUICK(&p1553dev->_1553_spLock, &iregInterLevel); /* 关闭中断*/
+ if (__1553QFreeNum(p1553dev->_1553_pSendQueue) > 0) {
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel); /* 打开中断*/
+ API_SemaphoreBPost(p1553dev->_1553_sendSemB); /* 缓冲区还有空间*/
+ } else {
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel); /* 打开中断*/
+ }
+
+ DEV1553_UNLOCK(p1553dev); /* 释放设备使用权*/
+ }
+
+ return ((ssize_t)(i * sizeof(FRAME_1553DATA)));
+}
+/*********************************************************************************************************
+** 函数名称: __1553Read
+** 功能描述: 读 1553 设备
+** 输 入 :
+** p1553dev 1553设备
+** p1553frame 1553发送缓冲区指针
+** stNBytes 读取缓冲区的字节数
+** 输 出 : 返回实际读取的个数
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static ssize_t __1553Read (_1553_DEV *p1553dev,
+ P1553DATA_FRAME p1553frame,
+ size_t stNBytes)
+{
+ INTREG iregInterLevel;
+ REGISTER ssize_t sstNRead;
+ size_t stNumber = stNBytes / sizeof(FRAME_1553DATA); /* 转换为数据包个数*/
+ ULONG ulError;
+
+ for (;;) {
+ ulError = API_SemaphoreBPend(p1553dev->_1553_recvSemB, p1553dev->_1553_recvTimeout);
+ if (ulError) {
+ _ErrorHandle(ERROR_IO_DEVICE_TIMEOUT); /* 超时*/
+ return (0);
+ }
+
+ DEV1553_LOCK(p1553dev); /* 等待设备使用权*/
+
+ LW_SPIN_LOCK_QUICK(&p1553dev->_1553_spLock, &iregInterLevel); /* 关闭中断*/
+ if (__1553QCount(p1553dev->_1553_pRecvQueue)) { /* 检查是否有数据*/
+ break;
+ } else {
+ if (p1553dev->_1553_busState != _1553_DEV_BUS_OK) { /* 总线错误*/
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ DEV1553_UNLOCK(p1553dev);
+ _ErrorHandle(EIO);
+ return (0);
+ }
+ }
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel); /* 打开中断*/
+ DEV1553_UNLOCK(p1553dev); /* 释放设备使用权*/
+ }
+
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ sstNRead = __1553ReadQueue(p1553dev,
+ p1553dev->_1553_pRecvQueue,
+ p1553frame,
+ (INT)stNumber);
+ LW_SPIN_LOCK_QUICK(&p1553dev->_1553_spLock, &iregInterLevel);
+
+ if (__1553QCount(p1553dev->_1553_pRecvQueue)) { /* 是否还有数据*/
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ API_SemaphoreBPost(p1553dev->_1553_recvSemB); /* 通知其他等待读的线程*/
+ } else {
+ LW_SPIN_UNLOCK_QUICK(&p1553dev->_1553_spLock, iregInterLevel);
+ }
+
+ DEV1553_UNLOCK(p1553dev);/* 释放设备使用权*/
+
+ return (sstNRead * sizeof(FRAME_1553DATA));
+}
+
+/*********************************************************************************************************
+** 函数名称: API_1553DrvInstall
+** 功能描述: 安装1553驱动程序
+** 输 入 : NONE
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+** API 函数
+*********************************************************************************************************/
+LW_API
+INT API_1553DrvInstall (void)
+{
+ if (_G_1553DrvNum > 0) {
+ return (ERROR_NONE);
+ }
+
+ _G_1553DrvNum = iosDrvInstall(__1553Open, LW_NULL, __1553Open, __1553Close,
+ __1553Read, __1553Write, __1553Ioctl);
+
+ DRIVER_LICENSE(_G_1553DrvNum, "GPL->Ver 2.0");
+ DRIVER_AUTHOR(_G_1553DrvNum, "Jianhui.Zhu");
+ DRIVER_DESCRIPTION(_G_1553DrvNum, "1553 Bus driver.");
+
+ return (_G_1553DrvNum > 0) ? (ERROR_NONE) : (PX_ERROR);
+}
+/*********************************************************************************************************
+** 函数名称: API_1553DevCreate
+** 功能描述: 创建 1553设备
+** 输 入 : pcName 设备名
+** pc1553chan 通道
+** uiRdFrameSize 接收缓冲区大小
+** uiWrtFrameSize 发送缓冲区大小
+** 输 出 : 是否成功
+** 全局变量:
+** 调用模块:
+** API 函数
+*********************************************************************************************************/
+LW_API
+INT API_1553DevCreate (PCHAR pcName,
+ CHAN_1553 *p1553chan,
+ UINT uiRdFrameSize,
+ UINT uiWrtFrameSize)
+{
+ P1553_PORT p1553port;
+ INT iTemp;
+
+ if ((!pcName) || (!p1553chan)) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "parameter error.\r\n");
+ _ErrorHandle(EINVAL);
+ return (PX_ERROR);
+ }
+
+ if ((uiRdFrameSize < 1) || (uiWrtFrameSize < 1)) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "parameter error.\r\n");
+ _ErrorHandle(EINVAL);
+ return (PX_ERROR);
+ }
+
+ if ((!p1553chan->pDrvFuncs->callbackInstall) ||
+ (!p1553chan->pDrvFuncs->txStartup)) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "parameter error.\r\n");
+ _ErrorHandle(EINVAL);
+ return (PX_ERROR);
+ }
+
+ if (_G_1553DrvNum <= 0) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "1553 Driver invalidate.\r\n");
+ _ErrorHandle(ERROR_IO_NO_DRIVER);
+ return (PX_ERROR);
+ }
+
+ p1553port = (P1553_PORT)__SHEAP_ALLOC(sizeof(_1553_PORT));
+ if (!p1553port) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "system low memory.\r\n");
+ _ErrorHandle(ERROR_SYSTEM_LOW_MEMORY);
+ return (PX_ERROR);
+ }
+ lib_bzero(p1553port, sizeof(_1553_PORT));
+
+ iTemp = __1553DevInit(&p1553port->PORT_1553Dev,
+ uiRdFrameSize,
+ uiWrtFrameSize); /* 初始化设备控制块 */
+
+ if (iTemp != ERROR_NONE) {
+ __SHEAP_FREE(p1553port);
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "system low memory.\r\n");
+ _ErrorHandle(ERROR_SYSTEM_LOW_MEMORY);
+ return (PX_ERROR);
+ }
+
+ __1553FlushRd(p1553port);
+ __1553FlushWrt(p1553port);
+
+ p1553port->PORT_1553Chan = p1553chan;
+
+ p1553chan->pDrvFuncs->callbackInstall(p1553chan, _1553_CB_GET_TX_FRAME,
+ (CALLBACK_1553)__1553GetFrameFromTxQueue, (PVOID)p1553port);
+ p1553chan->pDrvFuncs->callbackInstall(p1553chan, _1553_CB_PUT_RX_FRAME,
+ (CALLBACK_1553)__1553PutFrameToRxQueue, (PVOID)p1553port);
+ p1553chan->pDrvFuncs->callbackInstall(p1553chan, _1553_CB_PUT_BUS_STATE,
+ (CALLBACK_1553)__1553SetBusState, (PVOID)p1553port);
+
+ iTemp = (INT)iosDevAddEx(&p1553port->PORT_1553Dev._1553_devhdr,
+ pcName,
+ _G_1553DrvNum,
+ DT_CHR);
+ if (iTemp) {
+ __1553DevDelete(&p1553port->PORT_1553Dev);
+ __SHEAP_FREE(p1553port);
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "add device error.\r\n");
+ _ErrorHandle(ERROR_SYSTEM_LOW_MEMORY);
+ return (PX_ERROR);
+
+ } else {
+ return (ERROR_NONE);
+ }
+}
+/*********************************************************************************************************
+** 函数名称: API_1553DevRemove
+** 功能描述: 移除一个 1553 设备
+** 输 入 :
+** pcName 需要移除的 1553 设备
+** bForce 模式选择
+** 输 出 : ERROR_NONE or PX_ERROR
+** 全局变量:
+** 调用模块:
+** API 函数
+*********************************************************************************************************/
+LW_API
+INT API_1553DevRemove (PCHAR pcName, BOOL bForce)
+{
+ P1553_PORT pDevHdr;
+ PCHAR pcTail = LW_NULL;
+
+ if (LW_CPU_GET_CUR_NESTING()) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n");
+ _ErrorHandle(ERROR_KERNEL_IN_ISR);
+ return (PX_ERROR);
+ }
+
+ if (_G_1553DrvNum <= 0) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "no driver.\r\n");
+ _ErrorHandle(ERROR_IO_NO_DRIVER);
+ return (PX_ERROR);
+ }
+
+ pDevHdr = (P1553_PORT)iosDevFind(pcName, &pcTail);
+ if ((pDevHdr == LW_NULL) || (pcName == pcTail)) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "device not found.\r\n");
+ _ErrorHandle(ERROR_IOS_DEVICE_NOT_FOUND);
+ return (PX_ERROR);
+ }
+
+ if (bForce == LW_FALSE) {
+ if (LW_DEV_GET_USE_COUNT(&pDevHdr->PORT_1553Dev._1553_devhdr)) {
+ _DebugHandle(__ERRORMESSAGE_LEVEL, "too many open files.\r\n");
+ _ErrorHandle(EBUSY);
+ return (PX_ERROR);
+ }
+ if (SEL_WAKE_UP_LIST_LEN(&pDevHdr->PORT_1553Dev._1553_selwulList) > 0) {
+ errno = EBUSY;
+ return (PX_ERROR);
+ }
+ }
+
+ iosDevFileAbnormal(&pDevHdr->PORT_1553Dev._1553_devhdr);
+ iosDevDelete(&pDevHdr->PORT_1553Dev._1553_devhdr);
+
+ SEL_WAKE_UP_LIST_TERM(&pDevHdr->PORT_1553Dev._1553_selwulList);
+
+ __1553DevDelete(&pDevHdr->PORT_1553Dev);
+
+ __SHEAP_FREE((PVOID)pDevHdr);
+
+ return (ERROR_NONE);
+}
+
diff --git a/SylixOS/system/device/1553B/bus1553.h b/SylixOS/system/device/1553B/bus1553.h
new file mode 100644
index 0000000..c1199f2
--- /dev/null
+++ b/SylixOS/system/device/1553B/bus1553.h
@@ -0,0 +1,175 @@
+/*********************************************************************************************************
+**
+** 中国软件开源组织
+**
+** 嵌入式实时操作系统
+**
+** SylixOS(TM) LW : long wing
+**
+** Copyright All Rights Reserved
+**
+**--------------文件信息--------------------------------------------------------------------------------
+**
+** 文 件 名: bus1553.h
+**
+** 创 建 人: Zhu.Jianhui (朱剑辉)
+**
+** 文件创建日期: 2019 年 08 月 05 日
+**
+** 描 述: 1553B 总线驱动支持.
+*********************************************************************************************************/
+#ifndef __bus1553__
+#define __bus1553__
+
+/*********************************************************************************************************
+ 定义驱动安装回调函数时的命令
+*********************************************************************************************************/
+#ifdef __SYLIXOS_KERNEL
+#define _1553_CB_GET_TX_FRAME 1 /* 安装发送数据时的回调 */
+#define _1553_CB_PUT_RX_FRAME 2 /* 安装接收数据时的回调 */
+#define _1553_CB_PUT_BUS_STATE 3 /* 安装总线状态改变时的回调 */
+#endif /* __SYLIXOS_KERNEL */
+
+/*********************************************************************************************************
+ 总线状态宏定义
+*********************************************************************************************************/
+#define _1553_DEV_BUS_OK 0x0000 /* 正常状态*/
+#define _1553_DEV_BUS_ERROR 0x0001 /* 错误状态*/
+
+#ifdef __SYLIXOS_KERNEL
+#define _1553_DEV_OPEN LW_OSIO('c', 201) /* 1553 设备打开命令*/
+#define _1553_DEV_CLOSE LW_OSIO('c', 202) /* 1553 设备关闭命令 */
+#endif /* __SYLIXOS_KERNEL */
+
+#define _1553_DEV_GET_BUS_STATE LW_OSIOR('c', 203, LONG) /* 获取1553总线状态*/
+#define _1553_DEV_REST_BUS LW_OSIO( 'c', 204) /* 复位 1553总线 控制器*/
+#define _1553_DEV_STARTUP LW_OSIO( 'c', 205) /* 启动 1553总线 控制器 */
+#define _1553_DEV_SET_MODE LW_OSIOD('c', 206, INT) /* 设置15553总线模式*/
+
+//msg type ,仅限于BC 模式用
+#define BC_RT 0 // 0
+#define RT_BC 1 // 0
+#define RT_RT 2 // 1
+#define BCST 3 // 2 broadcast
+#define RT_RTS 4 // 3
+#define MC_ND 5 // 4 mode code no data
+#define TX_MC_WD 6 // 4 mode code with data
+#define RX_MC_WD 7 // 4 mode code with data
+#define MCND_BCST 8 // 6 mode code broadcast
+#define MCWD_BCST 9 // 6 mode code broadcast
+
+//msg type ,仅限于RT 模式用
+#define BC_TO_RT 0
+#define RT_TO_BC 1
+#define BROADCAST 2
+#define MODE_CODE 3
+
+#define TX 1
+#define RX 0
+
+typedef enum {BC_MOD, RT_MOD, MT_MOD,INIT_MOD} BUSMOD;
+
+#define CHANNEL_A 1
+#define CHANNEL_B 0
+
+//1553B寄存器片内偏移地址
+#define INTRMASK 0x0000
+#define CONFIG1 0x0001
+#define CONFIG2 0x0002
+#define STR_RST 0x0003 //WR
+#define CMD_STK_POINT 0x0003 //RD
+#define RT_SA_CTRL_WORD 0x0004
+#define BC_CTRL_WORD 0x0004
+#define TIMETAG 0x0005
+#define INTR_STATUS 0x0006
+#define CONFIG3 0x0007
+#define CONFIG4 0x0008
+#define CONFIG5 0x0009
+#define RT_DATA_STACK_ADDR 0x000a
+#define BC_FRM_TIME_REMAIN 0x000b
+#define BC_MSG_TIME 0x000c
+#define RT_LAST_CMD_WORD 0x000d
+#define BC_FRM_TIME 0x000d
+#define RT_STATUS 0x000e
+#define RT_BIT_WORD 0x000f
+
+
+typedef struct CmdWord
+{
+ UINT32 Reserve:16; //16位与32位对齐
+ UINT32 RTADDR:5;
+ UINT32 TR:1;
+ UINT32 SADDR:5;
+ UINT32 WCOUNT:5;
+} COMMANDWORD;
+
+/*********************************************************************************************************
+ 1553数据帧结构定义
+*********************************************************************************************************/
+typedef struct _1553DataFrame{
+ UINT16 msgType;
+ UINT16 msgChannel;
+ union{
+ COMMANDWORD CMDWORD1;
+ UINT32 cmdWord1;
+ }frameInfo1;
+ union{
+ COMMANDWORD CMDWORD2;
+ UINT32 cmdWord2;
+ }frameInfo2;
+ UINT16 msgCtrlWord;
+ UINT16 msgStatus1; /* RT 接收消息状态字,仅用于RT模式读*/
+ UINT16 msgStatus2; /* 仅RT-RT 模式使用*/
+ UINT16 loopBack; //仅限于BC模式使用
+ UINT32 msgGapTime;
+ UINT16 msgData[32];
+}FRAME_1553DATA, *P1553DATA_FRAME; /* 1553数据帧类型*/
+
+#ifdef __cplusplus
+typedef INT (*CALLBACK_1553)(...);
+#else
+typedef INT (*CALLBACK_1553)();
+#endif
+
+typedef struct _1553_drv_funcs DRV_FUNCS_1553;
+
+typedef struct _1553_chan {
+ DRV_FUNCS_1553 *pDrvFuncs;
+} CHAN_1553;
+
+struct _1553_drv_funcs {
+ INT (*ioctl)
+ (
+ CHAN_1553 *p1553chan,
+ INT cmd,
+ PVOID arg
+ );
+
+ INT (*txStartup)
+ (
+ CHAN_1553 *p1553chan
+ );
+
+ INT (*callbackInstall)
+ (
+ CHAN_1553 *p1553chan,
+ INT callbackType,
+ CALLBACK_1553 callback,
+ PVOID callbackArg
+ );
+};
+
+LW_API INT API_1553DrvInstall(VOID);
+LW_API INT API_1553DevCreate (PCHAR pcName,
+ CHAN_1553 *p1553chan,
+ UINT uiRdFrameSize,
+ UINT uiWrtFrameSize);
+LW_API INT API_1553DevRemove(PCHAR pcName, BOOL bForce);
+
+
+#define bus1553Drv API_1553DrvInstall
+#define bus1553DevCreate API_1553DevCreate
+#define bus1553DevRemove API_1553DevRemove
+
+
+#endif /* __bus1553__ */
diff --git a/SylixOS/system/include/s_system.h b/SylixOS/system/include/s_system.h
index ec3adb7..54492e1 100644
--- a/SylixOS/system/include/s_system.h
+++ b/SylixOS/system/include/s_system.h
@@ -89,6 +89,7 @@
#include "../SylixOS/system/device/pty/pty.h" /* pseudo terminal */
#include "../SylixOS/system/device/block/blockIo.h" /* block device */
#include "../SylixOS/system/device/can/can.h" /* CAN bus device */
+#include "../SylixOS/system/device/1553B/bus1553.h"
#include "../SylixOS/system/device/buzzer/buzzer.h" /* buzzer device */
#include "../SylixOS/system/device/graph/gmemDev.h" /* graph memory device */
diff --git a/libsylixos.mk b/libsylixos.mk
index 026c5cf..df988fa 100644
--- a/libsylixos.mk
+++ b/libsylixos.mk
@@ -775,6 +775,7 @@ SylixOS/debug/hwdbg/openocd.c
#*********************************************************************************************************
DRV_SRCS = \
SylixOS/driver/can/sja1000.c \
+SylixOS/driver/bu61580/bu61580.c \
SylixOS/driver/dma/i8237a.c \
SylixOS/driver/int/i8259a.c \
SylixOS/driver/net/dm9000.c \
@@ -1804,6 +1805,7 @@ SylixOS/system/device/bmsg/bmsgDev.c \
SylixOS/system/device/bmsg/bmsgfd.c \
SylixOS/system/device/buzzer/buzzer.c \
SylixOS/system/device/can/can.c \
+SylixOS/system/device/1553B/bus1553.c \
SylixOS/system/device/dma/dma.c \
SylixOS/system/device/dma/dmaLib.c \
SylixOS/system/device/eventfd/eventfdDev.c \