feat: rcc522 card reader

This commit is contained in:
Timothy Yin 2025-03-20 04:30:01 +08:00
parent 05dc5e3f53
commit 2fd2a63235
5 changed files with 105 additions and 72 deletions

28
.idea/workspace.xml generated
View File

@ -22,17 +22,18 @@
<config projectName="IntelliChargingPile" targetName="IntelliChargingPile.elf" />
</generated>
</component>
<component name="CMakeSettings">
<component name="CMakeSettings" AUTO_RELOAD="true">
<configurations>
<configuration PROFILE_NAME="Debug" ENABLED="true" CONFIG_NAME="Debug" />
<configuration PROFILE_NAME="Debug" ENABLED="true" CONFIG_NAME="Debug" GENERATION_OPTIONS="-DCMAKE_EXE_LINKER_FLAGS:STRING=--specs=nano.specs" />
</configurations>
</component>
<component name="ChangeListManager">
<list default="true" id="56631632-52f0-4e39-b905-89e46eb6e768" name="Changes" comment="initial commit: oled, rc522, exti">
<change afterPath="$PROJECT_DIR$/Core/Inc/retarget.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Core/Src/retarget.c" afterDir="false" />
<list default="true" id="56631632-52f0-4e39-b905-89e46eb6e768" name="Changes" comment="feat: usart retarget">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Core/Src/gpio.c" beforeDir="false" afterPath="$PROJECT_DIR$/Core/Src/gpio.c" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Core/Src/main.c" beforeDir="false" afterPath="$PROJECT_DIR$/Core/Src/main.c" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Core/Src/rc522.c" beforeDir="false" afterPath="$PROJECT_DIR$/Core/Src/rc522.c" afterDir="false" />
<change beforePath="$PROJECT_DIR$/IntelliChargingPile.ioc" beforeDir="false" afterPath="$PROJECT_DIR$/IntelliChargingPile.ioc" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -85,7 +86,7 @@
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "com.jetbrains.cidr.embedded.config",
"settings.editor.selected.configurable": "CMakeSettings",
"vue.rearranger.settings.migration": "true"
}
}]]></component>
@ -129,7 +130,7 @@
<workItem from="1742375147816" duration="297000" />
<workItem from="1742375453292" duration="8414000" />
<workItem from="1742385111843" duration="9995000" />
<workItem from="1742408425253" duration="828000" />
<workItem from="1742408425253" duration="7279000" />
</task>
<task id="LOCAL-00001" summary="initial commit: oled, rc522, exti">
<option name="closed" value="true" />
@ -139,7 +140,15 @@
<option name="project" value="LOCAL" />
<updated>1742407507479</updated>
</task>
<option name="localTasksCounter" value="2" />
<task id="LOCAL-00002" summary="feat: usart retarget">
<option name="closed" value="true" />
<created>1742409329840</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1742409329840</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -152,6 +161,7 @@
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="initial commit: oled, rc522, exti" />
<option name="LAST_COMMIT_MESSAGE" value="initial commit: oled, rc522, exti" />
<MESSAGE value="feat: usart retarget" />
<option name="LAST_COMMIT_MESSAGE" value="feat: usart retarget" />
</component>
</project>

View File

@ -51,7 +51,7 @@ void MX_GPIO_Init(void)
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, RC522_CS_Pin|RC522_RST_Pin, GPIO_PIN_RESET);

View File

