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

📄 sb_custom.c

📁 UART sample code for AVR
💻 C
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************

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

    Description: 
    Implementation of Simply Blue Interface framework for embedded
    development platform preferably using MicroC/OS-II RTOS.   
    
    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.
*****************************************************************************/

#include <stdio.h>                         
#include <stdlib.h>
#include <string.h>
#include "drivers.h"      /* CP3000 specific: Drivers for CP3000 products */
#include "dbgmsg.h"       /* Debug interface used with our CP3000 embedded products */

#include "sb_custom.h"    /* Header file for this c file*/
#include "ucos.h"         /* Header file for UCOS functions */

#define SB_UART_PORT      2       /* UART port for debug. To connect to a terminal */

//#define MAX_CONNECTED_DEVICES 3 /* specific LMX9820A */
#define MAX_CONNECTED_DEVICES 7   /* specific LMX9830 */

#define SB_TIMER_TICKS        4   /* N  => (N * 10) ms */

static const usart_conf UsartInitConf =
{                          /* UART parameters for CP3SP33 - module communication */
    USART_XFER_DMA,        /* Use of UART DMA transfer */ 
    BPS_115200,            /* Speed 115200 BPS */
    HW_FLOW_CONTROL,       /* Use of Hardware Flow control */
    10000,                 /* UART timeout = 10000 */
    NULL
};

//Simply Blue supported codec types
typedef enum {
    SBCODEC_NONE,          /* No codec connected */
    SBCODEC_MOTOROLA,      /* Motorola codec MC145483 */
    SBCODEC_OKI,           /* OKI codec MSM7717 (Obsoleted!)*/
    SBCODEC_PCMSLAVE       /* PCM slave mode */
}SbCodecType_T;

//Simply Blue supported air formats
typedef enum {
    SBAIRFMT_CVSD,         /* CVSD compression */
    SBAIRFMT_MULAW,        /* uLAW compression */
    SBAIRFMT_ALAW          /* aLAW compression */
}SbAirFormat_T;


typedef struct 
{
	uint8   bStart;             
	uint8   bType;              
	uint8   bOpcode;
	uint16  wPayloadlen;
	uint16  wPayloadOffset;
	uint8   bChecksum;
	uint8   pPayload[340];
	uint8   bEnd;
} SBEvent_T;

//local device information
typedef struct {
    uint32   PortsToOpen;          /* The port numbers that are opened on the local device */
	OS_EVENT *SbCmdMbox;	       /* Mailbox for Simply Blue commands */
    SbPacketState_T SbPacketState; /* Current Sb packet byte position */
}SbDeviceInfoType;

static SbDeviceInfoType SbDevInfo; /* local device context information */

static SBEvent_T        SbEvent;   /* Current result of Simply Blue response */

static int sbTimerHandle = 0;      /* Timer handle */

Boolean Transparent_flag = FALSE;  /* Transparent flag to manage transparent mode /*

/************ Functions Prototypes *******************************/
static SBStatus_T SbSendCommand(uint8* SbCommand, uint16 Size);
static void       SbProcessFrame(void);
static SBStatus_T SbInit(void);
static SBStatus_T SbEstablishLink(BdAddrType,uint8,uint8);  
static SBStatus_T SbReleaseLink(uint8 LocalPortNo);  
static SBStatus_T SbEnterTransparentMode(uint8 LocalPortNo);
static void       SbSendUartBreak(void);
static boolean    SbSdapStartSession( BdAddrType bdAddr );
static boolean    SbSdapStopSession(void);
static SBStatus_T SbDeleteSdpRecords(void);
static SBStatus_T SbStoreSdpRecord(uint8* entry, uint16 length);
static SBStatus_T SbSetOperationMode(SBOperationMode_T mode);
static void       SbNvsReadBdAddr(BdAddrType BdAddr);
static void       SbNvsWriteBdAddr(BdAddrType BdAddr);
static uint8      SbNvsReadDeviceName(BtNameType, uint8*, uint8);
static void       SbNvsWriteDeviceName(BtNameType Name,uint8 length);
static SBStatus_T SbSetNvsDefaultAudioSettings(uint8, uint8);
static SBStatus_T SbResetFactorySettings(void);
static SBStatus_T SbResetDevice(void);
static SBStatus_T SbConfigureLocalDevice(void);

static void SbTimerTask( void );

