DA14681如何在休眠狀態(tài)下正常接收到串口數(shù)據(jù)_第1頁(yè)
DA14681如何在休眠狀態(tài)下正常接收到串口數(shù)據(jù)_第2頁(yè)
DA14681如何在休眠狀態(tài)下正常接收到串口數(shù)據(jù)_第3頁(yè)
DA14681如何在休眠狀態(tài)下正常接收到串口數(shù)據(jù)_第4頁(yè)
DA14681如何在休眠狀態(tài)下正常接收到串口數(shù)據(jù)_第5頁(yè)
已閱讀5頁(yè),還剩20頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、中電港鐘志峰BDF008-DA14681如何在休眠狀態(tài)下正常接收到串口數(shù)據(jù)文檔版本:V0.0.1使用過(guò)DA14681的朋友一定知道,在芯片進(jìn)入低功耗模式后,串口的接收就會(huì)有問(wèn)題,往往會(huì)出現(xiàn)數(shù)據(jù)丟包的情況(程序被設(shè)定為收到數(shù)據(jù)立即轉(zhuǎn)發(fā)):11 54:2C.LI 54:22.5TE:l 154: 23. 12411:54: £3. £1B23. &58:11 :54:2j. T05.11:4: 33. &4 7TAI 4681 VAKTIQ CAKGei VAKT!OHello DAI 4661 VAKT'QSkHellQ BAI4SBI VAST!DA

2、I 46S1 UAKT! 口At唱"陰 4561 WT!sleep芯片在ddtVE狀態(tài)下丁每一個(gè)發(fā)岀去的數(shù)據(jù)包都能祓返回25旦芯片進(jìn)入休眠狀 態(tài),大多數(shù)數(shù)據(jù)都無(wú)法正常返回11 S4:37.BA14SB1 UAKT! O11 54:36 161TO-K>Hdlo DAI 4861 UAKT 口 11 S4:38. 754*g-OMaid IM40Bi HAM IO 11 54:38.441®-n>HaUo DAI 45B1 UAKT! 口 ;tl 54:56 535收*-H«lLo DAI4661 VAST! :54:4C. 15re->OHall

3、Q Udi4661 VAKTIDI 11 :5理:4D. TE刃麥gjggj 1TART!D那么,問(wèn)題來(lái)了,如果要保證所有上位機(jī)發(fā)送過(guò)來(lái)的數(shù)據(jù)都能被DA14681接收到,那豈不是要一直讓 DA14681處于active狀態(tài)呢?這樣,低功耗藍(lán)牙的意 義又何在?答案當(dāng)然是否定的,聰明的讀者應(yīng)該已經(jīng)想到了,可以讓上位機(jī)需要發(fā)送數(shù) 據(jù)的時(shí)候,想辦法先告訴 DA14681 “我要發(fā)送數(shù)據(jù)了,請(qǐng)準(zhǔn)備好”。本文所作的 工作就是告訴你如何來(lái)喚醒 DA14681,方法有兩種,一個(gè)是利用第三個(gè)10 口來(lái) 喚醒DA14681,也就是整個(gè)UART通信需要用三根線:RX,TX,INT第二種就是只 用RX,TX兩根線。最

4、終達(dá)到的目的就是:12:10:33.78B收cKi p ac weFiz: 10:39. 279 :12: 10:39. 372' ;12:10:33 B69: :12:10'39 773 :12: 10:39. 934: '12: 10:39.g65d '12:10:40. 137 J1Z:1O:«. 1B6;12:10 :40. 355;12:10:40.464EMWlSA14GS1 DAl4681 DA14&31DA14B81 DAI 4631 DA14G31 DA14681DA1U&.1 DA14&81Ji5r|a在需要

5、發(fā)送數(shù)據(jù)前先喚醍 牆若口 DA 14681.WI! WAIT! If AIT! UA1T! UJCETIDUART i同時(shí),在正常狀態(tài)下的平均功耗也是可以接受的(200多uA是由于程序還 在發(fā)廣播):InfoPealc Current (mA)31 58281Avg Current (mA) 。左了范3 JCharge (uC)4242.90315-Extended n trol接下去我們來(lái)一步步說(shuō)明如何來(lái)添加代碼,基礎(chǔ)代碼我們依舊選擇DA1468x_SDK_BTLE_v_1.0.8.1050的 pxp_reporter 例子,工具為 Smartsnippets Studio v1.4.3.7

