主題:[求助]6800 SPI通訊的問(wèn)題 共有60388人關(guān)注過(guò)本帖 |
---|
![]() ardy |
1樓 信息 | 搜索 | 郵箱 |
![]() ![]() ![]() ![]() |
[求助]6800 SPI通訊的問(wèn)題 ![]() 我用6800的核心板通過(guò)spi接口與外部芯片通訊,程序基本是光盤(pán)里的示例程序,就是改了發(fā)送數(shù)據(jù)的內(nèi)容,代碼如下: static const char *device = "/dev/spidev1.0"; static uint8_t mode = 3; static uint8_t bits = 8; static uint32_t speed = 5000000; static uint16_t delay; uint8_t tx[] = { 0x55, 0x00, 0x84, 0x00,0x08,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00 }; uint8_t rx[ARRAY_SIZE(tx)]; struct spi_ioc_transfer tr[2]; void transfer(int fd) { int ret; int i1; printf( "transfer\n"); tr[0].tx_buf = (unsigned long)tx; tr[0].rx_buf = (unsigned long)rx; tr[0].len = ARRAY_SIZE(tx); tr[0].delay_usecs = delay; tr[0].speed_hz = speed; tr[0].bits_per_word = bits; /*tr[1].tx_buf = NULL; tr[1].rx_buf = (unsigned long)rx; tr[1].len = 1; tr[1].delay_usecs = 0; tr[1].speed_hz = speed; tr[1].bits_per_word = bits;*/ ret = ioctl(fd, SPI_IOC_MESSAGE(1), tr ); if (ret < ARRAY_SIZE(tx) ) pabort("can't send spi message"); printf("ret=%d\n", ret ); for( i1=0; i1<ret; i1++ ) printf("%.2X ", rx[i1] ); printf("\n"); } int main( int argc, char *argv[] ) { char wr_buf[]={ 0x1b,0x55,0x1f,0x0f,0xff,0x00,0x1f,0x0f, 0xff,0x00 }; char rd_buf[10]; int i; int ret = 0; int fd; fd = open(device, O_RDWR); if (fd < 0) pabort("can't open device"); /* * spi mode */ ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); if (ret == -1) pabort("can't set spi mode"); ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); if (ret == -1) pabort("can't get spi mode"); /* * bits per word */ ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't set bits per word"); ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't get bits per word"); /* * max speed hz */ ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't set max speed hz"); ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't get max speed hz"); printf("spi mode: %d\n", mode); printf("bits per word: %d\n", bits); printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); if( argc > 1 ) { if( write(fd, wr_buf, 2) != 2 ) perror("Write Error"); } else transfer( fd ); close(fd); return ret; } 程序運(yùn)行后的顯現(xiàn)是沒(méi)有收到設(shè)備返回的數(shù)據(jù), 命令行顯示結(jié)果是: 用示波器監(jiān)測(cè)CS和MOSI信號(hào)波形如下:紅色是CS信號(hào),藍(lán)色是MOSI信號(hào),發(fā)送的數(shù)據(jù)是正確的,但是發(fā)現(xiàn)CS信號(hào)在每發(fā)送一個(gè)字節(jié)后都會(huì)變?yōu)楦唠娖剑皇窃谡麄(gè)報(bào)文發(fā)送過(guò)程中始終保存低電平,可能就是這個(gè)問(wèn)題導(dǎo)致了連接的spi芯片不響應(yīng)。請(qǐng)問(wèn)如何設(shè)置能夠在一幀數(shù)據(jù)發(fā)送的全過(guò)程中,CS信號(hào)始終保持低電平。 另外我使用的通訊芯片,還要求在開(kāi)始發(fā)送前,CS信號(hào)先保持低電平一段時(shí)間,如何通過(guò)程序手工控制CS信號(hào)的狀態(tài)? |
單帖管理 | 引用 | 回復(fù) ![]() |
![]() ardy |
2樓 信息 | 搜索 | 郵箱 |
![]() ![]() ![]() ![]() |
[求助]6800 SPI通訊的問(wèn)題 ![]() 我在程序中確實(shí)是只調(diào)用了一次ioctl操作傳輸數(shù)據(jù),buf長(zhǎng)度就13個(gè)byte。但是事實(shí)情況就是變成了13次片選操作。有其它初始化設(shè)置會(huì)導(dǎo)致這種情況嗎? |
單帖管理 | 引用 | 回復(fù) ![]() |
![]() ardy |
3樓 信息 | 搜索 | 郵箱 |
![]() ![]() ![]() ![]() |
[求助]6800 SPI通訊的問(wèn)題 ![]() 我的郵箱:83699938@qq.com 謝謝! |
單帖管理 | 引用 | 回復(fù) ![]() |