1. 程式人生 > >ESP8266--學習筆記(七)UART轉發

ESP8266--學習筆記(七)UART轉發

 因為需要使用串列埠傳送資料,而又決定自己寫韌體,所以就需要完成一項功能:

  • ESP接收串列埠傳來的資料,並判斷
  • 將接收的串列埠資料轉發出去

 ESP8266給串列埠傳送資料很容易

uart_sendString("串列埠傳送資料");

usrt0_sendStr("\r\n Hello World\r\n");

os_printf("傳送資料成功!!\r\n");

 這些語句都可以使用串列埠傳送資料,但是串列埠接收資料就不容易了。
 有時候,需要ESP8266接收資料並作出判斷。於是就搞出來這個程式了。

————————————————————

user_main.c

#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "driver/uart.h"
#include "driver/pwm.h"
void delay_ms(uint16 x)
{
    for(;x>0;x--)
    {
    os_delay_us(1000);
    }
}

void user_init(void)
{
    uart_init(BIT_RATE_115200,BIT_RATE_115200);
    uart_sendString("串列埠傳送程式");
    //接收程式在uart.c uart0_rx_intr_handler(void *para)函式裡面
}

uart.c

#include "ets_sys.h"
#include "osapi.h"
#include "driver/uart.h"

#define UART0   0
#define UART1   1

// UartDev is defined and initialized in rom code.
extern UartDevice UartDev;

LOCAL void uart0_rx_intr_handler(void *para);//串列埠接收資料中斷函式

LOCAL void ICACHE_FLASH_ATTR
uart_config(uint8 uart_no)
{
    if
(uart_no == UART1) { PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); } else { /* rcv_buff size if 0x100 */ ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff)); PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); } uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate)); WRITE_PERI_REG(UART_CONF0(uart_no), UartDev.exist_parity | UartDev.parity | (UartDev.stop_bits << UART_STOP_BIT_NUM_S) | (UartDev.data_bits << UART_BIT_NUM_S)); //clear rx and tx fifo,not ready SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST); CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST); //set rx fifo trigger WRITE_PERI_REG(UART_CONF1(uart_no), (UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S); //clear all interrupt WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff); //enable rx_interrupt SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA); } /*************/ /*傳送一個字元*/ /*************/ LOCAL STATUS ICACHE_FLASH_ATTR uart1_tx_one_char(uint8 TxChar) { while (true) { uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(UART1)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S); if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) { break; } } WRITE_PERI_REG(UART_FIFO(UART1) , TxChar); return OK; } LOCAL void ICACHE_FLASH_ATTR uart1_write_char(char c) { if (c == '\n') { uart1_tx_one_char('\r'); uart1_tx_one_char('\n'); } else if (c == '\r') { } else { uart1_tx_one_char(c); } } /*************************/ /**串列埠接收資料中斷處理函式**/ /************************/ LOCAL void uart0_rx_intr_handler(void *para) { /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents * uart1 and uart0 respectively */ RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para; uint8 RcvChar; if (UART_RXFIFO_FULL_INT_ST != (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST)) { return; } WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR); while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { RcvChar = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; uart_tx_one_char(RcvChar); // uart0_tx_buffer(RcvChar,1); if (pRxBuff->pWritePos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) { // overflow ...we may need more error handle here. pRxBuff->pWritePos = pRxBuff->pRcvMsgBuff ; } } } void ICACHE_FLASH_ATTR uart0_tx_buffer(uint8 *buf, uint16 len) { uint16 i; for (i = 0; i < len; i++) { uart_tx_one_char(buf[i]); } } /*********************************/ /****串列埠初始化函式****/ /*********************************/ void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) { // rom use 74880 baut_rate, here reinitialize UartDev.baut_rate = uart0_br; uart_config(UART0); UartDev.baut_rate = uart1_br; uart_config(UART1); ETS_UART_INTR_ENABLE(); // install uart1 putc callback os_install_putc1((void *)uart1_write_char); } void ICACHE_FLASH_ATTR uart_sendString(uint8 *st) { uint8 x; for(x=0;x<os_strlen(st);x++) { uart_tx_one_char(st[x]); } }

相關推薦

ESP8266--學習筆記UART轉發

 因為需要使用串列埠傳送資料,而又決定自己寫韌體,所以就需要完成一項功能: ESP接收串列埠傳來的資料,並判斷 將接收的串列埠資料轉發出去  ESP8266給串列埠傳送資料很容易 uart_sendString("串列埠傳送資料"); us

mysql學習筆記—— MySQL內連接和外連接

聚集函數 信息 _id left tro 做了 學習 作用 group MySQL內連接(inner join on) MySQL的內連接使用inner join on,它的效果跟使用where是一樣的,如果聯結的是兩個表,那麽需要左右的條件或者說字段是

Spring 學習筆記—— 切入點表達式

service string 出現 targe || 參數 public 例如 語法   為了能夠靈活定義切入點位置,Spring AOP提供了多種切入點指示符。 execution———用來匹配執行方法的連接點   

