[DeviceDrivers] code cleanup for CAN driver and fix some critical issue
This commit is contained in:
parent
eb23f0def3
commit
1488568801
|
@ -10,7 +10,9 @@
|
|||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-05-14 aubrcool@qq.com first version
|
||||
* 2015-07-06 Bernard code cleanup.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
@ -33,6 +35,7 @@ static rt_err_t rt_can_init(struct rt_device *dev)
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* can interrupt routines
|
||||
*/
|
||||
|
@ -51,21 +54,26 @@ rt_inline int _can_int_rx(struct rt_can_device *can, struct rt_can_msg *data, in
|
|||
while (msgs)
|
||||
{
|
||||
rt_base_t level;
|
||||
rt_int32_t hdr;
|
||||
struct rt_can_msg_list *listmsg = RT_NULL;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
rt_int32_t hdr = data->hdr;
|
||||
hdr = data->hdr;
|
||||
|
||||
if (hdr >= 0 && can->hdr && hdr < can->config.maxhdr && !rt_list_isempty(&can->hdr[hdr].list))
|
||||
{
|
||||
listmsg = rt_list_entry(can->hdr[hdr].list.next, struct rt_can_msg_list, hdrlist);
|
||||
rt_list_remove(&listmsg->list);
|
||||
rt_list_remove(&listmsg->hdrlist);
|
||||
if(can->hdr[hdr].msgs) {
|
||||
if (can->hdr[hdr].msgs)
|
||||
{
|
||||
can->hdr[hdr].msgs--;
|
||||
}
|
||||
listmsg->owner = RT_NULL;
|
||||
} else if(hdr == -1)
|
||||
}
|
||||
else if (hdr == -1)
|
||||
#endif /*RT_CAN_USING_HDR*/
|
||||
if (!rt_list_isempty(&rx_fifo->uselist))
|
||||
{
|
||||
|
@ -73,7 +81,8 @@ rt_inline int _can_int_rx(struct rt_can_device *can, struct rt_can_msg *data, in
|
|||
rt_list_remove(&listmsg->list);
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
rt_list_remove(&listmsg->hdrlist);
|
||||
if(listmsg->owner != RT_NULL && listmsg->owner->msgs) {
|
||||
if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
|
||||
{
|
||||
listmsg->owner->msgs--;
|
||||
}
|
||||
listmsg->owner = RT_NULL;
|
||||
|
@ -97,10 +106,13 @@ rt_inline int _can_int_rx(struct rt_can_device *can, struct rt_can_msg *data, in
|
|||
RT_ASSERT(rx_fifo->freenumbers <= can->config.msgboxsz);
|
||||
rt_hw_interrupt_enable(level);
|
||||
listmsg = RT_NULL;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
data ++; msgs -= sizeof(struct rt_can_msg);
|
||||
data ++;
|
||||
msgs -= sizeof(struct rt_can_msg);
|
||||
}
|
||||
|
||||
return (size - msgs);
|
||||
|
@ -121,19 +133,24 @@ rt_inline int _can_int_tx(struct rt_can_device *can, const struct rt_can_msg *da
|
|||
{
|
||||
rt_base_t level;
|
||||
rt_uint32_t no;
|
||||
rt_uint32_t result;
|
||||
struct rt_can_sndbxinx_list *tx_tosnd = RT_NULL;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
if (!rt_list_isempty(&tx_fifo->freelist))
|
||||
{
|
||||
tx_tosnd = rt_list_entry(tx_fifo->freelist.next, struct rt_can_sndbxinx_list, list);
|
||||
RT_ASSERT(tx_tosnd != RT_NULL);
|
||||
rt_list_remove(&tx_tosnd->list);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_hw_interrupt_enable(level);
|
||||
rt_completion_wait(&(tx_fifo->completion), RT_WAITING_FOREVER);
|
||||
continue;
|
||||
}
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
no = ((rt_uint32_t)tx_tosnd - (rt_uint32_t)tx_fifo->buffer) / sizeof(struct rt_can_sndbxinx_list);
|
||||
tx_tosnd->result = RT_CAN__SND_RESUTL_WAIT;
|
||||
if (can->ops->sendmsg(can, data , no))
|
||||
|
@ -145,9 +162,11 @@ rt_inline int _can_int_tx(struct rt_can_device *can, const struct rt_can_msg *da
|
|||
}
|
||||
can->status.sndchange = 1;
|
||||
rt_completion_wait(&(tx_tosnd->completion), RT_WAITING_FOREVER);
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_uint32_t result = tx_tosnd->result;
|
||||
if(!rt_list_isempty(&tx_tosnd->list)) {
|
||||
result = tx_tosnd->result;
|
||||
if (!rt_list_isempty(&tx_tosnd->list))
|
||||
{
|
||||
rt_list_remove(&tx_tosnd->list);
|
||||
}
|
||||
rt_list_insert_before(&tx_fifo->freelist, &tx_tosnd->list);
|
||||
|
@ -158,7 +177,8 @@ rt_inline int _can_int_tx(struct rt_can_device *can, const struct rt_can_msg *da
|
|||
level = rt_hw_interrupt_disable();
|
||||
can->status.sndpkg++;
|
||||
rt_hw_interrupt_enable(level);
|
||||
data ++; msgs -= sizeof(struct rt_can_msg);
|
||||
data ++;
|
||||
msgs -= sizeof(struct rt_can_msg);
|
||||
if (!msgs) break;
|
||||
}
|
||||
else
|
||||
|
@ -183,9 +203,12 @@ rt_inline int _can_int_tx(struct rt_can_device *can, const struct rt_can_msg *da
|
|||
|
||||
return (size - msgs);
|
||||
}
|
||||
|
||||
rt_inline int _can_int_tx_priv(struct rt_can_device *can, const struct rt_can_msg *data, int msgs)
|
||||
{
|
||||
int size;
|
||||
rt_base_t level;
|
||||
rt_uint32_t no, result;
|
||||
struct rt_can_tx_fifo *tx_fifo;
|
||||
|
||||
RT_ASSERT(can != RT_NULL);
|
||||
|
@ -194,36 +217,39 @@ rt_inline int _can_int_tx_priv(struct rt_can_device *can, const struct rt_can_ms
|
|||
tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
|
||||
RT_ASSERT(tx_fifo != RT_NULL);
|
||||
|
||||
rt_base_t level;
|
||||
rt_uint32_t no;
|
||||
rt_uint32_t result;
|
||||
while (msgs)
|
||||
{
|
||||
no = data->priv;
|
||||
if(no >= can->config.sndboxnumber) {
|
||||
if (no >= can->config.sndboxnumber)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
if((tx_fifo->buffer[no].result != RT_CAN__SND_RESUTL_OK)) {
|
||||
if ((tx_fifo->buffer[no].result != RT_CAN__SND_RESUTL_OK))
|
||||
{
|
||||
rt_hw_interrupt_enable(level);
|
||||
rt_completion_wait(&(tx_fifo->buffer[no].completion), RT_WAITING_FOREVER);
|
||||
continue;
|
||||
}
|
||||
tx_fifo->buffer[no].result = RT_CAN__SND_RESUTL_WAIT;
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
if (can->ops->sendmsg(can, data , no) != RT_EOK)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
can->status.sndchange = 1;
|
||||
rt_completion_wait(&(tx_fifo->buffer[no].completion), RT_WAITING_FOREVER);
|
||||
|
||||
result = tx_fifo->buffer[no].result;
|
||||
if (result == RT_CAN__SND_RESUTL_OK)
|
||||
{
|
||||
level = rt_hw_interrupt_disable();
|
||||
can->status.sndpkg++;
|
||||
rt_hw_interrupt_enable(level);
|
||||
data ++; msgs -= sizeof(struct rt_can_msg);
|
||||
data ++;
|
||||
msgs -= sizeof(struct rt_can_msg);
|
||||
if (!msgs) break;
|
||||
}
|
||||
else
|
||||
|
@ -237,6 +263,7 @@ rt_inline int _can_int_tx_priv(struct rt_can_device *can, const struct rt_can_ms
|
|||
|
||||
return (size - msgs);
|
||||
}
|
||||
|
||||
static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||
{
|
||||
struct rt_can_device *can;
|
||||
|
@ -251,6 +278,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
|||
{
|
||||
if (oflag & RT_DEVICE_FLAG_INT_RX)
|
||||
{
|
||||
int i=0;
|
||||
struct rt_can_rx_fifo *rx_fifo;
|
||||
|
||||
rx_fifo = (struct rt_can_rx_fifo *) rt_malloc(sizeof(struct rt_can_rx_fifo) +
|
||||
|
@ -261,7 +289,6 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
|||
rt_list_init(&rx_fifo->freelist);
|
||||
rt_list_init(&rx_fifo->uselist);
|
||||
rx_fifo->freenumbers = can->config.msgboxsz;
|
||||
int i = 0;
|
||||
for (i = 0; i < can->config.msgboxsz; i++)
|
||||
{
|
||||
rt_list_insert_before(&rx_fifo->freelist, &rx_fifo->buffer[i].list);
|
||||
|
@ -272,6 +299,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
|||
}
|
||||
can->can_rx = rx_fifo;
|
||||
rt_exit_critical();
|
||||
|
||||
dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
|
||||
/* configure low level device */
|
||||
can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||
|
@ -281,14 +309,18 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
|||
can->can_rx = RT_NULL;
|
||||
rt_exit_critical();
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_exit_critical();
|
||||
}
|
||||
|
||||
rt_enter_critical();
|
||||
if (can->can_tx == RT_NULL)
|
||||
{
|
||||
if (oflag & RT_DEVICE_FLAG_INT_TX)
|
||||
{
|
||||
int i = 0;
|
||||
struct rt_can_tx_fifo *tx_fifo;
|
||||
|
||||
tx_fifo = (struct rt_can_tx_fifo *) rt_malloc(sizeof(struct rt_can_tx_fifo) +
|
||||
|
@ -298,7 +330,6 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
|||
rt_memset(tx_fifo->buffer, 0,
|
||||
can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
|
||||
rt_list_init(&tx_fifo->freelist);
|
||||
int i = 0;
|
||||
for (i = 0; i < can->config.sndboxnumber; i++)
|
||||
{
|
||||
rt_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list);
|
||||
|
@ -317,50 +348,71 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
|||
can->can_tx = RT_NULL;
|
||||
rt_exit_critical();
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_exit_critical();
|
||||
}
|
||||
|
||||
can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_CAN_INT_ERR);
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
rt_enter_critical();
|
||||
if(can->hdr == RT_NULL) {
|
||||
if (can->hdr == RT_NULL)
|
||||
{
|
||||
int i = 0;
|
||||
struct rt_can_hdr *phdr;
|
||||
|
||||
/* exit critical region for malloc a header. */
|
||||
rt_exit_critical();
|
||||
|
||||
phdr = (struct rt_can_hdr *) rt_malloc(can->config.maxhdr * sizeof(struct rt_can_hdr));
|
||||
RT_ASSERT(phdr != RT_NULL);
|
||||
rt_memset(phdr, 0, can->config.maxhdr * sizeof(struct rt_can_hdr));
|
||||
int i = 0;
|
||||
for (i = 0; i < can->config.maxhdr; i++)
|
||||
{
|
||||
rt_list_init(&phdr[i].list);
|
||||
}
|
||||
|
||||
rt_enter_critical();
|
||||
can->hdr = phdr;
|
||||
rt_exit_critical();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_exit_critical();
|
||||
}
|
||||
#endif
|
||||
|
||||
rt_enter_critical();
|
||||
if(!can->timerinitflag) {
|
||||
if (!can->timerinitflag)
|
||||
{
|
||||
can->timerinitflag = 1;
|
||||
rt_exit_critical();
|
||||
|
||||
#ifdef RT_CAN_USING_LED
|
||||
if(can->config.rcvled != RT_NULL) {
|
||||
if (can->config.rcvled != RT_NULL)
|
||||
{
|
||||
rt_pin_mode(can->config.rcvled->pin, can->config.rcvled->mode);
|
||||
rt_pin_write(can->config.rcvled->pin, can->config.rcvled->init);
|
||||
}
|
||||
if(can->config.sndled != RT_NULL) {
|
||||
if (can->config.sndled != RT_NULL)
|
||||
{
|
||||
rt_pin_mode(can->config.sndled->pin, can->config.sndled->mode);
|
||||
rt_pin_write(can->config.sndled->pin, can->config.sndled->init);
|
||||
}
|
||||
if(can->config.errled != RT_NULL) {
|
||||
if (can->config.errled != RT_NULL)
|
||||
{
|
||||
rt_pin_mode(can->config.errled->pin, can->config.errled->mode);
|
||||
rt_pin_write(can->config.errled->pin, can->config.errled->init);
|
||||
}
|
||||
#endif
|
||||
rt_timer_start(&can->timer);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_exit_critical();
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
@ -373,8 +425,10 @@ static rt_err_t rt_can_close(struct rt_device *dev)
|
|||
|
||||
/* this device has more reference count */
|
||||
if (dev->ref_count > 1) return RT_EOK;
|
||||
|
||||
rt_enter_critical();
|
||||
if(can->timerinitflag) {
|
||||
if (can->timerinitflag)
|
||||
{
|
||||
can->timerinitflag = 0;
|
||||
rt_exit_critical();
|
||||
rt_timer_stop(&can->timer);
|
||||
|
@ -383,20 +437,31 @@ static rt_err_t rt_can_close(struct rt_device *dev)
|
|||
rt_pin_write(can->config.rcvled->pin, can->config.sndled->init);
|
||||
rt_pin_write(can->config.rcvled->pin, can->config.errled->init);
|
||||
#endif
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_exit_critical();
|
||||
}
|
||||
|
||||
rt_enter_critical();
|
||||
can->status_indicate.ind = RT_NULL;
|
||||
can->status_indicate.args = RT_NULL;
|
||||
rt_exit_critical();
|
||||
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
rt_enter_critical();
|
||||
if(can->hdr != RT_NULL) {
|
||||
rt_free(can->hdr);
|
||||
if (can->hdr != RT_NULL)
|
||||
{
|
||||
struct rt_can_hdr *hdr;
|
||||
|
||||
hdr = can->hdr;
|
||||
can->hdr = RT_NULL;
|
||||
rt_exit_critical();
|
||||
} else {
|
||||
|
||||
rt_free(hdr);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_exit_critical();
|
||||
}
|
||||
#endif
|
||||
|
@ -444,6 +509,7 @@ static rt_size_t rt_can_read(struct rt_device *dev,
|
|||
{
|
||||
return _can_int_rx(can, buffer, size);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -461,9 +527,12 @@ static rt_size_t rt_can_write(struct rt_device *dev,
|
|||
|
||||
if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
|
||||
{
|
||||
if(can->config.privmode) {
|
||||
if (can->config.privmode)
|
||||
{
|
||||
return _can_int_tx_priv(can, buffer, size);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return _can_int_tx(can, buffer, size);
|
||||
}
|
||||
}
|
||||
|
@ -498,28 +567,36 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
break;
|
||||
case RT_CAN_CMD_SET_PRIV:
|
||||
/* configure device */
|
||||
if((rt_uint32_t)args != can->config.privmode) {
|
||||
if(res = can->ops->control(can, cmd, args) != RT_EOK) {
|
||||
return res;
|
||||
}
|
||||
struct rt_can_tx_fifo* tx_fifo;
|
||||
tx_fifo = (struct rt_can_tx_fifo*) can->can_tx;
|
||||
if ((rt_uint32_t)args != can->config.privmode)
|
||||
{
|
||||
int i;
|
||||
rt_base_t level;
|
||||
if(can->config.privmode) {
|
||||
struct rt_can_tx_fifo *tx_fifo;
|
||||
|
||||
if (res = can->ops->control(can, cmd, args) != RT_EOK)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
|
||||
if (can->config.privmode)
|
||||
{
|
||||
rt_completion_done(&(tx_fifo->completion));
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
for (i = 0; i < can->config.sndboxnumber; i++)
|
||||
{
|
||||
rt_list_remove(&tx_fifo->buffer[i].list);
|
||||
}
|
||||
rt_hw_interrupt_enable(level);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < can->config.sndboxnumber; i++)
|
||||
{
|
||||
rt_base_t level;
|
||||
level = rt_hw_interrupt_disable();
|
||||
if(tx_fifo->buffer[i].result == RT_CAN__SND_RESUTL_OK) {
|
||||
if (tx_fifo->buffer[i].result == RT_CAN__SND_RESUTL_OK)
|
||||
{
|
||||
rt_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list);
|
||||
}
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
@ -535,26 +612,34 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
#ifdef RT_CAN_USING_HDR
|
||||
case RT_CAN_CMD_SET_FILTER:
|
||||
res = can->ops->control(can, cmd, args);
|
||||
if(res != RT_EOK || can->hdr == RT_NULL) {
|
||||
if (res != RT_EOK || can->hdr == RT_NULL)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
{
|
||||
struct rt_can_filter_config *pfilter;
|
||||
struct rt_can_filter_item *pitem;
|
||||
rt_uint32_t count;
|
||||
rt_base_t level;
|
||||
|
||||
pfilter = (struct rt_can_filter_config *)args;
|
||||
count = pfilter->count;
|
||||
pitem = pfilter->items;
|
||||
if(pfilter->actived) {
|
||||
while(count) {
|
||||
if(pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) {
|
||||
if (pfilter->actived)
|
||||
{
|
||||
while (count)
|
||||
{
|
||||
if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
|
||||
{
|
||||
count--;
|
||||
pitem++;
|
||||
continue;
|
||||
}
|
||||
level = rt_hw_interrupt_disable();
|
||||
if(!can->hdr[pitem->hdr].connected) {
|
||||
|
||||
if (!can->hdr[pitem->hdr].connected)
|
||||
{
|
||||
rt_memcpy(&can->hdr[pitem->hdr].filter, pitem,
|
||||
sizeof(struct rt_can_filter_item));
|
||||
can->hdr[pitem->hdr].connected = 1;
|
||||
|
@ -565,15 +650,21 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
count--;
|
||||
pitem++;
|
||||
}
|
||||
} else {
|
||||
while(count) {
|
||||
if(pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) {
|
||||
}
|
||||
else
|
||||
{
|
||||
while (count)
|
||||
{
|
||||
if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
|
||||
{
|
||||
count--;
|
||||
pitem++;
|
||||
continue;
|
||||
}
|
||||
level = rt_hw_interrupt_disable();
|
||||
if(can->hdr[pitem->hdr].connected) {
|
||||
|
||||
if (can->hdr[pitem->hdr].connected)
|
||||
{
|
||||
rt_memset(&can->hdr[pitem->hdr].filter, 0,
|
||||
sizeof(struct rt_can_filter_item));
|
||||
can->hdr[pitem->hdr].connected = 0;
|
||||
|
@ -602,6 +693,7 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/*
|
||||
* can timer
|
||||
*/
|
||||
|
@ -611,67 +703,97 @@ static void cantimeout(void* arg)
|
|||
rt_uint32_t ledonflag = 0;
|
||||
#endif /*RT_CAN_USING_LED*/
|
||||
rt_can_t can = (rt_can_t)arg;
|
||||
|
||||
rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status);
|
||||
if(can->timerinitflag == 1) {
|
||||
if (can->timerinitflag == 1)
|
||||
{
|
||||
#ifdef RT_CAN_USING_LED
|
||||
ledonflag = 1;
|
||||
#endif /*RT_CAN_USING_LED*/
|
||||
can->timerinitflag = 0xFF;
|
||||
}
|
||||
#ifdef RT_CAN_USING_LED
|
||||
if(can->config.rcvled != RT_NULL && can->config.sndled == RT_NULL) {
|
||||
if(ledonflag == 1) {
|
||||
if (can->config.rcvled != RT_NULL && can->config.sndled == RT_NULL)
|
||||
{
|
||||
if (ledonflag == 1)
|
||||
{
|
||||
rt_pin_write(can->config.rcvled->pin, can->config.rcvled->init ? 0 : 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (can->status.rcvchange == 1 || can->status.sndchange == 1)
|
||||
{
|
||||
can->status.rcvchange = 0;
|
||||
can->status.sndchange = 0;
|
||||
rt_pin_write(can->config.rcvled->pin, rt_pin_read(can->config.rcvled->pin) ? 0 : 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(can->config.rcvled->pin, can->config.rcvled->init);
|
||||
}
|
||||
}
|
||||
} else if(can->config.rcvled != RT_NULL && can->config.sndled != RT_NULL) {
|
||||
if(ledonflag == 1) {
|
||||
}
|
||||
else if (can->config.rcvled != RT_NULL && can->config.sndled != RT_NULL)
|
||||
{
|
||||
if (ledonflag == 1)
|
||||
{
|
||||
rt_pin_write(can->config.rcvled->pin, can->config.rcvled->init ? 0 : 1);
|
||||
rt_pin_write(can->config.sndled->pin, can->config.sndled->init ? 0 : 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (can->status.rcvchange == 1)
|
||||
{
|
||||
can->status.rcvchange = 0;
|
||||
rt_pin_write(can->config.rcvled->pin, rt_pin_read(can->config.rcvled->pin) ? 0 : 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(can->config.rcvled->pin, can->config.rcvled->init);
|
||||
}
|
||||
if (can->status.sndchange == 1)
|
||||
{
|
||||
can->status.sndchange = 0;
|
||||
rt_pin_write(can->config.sndled->pin, rt_pin_read(can->config.sndled->pin) ? 0 : 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(can->config.sndled->pin, can->config.sndled->init);
|
||||
}
|
||||
}
|
||||
} else if(can->config.rcvled == RT_NULL && can->config.sndled != RT_NULL) {
|
||||
if(ledonflag == 1) {
|
||||
}
|
||||
else if (can->config.rcvled == RT_NULL && can->config.sndled != RT_NULL)
|
||||
{
|
||||
if (ledonflag == 1)
|
||||
{
|
||||
rt_pin_write(can->config.sndled->pin, can->config.sndled->init ? 0 : 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (can->status.rcvchange == 1 || can->status.sndchange == 1)
|
||||
{
|
||||
can->status.rcvchange = 0;
|
||||
can->status.sndchange = 0;
|
||||
rt_pin_write(can->config.sndled->pin, rt_pin_read(can->config.sndled->pin) ? 0 : 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(can->config.sndled->pin, can->config.sndled->init);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(ledonflag == 1) {
|
||||
if (ledonflag == 1)
|
||||
{
|
||||
rt_pin_write(can->config.errled->pin, can->config.errled->init ? 0 : 1);
|
||||
} else {
|
||||
if(can->status.errcode) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (can->status.errcode)
|
||||
{
|
||||
rt_pin_write(can->config.errled->pin, can->config.errled->init ? 0 : 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(can->config.errled->pin, can->config.errled->init);
|
||||
}
|
||||
}
|
||||
|
@ -749,15 +871,17 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
|
|||
#endif
|
||||
int ch = -1;
|
||||
rt_base_t level;
|
||||
rt_uint32_t no;
|
||||
|
||||
rx_fifo = (struct rt_can_rx_fifo *)can->can_rx;
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
/* interrupt mode receive */
|
||||
RT_ASSERT(can->parent.open_flag & RT_DEVICE_FLAG_INT_RX);
|
||||
|
||||
rt_uint32_t no;
|
||||
no = event >> 8;
|
||||
ch = can->ops->recvmsg(can, &tmpmsg, no);
|
||||
if (ch == -1) break;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
can->status.rcvpkg++;
|
||||
|
@ -768,20 +892,24 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
|
|||
rt_list_remove(&listmsg->list);
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
rt_list_remove(&listmsg->hdrlist);
|
||||
if(listmsg->owner != RT_NULL && listmsg->owner->msgs) {
|
||||
if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
|
||||
{
|
||||
listmsg->owner->msgs--;
|
||||
}
|
||||
listmsg->owner = RT_NULL;
|
||||
#endif /*RT_CAN_USING_HDR*/
|
||||
RT_ASSERT(rx_fifo->freenumbers > 0);
|
||||
rx_fifo->freenumbers--;
|
||||
} else if(!rt_list_isempty(&rx_fifo->uselist)) {
|
||||
}
|
||||
else if (!rt_list_isempty(&rx_fifo->uselist))
|
||||
{
|
||||
listmsg = rt_list_entry(rx_fifo->uselist.next, struct rt_can_msg_list, list);
|
||||
can->status.dropedrcvpkg++;
|
||||
rt_list_remove(&listmsg->list);
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
rt_list_remove(&listmsg->hdrlist);
|
||||
if(listmsg->owner != RT_NULL && listmsg->owner->msgs) {
|
||||
if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
|
||||
{
|
||||
listmsg->owner->msgs--;
|
||||
}
|
||||
listmsg->owner = RT_NULL;
|
||||
|
@ -789,15 +917,19 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
|
|||
}
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
if(listmsg != RT_NULL) {
|
||||
|
||||
if (listmsg != RT_NULL)
|
||||
{
|
||||
rt_memcpy(&listmsg->data, &tmpmsg, sizeof(struct rt_can_msg));
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_list_insert_before(&rx_fifo->uselist, &listmsg->list);
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
hdr = tmpmsg.hdr;
|
||||
if(can->hdr != RT_NULL) {
|
||||
if (can->hdr != RT_NULL)
|
||||
{
|
||||
RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
|
||||
if(can->hdr[hdr].connected) {
|
||||
if (can->hdr[hdr].connected)
|
||||
{
|
||||
rt_list_insert_before(&can->hdr[hdr].list, &listmsg->hdrlist);
|
||||
listmsg->owner = &can->hdr[hdr];
|
||||
can->hdr[hdr].msgs++;
|
||||
|
@ -810,20 +942,24 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
|
|||
|
||||
/* invoke callback */
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
if(can->hdr != RT_NULL && can->hdr[hdr].connected && can->hdr[hdr].filter.ind) {
|
||||
RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
|
||||
if (can->hdr != RT_NULL && can->hdr[hdr].connected && can->hdr[hdr].filter.ind)
|
||||
{
|
||||
rt_size_t rx_length;
|
||||
RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
rx_length = can->hdr[hdr].msgs * sizeof(struct rt_can_msg);
|
||||
rt_hw_interrupt_enable(level);
|
||||
can->hdr[hdr].filter.ind(&can->parent, can->hdr[hdr].filter.args, hdr, rx_length);
|
||||
|
||||
} else
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (can->parent.rx_indicate != RT_NULL) {
|
||||
if (can->parent.rx_indicate != RT_NULL)
|
||||
{
|
||||
rt_size_t rx_length;
|
||||
/* get rx length */
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
/* get rx length */
|
||||
rx_length = rx_fifo->freenumbers * sizeof(struct rt_can_msg);
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
|
@ -831,6 +967,7 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
|
|||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case RT_CAN_EVENT_TX_DONE:
|
||||
case RT_CAN_EVENT_TX_FAIL:
|
||||
{
|
||||
|
@ -839,9 +976,12 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
|
|||
no = event >> 8;
|
||||
tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
|
||||
RT_ASSERT(tx_fifo != RT_NULL);
|
||||
if((event & 0xff) == RT_CAN_EVENT_TX_DONE) {
|
||||
if ((event & 0xff) == RT_CAN_EVENT_TX_DONE)
|
||||
{
|
||||
tx_fifo->buffer[no].result = RT_CAN__SND_RESUTL_OK;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
tx_fifo->buffer[no].result = RT_CAN__SND_RESUTL_ERR;
|
||||
}
|
||||
rt_completion_done(&(tx_fifo->buffer[no].completion));
|
||||
|
@ -849,24 +989,30 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
int cmd_canstat(int argc, void **argv)
|
||||
{
|
||||
static const char* ErrCode[] = {
|
||||
static const char *ErrCode[] =
|
||||
{
|
||||
"No Error!",
|
||||
"Warning !",
|
||||
"Passive !",
|
||||
"Bus Off !"
|
||||
};
|
||||
if(argc >= 2) {
|
||||
|
||||
if (argc >= 2)
|
||||
{
|
||||
struct rt_can_status status;
|
||||
rt_device_t candev = rt_device_find(argv[1]);
|
||||
if(!candev) {
|
||||
if (!candev)
|
||||
{
|
||||
rt_kprintf(" Can't find can device %s\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
rt_kprintf(" Finded can device: %s...", argv[1]);
|
||||
struct rt_can_status status;
|
||||
|
||||
rt_device_control(candev, RT_CAN_CMD_GET_STATUS, &status);
|
||||
rt_kprintf("\n Receive...error..count: %010ld. Send.....error....count: %010ld.",
|
||||
status.rcverrcnt, status.snderrcnt);
|
||||
|
@ -876,7 +1022,8 @@ int cmd_canstat(int argc,void** argv)
|
|||
status.ackerrcnt, status.biterrcnt);
|
||||
rt_kprintf("\n CRC.......error..count: %010ld. Error.code.[%010ld]: ",
|
||||
status.crcerrcnt, status.errcode);
|
||||
switch(status.errcode) {
|
||||
switch (status.errcode)
|
||||
{
|
||||
case 0:
|
||||
rt_kprintf("%s.", ErrCode[0]);
|
||||
break;
|
||||
|
@ -898,7 +1045,9 @@ int cmd_canstat(int argc,void** argv)
|
|||
status.rcvpkg, status.dropedrcvpkg);
|
||||
rt_kprintf("\n Total..send...packages: %010ld. Droped...send..packages: %010ld.\n",
|
||||
status.sndpkg + status.dropedsndpkg, status.dropedsndpkg);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf(" Invalid Call %s\n", argv[0]);
|
||||
rt_kprintf(" Please using %s cannamex .Here canname is driver name and x is candrive number.\n", argv[0]);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue