This commit is contained in:
xswet
2022-12-05 15:23:01 +08:00
parent 82852ecd37
commit 427cbd4d50
13 changed files with 357 additions and 32 deletions

View File

@@ -6,3 +6,4 @@
/.clang-format
/*/build
/*/dist
*.docx

View File

@@ -26,7 +26,7 @@ CC=$(CROSS_COMPILE)gcc
CFLAGS= -Wall
CFLAGS += $(DEBFLAGS)
LDFLAGS =
LDFLAGS = -L. -lserial
cname?=serial_test
oname=$(cname).o
@@ -34,7 +34,7 @@ targetname = $(cname)
all: $(targetname)
#compiler ruler
$(targetname):$(oname) c_serial.o
$(targetname):$(oname)
$(CC) $(CFLAGS) -o $@ $? $(LDFLAGS)
rm *.o
rm -f *.bak

View File

@@ -30,6 +30,7 @@ extern "C" {
#define EXIT_ARGS_LEN_MIN 3
#define BITS_8 8
#define BITS_32 32
#define SPI_MAX_TRANSFER_LEN 28 // spi单次最大传输长度
#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args)
#define WARN_LOG(fmt, args...) \
@@ -55,9 +56,6 @@ int spi_open(const char *path, unsigned int mode, uint32_t max_speed);
int spi_open_advanced(const char *path, unsigned int mode,
uint32_t max_speed, spi_bit_order_t bit_order,
uint8_t bits_per_word, uint8_t extra_flags);
int spi_open_advanced2(const char *path, unsigned int mode,
uint32_t max_speed, spi_bit_order_t bit_order,
uint8_t bits_per_word, uint32_t extra_flags);
int spi_xfer(int fd, const uint8_t *txbuf, uint8_t *rxbuf, size_t len);
int spi_xfer2(int fd, const uint8_t *txbuf, uint8_t *rxbuf, size_t len,
uint16_t delay_usecs, uint8_t bits_per_word, uint32_t speed_hz);

View File

@@ -54,11 +54,7 @@ int spi_open(const char *path, unsigned int mode, uint32_t max_speed) {
return spi_open_advanced(path, mode, max_speed, MSB_FIRST, 8, 0);
}
int spi_open_advanced(const char *path, unsigned int mode, uint32_t max_speed, spi_bit_order_t bit_order, uint8_t bits_per_word, uint8_t extra_flags) {
return spi_open_advanced2(path, mode, max_speed, bit_order, bits_per_word, extra_flags);
}
int spi_open_advanced2(const char *path, unsigned int mode, uint32_t max_speed, spi_bit_order_t bit_order, uint8_t bits_per_word, uint32_t extra_flags) {
int spi_open_advanced(const char *path, unsigned int mode, uint32_t max_speed, spi_bit_order_t bit_order, uint8_t bits_per_word, uint32_t extra_flags) {
uint32_t data32;
uint8_t data8;
int fd;
@@ -291,7 +287,6 @@ int spi_read(int fd, uint8_t *rxbuf, size_t len) {
if (reback < 0) {
fprintf(stderr, "spi_read ():errno:%d --%s\n", errno, strerror(errno));
}
return reback;
}
@@ -309,13 +304,13 @@ int spi_close(int *fd) {
}
int spi_set_m(int fd, uint8_t mode) {
uint8_t test;
if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
ERROR_LOG("failed to get SPI mode");
uint8_t test = mode;
if (ioctl(fd, SPI_IOC_WR_MODE, &test) == -1) {
ERROR_LOG("failed to set SPI mode");
return -1;
}
if (ioctl(fd, SPI_IOC_RD_MODE, &test) == -1) {
ERROR_LOG("failed to set SPI mode");
ERROR_LOG("failed to get SPI mode");
return -1;
}
if (test != mode) {
@@ -339,7 +334,6 @@ int spi_get_mode(int fd, uint8_t *mode) {
uint8_t data8;
if (spi_get_mode__(fd, &data8)) {
ERROR_LOG("Getting SPI mode");
return -1;
}
*mode = data8 & (SPI_CPHA | SPI_CPOL);
@@ -400,7 +394,7 @@ int spi_get_extra_flags(int fd, uint8_t *extra_flags) {
int spi_get_cshigh(int fd, bool *cs) {
uint8_t mode = 0;
if (spi_get_mode__(fd, &mode) < 0) { return -1; }
if (spi_get_mode(fd, &mode) < 0) { return -1; }
*cs = (mode & SPI_CS_HIGH) ? true : false;
@@ -409,7 +403,7 @@ int spi_get_cshigh(int fd, bool *cs) {
int spi_get_loop(int fd, bool *result) {
uint8_t mode = 0;
if (spi_get_mode__(fd, &mode) < 0) { return -1; }
if (spi_get_mode(fd, &mode) < 0) { return -1; }
*result = (mode & SPI_LOOP) ? true : false;
@@ -418,7 +412,7 @@ int spi_get_loop(int fd, bool *result) {
int spi_get_no_cs(int fd, bool *result) {
uint8_t mode = 0;
if (spi_get_mode__(fd, &mode) < 0) { return -1; }
if (spi_get_mode(fd, &mode) < 0) { return -1; }
*result = (mode & SPI_NO_CS) ? true : false;
@@ -504,7 +498,7 @@ int spi_set_bits_per_word(int fd, uint8_t bits_per_word) {
int spi_set_cshigh(int fd, bool val) {
uint8_t tmp, mode = 0;
if (spi_get_mode__(fd, &mode) != 0) { return -1; }
if (spi_get_mode(fd, &mode) != 0) { return -1; }
tmp = (val == true) ? (mode | SPI_CS_HIGH) : (mode & ~SPI_CS_HIGH);
@@ -516,7 +510,7 @@ int spi_set_cshigh(int fd, bool val) {
int spi_set_no_cs(int fd, bool val) {
uint8_t tmp, mode;
if (spi_get_mode__(fd, &mode) < 0) { return -1; }
if (spi_get_mode(fd, &mode) < 0) { return -1; }
tmp = (val == true) ? (mode | SPI_NO_CS) : (mode & ~SPI_NO_CS);
@@ -528,7 +522,7 @@ int spi_set_no_cs(int fd, bool val) {
int spi_set_loop(int fd, bool val) {
uint8_t tmp, mode;
if (spi_get_mode__(fd, &mode) < 0) { return -1; }
if (spi_get_mode(fd, &mode) < 0) { return -1; }
tmp = (val == true) ? (mode | SPI_LOOP) : mode & ~SPI_LOOP;

View File

@@ -152,7 +152,7 @@ static PyObject *SpiDev_writebytes(SpiDevObject *self, PyObject *args) {
static PyObject *SpiDev_readbytes(SpiDevObject *self, PyObject *args) {
uint8_t rxbuf[SPIDEV_MAXPATH];
int status, len, ii;
int len, ii;
PyObject *list;
if (!PyArg_ParseTuple(args, "i:read", &len))
@@ -166,7 +166,7 @@ static PyObject *SpiDev_readbytes(SpiDevObject *self, PyObject *args) {
}
memset(rxbuf, 0, sizeof rxbuf);
status = read(self->fd, &rxbuf[0], len);
status = read(self->fd, rxbuf, len);
if (status < 0) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
@@ -759,8 +759,8 @@ static PyObject *SpiDev_xfer3(SpiDevObject *self, PyObject *args) {
}
static int spidev_set_mode__(int fd, uint8_t mode) {
uint8_t test;
if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
uint8_t test = mode;
if (ioctl(fd, SPI_IOC_WR_MODE, &test) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
@@ -883,7 +883,7 @@ static int SpiDev_set_cshigh(SpiDevObject *self, PyObject *val, void *closure) {
return -1;
}
tmp = (val == Py_True) ? (self->mode | SPI_CS_HIGH) : (tmp = self->mode & ~SPI_CS_HIGH);
tmp = (val == Py_True) ? (self->mode | SPI_CS_HIGH) : (self->mode & ~SPI_CS_HIGH);
ret = spidev_set_mode__(self->fd, tmp);
if (ret != -1) { self->mode = tmp; }
return ret;
@@ -924,7 +924,7 @@ static int SpiDev_set_3wire(SpiDevObject *self, PyObject *val, void *closure) {
}
tmp = (val == Py_True) ? (self->mode | SPI_3WIRE) : (self->mode & ~SPI_3WIRE);
ret = spidev_set_mode__(self->fd, tmp);
if (ret != -1) { self->mode = tmp; }
return ret;
}

View File

@@ -26,7 +26,7 @@ CC=$(CROSS_COMPILE)gcc
CFLAGS= -Wall
CFLAGS += $(DEBFLAGS)
LDFLAGS =
LDFLAGS = -L. -lspidev
cname?=test01
oname=$(cname).o
@@ -34,7 +34,7 @@ targetname = $(cname)
all: $(targetname)
#compiler ruler
$(targetname):$(oname) c_spidev.o
$(targetname):$(oname)
$(CC) $(CFLAGS) -o $@ $? $(LDFLAGS)
rm *.o
rm -f *.bak

View File

@@ -27,6 +27,7 @@ int main(void) {
int fd, res;
uint8_t new[LEN_MAX];
uint8_t buf[BUF_LEN] = {0xaa, 0xbb, 0xcc, 0xdd};
uint8_t rbuf[BUF_LEN] = {0};
memset(new, 0, LEN_MAX);
@@ -40,12 +41,12 @@ int main(void) {
res = spi_tostring(fd, new, SPI_SPEED);
printf("%s\n", new);
if (spi_xfer(fd, buf, buf, sizeof(buf)) < 0) {
if (spi_xfer(fd, buf, rbuf, sizeof(buf)) < 0) {
printf("failed to transfer\n");
exit(1);
}
printf("shifted in: 0x%02x 0x%02x 0x%02x 0x%02x\n", buf[0], buf[1], buf[2], buf[3]);
printf("shifted in: 0x%02x 0x%02x 0x%02x 0x%02x\n", rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
spi_close(&fd);

View File

@@ -21,5 +21,6 @@ SPI_SPEED = 1000000
spi.max_speed_hz = SPI_SPEED
spi.mode = 0b01
spi.writebytes2([01, 01])
print(spi.readbytes(2))
spi.close()

View File

@@ -0,0 +1,54 @@
/**
* Copyright(C) 2022. Huawei Technologies Co.,Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "a200dkspi.h"
#define SPI_SPEED 1000000
#define LEN_MAX 1024
#define BUF_LEN 4
int main(void) {
int fd, res;
uint8_t new[LEN_MAX];
uint8_t buf[BUF_LEN] = {0xaa, 0xbb, 0xcc, 0xdd};
uint8_t read_buf[BUF_LEN] = {0};
memset(new, 0, LEN_MAX);
if ((fd = spi_open("/dev/spidev0.0", 0, SPI_SPEED)) < 0) {
printf("failed to open\n");
exit(1);
} else {
printf("%d\n", fd);
}
res = spi_tostring(fd, new, SPI_SPEED);
printf("%s\n", new);
if (spi_write(fd, buf, sizeof(buf)) < 0) {
printf("failed to transfer\n");
exit(1);
}
printf("shifted in: 0x%02x 0x%02x 0x%02x 0x%02x\n", read_buf[0], read_buf[1], read_buf[2], read_buf[3]);
spi_close(&fd);
return 0;
}

View File

@@ -0,0 +1,39 @@
# Copyright(C) 2022. Huawei Technologies Co.,Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import spidev
spi = spidev.SpiDev()
spi.open(0, 0)
SPI_SPEED = 1000000
SPI_SPEED_2 = 2000000
BITS_PER_WORD_32 = 32
spi.max_speed_hz = SPI_SPEED
spi.mode = 0b01
print(spi.xfer([1, 1]))
print(spi.mode)
print(spi.bits_per_word)
print(spi.max_speed_hz)
spi.mode = 0
spi.bits_per_word = BITS_PER_WORD_32
spi.max_speed_hz = SPI_SPEED_2
print(spi.mode)
print(spi.bits_per_word)
print(spi.max_speed_hz)
spi.close()

View File

@@ -0,0 +1,61 @@
/**
* Copyright(C) 2022. Huawei Technologies Co.,Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "a200dkspi.h"
#define SPI_SPEED 1000000
#define SPI_MAX_SPEED 3000000
#define LEN_MAX 1024
#define BUF_LEN 4
#define BIIS_PER_WORD_32 32
int main(void) {
int fd, res;
uint8_t new[LEN_MAX];
uint8_t buf[BUF_LEN] = {0xaa, 0xbb, 0xcc, 0xdd};
uint8_t read_buf[BUF_LEN] = {0};
memset(new, 0, LEN_MAX);
if ((fd = spi_open("/dev/spidev0.0", 0, SPI_SPEED)) < 0) {
printf("failed to open\n");
exit(1);
} else {
printf("%d\n", fd);
}
spi_read(fd, read_buf, BUF_LEN);
printf("shifted in: 0x%02x 0x%02x 0x%02x 0x%02x\n", read_buf[0], read_buf[1], read_buf[2], read_buf[3]);
res = spi_tostring(fd, new, LEN_MAX);
printf("%s\n", new);
spi_set_mode(fd, 0);
spi_set_max_speed(fd, SPI_MAX_SPEED);
spi_set_bit_order(fd, 0);
spi_set_bits_per_word(fd, BIIS_PER_WORD_32);
res = spi_tostring(fd, new, LEN_MAX);
printf("%s\n", new);
spi_close(&fd);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,176 @@
1) 下载内核源码,下载地址:
https://www.hiascend.com/hardware/firmware-drivers?tag=community
在AI加速模块中选择对应的固件与驱动版本如图4-2-1-1所示。
![img](file:///E:/TEMP/msohtmlclip1/01/clip_image002.jpg)
图4-2-1-1 下载内核源码URL和选项
选择Atlas-200-sdk_21.0.3.1.zip下载如图4-2-1-2所示。
![图形用户界面, 文本, 应用程序, 电子邮件 描述已自动生成](file:///E:/TEMP/msohtmlclip1/01/clip_image004.jpg)
图4-2-1-2 选择下载对应的200DK
解压后将Ascend310-source-minirc.tar.gz上传至200DK任一目录下例如/opt。
2) 在200DK执行su root切换至root用户。
3) 通过如下命令进行安装依赖该步骤200DK需要联网。
```bash
apt-get install -y python make gcc unzip bison flex libncurses-dev squashfs-tools bc
```
执行cd /opt进入源码包所在目录例如/opt。
4) 执行解压命令解压源码包“Ascend310-source-minirc.tar.gz”。
```bash
tar -xzvf Ascend310-source-minirc.tar.gz
```
5) 执行命令cd source进入source目录。
6) 配置内核。
a) 执行如下命令,进入内核目录。
cd kernel/linux-4.19
b) 执行如下命令,读取内核默认配置。
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- mini_defconfig
c) 执行如下命令,并使能相关配置。
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
i. 按以下选项配置内核“Y”键选择*编译进内核“M”键选择M编译成模块
Device Drivers --->
![img](file:///E:/TEMP/msohtmlclip1/01/clip_image006.png)
ii. 选择“Save”保存配置系统默认配置文件为.config。
d) 执行如下命令,保存配置。
cp -f .config arch/arm64/configs/mini_defconfig
e) 返回source目录。
cd /opt/source
7) 修改设备树。
a. 执行如下命令修改文件hi1910-asic-1004.dts。
```bash
vim dtb/hi1910-asic-1004.dts
```
修改bootargs字段如下使能uart0串口配置。
```
chosen {
bootargs = "console=ttyAMA0,115200 root=/dev/mmcblk1p1 rw rootdelay=1 syslog no_console_suspend earlycon=pl011,mmio32,0x10cf80000 initrd=0x880004000,200M cma=256M@0x1FC00000 log_redirect=0x1fc000@0x6fe04000 default_hugepagesz=2M";
};
```
![文本 描述已自动生成](file:///E:/TEMP/msohtmlclip1/01/clip_image008.jpg)c.在Ascend310-source-minirc\source\dtb\ hi1910-fpga-spi.dtsi里添加
```bash
vim dtb/hi1910-fpga-spi.dtsi
```
```dtd
spidev0: spi@0 {
compatible = "a200dk,spidev";
reg = <0>;
status = "ok";
spi-max-frequency=<32000000>;
};
```
![img](file:///E:/TEMP/msohtmlclip1/01/clip_image010.png)
8) 执行如下命令,编译模块、设备树。
```bash
bash build.sh modules;bash build.sh dtb
```
编译出的模块在文件source/output中。
1) 下载Atlas200DK驱动包下载地址
https://www.hiascend.com/hardware/firmware-drivers?tag=community
在AI开发者套件中选择对应的CANN版本和固件与驱动版本如图4-2-1-7所示。
![图形用户界面, 文本, 应用程序, 电子邮件 描述已自动生成](file:///E:/TEMP/msohtmlclip1/01/clip_image012.jpg)
图4-2-1-7 CANN版本和固件与驱动选择
选择A200dk-npu-driver-21.0.3.1-ubuntu18.04-aarch64-minirc.tar下载如图4-2-1-8所示。
![img](file:///E:/TEMP/msohtmlclip1/01/clip_image014.jpg)
图4-2-1-8 选择minirc包版本
将A200dk-npu-driver-21.0.3.1-ubuntu18.04-aarch64-minirc.tar上传至200DK目录/opt/mini。
2) 执行如下命令,进入/opt/mini目录。
```bash
su root
cd /opt/mini
```
3) 执行如下命令,解压驱动包。
```bash
tar -xzvf A200dk-npu-driver-21.0.3.1-ubuntu18.04-aarch64-minirc.tar.gz
```
4) 执行如下命令将“minirc_install_phase1.sh”拷贝至目标版本驱动包所在目录。
```bash
cp driver/scripts/minirc_install_phase1.sh /opt/mini
```
5) 执行如下命令,用重新编译后的设备树替换驱动包的设备树。
```bash
cp /opt/source/output/out_header/dt.img driver
```
6) 执行如下命令,压缩新的驱动包。
```bash
tar -zcvf A200dk-npu-driver-21.0.3.1-ubuntu18.04-aarch64-minirc.tar.gz driver
```
7) 执行如下命令,升级脚本。
```bash
./minirc_install_phase1.sh
```
8) 执行如下命令重启Atlas 200 AI加速模块。
```bash
reboot
```