Car/Core/Inc/syscalls.h

80 lines
1.7 KiB
C
Raw Permalink Normal View History

2024-07-13 15:22:46 +08:00
#ifndef __SYSCALLS_H
#define __SYSCALLS_H
2024-07-14 16:07:46 +08:00
2024-07-13 15:22:46 +08:00
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stm32f1xx_hal_uart.h>
#include <time.h>
2024-07-14 19:09:55 +08:00
extern UART_HandleTypeDef huart1;
2024-07-13 15:22:46 +08:00
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
__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__ */
/**
* @brief C printf huart2
* @author Suroy
* @param ch
* @param f
* @return int
*
* @usage printf("USART1_Target:\r\n");
*/
2024-07-14 16:07:46 +08:00
PUTCHAR_PROTOTYPE {
2024-07-14 19:09:55 +08:00
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // 阻塞式无限等待
2024-07-13 15:22:46 +08:00
return ch;
}
/**
2024-07-14 16:07:46 +08:00
* @brief c库函数 getchar,scanf到 DEBUG_USARTx
2024-07-13 15:22:46 +08:00
* C scanf huart2
*
* @param f
* @return int
*
* @usage scanf("%c", &RecData);
*/
2024-07-14 16:07:46 +08:00
GETCHAR_PROTOTYPE {
uint8_t ch = 0;
2024-07-14 19:09:55 +08:00
HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
2024-07-13 15:22:46 +08:00
return ch;
}
void delay_us(uint16_t us) {
uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
while (delay--);
}
#endif