主题中讨论的其他器件:C2000WARE、 DRV8311、 DRV8376、DRV8316
工具与软件:
您好!
我已经在这方面工作了一段时间,我真的很困倦,所以任何帮助都会被感激!
我有一个代码可以与具有16位 SPI 帧的 DRV 器件通信、我正在尝试更改此代码、以便可以与具有24位 SPI 帧的另一个 DRV 器件通信。
以下是我之前尝试转换的 SPI 帧的当前代码:
void Config_evm_spi(void) { //Pin Config EALLOW; // SPI_MOSI GPIO_SetupPinOptions(16, GPIO_INPUT, GPIO_ASYNC | GPIO_PULLUP); // SPI_MISO GPIO_SetupPinOptions(17, GPIO_INPUT, GPIO_ASYNC | GPIO_PULLUP); // SPI_CS GPIO_SetupPinOptions(56, GPIO_INPUT, GPIO_ASYNC | GPIO_PULLUP); // SPI_CLK GPIO_SetupPinOptions(57, GPIO_INPUT, GPIO_ASYNC | GPIO_PULLUP); GPIO_SetupPinMux(16, GPIO_MUX_CPU1, 1); GPIO_SetupPinMux(17, GPIO_MUX_CPU1, 1); GPIO_SetupPinMux(56, GPIO_MUX_CPU1, 1); GPIO_SetupPinMux(57, GPIO_MUX_CPU1, 1); EDIS; EALLOW; ClkCfgRegs.LOSPCP.all = 0; EDIS; // Initialize SPI FIFO registers SpiaRegs.SPIFFTX.all=0xE040; SpiaRegs.SPIFFRX.all=0x2044; SpiaRegs.SPIFFCT.all=0x0; //SPI Settings SpiaRegs.SPICCR.bit.SPISWRESET = 0; //SPI Reset On SpiaRegs.SPICCR.bit.CLKPOLARITY = 0; //SCLK Active High SpiaRegs.SPICCR.bit.SPICHAR = 0xF; //16-bit SPI char SpiaRegs.SPICCR.bit.SPILBK = 0; SpiaRegs.SPICTL.bit.OVERRUNINTENA = 0; //No overrun interrupt SpiaRegs.SPICTL.bit.CLK_PHASE = 0; //Phase 0 SpiaRegs.SPICTL.bit.MASTER_SLAVE = 1; //Master mode SpiaRegs.SPICTL.bit.TALK = 1; //nSCS enabled SpiaRegs.SPICTL.bit.SPIINTENA = 0; //TX/RX Interrupt Disabled SpiaRegs.SPIBRR.bit.SPI_BIT_RATE = ((25000000 / 1000000) - 1); //Set baud rate to 1MHz SpiaRegs.SPIPRI.bit.FREE = 1; //Set so breakpoints don't disturb transmission SpiaRegs.SPICCR.bit.SPISWRESET = 1; //Exit SPI reset } Uint16 spi_xmit(Uint16 spiFrame) { SpiaRegs.SPITXBUF=spiFrame; //Wait for RX flag to indicate SPI frame completion while(SpiaRegs.SPIFFRX.bit.RXFFST != 1) { } return SpiaRegs.SPIRXBUF; } Uint16 spi_read(Uint16 addr) { Uint16 commandword = 0; uint16_t p_addr = addr; uint16_t p_data = 0; uint16_t calc = ((p_addr << 9) & 0x7E00) | (p_data & 0x00FF); uint16_t parity = 0; while(calc) { parity ^= (calc & 1); calc >>= 1; } commandword = (0x8000 | (addr << 9) | (parity << 8)); return spi_xmit(commandword); } Uint16 spi_write(Uint16 addr, Uint16 data) { Uint16 commandword = 0; uint16_t p_addr = addr; uint16_t p_data = data; uint16_t calc = ((p_addr << 9) & 0x7E00) | (p_data & 0x00FF); uint16_t parity = 0; while(calc) { parity ^= (calc & 1); calc >>= 1; } commandword = ((addr << 9) | (parity << 8) | data); return spi_xmit(commandword); }
我暂时主要关注 SPI_READ 函数、我想如果可以的话、就能算出 SPI_WRITE
下面是我的尝试,它不起作用:
void Config_evm_spi(void) { //Pin Config EALLOW; // SPI_MOSI GPIO_SetupPinOptions(16, GPIO_INPUT, GPIO_ASYNC | GPIO_PULLUP); // SPI_MISO GPIO_SetupPinOptions(17, GPIO_INPUT, GPIO_ASYNC | GPIO_PULLUP); // SPI_CS GPIO_SetupPinOptions(56, GPIO_INPUT, GPIO_ASYNC | GPIO_PULLUP); // SPI_CLK GPIO_SetupPinOptions(57, GPIO_INPUT, GPIO_ASYNC | GPIO_PULLUP); GPIO_SetupPinMux(16, GPIO_MUX_CPU1, 1); GPIO_SetupPinMux(17, GPIO_MUX_CPU1, 1); GPIO_SetupPinMux(56, GPIO_MUX_CPU1, 1); GPIO_SetupPinMux(57, GPIO_MUX_CPU1, 1); EDIS; EALLOW; ClkCfgRegs.LOSPCP.all = 0; EDIS; // Initialize SPI FIFO registers SpiaRegs.SPIFFTX.all = 0xE040; SpiaRegs.SPIFFRX.all = 0x2044; SpiaRegs.SPIFFCT.all = 0x0; //SPI Settings SpiaRegs.SPICCR.bit.SPISWRESET = 0; //SPI Reset On SpiaRegs.SPICCR.bit.CLKPOLARITY = 0; //SCLK Active High SpiaRegs.SPICCR.bit.SPICHAR = 0xF; //16-bit SPI char SpiaRegs.SPICCR.bit.SPILBK = 0; SpiaRegs.SPICTL.bit.OVERRUNINTENA = 0; //No overrun interrupt SpiaRegs.SPICTL.bit.CLK_PHASE = 0; //Phase 0 SpiaRegs.SPICTL.bit.MASTER_SLAVE = 1; //Master mode SpiaRegs.SPICTL.bit.TALK = 1; //nSCS enabled SpiaRegs.SPICTL.bit.SPIINTENA = 0; //TX/RX Interrupt Disabled SpiaRegs.SPIBRR.bit.SPI_BIT_RATE = ((25000000 / 1000000) - 1); //Set baud rate to 1MHz SpiaRegs.SPIPRI.bit.FREE = 1; //Set so breakpoints don't disturb transmission SpiaRegs.SPICCR.bit.SPISWRESET = 1; //Exit SPI reset } Uint32 tspi_xmit(Uint32 spiFrame) { SpiaRegs.SPITXBUF = spiFrame; //Wait for RX flag to indicate SPI frame completion while(SpiaRegs.SPIFFRX.bit.RXFFST != 1) { } return SpiaRegs.SPIRXBUF; } Uint32 tspi_read(Uint16 id, Uint16 addr) { Uint32 commandword = 0; Uint16 data = 0; // No data to read Uint16 p_addr = addr & 0x3F; // Ensure address is 6 bits (0-63) // Construct the command word commandword = (data & 0x7FFF) | (1 << 16) | (p_addr << 17); // 1 for read // Calculate even parity for bits 0-15 and bits 16-23 Uint16 parity0 = 0, parity1 = 0; int i; for (i = 0; i < 16; i++) { parity0 ^= (commandword >> i) & 1; } for (i = 16; i < 24; i++) { parity1 ^= (commandword >> i) & 1; } // Set the parity bits commandword |= (parity0 << 15) | (parity1 << 23); return tspi_xmit(commandword); } Uint32 tspi_write(Uint16 id, Uint16 addr, Uint16 data) { Uint32 commandword = 0; Uint16 p_addr = addr & 0x3F; // Ensure address is 6 bits (0-63) // Construct the command word commandword = (data & 0x7FFF) | (0 << 16) | (p_addr << 17); // 0 for write // Calculate even parity for bits 0-15 and bits 16-23 Uint16 parity0 = 0, parity1 = 0; int i; for (i = 0; i < 16; i++) { parity0 ^= (commandword >> i) & 1; } for (i = 16; i < 24; i++) { parity1 ^= (commandword >> i) & 1; } // Set the parity bits commandword |= (parity0 << 15) | (parity1 << 23); return tspi_xmit(commandword); }
谢谢!
Yara