Compare commits

...

2 Commits

Author SHA1 Message Date
2fd2a63235 feat: rcc522 card reader 2025-03-20 04:30:01 +08:00
05dc5e3f53 feat: usart retarget 2025-03-20 02:35:29 +08:00
8 changed files with 278 additions and 98 deletions

48
.idea/workspace.xml generated
View File

@ -22,14 +22,18 @@
<config projectName="IntelliChargingPile" targetName="IntelliChargingPile.elf" /> <config projectName="IntelliChargingPile" targetName="IntelliChargingPile.elf" />
</generated> </generated>
</component> </component>
<component name="CMakeSettings"> <component name="CMakeSettings" AUTO_RELOAD="true">
<configurations> <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> </configurations>
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="56631632-52f0-4e39-b905-89e46eb6e768" name="Changes" comment=""> <list default="true" id="56631632-52f0-4e39-b905-89e46eb6e768" name="Changes" comment="feat: usart retarget">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <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> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -62,14 +66,17 @@
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent"><![CDATA[{
"keyToString": { "keyToString": {
"ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true",
"CMake Application.IntelliChargingPile.elf.executor": "Run", "CMake Application.IntelliChargingPile.elf.executor": "Run",
"OpenOCD Download & Run.IntelliChargingPile.elf.executor": "Run", "OpenOCD Download & Run.IntelliChargingPile.elf.executor": "Run",
"OpenOCD Download & Run.OCD IntelliChargingPile.executor": "Run", "OpenOCD Download & Run.OCD IntelliChargingPile.executor": "Run",
"RunOnceActivity.RadMigrateCodeStyle": "true", "RunOnceActivity.RadMigrateCodeStyle": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.cidr.known.project.marker": "true", "RunOnceActivity.cidr.known.project.marker": "true",
"RunOnceActivity.git.unshallow": "true",
"RunOnceActivity.readMode.enableVisualFormatting": "true", "RunOnceActivity.readMode.enableVisualFormatting": "true",
"RunOnceActivity.west.config.association.type.startup.service": "true", "RunOnceActivity.west.config.association.type.startup.service": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"cf.first.check.clang-format": "false", "cf.first.check.clang-format": "false",
"cidr.known.project.marker": "true", "cidr.known.project.marker": "true",
"git-widget-placeholder": "main", "git-widget-placeholder": "main",
@ -79,7 +86,7 @@
"node.js.selected.package.eslint": "(autodetect)", "node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)", "node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm", "nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "com.jetbrains.cidr.embedded.config", "settings.editor.selected.configurable": "CMakeSettings",
"vue.rearranger.settings.migration": "true" "vue.rearranger.settings.migration": "true"
} }
}]]></component> }]]></component>
@ -89,12 +96,12 @@
</key> </key>
</component> </component>
<component name="RunManager" selected="OpenOCD Download &amp; Run.OCD IntelliChargingPile"> <component name="RunManager" selected="OpenOCD Download &amp; Run.OCD IntelliChargingPile">
<configuration name="IntelliChargingPile.elf" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="IntelliChargingPile" TARGET_NAME="IntelliChargingPile.elf" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="IntelliChargingPile" RUN_TARGET_NAME="IntelliChargingPile.elf"> <configuration default="true" type="CLionExternalRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true">
<method v="2"> <method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" /> <option name="CLION.EXTERNAL.BUILD" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration default="true" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true"> <configuration name="IntelliChargingPile.elf" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="IntelliChargingPile" TARGET_NAME="IntelliChargingPile.elf" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="IntelliChargingPile" RUN_TARGET_NAME="IntelliChargingPile.elf">
<method v="2"> <method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" /> <option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method> </method>
@ -122,8 +129,26 @@
<updated>1742375125098</updated> <updated>1742375125098</updated>
<workItem from="1742375147816" duration="297000" /> <workItem from="1742375147816" duration="297000" />
<workItem from="1742375453292" duration="8414000" /> <workItem from="1742375453292" duration="8414000" />
<workItem from="1742385111843" duration="9863000" /> <workItem from="1742385111843" duration="9995000" />
<workItem from="1742408425253" duration="7279000" />
</task> </task>
<task id="LOCAL-00001" summary="initial commit: oled, rc522, exti">
<option name="closed" value="true" />
<created>1742407507479</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1742407507479</updated>
</task>
<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 /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -134,4 +159,9 @@
<isAutomaticFoundErrors value="true" /> <isAutomaticFoundErrors value="true" />
<isAutomaticReloadCMake value="true" /> <isAutomaticReloadCMake value="true" />
</component> </component>
<component name="VcsManagerConfiguration">
<MESSAGE value="initial commit: oled, rc522, exti" />
<MESSAGE value="feat: usart retarget" />
<option name="LAST_COMMIT_MESSAGE" value="feat: usart retarget" />
</component>
</project> </project>

