-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtmslclraspihw.pas
More file actions
566 lines (452 loc) · 13.2 KB
/
tmslclraspihw.pas
File metadata and controls
566 lines (452 loc) · 13.2 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
{***************************************************************************}
{ TMS LCL HW components for Raspberry Pi }
{ for Lazarus }
{ }
{ written by TMS Software }
{ copyright © 2015 }
{ Email : info@tmssoftware.com }
{ Web : http://www.tmssoftware.com }
{ }
{ The source code is given as is. The author is not responsible }
{ for any possible damage done due to the use of this code. }
{ The component can be freely used in any application. The complete }
{ source code remains property of the author and may not be distributed, }
{ published, given or sold in any form as such. No parts of the source }
{ code can be included in any other component or application without }
{ written authorization of the author. }
{***************************************************************************}
// CORE I2C ACCESS
unit TMSLCLRaspiHW;
{$mode delphi}
interface
uses
Classes, SysUtils, baseunix;
type
TArrowDirection = (aUp,aDown,aLeft,aRight);
type
spi_ioc_transfer_t = record (* sizeof(spi_ioc_transfer_t) = 32 *)
tx_buf_ptr : qword;
rx_buf_ptr : qword;
len : longword;
speed_hz : longword;
delay_usecs : word;
bits_per_word : byte;
cs_change : byte;
pad : longword;
end;
{ TTMSLCLRaspiI2C }
TTMSLCLRaspiI2C = class(TComponent)
private
FHandle: integer;
FI2CAddress: integer;
FI2CPort: CInt;
protected
property Handle: Integer read FHandle;
public
constructor Create(AOwner: TComponent); override;
function Open: boolean; virtual;
function Close: boolean; virtual;
function Connected: boolean;
function SetByteRegister(RegNo: Integer; Val: Byte): Integer;
function GetByteRegister(RegNo: Integer): byte;
published
property I2CAddress: integer read FI2CAddress write FI2CAddress;
property I2CPort: CInt read FI2CPort write FI2CPort;
end;
TSPIPortNum = (spi0, spi1);
{ TTMSLCLRaspiSPI }
TTMSLCLRaspiSPI = class(TComponent)
private
FHandle: integer;
FMode: integer;
FBits: byte;
FSpeed: longword;
FDelay: word;
FPortNum: TSPIPortNum;
SPI_IOC_RD_MODE, SPI_IOC_WR_MODE: longword;
SPI_IOC_WR_BITS_PER_WORD, SPI_IOC_RD_BITS_PER_WORD: longword;
SPI_IOC_WR_MAX_SPEED_HZ, SPI_IOC_RD_MAX_SPEED_HZ: longword;
protected
property Handle: Integer read FHandle;
procedure InitTransfer(var spi_struct:spi_ioc_transfer_t; rx_bufptr,tx_bufptr:pointer; xferlen:longword);
public
constructor Create(AOwner: TComponent); override;
function Open: boolean; virtual;
function Close: boolean; virtual;
function Connected: boolean;
function Transfer(cmd: byte): boolean;
function ReadTransfer(buf: pointer; wsize, rsize: integer): boolean;
function WriteTransfer(buf: pointer; wsize: integer): boolean;
function SetByteRegister(RegNo: Integer; Val: Byte): Integer;
function GetByteRegister(RegNo: Integer): byte;
function Command(cmd: byte): boolean;
published
property PortNum: TSPIPortNum read FPortNum write FPortNum;
end;
implementation
const
I2C_SLAVE = 1795;
DeviceID = '/dev/i2c-'; // i2c port
SPI_CPHA = $01;
SPI_CPOL = $02;
SPI_MODE_0 = 0;
SPI_MODE_1 = SPI_CPHA;
SPI_MODE_2 = SPI_CPOL;
SPI_MODE_3 = SPI_CPOL or SPI_CPHA;
SPI_IOC_MAGIC = 'k';
SPI_BUF_SIZE_c = 64;
_IOC_NONE = $00;
_IOC_WRITE = $01;
_IOC_READ = $02;
_IOC_NRBITS = 8;
_IOC_TYPEBITS = 8;
_IOC_SIZEBITS = 14;
_IOC_DIRBITS = 2;
_IOC_NRSHIFT = 0;
_IOC_TYPESHIFT = (_IOC_NRSHIFT + _IOC_NRBITS);
_IOC_SIZESHIFT = (_IOC_TYPESHIFT +_IOC_TYPEBITS);
_IOC_DIRSHIFT = (_IOC_SIZESHIFT +_IOC_SIZEBITS);
spidevice = '/dev/spidev0.'; // spi port
function InitI2cDevice(devpath: String; iDevAddr: Cint; var hInst: Integer):Integer;
var
iio : integer;
begin
try
hInst := fpopen(devpath,O_RDWR); //Open the I2C bus in Read/Write mode
iio := FpIOCtl(hInst, I2C_SLAVE, pointer(iDevAddr)); //Set options
if (iio = 0) and (hInst > 0) then
InitI2cDevice := hInst
else
InitI2cDevice := -1;
except
InitI2cDevice := -1;
end;
end;
procedure CloseI2cDevice(hInst: Integer);
begin
fpclose(hInst);
end;
{ TTMSLCLRaspiI2C }
constructor TTMSLCLRaspiI2C.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHandle := -1;
FI2CPort := 1;
FI2CAddress := 0;
end;
function TTMSLCLRaspiI2C.Open: boolean;
var
DevName: string;
begin
DevName := DeviceID + inttostr(I2CPort);
Result := InitI2cDevice(DevName,I2CAddress,FHandle) <> -1;
end;
function TTMSLCLRaspiI2C.Close: boolean;
begin
Result := false;
if FHandle <> -1 then
begin
CloseI2CDevice(FHandle);
Result := true;
FHandle := -1;
end;
end;
function TTMSLCLRaspiI2C.Connected: boolean;
begin
Result := FHandle <> -1;
end;
function TTMSLCLRaspiI2C.SetByteRegister(RegNo: Integer; Val: Byte): Integer;
var
buf: array[0..1] of byte;
begin
try
buf[0] := RegNo;
buf[1] := val;
fpwrite(FHandle, buf[0], 2);
except
Result := -1;
end;
Result := 1;
end;
function TTMSLCLRaspiI2C.GetByteRegister(RegNo: Integer): byte;
var
buf: array[0..1] of byte;
iRet : Byte;
begin
try
buf[0] := RegNo;
iRet := 0;
fpwrite(FHandle, buf[0], 1);
fpread(FHandle, iRet, 1);
except
Result := 0;
end;
Result := iRet;
end;
//------------------------------------------------------------------------------
// SPI IOC commands
function _IOC(dir:byte; typ:char; nr,size:word): longword;
begin
_ioc := (dir shl _IOC_DIRSHIFT) or
(ord(typ) shl _IOC_TYPESHIFT) or
(nr shl _IOC_NRSHIFT) or
(size shl _IOC_SIZESHIFT);
end;
function _IO(typ:char; nr:word):longword;
begin
_IO := _IOC(_IOC_NONE,typ,nr,0);
end;
function _IOR(typ:char; nr,size:word):longword;
begin
_IOR := _IOC(_IOC_Read,typ,nr,size);
end;
function _IOW(typ:char; nr,size:word):longword;
begin
_IOW := _IOC(_IOC_Write, typ,nr,size);
end;
function _IOWR(typ:char; nr,size:word):longword;
begin
_IOWR := _IOC((_IOC_Write or _IOC_Read),typ,nr,size);
end;
function SPI_MSGSIZE(n:byte):word;
var
siz:word;
begin
if n * SizeOf(spi_ioc_transfer_t) < (1 shl _IOC_SIZEBITS) then
siz := n * SizeOf(spi_ioc_transfer_t)
else
siz := 0;
SPI_MSGSIZE := siz;
end;
function SPI_IOC_MESSAGE(n:byte):longword;
begin
SPI_IOC_MESSAGE := _IOW(SPI_IOC_MAGIC, 0, SPI_MSGSIZE(n));
end;
//------------------------------------------------------------------------------
constructor TTMSLCLRaspiSPI.Create(AOwner: TComponent);
begin
inherited;
FHandle := -1;
FPortNum := spi0;
end;
function TTMSLCLRaspiSPI.Open: boolean;
var
ret: integer;
spidevid: string;
begin
SPI_IOC_RD_MODE := _IOR(SPI_IOC_MAGIC, 1, 1);
SPI_IOC_WR_MODE := _IOW(SPI_IOC_MAGIC, 1, 1);
SPI_IOC_WR_BITS_PER_WORD := _IOW(SPI_IOC_MAGIC, 3, 1);
SPI_IOC_RD_BITS_PER_WORD := _IOR(SPI_IOC_MAGIC, 3, 1);
SPI_IOC_WR_MAX_SPEED_HZ := _IOW(SPI_IOC_MAGIC, 4, 4);
SPI_IOC_RD_MAX_SPEED_HZ := _IOR(SPI_IOC_MAGIC, 4, 4);
case FPortNum of
spi0: spidevid := spidevice + '0';
spi1: spidevid := spidevice + '1';
end;
fHandle := fpopen(spidevid, O_RDWR);
if (fHandle > 0) then
begin
fmode := 0;
fdelay := 5;
ret := fpioctl(fHandle, SPI_IOC_WR_MODE, addr(fmode));
if (ret = -1) then
raise exception.Create('Can''t set SPI write mode');
ret := fpioctl(fHandle, SPI_IOC_RD_MODE, addr(fmode));
if (ret = -1) then
raise exception.Create('Can''t get SPI read mode');
fbits := 8;
ret := fpioctl(fHandle, SPI_IOC_WR_BITS_PER_WORD, addr(fbits));
if (ret = -1) then
raise exception.Create('Can''t set SPI write bits per word');
ret := fpioctl(fHandle, SPI_IOC_RD_BITS_PER_WORD, addr(fbits));
if (ret = -1) then
raise exception.Create('Can''t set SPI read bits per word');
fspeed := 1000000;
ret := fpioctl(fHandle, SPI_IOC_WR_MAX_SPEED_HZ, addr(fspeed));
if (ret = -1) then
raise exception.Create('Can''t set SPI write speed');
ret := fpioctl(fHandle, SPI_IOC_RD_MAX_SPEED_HZ, addr(fspeed));
if (ret = -1) then
raise exception.Create('Can''t set SPI read speed');
end;
end;
procedure TTMSLCLRaspiSPI.InitTransfer(var spi_struct:spi_ioc_transfer_t; rx_bufptr,tx_bufptr:pointer; xferlen:longword);
var
xlen:longword;
begin
xlen := xferlen;
if xlen > SPI_BUF_SIZE_c+1 then
xlen := SPI_BUF_SIZE_c+1;
with spi_struct do
begin
{$warnings off}
rx_buf_ptr := qword(rx_bufptr);
tx_buf_ptr := qword(tx_bufptr);
{$warnings on}
len := xlen;
delay_usecs := fdelay;
speed_hz := fspeed;
bits_per_word := fbits;
cs_change := 0;
pad := 0;
end;
end;
function TTMSLCLRaspiSPI.SetByteRegister(RegNo: Integer; Val: Byte): Integer;
var
xfer: spi_ioc_transfer_t;
buf: array[0..1] of byte;
begin
buf[0] := (regNo and $FF) or $80;
buf[1] := Val;
InitTransfer(xfer, addr(buf), addr(buf), 2);
Result := fpioctl(fHandle, SPI_IOC_MESSAGE(1), addr(xfer));
end;
function TTMSLCLRaspiSPI.GetByteRegister(RegNo: Integer): byte;
var
xfer: array[0..1] of spi_ioc_transfer_t;
buf: array[0..1] of byte;
res: integer;
begin
Result := -1;
buf[0] := (regNo and $FF);
InitTransfer(xfer[0], addr(buf), addr(buf), 1);
InitTransfer(xfer[1], addr(buf), addr(buf), 1);
res := fpioctl(fHandle, SPI_IOC_MESSAGE(2), addr(xfer));
if res >= 0 then
Result := buf[0];
end;
function TTMSLCLRaspiSPI.Command(cmd: byte): boolean;
var
xfer: spi_ioc_transfer_t;
buf: array[0..1] of byte;
begin
buf[0] := (cmd and $FF) or $80;
buf[1] := 0;
InitTransfer(xfer, addr(buf), addr(buf), 1);
Result := fpioctl(fHandle, SPI_IOC_MESSAGE(0), addr(xfer)) >= 0;
end;
function TTMSLCLRaspiSPI.Close: boolean;
begin
fpclose(fHandle);
end;
function TTMSLCLRaspiSPI.Connected: boolean;
begin
Result := fHandle <> -1;
end;
(*
function TTMSLCLRaspiSPI.GetDeviceID(var manufid, prodid:word): boolean;
var
xfer: array[0..1] of spi_ioc_transfer_t;
buf: array[0..4] of byte;
res: integer;
begin
Result := false;
buf[0] := $9F; //
InitTransfer(xfer[0], addr(buf), addr(buf), 1);
InitTransfer(xfer[1], addr(buf), addr(buf), 4);
res := fpioctl(fHandle, SPI_IOC_MESSAGE(2), addr(xfer));
Result := (res >= 0);
if Result then
begin
manufid := buf[0];
prodid := buf[2];
prodid := (prodid shl 8) + buf[3];
end;
end;
function TTMSLCLRaspiSPI.SetStatusReg(val: byte): boolean;
var
xfer: array[0..1] of spi_ioc_transfer_t;
buf: array[0..1] of byte;
res: integer;
begin
Result := false;
buf[0] := $01; //
buf[1] := val; //
InitTransfer(xfer[0], addr(buf), addr(buf), 2);
res := fpioctl(fHandle, SPI_IOC_MESSAGE(1), addr(xfer));
Result := (res >= 0);
if Result then
begin
val := buf[0];
end;
end;
function TTMSLCLRaspiSPI.GetStatusReg(var val: byte): boolean;
var
xfer: array[0..1] of spi_ioc_transfer_t;
buf: array[0..1] of byte;
res: integer;
begin
Result := false;
buf[0] := $05; //
InitTransfer(xfer[0], addr(buf), addr(buf), 1);
InitTransfer(xfer[1], addr(buf), addr(buf), 1);
res := fpioctl(fHandle, SPI_IOC_MESSAGE(2), addr(xfer));
Result := (res >= 0);
if Result then
begin
val := buf[0];
end;
end;
*)
function TTMSLCLRaspiSPI.Transfer(cmd: byte): boolean;
var
xfer: spi_ioc_transfer_t;
buf: array[0..1] of byte;
res: integer;
begin
buf[0] := cmd;
InitTransfer(xfer, addr(buf), addr(buf), 1);
res := fpioctl(fHandle, SPI_IOC_MESSAGE(1), addr(xfer));
Result := res >= 0;
end;
function TTMSLCLRaspiSPI.ReadTransfer(buf: pointer; wsize, rsize: integer): boolean;
var
xfer: array[0..1] of spi_ioc_transfer_t;
begin
InitTransfer(xfer[0], buf, buf, wsize);
InitTransfer(xfer[1], buf, buf, rsize);
Result := fpioctl(fHandle, SPI_IOC_MESSAGE(2), addr(xfer)) >= 0;
end;
function TTMSLCLRaspiSPI.WriteTransfer(buf: pointer; wsize: integer): boolean;
var
xfer: spi_ioc_transfer_t;
begin
InitTransfer(xfer, buf, buf, wsize);
Result := fpioctl(fHandle, SPI_IOC_MESSAGE(1), addr(xfer)) >= 0;
end;
(*
function TTMSLCLRaspiSPI.Write(Adr: word; val: byte): boolean;
var
xfer: spi_ioc_transfer_t;
buf: array[0..1] of byte;
res: integer;
begin
buf[0] := $2;
buf[1] := adr shr 8;
buf[2] := adr and $FF;
buf[3] := val;
InitTransfer(xfer, addr(buf), addr(buf), 4);
res := fpioctl(fHandle, SPI_IOC_MESSAGE(1), addr(xfer));
Result := res >= 0;
end;
function TTMSLCLRaspiSPI.Read(Adr: word; var val: byte): boolean;
var
xfer: array[0..1] of spi_ioc_transfer_t;
buf: array[0..1] of byte;
res: integer;
begin
buf[0] := $3;
buf[1] := adr shr 8;
buf[2] := adr and $FF;
InitTransfer(xfer[0], addr(buf), addr(buf), 3);
InitTransfer(xfer[1], addr(buf), addr(buf), 3);
res := fpioctl(fHandle, SPI_IOC_MESSAGE(2), addr(xfer));
Result := res >= 0;
if Result then
begin
val := buf[0];
end;
end;
*)
end.