34 miosix::Priority threadPriority)
35 : can(can), onReceive(
std::move(onReceive)), threadPriority(threadPriority)
49 sndThread = miosix::Thread::create(
51 reinterpret_cast<void*
>(
this), miosix::Thread::JOINABLE);
53 if (sndThread !=
nullptr)
56 LOG_ERR(logger,
"Could not start sender!");
62 rcvThread = miosix::Thread::create(rcvLauncher,
skywardStack(4 * 1024),
64 reinterpret_cast<void*
>(
this));
66 if (rcvThread !=
nullptr)
69 LOG_ERR(logger,
"Could not start receiver!");
72 if (sndStarted && rcvStarted)
73 LOG_DEBUG(logger,
"Sender and receiver started");
75 return sndStarted && rcvStarted;
103 if (src > 0xF || dst > 0xF)
122void CanProtocol::sendMessage(
const CanMessage& msg)
125 uint32_t leftToSend = msg.
length - 1;
131 packet.
id =
static_cast<uint32_t
>(msg.
id) |
132 ((
static_cast<uint32_t
>(0x3F) - leftToSend) &
137 for (
int i = 0; i < packet.
length; i++)
145 for (
int i = 1; i < msg.
length; i++)
148 static_cast<uint32_t
>(msg.
id) |
150 ((
static_cast<uint32_t
>(0x3F) - leftToSend) &
155 for (
int k = 0; k < packet.
length; k++)
164 uint8_t source, uint8_t destination,
165 uint8_t secondaryType)
168 secondaryType, 0xFF);
172 uint8_t source, uint8_t destination,
173 uint8_t secondaryType, uint64_t payload)
175 if (priority > 0xF || primaryType > 0x3F || source > 0xF ||
176 destination > 0xF || secondaryType > 0xF)
195 LOG_DEBUG(logger,
"Sending message with id: {:x}", msg.
id);
200void CanProtocol::runReceiver()
203 uint8_t nReceived = 0;
215 uint8_t leftToReceive =
216 static_cast<uint32_t
>(0x3F) -
222 if ((pkt.
id &
static_cast<uint32_t
>(
227 msg.
id = pkt.
id &
static_cast<uint32_t
>(
229 msg.
length = leftToReceive + 1;
245 if (msg.
length - nReceived - 1 == leftToReceive)
247 uint64_t payload = 0;
250 for (uint8_t i = 0; i < pkt.
length; i++)
252 uint64_t tmp = pkt.
data[i];
253 payload |= tmp << (i * 8);
264 if (nReceived == msg.
length && nReceived != 0)
266 LOG_DEBUG(logger,
"Message ready with id: {:x}", msg.
id);
278void CanProtocol::runSender()
285 outQueue.waitUntilNotEmpty();
287 if (!outQueue.isEmpty())
290 msg = outQueue.pop();
292 LOG_DEBUG(logger,
"Sending message, length: {}", msg.length);
301 miosix::Thread::sleep(50);
306void CanProtocol::rcvLauncher(
void* arg)
308 reinterpret_cast<CanProtocol*
>(arg)->runReceiver();
311void CanProtocol::sndLauncher(
void* arg)
316uint8_t CanProtocol::byteForUint64(uint64_t number)
320 for (i = 1; i <= 8; i++)
#define LOG_ERR(logger,...)
#define LOG_DEBUG(logger,...)
CanProtocol(CanbusDriver *can, MsgHandler onReceive, miosix::Priority threadPriority)
Construct a new CanProtocol object.
bool enqueueMsg(const CanMessage &msg)
Non-blocking send function, puts the messages in a queue. Message is discarded if the queue is full.
bool start()
Start the receiving and sending threads.
bool isStarted()
Tells whether the driver was started.
bool enqueueSimplePacket(uint8_t priority, uint8_t primaryType, uint8_t source, uint8_t destination, uint8_t secondaryType, uint64_t payload)
Non-blocking send function for a simple packet with a payload of 1 can packet, useful to send generic...
bool addFilter(uint8_t src, uint64_t dst)
Adds a filter to the can peripheral to receive only messages from the given source and targeted to th...
void stop()
Stops sender and receiver threads.
bool enqueueEvent(uint8_t priority, uint8_t primaryType, uint8_t source, uint8_t destination, uint8_t secondaryType)
Non-blocking send function for an event (a message without payload).
Low level CanBus driver, with support for both peripherals (CAN1 and CAN2) on stm32f4 microcontroller...
uint32_t send(CanPacket packet)
Sends a packet on the bus. This function blocks until the message has been successfully put into a TX...
IRQCircularBuffer< CanRXPacket, RX_BUF_SIZE > & getRXBuffer()
Returns a reference to the buffer containing received packets.
bool addFilter(FilterBank filter)
Adds a new filter to the bus, or returns false if there are no more filter banks available.
Driver for the VN100S IMU.
unsigned int skywardStack(unsigned int stack)
Generic struct that contains a can protocol message.
int32_t id
Id of the message without sequential infos.
uint8_t length
Length of the message content.
bool ext
Whether to use extended packet id.