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

📄 sbappli_custom.c

📁 UART sample code for AVR
💻 C
字号:
/*****************************************************************************

    Filename: sbappli_custom.c
    Date: 13/02/2006
    Revision: 1.0 
    Author: SM 
    
    National Semiconductor Corporation

    Description: 
    Basic framework for embedded development platform preferably using 
    MicroC/OS-II RTOS. The application uses NSC CP3SP33 chip as
    Microcontroller to program the SimplyBlue module.  
    
    The scope of this file is not to give a complete code compilable and 
    runnable, but deliver a framework to start with. The implementation of 
    the embedded application depends on the need and the platform used.
*****************************************************************************/

#define TICK_TASK_DELAY 1

/* INCLUDES */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crcommon.h"
#include "config.h"
#include "drivers.h"
#include "boarddrv.h"
#include "ucos.h"
#include "dbgmsg.h"

#include "sb_custom.h"

/************ HEAP and STACK size config *****/
#define HEAP_SIZE 3700 
#include "heap.h"

/* DEFINES */
#define STACK_SIZE0  600 /* application */
#define STACK_SIZE2  300 /* tick task */

#define UART_RX_BUF_SIZE   30
#define UART_TX_BUF_SIZE   30

/************ UART DEBUG CONFIG **************/
#define UART_DEBUG_PORT 0

static const usart_conf UsartInitConf_Debug =
{
    USART_XFER_DL,
    BPS_115200,
    HW_FLOW_CONTROL,
    10000,
    NULL
};

/************ UART HOST CONFIG **************/
/*    Beeing configured in sb_custom.c during SBInit()
  Using UART port 2
  Speed 115200 BPS
  No parity, 1 bit stop 
  Hardware control
*/

#define BUF_SIZE 30

unsigned char UartTxBuf[BUF_SIZE];


/*********** Variables **********************/
/* Task stacks */
static OS_STK stack0[ STACK_SIZE0 ];
static OS_STK stack2[ STACK_SIZE2 ];
uint8    status;

uint16 cmd;
uint16 cmdLength;

unsigned int Ticks;

uint8 remotePort;

/*********** Functions prototype *************/
static void SbHwInit(void);
static void HeartBeat(void);

unsigned int UsartTxWrapper(unsigned char *Buf, unsigned int Size)
{
    return usart_tx(UART_DEBUG_PORT, Buf, Size);
}

/*
---------
Tasks
---------
*/
void tickTask(void *p);         
void DemoTask( void *p );       // top level task

/*******************************************************************
Function main
Input:         ---
Description:   Initializes HW and OS, creates tasks, and starts the OS
********************************************************************/
void main(void)
{

#ifdef STK_DBG
    uint ctr;
#endif

    OSInit();      /* Init OS */

    SbHwInit();   /* Init CP3SP33 dev board for Simply Blue development */

#ifdef STK_DBG
    for(ctr = 0; ctr < STACK_SIZE0; ctr++)
        stack0[ctr] = 0xAAAA;
    for(ctr = 0; ctr < STACK_SIZE2; ctr++)
        stack2[ctr] = 0xCCCC;
#endif

    // Create tasks ##
    status = OSTaskCreate( tickTask, ( void * )0, stack2 + STACK_SIZE2 - 1, 4 );
    status = OSTaskCreate( DemoTask, (void *)0, stack0 + STACK_SIZE0 - 1, 8 );

    /* Initialize the debug message module. */
    dbg_init(UsartTxWrapper);

    // Start OS ##
    OSStart();
}

/*******************************************************************
Task DemoTask
********************************************************************/
void DemoTask( void *p )
{
/* To be implemented to include the application needs...
   This is should be the top function of the application
*/

    /* Enable all Interrupts */
    set_i_bit();

    /* Schedule the heartbeat function to run every 50 ticks (500 ms) */
    timer_set_wakeup(HeartBeat, 50, TRUE);

    /* Initialize the Simply Blue Framework Api */
    SbApiInit();

    /* Initialize the Simply Blue device : 
       THIS NEEDS TO BE DONE ONLY ONCE IN A LIFETIME */
    SbInit();      /* Init SB module: Restore Factory settings */

    
/* To be continued depending on the needs ...*/
}

/*******************************************************************
Task tickTask
********************************************************************/
void tickTask(void *p)
{
    p = p;

    while(1) {
        OSTimeDly(TICK_TASK_DELAY);
        Ticks++;
        timer_wakeup_check();
    }
}

/******************************************************************************
    Function: SbHwInit

    Description
    -----------
    This function initializes hardware components and their respective
    drivers.

    This function is specific to NSC CP3000 chips family and is not compatible
    with other microcontroller.

    Arguments
    ---------
    None.
*******************************************************************************/

void SbHwInit(void)
{
    /* Initialize the peripherals */
    SYSCFGEnableIoExpansion(); /* Enable I/O expansion */
    /* Initialize the clock driver */
    ClockInit();

    /* Enable the main Oscillator */
    ClockEnable(MAIN_OSC, MAIN_CLOCK_FREQ, MAIN_OSC);

    /* Set high frequency clock */
    if(MAIN_CLOCK_FREQ != AHB_CLOCK_FREQ)
    {
        /* Enable the PLL before taking the system clock from it */
        ClockEnable(PLL_1, AHB_CLOCK_FREQ, MAIN_OSC);
        ClockEnable(AHB_CLOCK, AHB_CLOCK_FREQ, PLL_1);
        ClockEnable(APB_CLOCK, APB_CLOCK_FREQ, AHB_CLOCK);
    }
    else
    {
        ClockEnable(AHB_CLOCK, AHB_CLOCK_FREQ, MAIN_OSC);
        ClockEnable(APB_CLOCK, APB_CLOCK_FREQ, AHB_CLOCK);
    }
    icu_init();                            /* Initialize the ICU */
    timer_init(1000 / OS_TICKS_PER_SEC);   /* Initialize timer module */
    usart_init(UART_DEBUG_PORT, &UsartInitConf_Debug);  /* Initialize the USART DEBUG*/

    /* Initialize the memory */
    if(!MemInterfaceInit())
        panic();

    /* Initialize LEDs. */
    led_init();
}

/******************************************************************************
Function: HeartBeat

  Description
  -----------
  This function is the heartbeat function. It toggles a LED every time
  it's called.

    Arguments
    ---------
    None.
*******************************************************************************/
static void HeartBeat(void)
{
    led_toggle(0);
}


⌨️ 快捷键说明

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