|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Andrey Kunitsyn |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include "../device.hpp" |
| 15 | +#include <modm/platform/gpio/connector.hpp> |
| 16 | +#include <modm/architecture/interface/i2c_master.hpp> |
| 17 | +#include <modm/math/algorithm/prescaler.hpp> |
| 18 | +#include <hardware/structs/i2c.h> |
| 19 | + |
| 20 | +namespace modm::platform |
| 21 | +{ |
| 22 | + |
| 23 | +/** |
| 24 | + * I2cMaster implementation of I2C{{ id }} module. |
| 25 | + * |
| 26 | + * @author Andrey Kunitsyn |
| 27 | + * @ingroup modm_platform_i2c modm_platform_i2c_{{id}} |
| 28 | + */ |
| 29 | +class I2cMaster{{ id }} : public ::modm::I2cMaster |
| 30 | +{ |
| 31 | +public: |
| 32 | + static inline i2c_hw_t& hw() { return *i2c{{ id }}_hw; } |
| 33 | +public: |
| 34 | + template<class... Signals, ResetDevices reset = ResetDevices::Standard> |
| 35 | + static void |
| 36 | + connect(PullUps pullups = PullUps::External) |
| 37 | + { |
| 38 | + using Connector = GpioConnector<Peripheral::I2c{{ id }}, Signals...>; |
| 39 | + using Scl = typename Connector::template GetSignal<Gpio::Signal::Scl>; |
| 40 | + using Sda = typename Connector::template GetSignal<Gpio::Signal::Sda>; |
| 41 | + static_assert(sizeof...(Signals) == 2 and |
| 42 | + Connector::template IsValid<Scl> and Connector::template IsValid<Sda>, |
| 43 | + "I2cMaster{{id}}::connect() requires one Scl and one Sda signal!"); |
| 44 | + const Gpio::InputType input = |
| 45 | + (pullups == PullUps::Internal) ? Gpio::InputType::PullUp : Gpio::InputType::Floating; |
| 46 | + |
| 47 | + Connector::disconnect(); |
| 48 | + Scl::configure(input); |
| 49 | + Scl::setSlewRate(Gpio::SlewRate::Slow); |
| 50 | + Sda::configure(input); |
| 51 | + Sda::setSlewRate(Gpio::SlewRate::Slow); |
| 52 | + if (reset != ResetDevices::NoReset) resetDevices<Scl, uint32_t(reset)>(); |
| 53 | + Connector::connect(); |
| 54 | + } |
| 55 | + |
| 56 | + static void hwReset(); |
| 57 | + static void hwUnReset(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Set up the I2C module for master operation. |
| 61 | + * |
| 62 | + * @param rate |
| 63 | + * `Standard` or `Fast` or `Fast+`, `High` datarate is not supported |
| 64 | + */ |
| 65 | + template<class SystemClock, baudrate_t baudrate=kBd(100), percent_t tolerance=pct(5)> |
| 66 | + static void |
| 67 | + initialize(); |
| 68 | + |
| 69 | + template< class SystemClock, baudrate_t baudrate, percent_t tolerance=pct(5)> |
| 70 | + static uint32_t |
| 71 | + setBaudrate(); |
| 72 | + |
| 73 | + // start documentation inherited |
| 74 | + static bool |
| 75 | + start(I2cTransaction *transaction, ConfigurationHandler handler = nullptr); |
| 76 | + |
| 77 | + static Error |
| 78 | + getErrorState(); |
| 79 | + |
| 80 | + static void |
| 81 | + reset(); |
| 82 | + // end documentation inherited |
| 83 | + |
| 84 | + template <typename Wait> |
| 85 | + static Error transfer(uint8_t addr,const uint8_t* write,size_t writeLen, |
| 86 | + uint8_t* read, size_t readLen); |
| 87 | + |
| 88 | +private: |
| 89 | + template <typename Wait> |
| 90 | + static void doWrite(const uint8_t* write,size_t writeLen,bool nostop); |
| 91 | + template <typename Wait> |
| 92 | + static void doRead(uint8_t* read,size_t readLen,bool nostop); |
| 93 | + static bool isReadAvailable() { |
| 94 | + return hw().rxflr; |
| 95 | + } |
| 96 | + static bool isWriteAvailable() { |
| 97 | + constexpr size_t IC_TX_BUFFER_DEPTH = 16; |
| 98 | + return IC_TX_BUFFER_DEPTH - hw().txflr; |
| 99 | + } |
| 100 | + static inline Error errorState{Error::NoError}; |
| 101 | + static inline I2c::ConfigurationHandler configuration{nullptr}; |
| 102 | + static inline bool restartOnNext{false}; |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace modm::platform |
| 106 | +#include "i2c_master_impl_{{id}}.hpp" |
0 commit comments