为了确定每个片选芯片都是有效的,编写了新的测试程序,其中STM32通过GPIO中断来判断片选信号是否触发。
测试结果验证了两个片选信号分别有效,那么问题就出在SPI信号收发的过程中,一步步缩小了问题范围。
#include "rt_spi.h"
// #include "PeriodicTask.h"
#include <cmath>
#include <iostream>
#include <unistd.h>
int spi_1_fd = -1;
int spi_2_fd = -1;
const char* name1 = "/dev/spidev0.0";
const char* name2 = "/dev/spidev0.1";
int main()
{
spi_open(spi_1_fd, name1);
uint8_t tx[16];
uint8_t rx_1[16];
uint8_t rx_2[16];
for (int i = 0; i < 16; ++i)
{
tx[i] = i;
}
int rv = transfer(spi_1_fd, tx, rx_1, 16);
(void)rv;
auto str = hex2str(rx_1, 16);
std::cout << name1 << ": "<< str << std::endl;
spi_close(spi_1_fd);
return 0;
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CAN1_Init();
MX_CAN2_Init();
MX_SPI1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
for(int i = 0; i < 16; ++i)
{
tx[i] = i;
}
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == SPI_CS_Pin)
{
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
}
}