[DeviceDrivers] code cleanup for CAN driver and fix some critical issue

This commit is contained in:
BernardXiong 2015-07-06 10:38:07 +08:00
parent eb23f0def3
commit 1488568801
1 changed files with 616 additions and 467 deletions

View File

@ -10,7 +10,9 @@
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2015-05-14 aubrcool@qq.com first version * 2015-05-14 aubrcool@qq.com first version
* 2015-07-06 Bernard code cleanup.
*/ */
#include <rthw.h> #include <rthw.h>
#include <rtthread.h> #include <rtthread.h>
#include <rtdevice.h> #include <rtdevice.h>
@ -33,6 +35,7 @@ static rt_err_t rt_can_init(struct rt_device *dev)
return result; return result;
} }
/* /*
* can interrupt routines * 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) while (msgs)
{ {
rt_base_t level; rt_base_t level;
rt_int32_t hdr;
struct rt_can_msg_list *listmsg = RT_NULL; struct rt_can_msg_list *listmsg = RT_NULL;
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
#ifdef RT_CAN_USING_HDR #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)) 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); listmsg = rt_list_entry(can->hdr[hdr].list.next, struct rt_can_msg_list, hdrlist);
rt_list_remove(&listmsg->list); rt_list_remove(&listmsg->list);
rt_list_remove(&listmsg->hdrlist); rt_list_remove(&listmsg->hdrlist);
if(can->hdr[hdr].msgs) { if (can->hdr[hdr].msgs)
{
can->hdr[hdr].msgs--; can->hdr[hdr].msgs--;
} }
listmsg->owner = RT_NULL; listmsg->owner = RT_NULL;
} else if(hdr == -1) }
else if (hdr == -1)
#endif /*RT_CAN_USING_HDR*/ #endif /*RT_CAN_USING_HDR*/
if (!rt_list_isempty(&rx_fifo->uselist)) 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); rt_list_remove(&listmsg->list);
#ifdef RT_CAN_USING_HDR #ifdef RT_CAN_USING_HDR
rt_list_remove(&listmsg->hdrlist); 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->msgs--;
} }
listmsg->owner = RT_NULL; 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_ASSERT(rx_fifo->freenumbers <= can->config.msgboxsz);
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
listmsg = RT_NULL; listmsg = RT_NULL;
} else { }
else
{
break; break;
} }
data ++; msgs -= sizeof(struct rt_can_msg); data ++;
msgs -= sizeof(struct rt_can_msg);
} }
return (size - msgs); 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_base_t level;
rt_uint32_t no; rt_uint32_t no;
rt_uint32_t result;
struct rt_can_sndbxinx_list *tx_tosnd = RT_NULL; struct rt_can_sndbxinx_list *tx_tosnd = RT_NULL;
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if (!rt_list_isempty(&tx_fifo->freelist)) if (!rt_list_isempty(&tx_fifo->freelist))
{ {
tx_tosnd = rt_list_entry(tx_fifo->freelist.next, struct rt_can_sndbxinx_list, list); tx_tosnd = rt_list_entry(tx_fifo->freelist.next, struct rt_can_sndbxinx_list, list);
RT_ASSERT(tx_tosnd != RT_NULL); RT_ASSERT(tx_tosnd != RT_NULL);
rt_list_remove(&tx_tosnd->list); rt_list_remove(&tx_tosnd->list);
} else { }
else
{
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
rt_completion_wait(&(tx_fifo->completion), RT_WAITING_FOREVER); rt_completion_wait(&(tx_fifo->completion), RT_WAITING_FOREVER);
continue; continue;
} }
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
no = ((rt_uint32_t)tx_tosnd - (rt_uint32_t)tx_fifo->buffer) / sizeof(struct rt_can_sndbxinx_list); 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; tx_tosnd->result = RT_CAN__SND_RESUTL_WAIT;
if (can->ops->sendmsg(can, data , no)) 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; can->status.sndchange = 1;
rt_completion_wait(&(tx_tosnd->completion), RT_WAITING_FOREVER); rt_completion_wait(&(tx_tosnd->completion), RT_WAITING_FOREVER);
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
rt_uint32_t result = tx_tosnd->result; result = tx_tosnd->result;
if(!rt_list_isempty(&tx_tosnd->list)) { if (!rt_list_isempty(&tx_tosnd->list))
{
rt_list_remove(&tx_tosnd->list); rt_list_remove(&tx_tosnd->list);
} }
rt_list_insert_before(&tx_fifo->freelist, &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(); level = rt_hw_interrupt_disable();
can->status.sndpkg++; can->status.sndpkg++;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
data ++; msgs -= sizeof(struct rt_can_msg); data ++;
msgs -= sizeof(struct rt_can_msg);
if (!msgs) break; if (!msgs) break;
} }
else 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); return (size - msgs);
} }
rt_inline int _can_int_tx_priv(struct rt_can_device *can, const struct rt_can_msg *data, int msgs) rt_inline int _can_int_tx_priv(struct rt_can_device *can, const struct rt_can_msg *data, int msgs)
{ {
int size; int size;
rt_base_t level;
rt_uint32_t no, result;
struct rt_can_tx_fifo *tx_fifo; struct rt_can_tx_fifo *tx_fifo;
RT_ASSERT(can != RT_NULL); 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; tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
RT_ASSERT(tx_fifo != RT_NULL); RT_ASSERT(tx_fifo != RT_NULL);
rt_base_t level;
rt_uint32_t no;
rt_uint32_t result;
while (msgs) while (msgs)
{ {
no = data->priv; no = data->priv;
if(no >= can->config.sndboxnumber) { if (no >= can->config.sndboxnumber)
{
break; break;
} }
level = rt_hw_interrupt_disable(); 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_hw_interrupt_enable(level);
rt_completion_wait(&(tx_fifo->buffer[no].completion), RT_WAITING_FOREVER); rt_completion_wait(&(tx_fifo->buffer[no].completion), RT_WAITING_FOREVER);
continue; continue;
} }
tx_fifo->buffer[no].result = RT_CAN__SND_RESUTL_WAIT; tx_fifo->buffer[no].result = RT_CAN__SND_RESUTL_WAIT;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
if (can->ops->sendmsg(can, data , no) != RT_EOK) if (can->ops->sendmsg(can, data , no) != RT_EOK)
{ {
continue; continue;
} }
can->status.sndchange = 1; can->status.sndchange = 1;
rt_completion_wait(&(tx_fifo->buffer[no].completion), RT_WAITING_FOREVER); rt_completion_wait(&(tx_fifo->buffer[no].completion), RT_WAITING_FOREVER);
result = tx_fifo->buffer[no].result; result = tx_fifo->buffer[no].result;
if (result == RT_CAN__SND_RESUTL_OK) if (result == RT_CAN__SND_RESUTL_OK)
{ {
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
can->status.sndpkg++; can->status.sndpkg++;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
data ++; msgs -= sizeof(struct rt_can_msg); data ++;
msgs -= sizeof(struct rt_can_msg);
if (!msgs) break; if (!msgs) break;
} }
else 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); return (size - msgs);
} }
static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag) static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
{ {
struct rt_can_device *can; 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) if (oflag & RT_DEVICE_FLAG_INT_RX)
{ {
int i=0;
struct rt_can_rx_fifo *rx_fifo; struct rt_can_rx_fifo *rx_fifo;
rx_fifo = (struct rt_can_rx_fifo *) rt_malloc(sizeof(struct rt_can_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->freelist);
rt_list_init(&rx_fifo->uselist); rt_list_init(&rx_fifo->uselist);
rx_fifo->freenumbers = can->config.msgboxsz; rx_fifo->freenumbers = can->config.msgboxsz;
int i = 0;
for (i = 0; i < can->config.msgboxsz; i++) for (i = 0; i < can->config.msgboxsz; i++)
{ {
rt_list_insert_before(&rx_fifo->freelist, &rx_fifo->buffer[i].list); 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; can->can_rx = rx_fifo;
rt_exit_critical(); rt_exit_critical();
dev->open_flag |= RT_DEVICE_FLAG_INT_RX; dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
/* configure low level device */ /* configure low level device */
can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX); 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; can->can_rx = RT_NULL;
rt_exit_critical(); rt_exit_critical();
} }
} else { }
else
{
rt_exit_critical(); rt_exit_critical();
} }
rt_enter_critical(); rt_enter_critical();
if (can->can_tx == RT_NULL) if (can->can_tx == RT_NULL)
{ {
if (oflag & RT_DEVICE_FLAG_INT_TX) if (oflag & RT_DEVICE_FLAG_INT_TX)
{ {
int i = 0;
struct rt_can_tx_fifo *tx_fifo; struct rt_can_tx_fifo *tx_fifo;
tx_fifo = (struct rt_can_tx_fifo *) rt_malloc(sizeof(struct rt_can_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, rt_memset(tx_fifo->buffer, 0,
can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list)); can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
rt_list_init(&tx_fifo->freelist); rt_list_init(&tx_fifo->freelist);
int i = 0;
for (i = 0; i < can->config.sndboxnumber; i++) for (i = 0; i < can->config.sndboxnumber; i++)
{ {
rt_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list); 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; can->can_tx = RT_NULL;
rt_exit_critical(); rt_exit_critical();
} }
} else { }
else
{
rt_exit_critical(); rt_exit_critical();
} }
can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_CAN_INT_ERR); can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_CAN_INT_ERR);
#ifdef RT_CAN_USING_HDR #ifdef RT_CAN_USING_HDR
rt_enter_critical(); rt_enter_critical();
if(can->hdr == RT_NULL) { if (can->hdr == RT_NULL)
{
int i = 0;
struct rt_can_hdr *phdr; 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)); phdr = (struct rt_can_hdr *) rt_malloc(can->config.maxhdr * sizeof(struct rt_can_hdr));
RT_ASSERT(phdr != RT_NULL); RT_ASSERT(phdr != RT_NULL);
rt_memset(phdr, 0, can->config.maxhdr * sizeof(struct rt_can_hdr)); rt_memset(phdr, 0, can->config.maxhdr * sizeof(struct rt_can_hdr));
int i = 0;
for (i = 0; i < can->config.maxhdr; i++) for (i = 0; i < can->config.maxhdr; i++)
{ {
rt_list_init(&phdr[i].list); rt_list_init(&phdr[i].list);
} }
rt_enter_critical();
can->hdr = phdr; can->hdr = phdr;
rt_exit_critical(); rt_exit_critical();
} else { }
else
{
rt_exit_critical(); rt_exit_critical();
} }
#endif #endif
rt_enter_critical(); rt_enter_critical();
if(!can->timerinitflag) { if (!can->timerinitflag)
{
can->timerinitflag = 1; can->timerinitflag = 1;
rt_exit_critical(); rt_exit_critical();
#ifdef RT_CAN_USING_LED #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_mode(can->config.rcvled->pin, can->config.rcvled->mode);
rt_pin_write(can->config.rcvled->pin, can->config.rcvled->init); 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_mode(can->config.sndled->pin, can->config.sndled->mode);
rt_pin_write(can->config.sndled->pin, can->config.sndled->init); 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_mode(can->config.errled->pin, can->config.errled->mode);
rt_pin_write(can->config.errled->pin, can->config.errled->init); rt_pin_write(can->config.errled->pin, can->config.errled->init);
} }
#endif #endif
rt_timer_start(&can->timer); rt_timer_start(&can->timer);
} else { }
else
{
rt_exit_critical(); rt_exit_critical();
} }
return RT_EOK; return RT_EOK;
} }
@ -373,8 +425,10 @@ static rt_err_t rt_can_close(struct rt_device *dev)
/* this device has more reference count */ /* this device has more reference count */
if (dev->ref_count > 1) return RT_EOK; if (dev->ref_count > 1) return RT_EOK;
rt_enter_critical(); rt_enter_critical();
if(can->timerinitflag) { if (can->timerinitflag)
{
can->timerinitflag = 0; can->timerinitflag = 0;
rt_exit_critical(); rt_exit_critical();
rt_timer_stop(&can->timer); 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.sndled->init);
rt_pin_write(can->config.rcvled->pin, can->config.errled->init); rt_pin_write(can->config.rcvled->pin, can->config.errled->init);
#endif #endif
} else { }
else
{
rt_exit_critical(); rt_exit_critical();
} }
rt_enter_critical(); rt_enter_critical();
can->status_indicate.ind = RT_NULL; can->status_indicate.ind = RT_NULL;
can->status_indicate.args = RT_NULL; can->status_indicate.args = RT_NULL;
rt_exit_critical(); rt_exit_critical();
#ifdef RT_CAN_USING_HDR #ifdef RT_CAN_USING_HDR
rt_enter_critical(); rt_enter_critical();
if(can->hdr != RT_NULL) { if (can->hdr != RT_NULL)
rt_free(can->hdr); {
struct rt_can_hdr *hdr;
hdr = can->hdr;
can->hdr = RT_NULL; can->hdr = RT_NULL;
rt_exit_critical(); rt_exit_critical();
} else {
rt_free(hdr);
}
else
{
rt_exit_critical(); rt_exit_critical();
} }
#endif #endif
@ -444,6 +509,7 @@ static rt_size_t rt_can_read(struct rt_device *dev,
{ {
return _can_int_rx(can, buffer, size); return _can_int_rx(can, buffer, size);
} }
return 0; 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 (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
{ {
if(can->config.privmode) { if (can->config.privmode)
{
return _can_int_tx_priv(can, buffer, size); return _can_int_tx_priv(can, buffer, size);
} else { }
else
{
return _can_int_tx(can, buffer, size); return _can_int_tx(can, buffer, size);
} }
} }
@ -498,28 +567,36 @@ static rt_err_t rt_can_control(struct rt_device *dev,
break; break;
case RT_CAN_CMD_SET_PRIV: case RT_CAN_CMD_SET_PRIV:
/* configure device */ /* configure device */
if((rt_uint32_t)args != can->config.privmode) { 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;
int i; int i;
rt_base_t level; 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)); rt_completion_done(&(tx_fifo->completion));
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
for (i = 0; i < can->config.sndboxnumber; i++) for (i = 0; i < can->config.sndboxnumber; i++)
{ {
rt_list_remove(&tx_fifo->buffer[i].list); rt_list_remove(&tx_fifo->buffer[i].list);
} }
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
} else { }
else
{
for (i = 0; i < can->config.sndboxnumber; i++) for (i = 0; i < can->config.sndboxnumber; i++)
{ {
rt_base_t level;
level = rt_hw_interrupt_disable(); 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_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list);
} }
rt_hw_interrupt_enable(level); 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 #ifdef RT_CAN_USING_HDR
case RT_CAN_CMD_SET_FILTER: case RT_CAN_CMD_SET_FILTER:
res = can->ops->control(can, cmd, args); 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; return res;
} }
{ {
struct rt_can_filter_config *pfilter; struct rt_can_filter_config *pfilter;
struct rt_can_filter_item *pitem; struct rt_can_filter_item *pitem;
rt_uint32_t count; rt_uint32_t count;
rt_base_t level; rt_base_t level;
pfilter = (struct rt_can_filter_config *)args; pfilter = (struct rt_can_filter_config *)args;
count = pfilter->count; count = pfilter->count;
pitem = pfilter->items; pitem = pfilter->items;
if(pfilter->actived) { if (pfilter->actived)
while(count) { {
if(pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) { while (count)
{
if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
{
count--; count--;
pitem++; pitem++;
continue; continue;
} }
level = rt_hw_interrupt_disable(); 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, rt_memcpy(&can->hdr[pitem->hdr].filter, pitem,
sizeof(struct rt_can_filter_item)); sizeof(struct rt_can_filter_item));
can->hdr[pitem->hdr].connected = 1; can->hdr[pitem->hdr].connected = 1;
@ -565,15 +650,21 @@ static rt_err_t rt_can_control(struct rt_device *dev,
count--; count--;
pitem++; pitem++;
} }
} else { }
while(count) { else
if(pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) { {
while (count)
{
if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
{
count--; count--;
pitem++; pitem++;
continue; continue;
} }
level = rt_hw_interrupt_disable(); 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, rt_memset(&can->hdr[pitem->hdr].filter, 0,
sizeof(struct rt_can_filter_item)); sizeof(struct rt_can_filter_item));
can->hdr[pitem->hdr].connected = 0; can->hdr[pitem->hdr].connected = 0;
@ -602,6 +693,7 @@ static rt_err_t rt_can_control(struct rt_device *dev,
return RT_EOK; return RT_EOK;
} }
/* /*
* can timer * can timer
*/ */
@ -611,67 +703,97 @@ static void cantimeout(void* arg)
rt_uint32_t ledonflag = 0; rt_uint32_t ledonflag = 0;
#endif /*RT_CAN_USING_LED*/ #endif /*RT_CAN_USING_LED*/
rt_can_t can = (rt_can_t)arg; rt_can_t can = (rt_can_t)arg;
rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status); 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 #ifdef RT_CAN_USING_LED
ledonflag = 1; ledonflag = 1;
#endif /*RT_CAN_USING_LED*/ #endif /*RT_CAN_USING_LED*/
can->timerinitflag = 0xFF; can->timerinitflag = 0xFF;
} }
#ifdef RT_CAN_USING_LED #ifdef RT_CAN_USING_LED
if(can->config.rcvled != RT_NULL && can->config.sndled == RT_NULL) { if (can->config.rcvled != RT_NULL && can->config.sndled == RT_NULL)
if(ledonflag == 1) { {
if (ledonflag == 1)
{
rt_pin_write(can->config.rcvled->pin, can->config.rcvled->init ? 0 : 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) if (can->status.rcvchange == 1 || can->status.sndchange == 1)
{ {
can->status.rcvchange = 0; can->status.rcvchange = 0;
can->status.sndchange = 0; can->status.sndchange = 0;
rt_pin_write(can->config.rcvled->pin, rt_pin_read(can->config.rcvled->pin) ? 0 : 1); 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); 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.rcvled->pin, can->config.rcvled->init ? 0 : 1);
rt_pin_write(can->config.sndled->pin, can->config.sndled->init ? 0 : 1); rt_pin_write(can->config.sndled->pin, can->config.sndled->init ? 0 : 1);
} else { }
else
{
if (can->status.rcvchange == 1) if (can->status.rcvchange == 1)
{ {
can->status.rcvchange = 0; can->status.rcvchange = 0;
rt_pin_write(can->config.rcvled->pin, rt_pin_read(can->config.rcvled->pin) ? 0 : 1); 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); rt_pin_write(can->config.rcvled->pin, can->config.rcvled->init);
} }
if (can->status.sndchange == 1) if (can->status.sndchange == 1)
{ {
can->status.sndchange = 0; can->status.sndchange = 0;
rt_pin_write(can->config.sndled->pin, rt_pin_read(can->config.sndled->pin) ? 0 : 1); 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); 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); rt_pin_write(can->config.sndled->pin, can->config.sndled->init ? 0 : 1);
} else { }
else
{
if (can->status.rcvchange == 1 || can->status.sndchange == 1) if (can->status.rcvchange == 1 || can->status.sndchange == 1)
{ {
can->status.rcvchange = 0; can->status.rcvchange = 0;
can->status.sndchange = 0; can->status.sndchange = 0;
rt_pin_write(can->config.sndled->pin, rt_pin_read(can->config.sndled->pin) ? 0 : 1); 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); 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); 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); 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); 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 #endif
int ch = -1; int ch = -1;
rt_base_t level; rt_base_t level;
rt_uint32_t no;
rx_fifo = (struct rt_can_rx_fifo *)can->can_rx; rx_fifo = (struct rt_can_rx_fifo *)can->can_rx;
RT_ASSERT(rx_fifo != RT_NULL); RT_ASSERT(rx_fifo != RT_NULL);
/* interrupt mode receive */ /* interrupt mode receive */
RT_ASSERT(can->parent.open_flag & RT_DEVICE_FLAG_INT_RX); RT_ASSERT(can->parent.open_flag & RT_DEVICE_FLAG_INT_RX);
rt_uint32_t no;
no = event >> 8; no = event >> 8;
ch = can->ops->recvmsg(can, &tmpmsg, no); ch = can->ops->recvmsg(can, &tmpmsg, no);
if (ch == -1) break; if (ch == -1) break;
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
can->status.rcvpkg++; can->status.rcvpkg++;
@ -768,20 +892,24 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
rt_list_remove(&listmsg->list); rt_list_remove(&listmsg->list);
#ifdef RT_CAN_USING_HDR #ifdef RT_CAN_USING_HDR
rt_list_remove(&listmsg->hdrlist); 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->msgs--;
} }
listmsg->owner = RT_NULL; listmsg->owner = RT_NULL;
#endif /*RT_CAN_USING_HDR*/ #endif /*RT_CAN_USING_HDR*/
RT_ASSERT(rx_fifo->freenumbers > 0); RT_ASSERT(rx_fifo->freenumbers > 0);
rx_fifo->freenumbers--; 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); listmsg = rt_list_entry(rx_fifo->uselist.next, struct rt_can_msg_list, list);
can->status.dropedrcvpkg++; can->status.dropedrcvpkg++;
rt_list_remove(&listmsg->list); rt_list_remove(&listmsg->list);
#ifdef RT_CAN_USING_HDR #ifdef RT_CAN_USING_HDR
rt_list_remove(&listmsg->hdrlist); 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->msgs--;
} }
listmsg->owner = RT_NULL; listmsg->owner = RT_NULL;
@ -789,15 +917,19 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
} }
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
if(listmsg != RT_NULL) {
if (listmsg != RT_NULL)
{
rt_memcpy(&listmsg->data, &tmpmsg, sizeof(struct rt_can_msg)); rt_memcpy(&listmsg->data, &tmpmsg, sizeof(struct rt_can_msg));
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
rt_list_insert_before(&rx_fifo->uselist, &listmsg->list); rt_list_insert_before(&rx_fifo->uselist, &listmsg->list);
#ifdef RT_CAN_USING_HDR #ifdef RT_CAN_USING_HDR
hdr = tmpmsg.hdr; hdr = tmpmsg.hdr;
if(can->hdr != RT_NULL) { if (can->hdr != RT_NULL)
{
RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0); 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); rt_list_insert_before(&can->hdr[hdr].list, &listmsg->hdrlist);
listmsg->owner = &can->hdr[hdr]; listmsg->owner = &can->hdr[hdr];
can->hdr[hdr].msgs++; can->hdr[hdr].msgs++;
@ -810,20 +942,24 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
/* invoke callback */ /* invoke callback */
#ifdef RT_CAN_USING_HDR #ifdef RT_CAN_USING_HDR
if(can->hdr != RT_NULL && can->hdr[hdr].connected && can->hdr[hdr].filter.ind) { if (can->hdr != RT_NULL && can->hdr[hdr].connected && can->hdr[hdr].filter.ind)
RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0); {
rt_size_t rx_length; rt_size_t rx_length;
RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
rx_length = can->hdr[hdr].msgs * sizeof(struct rt_can_msg); rx_length = can->hdr[hdr].msgs * sizeof(struct rt_can_msg);
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
can->hdr[hdr].filter.ind(&can->parent, can->hdr[hdr].filter.args, hdr, rx_length); can->hdr[hdr].filter.ind(&can->parent, can->hdr[hdr].filter.args, hdr, rx_length);
}
} else else
#endif #endif
if (can->parent.rx_indicate != RT_NULL) { if (can->parent.rx_indicate != RT_NULL)
{
rt_size_t rx_length; rt_size_t rx_length;
/* get rx length */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
/* get rx length */
rx_length = rx_fifo->freenumbers * sizeof(struct rt_can_msg); rx_length = rx_fifo->freenumbers * sizeof(struct rt_can_msg);
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
@ -831,6 +967,7 @@ void rt_hw_can_isr(struct rt_can_device *can, int event)
} }
break; break;
} }
case RT_CAN_EVENT_TX_DONE: case RT_CAN_EVENT_TX_DONE:
case RT_CAN_EVENT_TX_FAIL: 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; no = event >> 8;
tx_fifo = (struct rt_can_tx_fifo *) can->can_tx; tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
RT_ASSERT(tx_fifo != RT_NULL); 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; tx_fifo->buffer[no].result = RT_CAN__SND_RESUTL_OK;
} else { }
else
{
tx_fifo->buffer[no].result = RT_CAN__SND_RESUTL_ERR; tx_fifo->buffer[no].result = RT_CAN__SND_RESUTL_ERR;
} }
rt_completion_done(&(tx_fifo->buffer[no].completion)); 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 #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
int cmd_canstat(int argc, void **argv) int cmd_canstat(int argc, void **argv)
{ {
static const char* ErrCode[] = { static const char *ErrCode[] =
{
"No Error!", "No Error!",
"Warning !", "Warning !",
"Passive !", "Passive !",
"Bus Off !" "Bus Off !"
}; };
if(argc >= 2) {
if (argc >= 2)
{
struct rt_can_status status;
rt_device_t candev = rt_device_find(argv[1]); rt_device_t candev = rt_device_find(argv[1]);
if(!candev) { if (!candev)
{
rt_kprintf(" Can't find can device %s\n", argv[1]); rt_kprintf(" Can't find can device %s\n", argv[1]);
return -1; return -1;
} }
rt_kprintf(" Finded can device: %s...", argv[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_device_control(candev, RT_CAN_CMD_GET_STATUS, &status);
rt_kprintf("\n Receive...error..count: %010ld. Send.....error....count: %010ld.", rt_kprintf("\n Receive...error..count: %010ld. Send.....error....count: %010ld.",
status.rcverrcnt, status.snderrcnt); status.rcverrcnt, status.snderrcnt);
@ -876,7 +1022,8 @@ int cmd_canstat(int argc,void** argv)
status.ackerrcnt, status.biterrcnt); status.ackerrcnt, status.biterrcnt);
rt_kprintf("\n CRC.......error..count: %010ld. Error.code.[%010ld]: ", rt_kprintf("\n CRC.......error..count: %010ld. Error.code.[%010ld]: ",
status.crcerrcnt, status.errcode); status.crcerrcnt, status.errcode);
switch(status.errcode) { switch (status.errcode)
{
case 0: case 0:
rt_kprintf("%s.", ErrCode[0]); rt_kprintf("%s.", ErrCode[0]);
break; break;
@ -898,7 +1045,9 @@ int cmd_canstat(int argc,void** argv)
status.rcvpkg, status.dropedrcvpkg); status.rcvpkg, status.dropedrcvpkg);
rt_kprintf("\n Total..send...packages: %010ld. Droped...send..packages: %010ld.\n", rt_kprintf("\n Total..send...packages: %010ld. Droped...send..packages: %010ld.\n",
status.sndpkg + status.dropedsndpkg, status.dropedsndpkg); status.sndpkg + status.dropedsndpkg, status.dropedsndpkg);
} else { }
else
{
rt_kprintf(" Invalid Call %s\n", argv[0]); 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]); rt_kprintf(" Please using %s cannamex .Here canname is driver name and x is candrive number.\n", argv[0]);
} }