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