@ -50,8 +50,16 @@
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t CardType[2] = {0};
uint8_t CardID[4] = {0};
uint8_t rf_status;
uint8_t rf_card_type[3];
uint8_t rf_card_id[5];
uint8_t rf_data[16];
uint8_t ADDR = 0x01 * 4 + 0x03;
uint8_t KEY_A[6]= {0xff,0xff,0xff,0xff,0xff,0xff};
uint8_t KEY_B[6]= {0xff,0xff,0xff,0xff,0xff,0xff};
const char* SEP = "-------------------------\n";
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@ -62,29 +70,7 @@ void SystemClock_Config(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void format_hex_string(const uint8_t *values, char *buffer) {
const char hex_chars[] = "0123456789ABCDEF";
// 处理第一个字节
buffer[0] = hex_chars[values[0] >> 4];
buffer[1] = hex_chars[values[0] & 0x0F];
buffer[2] = '-';
// 处理第二个字节
buffer[3] = hex_chars[values[1] >> 4];
buffer[4] = hex_chars[values[1] & 0x0F];
buffer[5] = '-';
// 处理第三个字节
buffer[6] = hex_chars[values[2] >> 4];
buffer[7] = hex_chars[values[2] & 0x0F];
buffer[8] = '-';
// 处理第四个字节并终止字符串
buffer[9] = hex_chars[values[3] >> 4];
buffer[10] = hex_chars[values[3] & 0x0F];
buffer[11] = '\0';
}
/* USER CODE END 0 */
/**
@ -131,39 +117,86 @@ int main(void)
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"Card scan...",12);
printf("startup\n");
OLED_ShowString(0,0,"scanning...",12);
printf("[icpile] dev\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
const char status = PCD_Request(PICC_REQALL, CardType);
if (CardType[0] == 0x04 && CardType[1] == 0x00)
{
OLED_ShowString(0,2,"Type: Classic",13);
}
else if (CardType[0] == 0x44 && CardType[1] == 0x00)
{
OLED_ShowString(0,2,"Type: Ultralight",16);
}
else
{
OLED_ShowString(0,2,"Type: Unknown",13);
}
rf_status = PCD_Request(PICC_REQALL, rf_card_type);
if (!status) {
PCD_AntiColl(CardID);
OLED_ShowString(0,4,"Card ID:",8);
// show parsed card id with pattern: %x-%x-%x-%x
char buffer[12];
format_hex_string(CardID, buffer);
OLED_ShowString(0,6,buffer,12);
if (!rf_status) {
rf_status = PCD_ERR;
HAL_GPIO_WritePin(LED_BTN1_GPIO_Port, LED_BTN1_Pin, GPIO_PIN_SET);
rf_status = PCD_AntiColl(rf_card_id);
}
HAL_Delay(200);
if (!rf_status) {
rf_status = PCD_ERR;
printf(SEP);
printf("[i] Card Type: %02X %02X %02X\n", rf_card_type[0], rf_card_type[1], rf_card_type[2]);
printf("[i] Card ID : %02X-%02X-%02X-%02X\n", rf_card_id[0], rf_card_id[1], rf_card_id[2], rf_card_id[3]);
OLED_ShowString(0, 2, "Typ:", 12);
uint8_t card_type_buffer[9];
snprintf(card_type_buffer, sizeof card_type_buffer, "%02X %02X %02X", rf_card_type[0], rf_card_type[1], rf_card_type[2]);
OLED_ShowString(38, 2, card_type_buffer, 12);
OLED_ShowString(0, 4, "ID :", 12);
uint8_t card_id_buffer[12];
snprintf(card_id_buffer, sizeof card_id_buffer, "%02X-%02X-%02X-%02X", rf_card_id[0], rf_card_id[1], rf_card_id[2], rf_card_id[3]);
OLED_ShowString(36, 4, card_id_buffer, 12);
rf_status = PCD_Select(rf_card_id);
}
if (!rf_status) {
rf_status = PCD_ERR;
rf_status = PCD_AuthState(PICC_AUTHENT1A, ADDR, KEY_A, rf_card_id);
if(rf_status == PCD_OK)
{
printf("[*] Key A pass\n");
}
else
{
printf("[!] Key A verification failed\n");
}
rf_status = PCD_AuthState(PICC_AUTHENT1B, ADDR, KEY_B, rf_card_id);
if(rf_status == PCD_OK)
{
printf("[*] Key B pass\n");
}
else
{
printf("[!] Key B verification failed\n");
}
}
if(rf_status == PCD_OK)
{
rf_status = PCD_ERR;
rf_status = PCD_ReadBlock(ADDR, rf_data);
if(rf_status == PCD_OK)
{
printf("[+] Data in sector %d block %d: ", ADDR/4, ADDR%4);
for(int i = 0; i < 16; i++)
{
printf("%02x", rf_data[i]);
}
printf("\n");
}
else
{
printf("Read card failed\n");
}
HAL_GPIO_WritePin(LED_BTN1_GPIO_Port, LED_BTN1_Pin, GPIO_PIN_RESET);
HAL_Delay(3000);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
@ -215,15 +248,7 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == BTN1_Pin)
{
HAL_GPIO_WritePin(LED_BTN1_GPIO_Port, LED_BTN1_Pin, GPIO_PIN_RESET);
CardType[0] = 0;
CardType[1] = 0;
CardID[0] = 0;
CardID[1] = 0;
CardID[2] = 0;
CardID[3] = 0;
OLED_ShowString(0,4, " ",8);
OLED_ShowString(0,6, " ",12);
OLED_Clear();
}
}
/* USER CODE END 4 */

View File

@ -286,9 +286,6 @@ void PCD_Reset(void)
PCD_AntennaOff(); //关天线
osDelay(2);
PCD_AntennaOn(); //开天线
printf("初始化完成\n");
}
@ -739,7 +736,7 @@ char PCD_Halt(void)
//
// uint8_t status;
// uint8_t addr = 0x01*4 + 0x03; // 总共16个扇区。一个扇区4个块从0开始算表示第一扇区第三块
// void CardCompare(void)
// {
// uint8_t i;

View File

@ -95,9 +95,10 @@ PB9.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
PB9.GPIO_PuPd=GPIO_PULLUP
PB9.Locked=true
PB9.Signal=GPXTI9
PC13-TAMPER-RTC.GPIOParameters=GPIO_Label
PC13-TAMPER-RTC.GPIOParameters=PinState,GPIO_Label
PC13-TAMPER-RTC.GPIO_Label=LED0
PC13-TAMPER-RTC.Locked=true
PC13-TAMPER-RTC.PinState=GPIO_PIN_SET
PC13-TAMPER-RTC.Signal=GPIO_Output
PD0-OSC_IN.Mode=HSE-External-Oscillator
PD0-OSC_IN.Signal=RCC_OSC_IN
@ -135,7 +136,7 @@ ProjectManager.ToolChainLocation=
ProjectManager.UAScriptAfterPath=
ProjectManager.UAScriptBeforePath=
ProjectManager.UnderRoot=true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C1_Init-I2C1-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C1_Init-I2C1-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true
RCC.ADCFreqValue=36000000
RCC.AHBFreq_Value=72000000
RCC.APB1CLKDivider=RCC_HCLK_DIV2