sitara_depot/components/free_rtos/ethernet_industry/eth_ecat_api.hpp

90 lines
3.2 KiB
C++
Raw Normal View History

/*
* eth_ecat_api.hpp
*
* Created on: May 29, 2023
* Author: algin
*/
#ifndef FREE_RTOS_ETHERNET_INDUSTRY_ETH_ECAT_API_HPP_
#define FREE_RTOS_ETHERNET_INDUSTRY_ETH_ECAT_API_HPP_
#define COMX 1
#include "free_rtos/ethernet/eth.hpp"
#include "free_rtos/ethernet_industry/eth_ecat.hpp"
#include "free_rtos/ethernet_industry/eth_ecat_buffer.hpp"
#include "free_rtos/ethernet_industry/CoE/eth_ecat_sdo_mailbox.hpp"
#include "free_rtos/ethernet_industry/CoE/eth_ecat_pdo_fmmu.hpp"
namespace free_rtos {
/*
* Порядок инициализации:
* Инициализировать и открыть драйвер eth_.Init(...), eth_.Open()
* Создать служебный поток ecat_task_.Create(...) с вызовом EthEcatApi::process()
* Вызвать EthEcatApi::config_init(...)
* Создать пользовательский поток ecat_task_pdo_.Create(...)
* Для чтения/записи данных в пользовательском потоке вызвать
* pdo_write(...), pdo_read(...) или pdo_write_async(...), pdo_read_async(...)
*/
class EthEcatApi {
public:
EthEcatApi(Eth *eth)
: eth_{eth}
, ecat_{*eth}
, ecat_buffer_sdo_{ecat_}
, ecat_buffer_pdo_{ecat_}
, ecat_sdo_mailbox_{ecat_buffer_sdo_}
, ecat_pdo_fmmu_{ecat_buffer_pdo_} { }
bool config_init(TEthMacPorts port_id, uint16_t address_base, uint32_t period_microsec);
void process(); // Внутри бесконечный цикл. Запускать в отдельном потоке
void process_fake(uint32_t period_microsec);
std::vector<ecat_buffer::PDOMap>& get_ecat_pdo_map();
uint32_t get_pdo_counter() {
return ecat_pdo_fmmu_.get_pdo_counter();
}
template<typename... DataTypes>
ecat_sdo_mailbox::CompleteSize sdo_write(size_t slave_index, uint16_t index, uint8_t subindex, DataTypes&... data) {
return ecat_sdo_mailbox_.sdo_write<command::FP, DataTypes...>(slave_index, index, subindex, data...);
}
template<typename... DataTypes>
ecat_sdo_mailbox::CompleteSize sdo_read(size_t slave_index, uint16_t index, uint8_t subindex, DataTypes&... data) {
return ecat_sdo_mailbox_.sdo_read<command::FP, DataTypes...>(slave_index, index, subindex, data...);
}
template<typename... DataTypes>
bool pdo_write(uint32_t timeout_ticks, address::Offset offset, DataTypes&... data) {
return ecat_pdo_fmmu_.pdo_write<DataTypes...>(timeout_ticks, offset, data...);
}
template<typename... DataTypes>
bool pdo_read(uint32_t timeout_ticks, address::Offset offset, DataTypes&... data) {
return ecat_pdo_fmmu_.pdo_read<DataTypes...>(timeout_ticks, offset, data...);
}
void pdo_write_async(custom_promise::IPromise& promise) {
ecat_pdo_fmmu_.pdo_write_async(promise);
}
void pdo_read_async(custom_promise::IPromise& promise) {
ecat_pdo_fmmu_.pdo_read_async(promise);
}
private:
Eth *eth_;
EthEcat ecat_;
ecat_buffer::EthEcatBuffer ecat_buffer_sdo_;
ecat_buffer::EthEcatBuffer ecat_buffer_pdo_;
ecat_sdo_mailbox::EthEcatSdoMailbox ecat_sdo_mailbox_;
ecat_pdo_fmmu::EthEcatPdoFMMU ecat_pdo_fmmu_;
};
}
#endif /* FREE_RTOS_ETHERNET_INDUSTRY_ETH_ECAT_API_HPP_ */