APM:Libraries
I2CDevice.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2016 Intel Corporation. All rights reserved.
3  *
4  * This file is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Modified for use in AP_HAL_ChibiOS by Andrew Tridgell and Siddharth Bharat Purohit
18  */
19 
20 #pragma once
21 
22 #include <inttypes.h>
23 
24 #include <AP_HAL/HAL.h>
25 #include <AP_HAL/I2CDevice.h>
26 #include <AP_HAL/utility/OwnPtr.h>
27 #include "Semaphores.h"
28 #include "Device.h"
29 #include "shared_dma.h"
30 
31 #if HAL_USE_I2C == TRUE
32 
33 namespace ChibiOS {
34 
35 class I2CBus : public DeviceBus {
36 public:
37  I2CConfig i2ccfg;
38  uint8_t busnum;
39  uint32_t busclock;
41  bool i2c_active;
42 
43  void dma_allocate(Shared_DMA *);
44  void dma_deallocate(Shared_DMA *);
45  void dma_init(void);
46  static void clear_all(void);
47  static void clear_bus(ioline_t scl_line, uint8_t scl_af);
48 };
49 
50 class I2CDevice : public AP_HAL::I2CDevice {
51 public:
53  {
54  return static_cast<I2CDevice*>(dev);
55  }
56 
57  I2CDevice(uint8_t bus, uint8_t address, uint32_t bus_clock, bool use_smbus, uint32_t timeout_ms);
58  ~I2CDevice();
59 
60  /* See AP_HAL::I2CDevice::set_address() */
61  void set_address(uint8_t address) override { _address = address; }
62 
63  /* See AP_HAL::I2CDevice::set_retries() */
64  void set_retries(uint8_t retries) override { _retries = retries; }
65 
66  /* See AP_HAL::Device::set_speed(): Empty implementation, not supported. */
67  bool set_speed(enum Device::Speed speed) override { return true; }
68 
69  /* See AP_HAL::Device::transfer() */
70  bool transfer(const uint8_t *send, uint32_t send_len,
71  uint8_t *recv, uint32_t recv_len) override;
72 
73  bool read_registers_multiple(uint8_t first_reg, uint8_t *recv,
74  uint32_t recv_len, uint8_t times) override;
75 
76  /* See AP_HAL::Device::register_periodic_callback() */
78  uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
79 
80  /* See AP_HAL::Device::adjust_periodic_callback() */
81  bool adjust_periodic_callback(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
82 
84  // if asking for invalid bus number use bus 0 semaphore
85  return &bus.semaphore;
86  }
87 
88  void set_split_transfers(bool set) override {
89  _split_transfers = set;
90  }
91 
92 private:
94  bool _transfer(const uint8_t *send, uint32_t send_len,
95  uint8_t *recv, uint32_t recv_len);
96 
97  /* I2C interface #2 */
98  uint8_t _retries;
99  uint8_t _address;
100  char *pname;
103  uint32_t _timeout_ms;
104 };
105 
107 public:
108  friend class I2CDevice;
109 
110  static I2CBus businfo[];
111 
112  // constructor
114 
116  {
117  return static_cast<I2CDeviceManager*>(i2c_mgr);
118  }
119 
120  AP_HAL::OwnPtr<AP_HAL::I2CDevice> get_device(uint8_t bus, uint8_t address,
121  uint32_t bus_clock=400000,
122  bool use_smbus = false,
123  uint32_t timeout_ms=4) override;
124 };
125 }
126 
127 #endif // HAL_USE_I2C
128 
void set_retries(uint8_t retries) override
Definition: I2CDevice.h:64
void dma_allocate(Shared_DMA *)
Definition: I2CDevice.cpp:161
void dma_deallocate(Shared_DMA *)
Definition: I2CDevice.cpp:174
uint32_t busclock
Definition: I2CDevice.h:39
AP_HAL::OwnPtr< AP_HAL::Device > get_device(const char *name)
Definition: BusTest.cpp:22
void set_address(uint8_t address) override
Definition: I2CDevice.h:61
uint32_t _timeout_ms
Definition: I2CDevice.h:103
static I2CDeviceManager * from(AP_HAL::I2CDeviceManager *i2c_mgr)
Definition: I2CDevice.h:115
AP_HAL::Semaphore * get_semaphore() override
Definition: I2CDevice.h:83
static AP_HAL::OwnPtr< AP_HAL::Device > dev
Definition: ICM20789.cpp:16
I2CConfig i2ccfg
Definition: I2CDevice.h:37
bool i2c_started
Definition: I2CDevice.h:40
void * PeriodicHandle
Definition: Device.h:42
bool set_speed(enum Device::Speed speed) override
Definition: I2CDevice.h:67
static I2CDevice * from(AP_HAL::I2CDevice *dev)
Definition: I2CDevice.h:52
uint8_t _retries
Definition: I2CDevice.h:98
void dma_init(void)
Definition: I2CDevice.cpp:53
static void clear_all(void)
Definition: I2CDevice.cpp:61
AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb, AP_HAL::Device *hal_device)
Definition: Device.cpp:84
static void clear_bus(ioline_t scl_line, uint8_t scl_af)
Definition: I2CDevice.cpp:81
void set_split_transfers(bool set) override
Definition: I2CDevice.h:88
uint8_t _address
Definition: I2CDevice.h:99
uint8_t busnum
Definition: I2CDevice.h:38