diff --git a/IMX6ULL/doc_pic/11_SPI/07_DAC模块上机实验.md b/IMX6ULL/doc_pic/11_SPI/07_DAC模块上机实验.md index 0df054a..60f62f4 100644 --- a/IMX6ULL/doc_pic/11_SPI/07_DAC模块上机实验.md +++ b/IMX6ULL/doc_pic/11_SPI/07_DAC模块上机实验.md @@ -1,4 +1,4 @@ -# 使用spidev操作SPI_DAC模块 # +# SPI_DAC模块上机实验 # 参考资料: @@ -44,60 +44,15 @@ DAC模块接到IMX6ULL扩展板的SPI_A插座上: #### 1.2.2 STM32MP157 +DAC模块接到STM32MP157扩展板的SPI_A插座上: + +![image-20220311100618477](pic/43_dac_on_stm32mp157.png) -## 2. DAC操作原理 -### 2.1 内部框图 - -![image-20220309155625021](pic/36_dac_block.png) - -操作过程为: - -* CS为低 -* 在SCLK的上升沿,从DIN采集16位数据,存入上图中的`16-Bit Shift Register` -* 在CS的上升沿,把`16-Bit Shift Register`中的10位数据传入`10-Bit DAC Register`,作为模拟量在OUT引脚输出 - -**注意**: - -* 传输的16位数据中,高4位是无意义的 -* 中间10位才被转换为模拟量 -* 最低2位必须是0 - - - -### 2.2 时序图 - -![image-20220309160306094](pic/37_dac_timing_diagram.png) - -使用SPI传输的细节: - -* SCLK初始电平为低 -* 使用16个SCLK周期来传输16位数据 -* 在SCLK上升沿读取DIN电平 -* 在SCLK上升沿发出DOUT信号 -* DOUT数据来自`16-Bit Shift Register` - * 第1个数据是上次数据遗留下的LSB位 - * 其余15个数据来自`16-Bit Shift Register`的高15位 - * `16-Bit Shift Register`的LSB在下一个周期的第1个时钟传输 - * LSB必定是0,所以当前的周期里读出`16-Bit Shift Register`的15位数据也足够了 - -### 2.3 DAC公式 - -![image-20220309162256125](pic/38_dac_value.png) - - - -```shell -输出电压 = 2 * VREFIN * n / 1024 = 2 * 2.048 * n / 1024 -其中: n为10位数值 -``` - - - -## 3. 编写设备树 +## 2. 编写设备树 确认SPI时钟最大频率: @@ -122,23 +77,229 @@ F = 20000000 = 20MHz +### 2.1 IMX6ULL + +![image-20220311101017666](pic/44_imx6ull_pro_extend_spi_a.png) + +DAC模块接在这个插座上,那么要在设备树里spi1的节点下创建子节点。 + +代码在`arch/arm/boot/dts/100ask_imx6ull-14x14.dtb`中,如下: + +```shell +&ecspi1 { + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_ecspi1>; + + fsl,spi-num-chipselects = <2>; + cs-gpios = <&gpio4 26 GPIO_ACTIVE_LOW>, <&gpio4 24 GPIO_ACTIVE_LOW>; + status = "okay"; + + dac: dac { + compatible = "spidev"; + reg = <0>; + spi-max-frequency = <20000000>; + }; +}; +``` + + + + + +### 2.2 STM32MP157 + +![image-20220311101127305](pic/45_stm32mp157_pro_extend_spi_a.png) + +DAC模块接在这个插座上,那么要在设备树里spi5的节点下创建子节点。 + +代码在`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dts`中,如下: + +```shell +&spi5 { + pinctrl-names = "default", "sleep"; + pinctrl-0 = <&spi5_pins_a>; + pinctrl-1 = <&spi5_sleep_pins_a>; + status = "okay"; + cs-gpios = <&gpioh 5 GPIO_ACTIVE_LOW>, <&gpioz 4 GPIO_ACTIVE_LOW>; + spidev: icm20608@0{ + compatible = "invensense,icm20608"; + interrupts = <0 IRQ_TYPE_EDGE_FALLING>; + interrupt-parent = <&gpioz>; + spi-max-frequency = <8000000>; + reg = <0>; + }; + dac_test: dac_test@1{ + compatible = "spidev"; + spi-max-frequency = <20000000>; + reg = <1>; + }; +}; +``` + + + + + +## 3. 编译替换设备树 + ### 3.1 IMX6ULL +#### 3.1.1 设置工具链 + +```shell +export ARCH=arm +export CROSS_COMPILE=arm-buildroot-linux-gnueabihf- + export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin +``` + + +#### 3.1.2 编译、替换设备树 + + * 编译设备树: + 在Ubuntu的IMX6ULL内核目录下执行如下命令, + 得到设备树文件:`arch/arm/boot/dts/100ask_imx6ull-14x14.dtb` + + ```shell + make dtbs + ``` + + * 复制到NFS目录: + + ```shell + $ cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs/ + ``` + + * 开发板上挂载NFS文件系统 + + ```shell + [root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt + ``` + +* 更新设备树 + + ```shell + [root@100ask:~]# cp /mnt/100ask_imx6ull-14x14.dtb /boot + [root@100ask:~]# sync + ``` + +* 重启开发板 + ### 3.2 STM32MP157 +#### 3.2.1 设置工具链 + +```shell +export ARCH=arm +export CROSS_COMPILE=arm-buildroot-linux-gnueabihf- +export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin +``` + + +#### 3.2.2 编译、替换设备树 + + * 编译设备树: + 在Ubuntu的STM32MP157内核目录下执行如下命令, + 得到设备树文件:`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb` + + ```shell + make dtbs + ``` + + * 复制到NFS目录: + + ```shell + $ cp arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb ~/nfs_rootfs/ + ``` + + * 开发板上挂载NFS文件系统 + + ```shell + [root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt + ``` + +* 确定设备树分区挂载在哪里 + + 由于版本变化,STM32MP157单板上烧录的系统可能有细微差别。 + 在开发板上执行`cat /proc/mounts`后,可以得到两种结果(见下图): + + * mmcblk2p2分区挂载在/boot目录下(下图左边):无需特殊操作,下面把文件复制到/boot目录即可 + + * mmcblk2p2挂载在/mnt目录下(下图右边) + + * 在视频里、后面文档里,都是更新/boot目录下的文件,所以要先执行以下命令重新挂载: + * `mount /dev/mmcblk2p2 /boot` + + ![](pic/46_boot_mount.png) + +* 更新设备树 + + ```shell + [root@100ask:~]# cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot/ + [root@100ask:~]# sync + ``` + +* 重启开发板 + + + +## 4. 编译spidev驱动 + +首先要确定内核中已经含有spidev。在内核目录下执行make menuconfig,查看是否有改驱动,如下图: + +```shell +-> Device Drivers + -> SPI support (SPI [=y]) + < > User mode SPI device driver support +``` + +如果`User mode SPI device driver support`前面不是``或``,可以输入`M`表示把它编译为模块。 + +* 如果已经是``,则不用再做其他事情。 +* 如果你设置为``,在内核目录下执行`make modules`,把生成的`drivers/spi/spidev.ko`复制到NFS目录备用 + + + +## 5. 编译APP + +```shell +arm-buildroot-linux-gnueabihf-gcc -o dac_test dac_test.c +``` + + + +## 6. 上机实验 + +如果spidev没有被编译进内核,那么先执行: + +```shell +insmod spidev.ko +``` + + + +确定设备节点: + +```shell +ls /dev/spidev* +``` + + + +假设设备节点为`/dev/spidev0.0`,执行测试程序: + +```shell +./dac_test /dev/spidev0.0 500 +./dac_test /dev/spidev0.0 600 +./dac_test /dev/spidev0.0 1000 +``` + -## 3. 编写APP - -源码在GIT仓库里,这2个位置里的源码是一样的: - -![image-20220310120532411](pic/41_dac_app_use_spidev.png) -![image-20220310120646206](pic/42_dac_app_use_spidev.png) diff --git a/IMX6ULL/doc_pic/11_SPI/07_DAC模块上机实验.tif b/IMX6ULL/doc_pic/11_SPI/07_DAC模块上机实验.tif new file mode 100644 index 0000000..e9c0fe4 Binary files /dev/null and b/IMX6ULL/doc_pic/11_SPI/07_DAC模块上机实验.tif differ diff --git a/IMX6ULL/doc_pic/11_SPI/pic/43_dac_on_stm32mp157.png b/IMX6ULL/doc_pic/11_SPI/pic/43_dac_on_stm32mp157.png new file mode 100644 index 0000000..ccdc4bc Binary files /dev/null and b/IMX6ULL/doc_pic/11_SPI/pic/43_dac_on_stm32mp157.png differ diff --git a/IMX6ULL/doc_pic/11_SPI/pic/44_imx6ull_pro_extend_spi_a.png b/IMX6ULL/doc_pic/11_SPI/pic/44_imx6ull_pro_extend_spi_a.png new file mode 100644 index 0000000..6dc64e6 Binary files /dev/null and b/IMX6ULL/doc_pic/11_SPI/pic/44_imx6ull_pro_extend_spi_a.png differ diff --git a/IMX6ULL/doc_pic/11_SPI/pic/45_stm32mp157_pro_extend_spi_a.png b/IMX6ULL/doc_pic/11_SPI/pic/45_stm32mp157_pro_extend_spi_a.png new file mode 100644 index 0000000..44f1f1a Binary files /dev/null and b/IMX6ULL/doc_pic/11_SPI/pic/45_stm32mp157_pro_extend_spi_a.png differ diff --git a/IMX6ULL/doc_pic/11_SPI/pic/46_boot_mount.png b/IMX6ULL/doc_pic/11_SPI/pic/46_boot_mount.png new file mode 100644 index 0000000..a3967b7 Binary files /dev/null and b/IMX6ULL/doc_pic/11_SPI/pic/46_boot_mount.png differ diff --git a/IMX6ULL/source/11_SPI/02_dac_use_spidev/dac_test.c b/IMX6ULL/source/11_SPI/02_dac_use_spidev/dac_test.c index b613bda..00e8306 100644 --- a/IMX6ULL/source/11_SPI/02_dac_use_spidev/dac_test.c +++ b/IMX6ULL/source/11_SPI/02_dac_use_spidev/dac_test.c @@ -61,7 +61,7 @@ int main(int argc, char **argv) /* 打印 */ val = (rx_buf[0] << 8) | (rx_buf[1]); - val >>= 1; + val >>= 2; printf("Pre val = %d\n", val); diff --git a/STM32MP157/doc_pic/11_SPI/07_DAC模块上机实验.md b/STM32MP157/doc_pic/11_SPI/07_DAC模块上机实验.md index 0df054a..60f62f4 100644 --- a/STM32MP157/doc_pic/11_SPI/07_DAC模块上机实验.md +++ b/STM32MP157/doc_pic/11_SPI/07_DAC模块上机实验.md @@ -1,4 +1,4 @@ -# 使用spidev操作SPI_DAC模块 # +# SPI_DAC模块上机实验 # 参考资料: @@ -44,60 +44,15 @@ DAC模块接到IMX6ULL扩展板的SPI_A插座上: #### 1.2.2 STM32MP157 +DAC模块接到STM32MP157扩展板的SPI_A插座上: + +![image-20220311100618477](pic/43_dac_on_stm32mp157.png) -## 2. DAC操作原理 -### 2.1 内部框图 - -![image-20220309155625021](pic/36_dac_block.png) - -操作过程为: - -* CS为低 -* 在SCLK的上升沿,从DIN采集16位数据,存入上图中的`16-Bit Shift Register` -* 在CS的上升沿,把`16-Bit Shift Register`中的10位数据传入`10-Bit DAC Register`,作为模拟量在OUT引脚输出 - -**注意**: - -* 传输的16位数据中,高4位是无意义的 -* 中间10位才被转换为模拟量 -* 最低2位必须是0 - - - -### 2.2 时序图 - -![image-20220309160306094](pic/37_dac_timing_diagram.png) - -使用SPI传输的细节: - -* SCLK初始电平为低 -* 使用16个SCLK周期来传输16位数据 -* 在SCLK上升沿读取DIN电平 -* 在SCLK上升沿发出DOUT信号 -* DOUT数据来自`16-Bit Shift Register` - * 第1个数据是上次数据遗留下的LSB位 - * 其余15个数据来自`16-Bit Shift Register`的高15位 - * `16-Bit Shift Register`的LSB在下一个周期的第1个时钟传输 - * LSB必定是0,所以当前的周期里读出`16-Bit Shift Register`的15位数据也足够了 - -### 2.3 DAC公式 - -![image-20220309162256125](pic/38_dac_value.png) - - - -```shell -输出电压 = 2 * VREFIN * n / 1024 = 2 * 2.048 * n / 1024 -其中: n为10位数值 -``` - - - -## 3. 编写设备树 +## 2. 编写设备树 确认SPI时钟最大频率: @@ -122,23 +77,229 @@ F = 20000000 = 20MHz +### 2.1 IMX6ULL + +![image-20220311101017666](pic/44_imx6ull_pro_extend_spi_a.png) + +DAC模块接在这个插座上,那么要在设备树里spi1的节点下创建子节点。 + +代码在`arch/arm/boot/dts/100ask_imx6ull-14x14.dtb`中,如下: + +```shell +&ecspi1 { + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_ecspi1>; + + fsl,spi-num-chipselects = <2>; + cs-gpios = <&gpio4 26 GPIO_ACTIVE_LOW>, <&gpio4 24 GPIO_ACTIVE_LOW>; + status = "okay"; + + dac: dac { + compatible = "spidev"; + reg = <0>; + spi-max-frequency = <20000000>; + }; +}; +``` + + + + + +### 2.2 STM32MP157 + +![image-20220311101127305](pic/45_stm32mp157_pro_extend_spi_a.png) + +DAC模块接在这个插座上,那么要在设备树里spi5的节点下创建子节点。 + +代码在`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dts`中,如下: + +```shell +&spi5 { + pinctrl-names = "default", "sleep"; + pinctrl-0 = <&spi5_pins_a>; + pinctrl-1 = <&spi5_sleep_pins_a>; + status = "okay"; + cs-gpios = <&gpioh 5 GPIO_ACTIVE_LOW>, <&gpioz 4 GPIO_ACTIVE_LOW>; + spidev: icm20608@0{ + compatible = "invensense,icm20608"; + interrupts = <0 IRQ_TYPE_EDGE_FALLING>; + interrupt-parent = <&gpioz>; + spi-max-frequency = <8000000>; + reg = <0>; + }; + dac_test: dac_test@1{ + compatible = "spidev"; + spi-max-frequency = <20000000>; + reg = <1>; + }; +}; +``` + + + + + +## 3. 编译替换设备树 + ### 3.1 IMX6ULL +#### 3.1.1 设置工具链 + +```shell +export ARCH=arm +export CROSS_COMPILE=arm-buildroot-linux-gnueabihf- + export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin +``` + + +#### 3.1.2 编译、替换设备树 + + * 编译设备树: + 在Ubuntu的IMX6ULL内核目录下执行如下命令, + 得到设备树文件:`arch/arm/boot/dts/100ask_imx6ull-14x14.dtb` + + ```shell + make dtbs + ``` + + * 复制到NFS目录: + + ```shell + $ cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs/ + ``` + + * 开发板上挂载NFS文件系统 + + ```shell + [root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt + ``` + +* 更新设备树 + + ```shell + [root@100ask:~]# cp /mnt/100ask_imx6ull-14x14.dtb /boot + [root@100ask:~]# sync + ``` + +* 重启开发板 + ### 3.2 STM32MP157 +#### 3.2.1 设置工具链 + +```shell +export ARCH=arm +export CROSS_COMPILE=arm-buildroot-linux-gnueabihf- +export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin +``` + + +#### 3.2.2 编译、替换设备树 + + * 编译设备树: + 在Ubuntu的STM32MP157内核目录下执行如下命令, + 得到设备树文件:`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb` + + ```shell + make dtbs + ``` + + * 复制到NFS目录: + + ```shell + $ cp arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb ~/nfs_rootfs/ + ``` + + * 开发板上挂载NFS文件系统 + + ```shell + [root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt + ``` + +* 确定设备树分区挂载在哪里 + + 由于版本变化,STM32MP157单板上烧录的系统可能有细微差别。 + 在开发板上执行`cat /proc/mounts`后,可以得到两种结果(见下图): + + * mmcblk2p2分区挂载在/boot目录下(下图左边):无需特殊操作,下面把文件复制到/boot目录即可 + + * mmcblk2p2挂载在/mnt目录下(下图右边) + + * 在视频里、后面文档里,都是更新/boot目录下的文件,所以要先执行以下命令重新挂载: + * `mount /dev/mmcblk2p2 /boot` + + ![](pic/46_boot_mount.png) + +* 更新设备树 + + ```shell + [root@100ask:~]# cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot/ + [root@100ask:~]# sync + ``` + +* 重启开发板 + + + +## 4. 编译spidev驱动 + +首先要确定内核中已经含有spidev。在内核目录下执行make menuconfig,查看是否有改驱动,如下图: + +```shell +-> Device Drivers + -> SPI support (SPI [=y]) + < > User mode SPI device driver support +``` + +如果`User mode SPI device driver support`前面不是``或``,可以输入`M`表示把它编译为模块。 + +* 如果已经是``,则不用再做其他事情。 +* 如果你设置为``,在内核目录下执行`make modules`,把生成的`drivers/spi/spidev.ko`复制到NFS目录备用 + + + +## 5. 编译APP + +```shell +arm-buildroot-linux-gnueabihf-gcc -o dac_test dac_test.c +``` + + + +## 6. 上机实验 + +如果spidev没有被编译进内核,那么先执行: + +```shell +insmod spidev.ko +``` + + + +确定设备节点: + +```shell +ls /dev/spidev* +``` + + + +假设设备节点为`/dev/spidev0.0`,执行测试程序: + +```shell +./dac_test /dev/spidev0.0 500 +./dac_test /dev/spidev0.0 600 +./dac_test /dev/spidev0.0 1000 +``` + -## 3. 编写APP - -源码在GIT仓库里,这2个位置里的源码是一样的: - -![image-20220310120532411](pic/41_dac_app_use_spidev.png) -![image-20220310120646206](pic/42_dac_app_use_spidev.png) diff --git a/STM32MP157/doc_pic/11_SPI/07_DAC模块上机实验.tif b/STM32MP157/doc_pic/11_SPI/07_DAC模块上机实验.tif new file mode 100644 index 0000000..e9c0fe4 Binary files /dev/null and b/STM32MP157/doc_pic/11_SPI/07_DAC模块上机实验.tif differ diff --git a/STM32MP157/doc_pic/11_SPI/pic/43_dac_on_stm32mp157.png b/STM32MP157/doc_pic/11_SPI/pic/43_dac_on_stm32mp157.png new file mode 100644 index 0000000..ccdc4bc Binary files /dev/null and b/STM32MP157/doc_pic/11_SPI/pic/43_dac_on_stm32mp157.png differ diff --git a/STM32MP157/doc_pic/11_SPI/pic/44_imx6ull_pro_extend_spi_a.png b/STM32MP157/doc_pic/11_SPI/pic/44_imx6ull_pro_extend_spi_a.png new file mode 100644 index 0000000..6dc64e6 Binary files /dev/null and b/STM32MP157/doc_pic/11_SPI/pic/44_imx6ull_pro_extend_spi_a.png differ diff --git a/STM32MP157/doc_pic/11_SPI/pic/45_stm32mp157_pro_extend_spi_a.png b/STM32MP157/doc_pic/11_SPI/pic/45_stm32mp157_pro_extend_spi_a.png new file mode 100644 index 0000000..44f1f1a Binary files /dev/null and b/STM32MP157/doc_pic/11_SPI/pic/45_stm32mp157_pro_extend_spi_a.png differ diff --git a/STM32MP157/doc_pic/11_SPI/pic/46_boot_mount.png b/STM32MP157/doc_pic/11_SPI/pic/46_boot_mount.png new file mode 100644 index 0000000..a3967b7 Binary files /dev/null and b/STM32MP157/doc_pic/11_SPI/pic/46_boot_mount.png differ diff --git a/STM32MP157/source/A7/11_SPI/02_dac_use_spidev/dac_test.c b/STM32MP157/source/A7/11_SPI/02_dac_use_spidev/dac_test.c index b613bda..00e8306 100644 --- a/STM32MP157/source/A7/11_SPI/02_dac_use_spidev/dac_test.c +++ b/STM32MP157/source/A7/11_SPI/02_dac_use_spidev/dac_test.c @@ -61,7 +61,7 @@ int main(int argc, char **argv) /* 打印 */ val = (rx_buf[0] << 8) | (rx_buf[1]); - val >>= 1; + val >>= 2; printf("Pre val = %d\n", val);