22
Core/Inc/retarget.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef _RETARGET_H__
#define _RETARGET_H__
#include "stm32f1xx_hal.h"
#include <sys/stat.h>
#include <stdio.h>
void RetargetInit(UART_HandleTypeDef *huart);
int _isatty(int fd);
int _write(int fd, char *ptr, int len);
int _close(int fd);
int _lseek(int fd, int ptr, int dir);
int _read(int fd, char *ptr, int len);
int _fstat(int fd, struct stat *st);
#endif //#ifndef _RETARGET_H__

View File

@ -51,7 +51,7 @@ void MX_GPIO_Init(void)
__HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */ /*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 */ /*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, RC522_CS_Pin|RC522_RST_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOA, RC522_CS_Pin|RC522_RST_Pin, GPIO_PIN_RESET);

View File

@ -27,6 +27,7 @@
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include <stdio.h> #include <stdio.h>
#include "retarget.h"
#include "oled.h" #include "oled.h"
#include "rc522.h" #include "rc522.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
@ -49,8 +50,16 @@
/* Private variables ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */ /* USER CODE BEGIN PV */
uint8_t CardType[2] = {0}; uint8_t rf_status;
uint8_t CardID[4] = {0}; 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 */ /* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
@ -61,29 +70,7 @@ void SystemClock_Config(void);
/* Private user code ---------------------------------------------------------*/ /* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */ /* 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 */ /* USER CODE END 0 */
/** /**
@ -119,44 +106,97 @@ int main(void)
MX_USART1_UART_Init(); MX_USART1_UART_Init();
MX_SPI1_Init(); MX_SPI1_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
// Retarget uart
RetargetInit(&huart1);
// Init RC522
MFRC_Init(); MFRC_Init();
PCD_Reset(); PCD_Reset();
// Init OLED
OLED_Init(); OLED_Init();
OLED_Clear(); OLED_Clear();
OLED_ShowString(0,0,"Card scan...",12); OLED_ShowString(0,0,"scanning...",12);
printf("[icpile] dev\n");
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
while (1) while (1)
{ {
const char status = PCD_Request(PICC_REQALL, CardType); rf_status = PCD_Request(PICC_REQALL, rf_card_type);
if (CardType[0] == 0x04 && CardType[1] == 0x00)
{ if (!rf_status) {
OLED_ShowString(0,2,"Type: Classic",13); rf_status = PCD_ERR;
HAL_GPIO_WritePin(LED_BTN1_GPIO_Port, LED_BTN1_Pin, GPIO_PIN_SET);
rf_status = PCD_AntiColl(rf_card_id);
} }
else if (CardType[0] == 0x44 && CardType[1] == 0x00)
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)
{ {
OLED_ShowString(0,2,"Type: Ultralight",16); printf("[*] Key A pass\n");
} }
else else
{ {
OLED_ShowString(0,2,"Type: Unknown",13); printf("[!] Key A verification failed\n");
} }
if (!status) { rf_status = PCD_AuthState(PICC_AUTHENT1B, ADDR, KEY_B, rf_card_id);
PCD_AntiColl(CardID); if(rf_status == PCD_OK)
OLED_ShowString(0,4,"Card ID:",8); {
// show parsed card id with pattern: %x-%x-%x-%x printf("[*] Key B pass\n");
char buffer[12]; }
format_hex_string(CardID, buffer); else
OLED_ShowString(0,6,buffer,12); {
HAL_GPIO_WritePin(LED_BTN1_GPIO_Port, LED_BTN1_Pin, GPIO_PIN_SET); printf("[!] Key B verification failed\n");
}
} }
HAL_Delay(200); 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 END WHILE */
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
@ -208,15 +248,7 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{ {
if(GPIO_Pin == BTN1_Pin) if(GPIO_Pin == BTN1_Pin)
{ {
HAL_GPIO_WritePin(LED_BTN1_GPIO_Port, LED_BTN1_Pin, GPIO_PIN_RESET); OLED_Clear();
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);
} }
} }
/* USER CODE END 4 */ /* USER CODE END 4 */

View File

