MotionLib  1.0.0
SamBuCa motion library
EdgeGrblDeviceBase.hpp
1 /*
2  * Copyright (C) 2022 CERN
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * Created on: 2022-03-08
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #ifndef EDGEGRBLDEVICEBASE_HPP__
26 #define EDGEGRBLDEVICEBASE_HPP__
27 
28 #include <atomic>
29 #include <chrono>
30 #include <condition_variable>
31 #include <future>
32 #include <mutex>
33 #include <string>
34 #include <thread>
35 #include <vector>
36 
37 #include <fmc_mfe/libfmc_mfe.h>
38 
39 #include "util/grbl/GrblDeviceBase.hpp"
40 
41 namespace edge {
51 {
52 public:
59  explicit EdgeGrblDeviceBase(int lun,
60  const std::string &driverPath = std::string());
61 
63 
64  void read() override;
65  void write(const std::string &data) override;
66  bool wait(const std::chrono::milliseconds &timeout) override;
67  void wake() override;
68 
76  inline void read(struct edge_reg *reg, uint32_t &value)
77  {
78  return read(reg, 0, value);
79  }
80 
89  void read(struct edge_reg *reg, size_t offset, uint32_t &value);
90 
98  void read(struct edge_reg *reg, std::vector<uint32_t> &values);
99 
107  inline void write(struct edge_reg *reg, uint32_t value)
108  {
109  return write(reg, 0, value);
110  }
111 
120  void write(struct edge_reg *reg, size_t offset, uint32_t value);
121 
125  inline struct fmc_mfe_hdl *getDrv() const { return m_edgeDrvHdl; }
126 
127 protected:
129 
130  void init(int lun, const std::string &driverPath);
135  void irqLoop();
136 
146  bool waitIrq(const std::chrono::milliseconds &timeout,
147  const struct edge_reg *const expectedReg,
148  uint32_t expectedBit);
149 
157  void drvOpen(int lun, const std::string &simPath, bool isSim);
158 
163  void drvClose();
164 
165  int m_wakeFd[2];
166 
167  struct fmc_mfe_hdl *m_edgeDrvHdl;
168  std::vector<char> m_fifoBytes;
169 
170  static const std::string loggerCat;
171 };
172 
176 } /* namespace edge */
177 
178 #endif
void drvOpen(int lun, const std::string &simPath, bool isSim)
Open FMC_MFE device with given driver.
void write(struct edge_reg *reg, uint32_t value)
Write a value to register.
void irqLoop()
Wait for irq using blocking API and set variables once available.
bool wait(const std::chrono::milliseconds &timeout) override
wait for input
struct fmc_mfe_hdl * getDrv() const
get driver handler
bool waitIrq(const std::chrono::milliseconds &timeout, const struct edge_reg *const expectedReg, uint32_t expectedBit)
Wait for irq from given source.
void drvClose()
Close the driver.
void write(const std::string &data) override
raw write some bytes
void read(struct edge_reg *reg, uint32_t &value)
Get a register value.
void wake() override
wake thread waiting for input
void read() override
fill LineBuffer with incoming bytes