1 // Copyright 2024 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <cstdint> 17 18 #include "hardware/i2c.h" 19 #include "pw_i2c/initiator.h" 20 #include "pw_sync/lock_annotations.h" 21 #include "pw_sync/mutex.h" 22 23 namespace pw::i2c { 24 25 // Initiator interface implementation based on I2C driver in Raspberry Pi Pico 26 // SDK. Currently supports only devices with 7 bit adresses. 27 class Rp2040Initiator final : public Initiator { 28 public: 29 struct Config { 30 uint32_t clock_frequency_hz; 31 uint8_t sda_pin; 32 uint8_t scl_pin; 33 }; 34 Rp2040Initiator(const Config & config,i2c_inst_t * instance)35 Rp2040Initiator(const Config& config, i2c_inst_t* instance) 36 : config_(config), instance_(instance) {} 37 38 // Should be called before attempting any transfers. 39 void Enable() PW_LOCKS_EXCLUDED(mutex_); 40 void Disable() PW_LOCKS_EXCLUDED(mutex_); 41 42 ~Rp2040Initiator() final; 43 44 private: 45 Status DoWriteReadFor(Address device_address, 46 ConstByteSpan tx_buffer, 47 ByteSpan rx_buffer, 48 chrono::SystemClock::duration timeout) override 49 PW_LOCKS_EXCLUDED(mutex_); 50 51 private: 52 sync::Mutex mutex_; 53 Config const config_; 54 i2c_inst_t* instance_ PW_GUARDED_BY(mutex_); 55 bool enabled_ PW_GUARDED_BY(mutex_); 56 }; 57 58 } // namespace pw::i2c 59