Lazarus on Raspberry Pi/zh CN: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | 树莓派上的Lazarus | |
| 2 | 2 | ||
| 3 | - | ||
| 3 | + |  *(Raspberry Pi only)* | |
| 4 | 4 | ||
| 5 | 5 | Raspberry Pi(数莓派)是一个信用卡大小的单板计算机。它由英国的Raspberry Pi基金为了激励在学校的计算机基础科学教学发起的。Raspberry Pis也可以用来作为媒体服务器以及其他用途机器人、控制工程等多种用途。 Raspberry Pi基金会推荐[[Raspbian]] Wheezy作为标准的操作系统。大家可以选择在RPI运行RISC OS、各种Linux发行版以及[[Android]]等操作系统。 Lazarus可以在原生的Raspbian操作系统下运行。 | |
| 6 | 6 | ||
| 7 | 7 | ## 安装与编译器Lazarus | |
| 8 | 8 | ||
| 9 | 9 | ### 在 Raspbian上简单安装 | |
| 10 | 10 | ||
| 11 | 11 | #### Raspberry Pi 1 | |
| 12 | 12 | ||
| 13 | 13 | 在 Raspbian OS上,安装Lazarus与 Free Pascal很容易。打开命令行窗口输入下面的指令: | |
| 14 | 14 | ||
| 15 | 15 | ``` bash | |
| 16 | 16 | sudo apt-get update | |
| 17 | 17 | sudo apt-get upgrade | |
| 18 | 18 | sudo apt-get install fpc | |
| 19 | 19 | sudo apt-get install lazarus | |
| 20 | 20 | ``` | |
| 21 | 21 | ||
| 22 | 22 | 这里安装的是为Raspberry Pi预编译的稳定版FPC与Lazarus 。当然,需要有网络连接。安装过程大概需要30分钟,这个过程是自动完成等的。安装完成以后,你就可以从LXDE的开始菜单的“Programming”启动Lazarus了。 | |
| 23 | 23 | ||
| 24 | 24 | Install Lazarus Rasbian Part1 en.png\|安装步骤 1 Install Lazarus Rasbian Part2 en.png\|安装步骤 2 Install Lazarus Rasbian Part3 en.png\|安装步骤 3 Install Lazarus Rasbian Part4 en.png\|安装步骤 4 | |
| 25 | 25 | ||
| 26 | 26 | 如果你需要一个新的版本,或者抱怨Lazarus 的问题, [点击](http://www.raspberrypi.org/forums/viewtopic.php?f=34&t=88145). | |
| 27 | 27 | ||
| 28 | 28 | #### Raspberry Pi 2 | |
| 29 | 29 | ||
| 30 | 30 | 自2015六月起,普通的“out of the box”安装方法也适用于安装树莓派2的B型。但是安装时获得的版本是相当旧的。想要获得新版FPC和Lazarus请看链接 [相关文章](http://www.tweaking4all.com/hardware/raspberry-pi/install-lazarus-pascal-on-raspberry-pi-2/). | |
| 31 | 31 | ||
| 32 | 32 | <File:Lazarus> 0 9 30 4 on RPi 2.png\|Lazarus "out of the box" on Raspberry Pi 2 | |
| 33 | 33 | ||
| 34 | 34 | ### 从Windows 对Raspberry Pi的交叉编译 | |
| 35 | 35 | ||
| 36 | 36 | 1\. 使用 fpcup | |
| 37 | 37 | ||
| 38 | 38 | 使用fpcup是实现交叉编译的一种方法,输入下面的指令: [[fpcup|fpcup#Linux_ARM_cross_compiler]] | |
| 39 | 39 | ||
| 40 | 40 | 2\. 使用脚本 | |
| 41 | 41 | ||
| 42 | 42 | 可以使用手动批处理文件,按照以下步骤。 | |
| 43 | 43 | ||
| 44 | 44 | 2.1 先决条件 | |
| 45 | 45 | ||
| 46 | 46 | `FPC 2.7.1或更高的安装源代码`\ | |
| 47 | 47 | `下载并安装Windows版本的Linaro binutils for linux gnueabihf到%FPCPATH%/bin/win32-armhf-linux `[`1`](https://launchpad.net/linaro-toolchain-binaries/trunk/2013.10/+download/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_win32.zip) | |
| 48 | 48 | ||
| 49 | 49 | 2.2 创建脚本(需要匹配实际路径) | |
| 50 | 50 | ||
| 51 | 51 | ``` bat | |
| 52 | 52 | set PATH=C:\pp\bin\i386-win32;%PATH%; | |
| 53 | 53 | set FPCMAKEPATH=C:/pp | |
| 54 | 54 | set FPCPATH=C:/pp | |
| 55 | 55 | set OUTPATH=C:/pp271 | |
| 56 | 56 | %FPCMAKEPATH%/bin/i386-win32/make distclean OS_TARGET=linux CPU_TARGET=arm CROSSBINDIR=%FPCPATH%/bin/win32-armhf-linux CROSSOPT="-CpARMV6 -CfVFPV2 -OoFASTMATH" FPC=%FPCPATH%/bin/i386-win32/ppc386.exe | |
| 57 | 57 | ||
| 58 | 58 | %FPCMAKEPATH%/bin/i386-win32/make all OS_TARGET=linux CPU_TARGET=arm CROSSBINDIR=%FPCPATH%/bin/win32-armhf-linux CROSSOPT="-CpARMV6 -CfVFPV2 -OoFASTMATH" FPC=%FPCPATH%/bin/i386-win32/ppc386.exe | |
| 59 | 59 | if errorlevel 1 goto quit | |
| 60 | 60 | %FPCMAKEPATH%/bin/i386-win32/make crossinstall CROSSBINDIR=%FPCPATH%/bin/win32-armhf-linux CROSSOPT="-CpARMV6 -CfVFPV2 -OoFASTMATH" OS_TARGET=linux CPU_TARGET=arm FPC=%FPCPATH%/bin/i386-win32/ppc386.exe INSTALL_BASEDIR=%OUTPATH% | |
| 61 | 61 | ||
| 62 | 62 | :quit | |
| 63 | 63 | pause | |
| 64 | 64 | ``` | |
| 65 | 65 | ||
| 66 | 66 | 使用生成的ppcrossarm.exe和 ARM RTL,你可以创建跨平台的Lazarus工程,生成Raspberry Pi和其他armhf设备的应用程序。 注意,这里不包括所有Window 库。 | |
| 67 | 67 | ||
| 68 | 68 | ### 编译源代码 | |
| 69 | 69 | ||
| 70 | 70 | 如果你想要从Subversion编译Lazarus源代码。请看 \[<http://www.michellcomputing.co.uk/blog/?p=72>: Michell Computing: Lazarus on the Raspberry Pi\] | |
| 71 | 71 | ||
| 72 | 72 | <File:Start> Menu with Lazarus en.png\|How to start Lazarus in Raspbian Wheezy <File:Lazarus> on Raspberry Pi.jpg\|Lazarus on Raspberry Pi <File:rpi> gpio pins.png\|Numbering of GPIO pins | |
| 73 | 73 | ||
| 74 | 74 | 用Gentoo(及其他版本)在Raspberry上编译源码 | |
| 75 | 75 | ||
| 76 | 76 | - Gentoo是一个基于Linux的自由操作系统 | |
| 77 | 77 | ||
| 78 | 78 | ------------------------------------------------------------------------ | |
| 79 | 79 | ||
| 80 | 80 | 如果你想安装最新版的FPC及附件,或者是集成FPC编译器:请阅读下面的指南 这是关于Gentoo的例子,本指南适用其他版本:[[Install fpc on Raspberry with Gentoo]] | |
| 81 | 81 | ||
| 82 | 82 | ## 访问外部硬件 | |
| 83 | 83 | ||
| 84 | - | ||
| 84 | + |  | |
| 85 | 85 | ||
| 86 | 86 | Raspberry Pi的发展的目标之一是方便轻松地访问外部设备,如传感器和控制器。Lazarus有五种方法来访问I/O设备: | |
| 87 | 87 | ||
| 88 | 88 | 1. [[Lazarus on Raspberry Pi|原生代码直接访问]],使用[[BaseUnix]]单元 | |
| 89 | 89 | 2. 通过 [[Lazarus on Raspberry Pi|封装外壳调用]] | |
| 90 | 90 | 3. 通过 [[Lazarus on Raspberry Pi|wiringPi 库]]. | |
| 91 | 91 | 4. 使用单元 [[Lazarus on Raspberry Pi|rpi_hal]]. | |
| 92 | 92 | 5. 使用单元 [[Lazarus on Raspberry Pi|PiGpio]]. | |
| 93 | 93 | 6. 通过 [PascalIO](https://github.com/SAmeis/pascalio) 库访问. | |
| 94 | 94 | ||
| 95 | 95 | ### 1. 原生代码直接访问 | |
| 96 | 96 | ||
| 97 | 97 |    | |
| 98 | 98 | ||
| 99 | 99 | 这种访问外部硬件的方法,不需要额外的库。唯一的要求是baseunix库,这是Free Pascal的RTL部分。 | |
| 100 | 100 | ||
| 101 | 101 | #### 通过GPIO端口控制开关 | |
| 102 | 102 | ||
| 103 | 103 | 下面将演示一个简单程序。控制GPIO的17号引脚的电压,可以用来控制LED、晶体管或继电器。 程序包含*GPIO17ToggleBox*的实例[[doc:lcl/stdctrls/ttogglebox.html|ToggleBox]]和日志返回*LogMemo*类的实例[[doc:lcl/stdctrls/tmemo.html|TMemo]]。 | |
| 104 | 104 | ||
| 105 | 105 | 例如,一个LED的阳极连接到引脚11上(BCM2835 SoC对应的GPIO为17)和LED的阴极通过一个68欧姆的电阻连接引脚6(GND)。随后,LED会随着应用程序的toggle box切换点亮和熄灭。 | |
| 106 | 106 | ||
| 107 | 107 | 该代码需要root权限运行,即从root帐号(不推荐)运行或通过“*su*'。 | |
| 108 | 108 | ||
| 109 | 109 | *控制单元:* | |
| 110 | 110 | ||
| 111 | 111 | ``` pascal | |
| 112 | 112 | unit Unit1; | |
| 113 | 113 | ||
| 114 | 114 | {Demo application for GPIO on Raspberry Pi} | |
| 115 | 115 | {Inspired by the Python input/output demo application by Gareth Halfacree} | |
| 116 | 116 | {written for the Raspberry Pi User Guide, ISBN 978-1-118-46446-5} | |
| 117 | 117 | ||
| 118 | 118 | {$mode objfpc}{$H+} | |
| 119 | 119 | ||
| 120 | 120 | interface | |
| 121 | 121 | ||
| 122 | 122 | uses | |
| 123 | 123 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, | |
| 124 | 124 | Unix, BaseUnix; | |
| 125 | 125 | ||
| 126 | 126 | type | |
| 127 | 127 | ||
| 128 | 128 | { TForm1 } | |
| 129 | 129 | ||
| 130 | 130 | TForm1 = class(TForm) | |
| 131 | 131 | LogMemo: TMemo; | |
| 132 | 132 | GPIO17ToggleBox: TToggleBox; | |
| 133 | 133 | procedure FormActivate(Sender: TObject); | |
| 134 | 134 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); | |
| 135 | 135 | procedure GPIO17ToggleBoxChange(Sender: TObject); | |
| 136 | 136 | private | |
| 137 | 137 | { private declarations } | |
| 138 | 138 | public | |
| 139 | 139 | { public declarations } | |
| 140 | 140 | end; | |
| 141 | 141 | ||
| 142 | 142 | const | |
| 143 | 143 | PIN_17: PChar = '17'; | |
| 144 | 144 | PIN_ON: PChar = '1'; | |
| 145 | 145 | PIN_OFF: PChar = '0'; | |
| 146 | 146 | OUT_DIRECTION: PChar = 'out'; | |
| 147 | 147 | ||
| 148 | 148 | var | |
| 149 | 149 | Form1: TForm1; | |
| 150 | 150 | gReturnCode: longint; {stores the result of the IO operation} | |
| 151 | 151 | ||
| 152 | 152 | implementation | |
| 153 | 153 | ||
| 154 | 154 | {$R *.lfm} | |
| 155 | 155 | ||
| 156 | 156 | { TForm1 } | |
| 157 | 157 | ||
| 158 | 158 | procedure TForm1.FormActivate(Sender: TObject); | |
| 159 | 159 | var | |
| 160 | 160 | fileDesc: integer; | |
| 161 | 161 | begin | |
| 162 | 162 | { Prepare SoC pin 17 (pin 11 on GPIO port) for access: } | |
| 163 | 163 | try | |
| 164 | 164 | fileDesc := fpopen('/sys/class/gpio/export', O_WrOnly); | |
| 165 | 165 | gReturnCode := fpwrite(fileDesc, PIN_17[0], 2); | |
| 166 | 166 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 167 | 167 | finally | |
| 168 | 168 | gReturnCode := fpclose(fileDesc); | |
| 169 | 169 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 170 | 170 | end; | |
| 171 | 171 | { Set SoC pin 17 as output: } | |
| 172 | 172 | try | |
| 173 | 173 | fileDesc := fpopen('/sys/class/gpio/gpio17/direction', O_WrOnly); | |
| 174 | 174 | gReturnCode := fpwrite(fileDesc, OUT_DIRECTION[0], 3); | |
| 175 | 175 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 176 | 176 | finally | |
| 177 | 177 | gReturnCode := fpclose(fileDesc); | |
| 178 | 178 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 179 | 179 | end; | |
| 180 | 180 | end; | |
| 181 | 181 | ||
| 182 | 182 | procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); | |
| 183 | 183 | var | |
| 184 | 184 | fileDesc: integer; | |
| 185 | 185 | begin | |
| 186 | 186 | { Free SoC pin 17: } | |
| 187 | 187 | try | |
| 188 | 188 | fileDesc := fpopen('/sys/class/gpio/unexport', O_WrOnly); | |
| 189 | 189 | gReturnCode := fpwrite(fileDesc, PIN_17[0], 2); | |
| 190 | 190 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 191 | 191 | finally | |
| 192 | 192 | gReturnCode := fpclose(fileDesc); | |
| 193 | 193 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 194 | 194 | end; | |
| 195 | 195 | end; | |
| 196 | 196 | ||
| 197 | 197 | procedure TForm1.GPIO17ToggleBoxChange(Sender: TObject); | |
| 198 | 198 | var | |
| 199 | 199 | fileDesc: integer; | |
| 200 | 200 | begin | |
| 201 | 201 | if GPIO17ToggleBox.Checked then | |
| 202 | 202 | begin | |
| 203 | 203 | { Swith SoC pin 17 on: } | |
| 204 | 204 | try | |
| 205 | 205 | fileDesc := fpopen('/sys/class/gpio/gpio17/value', O_WrOnly); | |
| 206 | 206 | gReturnCode := fpwrite(fileDesc, PIN_ON[0], 1); | |
| 207 | 207 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 208 | 208 | finally | |
| 209 | 209 | gReturnCode := fpclose(fileDesc); | |
| 210 | 210 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 211 | 211 | end; | |
| 212 | 212 | end | |
| 213 | 213 | else | |
| 214 | 214 | begin | |
| 215 | 215 | { Switch SoC pin 17 off: } | |
| 216 | 216 | try | |
| 217 | 217 | fileDesc := fpopen('/sys/class/gpio/gpio17/value', O_WrOnly); | |
| 218 | 218 | gReturnCode := fpwrite(fileDesc, PIN_OFF[0], 1); | |
| 219 | 219 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 220 | 220 | finally | |
| 221 | 221 | gReturnCode := fpclose(fileDesc); | |
| 222 | 222 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 223 | 223 | end; | |
| 224 | 224 | end; | |
| 225 | 225 | end; | |
| 226 | 226 | ||
| 227 | 227 | end. | |
| 228 | 228 | ``` | |
| 229 | 229 | ||
| 230 | 230 | *Main program:* | |
| 231 | 231 | ||
| 232 | 232 | ``` pascal | |
| 233 | 233 | program io_test; | |
| 234 | 234 | ||
| 235 | 235 | {$mode objfpc}{$H+} | |
| 236 | 236 | ||
| 237 | 237 | uses | |
| 238 | 238 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 239 | 239 | cthreads, | |
| 240 | 240 | {$ENDIF}{$ENDIF} | |
| 241 | 241 | Interfaces, // this includes the LCL widgetset | |
| 242 | 242 | Forms, Unit1 | |
| 243 | 243 | { you can add units after this }; | |
| 244 | 244 | ||
| 245 | 245 | {$R *.res} | |
| 246 | 246 | ||
| 247 | 247 | begin | |
| 248 | 248 | Application.Initialize; | |
| 249 | 249 | Application.CreateForm(TForm1, Form1); | |
| 250 | 250 | Application.Run; | |
| 251 | 251 | end. | |
| 252 | 252 | ``` | |
| 253 | 253 | ||
| 254 | 254 | #### 读取引脚的状态 | |
| 255 | 255 | ||
| 256 | 256 |    | |
| 257 | 257 | ||
| 258 | 258 | 当然也可以从GPIO端口读取一个开关的状态。 | |
| 259 | 259 | ||
| 260 | 260 | 下面的简单例子非常类似于前一个例子。使用GPIO的18号引脚,控制二进制设备如开关输入,晶体管或继电器。这个程序包含一个名称为[[doc:lcl/stdctrls/tcheckbox.html|CheckBox]]的*GPIO18CheckBox* 与*LogMemo*的实例[[doc:lcl/stdctrls/tmemo.html|TMemo]]日志返回代码。 | |
| 261 | 261 | ||
| 262 | 262 | 在实例中,第12引脚的一端连接一个按钮,按钮的一端(对应的GPIO引脚18的BCM2835 SoC)通过一个10欧姆的上拉电阻和引脚1连接(+3.3 V,看接线图)。按钮的另一端连接6号引脚(GND)。按钮的开关状态对应和程序中复选框的打开或关闭状态对应。 | |
| 263 | 263 | ||
| 264 | 264 | 注意,当按钮释放时18脚的电位为高(由通过上拉电阻连接到引脚1),当按钮压下时电压为低(因为在这种情况下,引脚18是通过开关连接到GND)。因此,按钮按下时GPIO引脚信号为0,当释放时信号为1。 | |
| 265 | 265 | ||
| 266 | 266 | 这个程序同样需要root权限 | |
| 267 | 267 | ||
| 268 | 268 | *控制单元:* | |
| 269 | 269 | ||
| 270 | 270 | ``` pascal | |
| 271 | 271 | unit Unit1; | |
| 272 | 272 | ||
| 273 | 273 | {Demo application for GPIO on Raspberry Pi} | |
| 274 | 274 | {Inspired by the Python input/output demo application by Gareth Halfacree} | |
| 275 | 275 | {written for the Raspberry Pi User Guide, ISBN 978-1-118-46446-5} | |
| 276 | 276 | ||
| 277 | 277 | {This application reads the status of a push-button} | |
| 278 | 278 | ||
| 279 | 279 | {$mode objfpc}{$H+} | |
| 280 | 280 | ||
| 281 | 281 | interface | |
| 282 | 282 | ||
| 283 | 283 | uses | |
| 284 | 284 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, | |
| 285 | 285 | ButtonPanel, Unix, BaseUnix; | |
| 286 | 286 | ||
| 287 | 287 | type | |
| 288 | 288 | ||
| 289 | 289 | { TForm1 } | |
| 290 | 290 | ||
| 291 | 291 | TForm1 = class(TForm) | |
| 292 | 292 | ApplicationProperties1: TApplicationProperties; | |
| 293 | 293 | GPIO18CheckBox: TCheckBox; | |
| 294 | 294 | LogMemo: TMemo; | |
| 295 | 295 | procedure ApplicationProperties1Idle(Sender: TObject; var Done: Boolean); | |
| 296 | 296 | procedure FormActivate(Sender: TObject); | |
| 297 | 297 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); | |
| 298 | 298 | private | |
| 299 | 299 | { private declarations } | |
| 300 | 300 | public | |
| 301 | 301 | { public declarations } | |
| 302 | 302 | end; | |
| 303 | 303 | ||
| 304 | 304 | const | |
| 305 | 305 | PIN_18: PChar = '18'; | |
| 306 | 306 | IN_DIRECTION: PChar = 'in'; | |
| 307 | 307 | ||
| 308 | 308 | var | |
| 309 | 309 | Form1: TForm1; | |
| 310 | 310 | gReturnCode: longint; {stores the result of the IO operation} | |
| 311 | 311 | ||
| 312 | 312 | implementation | |
| 313 | 313 | ||
| 314 | 314 | {$R *.lfm} | |
| 315 | 315 | ||
| 316 | 316 | { TForm1 } | |
| 317 | 317 | ||
| 318 | 318 | procedure TForm1.FormActivate(Sender: TObject); | |
| 319 | 319 | var | |
| 320 | 320 | fileDesc: integer; | |
| 321 | 321 | begin | |
| 322 | 322 | { Prepare SoC pin 18 (pin 12 on GPIO port) for access: } | |
| 323 | 323 | try | |
| 324 | 324 | fileDesc := fpopen('/sys/class/gpio/export', O_WrOnly); | |
| 325 | 325 | gReturnCode := fpwrite(fileDesc, PIN_18[0], 2); | |
| 326 | 326 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 327 | 327 | finally | |
| 328 | 328 | gReturnCode := fpclose(fileDesc); | |
| 329 | 329 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 330 | 330 | end; | |
| 331 | 331 | { Set SoC pin 18 as input: } | |
| 332 | 332 | try | |
| 333 | 333 | fileDesc := fpopen('/sys/class/gpio/gpio18/direction', O_WrOnly); | |
| 334 | 334 | gReturnCode := fpwrite(fileDesc, IN_DIRECTION[0], 2); | |
| 335 | 335 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 336 | 336 | finally | |
| 337 | 337 | gReturnCode := fpclose(fileDesc); | |
| 338 | 338 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 339 | 339 | end; | |
| 340 | 340 | end; | |
| 341 | 341 | ||
| 342 | 342 | procedure TForm1.ApplicationProperties1Idle(Sender: TObject; var Done: Boolean); | |
| 343 | 343 | var | |
| 344 | 344 | fileDesc: integer; | |
| 345 | 345 | buttonStatus: string[1] = '1'; | |
| 346 | 346 | begin | |
| 347 | 347 | try | |
| 348 | 348 | { Open SoC pin 18 (pin 12 on GPIO port) in read-only mode: } | |
| 349 | 349 | fileDesc := fpopen('/sys/class/gpio/gpio18/value', O_RdOnly); | |
| 350 | 350 | if fileDesc > 0 then | |
| 351 | 351 | begin | |
| 352 | 352 | { Read status of this pin (0: button pressed, 1: button released): } | |
| 353 | 353 | gReturnCode := fpread(fileDesc, buttonStatus[1], 1); | |
| 354 | 354 | LogMemo.Lines.Add(IntToStr(gReturnCode) + ': ' + buttonStatus); | |
| 355 | 355 | LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1; | |
| 356 | 356 | if buttonStatus = '0' then | |
| 357 | 357 | GPIO18CheckBox.Checked := true | |
| 358 | 358 | else | |
| 359 | 359 | GPIO18CheckBox.Checked := false; | |
| 360 | 360 | end; | |
| 361 | 361 | finally | |
| 362 | 362 | { Close SoC pin 18 (pin 12 on GPIO port) } | |
| 363 | 363 | gReturnCode := fpclose(fileDesc); | |
| 364 | 364 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 365 | 365 | LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1; | |
| 366 | 366 | end; | |
| 367 | 367 | end; | |
| 368 | 368 | ||
| 369 | 369 | procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); | |
| 370 | 370 | var | |
| 371 | 371 | fileDesc: integer; | |
| 372 | 372 | begin | |
| 373 | 373 | { Free SoC pin 18: } | |
| 374 | 374 | try | |
| 375 | 375 | fileDesc := fpopen('/sys/class/gpio/unexport', O_WrOnly); | |
| 376 | 376 | gReturnCode := fpwrite(fileDesc, PIN_18[0], 2); | |
| 377 | 377 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 378 | 378 | finally | |
| 379 | 379 | gReturnCode := fpclose(fileDesc); | |
| 380 | 380 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 381 | 381 | end; | |
| 382 | 382 | end; | |
| 383 | 383 | ||
| 384 | 384 | end. | |
| 385 | 385 | ``` | |
| 386 | 386 | ||
| 387 | 387 | 上面的例子主程序是相同的,。 | |
| 388 | 388 | ||
| 389 | 389 | ### 2. 通过封装的外壳访问硬件 | |
| 390 | 390 | ||
| 391 | 391 | 访问硬件的另一种方法是通过封装终端命令。这是通过使用[[Executing External Programs|fpsystem]]功能。这种方法可以访问baseunix单元不支持的功能。下面的代码实现了一个具有相同功能的程序,该程序是从上面的第一个清单所产生的。 | |
| 392 | 392 | ||
| 393 | 393 | *控制单元:* | |
| 394 | 394 | ||
| 395 | 395 | ``` pascal | |
| 396 | 396 | unit Unit1; | |
| 397 | 397 | ||
| 398 | 398 | {Demo application for GPIO on Raspberry Pi} | |
| 399 | 399 | {Inspired by the Python input/output demo application by Gareth Halfacree} | |
| 400 | 400 | {written for the Raspberry Pi User Guide, ISBN 978-1-118-46446-5} | |
| 401 | 401 | ||
| 402 | 402 | {$mode objfpc}{$H+} | |
| 403 | 403 | ||
| 404 | 404 | interface | |
| 405 | 405 | ||
| 406 | 406 | uses | |
| 407 | 407 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Unix; | |
| 408 | 408 | ||
| 409 | 409 | type | |
| 410 | 410 | ||
| 411 | 411 | { TForm1 } | |
| 412 | 412 | ||
| 413 | 413 | TForm1 = class(TForm) | |
| 414 | 414 | LogMemo: TMemo; | |
| 415 | 415 | GPIO17ToggleBox: TToggleBox; | |
| 416 | 416 | procedure FormActivate(Sender: TObject); | |
| 417 | 417 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); | |
| 418 | 418 | procedure GPIO17ToggleBoxChange(Sender: TObject); | |
| 419 | 419 | private | |
| 420 | 420 | { private declarations } | |
| 421 | 421 | public | |
| 422 | 422 | { public declarations } | |
| 423 | 423 | end; | |
| 424 | 424 | ||
| 425 | 425 | var | |
| 426 | 426 | Form1: TForm1; | |
| 427 | 427 | gReturnCode: longint; {stores the result of the IO operation} | |
| 428 | 428 | ||
| 429 | 429 | implementation | |
| 430 | 430 | ||
| 431 | 431 | {$R *.lfm} | |
| 432 | 432 | ||
| 433 | 433 | { TForm1 } | |
| 434 | 434 | ||
| 435 | 435 | procedure TForm1.FormActivate(Sender: TObject); | |
| 436 | 436 | begin | |
| 437 | 437 | { Prepare SoC pin 17 (pin 11 on GPIO port) for access: } | |
| 438 | 438 | gReturnCode := fpsystem('echo "17" > /sys/class/gpio/export'); | |
| 439 | 439 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 440 | 440 | { Set SoC pin 17 as output: } | |
| 441 | 441 | gReturnCode := fpsystem('echo "out" > /sys/class/gpio/gpio17/direction'); | |
| 442 | 442 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 443 | 443 | end; | |
| 444 | 444 | ||
| 445 | 445 | procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); | |
| 446 | 446 | begin | |
| 447 | 447 | { Free SoC pin 17: } | |
| 448 | 448 | gReturnCode := fpsystem('echo "17" > /sys/class/gpio/unexport'); | |
| 449 | 449 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 450 | 450 | end; | |
| 451 | 451 | ||
| 452 | 452 | procedure TForm1.GPIO17ToggleBoxChange(Sender: TObject); | |
| 453 | 453 | begin | |
| 454 | 454 | if GPIO17ToggleBox.Checked then | |
| 455 | 455 | begin | |
| 456 | 456 | { Swith SoC pin 17 on: } | |
| 457 | 457 | gReturnCode := fpsystem('echo "1" > /sys/class/gpio/gpio17/value'); | |
| 458 | 458 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 459 | 459 | end | |
| 460 | 460 | else | |
| 461 | 461 | begin | |
| 462 | 462 | { Switch SoC pin 17 off: } | |
| 463 | 463 | gReturnCode := fpsystem('echo "0" > /sys/class/gpio/gpio17/value'); | |
| 464 | 464 | LogMemo.Lines.Add(IntToStr(gReturnCode)); | |
| 465 | 465 | end; | |
| 466 | 466 | end; | |
| 467 | 467 | ||
| 468 | 468 | end. | |
| 469 | 469 | ``` | |
| 470 | 470 | ||
| 471 | 471 | 主程序与上面的例子相同。这个程序必须用根权限执行。 | |
| 472 | 472 | ||
| 473 | 473 | ### 3. wiringPi方法与函数 | |
| 474 | 474 | ||
| 475 | 475 | Alex Schaller封装的Gordon Henderson的Arduino兼容wiringpi库,提供了一种类似Arduino电路板的编码方案。 | |
| 476 | 476 | ||
| 477 | 477 | **Function wiringPiSetup:longint:** 使用wiringpi引脚编号方案初始化wiringpi系统 | |
| 478 | 478 | ||
| 479 | 479 | **Procedure wiringPiGpioMode(mode:longint):** 用Broadcom的GPIO引脚编号方案wiringpi初始化系统。 | |
| 480 | 480 | ||
| 481 | 481 | **Procedure pullUpDnControl(pin:longint;** pud:longint): 控制内部GPIO引脚上拉/下拉电阻。 | |
| 482 | 482 | ||
| 483 | 483 | **Procedure pinMode(pin:longint; mode:longint):** 设置一个PIN的模式:输入、输出或pwm_output。 | |
| 484 | 484 | ||
| 485 | 485 | **Procedure digitalWrite(pin:longint; value:longint):** 设置一个输出位。 | |
| 486 | 486 | ||
| 487 | 487 | **Procedure pwmWrite(pin:longint; value:longint):** 设置一个输出PWM(脉宽调制)值,0到1024之间。 | |
| 488 | 488 | ||
| 489 | 489 | **Function digitalRead(pin:longint):longint:** 读取一个给定引脚的值,返回1或0。 | |
| 490 | 490 | ||
| 491 | 491 | **Procedure delay(howLong:dword):** 等待多少毫秒。 | |
| 492 | 492 | ||
| 493 | 493 | **Procedure delayMicroseconds(howLong:dword):** 等待多少微秒。 | |
| 494 | 494 | ||
| 495 | 495 | **Function millis:dword:** 返回程序调用wiringpisetup函数花费的毫秒数。 | |
| 496 | 496 | ||
| 497 | 497 | ### 4. rpi_hal-hardware抽象库 (GPIO, I2C and SPI 函数与方法) | |
| 498 | 498 | ||
| 499 | 499 | 这个单元大约1700行代码,由Stefan Fischer提供,提供程序和函数访问RPI硬件I2C,SPI和GPIO: | |
| 500 | 500 | ||
| 501 | 501 | 下面是函数和方法的摘录: | |
| 502 | 502 | ||
| 503 | 503 | **procedure gpio_set_pin (pin:longword;highlevel:boolean);** { 设置 RPi GPIO 引脚速率为高或低; 速率 @ 700MHz -\> 0.65MHz } | |
| 504 | 504 | ||
| 505 | 505 | **function gpio_get_PIN (pin:longword):boolean;** { 获取 RPi GPIO 引脚等级,当Pin为'1'时返回True,为'0'时返回false; 速率 @ 700MHz -\> 1.17MHz } | |
| 506 | 506 | ||
| 507 | 507 | **procedure gpio_set_input (pin:longword);** { 设置 RPi GPIO 引脚为直接输入} | |
| 508 | 508 | ||
| 509 | 509 | **procedure gpio_set_output(pin:longword);** { 设置 RPi GPIO 引脚为直接输出 } | |
| 510 | 510 | ||
| 511 | 511 | **procedure gpio_set_alt (pin,altfunc:longword);** { 设置 RPi GPIO 引脚为备用功能 0..5 } | |
| 512 | 512 | ||
| 513 | 513 | **procedure gpio_set_gppud (mask:longword);** { 设置RPI GPIO拉上/下登记(gppud)} | |
| 514 | 514 | ||
| 515 | 515 | ... | |
| 516 | 516 | ||
| 517 | 517 | **function rpi_snr :string;** { 信噪比: 0000000012345678 } | |
| 518 | 518 | ||
| 519 | 519 | **function rpi_hw :string;** { 处理器类型: BCM2708 } | |
| 520 | 520 | ||
| 521 | 521 | **function rpi_proc:string;** { ARMv6-compatible processor rev 7 (v6l) } | |
| 522 | 522 | ||
| 523 | 523 | ... | |
| 524 | 524 | ||
| 525 | 525 | **function i2c_bus_write(baseadr,reg:word; var <data:databuf_t>; lgt:byte; testnr:integer) : integer;** | |
| 526 | 526 | ||
| 527 | 527 | **function i2c_bus_read (baseadr,reg:word; var <data:databuf_t>; lgt:byte; testnr:integer) : integer;** | |
| 528 | 528 | ||
| 529 | 529 | **function i2c_string_read(baseadr,reg:word; var <data:databuf_t>; lgt:byte; testnr:integer) : string;** | |
| 530 | 530 | ||
| 531 | 531 | **function i2c_string_write(baseadr,reg:word; s:string; testnr:integer) : integer;** | |
| 532 | 532 | ||
| 533 | 533 | ... | |
| 534 | 534 | ||
| 535 | 535 | **procedure SPI_Write(devnum:byte; reg,data:word);** | |
| 536 | 536 | ||
| 537 | 537 | **function SPI_Read(devnum:byte; reg:word) : byte;** | |
| 538 | 538 | ||
| 539 | 539 | **procedure SPI_BurstRead2Buffer (devnum,start_reg:byte; xferlen:longword);** | |
| 540 | 540 | ||
| 541 | 541 | **procedure SPI_BurstWriteBuffer (devnum,start_reg:byte; xferlen:longword);** { 从缓冲区SPI的地址写入一段字节的内容 } | |
| 542 | 542 | ||
| 543 | 543 | ... | |
| 544 | 544 | ||
| 545 | 545 | *测试程序 (testrpi.pas):* | |
| 546 | 546 | ||
| 547 | 547 | ``` pascal | |
| 548 | 548 | //Simple Test program, which is using rpi_hal; | |
| 549 | 549 | ||
| 550 | 550 | program testrpi; | |
| 551 | 551 | uses rpi_hal; | |
| 552 | 552 | begin | |
| 553 | 553 | writeln('Show CPU-Info, RPI-HW-Info and Registers:'); | |
| 554 | 554 | rpi_show_all_info; | |
| 555 | 555 | writeln('Let Status LED Blink. Using GPIO functions:'); | |
| 556 | 556 | GPIO_PIN_TOGGLE_TEST; | |
| 557 | 557 | writeln('Test SPI Read function. (piggy back board with installed RFM22B Module is required!)'); | |
| 558 | 558 | Test_SPI; | |
| 559 | 559 | end. | |
| 560 | 560 | ``` | |
| 561 | 561 | ||
| 562 | 562 | ### 5. 底层pascal单元 PiGpio(代替wiringpi C语言库的GPIO控制) | |
| 563 | 563 | ||
| 564 | 564 | *这个单元 (pigpio.pas[2](https://docs.google.com/file/d/0B-b-5ooIWPvGN1BIcEJzRDgyeUE/edit?usp=sharing)) 270 行代码是由Gabor Szollosi提供的 , 运行速度飞快(例如GPIO引脚输出开关频率达到8MHz) :* | |
| 565 | 565 | ||
| 566 | 566 | ``` pascal | |
| 567 | 567 | unit PiGpio; | |
| 568 | 568 | { | |
| 569 | 569 | BCM2835 GPIO Registry Driver, also can use to manipulate cpu other registry areas | |
| 570 | 570 | ||
| 571 | 571 | This code is tested only Broadcom bcm2835 cpu, different arm cpus may need different | |
| 572 | 572 | gpio driver implementation | |
| 573 | 573 | ||
| 574 | 574 | 2013 Gabor Szollosi | |
| 575 | 575 | } | |
| 576 | 576 | {$mode objfpc}{$H+} | |
| 577 | 577 | ||
| 578 | 578 | interface | |
| 579 | 579 | ||
| 580 | 580 | uses | |
| 581 | 581 | Classes, SysUtils; | |
| 582 | 582 | ||
| 583 | 583 | const | |
| 584 | 584 | REG_GPIO = $20000;//bcm2835 gpio register 0x2000 0000. new fpMap uses page offset, one page is 4096bytes | |
| 585 | 585 | // hex 0x1000 so simply calculate 0x2000 0000 / 0x1000 = 0x2000 0 | |
| 586 | 586 | PAGE_SIZE = 4096; | |
| 587 | 587 | BLOCK_SIZE = 4096; | |
| 588 | 588 | // The BCM2835 has 54 GPIO pins. | |
| 589 | 589 | // BCM2835 data sheet, Page 90 onwards. | |
| 590 | 590 | // There are 6 control registers, each control the functions of a block | |
| 591 | 591 | // of 10 pins. | |
| 592 | 592 | ||
| 593 | 593 | CLOCK_BASE = (REG_GPIO + $101); | |
| 594 | 594 | GPIO_BASE = (REG_GPIO + $200); | |
| 595 | 595 | GPIO_PWM = (REG_GPIO + $20C); | |
| 596 | 596 | ||
| 597 | 597 | INPUT = 0; | |
| 598 | 598 | OUTPUT = 1; | |
| 599 | 599 | PWM_OUTPUT = 2; | |
| 600 | 600 | LOW = False; | |
| 601 | 601 | HIGH = True; | |
| 602 | 602 | PUD_OFF = 0; | |
| 603 | 603 | PUD_DOWN = 1; | |
| 604 | 604 | PUD_UP = 2; | |
| 605 | 605 | ||
| 606 | 606 | // PWM | |
| 607 | 607 | ||
| 608 | 608 | PWM_CONTROL = 0; | |
| 609 | 609 | PWM_STATUS = 4; | |
| 610 | 610 | PWM0_RANGE = 16; | |
| 611 | 611 | PWM0_DATA = 20; | |
| 612 | 612 | PWM1_RANGE = 32; | |
| 613 | 613 | PWM1_DATA = 36; | |
| 614 | 614 | ||
| 615 | 615 | PWMCLK_CNTL = 160; | |
| 616 | 616 | PWMCLK_DIV = 164; | |
| 617 | 617 | ||
| 618 | 618 | PWM1_MS_MODE = $8000; // Run in MS mode | |
| 619 | 619 | PWM1_USEFIFO = $2000; // Data from FIFO | |
| 620 | 620 | PWM1_REVPOLAR = $1000; // Reverse polarity | |
| 621 | 621 | PWM1_OFFSTATE = $0800; // Ouput Off state | |
| 622 | 622 | PWM1_REPEATFF = $0400; // Repeat last value if FIFO empty | |
| 623 | 623 | PWM1_SERIAL = $0200; // Run in serial mode | |
| 624 | 624 | PWM1_ENABLE = $0100; // Channel Enable | |
| 625 | 625 | ||
| 626 | 626 | PWM0_MS_MODE = $0080; // Run in MS mode | |
| 627 | 627 | PWM0_USEFIFO = $0020; // Data from FIFO | |
| 628 | 628 | PWM0_REVPOLAR = $0010; // Reverse polarity | |
| 629 | 629 | PWM0_OFFSTATE = $0008; // Ouput Off state | |
| 630 | 630 | PWM0_REPEATFF = $0004; // Repeat last value if FIFO empty | |
| 631 | 631 | PWM0_SERIAL = $0002; // Run in serial mode | |
| 632 | 632 | PWM0_ENABLE = $0001; // Channel Enable | |
| 633 | 633 | ||
| 634 | 634 | type | |
| 635 | 635 | ||
| 636 | 636 | { TIoPort } | |
| 637 | 637 | ||
| 638 | 638 | TIoPort = class // IO bank object | |
| 639 | 639 | private // | |
| 640 | 640 | ||
| 641 | 641 | //function get_pinDirection(aPin: TGpIoPin): TGpioPinConf; | |
| 642 | 642 | ||
| 643 | 643 | public | |
| 644 | 644 | FGpio: ^LongWord; | |
| 645 | 645 | FClk: ^LongWord; | |
| 646 | 646 | FPwm: ^LongWord; | |
| 647 | 647 | procedure SetPinMode(gpin, mode: byte); | |
| 648 | 648 | function GetBit(gpin : byte):boolean;inline; // gets pin bit} | |
| 649 | 649 | procedure ClearBit(gpin : byte);inline;// write pin to 0 | |
| 650 | 650 | procedure SetBit(gpin : byte);inline;// write pin to 1 | |
| 651 | 651 | procedure SetPullMode(gpin, mode: byte); | |
| 652 | 652 | procedure PwmWrite(gpin : byte; value : LongWord);inline;// write pin to pwm value | |
| 653 | 653 | end; | |
| 654 | 654 | ||
| 655 | 655 | { TIoDriver } | |
| 656 | 656 | ||
| 657 | 657 | TIoDriver = class | |
| 658 | 658 | private | |
| 659 | 659 | ||
| 660 | 660 | public | |
| 661 | 661 | destructor Destroy;override; | |
| 662 | 662 | function MapIo:boolean;// creates io memory mapping | |
| 663 | 663 | procedure UnmapIoRegisrty(FMap: TIoPort);// close io memory mapping | |
| 664 | 664 | function CreatePort(PortGpio, PortClk, PortPwm: LongWord):TIoPort; // create new IO port | |
| 665 | 665 | end; | |
| 666 | 666 | ||
| 667 | 667 | var | |
| 668 | 668 | ||
| 669 | 669 | fd: integer;// /dev/mem file handle | |
| 670 | 670 | procedure delayNanoseconds (howLong : LongWord); | |
| 671 | 671 | ||
| 672 | 672 | implementation | |
| 673 | 673 | ||
| 674 | 674 | uses | |
| 675 | 675 | baseUnix, Unix; | |
| 676 | 676 | ||
| 677 | 677 | procedure delayNanoseconds (howLong : LongWord); | |
| 678 | 678 | var | |
| 679 | 679 | sleeper, dummy : timespec; | |
| 680 | 680 | begin | |
| 681 | 681 | sleeper.tv_sec := 0 ; | |
| 682 | 682 | sleeper.tv_nsec := howLong ; | |
| 683 | 683 | fpnanosleep (@sleeper,@dummy) ; | |
| 684 | 684 | end; | |
| 685 | 685 | { TIoDriver } | |
| 686 | 686 | //******************************************************************************* | |
| 687 | 687 | destructor TIoDriver.Destroy; | |
| 688 | 688 | begin | |
| 689 | 689 | inherited Destroy; | |
| 690 | 690 | end; | |
| 691 | 691 | //******************************************************************************* | |
| 692 | 692 | function TIoDriver.MapIo: boolean; | |
| 693 | 693 | begin | |
| 694 | 694 | Result := True; | |
| 695 | 695 | fd := fpopen('/dev/mem', O_RdWr or O_Sync); // Open the master /dev/memory device | |
| 696 | 696 | if fd < 0 then | |
| 697 | 697 | begin | |
| 698 | 698 | Result := False; // unsuccessful memory mapping | |
| 699 | 699 | end; | |
| 700 | 700 | // | |
| 701 | 701 | end; | |
| 702 | 702 | //******************************************************************************* | |
| 703 | 703 | procedure TIoDriver.UnmapIoRegisrty(FMap:TIoPort); | |
| 704 | 704 | begin | |
| 705 | 705 | if FMap.FGpio <> nil then | |
| 706 | 706 | begin | |
| 707 | 707 | fpMUnmap(FMap.FGpio,PAGE_SIZE); | |
| 708 | 708 | FMap.FGpio := Nil; | |
| 709 | 709 | end; | |
| 710 | 710 | if FMap.FClk <> nil then | |
| 711 | 711 | begin | |
| 712 | 712 | fpMUnmap(FMap.FClk,PAGE_SIZE); | |
| 713 | 713 | FMap.FClk := Nil; | |
| 714 | 714 | end; | |
| 715 | 715 | if FMap.FPwm <> nil then | |
| 716 | 716 | begin | |
| 717 | 717 | fpMUnmap(FMap.FPwm ,PAGE_SIZE); | |
| 718 | 718 | FMap.FPwm := Nil; | |
| 719 | 719 | end; | |
| 720 | 720 | end; | |
| 721 | 721 | //******************************************************************************* | |
| 722 | 722 | function TIoDriver.CreatePort(PortGpio, PortClk, PortPwm: LongWord): TIoPort; | |
| 723 | 723 | begin | |
| 724 | 724 | Result := TIoPort.Create;// new io port, pascal calls new fpMap, where offst is page sized 4096 bytes!!! | |
| 725 | 725 | Result.FGpio := FpMmap(Nil, PAGE_SIZE, PROT_READ or PROT_WRITE, MAP_SHARED, fd, PortGpio); // port config gpio memory | |
| 726 | 726 | Result.FClk:= FpMmap(Nil, PAGE_SIZE, PROT_READ or PROT_WRITE, MAP_SHARED, fd, PortClk);; // port clk | |
| 727 | 727 | Result.FPwm:= FpMmap(Nil, PAGE_SIZE, PROT_READ or PROT_WRITE, MAP_SHARED, fd, PortPwm);; // port pwm | |
| 728 | 728 | end; | |
| 729 | 729 | //******************************************************************************* | |
| 730 | 730 | procedure TIoPort.SetPinMode(gpin, mode: byte); | |
| 731 | 731 | var | |
| 732 | 732 | fSel, shift, alt : byte; | |
| 733 | 733 | gpiof, clkf, pwmf : ^LongWord; | |
| 734 | 734 | begin | |
| 735 | 735 | fSel := (gpin div 10)*4 ; //Select Gpfsel 0 to 5 register | |
| 736 | 736 | shift := (gpin mod 10)*3 ; //0-9 pin shift | |
| 737 | 737 | gpiof := Pointer(LongWord(Self.FGpio)+fSel); | |
| 738 | 738 | if (mode = INPUT) then | |
| 739 | 739 | gpiof^ := gpiof^ and ($FFFFFFFF - (7 shl shift)) //7 shl shift komplemens - Sets bits to zero = input | |
| 740 | 740 | else if (mode = OUTPUT) then | |
| 741 | 741 | begin | |
| 742 | 742 | gpiof^ := gpiof^ and ($FFFFFFFF - (7 shl shift)) or (1 shl shift); | |
| 743 | 743 | end | |
| 744 | 744 | else if (mode = PWM_OUTPUT) then | |
| 745 | 745 | begin | |
| 746 | 746 | Case gpin of | |
| 747 | 747 | 12,13,40,41,45 : alt:= 4 ; | |
| 748 | 748 | 18,19 : alt:= 2 ; | |
| 749 | 749 | else alt:= 0 ; | |
| 750 | 750 | end; | |
| 751 | 751 | If alt > 0 then | |
| 752 | 752 | begin | |
| 753 | 753 | gpiof^ := gpiof^ and ($FFFFFFFF - (7 shl shift)) or (alt shl shift); | |
| 754 | 754 | clkf := Pointer(LongWord(Self.FClk)+PWMCLK_CNTL); | |
| 755 | 755 | clkf^ := $5A000011 or (1 shl 5) ; //stop clock | |
| 756 | 756 | delayNanoseconds(200); | |
| 757 | 757 | clkf := Pointer(LongWord(Self.FClk)+PWMCLK_DIV); | |
| 758 | 758 | clkf^ := $5A000000 or (32 shl 12) ; // set pwm clock div to 32 (19.2/3 = 600KHz) | |
| 759 | 759 | clkf := Pointer(LongWord(Self.FClk)+PWMCLK_CNTL); | |
| 760 | 760 | clkf^ := $5A000011 ; //start clock | |
| 761 | 761 | Self.ClearBit(gpin); | |
| 762 | 762 | pwmf := Pointer(LongWord(Self.FPwm)+PWM_CONTROL); | |
| 763 | 763 | pwmf^ := 0 ; // Disable PWM | |
| 764 | 764 | delayNanoseconds(200); | |
| 765 | 765 | pwmf := Pointer(LongWord(Self.FPwm)+PWM0_RANGE); | |
| 766 | 766 | pwmf^ := $400 ; //max: 1023 | |
| 767 | 767 | delayNanoseconds(200); | |
| 768 | 768 | pwmf := Pointer(LongWord(Self.FPwm)+PWM1_RANGE); | |
| 769 | 769 | pwmf^ := $400 ; //max: 1023 | |
| 770 | 770 | delayNanoseconds(200); | |
| 771 | 771 | // Enable PWMs | |
| 772 | 772 | pwmf := Pointer(LongWord(Self.FPwm)+PWM0_DATA); | |
| 773 | 773 | pwmf^ := 0 ; //start value | |
| 774 | 774 | pwmf := Pointer(LongWord(Self.FPwm)+PWM1_DATA); | |
| 775 | 775 | pwmf^ := 0 ; //start value | |
| 776 | 776 | pwmf := Pointer(LongWord(Self.FPwm)+PWM_CONTROL); | |
| 777 | 777 | pwmf^ := PWM0_ENABLE or PWM1_ENABLE ; | |
| 778 | 778 | end; | |
| 779 | 779 | end; | |
| 780 | 780 | end; | |
| 781 | 781 | //******************************************************************************* | |
| 782 | 782 | procedure TIoPort.SetBit(gpin : byte); | |
| 783 | 783 | var | |
| 784 | 784 | gpiof : ^LongWord; | |
| 785 | 785 | begin | |
| 786 | 786 | gpiof := Pointer(LongWord(Self.FGpio) + 28 + (gpin shr 5) shl 2); | |
| 787 | 787 | gpiof^ := 1 shl gpin; | |
| 788 | 788 | end; | |
| 789 | 789 | //******************************************************************************* | |
| 790 | 790 | procedure TIoPort.ClearBit(gpin : byte); | |
| 791 | 791 | var | |
| 792 | 792 | gpiof : ^LongWord; | |
| 793 | 793 | begin | |
| 794 | 794 | gpiof := Pointer(LongWord(Self.FGpio) + 40 + (gpin shr 5) shl 2); | |
| 795 | 795 | gpiof^ := 1 shl gpin; | |
| 796 | 796 | end; | |
| 797 | 797 | //******************************************************************************* | |
| 798 | 798 | function TIoPort.GetBit(gpin : byte):boolean; | |
| 799 | 799 | var | |
| 800 | 800 | gpiof : ^LongWord; | |
| 801 | 801 | begin | |
| 802 | 802 | gpiof := Pointer(LongWord(Self.FGpio) + 52 + (gpin shr 5) shl 2); | |
| 803 | 803 | if (gpiof^ and (1 shl gpin)) = 0 then Result := False else Result := True; | |
| 804 | 804 | end; | |
| 805 | 805 | //******************************************************************************* | |
| 806 | 806 | procedure TIoPort.SetPullMode(gpin, mode: byte); | |
| 807 | 807 | var | |
| 808 | 808 | pudf, pudclkf : ^LongWord; | |
| 809 | 809 | begin | |
| 810 | 810 | pudf := Pointer(LongWord(Self.FGpio) + 148 ); | |
| 811 | 811 | pudf^ := mode; //mode = 0, 1, 2 :Off, Down, Up | |
| 812 | 812 | delayNanoseconds(200); | |
| 813 | 813 | pudclkf := Pointer(LongWord(Self.FGpio) + 152 + (gpin shr 5) shl 2); | |
| 814 | 814 | pudclkf^ := 1 shl gpin ; | |
| 815 | 815 | delayNanoseconds(200); | |
| 816 | 816 | pudf^ := 0 ; | |
| 817 | 817 | pudclkf^ := 0 ; | |
| 818 | 818 | end; | |
| 819 | 819 | //******************************************************************************* | |
| 820 | 820 | procedure TIoPort.PwmWrite(gpin : byte; value : Longword); | |
| 821 | 821 | var | |
| 822 | 822 | pwmf : ^LongWord; | |
| 823 | 823 | port : byte; | |
| 824 | 824 | begin | |
| 825 | 825 | Case gpin of | |
| 826 | 826 | 12,18,40 : port:= PWM0_DATA ; | |
| 827 | 827 | 13,19,41,45 : port:= PWM1_DATA ; | |
| 828 | 828 | else exit; | |
| 829 | 829 | end; | |
| 830 | 830 | pwmf := Pointer(LongWord(Self.FPwm) + port); | |
| 831 | 831 | pwmf^ := value and $FFFFFBFF; // $400 complemens | |
| 832 | 832 | end; | |
| 833 | 833 | //******************************************************************************* | |
| 834 | 834 | end. | |
| 835 | 835 | ``` | |
| 836 | 836 | ||
| 837 | 837 | *Controlling Lazarus unit:(Project files[3](https://drive.google.com/folderview?id=0B-b-5ooIWPvGU043ZTZ0cUc2YkE&usp=sharing))* | |
| 838 | 838 | ||
| 839 | 839 | ``` pascal | |
| 840 | 840 | unit Unit1; | |
| 841 | 841 | ||
| 842 | 842 | {Demo application for GPIO on Raspberry Pi} | |
| 843 | 843 | {Inspired by the Python input/output demo application by Gareth Halfacree} | |
| 844 | 844 | {written for the Raspberry Pi User Guide, ISBN 978-1-118-46446-5} | |
| 845 | 845 | ||
| 846 | 846 | {$mode objfpc}{$H+} | |
| 847 | 847 | ||
| 848 | 848 | interface | |
| 849 | 849 | ||
| 850 | 850 | uses | |
| 851 | 851 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, | |
| 852 | 852 | ExtCtrls, Unix, PiGpio; | |
| 853 | 853 | ||
| 854 | 854 | type | |
| 855 | 855 | ||
| 856 | 856 | { TForm1 } | |
| 857 | 857 | ||
| 858 | 858 | TForm1 = class(TForm) | |
| 859 | 859 | GPIO25In: TButton; | |
| 860 | 860 | SpeedButton: TButton; | |
| 861 | 861 | LogMemo: TMemo; | |
| 862 | 862 | GPIO23switch: TToggleBox; | |
| 863 | 863 | Timer1: TTimer; | |
| 864 | 864 | GPIO18Pwm: TToggleBox; | |
| 865 | 865 | Direction: TToggleBox; | |
| 866 | 866 | procedure DirectionChange(Sender: TObject); | |
| 867 | 867 | procedure GPIO18PwmChange(Sender: TObject); | |
| 868 | 868 | procedure GPIO25InClick(Sender: TObject); | |
| 869 | 869 | procedure FormActivate(Sender: TObject); | |
| 870 | 870 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); | |
| 871 | 871 | procedure GPIO23switchChange(Sender: TObject); | |
| 872 | 872 | procedure SpeedButtonClick(Sender: TObject); | |
| 873 | 873 | procedure Timer1Timer(Sender: TObject); | |
| 874 | 874 | private | |
| 875 | 875 | { private declarations } | |
| 876 | 876 | public | |
| 877 | 877 | { public declarations } | |
| 878 | 878 | end; | |
| 879 | 879 | ||
| 880 | 880 | const | |
| 881 | 881 | INPUT = 0; | |
| 882 | 882 | OUTPUT = 1; | |
| 883 | 883 | PWM_OUTPUT = 2; | |
| 884 | 884 | LOW = False; | |
| 885 | 885 | HIGH = True; | |
| 886 | 886 | PUD_OFF = 0; | |
| 887 | 887 | PUD_DOWN = 1; | |
| 888 | 888 | PUD_UP = 2; | |
| 889 | 889 | // Convert Raspberry Pi P1 pins (Px) to GPIO port | |
| 890 | 890 | P3 = 0; | |
| 891 | 891 | P5 = 1; | |
| 892 | 892 | P7 = 4; | |
| 893 | 893 | P8 = 14; | |
| 894 | 894 | P10 = 15; | |
| 895 | 895 | P11 = 17; | |
| 896 | 896 | P12 = 18; | |
| 897 | 897 | P13 = 21; | |
| 898 | 898 | P15 = 22; | |
| 899 | 899 | P16 = 23; | |
| 900 | 900 | P18 = 24; | |
| 901 | 901 | P19 = 10; | |
| 902 | 902 | P21 = 9; | |
| 903 | 903 | P22 = 25; | |
| 904 | 904 | P23 = 11; | |
| 905 | 905 | P24 = 8; | |
| 906 | 906 | P26 = 7; | |
| 907 | 907 | ||
| 908 | 908 | var | |
| 909 | 909 | Form1: TForm1; | |
| 910 | 910 | GPIO_Driver: TIoDriver; | |
| 911 | 911 | GpF: TIoPort; | |
| 912 | 912 | PWM :Boolean; | |
| 913 | 913 | i, d : integer; | |
| 914 | 914 | Pin,Pout,Ppwm : byte; | |
| 915 | 915 | ||
| 916 | 916 | implementation | |
| 917 | 917 | ||
| 918 | 918 | {$R *.lfm} | |
| 919 | 919 | ||
| 920 | 920 | { TForm1 } | |
| 921 | 921 | ||
| 922 | 922 | procedure TForm1.FormActivate(Sender: TObject); | |
| 923 | 923 | begin | |
| 924 | 924 | if not GPIO_Driver.MapIo then | |
| 925 | 925 | begin | |
| 926 | 926 | LogMemo.Lines.Add('Error mapping gpio registry'); | |
| 927 | 927 | end | |
| 928 | 928 | else | |
| 929 | 929 | begin | |
| 930 | 930 | GpF := GpIo_Driver.CreatePort(GPIO_BASE, CLOCK_BASE, GPIO_PWM); | |
| 931 | 931 | end ; | |
| 932 | 932 | Timer1.Enabled:= True; | |
| 933 | 933 | Timer1.Interval:= 25; //25 ms controll interval | |
| 934 | 934 | Pin := P22; | |
| 935 | 935 | Pout := P16; | |
| 936 | 936 | Ppwm := P12; | |
| 937 | 937 | i:=1; | |
| 938 | 938 | GpF.SetPinMode(Pout,OUTPUT); | |
| 939 | 939 | GpF.SetPinMode(Pin,INPUT); | |
| 940 | 940 | GpF.SetPullMode(Pin,PUD_Up); // Input PullUp High level | |
| 941 | 941 | end; | |
| 942 | 942 | ||
| 943 | 943 | procedure TForm1.GPIO25InClick(Sender: TObject); | |
| 944 | 944 | begin | |
| 945 | 945 | If GpF.GetBit(Pin) then LogMemo.Lines.Add('In: '+IntToStr(1)) | |
| 946 | 946 | else LogMemo.Lines.Add('In: '+IntToStr(0)); | |
| 947 | 947 | end; | |
| 948 | 948 | ||
| 949 | 949 | procedure TForm1.GPIO18PwmChange(Sender: TObject); | |
| 950 | 950 | begin | |
| 951 | 951 | if GPIO18Pwm.Checked then | |
| 952 | 952 | begin | |
| 953 | 953 | GpF.SetPinMode(Ppwm,PWM_OUTPUT); | |
| 954 | 954 | PWM := True; //PWM on | |
| 955 | 955 | end | |
| 956 | 956 | else | |
| 957 | 957 | begin | |
| 958 | 958 | GpF.SetPinMode(Ppwm,INPUT); | |
| 959 | 959 | PWM := False; //PWM off | |
| 960 | 960 | end; | |
| 961 | 961 | end; | |
| 962 | 962 | ||
| 963 | 963 | procedure TForm1.DirectionChange(Sender: TObject); | |
| 964 | 964 | begin | |
| 965 | 965 | if Direction.Checked then d:=10 else d:=-10; | |
| 966 | 966 | end; | |
| 967 | 967 | ||
| 968 | 968 | ||
| 969 | 969 | procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); | |
| 970 | 970 | begin | |
| 971 | 971 | GpF.SetPinMode(Ppwm,INPUT); | |
| 972 | 972 | GpF.ClearBit(Pout); | |
| 973 | 973 | GpIo_Driver.UnmapIoRegisrty(GpF); | |
| 974 | 974 | end; | |
| 975 | 975 | ||
| 976 | 976 | procedure TForm1.GPIO23switchChange(Sender: TObject); | |
| 977 | 977 | ||
| 978 | 978 | Begin | |
| 979 | 979 | Timer1.Enabled := False; | |
| 980 | 980 | if GPIO23switch.Checked then | |
| 981 | 981 | begin | |
| 982 | 982 | GpF.SetBit(Pout); //Turn LED on | |
| 983 | 983 | end | |
| 984 | 984 | else | |
| 985 | 985 | begin | |
| 986 | 986 | GpF.ClearBit(Pout); //Turn LED off | |
| 987 | 987 | end; | |
| 988 | 988 | Timer1.Enabled := True; | |
| 989 | 989 | end; | |
| 990 | 990 | ||
| 991 | 991 | procedure TForm1.SpeedButtonClick(Sender: TObject); | |
| 992 | 992 | var | |
| 993 | 993 | i,p,k,v: longint; | |
| 994 | 994 | ido:TDateTime; | |
| 995 | 995 | begin | |
| 996 | 996 | ido:= Time; | |
| 997 | 997 | k:= TimeStampToMSecs(DateTimeToTimeStamp(ido)); | |
| 998 | 998 | LogMemo.Lines.Add('Start: '+TimeToStr(ido)); | |
| 999 | 999 | p:=10000000 ; | |
| 1000 | 1000 | For i:=1 to p do Begin | |
| 1001 | 1001 | GpF.SetBit(P16); | |
| 1002 | 1002 | GpF.ClearBit(P16); | |
| 1003 | 1003 | end; | |
| 1004 | 1004 | ido:= Time; | |
| 1005 | 1005 | v:= TimeStampToMSecs(DateTimeToTimeStamp(ido)); | |
| 1006 | 1006 | LogMemo.Lines.Add('Stop: '+TimeToStr(ido)+' Frequency: '+ | |
| 1007 | 1007 | IntToStr(p div (v-k))+' kHz'); | |
| 1008 | 1008 | end; | |
| 1009 | 1009 | ||
| 1010 | 1010 | procedure TForm1.Timer1Timer(Sender: TObject); | |
| 1011 | 1011 | begin | |
| 1012 | 1012 | If PWM then Begin | |
| 1013 | 1013 | If (d > 0) and (i+d < 1024) then begin | |
| 1014 | 1014 | i:=i+d; | |
| 1015 | 1015 | GpF.PwmWrite(Ppwm,i); | |
| 1016 | 1016 | end ; | |
| 1017 | 1017 | If (d < 0) and (i+d > -1) then begin | |
| 1018 | 1018 | i:=i+d; | |
| 1019 | 1019 | GpF.PwmWrite(Ppwm,i); | |
| 1020 | 1020 | end; | |
| 1021 | 1021 | end; | |
| 1022 | 1022 | end; | |
| 1023 | 1023 | ||
| 1024 | 1024 | ||
| 1025 | 1025 | end. | |
| 1026 | 1026 | ``` | |
| 1027 | 1027 | ||
| 1028 | 1028 | ## 参考 | |
| 1029 | 1029 | ||
| 1030 | 1030 | - Eben Upton and Gareth Halfacree. Raspberry Pi User Guide. John Wiley and Sons, Chichester 2012, ISBN 111846446X | |
| 1031 | 1031 | - B. J. Rao (2013) The Raspberry Pi, Pi Vision and Lazarus/FPC. [Blaise Pascal Magazine](http://www.blaisepascal.eu). 29: 14-21. | |
| 1032 | 1032 | ||
| 1033 | 1033 | ## 外部链接 | |
| 1034 | 1034 | ||
| 1035 | 1035 | - [Raspberry Pi 基础](http://www.raspberrypi.org) | |
| 1036 | 1036 | - [Raspberry Pi Spy: Raspberry Pi tutorials, scripts, help and downloads](http://www.raspberrypi-spy.co.uk) | |
| 1037 | 1037 | - [Lazarus wrapper unit for Gordon Henderson's wiringPi C library](http://www.lazarus.freepascal.org/index.php/topic,17404.0.html) | |
| 1038 | 1038 | - [Pin layout of the wiringPi library](https://projects.drogon.net/raspberry-pi/wiringpi/pins/) | |
| 1039 | 1039 | - [Additional information on Lazarus and Raspberry Pi at eLinux.org](http://www.elinux.org/Lazarus_on_RPi) | |
| 1040 | 1040 | - [Getting Started with Pascal on the Pi](http://www.pp4s.co.uk/main/gs-pi-intro.html) | |
| 1041 | 1041 | - [Lazberry Pi: Comprehensive information on Lazarus on the Raspberry Pi computer](http://superbitysoft.co.uk/lazberrypi/). | |
| 1042 | 1042 | - [Stefan Fischer's Lazarus unit rpi_hal](http://www.lazarus.freepascal.org/index.php/topic,20991.0.html). | |
| 1043 | 1043 | - [Improved Lazarus Unit for fast access to GPIO](http://www.meltonisl.com/software.html). | |
| 1044 | 1044 | ||
| 1045 | 1045 | --- | |
| 1046 | 1046 | *From the Free Pascal wiki, page "Lazarus on Raspberry Pi/zh CN", as it stood on 2020-02-19 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
