📄 app.c
字号:
/*
\\\|///
\\ - - //
( @ @ )
+---------------------oOOo-(_)-oOOo-------------------------+
| 智林STM32开发板试验程序 |
| uC/OS实验 |
| 刘笑然 by Xiaoran Liu |
| 2008.4.16 |
| |
| 智林测控技术研究所 ZERO research group |
| www.the0.net |
| Oooo |
+-----------------------oooO--( )-------------------------+
( ) ) /
\ ( (_/
\_)
*/
/*----------------------------------------------------------*\
| 引入相关芯片的头文件 |
\*----------------------------------------------------------*/
#include <ucos_ii.h>
//#include <app_cfg.h>
#include <stdio.h>
#include <stm32f10x_lib.h> // STM32F10x Library Definitions
//#include "STM32_Init.h" // STM32 Initialization
//#include "TFT018.h"
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
bool PreemptionOccured = FALSE;
u8 PreemptionPriorityValue = 0;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
void RCC_Configuration(void);
void SysTick_Config(void)
int ledLight = 0;
/*----------------------------------------------------------*\
| HARDWARE DEFINE |
\*----------------------------------------------------------*/
#define LED ( 1 << 5 ) // PB5: LED D2
/*----------------------------------------------------------*\
| Timer1 Update Interrupt Handler |
\*----------------------------------------------------------*/
void TIM1_UP_IRQHandler (void) {
if ((TIM1->SR & 0x0001) != 0) { // check interrupt source
ledLight = ~ledLight;
if( ledLight )
GPIOB->ODR &= ~LED; // switch on LED
else
GPIOB->ODR |= LED; // switch off LED
TIM1->SR &= ~(1<<0); // clear UIF flag
}
} // end TIM1_UP_IRQHandler
#define TASK_STK_SIZE 128 /* Size of each task's stacks (# of WORDs) */
OS_STK TaskStartStk[TASK_STK_SIZE];
OS_STK Task1Stk[TASK_STK_SIZE];
OS_STK Task2Stk[TASK_STK_SIZE];
void Task1 (void *pdata) {
pdata = pdata; /* Prevent compiler warning */
for(;;) {
GPIOB->ODR |= LED; // switch on LED
OSTimeDly(5);
GPIOB->ODR &= ~LED; // switch off LED
OSTimeDly(5);
}
}
void Task2Display (void *pdata) {
pdata = pdata; /* Prevent compiler warning */
for(;;) {
char s[30];
sprintf(s,"%d",(unsigned int)OSCtxSwCtr);
// LCD_PutString(36,50,s,Magenta,Blue);
}
}
void TaskStart (void *pdata)
{
pdata = pdata; /* Prevent compiler warning */
//BSP_init();
OSTaskCreate(Task1, (void *)0, &Task1Stk[TASK_STK_SIZE - 1], 1);
OSTaskCreate(Task2Display, (void *)0, &Task2Stk[TASK_STK_SIZE - 1], 2);
OSTaskDel(0);
}
/*----------------------------------------------------------*\
| MIAN ENTRY |
\*----------------------------------------------------------*/
int main (void) {
//stm32_Init (); // STM32 setup
/* Configure the system clocks */
RCC_Configuration();
/* Configure GPIOs */
GPIO_Configuration();
/* Configure TIMs */
EXTI_Configuration();
/* Configure NVIC */
NVIC_Configuration();
SysTick_Config() ;
/*LCD_Init();
LCD_Clear_Screen(Blue);
Font = 0;
LCD_PutString(30,0,"STM32F 开发板",Cyan,Blue);
LCD_PutString(40,16,"uC/OS实验",Red,Yellow);
Font = 1;
LCD_PutString(38,32,"Version 1.0",Green,Blue);
Font = 0;
LCD_PutString(10,94,"智林测控技术研究所",Yellow,Blue);
LCD_PutString(36,111,"www.the0.net",Magenta,Blue);*/
OSInit();
OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0);
OSStart(); /* Start multitasking */
for(;;) {
}
}
/*----------------------------------------------------------*\
| END OF FILE |
\*----------------------------------------------------------*/
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 6 = 48 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable GPIO_LED clock */
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE);
/* Enable TIM2, TIM3 and TIM4 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOF|RCC_APB2Periph_AFIO, ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the used GPIO pins.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure GPIO_LED pin 6, GPIO_LED pin 7, GPIO_LED pin 8 as output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Configure GPIOA_0 as IN_FLOATING*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure GPIOB_9 as IN_FLOATING*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : EXTI_Configuration
* Description : Configures the used Timers.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI_Configuration(void)
{
/*connect EXTI_0 with PA0*/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd =ENABLE;
EXTI_Init(&EXTI_InitStructure);
/*connect EXTI_9 with PB9*/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9);
/*configurate the EXTI_0 as falling_edge */
EXTI_InitStructure.EXTI_Line = EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd =ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void Delay( vu32 nCount)
{
for (;nCount !=0; nCount --);
}
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the EXTI0 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the EXTI9_5_IRQChannel Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* configurate SysTick InterruptPriority */
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick,55,0);
}
/*******************************************************************************
* Function Name : SysTick_Config
* Description : Configure a SysTick Base time to 10 ms.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SysTick_Config(void)
{
/* Configure HCLK clock as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
/* SysTick interrupt each 100 Hz with HCLK equal to 72MHz */
SysTick_SetReload(7199999);
/* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
}
/*
*********************************************************************************************************
* TIMER IRQ HANDLER
*
* Description : This function handles the timer interrupt that is used to generate TICKs for uC/OS-II.
*
* Arguments : none
*
* Note(s) : 1) The timer is 'reloaded' with the count at compare + the time for the next interrupt.
* Since we are using 'unsigned' integer math, overflows are irrelevant.
*********************************************************************************************************
*/
void SysTick_Handler (void)
{
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR */
OSIntNesting++;
OS_EXIT_CRITICAL();
OSTimeTick(); /* Call uC/OS-II's OSTimeTick() */
OSIntExit(); /* Tell uC/OS-II that we are leaving the ISR */
}
/*******************************************************************************
* Function Name : EXTI0_IRQHandler
* Description : This function handles External interrupt Line 0 request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI0_IRQHandler(void)
{
/* */
NVIC_SetSystemHandlerPendingBit(SystemHandler_SysTick);
EXTI_ClearITPendingBit(EXTI_Line0);
}
void EXTI9_5_IRQHandler(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
if(EXTI_GetITStatus(EXTI_Line9)!=RESET)
{
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=PreemptionPriorityValue;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick,!PreemptionPriorityValue,0);
EXTI_ClearITPendingBit(EXTI_Line9);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -