用树莓派给 arduino pro mini 下载

Raspberry 2017-06-24

新入手一枚 arduino pro mini ATmega328P 没有 usb-to-ttl 转换器,于是考虑使用树莓派给 arduino 下载,也省了转换器的钱。过期的不负责任的教程害人,不要设置 force_turbo = 1,否则树莓派的保修就没了!!!说在前面,树莓派型号是三代 B+,2017年6月最新系统 rasbian,与老教程里系统的文件不太相同。

0x00 恢复硬件串口

树莓派从大的方向来说一共出了3代,每一代的CPU外设基本相同,但内核不同,外设里面一共包含两个串口,一个称之为硬件串口(/dev/ttyAMA0),一个称之为mini串口(/dev/ttyS0)。硬件串口由硬件实现,有单独的波特率时钟源,性能高、可靠,mini串口性能低,功能也简单,并且没有波特率专用的时钟源而是由CPU内核时钟提供,因此mini串口有个致命的弱点是:波特率受到内核时钟的影响。内核若在智能调整功耗降低主频时,相应的这个mini串口的波特率便受到牵连了,虽然你可以固定内核的时钟频率,但这显然不符合低碳、节能的口号。在所有的树莓派板卡中都通过排针将一个串口引出来了,目前除了树莓派3代以外 ,引出的串口默认是CPU的那个硬件串口。而在树莓派3代中,由于板载蓝牙模块,因此这个硬件串口被默认分配给与蓝牙模块通信了,而把那个mini串口默认分配给了排针引出的GPIO Tx Rx,下图是树莓派3的接口图
gpio.png
其中红框中就是引出的串口IO,如果我们需要通过UART外接模块,默认情况下必须得使用性能很低的mini串口了,而且随着内核主频的变化,还会造成波特率的变化导致通信的失败,几乎很难使用。所以我们希望恢复硬件串口与GPIO 14/15的映射关系,使得我们能够通过GPIO使用高性能的硬件串口来连接我们的串口设备。

恢复硬件串口的方法:

暴力一点,使用 pi3-miniuart-bt-overlay.dtb

这种方法会使 serial0, serial1 这两个链接直接消失,而如果与此(serial0, serial1)相关配置文件里面写的也是 serial0, serial1 的话,那当然就自动失效了。而如果与此相关配置文件里面写的是 ttyAMA0, ttyS0 的话,参照方法二。
1.下载 pi3-miniuart-bt-overlay 文件,解压出 pi3-miniuart-bt-overlay.dtb 文件,并将 dtb 文件拷贝到 /boot/overlays/ 目录下
2.编辑/boot目录下的config.txt文件
sudo vim /boot/config.txt
添加下面一行:
dtoverlay=pi3-miniuart-bt-overlay

交换硬件串口与mini串口的映射关系

使用 pi3-miniuart-bt
这些使用的文件都可以在 /boot/overlays 中看到,没有的话就下载,可以查看目录下的 README 文件了解详情
sudo vim /boot/config.txt
在最后加上 btoverlay=pi3-minuart-bt

pi3-miniuart-bt 的 README

 942 Name:   pi3-miniuart-bt
 943 Info:   Switch Pi3 Bluetooth function to use the mini-UART (ttyS0) and restore
 944         UART0/ttyAMA0 over GPIOs 14 & 15. Note that this may reduce the maximum
 945         usable baudrate.
 946         N.B. It is also necessary to edit /lib/systemd/system/hciuart.service
 947         and replace ttyAMA0 with ttyS0, unless you have a system with udev rules
 948         that create /dev/serial0 and /dev/serial1, in which case use
 949         /dev/serial1 instead because it will always be correct. Furthermore,
 950         you must also set core_freq=250 in config.txt or the miniuart will not
 951         work.
 952 Load:   dtoverlay=pi3-miniuart-bt                                                              
 953 Params: 

禁用串口的控制台功能

前面的步骤已经交换了硬件串口与mini串口的映射关系,但现在想使用树莓派外接串口模块进行通信还不行,因为树莓派IO引出的串口默认是用来做控制台使用的,它的初衷是为了在没有网络接口时,通过串口对树莓派进行相关的配置。因此需要禁用这个默认功能,使得串口为我们自由使用。
图形界面操作:
sudo raspi-config
进入5 Interfacing Options
选择P6 Serial
Would you like a login shell to be accessible over serial?
选择 No
Would you like the serial port hardware to be enabled?
选择 Yes
或者 字符界面执行:
sudo systemctl stop serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service

0x01 安装相关工具

ino 是一个项目构建工具,用 python 写的,它的依赖是 arduino avrdude,也就是说,没有这两个,运行会出错。

Ino is a command-line toolkit for working with Arduino hardware.
It is intended to replace Arduino IDE UI for those who prefer to work in
terminal or want to integrate Arduino development in a 3rd party IDE.
Ino can build sketches, libraries, upload firmwares, establish
serial-communication. For this it is split in a bunch of subcommands, like git
or mercurial do.

avrdude 负责把编译出来的机器码上传到 arduino mini 上面
安装 autoreset 工具
其实是用 python 对原来的 autorest 进行一下修改和包装,最后两步其实是把原来的 avrdude 的脚本备份,用新文件制作符号链接。
需要的命令如下,可以直接复制粘贴
sudo apt install arduino avrdude
pip install ino
git clone https://github.com/deanmao/avrdude-rpi.git
cd avrdude-rpi-master
sudo cp autoreset /usr/bin
sudo cp avrdude-autoreset /usr/bin
sudo mv /usr/bin/avrdude /usr/bin/avrdude-original
sudo ln -s /usr/bin/avrdude-autoreset /usr/bin/avrdude

0x02 关闭树莓派的板载蓝牙

个人认为可做可不做,因为按照上述修改后,蓝牙已经不能正常工作。
sudo systemctl disable hciuart.service

0x03 构建一个项目

ino list-models 可查看 ino 支持的设备列表,你需要用你自己的设备代码来替换我这里的 pro5v328
新建一个项目文件夹 project
cd project
ino init -t blink
此时会自动创建一个默认的示例项目,板子上 led 灯闪烁
ino build -m pro5v328 构建,完毕后在项目文件夹里多出来一个.build文件夹
里面的 firmware.hex 是编译好的机器码
ino upload -p /dev/serial0 -m pro5v328
多说几句,等价于执行
avrdude -p m328p -P /dev/ttyAMA0 -b 57600 -c arduino -U flash:w:firmware.hex
-p arduino 的具体型号,该参数强制要求,可以通过 avrdude ? 来查看支持的设备列表
-P 设备,此处是树莓派的串口设备
-b 波特率,我的设备的波特率是 57600
-U 把 firmware.hex 写入 arduino 的 flash 存储器里

0x04 参考链接

http://etrd.org/2017/01/29/%E6%A0%91%E8%8E%93%E6%B4%BE3%E7%A1%AC%E4%BB%B6%E4%B8%B2%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8%E5%8F%8A%E7%BC%96%E7%A8%8B/
https://my.oschina.net/funnky/blog/132885#OSC_h3_11
http://blog.csdn.net/qq_31396093/article/details/58640995
http://www.cnblogs.com/einstein-2014731/p/5551846.html
http://blog.csdn.net/iware99/article/details/51213602
https://pay.reddit.com/r/arduino/comments/2lk1pf/looking_for_help_pulling_my_hair_out_trying_to/
http://www.raspberrypi-spy.co.uk/2013/12/free-your-raspberry-pi-serial-port/
https://www.raspberrypi.org/forums/viewtopic.php?f=107&t=138223


本文由 Mark 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论