feat: usart retarget

This commit is contained in:
Timothy Yin 2025-03-20 02:35:29 +08:00
parent f515d5fa96
commit 05dc5e3f53
5 changed files with 180 additions and 33 deletions

32
.idea/workspace.xml generated
View File

@ -28,8 +28,11 @@
</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="initial commit: oled, rc522, exti">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change afterPath="$PROJECT_DIR$/Core/Inc/retarget.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Core/Src/retarget.c" 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/main.c" beforeDir="false" afterPath="$PROJECT_DIR$/Core/Src/main.c" 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 +65,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",
@ -89,12 +95,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 +128,18 @@
<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="828000" />
</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>
<option name="localTasksCounter" value="2" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -134,4 +150,8 @@
<isAutomaticFoundErrors value="true" /> <isAutomaticFoundErrors value="true" />
<isAutomaticReloadCMake value="true" /> <isAutomaticReloadCMake value="true" />
</component> </component>
<component name="VcsManagerConfiguration">
<MESSAGE value="initial commit: oled, rc522, exti" />
<option name="LAST_COMMIT_MESSAGE" value="initial commit: oled, rc522, exti" />
</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

@ -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 */
@ -119,13 +120,19 @@ 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,"Card scan...",12);
printf("startup\n");
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */

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, ...)
{ {