NNP STM Generic Remote Module git-main
Loading...
Searching...
No Matches
can_stm.c
Go to the documentation of this file.
1
10#include "main.h"
11#include "can_stm.h"
12#include "canfestival.h"
13#include <stdlib.h>
14#include "ObjDict.h"
15
16#include "app.h"
17
18volatile unsigned char msg_received = 0;
19
20volatile UNS16 BitErrors = 0 ;
21volatile UNS16 StuffErrors = 0;
22volatile UNS16 FormErrors = 0;
23volatile UNS16 OtherErrors = 0;
24volatile UNS16 receivedMessages = 0;
25
26UNS8 txErr = 0;
27UNS8 rxErr = 0;
28
29unsigned char canInit()
30{
31 unsigned char j;
32 UNS32 PDOData[4];
33 UNS8 type = 0;
34 UNS32 size = 0;
35 UNS32 pdata = 0;
36
37 for ( j = 0; j < 4; j++)
38 {
39 getODentry( &ObjDict_Data,
40 0x1400 + j,
41 1,
42 &pdata,
43 &size,
44 &type,
45 0);
46 PDOData[j] = pdata;
47 }
48 UNS16 identifiers[] = { 0x0080, (UNS16)getNodeId(&ObjDict_Data), (UNS16)PDOData[0], (UNS16)PDOData[1], (UNS16)PDOData[2], (UNS16)PDOData[3], \
49 0x0000, 0x0140, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 };
50
51 // identifiers[] can be used to assign the CAN filter, is unused here
52 TxHeader.StdId = identifiers[1];
53
54 return 1;
55}
56
57void UpdateCANerrors(void)
58{
59
60 CAN_BitErrors = BitErrors;
61 CAN_StuffErrors = StuffErrors;
62 CAN_FormErrors = FormErrors;
63 CAN_OtherErrors = OtherErrors;
64 CAN_Rx_ErrCounter = (UNS16)rxErr;
65 CAN_Tx_ErrCounter = (UNS16)txErr;
66// CAN_Receive_BEI != (0xFF & CANGCON);
67// CAN_Interrupts_Off |= (0xFF & CANGSTA);
68}
69
77UNS8 canSend(CAN_HandleTypeDef *hcan, Message *m)
78{
79 if(HAL_CAN_GetTxMailboxesFreeLevel(hcan) > 0)
80 {
81 TxHeader.StdId = m->cob_id;
82 TxHeader.IDE = CAN_ID_STD;
83 if(m->rtr)
84 TxHeader.RTR = CAN_RTR_REMOTE;
85 else
86 TxHeader.RTR = CAN_RTR_DATA;
87
88 TxHeader.DLC = m->len; // Set data length code
89 memcpy(&TxData, m->data, m->len);
90
91 if (HAL_CAN_AddTxMessage(hcan, &TxHeader, TxData, &TxMailbox) != HAL_OK)
92 {
93 return 0;
94 }
95
96 CAN_Transmit_Messages++;
97 return 1;
98 }
99
100 return 0;
101}
102
103//Rx Functions
104void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
105{
106 HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData);
107 CAN_Receive_Messages++;
108
109 Message rxm;
110
111 if(RxHeader.IDE == CAN_ID_EXT)
112 return;
113
114 rxm.cob_id = RxHeader.StdId;
115 if(RxHeader.RTR == CAN_RTR_REMOTE)
116 rxm.rtr = 1;
117 rxm.len = RxHeader.DLC;
118 memcpy(&rxm.data, RxData, RxHeader.DLC);
119
120 canDispatch(&ObjDict_Data, &rxm);
121 UpdateCANerrors();
122}
123
124void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
125{
126 HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &RxHeader, RxData);
127 CAN_Receive_Messages++;
128}
129
130void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
131{
132 if(hcan->ErrorCode & HAL_CAN_ERROR_NONE)
133 {
134 return;
135 }
136 if(hcan->ErrorCode & HAL_CAN_ERROR_STF)
137 {
138 StuffErrors++;
139 }
140 if(hcan->ErrorCode & HAL_CAN_ERROR_FOR)
141 {
142 FormErrors++;
143 }
144 if(hcan->ErrorCode & HAL_CAN_ERROR_ACK)
145 {
146 OtherErrors++;
147 }
148 if((hcan->ErrorCode & HAL_CAN_ERROR_BR) ||
149 (hcan->ErrorCode & HAL_CAN_ERROR_BD))
150 {
151 BitErrors++;
152 }
153 if((hcan->ErrorCode & HAL_CAN_ERROR_TIMEOUT) ||
154 (hcan->ErrorCode & HAL_CAN_ERROR_EWG) ||
155 (hcan->ErrorCode & HAL_CAN_ERROR_EPV) ||
156 (hcan->ErrorCode & HAL_CAN_ERROR_TIMEOUT) ||
157 (hcan->ErrorCode & HAL_CAN_ERROR_NOT_INITIALIZED) ||
158 (hcan->ErrorCode & HAL_CAN_ERROR_NOT_READY) ||
159 (hcan->ErrorCode & HAL_CAN_ERROR_NOT_STARTED) ||
160 (hcan->ErrorCode & HAL_CAN_ERROR_PARAM))
161 {
162 OtherErrors++;
163 }
164 if(hcan->ErrorCode & HAL_CAN_ERROR_BOF)
165 {
166 OtherErrors++;
167 }
168 if(hcan->ErrorCode & HAL_CAN_ERROR_CRC)
169 {
170 OtherErrors++;
171 }
172 if((hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR0) || (hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR1) || (hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR2))
173 {
174 txErr++;
175 }
176 if((hcan->ErrorCode & HAL_CAN_ERROR_RX_FOV0) || (hcan->ErrorCode & HAL_CAN_ERROR_RX_FOV1))
177 {
178 rxErr++;
179 }
180 if(hcan->ErrorCode & HAL_CAN_ERROR_STF){
181 StuffErrors++;
182 }
183
184 CAN_TotalErrors++;
185
186 hcan->ErrorCode = 0;
187}
188
195unsigned char canChangeBaudRate_driver(CAN_HandleTypeDef *hcan, char* baud)
196{
197 return 0;
198}
This file is generated by the NNP Tool – Object Dictionary Editor, as originally developed by CAN Fes...
Header file for app.c.
#define UNS8
Unsigned int8 representation in CANFest.
Definition applicfg.h:25
#define UNS16
Unsigned int16 representation in CANFest.
Definition applicfg.h:26
#define UNS32
Unsigned int32 representation in CANFest.
Definition applicfg.h:27
unsigned char canChangeBaudRate_driver(CAN_HandleTypeDef *hcan, char *baud)
Definition can_stm.c:195
UNS8 canSend(CAN_HandleTypeDef *hcan, Message *m)
The driver send a CAN message passed from the CANopen stack.
Definition can_stm.c:77
UNS8 getNodeId(CO_Data *d)
Returns the nodId.
Definition states.c:250
: Header for main.c file. This file contains the common defines of the application.
void canDispatch(CO_Data *d, Message *m)
Called by driver/app when receiving messages.
Definition states.c:43
Definition can.h:13
UNS16 cob_id
Definition can.h:14
UNS8 data[8]
Definition can.h:17
UNS8 rtr
Definition can.h:15
UNS8 len
Definition can.h:16