cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_SPI_ functions to read rc522 register have echo!

DAbba
Associate II
 
1 REPLY 1
DAbba
Associate II

Hi every one

I am trying to read registers from RC522 by using Stm32f103 and HAL_spi function .

The problem is when I put in a loop keep reading a register -for example VersionReg           0x37 the only first time I receive 0x92 which seems OK , but the next time and I receive 0xee .

here is my code and configuration , could anyne help me , I would appropriate.

void MX_SPI1_Init(void)
{
 
	/* USER CODE BEGIN SPI1_Init 0 */
 
	/* USER CODE END SPI1_Init 0 */
 
	/* USER CODE BEGIN SPI1_Init 1 */
 
	/* USER CODE END SPI1_Init 1 */
	/* SPI1 parameter configuration*/
	hspi1.Instance = SPI1;
	hspi1.Init.Mode = SPI_MODE_MASTER;
	hspi1.Init.Direction = SPI_DIRECTION_2LINES;
	hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
	hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
	hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
	hspi1.Init.NSS =SPI_NSS_HARD_OUTPUT;// SPI_NSS_SOFT;
	hspi1.Init.BaudRatePrescaler =  SPI_BAUDRATEPRESCALER_64;
	hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
	hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
	hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
	hspi1.Init.CRCPolynomial = 7;
	//HAL_Delay(5);
	if (HAL_SPI_Init(&hspi1) != HAL_OK)
	{
		Error_Handler();
	}
	/* USER CODE BEGIN SPI1_Init 2 */
	//__HAL_SPI_ENABLE(&hspi1);
	/* USER CODE END SPI1_Init 2 */
 
}
 
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hspi->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */
 
  /* USER CODE END SPI1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();
 
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**SPI1 GPIO Configuration
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  /* USER CODE BEGIN SPI1_MspInit 1 */
  //  HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
  //    HAL_NVIC_EnableIRQ(SPI1_IRQn);
  /* USER CODE END SPI1_MspInit 1 */
  }
 
}
 
 
unsigned char ReadRawRC(unsigned char Address)
{
 
	uint8_t val;
	uint8_t buf;
	HAL_GPIO_WritePin(RD_SS_Port, RD_SS_Pin, GPIO_PIN_RESET);
	buf = ((Address << 1) & 0x7E) | 0x80;
	if (HAL_SPI_Transmit(&hspi1, &buf, 1, 0xFFFF) == HAL_OK)
	{
		buf = MFRC522_DUMMY;
		HAL_SPI_TransmitReceive(&hspi1, &buf, &val, 1, 0xFFFF);
 
	}
	HAL_GPIO_WritePin(RD_SCK_Port, RD_SCK_Pin, GPIO_PIN_SET);
return val;
 
 
}
 
 
int main(void)
{
	/* USER CODE BEGIN 1 */
	unsigned char status;
	unsigned char g_ucTempbuf[20];
	uint8_t keyboard;
	uint8_t i,temp;
	/* 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_SPI1_Init();
 
	MX_USART1_UART_Init();
	HAL_UART_Receive_IT(&huart1, g_ucTempbuf, 1);
 
	//MX_USART2_UART_Init();
	RetargetInit(&huart1);
 
	MX_RTC_Init();
	MX_I2C1_Init();
	//HAL_SPI_Receive_IT(&hspi1,g_ucTempbuf,1);
	/* USER CODE BEGIN 2 */
	HAL_UART_Transmit(&huart1, "_DARIUSH_ \r\n", 11, 100);
	HAL_UART_Transmit(&huart2, "ABBASI ", 8, 100);
	printf(" time=%d\r\n",Time.Seconds);
	printf(" %s\r\n",__DATE__);
	printf(" %s\r\n",__TIME__);
	printf(" time=%d\r\n",Time.Seconds);
	led_ticks=ticksCount;
	mif_ticks=ticksCount;
	read_keys_ticks=ticksCount;
	init_func();
	//HAL_Delay(600);
 
	//	HAL_SPI_Receive(&hspi1,g_ucTempbuf,1,2000);
	//status=ReadRawRC(ComIEnReg);
	//	printf(" version_reg=%x\r\n",status);
		while (1)
 
		{
 
			status=ReadRawRC(VersionReg);
			printf(" version_reg=%x\r\n",status);
 
			HAL_Delay(4000);
		}
}