/******************************************************************************
* 物联网黑手党出品
*******************************************************************************
文件名: mac.c
目标硬件: cc1110/CC2510
作者: 罗一鸣
修改日期: 2011-3-18
版本: 1.3.2
Description:
MAC层的收发控制
设置了Tx Cache和Rx Cache。
Tx Cache 设置的默认CSMA时间是250毫秒,一次发送,有应答请求。如果没有应答,应
在用户层手动设置应答。而如果模块收到应答请求,而有Tx Cache处于等待发送或者等待应
答,则优先发送Ack。
由于超时调度函数是用于CSMA监听和ACK接受,在收到ACK时,应停止MAC超时触发函数
调度。
******************************************************************************/
#include "hal_rf.h"
#include "mac_timer.h"
/* Tx Status */
#define MAC_TX_STA 0x0F
#define MAC_TX_IDLE 0x00
#define MAC_TX_CSMA 0x01
#define MAC_TX_ALLOW 0x02 //
#define MAC_TX_ING 0x03 // Transmiting
#define MAC_TX_WAIT 0x04 // Wait Ack
#define MAC_TX_END 0x05 // Tx is success
#define MAC_TX_NOACK 0x06 // Tx is no ack
#define MAC_TX_FAIL 0x07 // Tx is fail
/* ACK STA */
#define MAC_TX_ACKEN 0x10
/* CSMA EN */
#define MAC_TX_FAST 0x20
/* Tx Retry Time */
/* Define Mac Packet */
#define MAC_HEAD_PAD(x) ((MacHead_t*)(x))
//RF_DMA_PACKET MacTxCache;
typedef struct{
BOOL en;
UINT8 sta;
MacHead_t ack;
}MacAck_t;
/* Globle FIFO */
UINT16 MacRxTime;
RF_DMA_PACKET* MacRxDma = 0;
MacRxPkt_t MacRxCache;
MacTxPkt_t MacTxCache;
MacHead_t MacAck;
void (*AckCallbackStore)(void);
UINT8 AckTimeoutStore;
/* */
UINT8 MacMyAddr;
/* Rx Call back & Tx Call back */
void (*MacTxCb)(MacBuff_t* buff,UINT8 event,UINT8 sta);
void (*MacRxCb)(MacBuff_t* buff,UINT8 len,UINT8 rssi,UINT16 time);
/* MAC TIMER FUNTION */
UINT8 MacTimeoutKeep(MacTimerCb_t*);
void MacTimeoutSetCb(UINT8 time,void (*cb)(void));
void MacTimeoutClean(void);
void MacTimerPoll(void);
/*------------------------------------------------------------------------------
MAC in RF Interrupt's Funtion
------------------------------------------------------------------------------*/
void HalRfTxFinishIsr(RF_DMA_PACKET* pkt,UINT8 evt)
{
//If Tx Ack , pass
if(MacTxCache.event == evt && (MacTxCache.sta & MAC_TX_STA) == MAC_TX_ING) //Tx Normal Data
{
if((MacTxCache.sta & MAC_TX_ACKEN) == MAC_TX_ACKEN)
{
MacTxCache.sta = MAC_TX_WAIT; //MAC_TX_ACKEN is Cleaned
}
else
{
MacTxCache.sta = MAC_TX_END;
}
}
}
void HalRfRxFinishIsr(RF_DMA_PACKET* pkt,UINT16 time)
{
UINT8 len = pkt->len;
MacBuff_t* buff = (MacBuff_t*)pkt->buff;
//CRC chech;
if(!(pkt->buff[len+1] & 0x80)) //Check Crc is Ok? Or give up this packet
{
pkt->len = 0; //clean dma.
return;
}
//set buff
//Address Detect
if(buff->DestAddr != MacMyAddr && buff->DestAddr != 0xFF)
{
pkt->len = 0; //clean dma
return;
}
//Ack Req Proccess
if((buff->Cmd & MAC_ACK_MSK) == MAC_ACK_ASW)
{
//Rx a ack
if(MacTxCache.event == buff->Event) //ack is for now Tx packet
{
pkt->len = 0;
if(MacTxCache.sta == MAC_TX_WAIT)
{
MacTxCache.sta = MAC_TX_END; //ACK is Ok
}
return ;
}
}
//Rx Payload,Read Dma buffer,and Clean it.
if(MacRxCache.len == 0) //if Rx Cache is empty,push dma to cache
{
pkt->len = 0; //clean dma
MacRxCache.len = len;
MacRxCache.rssi = pkt->buff[len];
MacRxCache.time = time;
memcpy(&MacRxCache.buff,buff,len);
}
else //if chache is full,keep dma and push it to cache when cache empty.
{
MacRxTime = time;
MacRxDma = pkt;
}
}
/*------------------------------------------------------------------------------
MAC in Poll's Funtion
------------------------------------------------------------------------------*/
/* Mac Random Ms Timeout */
UINT8 MacRandomGet(void)
{
UINT8 random = 0;
GET_RANDOM_BYTE(random);
do{
GET_RANDOM_BYTE(random);
random &= 0x07;
}while(random == 0);
return random;
}
/* CSMA Start */
void MacCsmaCb(void)
{
if(MacTxCache.len)
{
if((MacTxCache.sta & MAC_TX_STA) == MAC_TX_CSMA) //timeout is finish
{
MacTxCache.sta &= 0xF0;
MacTxCache.sta |= MAC_TX_ALLOW; //if it is Txing,wait it finish.
}
}
}
void MacWaitAckCb(void)
{
if(MacTxCache.len)
{
if((MacTxCache.sta & MAC_TX_STA) == MAC_TX_WAIT)
{
MacTxCache.sta &= 0xF0;
MacTxCache.sta |= MAC_TX_NOACK;
}
}
}
void MacTxAck(void)
{
HalRfSendFast((UINT8*)&(MacAck),4,MacAck.Event);
}
/*------------------------------------------------------------------------------
MAC polls,include RxPoll,TxPoll and TxEndPoll
MAC层内部函数,用于NWK层调度。
------------------------------------------------------------------------------*/
/* Tx Poll , if there is data to be send and the channel is clear,send all data*/
void MacTxPoll(void)
{
if(MacTxCache.len) //TxBuffer is Full,wait to be send
{
if((MacTxCache.sta & MAC_TX_STA) == MAC_TX_ALLOW) //try to send
{
if(MacTxCache.sta & MAC_TX_FAST) //CSMA IS OFF
{
HalRfSendFast((UINT8*)&MacTxCache.buff,MacTxCache.len,MacTxCache.event); //No Csma
MacTxCache.sta &= 0xF0;
MacTxCache.sta |= MAC_TX_ING;
//If Need Wait Ack?
if((MacTxCache.sta & MAC_TX_ACKEN) == MAC_TX_ACKEN)
{
MacTimeoutSetCb(32,MacWaitAckCb);
}
}
else
{
if(HalRfSend((UINT8*)&MacTxCache.buff,MacTxCache.len,MacTxCache.event)) //SEND CSMA
{
MacTxCache.sta &= 0xF0;
MacTxCache.sta |= MAC_TX_ING;
//If Need Wait Ack?
if((MacTxCache.sta & MAC_TX_ACKEN) == MAC_TX_ACKEN)
{
MacTimeoutSetCb(32,MacWaitAckCb);
}
}
else //channel is busy
{
UINT8 random = MacRandomGet(); //ACK,16ms CSMA
if(MacTxCache.timeout) //CSMA timeout is still
{
if(MacTxCache.timeout > random)
{
MacTxCache.timeout -= random;
}
else
{
random = MacTxCache.timeout;
MacTxCache.timeout = 0;
}
MacTxCache.sta &= 0xF0;
MacTxCache.sta |= MAC_TX_CSMA;
MacTimeoutSetCb(random,MacCsmaCb);
}
else //CSMA timeout is runout
{
MacTxCache.sta &= 0xF0;
MacTxCache.sta |= MAC_TX_FAIL;
}
}
}
}
}
}
void MacTxEndPoll(void)
{
if(MacTxCache.sta == MAC_TX_END || MacTxCache.sta == MAC_TX_FAIL || MacTxCache.sta == MAC_TX_NOACK)
{
UINT8 sta = MacTxCache.sta;
MacTimeoutClean();
MacTxCache.len = 0;
MacTxCache.sta = MAC_TX_IDLE;
if(MacTxCb)
{
sta -= MAC_TX_END;
MacTxCb(&MacTxCache.buff, MacTxCache.event, sta);
}
}
}
void MacRxEndPoll(void)
{
if(MacRxCache.len)
{
if((MacRxCache.buff.Cmd & MAC_ACK_MSK) == MAC_ACK_REQ)
{
//Send Ack
MacAck.DestAddr = MacRxCache.buff.SrcAddr;
MacAck.SrcAddr = MacMyAddr;
MacAck.Cmd = MAC_ACK_ASW;
MacAck.Event = MacRxCache.buff.Event;
MacTxAck();
}
if(MacRxCb)//run rx callback
{
MacRxCb(&MacRxCache.buff,MacRxCache.len - 4,MacRxCache.rssi,MacRxCache.time);
}
// Clean Rx Cache
MacRxCache.len = 0;
// If Rx Dma have new data,copy to Rx Cache.
if(MacRxDma && MacRxDma->len)
{
UINT8 len = MacRxDma->len;
MacRxDma->len = 0;
MacRxCache.len = len;
MacRxCache.rssi = MacRxDma->buff[len];
没有合适的资源?快使用搜索试试~ 我知道了~
send-and-receive-data-DMA-interrupt.zip_dma中断_interrupt dma
共87个文件
c:22个
r51:21个
pbi:16个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 34 浏览量
2022-09-20
18:29:37
上传
评论
收藏 181KB ZIP 举报
温馨提示
采用DMA中断的方式收发数据采用DMA中断的方式收发数据
资源推荐
资源详情
资源评论
收起资源包目录
send-and-receive-data-DMA-interrupt.zip (87个子文件)
CC1110
component
hal
hal_driver
sleep.h 151B
sleep.c 1000B
uart.h 918B
gpio.h 657B
gpio.c 598B
dma.c 5KB
uart_s.c 3KB
timer_.c 2KB
uart.c 3KB
timer.c 2KB
dma.h 1KB
timer.h 536B
hal_lib
hal.c 7KB
halSysTimer.h 1KB
hal.h 73KB
halSysTimer.c 2KB
hal_rf
rfConfig.srfs 1KB
rfConfig1.c 2KB
hal_rf.h 4KB
rfConfig.h 81B
mac_timer.c 2KB
mac_timer.h 578B
rf_mac.c 10KB
rfConfig.c 2KB
rf_dma.c 7KB
os
os_time.c 1KB
os_drivers.c 0B
os_mac.h 0B
os_main.h 0B
os_main.c 0B
os_task.c 24B
os_task.h 76B
os_time.h 658B
os_driver.c 0B
os_drivers.h 0B
os_mac.c 0B
project
sample
IAR
sample.ewd 33KB
sample.dep 25KB
Release
List
Exe
Obj
mac_timer.pbi 507B
main.pbi 509B
rfConfig.r51 2KB
rfConfig.pbi 505B
timer.pbi 507B
rf_dma.pbi 501B
hal.pbi 497B
gpio.pbi 505B
sample.pbd 786B
rf_mac.pbi 501B
os_time.pbi 487B
uart.pbi 505B
dma.pbi 503B
hal.r51 34KB
sample.ewp 56KB
sample.eww 160B
settings
sample.dni 1KB
sample.dbgdt 14KB
sample.wsdt 4KB
sample.cspy.bat 2KB
Debug
List
sample.map 101KB
Exe
sample.d51 52KB
Obj
os_main.r51 1KB
mac.r51 1KB
os_task.pbi 803B
rfConfig.r51 3KB
os_drivers.pbi 809B
os_time.r51 7KB
mac.pbi 861B
uart.r51 34KB
mac_timer.r51 17KB
os_main.pbi 803B
timer.r51 2KB
rfConfig1.r51 3KB
dma.r51 21KB
halSysTimer.r51 10KB
test.r51 25KB
sample.pbd 712B
os_time.pbi 803B
rf_dma.r51 31KB
os_drivers.r51 1KB
main.r51 21KB
os_task.r51 2KB
hal.r51 43KB
rf_mac.r51 40KB
gpio.r51 3KB
sleep.r51 15KB
application
main.c 2KB
test.c 2KB
共 87 条
- 1
资源评论
御道御小黑
- 粉丝: 58
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功