/**
* @file STM32_HAL_PRINTF_ATY.c
*
* @param Project DEVICE_GENERAL_ATY_LIB
*
* @author ATY
*
* @copyright
* - Copyright 2017 - 2023 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - Your use will be deemed to have accepted the terms of this statement.
*
* @brief Familiar functions of printf for STM32 device HAL lib
*
* @version
* - 1_01_221029 > ATY
* -# Preliminary version, first Release
********************************************************************************
*/
#ifndef __STM32_HAL_PRINTF_ATY_C
#define __STM32_HAL_PRINTF_ATY_C
#include "STM32_HAL_PRINTF_ATY.h"
#include "usart.h"
/******************************* For user *************************************/
#if defined (__STM32_HAL_ATY) && defined(DEBUG_PRINTF_RECEIVE)
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef* huart, uint16_t Size)
{
HAL_UART_DMAStop(&PRINTF_UART);
uartPrintfMsgStruct.rxCount = PRINTF_RX_MAX_LEN - __HAL_DMA_GET_COUNTER(&PRINTF_DMA);
uartPrintfMsgStruct.rcvOverFlag = 1;
/* if process is not too long, process function can be used at this call back;
otherwise put process at other cycle like while or timer cycle */
// PrintfReceiveProcess();
}
#endif /* __STM32_HAL_ATY && DEBUG_PRINTF_RECEIVE */
/******************************************************************************/
#ifndef __MICROLIB
// for standart lib(not use MicroLIB)
#pragma import(__use_no_semihosting)
// define _sys_exit to avoid semi-host mode
void _sys_exit(int x)
{
x = x;
}
struct __FILE
{
int handle;
};
FILE __stdout;
#endif /* __MICROLIB */
/* In gcc, using printf() output, if there is no "\n" in output data, no data
will be printed on the screen until "\n" is encountered or the buffer overflows
Another way to refresh the output data is to run "fflush(stdout)" after
sending the data to force a refresh of the output stream
*/
#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__ */
PUTCHAR_PROTOTYPE \
{
#ifdef LL
LL_USART_TransmitData8(USART1, ch);
while(!LL_USART_IsActiveFlag_TC(USART1)){}
#else
HAL_UART_Transmit(&PRINTF_UART, (uint8_t*)&ch, 1, 0xFFFF);
#endif
return ch;
}
GETCHAR_PROTOTYPE \
{
uint8_t ch = 0;
HAL_UART_Receive(&PRINTF_UART, &ch, 1, 0xFFFF);
return ch;
}
#ifdef DEBUG_PRINTF_RECEIVE
uint8_t uartPrintfRxBuf[PRINTF_RX_MAX_LEN];
struct _uartMsgStruct uartPrintfMsgStruct = {uartPrintfRxBuf};
/**
* @brief init uart IT and DMA etc.
*/
void PrintfReceiveInit(void)
{
HAL_UARTEx_ReceiveToIdle_DMA(&PRINTF_UART, uartPrintfMsgStruct.rx, PRINTF_RX_MAX_LEN);
__HAL_DMA_DISABLE_IT(&PRINTF_DMA, DMA_IT_HT);
}
/**
* @brief uart receive data analysis
* @note put at main while
*/
__WEAK_ATY void PrintfReceiveProcess(void)
{
struct _uartMsgStruct* ptr = &uartPrintfMsgStruct;
if(ptr->rcvOverFlag)
{
ptr->rcvOverFlag = 0;
PrintfReceiveProcess_User(ptr);
PrintfReceiveInit();
}
}
__WEAK_ATY void PrintfReceiveProcess_User(struct _uartMsgStruct* ptr)
{
if(memcmp("DSTART", (char*)ptr->rx, strlen("DSTART")) == 0)
{
printf("\r\nDebug START!");
}
else if(memcmp("temp", (char*)ptr->rx, strlen("temp")) == 0)
{
printf("\r\ntemp %c%c%c",
ptr->rx[strlen("temp_")],
ptr->rx[strlen("temp_") + 1],
ptr->rx[strlen("temp_") + 2]);
}
}
#endif /* DEBUG_PRINTF_RECEIVE */
#endif /* __STM32_HAL_PRINTF_ATY_C */
/******************************** End Of File *********************************/