80 lines
1.7 KiB
C
80 lines
1.7 KiB
C
#ifndef __SYSCALLS_H
|
|
#define __SYSCALLS_H
|
|
|
|
#include <errno.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stm32f1xx_hal_uart.h>
|
|
#include <time.h>
|
|
|
|
extern UART_HandleTypeDef huart1;
|
|
|
|
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");
|
|
*/
|
|
PUTCHAR_PROTOTYPE {
|
|
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // 阻塞式无限等待
|
|
return ch;
|
|
}
|
|
|
|
/**
|
|
* @brief 重定向 c库函数 getchar,scanf到 DEBUG_USARTx
|
|
* 重定向 C 标准库 scanf 函数到串口 huart2
|
|
* 注意以 空格 为结束
|
|
* @param f
|
|
* @return int
|
|
*
|
|
* @usage scanf("%c", &RecData);
|
|
*/
|
|
GETCHAR_PROTOTYPE {
|
|
uint8_t ch = 0;
|
|
HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
|
|
return ch;
|
|
}
|
|
|
|
void delay_us(uint16_t us) {
|
|
uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
|
|
while (delay--);
|
|
}
|
|
#endif
|