unit libwiringpi;
(* Pascal wrapper unit for Gordon Henderson wiringPi library. The source can
* be found at https://http://wiringpi.com
*
* hwiringpi: origanal Wrapper and
* pascal sample by Alex Schaller.
*
* Fork
* h2wiringpi: Version 0.1 By Allen Roton
* wiringPi Version 2.26
*
* libwiringpi: Version 0.2 by Pelican (pel.hu)
* I2C, SPI, serial and other functions
*
* wiringPi:
* Arduino compatable (ish) Wiring library for the Raspberry Pi
* Copyright (c) 2012 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*)
{$linklib c}
{$linklib libwiringPi}
interface
// Handy defines
const
// Pin modes
INPUT = 0;
OUTPUT = 1;
PWM_OUTPUT= 2;
GPIO_CLOCK= 3;
SOFT_PWM_OUTPUT= 4;
SOFT_TONE_OUTPUT= 5;
PWM_TONE_OUTPUT= 6;
LOW= 0;
HIGH= 1;
// Pull up/down/none
PUD_OFF= 0;
PUD_DOWN= 1;
PUD_UP= 2;
// PWM
PWM_MODE_MS= 0;
PWM_MODE_BAL= 1;
// Interrupt levels
INT_EDGE_SETUP= 0;
INT_EDGE_FALLING= 1;
INT_EDGE_RISING= 2;
INT_EDGE_BOTH= 3;
PI_MODELS : array [0..7] of string = (
'Unknown',
'model A',
'model B',
'model B+',
'model CM',
'model A+',
'model 2',
'New unknown'
);
PI_VERSIONS : array [0..5] of string = (
'version unknown',
'v1',
'v1.1',
'v1.2',
'v2',
'new unknown'
);
PI_MAKERS : array [0..5] of string = (
'Unknown',
'EGOMAN',
'SONY',
'QISDA',
'MBEST',
'New unknown');
// Pin mappings from P1 connector to WiringPi library
// Px represents to physical pin on the RaspberryPi P1 connector
// P1 = 3.3V
// P2 = 5V
P3 = 8;
// P4 = 5V
P5 = 9;
//P6 = GND
P7 = 7;
P8 = 15;
//P9 = GND
P10 = 16;
P11 = 0;
P12 = 1;
P13 = 2;
//P14 = GND
P15 = 3;
P16 = 4;
// P17 = 3.3V
P18 = 5;
P19 = 12;
// P20 = GND
P21 = 13;
P22 = 6;
P23 = 14;
P24 = 10;
// P25 = GND
P26 = 11;
// pi2
P27 = 30;
P28 = 31;
P29 = 21;
// P30 = GND
P31 = 22;
P32 = 26;
P33 = 23;
// P34 = GND
P35 = 24;
P36 = 27;
P37 = 25; // ops was 24
P38 = 28;
// P39 = GND
P40 = 29;
PI_2_WiretoPhy : array[0..29] of integer = (
11, 12, //0,1
13, 15, //2,3
16, 18, //4,5
22, 7, //6,7
3, 5, //8,9
24, 26, //10,11
19, 21, //12,13
23, 8, //14,15
10,-17, //16,17
-18,-19, //18,19
-20, 29, //20,21
31, 33, //22,23
35, 37, //24,25
32, 36, //26,27
38, 40 //28,29
) ;
// -1 GND
// -5 5V
// -3 3.3V
// -6
// -7
PI_2_Phy_toWire : array [1..40] of integer =
( -3, -5,
8, -5,
9, -1,
7, 15,
-1, 16,
0, 1,
2, -1,
3, 4,
-3, 5,
12, -1,
13, 6,
14, 10,
-1, 11,
-6, -7,
21, -1,
22, 26,
23, -1,
24, 27,
25, 28,
-1, 29
);
altmodes : array[0..7] of string = ('IN', 'OUT', 'ALT5', 'ALT4', 'ALT0', 'ALT1', 'ALT2', 'ALT3');
type
TPinMode = (pm_INPUT,pm_OUTPUT,pm_PWM_OUTPUT,pm_GPIO_CLOCK,pm_SOFT_PWM_OUTPUT,pm_SOFT_TONE_OUTPUT,pm_PWM_TONE_OUTPUT);
TpullRisistor = (prPUD_OFF ,prPUD_DOWN , prPUD_UP );
Tintlevles = (il_EDGE_SETUP,il_EDIGE_FALLING,il_EDGE_RISING,il_EDGE_BOTH);
// Core wiringPi functions
//extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
//extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
//This initialises wiringPi and assumes that the calling program is going to be using the wiringPi pin numbering scheme. This is a simplified numbering scheme which provides a mapping from virtual pin numbers 0 through 16 to the real underlying Broadcom GPIO pin numbers. See the pins page for a table which maps the wiringPi pin number to the Broadcom GPIO pin number to the physical location on the edge connector.
//This function needs to be called with root privileges.
function wiringPiSetup:longint;cdecl;external;
//This initialises wiringPi but uses the /sys/class/gpio interface rather than accessing the hardware directly. This can be called as a non-root user provided the GPIO pins have been exported before-hand using the gpio program. Pin numbering in this mode is the native Broadcom GPIO numbers the same as wiringPiSetupGpio() above, so be aware of the differences between Rev 1 and Rev 2 boards.
//Note: In this mode you can only use the pins which have been exported via the /sys/class/gpio interface before you run your program. You can do this in a separate shell-script, or by using the system() function from inside your program to call the gpio program.
//Also note that some functions have no effect when using this mode as theyre not currently possible to action unless called with root privileges. (although you can use system() to call gpio to set/change modes if needed)
function wiringPiSetupSys:longint;cdecl;external;
// This is identical to wiringPiSetup, however it allows the calling programs to use the Broadcom GPIO pin numbers directly with no re-mapping.
// this function needs to be called with root privileges, and note that some pins are different from revision 1 to revision 2 boards.
function wiringPiSetupGpio:longint;cdecl;external;
//Identical to wiringPiSetupGpio, however it allows the calling programs to use the physical pin numbers on the P1 connector only.
//this function needs to be called with root priviliges.
function wiringPiSetupPhys:longint;cdecl;external;
procedure pinModeAlt(pin:longint; mode:longint);cdecl;external;
procedure pinMode(pin:longint; mode:longint);cdecl;external;
procedure pinMode_pas(pin:longint; mode:TPinMode);
procedure pullUpDnControl(pin:longint; pud:longint);cdecl;external;
//This sets the pull-up or pull-down resistor mode on the given pin, which should be set as an input.
//Unlike the Arduino, the BCM2835 has both pull-up an down internal resistors.
//The parameter pud should be; PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v)
//The internal pull up/down resistors have a value of approximately 50K? on the Raspberry Pi.
//This function has no effect on the Raspberry Pis GPIO pins when in Sys mode.
//If you need to activate a pull-up/pull-down, then you can do it with the gpio program in a script before you start your program.
procedure pullUpDnControl_pas(pin : longint ; pud : tpullRisistor);
function digitalRead(pin:longint):longint;cdecl;external;
procedure digitalWrite(pin:longint; value:longint);cdecl;external;
function digitalWriteByte(value:longint):longint;cdecl;external;
procedure pwmWrite(pin:longint; value:longint);cdecl;external;
function analogRead(pin:longint):longint;cdecl;external;
procedure analogWrite(pin:longint; value:longint);cdecl;external;
// PiFace specifics
// (Deprecated)
//extern int wiringPiSetupPiFace (void) ;
//extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
// On-Board Raspberry Pi hardware specific stuff
function piBoardRev:longint;cdecl;external;
// USE AT YOU OWN RISK
procedure piBoardId(
var model : longint ;
var rev : longint;
var mem : longint;
var maker : longint ;
var overvolted :longint );cdecl;external;
// USE AT YOUR OWN RISK
function piboardtostr() : string ;
function wpiPinToGpio(wpiPin:longint):longint;cdecl;external;
function physPinToGpio(physPin:longint):longint;cdecl;external;
procedure setPadDrive(group:longint; value:longint);cdecl;external;
function getAlt(pin:longint):longint;cdecl;external;
// from getalt to array of sting
function getaltpintostr(pin : longint):string;
procedure pwmToneWrite(group:longint; freq:longint);cdecl;external;
procedure pwmSetMode(range:longint);cdecl;external;
procedure pwmSetRange(range:dword);cdecl;external; //unsigned
procedure pwmSetClock(divisor:longint);cdecl;external;
procedure gpioClockSet(pin:longint; freq:longint);cdecl;external;
// Interrupts
// (Also Pi hardware specific)
// DEPICATED use wiringPiISR
function waitForInterrupt(pin:longint;mS:longint):longint;cdecl;external;
function wiringPiISR(pin:longint;mode:longint ; pcallbback : pointer):longint;cdecl;external;
// wiringPiISR
function wiringPiISR_pas(pin:longint; mode:Tintlevles ; pcallbback : pointer):longint;
// Threads
// function piThreadCreate(ptr:pointer):longint;cdecl;external;
procedure piLock(key:longint);cdecl;external;
procedure piUnlock(key:longint);cdecl;external;
// Schedulling priority
function piHiPri(const pri:longint):longint;cdecl;external;
// Extras from arduino land
procedure delay(howLong:dword);cdecl;external;
procedure delayMicroseconds(howLong:dword);cdecl;external;
//This returns a number representing the number of milliseconds since your
//program called one of the wiringPiSetup functions. It returns an
//unsigned 32-bit number which wraps after 49 days.
function millis:dword;cdecl;external;
function micros:dword;cdecl;external;
procedure wiringPiGpioMode(mode:longint);cdecl;external;
function wiringPiI2CSetupInterface(const device:PChar; devId: longint):longint;cdecl;external;
function wiringPiI2CSetup(const devId: longint):longint;cdecl;external;
function wiringPiI2CRead(fd:longint):longint;cdecl;external;
function wiringPiI2CReadReg8(fd,reg:longint):longint;cdecl;external;
function wiringPiI2CReadReg16(fd,reg:longint):longint;cdecl;external;
function wiringPiI2CWrite(fd,data:longint):longint;cdecl;external;
function wiringPiI2CWriteReg8(fd,reg,data:longint):longint;cdecl;external;
function wiringPiI2CWriteReg16(fd,reg,data:longint):longint;cdecl;external;
function wiringPiSPIGetFd(channel:longint):longint;cdecl;external;
function wiringPiSPIDataRW(channel:longint; data: Pointer; len:longint):longint;cdecl;external;
function wiringPiSPISetup(channel,speed:longint):longint;cdecl;external;
function serialOpen(const device:PChar; const baud: longint):longint;cdecl;external;
procedure serialClose(const fd:longint);cdecl;external;
procedure serialFlush(const fd:longint);cdecl;external;
procedure serialPutchar(const fd:longint; const c: char);cdecl;external;
procedure serialPuts(const fd:longint; const s:PChar);cdecl;external;
// procedure serialPrintf(const fd:longint; const message);cdecl;external;
function serialDataAvail(const fd:longint):longint;cdecl;external;
function serialGetchar(const fd:longint):longint;cdecl;external;
function serialGetString(const fd:longint): String;
procedure serialPutString(const fd:longint; s:string);
//extern void gpioClockSet (int pin, int freq) ;
function max31855Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
function max5322Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
function mcp23008Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
function mcp23016Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
function mcp23017Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
function mcp23s08Setup (const pinBase:longint; const spiPort:longint; const devId: longint):longint;cdecl;external;
function mcp23s17Setup (const pinBase:longint; const spiPort:longint; const devId: longint):longint;cdecl;external;
function mcp3002Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
function mcp3004Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
function mcp4802Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
function sn3218Setup(const pinBase:longint):longint;cdecl;external;
function softPwmCreate(pin,value,range:longint):longint;cdecl;external;
procedure softPwmWrite(pin,value:longint);cdecl;external;
function pcf8574Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
function pcf8591Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
procedure setBit(pin:longint);
procedure clearBit(pin:longint);
function getBit(pin:longint):longint;
implementation
uses sysutils;
procedure pullUpDnControl_pas(pin : longint ; pud : tpullRisistor);
begin
pullUpDnControl(pin,ord(pud));
end;
procedure pinMode_pas(pin:longint; mode:TPinMode);
begin
pinMode(pin, ord(mode));
end;
function wiringPiISR_pas(pin:longint; mode:Tintlevles ; pcallbback : pointer):longint;
begin
result := wiringPiISR(pin,ord(mode) , pcallbback) ;
end;
function piboardtostr() : string ;
var model : longint ;
var rev : longint;
var mem : longint;
var maker : longint ;
var overvolted :longint;
begin
result:='Unkown';
piBoardId(model,rev,mem,maker,overvolted );
result:='MODEL: '+PI_MODELS[model]+#13#10+'REV: '+IntToStr(rev)+#13#10+'MAKER: '+PI_MAKERS[maker]+
#13#10+'MEM: '+IntToStr(mem);
// if overvolted <> 0 then
// result := result + ' OVER VOLT' + inttostr(overvolted );
end;
function getaltpintostr(pin : longint):string;
begin
result := altmodes[ getAlt(pin) ];
end;
function serialGetString(const fd:longint): String;
begin
Result:='';
while serialDataAvail(fd)>0 do Result:=Result+chr(serialGetChar(fd));
end;
procedure serialPutString(const fd:longint; s:string);
begin
serialPuts(fd,PChar(s));
end;
procedure setBit(pin:longint);
begin
digitalWrite(pin,1);
end;
procedure clearBit(pin:longint);
begin
digitalWrite(pin,0);
end;
function getBit(pin:longint):longint;
begin
Result:=digitalRead(pin);
end;
end.