From d1938ebdb8c67fe07f2ee0d2ced607293fdad58c Mon Sep 17 00:00:00 2001 From: Christopher Durand Date: Tue, 28 Mar 2023 15:57:50 +0200 Subject: [PATCH] [test] Add SAMV71 I2C unit test for Xplained Ultra board --- .../platform/i2c/samx7x/i2c_platform_test.cpp | 71 +++++++++++++++++++ .../platform/i2c/samx7x/i2c_platform_test.hpp | 26 +++++++ test/modm/platform/i2c/samx7x/module.lb | 36 ++++++++++ 3 files changed, 133 insertions(+) create mode 100644 test/modm/platform/i2c/samx7x/i2c_platform_test.cpp create mode 100644 test/modm/platform/i2c/samx7x/i2c_platform_test.hpp create mode 100644 test/modm/platform/i2c/samx7x/module.lb diff --git a/test/modm/platform/i2c/samx7x/i2c_platform_test.cpp b/test/modm/platform/i2c/samx7x/i2c_platform_test.cpp new file mode 100644 index 0000000000..57c397cb98 --- /dev/null +++ b/test/modm/platform/i2c/samx7x/i2c_platform_test.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023, Christopher Durand + * + * 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 "i2c_platform_test.hpp" + +#include +#include +#include + +using namespace modm::platform; +using namespace Board; + +namespace +{ + modm::At24Mac402 eeprom{0x57}; +} + +void +I2cPlatformTest::setUp() +{ + I2c::connect(); + I2c::initialize(); +} + +void +I2cPlatformTest::testPing() +{ + // ping at wrong address + for (uint8_t address = 0x50; address <= 0x56; ++address) { + eeprom.setAddress(address); + const bool response = RF_CALL_BLOCKING(eeprom.ping()); + TEST_ASSERT_FALSE(response); + } + // set correct address 0x57 + eeprom.setAddress(0x57); + // ping at correct address + for (int i = 0; i < 20; ++i) { + const bool response = RF_CALL_BLOCKING(eeprom.ping()); + TEST_ASSERT_TRUE(response); + } +} + +void +I2cPlatformTest::testDataRead() +{ + std::array buffer{}; + + // read pre-programmed MAC address + + // read at wrong address + eeprom.setAddress(0x55); + bool readSuccess = RF_CALL_BLOCKING(eeprom.readMac(buffer)); + TEST_ASSERT_FALSE(readSuccess); + + // read at correct address + eeprom.setAddress(0x57); + readSuccess = RF_CALL_BLOCKING(eeprom.readMac(buffer)); + TEST_ASSERT_TRUE(readSuccess); + + TEST_ASSERT_EQUALS(buffer[0], 0xfc); + TEST_ASSERT_EQUALS(buffer[1], 0xc2); + TEST_ASSERT_EQUALS(buffer[2], 0x3d); +} diff --git a/test/modm/platform/i2c/samx7x/i2c_platform_test.hpp b/test/modm/platform/i2c/samx7x/i2c_platform_test.hpp new file mode 100644 index 0000000000..5c622d1517 --- /dev/null +++ b/test/modm/platform/i2c/samx7x/i2c_platform_test.hpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023, Christopher Durand + * + * 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 + +/// @ingroup modm_test_test_platform_i2c +class I2cPlatformTest : public unittest::TestSuite +{ +public: + void + setUp() override; + + void + testPing(); + + void + testDataRead(); +}; diff --git a/test/modm/platform/i2c/samx7x/module.lb b/test/modm/platform/i2c/samx7x/module.lb new file mode 100644 index 0000000000..f7da155f2b --- /dev/null +++ b/test/modm/platform/i2c/samx7x/module.lb @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright (c) 2023, Christopher Durand +# +# 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/. + + +def init(module): + module.name = ":test:platform:i2c" + module.description = "Tests for SAMx7x I2C" + +def prepare(module, options): + target = options[":target"] + + identifier = target.identifier + if identifier.platform != "sam" or identifier.family != "e7x/s7x/v7x": + return False + + module.depends(":platform:i2c:0") + module.depends(":driver:at24mac402") + return True + +def build(env): + if not env.has_module(":board:samv71-xplained-ultra"): + env.log.warn("The test requires an AT24MAC402 EEPROM to be connected to specific pins." + "Only the SAMV71 Xplained Ultra board is supported for now.") + return + + env.outbasepath = "modm-test/src/modm-test/platform/i2c_test" + env.copy("i2c_platform_test.hpp") + env.copy("i2c_platform_test.cpp")