IMX6ULL:
原理图:
接线图:
操作过程:
注意:
使用SPI传输的细节:
输出电压 = 2 * VREFIN * n / 1024 = 2 * 2.048 * n / 1024
其中:n为10位数值
确认SPI时钟最大频率
这里一个周期的时间就是
T
W
(
C
L
)
+
T
W
(
C
H
)
=
25
+
25
=
50
n
s
T_W(CL)+T_W(CH) = 25 + 25 = 50ns
TW(CL)+TW(CH)=25+25=50ns
对应的频率就是
1
/
50
n
s
=
20
∗
1
0
6
=
20
M
H
z
1/50ns=20*10^6=20MHz
1/50ns=20∗106=20MHz
设备树如下:
dac: dac@00 {
compatible = "spidev";
reg = <0>;
spi-max-frequency = <20000000>; //从DAC芯片手册获得
};
需要开启user spi support
-> Device Drivers
-> SPI support (SPI [=y])
< > User mode SPI device driver support
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* dac_test /dev/spidevB.D */
int main(int argc, char **argv)
{
int fd;
unsigned int val;
struct spi_ioc_transfer xfer[1];
unsigned char tx_buf[2];
unsigned char rx_buf[2];
int status;
if(argc != 3) {
printf("Usage: %s /dev/spidevB.D \n" , argv[0]);
return 0;
}
fd = open(argv[1], O_RDWR);
if (fd < 0) {
printf("can not open %s\n", argv[1]);
return 1;
}
val = strtoul(argv[2], NULL, 0);
val <<= 2; /* bit0, bit1 = 0b00 */
val &= 0xFFC; /* only reserve 10 bit */
tx_buf[0] = (val>>8) & 0xff;
tx_buf[1] = val & 0xff;
memset(xfer, 0, sizeof xfer);
xfer[0].len = 2;
xfer[0].tx_buf = (unsigned long) tx_buf;
xfer[0].rx_buf = (unsigned long) rx_buf;
status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);
if (status < 0) {
printf("SPI_IOC_MESSAGE\n");
return -1;
}
/* print result */
val = (rx_buf[0] <<8)|(rx_buf[1]);
val >>= 2;
printf("pre val = %d\n", val);
return 0;
}
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 0
pre val = 0
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 100
pre val = 0
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 100 0
pre val = 100
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 100
^C
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 100 100
0
pre val = 100
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 100 ^C
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 200
0
pre val = 1000
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 200 1500
Usage: ./dac_test /dev/spidevB.D <val>
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 100
0
pre val = 976
[root@firefly-rk3288:/home/picture/spi]# ./dac_test /dev/spidev0.0 100 500
pre val = 1000