嵌入式基础篇--环境搭建

1.手动搭建

1.1 虚拟机

1.21.1 TFTP服务端安装

  TFTP用作让树莓派的uboot通过网络读取主机里准备好的Linux内核和设备树文件,相较于用从sd卡读取内核在调试,不需要频繁的插拔sd卡。
  TFTP是一个基于UDP协议实现的用于在客户机和服务器之间进行简单文件传输的协议,适用于开销不大、不复杂的应用场合。TFTP协议专门为小文件传输而设计,只能从服务器上获取文件,或者向服务器写入文件,不能列出目录,也不能进行认证。

安装步骤:

  • 1.安装TFTP服务器:sudo apt-get install tftp-hpa tftpd-hpa xinetd
  • 2.创建文件夹存放tftp文件:xd@xd:~$ mkdir tftp
  • 3.配置TFTP:安装完成以后新建文件/etc/xinetd.d/tftp, 如果没有/etc/xinetd.d 目录的话自行创建,然后在里面输入如下内容:
         service tftp
         {
         socket_type = dgram
         protocol = udp
         wait = yes
         user = root
         server = /usr/sbin/in.tftpd
          server_args = -s >      /home/xd/tftp/
         disable = no
         per_source = 11
         cps = 100 2
         flags = IPv4
         }
          server_args设置的/home/xd/tftpboot目录是tftp服务器的目录,TFTP客户 端就是从这个目录里获取文件的。
  • 4.启动tftp服务器:xd@xd:~$ sudo service tftpd-hpa start
  • 5.修改/etc/default/tftpd-hpa 文件:
         TFTP_USERNAME=“tftp”
          TFTP_DIRECTORY=“/home/xd/tftp”
         TFTP_ADDRESS=“:69”
         TFTP_OPTIONS=“-l -c -s”
  • 6.重启tftp服务器:xd@xd:~$ sudo service tftpd-hpa restart
  • 7.测试:
         xd@xd:~$ cd tftp/
          xd@xd:~/tftp$ ls
          xd@xd:~/tftp$ nano test
          xd@xd:~/tftp$ cat test
          hello world 2022!
          xd@xd:~$ mkdir test
          xd@xd:~$ cd test/
          xd@xd:~/test$ tftp 127.0.0.1
          tftp> get test
          tftp> q
          xd@xd:~/test$ cat test
         hello world 2022!
          xd@xd:~/test$

1.1.2 NFS服务端安装

  NFS用于让Linux内核通过网络挂载主机里准备好的根目录,方便调试宿主机+开发板的开发模式,两者之间的网络通讯与文件共享是进行高效开发调试的前提条件。NFS文件系统是嵌入式开发调试的快速高效手段,掌握与熟练使用是必备技能。
  通过NFS挂载远程主机的目录,访问该目录就像访问本地目录一样,使用NFS服务能够方便的使各unix-like系统之间实现共享;Samba是在unix-like系统和windows系统直接共享。
  NFS模式:C/S模式,RPC服务架构
  NFS Server <---------------->NFS Client
  NFS运行在SUN的RPC(Remote Procedure Call,远程过程调用)基础上,RPC定义了一种 与系统无关的方法来实现进程间通信

安装步骤:

  • 1.NFS安装:
         安装:sudo apt-get install nfs-kernel-server
         启动:sudo /etc/init.d/nfs-kernel-server start
         停止:sudo /etc/init.d/nfs-kernel-server stop
         状态:sudo /etc/init.d/nfs-kernel-server status
         重启:sudo /etc/init.d/nfs-kernel-server restart
  • 2.NFS使用配置/创建共享目录和测试目录
         xd@xd:~$ mkdir nfs
         xd@xd:~$ mkdir test
         xd@xd:~/nfs$ sudo nano test (内容为:hello world 2022!)
         /etc/exports文件最后添加内容:/home/xd/nfs *(rw,sync,no_root_squash)内容详解
  • 3.NFS配置命令:
         $sudo exportfs -r //重新加载/etc/exports的共享目录
         $sudo showmount -e //查看共享目录下的所有挂载信息
         $sudo showmount -a //查看导出列表
  • 4.测试:
         -o nolock:指定不要去锁定我们挂载的NFS目录
         挂载格式:mount [Server IP]:/[share dir] [local mount point]
