@@ -166,46 +166,44 @@ static void nu_can_isr(nu_can_t can)
166166 CAN_T * can_base = ((nu_can_t )can )-> can_base ;
167167
168168 /* Get interrupt event */
169- u32IIDRstatus = can_base -> IIDR ;
169+ u32IIDRstatus = CAN_GET_INT_PENDING_STATUS ( can_base ) ;
170170
171171 if (u32IIDRstatus == 0x00008000 ) /* Check Status Interrupt Flag (Error status Int and Status change Int) */
172172 {
173173 /**************************/
174174 /* Status Change interrupt*/
175175 /**************************/
176- if (can_base -> STATUS & CAN_STATUS_RXOK_Msk )
176+ if (can_base -> STATUS & CAN_STATUS_TXOK_Msk )
177177 {
178-
178+ can_base -> STATUS &= ~ CAN_STATUS_TXOK_Msk ; /* Clear Tx Ok status*/
179179#ifndef RT_CAN_USING_HDR
180180 /* Using as Lisen,Loopback,Loopback+Lisen mode*/
181- rt_hw_can_isr (& can -> dev , RT_CAN_EVENT_RX_IND );
181+ rt_hw_can_isr (& can -> dev , RT_CAN_EVENT_TX_DONE );
182182#endif
183- can_base -> STATUS &= ~CAN_STATUS_RXOK_Msk ; /* Clear Rx Ok status*/
184- rt_kprintf ("RX OK INT\n" ) ;
183+ //rt_kprintf("[%s]TX OK INT\n", can->name) ;
185184 }
186185
187- if (can_base -> STATUS & CAN_STATUS_TXOK_Msk )
186+ if (can_base -> STATUS & CAN_STATUS_RXOK_Msk )
188187 {
189-
188+ can_base -> STATUS &= ~ CAN_STATUS_RXOK_Msk ; /* Clear Rx Ok status*/
190189#ifndef RT_CAN_USING_HDR
191190 /* Using as Lisen,Loopback,Loopback+Lisen mode*/
192- rt_hw_can_isr (& can -> dev , RT_CAN_EVENT_TX_DONE );
191+ rt_hw_can_isr (& can -> dev , RT_CAN_EVENT_RX_IND );
193192#endif
194- can_base -> STATUS &= ~CAN_STATUS_TXOK_Msk ; /* Clear Tx Ok status*/
195- rt_kprintf ("TX OK INT\n" ) ;
193+ //rt_kprintf("[%s]RX OK INT\n", can->name) ;
196194 }
197195
198196 /**************************/
199197 /* Error Status interrupt */
200198 /**************************/
201199 if (can_base -> STATUS & CAN_STATUS_EWARN_Msk )
202200 {
203- rt_kprintf ("EWARN INT\n" ) ;
201+ rt_kprintf ("[%s] EWARN INT\n" , can -> name ) ;
204202 }
205203
206204 if (can_base -> STATUS & CAN_STATUS_BOFF_Msk )
207205 {
208- rt_kprintf ("BOFF INT\n" ) ;
206+ rt_kprintf ("[%s]BUSOFF INT\n" , can -> name ) ;
209207
210208 /* Do Init to release busoff pin */
211209 can_base -> CON = (CAN_CON_INIT_Msk | CAN_CON_CCE_Msk );
@@ -214,18 +212,21 @@ static void nu_can_isr(nu_can_t can)
214212 }
215213 }
216214#ifdef RT_CAN_USING_HDR
217- /*Number of Message Object which caused the interrupt*/
218- else if (u32IIDRstatus != 0 && u32IIDRstatus <= 32 )
215+ /*IntId: 0x0001-0x0020, Number of Message Object which caused the interrupt. */
216+ else if (u32IIDRstatus > 0 && u32IIDRstatus <= 32 )
219217 {
220- rt_kprintf ("=> Interrupt Pointer = %d\n" , can_base -> IIDR - 1 );
221- /*Message RAM 0~15 for CAN Tx using*/
222- if (u32IIDRstatus < 16 )
218+ /*Message RAM 0~RX_MSG_ID_INDEX for CAN Tx using*/
219+ if (u32IIDRstatus <= RX_MSG_ID_INDEX )
220+ {
221+ //rt_kprintf("[%s-Tx]IntId = %d\n", can->name, u32IIDRstatus);
223222 rt_hw_can_isr (& can -> dev , RT_CAN_EVENT_TX_DONE );
224- else /*Message RAM 16~31 for CAN Rx using*/
223+ }
224+ else /*Message RAM RX_MSG_ID_INDEX~31 for CAN Rx using*/
225225 {
226- rt_hw_can_isr (& can -> dev , (RT_CAN_EVENT_RX_IND | (((can_base -> IIDR ) - 1 ) << 8 )));
226+ //rt_kprintf("[%s-Rx]IntId = %d\n", can->name, u32IIDRstatus);
227+ rt_hw_can_isr (& can -> dev , (RT_CAN_EVENT_RX_IND | ((u32IIDRstatus - 1 ) << 8 )));
227228 }
228- CAN_CLR_INT_PENDING_BIT (can_base , (( can_base -> IIDR ) - 1 )); /* Clear Interrupt Pending */
229+ CAN_CLR_INT_PENDING_BIT (can_base , (u32IIDRstatus - 1 )); /* Clear Interrupt Pending */
229230 }
230231#endif
231232
@@ -316,7 +317,7 @@ static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
316317 CAN_T * can_base = ((nu_can_t )can )-> can_base ;
317318
318319 RT_ASSERT (can_base != RT_NULL );
319- /* Check baudrate */
320+ /* Check baud rate */
320321 RT_ASSERT (can -> config .baud_rate != 0 );
321322
322323 switch (cmd )
@@ -359,8 +360,8 @@ static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
359360 }
360361 break ;
361362
362- case RT_CAN_CMD_SET_FILTER :
363363#ifdef RT_CAN_USING_HDR
364+ case RT_CAN_CMD_SET_FILTER :
364365 filter_cfg = (struct rt_can_filter_config * )arg ;
365366
366367 for (int i = 0 ; i < filter_cfg -> count ; i ++ )
@@ -369,7 +370,7 @@ static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
369370 /*set the filter message object*/
370371 if (filter_cfg -> items [i ].mode == 1 )
371372 {
372- if (CAN_SetRxMsgObjAndMsk (can_base , MSG (i + RX_MSG_ID_INDEX ), filter_cfg -> items [i ].ide , filter_cfg -> items [i ].id , filter_cfg -> items [i ].mask , FALSE) == FALSE)
373+ if (CAN_SetRxMsgObjAndMsk (can_base , MSG (filter_cfg -> items [ i ]. hdr + RX_MSG_ID_INDEX ), filter_cfg -> items [i ].ide , filter_cfg -> items [i ].id , filter_cfg -> items [i ].mask , FALSE) == FALSE)
373374 {
374375 return - (RT_ERROR );
375376 }
@@ -378,14 +379,15 @@ static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
378379
379380 {
380381 /*set the filter message object*/
381- if (CAN_SetRxMsgAndMsk (can_base , MSG (i + RX_MSG_ID_INDEX ), filter_cfg -> items [i ].ide , filter_cfg -> items [i ].id , filter_cfg -> items [i ].mask ) == FALSE)
382+ if (CAN_SetRxMsgAndMsk (can_base , MSG (filter_cfg -> items [ i ]. hdr + RX_MSG_ID_INDEX ), filter_cfg -> items [i ].ide , filter_cfg -> items [i ].id , filter_cfg -> items [i ].mask ) == FALSE)
382383 {
383384 return - (RT_ERROR );
384385 }
385386 }
386387 }
387- #endif
388388 break ;
389+ #endif
390+
389391 case RT_CAN_CMD_SET_MODE :
390392 argval = (rt_uint32_t ) arg ;
391393 if (argval != RT_CAN_MODE_NORMAL && argval != RT_CAN_MODE_LISEN &&
@@ -399,6 +401,7 @@ static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
399401 return nu_can_configure (can , & can -> config );
400402 }
401403 break ;
404+
402405 case RT_CAN_CMD_SET_BAUD :
403406 argval = (rt_uint32_t ) arg ;
404407 if (argval != CAN1MBaud && argval != CAN800kBaud && argval != CAN500kBaud && argval != CAN250kBaud &&
@@ -412,6 +415,7 @@ static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
412415 return nu_can_configure (can , & can -> config );
413416 }
414417 break ;
418+
415419 case RT_CAN_CMD_SET_PRIV :
416420 argval = (rt_uint32_t ) arg ;
417421 if (argval != RT_CAN_MODE_PRIV && argval != RT_CAN_MODE_NOPRIV )
@@ -439,7 +443,7 @@ static rt_err_t nu_can_control(struct rt_can_device *can, int cmd, void *arg)
439443 }
440444 break ;
441445 default :
442- return - (RT_EINVAL );
446+ return - (RT_EINVAL );
443447
444448 }
445449
@@ -503,11 +507,16 @@ static int nu_can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxn
503507 RT_ASSERT (buf != RT_NULL );
504508
505509 /* get data */
506- CAN_Receive (can_base , boxno , & tMsg );
510+ if (CAN_Receive (can_base , boxno , & tMsg ) == FALSE)
511+ {
512+ rt_kprintf ("No available RX Msg.\n" );
513+ return - (RT_ERROR );
514+ }
507515
508516#ifdef RT_CAN_USING_HDR
509517 /* Hardware filter messages are valid */
510- can -> hdr -> connected = 1 ;
518+ pmsg -> hdr = boxno - RX_MSG_ID_INDEX ;
519+ can -> hdr [pmsg -> hdr ].connected = 1 ;
511520#endif
512521
513522 /* Standard ID (11 bits)*/
@@ -521,6 +530,7 @@ static int nu_can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxn
521530 pmsg -> ide = RT_CAN_EXTID ;
522531 pmsg -> id = tMsg .Id ;
523532 }
533+
524534 if (tMsg .FrameType == CAN_DATA_FRAME )
525535 {
526536 /* Data frame */
@@ -531,9 +541,10 @@ static int nu_can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxn
531541 /* Remote frame */
532542 pmsg -> rtr = RT_CAN_RTR ;
533543 }
544+
534545 pmsg -> len = tMsg .DLC ;
535- rt_memcpy (pmsg -> data , tMsg .Data , pmsg -> len );
536546
547+ rt_memcpy (pmsg -> data , tMsg .Data , pmsg -> len );
537548
538549 return RT_EOK ;
539550}
0 commit comments