6、72。注意,代碼只是教你使用方法,真正應(yīng)用到工程中還需要進(jìn)一步優(yōu)化。一,三線方法(TX,RX,INT首先,我們打開(kāi)Smartsnippets Studio工具,然后導(dǎo)入工程,并添加兩個(gè)空白 文件 uart_task.c 和 uart_task.h:11_f=0 回 Includestonfigb 日 DA145S1 -01- Rel se.QS PIp & Idcripts> C® misu> q> tS startup卜園main.eb 園 pxp_re po rt er_ta sk. c回 sw_yer5ion. IiA 園 U 呂匚| 卜 i,h| y

7、ai_t曰wk,h;"J' initiaLflash.bat這兩個(gè)文件作為我們UART TAS使用,先放著備用接下去,我們?cè)赾ustom_config_qspi.h文件中添加幾個(gè)宏定義:國(guó) uarltask.e 田 mainx custom conFg qspi.Ji#defint dg config.MVPAR ADAPTERKdefint dgonfi&GPADC AOAPTER11Sifdef TalkBT UAR.T ADAPTER TASK TESTdefine dg configJSE Hl-J JAkT1#define dgconfigUWRT ADAP

8、TER1flendif#ifdef TslkBT_UART_ADPTER_TA5K_TE5Tefine dg_configU5E_HWJJART1define dg_configUART_ADAPTER1_ iendif#ifdef TalkBT_UART_ADAPTER_TASK_TEST#define dg_c on figUSE_HW_UART1#define dg_co nfigUART_ADAPTER1#en dif這兩個(gè)定義的作用是打開(kāi) UART ADAPTE功能,因?yàn)槲覀冞@里要用到uart控 制臺(tái)。然后在文件最上面添加以下兩個(gè)定義:1.爲(wèi) uartjtask.c衛(wèi) min.c1

9、日nfiq5pi.h 坯ittfndef CUSTCHCOtJFIG_QSPI_Hitdefine CUSTaa_COIdFIG_Q5PI_Hinclude "b5p_definition5#defire TalkBT_UART_ADAPTEfi_7ASIC_TEST ttdefine 匚OM尸工(i一槍RE.廳Lb¥_DETE£pdefine CONFIG_USE_9LE#defi ne TalkBT_UART_ADAPTER_TASK_TEST#defi ne CONFIG_UART_IGNORE_BUSY_DETECT再找到以下位置,將其修改為零:園 uar

10、tjakiCLd mainiClhh| custom.config.qspi-h 吃dg configlMAGE ETJPDEVELOPMEMTJIODE#deflne dgconfigEWULATE_OTP_COPY(0)#define dgconfigJSERCANUSEIMERl(0)冋嚴(yán)h Controls the ttRAM size ue豐ct by the project.* 0: all R/y4 i£ retained* 1: retention nencry iize is aptim2|ttdefine pr®j configVPTimiJRETRW8

11、) I#if ! defined(;RELEASE_BLHLD)(pro j_confi&OPTIMAL_F<ETRAM 1) 嚴(yán) 14ARNIN-G: retRA/1 optimizations are disabled in DEBIM builds! #undef t>roi confieOPTLW RETRAH#defi ne proj_co nfigOPTIMAL_RETRAM(0)隨后,打開(kāi)main.c添加以下代碼:i c| uart_ta&k.tLH miri.c 圏 |h| cu5tQm_fQnfig_q5pLh#include "bltm

12、gr,h"#include "hw cp<i_h'p ttinclude " hw_gp£cih"#include '*sy5_elorlc_mgr. h" ffimrlude ,Fsy5_power_mgr. h" #include ,Fsys_Latchdog nh"#include Mplatforffi_devices.h,T#i4def Talk盯JAR_ADAPTER_TA5K_TF£T #include 'artask.h" ffemdif#ifdef

