[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 4 | // %flair:license}
|
---|
| 5 | // created: 2013/09/04
|
---|
| 6 | // filename: RTDM_RTDM_I2cPort.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Class for real time i2c port using RTDM
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "RTDM_I2cPort.h"
|
---|
| 19 |
|
---|
| 20 | #ifdef __XENO__
|
---|
| 21 |
|
---|
| 22 | #include <rtdm/rti2c.h>
|
---|
| 23 | #include <errno.h>
|
---|
| 24 | #include <cstring>
|
---|
| 25 |
|
---|
| 26 | using std::string;
|
---|
| 27 |
|
---|
[15] | 28 | namespace flair {
|
---|
| 29 | namespace core {
|
---|
[2] | 30 |
|
---|
[15] | 31 | RTDM_I2cPort::RTDM_I2cPort(const Object *parent, string name, string device)
|
---|
| 32 | : I2cPort(parent, name) {
|
---|
| 33 | int err = 0;
|
---|
| 34 | struct rti2c_config write_config;
|
---|
[2] | 35 |
|
---|
[15] | 36 | // printf("i2c integer le mutex dans le driver, avec acces ioctl\n");
|
---|
| 37 | fd = rt_dev_open(device.c_str(), 0);
|
---|
| 38 | if (fd < 0) {
|
---|
| 39 | Err("rt_dev_open (%s)\n", ObjectName().c_str(), strerror(-fd));
|
---|
| 40 | }
|
---|
[2] | 41 |
|
---|
[15] | 42 | write_config.config_mask =
|
---|
| 43 | RTI2C_SET_BAUD | RTI2C_SET_TIMEOUT_RX | RTI2C_SET_TIMEOUT_TX;
|
---|
| 44 | write_config.baud_rate = 400000;
|
---|
| 45 | write_config.rx_timeout = 500000;
|
---|
| 46 | write_config.tx_timeout = 1000000; // 5ms pour les xbldc, normalement 1ms
|
---|
| 47 | // the rest implicitely remains default
|
---|
[2] | 48 |
|
---|
[15] | 49 | // config
|
---|
| 50 | err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_CONFIG, &write_config);
|
---|
| 51 | if (err) {
|
---|
| 52 | Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG error (%s)\n",
|
---|
| 53 | ObjectName().c_str(), strerror(-err));
|
---|
| 54 | }
|
---|
[2] | 55 | }
|
---|
| 56 |
|
---|
[15] | 57 | RTDM_I2cPort::~RTDM_I2cPort() { rt_dev_close(fd); }
|
---|
[2] | 58 |
|
---|
[15] | 59 | int RTDM_I2cPort::SetSlave(uint16_t address) {
|
---|
| 60 | int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_SLAVE, &address);
|
---|
| 61 | if (err) {
|
---|
| 62 | Err("rt_dev_ioctl RTI2C_RTIOC_SET_SLAVE error (%s)\n", strerror(-err));
|
---|
| 63 | }
|
---|
[2] | 64 |
|
---|
[15] | 65 | return err;
|
---|
[2] | 66 | }
|
---|
| 67 |
|
---|
[15] | 68 | void RTDM_I2cPort::SetRxTimeout(Time timeout_ns) {
|
---|
| 69 | struct rti2c_config write_config;
|
---|
[2] | 70 |
|
---|
[15] | 71 | write_config.config_mask = RTI2C_SET_TIMEOUT_RX;
|
---|
| 72 | write_config.rx_timeout = timeout_ns;
|
---|
[2] | 73 |
|
---|
[15] | 74 | int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_CONFIG, &write_config);
|
---|
| 75 | if (err) {
|
---|
| 76 | Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG (%s)\n", ObjectName().c_str(),
|
---|
| 77 | strerror(-err));
|
---|
| 78 | }
|
---|
[2] | 79 | }
|
---|
| 80 |
|
---|
[15] | 81 | void RTDM_I2cPort::SetTxTimeout(Time timeout_ns) {
|
---|
| 82 | struct rti2c_config write_config;
|
---|
[2] | 83 |
|
---|
[15] | 84 | write_config.config_mask = RTI2C_SET_TIMEOUT_TX;
|
---|
| 85 | write_config.tx_timeout = timeout_ns;
|
---|
[2] | 86 |
|
---|
[15] | 87 | int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_CONFIG, &write_config);
|
---|
| 88 | if (err) {
|
---|
| 89 | Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG (%s)\n", ObjectName().c_str(),
|
---|
| 90 | strerror(-err));
|
---|
| 91 | }
|
---|
[2] | 92 | }
|
---|
| 93 |
|
---|
[15] | 94 | ssize_t RTDM_I2cPort::Write(const void *buf, size_t nbyte) {
|
---|
| 95 | return rt_dev_write(fd, buf, nbyte);
|
---|
[2] | 96 | }
|
---|
| 97 |
|
---|
[15] | 98 | ssize_t RTDM_I2cPort::Read(void *buf, size_t nbyte) {
|
---|
| 99 | return rt_dev_read(fd, buf, nbyte);
|
---|
[2] | 100 | }
|
---|
| 101 |
|
---|
| 102 | } // end namespace core
|
---|
| 103 | } // end namespace flair
|
---|
| 104 |
|
---|
| 105 | #else //__XENO__
|
---|
| 106 |
|
---|
| 107 | using std::string;
|
---|
| 108 | using namespace flair::core;
|
---|
| 109 |
|
---|
[15] | 110 | namespace flair {
|
---|
| 111 | namespace core {
|
---|
[2] | 112 |
|
---|
[15] | 113 | RTDM_I2cPort::RTDM_I2cPort(const Object *parent, string name, string device)
|
---|
| 114 | : I2cPort(parent, name) {
|
---|
| 115 | Err("not available in non real time\n");
|
---|
[2] | 116 | }
|
---|
| 117 |
|
---|
[15] | 118 | RTDM_I2cPort::~RTDM_I2cPort() {}
|
---|
[2] | 119 |
|
---|
[15] | 120 | int RTDM_I2cPort::SetSlave(uint16_t address) { return -1; }
|
---|
[2] | 121 |
|
---|
[15] | 122 | void RTDM_I2cPort::SetRxTimeout(Time timeout_ns) {}
|
---|
[2] | 123 |
|
---|
[15] | 124 | void RTDM_I2cPort::SetTxTimeout(Time timeout_ns) {}
|
---|
[2] | 125 |
|
---|
[15] | 126 | ssize_t RTDM_I2cPort::Write(const void *buf, size_t nbyte) { return -1; }
|
---|
[2] | 127 |
|
---|
[15] | 128 | ssize_t RTDM_I2cPort::Read(void *buf, size_t nbyte) { return -1; }
|
---|
[2] | 129 |
|
---|
| 130 | } // end namespace core
|
---|
| 131 | } // end namespace flair
|
---|
| 132 |
|
---|
| 133 | #endif
|
---|