#include "usart.h"
#include "delay.h"
#include "led.h"
#include "timer.h"
int main(void)
{
Stm32_Clock_Init(10);
delay_init(120);
uart_init(72,9600);
TIM3_Int_Init(555,7199);
LED_Init();
while(1)
{
LEDA0=0;
LEDA1=0;
LEDA2=0;
LEDA3=0;
LEDA4=0;
LEDA5=0;
LEDA6=0;
LEDA7=0;
LEDA8=0;
LEDA9=0;
LEDA10=0;
LEDA11=0;
LEDA12=0;
LEDA13=0;
LEDA14=0;
LEDA15=0;
LEDB0=0;
LEDB1=0;
LEDB2=0;
LEDB3=0;
LEDB4=0;
LEDB5=0;
LEDB6=0;
LEDB7=0;
LEDB8=0;
LEDB9=0;
LEDB10=0;
LEDB11=0;
LEDB12=0;
LEDB13=0;
LEDB14=0;
LEDB15=0;
LEDC0=0;
LEDC1=0;
LEDC2=0;
LEDC3=0;
LEDC4=0;
LEDC5=0;
LEDC6=0;
LEDC7=0;
LEDC8=0;
LEDC9=0;
LEDC10=0;
LEDC11=0;
LEDC12=0;
LEDC14=0;
LEDC15=0;
LEDD0=0;
LEDD1=0;
LEDD2=0;
LEDD3=0;
LEDD4=0;
LEDD5=0;
LEDD6=0;
LEDD7=0;
LEDD8=0;
LEDD9=0;
LEDD10=0;
LEDD11=0;
LEDD12=0;
LEDD13=0;
LEDD14=0;
LEDD15=0;
delay_ms(1000);
LEDA1=1;
LEDA2=1;
LEDA3=1;
LEDA4=1;
LEDA5=1;
LEDA6=1;
LEDA7=1;
LEDA8=1;
LEDA9=1;
LEDA10=1;
LEDA11=1;
LEDA12=1;
LEDA13=1;
LEDA14=1;
LEDA15=1;
LEDB1=1;
LEDB2=1;
LEDB3=1;
LEDB4=1;
LEDB5=1;
LEDB6=1;
LEDB7=1;
LEDB8=1;
LEDB9=1;
LEDB10=1;
LEDB11=1;
LEDB12=1;
LEDB13=1;
LEDB14=1;
LEDB15=1;
LEDC1=1;
LEDC2=1;
LEDC3=1;
LEDC4=1;
LEDC5=1;
LEDC6=1;
LEDC7=1;
LEDC8=1;
LEDC9=1;
LEDC10=1;
LEDC11=1;
LEDC12=1;
LEDC14=1;
LEDC15=1;
LEDD1=1;
LEDD2=1;
LEDD3=1;
LEDD4=1;
LEDD5=1;
LEDD6=1;
LEDD7=1;
LEDD8=1;
LEDD9=1;
LEDD10=1;
LEDD11=1;
LEDD12=1;
LEDD13=1;
LEDD14=1;
LEDD15=1;
delay_ms(1000);
}
}
#include "delay.h"
#if SYSTEM_SUPPORT_OS
#include "includes.h"
#endif
static u8 fac_us=0;
static u16 fac_ms=0;
#if SYSTEM_SUPPORT_OS
#ifdef OS_CRITICAL_METHOD
#define delay_osrunning OSRunning
#define delay_ostickspersec OS_TICKS_PER_SEC
#define delay_osintnesting OSIntNesting
#endif
#ifdef CPU_CFG_CRITICAL_METHOD
#define delay_osrunning OSRunning
#define delay_ostickspersec OSCfg_TickRate_Hz
#define delay_osintnesting OSIntNestingCtr
#endif
void delay_osschedlock(void)
{
#ifdef CPU_CFG_CRITICAL_METHOD
OS_ERR err;
OSSchedLock(&err);
#else
OSSchedLock();
#endif
}
void delay_osschedunlock(void)
{
#ifdef CPU_CFG_CRITICAL_METHOD
OS_ERR err;
OSSchedUnlock(&err);
#else
OSSchedUnlock();
#endif
}
void delay_ostimedly(u32 ticks)
{
#ifdef CPU_CFG_CRITICAL_METHOD
OS_ERR err;
OSTimeDly(ticks,OS_OPT_TIME_PERIODIC,&err);
#else
OSTimeDly(ticks);
#endif
}
void SysTick_Handler(void)
{
if(delay_osrunning==1)
{
OSIntEnter();
OSTimeTick();
OSIntExit();
}
}
#endif
void delay_init(u8 SYSCLK)
{
#if SYSTEM_SUPPORT_OS
u32 reload;
#endif
SysTick->CTRL&=~(1<<2);
fac_us=SYSCLK/8;
#if SYSTEM_SUPPORT_OS
reload=SYSCLK/8;
reload*=1000000/delay_ostickspersec;
fac_ms=1000/delay_ostickspersec;
SysTick->CTRL|=1<<1;
SysTick->LOAD=reload;
SysTick->CTRL|=1<<0;
#else
fac_ms=(u16)fac_us*1000;
#endif
}
#if SYSTEM_SUPPORT_OS
void delay_us(u32 nus)
{
u32 ticks;
u32 told,tnow,tcnt=0;
u32 reload=SysTick->LOAD;
ticks=nus*fac_us;
delay_osschedlock();
told=SysTick->VAL;
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow<told)tcnt+=told-tnow;
else tcnt+=reload-tnow+told;
told=tnow;
if(tcnt>=ticks)break;
}
};
delay_osschedunlock();
}
void delay_ms(u16 nms)
{
if(delay_osrunning&&delay_osintnesting==0)
{
if(nms>=fac_ms)
{
delay_ostimedly(nms/fac_ms);
}
nms%=fac_ms;
}
delay_us((u32)(nms*1000));
}
#else
void delay_us(u32 nus)
{
u32 temp;
SysTick->LOAD=nus*fac_us;
SysTick->VAL=0x00;
SysTick->CTRL=0x01 ;
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16)));
SysTick->CTRL=0x00;
SysTick->VAL =0X00;
}
void delay_ms(u16 nms)
{
u32 temp;
SysTick->LOAD=(u32)nms*fac_ms;
SysTick->VAL =0x00;
SysTick->CTRL=0x01 ;
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16)));
SysTick->CTRL=0x00;
SysTick->VAL =0X00;
}
#endif
#include "sys.h"
#include "led.h"
void LED_Init(void)
{
RCC->APB2ENR|=1<<2;
RCC->APB2ENR|=1<<3;
RCC->APB2ENR|=1<<4;
RCC->APB2ENR|=1<<5;
GPIOA->CRH&=0X00000000;
GPIOA->CRH|=0x33333333;
GPIOA->CRL&=0X00000000;
GPIOA->CRL|=0x33333333;
GPIOA->ODR|=1<<0;
GPIOA->ODR|=1<<1;
GPIOA->ODR|=1<<2;
GPIOA->ODR|=1<<3;
GPIOA->ODR|=1<<4;
GPIOA->ODR|=1<<5;
GPIOA->ODR|=1<<6;
GPIOA->ODR|=1<<7;
GPIOA->ODR|=1<<8;
GPIOA->ODR|=1<<9;
GPIOA->ODR|=1<<10;
GPIOA->ODR|=1<<11;
GPIOA->ODR|=1<<12;
GPIOA->ODR|=1<<13;
GPIOA->ODR|=1<<14;
GPIOA->ODR|=1<<15;
GPIOB->CRH&=0X00000000;
GPIOB->CRH|=0x33333333;
GPIOB->CRL&=0X00000000;
GPIOB->CRL|=0x33333333;
GPIOB->ODR|=1<<0;
GPIOB->ODR|=1<<1;
GPIOB->ODR|=1<<2;
GPIOB->ODR|=1<<3;
GPIOB->ODR|=1<<4;
GPIOB->ODR|=1<<5;
GPIOB->ODR|=1<<6;
GPIOB->ODR|=1<<7;
GPIOB->ODR|=1<<8;
GPIOB->ODR|=1<<9;
GPIOB->ODR|=1<<10;
GPIOB->ODR|=1<<11;
GPIOB->ODR|=1<<12;
GPIOB->ODR|=1<<13;
GPIOB->ODR|=1<<14;
GPIOB->ODR|=1<<15;
GPIOC->CRH&=0X00000000;
GPIOC->CRH|=0x33333333;
GPIOC->CRL&=0X00000000;
GPIOC->CRL|=0x33333333;
GPIOC->ODR|=1<<0;
GPIOC->ODR|=1<<1;
GPIOC->ODR|=1<<2;
GPIOC->ODR|=1<<3;
GPIOC->ODR|=1<<4;
GPIOC->ODR|=1<<5;
GPIOC->ODR|=1<<6;
GPIOC->ODR|=1<<7;
GPIOC->ODR|=1<<8;
GPIOC->ODR|=1<<9;
GPIOC->ODR|=1<<10;
GPIOC->ODR|=1<<11;
GPIOC->ODR|=1<<12;
GPIOC->ODR|=1<<13;
GPIOC->ODR|=1<<14;
GPIOC->ODR|=1<<15;
GPIOD->CRH&=0X00000000;
GPIOD->CRH|=0x33333333;
GPIOD->CRL&=0X00000000;
GPIOD->CRL|=0x33333333;
GPIOD->ODR|=1<<0;
GPIOD->ODR|=1<<1;
GPIOD->ODR|=1<<2;
GPIOD->ODR|=1<<3;
GPIOD->ODR|=1<<4;
GPIOD->ODR|=1<<5;
GPIOD->ODR|=1<<6;
GPIOD->ODR|=1<<7;
GPIOD->ODR|=1<<8;
GPIOD->ODR|=1<<9;
GPIOD->ODR|=1<<10;
GPIOD->ODR|=1<<11;
GPIOD->ODR|=1<<12;
GPIOD->ODR|=1<<13;
GPIOD->ODR|=1<<14;
GPIOD->ODR|=1<<15;
}
#include "sys.h"
void MY_NVIC_SetVectorTable(u32 NVIC_VectTab, u32 Offset)
{
SCB->VTOR = NVIC_VectTab|(Offset & (u32)0x1FFFFF80);
}
void MY_NVIC_PriorityGroupConfig(u8 NVIC_Group)
{
u32 temp,temp1;
temp1=(~NVIC_Group)&0x07;
temp1<<=8;
temp=SCB->AIRCR;
temp&=0X0000F8FF;
temp|=0X05FA0000;
temp|=temp1;
SCB->AIRCR=temp;
}
void MY_NVIC_Init(u8 NVIC_PreemptionPriority,u8 NVIC_SubPriority,u8 NVIC_Channel,u8 NVIC_Group)
{
u32 temp;
MY_NVIC_PriorityGroupConfig(NVIC_Group);
temp=NVIC_PreemptionPriority<<(4-NVIC_Group);
temp|=NVIC_SubPriority&(0x0f>>NVIC_Group);
temp&=0xf;
NVIC->ISER[NVIC_Channel/32]|=(1<<NVIC_Channel%32);
NVIC->IP[NVIC_Channel]|=temp<<4;
}
void Ex_NVIC_Config(u8 GPIOx,u8 BITx,u8 TRIM)
{
u8 EXTADDR;
u8 EXTOFFSET;
EXTADDR=BITx/4;
EXTOFFSET=(BITx%4)*4;
RCC->APB2ENR|=0x01;
AFIO->EXTICR[EXTADDR]&=~(0x000F<<EXTOFFSET);
AFIO->EXTICR[EXTADDR]|=GPIOx<<EXTOFFSET;
EXTI->IMR|=1<<BITx;
if(TRIM&0x01)EXTI->FTSR|=1<<BITx;
if(TRIM&0x02)EXTI->RTSR|=1<<BITx;
}
void MYRCC_DeInit(void)
{
RCC->APB1RSTR = 0x00000000;
RCC->APB2RSTR = 0x00000000;
RCC->AHBENR = 0x00000014;
RCC->APB2ENR = 0x00000000;
RCC->APB1ENR = 0x00000000;
RCC->CR |= 0x00000001;
RCC->CFGR &= 0xF8FF0000;
RCC->CR &= 0xFEF6FFFF;
RCC->CR &= 0xFFFBFFFF;
RCC->CFGR &= 0xFF80FFFF;
RCC->CIR = 0x00000000;
#ifdef VECT_TAB_RAM
MY_NVIC_SetVectorTable(0x20000000, 0x0);
#else
MY_NVIC_SetVectorTable(0x08000000,0x0);
#endif
}
void WFI_SET(void)
{
__ASM volatile("wfi");
}
void INTX_DISABLE(void)
{
__ASM volatile("cpsid i");
}
void INTX_ENABLE(void)
{
__ASM volatile("cpsie i");
}
__asm void MSR_MSP(u32 addr)
{
MSR MSP, r0
BX r14
}
void Sys_Standby(void)
{
SCB->SCR|=1<<2;
RCC->APB1ENR|=1<<28;
PWR->CSR|=1<<8;
PWR->CR|=1<<2;
PWR->CR|=1<<1;
WFI_SET();
}
void Sys_Soft_Reset(void)
{
SCB->AIRCR =0X05FA0000|(u32)0x04;
}
void JTAG_Set(u8 mode)
{
u32 temp;
temp=mode;
temp<<=25;
RCC->APB2ENR|=1<<0;
AFIO->MAPR&=0XF8FFFFFF;
AFIO->MAPR|=temp;
}
void Stm32_Clock_Init(u8 PLL)
{
unsigned char temp=0;
MYRCC_DeInit();
RCC->CR|=0x00010000;
while(!(RCC->CR>>17));
RCC->CFGR=0X00000400;
PLL-=2;
RCC->CFGR|=PLL<<18;
RCC->CFGR|=1<<16;
FLASH->ACR|=0x32;
RCC->CR|=0x01000000;
while(!(RCC->CR>>25));
RCC->CFGR|=0x00000002;
while(temp!=0x02)
{
temp=RCC->CFGR>>2;
temp&=0x03;
}
}
#include "usart.h"
#if SYSTEM_SUPPORT_OS
#include "includes.h"
#endif
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX
u8 USART_RX_BUF[USART_REC_LEN];
u16 USART_RX_STA=0;
void USART1_IRQHandler(void)
{
u8 res;
#if SYSTEM_SUPPORT_OS
OSIntEnter();
#endif
if(USART1->SR&(1<<5))
{
res=USART1->DR;
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000)
{
if(res!=0x0a)USART_RX_STA=0;
else USART_RX_STA|=0x8000;
}else
{
if(res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=res;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;
}
}
}
}
#if SYSTEM_SUPPORT_OS
OSIntExit();
#endif
}
#endif
void uart_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);
mantissa=temp;
fraction=(temp-mantissa)*16;
mantissa<<=4;
mantissa+=fraction;
RCC->APB2ENR|=1<<2;
RCC->APB2ENR|=1<<14;
GPIOA->CRH&=0XFFFFF00F;
GPIOA->CRH|=0X000008B0;
RCC->APB2RSTR|=1<<14;
RCC->APB2RSTR&=~(1<<14);
USART1->BRR=mantissa;
USART1->CR1|=0X200C;
#if EN_USART1_RX
USART1->CR1|=1<<5;
MY_NVIC_Init(3,3,USART1_IRQn,2);
#endif
}
|