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

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

flaircore

File size: 3.3 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 <errno.h>
24#include <cstring>
25
26using std::string;
27
28namespace flair
29{
30namespace core
31{
32
33RTDM_I2cPort::RTDM_I2cPort(const Object* parent,string name,string device) : I2cPort(parent,name)
34{
35 int err=0;
36 struct rti2c_config write_config;
37
38 //printf("i2c integer le mutex dans le driver, avec acces ioctl\n");
39 fd = rt_dev_open(device.c_str(), 0);
40 if (fd < 0)
41 {
42 Err("rt_dev_open (%s)\n",ObjectName().c_str(),strerror(-fd));
43 }
44
45 write_config.config_mask = RTI2C_SET_BAUD|RTI2C_SET_TIMEOUT_RX|RTI2C_SET_TIMEOUT_TX;
46 write_config.baud_rate = 400000;
47 write_config.rx_timeout = 500000;
48 write_config.tx_timeout = 1000000;//5ms pour les xbldc, normalement 1ms
49 // the rest implicitely remains default
50
51 // config
52 err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_CONFIG, &write_config );
53 if (err)
54 {
55 Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG error (%s)\n",ObjectName().c_str(),strerror(-err));
56 }
57}
58
59RTDM_I2cPort::~RTDM_I2cPort()
60{
61 rt_dev_close(fd);
62}
63
64int RTDM_I2cPort::SetSlave(uint16_t address)
65{
66 int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_SLAVE, &address);
67 if (err)
68 {
69 Err("rt_dev_ioctl RTI2C_RTIOC_SET_SLAVE error (%s)\n",strerror(-err));
70 }
71
72 return err;
73}
74
75void RTDM_I2cPort::SetRxTimeout(Time timeout_ns)
76{
77 struct rti2c_config write_config;
78
79 write_config.config_mask = RTI2C_SET_TIMEOUT_RX;
80 write_config.rx_timeout = timeout_ns;
81
82 int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_CONFIG, &write_config );
83 if (err)
84 {
85 Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG (%s)\n",ObjectName().c_str(),strerror(-err));
86 }
87}
88
89void RTDM_I2cPort::SetTxTimeout(Time timeout_ns)
90{
91 struct rti2c_config write_config;
92
93 write_config.config_mask = RTI2C_SET_TIMEOUT_TX;
94 write_config.tx_timeout = timeout_ns;
95
96 int err = rt_dev_ioctl(fd, RTI2C_RTIOC_SET_CONFIG, &write_config );
97 if (err)
98 {
99 Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG (%s)\n",ObjectName().c_str(),strerror(-err));
100 }
101}
102
103ssize_t RTDM_I2cPort::Write(const void *buf,size_t nbyte)
104{
105 return rt_dev_write(fd, buf, nbyte);
106}
107
108ssize_t RTDM_I2cPort::Read(void *buf,size_t nbyte)
109{
110 return rt_dev_read(fd, buf, nbyte);
111}
112
113} // end namespace core
114} // end namespace flair
115
116#else //__XENO__
117
118using std::string;
119using namespace flair::core;
120
121
122namespace flair
123{
124namespace core
125{
126
127RTDM_I2cPort::RTDM_I2cPort(const Object* parent,string name,string device) : I2cPort(parent,name)
128{
129 Err("not available in non real time\n");
130}
131
132RTDM_I2cPort::~RTDM_I2cPort()
133{
134
135}
136
137int RTDM_I2cPort::SetSlave(uint16_t address)
138{
139 return -1;
140}
141
142void RTDM_I2cPort::SetRxTimeout(Time timeout_ns)
143{
144}
145
146void RTDM_I2cPort::SetTxTimeout(Time timeout_ns)
147{
148}
149
150ssize_t RTDM_I2cPort::Write(const void *buf,size_t nbyte)
151{
152 return -1;
153}
154
155ssize_t RTDM_I2cPort::Read(void *buf,size_t nbyte)
156{
157 return -1;
158}
159
160} // end namespace core
161} // end namespace flair
162
163#endif
Note: See TracBrowser for help on using the repository browser.