⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsp.c

📁 uCOS-II example for MC9S12DPxxx
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*********************************************************************************************************
*                               Wytec Dragon12 Board Support Package 
*
*                                       Freescale MC9S12DP256
*
* File       : bsp.c
* By         : Eric Shufro
*
* Description: Provides low level hardware functions for use with portable application code
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                        INCLUDES
*********************************************************************************************************
*/

#include <includes.h>


/*
*********************************************************************************************************
*                                       REGISTERS 
*
* Registers that are not included in the processor header file may be defind here
*********************************************************************************************************
*/

#ifndef  BDMSTS
#define  BDMSTS                (*((INT8U *)0xFF01))                     /* Cast a pointer to the BDM Status register                */
#define  BDMSTS_CLKSW_MASK                     (4)						/* Bit mask for the CLKSW bit in the BDMSTS register        */
#endif


/*
*********************************************************************************************************
*                                 CHARACTER LCD CONSTANT DATA
*
* Notes       : 1) RW is permanently pulled LOW in hardware. Under normal circumstances, 
*                  it too should be declared below and referenced in the LCD Write functions
*                  below.
*********************************************************************************************************
*/

#define  LCD_LOWEST_DATA_BIT                 (INT8U)(2)   		        /* Bit two is the lowest order data bit                     */
#define  LCD_BIT_RS                          (INT8U)(1 <<  0)		    /* LCD Screen bit RS maps to PORTK, bit 0                   */   
#define  LCD_BIT_E                           (INT8U)(1 <<  1)           /* LCD Screen bit EN maps to PORTK, bit 1                   */   
#define  LCD_BIT_DATA0                       (INT8U)(1 <<  2)           /* LCD Screen bit D4 maps to PORTK, bit 2                   */   
#define  LCD_BIT_DATA1                       (INT8U)(1 <<  3)           /* LCD Screen bit D5 maps to PORTK, bit 3                   */   
#define  LCD_BIT_DATA2                       (INT8U)(1 <<  4)			/* LCD Screen bit D6 maps to PORTK, bit 4                   */   
#define  LCD_BIT_DATA3                       (INT8U)(1 <<  5)    	    /* LCD Screen bit D7 maps to PORTK, bit 5                   */


/*
*********************************************************************************************************
*                                         GLOBALS
*********************************************************************************************************
*/

static  INT16U  OSTickCnts;			                                    /* Holds the number of timer increments for the OS Ticker   */  


/*
*********************************************************************************************************
*                                        PROTOTYPES
*********************************************************************************************************
*/

static  void     PLL_Init(void);                                        /* Initialize the CPU's PLL                                 */
static  void     OSTickISR_Init(void);                                  /* Initialize the OS Ticker                                 */
static  void     LED_Init(void);                                        /* Initialize the LED hardware                              */
static  void     Sw_Init(void);                                         /* Initialize Switch hardware                               */
static  void     BSP_SetECT_Prescaler(INT8U prescaler);                 /* Initialize the ECT Prescaler                             */


/*
*********************************************************************************************************
*                                        BSP_Init()
*
* Description: Initialize BSP, called from app.c instead of calling all of the internal bsp init functions
*              seperately, they are grouped together here.
*********************************************************************************************************
*/

void BSP_Init (void)
{
#if PLL_EN > 0
    PLL_Init();                                                         /* Initialize the PLL. This must come before CalcPrescaler  */
    BSP_SetECT_Prescaler(4);                                            /* When using the PLL, an ECT prescaler > 4 MUST be set in  */    
#endif                                                                  /* order to prevent OSTickCnts overflow during Tick Init    */ 
    
    OSTickISR_Init();
    LED_Init();   
}


/*
*********************************************************************************************************
*                                        PLL_Init()
*
* Notes: Will HANG the application if PLL fails to initialize!
*********************************************************************************************************
*/

