Browse files

update CAN handler to get all receive all pending messages

  • Loading branch information...
1 parent 27d0b93 commit 35d66b269fa7a5082027913dcf766f12423aebdb @hartmutbehrens committed Feb 8, 2013
Showing with 43 additions and 35 deletions.
  1. +37 −32 c2e_can.c
  2. +1 −1 c2e_main.c
  3. +1 −0 c2e_udp.c
  4. +4 −2 config.h
View
69 c2e_can.c
@@ -46,42 +46,47 @@ static void write_to_ringbuf(tCANMsgObject *can_object, uint32_t id)
RingBufWrite(&g_can_ringbuf, &frame[0], CAN_FRAME_SIZE);
}
-// CAN controller interrupt handler.
void CAN_handler(void)
{
- uint32_t status = CANIntStatus(CAN0_BASE, CAN_INT_STS_CAUSE); // Find the cause of the interrupt, status 1-32 = ID of message object with highest priority
- if(status <= 8) // The first eight message objects make up the Transmit message FIFO.
+ uint32_t status = CANIntStatus(CAN0_BASE, CAN_INT_STS_CAUSE); // Find the cause of the interrupt, status 1-32 = ID of message object with highest priority
+ while (status)
{
- CAN_data.bytes_transmitted += 8; // Increment the number of bytes transmitted.
- }
- else if((status > 8) && (status <= 16)) // The second eight message objects make up the Receive message FIFO.
- {
- rx_message_count += 1;
- update_count += 1;
- CANMessageGet(CAN0_BASE, status, &CAN_data.rx_msg_object, 1); // Read the data out and acknowledge that it was read.
+ if(status <= 2) // The first eight message objects make up the Transmit message FIFO.
+ {
+ CANIntClear(CAN0_BASE, status); // Acknowledge the CAN controller interrupt has been handled.
+ CAN_data.bytes_transmitted += 8; // Increment the number of bytes transmitted.
+ }
+ else if((status > 2) && (status <= 32)) // The second eight message objects make up the Receive message FIFO.
+ {
+ rx_message_count += 1;
+ update_count += 1;
+ CANMessageGet(CAN0_BASE, status, &CAN_data.rx_msg_object, 1); // Read the data out and acknowledge that it was read.
- if(CAN_data.rx_msg_object.ulFlags & MSG_OBJ_DATA_LOST) // Check to see if there is an indication that some messages were lost.
- {
- lost_message_count += 1;
- }
+ if(CAN_data.rx_msg_object.ulFlags & MSG_OBJ_DATA_LOST) // Check to see if there is an indication that some messages were lost.
+ {
+ lost_message_count += 1;
+ }
- write_to_ringbuf(&CAN_data.rx_msg_object, (status - 9) ); // write the CAN data to a ringbuffer for further processing
-
- CAN_data.rx_msg_object.pucMsgData += 8; // Advance the read pointer.
- CAN_data.bytes_remaining -= 8; // Decrement the expected bytes remaining.
-
- if(CAN_data.bytes_remaining == 0) // this is to avoid memory filling up
- {
- CAN_data.rx_msg_object.pucMsgData = CAN_data.rx_buffer; // re-assign pointer to buffer that will hold message data - seems to be necessary to prevent lock-up (presumably due to memory fillup?)
- CAN_data.bytes_remaining = CAN_FIFO_SIZE; // reset number of bytes expected
- }
- HWREG(NVIC_INT_CTRL) = NVIC_INT_CTRL_PEND_SV; // Trigger PendSV in order to send CAN packets
- }
- else
- {
- CANStatusGet(CAN0_BASE, CAN_STS_CONTROL); // status interrupt so read the current status to clear the interrupt
+ write_to_ringbuf(&CAN_data.rx_msg_object, (status - 3) ); // write the CAN data to a ringbuffer for further processing
+
+ CAN_data.rx_msg_object.pucMsgData += 8; // Advance the read pointer.
+ CAN_data.bytes_remaining -= 8; // Decrement the expected bytes remaining.
+
+ if(CAN_data.bytes_remaining == 0) // this is to avoid memory filling up
+ {
+ CAN_data.rx_msg_object.pucMsgData = CAN_data.rx_buffer; // re-assign pointer to buffer that will hold message data - seems to be necessary to prevent lock-up (presumably due to memory fillup?)
+ CAN_data.bytes_remaining = CAN_FIFO_SIZE; // reset number of bytes expected
+ }
+
+ HWREG(NVIC_INT_CTRL) = NVIC_INT_CTRL_PEND_SV; // Trigger PendSV in order to send CAN packets
+ }
+ else
+ {
+ CANStatusGet(CAN0_BASE, CAN_STS_CONTROL); // status interrupt so read the current status to clear the interrupt
+ }
+ //CANIntClear(CAN0_BASE, status); // Acknowledge the CAN controller interrupt has been handled.
+ status = CANIntStatus(CAN0_BASE, CAN_INT_STS_CAUSE);
}
- CANIntClear(CAN0_BASE, status); // Acknowledge the CAN controller interrupt has been handled.
}
@@ -135,13 +140,13 @@ int CAN_receive_FIFO(unsigned char *data, uint32_t rx_size)
CAN_data.rx_msg_object.ulMsgLen = 8; // The length is always eight as the full buffer is divisible by 8.
rx_size -=8; // There are now eight less bytes to receive.
CAN_data.rx_msg_object.ulFlags |= MSG_OBJ_FIFO; // Set the MSG_OBJ_FIFO to indicate that this is not the last data in a chain of FIFO entries.
- CANMessageSet(CAN0_BASE, idx + 9, &CAN_data.rx_msg_object, MSG_OBJ_TYPE_RX); // Make sure that all message objects up to the last indicate that they are part of a FIFO.
+ CANMessageSet(CAN0_BASE, idx + 3, &CAN_data.rx_msg_object, MSG_OBJ_TYPE_RX); // Make sure that all message objects up to the last indicate that they are part of a FIFO.
}
else
{
CAN_data.rx_msg_object.ulMsgLen = rx_size; // Get the remaining bytes.
CAN_data.rx_msg_object.ulFlags &= ~MSG_OBJ_FIFO; // Clear the MSG_OBJ_FIFO to indicate that this is the last data in a chain of FIFO entries.
- CANMessageSet(CAN0_BASE, idx + 9, &CAN_data.rx_msg_object, MSG_OBJ_TYPE_RX); // This is the last message object in a FIFO so don't set the FIFO to indicate that the FIFO ends with this message object.
+ CANMessageSet(CAN0_BASE, idx + 3, &CAN_data.rx_msg_object, MSG_OBJ_TYPE_RX); // This is the last message object in a FIFO so don't set the FIFO to indicate that the FIFO ends with this message object.
}
}
return(0);
View
2 c2e_main.c
@@ -79,7 +79,7 @@ static uint32_t broadcast_presence(void)
static uint32_t wait(void)
{
display_state();
- display_UDP_statistics();
+ //display_UDP_statistics();
display_CAN_statistics();
return g_state;
}
View
1 c2e_udp.c
@@ -142,6 +142,7 @@ void UDP_send_CAN(unsigned char *data, uint32_t size)
for (int i = 0; i < g_gw_count; i++)
{
UDP_send_msg(&message[0], total_size, &g_gateways[g_gw_count]); // send to registered gateway
+ //UDP_send_msg(&message[0], total_size, IP_ADDR_BROADCAST); // broadcast
}
}
View
6 config.h
@@ -2,6 +2,8 @@
#define __C2E_CONFIG_H
#include <stdint.h>
+#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
+
#ifndef DHCP_EXPIRE_TIMER_SECS
#define DHCP_EXPIRE_TIMER_SECS 45 // Timeout for DHCP address request
#endif
@@ -10,10 +12,10 @@
#define CAN_FRAME_SIZE 14 // size of CAN frame to be sent via UDP
#define CAN_UPDATERATE 100 // rate at which to display CAN message statistics
#define UDP_UPDATERATE 100 // rate at which to display UDP message statistics
-#define CAN_RINGBUF_SIZE (CAN_FRAME_SIZE*80) // size of ring buffer, accomodate up to 160 frames, probably a bit excessive
+#define CAN_RINGBUF_SIZE (CAN_FRAME_SIZE*80) // size of ring buffer, accomodate up to 80 frames, probably a bit excessive
#define PRINT_BUF_SIZE 20 // OLED print buffer size
#define EV_RINGBUF_SIZE 64 // size of event ring buffer, accomodate up to 64 events
-#define CAN_FIFO_SIZE (8 * 8) // size of FIFO buffers allocated to the CAN controller - 8 messages can each hold a max of 8 bytes
+#define CAN_FIFO_SIZE (30 * 8) // size of FIFO buffers allocated to the CAN controller - 8 messages can each hold a max of 8 bytes
#define CAN_BITRATE 800000 // set CAN bitrate to 1Mbps
#define MAX_CAN_GATEWAYS 4 // number of CAN gateways to keep track of
#define CAN_ID_POS 0 // position of CAN message ID in CAN frame

0 comments on commit 35d66b2

Please sign in to comment.