spi5 of my STM32MP157F-K2 CAN'T read data from the RC522 CARD

26 Views Asked by At

i was trying to connect the rfid card to spi5 of the stm32MP157 since im a totally beginner in the cube ide i had a problem to understand the reasons. so i m gonna share with u the clock configuration and the spi5 configuration plus the rc522.c related to my code spi5

 hspi5.Instance = SPI5;
  hspi5.Init.Mode = SPI_MODE_MASTER;
  hspi5.Init.Direction = SPI_DIRECTION_2LINES;
  hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi5.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi5.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi5.Init.NSS = SPI_NSS_SOFT;
  hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  hspi5.Init.FirstBit = SPI_FIRSTBIT_LSB;
  hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi5.Init.CRCPolynomial = 0x0;
  hspi5.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  hspi5.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
  hspi5.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
  hspi5.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
  hspi5.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
  hspi5.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
  hspi5.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
  hspi5.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
  hspi5.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
  hspi5.Init.IOSwap = SPI_IO_SWAP_DISABLE;

clock configuration


  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure LSE Drive Capability
  */
  HAL_PWR_EnableBkUpAccess();
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_MEDIUMHIGH);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_CSI|RCC_OSCILLATORTYPE_HSI
                              |RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS_DIG;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDivValue = RCC_HSI_DIV1;
  RCC_OscInitStruct.CSIState = RCC_CSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL2.PLLSource = RCC_PLL12SOURCE_HSE;
  RCC_OscInitStruct.PLL2.PLLM = 3;
  RCC_OscInitStruct.PLL2.PLLN = 66;
  RCC_OscInitStruct.PLL2.PLLP = 2;
  RCC_OscInitStruct.PLL2.PLLQ = 1;
  RCC_OscInitStruct.PLL2.PLLR = 1;
  RCC_OscInitStruct.PLL2.PLLFRACV = 0x1400;
  RCC_OscInitStruct.PLL2.PLLMODE = RCC_PLL_FRACTIONAL;
  RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL3.PLLSource = RCC_PLL3SOURCE_HSE;
  RCC_OscInitStruct.PLL3.PLLM = 2;
  RCC_OscInitStruct.PLL3.PLLN = 56;
  RCC_OscInitStruct.PLL3.PLLP = 4;
  RCC_OscInitStruct.PLL3.PLLQ = 50;
  RCC_OscInitStruct.PLL3.PLLR = 37;
  RCC_OscInitStruct.PLL3.PLLRGE = RCC_PLL3IFRANGE_1;
  RCC_OscInitStruct.PLL3.PLLFRACV = 4096;
  RCC_OscInitStruct.PLL3.PLLMODE = RCC_PLL_FRACTIONAL;
  RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL4.PLLSource = RCC_PLL4SOURCE_HSE;
  RCC_OscInitStruct.PLL4.PLLM = 4;
  RCC_OscInitStruct.PLL4.PLLN = 99;
  RCC_OscInitStruct.PLL4.PLLP = 6;
  RCC_OscInitStruct.PLL4.PLLQ = 8;
  RCC_OscInitStruct.PLL4.PLLR = 8;
  RCC_OscInitStruct.PLL4.PLLRGE = RCC_PLL4IFRANGE_0;
  RCC_OscInitStruct.PLL4.PLLFRACV = 0;
  RCC_OscInitStruct.PLL4.PLLMODE = RCC_PLL_INTEGER;

main code

HAL_Delay(1000);
       status = MFRC522_Request(PICC_REQIDL, str);
        status = MFRC522_Anticoll(str);
        memcpy(sNum, str, 5);
        HAL_Delay(100);

        // Print out the entire str array
        printf("Received data: ");
        for (int i = 0; i < 5; i++) {
            printf("%02X ", str[i]);
        }
        printf("\n");

        // Print out individual bytes
        printf("Byte 0: %02X\n", str[0]);
        printf("Byte 1: %02X\n", str[1]);
        // Repeat for other bytes...

        if((str[0]==115) && (str[1]==93) && (str[2]==75) && (str[3]==22) && (str[4]==115))
        {
            HAL_GPIO_WritePin(GPIOH, GPIO_PIN_7, 0);
            HAL_Delay(100);
        }
        else if((str[0]==199) && (str[1]==102) && (str[2]==209) && (str[3]==215) && (str[4]==167))
        {
            HAL_GPIO_WritePin(GPIOH, GPIO_PIN_7, 0);
            HAL_Delay(2000);
        }
        else
        {
            HAL_GPIO_WritePin(GPIOH, GPIO_PIN_7, 1);
        }

function transmit data in rc522.c

HAL_StatusTypeDef RC522_SPI_Transmit(uint8_t data) {
    return HAL_SPI_Transmit(HSPI_INSTANCE, &data, 1, HAL_MAX_DELAY);
}

// Function to receive data over SPI
HAL_StatusTypeDef RC522_SPI_Receive(uint8_t *rx_data) {
    return HAL_SPI_Receive(HSPI_INSTANCE, rx_data, 1, HAL_MAX_DELAY);
}

// Combined function to transmit and receive data over SPI
uint8_t RC522_SPI_Transfer(uint8_t data) {
    uint8_t rx_data;

    // Transmit data
    if (RC522_SPI_Transmit(data) != HAL_OK) {
        // Handle transmit error
        return 0; // Or any error handling mechanism you prefer
    }

    // Receive data
    if (RC522_SPI_Receive(&rx_data) != HAL_OK) {
        // Handle receive error
        return 0; // Or any error handling mechanism you prefer
    }

    return rx_data;
}

Read_MFRC522

uchar Read_MFRC522(uchar addr)
{
    uchar data = 0x00;
    uchar val = 0x00;
    /* CS LOW */
//  HAL_GPIO_WritePin(GPIOG,3,GPIO_PIN_RESET);

      // even though we are calling transfer frame once, we are really sending
      // two 8-bit frames smooshed together-- sending two 8 bit frames back to back
      // results in a spike in the select line which will jack with transactions
      // - top 8 bits are the address. Per the spec, we shift the address left
      //   1 bit, clear the LSb, and set the MSb to indicate a read
      // - bottom 8 bits are all 0s on a read per 8.1.2.1 Table 6
    RC522_SPI_Transfer(((addr<<1)&0x7E) | 0x80);
    HAL_SPI_TransmitReceive(HSPI_INSTANCE,&data,&val,1,HAL_MAX_DELAY);
//  val = RC522_SPI_Transfer(0x00);

    /* CS HIGH */
//  HAL_GPIO_WritePin(GPIOG,3,GPIO_PIN_SET);

    return val;

}

i double checked the wiring even i tried to use spi4 and it didn't work i tried fix the baud rate to 9Mbites also i changed the spi and the clock configuration still there are no data to read i have been stuck in here for a while know i want know if anyone have faced the same problem before ? is it a problem with the spi ? or the configuration ? or the function RC522_SPI_Transfer ?

0

There are 0 best solutions below