micro_climate/Drivers/Shell/shell_uart.c

106 lines
2.4 KiB
C

#include "shell_uart.h"
//发送一个字符
void dev_uart_out_char(char c)
{
uart_putchar(TERM_UART_INDEX, c);
}
//读取一个字符
char dev_uart_in_char()
{
uart_device_info *dev = (uart_device_info *)g_term_uart_handle;
//osSemaphoreWait(dev->sem_recv_one_byte_data,0);
osDelay(300);
return uart_dev_in_char(g_term_uart_handle);
}
#if 0
//shell命令行使用
__weak size_t __write(int handle, const unsigned char * buffer, size_t size)
{
size_t nChars = 0;
if (buffer == 0)
{
/* This means that we should flush internal buffers. Since we*/
/* don't we just return. (Remember, "handle" == -1 means that all*/
/* handles should be flushed.)*/
return 0;
}
/* This function only writes to "standard out" and "standard err",*/
/* for all other file handles it returns failure.*/
if ((handle != _LLIO_STDOUT) && (handle != _LLIO_STDERR))
{
return _LLIO_ERROR;
}
/* Send data.*/
while (size--)
{
dev_uart_out_char(*buffer++);
++nChars;
}
return nChars;
}
__weak size_t __read(int handle, unsigned char * buffer, size_t size)
{
size_t nChars = 0;
uint16_t ch = 0;
if (buffer == 0)
{
/* This means that we should flush internal buffers. Since we*/
/* don't we just return. (Remember, "handle" == -1 means that all*/
/* handles should be flushed.)*/
return 0;
}
/* This function only writes to "standard out" and "standard err",*/
/* for all other file handles it returns failure.*/
if ((handle != _LLIO_STDIN) && (handle != _LLIO_STDERR))
{
return _LLIO_ERROR;
}
/* read data.*/
while (size--)
{
ch=dev_uart_in_char();
*buffer++ = (char)ch & 0xFF;
++nChars;
}
return nChars;
}
/**
* \brief UART putchar function
* \attention 内部函数,用户无法调用
* \param[in] instance UART模块号
* \param[in] str 指向字符串常量的指针
* \retval None
*/
static void UART_putstr(UART_MemMapPtr instance, const char *str)
{
while(*str != '\0')
{
uart_putchar(UART1_BASE_PTR, *str++);
}
}
/**
* \brief 进制转换
* \attention 内部函数,用户无法调用
* \retval None
*/
static void printn(unsigned int n, unsigned int b)
{
static char *ntab = "0123456789ABCDEF";
unsigned int a, m;
if (n / b)
{
a = n / b;
printn(a, b);
}
m = n % b;
uart_putchar(UART1_BASE_PTR, ntab[m]);
}
#endif