xd@xd:~$ sudo mount -t nfs -o nolock 10.1.42.101:/home/xd/nfs/ ~/test
xd@xd:~/tmp$ ls
test
xd@xd:~/tmp$ cat test 
hello world 2022!
xd@xd:~$ df -h
文件系统                  容量  已用  可用 已用% 挂载点
udev                      463M     0  463M    0% /dev
tmpfs                      97M  6.2M   91M    7% /run
/dev/sda1                  39G  5.0G   32G   14% /
tmpfs                     482M     0  482M    0% /dev/shm
tmpfs                     5.0M  4.0K  5.0M    1% /run/lock
tmpfs                     482M     0  482M    0% /sys/fs/cgroup
tmpfs                      97M  3.5M   93M    4% /run/user/1000
10.1.42.102:/home/xd/nfs   39G  5.0G   32G   14% /home/xd/tmp
pi@debian:~$ sudo umount /home/xd/tmp
pi@debian:~$ df -h
文件系统        容量  已用  可用 已用% 挂载点
udev            463M     0  463M    0% /dev
tmpfs            97M  6.2M   91M    7% /run
/dev/sda1        39G  5.0G   32G   14% /
tmpfs           482M     0  482M    0% /dev/shm
tmpfs           5.0M  4.0K  5.0M    1% /run/lock
tmpfs           482M     0  482M    0% /sys/fs/cgroup
tmpfs            97M  3.5M   93M    4% /run/user/1000

1.1.3 交叉编译工具链下载与说明

  交叉编译器可以使我们在主机上编译出可以在嵌入式设备上运行的程序,64位交叉编译工具要在64位主机系统里面,否则会出现错误。

安装步骤:

  • 1.交叉编译工具下载:
         arm官方下载地址
         Linaro GCC 编译器下载地址
         交叉编译器名称解释
  • 2.下载解压到目录下,然后在~/.bashrc里将目录加入环境
         xd@xd-virtual-machine:~/RPI3B/tools$
         tar -xvf gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf.tar xz -d *****.tar.xz
         xd@xd-virtual-machine:~/RPI3B/tools$
         mv gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf tools-master
         xd@xd-virtual-machine: sudo vim ~/.bashrc
         export PATH=$PATH:/home/xd/RPI3B/tools/tools-master/bin
         xd@xd:~$ source .bashrc //使修改生效
  • 3.查看是否成功
         在这里插入图片描述

1.1.4 更换国内源

清华源:sudo vim /etc/apt/sources.list

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

1.1.5 安装必要库

sudo apt-get install make gcc
sudo apt-get install libncurses5-dev bison flex //首次编译uboot的时候需要的库
sudo apt-get install lib32z1 //交叉编译工具链是32位,Ubuntu是64位,需安装32位的库
sudo apt-get install lib32stdc++6//交叉编译工具链是32位,Ubuntu是64位,需安装
sudo apt-get install lzop //内核需安装此库,否则内核编译会失败

1.2 开发板(树莓派3B)

1.2.1 系统下载

下载地址:https://shumeipai.nxez.com/download#os
默认帐号:Username: pi Password: raspberry

1.2.2 串口登录

选择使用串口登录树莓派的原因很简单:

  • 1.对于树莓派第一次上电的时候,大部分都是自动获取和分配IP地址,不清楚树莓派一开始上电时的IP是多少,通过串口登录可以很轻易的获取到树莓派的IP地址
  • 2.对于调试内核和uboot的时候很有用,可以打印出相应信息

