libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
i2c.test.h
Go to the documentation of this file.
1 // Copyright 2013 SRLM and Red9
2 #include <propeller.h>
3 #include "unity.h"
4 #include "libpropeller/i2c/i2c.h"
5 
6 const int kSDAPin = 1;
7 const int kSCLPin = 0;
8 
9 
10 //static const unsigned char EEPROM = 0b10100000;
11 static const unsigned char Accl = 0b00110010;
12 static const unsigned char GYRO = 0b11010110;
13 static const unsigned char Magn = 0b00111100;
14 static const unsigned char BARO = 0b11101110;
15 static const unsigned char Fuel = 0b01101100;
16 
18 
20 int randomAddress = 0;
21 
22 class UnityTests {
23 public:
24 
25  static int RandomAddress() {
26  registerAddress = registerAddress * 1103515245 + 12345;
27  registerAddress = (registerAddress & 0xFFFF0000) & 0x8FFF; //Note: keep this & 0x8FFF so that it's within 32kB address range
28  return registerAddress;
29  }
30 
31  static void setUp(void) {
32  randomAddress = RandomAddress();
33  sut.Init(kSCLPin, kSDAPin);
34  }
35 
36  static void tearDown(void) {
37 
38  }
39 
40  // -----------------------------------------------------------------------------
41  // "Normal I2C"
42  // -----------------------------------------------------------------------------
43 
44  static void test_L3GD20Ping(void) {
45  TEST_ASSERT_TRUE(sut.Ping(GYRO));
46  }
47 
48  static void test_PingNonExistentDevice(void) {
49  TEST_ASSERT_FALSE(sut.Ping(0x38));
50  }
51 
52  static void test_L3GD20ReadWhoAmIRegister(void) {
53  TEST_ASSERT_EQUAL_INT(0b11010100, sut.Get(GYRO, 0b00001111));
54  }
55 
56  static void test_L3GD20WriteCtrlReg1(void) {
57  //Test twice so that we *know* that it actually wrote (instead of from a previous test)
58  TEST_ASSERT_TRUE(sut.Put(GYRO, 0x20, 0b01111111));
59  TEST_ASSERT_EQUAL_HEX8(0b01111111, sut.Get(GYRO, 0x20));
60 
61  TEST_ASSERT_TRUE(sut.Put(GYRO, 0x20, 0xFF));
62  TEST_ASSERT_EQUAL_HEX8(0xFF, sut.Get(GYRO, 0x20));
63  }
64 
65  static void test_L3GD20WriteMultipleBytes(void) {
66  //Note that the address (SUB) is bitwise OR'd with 0x80 (set the MSb high) in
67  // order to indicate to the slave device to auto increment the address.
68  unsigned char data_address = 0x32 | 0x80; //Interupt threshold registers
69  char indata1[] = {0x0F, 0xFA, 0x0E, 0x80, 0x01, 0x22};
70  char indata2[] = {0x0E, 0xF9, 0x0D, 0x7F, 0x00, 0x21};
71  int data_size = 6;
72  char outdata[data_size];
73 
74  //First Write
75  TEST_ASSERT_TRUE(sut.Put(GYRO, data_address, indata1, data_size));
76  sut.Get(GYRO, data_address, outdata, data_size);
77  TEST_ASSERT_EQUAL_MEMORY(indata1, outdata, data_size);
78 
79  //Second Write
80  TEST_ASSERT_TRUE(sut.Put(GYRO, data_address, indata2, data_size));
81  sut.Get(GYRO, data_address, outdata, data_size);
82  TEST_ASSERT_EQUAL_MEMORY(indata2, outdata, data_size);
83 
84  }
85 
86  static void test_L3GD20WriteMultipleBytesButOnlyOne(void) {
87  //Note that the address (SUB) is bitwise OR'd with 0x80 (set the MSb high) in
88  // order to indicate to the slave device to auto increment the address.
89  //Test using the multiple byte reads for just one byte
90  unsigned char data_address = 0x32 | 0x80;
91  char indata1[] = {0x0F};
92  char indata2[] = {0x0E};
93  int data_size = 1;
94  char outdata[data_size];
95 
96 
97  //First Write
98  TEST_ASSERT_TRUE(sut.Put(GYRO, data_address, indata1, data_size));
99  sut.Get(GYRO, data_address, outdata, data_size);
100  TEST_ASSERT_EQUAL_MEMORY(indata1, outdata, data_size);
101 
102  //Second Write
103  TEST_ASSERT_TRUE(sut.Put(GYRO, data_address, indata2, data_size));
104  sut.Get(GYRO, data_address, outdata, data_size);
105  TEST_ASSERT_EQUAL_MEMORY(indata2, outdata, data_size);
106  }
107 
108  // -----------------------------------------------------------------------------
109  // MS5611
110  // -----------------------------------------------------------------------------
111 
112  static void test_MS5611PutSingleByteGetMultipleBytes(void) {
113  TEST_IGNORE_MESSAGE("Must confirm function with Logic16");
114  //Command read memory address 011
115  TEST_ASSERT_TRUE(sut.Put(BARO, 0b10100110));
116 
117  int data_size = 2;
118  char indata1[data_size];
119  TEST_ASSERT_TRUE(sut.Get(BARO, indata1, data_size));
120  }
121 
122 
123 
124 
125 };