[LPC4088] format code by Astyle.

This commit is contained in:
aozima 2015-07-08 10:44:49 +08:00
parent defa6f37ff
commit b15fb70616
1 changed files with 906 additions and 809 deletions

View File

@ -27,10 +27,13 @@
#include "lpc_exti.h" #include "lpc_exti.h"
#include "lpc_clkpwr.h" #include "lpc_clkpwr.h"
struct lpccandata { struct lpccandata
{
en_CAN_unitId id; en_CAN_unitId id;
}; };
static const rt_uint32_t LPCBAUDTAB[] = {
static const rt_uint32_t LPCBAUDTAB[] =
{
1000000, 1000000,
800000, 800000,
500000, 500000,
@ -41,11 +44,13 @@ static const rt_uint32_t LPCBAUDTAB[] = {
20000, 20000,
10000, 10000,
}; };
static LPC_CAN_TypeDef* lcpcan_get_reg_base(rt_uint32_t id) static LPC_CAN_TypeDef* lcpcan_get_reg_base(rt_uint32_t id)
{ {
LPC_CAN_TypeDef* pCan; LPC_CAN_TypeDef* pCan;
switch (id) { switch (id)
{
case CAN_ID_1: case CAN_ID_1:
pCan = LPC_CAN1; pCan = LPC_CAN1;
break; break;
@ -58,6 +63,7 @@ static LPC_CAN_TypeDef* lcpcan_get_reg_base(rt_uint32_t id)
} }
return pCan; return pCan;
} }
static void lpccan_irqstate_init(rt_uint32_t id) static void lpccan_irqstate_init(rt_uint32_t id)
{ {
LPC_CAN_TypeDef* pCan = lcpcan_get_reg_base(id); LPC_CAN_TypeDef* pCan = lcpcan_get_reg_base(id);
@ -74,6 +80,7 @@ static void lpccan_irqstate_init(rt_uint32_t id)
rt_int32_t i = pCan->ICR; rt_int32_t i = pCan->ICR;
pCan->MOD = 0;// Return Normal operating pCan->MOD = 0;// Return Normal operating
} }
static void lpccan_baud_set(rt_uint32_t id, rt_uint32_t baud) static void lpccan_baud_set(rt_uint32_t id, rt_uint32_t baud)
{ {
uint32_t result = 0; uint32_t result = 0;
@ -110,6 +117,7 @@ static void lpccan_baud_set(rt_uint32_t id, rt_uint32_t baud)
/* Return to normal operating */ /* Return to normal operating */
pCan->MOD = 0; pCan->MOD = 0;
} }
static void lpccan_init_alut_ram(void) static void lpccan_init_alut_ram(void)
{ {
//Reset CANAF value //Reset CANAF value
@ -125,17 +133,21 @@ static void lpccan_init_alut_ram(void)
// Set AF Mode // Set AF Mode
CAN_SetAFMode(CAN_NORMAL); CAN_SetAFMode(CAN_NORMAL);
} }
#ifdef RT_USING_LPCCAN1 #ifdef RT_USING_LPCCAN1
static void lpccan1_turnon_clk(void) static void lpccan1_turnon_clk(void)
{ {
CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, ENABLE); CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, ENABLE);
} }
static void lpccan1_filter_init(struct rt_can_device *can) static void lpccan1_filter_init(struct rt_can_device *can)
{ {
} }
static void lpccan1_hw_init(enum CANBAUD baud, CAN_MODE_Type mode)
static void lpccan1_hw_init(uint32_t baud, CAN_MODE_Type mode)
{ {
if(mode != CAN_SELFTEST_MODE) { if(mode != CAN_SELFTEST_MODE)
{
#ifndef LPCCAN1_USEING_GPIO_SECOND #ifndef LPCCAN1_USEING_GPIO_SECOND
PINSEL_ConfigPin (0, 0, 1); PINSEL_ConfigPin (0, 0, 1);
PINSEL_ConfigPin (0, 1, 1); PINSEL_ConfigPin (0, 1, 1);
@ -150,23 +162,28 @@ static void lpccan1_hw_init(enum CANBAUD baud, CAN_MODE_Type mode)
lpccan1_turnon_clk(); lpccan1_turnon_clk();
lpccan_baud_set(CAN_1, baud); lpccan_baud_set(CAN_1, baud);
CAN_ModeConfig(CAN_1, mode, ENABLE); CAN_ModeConfig(CAN_1, mode, ENABLE);
if(mode == CAN_SELFTEST_MODE) { if(mode == CAN_SELFTEST_MODE)
{
//CAN_ModeConfig(CAN_1, CAN_TEST_MODE, ENABLE); //CAN_ModeConfig(CAN_1, CAN_TEST_MODE, ENABLE);
CAN_SetAFMode(CAN_ACC_BP); CAN_SetAFMode(CAN_ACC_BP);
} }
} }
#endif /*RT_USING_LPCCAN1*/ #endif /*RT_USING_LPCCAN1*/
#ifdef RT_USING_LPCCAN2 #ifdef RT_USING_LPCCAN2
static void lpccan2_turnon_clk(void) static void lpccan2_turnon_clk(void)
{ {
CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, ENABLE); CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, ENABLE);
} }
static void lpccan2_filter_init(struct rt_can_device *can) static void lpccan2_filter_init(struct rt_can_device *can)
{ {
} }
static void lpccan2_hw_init(enum CANBAUD baud, CAN_MODE_Type mode) static void lpccan2_hw_init(enum CANBAUD baud, CAN_MODE_Type mode)
{ {
if(mode != CAN_SELFTEST_MODE) { if(mode != CAN_SELFTEST_MODE)
{
#ifndef LPCCAN2_USEING_GPIO_SECOND #ifndef LPCCAN2_USEING_GPIO_SECOND
PINSEL_ConfigPin (0, 4, 2); PINSEL_ConfigPin (0, 4, 2);
PINSEL_ConfigPin (0, 5, 2); PINSEL_ConfigPin (0, 5, 2);
@ -182,15 +199,18 @@ static void lpccan2_hw_init(enum CANBAUD baud, CAN_MODE_Type mode)
#endif /*RT_USING_LPCCAN1*/ #endif /*RT_USING_LPCCAN1*/
lpccan_baud_set(CAN_2, baud); lpccan_baud_set(CAN_2, baud);
CAN_ModeConfig(CAN_2, mode, ENABLE); CAN_ModeConfig(CAN_2, mode, ENABLE);
if(mode == CAN_SELFTEST_MODE) { if(mode == CAN_SELFTEST_MODE)
{
CAN_SetAFMode(CAN_ACC_BP); CAN_SetAFMode(CAN_ACC_BP);
} }
} }
#endif /*RT_USING_LPCCAN2*/ #endif /*RT_USING_LPCCAN2*/
static rt_err_t configure(struct rt_can_device *can, struct can_configure *cfg) static rt_err_t configure(struct rt_can_device *can, struct can_configure *cfg)
{ {
CAN_MODE_Type mode; CAN_MODE_Type mode;
switch(cfg->mode) { switch(cfg->mode)
{
case RT_CAN_MODE_NORMAL: case RT_CAN_MODE_NORMAL:
mode = CAN_OPERATING_MODE; mode = CAN_OPERATING_MODE;
break; break;
@ -223,6 +243,7 @@ static rt_err_t configure(struct rt_can_device *can, struct can_configure *cfg)
#endif /*RT_USING_LPCCAN2*/ #endif /*RT_USING_LPCCAN2*/
return RT_EOK; return RT_EOK;
} }
static CAN_ERROR findfilter(struct lpccandata* plpccan, struct rt_can_filter_item* pitem, rt_int32_t* pos) static CAN_ERROR findfilter(struct lpccandata* plpccan, struct rt_can_filter_item* pitem, rt_int32_t* pos)
{ {
extern uint16_t CANAF_FullCAN_cnt; extern uint16_t CANAF_FullCAN_cnt;
@ -235,12 +256,16 @@ static CAN_ERROR findfilter(struct lpccandata* plpccan, struct rt_can_filter_ite
rt_int16_t cnt1 = 0, cnt2 = 0, bound1 = 0; rt_int16_t cnt1 = 0, cnt2 = 0, bound1 = 0;
CAN_ID_FORMAT_Type format; CAN_ID_FORMAT_Type format;
*pos = -1; *pos = -1;
if(pitem->ide) { if(pitem->ide)
{
format = EXT_ID_FORMAT; format = EXT_ID_FORMAT;
} else { }
else
{
format = STD_ID_FORMAT; format = STD_ID_FORMAT;
} }
if(pitem->mode) { if(pitem->mode)
{
rt_uint32_t id = pitem->id; rt_uint32_t id = pitem->id;
if(format == STD_ID_FORMAT) if(format == STD_ID_FORMAT)
{ {
@ -255,7 +280,8 @@ static CAN_ERROR findfilter(struct lpccandata* plpccan, struct rt_can_filter_ite
else if (CANAF_std_cnt == 1) else if (CANAF_std_cnt == 1)
{ {
cnt2 = (CANAF_FullCAN_cnt + 1) >> 1; cnt2 = (CANAF_FullCAN_cnt + 1) >> 1;
if(id != LPC_CANAF_RAM->mask[cnt2] >> 16) { if(id != LPC_CANAF_RAM->mask[cnt2] >> 16)
{
return CAN_ENTRY_NOT_EXIT_ERROR; return CAN_ENTRY_NOT_EXIT_ERROR;
} }
} }
@ -319,7 +345,9 @@ static CAN_ERROR findfilter(struct lpccandata* plpccan, struct rt_can_filter_ite
cnt2++; cnt2++;
} }
} }
} else { }
else
{
rt_uint32_t lowerID = pitem->id; rt_uint32_t lowerID = pitem->id;
rt_uint32_t upperID = pitem->mask; rt_uint32_t upperID = pitem->mask;
rt_uint32_t LID,UID; rt_uint32_t LID,UID;
@ -338,7 +366,8 @@ static CAN_ERROR findfilter(struct lpccandata* plpccan, struct rt_can_filter_ite
{ {
bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt; bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt;
while(cnt1 < bound1) while(cnt1 < bound1)
{ //compare controller first {
//compare controller first
while((LPC_CANAF_RAM->mask[cnt1] >> 29) < (plpccan->id))//increase until meet greater or equal controller while((LPC_CANAF_RAM->mask[cnt1] >> 29) < (plpccan->id))//increase until meet greater or equal controller
cnt1++; cnt1++;
buf0 = LPC_CANAF_RAM->mask[cnt1]; buf0 = LPC_CANAF_RAM->mask[cnt1];
@ -429,6 +458,7 @@ static CAN_ERROR findfilter(struct lpccandata* plpccan, struct rt_can_filter_ite
} }
return CAN_ENTRY_NOT_EXIT_ERROR; return CAN_ENTRY_NOT_EXIT_ERROR;
} }
static rt_err_t setfilter(struct lpccandata* plpccan,struct rt_can_filter_config *pconfig) static rt_err_t setfilter(struct lpccandata* plpccan,struct rt_can_filter_config *pconfig)
{ {
struct rt_can_filter_item* pitem = pconfig->items; struct rt_can_filter_item* pitem = pconfig->items;
@ -436,39 +466,61 @@ static rt_err_t setfilter(struct lpccandata* plpccan,struct rt_can_filter_config
rt_int32_t pos; rt_int32_t pos;
CAN_ID_FORMAT_Type format; CAN_ID_FORMAT_Type format;
CAN_ERROR lpccanres; CAN_ERROR lpccanres;
while(count) { while(count)
if(pitem->ide) { {
if(pitem->ide)
{
format = EXT_ID_FORMAT; format = EXT_ID_FORMAT;
} else { }
else
{
format = STD_ID_FORMAT; format = STD_ID_FORMAT;
} }
lpccanres = findfilter(plpccan, pitem, &pos); lpccanres = findfilter(plpccan, pitem, &pos);
if(pconfig->actived && lpccanres != CAN_OK) { if(pconfig->actived && lpccanres != CAN_OK)
if(pitem->mode) { {
if(pitem->mode)
{
lpccanres = CAN_LoadGroupEntry(plpccan->id, pitem->id, pitem->mask, format); lpccanres = CAN_LoadGroupEntry(plpccan->id, pitem->id, pitem->mask, format);
} else { }
else
{
lpccanres = CAN_LoadExplicitEntry(plpccan->id, pitem->id, format); lpccanres = CAN_LoadExplicitEntry(plpccan->id, pitem->id, format);
} }
} else if(!pconfig->actived && lpccanres == CAN_OK) { }
else if(!pconfig->actived && lpccanres == CAN_OK)
{
AFLUT_ENTRY_Type type; AFLUT_ENTRY_Type type;
if(pitem->mode) { if(pitem->mode)
if(format == EXT_ID_FORMAT) { {
if(format == EXT_ID_FORMAT)
{
type = GROUP_EXTEND_ENTRY; type = GROUP_EXTEND_ENTRY;
} else { }
else
{
type = GROUP_STANDARD_ENTRY; type = GROUP_STANDARD_ENTRY;
} }
} else { }
if(format == EXT_ID_FORMAT) { else
{
if(format == EXT_ID_FORMAT)
{
type = EXPLICIT_EXTEND_ENTRY; type = EXPLICIT_EXTEND_ENTRY;
} else { }
else
{
type = EXPLICIT_STANDARD_ENTRY; type = EXPLICIT_STANDARD_ENTRY;
} }
} }
lpccanres = CAN_RemoveEntry(type, (rt_uint16_t)(pos)); lpccanres = CAN_RemoveEntry(type, (rt_uint16_t)(pos));
} else if(!pconfig->actived && lpccanres != CAN_OK) { }
else if(!pconfig->actived && lpccanres != CAN_OK)
{
lpccanres = CAN_OK; lpccanres = CAN_OK;
} }
if(lpccanres != CAN_OK) { if(lpccanres != CAN_OK)
{
return RT_EIO; return RT_EIO;
} }
pitem++; pitem++;
@ -476,6 +528,7 @@ static rt_err_t setfilter(struct lpccandata* plpccan,struct rt_can_filter_config
} }
return RT_EOK; return RT_EOK;
} }
static rt_err_t control(struct rt_can_device *can, int cmd, void *arg) static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
{ {
struct lpccandata* plpccan; struct lpccandata* plpccan;
@ -491,11 +544,15 @@ static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
{ {
CAN_IRQCmd(plpccan->id, CANINT_RIE, DISABLE); CAN_IRQCmd(plpccan->id, CANINT_RIE, DISABLE);
CAN_IRQCmd(plpccan->id, CANINT_DOIE, DISABLE); CAN_IRQCmd(plpccan->id, CANINT_DOIE, DISABLE);
} else if(argval == RT_DEVICE_FLAG_INT_TX) { }
else if(argval == RT_DEVICE_FLAG_INT_TX)
{
CAN_IRQCmd(plpccan->id, CANINT_TIE1, DISABLE); CAN_IRQCmd(plpccan->id, CANINT_TIE1, DISABLE);
CAN_IRQCmd(plpccan->id, CANINT_TIE2, DISABLE); CAN_IRQCmd(plpccan->id, CANINT_TIE2, DISABLE);
CAN_IRQCmd(plpccan->id, CANINT_TIE3, DISABLE); CAN_IRQCmd(plpccan->id, CANINT_TIE3, DISABLE);
} else if(argval == RT_DEVICE_CAN_INT_ERR) { }
else if(argval == RT_DEVICE_CAN_INT_ERR)
{
CAN_IRQCmd(plpccan->id, CANINT_EIE, DISABLE); CAN_IRQCmd(plpccan->id, CANINT_EIE, DISABLE);
} }
break; break;
@ -505,11 +562,15 @@ static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
{ {
CAN_IRQCmd(plpccan->id, CANINT_RIE, ENABLE); CAN_IRQCmd(plpccan->id, CANINT_RIE, ENABLE);
CAN_IRQCmd(plpccan->id, CANINT_DOIE, ENABLE); CAN_IRQCmd(plpccan->id, CANINT_DOIE, ENABLE);
} else if(argval == RT_DEVICE_FLAG_INT_TX) { }
else if(argval == RT_DEVICE_FLAG_INT_TX)
{
CAN_IRQCmd(plpccan->id, CANINT_TIE1, ENABLE); CAN_IRQCmd(plpccan->id, CANINT_TIE1, ENABLE);
CAN_IRQCmd(plpccan->id, CANINT_TIE2, ENABLE); CAN_IRQCmd(plpccan->id, CANINT_TIE2, ENABLE);
CAN_IRQCmd(plpccan->id, CANINT_TIE3, ENABLE); CAN_IRQCmd(plpccan->id, CANINT_TIE3, ENABLE);
} else if(argval == RT_DEVICE_CAN_INT_ERR) { }
else if(argval == RT_DEVICE_CAN_INT_ERR)
{
CAN_IRQCmd(plpccan->id, CANINT_EIE, ENABLE); CAN_IRQCmd(plpccan->id, CANINT_EIE, ENABLE);
} }
break; break;
@ -518,13 +579,15 @@ static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
case RT_CAN_CMD_SET_MODE: case RT_CAN_CMD_SET_MODE:
argval = (rt_uint32_t) arg; argval = (rt_uint32_t) arg;
if(argval != RT_CAN_MODE_NORMAL || if(argval != RT_CAN_MODE_NORMAL ||
argval != RT_CAN_MODE_LISEN) { argval != RT_CAN_MODE_LISEN)
{
return RT_ERROR; return RT_ERROR;
} }
if(argval != can->config.mode) if(argval != can->config.mode)
{ {
can->config.mode = argval; can->config.mode = argval;
switch(argval) { switch(argval)
{
case RT_CAN_MODE_NORMAL: case RT_CAN_MODE_NORMAL:
mode = CAN_OPERATING_MODE; mode = CAN_OPERATING_MODE;
break; break;
@ -538,7 +601,8 @@ static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
return RT_EIO; return RT_EIO;
} }
CAN_ModeConfig(plpccan->id, mode, ENABLE); CAN_ModeConfig(plpccan->id, mode, ENABLE);
if(mode == CAN_SELFTEST_MODE) { if(mode == CAN_SELFTEST_MODE)
{
//CAN_ModeConfig(CAN_1, CAN_TEST_MODE, ENABLE); //CAN_ModeConfig(CAN_1, CAN_TEST_MODE, ENABLE);
CAN_SetAFMode(CAN_ACC_BP); CAN_SetAFMode(CAN_ACC_BP);
} }
@ -554,7 +618,8 @@ static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
argval != CAN100kBaud && argval != CAN100kBaud &&
argval != CAN50kBaud && argval != CAN50kBaud &&
argval != CAN20kBaud && argval != CAN20kBaud &&
argval != CAN10kBaud ) { argval != CAN10kBaud )
{
return RT_ERROR; return RT_ERROR;
} }
if(argval != can->config.baud_rate) if(argval != can->config.baud_rate)
@ -566,7 +631,8 @@ static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
case RT_CAN_CMD_SET_PRIV: case RT_CAN_CMD_SET_PRIV:
argval = (rt_uint32_t) arg; argval = (rt_uint32_t) arg;
if(argval != RT_CAN_MODE_PRIV || if(argval != RT_CAN_MODE_PRIV ||
argval != RT_CAN_MODE_NOPRIV) { argval != RT_CAN_MODE_NOPRIV)
{
return RT_ERROR; return RT_ERROR;
} }
if(argval != can->config.privmode) if(argval != can->config.privmode)
@ -581,7 +647,8 @@ static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
can->status.rcverrcnt = 0; can->status.rcverrcnt = 0;
can->status.snderrcnt = 0; can->status.snderrcnt = 0;
can->status.errcode = 0; can->status.errcode = 0;
if(arg != &can->status) { if(arg != &can->status)
{
rt_memcpy(arg,&can->status,sizeof(can->status)); rt_memcpy(arg,&can->status,sizeof(can->status));
} }
} }
@ -589,6 +656,7 @@ static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
} }
return RT_EOK; return RT_EOK;
} }
static int sendmsg(struct rt_can_device *can, const void* buf, rt_uint32_t boxno) static int sendmsg(struct rt_can_device *can, const void* buf, rt_uint32_t boxno)
{ {
struct lpccandata* plpccan; struct lpccandata* plpccan;
@ -598,7 +666,8 @@ static int sendmsg(struct rt_can_device *can, const void* buf, rt_uint32_t boxno
RT_ASSERT(pCan != RT_NULL); RT_ASSERT(pCan != RT_NULL);
struct rt_can_msg* pmsg = (struct rt_can_msg*) buf; struct rt_can_msg* pmsg = (struct rt_can_msg*) buf;
rt_uint32_t SR_Mask; rt_uint32_t SR_Mask;
if(boxno > 2) { if(boxno > 2)
{
return RT_ERROR; return RT_ERROR;
} }
rt_uint32_t CMRMsk = 0x01 | (0x01 << (boxno + 5)); rt_uint32_t CMRMsk = 0x01 | (0x01 << (boxno + 5));
@ -608,7 +677,8 @@ static int sendmsg(struct rt_can_device *can, const void* buf, rt_uint32_t boxno
volatile unsigned int *pTDA = (&pCan->TFI1 + 2 + 4 * boxno); volatile unsigned int *pTDA = (&pCan->TFI1 + 2 + 4 * boxno);
volatile unsigned int *pTDB = (&pCan->TFI1 + 3 + 4 * boxno); volatile unsigned int *pTDB = (&pCan->TFI1 + 3 + 4 * boxno);
rt_uint32_t data; rt_uint32_t data;
if(pCan->SR & SR_Mask) { if(pCan->SR & SR_Mask)
{
/* Transmit Channel 1 is available */ /* Transmit Channel 1 is available */
/* Write frame informations and frame data into its CANxTFI1, /* Write frame informations and frame data into its CANxTFI1,
* CANxTID1, CANxTDA1, CANxTDB1 register */ * CANxTID1, CANxTDA1, CANxTDB1 register */
@ -631,7 +701,8 @@ static int sendmsg(struct rt_can_device *can, const void* buf, rt_uint32_t boxno
{ {
*pTFI &= ~(((uint32_t)1) << 31); *pTFI &= ~(((uint32_t)1) << 31);
} }
if(can->config.privmode) { if(can->config.privmode)
{
*pTFI &= ~0x000000FF; *pTFI &= ~0x000000FF;
*pTFI |= pmsg->priv; *pTFI |= pmsg->priv;
} }
@ -646,10 +717,13 @@ static int sendmsg(struct rt_can_device *can, const void* buf, rt_uint32_t boxno
/*Write transmission request*/ /*Write transmission request*/
pCan->CMR = CMRMsk; pCan->CMR = CMRMsk;
return RT_EOK; return RT_EOK;
} else { }
else
{
return RT_ERROR; return RT_ERROR;
} }
} }
static int recvmsg(struct rt_can_device *can, void* buf, rt_uint32_t boxno) static int recvmsg(struct rt_can_device *can, void* buf, rt_uint32_t boxno)
{ {
struct lpccandata* plpccan; struct lpccandata* plpccan;
@ -680,7 +754,8 @@ static int recvmsg(struct rt_can_device *can, void* buf, rt_uint32_t boxno)
pmsg->data[2] = (data & 0x00FF0000) >> 16; pmsg->data[2] = (data & 0x00FF0000) >> 16;
pmsg->data[3] = (data & 0xFF000000) >> 24; pmsg->data[3] = (data & 0xFF000000) >> 24;
/* Read second 4 data bytes */ /* Read second 4 data bytes */
if(pmsg->len > 4) { if(pmsg->len > 4)
{
data = pCan->RDB; data = pCan->RDB;
pmsg->data[4] = data & 0x000000FF; pmsg->data[4] = data & 0x000000FF;
pmsg->data[5] = (data & 0x0000FF00) >> 8; pmsg->data[5] = (data & 0x0000FF00) >> 8;
@ -714,11 +789,13 @@ static const struct rt_can_ops canops =
sendmsg, sendmsg,
recvmsg, recvmsg,
}; };
#ifdef RT_USING_LPCCAN1 #ifdef RT_USING_LPCCAN1
#ifdef RT_CAN_USING_LED #ifdef RT_CAN_USING_LED
#endif #endif
static struct lpccandata lpccandata1 = { static struct lpccandata lpccandata1 =
CAN_1, {
CAN_ID_1,
}; };
static struct rt_can_device lpccan1; static struct rt_can_device lpccan1;
#endif /*RT_USINGLPCCAN1*/ #endif /*RT_USINGLPCCAN1*/
@ -726,11 +803,13 @@ static struct rt_can_device lpccan1;
#ifdef RT_USING_LPCCAN2 #ifdef RT_USING_LPCCAN2
#ifdef RT_CAN_USING_LED #ifdef RT_CAN_USING_LED
#endif #endif
static struct lpccandata lpccandata2 = { static struct lpccandata lpccandata2 =
CAN_2, {
CAN_ID_2,
}; };
static struct rt_can_device lpccan2; static struct rt_can_device lpccan2;
#endif /*RT_USINGLPCCAN2*/ #endif /*RT_USINGLPCCAN2*/
/*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/ /*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
/*********************************************************************//** /*********************************************************************//**
* @brief Event Router IRQ Handler * @brief Event Router IRQ Handler
@ -755,7 +834,9 @@ void CAN_IRQHandler(void)
if(state & (0x01 << 3)) if(state & (0x01 << 3))
{ {
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 0<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 0<<8);
} else { }
else
{
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 0<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 0<<8);
} }
} }
@ -764,8 +845,10 @@ void CAN_IRQHandler(void)
{ {
rt_uint32_t errtype; rt_uint32_t errtype;
errtype = (IntStatus >> 16); errtype = (IntStatus >> 16);
if(errtype & 0x1F && lpccan1.status.lasterrtype == (errtype & 0x1F)) { if(errtype & 0x1F && lpccan1.status.lasterrtype == (errtype & 0x1F))
switch((errtype & 0x1F)) { {
switch((errtype & 0x1F))
{
case 00011: // Start of Frame case 00011: // Start of Frame
case 00010: // ID28 ... ID21 case 00010: // ID28 ... ID21
case 00110: //ID20 ... ID18 case 00110: //ID20 ... ID18
@ -817,7 +900,9 @@ void CAN_IRQHandler(void)
if(state & (0x01 << 11)) if(state & (0x01 << 11))
{ {
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 1<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 1<<8);
} else { }
else
{
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 1<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 1<<8);
} }
} }
@ -829,11 +914,14 @@ void CAN_IRQHandler(void)
if(state & (0x01 << 19)) if(state & (0x01 << 19))
{ {
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 2<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 2<<8);
} else { }
else
{
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 2<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 2<<8);
} }
} }
#endif /*RT_USING_LPCCAN1*/ #endif /*RT_USING_LPCCAN1*/
#ifdef RT_USING_LPCCAN2 #ifdef RT_USING_LPCCAN2
IntStatus = CAN_IntGetStatus(CAN_2); IntStatus = CAN_IntGetStatus(CAN_2);
//check receive interrupt //check receive interrupt
@ -849,7 +937,9 @@ void CAN_IRQHandler(void)
if(state & (0x01 << 3)) if(state & (0x01 << 3))
{ {
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 0<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 0<<8);
} else { }
else
{
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 0<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 0<<8);
} }
} }
@ -858,8 +948,10 @@ void CAN_IRQHandler(void)
{ {
rt_uint32_t errtype; rt_uint32_t errtype;
errtype = (IntStatus >> 16); errtype = (IntStatus >> 16);
if(errtype & 0x1F && lpccan2.status.lasterrtype == (errtype & 0x1F)) { if(errtype & 0x1F && lpccan2.status.lasterrtype == (errtype & 0x1F))
switch((errtype & 0x1F)) { {
switch((errtype & 0x1F))
{
case 00011: // Start of Frame case 00011: // Start of Frame
case 00010: // ID28 ... ID21 case 00010: // ID28 ... ID21
case 00110: //ID20 ... ID18 case 00110: //ID20 ... ID18
@ -911,7 +1003,9 @@ void CAN_IRQHandler(void)
if(state & (0x01 << 11)) if(state & (0x01 << 11))
{ {
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 1<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 1<<8);
} else { }
else
{
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 1<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 1<<8);
} }
} }
@ -923,12 +1017,15 @@ void CAN_IRQHandler(void)
if(state & (0x01 << 19)) if(state & (0x01 << 19))
{ {
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 2<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 2<<8);
} else { }
else
{
rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 2<<8); rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 2<<8);
} }
} }
#endif /*RT_USING_LPCCAN2*/ #endif /*RT_USING_LPCCAN2*/
} }
int lpc_can_init(void) int lpc_can_init(void)
{ {
#ifdef RT_USING_LPCCAN1 #ifdef RT_USING_LPCCAN1