樹莓派4B學習札記


防靜電

樹莓派比較容易被靜電損壞,要做好以下預防措施

  1. 使用的時候不要用手去觸摸PCB和針腳!特別是上電之后!
  2. 拿板卡的時候,要習慣性拿板卡的邊緣
  3. 勤洗手,勤摸牆壁,釋放身上的靜電

系統安裝

  • 8GB以上的Mirco SD卡
  • 讀卡器

將SD卡連接到電腦,格式化SD卡

樹莓派官網下載燒錄程序,從左到右完成設置,寫入SD卡

image

完成后將SD卡插入卡槽,首次啟動還需要連接鍵盤鼠標和顯示器(HDMI轉接線),連接電源線后給樹莓派上電,完成初始化設定。

使用命令行查詢樹莓派的硬件參數

查看CPU信息

$ lscpu

Architecture: armv7l Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 1 Core(s) per socket: 4 Socket(s): 1 Vendor ID: ARM Model: 3 Model name: Cortex-A72 Stepping: r0p3 CPU max MHz: 1500.0000 CPU min MHz: 600.0000

查看內存的使用情況。

free -h

查看SD卡的存儲情況

$ sudo fdisk -l

查看操作系統的信息

$ uname -a

Linux raspberrypi 5.10.63-v7l+ #1459 SMP Wed Oct 6 16:41:57 BST 2021 armv7l GNU/Linux

查看網絡接口

$ ifconfig

這個命令很重要!!!

查看板卡信息

$ pinout

image

從這里可用知道這塊樹莓派是Model 4B型號,BCM2711,可以去讀對應的官方文檔。

局域網遠程桌面

安裝xrdp

檢查樹莓派的網絡,使用ping命令

image

輸入命令

$ sudo apt-get install xrdp

回車之后會自動進入安裝,安裝完成后輸入命令

$ ifconfig

image

記住划線處的IP地址

遠程桌面

在主機,按下win + r組合鍵,輸入cmd,回車,ping剛剛加下的地址
image

確認連通后,按下win + q,輸入遠程桌面
image

輸入之前記下的IP地址

image

輸入用戶名pi和密碼,連接

image

連接成功!

image

用遠程桌面來操控樹莓派,可以省去很多麻煩。

需要注意的事項

  • 樹莓派和主機需要處在同一個局域網內,連接之前多ping幾次確認連通
  • 使用win + Q搜索遠程桌面打開,輸入用戶名pi注意不要大寫,否則會出現load failed for Display 0的錯誤

參考

https://shumeipai.nxez.com/2013/10/06/windows-remote-desktop-connection-raspberry-pi.html

SPI總線通信

SPI是什么

官方文檔鏈接
SPI總線是一種串行總線

MOTOROLA公司的SPI總線的基本信號線為3根傳輸線,即

  • MOSI:主片數據輸出和從片數據輸入
  • MISO:從片數據輸出和主片數據輸入
  • SCK: 主片在這條線上發出SCK頻率,用來決定傳輸速率

image

主片通過發出片選信號/cs來選擇和哪個從片進行通信,當某個從片的/cs信號有效時,便能通過MOSI傳輸線接受指令、數據,並通過MISO傳輸線發回數據。

對具有SPI接口的從片器件來講,SCK、MOSI是輸入信號,MISO是輸出信號。

  • SCK用於主片和從片通信的同步
  • MOSI用於將信息傳輸到器件

指令、地址和數據的變化在SCK的低電平期間進行,並由SCK信號的上升沿鎖存。

  • MISO用於將信息從器件傳出

傳出的信息包括狀態和數據,信息在SCK信號的下降沿送出。

利用SPI總線進行自發自收

樹莓派提供了一組GPIO接口,找到MOSI和MISO引腳
image

在官方文檔中進一步查詢得到更多信息

image

用一個跳線帽短接MOSI和MISO,讓樹莓派通過SPI總線實現自發自收

在樹莓派的終端輸入

$ sudo raspi-config

