一、nuc972 spi
nuc972是一款基於ARM926EJ-S內核的嵌入式高性能開發板,支持SPI介面,可以實現與SPI設備的通信。以下是SPI模式讀寫的代碼示例:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> #include <linux/spi/spidev.h> static const char *dev = "/dev/spidev0.0"; static uint8_t mode = 0; static uint8_t bits = 8; static uint32_t speed = 500000; static uint16_t delay = 0; int main(int argc, char *argv[]) { int fd, ret; uint8_t tx[3]; uint8_t rx[3]; fd = open(dev, O_RDWR); if (fd < 0) { perror("open"); return 1; } ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); if (ret == -1) { perror("can't set spi mode"); return 1; } ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) { perror("can't set bits per word"); return 1; } ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) { perror("can't set max speed hz"); return 1; } memset(tx, 0, sizeof(tx)); memset(rx, 0, sizeof(rx)); tx[0] = 0x9f; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx, .rx_buf = (unsigned long)rx, .len = 3, .delay_usecs = delay, .speed_hz = speed, .bits_per_word = bits, }; ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); if (ret < 1) { perror("can't send spi message"); return 1; } for (int i = 0; i < 3; i++) { printf("%02x ", rx[i]); } printf("\n"); close(fd); return status; }
以上代碼為nuc972讀取SPI設備的代碼示例,通過SPI介面讀取設備數據。其中dev是SPI介面的設備文件名,spi_ioc_transfer結構體中初始化了發送和接收緩衝區,以及SPI的參數。
二、nuc972 8720
nuc972 8720是nuc972系列中一款常用的晶元,以下是用nuc972 8720編寫的二進位文件燒寫的代碼示例:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #define FILENAME "nuc972.bin" #define MEM_DEV "/dev/mem" static unsigned int Nuc_Phy_Addr = 0x00; static unsigned int Nuc_Virt_Addr = 0x00; static unsigned int Nuc_Size = 0x00; int main(int argc, char *argv[]) { int fd, fdbin, ret; unsigned long offset; char *bin_buf; void *mem_ptr; fdbin = open(FILENAME, O_RDONLY); if (fdbin < 0) { printf("open %s error, %s\n", FILENAME, strerror(errno)); return -1; } fd = open(MEM_DEV, O_RDWR | O_SYNC); if (fd < 0) { printf("open %s error, %s\n", MEM_DEV, strerror(errno)); return -1; } bin_buf = malloc(BIN_SIZE); if (bin_buf == NULL) { printf("malloc failed\n"); return -1; } memset(bin_buf, 0, BIN_SIZE); ret = read(fd, bin_buf, BIN_SIZE); if (ret != BIN_SIZE) { printf("read %s error, %s\n", FILENAME, strerror(errno)); return -1; } mem_ptr = mmap(0, BIN_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, Nuc_Phy_Addr); if (mem_ptr == MAP_FAILED) { printf("mmap failed, %s\n", strerror(errno)); return -1; } memcpy(mem_ptr + Nuc_Virt_Addr, bin_buf, BIN_SIZE); munmap(mem_ptr, BIN_SIZE); close(fd); close(fdbin); return 0; }
以上代碼為將二進位文件nuc972.bin燒寫到nuc972 8720晶元的程序中。代碼通過打開/dev/mem設備,然後將nuc972.bin讀入內存緩衝區,再將其映射到nuc972 8720的物理地址上,修改虛擬地址的內容為bin_buf。最後釋放映射區和關閉設備文件。
三、NUC972DF71YC
NUC972DF71YC是一款低功耗、高性能的嵌入式開發板。以下是通過NUC972DF71YC串口的代碼示例:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <termios.h> static int fd; int open_port(void) { fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { printf("open ttyS1 failed\n"); return -1; } struct termios options; tcgetattr(fd, &options); options.c_cflag = B115200 | CS8 | CLOCAL | CREAD; options.c_iflag = 0; options.c_oflag = 0; options.c_lflag = 0; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &options); return fd; } int main(int argc, char *argv[]) { open_port(); int ret; char *send_buf = "hello from nuc972\r\n"; char recv_buf[50]; ret = write(fd, send_buf, strlen(send_buf)); printf("send: %s\n", send_buf); usleep(10000); ret = read(fd, recv_buf, sizeof(recv_buf)); printf("recv: %s\n", recv_buf); close(fd); return 0; }
以上代碼為nu972串口通信的代碼示例,程序中定義了UART的屬性,並利用串口傳輸數據。首先打開串口設備文件,通過tcgetattr()函數獲取串口屬性,再通過tcsetattr()函數設置串口屬性。然後通過read()和write()函數進行串口通信。最後關閉串口設備文件。
四、NUC972什麼時候發布
NUC972是由國內知名晶元設計公司——翱捷科技開發的具有國際水平的高性能晶元。該晶元於2015年12月發布,經過多年的市場檢驗,已經成為嵌入式領域中的重要晶元之一。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/185875.html