//SB framework api
/******************************************************************************  
    Function: SbApiInit

	Description
	-----------
    This function Intializes the following :
    - initializes the simply blue software architecture
    - initializes the usart port that is being used for simply blue connection.
    - initializes the simply blue firmware

	Arguments
	---------
    None   

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbApiInit(void)
{
    /* Create a mailbox for inter-task communication */
	SbDevInfo.SbCmdMbox = OSMboxCreate((void *)0);
    
    if (SbDevInfo.SbCmdMbox == NULL) {
        return SBSTATUS_ERROR;
    }

    SbDevInfo.PortsToOpen     = 0;
    SbDevInfo.SbPacketState   = SBPACKET_START;

    SbEvent.wPayloadOffset = 0;

    sbTimerHandle = timer_set_wakeup( SbTimerTask, SB_TIMER_TICKS, TRUE );
    
    if (sbTimerHandle == -1) {
        return SBSTATUS_NO_TIMER;
    }

    /* Initialize DMA driver for UART DMA use */
    DmaInit();

    /* Initialize UART port communication with SB module */
    usart_init(SB_UART_PORT, &UsartInitConf);

    return SBSTATUS_OK;
}
/******************************************************************************  
    Function: SbInit

	Description
	-----------
    This function Intializes the SimplyBlue module.
    It basically does a restore to factory settings

	Arguments
	---------
    None   

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbInit(void)
{
    return SbConfigureLocalDevice();
}
/******************************************************************************  
    Function: SbApiShutdown

	Description
	-----------
    This function shutsdown the simply blue software system and associated
    hardware peripherals.

	Arguments
	---------
    None   

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbApiShutdown(void)
{
    uint8 err;

    OSMboxDel(SbDevInfo.SbCmdMbox, OS_DEL_ALWAYS, &err);

    timer_clear_wakeup( sbTimerHandle );
    
    return SBSTATUS_OK;
}

/******************************************************************************  
    Function: SbSendCommand

	Description
	-----------
    This function sends the specified simply blue command bytes over usart.
    The routine is a blocking call except for SPP_SEND_DATA command. The routine
    either will timeout or will be unblocked as soon as simply blue software
    receives and processes corresponding CFM message for the simply blue command
    being sent. SbProcessFrame routine unblocks the pending event.

	Arguments
	---------
    SbCommand : simply blue command bytes  
    Size      : size of simply blue command frame

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbSendCommand(uint8* SbCommand, uint16 Size)
{
    uint8  err;
    void   *msg;
    uint16 checksum;

    SbCommand[0]    = STX;
    SbCommand[1]    = REQ;
    checksum        = SbCommand[1] + SbCommand[2] + SbCommand[3] + SbCommand[4];
    SbCommand[5]    = checksum % 256;
	
    SbCommand[Size - 1] = ETX;
    
    if (usart_tx(SB_UART_PORT, SbCommand, Size) == Size) {
        if (SbCommand[2] == SPP_SEND_DATA) {
            return SBSTATUS_OK;
        }
    	msg = OSMboxPend(SbDevInfo.SbCmdMbox, BTCORE_CALLBACK_TIMEOUT, &err);
        if (err == OS_NO_ERR) {
            if ((uint32)msg == SBSTATUS_OK) {
                return SBSTATUS_OK;
            }
            else {
                return SBSTATUS_ERROR;
            }
        }
        else {
            return SBSTATUS_TIMEOUT;
        }
    }
	else {
		return SBSTATUS_UART_INCOMPLETE_TRANSFER;
	}
}

/******************************************************************************  
    Function: SbProcessFrame

	Description
	-----------
    This function is being called from SbTimerTask to process the received complete
    simply blue packet.This routine unblocks the pending events to denote a simply command
    is confirmed and completed.

	Arguments
	---------
    None.

    Returns
	-------
    None.
	
*******************************************************************************/
void SbProcessFrame(void)
{
    int    i;
    uint16 ServiceResponseStart=0;
    uint32 status;

    switch (SbEvent.bOpcode) 
    {
        case GAP_DEVICE_FOUND:              /* to be implemented by the user */ return; 
        case SPP_LINK_ESTABLISHED:          /* to be implemented by the user */ return;
        case SPP_INCOMING_LINK_ESTABLISHED: /* to be implemented by the user */ return;
	case SPP_LINK_RELEASED:             /* to be implemented by the user */ return;
        case GAP_ESTABLISH_SCO_LINK:        /* to be implemented by the user */ return;
        case GAP_RELEASE_SCO_LINK:          /* to be implemented by the user */ return;
	case SPP_SEND_DATA:                 /* to be implemented by the user */ return;
    	case SPP_INCOMING_DATA:             /* to be implemented by the user */ return;
	case SDAP_SERVICE_BROWSE:           /* to be implemented by the user */ return;
	case SDAP_SERVICE_REQUEST:          /* to be implemented by the user */ return;
	case MODULE_READY:
            if ( COMMON_MboxWaitingTasks( SbDevInfo.SbCmdMbox ) != 0 ) {
                OSMboxPost(SbDevInfo.SbCmdMbox, (void*)1);
            }
            /* At this point, the Simply Blue device is reset and ready to work */
            return;
        default:
            break;
	}
    
    if (SbEvent.bType == CFM) {
        //signal that sb command is completed
        status = ((!SbEvent.pPayload[0]) ? SBSTATUS_OK : SbEvent.pPayload[0]);
        OSMboxPost(SbDevInfo.SbCmdMbox, (void*)status);
    }
}

/******************************************************************************  
    Function: SbEstablishLink

	Description
	-----------
    This function initates establishment of ACL link with remote device that is 
    specified by passed in Bd Address. The serial port link is established between 
    specified local port and remote port on the local and remote devices. 
    
    This function waits on Mbox events untill simply blue CFM message and 
    link_established messages are processed.

	Arguments
	---------
    BdAddr       : Bd Address of the remote device
    LocalPortNo  : port number of the local device
    RemotePortNo : port number of the remote device

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbEstablishLink(BdAddrType BdAddr,
                           uint8 LocalPortNo, 
                           uint8 RemotePortNo)  
{
    uint16 payloadlen;
    uint8  SbCommand[15];
    int    i;
    uint8  err;
    void   *msg;

    payloadlen = 0x0008;

    SbCommand[2] = SPP_ESTABLISH_LINK; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion

    SbCommand[6] = LocalPortNo;           // Localport
    
    for (i = 0; i < 6; i++) {
        SbCommand[7 + i] = BdAddr[i];     // BDA
    }
    
    SbCommand[7 + i] = RemotePortNo;      // Remoteport

    if (SbSendCommand(SbCommand, 7 + payloadlen) == SBSTATUS_OK) {
        //wait till link is established
    	msg = OSMboxPend(SbDevInfo.SbCmdMbox, BTCORE_CALLBACK_TIMEOUT, &err);
        if ( err == OS_NO_ERR && ((uint32)(msg) == SBSTATUS_OK)) {
            return SBSTATUS_OK;
        }
    }

    return SBSTATUS_ERROR;
}

/******************************************************************************  

⌨️ 快捷键说明

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