默认情况,树莓派3B与蓝牙连接,需要断开蓝牙连接,通过串口进行数据通讯,因为蓝牙和串口使用的端口是相同的,因此需要做如下修改:

  • 1.修改fat32下的 config.txt 文件
         enable_uart=1 //使能串口
         dtoverlay=pi3-miniuart-bt //使得Mini Uart和蓝牙设备连接
         dtoverlay=disable-bt //禁用板载蓝牙
         dtoverlay=pi3-disable-bt //使得PL011不再连接蓝牙设备,并重新映射到GPIO 14/15
  • 2.修改fat32下的 cmdline.txt 文件
         cmdline.txt文件位于 boot 分区,它包含了启动时传递给内核的命令行参数,这些参数可以影响内核的启动行为。
  • 3.上电测试
         USB-TTL 连接树莓派串口,TTL 的 RXD 针脚连接到树莓的 TXD 上,TTL 的 TXD 针脚连接到树莓的 RXD 上,GND 连接 GND,如图
         在这里插入图片描述
    树莓派默认账号:pi
    树莓派默认密码:raspberry(错误:串口登录密码输入始终错误,导致登录不了

1.2.3 设置静态IP

1.修改ip,网关,掩码:sudo vim /etc/network/interfaces

source-directory  /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 10.1.42.101
gateway 10.1.42.1
netmask 255.255.255.0
network 10.1.42.0
broadcast 10.1.42.255

allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

#必须填写不然ping不通百度
ins-nameservers 223.5.5.5
ins-nameservers 8.8.8.8

2.修改DNS,保存之后重启:sudo nano /etc/resolv.conf,(错误:DNS重启失效)

nameserver 114.114.114.114
nameserver 8.8.8.8

3.最终效果
在这里插入图片描述

1.2.4 时区设置

>pi@raspberry:~ $ sudo raspi-config
>pi@raspberrypi:~$ date                                                         
Mon 11 Apr 18:26:54 CST 2022

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.2.5 更换软件源

pi@raspberrypi:~$ lsb_release -a	//查看树莓派系统版本
No LSB modules are available.
Distributor ID:	Raspbian
Description:	Raspbian GNU/Linux 8.0 (jessie)
Release:	8.0
Codename:	jessie

Debian 10(buster) —————— 当前的稳定版(stable)
Debian 9(stretch)——————— 旧的稳定版(oldstable)
Debian 8(jessie) ——————— 更旧的稳定版(oldoldstable)
Debian 7(wheezy) —————— 被淘汰的稳定版
Debian 6.0(squeeze) ————— 被淘汰的稳定版
Debian GNU/Linux 5.0(lenny) -— 被淘汰的稳定版
Debian GNU/Linux 4.0(etch)—— 被淘汰的稳定版
Debian GNU/Linux 3.1(sarge) -— 被淘汰的稳定版
Debian GNU/Linux 3.0(woody) — 被淘汰的稳定版
Debian GNU/Linux 2.2(potato) — 被淘汰的稳定版
Debian GNU/Linux 2.1(slink)—— 被淘汰的稳定版
Debian GNU/Linux 2.0(hamm) — 被淘汰的稳定版

树莓派的所有软件源
1.更换国内软件源:

32位debian8系统镜像源:
/etc/apt/sources.list
	deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ bullseye main contrib non-free rpi
	deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ bullseye main contrib non-free rpi
	
/etc/apt/sources.list.d/raspi.list 
	deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ bullseye main
	
64位debian10系统镜像源:
/etc/apt/sources.list
	deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
	deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
	deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
	deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
	
/etc/apt/sources.list.d/raspi.list
	deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi buster main

2.更新软件:
sudo apt-get update时会出现各种各样的错误,错误情况及解决办法见:sudo apt-get update错误情形分析

pi@raspberrypi:~$ sudo apt-get update
pi@raspberrypi:~$ sudo apt-get upgrade
pi@raspberrypi:~$ sudo apt-get autoclean	//将已经删除了的软件包的.deb安装文件从硬盘中删除掉,节省磁盘空间

1.2.6 ssh登录

树莓派开发板默认ssh没有开启
解决办法:

  • 1.在boot分区创建一个空的ssh文件即可
  • 2.串口登录开发板,配置ssh开启:pi@raspberrypi:~ $ sudo raspi-config

错误:64位系统会出现ssh登录错误(pi用户登录可以,root用户登录输入命令总是提示错误)
解决:将 etc/ssh/sshd_config/文件中的PermitRootLogin 设置为yes,重新设

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值