|
| 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