• HC-05底层驱动


    INT8U BT_INIT(const char * pNAME, INT32U BAUD, INT8U ROLE, INT32U PSWD)
    {
        INT8U    OS_ERR    = OS_ERR_NONE;
        INT8U    BT_ERR    = BT_ERR_NONE;
        INT8U    RETRY    = 0;
        BOOLEAN    F_SET    = FALSE;
        BOOLEAN    F_BOOT    = FALSE;
        char    *MAIL;
        
        
        // Ç¿ÖÆÖØÆô½øÈëATģʽ
        BT_PWR_SET(0);
        _delay_ms(25);
        BT_DAT_MOD_SET(DAT_MOD_AT);
        BT_PWR_SET(1);
        _delay_ms(25);
        
        /* È·¶¨À¶ÑÀ½øÈëATģʽ *************************************************************/
        if(BT_ERR == BT_ERR_NONE) {
            for(RETRY=25; RETRY>0; RETRY--) {
                xfprintf(BT_OUT, "AT
    ");
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT
    ");
                    // printf("%s
    ", mail);
                    if(strstr(MAIL, "OK")) {
                        BT_ERR = BT_ERR_NONE;
                        break;
                    }
                    else {
                        BT_ERR = BT_ERR_RES;
                    }
                }
                else {
                    // debug info...
                    // printf("BT->AT -> ERROR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        
        /* »ñÈ¡±¾»úÀ¶ÑÀµØÖ· *****************************************************************/
        if(BT_ERR == BT_ERR_NONE) {
            for(RETRY=5; RETRY>0; RETRY--) {
                xfprintf(BT_OUT, "AT+ADDR?
    ");
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT+ADDR?
    ");
                    // printf("%s
    ", mail);
                    if(strstr((char *)MAIL, "ADDR")) {
                        ADDR_PARSE(MAIL, (INT32U *)&BT_API.MAC);
                        BT_ERR = BT_ERR_NONE;
                        break;
                    }
                    else {
                        BT_ERR = BT_ERR_RES;
                    }
                }
                else {
                    // debug info...
                    // printf("BT->ADDR : ERR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        
        /* ½«À¶ÑÀÉèÖÃΪÖ÷»ú/´Ó»úģʽ *************************************************************/
        if(BT_ERR == BT_ERR_NONE) {
            for(RETRY=5; RETRY>0; RETRY--) {
                xfprintf(BT_OUT, "AT+ROLE?
    ");
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT+ROLE?
    ");
                    // printf("%s
    ", mail);
                    if(ROLE == HOST) {
                        if(strstr(MAIL, "ROLE:1")) {
                            F_SET = FALSE;
                            BT_ERR = BT_ERR_NONE;
                            break;
                        }
                        else if(strstr(MAIL, "ROLE:0")) {
                            F_SET = TRUE;
                            F_BOOT = TRUE;
                            BT_ERR = BT_ERR_NONE;
                            break;
                        }
                    }
                    else {
                        if(strstr(MAIL, "ROLE:0")) {
                            F_SET = FALSE;
                            BT_ERR = BT_ERR_NONE;
                            break;
                        }
                        else if(strstr(MAIL, "ROLE:1")) {
                            F_SET = TRUE;
                            F_BOOT = TRUE;
                            BT_ERR = BT_ERR_NONE;
                            break;
                        }
                    }
                }
                else {
                    // debug info...
                    // printf("BT->ROLE? -> OS_ERR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        if(BT_ERR==BT_ERR_NONE && F_SET==TRUE) {
            F_SET = FALSE;
            for(RETRY=5; RETRY>0; RETRY--) {
                if(ROLE == HOST) {
                    xfprintf(BT_OUT, "AT+ROLE=1
    ");
                }
                else {
                    xfprintf(BT_OUT, "AT+ROLE=0
    ");
                }
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT+ROLE=x
    ");
                    // printf("%s
    ", mail);
                    if(strstr(MAIL, "OK")) {
                        BT_ERR = BT_ERR_NONE;
                        break;
                    }
                    else {
                        BT_ERR = BT_ERR_RES;
                    }
                }
                else {
                    // debug info...
                    // printf("BT->AT_ROLE->ERROR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        
        /* ²éѯºÍÉèÖÃÀ¶ÑÀ²¨ÌØÂÊ *************************************************************/
        if(BT_ERR == BT_ERR_NONE) {
            for(RETRY=5; RETRY>0; RETRY--) {
                xfprintf(BT_OUT, "AT+UART?
    ");
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT+UART?
    ");
                    // printf("%s
    ", mail);
                    if(strstr(MAIL, "OK") && strstr(MAIL, "UART")) {
                        char *pnum = strchr(MAIL, ':');
                        if(pnum) {
                            pnum++;
                            if(atoi(pnum) != BAUD) {
                                F_SET = TRUE;
                                F_BOOT = TRUE;
                            }
                            else {
                                F_SET = FALSE;
                            }
                        }
                        BT_ERR = BT_ERR_NONE;
                        break;
                    }
                    else {
                        BT_ERR = BT_ERR_RES;
                    }
                }
                else {
                    // debug info...
                    // printf("BT->AT_BAUD? -> ERROR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        if(BT_ERR==BT_ERR_NONE && F_SET==TRUE) {
            F_SET = FALSE;
            for(RETRY=5; RETRY>0; RETRY--) {
                xfprintf(BT_OUT, "AT+UART=%d,0,0
    ", BAUD);
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT+UART=%d,0,0
    ", BAUD);
                    // printf("%s
    ", mail);
                    if(strstr(MAIL, "OK")) {
                        BT_ERR = BT_ERR_NONE;
                        break;
                    }
                    else {
                        BT_ERR = BT_ERR_RES;
                    }
                }
                else {
                    // debug info...
                    // printf("BT->AT_BAUD=xxxxxx -> ERROR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        
        /* ²éѯ/ÉèÖÃ-Åä¶ÔÂë **************************************************************/
        if(BT_ERR == BT_ERR_NONE) {
            for(RETRY=5; RETRY>0; RETRY--) {
                xfprintf(BT_OUT, "AT+PSWD?
    ");
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT+PSWD?
    ");
                    // printf("%s
    ", mail);
                    if(strstr(MAIL, "PSWD") && strstr(MAIL, "OK")) {
                        char * pnum = strchr(MAIL, ':');
                        if(pnum) {
                            pnum++;
                            if(atoi(pnum) == PSWD) {
                                F_SET = FALSE;
                            }
                            else {
                                F_SET = TRUE;
                                F_BOOT = TRUE;
                            }
                        }
                        BT_ERR = BT_ERR_NONE;
                        break;
                    }
                    else {
                        BT_ERR = BT_ERR_RES;
                    }
                }
                else {
                    // debug info...
                    // printf("BT->AT+PSWD? -> ERROR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        if(BT_ERR==BT_ERR_NONE && F_SET==TRUE) {
            F_SET = FALSE;
            for(RETRY=5; RETRY>0; RETRY--) {
                xfprintf(BT_OUT, "AT+PSWD=%04d
    ", PSWD);
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT+PSWD=%d
    ", PSWD);
                    // printf("%s
    ", mail);
                    if(strstr(MAIL, "OK")) {
                        BT_ERR = BT_ERR_NONE;
                        break;
                    }
                    else {
                        BT_ERR = BT_ERR_RES;
                    }
                }
                else {
                    // debug info...
                    // printf("BT->AT+PSWD=xxxx -> ERROR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        
        /* ²éѯ/ÉèÖÃÖ÷»úÃû³Æ **************************************************************/
        if(BT_ERR == BT_ERR_NONE) {
            for(RETRY=5; RETRY>0; RETRY--) {
                xfprintf(BT_OUT, "AT+NAME?
    ");
                MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                if(OS_ERR == OS_ERR_NONE) {
                    // debug info...
                    // printf("AT+NAME?
    ");
                    // printf("%s
    ", mail);
                    if(strstr(MAIL, "NAME")) {
                        BT_ERR = BT_ERR_NONE;
                        if(strstr(MAIL, pNAME)) {
                            F_SET = FALSE;
                        }
                        else {
                            F_SET = TRUE;
                            F_BOOT = TRUE;
                        }
                        break;
                    }
                    else {
                        BT_ERR = BT_ERR_RES;
                    }
                }
                else {
                    // debug info...
                    // printf("BT->AT+NAME? : ERR!
    ");
                    _delay_ms(25);
                }
            }
            if(RETRY==0) BT_ERR = BT_ERR_OS;
        }
        if(F_SET == TRUE) {
            if(BT_ERR == BT_ERR_NONE) {
                for(RETRY=5; RETRY>0; RETRY--) {
                    xfprintf(BT_OUT, "AT+NAME=%s
    ", pNAME);
                    MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                    if(OS_ERR == OS_ERR_NONE) {
                        // debug info...
                        // printf("AT+NAME=%s
    ", pNAME);
                        // printf("%s
    ", mail);
                        if(strstr(MAIL, "OK")) {
                            BT_ERR = BT_ERR_NONE;
                            break;
                        }
                        else {
                            BT_ERR = BT_ERR_RES;
                        }
                    }
                    else {
                        // debug info...
                        // printf("BT->AT+NAME=xxxx : ERR!
    ");
                        _delay_ms(25);
                    }
                }
                if(RETRY==0) BT_ERR = BT_ERR_OS;
            }
        }
        
        // ½«À¶ÑÀÈí¸´Î»£¬ÉúЧ¸ü¸Ä
        if(F_BOOT == TRUE) {
            if(BT_ERR == BT_ERR_NONE) {
                for(RETRY=5; RETRY>0; RETRY--) {
                    xfprintf(BT_OUT, "AT+RESET
    ");
                    MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                    if(OS_ERR == OS_ERR_NONE) {
                        // debug info...
                        // printf("AT+RESET=1
    ");
                        // printf("%s
    ", mail);
                        if(strstr(MAIL, "OK")) {
                            BT_ERR = BT_ERR_NONE;
                            break;
                        }
                        else {
                            BT_ERR = BT_ERR_RES;
                        }
                    }
                    else {
                        // debug info...
                        // printf("BT->AT+RESET : ERR!
    ");
                        _delay_ms(25);
                    }
                }
                if(RETRY==0) BT_ERR = BT_ERR_OS;
            }
            
            // µÈ´ýÀ¶ÑÀ¸´Î»³É¹¦
            if(BT_ERR == BT_ERR_NONE) {
                for(RETRY=25; RETRY>0; RETRY--) {
                    xfprintf(BT_OUT, "AT
    ");
                    MAIL = (char *)OSMboxPend(BT_MEvent, OS_TICKS_PER_SEC/10, &OS_ERR);
                    if(OS_ERR == OS_ERR_NONE) {
                        // debug info...
                        // printf("AT
    ");
                        // printf("%s
    ", mail);
                        if(strstr(MAIL, "OK")) {
                            BT_ERR = BT_ERR_NONE;
                            break;
                        }
                        else {
                            BT_ERR = BT_ERR_RES;
                        }
                    }
                    else {
                        // debug info...
                        // printf("BT->AT -> ERROR!
    ");
                        _delay_ms(25);
                    }
                }
                if(RETRY==0) BT_ERR = BT_ERR_OS;
            }
        }
        
        return BT_ERR;
    }
  • 相关阅读:
    微服务架构下分布式事务解决方案——阿里GTS
    兼容IE8以下,获取className节点的元素(document.getElementsByClassName()兼容写法)。
    解决移动端ios下overflow-x scroll无法隐藏滚动条的问题
    skeleton在心意web上的实践
    小程序开发笔记(八)—Js数组按日期分组显示数据
    小程序开发笔记(七)—加入内容安全检测
    使用react+redux+react-redux+react-router+axios+scss技术栈从0到1开发一个applist应用
    vuex无法获取getters属性this.$store.getters.getCurChildId undefined
    函数防抖节流的理解及在Vue中的应用
    Javascript Object常用方法总结
  • 原文地址:https://www.cnblogs.com/qdrs/p/10079860.html
Copyright © 2020-2023  润新知