Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit 9c5dd1c

Browse files
authored
Merge pull request #456 from doingnz/OneWire
Change OneWire API to use a logical port number
2 parents 475e2b7 + 3543b56 commit 9c5dd1c

12 files changed

+104
-56
lines changed

CLR/Libraries/SPOT_Hardware/SPOT_OneWire/spot_hardware_onewire_native.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
struct Library_spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire
1010
{
1111
static const int FIELD___pin = 1;
12-
12+
static const int FIELD___logicalPort = 2;
1313
TINYCLR_NATIVE_DECLARE(TouchReset___I4);
1414
TINYCLR_NATIVE_DECLARE(TouchBit___I4__I4);
1515
TINYCLR_NATIVE_DECLARE(TouchByte___I4__I4);

CLR/Libraries/SPOT_Hardware/SPOT_OneWire/spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,81 @@ using namespace Microsoft::SPOT::Hardware;
1111
INT32 OneWire::TouchReset( CLR_RT_HeapBlock* pMngObj, HRESULT &hr )
1212
{
1313
INT32 retVal = 0;
14-
UINT32 pin = Get__pin(pMngObj);
15-
retVal = owTouchReset(pin);
14+
UINT32 pin = Get__logicalPort(pMngObj);
15+
retVal = owTouchReset(pin);
1616
return retVal;
1717
}
1818

1919
INT32 OneWire::TouchBit( CLR_RT_HeapBlock* pMngObj, INT32 param0, HRESULT &hr )
2020
{
2121
INT32 retVal = 0;
22-
UINT32 pin = Get__pin(pMngObj);
23-
retVal = owTouchBit(pin, param0);
22+
UINT32 pin = Get__logicalPort(pMngObj);
23+
retVal = owTouchBit(pin, param0);
2424
return retVal;
2525
}
2626

2727
INT32 OneWire::TouchByte( CLR_RT_HeapBlock* pMngObj, INT32 param0, HRESULT &hr )
2828
{
2929
INT32 retVal = 0;
30-
UINT32 pin = Get__pin(pMngObj);
31-
retVal = owTouchByte(pin, param0);
30+
UINT32 pin = Get__logicalPort(pMngObj);
31+
retVal = owTouchByte(pin, param0);
3232
return retVal;
3333
}
3434

3535
INT32 OneWire::WriteByte( CLR_RT_HeapBlock* pMngObj, INT32 param0, HRESULT &hr )
3636
{
3737
INT32 retVal = 0;
38-
UINT32 pin = Get__pin(pMngObj);
39-
retVal = owWriteByte(pin, param0);
38+
UINT32 pin = Get__logicalPort(pMngObj);
39+
retVal = owWriteByte(pin, param0);
4040
return retVal;
4141
}
4242

4343
INT32 OneWire::ReadByte( CLR_RT_HeapBlock* pMngObj, HRESULT &hr )
4444
{
4545
INT32 retVal = 0;
46-
UINT32 pin = Get__pin(pMngObj);
47-
retVal = owReadByte(pin);
46+
UINT32 pin = Get__logicalPort(pMngObj);
47+
retVal = owReadByte(pin);
4848
return retVal;
4949
}
5050

5151
INT32 OneWire::AcquireEx( CLR_RT_HeapBlock* pMngObj, HRESULT &hr )
5252
{
5353
INT32 retVal = 0;
54-
UINT32 pin = Get__pin(pMngObj);
55-
retVal = owAcquire(pin, "\\\\OneWire\\");
54+
UINT32 pin = Get__pin(pMngObj);
55+
UINT32 logicalPort = Get__logicalPort(pMngObj);
56+
retVal = owAcquire(logicalPort, pin);
5657
return retVal;
5758
}
5859

5960
INT32 OneWire::Release( CLR_RT_HeapBlock* pMngObj, HRESULT &hr )
6061
{
6162
INT32 retVal = 0;
62-
UINT32 pin = Get__pin(pMngObj);
63-
owRelease(pin);
63+
UINT32 pin = Get__logicalPort(pMngObj);
64+
owRelease(pin);
6465
return retVal;
6566
}
6667

6768
INT32 OneWire::First( CLR_RT_HeapBlock* pMngObj, INT8 param0, INT8 param1, HRESULT &hr )
6869
{
6970
INT32 retVal = 0;
70-
UINT32 pin = Get__pin(pMngObj);
71-
retVal = owFirst(pin, param0, param1);
71+
UINT32 pin = Get__logicalPort(pMngObj);
72+
retVal = owFirst(pin, param0, param1);
7273
return retVal;
7374
}
7475

7576
INT32 OneWire::Next( CLR_RT_HeapBlock* pMngObj, INT8 param0, INT8 param1, HRESULT &hr )
7677
{
7778
INT32 retVal = 0;
78-
UINT32 pin = Get__pin(pMngObj);
79-
retVal = owNext(pin, param0, param1);
79+
UINT32 pin = Get__logicalPort(pMngObj);
80+
retVal = owNext(pin, param0, param1);
8081
return retVal;
8182
}
8283

8384
INT32 OneWire::SerialNum( CLR_RT_HeapBlock* pMngObj, CLR_RT_TypedArray_UINT8 param0, INT8 param1, HRESULT &hr )
8485
{
8586
INT32 retVal = 0;
86-
UINT32 pin = Get__pin(pMngObj);
87-
owSerialNum(pin, param0.GetBuffer(), param1);
87+
UINT32 pin = Get__logicalPort(pMngObj);
88+
owSerialNum(pin, param0.GetBuffer(), param1);
8889
return retVal;
8990
}
9091

CLR/Libraries/SPOT_Hardware/SPOT_OneWire/spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace Microsoft
1414
struct OneWire
1515
{
1616
// Helper Functions to access fields of managed object
17-
static UINT32& Get__pin( CLR_RT_HeapBlock* pMngObj ) { return Interop_Marshal_GetField_UINT32( pMngObj, Library_spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire::FIELD___pin ); }
17+
static UINT32& Get__pin( CLR_RT_HeapBlock* pMngObj ) { return Interop_Marshal_GetField_UINT32( pMngObj, Library_spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire::FIELD___pin ); }
18+
static UINT32& Get__logicalPort( CLR_RT_HeapBlock* pMngObj ) { return Interop_Marshal_GetField_UINT32( pMngObj, Library_spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire::FIELD___logicalPort ); }
1819

1920
// Declaration of stubs. These functions are implemented by Interop code developers
2021
static INT32 TouchReset( CLR_RT_HeapBlock* pMngObj, HRESULT &hr );

DeviceCode/pal/OneWire/DallasSemi/crcutil.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static uchar dscrc_table[] = {
5959
//
6060
void setcrc16(int portnum, ushort reset)
6161
{
62-
utilcrc16[portnum&0x0FF] = reset;
62+
if((portnum < MAX_PORTNUM) && (portnum >= 0) ) utilcrc16[portnum&0x0FF] = reset;
6363
return;
6464
}
6565

@@ -72,7 +72,7 @@ void setcrc16(int portnum, ushort reset)
7272
//
7373
void setcrc8(int portnum, uchar reset)
7474
{
75-
utilcrc8[portnum&0x0FF] = reset;
75+
if((portnum < MAX_PORTNUM) && (portnum >= 0) ) utilcrc8[portnum&0x0FF] = reset;
7676
return;
7777
}
7878

@@ -88,6 +88,8 @@ void setcrc8(int portnum, uchar reset)
8888
//
8989
ushort docrc16(int portnum, ushort cdata)
9090
{
91+
if((portnum >= MAX_PORTNUM) || (portnum < 0)) return 0;
92+
9193
cdata = (cdata ^ (utilcrc16[portnum&0x0FF] & 0xff)) & 0xff;
9294
utilcrc16[portnum&0x0FF] >>= 8;
9395

@@ -114,6 +116,8 @@ ushort docrc16(int portnum, ushort cdata)
114116
//
115117
uchar docrc8(int portnum, uchar x)
116118
{
119+
if((portnum >= MAX_PORTNUM) || (portnum < 0)) return 0;
120+
117121
utilcrc8[portnum&0x0FF] = dscrc_table[utilcrc8[portnum&0x0FF] ^ x];
118122
return utilcrc8[portnum&0x0FF];
119123
}

DeviceCode/pal/OneWire/DallasSemi/ownet.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@
4545
SMALLINT bitacc(SMALLINT,SMALLINT,SMALLINT,uchar *);
4646

4747
// global variables for this module to hold search state information
48-
static SMALLINT LastDiscrepancy[MAX_PORTNUM];
49-
static SMALLINT LastFamilyDiscrepancy[MAX_PORTNUM];
50-
static SMALLINT LastDevice[MAX_PORTNUM];
51-
uchar SerialNum[MAX_PORTNUM][8];
48+
static SMALLINT LastDiscrepancy[MAX_PORTNUM] = { 0 };
49+
static SMALLINT LastFamilyDiscrepancy[MAX_PORTNUM] = { 0 };
50+
static SMALLINT LastDevice[MAX_PORTNUM] = { 0 };
51+
SMALLINT owPortPin[MAX_PORTNUM] = { 0 };
52+
uchar SerialNum[MAX_PORTNUM][8] = { 0 };
5253

5354
//--------------------------------------------------------------------------
5455
// The 'owFirst' finds the first device on the 1-Wire Net This function
@@ -109,6 +110,8 @@ SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only)
109110
uchar serial_byte_mask;
110111
uchar lastcrc8;
111112

113+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;
114+
112115
// initialize for search
113116
lastcrc8=0;
114117
bit_number = 1;
@@ -262,7 +265,11 @@ SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only)
262265
void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read)
263266
{
264267
uchar i;
265-
268+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0))
269+
{
270+
if (serialnum_buf != NULL) *serialnum_buf=0;
271+
return;
272+
}
266273
// read the internal buffer and place in 'serialnum_buf'
267274
if (do_read)
268275
{
@@ -289,6 +296,8 @@ void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read)
289296
void owFamilySearchSetup(int portnum, SMALLINT search_family)
290297
{
291298
uchar i;
299+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return;
300+
292301

293302
// set the search state to find SearchFamily type devices
294303
SerialNum[portnum][0] = search_family;
@@ -306,6 +315,8 @@ void owFamilySearchSetup(int portnum, SMALLINT search_family)
306315
//
307316
void owSkipFamily(int portnum)
308317
{
318+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return;
319+
309320
// set the Last discrepancy to last family discrepancy
310321
LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum];
311322
LastFamilyDiscrepancy[portnum] = 0;
@@ -333,6 +344,7 @@ SMALLINT owAccess(int portnum)
333344
{
334345
uchar sendpacket[9];
335346
uchar i;
347+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return FALSE;
336348

337349
// reset the 1-wire
338350
if (owTouchReset(portnum))
@@ -396,7 +408,7 @@ SMALLINT owVerify(int portnum, SMALLINT alarm_only)
396408
{
397409
uchar i,sendlen=0,goodbits=0,cnt=0,s,tst;
398410
uchar sendpacket[50];
399-
411+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return FALSE;
400412
// construct the search
401413
if (alarm_only)
402414
sendpacket[sendlen++] = 0xEC; // issue the alarming search command
@@ -460,7 +472,7 @@ SMALLINT owOverdriveAccess(int portnum)
460472
{
461473
uchar sendpacket[8];
462474
uchar i, bad_echo = FALSE;
463-
475+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return FALSE;
464476
// make sure normal level
465477
owLevel(portnum,MODE_NORMAL);
466478

DeviceCode/pal/OneWire/DallasSemi/ownet.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ SMALLINT owVerify(int portnum, SMALLINT alarm_only);
375375
SMALLINT owOverdriveAccess(int portnum);
376376

377377

378-
// external One Wire functions defined in owsesu.c
379-
SMALLINT owAcquire(int portnum, char *port_zstr);
380-
int owAcquireEx(char *port_zstr);
381-
void owRelease(int portnum);
378+
// external One Wire functions defined in OneWireLinkLevelSession.cpp
379+
SMALLINT owAcquire(int portnum, int pin);
380+
//int owAcquireEx(char *port_zstr);
381+
void owRelease(int portnum);
382382

383383
// external One Wire functions defined in findtype.c
384384
// SMALLINT FindDevices(int,uchar FamilySN[][8],SMALLINT,int);
@@ -433,4 +433,6 @@ ushort docrc16(int portnum, ushort cdata);
433433
void setcrc8(int portnum, uchar reset);
434434
uchar docrc8(int portnum, uchar x);
435435

436+
extern SMALLINT owPortPin[MAX_PORTNUM];
437+
436438
#endif //OWNET_H

DeviceCode/pal/OneWire/OneWireLinkLevelDriver.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ static SMALLINT ULevel; // current 1-Wire Net level
5656
SMALLINT owTouchReset(int portnum)
5757
{
5858
uchar result;
59-
UINT32 pin = (UINT32)portnum;
59+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;
60+
61+
UINT32 pin = (UINT32)owPortPin[portnum];
6062

6163
// Code from appnote 126.
6264
CPU_GPIO_EnableOutputPin( pin, false ); // impulse start OW_PORT = 0; // drive bus low.
@@ -94,7 +96,10 @@ SMALLINT owTouchReset(int portnum)
9496
SMALLINT owTouchBit(int portnum, SMALLINT sendbit)
9597
{
9698
unsigned char result=0;
97-
UINT32 pin = (UINT32)portnum;
99+
100+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0) ) return 0;
101+
102+
UINT32 pin = (UINT32)owPortPin[portnum];
98103

99104
//timing critical, so I'll disable interrupts here
100105
GLOBAL_LOCK(irq); //EA = 0;
@@ -156,6 +161,8 @@ SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
156161
uchar i;
157162
uchar result = 0;
158163

164+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;
165+
159166
for (i = 0; i < 8; i++)
160167
{
161168
result |= (owTouchBit(portnum,sendbyte & 1) << i);
@@ -179,6 +186,8 @@ SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
179186
//
180187
SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
181188
{
189+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;
190+
182191
return (owTouchByte(portnum,sendbyte) == sendbyte) ? TRUE : FALSE;
183192
}
184193

@@ -193,6 +202,7 @@ SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
193202
//
194203
SMALLINT owReadByte(int portnum)
195204
{
205+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;
196206
return owTouchByte(portnum,0xFF);
197207
}
198208

@@ -209,7 +219,7 @@ SMALLINT owReadByte(int portnum)
209219
//
210220
SMALLINT owSpeed(int portnum, SMALLINT new_speed)
211221
{
212-
portnum = 0;
222+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;
213223

214224
USpeed = new_speed;
215225
// not supported yet
@@ -236,7 +246,7 @@ SMALLINT owSpeed(int portnum, SMALLINT new_speed)
236246
// Note: Strong and Program not supported on 520 target.
237247
SMALLINT owLevel(int portnum, SMALLINT new_level)
238248
{
239-
portnum = 0;
249+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;
240250

241251
UINT32 pin = (UINT32)portnum;
242252

@@ -268,7 +278,7 @@ SMALLINT owLevel(int portnum, SMALLINT new_level)
268278
//
269279
SMALLINT owProgramPulse(int portnum)
270280
{
271-
portnum = 0;
281+
//portnum = 0;
272282
// Not supported
273283
return 0;
274284
}

DeviceCode/pal/OneWire/OneWireLinkLevelSession.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
//extern void usDelay(int);
3939

4040
// local function prototypes
41-
SMALLINT owAcquire(int,char *);
42-
void owRelease(int);
41+
//SMALLINT owAcquire(int,int);
42+
//void owRelease(int);
4343

4444
//---------------------------------------------------------------------------
4545
// Attempt to acquire a 1-Wire net
@@ -50,12 +50,12 @@ void owRelease(int);
5050
//
5151
// Returns: TRUE - success, port opened
5252
//
53-
SMALLINT owAcquire(int portnum, char *port_zstr)
53+
SMALLINT owAcquire(int portnum, int pin)
5454
{
55-
port_zstr = 0;
55+
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] != 0)) return FALSE; // already in use
5656

5757
//portnum = 0;
58-
UINT32 pin = (UINT32)portnum;
58+
//UINT32 pin = (UINT32)atoi(port_zstr);
5959

6060
// drive bus high.
6161
CPU_GPIO_EnableOutputPin( pin, true );
@@ -65,9 +65,17 @@ SMALLINT owAcquire(int portnum, char *port_zstr)
6565

6666
// checks to make sure the line is idling high.
6767
CPU_GPIO_EnableInputPin( pin, false, NULL, GPIO_INT_EDGE_HIGH, RESISTOR_PULLUP );
68-
return (CPU_GPIO_GetPinState(pin) ==1 ? TRUE : FALSE);
68+
69+
if (CPU_GPIO_GetPinState(pin)==1)
70+
{
71+
owPortPin[portnum] = pin;
72+
return TRUE;
73+
}
74+
else
75+
{
76+
return FALSE;
77+
}
6978
}
70-
7179
//---------------------------------------------------------------------------
7280
// Release the previously acquired a 1-Wire net.
7381
//
@@ -76,7 +84,14 @@ SMALLINT owAcquire(int portnum, char *port_zstr)
7684
//
7785
void owRelease(int portnum)
7886
{
79-
portnum = 0;
87+
if((portnum < MAX_PORTNUM) && (portnum >= 0) )
88+
{
89+
UINT32 pin = (UINT32)owPortPin[portnum];
90+
91+
CPU_GPIO_EnableInputPin( pin, false, NULL, GPIO_INT_NONE, RESISTOR_PULLUP );
92+
93+
owPortPin[portnum] = 0;
94+
}
8095
}
8196

8297

0 commit comments

Comments
 (0)