-
Notifications
You must be signed in to change notification settings - Fork 3
/
STM32_CAN.hpp
185 lines (136 loc) · 6.03 KB
/
STM32_CAN.hpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* STM32_CAN.hpp
use with STM32 HAL for your MCU!
Copyright (c) 2022 Minos Eigenheer
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Inherited NMEA2000 object for STM32 MCU's with internal CAN
based setup. See also NMEA2000 library.
*/
#ifndef STM32_CAN_H_
#define STM32_CAN_H_
#include "stm32f1xx_hal.h"
#include "main.h"
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string>
#include "RingBuffer.h"
extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
extern CAN_HandleTypeDef hcan3;
class tSTM32_CAN
{
public:
enum CANbaudRatePrescaler {
CAN1000kbit = 1,
CAN500kbit = 2,
CAN250kbit = 4,
CAN200kbit = 5,
CAN125kbit = 8,
CAN100kbit = 10,
CAN50kbit = 20
};
tSTM32_CAN(CAN_HandleTypeDef *_canBus, CANbaudRatePrescaler _CANbaudRate);
CAN_HandleTypeDef *canBus;
struct CAN_message_t {
uint32_t id = 0; // can identifier
// uint16_t timestamp = 0; // time when message arrived
// uint8_t idhit = 0; // filter that id came from
struct {
bool extended = 0; // identifier is extended (29-bit)
bool remote = 0; // remote transmission request packet type
bool overrun = 0; // message overrun
bool reserved = 0;
} flags;
uint8_t len = 8; // length of data
uint8_t buf[8] = { 0 }; // data
// uint8_t mb = 0; // used to identify mailbox reception
// uint8_t bus = 0; // used to identify where the message came from when events() is used.
// bool seq = 0; // sequential frames
};
std::string CANname;
protected:
uint16_t MaxCANReceiveFrames;
uint16_t MaxCANSendFrames;
uint8_t prioBits;
uint32_t maxPrio;
CAN_TxHeaderTypeDef CANTxHeader;
CAN_RxHeaderTypeDef CANRxHeader;
uint8_t CANTxdata[8];
uint8_t CANRxdata[8];
uint32_t CANTxMailbox;
const CANbaudRatePrescaler CANbaudRate;
uint32_t CANRxFIFO;
bool bufferFull;
uint32_t bxCanErrorCode;
protected:
tPriorityRingBuffer<CAN_message_t> *rxRing;
tPriorityRingBuffer<CAN_message_t> *txRing;
bool CANWriteTxMailbox(unsigned long id, unsigned char len, const unsigned char *buf, bool extended);
public:
void InitCANFrameBuffers();
HAL_StatusTypeDef CANInit();
void CANDeinit();
HAL_StatusTypeDef SetCANFilter(bool ExtendedId, uint32_t FilterNum, uint32_t Mask, uint32_t Id);
bool CANOpen();
bool CANSendFrame(unsigned long id, unsigned char len, const unsigned char* buf, bool wait_sent = false, bool extended_id = true);
bool CANSendFrame(CAN_message_t* message);
bool CANGetFrame(unsigned long& id, unsigned char& len, unsigned char* buf);
bool CANGetFrameStruct(CAN_message_t* message);
inline uint32_t getCanError() {return bxCanErrorCode;}
inline void setImportantCanError(uint32_t err) {bxCanErrorCode = err & (
// HAL_CAN_ERROR_EWG | // Protocol Error Warning
// HAL_CAN_ERROR_EPV | // Error Passive
HAL_CAN_ERROR_BOF | // Bus-off error
HAL_CAN_ERROR_STF | // Stuff error
HAL_CAN_ERROR_FOR | // Form error
HAL_CAN_ERROR_ACK | // Acknowledgment error
HAL_CAN_ERROR_BR | // Bit recessive error
HAL_CAN_ERROR_BD | // Bit dominant error
HAL_CAN_ERROR_CRC | // CRC error
HAL_CAN_ERROR_RX_FOV0 | // Rx FIFO0 overrun error
HAL_CAN_ERROR_RX_FOV1 | // Rx FIFO1 overrun error
HAL_CAN_ERROR_TX_ALST0 | // TxMailbox 0 transmit failure due to arbitration lost
HAL_CAN_ERROR_TX_TERR0 | // TxMailbox 0 transmit failure due to transmit error
HAL_CAN_ERROR_TX_ALST1 | // TxMailbox 1 transmit failure due to arbitration lost
HAL_CAN_ERROR_TX_TERR1 | // TxMailbox 1 transmit failure due to transmit error
HAL_CAN_ERROR_TX_ALST2 | // TxMailbox 2 transmit failure due to arbitration lost
HAL_CAN_ERROR_TX_TERR2 | // TxMailbox 2 transmit failure due to transmit error
HAL_CAN_ERROR_TIMEOUT | // Timeout error
HAL_CAN_ERROR_NOT_INITIALIZED | // Peripheral not initialized
HAL_CAN_ERROR_NOT_READY | // Peripheral not ready
HAL_CAN_ERROR_NOT_STARTED | // Peripheral not started
HAL_CAN_ERROR_PARAM // Parameter error
);}
inline int32_t getCanTxBufferSpace() {return txRing->getSize() - txRing->count() - 1;}
inline int32_t getCanRxBufferSpace() {return rxRing->getSize() - rxRing->count() - 1;}
// void LoopbackMode();
// void NormalMode();
// triggered by interrupt
void CANReadRxMailbox(CAN_HandleTypeDef *hcan, uint32_t CANRxFIFO);
bool SendFromTxRing();
};
tSTM32_CAN* getInstance(CAN_HandleTypeDef *hcan); // returns instance with certain CAN_HandleTypeDef struct
//-----------------------------------------------------------------------------
extern "C" int _write(int file, char *ptr, int len);
uint32_t pow(uint32_t base, uint32_t exp);
#endif /* STM32_CAN_H_ */