aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/testhal/NRF52/NRF52832/I2C/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/testhal/NRF52/NRF52832/I2C/main.c')
-rw-r--r--lib/chibios-contrib/testhal/NRF52/NRF52832/I2C/main.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/NRF52/NRF52832/I2C/main.c b/lib/chibios-contrib/testhal/NRF52/NRF52832/I2C/main.c
new file mode 100644
index 000000000..15fa5e7b0
--- /dev/null
+++ b/lib/chibios-contrib/testhal/NRF52/NRF52832/I2C/main.c
@@ -0,0 +1,165 @@
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 */
38static THD_WORKING_AREA(PollEepromThreadWA, 1024);
39static THD_FUNCTION(PollEepromThread, arg) {
40
41 unsigned i;
42 uint8_t tx_data[6];
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] = EEPROM_START_ADDR;
53 tx_data[2] = 0xA0;
54 tx_data[3] = 0xA1;
55 tx_data[4] = 0xA2;
56 tx_data[5] = 0xA3;
57
58 while (true) {
59
60 /* write out initial data */
61 i2cAcquireBus(&I2CD1);
62 status = i2cMasterTransmitTimeout(&I2CD1, I2C_ADDR, tx_data, sizeof(tx_data), NULL, 0, TIME_INFINITE);
63 i2cReleaseBus(&I2CD1);
64 osalDbgCheck(MSG_OK == status);
65
66 /* read back inital data */
67 osalThreadSleepMilliseconds(2);
68 i2cAcquireBus(&I2CD1);
69 status = i2cMasterTransmitTimeout(&I2CD1, I2C_ADDR, tx_data, 2, rx_data, sizeof(rx_data), TIME_INFINITE);
70 i2cReleaseBus(&I2CD1);
71 osalDbgCheck(MSG_OK == status);
72
73 /* invert the data */
74 for (i = 2; i < sizeof(tx_data); i++)
75 tx_data[i] ^= 0xff;
76
77 /* write out inverted data */
78 osalThreadSleepMilliseconds(2);
79 i2cAcquireBus(&I2CD1);
80 status = i2cMasterTransmitTimeout(&I2CD1, I2C_ADDR, tx_data, sizeof(tx_data), NULL, 0, TIME_INFINITE);
81 i2cReleaseBus(&I2CD1);
82 osalDbgCheck(MSG_OK == status);
83
84 /* read back inverted data */
85 osalThreadSleepMilliseconds(2);
86 i2cAcquireBus(&I2CD1);
87 status = i2cMasterTransmitTimeout(&I2CD1, I2C_ADDR, tx_data, 2, rx_data, sizeof(rx_data), TIME_INFINITE);
88 i2cReleaseBus(&I2CD1);
89 osalDbgCheck(MSG_OK == status);
90
91 osalThreadSleepMilliseconds(TIME_INFINITE);
92 }
93}
94
95/*
96 * Fake polling thread.
97 */
98static THD_WORKING_AREA(PollFakeThreadWA, 256);
99static THD_FUNCTION(PollFakeThread, arg) {
100
101 (void)arg;
102
103 chRegSetThreadName("PollFake");
104 while (true) {
105
106 msg_t status;
107 uint8_t rx_data[2];
108 i2cflags_t errors;
109
110 i2cAcquireBus(&I2CD1);
111 status = i2cMasterReceiveTimeout(&I2CD1, I2C_FAKE_ADDR, rx_data, 2, TIME_MS2I(4));
112 i2cReleaseBus(&I2CD1);
113
114 if (status == MSG_RESET){
115 errors = i2cGetErrors(&I2CD1);
116 osalDbgCheck(I2C_ACK_FAILURE == errors);
117 }
118
119 palTogglePad(IOPORT1, LED1); /* on */
120 osalThreadSleepMilliseconds(1000);
121 }
122}
123
124/*
125 * I2C1 config.
126 */
127static const I2CConfig i2cfg = {
128 100000,
129 I2C_SCL,
130 I2C_SDA,
131};
132
133/*
134 * Entry point, note, the main() function is already a thread in the system
135 * on entry.
136 */
137int main(void) {
138
139 halInit();
140 chSysInit();
141
142 i2cStart(&I2CD1, &i2cfg);
143
144 /* Create EEPROM thread. */
145 chThdCreateStatic(PollEepromThreadWA,
146 sizeof(PollEepromThreadWA),
147 NORMALPRIO,
148 PollEepromThread,
149 NULL);
150
151 /* Create not responding thread. */
152 chThdCreateStatic(PollFakeThreadWA,
153 sizeof(PollFakeThreadWA),
154 NORMALPRIO,
155 PollFakeThread,
156 NULL);
157
158 /* main loop handles LED */
159 while (true) {
160 palTogglePad(IOPORT1, LED2); /* on */
161 osalThreadSleepMilliseconds(500);
162 }
163
164 return 0;
165}