MCU作为SPI从器件,读取数据

时间:2018-05-29 09:40:15

标签: c spi master-slave keil hal

我有一个我无法控制它的主人只是发送数据时钟。我把我的stm32f4称为全双工奴隶。我设法将它们与HAL功能同步:

HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)spi_buffer, (uint8_t *)spi_buffer, 16, 2000);  
      // receive 16 bytes then send them for debugg

我从slave发送我从master获取的字节,用于调试, 和数据是一样的,所以它完美同步,看起来像奴隶读取所有数据主发送。这是图像(抱歉,不能用stackoverflow下载它,因为我的点数很低):

  1. https://imgur.com/wa6rF5J - ocsillo的形象
  2. https://imgur.com/a/ZVCPaNT - 放大
  3. 问题是我不知道如何读取这些数据,我需要以某种方式读取它的16字节而不会减慢spi的速度。我尝试这样做的所有尝试都失败了。 整个代码是:

        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_DMA_Init();
      MX_USB_DEVICE_Init();
      MX_SPI1_Init();
      /* USER CODE BEGIN 2 */
      /* USER CODE END 2 */
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
            uint8_t collect_bits[16] = {0};
            char string_array[21] = {0};
            uint8_t spi_buffer[16] ={0} ;
            uint8_t flag_when_start_print = 0;
            spi_buffer[0] = 5;
            uint8_t spi_transmit[16] = {0};
    
            HAL_SPI_Init(&hspi1); //init spi
    
      while (1)
      {
      HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)spi_buffer, (uint8_t *)spi_buffer, 16, 2000);
      if (spi_buffer[0] == 0xFE) // just tring to catch 0xFE, doesnt work
    {   
        sprintf(string_array, "%X", spi_buffer[0]); // just simple convertation for PC output
    CDC_Transmit_FS((uint8_t*)string_array, sizeof(string_array));   // print result. }
        }
       }
    

    重要的事实是,我想要捕获的信息的开始总是以0xFE0010开头,所以在那些字节之后我需要读取下一个13字节

0 个答案:

没有答案