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

Last change on this file since 42 was 15, checked in by Bayard Gildas, 5 years ago

sources reformatted with flair-format-dir script

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 {
29namespace core {
30
31RTDM_I2cPort::RTDM_I2cPort(const Object *parent, string name, string device)
32    : I2cPort(parent, name) {
33  int err = 0;
34  struct rti2c_config write_config;
35
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  }
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    Err("rt_dev_ioctl RTI2C_RTIOC_SET_CONFIG error (%s)\n",
53        ObjectName().c_str(), strerror(-err));
54  }
55}
56
57RTDM_I2cPort::~RTDM_I2cPort() { rt_dev_close(fd); }
58
59int 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  }
64
65  return err;
66}
67
68void RTDM_I2cPort::SetRxTimeout(Time timeout_ns) {
69  struct rti2c_config write_config;
70
71  write_config.config_mask = RTI2C_SET_TIMEOUT_RX;
72  write_config.rx_timeout = timeout_ns;
73
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  }
79}
80
81void RTDM_I2cPort::SetTxTimeout(Time timeout_ns) {
82  struct rti2c_config write_config;
83
84  write_config.config_mask = RTI2C_SET_TIMEOUT_TX;
85  write_config.tx_timeout = timeout_ns;
86
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  }
92}
93
94ssize_t RTDM_I2cPort::Write(const void *buf, size_t nbyte) {
95  return rt_dev_write(fd, buf, nbyte);
96}
97
98ssize_t RTDM_I2cPort::Read(void *buf, size_t nbyte) {
99  return rt_dev_read(fd, buf, nbyte);
100}
101
102} // end namespace core
103} // end namespace flair
104
105#else //__XENO__
106
107using std::string;
108using namespace flair::core;
109
110namespace flair {
111namespace core {
112
113RTDM_I2cPort::RTDM_I2cPort(const Object *parent, string name, string device)
114    : I2cPort(parent, name) {
115  Err("not available in non real time\n");
116}
117
118RTDM_I2cPort::~RTDM_I2cPort() {}
119
120int RTDM_I2cPort::SetSlave(uint16_t address) { return -1; }
121
122void RTDM_I2cPort::SetRxTimeout(Time timeout_ns) {}
123
124void RTDM_I2cPort::SetTxTimeout(Time timeout_ns) {}
125
126ssize_t RTDM_I2cPort::Write(const void *buf, size_t nbyte) { return -1; }
127
128ssize_t RTDM_I2cPort::Read(void *buf, size_t nbyte) { return -1; }
129
130} // end namespace core
131} // end namespace flair
132
133#endif
Note: See TracBrowser for help on using the repository browser.