Car/Core/Inc/syscalls.h

139 lines
3.4 KiB
C
Raw Normal View History

2024-07-13 15:22:46 +08:00
/**
******************************************************************************
* @file syscalls.c
* @author Suroy Wrote with Auto-generated by STM32CubeIDE
* @url https://suroy.cn
* @brief STM32CubeIDE Minimal System calls file
*
* For more information about which c-functions
* need which of these lowlevel functions
* please consult the Newlib libc-manual
******************************************************************************
* @attention
*
* Copyright (c) 2020-2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes */
#ifndef __SYSCALLS_H
#define __SYSCALLS_H
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stm32f1xx_hal_uart.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
extern UART_HandleTypeDef huart2;
/* Variables */
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
/* Functions */
__attribute__((weak)) int _read(int file, char *ptr, int len) {
(void)file;
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++) {
*ptr++ = __io_getchar();
}
return len;
}
__attribute__((weak)) int _write(int file, char *ptr, int len) {
(void)file;
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++) {
__io_putchar(*ptr++);
}
return len;
}
// 条件编译
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(void)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */
/**
* : c库函数 printf到 DEBUG_USARTx
* :
* :
*
*/
PUTCHAR_PROTOTYPE {
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF); // 阻塞式无限等待
return ch;
}
/**
* : c库函数 getchar,scanf到 DEBUG_USARTx
* :
* :
*
*/
GETCHAR_PROTOTYPE {
uint8_t ch = 0;
HAL_UART_Receive(&huart2, &ch, 1, 0xFFFF);
return ch;
}
/* 非GCC模式才允许编译使用即 Keil、IAR 等 */
#ifndef __GNUC__
/**
* @brief C printf huart2
* KeilIAR IDE GCC
* @author Suroy
* @param ch
* @param f
* @return int
*
* @usage printf("USART1_Target:\r\n");
*/
int fputc(int ch, FILE *f) {
// 采用轮询方式发送1字节数据超时时间为无限等待
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // huart2是串口的句柄
return ch;
}
/**
* @brief fgets
* C scanf huart2
*
* @param f
* @return int
*
* @usage scanf("%c", &RecData);
*/
int fgetc(FILE *f) {
uint8_t ch;
HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // huart2是串口的句柄
return ch;
}
#endif /* __GNUC__ */
void delay_us(uint16_t us) {
uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
while (delay--);
}
#endif