[GD32L233C-START Review] - 6. SPI drives RC522 card swiping
[Copy link]
I just wanted to do a card swiping test, so I started using GD
I bought the cheap RC522 from the Internet. RST is connected to PB0, SDA is connected to PB12, SCK is connected to PB13, MISO is connected to PB14, and MOSI is connected to PB15.
It can be found in the GD32 MCU manual that using spi needs to be remapped to AF6
So, I found a big pit.
There was always a problem with PB12. Later I checked other testers' articles and found that it was OK if I only mapped PB13-PB15. I wonder if other testers have encountered similar problems.
#define SPI_CS_PORT GPIOB
#define SPI_CS_PIN GPIO_PIN_12
#define RST_522_PORT GPIOB
#define RST_522_PIN GPIO_PIN_0
#define RCU_SPI_PORT RCU_GPIOB
#define SPI_CS_HIGH gpio_bit_set(SPI_CS_PORT, SPI_CS_PIN)
#define SPI_CS_LOW gpio_bit_reset(SPI_CS_PORT, SPI_CS_PIN)
#define RST_522_HIGH gpio_bit_set(RST_522_PORT, RST_522_PIN)
#define RST_522_LOW gpio_bit_reset(RST_522_PORT, RST_522_PIN)
The initialization code after the macro definition is as follows
void SpiInit(void)
{
spi_parameter_struct spi_init_struct;
rcu_periph_clock_enable(RCU_SPI_PORT);
rcu_periph_clock_enable(RCU_SPI1);
gpio_mode_set(RST_522_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RST_522_PIN );
gpio_output_options_set(RST_522_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RST_522_PIN);
/* SPI1 GPIO configuration: NSS/PB12 */
gpio_mode_set(SPI_CS_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, SPI_CS_PIN );
gpio_output_options_set(SPI_CS_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SPI_CS_PIN);
/* SPI1 GPIO configuration: SCK/PB13, MISO/PB14, MOSI/PB15 */
gpio_af_set(GPIOB, GPIO_AF_6, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
/* deinitilize SPI and the parameters */
spi_i2s_deinit(SPI1);
spi_struct_para_init(&spi_init_struct);
/* SPI1 parameter configuration */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_8;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI1, &spi_init_struct);
spi_crc_polynomial_set(SPI1, 7);
spi_enable(SPI1);
}
The code of Rc522 is copied from the previous stm32 project.
uint8_t Scan_Card(void)
{
uint8_t status, i, snr;
static u8 Key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
//static u8 Tempbuf[16] ={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C};;
u8 CardID[5];
status = MI_NOTAGERR;
for(i = 0; i < 4; i++)//将存放卡号的数组清空
CardID[i] = 0;
status = PcdRequest(PICC_REQALL, CardID);//寻卡,CardID存放卡类型
if(status == MI_OK)
{
status = PcdAnticoll(CardID);//防冲撞
if(status == MI_OK)
{
//printf ( "\r\n防冲撞OK" );
status = PcdSelect(CardID);//选定卡片--参数为卡的序列号
if(status == MI_OK)
{
printf ( "\r\n选定卡片OK" );
// snr = 8;
// status = PcdAuthState(PICC_AUTHENT1A, (snr * 4 - 1), Key, CardID);
// //验证密码
// if(status == MI_OK)
// printf ( "\r\n验证密码成功" );
// else
// printf ( "\r\n验证密码失败" );
//
//Display_CARD( CardID [ 0 ], CardID [ 1 ], CardID [ 2 ], CardID [ 3 ] );
printf("\r\n Scan_Card No.: %d-%d-%d-%d",CardID [ 0 ], CardID [ 1 ], CardID [ 2 ], CardID [ 3 ]);
return MI_OK;
}
}
}
else
status = MI_NOTAGERR;
return status;
}
After initialization, call Scan_Card in the main loop to see the effect
|