-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c_sub.cpp
More file actions
258 lines (184 loc) · 4.54 KB
/
i2c_sub.cpp
File metadata and controls
258 lines (184 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "predef.h"
#include <ctype.h>
#include <basictypes.h>
#include <system.h>
#include <constants.h>
#include <ucos.h>
#include <ucosmcfc.h>
#include <serialirq.h>
#include <stdio.h>
#include <smarttrap.h>
#include <serialupdate.h>
//#include "i2cmulti.h" //Used for Multi-Master I2C
#include "i2cmaster.h" //Used for Master Only I2C
#include <string.h>
#include <Pins.h>
#include <sim.h>
#include <cfinter.h>
#include "i2c_sub.h"
#include "filereporter.h"
REPORT_FILE;
// Instruct the C++ compiler not to mangle the function name
extern "C"
{
void SetIntc( long func, int vector, int level, int prio );
}
volatile bool bNewCompass;
volatile COMPASS_READING Compass_Result;
volatile ALTIMETER_READING Alt_Result;
//volatile DWORD dwPressure;
//volatile DWORD dwTemperature;
OS_SEM Irq3_Sem;
INTERRUPT( irq_3, 0x2300 )
{
sim.eport.epfr |= 0x08; // Clear IRQ3 flag
OSSemPost(&Irq3_Sem);
}
void InitIrq3()
{
OSSemInit(&Irq3_Sem,0);
sim.gpio.pnqpar |= ( 8 );
sim.eport.eppar &= ~0x00C0; // Configure IRQ3 to trigger an falling edge
sim.eport.eppar |= 0x0080; // Configure IRQ3 to trigger an falling edge
sim.eport.epddr &= ~0x08; // Configure IRQ3 as input
sim.eport.epier = 0x08; // Enable IRQ3
SetIntc( ( long ) &irq_3, 3, 3, 7 );
}
#define HMC588_ADDR (0x1E)
#define MS5611_ADDR (0x77)
bool InitHMB588()
{
BYTE rv=0;
BYTE buffer[10];
buffer[0]=2;
buffer[1]=0;
rv=I2CSendBuf(HMC588_ADDR,buffer, 2, true);
InitIrq3();
if(rv==0) return true;
return false;
}
void ReadHMC588()
{
BYTE rv=0;
BYTE buffer[10];
buffer[0]=3;
rv=I2CSendBuf(HMC588_ADDR,buffer, 1, false);
//iprintf("Send RV=%02X\r\n",rv);
rv=I2CRestart(HMC588_ADDR,true,20);
//iprintf("Restart RV=%02X\r\n",rv);
rv=I2CReadBuf(HMC588_ADDR,buffer, 6, true);
iprintf("at %d RV=%02X %02X %02X %02X %02X %02X %02X\r\n",Secs,rv,buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5]);
}
bool InitMS5611()
{
BYTE rv=0;
BYTE buffer[2];
buffer[0]=0x1E;
buffer[1]=0;
rv=I2CSendBuf(MS5611_ADDR,buffer, 1, true);
if(rv==0) return true;
return false;
}
void ReadMS5611()
{
BYTE rv=0;
BYTE buffer[4];
buffer[0]=0x48; //Pressure
rv=I2CSendBuf(MS5611_ADDR,buffer, 1, true);
iprintf("Send RV=%02X\r\n",rv);
OSTimeDly(2);
buffer[0]=0x0;
rv=I2CSendBuf(MS5611_ADDR,buffer, 1, true);
iprintf("Send RV=%02X\r\n",rv);
rv=I2CReadBuf(MS5611_ADDR,buffer, 3, true);
iprintf("P At %d RV=%02X %02X %02X %02X \r\n",Secs,rv,buffer[0],buffer[1],buffer[2]);
buffer[0]=0x58; //Temperaute
rv=I2CSendBuf(MS5611_ADDR,buffer, 1, true);
iprintf("Send RV=%02X\r\n",rv);
OSTimeDly(2);
buffer[0]=0x0;
rv=I2CSendBuf(MS5611_ADDR,buffer, 1, true);
iprintf("Send RV=%02X\r\n",rv);
rv=I2CReadBuf(MS5611_ADDR,buffer, 3, true);
iprintf("T at %d RV=%02X %02X %02X %02X \r\n",Secs,rv,buffer[0],buffer[1],buffer[2]);
}
void HardWarePinsInit()
{
Pins[4].function( PIN4_SDA );
Pins[5].function( PIN5_SCL );
Pins[35].function( 0);
Pins[38].function( 0);
}
static OS_SEM * pDataSem;
void I2CTask( void * pd)
{
BYTE rv=0;
BYTE buffer[10];
bool bPressure;
COMPASS_READING * pCompass=(COMPASS_READING *)buffer;
static WORD ReadingNumber;
buffer[0]=0x48; //Pressure
rv=I2CSendBuf(MS5611_ADDR,buffer, 1, true);
OSTimeDly(2);
bPressure=true;
while(1)
{
OSSemPend(&Irq3_Sem,0);
buffer[0]=3;
rv=I2CSendBuf(HMC588_ADDR,buffer, 1, false);
rv=I2CRestart(HMC588_ADDR,true,20);
rv=I2CReadBuf(HMC588_ADDR,buffer, 6, true);
if(rv<=I2C_MASTER_OK )
{
pCompass->ReadingNum=ReadingNumber++;
OSLock();
memcpy((void *)&Compass_Result,pCompass,sizeof(COMPASS_READING));
OSUnlock();
bNewCompass=true;
}
buffer[0]=0x0;
rv=I2CSendBuf(MS5611_ADDR,buffer, 1, true);
rv=I2CReadBuf(MS5611_ADDR,buffer+1, 3, true);
buffer[0]=0;
if(bPressure)
{
Alt_Result.dwPressure=*(PDWORD)buffer;
buffer[0]=0x58; //Temp
bPressure=false;
}
else
{
Alt_Result.dwTemperature=*(PDWORD)buffer;
buffer[0]=0x48; //Pressure
bPressure=true;
}
rv=I2CSendBuf(MS5611_ADDR,buffer, 1, true);
if(pDataSem) OSSemPost(pDataSem);
}
}
#define SM_TASK_STK (512)
#define SmOSSimpleTaskCreate(x,p) { static DWORD func_##x_Stk[SM_TASK_STK] __attribute__( ( aligned( 4 ) ) ); OSTaskCreate(x,NULL,(void *)&func_##x_Stk[SM_TASK_STK],(void*)func_##x_Stk,p); }
void InitI2CSubSystem(WORD prio, OS_SEM * pSem)
{
pDataSem=pSem;
HardWarePinsInit();
Master_I2CInit();
HardWarePinsInit();
InitMS5611();
InitHMB588();
SmOSSimpleTaskCreate(I2CTask,prio);
}
void Testcompass()
{
HardWarePinsInit();
Master_I2CInit();
HardWarePinsInit();
InitHMB588();
InitMS5611();
while(1)
{
ReadMS5611();
// ReadHMC588();
OSTimeDly(2);
}
}