Backup before error handling refactoring
This commit is contained in:
296
USER/Ref/bmu-18s-firmware/SYSTEM/delay.c
Normal file
296
USER/Ref/bmu-18s-firmware/SYSTEM/delay.c
Normal file
@@ -0,0 +1,296 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file STM32F4xx_IAP/system/delay.c
|
||||
* @author AMO ESS Application Team
|
||||
* @version V1.0.0
|
||||
* @date 11-October-2019
|
||||
* @brief This file provides delay operation functions.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
||||
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
||||
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
||||
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
||||
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
Modify the description
|
||||
V1.1 20140803
|
||||
1,delay_us, add parameter equal to 0 to determine, if the parameter is equal to 0, then exit.
|
||||
2, under the modified ucosii, delay_ms function, join the OSLockNesting judgment, entered after a break, also can be used to delay.
|
||||
V1.2 20150411
|
||||
Modify the way OS support to support any OS (not limited to UCOSII and UCOSIII, in theory any OS can support)
|
||||
Add: delay_osrunning/delay_ostickspersec/delay_osintnesting three macro definitions
|
||||
Add: delay_osschedlock/delay_osschedunlock/delay_ostimedly three-function
|
||||
V1.3 20150521
|
||||
2 bug when amend UCOSIII support:
|
||||
Delay_tickspersec read: delay_ostickspersec
|
||||
Delay_intnesting read: delay_osintnesting
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F4xx_IAP
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "delay.h"
|
||||
#include "sys.h"
|
||||
//********************************************************************************
|
||||
//If you are using ucos, include the following header file.
|
||||
#if SYSTEM_SUPPORT_OS
|
||||
#include "includes.h" //ucos use
|
||||
#endif
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
static u32 fac_us=0; //us delay times multiplier
|
||||
static u32 fac_ms=0; //ms delay times multiplier under the OS, ms representing each beat
|
||||
|
||||
#if SYSTEM_SUPPORT_OS //If SYSTEM_SUPPORT_OS is defined, and instructions to support OS (not limited to UCOS).
|
||||
/**
|
||||
******************************************************************************
|
||||
When delay_us/delay_ms needs to support OS requires three macro definitions and functions associated with the OS to support
|
||||
First 3 macro definition:
|
||||
delay_osrunning: used to indicate whether the OS is currently running to determine if you can use the correlation function
|
||||
delay_ostickspersec: used to represent the OS set the clock ticks, delay_init will be based on the parameters to initial systick
|
||||
delay_osintnesting: used to represent OS interrupt nesting level, since interrupts cannot be scheduled, delay_ms uses this parameter to determine how to run
|
||||
Then the 3 functions:
|
||||
delay_osschedlock:Used to lock a OS task scheduling, prohibit scheduling
|
||||
delay_osschedunlock:Used to unlock the OS task scheduling, reopening schedule
|
||||
delay_ostimedly:For the OS delay, can cause the task scheduling.
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
// This routine only UCOSII and UCOSIII support, other OS, please make reference to transplantation
|
||||
// Support UCOSII
|
||||
#ifdef OS_CRITICAL_METHOD // OS_CRITICAL_METHOD definitions, instructions to support UCOSII
|
||||
#define delay_osrunning OSRunning // OS runs mark, 0, does not run 1, run
|
||||
#define delay_ostickspersec OS_TICKS_PER_SEC // OS clock ticks, or scheduled per second times
|
||||
#define delay_osintnesting OSIntNesting // Interrupt nesting level, which interrupt nesting times
|
||||
#endif
|
||||
|
||||
// Support UCOSIII
|
||||
#ifdef CPU_CFG_CRITICAL_METHOD // CPU_CFG_CRITICAL_METHOD definitions, instructions to support UCOSIII
|
||||
#define delay_osrunning OSRunning // OS runs mark, 0, does not run 1, run
|
||||
#define delay_ostickspersec OSCfg_TickRate_Hz // OS clock ticks, or scheduled per second times
|
||||
#define delay_osintnesting OSIntNestingCtr // Interrupt nesting level, which interrupt nesting times
|
||||
#endif
|
||||
|
||||
|
||||
//us-level delay time, close the task schedule (to prevent interrupted us delay)
|
||||
void delay_osschedlock(void)
|
||||
{
|
||||
#ifdef CPU_CFG_CRITICAL_METHOD //Use UCOSIII
|
||||
OS_ERR err;
|
||||
OSSchedLock(&err); //UCOSIII way, prohibit scheduling to prevent interrupted us delayed
|
||||
#else //Otherwise UCOSII
|
||||
OSSchedLock(); //UCOSII way prohibit scheduling to prevent interrupted us delayed
|
||||
#endif
|
||||
}
|
||||
|
||||
//us-level delay time, restore task scheduling
|
||||
void delay_osschedunlock(void)
|
||||
{
|
||||
#ifdef CPU_CFG_CRITICAL_METHOD //Use UCOSIII
|
||||
OS_ERR err;
|
||||
OSSchedUnlock(&err); //UCOSIII way, prohibit scheduling to prevent interrupted us delayed
|
||||
#else //Otherwise UCOSII
|
||||
OSSchedUnlock(); //UCOSII way prohibit scheduling to prevent interrupted us delayed
|
||||
#endif
|
||||
}
|
||||
|
||||
//OS is called the delay function delay
|
||||
//ticks:Delay number of beats
|
||||
void delay_ostimedly(u32 ticks)
|
||||
{
|
||||
#ifdef CPU_CFG_CRITICAL_METHOD
|
||||
OS_ERR err;
|
||||
OSTimeDly(ticks,OS_OPT_TIME_PERIODIC,&err);//UCOSIII delay adoption cycle mode
|
||||
#else
|
||||
OSTimeDly(ticks); //UCOSII delay
|
||||
#endif
|
||||
}
|
||||
|
||||
void delay_os_ms(u32 ms)
|
||||
{
|
||||
delay_ostimedly(ms);
|
||||
}
|
||||
|
||||
//systick interrupt function, used to use the OS
|
||||
//void SysTick_Handler(void)
|
||||
//{
|
||||
// if(delay_osrunning==1) //OS begins to run and perform normal scheduling process
|
||||
// {
|
||||
// OSIntEnter(); //To enter a break
|
||||
// OSTimeTick(); //Call the ucos clock service program
|
||||
// OSIntExit(); //Task switching is triggered a soft interrupt
|
||||
// }
|
||||
//}
|
||||
#endif
|
||||
|
||||
// Initialization delay function
|
||||
// When using ucos when this function initializes the ucos ticks
|
||||
// SYSTICK clock fixed to the AHB clock 1/8
|
||||
// SYSCLK:System clock frequency
|
||||
void delay_init(u32 SYSCLK)
|
||||
{
|
||||
#if SYSTEM_SUPPORT_OS //If you need to support OS.
|
||||
u32 reload;
|
||||
#endif
|
||||
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); // SYSTICK using an external clock source
|
||||
fac_us = (SYSCLK / 1000000) / 8; // Regardless of whether or not to use OS,fac_us you need to use
|
||||
#if SYSTEM_SUPPORT_OS // If you need to support OS.
|
||||
reload = (SYSCLK / 1000000) / 8; // Count number of times per second unit k
|
||||
reload *= 1000000 / delay_ostickspersec; // According to the delay_ostickspersec set overrun time
|
||||
// Reload is a 24-bit registers, and the maximum value: 16777216, 72M, about 1.86s
|
||||
fac_ms = 1000 / delay_ostickspersec; // Representing OS can delay the minimum unit
|
||||
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable interrupted SYSTICK
|
||||
SysTick->LOAD = reload; // Interrupted once every 1/OS_TICKS_PER_SEC seconds
|
||||
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Enable the SYSTICK
|
||||
#else
|
||||
fac_ms = (u16)fac_us * 1000; //Under non-OS, representing each of the MS needs systick clock
|
||||
#endif
|
||||
}
|
||||
|
||||
#if SYSTEM_SUPPORT_OS //If you need to support OS.
|
||||
//Delay nus
|
||||
//nus:Number of us to delay.
|
||||
//nus:0~204522252(Maximum 2^32/fac_us@fac_us=21)
|
||||
void delay_us(u32 nus)
|
||||
{
|
||||
u32 ticks;
|
||||
u32 told, tnow, tcnt = 0;
|
||||
u32 reload = SysTick->LOAD; //LOAD value
|
||||
ticks = nus * fac_us; //Number of beats you want
|
||||
delay_osschedlock(); //Stop OS scheduling to prevent interrupted us delayed
|
||||
told = SysTick->VAL; //When entering the counter value
|
||||
while (1)
|
||||
{
|
||||
tnow = SysTick->VAL;
|
||||
if (tnow != told)
|
||||
{
|
||||
if (tnow < told) tcnt += told - tnow; //Here look at the SYSTICK is a declining counters can.
|
||||
else tcnt += reload - tnow + told;
|
||||
told = tnow;
|
||||
if(tcnt >= ticks) break; //Time greater than or equal to the delay time, then exit.
|
||||
}
|
||||
};
|
||||
delay_osschedunlock(); //Restore OS scheduling
|
||||
}
|
||||
//Delay nms
|
||||
//nms:Number of ms to delay
|
||||
//nms:0~65535
|
||||
void delay_ms(u16 nms)
|
||||
{
|
||||
if (delay_osrunning && delay_osintnesting==0)//If the OS is already running, and is not interrupted (broken not scheduling)
|
||||
{
|
||||
if (nms >= fac_ms) //Delay time is greater than the minimum OS time cycle
|
||||
{
|
||||
delay_ostimedly(nms/fac_ms); //OS extended
|
||||
}
|
||||
nms %= fac_ms; //OS has been unable to provide such a small time delay, using ordinary delay
|
||||
}
|
||||
delay_us((u32)(nms * 1000)); //Average delay
|
||||
}
|
||||
#else //When no ucos
|
||||
//Delay nus
|
||||
//nus as a number of us to delay.
|
||||
//Note: the value of the nus, not greater than 798915us (max 2^24/fac_us@fac_us=21)
|
||||
void delay_us(u32 nus)
|
||||
{
|
||||
u32 temp;
|
||||
|
||||
SysTick->LOAD = nus*fac_us; //Time to load
|
||||
SysTick->VAL = 0x00; //Clear counters
|
||||
SysTick->CTRL = 0x01 ; //Start the countdown
|
||||
do
|
||||
{
|
||||
temp = SysTick->CTRL;
|
||||
} while ((temp & 0x01) && !(temp & (1 << 16))); //Waiting for the time to arrive
|
||||
SysTick->CTRL = 0x00; //Closed counters
|
||||
SysTick->VAL = 0X00; //Clear counters
|
||||
}
|
||||
|
||||
//Delay nms
|
||||
//Note range of nms
|
||||
//SysTick->LOAD is a 24-bit registers, so the delay as follows:
|
||||
//nms < 0xffffff * 8 * 1000/SYSCLK =
|
||||
//SYSCLK units are Hz,nms for ms
|
||||
//Under the 168M, nms<=798ms
|
||||
void delay_xms(u16 nms)
|
||||
{
|
||||
u32 temp;
|
||||
|
||||
SysTick->LOAD = (u32)nms * fac_ms; // Load time (SysTick->LOAD 24bit)
|
||||
SysTick->VAL = 0x00; // Clear counters
|
||||
SysTick->CTRL = 0x01 ; // Start the countdown
|
||||
do
|
||||
{
|
||||
temp = SysTick->CTRL;
|
||||
} while ((temp & 0x01) && !(temp & (1 << 16))); //Waiting for the time to arrive
|
||||
SysTick->CTRL = 0x00; //Closed counters
|
||||
SysTick->VAL = 0X00; //Clear counters
|
||||
}
|
||||
//Delay nms
|
||||
//nms:0~65535
|
||||
void delay_ms(u16 nms)
|
||||
{
|
||||
u8 repeat = nms / 1000; // 540 here, some customers may overclock is taken into account,
|
||||
// For example when overclocked to 248M, delay_xms maximum can only delay 541ms
|
||||
u16 remain = nms % 1000;
|
||||
while (repeat)
|
||||
{
|
||||
delay_xms(1000);
|
||||
repeat--;
|
||||
}
|
||||
if (remain) delay_xms(remain);
|
||||
|
||||
IWDG_ReloadCounter();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
65
USER/Ref/bmu-18s-firmware/SYSTEM/delay.h
Normal file
65
USER/Ref/bmu-18s-firmware/SYSTEM/delay.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef __DELAY_H
|
||||
#define __DELAY_H
|
||||
#include <sys.h>
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//This program is for educational purposes only, without the author's permission, may not be used for any other purpose
|
||||
//ALIENTEK STM32F407 Development Board
|
||||
//SysTick managed to delay the normal counting mode (supports ucosii)
|
||||
//Include delay_us,delay_ms
|
||||
//Hot Atomic @ALIENTEK
|
||||
//Forum: www.openedv.com
|
||||
//Date created: 2014/5/2
|
||||
//Version: V1.3
|
||||
//All rights reserved pirated reserved.
|
||||
//Copyright(C) Guangzhou wing electronic technology limited company 2014-2024
|
||||
//All rights reserved
|
||||
//********************************************************************************
|
||||
//Modify the description
|
||||
//V1.1 20140803
|
||||
// 1,delay_us, add parameter equal to 0 to determine, if the parameter is equal to 0, then exit.
|
||||
// 2, under the modified ucosii, delay_ms function, join the OSLockNesting judgment, entered after a break, also can be used to delay.
|
||||
//V1.2 20150411
|
||||
// Modify the way OS support to support any OS (not limited to UCOSII and UCOSIII, in theory any OS can support)
|
||||
// Add: delay_osrunning/delay_ostickspersec/delay_osintnesting three macro definitions
|
||||
// Add: delay_osschedlock/delay_osschedunlock/delay_ostimedly three-function
|
||||
//V1.3 20150521
|
||||
// 2 bug when amend UCOSIII support:
|
||||
// delay_tickspersec read: delay_ostickspersec
|
||||
// delay_intnesting read: delay_osintnesting
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void delay_init(u32 SYSCLK);
|
||||
void delay_ms(u16 nms);
|
||||
void delay_us(u32 nus);
|
||||
void delay_os_ms(u32 ms);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
26
USER/Ref/bmu-18s-firmware/SYSTEM/sys.c
Normal file
26
USER/Ref/bmu-18s-firmware/SYSTEM/sys.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "sys.h"
|
||||
|
||||
void WFI_SET(void)
|
||||
{
|
||||
__ASM volatile("wfi");
|
||||
}
|
||||
|
||||
void INTX_DISABLE(void)
|
||||
{
|
||||
__ASM volatile("cpsid i");
|
||||
}
|
||||
|
||||
void INTX_ENABLE(void)
|
||||
{
|
||||
__ASM volatile("cpsie i");
|
||||
}
|
||||
|
||||
void MSR_MSP(u32 addr)
|
||||
{
|
||||
__ASM volatile("msr msp, %0" : : "r" (addr) );
|
||||
}
|
||||
|
||||
void Sys_Soft_Reset(void)
|
||||
{
|
||||
SCB->AIRCR = 0X05FA0000 | (u32)0x04;
|
||||
}
|
||||
96
USER/Ref/bmu-18s-firmware/SYSTEM/sys.h
Normal file
96
USER/Ref/bmu-18s-firmware/SYSTEM/sys.h
Normal file
@@ -0,0 +1,96 @@
|
||||
#ifndef __SYS_H
|
||||
#define __SYS_H
|
||||
#include "stm32f10x.h"
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//This program is for educational purposes only, without the author's permission, may not be used for any other purpose
|
||||
//ALIENTEK STM32F407 Development Board
|
||||
//System clock initialize
|
||||
//Hot Atom@ALIENTEK
|
||||
//Technical Forum:www.openedv.com
|
||||
//Date created:2014/5/2
|
||||
//Version: V1.0
|
||||
//All rights reserved pirated reserved.
|
||||
//Copyright(C) Guangzhou wing electronic technology limited company 2014-2024
|
||||
//All rights reserved
|
||||
//********************************************************************************
|
||||
//Modify the description
|
||||
//No
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
//0,Not supported OS
|
||||
//1,Supported OS
|
||||
#define SYSTEM_SUPPORT_OS 1 //Defines whether the system folders supported OS
|
||||
|
||||
//-Bit operation, 51 similar GPIO functions
|
||||
//Specific ideas, definitive guide to reference <<CM3 >> the fifth chapter (87 pages ~92 pages). M4 and M3, just register the address has changed.
|
||||
//IO operation macro definitions
|
||||
#define BITBAND(addr, bitnum) ((addr & 0xF0000000) + 0x2000000 + ((addr & 0xFFFFF) << 5) + (bitnum << 2))
|
||||
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
|
||||
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
|
||||
|
||||
//IO port address mapping
|
||||
#define GPIOA_ODR_Addr (GPIOA_BASE + 12) // 0x4001080C
|
||||
#define GPIOB_ODR_Addr (GPIOB_BASE + 12) // 0x40010C0C
|
||||
#define GPIOC_ODR_Addr (GPIOC_BASE + 12) // 0x4001100C
|
||||
#define GPIOD_ODR_Addr (GPIOD_BASE + 12) // 0x4001140C
|
||||
#define GPIOE_ODR_Addr (GPIOE_BASE + 12) // 0x4001180C
|
||||
#define GPIOF_ODR_Addr (GPIOF_BASE + 12) // 0x40011A0C
|
||||
#define GPIOG_ODR_Addr (GPIOG_BASE + 12) // 0x40011E0C
|
||||
|
||||
#define GPIOA_IDR_Addr (GPIOA_BASE + 8) // 0x40010808
|
||||
#define GPIOB_IDR_Addr (GPIOB_BASE + 8) // 0x40010C08
|
||||
#define GPIOC_IDR_Addr (GPIOC_BASE + 8) // 0x40011008
|
||||
#define GPIOD_IDR_Addr (GPIOD_BASE + 8) // 0x40011408
|
||||
#define GPIOE_IDR_Addr (GPIOE_BASE + 8) // 0x40011808
|
||||
#define GPIOF_IDR_Addr (GPIOF_BASE + 8) // 0x40011A08
|
||||
#define GPIOG_IDR_Addr (GPIOG_BASE + 8) // 0x40011E08
|
||||
|
||||
//IO operation, only a single IO!
|
||||
//Ensure that the value of n is less than 16!
|
||||
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //Output
|
||||
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //Input
|
||||
|
||||
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //Output
|
||||
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //Input
|
||||
|
||||
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //Output
|
||||
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //Input
|
||||
|
||||
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //Output
|
||||
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //Input
|
||||
|
||||
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //Output
|
||||
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //Input
|
||||
|
||||
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //Output
|
||||
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //Input
|
||||
|
||||
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //Output
|
||||
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //Input
|
||||
|
||||
// Ex_NVIC_Config
|
||||
#define GPIO_A 0
|
||||
#define GPIO_B 1
|
||||
#define GPIO_C 2
|
||||
#define GPIO_D 3
|
||||
#define GPIO_E 4
|
||||
#define GPIO_F 5
|
||||
#define GPIO_G 6
|
||||
#define FTIR 1
|
||||
#define RTIR 2
|
||||
|
||||
//JTAG Remap
|
||||
#define JTAG_SWD_DISABLE 0X02
|
||||
#define SWD_ENABLE 0X01
|
||||
#define JTAG_SWD_ENABLE 0X00
|
||||
|
||||
//Following is a compilation of a function
|
||||
void WFI_SET(void); //WFI instruction is executed
|
||||
void INTX_DISABLE(void);//Disable all interrupts
|
||||
void INTX_ENABLE(void); //Enable all the interrupts
|
||||
void MSR_MSP(u32 addr); //Setting the stack address
|
||||
|
||||
void Sys_Soft_Reset(void);
|
||||
|
||||
#endif
|
||||
199
USER/Ref/bmu-18s-firmware/SYSTEM/usart.c
Normal file
199
USER/Ref/bmu-18s-firmware/SYSTEM/usart.c
Normal file
@@ -0,0 +1,199 @@
|
||||
#include "usart.h"
|
||||
#include "delay.h"
|
||||
#include "sys.h"
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
// If you are using ucos, include the following header file.
|
||||
#if SYSTEM_SUPPORT_OS
|
||||
#include "includes.h" //ucos Use
|
||||
#endif
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// Add the following code, support for printf function and do not need to select
|
||||
// use MicroLIB
|
||||
#if 1
|
||||
#pragma import(__use_no_semihosting)
|
||||
// Standard library required support function
|
||||
struct __FILE {
|
||||
int handle;
|
||||
};
|
||||
|
||||
FILE __stdout;
|
||||
// Define _sys_exit () to avoid using host model
|
||||
void _sys_exit(int x) { x = x; }
|
||||
|
||||
// GCC 환경에서 printf를 위해 구현해야 하는 시스템 호출 함수
|
||||
int _write(int file, char *ptr, int len) {
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
UART1_PutChar((u8)ptr[i]);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
// 기존 fputc도 호환성을 위해 유지
|
||||
int fputc(int ch, FILE *f) {
|
||||
UART1_PutChar((u8)ch);
|
||||
return ch;
|
||||
}
|
||||
#endif
|
||||
|
||||
UART1_Info Uart1;
|
||||
|
||||
// Initializes IO serial port 1
|
||||
// Bound: baud rate
|
||||
void uart1_init(u32 bound) {
|
||||
// GPIO port settings
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
USART_InitTypeDef USART_InitStructure;
|
||||
|
||||
memset(&Uart1, 0x00, sizeof(UART1_Info));
|
||||
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Enables GPIOA clock
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Enable USART1
|
||||
|
||||
// Serial port 1 pins reuse maps
|
||||
GPIO_PinRemapConfig(GPIO_Remap_USART1, DISABLE);
|
||||
|
||||
// USART1 TX port configuration
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // GPIOA9
|
||||
GPIO_InitStructure.GPIO_Mode =
|
||||
GPIO_Mode_AF_PP; // AF Push-pull multiplexed output
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Speed 50MHz
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure); // Initialize PA9
|
||||
|
||||
// USART1 RX port configuration
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // GPIOA10
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // AF Input pull up
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure); // Initialize PA10
|
||||
|
||||
// USART1 Initialization settings
|
||||
USART_InitStructure.USART_BaudRate = bound; // Baud rate setting
|
||||
USART_InitStructure.USART_WordLength =
|
||||
USART_WordLength_8b; // Word is 8-bit data format
|
||||
USART_InitStructure.USART_StopBits = USART_StopBits_1; // One stop bit
|
||||
USART_InitStructure.USART_Parity = USART_Parity_No; // No parity bit
|
||||
USART_InitStructure.USART_HardwareFlowControl =
|
||||
USART_HardwareFlowControl_None; // No hardware flow control
|
||||
USART_InitStructure.USART_Mode =
|
||||
USART_Mode_Rx | USART_Mode_Tx; // Send and receive mode
|
||||
USART_Init(USART1, &USART_InitStructure); // Initialize serial port 1
|
||||
|
||||
// NVIC 설정 추가 (인터럽트 활성화)
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; // 우선순위 설정
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
|
||||
USART_Cmd(USART1, ENABLE); // Enabling serial port 1
|
||||
|
||||
USART_ClearFlag(USART1, USART_FLAG_TC);
|
||||
|
||||
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Open break
|
||||
}
|
||||
|
||||
void USART1_IRQHandler(void) // Serial port 1 interrupt service routine
|
||||
{
|
||||
USART_TypeDef *usart;
|
||||
CPU_INT08U rx_data;
|
||||
|
||||
#if SYSTEM_SUPPORT_OS // Use operating system UCOS
|
||||
OSIntEnter();
|
||||
#endif
|
||||
usart = USART1;
|
||||
|
||||
if (USART_GetITStatus(usart, USART_IT_RXNE) != RESET) {
|
||||
rx_data = USART_ReceiveData(usart) &
|
||||
0xFF; /* Read one byte from the receive data register. */
|
||||
|
||||
Uart1.RxBuf[Uart1.RxInPos++] = rx_data;
|
||||
Uart1.RxInPos %= UART1_RX_BUFFER_SIZE;
|
||||
|
||||
if (Uart1.RxInPos == Uart1.RxOutPos) {
|
||||
Uart1.RxOutPos++;
|
||||
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
USART_ClearITPendingBit(
|
||||
usart, USART_IT_RXNE); /* Clear the USART1 receive interrupt. */
|
||||
}
|
||||
|
||||
if (USART_GetITStatus(usart, USART_IT_TXE) != RESET) {
|
||||
if (Uart1.TxOutPos != Uart1.TxInPos) {
|
||||
/* Write one byte to the transmit data register */
|
||||
USART_SendData(usart, Uart1.TxBuf[Uart1.TxOutPos++]);
|
||||
Uart1.TxOutPos %= UART1_TX_BUFFER_SIZE;
|
||||
} else {
|
||||
/* Disable the USART1 Transmit interrupt */
|
||||
USART_ITConfig(usart, USART_IT_TXE, DISABLE);
|
||||
}
|
||||
USART_ClearITPendingBit(usart, USART_IT_TXE);
|
||||
}
|
||||
#if SYSTEM_SUPPORT_OS
|
||||
OSIntExit(); // Exit interrupt
|
||||
#endif
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* Function Name : void UART1_PutChar(char data)
|
||||
* Description : UART1 1
|
||||
* Parameters : ƽŰڵ
|
||||
* Return : None
|
||||
*******************************************************************************/
|
||||
void UART1_PutChar(const u8 ascii) {
|
||||
CPU_SR_ALLOC();
|
||||
|
||||
// 버퍼가 가득 찼으면 빌 때까지 대기
|
||||
while (((Uart1.TxInPos + 1) % UART1_TX_BUFFER_SIZE) ==
|
||||
(Uart1.TxOutPos % UART1_TX_BUFFER_SIZE)) {
|
||||
// 인터럽트가 꺼져있다면 강제로 켜서 전송 유도
|
||||
// if ((USART1->CR1 & USART_CR1_TXEIE) == 0)
|
||||
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
|
||||
}
|
||||
|
||||
CPU_CRITICAL_ENTER();
|
||||
|
||||
Uart1.TxBuf[Uart1.TxInPos++] = ascii;
|
||||
Uart1.TxInPos %= UART1_TX_BUFFER_SIZE;
|
||||
|
||||
// TXE 인터럽트 활성화하여 전송 시작
|
||||
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
|
||||
|
||||
CPU_CRITICAL_EXIT();
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* Function Name : u8 UART1_GetChar(char *data)
|
||||
* Description : UART1 Źۿ Ÿ(1byte)
|
||||
* Parameters : none
|
||||
* Return : Ÿ (0: Ź۰ , 1: Źۿ Ÿ )
|
||||
*******************************************************************************/
|
||||
ErrorStatus UART1_GetChar(u8 *data) {
|
||||
if (Uart1.RxInPos != Uart1.RxOutPos) {
|
||||
data[0] = Uart1.RxBuf[Uart1.RxOutPos];
|
||||
Uart1.RxOutPos++;
|
||||
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
|
||||
return SUCCESS;
|
||||
} else
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* Function Name : u8 UART1_GetChar(char *data)
|
||||
* Description : UART1 Źۿ Ÿ(1byte)
|
||||
* Parameters : none
|
||||
* Return : Ÿ (0: Ź۰ , 1: Ź Ÿ )
|
||||
*******************************************************************************/
|
||||
u8 UART1_GC(void) {
|
||||
u8 result = 0;
|
||||
|
||||
if (Uart1.RxInPos != Uart1.RxOutPos) {
|
||||
result = Uart1.RxBuf[Uart1.RxOutPos];
|
||||
Uart1.RxOutPos++;
|
||||
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
51
USER/Ref/bmu-18s-firmware/SYSTEM/usart.h
Normal file
51
USER/Ref/bmu-18s-firmware/SYSTEM/usart.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef __USART_H
|
||||
#define __USART_H
|
||||
#include "stdio.h"
|
||||
#include "stm32f10x_conf.h"
|
||||
#include "sys.h"
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
// This program is for educational purposes only, without the author's
|
||||
// permission, may not be used for any other purpose ALIENTEK STM32F4 Explorer
|
||||
// Board Initialize serial port 1 Hot Atomic @ALIENTEK Forum: www.openedv.com
|
||||
// Date modified: 2014/6/10
|
||||
// Version: V1.4
|
||||
// All rights reserved pirated reserved.
|
||||
// Copyright(C) Guangzhou wing electronic technology limited company 2009-2019
|
||||
// All rights reserved
|
||||
//********************************************************************************
|
||||
// V1.3 changes description
|
||||
// Supports serial port baud rate setting at different frequencies.
|
||||
// Added support for printf
|
||||
// Serial port receive command features have been added.
|
||||
// Fixed printf first character missing bug
|
||||
// V1.4 amended description
|
||||
// 1, modify the initialize serial IO bug
|
||||
// 2, modify the USART_RX_STA makes serial port maximum bytes received 2 of 14
|
||||
// 3, USART_REC_LEN, defines the serial port the maximum allowable number of
|
||||
// bytes received (not more than 2 of 14) 4, the modified EN_USART1_RX method
|
||||
// V1.5 modify the description
|
||||
// 1, added support for UCOSII
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
#define DEBUG_PORT USART1
|
||||
|
||||
/* Defines -------------------------------------------------------------------*/
|
||||
#define UART1_TX_BUFFER_SIZE 512
|
||||
#define UART1_RX_BUFFER_SIZE 32
|
||||
|
||||
/* Type declarations ---------------------------------------------------------*/
|
||||
typedef struct {
|
||||
u16 RxInPos, RxOutPos;
|
||||
u16 TxInPos, TxOutPos;
|
||||
u8 RxBuf[UART1_RX_BUFFER_SIZE];
|
||||
u8 TxBuf[UART1_TX_BUFFER_SIZE];
|
||||
} UART1_Info;
|
||||
|
||||
void uart1_init(u32 bound);
|
||||
ErrorStatus UART1_GetChar(u8 *data);
|
||||
void UART1_PutChar(const u8 ascii);
|
||||
void UART1_PutStr(u8 *str, u16 len);
|
||||
void UART1_printf(const char *fmt, ...);
|
||||
|
||||
u8 UART1_GC(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user