static void PLL_Init (void)
{  
    BDMSTS |=   BDMSTS_CLKSW_MASK;                                      /* Avoid MC9S12DP256B BDM Looses Sync errata when PLL on    */
                                                                        /* This errata can be avoided by not using a const clk src  */
    
    CLKSEL  = 0x00;                                                     /* Meaning for CLKSEL:
                                                                            Bit 7: PLLSEL = 0 Use OSCCLK until ready to switch to PLLCLK
                                                                            Bit 6: PSTP   = 0 Do not need to go to Pseudo-Stop Mode
                                                                            Bit 5: SYSWAI = 0 In wait mode system clocks stop.
                                                                            Bit 4: ROAWAI = 0 Do not reduce oscillator amplitude in wait mode.
                                                                            Bit 3: PLLWAI = 0 Do not turn off PLL in wait mode
                                                                            Bit 2: CWAI	= 0 Do not stop the core during wait mode
                                                                            Bit 1: RTIWAI = 0 Do not stop the RTI in wait mode
                                                                            Bit 0: COPWAI = 0 Do not stop the COP in wait mode
                                                                        */  

 
    SYNR    = PLL_CLK_MUL;                                              /* Set the desired clock multiplier                         */
    REFDV   = PLL_CLK_DIV;                                              /* Set the desired clock divider                            */

 
    PLLCTL  = 0xC0;                                                     /* Meaning for PLLCTL:
                                                                            Bit 7: CME   = 1; Clock monitor enable
                                                                            Bit 6: PLLON = 1; PLL On bit
                                                                            Bit 5: AUTO  = 0; No automatic control of bandwidth, manual through ACQ
                                                                            Bit 4: ACQ   = 1; 1 for high bandwidth filter (acquisition); 0 for low (tracking)
                                                                            Bit 3:            (Not Used by MC9S12C32)
                                                                            Bit 2: PRE   = 0; RTI stops during Pseudo Stop Mode
                                                                            Bit 1: PCE   = 0; COP diabled during Pseudo STOP mode
                                                                            Bit 0: SCME  = 1; Crystal Clock Failure -> Use Self Clock mode.                                                  
                                                                        */
  
    while((CRGFLG & 0x08) == 0) { 	                                    /* Wait for PLLCLK to stabilize.                            */
        ;				                                                /* If the PLL never stabilizes, this will hang forever      */
    }  
  
    CLKSEL_PLLSEL = 1;                                                  /* Switch to PLL clock                                      */
}


/*
*********************************************************************************************************
*                                        BSP_CPU_ClkFreq()
*
* Description : Returns the CPU operating frequency. (This is NOT the CPU BUS frequency)
*               However, the bus frequency is (clk_frq / 2)
*********************************************************************************************************
*/

INT32U  BSP_CPU_ClkFreq (void)
{
    INT32U clk_frq;
    
    
    if ((CLKSEL & CLKSEL_PLLSEL_MASK) == CLKSEL_PLLSEL_MASK) {
        clk_frq = ((OSCFREQ * 2) * (PLL_CLK_MUL + 1) / (PLL_CLK_DIV + 1));
    } else {
        clk_frq =  (OSCFREQ);
    }
        
    return clk_frq;        
}


/*
*********************************************************************************************************
*                             LED Init (Modify to init appropriate LED Port)
*
* Description : Initialize the data direction registers for any ports that have LEDs attached.
*********************************************************************************************************
*/

void LED_Init (void)
{
    DDRB |=  0xFF;                                                      /* Set the LED pins to output mode                          */
    DDRJ |=  (1 << 1);                                                  /* PTJ Pin1 Controls the LED selection, set for output      */
    PTJ  &= ~(1 << 1);                                                  /* Clear PTJ Pin1 to enable the onboard LED's               */
}


/*
*********************************************************************************************************
*                                        LED Toggle (Using PORTB)
*
* Description : Toggle an LED Pin
*********************************************************************************************************
*/

void LED_Toggle (INT8U led)
{
    if (led <= 7) {
        DDRJ |=  (1 << 1);                                              /* PTJ Pin1 Controls the LED selection, set for output      */
        PTJ  &= ~(1 << 1);                                              /* Clear PTJ Pin1 to enable the onboard LED's               */    
        PORTB ^= (1 << led);					      
    }
}


/*
*********************************************************************************************************
*                                        LED Off (Using PORTB)
*
* Description : Turn off an LED
*********************************************************************************************************
*/

void  LED_Off (INT8U led)
{
    if (led <= 7) {
        DDRJ |=  (1 << 1);                                              /* PTJ Pin1 Controls the LED selection, set for output      */
        PTJ  &= ~(1 << 1);                                              /* Clear PTJ Pin1 to enable the onboard LED's               */
        PORTB &= ~(1 << led);
    }
}


/*
*********************************************************************************************************
*                                        LED On (Using PORTB)
*
* Description : Turn On an LED
*********************************************************************************************************
*/

void  LED_On (INT8U led)
{
    if (led <= 7) {
        DDRJ |=  (1 << 1);                                              /* PTJ Pin1 Controls the LED selection, set for output      */
        PTJ  &= ~(1 << 1);                                              /* Clear PTJ Pin1 to enable the onboard LED's               */
        PORTB |= (1 << led);
    }
}


/*
*********************************************************************************************************
*                                 BSP OS TIME DELAY (Milliseconds)
*
* Description : This function provides uC/OS-II Time Delays (0-255 ms) to portable application code
*********************************************************************************************************
*/

void  BSP_DlyMS (INT8U ms)
{
    OSTimeDlyHMSM (0, 0, 0, ms);    
}


/*
*********************************************************************************************************
*                                     uC/OS-II TICK ISR INITIALIZATION
*
* Description : This function is used to initialize one of the eight output compares to generate an

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -