2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015-2016 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-10-20 10:31:12 -03:00
|
|
|
*
|
2018-01-05 02:19:51 -04:00
|
|
|
* Modified for use in AP_HAL_ChibiOS by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include <AP_HAL/HAL.h>
|
|
|
|
#include <AP_HAL/I2CDevice.h>
|
|
|
|
#include <AP_HAL/utility/OwnPtr.h>
|
2018-08-07 05:47:10 -03:00
|
|
|
#include "AP_HAL_ChibiOS.h"
|
|
|
|
|
|
|
|
#if HAL_USE_I2C == TRUE
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
#include "Semaphores.h"
|
|
|
|
#include "Device.h"
|
|
|
|
#include "shared_dma.h"
|
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
namespace ChibiOS {
|
2018-03-01 20:46:30 -04:00
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
class I2CBus : public DeviceBus {
|
2018-01-10 00:26:05 -04:00
|
|
|
public:
|
|
|
|
I2CConfig i2ccfg;
|
|
|
|
uint8_t busnum;
|
2018-04-26 15:56:40 -03:00
|
|
|
uint32_t busclock;
|
2018-06-04 08:27:54 -03:00
|
|
|
|
|
|
|
// we need an additional lock in the dma_allocate and
|
|
|
|
// dma_deallocate functions to cope with 3-way contention as we
|
|
|
|
// have two DMA channels that we are handling with the shared_dma
|
|
|
|
// code
|
|
|
|
mutex_t dma_lock;
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-03-14 03:06:30 -03:00
|
|
|
void dma_allocate(Shared_DMA *);
|
|
|
|
void dma_deallocate(Shared_DMA *);
|
2018-01-10 00:26:05 -04:00
|
|
|
void dma_init(void);
|
2018-02-05 15:40:05 -04:00
|
|
|
static void clear_all(void);
|
2018-11-15 06:25:29 -04:00
|
|
|
static void clear_bus(uint8_t busidx);
|
|
|
|
static uint8_t read_sda(uint8_t busidx);
|
2021-10-04 13:53:26 -03:00
|
|
|
static bool check_select_pins(uint8_t check_pins);
|
|
|
|
static void set_bus_to_floating(uint8_t busidx);
|
2018-01-10 00:26:05 -04:00
|
|
|
};
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
class I2CDevice : public AP_HAL::I2CDevice {
|
2018-01-05 02:19:51 -04:00
|
|
|
public:
|
|
|
|
static I2CDevice *from(AP_HAL::I2CDevice *dev)
|
|
|
|
{
|
|
|
|
return static_cast<I2CDevice*>(dev);
|
|
|
|
}
|
|
|
|
|
2018-01-10 00:26:05 -04:00
|
|
|
I2CDevice(uint8_t bus, uint8_t address, uint32_t bus_clock, bool use_smbus, uint32_t timeout_ms);
|
2018-01-05 02:19:51 -04:00
|
|
|
~I2CDevice();
|
|
|
|
|
|
|
|
/* See AP_HAL::I2CDevice::set_address() */
|
|
|
|
void set_address(uint8_t address) override { _address = address; }
|
|
|
|
|
|
|
|
/* See AP_HAL::I2CDevice::set_retries() */
|
|
|
|
void set_retries(uint8_t retries) override { _retries = retries; }
|
|
|
|
|
|
|
|
/* See AP_HAL::Device::set_speed(): Empty implementation, not supported. */
|
|
|
|
bool set_speed(enum Device::Speed speed) override { return true; }
|
|
|
|
|
|
|
|
/* See AP_HAL::Device::transfer() */
|
|
|
|
bool transfer(const uint8_t *send, uint32_t send_len,
|
|
|
|
uint8_t *recv, uint32_t recv_len) override;
|
|
|
|
|
|
|
|
bool read_registers_multiple(uint8_t first_reg, uint8_t *recv,
|
|
|
|
uint32_t recv_len, uint8_t times) override;
|
|
|
|
|
|
|
|
/* See AP_HAL::Device::register_periodic_callback() */
|
|
|
|
AP_HAL::Device::PeriodicHandle register_periodic_callback(
|
|
|
|
uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
|
|
|
|
|
|
|
|
/* See AP_HAL::Device::adjust_periodic_callback() */
|
|
|
|
bool adjust_periodic_callback(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
|
|
|
|
|
|
|
|
AP_HAL::Semaphore* get_semaphore() override {
|
|
|
|
// if asking for invalid bus number use bus 0 semaphore
|
2018-01-10 00:26:05 -04:00
|
|
|
return &bus.semaphore;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_split_transfers(bool set) override {
|
|
|
|
_split_transfers = set;
|
|
|
|
}
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
private:
|
2018-01-10 00:26:05 -04:00
|
|
|
I2CBus &bus;
|
2018-01-05 02:19:51 -04:00
|
|
|
bool _transfer(const uint8_t *send, uint32_t send_len,
|
|
|
|
uint8_t *recv, uint32_t recv_len);
|
|
|
|
|
|
|
|
/* I2C interface #2 */
|
|
|
|
uint8_t _retries;
|
|
|
|
uint8_t _address;
|
|
|
|
char *pname;
|
|
|
|
bool _split_transfers;
|
2018-01-10 00:26:05 -04:00
|
|
|
bool _use_smbus;
|
|
|
|
uint32_t _timeout_ms;
|
2018-01-05 02:19:51 -04:00
|
|
|
};
|
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
class I2CDeviceManager : public AP_HAL::I2CDeviceManager {
|
2018-01-05 02:19:51 -04:00
|
|
|
public:
|
|
|
|
friend class I2CDevice;
|
|
|
|
|
2018-01-10 00:26:05 -04:00
|
|
|
static I2CBus businfo[];
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-10 00:26:05 -04:00
|
|
|
// constructor
|
|
|
|
I2CDeviceManager();
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
static I2CDeviceManager *from(AP_HAL::I2CDeviceManager *i2c_mgr)
|
|
|
|
{
|
|
|
|
return static_cast<I2CDeviceManager*>(i2c_mgr);
|
|
|
|
}
|
|
|
|
|
2018-01-10 00:26:05 -04:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> get_device(uint8_t bus, uint8_t address,
|
|
|
|
uint32_t bus_clock=400000,
|
|
|
|
bool use_smbus = false,
|
|
|
|
uint32_t timeout_ms=4) override;
|
2018-06-21 00:09:15 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
get mask of bus numbers for all configured I2C buses
|
|
|
|
*/
|
|
|
|
uint32_t get_bus_mask(void) const override;
|
|
|
|
|
|
|
|
/*
|
|
|
|
get mask of bus numbers for all configured external I2C buses
|
|
|
|
*/
|
|
|
|
uint32_t get_bus_mask_external(void) const override;
|
|
|
|
|
|
|
|
/*
|
|
|
|
get mask of bus numbers for all configured internal I2C buses
|
|
|
|
*/
|
|
|
|
uint32_t get_bus_mask_internal(void) const override;
|
2018-01-05 02:19:51 -04:00
|
|
|
};
|
2018-03-02 00:37:10 -04:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_I2C
|