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

📄 sb_custom.c

📁 UART sample code for AVR
💻 C
📖 第 1 页 / 共 3 页
字号:
    Name          : Name of the local device will be fetched in to this parameter. The applications
                    should make sure to pass in the same size as BtNameType specifies in sb.h
    
    MaxNameLength : The actual length of the local device name that is being received.

    Offset        : unused parameter
           
	
    Returns
	-------
	Length of the device name
	
*******************************************************************************/
uint8 SbNvsReadDeviceName(BtNameType Name, uint8 *MaxNameLength, uint8 Offset)
{
    uint16 payloadlen;
    uint8  SbCommand[7];
    uint8  len;
    int    i;

    payloadlen = 0x0000;

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

    Offset = Offset;
    len    = 0;

    if (SbSendCommand(SbCommand, 7 + payloadlen) == SBSTATUS_OK) {
    	len=SbEvent.pPayload[1];
        for (i=0;i<len;i++) {
    		Name[i]=SbEvent.pPayload[i+2];
        }
        *MaxNameLength = len;
    }

    return len;
}

/******************************************************************************  
    Function: SbNvsWriteDeviceName

	Description
	-----------
    This function writes the local device name in the NVS.

	Arguments
	---------
    Name          : Name of the local device will be fetched in to this parameter. The applications
                    should make sure to pass in the same size as BtNameType specifies in sb.h
    
    Returns
	-------
	Status of the command, TRUE command successfull, FALSE command failed
	
*******************************************************************************/
void SbNvsWriteDeviceName(BtNameType Name,uint8 length) 
{
    uint16 payloadlen;
    uint8  SbCommand[7+BT_NAME_LENGTH];
    int    i=0;

    while(Name[i] != '\0')
    {
        SbCommand[7+i] = Name[i];
        i++;
    }
    SbCommand[7+i] = '\0';
    payloadlen = length + 1;   // Payload length

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

    SbSendCommand(SbCommand, 7 + payloadlen);
}

/******************************************************************************  
    Function: SbSetNvsDefaultAudioSettings

	Description
	-----------
    This function sets the default audio settings in Simply Blue NVS storage. These
    settings are used while creating bluetooth audio link, to configure AAI bus and any external
    audio codec boards connected to simply blue board.

	Arguments
	---------
    CodecType : valid values follow SbCodecType_T enum type
    AirFormat : valid values follow SbAirFormat_T enum type.
	
    Returns
	-------
	A SBStatus_T value indicating whether default audio settings are stored successfully in 
	simply Blue NVS or not.
*******************************************************************************/
SBStatus_T SbSetNvsDefaultAudioSettings(uint8 CodecType, uint8 AirFormat)
{
    uint16 payloadlen;
    uint8  SbCommand[9];

    payloadlen = 0x0002;

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

    SbCommand[6] = CodecType;
    SbCommand[7] = AirFormat;
	
    return SbSendCommand(SbCommand, 7 + payloadlen);
}

/******************************************************************************  
    Function: SbResetFactorySettings

	Description
	-----------
    This function initializes the simply blue device opeartion with factory settings
    please check simply blue software documentation for the details.

	Arguments
	---------
    None  : 

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbResetFactorySettings(void)
{
    uint16 payloadlen;
    uint8  SbCommand[7];

    payloadlen = 0x0000;

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

    return SbSendCommand(SbCommand, 7 + payloadlen);
}

/******************************************************************************  
    Function: SbResetDevice

	Description
	-----------
    This function does Software RESET on local simply blue device.Please check simply blue
    software documentation for the details.

	Arguments
	---------
    None   

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbResetDevice(void)
{
    uint16 payloadlen;
    uint8  SbCommand[7];

    payloadlen = 0x0000;

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

    return SbSendCommand(SbCommand, 7 + payloadlen);
}

/******************************************************************************  
    Function: SbGetPacketState

	Description
	-----------
    This function returns the current simply blue(sb) packet processing state.
    A value of SBPACKET_ERR means an error occured in sb packet reception. No
    error recovery is being implemented. Applications should call this function
    and if SBPACKET_ERR happens, sb software stops processing further sb packets
    at this time.
    

	Arguments
	---------
	EventP:
	A pointer to the Connection_Info_Event_Type

	Returns
	-------
	A SbPacketState_T value indicating current state of sb packet processing.

*******************************************************************************/
SbPacketState_T SbGetPacketState(void)
{
    return SbDevInfo.SbPacketState;
}

