impl printf
This commit is contained in:
parent
375ab76a2e
commit
7f4f50f9b7
5 changed files with 429 additions and 0 deletions
165
vendor/my_printf/my_printf.c
vendored
Normal file
165
vendor/my_printf/my_printf.c
vendored
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include "my_printf.h"
|
||||
|
||||
|
||||
void my_uart_txchar(char ch)
|
||||
{
|
||||
LL_USART_TransmitData8(USART2, ch);
|
||||
while (!LL_USART_IsActiveFlag_TC(USART2));
|
||||
LL_USART_ClearFlag_TC(USART2);
|
||||
}
|
||||
|
||||
void my_uart_txstring(char *str)
|
||||
{
|
||||
while (*str) my_uart_txchar(*str++);
|
||||
}
|
||||
|
||||
|
||||
void my_printf_config(uint32_t baudRate)
|
||||
{
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
|
||||
|
||||
/* USART Init */
|
||||
LL_USART_SetBaudRate(USART2, SystemCoreClock, LL_USART_PRESCALER_DIV1, LL_USART_OVERSAMPLING_16, baudRate);
|
||||
LL_USART_SetDataWidth(USART2, LL_USART_DATAWIDTH_8B);
|
||||
LL_USART_SetStopBitsLength(USART2, LL_USART_STOPBITS_1);
|
||||
LL_USART_SetParity(USART2, LL_USART_PARITY_NONE);
|
||||
LL_USART_SetHWFlowCtrl(USART2, LL_USART_HWCONTROL_NONE);
|
||||
LL_USART_SetTransferDirection(USART2, LL_USART_DIRECTION_TX_RX);
|
||||
LL_USART_Enable(USART2);
|
||||
LL_USART_ClearFlag_TC(USART2);
|
||||
|
||||
/**USART GPIO Configuration
|
||||
PA2 ------> USART2_TX
|
||||
PA3 ------> USART2_RX
|
||||
*/
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
|
||||
|
||||
LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_2, LL_GPIO_MODE_ALTERNATE);
|
||||
LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_2, LL_GPIO_SPEED_FREQ_VERY_HIGH);
|
||||
LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_2, LL_GPIO_PULL_UP);
|
||||
LL_GPIO_SetAFPin_0_7(GPIOA, LL_GPIO_PIN_2, LL_GPIO_AF_1);
|
||||
|
||||
LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_3, LL_GPIO_MODE_ALTERNATE);
|
||||
LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_3, LL_GPIO_SPEED_FREQ_VERY_HIGH);
|
||||
LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_3, LL_GPIO_PULL_UP);
|
||||
LL_GPIO_SetAFPin_0_7(GPIOA, LL_GPIO_PIN_3, LL_GPIO_AF_1);
|
||||
|
||||
#if defined (__GNUC__) && !defined (__clang__)
|
||||
// To avoid io buffer
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
setvbuf(stdin, NULL, _IONBF, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined (__GNUC__) && !defined (__clang__)
|
||||
#define GETCHAR_PROTOTYPE __attribute__((weak)) int __io_getchar (void)
|
||||
#define PUTCHAR_PROTOTYPE __attribute__((weak)) int __io_putchar(int ch)
|
||||
#else
|
||||
#define GETCHAR_PROTOTYPE int fgetc(FILE * f)
|
||||
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief retargets the c library printf function to the usart.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
PUTCHAR_PROTOTYPE
|
||||
{
|
||||
/* Send a byte to USART */
|
||||
LL_USART_TransmitData8(USART2, ch);
|
||||
while (!LL_USART_IsActiveFlag_TC(USART2));
|
||||
LL_USART_ClearFlag_TC(USART2);
|
||||
|
||||
return (ch);
|
||||
}
|
||||
|
||||
GETCHAR_PROTOTYPE
|
||||
{
|
||||
int ch;
|
||||
while (!LL_USART_IsActiveFlag_RXNE(USART2));
|
||||
ch = LL_USART_ReceiveData8(USART2);
|
||||
return (ch);
|
||||
}
|
||||
|
||||
#if defined (__GNUC__) && !defined (__clang__)
|
||||
__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;
|
||||
}
|
||||
#endif
|
||||
|
||||
__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 _isatty(int fd)
|
||||
{
|
||||
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
|
||||
return 1;
|
||||
|
||||
errno = EBADF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _close(int fd)
|
||||
{
|
||||
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
|
||||
return 0;
|
||||
|
||||
errno = EBADF;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _lseek(int fd, int ptr, int dir)
|
||||
{
|
||||
(void)fd;
|
||||
(void)ptr;
|
||||
(void)dir;
|
||||
|
||||
errno = EBADF;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) 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;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _getpid(void)
|
||||
{
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _kill(pid_t pid, int sig)
|
||||
{
|
||||
(void)pid;
|
||||
(void)sig;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue