first commit
This commit is contained in:
+196
@@ -0,0 +1,196 @@
|
||||
#include "Bsp.h"
|
||||
|
||||
#ifdef TTY_COM
|
||||
#include "LetterShell.h"
|
||||
#endif
|
||||
|
||||
// LED灯数组
|
||||
#if (LED_NUM != 0)
|
||||
const GPIO_CONFIG LED_PERIPH[] = {
|
||||
{.GpioX = GPIO21, .Mode = GPIO_Mode_Out_PP, .Speed = GPIO_Speed_10MHz},
|
||||
{.GpioX = GPIO69, .Mode = GPIO_Mode_Out_PP, .Speed = GPIO_Speed_10MHz},
|
||||
};
|
||||
#endif
|
||||
//数字输入数组
|
||||
#if (DI_NUM != 0)
|
||||
const GPIO_CONFIG DI_PERIPH[] = {
|
||||
{},
|
||||
};
|
||||
#endif
|
||||
//数字输出数组
|
||||
#if (DO_NUM != 0)
|
||||
const GPIO_CONFIG DO_PERIPH[] = {
|
||||
{},
|
||||
};
|
||||
#endif
|
||||
//串口配置表
|
||||
const COM_CONFIG COM_PERIPH[] = {
|
||||
{COM0,.TxPort = GPIO9, .RxPort =GPIO10},
|
||||
{COM1,.TxPort = GPIO2, .RxPort = GPIO3,.TxRxEn = GPIO55},
|
||||
};
|
||||
|
||||
void BspExtraInit(void);
|
||||
|
||||
/**
|
||||
* @brief Voilet BSP的初始化函数
|
||||
* @note void
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void BspInit(void) {
|
||||
unsigned char i = 0;
|
||||
|
||||
INTERRUPT_DISABLE
|
||||
|
||||
//初始化中断系统
|
||||
IrqInit();
|
||||
|
||||
//初始化延时库
|
||||
DelayConfig();
|
||||
|
||||
//LED灯配置
|
||||
#if (LED_NUM != 0)
|
||||
for (i = 0;i < ARRAY_LEN(LED_PERIPH);i++) {
|
||||
//配置GPIO口
|
||||
GpioConfig(&LED_PERIPH[i].GpioX, LED_PERIPH[i].Mode, LED_PERIPH[i].Speed);
|
||||
}
|
||||
#endif
|
||||
|
||||
//DI采集配置
|
||||
#if (DI_NUM != 0)
|
||||
// for (i = 0;i < DI_NUM;i++) {
|
||||
// //打开外设时钟
|
||||
// GpioClockEnable(&LED_PERIPH[i].GpioX);
|
||||
// //配置GPIO口
|
||||
// GpioConfig(&LED_PERIPH[i].GpioX, LED_PERIPH[i].Mode, LED_PERIPH[i].Speed);
|
||||
// }
|
||||
#endif
|
||||
|
||||
//DO输出配置
|
||||
#if (DO_NUM != 0)
|
||||
// for (i = 0;i < DO_NUM;i++) {
|
||||
// //打开外设时钟
|
||||
// GpioClockEnable(&LED_PERIPH[i].GpioX);
|
||||
// //配置GPIO口
|
||||
// GpioConfig(&LED_PERIPH[i].GpioX, LED_PERIPH[i].Mode, LED_PERIPH[i].Speed);
|
||||
// }
|
||||
#endif
|
||||
|
||||
//串口配置
|
||||
for (i = 0;i < ARRAY_LEN(COM_PERIPH);i++) {
|
||||
GpioConfig(&COM_PERIPH[i].TxPort, GPIO_Mode_AF_PP, GPIO_Speed_2MHz);
|
||||
GpioConfig(&COM_PERIPH[i].RxPort, GPIO_Mode_IPU, GPIO_Speed_2MHz);
|
||||
}
|
||||
|
||||
INTERRUPT_ENABLE
|
||||
|
||||
BspExtraInit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 扩展库初始化
|
||||
* @note 理论上扩展库要在BSP的最后加载
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void BspExtraInit(void)
|
||||
{
|
||||
//LetterShell初始化
|
||||
#ifdef TTY_COM
|
||||
LetterShellInit(TTY_COM,TTY_BAUD);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 打开LED灯
|
||||
* @note void
|
||||
* @param Chnl 通道
|
||||
* @retval void
|
||||
*/
|
||||
void LedOn(uint8_t Chnl)
|
||||
{
|
||||
if (Chnl >= LED_NUM)
|
||||
return;
|
||||
GpioSet(&LED_PERIPH[Chnl].GpioX, RESET);
|
||||
}
|
||||
/**
|
||||
* @brief 关闭LED灯
|
||||
* @note void
|
||||
* @param Chnl 通道
|
||||
* @retval void
|
||||
*/
|
||||
void LedOff(uint8_t Chnl)
|
||||
{
|
||||
if (Chnl >= LED_NUM)
|
||||
return;
|
||||
GpioSet(&LED_PERIPH[Chnl].GpioX, SET);
|
||||
}
|
||||
|
||||
void LedSet(uint8_t Chnl,uint8_t Value)
|
||||
{
|
||||
if (Chnl >= LED_NUM)
|
||||
return;
|
||||
if (Value == LED_ON) {
|
||||
GpioSet(&LED_PERIPH[Chnl].GpioX, RESET);
|
||||
}
|
||||
else {
|
||||
GpioSet(&LED_PERIPH[Chnl].GpioX, SET);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 切换LED灯状态
|
||||
* @note void
|
||||
* @param Chnl 通道 Status 状态
|
||||
* @retval void
|
||||
*/
|
||||
void LedToggle(uint8_t Chnl)
|
||||
{
|
||||
if (Chnl >= LED_NUM)
|
||||
return;
|
||||
if (GpioGet(&LED_PERIPH[Chnl].GpioX) == LED_ON)
|
||||
{
|
||||
GpioSet(&LED_PERIPH[Chnl].GpioX,LED_OFF);
|
||||
}
|
||||
else
|
||||
{
|
||||
GpioSet(&LED_PERIPH[Chnl].GpioX,LED_ON);
|
||||
}
|
||||
}
|
||||
|
||||
void Rs485StdConfig(const COM_MAP_T *ComX,unsigned int Baud)
|
||||
{
|
||||
for (unsigned char i = 0;i < ARRAY_LEN(COM_PERIPH);i++) {
|
||||
if (ComX == COM_PERIPH[i].ComX) {
|
||||
if (COM_PERIPH[i].TxRxEn.ClockTree) {
|
||||
GpioSet(&COM_PERIPH[i].TxRxEn,Baud);
|
||||
}
|
||||
ComStdConfig(COM_PERIPH[i].ComX,Baud);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Rs485AdvConfig(const COM_MAP_T *ComX, uint32_t Baud, uint8_t DataBits, uint8_t StopBits, uint8_t Parity)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 重写Printf函数
|
||||
* @note void
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
#if defined(__CC_ARM) || defined(__CLANG_ARM)
|
||||
int fputc(int ch, FILE *f)
|
||||
{
|
||||
UsartSendChar(TTY_COM, ch);
|
||||
return ch;
|
||||
}
|
||||
int fgetc(FILE *f)
|
||||
{
|
||||
uint8_t ch;
|
||||
ch = UsartReceiveChar(TTY_COM);
|
||||
return ch;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "Bsp.h"
|
||||
|
||||
#define DEM_CR *(volatile u32 *)0xE000EDFC
|
||||
#define DWT_CR *(volatile u32 *)0xE0001000
|
||||
#define DWT_CYCCNT *(volatile u32 *)0xE0001004
|
||||
|
||||
#define DEM_CR_TRCENA (1 << 24)
|
||||
#define DWT_CR_CYCCNTENA (1 << 0)
|
||||
|
||||
/**
|
||||
* @brief 初始化延时函数
|
||||
* @param void
|
||||
* @retval void
|
||||
* @note void
|
||||
* @example void
|
||||
*/
|
||||
void DelayConfig(void)
|
||||
{
|
||||
DEM_CR |= DEM_CR_TRCENA;
|
||||
DWT_CR |= DWT_CR_CYCCNTENA;
|
||||
DWT_CYCCNT = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 读取DWT计数器的值
|
||||
* @param void
|
||||
* @retval void
|
||||
* @note void
|
||||
* @example void
|
||||
*/
|
||||
unsigned int DwtCntGet(void)
|
||||
{
|
||||
return((uint32_t)DWT_CYCCNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 微秒级延时函数
|
||||
* @param void
|
||||
* @retval void
|
||||
* @note void
|
||||
* @example void
|
||||
*/
|
||||
void DelayUs(unsigned int Us)
|
||||
{
|
||||
uint32_t Start = 0,End = 0,Ts = 0;
|
||||
Start = DwtCntGet();
|
||||
|
||||
Ts = Us * (SystemCoreClock / 1000000U);
|
||||
|
||||
End = Start + Ts;
|
||||
|
||||
while(DwtCntGet() < End){;}
|
||||
}
|
||||
/**
|
||||
* @brief 毫秒级延时函数
|
||||
* @param void
|
||||
* @retval void
|
||||
* @note void
|
||||
* @example void
|
||||
*/
|
||||
void DelayMs(unsigned int Ms)
|
||||
{
|
||||
DelayUs(Ms * 1000);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include "Bsp.h"
|
||||
|
||||
/**
|
||||
* @brief 启用GPIO口时钟
|
||||
* @note void
|
||||
* @param GpioX:GPIO引脚
|
||||
* @retval void
|
||||
*/
|
||||
void GpioClockEnable(const GPIO_MAP_T *GpioX)
|
||||
{
|
||||
GpioX->ClockCmd(GpioX->ClockTree,ENABLE);
|
||||
}
|
||||
/**
|
||||
* @brief 配置GPIO口
|
||||
* @note void
|
||||
* @param GpioX:引脚 GpioMode:GPIO模式 GpioSpeed:GPIO速度
|
||||
* @retval void
|
||||
*/
|
||||
void GpioConfig(const GPIO_MAP_T *GpioX, GPIOMode_TypeDef GpioMode, GPIOSpeed_TypeDef GpioSpeed)
|
||||
{
|
||||
GPIO_InitTypeDef GpioInitSt;
|
||||
|
||||
GpioInitSt.GPIO_Pin = GpioX->Periph.GpioPin;
|
||||
GpioInitSt.GPIO_Mode = GpioMode;
|
||||
GpioInitSt.GPIO_Speed = GpioSpeed;
|
||||
//打开外设时钟
|
||||
GpioClockEnable(GpioX);
|
||||
GPIO_Init(GpioX->Periph.GpioPort, &GpioInitSt);
|
||||
}
|
||||
/**
|
||||
* @brief 改变GPIO口状态
|
||||
* @note
|
||||
* @param GpioX:GPIO引脚 Value:设置值
|
||||
* @retval void
|
||||
*/
|
||||
void GpioSet(const GPIO_MAP_T *GpioX, uint8_t Value)
|
||||
{
|
||||
if (Value == (uint8_t)RESET)
|
||||
{
|
||||
GPIO_ResetBits(GpioX->Periph.GpioPort, GpioX->Periph.GpioPin);
|
||||
}
|
||||
else if (Value == (uint8_t)SET)
|
||||
{
|
||||
GPIO_SetBits(GpioX->Periph.GpioPort, GpioX->Periph.GpioPin);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief 读取GPIO口状态
|
||||
* @note
|
||||
* @param GpioX:GPIO引脚
|
||||
* @retval 0:低电平,1高电平
|
||||
*/
|
||||
unsigned char GpioGet(const GPIO_MAP_T *GpioX)
|
||||
{
|
||||
return (unsigned char)GPIO_ReadInputDataBit(GpioX->Periph.GpioPort, GpioX->Periph.GpioPin);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
#include <stddef.h>
|
||||
|
||||
#include "Bsp.h"
|
||||
|
||||
IRQ_MAP_T IRQ_PERIPH[IRQ_NUM];
|
||||
|
||||
/**
|
||||
* @brief 中断回调函数初始化为空
|
||||
* @note 初始化系统中断
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void IrqInit(void)
|
||||
{
|
||||
//设定系统中断组
|
||||
NVIC_PriorityGroupConfig(NVIC_GROUP_LEVEL);
|
||||
for (uint8_t i = 0; i < IRQ_NUM; i++) {
|
||||
IRQ_PERIPH[i].IrqCallback = NULL;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief 中断回调函数注册
|
||||
* @note 将外部传入的函数地址关联到INTERRUPT_ST表中
|
||||
* @param Vector:中断号 void(*Func)(uint32_t):回调函数
|
||||
* @retval void
|
||||
*/
|
||||
void IrqRegister(unsigned char Irqn,void(*Func)(void *Param)) {
|
||||
IRQ_PERIPH[Irqn].Irqn = Irqn;
|
||||
IRQ_PERIPH[Irqn].IrqCallback = Func;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 中断NVIC配置
|
||||
* @note 设定一个中断的优先级
|
||||
* @param Vector:中断号 NvicPrePriority:主优先级 NvicSubPriority:抢占优先级
|
||||
* @retval void
|
||||
*/
|
||||
void IrqEnable(uint32_t Irq, uint8_t NvicPrePriority, uint8_t NvicSubPriority)
|
||||
{
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
|
||||
NVIC_InitStructure.NVIC_IRQChannel = Irq;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NvicPrePriority;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NvicSubPriority;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 关闭中断
|
||||
* @note void
|
||||
* @param Vector:中断号
|
||||
* @retval void
|
||||
*/
|
||||
void IrqDisable(uint32_t Irq)
|
||||
{
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
NVIC_InitStructure.NVIC_IRQChannel = Irq;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
}
|
||||
|
||||
void USART1_IRQHandler(void)
|
||||
{
|
||||
if (IRQ_PERIPH[COM0->Periph.Irqn].IrqCallback != NULL) {
|
||||
if (USART_GetFlagStatus(COM0->Periph.UartId, USART_FLAG_ORE) == SET)
|
||||
{
|
||||
USART_ClearFlag(COM0->Periph.UartId, USART_FLAG_ORE);
|
||||
}
|
||||
IRQ_PERIPH[COM0->Periph.Irqn].IrqCallback(COM0);
|
||||
}
|
||||
}
|
||||
|
||||
void USART2_IRQHandler(void)
|
||||
{
|
||||
if (USART_GetFlagStatus(COM1->Periph.UartId, USART_FLAG_ORE) == SET)
|
||||
{
|
||||
USART_ClearFlag(COM1->Periph.UartId, USART_FLAG_ORE);
|
||||
}
|
||||
if (IRQ_PERIPH[COM1->Periph.Irqn].IrqCallback != NULL) {
|
||||
IRQ_PERIPH[COM1->Periph.Irqn].IrqCallback(COM1);
|
||||
}
|
||||
}
|
||||
|
||||
void USART3_IRQHandler(void)
|
||||
{
|
||||
if (USART_GetFlagStatus(COM2->Periph.UartId, USART_FLAG_ORE) == SET)
|
||||
{
|
||||
USART_ClearFlag(COM2->Periph.UartId, USART_FLAG_ORE);
|
||||
}
|
||||
if (IRQ_PERIPH[COM2->Periph.Irqn].IrqCallback != NULL) {
|
||||
IRQ_PERIPH[COM2->Periph.Irqn].IrqCallback(COM2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void USART4_IRQHandler(void)
|
||||
{
|
||||
if (USART_GetFlagStatus(COM3->Periph.UartId, USART_FLAG_ORE) == SET)
|
||||
{
|
||||
USART_ClearFlag(COM3->Periph.UartId, USART_FLAG_ORE);
|
||||
}
|
||||
if (IRQ_PERIPH[COM3->Periph.Irqn].IrqCallback != NULL) {
|
||||
IRQ_PERIPH[COM3->Periph.Irqn].IrqCallback(COM3);
|
||||
}
|
||||
}
|
||||
|
||||
void USART5_IRQHandler(void)
|
||||
{
|
||||
if (USART_GetFlagStatus(COM4->Periph.UartId, USART_FLAG_ORE) == SET)
|
||||
{
|
||||
USART_ClearFlag(COM4->Periph.UartId, USART_FLAG_ORE);
|
||||
}
|
||||
if (IRQ_PERIPH[COM4->Periph.Irqn].IrqCallback != NULL) {
|
||||
IRQ_PERIPH[COM4->Periph.Irqn].IrqCallback(COM4);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
/*********************************************************************
|
||||
* (c) SEGGER Microcontroller GmbH *
|
||||
* The Embedded Experts *
|
||||
* www.segger.com *
|
||||
**********************************************************************
|
||||
|
||||
-------------------------- END-OF-HEADER -----------------------------
|
||||
Purpose : Implementation of low-level functions for I/O with the
|
||||
SEGGER Runtime Library
|
||||
using a UART (SEGGER's BSP UART module)
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* #include section
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
#if defined (__SEGGER__)
|
||||
#include "__SEGGER_RTL_Int.h"
|
||||
#include "stdio.h"
|
||||
#include "Bsp.h"
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Local types
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
struct __SEGGER_RTL_FILE_impl { // NOTE: Provides implementation for FILE
|
||||
int stub; // only needed so impl has size != 0.
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Static data
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
static FILE __SEGGER_RTL_stdin_file = { 0 }; // stdin reads from UART
|
||||
static FILE __SEGGER_RTL_stdout_file = { 0 }; // stdout writes to UART
|
||||
static FILE __SEGGER_RTL_stderr_file = { 0 }; // stderr writes to UART
|
||||
|
||||
static unsigned int _UART_Port = TTY_COM;
|
||||
static int _stdin_ungot = EOF;
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Public data
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
FILE *stdin = &__SEGGER_RTL_stdin_file; // NOTE: Provide implementation of stdin for RTL.
|
||||
FILE *stdout = &__SEGGER_RTL_stdout_file; // NOTE: Provide implementation of stdout for RTL.
|
||||
FILE *stderr = &__SEGGER_RTL_stderr_file; // NOTE: Provide implementation of stderr for RTL.
|
||||
|
||||
|
||||
void *__aeabi_read_tp(void) {
|
||||
return 0; // 单线程环境下直接返回 0
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Static code
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* _stdin_getc()
|
||||
*
|
||||
* Function description
|
||||
* Get character from standard input.
|
||||
*
|
||||
* Return value
|
||||
* Character received.
|
||||
*
|
||||
* Additional information
|
||||
* This function never fails to deliver a character.
|
||||
*/
|
||||
static char _stdin_getc(void) {
|
||||
unsigned char c;
|
||||
|
||||
if (_stdin_ungot != EOF) {
|
||||
c = _stdin_ungot;
|
||||
_stdin_ungot = EOF;
|
||||
} else {
|
||||
c = UsartReceiveChar(_UART_Port);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Public code
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* RTL_UART_Init()
|
||||
*
|
||||
* Function description
|
||||
* Initialize RTL to use given UART for stdio.
|
||||
*
|
||||
* Parameters
|
||||
* Unit : UART unit number (typically zero-based).
|
||||
* Baudrate : Baud rate to configure [Hz].
|
||||
* NumDataBits: Number of data bits to use.
|
||||
* Parity : One of the following values:
|
||||
* * BSP_UART_PARITY_NONE
|
||||
* * BSP_UART_PARITY_ODD
|
||||
* * BSP_UART_PARITY_EVEN
|
||||
* NumStopBits: Number of stop bits to use.
|
||||
*
|
||||
* Additional description
|
||||
* Parameters are same as for BSP_UART_Init().
|
||||
* This also sets appropriate RX and TX interrupt handlers.
|
||||
*/
|
||||
void RTL_UART_Init(unsigned int Unit, unsigned long Baudrate, unsigned char NumDataBits, unsigned char Parity, unsigned char NumStopBits) {
|
||||
_UART_Port = Unit;
|
||||
UsartStdConfig(_UART_Port, Baudrate);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* __SEGGER_RTL_X_file_stat()
|
||||
*
|
||||
* Function description
|
||||
* Get file status.
|
||||
*
|
||||
* Parameters
|
||||
* stream - Pointer to file.
|
||||
*
|
||||
* Additional information
|
||||
* Low-overhead test to determine if stream is valid. If stream
|
||||
* is a valid pointer and the stream is open, this function must
|
||||
* succeed. If stream is a valid pointer and the stream is closed,
|
||||
* this function must fail.
|
||||
*
|
||||
* The implementation may optionally determine whether stream is
|
||||
* a valid pointer: this may not always be possible and is not
|
||||
* required, but may assist debugging when clients provide wild
|
||||
* pointers.
|
||||
*
|
||||
* Return value
|
||||
* < 0 - Failure, stream is not a valid file.
|
||||
* >= 0 - Success, stream is a valid file.
|
||||
*/
|
||||
int __SEGGER_RTL_X_file_stat(FILE *stream) {
|
||||
if (stream == stdin || stream == stdout || stream == stderr) {
|
||||
return 0; // NOTE: stdin, stdout, and stderr are assumed to be valid.
|
||||
} else {
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* __SEGGER_RTL_X_file_bufsize()
|
||||
*
|
||||
* Function description
|
||||
* Get stream buffer size.
|
||||
*
|
||||
* Parameters
|
||||
* stream - Pointer to file.
|
||||
*
|
||||
* Additional information
|
||||
* Returns the number of characters to use for buffered I/O on
|
||||
* the file stream. The I/O buffer is allocated on the stack
|
||||
* for the duration of the I/O call, therefore this value should
|
||||
* not be set arbitrarily large.
|
||||
*
|
||||
* For unbuffered I/O, return 1.
|
||||
*
|
||||
* Return value
|
||||
* Nonzero number of characters to use for buffered I/O; for
|
||||
* unbuffered I/O, return 1.
|
||||
*/
|
||||
int __SEGGER_RTL_X_file_bufsize(FILE *stream) {
|
||||
(void)stream;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* __SEGGER_RTL_X_file_read()
|
||||
*
|
||||
* Function description
|
||||
* Read data from file.
|
||||
*
|
||||
* Parameters
|
||||
* stream - Pointer to file to read from.
|
||||
* s - Pointer to object that receives the input.
|
||||
* len - Number of characters to read from file.
|
||||
*
|
||||
* Return value
|
||||
* >= 0 - Success, amount of data read.
|
||||
* < 0 - Failure.
|
||||
*
|
||||
* Additional information
|
||||
* Reading from any stream other than stdin results in an error.
|
||||
*/
|
||||
int __SEGGER_RTL_X_file_read(FILE *stream, char *s, unsigned len) {
|
||||
int c;
|
||||
|
||||
if (stream == stdin) {
|
||||
c = 0;
|
||||
while (len > 0) {
|
||||
*s = _stdin_getc();
|
||||
++s;
|
||||
++c;
|
||||
--len;
|
||||
}
|
||||
} else {
|
||||
c = EOF;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* __SEGGER_RTL_X_file_write()
|
||||
*
|
||||
* Function description
|
||||
* Write data to file.
|
||||
*
|
||||
* Parameters
|
||||
* stream - Pointer to file to write to.
|
||||
* s - Pointer to object to write to file.
|
||||
* len - Number of characters to write to the file.
|
||||
*
|
||||
* Return value
|
||||
* >= 0 - Success.
|
||||
* < 0 - Failure.
|
||||
*
|
||||
* Additional information
|
||||
* this version is NOT reentrant!
|
||||
* stdout and stderr are directed to UART;
|
||||
* writing to any stream other than stdout or stderr results in an error
|
||||
*/
|
||||
int __SEGGER_RTL_X_file_write(FILE *stream, const char *s, unsigned len) {
|
||||
if ((stream == stdout) || (stream == stderr)) {
|
||||
UsartSendStr(_UART_Port, (uint8_t* ) s, len);
|
||||
return len;
|
||||
} else {
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* __SEGGER_RTL_X_file_unget()
|
||||
*
|
||||
* Function description
|
||||
* Push character back to stream.
|
||||
*
|
||||
* Parameters
|
||||
* stream - Pointer to file to push back to.
|
||||
* c - Character to push back.
|
||||
*
|
||||
* Return value
|
||||
* >= 0 - Success.
|
||||
* < 0 - Failure.
|
||||
*
|
||||
* Additional information
|
||||
* Push-back is only supported for standard input, and
|
||||
* only a single-character pushback buffer is implemented.
|
||||
*/
|
||||
int __SEGGER_RTL_X_file_unget(FILE *stream, int c) {
|
||||
if (stream == stdin) {
|
||||
if (c != EOF && _stdin_ungot == EOF) {
|
||||
_stdin_ungot = c;
|
||||
} else {
|
||||
c = EOF;
|
||||
}
|
||||
} else {
|
||||
c = EOF;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
#endif
|
||||
/*************************** End of file ****************************/
|
||||
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file syscalls.c
|
||||
* @author Suroy Wrote with Auto-generated by STM32CubeIDE
|
||||
* @url https://suroy.cn
|
||||
* @brief STM32CubeIDE Minimal System calls file
|
||||
*
|
||||
* For more information about which c-functions
|
||||
* need which of these lowlevel functions
|
||||
* please consult the Newlib libc-manual
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2020-2022 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
#if defined (__GNUC__)
|
||||
/* Includes */
|
||||
#include "Bsp.h"
|
||||
|
||||
|
||||
/* Variables */
|
||||
extern int __io_putchar(int ch) __attribute__((weak));
|
||||
extern int __io_getchar(void) __attribute__((weak));
|
||||
|
||||
|
||||
|
||||
/* Functions */
|
||||
|
||||
__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;
|
||||
}
|
||||
|
||||
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
|
||||
#define GETCHAR_PROTOTYPE int __io_getchar(void)
|
||||
|
||||
/**
|
||||
* 函数功能: 重定向 c库函数 printf到 DEBUG_USARTx
|
||||
* 输入参数: 无
|
||||
* 返 回 值: 无
|
||||
* 说 明:无
|
||||
*/
|
||||
PUTCHAR_PROTOTYPE
|
||||
{
|
||||
ComSendChar(TTY_COM, ch); //阻塞式无限等待
|
||||
return ch;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 函数功能: 重定向 c库函数 getchar,scanf到 DEBUG_USARTx
|
||||
* 输入参数: 无
|
||||
* 返 回 值: 无
|
||||
* 说 明:无
|
||||
*/
|
||||
GETCHAR_PROTOTYPE
|
||||
{
|
||||
uint8_t ch = 0;
|
||||
ch = ComReceiveChar(TTY_COM);
|
||||
return ch;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
#include "Bsp.h"
|
||||
|
||||
extern const COM_CONFIG COM_PERIPH[];
|
||||
|
||||
/**
|
||||
* @brief 初始化UART口外设的时钟
|
||||
* @note void
|
||||
* @param ComX:串口号
|
||||
* @retval void
|
||||
*/
|
||||
void UartClockEnable(const COM_MAP_T *ComX)
|
||||
{
|
||||
ComX->ClockCmd(ComX->ClockTree,ENABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化USART口
|
||||
* @note void
|
||||
* @param Comx 串口号 baud 波特率 DataBits 数据位 StopBits 停止位 Parity校验位
|
||||
* @retval void
|
||||
*/
|
||||
void ComInit(const COM_MAP_T *ComX, uint32_t baud, uint8_t DataBits, uint8_t StopBits, uint8_t Parity)
|
||||
{
|
||||
USART_InitTypeDef UsartInitSt;
|
||||
|
||||
//初始化串口
|
||||
USART_StructInit(&UsartInitSt);
|
||||
UsartInitSt.USART_BaudRate = baud;
|
||||
UsartInitSt.USART_WordLength = DataBits;
|
||||
UsartInitSt.USART_StopBits = StopBits;
|
||||
UsartInitSt.USART_Parity = Parity;
|
||||
UsartInitSt.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||||
UsartInitSt.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
||||
UartClockEnable(ComX);
|
||||
USART_Init(ComX->Periph.UartId, &UsartInitSt);
|
||||
//配置中断
|
||||
USART_ITConfig(ComX->Periph.UartId, USART_IT_RXNE, ENABLE);
|
||||
//启用串口
|
||||
USART_Cmd(ComX->Periph.UartId, ENABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART口发送数据
|
||||
* @note void
|
||||
* @param ComX 串口号 Data数据
|
||||
* @retval void
|
||||
*/
|
||||
void ComSendChar(const COM_MAP_T *ComX,uint8_t Data)
|
||||
{
|
||||
USART_SendData(ComX->Periph.UartId, Data);
|
||||
while (USART_GetFlagStatus(ComX->Periph.UartId, USART_FLAG_TC) != SET);
|
||||
}
|
||||
/**
|
||||
* @brief USART口发送字符串
|
||||
* @note void
|
||||
* @param ComX 串口号 Data数据 Len 长度
|
||||
* @retval void
|
||||
*/
|
||||
void ComSendStr(const COM_MAP_T *ComX,uint8_t *Data, uint64_t Len)
|
||||
{
|
||||
for (uint64_t i = 0;i < Len;i++)
|
||||
{
|
||||
USART_SendData(ComX->Periph.UartId, Data[i]);
|
||||
while (USART_GetFlagStatus(ComX->Periph.UartId, USART_FLAG_TC) != SET);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief USART口发送数据
|
||||
* @note void
|
||||
* @param ComX 串口号
|
||||
* @retval 读取的数据
|
||||
*/
|
||||
uint8_t ComReceiveChar(const COM_MAP_T *ComX)
|
||||
{
|
||||
return USART_ReceiveData(ComX->Periph.UartId);
|
||||
}
|
||||
/**
|
||||
* @brief 通用USART配置
|
||||
* @note void
|
||||
* @param ComX 串口号 baud 波特率
|
||||
* @retval void
|
||||
*/
|
||||
void ComStdConfig(const COM_MAP_T *ComX, uint32_t Baud)
|
||||
{
|
||||
ComInit(ComX, Baud, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No);
|
||||
}
|
||||
/**
|
||||
* @brief 高级串口配置
|
||||
* @note void
|
||||
* @param ComX 串口号 baud 波特率 Data Bits 数据位 StopBits 停止位 Parity校验位
|
||||
* @retval void
|
||||
*/
|
||||
void ComAdvConfig(const COM_MAP_T *ComX, uint32_t Baud, uint8_t DataBits, uint8_t StopBits, uint8_t Parity)
|
||||
{
|
||||
ComInit(ComX, Baud, DataBits, StopBits, Parity);
|
||||
}
|
||||
|
||||
unsigned int CalcBaudTransIoSpeed(unsigned int Baud) {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user