Skip to content

Commit d9b5a93

Browse files
Add Ice Tea I2C communication test
1 parent daefce6 commit d9b5a93

File tree

12 files changed

+1718
-0
lines changed

12 files changed

+1718
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "common.h"
19+
#include <stdio.h>
20+
#include "drivers/DigitalOut.h"
21+
22+
#if I2C_DEBUG_PIN_MASTER==1 || I2C_DEBUG_PIN_SLAVE==1
23+
24+
static mbed::DigitalOut test_pin(D8);
25+
26+
void test_pin_init()
27+
{
28+
test_pin.write(0);
29+
}
30+
31+
32+
void test_pin_toggle(int count)
33+
{
34+
int cv = test_pin.read();
35+
for (int i = 0; i < count; i++) {
36+
test_pin.write(cv == 0 ? 1 : 0);
37+
}
38+
test_pin.write(cv == 0 ? 0 : 1);
39+
}
40+
41+
42+
bool test_check(bool condition, const char *condition_str, const char *file, int line)
43+
{
44+
if (!condition) {
45+
printf("assertion failed: %s %s:%d\r\n", condition_str, file, line);
46+
return false;
47+
} else {
48+
return true;
49+
}
50+
}
51+
52+
#endif // I2C_DEBUG_PIN_MASTER==1 || I2C_DEBUG_PIN_SLAVE==1
53+
54+
bool test_check_message(bool condition, const char *condition_str, const char *message, const char *file, int line)
55+
{
56+
if (!condition) {
57+
printf("assertion failed: %s %s %s:%d\r\n", condition_str, message, file, line);
58+
return false;
59+
} else {
60+
return true;
61+
}
62+
}
63+
64+
bool test_check_equal_int(int32_t expected, int32_t actual, const char *file, int line)
65+
{
66+
if (expected != actual) {
67+
printf("assertion failed: %d != %d %s:%d\r\n", expected, actual, file, line);
68+
return false;
69+
} else {
70+
return true;
71+
}
72+
}
73+
74+
bool test_check_equal_uint(uint32_t expected, uint32_t actual, const char *file, int line)
75+
{
76+
if (expected != actual) {
77+
printf("assertion failed: %u != %u %s:%d\r\n", expected, actual, file, line);
78+
return false;
79+
} else {
80+
return true;
81+
}
82+
}
83+
84+
bool test_check_uint_within(uint32_t min, uint32_t max, uint32_t actual, const char *file, int line)
85+
{
86+
if (actual < min || actual > max) {
87+
printf("assertion failed: %u not in range (%u,%u) %s:%d\r\n", actual, min, max, file, line);
88+
return false;
89+
} else {
90+
return true;
91+
}
92+
}

TEST_APPS/device/i2c_com/common.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
20+
#ifndef I2C_TEST_COMMON_H
21+
#define I2C_TEST_COMMON_H
22+
23+
24+
// uncomment this in order to test i2c driver layer
25+
//#define TEST_I2C_DRIVER
26+
27+
#define I2C_DEBUG 0
28+
#define I2C_DEBUG_PIN_SLAVE 0
29+
#define I2C_DEBUG_PIN_MASTER 0
30+
31+
#define EMPTY_PARAM_INT (-1)
32+
33+
34+
#define MASTER_1_ID 111
35+
#define MASTER_2_ID 222
36+
37+
#define DEVICE_AS_MASTER false
38+
#define DEVICE_AS_SLAVE true
39+
40+
#define I2C_ADDRESS_MASK_7BIT (0x7F)
41+
#define I2C_ADDRESS_MASK_10BIT (0x3FF)
42+
#define I2C_10BIT_FIRST_BYTE (0xF0)
43+
44+
// 7bit addressing, allowed values from 0x8 to 0x77
45+
#define I2C_7BIT_ADDRESS_MIN (0x08)
46+
#define I2C_7BIT_ADDRESS_MAX (0x77)
47+
#define I2C_7BIT_ADDRESS (0x55)
48+
49+
#define MAKE_7BIT_SLAVE_ADDRESS(addr) ((I2C_ADDRESS_MASK_7BIT & addr) << 1)
50+
#define MAKE_7BIT_READ_ADDRESS(addr) (((I2C_ADDRESS_MASK_7BIT & addr) << 1) | 1)
51+
#define MAKE_7BIT_WRITE_ADDRESS(addr) ((I2C_ADDRESS_MASK_7BIT & addr) << 1)
52+
#define MAKE_10BIT_READ_ADDRESS(addr) ((((I2C_10BIT_FIRST_BYTE | (((addr & I2C_ADDRESS_MASK_10BIT)) >> 7)) | 1) << 8) | (addr & 0xFF))
53+
#define MAKE_10BIT_WRITE_ADDRESS(addr) ((((I2C_10BIT_FIRST_BYTE | (((addr & I2C_ADDRESS_MASK_10BIT)) >> 7)) & ~1) << 8) | (addr & 0xFF))
54+
55+
void test_pin_init();
56+
void test_pin_toggle(int count = 1);
57+
58+
#if I2C_DEBUG_PIN_SLAVE
59+
#define SLAVE_PIN_TOGGLE(n) test_pin_toggle(n)
60+
#else
61+
#define SLAVE_PIN_TOGGLE(n) (void)0
62+
#endif
63+
64+
#if I2C_DEBUG_PIN_MASTER
65+
#define MASTER_PIN_TOGGLE(n) test_pin_toggle(n)
66+
#else
67+
#define MASTER_PIN_TOGGLE(n) (void)0
68+
#endif
69+
70+
#if I2C_DEBUG
71+
#define I2C_DEBUG_PRINTF(...) printf(__VA_ARGS__)
72+
#else
73+
#define I2C_DEBUG_PRINTF(...) (void)0
74+
#endif
75+
76+
bool test_check(bool condition, const char *condition_str, const char *file, int line);
77+
bool test_check_message(bool condition, const char *condition_str, const char *message, const char *file, int line);
78+
bool test_check_equal_int(int32_t expected, int32_t actual, const char *file, int line);
79+
bool test_check_equal_uint(uint32_t expected, uint32_t actual, const char *file, int line);
80+
bool test_check_uint_within(uint32_t min, uint32_t max, uint32_t actual, const char *file, int line);
81+
82+
#define TEST_CHECK(condition) test_check(condition, #condition, __FILE__, __LINE__)
83+
#define TEST_CHECK_MESSAGE(condition, message) test_check_message(condition, #condition, message, __FILE__, __LINE__)
84+
#define TEST_CHECK_EQUAL_INT(expected, actual) test_check_equal_int(expected, actual, __FILE__, __LINE__)
85+
#define TEST_CHECK_EQUAL_UINT(expected, actual) test_check_equal_uint(expected, actual, __FILE__, __LINE__)
86+
#define TEST_CHECK_UINT_WITHIN(min, max, actual) test_check_uint_within(min, max, actual, __FILE__, __LINE__)
87+
88+
template<uint32_t SS>
89+
class WorkerThread : public Thread {
90+
unsigned char stack_mem[SS];
91+
public:
92+
WorkerThread(osPriority priority = osPriorityNormal) : Thread(priority, SS, stack_mem) { }
93+
};
94+
95+
#endif

0 commit comments

Comments
 (0)