-
Notifications
You must be signed in to change notification settings - Fork 0
/
can_uart.c
153 lines (116 loc) · 3.14 KB
/
can_uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* can_uart.c
*
* Created: 3/5/2018 6:14:03 PM
* Author: Jørgen
*/
#include "can_uart.h"
#include <stdio.h>
#define CAN_TX_BUFFER_SIZE 64
#define CAN_RX_BUFFER_SIZE 32
#define CAN_TX_BUFFER_MASK (CAN_TX_BUFFER_SIZE-1)
#define CAN_RX_BUFFER_MASK (CAN_RX_BUFFER_SIZE-1)
static unsigned char CAN_TX_BUFFER[CAN_TX_BUFFER_SIZE];
static unsigned char CAN_RX_BUFFER[CAN_RX_BUFFER_SIZE];
static volatile uint8_t tx_head;
static volatile uint8_t tx_tail;
static volatile uint8_t new_can_from_uart;
static volatile uart_channel_t mode;
uint8_t parseCanFromUART(CanMessage_t * txFrame);
uint8_t ascii_to_dec(char c);
void can_uart_init(uart_channel_t channel)
{
mode = channel;
}
uint8_t uart_to_can_if_new(CanMessage_t * txFrame)
{
if (mode == UART_BOTH) return 0;
uint8_t tmphead;
while (uart_has_new_message(mode))
{
unsigned char data = uart_receive(mode);
if (data == '\n')
{
new_can_from_uart++;
} else {
CAN_TX_BUFFER[tx_head] = data;
}
tmphead = (tx_head + 1) & CAN_TX_BUFFER_MASK;
tx_head = tmphead;
}
if (!new_can_from_uart) return 0;
new_can_from_uart--;
return parseCanFromUART(txFrame);
}
uint8_t parseCanFromUART(CanMessage_t * txFrame)
{
uint16_t canID = 0;
uint8_t canLength = 0;
uint8_t canData = 0;
while (CAN_TX_BUFFER[tx_tail] != '[')
{
if (tx_tail == tx_head) return 0;
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
}
//Skip [
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
//Add MSB of ID
canID += (ascii_to_dec(CAN_TX_BUFFER[tx_tail])<<8);
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
//Add next bit of ID
canID += (ascii_to_dec(CAN_TX_BUFFER[tx_tail])<<4);
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
//Add LSB of ID
canID += ascii_to_dec(CAN_TX_BUFFER[tx_tail]);
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
//Skip :
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
//Add length
canLength = ascii_to_dec(CAN_TX_BUFFER[tx_tail]);
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
//Skip :
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
//Fill in data
for (uint8_t byte = 0; byte < canLength; byte++)
{
canData = (ascii_to_dec(CAN_TX_BUFFER[tx_tail]) << 4);
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
canData |= ascii_to_dec(CAN_TX_BUFFER[tx_tail]);
tx_tail = (tx_tail + 1) & CAN_TX_BUFFER_MASK;
txFrame->data.u8[byte] = canData;
}
//Finish adding the CAN information
txFrame->id = canID;
txFrame->length = canLength;
//Send the CAN message
can_send_message(txFrame);
return 1;
}
void can_to_uart(CanMessage_t * rxFrame)
{
uint8_t index = 0;
index += sprintf((char*)&CAN_RX_BUFFER[index],"[%03X:%d:", rxFrame->id, rxFrame->length);
for (int byte = 0; byte < rxFrame->length; byte++)
{
index += sprintf((char*)&CAN_RX_BUFFER[index], "%02X", rxFrame->data.u8[byte]);
}
sprintf((char*)&CAN_RX_BUFFER[index],"]\n");
uart_transmit_str(mode,CAN_RX_BUFFER);
}
uint8_t ascii_to_dec(char c)
{
uint8_t dec = 0;
if ((c >= '0') && (c <= '9'))
{
dec = c - '0';
} else if ((c >= 'A') && (c <= 'F'))
{
dec = c - 'A' + 10;
} else if ((c >= 'a') && (c <= 'f'))
{
dec = c - 'a' + 10;
} else {
dec = 0;
}
return (uint8_t)dec;
}