diff --git a/examples/generic/i2c_multiplex/i2c_multiplex.cpp b/examples/generic/i2c_multiplex/i2c_multiplex.cpp new file mode 100644 index 0000000000..631dd9e42f --- /dev/null +++ b/examples/generic/i2c_multiplex/i2c_multiplex.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2018, Sascha Schade + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +#include +#include +#include +#include +#include +#include + +using MyI2cMaster = modm::platform::I2cMaster1; +using Mpx = modm::Pca9548a; +using I2cMultiplexer = modm::I2cMultiplexer; + + +#ifndef MODM_BOARD_HAS_LOGGER +#include +// Add logger manually +using LoggerUsart = Usart2; +using LoggerUsartTx = modm::platform::GpioA2; +using LoggerUsartRx = modm::platform::GpioA3; +modm::IODeviceWrapper< LoggerUsart, modm::IOBuffer::BlockIfFull > loggerDevice; + +// Set all four logger streams to use the UART +modm::log::Logger modm::log::debug(loggerDevice); +modm::log::Logger modm::log::info(loggerDevice); +modm::log::Logger modm::log::warning(loggerDevice); +modm::log::Logger modm::log::error(loggerDevice); +#endif + +#undef MODM_LOG_LEVEL +#define MODM_LOG_LEVEL modm::log::DEBUG + + +namespace multiplexer +{ + I2cMultiplexer i2cMultiplexer; + + // Instances for each channel + using Ch0 = I2cMultiplexer::Ch0< i2cMultiplexer >; + using Ch1 = I2cMultiplexer::Ch1< i2cMultiplexer >; + using Ch2 = I2cMultiplexer::Ch2< i2cMultiplexer >; + using Ch3 = I2cMultiplexer::Ch3< i2cMultiplexer >; + using Ch4 = I2cMultiplexer::Ch4< i2cMultiplexer >; + using Ch5 = I2cMultiplexer::Ch5< i2cMultiplexer >; + using Ch6 = I2cMultiplexer::Ch6< i2cMultiplexer >; + using Ch7 = I2cMultiplexer::Ch7< i2cMultiplexer >; +} + + +class DeviceThread: public modm::pt::Protothread +{ +public: + DeviceThread() : dev0(0x29), dev1(0x29), dev2(0x29), dev3(0x29) + {} + + bool + update(); + +private: + modm::ShortTimeout timeout; + + // Simple devices which are just pingable. + // Independent of real device. Any I2C device should be pingable at its address. + modm::I2cDevice dev0; + modm::I2cDevice dev1; + modm::I2cDevice dev2; + modm::I2cDevice dev3; +}; + +bool +DeviceThread::update() +{ + PT_BEGIN(); + + MODM_LOG_DEBUG << MODM_FILE_INFO; + MODM_LOG_DEBUG << "Ping the Devices" << modm::endl; + + // ping the devices repeatedly + while(true) + { + MODM_LOG_DEBUG.printf("[dev ] ping0\n"); + MODM_LOG_DEBUG.printf("[dev ] ping0 res: %d\n", PT_CALL(dev0.ping())); + MODM_LOG_DEBUG.printf("[dev ] ping1\n"); + MODM_LOG_DEBUG.printf("[dev ] ping1 res: %d\n", PT_CALL(dev1.ping())); + MODM_LOG_DEBUG.printf("[dev ] ping2\n"); + MODM_LOG_DEBUG.printf("[dev ] ping2 res: %d\n", PT_CALL(dev2.ping())); + MODM_LOG_DEBUG.printf("[dev ] ping3\n"); + MODM_LOG_DEBUG.printf("[dev ] ping3 res: %d\n", PT_CALL(dev3.ping())); + // Do again in 1s + this->timeout.restart(1000); + PT_WAIT_UNTIL(this->timeout.isExpired()); + } + + PT_END(); +} + +int +main() +{ + Board::initialize(); + +#ifndef MODM_BOARD_HAS_LOGGER + LoggerUsart::connect(); + LoggerUsart::initialize(12); +#endif + + modm::platform::I2cMaster1::connect(); + modm::platform::I2cMaster1::initialize(); + + constexpr uint32_t rate = 1; // Hz + constexpr float interval = 1000.0 / rate; // msec + modm::ShortPeriodicTimer heartbeat(interval); + + // Main loop + DeviceThread deviceThread; + + uint32_t counter(0); + while (true) + { + deviceThread.update(); + + if (heartbeat.execute()) { + Board::Leds::toggle(); + MODM_LOG_INFO << "Loop counter: " << (counter++) << modm::endl; + } + } + + return 0; +} diff --git a/examples/generic/i2c_multiplex/project.xml b/examples/generic/i2c_multiplex/project.xml new file mode 100644 index 0000000000..1b6f2f305c --- /dev/null +++ b/examples/generic/i2c_multiplex/project.xml @@ -0,0 +1,23 @@ + + + ../../../src/modm/board/nucleo_f303k8/board.xml + + + + + + + + + :debug + :platform:gpio + :driver + :driver:pca9548a + :platform:i2c:1 + :platform:uart:2 + :processing:protothread + :processing:resumable + :processing:timer + :build:scons + +