Preliminary main mbed library for nexpaq development
libraries/tests/mbed/i2c_master/main.cpp@0:6c56fb4bc5f0, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:27:58 2016 +0000
- Revision:
- 0:6c56fb4bc5f0
Moving to library for sharing updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 2 | #include "test_env.h" |
nexpaq | 0:6c56fb4bc5f0 | 3 | |
nexpaq | 0:6c56fb4bc5f0 | 4 | #if !DEVICE_I2C |
nexpaq | 0:6c56fb4bc5f0 | 5 | #error [NOT_SUPPORTED] I2C is not supported |
nexpaq | 0:6c56fb4bc5f0 | 6 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 7 | |
nexpaq | 0:6c56fb4bc5f0 | 8 | #define SIZE (10) |
nexpaq | 0:6c56fb4bc5f0 | 9 | #define ADDR (0x90) |
nexpaq | 0:6c56fb4bc5f0 | 10 | |
nexpaq | 0:6c56fb4bc5f0 | 11 | #if defined(TARGET_KL25Z) |
nexpaq | 0:6c56fb4bc5f0 | 12 | I2C i2c(PTE0, PTE1); |
nexpaq | 0:6c56fb4bc5f0 | 13 | #elif defined(TARGET_nRF51822) |
nexpaq | 0:6c56fb4bc5f0 | 14 | I2C i2c(p22,p20); |
nexpaq | 0:6c56fb4bc5f0 | 15 | #elif defined(TARGET_FF_ARDUINO) || defined(TARGET_MAXWSNENV) |
nexpaq | 0:6c56fb4bc5f0 | 16 | I2C i2c(I2C_SDA, I2C_SCL); |
nexpaq | 0:6c56fb4bc5f0 | 17 | #elif defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32GG_STK3700) || defined(TARGET_EFM32WG_STK3800) |
nexpaq | 0:6c56fb4bc5f0 | 18 | #define TEST_SDA_PIN PD6 |
nexpaq | 0:6c56fb4bc5f0 | 19 | #define TEST_SCL_PIN PD7 |
nexpaq | 0:6c56fb4bc5f0 | 20 | I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); |
nexpaq | 0:6c56fb4bc5f0 | 21 | #elif defined(TARGET_EFM32ZG_STK3200) |
nexpaq | 0:6c56fb4bc5f0 | 22 | #define TEST_SDA_PIN PE12 |
nexpaq | 0:6c56fb4bc5f0 | 23 | #define TEST_SCL_PIN PE13 |
nexpaq | 0:6c56fb4bc5f0 | 24 | I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); |
nexpaq | 0:6c56fb4bc5f0 | 25 | #elif defined(TARGET_EFM32HG_STK3400) |
nexpaq | 0:6c56fb4bc5f0 | 26 | #define TEST_SDA_PIN PD6 |
nexpaq | 0:6c56fb4bc5f0 | 27 | #define TEST_SCL_PIN PD7 |
nexpaq | 0:6c56fb4bc5f0 | 28 | I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); |
nexpaq | 0:6c56fb4bc5f0 | 29 | #elif defined(TARGET_EFM32PG_STK3401) |
nexpaq | 0:6c56fb4bc5f0 | 30 | #define TEST_SDA_PIN PC10 |
nexpaq | 0:6c56fb4bc5f0 | 31 | #define TEST_SCL_PIN PC11 |
nexpaq | 0:6c56fb4bc5f0 | 32 | I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); |
nexpaq | 0:6c56fb4bc5f0 | 33 | #elif defined(TARGET_SAMR21G18A) |
nexpaq | 0:6c56fb4bc5f0 | 34 | #define TEST_SDA_PIN PA16 |
nexpaq | 0:6c56fb4bc5f0 | 35 | #define TEST_SCL_PIN PA17 |
nexpaq | 0:6c56fb4bc5f0 | 36 | I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); |
nexpaq | 0:6c56fb4bc5f0 | 37 | #elif defined(TARGET_SAMD21J18A) || defined(TARGET_SAMD21G18A) || defined(TARGET_SAML21J18A) |
nexpaq | 0:6c56fb4bc5f0 | 38 | #define TEST_SDA_PIN PA08 |
nexpaq | 0:6c56fb4bc5f0 | 39 | #define TEST_SCL_PIN PA09 |
nexpaq | 0:6c56fb4bc5f0 | 40 | I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); |
nexpaq | 0:6c56fb4bc5f0 | 41 | #elif defined(TARGET_SAMG55J19) |
nexpaq | 0:6c56fb4bc5f0 | 42 | #define TEST_SDA_PIN PA10 |
nexpaq | 0:6c56fb4bc5f0 | 43 | #define TEST_SCL_PIN PA09 |
nexpaq | 0:6c56fb4bc5f0 | 44 | I2C i2c(TEST_SDA_PIN, TEST_SCL_PIN); |
nexpaq | 0:6c56fb4bc5f0 | 45 | #else |
nexpaq | 0:6c56fb4bc5f0 | 46 | I2C i2c(p28, p27); |
nexpaq | 0:6c56fb4bc5f0 | 47 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 48 | |
nexpaq | 0:6c56fb4bc5f0 | 49 | int main() { |
nexpaq | 0:6c56fb4bc5f0 | 50 | bool success = true; |
nexpaq | 0:6c56fb4bc5f0 | 51 | char buf[] = {3, 2, 1, 4, 5, 6, 7, 8, 9, 10}; |
nexpaq | 0:6c56fb4bc5f0 | 52 | char res[SIZE]; |
nexpaq | 0:6c56fb4bc5f0 | 53 | |
nexpaq | 0:6c56fb4bc5f0 | 54 | i2c.write(ADDR, buf, SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 55 | i2c.read(ADDR, res, SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 56 | |
nexpaq | 0:6c56fb4bc5f0 | 57 | // here should be buf[all]++ |
nexpaq | 0:6c56fb4bc5f0 | 58 | i2c.write(ADDR, res, SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 59 | i2c.read(ADDR, res, SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 60 | |
nexpaq | 0:6c56fb4bc5f0 | 61 | // here should be buf[all]+=2 |
nexpaq | 0:6c56fb4bc5f0 | 62 | i2c.write(ADDR, res, SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 63 | i2c.write(ADDR, res, SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 64 | |
nexpaq | 0:6c56fb4bc5f0 | 65 | // here should be buf[all]+=3 |
nexpaq | 0:6c56fb4bc5f0 | 66 | i2c.read(ADDR, res, SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 67 | i2c.read(ADDR, res, SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 68 | |
nexpaq | 0:6c56fb4bc5f0 | 69 | for(int i = 0; i < SIZE; i++) { |
nexpaq | 0:6c56fb4bc5f0 | 70 | if (res[i] != (buf[i] + 3)) { |
nexpaq | 0:6c56fb4bc5f0 | 71 | success = false; |
nexpaq | 0:6c56fb4bc5f0 | 72 | break; |
nexpaq | 0:6c56fb4bc5f0 | 73 | } |
nexpaq | 0:6c56fb4bc5f0 | 74 | } |
nexpaq | 0:6c56fb4bc5f0 | 75 | |
nexpaq | 0:6c56fb4bc5f0 | 76 | notify_completion(success); |
nexpaq | 0:6c56fb4bc5f0 | 77 | } |