diff options
Diffstat (limited to 'lib/chibios-contrib/testhal/NRF51/NRF51822/I2C/main.c')
-rw-r--r-- | lib/chibios-contrib/testhal/NRF51/NRF51822/I2C/main.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/NRF51/NRF51822/I2C/main.c b/lib/chibios-contrib/testhal/NRF51/NRF51822/I2C/main.c new file mode 100644 index 000000000..d0c437288 --- /dev/null +++ b/lib/chibios-contrib/testhal/NRF51/NRF51822/I2C/main.c | |||
@@ -0,0 +1,164 @@ | |||
1 | /* | ||
2 | Copyright (C) 2015 Stephen Caudle | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | /* | ||
18 | This demo: | ||
19 | 1) Writes bytes to the EEPROM | ||
20 | 2) Reads the same bytes back | ||
21 | 3) Inverts the byte values | ||
22 | 4) Writes them | ||
23 | 5) Reads them back | ||
24 | */ | ||
25 | |||
26 | #include <stdlib.h> | ||
27 | |||
28 | #include "ch.h" | ||
29 | #include "hal.h" | ||
30 | |||
31 | #define I2C_ADDR 0x50 | ||
32 | #define I2C_FAKE_ADDR 0x4C | ||
33 | #define EEPROM_START_ADDR 0x00 | ||
34 | |||
35 | /* | ||
36 | * EEPROM thread. | ||
37 | */ | ||
38 | static THD_WORKING_AREA(PollEepromThreadWA, 1024); | ||
39 | static THD_FUNCTION(PollEepromThread, arg) { | ||
40 | |||
41 | unsigned i; | ||
42 | uint8_t tx_data[5]; | ||
43 | uint8_t rx_data[4]; | ||
44 | msg_t status; | ||
45 | |||
46 | (void)arg; | ||
47 | |||
48 | chRegSetThreadName("PollEeprom"); | ||
49 | |||
50 | /* set initial data to write */ | ||
51 | tx_data[0] = EEPROM_START_ADDR; | ||
52 | tx_data[1] = 0xA0; | ||
53 | tx_data[2] = 0xA1; | ||
54 | tx_data[3] = 0xA2; | ||
55 | tx_data[4] = 0xA3; | ||
56 | |||
57 | while (true) { | ||
58 | |||
59 | /* write out initial data */ | ||
60 | i2cAcquireBus(&I2CD1); | ||
61 | status = i2cMasterTransmitTimeout(&I2CD1, I2C_ADDR, tx_data, sizeof(tx_data), NULL, 0, TIME_INFINITE); | ||
62 | i2cReleaseBus(&I2CD1); | ||
63 | osalDbgCheck(MSG_OK == status); | ||
64 | |||
65 | /* read back inital data */ | ||
66 | osalThreadSleepMilliseconds(2); | ||
67 | i2cAcquireBus(&I2CD1); | ||
68 | status = i2cMasterTransmitTimeout(&I2CD1, I2C_ADDR, tx_data, 1, rx_data, sizeof(rx_data), TIME_INFINITE); | ||
69 | i2cReleaseBus(&I2CD1); | ||
70 | osalDbgCheck(MSG_OK == status); | ||
71 | |||
72 | /* invert the data */ | ||
73 | for (i = 1; i < sizeof(tx_data); i++) | ||
74 | tx_data[i] ^= 0xff; | ||
75 | |||
76 | /* write out inverted data */ | ||
77 | osalThreadSleepMilliseconds(2); | ||
78 | i2cAcquireBus(&I2CD1); | ||
79 | status = i2cMasterTransmitTimeout(&I2CD1, I2C_ADDR, tx_data, sizeof(tx_data), NULL, 0, TIME_INFINITE); | ||
80 | i2cReleaseBus(&I2CD1); | ||
81 | osalDbgCheck(MSG_OK == status); | ||
82 | |||
83 | /* read back inverted data */ | ||
84 | osalThreadSleepMilliseconds(2); | ||
85 | i2cAcquireBus(&I2CD1); | ||
86 | status = i2cMasterTransmitTimeout(&I2CD1, I2C_ADDR, tx_data, 1, rx_data, sizeof(rx_data), TIME_INFINITE); | ||
87 | i2cReleaseBus(&I2CD1); | ||
88 | osalDbgCheck(MSG_OK == status); | ||
89 | |||
90 | osalThreadSleepMilliseconds(TIME_INFINITE); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | /* | ||
95 | * Fake polling thread. | ||
96 | */ | ||
97 | static THD_WORKING_AREA(PollFakeThreadWA, 256); | ||
98 | static THD_FUNCTION(PollFakeThread, arg) { | ||
99 | |||
100 | (void)arg; | ||
101 | |||
102 | chRegSetThreadName("PollFake"); | ||
103 | while (true) { | ||
104 | |||
105 | msg_t status; | ||
106 | uint8_t rx_data[2]; | ||
107 | i2cflags_t errors; | ||
108 | |||
109 | i2cAcquireBus(&I2CD1); | ||
110 | status = i2cMasterReceiveTimeout(&I2CD1, I2C_FAKE_ADDR, rx_data, 2, TIME_MS2I(4)); | ||
111 | i2cReleaseBus(&I2CD1); | ||
112 | |||
113 | if (status == MSG_RESET){ | ||
114 | errors = i2cGetErrors(&I2CD1); | ||
115 | osalDbgCheck(I2C_ACK_FAILURE == errors); | ||
116 | } | ||
117 | |||
118 | palTogglePad(IOPORT1, LED1); /* on */ | ||
119 | osalThreadSleepMilliseconds(1000); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | /* | ||
124 | * I2C1 config. | ||
125 | */ | ||
126 | static const I2CConfig i2cfg = { | ||
127 | 400000, | ||
128 | I2C_SCL, | ||
129 | I2C_SDA, | ||
130 | }; | ||
131 | |||
132 | /* | ||
133 | * Entry point, note, the main() function is already a thread in the system | ||
134 | * on entry. | ||
135 | */ | ||
136 | int main(void) { | ||
137 | |||
138 | halInit(); | ||
139 | chSysInit(); | ||
140 | |||
141 | i2cStart(&I2CD1, &i2cfg); | ||
142 | |||
143 | /* Create EEPROM thread. */ | ||
144 | chThdCreateStatic(PollEepromThreadWA, | ||
145 | sizeof(PollEepromThreadWA), | ||
146 | NORMALPRIO, | ||
147 | PollEepromThread, | ||
148 | NULL); | ||
149 | |||
150 | /* Create not responding thread. */ | ||
151 | chThdCreateStatic(PollFakeThreadWA, | ||
152 | sizeof(PollFakeThreadWA), | ||
153 | NORMALPRIO, | ||
154 | PollFakeThread, | ||
155 | NULL); | ||
156 | |||
157 | /* main loop handles LED */ | ||
158 | while (true) { | ||
159 | palTogglePad(IOPORT1, LED0); /* on */ | ||
160 | osalThreadSleepMilliseconds(500); | ||
161 | } | ||
162 | |||
163 | return 0; | ||
164 | } | ||