37template <
class FlightPhases,
class SimulatorData,
class ActuatorData>
67 miosix::Lock<miosix::FastMutex> l(
mutex);
88template <
class FlightPhases,
class SimulatorData,
class ActuatorData>
99 ActuatorData>* hilPhasesManager)
101 hilPhasesManager(hilPhasesManager),
102 dmaStreamTx(
DMADriver::instance().acquireStreamForPeripheral(
104 DMADefs::Peripherals::PE_UART4_TX,
std::chrono::seconds(1)))
120 miosix::Lock<miosix::FastMutex> l(
mutex);
126 this->actuatorData = actuatorData;
143 SimulatorData simulatorData;
144 ActuatorData actuatorData;
152 bool writeDma(
void* buffer, uint16_t nBytes,
153 std::chrono::nanoseconds timeout);
168template <
class FlightPhases,
class SimulatorData,
class ActuatorData>
169void HILTransceiver<FlightPhases, SimulatorData, ActuatorData>::run()
171 LOG_INFO(logger,
"HIL Transceiver started");
172 hilSerial.clearQueue();
175 hilSerial.write(&actuatorData,
sizeof(ActuatorData));
178 while (!shouldStop())
182 SimulatorData tempData;
186 if (!hilSerial.readBlocking(&tempData,
sizeof(SimulatorData),
189 LOG_ERR(logger,
"Failed serial read");
192 assert(nRead ==
sizeof(SimulatorData) &&
193 "Read less then SimulatorData bytes");
195 hilSerial.clearQueue();
198 miosix::PauseKernelLock kLock;
199 simulatorData = tempData;
200 timestampSimulatorData = miosix::getTime();
205 if (!receivedFirstPacket)
207 receivedFirstPacket =
true;
208 hilPhasesManager->simulationStarted();
212 hilPhasesManager->processFlags(simulatorData);
214 if (nLostUpdates > 0)
219 LOG_WARN(logger,
"{} lost updates", nLostUpdates);
226 writeDma(&actuatorData,
sizeof(ActuatorData),
227 std::chrono::milliseconds(100));
229 hilSerial.write(&actuatorData,
sizeof(ActuatorData));
235template <
class FlightPhases,
class SimulatorData,
class ActuatorData>
236bool HILTransceiver<FlightPhases, SimulatorData, ActuatorData>::writeDma(
237 void* buffer, uint16_t nBytes, std::chrono::nanoseconds timeout)
239 USARTType* usart = hilSerial.getPeripheral();
241 DMATransaction setup{
246 .srcAddress = buffer,
247 .dstAddress = (
void*)&(usart->TDR),
248 .numberOfDataItems = nBytes,
249 .srcIncrement =
true,
250 .dstIncrement =
false,
251 .enableTransferCompleteInterrupt =
true,
252 .enableTransferErrorInterrupt =
true,
255 dmaStreamTx->setup(setup);
259 usart->ICR |= USART_ICR_TCCF;
261 dmaStreamTx->enable();
262 bool ret = dmaStreamTx->timedWaitForTransferComplete(timeout);
271 while ((usart->ISR & USART_ISR_TC) == 0)
#define LOG_WARN(logger,...)
#define LOG_INFO(logger,...)
#define LOG_ERR(logger,...)
This class is responsible for streams acquisition, streams release and interrupt handling.
Simple RAII class to handle DMA streams.
Singleton object that manages all the phases of the simulation. After his instantiation we need to se...
miosix::ConditionVariable condVar
void waitActuatorData()
Waits for the control algorithm(s) to update actuatorData.
int getLostUpdates() const
Returns the number of lost updates.
int64_t timestampSimulatorData
HILTransceiverBase(USART &hilSerial)
Construct a serial connection attached to a control algorithm.
int64_t getTimestampSimulatorData() const
Returns the value in ns of the timestamp of the last received simulatorData.
HILTransceiver is a Singleton and provides an easy interface for the control algorithms to send and r...
HILTransceiver(USART &hilSerial, HILPhasesManager< FlightPhases, SimulatorData, ActuatorData > *hilPhasesManager)
Construct a serial connection attached to a control algorithm.
const SimulatorData * getSensorData() const
returns the reference of the SimulatorData
void setActuatorData(ActuatorData actuatorData)
sets the actuator data and then wakes up the MatlabTransceiver thread in order to send the data back ...
static PrintLogger getLogger(const string &name)
Driver for STM32F4 low level USART/UART peripheral.
USARTType * getPeripheral()
Driver for the VN100S IMU.