@ -286,9 +286,6 @@ void PCD_Reset(void)
PCD_AntennaOff(); //关天线 PCD_AntennaOff(); //关天线
osDelay(2); osDelay(2);
PCD_AntennaOn(); //开天线 PCD_AntennaOn(); //开天线
printf("初始化完成\n");
} }

98
Core/Src/retarget.c Normal file
View File

@ -0,0 +1,98 @@
#include <_ansi.h>
#include <_syslist.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/times.h>
#include <retarget.h>
#include <stdint.h>
#if !defined(OS_USE_SEMIHOSTING)
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
UART_HandleTypeDef *gHuart;
void RetargetInit(UART_HandleTypeDef *huart)
{
gHuart = huart;
/* Disable I/O buffering for STDOUT stream, so that
* chars are sent out as soon as they are printed. */
setvbuf(stdout, NULL, _IONBF, 0);
}
int _isatty(int fd)
{
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
return 1;
errno = EBADF;
return 0;
}
int _write(int fd, char *ptr, int len)
{
HAL_StatusTypeDef hstatus;
if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
{
hstatus = HAL_UART_Transmit(gHuart, (uint8_t *) ptr, len, HAL_MAX_DELAY);
if (hstatus == HAL_OK)
return len;
else
return EIO;
}
errno = EBADF;
return -1;
}
int _close(int fd)
{
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
return 0;
errno = EBADF;
return -1;
}
int _lseek(int fd, int ptr, int dir)
{
(void) fd;
(void) ptr;
(void) dir;
errno = EBADF;
return -1;
}
int _read(int fd, char *ptr, int len)
{
HAL_StatusTypeDef hstatus;
if (fd == STDIN_FILENO)
{
hstatus = HAL_UART_Receive(gHuart, (uint8_t *) ptr, 1, HAL_MAX_DELAY);
if (hstatus == HAL_OK)
return 1;
else
return EIO;
}
errno = EBADF;
return -1;
}
int _fstat(int fd, struct stat *st)
{
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
{
st->st_mode = S_IFCHR;
return 0;
}
errno = EBADF;
return 0;
}
#endif //#if !defined(OS_USE_SEMIHOSTING)

View File

@ -89,33 +89,33 @@ __attribute__((weak)) int _write(int file, char *ptr, int len)
return len; return len;
} }
int _close(int file) // int _close(int file)
{ // {
(void)file; // (void)file;
return -1; // return -1;
} // }
//
//
int _fstat(int file, struct stat *st) // int _fstat(int file, struct stat *st)
{ // {
(void)file; // (void)file;
st->st_mode = S_IFCHR; // st->st_mode = S_IFCHR;
return 0; // return 0;
} // }
//
int _isatty(int file) // int _isatty(int file)
{ // {
(void)file; // (void)file;
return 1; // return 1;
} // }
//
int _lseek(int file, int ptr, int dir) // int _lseek(int file, int ptr, int dir)
{ // {
(void)file; // (void)file;
(void)ptr; // (void)ptr;
(void)dir; // (void)dir;
return 0; // return 0;
} // }
int _open(char *path, int flags, ...) int _open(char *path, int flags, ...)
{ {

View File

@ -95,9 +95,10 @@ PB9.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
PB9.GPIO_PuPd=GPIO_PULLUP PB9.GPIO_PuPd=GPIO_PULLUP
PB9.Locked=true PB9.Locked=true
PB9.Signal=GPXTI9 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.GPIO_Label=LED0
PC13-TAMPER-RTC.Locked=true PC13-TAMPER-RTC.Locked=true
PC13-TAMPER-RTC.PinState=GPIO_PIN_SET
PC13-TAMPER-RTC.Signal=GPIO_Output PC13-TAMPER-RTC.Signal=GPIO_Output
PD0-OSC_IN.Mode=HSE-External-Oscillator PD0-OSC_IN.Mode=HSE-External-Oscillator
PD0-OSC_IN.Signal=RCC_OSC_IN PD0-OSC_IN.Signal=RCC_OSC_IN
@ -135,7 +136,7 @@ ProjectManager.ToolChainLocation=
ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptAfterPath=
ProjectManager.UAScriptBeforePath= ProjectManager.UAScriptBeforePath=
ProjectManager.UnderRoot=true 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.ADCFreqValue=36000000
RCC.AHBFreq_Value=72000000 RCC.AHBFreq_Value=72000000
RCC.APB1CLKDivider=RCC_HCLK_DIV2 RCC.APB1CLKDivider=RCC_HCLK_DIV2