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

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

modif sterror

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