/*

                         \\\|///
                       \\  - -  //
                        (  @ @  )
+---------------------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"

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
  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                                              |
\*----------------------------------------------------------*/

/*
*********************************************************************************************************
*                                         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                */
}