13、 TalkBT_UART_ADAPTER_TASK_TEST#in clude "uart_task.h"#en dif然后在systemnit函數(shù)中添加初始化代碼:回 uarttask.c 園 mainx £3 劇 cus+om config qspi.he t_s leepmodefextended sLeep);/ f pm_s 61_£ 1 ee p_n*od e ( pm_iriod e_a ct i ve);/" Initialize BLE rianager */ ble_ingr_inlt ():#ifde-F 7alkBTJJ

14、ART_ADAPTER_TASK:_TE5T duartinitf); createuarttask()#cndi-f嚴(yán) Start the PKP reporter application tsk." OS_TAK_CPEATE("PXP Repo#ifdef TalkBT_UART_ADAPTER_TASK_TESTad_uart_ in it();create_uart_task();#en dif在periphnit函數(shù)中添加串口初始化代碼:|3 Lart_t35kh h clstcspi +1回 Fmj n,c 左 hj plarfonn_dpvke£.

15、h - _J . 二 一 = .童 7= -*hw_gp ro_r UNC_UAnii_tx); hwjp io_i etin fu nction(Hi 3>I0 POItr a f mi fiPIO PlN Sj HhL$Pm_N06fc_ftJ亍WT _HW_GPra_nJI4C_UJRT?_RM);#Hdef TalkST_UAFl7_MAP TEfL_TA£ K_TESThw_gpio_conf±gure_pLn(riUft!_2t 盧吐護(hù)丄出_10_x10Dt_LU PLitt_giV_F'dFiL_30 盯; hw pdo set pin fun=

16、tion(M/ GPIO FORT lt 昭 GPJO PIN 冇 吊 GPJOQUTPiJTt-mu_ sp io_ Fuwc_miTFj_ nQ jhw_§pia_2et-pin.-funzti<jnIT_2p 4WPlN_3t CPIO !OQE WV,沖匹* U/iL_UAftI2_ itjij iHendiF#ifdef TalkBT UART ADAPTER TASK TEST hw gpio co nfigure pi n(HW GPIO PORT 2, HW GPIO PIN 3, HW_GPIO_MODE_OUTPUT, HW_GPIO_FUNC_GPIO,

17、1);HW_GPIO_FUNC_UART2_RX);#en dif除了這里,還有一個(gè)地方需要配置,很明顯,這里是配置UART口的參數(shù)的, 默認(rèn)有UART1和UART2我們這里使用的是UART2并使用其默認(rèn)值:nJ stern| 國(guó) pl迪 main.c -£| pip_rpQrt?r_t3 eIgitincJucJefrifeef _cpluspluilextern F" fSendlf樹(shù) dgconf i gUART ADAP"E RUART BUSCUBTl h CHIALX . HW_UAflT QAUDRArE 山躍MT盟TTS B,帀 MRT PARIT

18、Y 阿Hh_UAFlT_5TQP0:T5_l. 6b &» ttU_QHA_CL_L. hfei_D_CtWJNfL_6乳 町U曲 l_ELi£U=HTl htRXALj HV!_U/.R' _BAUDRAI E_LLS200> l-lJ_JflliT DAABITS_匕上 Hk_LAkT_kH2 ' ,*_r.LUNLJ Hd UART S TOPB lTS_lf&, 1?d_D_CHAWJE 匚器 G/GJU&niif P ci_ccnfigL'AR'_ADAPTt:R f接下去,我們來(lái)編輯最重要的代碼,即u

19、art_task.h和uart_task.c,首先是uart_task.h:c mart taIc.c|.ti uarl takh 淫UART_T/<5K_t1define UART_TA5K_Hinclude <stdbool.h>extern void 匚reate_uar七_(dá)七日01( (woiti) jftendif /* UART_TASK_H_ r/#ifndef UART_TASK_H_ #defi ne UART_TASK_H_#in elude <stdbool.h>exter n void create_uart_task(void);#en

20、dif /* UART_TASK_H_ */頭文件的代碼很簡(jiǎn)單,就是申明了一個(gè)task創(chuàng)建函數(shù),也就是前面systemnit 函數(shù)中所調(diào)用的函數(shù)。接下去是uart_task.c,由于代碼比較長(zhǎng),就不截圖了,直接全部復(fù)制過(guò)來(lái), 相應(yīng)的解釋也直接看代碼的注釋吧:#i nclude<stdbool.h>#i nclude<stri ng.h>#i nclude"osal.h"#i nclude"ble_att.h"#i nclude"ble_commo n.h"#i nclude"ble_gap.h&quo

21、t;#i nclude"ble_gatts.h"#i nclude"ble2cap.h"#i nclude"util/list.h"#i nclude"bas.h"#i nclude"ias.h"#i nclude"lls.h"#i nclude"tps.h"#i nclude"sys_power_mgr.h"#i nclude"sys_socf.h"#i nclude"ad_ nvparam.h"

22、;#i nclude"ad_battery.h"#i nclude"hw_breath.h"#i nclude"hw_gpio.h"#in clude"hw_wkup.h"#i nclude "hw_led.h"#in elude "sys_watchdog.h"#i nclude "platform _n vparam.h"#in clude "pxp_reporter_c on fig.h"#i nclude "ble_s

23、torage.h"#i nclude "platform_devices.h"#in clude "uart_task.h"#define MAX_UART_SIZE 100/定義收發(fā)緩存最大長(zhǎng)度,可根據(jù)需求修改char read_buffMAX_UART_SIZE; /UART 接收數(shù)據(jù)緩存uint32_tread_len=0;/接收到的有效數(shù)據(jù)長(zhǎng)度char write_buffMAX_UART_SIZE; /UART 發(fā)送數(shù)據(jù)緩存uint32_twrite_len=0;/ 發(fā)送數(shù)據(jù)長(zhǎng)度char read_temp; /存放讀到的第一個(gè)數(shù)據(jù)u

24、art_device uart_dev; / 定義 UART設(shè)備bool keep_alive=false;/ 系統(tǒng)當(dāng)前是否為 active 狀態(tài)staticuint8_tactive_loop=0; /這個(gè)用于計(jì)算進(jìn)入休眠的時(shí)間,當(dāng)然也可以改成用定時(shí)器的方式,這樣更準(zhǔn)確bool uart_ready_falg=true;/ 當(dāng)前 uart 是否準(zhǔn)備好標(biāo)志/串口讀取完成回調(diào)函數(shù),每收到一個(gè)字節(jié)系統(tǒng)會(huì)跳轉(zhuǎn)到此函數(shù)staticvoid uart_read_cb (void *user_data,uint16_ttransferred)read_buffread_le n=read_temp;rea

25、d_le n+;if (readen=MAX_UART_SIZE)readen=MAX_UART_SIZE-1;/將當(dāng)前讀到的字節(jié)存放完畢后,在此啟動(dòng)異步讀取 uart數(shù)據(jù),回調(diào)函數(shù)依然為uart_read_cbad_uart_read_as yn c(uart_dev,&read_temp, 1, uart_read_cb, NULL);/串口發(fā)送完成回調(diào)函數(shù)staticvoid uart_write_cb (void *user_data,uint16_ttransferred)write_len=0;/發(fā)送完畢后將write_len 清零/io口中斷函數(shù)staticvoidwku

26、p_ha ndler()hw_wkup_reset_cou nter(); hw_wkup_reset_i nterrupt();if (!keep_alive)10 口中斷產(chǎn)生后,強(qiáng)制不讓系統(tǒng)進(jìn)入休眠keep_alive=true;pm_stay_alive();active_loop=0;/系統(tǒng)在active 狀態(tài)下不在需要10 口中斷,故暫時(shí)關(guān)閉中斷hw_wkup_co nfigure_p in(HW_GPI0_P0RT_1 HW_GPI0_PIN_6,0, HW_WKUP_PIN_STATE_LOW uart_ready_falg=true;/解除“強(qiáng)制不讓系統(tǒng)進(jìn)入休眠“函數(shù)stati

27、cvoid set_sys_mode_sleep ad_uart_write(uart_dev,/重新注冊(cè)I0 口中斷hw_wkup_c on figure_p in(void )"chip sleepn", 11);HW_GPI0_P0RT_1HW_GPI0_PIN_6,1, HW_WKUP_PIN_STATE_L0W if (keep_alive)/允許系統(tǒng)進(jìn)入休眠keep_alive=false; pm_resume_sleep();* 函數(shù)名稱(chēng):task_uart_func*描述:uart task 函數(shù)* 輸入:*pvParameters*輸岀:None*返回:N

28、one*/staticvoidtask_uart_fu nc(void *pvParameters)#define MAX_ACTIVE_L00P 100/ 定義進(jìn)入休眠時(shí)間 ,這里 100*0S_DELAY_MS(50)=5 秒/以下注冊(cè)P16為低電平中斷HW_GPI0_P0RT_1 HW_GPI0_PIN_6,hw_wkup_i nit(NULL); hw_gpio_set_p in_fun cti on(HW_GPI0_M0DE_INPUT_PULLUPHW_GPI0_FUNC_GPI0;hw_wkup_c on figure_pi n(HW_GPIO_PORT_1 HW_GPIO_PI

29、N_6,1, HW_WKUP_PIN_STATE_LOWhw_wkup_set_cou nter_threshold(1);hw_wkup_set_debou nce_time(10);hw_wkup_register_ in terrupt(wkup_ha ndler, 1);uart_dev = ad_uart_open(SERIAL2);/ 打開(kāi)串口 devicead_uart_read_async(uart_dev,&read_temp, 1, uart_read_cb, NULL);/ 設(shè)置讀取一個(gè)字節(jié)for (;)/ad_uart_bus_acquire(uart_dev)

30、;/告訴上位機(jī)UART已經(jīng)準(zhǔn)備好if (uart_ready_falg)uart_ready_falg=false;ad_uart_write(uart_dev,"chip activen",12);if (read_len>0 && write_len=0)/如果檢測(cè)到讀到的數(shù)據(jù)不為零,并且當(dāng)前正沒(méi)有數(shù)據(jù)在發(fā)送,則往UART口發(fā)送已收到的數(shù)據(jù)memcpy(write_buff,read_buff,read_le n);write_le n=read_le n;read_le n=0;ad_uart_write_as yn c(uart_dev,wri

31、te_buff,write_le n,uart_write_cb, NULL);active_loop=0;else /如果RX沒(méi)有收到數(shù)據(jù),則進(jìn)入休眠倒計(jì)時(shí)if (active_loop < MAX_ACTIVE_LOOP)active_loop+;if (active_loop = MAX_ACTIVE_LOOP)set_sys_mode_sleep();/設(shè)置系統(tǒng)允許進(jìn)入休眠ad_uart_bus_release(uart_dev);OS_DELAY_MS(50);ad_uart_close(uart_dev);* 函數(shù)名稱(chēng):create_uart_task*描述:創(chuàng)建uart t

32、ask*輸入:None*輸岀:None*返回:None*/(void )void create_uart_taskOS_TASK han dle;OS_TASK_CREATE( "uart_task" , the task, fordebugkern el. */task_uart_fu nc,task. */(void * ) 0,task. */512,OS_TASK_PRIORITY_NORMAL,task. */han dle);OS_ASSERT(ha ndle);/* The text n ame assig ned toonly; not used by th

33、e/* The fun cti on that impleme nts the/* The parameter passed to the/* The priority assigned to the/* The task han dle. */代碼如果看不清楚,可以新建一個(gè) c文件,然后拷貝進(jìn)去,用編輯器查看即 可。代碼中的邏輯大致是這樣:串口引腳為 P13(TX),P23(RX)喚醒引腳為P16低電平喚醒;正常工作模式(保持active狀態(tài))串口監(jiān)聽(tīng)RX管腳數(shù)據(jù)一旦收到數(shù)據(jù)通過(guò)TX 管腳將數(shù)據(jù)回發(fā);如果5秒鐘RX沒(méi)有收到數(shù)據(jù),則進(jìn)入休眠狀態(tài);如果需要發(fā)送數(shù)據(jù),則首選需要將P16拉低(給一個(gè)

34、下降沿脈沖即可,不需要全 程拉低),系統(tǒng)會(huì)重新進(jìn)入active狀態(tài)。隨后,我們編譯測(cè)試:tr tr - . n-runr jr* r-13:29:50. 206 收+ chij ac t ive:15:29:53. 125:13:2953 15613:29:53.827:l:29:53.83O:1;29;5A,576:13:29:54.60T: 13:29:59.459is總lib MU&SL mT!C *-Mello DA146C1 VAET! -+OHtllo DA146i UAET! 口 h*ho dak6«i mi!-OHsIIqUAHT! 口Hsll & D

35、AI 461 UUIT!*令uhi,l:30'13:3013:3015:3002:. 30002.571'03.495'06. 10B'2-OHello RA146C1 mi! 克亠0矗11。HA14BC1 VAETIC OM&no DA146C1 irmin13:30:07,61日13:30:07.682i3:3D:oa.zeo13:30:08.30613:30:Oa.-94513:30:08.992!-+OHtllo-OHello Hsll Ortello He HoW4&C1DA146S1DAK6C1DA146S1DA146S1HA146S

36、1UAET!口j能正常收發(fā)VUIT!iruiTinITAET!上電后系統(tǒng)處于目dtivm狀態(tài)"收發(fā) 正常系統(tǒng)休眠后,收發(fā)不正常拉低 P1® 喚 0IDA14681補(bǔ)充一點(diǎn),如果編譯結(jié)果出現(xiàn)以下報(bào)錯(cuò):Desch ptionResourceJ G Errors (2 items£3 Symbol 'SER1AL2' could not be resolved uart_task.c''J Type 'uart.device' could not be resolved uartjtak.c請(qǐng)做以下操作,再編譯:,兩線方法

37、(RX,TX當(dāng)然,以上做法有一個(gè)明顯的缺陷,就是需要額外的一個(gè)io 口(P16),這樣會(huì)有局限性,如果能只用 RX和TX實(shí)現(xiàn)此功能,那豈不快哉。所以,接下去我們來(lái)用代碼實(shí)現(xiàn),先講一下大致邏輯:串口引腳為 P13(TX) P23(RX)正常工作模式(保持active狀態(tài))串口監(jiān)聽(tīng)RX管腳數(shù)據(jù),一旦收到數(shù)據(jù)通過(guò)TX 管腳將數(shù)據(jù)回發(fā);如果5秒鐘RX沒(méi)有收到數(shù)據(jù),則進(jìn)入休眠狀態(tài),進(jìn)入休眠狀態(tài)之前把 RX口 設(shè)置為普通10 口和低電平中斷;如果需要發(fā)送數(shù)據(jù),上位機(jī)需要通過(guò)UART口給DA14681發(fā)送一個(gè)數(shù)據(jù)(這邊 測(cè)試發(fā)送十六進(jìn)制的00 00,能夠喚醒),由于RX口設(shè)置成了低電平中斷,一旦 DA146

38、81被喚醒后,在中斷函數(shù)中會(huì)關(guān)閉 RX喚醒功能并且重新配置為 UART功 能,隨后DA14681被設(shè)置為active狀態(tài),并向UART發(fā)送"chip active"以通知上位 機(jī)UART已經(jīng)能正常工作。代碼的話,只需要在先前三線代碼基礎(chǔ)上改動(dòng)以下幾個(gè)地方即可:將static void wkup_handler()函數(shù)中的以下代碼刪除:/系統(tǒng)在active狀態(tài)下不在需要IO 口中斷,故暫時(shí)關(guān)閉中斷hw_wkup_co nfigure_p in (HW_GPIO_PORT_1, HW_GPIO_PIN_6,0,HW_WKUP_PIN_STATE_LOW);并換成:/系統(tǒng)在act

39、ive狀態(tài)下,重新配置P23為RX端口,以便接收數(shù)據(jù)hw_wkup_co nfigure_pi n(HW_GPIO_PORT_2, HW_GPIO_PIN_3,0,HW_WKUP_PIN_STATE_LOW);hw gpio set pin function(HW GPIO PORT 2, HW GPIO PIN 3,HWGPIO MODE INPUTHW_GPIO_FUNC_UART2_RX);然后將static void set_sys_mode_sleep(void函數(shù)中的以下代碼刪除:7重新注冊(cè)IO 口中斷hw wkup con figure pi n(HW GPIO PORT 1,

40、HW GPIO PIN 6,1,HW WKUP PIN STATELOW);并換成:/配置P23(RX)口為普通IO 口并且配置為低電平觸發(fā)中斷hw_wkup_i nit(NULL);hw_gpio_set_pin_fu nctio n(HW_GPIO_PORT_2, HW_GPIO_PIN_3,HW_GPIO_MODE_INPUT_PULLU,PHW_GPIO_FUNC_GPIO);hw_wkup_co nfigure_pi n(HW_GPIO_PORT_2, HW_GPIO_PIN_3,1,HW_WKUP_PIN_STATE_LOW);hw_wkup_set_co un ter_thres

41、hold(1);hw_wkup_set_debo un ce_time(0);hw_wkup_register_ in terrupt(wkup_ha ndler, 1);最后刪除以下代碼:static void task_uart_func(void pvParameters) -#dcfine MAX_ACTIVE_La)P 10G “定義進(jìn)入依眠時(shí)間,10e*OS_JELAY_M&(5e)-5秒/F以下注低電平中師h_wkLfp_init(NUL_);hw_gp io set_p in f u n c t io n ( HVJj PIO PGR T_1, HW GPIO PIN

42、fiP 1D_MOOE_INPUT_PU 1_HW_GP IO_FUMC_C PIO);hw_wkup.®nfigure_pin(HW_CP工。-切前_ 1HW_GPIOPIM_S, 1Hk_WICUP_PIIM_5TATE_0W); hw_wk up_set_cg u n te r_t h re s ho1d(1);hw_wkup_set_debounce_timetl®);hw_Hkup_register_inte rrupt(wlcup-h5rcller» 1) j|uart_dev ad_uart_open(SERIAL2);/tT開(kāi)串口device ad

43、jjart_read_async(uart_devJiread_teffipJ 1, d3rt_read_cb, NULL);.'/設(shè)置謨?nèi)∫籪 寶節(jié) forf為了方便,還是將uart_task.c的內(nèi)容貼出來(lái):#i nclude<stdbool.h>#i nclude<stri ng.h>#i nclude"osal.h"#i nclude"ble_att.h"#i nclude"ble_commo n.h"#i nclude"ble_gap.h"#i nclude"bl

44、e_gatts.h"#i nclude"ble2cap.h"#i nclude"util/list.h"#i nclude"bas.h"#i nclude"ias.h"#i nclude"lls.h"#i nclude"tps.h"#i nclude"sys_power_mgr.h"#i nclude"sys_socf.h"#i nclude"ad_ nvparam.h"#i nclude"ad_

45、battery.h"#i nclude"hw_breath.h"#i nclude"hw_gpio.h"#i nclude"hw_wkup.h"#i nclude"hw_led.h"#i nclude"sys_watchdog.h"#i nclude"platform _n vparam.h"#i nclude"pxp_reporter_con fig.h"#i nclude"ble_storage.h"#i nclude&q

46、uot;platform_devices.h"#in clude"uart task.h"#define MAX_UART_SIZE 100/定義收發(fā)緩存最大長(zhǎng)度,可根據(jù)需求修改char read_buffMAX_UART_SIZE; /UART 接收數(shù)據(jù)緩存uint32_tread_len=0;/接收到的有效數(shù)據(jù)長(zhǎng)度char write_buffMAX_UART_SIZE; /UART 發(fā)送數(shù)據(jù)緩存uint32_twrite_len=0;/ 發(fā)送數(shù)據(jù)長(zhǎng)度char read_temp; /存放讀到的第一個(gè)數(shù)據(jù)uart_device uart_dev; / 定義 U

47、ART設(shè)備bool keep_alive=false;/ 系統(tǒng)當(dāng)前是否為 active 狀態(tài)static uint8_t active_loop=0; /這個(gè)用于計(jì)算進(jìn)入休眠的時(shí)間,當(dāng)然也可以改成用定時(shí)器的方式,這樣更準(zhǔn)確bool uart_ready_falg=true;/ 當(dāng)前 uart 是否準(zhǔn)備好標(biāo)志/串口讀取完成回調(diào)函數(shù),每收到一個(gè)字節(jié)系統(tǒng)會(huì)跳轉(zhuǎn)到此函數(shù)staticvoid uart_read_cb (void *user_data, uint16_t transferred)read_buffread_le n=read_temp;read_le n+;if (readen=MAX_

48、UART_SIZE)readen=MAX_UART_SIZE-1;/將當(dāng)前讀到的字節(jié)存放完畢后,在此啟動(dòng)異步讀取 uart數(shù)據(jù),回調(diào)函數(shù)依然為uart_read_cbad_uart_read_as yn c(uart_dev,&read_temp, 1, uart_read_cb, NULL);/串口發(fā)送完成回調(diào)函數(shù)staticvoid uart_write_cb (void *user_data,uint16_ttransferred)write_len=0;/發(fā)送完畢后將write_len 清零/中斷函數(shù)staticvoid wkup_ha ndler ()hw_wkup_rese

49、t_cou nter();hw_wkup_reset_i nterrupt();if (!keep_alive)/IO 口中斷產(chǎn)生后,強(qiáng)制不讓系統(tǒng)進(jìn)入休眠keep_alive=true;pm_stay_alive();active_loop=0;/系統(tǒng)在active 狀態(tài)下,重新配置P23為RX端口,以便接收數(shù)據(jù)hw_wkup_configure_pin(HW_GPIO_PORT_2 HW_GPIO_PIN_3,0, HW_WKUP_PIN_STATE_LOWHW_GPIO_PORT_2 HW_GPIO_PIN_3,hw_gpio_set_p in_fun cti on(HW_GPIO_MOD

50、E_INPUTHW_GPI0_FUNC_UART2_RXuart_ready_falg=true;staticvoid set_sys_mode_sleep (void ) ad_uart_write(uart_dev."chip sleepn", 11);/配置P23(RX) 口為普通IO 口并且配置為低電平觸發(fā)中斷hw_wkup_i nit(NULL);hw_gpio_set_p in_fun cti on(HW_GPIO_PORT_2 HW_GPIO_PIN_3,HW_GPIO_MODE_INPUT_PULLUPHW_GPIO_FUNC_GPIO;hw_wkup_co

51、nfigure_pin(HW_GPIO_PORT_2 HW_GPIO_PIN_3,1, HW_WKUP_PIN_STATE_LOW hw_wkup_set_cou nter_threshold(1); hw_wkup_set_debou nce_time(0);hw_wkup_register_ in terrupt(wkup_ha ndler, 1);if (keep_alive)/允許系統(tǒng)進(jìn)入休眠keep_alive=false; pm_resume_sleep();/* 函數(shù)名稱(chēng):task_uart_func*描述:uart task 函數(shù)* 輸入:*pvParameters*輸岀:No

52、ne*返回:None*/staticvoidtask_uart_fu nc(void *pvParameters)#define MAX_ACTIVE_LOOP 100/ 定義進(jìn)入休眠時(shí)間 ,這里 1OO*OS_DELAY_MS(5O)=5 秒/打開(kāi)串口 deviceuart_dev = ad_uart_ope n(SERIAL2);ad_uart_read_async(uart_dev,&read_temp, 1, uart_read_cb, NULL);/ 設(shè)置讀取一個(gè)字節(jié)for (;)ad_uart_bus_acquire(uart_dev);/告訴上位機(jī)UART已經(jīng)準(zhǔn)備好if (uart_ready_falg)uart_ready_falg=false;ad_uart_write(uart_dev,"chip activen",12);if (read_len>0 && write_len=0)/如果檢測(cè)到讀到的數(shù)據(jù)不為零,并且當(dāng)前正沒(méi)有數(shù)據(jù)在發(fā)送,則往UART口發(fā)送已收到的數(shù)據(jù)memcpy(write_buff,read_buff,read_le n);write_le n=read_le n;read_le n=0;ad_uart_write_as yn c(uart_dev,write_buff,wr

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論