|
|
@@ -4,7 +4,6 @@
|
|
|
|
|
|
extern uint8_t work_normal;
|
|
|
//********************************************************CAN-START**********************************************************
|
|
|
-static uint8_t can_tx_buf[CAN_TX_BUF_MAX];
|
|
|
static uint8_t can_rx_ctr_self_buf[CAN_RX_BUF_MAX];
|
|
|
static uint8_t can_rx_ctr_bro_buf[CAN_RX_CTR_BRO_BUF_MAX];
|
|
|
static uint8_t can_rx_test_bro_buf[CAN_RX_TEST_BRO_BUF_MAX];
|
|
|
@@ -25,14 +24,106 @@ uint32_t filter_test_bro_id = (PRINTF_TER_ID << 10)|(BROCAST_ID << 3)|CAN_FF_EXT
|
|
|
CAN_FRAME can_ctr_self;
|
|
|
CAN_FRAME can_ctr_bro;
|
|
|
CAN_FRAME can_test_bro;
|
|
|
-static CAN_FRAME can_tx_frame;
|
|
|
|
|
|
-static uint8_t can_busy = 0;
|
|
|
+static shark_can_frame_t shark_can_tx_queue[200];
|
|
|
+static shark_u8 shark_can_tx_head;
|
|
|
+static shark_u8 shark_can_tx_tail;
|
|
|
+
|
|
|
+static shark_bool shark_can_tx_frame(shark_can_frame_t *frame)
|
|
|
+{
|
|
|
+ u8 mailbox;
|
|
|
+
|
|
|
+ if (CAN_TSTAT_TME0 == (CAN_TSTAT(CAN0) & CAN_TSTAT_TME0)) {
|
|
|
+ mailbox = CAN_MAILBOX0;
|
|
|
+ } else if (CAN_TSTAT_TME1 == (CAN_TSTAT(CAN0) & CAN_TSTAT_TME1)) {
|
|
|
+ mailbox = CAN_MAILBOX1;
|
|
|
+ } else if (CAN_TSTAT_TME2 == (CAN_TSTAT(CAN0) & CAN_TSTAT_TME2)) {
|
|
|
+ mailbox = CAN_MAILBOX2;
|
|
|
+ } else {
|
|
|
+ return shark_false;
|
|
|
+ }
|
|
|
+
|
|
|
+ CAN_TMDATA0(CAN0, mailbox) = frame->values[0];
|
|
|
+ CAN_TMDATA1(CAN0, mailbox) = frame->values[1];
|
|
|
+
|
|
|
+ CAN_TMP(CAN0, mailbox) &= ~CAN_TMP_DLENC;
|
|
|
+ CAN_TMP(CAN0, mailbox) |= frame->efid.length + 1;
|
|
|
+ CAN_TMI(CAN0, mailbox) = frame->efid.value << 3 | CAN_FF_EXTENDED | CAN_FT_DATA | CAN_TMI_TEN;
|
|
|
+
|
|
|
+ return shark_true;
|
|
|
+}
|
|
|
+
|
|
|
+shark_bool shark_can_send_frame(shark_can_efid_t efid, const void *buff, u8 length)
|
|
|
+{
|
|
|
+ u8 tail = (shark_can_tx_tail + 1) % NELEM(shark_can_tx_queue);
|
|
|
+ shark_can_frame_t *frame;
|
|
|
+ u32 times = 20000;
|
|
|
+
|
|
|
+ if (work_normal == 0) {
|
|
|
+ return shark_false;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (tail == shark_can_tx_head) {
|
|
|
+ if (times > 0) {
|
|
|
+ times--;
|
|
|
+ } else {
|
|
|
+ return shark_false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ frame = shark_can_tx_queue + shark_can_tx_tail;
|
|
|
+ memcpy(frame->data, buff, length);
|
|
|
+ frame->efid.value = efid.value;
|
|
|
+
|
|
|
+ if (shark_can_tx_tail == shark_can_tx_head && shark_can_tx_frame(frame)) {
|
|
|
+ return shark_true;
|
|
|
+ }
|
|
|
+
|
|
|
+ shark_can_tx_tail = tail;
|
|
|
+ can_interrupt_enable(CAN0, CAN_INT_TME);
|
|
|
+
|
|
|
+ return shark_true;
|
|
|
+}
|
|
|
+
|
|
|
+shark_bool shark_can_send(u8 dest, u8 src, u8 type, const u8 *buff, u16 length)
|
|
|
+{
|
|
|
+ shark_can_efid_t efid;
|
|
|
+ u8 index = 1;
|
|
|
+
|
|
|
+ efid.dest = dest;
|
|
|
+ efid.src = src;
|
|
|
+ efid.type = type;
|
|
|
+ efid.priority = 6;
|
|
|
+ efid.total = (length + 7) / 8;
|
|
|
+
|
|
|
+ while (length > 0) {
|
|
|
+ u8 wrlen = length > 8 ? 8 : length;
|
|
|
+
|
|
|
+ efid.index = index;
|
|
|
+ efid.length = wrlen - 1;
|
|
|
+
|
|
|
+ if (shark_can_send_frame(efid, buff, wrlen)) {
|
|
|
+ length -= wrlen;
|
|
|
+ buff += wrlen;
|
|
|
+ index++;
|
|
|
+ } else {
|
|
|
+ return shark_false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return shark_true;
|
|
|
+}
|
|
|
|
|
|
void Can_Stop_Send(void)
|
|
|
{
|
|
|
- can_busy = 0;
|
|
|
+ shark_can_tx_head = shark_can_tx_tail = 0;
|
|
|
+}
|
|
|
+
|
|
|
+int8_t Send_Data_Can(CAN_FRAME*app_can_frame,uint8_t from)
|
|
|
+{
|
|
|
+ return shark_can_send(app_can_frame->head.dest, app_can_frame->head.sour, app_can_frame->head.rsp, app_can_frame->data, app_can_frame->len);
|
|
|
}
|
|
|
+
|
|
|
void Check_Can_Poll(void)
|
|
|
{
|
|
|
if(work_normal)
|
|
|
@@ -43,7 +134,6 @@ void Check_Can_Poll(void)
|
|
|
can_deinit(CAN0);
|
|
|
CAN_Config_HW();
|
|
|
Can_Power_Enable(1);
|
|
|
- can_busy = 0;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -82,52 +172,6 @@ uint16_t Get_Data_Can(CAN_FRAME*app_can_frame)
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-int8_t Send_Data_Can(CAN_FRAME*app_can_frame,uint8_t from)
|
|
|
-{
|
|
|
- can_trasnmit_message_struct can_tr_m;
|
|
|
- uint32_t exid = 0;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if(app_can_frame == NULL || can_busy || work_normal == 0)
|
|
|
- return 0;
|
|
|
-
|
|
|
- //Silent_Enable(0);
|
|
|
- //delay_1us(100);
|
|
|
-
|
|
|
- memcpy(&can_tx_frame.head,&app_can_frame->head,sizeof(can_tx_frame.head));
|
|
|
- can_tx_frame.len = app_can_frame->len;
|
|
|
- memcpy(can_tx_frame.data,app_can_frame->data,can_tx_frame.len);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- memset(&can_tr_m,0x00,sizeof(can_tr_m));
|
|
|
- memcpy(&exid,&can_tx_frame.head,sizeof(exid));
|
|
|
- can_tr_m.tx_efid = (exid>>3);
|
|
|
- can_tr_m.tx_ff = CAN_FF_EXTENDED;
|
|
|
- can_tr_m.tx_ft = CAN_FT_DATA;
|
|
|
-
|
|
|
- if(can_tx_frame.len > 8)
|
|
|
- can_tr_m.tx_dlen = 8;
|
|
|
- else
|
|
|
- can_tr_m.tx_dlen = can_tx_frame.len;
|
|
|
-
|
|
|
- memcpy(can_tr_m.tx_data,can_tx_frame.data,can_tr_m.tx_dlen);
|
|
|
-
|
|
|
- if(can_message_transmit(CAN0,&can_tr_m) == CAN_NOMAILBOX)
|
|
|
- return 0;
|
|
|
-
|
|
|
- if(can_tx_frame.head.total != 1)
|
|
|
- {
|
|
|
- can_busy = 1;
|
|
|
- can_interrupt_enable(CAN0, CAN_INT_TME);
|
|
|
- }
|
|
|
-
|
|
|
- return 1;
|
|
|
-
|
|
|
-}
|
|
|
static void Can_NVIC_Config(void)
|
|
|
{
|
|
|
nvic_irq_enable(USBD_HP_CAN0_TX_IRQn,3,0);
|
|
|
@@ -260,10 +304,6 @@ void CAN_Config(void)
|
|
|
can_ctr_self.data = can_rx_ctr_self_buf;
|
|
|
can_ctr_bro.data = can_rx_ctr_bro_buf;
|
|
|
can_test_bro.data = can_rx_test_bro_buf;
|
|
|
-
|
|
|
- memset(&can_tx_frame,0x00,sizeof(can_tx_frame));
|
|
|
- memset(can_tx_buf,0x00,sizeof(can_tx_buf));
|
|
|
- can_tx_frame.data = can_tx_buf;
|
|
|
|
|
|
|
|
|
/* enable CAN clock */
|
|
|
@@ -368,37 +408,18 @@ void CAN_Config(void)
|
|
|
|
|
|
void USBD_HP_CAN0_TX_IRQHandler(void)
|
|
|
{
|
|
|
- can_trasnmit_message_struct can_tr_m;
|
|
|
- uint32_t exid = 0;
|
|
|
-
|
|
|
- if(can_tx_frame.head.index != can_tx_frame.head.total)
|
|
|
- {
|
|
|
- can_tx_frame.head.index++;
|
|
|
-
|
|
|
- memset(&can_tr_m,0x00,sizeof(can_tr_m));
|
|
|
- memcpy(&exid,&can_tx_frame.head,sizeof(exid));
|
|
|
- can_tr_m.tx_efid = (exid>>3);
|
|
|
- can_tr_m.tx_ff = CAN_FF_EXTENDED;
|
|
|
- can_tr_m.tx_ft = CAN_FT_DATA;
|
|
|
- exid = get_index_total_value(can_tx_frame.head.index) -1;
|
|
|
- exid <<= 3;
|
|
|
-
|
|
|
- if(can_tx_frame.head.index != can_tx_frame.head.total)
|
|
|
- can_tr_m.tx_dlen = 8;
|
|
|
- else
|
|
|
- can_tr_m.tx_dlen = can_tx_frame.len - exid;
|
|
|
-
|
|
|
- memcpy(can_tr_m.tx_data,&can_tx_frame.data[exid],can_tr_m.tx_dlen);
|
|
|
-
|
|
|
- can_message_transmit(CAN0,&can_tr_m);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- can_busy = 0;
|
|
|
- can_interrupt_disable(CAN0, CAN_INT_TME);
|
|
|
- //Silent_Enable(1);
|
|
|
+ while (shark_true) {
|
|
|
+ if (shark_can_tx_head == shark_can_tx_tail) {
|
|
|
+ can_interrupt_disable(CAN0, CAN_INT_TME);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (shark_can_tx_frame(shark_can_tx_queue + shark_can_tx_head)) {
|
|
|
+ shark_can_tx_head = (shark_can_tx_head + 1) % NELEM(shark_can_tx_queue);
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
void USBD_LP_CAN0_RX0_IRQHandler(void)
|
|
|
@@ -448,33 +469,8 @@ void USBD_LP_CAN0_RX0_IRQHandler(void)
|
|
|
|
|
|
}
|
|
|
|
|
|
-static can_trasnmit_message_struct shark_can_log_message;
|
|
|
-static u32 shark_can_log_delay;
|
|
|
-
|
|
|
-static void shark_can_log_flush(uint8_t dest)
|
|
|
-{
|
|
|
- u32 ticks = shark_can_log_delay;
|
|
|
-
|
|
|
- shark_can_log_message.tx_efid = 3 << 24 | 1 << 19 | 1 << 14 | SELF_ID << 7 | dest;
|
|
|
- shark_can_log_message.tx_ff = CAN_FF_EXTENDED;
|
|
|
- shark_can_log_message.tx_ft = CAN_FT_DATA;
|
|
|
-
|
|
|
- while (shark_true) {
|
|
|
- if (can_message_transmit(CAN0, &shark_can_log_message) != CAN_NOMAILBOX) {
|
|
|
- shark_can_log_delay = 20000;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- if (ticks > 0) {
|
|
|
- ticks--;
|
|
|
- } else {
|
|
|
- shark_can_log_delay = 0;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- shark_can_log_message.tx_dlen = 0;
|
|
|
-}
|
|
|
+static u8 shark_can_log_buff[8];
|
|
|
+static u8 shark_can_log_length;
|
|
|
|
|
|
//
|
|
|
//ÓÃÓÚϵͳµ÷ÓÃprintf
|
|
|
@@ -482,18 +478,19 @@ static void shark_can_log_flush(uint8_t dest)
|
|
|
int fputc(int ch, FILE *f)
|
|
|
{
|
|
|
if (ch == '\n') {
|
|
|
- if (shark_can_log_message.tx_dlen > 0) {
|
|
|
- shark_can_log_flush(0x72);
|
|
|
+ if (shark_can_log_length > 0) {
|
|
|
+ shark_can_send(0x72, 0x42, 3, shark_can_log_buff, shark_can_log_length);
|
|
|
+ shark_can_log_length = 0;
|
|
|
}
|
|
|
- } else if (shark_can_log_message.tx_dlen < sizeof(shark_can_log_message.tx_data)) {
|
|
|
- shark_can_log_message.tx_data[shark_can_log_message.tx_dlen] = ch;
|
|
|
- shark_can_log_message.tx_dlen++;
|
|
|
+ } else if (shark_can_log_length < sizeof(shark_can_log_buff)) {
|
|
|
+ shark_can_log_buff[shark_can_log_length] = ch;
|
|
|
+ shark_can_log_length++;
|
|
|
} else {
|
|
|
- shark_can_log_flush(0x70);
|
|
|
- shark_can_log_message.tx_dlen = 1;
|
|
|
- shark_can_log_message.tx_data[0] = ch;
|
|
|
+ shark_can_send(0x70, 0x42, 3, shark_can_log_buff, shark_can_log_length);
|
|
|
+ shark_can_log_buff[0] = ch;
|
|
|
+ shark_can_log_length = 1;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return ch;
|
|
|
}
|
|
|
//********************************************************CAN-END**********************************************************
|