EF學習筆記:讀取關聯數據

取數據 microsoft image zha 手動 模型 取數 foreach ret 總目錄:ASP.NET MVC5 及 EF6 學習筆記 - (目錄整理) 本篇參考原文鏈接:Reading Related Data 本章主要講述加載顯示關聯數據; 數據加載分為以下三

Java語言基礎學習筆記

day tez lec mdk abd err .com mar mdm 烈7A茨諳9m繁5暗MChttp://www.zcool.com.cn/collection/ZMTg3NzE1Njg=.html 3馗iC蓖17握WM啦http://www.zcool.com.cn

python學習筆記函數

限制 指向 什麽 問題 www. img 值傳遞 在線的 comment 原鏈接:http://www.cnblogs.com/vamei/archive/2012/06/01/2529500.html#!comments 函數學習遇到了問題 1 #!/usr/bin/

PHP7 學習筆記如何使用zephir編譯一個擴展記錄

ring0 hub dev conf rep repo ase comm extension 一、zephir 編譯遇到的錯誤 安裝 git clone https://github.com/phalcon/zephir $ cd zephir $ ./instal

c++學習筆記- lambda表達式 叠代器 算法

tex 參數 p s 刷題 algo 叠代器 裏的 blog 而且 關於lambda表達式: 刷題的時候遇到一句代碼不懂: char ch = *it;auto it2 = find_if(it, b.end(), [ch](char x){ return x != ch

《Qt5 開發與實例第三版學習筆記

clu idg center ble mil detached pre tab etc 1 // 3.2 停靠窗口 QDockWidget類 2 setFeatures() 3 setAllowedAreas() 4 setWidget() 5 addDockW

Linux學習筆記環境變量PATH、cp命令、mv命令、文檔查看cat/more/less/h

查看 linux學習 文件覆蓋 echo txt 但是 學習 https ls命令 一、環境變量PATH對於環境變量,百度解釋為https://baike.baidu.com/item/%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F/1730949

筆記篇】最良心的計算幾何學習筆記

不一定 source spa hub 掃描 markdown 如何 urn 神奇 動態凸包 本文的github傳送門在這裏~ ====================================================================== 不會凸

hadoop學習筆記:Java HDFS API

on() apr name pin package 目錄 except 讀取 play 一、使用HDFS FileSystem詳解 HDFS依賴的第三方包:   hadoop 1.x版本:   commons-configuration-1.6.jar   comm

C Primer Plus學習筆記- C 控制語句:分支和跳轉

prim class wid int 但是 count 跳轉 ++ idt if 語句: if 語句被稱為分支語句(branching statement)或選擇語句(selection statement) if 語句的通用形式: if (expression)

FPGA學習筆記——FSMFinite State Machine,有限狀態機設計

fault mil 系統 time 編碼 代碼 ril esc 寫法   FPGA設計中,最重要的設計思想就是狀態機的設計思想!狀態機的本質就是對具有邏輯順序和時序規律的事件的一種描述方法,它有三個要素:狀態、輸入、輸出:狀態也叫做狀態變量(比如可以用電機的不同轉速作為狀態

Nodejs學習筆記—Node.js + Express 構建網站簡單示例

ren 結構 crypto 中間件 實現 cmd ews path releases 前言   上一篇學習了一些構建網站會用到的一些知識點:https://www.cnblogs.com/flyingeagle/p/9192936.html   這一篇主要結合前面講到的知識

官網英文版學習——RabbitMQ學習筆記Topic

fault 路徑 分享圖片 lazy ctrl+ hello sum byte[] style 在上一篇中使用直接交換器改進了我們的系統,使得它能夠有選擇的進行接收消息,但它仍然有局限性——它不能基於多個條件進行路由。本節我們就進行能夠基於多個條件進行路由的topi

ASP.NET Core 2 學習筆記路由

local quest urn AD term 執行 自動 routes code 原文:ASP.NET Core 2 學習筆記(七)路由ASP.NET Core通過路由(Routing)設定,將定義的URL規則找到相對應行為;當使用者Request的URL滿足特定規則條件

Python學習筆記

cookies 常用方法 .com 人的 https 初始化 面向 cache decode 用python代碼調用接口,使用urllib模塊,但是該模塊數據必須都是2進制的,比較麻煩 from urllib.request import urlopen from url

redux-form V.7.4.2學習筆記Field解析

ons nds rfi field 文字 class min 復雜 obj 引言 redux-form官方網站提供了操作form的許多API,其中最重要的無外乎三個:reduxForm(config:Object) 、props和 <Field/>。 <F

Python爬蟲學習筆記——智高考數據爬取

pid items bubuko strong eai res har href name 介紹 智高考是一個高考誌願網站,也是基於Ajax的。高中的時候我在wyz大神的幫忙下,嘗試過爬取信息來為填誌願做準備。但是當時沒有系統學習過爬蟲,幾乎都是靠大神帶飛,因此今天再次嘗試