回車,進入設置界面

![image](https://img20image

按下選到3 Interface Options

image

image

回車開啟SPI總線后,退出設置界面

用命令行創建一個c語言文件,將下列代碼粘貼到里面保存

/*代碼來源:https://blog.csdn.net/u014357799/article/details/112376163/*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "SPISet.h"
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <asm/ioctl.h>
#include <linux/spi/spidev.h>

static const char *spiDev0 = "/dev/spidev0.0";
static const char *spiDev1 = "/dev/spidev0.1";
static uint8_t spiBPW = 8;
static uint16_t spiDelay = 0;
static uint32_t spiSpeeds [2];
static int spiFds [2];

int SPIDataRW ( int channel, unsigned char *tx_data, unsigned char *rx_data, int len );
int SPISetupMode ( int channel, int speed, int mode );
int SPISetup( int channel, int speed );
int initSPI();

int main(){
	char tx_Data[] = {1,2,3,4,5,6,7,8,9,10};
	char rx_Data[] = {0,0,0,0,0,0,0,0,0,0};
	int i = 0;
	initSPI();
	while(1){
		SPIDataRW(0, tx_Data, rx_Data, 10);
		printf("read SPI_rx_data is: \n");
		for( i = 0; i < 10; i++ ){
			printf("%d\t",rx_Data[i]);
		}
		printf("\n");
		sleep(1);
	}
	return 0;
}

int initSPI(){
	int spiFd;
	spiFd = SPISetup(0,500000);
	if( spiFd == -1 ){
		printf("init spi failed!\n");
	}
}

int SPIDataRW (int channel, unsigned char *tx_data, unsigned char *rx_data, int len) {
	int i = 0;
	struct spi_ioc_transfer spi ;
	channel &= 1 ;
	memset (&spi, 0, sizeof (spi)) ;
	spi.tx_buf = (unsigned long)tx_data ;
	spi.rx_buf = (unsigned long)rx_data ;
	spi.len = len ;
	spi.delay_usecs = spiDelay ;
	spi.speed_hz = spiSpeeds [channel] ;
	spi.bits_per_word = spiBPW ;

	return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ; //SPI_IOC_MESSAGE(1)的1表示spi_ioc_transfer的數量
}

int SPISetupMode (int channel, int speed, int mode) {
	int fd ;
	if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0) {
		printf("Unable to open SPI device: %s\n", strerror (errno)) ;
		return -1;
	}
	spiSpeeds [channel] = speed ;
	spiFds [channel] = fd ;
	if (ioctl (fd, SPI_IOC_WR_MODE, &mode) < 0)	{
		printf("Can't set spi mode: %s\n", strerror (errno)) ;
		return -1;
	}
	if (ioctl (fd, SPI_IOC_RD_MODE, &mode) < 0) {
		printf("Can't get spi mode: %s\n", strerror (errno)) ;
		return -1;
	}
	if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) {
		printf("Can't set bits per word: %s\n", strerror (errno)) ;
		return -1;
	}
	if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) {
		printf("Can't get bits per word: %s\n", strerror (errno)) ;
		return -1;	
	}
	if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
		printf("Can't set max speed hz: %s\n", strerror (errno));
		return -1;
	}
	if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) {
		printf("Can't get max speed hz: %s\n", strerror (errno)); 
		return -1; 
	}
	return fd ; 
}

int SPISetup (int channel, int speed) {
	return SPISetupMode (channel, speed, 0) ;
}

保存后用gcc命令編譯生成.out文件,執行

image

取下跳線帽后,再次運行

image

至此完成了樹莓派通過SPI總線自收自發的任務

SPI通信的最大速率?

https://www.cnblogs.com/qiyuexin/p/6417830.html

留坑

WIFI傳輸數據?

留坑

參考鏈接

https://blog.csdn.net/u014357799/article/details/112376163
https://panqiincs.me/2020/07/10/rpi-stm32-spi-communication/
https://www.raspberrypi.com/documentation/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM