树莓派与嵌入式系统实验报告

一、Linux 系统编译工具链实践:mininim 源码编译
  1. 虚拟机 Ubuntu 编译流程

    • 环境配置问题
      编译时遇到虚拟机无法联网的情况,通过连接个人热点解决(校园网限制导致无法访问外部资源)。
      执行 ./bootstrap 时报错 gnulib-tool: command not found,因缺少 gnulib 工具和项目目录结构不完整,通过 sudo apt install gnulib 解决。
      运行 ./mininim 时提示动态库缺失,安装 liballegro5.2 和 liballegro-dev 解决。

    • 打包与迁移
      打包步骤包括复制可执行文件、游戏资源及依赖库,通过 ldd 命令自动获取依赖库路径,使用 cp -L 确保复制实际文件而非链接。
      启动脚本 run_mininim.sh 通过设置 LD_LIBRARY_PATH 指向本地库目录,确保程序在目标机器独立运行。

  2. 树莓派编译

          换源配置
 将树莓派软件源替换为清华镜像,适用于 Debian 12(bookworm)系统,提升下载速度。
配置:

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://security.debian.org/debian-security bookworm-security main contrib non-free

依赖管理
先通过 apt-get remove 清除旧版 allegro5 库,再用 apt-get build-dep 自动检测并安装编译依赖。
从 Gitee 下载 allegro5 v5.2.5 源码,通过 CMake 配置编译环境:

cd allegro5_v5.2.5/build
cmake ..
make
二、跨平台开发:Android 交叉编译与部署

环境准备

  • 安装 Android NDK R25b,通过 adb shell getprop ro.product.cpu.abi 查看手机架构(如 aarch64)。
  • 配置交叉编译工具链变量

编译流程

  • 克隆 mininim 源码后,通过 ./bootstrap 生成配置脚本,指定 Android 目标架构:
./configure --host=aarch64-linux-android \
CC="$CC" AR="$AR" \
LUA_LIB="-L$LUA50_INSTALL/lib -llua50 -llualib50"
make -j$(nproc)
```  {insert\_element\_9\_}

部署与调试
使用 adb push 传输文件到手机 /data/local/tmp 目录,通过 chmod +x 赋予执行权限。
若程序无法运行,检查依赖库是否完整或架构是否匹配(如手机为 ARM64 但编译为 ARM32)。

三、树莓派 GPIO 编程:流水灯实验
  1. 硬件连接

    选用 BCM 编号 17、27、22、10、9、11 的 GPIO 引脚,搭配 GND 完成电路连接。         每个 GPIO 引脚通过电阻连接 LED 正极,负极接地,避免短路。
  2. 程序实现
import RPi.GPIO as GPIO
import time

LED_PINS = [17, 27, 22, 10, 9, 11]
DELAY = 0.3  # 秒级延时控制流水速度

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    for pin in LED_PINS:
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, GPIO.LOW)  # 初始化为低电平

def flow_animation():
    try:
        while True:
            for pin in LED_PINS:
                GPIO.output(pin, GPIO.HIGH)
                time.sleep(DELAY)
                GPIO.output(pin, GPIO.LOW)
    except KeyboardInterrupt:
        GPIO.cleanup()  # 释放资源

if __name__ == "__main__":
    setup()
    flow_animation()

运行与异常处理
执行 sudo python3 led_flow.py 启动程序,通过 Ctrl+C 正常退出。
若提示引脚被占用,用 ps aux | grep led_flow.py 查看进程并终止。

四、技术总结与问题拓展
  1. 编译工具链关键要点

    • 跨平台编译需严格匹配目标架构(如树莓派 ARM、Android aarch64),通过 --host 参数指定。
    • 动态库依赖管理是打包核心,ldd + cp -L 可自动收集所有依赖文件。
  2. GPIO 编程注意事项

    • 树莓派 GPIO 引脚仅支持 3.3V 电平,避免直接连接 5V 设备以防烧毁。
    • 程序结束时必须调用 GPIO.cleanup(),否则可能导致引脚状态锁定。

备份树莓派专用源:

sudo gedit /etc/apt/sources.list 
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free 
non-free-firmware 
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib 
nonfree 
non-free-firmware 
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib 
non-free non-free-firmware 
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main 
contrib non-free non-free-firmware 
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib 
non-free non-free-firmware 
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main 
contrib non-free non-free-firmware 
deb https://security.debian.org/debian-security bookworm-security main contrib 
non-free non-free-firmware 
deb-src https://security.debian.org/debian-security bookworm-security main 
contrib non-free non-free-firmware 
deb http://archive.debian.org/debian buster main contrib non-free 
deb-src http://archive.debian.org/debian buster main contrib non-free 
sudo apt-get update 

生成配置:
 

./bootstrap 
You may need to add #include directives for the following .h files. 
#include <getopt.h> 
#include <glob.h> 
#include <signal.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <strings.h> 
#include <sys/ioctl.h> 
#include <unistd.h> 
#include <wchar.h> 
#include "argp.h" 
#include "error.h" 
#include "fnmatch.h" 
#include "getpass.h" 
#include "nonblocking.h" 
#include "progname.h" 
#include "vasnprintf.h" 
#include "vasprintf.h" 
#include "xprintf.h" 
#include "xvasprintf.h" 
You may need to use the following Makefile variables when linking. 
Use them in <program>_LDADD when linking a program, or 
in <library>_a_LDFLAGS or <library>_la_LDFLAGS when linking a library. 
$(LTLIBINTL) when linking with libtool, $(LIBINTL) otherwise 
$(LTLIBTHREAD) when linking with libtool, $(LIBTHREAD) otherwise 
Don't forget to 
- add "gnulib/Makefile" to AC_CONFIG_FILES in ./configure.ac, 
- mention "gnulib" in SUBDIRS in Makefile.am, 
- mention "-I gnulib/m4" in ACLOCAL_AMFLAGS in Makefile.am, 
- invoke gl_EARLY in ./configure.ac, right after AC_PROG_CC, 
- invoke gl_INIT in ./configure.ac. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值