libyggdrasil  v1.0.0
can.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Asgard - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 
30 #include <string>
31 #include <string_view>
32 #include <array>
33 
34 namespace bsp::asg_coproc::drv {
35 
42  template<auto Context>
43  struct CAN {
44 
50  static bool init() {
51  return HAL_FDCAN_Start(Context) == HAL_OK;
52  }
53 
59  static bool deinit() {
60  return true;
61  }
62 
66  static bool enable() {
67  return HAL_CAN_Start(Context) == HAL_OK;
68  }
69 
73  static bool disable() {
74  return HAL_CAN_Stop(Context) == HAL_OK;
75  }
76 
77 
86  static void read(u32 &id, u32 &extendedId, u32 &timestamp, std::array<u8, 8> &data) {
87  FDCAN_RxHeaderTypeDef rxHeader = {0};
88  while (HAL_CAN_GetRxFifoFillLevel(Context, FDCAN_RX_FIFO0) == 0);
89  HAL_CAN_GetRxMessage(Context, FDCAN_RX_FIFO0, &rxHeader, data.data());
90  if (rxHeader.IdType == FDCAN_STANDARD_ID) {
91  id = rxHeader.Identifier;
92  }
93  else {
94  extendedId = rxHeader.Identifier;
95  }
96  timestamp = rxHeader.RxTimestamp;
97  }
98 
105  template<typename T>
107  u32 pTxMailbox;
108  FDCAN_TxHeaderTypeDef txHeader = {0};
109  constexpr size_t Size = sizeof(T);
110 
111  std::array<u8, Size> data;
112  std::memcpy(data.data(), &packet.data, Size);
113  if (packet.extendedId > 0) {
114  txHeader.Identifier = packet.extendedId;
115  txHeader.IdType = FDCAN_EXTENDED_ID;
116  }
117  else {
118  txHeader.Identifier = packet.id;
119  txHeader.IdType = FDCAN_STANDARD_ID;
120  }
121 
122  txHeader.DataLength = Size;
123 
124  HAL_CAN_AddTxMessage(Context, &txHeader, const_cast<u8*>(data.data()), &pTxMailbox);
125  while (HAL_CAN_GetTxMailboxesFreeLevel(Context) != 3);
126  return pTxMailbox;
127  }
128 
129  };
130 
131 }
bsp::asg_coproc::drv::CAN::write
static u32 write(bsp::drv::CANPacket< T > packet)
CAN write.
Definition: can.hpp:106
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::asg_coproc::drv::CAN::init
static bool init()
Init function.
Definition: can.hpp:50
bsp::asg_coproc::drv
Definition: adc.hpp:32
bsp::asg_coproc::drv::CAN::enable
static bool enable()
Can enable.
Definition: can.hpp:66
bsp::drv::CANPacket::id
u32 id
Definition: can.hpp:39
bsp::drv::CANPacket::extendedId
u32 extendedId
Definition: can.hpp:39
u32
uint32_t u32
Definition: types.h:38
bsp::asg_coproc::drv::CAN::deinit
static bool deinit()
Deinit function.
Definition: can.hpp:59
bsp::drv::CANPacket
Definition: can.hpp:38
bsp::drv::CANPacket::data
T data
Definition: can.hpp:43
bsp::asg_coproc::drv::CAN::disable
static bool disable()
Can disable.
Definition: can.hpp:73
can.hpp
Frontend for the CAN abstraction.
bsp::asg_coproc::drv::CAN::read
static void read(u32 &id, u32 &extendedId, u32 &timestamp, std::array< u8, 8 > &data)
CAN receive.
Definition: can.hpp:86
bsp::asg_coproc::drv::CAN
CAN implementation for Asgard.
Definition: can.hpp:43