/******************************************************************************  
    Function: SbTimerTask

	Description
	-----------
    This function is being invoked repeatedly for every SB_TIMER_TICKS, process
    the received bytes from Usart that is connected to SB hardware's Usart.
    A value of SBPACKET_ERR means an error occured in sb packet reception. No
    error recovery is being implemented. Applications should call SbGetPacketState
    function and if SBPACKET_ERR happens, sb software stops processing further 
    sb packets at this time.
    
    NOTE: Simply Blue transparent mode is not implemented. This routine is currently
    applicable only for Command mode.

	Arguments
	---------
	None

	Returns
	-------
	None.

*******************************************************************************/
void SbTimerTask( void )
{
    uint16 rxCount;
    uint8  in;
    uint16 DataLen;
    uint8  cs;
	
    while ((rxCount = usart_rx_count(SB_UART_PORT)) > 0) {
        switch(SbDevInfo.SbPacketState) {
        case SBPACKET_START:
            usart_rx(SB_UART_PORT, &in, 1);
            rxCount--;
            if (in == 0x2) {
                SbDevInfo.SbPacketState = SBPACKET_TYPE;
            }
            else {
                SbDevInfo.SbPacketState = SBPACKET_ERR;
            }
            break;
        case SBPACKET_TYPE:
            usart_rx(SB_UART_PORT, &in, 1);
            rxCount--;
            if (in == CFM || in == IND || in == RES) {
                SbDevInfo.SbPacketState = SBPACKET_OPCODE;
                SbEvent.bType   = in;
            }
            else {
                SbDevInfo.SbPacketState = SBPACKET_ERR;
            }
            break;
        case SBPACKET_OPCODE:
            usart_rx(SB_UART_PORT, &in, 1);
            rxCount--;
            SbEvent.bOpcode = in;
            SbDevInfo.SbPacketState = SBPACKET_DATALENGTH;
            break;
        case SBPACKET_DATALENGTH:
            if (rxCount >= 2) {
                usart_rx(SB_UART_PORT, (uint8*)&DataLen, 2);
                rxCount -=2;
                SbEvent.wPayloadlen = DataLen;
                SbDevInfo.SbPacketState = SBPACKET_CHECKSUM;
            }
            else {
                return; //wait for some more bytes to receive
            }
            break;
        case SBPACKET_CHECKSUM:
            usart_rx(SB_UART_PORT, &in, 1);
            rxCount--;
            cs = SbEvent.bType + SbEvent.bOpcode + 
                 (SbEvent.wPayloadlen >> 8) + (SbEvent.wPayloadlen & 0x00FF);
            if (cs == in) {
                SbDevInfo.SbPacketState = SBPACKET_DATA;
            }
            else {
                SbDevInfo.SbPacketState = SBPACKET_ERR;
            }
            break;
        case SBPACKET_DATA:
            if (rxCount >= (SbEvent.wPayloadlen - SbEvent.wPayloadOffset)) {
                usart_rx(SB_UART_PORT, &SbEvent.pPayload[SbEvent.wPayloadOffset], 
                         SbEvent.wPayloadlen - SbEvent.wPayloadOffset);
                rxCount -=(SbEvent.wPayloadlen - SbEvent.wPayloadOffset);                         
                SbDevInfo.SbPacketState = SBPACKET_END;
                SbEvent.wPayloadOffset = 0;
            }
            else {
                usart_rx(SB_UART_PORT, &SbEvent.pPayload[SbEvent.wPayloadOffset], 
                         rxCount);
                SbEvent.wPayloadOffset = rxCount;
                rxCount = 0;
            }
            break;
        case SBPACKET_END:
            usart_rx(SB_UART_PORT, &in, 1);
            if (in == ETX) {
                SbProcessFrame();    /* process the frame once received */
                SbDevInfo.SbPacketState = SBPACKET_START;
                //return;
            }
            else {
                SbDevInfo.SbPacketState = SBPACKET_ERR;
            }
            break;
        default:
            // invalid Packet received from SB usart,skipping of bytes
            // to next valid packet is not incorporated. simply blue assumes
            // error free usart transport. No further processing.
            // apps should call SbGetPacketState() and do proper exit or
            // analyze on error state.
            SbDevInfo.SbPacketState = SBPACKET_ERR;
            return;
        }
    }
}

/******************************************************************************  
    Function: SbConfigureLocalDevice

	Description
	-----------
    This function resets the simply blue device to factory settings first, next
    deletes all sdp records, sets operation mode as Command mode, and finally does 
    Software RESET on local simply blue device.Please check simply blue
    software documentation for the details of these operations.

	Arguments
	---------
    None   

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbConfigureLocalDevice(void)
{
    SBStatus_T status;
    //Initialize audio codec settings
    SbCodecType_T CodecType;
    SbAirFormat_T AirFormat;

    CodecType = SBCODEC_OKI;
    AirFormat = SBAIRFMT_CVSD;

    status = SbResetFactorySettings();
    
    if (status == SBSTATUS_OK) {
        status = SbDeleteSdpRecords();
        if (status == SBSTATUS_OK) {
            status = SbSetOperationMode(SB_COMMAND_MODE);
            if (status == SBSTATUS_OK) {
                status = SbSetNvsDefaultAudioSettings(CodecType, AirFormat);
                if (status == SBSTATUS_OK) {
                    status = SbResetDevice();
                }
            }
        }
    }
    return status;
}


⌨️ 快捷键说明

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