Skip to content

Commit

Permalink
[test] Add SAMV71 I2C unit test for Xplained Ultra board
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durand committed Apr 6, 2023
1 parent 0689b05 commit d1938eb
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
71 changes: 71 additions & 0 deletions test/modm/platform/i2c/samx7x/i2c_platform_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <modm/platform.hpp>
#include <modm/board.hpp>
#include <modm/driver/storage/at24mac402.hpp>

using namespace modm::platform;
using namespace Board;

namespace
{
modm::At24Mac402<I2c> eeprom{0x57};
}

void
I2cPlatformTest::setUp()
{
I2c::connect<Sda::Twd, Scl::Twck>();
I2c::initialize<SystemClock, 400_kHz>();
}

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<uint8_t, 6> 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);
}
26 changes: 26 additions & 0 deletions test/modm/platform/i2c/samx7x/i2c_platform_test.hpp
Original file line number Diff line number Diff line change
@@ -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 <unittest/testsuite.hpp>

/// @ingroup modm_test_test_platform_i2c
class I2cPlatformTest : public unittest::TestSuite
{
public:
void
setUp() override;

void
testPing();

void
testDataRead();
};
36 changes: 36 additions & 0 deletions test/modm/platform/i2c/samx7x/module.lb
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit d1938eb

Please sign in to comment.