-
Notifications
You must be signed in to change notification settings - Fork 16
/
taskCANcommunicationJ1939.c
129 lines (107 loc) · 4.17 KB
/
taskCANcommunicationJ1939.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
/** ***************************************************************************
* @file taskCANCommunication.c
* @Author
* @date Aug, 2017
* @brief Copyright (c) 2017 All Rights Reserved.
*
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
******************************************************************************/
#include "main.h"
#include "indices.h"
#include "platformAPI.h"
#include "calibrationAPI.h"
#include "osapi.h"
// for can interface
#include "can.h"
#include "sae_j1939.h"
#include "user_message_can.h"
#include "user_config.h"
#include "car_data.h"
#define ADDRESS_CLAIM_RETRY 5
uint32_t packetRateCounter = 0;
uint32_t canLoopCounter = 0;
BOOL canStarted = FALSE;
/** ***************************************************************************
* @name TaskCANCommunication() CAN communication task
* @brief perform a thread of CAN transmission and receiving
*
* @param [in]
* @retval
******************************************************************************/
void TaskCANCommunicationJ1939(void const *argument)
{
int res;
_ECU_BAUD_RATE baudRate = (_ECU_BAUD_RATE)gEcuConfig.baudRate;
int address = gEcuConfig.address;
BOOL finished;
for (;;) {
if (gUserConfiguration.can_mode == 0) {
car_can_initialize();
while (1)
{
OS_Delay(1000);
if (gUserConfiguration.can_mode != 0) {
break;
}
}
} else if (gUserConfiguration.can_mode == 1) {
can_config(1, baudRate);
// initialize J1939 protocol stack
if (!(gEcuConfigPtr->ecu_name.words)) {
gEcuConfigPtr->ecu_name.bits.function = ACEINNA_SAE_J1939_FUNCTION;
gEcuConfigPtr->ecu_name.bits.manufacture_code = ACEINNA_SAE_J1939_MANUFACTURER_CODE;
gEcuConfigPtr->ecu_name.bits.identity_number = GetUnitSerialNum() & 0x1fffff;
}
sae_j1939_initialize(baudRate, address);
// deactivate semaphore if active
res = osSemaphoreWait(g_sem_can_data, 0);
// Main loop for the task
while( 1 )
{
// wait for events from DACQ task
// Semaphore given at 100Hz
res = osSemaphoreWait(g_sem_can_data, 1000);
if(res != osOK){
continue;
}
canLoopCounter++;
// Auto-detection
if (gEcuInst.state == _ECU_BAUDRATE_DETECT) {
canStartDetectRxIntCounter = canRxIntCounter;
finished = can_detect_baudrate(&baudRate);
if(!finished){
continue;
}
gEcuInst.state = _ECU_CHECK_ADDRESS;
}
canStarted = TRUE;
// address claiming state
if ((gEcuInst.state == _ECU_CHECK_ADDRESS) || (gEcuInst.state == _ECU_WAIT_ADDRESS)|| !(canLoopCounter % 100)) {
if (canLoopCounter > ADDRESS_CLAIM_RETRY){
gEcuInst.state = _ECU_READY;
}
send_address_claim(&gEcuInst);
}
// process incoming messages
ecu_process();
packetRateCounter++;
if (gEcuConfig.packet_rate != 0) {
if (packetRateCounter >= (100 / gEcuConfig.packet_rate)) {
packetRateCounter = 0;
// prepare and send outgoing periodic data packets
// send periodic packets
enqeue_periodic_packets();
}
}
ecu_transmit();
if (gUserConfiguration.can_mode != 1) {
break;
}
}
}
}
}