uint8_t Spi_RW_Data(uint8_t *p, uint8_t len)//读写函数 { uint8_t pdata[len]; HAL_SPI_TransmitReceive(&g_spi2_handler, p, pdata,len, 0XFFFF); return pdata[len - 1]; }len应该换成什么
时间: 2023-06-16 13:04:39 浏览: 91
在函数声明中,len是一个不确定的参数,应该使用动态内存分配函数来分配一个可变长度的数组。例如,可以使用malloc函数来动态分配len个字节的内存,然后在函数结束时释放内存。函数实现可以改为:
```c
uint8_t Spi_RW_Data(uint8_t *p, uint8_t len) {
uint8_t *pdata = (uint8_t*)malloc(len); // 动态分配len个字节的内存
HAL_SPI_TransmitReceive(&g_spi2_handler, p, pdata, len, 0XFFFF);
uint8_t result = pdata[len - 1];
free(pdata); // 释放内存
return result;
}
```
相关问题
void SPI1_1B_WR(uint32_t addr, uint32_t data) { uint32_t addr_real = addr << 8; //addr_real[31:0] = {addr[31:8], 8'h00} uint32_t wdata = 0x5a000000; //int32_t spi_setup_cmd_addr(SPI_TypeDef *spi, uint32_t cmd, uint32_t cmdlen, uint32_t addr, uint
32_t addrlen, uint32_t dummylen) {
uint32_t cmd = 0x02; // Write command
uint32_t cmdlen = 8; // Command length in bits
uint32_t addrlen = 24; // Address length in bits
uint32_t dummylen = 8; // Dummy length in bits
// Construct the command word
uint32_t cmd_word = (cmd << 24) | (cmdlen << 16) | (addrlen << 8) | dummylen;
// Send the command word
SPI_SendData(SPI1, cmd_word);
// Construct the address word
uint32_t addr_word = addr_real | (data & 0xFF);
// Send the address and data word
SPI_SendData(SPI1, addr_word);
// Wait for transfer to complete
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY));
// Clear the RX buffer
SPI_ReceiveData(SPI1);
}
SPI1_Read_Buff_Reg(uint8_t* reg_buff,uint8_t*buff,uint8_t len)
这是一个关于代码实现的问题,我可以回答。SPI1_Read_Buff_Reg(uint8_t* reg_buff,uint8_t*buff,uint8_t len)是一个函数,它的功能是读取SPI1总线上指定长度(len)的数据到缓冲区(buff)中,并把寄存器地址(reg_buff)的值读出来。具体实现可以参考对应的代码。
阅读全文