source: flair-src/trunk/lib/FlairCore/src/RTDM_I2cPort.cpp

Last change on this file was 133, checked in by Sanahuja Guillaume, 5 years ago

modif sterror

File size: 3.5 KB
RevLine 
[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 <cstring>
24
25using std::string;
26
[15]27namespace flair {
28namespace core {
[2]29
[15]30RTDM_I2cPort::RTDM_I2cPort(const Object *parent, string name, string device)
31 : I2cPort(parent, name) {
32 int err = 0;
33 struct rti2c_config write_config;
[2]34
[15]35 // printf("i2c integer le mutex dans le driver, avec acces ioctl\n");
36 fd = rt_dev_open(device.c_str(), 0);
37 if (fd < 0) {
[133]38 char errorMsg[256];
39 Err("rt_dev_open (%s)\n", ObjectName().c_str(), strerror_r(-fd, errorMsg, sizeof(errorMsg)));
[15]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) {
[133]52 char errorMsg[256];
[15]53 Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG error (%s)\n",
[133]54 ObjectName().c_str(), strerror_r(-err, errorMsg, sizeof(errorMsg)));
[15]55 }
[2]56}
57
[15]58RTDM_I2cPort::~RTDM_I2cPort() { rt_dev_close(fd); }
[2]59
[15]60int RTDM_I2cPort::SetSlave(uint16_t address) {
61 int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_SLAVE, &address);
62 if (err) {
[133]63 char errorMsg[256];
64 Err("rt_dev_ioctl RTI2C_RTIOC_SET_SLAVE error (%s)\n", strerror_r(-err, errorMsg, sizeof(errorMsg)));
[15]65 }
[2]66
[15]67 return err;
[2]68}
69
[15]70void RTDM_I2cPort::SetRxTimeout(Time timeout_ns) {
71 struct rti2c_config write_config;
[2]72
[15]73 write_config.config_mask = RTI2C_SET_TIMEOUT_RX;
74 write_config.rx_timeout = timeout_ns;
[2]75
[15]76 int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_CONFIG, &write_config);
77 if (err) {
[133]78 char errorMsg[256];
[15]79 Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG (%s)\n", ObjectName().c_str(),
[133]80 strerror_r(-err, errorMsg, sizeof(errorMsg)));
[15]81 }
[2]82}
83
[15]84void RTDM_I2cPort::SetTxTimeout(Time timeout_ns) {
85 struct rti2c_config write_config;
[2]86
[15]87 write_config.config_mask = RTI2C_SET_TIMEOUT_TX;
88 write_config.tx_timeout = timeout_ns;
[2]89
[15]90 int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_CONFIG, &write_config);
91 if (err) {
[133]92 char errorMsg[256];
[15]93 Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG (%s)\n", ObjectName().c_str(),
[133]94 strerror_r(-err, errorMsg, sizeof(errorMsg)));
[15]95 }
[2]96}
97
[15]98ssize_t RTDM_I2cPort::Write(const void *buf, size_t nbyte) {
99 return rt_dev_write(fd, buf, nbyte);
[2]100}
101
[15]102ssize_t RTDM_I2cPort::Read(void *buf, size_t nbyte) {
103 return rt_dev_read(fd, buf, nbyte);
[2]104}
105
106} // end namespace core
107} // end namespace flair
108
109#else //__XENO__
110
111using std::string;
112using namespace flair::core;
113
[15]114namespace flair {
115namespace core {
[2]116
[15]117RTDM_I2cPort::RTDM_I2cPort(const Object *parent, string name, string device)
118 : I2cPort(parent, name) {
119 Err("not available in non real time\n");
[2]120}
121
[15]122RTDM_I2cPort::~RTDM_I2cPort() {}
[2]123
[15]124int RTDM_I2cPort::SetSlave(uint16_t address) { return -1; }
[2]125
[15]126void RTDM_I2cPort::SetRxTimeout(Time timeout_ns) {}
[2]127
[15]128void RTDM_I2cPort::SetTxTimeout(Time timeout_ns) {}
[2]129
[15]130ssize_t RTDM_I2cPort::Write(const void *buf, size_t nbyte) { return -1; }
[2]131
[15]132ssize_t RTDM_I2cPort::Read(void *buf, size_t nbyte) { return -1; }
[2]133
134} // end namespace core
135} // end namespace flair
136
137#endif
Note: See TracBrowser for help on using the repository browser.