小弟最近在做从51上驱动RC500读卡器移植到STM32上的实验。
知道STM32是3.3V芯片,RC500是5V芯片.
硬件电路我是这样做的:
使用RC500的独立读/写选通的复用地址总线的连接方式,使用STM32的5V兼容引脚加5V上拉电阻和RC500相连
采用模拟总线的方式,底层相关驱动代码如下:/**
* @name static void GPIO_Write1(u8 value)
* [url=home.php?mod=space&uid=247401]@brief[/url] GPIOBÒý½Å¸ß8λÊä³öÊý¾Ý
* @param value ÒªÊä³öµÄÊý¾Ý
* @retval None
*/
void GPIOB_Write1(u8 value)
{
RC500_Adress_Data_GPIO_Config(GPIO_Mode_Out_OD);//ÅäÖÃΪ¿ªÂ©Êä³ö
GPIO_SetBits(GPIOB, (value<<8) & 0xffff);
GPIO_ResetBits(GPIOB, (((~value)<<8) & 0xffff));
}
/**
* @name static void GPIO_Write1(u8 value)
* @brief ¶ÁÈ¡GPIOBÒý½Å¸ß8λÊý¾Ý
* @param None
* @retval ·µ»Ø´ÓGPIOBÒý½Å¸ß8λ¶ÁÈ¡µÄÊý¾Ý
*/
u8 GPIOB_Read(void)
{
u8 value = 0;
u16 temp = 0;
GPIOB_Write1(0XFF);
Delay_1ms(1);
RC500_Adress_Data_GPIO_Config(GPIO_Mode_IN_FLOATING);//ÅäÖÃΪ¸¡¿ÕÊäÈë
Delay_1ms(1);
temp = GPIO_ReadInputData(GPIOB);
temp =(temp>>8);
value = temp;
return value;
}
/**
* @name static void WriteRawIO(u8 address,u8 value)
* @brief ÍùÒ»¸öµØַдһ¸öÊý¾Ý
* @param address µØÖ·
* value Êý¾Ý
* @retval None
*/
void WriteRawIO(u8 address,u8 value)
{
RC500_ALE(0);
RC500_NCS(1);
RC500_NWR(1);
RC500_ALE(1);
GPIOB_Write1(address);//дµØÖ·
RC500_ALE(0);
RC500_NCS(0);
GPIOB_Write1(value);//дÊý¾Ý
RC500_NWR(0);
__nop();
RC500_NWR(1);
RC500_NCS(1);
}
/**
* @name u8 ReadRawIO(u8 address)
* @brief ´ÓÒ»¸öµØÖ·¶ÁÒ»¸öÊý¾Ý
* @param address µØÖ·
* @retval ·µ»Ø¶Á³öµÄÊý¾Ý
*/
u8 ReadRawIO(u8 address)
{
u8 value;
RC500_ALE(0);
RC500_NCS(1);
RC500_NRD(1);
RC500_ALE(1);
GPIOB_Write1(address);//дµØÖ·
RC500_ALE(0);
RC500_NCS(0);
__nop();
Delay_1ms(1);
RC500_NRD(0);
value = GPIOB_Read();//¶Á³öÊý¾Ý
RC500_NRD(1);
RC500_NCS(1);
return value;
}
/**
* @name void WriteIO(u8 address, u8 value)
* @brief ÍùRC500Ò»¸öµØַдһ¸öÊý¾Ý
* @param address µØÖ·
value Êý¾Ý
* @retval None
*/
void WriteIO(u8 address, u8 value)
{
WriteRawIO(0x00,GetRegPage(address));
WriteRawIO(address,value);
}
/**
* @name u8 ReadRawIO(u8 address)
* @brief ´ÓÒ»¸öµØÖ·¶ÁÒ»¸öÊý¾Ý
* @param address µØÖ·
* @retval ·µ»Ø¶Á³öµÄÊý¾Ý
*/
u8 ReadIO(u8 address)
{
WriteRawIO(0x00,GetRegPage(address));
return ReadRawIO(address);
}
/**
* @name char M500PcdReset(void)
* @brief RC500¸´Î»
* @param None
* @retval ¸´Î»³É¹¦Ôò·µ»Ø0
*/
u8 M500PcdReset(void)
{
u8 status = MI_OK;
u16 timecnt=0;
RC500_NCS(0);
RC500_RST(0);
Delay_1ms(50);
RC500_RST(1);
Delay_1ms(20);
RC500_RST(0);
RC500_NCS(0);
Delay_1ms(10);
timecnt=3000;
while ((ReadIO(RegCommand) & PCD_RESETPHASE) && timecnt--);//µÈ´ý³õʼ»¯²¢ÐнӿÚÀàÐͽ׶νáÊø
if(!timecnt)
{
status = MI_RESETERR; //¸´Î»´íÎó
}
if (status == MI_OK)
{
WriteIO(RegPage,0x80);//½«0X80дÈëpage¼Ä´æÆ÷ÒÔ³õʼ»¯Î¢¿ØÖÆÆ÷½Ó¿Ú
if (ReadIO(RegCommand) != 0x00)//¶ÁCommand¼Ä´æÆ÷£¬Èç¹ûֵΪ0Ôò½Ó¿Ú³õʼ»¯³É¹¦
{
status = MI_INTERFACEERR; //½Ó¿ÚÀàÐÍ´íÎó£¬Î¢¿ØÖÆÆ÷½Ó¿Ú³õʼ»¯²»³É¹¦
}
}
return status;
}
但是复位不成功,测试为接口类型初始化错误(MI_INTERFACEERR)。望哪位大侠能给看一下,谢谢! |