wifi.rst 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224
  1. Wi-Fi Driver
  2. =============
  3. :link_to_translation:`zh_CN:[中文]`
  4. {IDF_TARGET_NAME} Wi-Fi Feature List
  5. ------------------------------------
  6. The following features are supported:
  7. .. only:: esp32 or esp32s2 or esp32c3 or esp32s3
  8. - 4 virtual Wi-Fi interfaces, which are STA, AP, Sniffer and reserved.
  9. - Station-only mode, AP-only mode, station/AP-coexistence mode
  10. - IEEE 802.11b, IEEE 802.11g, IEEE 802.11n, and APIs to configure the protocol mode
  11. - WPA/WPA2/WPA3/WPA2-Enterprise/WPA3-Enterprise/WAPI/WPS and DPP
  12. - AMSDU, AMPDU, HT40, QoS, and other key features
  13. - Modem-sleep
  14. - The Espressif-specific ESP-NOW protocol and Long Range mode, which supports up to **1 km** of data traffic
  15. - Up to 20 MBit/s TCP throughput and 30 MBit/s UDP throughput over the air
  16. - Sniffer
  17. - Both fast scan and all-channel scan
  18. - Multiple antennas
  19. - Channel state information
  20. .. only:: esp32c2
  21. - 3 virtual Wi-Fi interfaces, which are STA, AP and Sniffer.
  22. - Station-only mode, AP-only mode, station/AP-coexistence mode
  23. - IEEE 802.11b, IEEE 802.11g, IEEE 802.11n, and APIs to configure the protocol mode
  24. - WPA/WPA2/WPA3/WPA2-Enterprise/WPA3-Enterprise/WPS and DPP
  25. - AMSDU, AMPDU, QoS, and other key features
  26. - Modem-sleep
  27. - Up to 20 MBit/s TCP throughput and 30 MBit/s UDP throughput over the air
  28. - Sniffer
  29. - Both fast scan and all-channel scan
  30. - Multiple antennas
  31. How To Write a Wi-Fi Application
  32. ----------------------------------
  33. Preparation
  34. +++++++++++
  35. Generally, the most effective way to begin your own Wi-Fi application is to select an example which is similar to your own application, and port the useful part into your project. It is not a MUST, but it is strongly recommended that you take some time to read this article first, especially if you want to program a robust Wi-Fi application.
  36. This article is supplementary to the Wi-Fi APIs/Examples. It describes the principles of using the Wi-Fi APIs, the limitations of the current Wi-Fi API implementation, and the most common pitfalls in using Wi-Fi. This article also reveals some design details of the Wi-Fi driver. We recommend you to select an :example:`example <wifi>`.
  37. Setting Wi-Fi Compile-time Options
  38. ++++++++++++++++++++++++++++++++++++
  39. Refer to `Wi-Fi Menuconfig`_.
  40. Init Wi-Fi
  41. +++++++++++
  42. Refer to `{IDF_TARGET_NAME} Wi-Fi station General Scenario`_ and `{IDF_TARGET_NAME} Wi-Fi AP General Scenario`_.
  43. Start/Connect Wi-Fi
  44. ++++++++++++++++++++
  45. Refer to `{IDF_TARGET_NAME} Wi-Fi station General Scenario`_ and `{IDF_TARGET_NAME} Wi-Fi AP General Scenario`_.
  46. Event-Handling
  47. ++++++++++++++
  48. Generally, it is easy to write code in "sunny-day" scenarios, such as `WIFI_EVENT_STA_START`_ and `WIFI_EVENT_STA_CONNECTED`_. The hard part is to write routines in "rainy-day" scenarios, such as `WIFI_EVENT_STA_DISCONNECTED`_. Good handling of "rainy-day" scenarios is fundamental to robust Wi-Fi applications. Refer to `{IDF_TARGET_NAME} Wi-Fi Event Description`_, `{IDF_TARGET_NAME} Wi-Fi station General Scenario`_, and `{IDF_TARGET_NAME} Wi-Fi AP General Scenario`_. See also :doc:`an overview of event handling in ESP-IDF<event-handling>`.
  49. Write Error-Recovery Routines Correctly at All Times
  50. ++++++++++++++++++++++++++++++++++++++++++++++++++++
  51. Just like the handling of "rainy-day" scenarios, a good error-recovery routine is also fundamental to robust Wi-Fi applications. Refer to `{IDF_TARGET_NAME} Wi-Fi API Error Code`_.
  52. {IDF_TARGET_NAME} Wi-Fi API Error Code
  53. --------------------------------------
  54. All of the {IDF_TARGET_NAME} Wi-Fi APIs have well-defined return values, namely, the error code. The error code can be categorized into:
  55. - No errors, e.g., ESP_OK means that the API returns successfully.
  56. - Recoverable errors, such as ESP_ERR_NO_MEM.
  57. - Non-recoverable, non-critical errors.
  58. - Non-recoverable, critical errors.
  59. Whether the error is critical or not depends on the API and the application scenario, and it is defined by the API user.
  60. **The primary principle to write a robust application with Wi-Fi API is to always check the error code and write the error-handling code.** Generally, the error-handling code can be used:
  61. - For recoverable errors, in which case you can write a recoverable-error code. For example, when :cpp:func:`esp_wifi_start()` returns ESP_ERR_NO_MEM, the recoverable-error code vTaskDelay can be called in order to get a microseconds' delay for another try.
  62. - For non-recoverable, yet non-critical errors, in which case printing the error code is a good method for error handling.
  63. - For non-recoverable and also critical errors, in which case "assert" may be a good method for error handling. For example, if :cpp:func:`esp_wifi_set_mode()` returns ESP_ERR_WIFI_NOT_INIT, it means that the Wi-Fi driver is not initialized by :cpp:func:`esp_wifi_init()` successfully. You can detect this kind of error very quickly in the application development phase.
  64. In esp_err.h, ESP_ERROR_CHECK checks the return values. It is a rather commonplace error-handling code and can be used as the default error-handling code in the application development phase. However, it is strongly recommended that API users write their own error-handling code.
  65. {IDF_TARGET_NAME} Wi-Fi API Parameter Initialization
  66. ----------------------------------------------------
  67. When initializing struct parameters for the API, one of two approaches should be followed:
  68. - Explicitly set all fields of the parameter.
  69. - Use get API to get current configuration first, then set application specific fields.
  70. Initializing or getting the entire structure is very important, because most of the time the value 0 indicates that the default value is used. More fields may be added to the struct in the future and initializing these to zero ensures the application will still work correctly after ESP-IDF is updated to a new release.
  71. .. _wifi-programming-model:
  72. {IDF_TARGET_NAME} Wi-Fi Programming Model
  73. -----------------------------------------
  74. The {IDF_TARGET_NAME} Wi-Fi programming model is depicted as follows:
  75. .. blockdiag::
  76. :caption: Wi-Fi Programming Model
  77. :align: center
  78. blockdiag wifi-programming-model {
  79. # global attributes
  80. node_height = 60;
  81. node_width = 100;
  82. span_width = 100;
  83. span_height = 60;
  84. default_shape = roundedbox;
  85. default_group_color = none;
  86. # node labels
  87. TCP_STACK [label="TCP\n stack", fontsize=12];
  88. EVNT_TASK [label="Event\n task", fontsize=12];
  89. APPL_TASK [label="Application\n task", width = 120, fontsize=12];
  90. WIFI_DRV [label="Wi-Fi\n Driver", width = 120, fontsize=12];
  91. KNOT [shape=none];
  92. # node connections + labels
  93. TCP_STACK -> EVNT_TASK [label=event];
  94. EVNT_TASK -> APPL_TASK [label="callback\n or event"];
  95. # arrange nodes vertically
  96. group {
  97. label = "default handler";
  98. orientation = portrait;
  99. EVNT_TASK <- WIFI_DRV [label=event];
  100. }
  101. # intermediate node
  102. group {
  103. label = "user handler";
  104. orientation = portrait;
  105. APPL_TASK -- KNOT;
  106. }
  107. WIFI_DRV <- KNOT [label="API\n call"];
  108. }
  109. The Wi-Fi driver can be considered a black box that knows nothing about high-layer code, such as the TCP/IP stack, application task, and event task. The application task (code) generally calls :doc:`Wi-Fi driver APIs <../api-reference/network/esp_wifi>` to initialize Wi-Fi and handles Wi-Fi events when necessary. Wi-Fi driver receives API calls, handles them, and posts events to the application.
  110. Wi-Fi event handling is based on the :doc:`esp_event library <../api-reference/system/esp_event>`. Events are sent by the Wi-Fi driver to the :ref:`default event loop <esp-event-default-loops>`. Application may handle these events in callbacks registered using :cpp:func:`esp_event_handler_register()`. Wi-Fi events are also handled by :doc:`esp_netif component <../api-reference/network/esp_netif>` to provide a set of default behaviors. For example, when Wi-Fi station connects to an AP, esp_netif will automatically start the DHCP client by default.
  111. {IDF_TARGET_NAME} Wi-Fi Event Description
  112. -----------------------------------------
  113. WIFI_EVENT_WIFI_READY
  114. ++++++++++++++++++++++++++++++++++++
  115. The Wi-Fi driver will never generate this event, which, as a result, can be ignored by the application event callback. This event may be removed in future releases.
  116. WIFI_EVENT_SCAN_DONE
  117. ++++++++++++++++++++++++++++++++++++
  118. The scan-done event is triggered by :cpp:func:`esp_wifi_scan_start()` and will arise in the following scenarios:
  119. - The scan is completed, e.g., the target AP is found successfully, or all channels have been scanned.
  120. - The scan is stopped by :cpp:func:`esp_wifi_scan_stop()`.
  121. - The :cpp:func:`esp_wifi_scan_start()` is called before the scan is completed. A new scan will override the current scan and a scan-done event will be generated.
  122. The scan-done event will not arise in the following scenarios:
  123. - It is a blocked scan.
  124. - The scan is caused by :cpp:func:`esp_wifi_connect()`.
  125. Upon receiving this event, the event task does nothing. The application event callback needs to call :cpp:func:`esp_wifi_scan_get_ap_num()` and :cpp:func:`esp_wifi_scan_get_ap_records()` to fetch the scanned AP list and trigger the Wi-Fi driver to free the internal memory which is allocated during the scan **(do not forget to do this!)**.
  126. Refer to `{IDF_TARGET_NAME} Wi-Fi Scan`_ for a more detailed description.
  127. WIFI_EVENT_STA_START
  128. ++++++++++++++++++++++++++++++++++++
  129. If :cpp:func:`esp_wifi_start()` returns ESP_OK and the current Wi-Fi mode is station or station/AP, then this event will arise. Upon receiving this event, the event task will initialize the LwIP network interface (netif). Generally, the application event callback needs to call :cpp:func:`esp_wifi_connect()` to connect to the configured AP.
  130. WIFI_EVENT_STA_STOP
  131. ++++++++++++++++++++++++++++++++++++
  132. If :cpp:func:`esp_wifi_stop()` returns ESP_OK and the current Wi-Fi mode is station or station/AP, then this event will arise. Upon receiving this event, the event task will release the station's IP address, stop the DHCP client, remove TCP/UDP-related connections, and clear the LwIP station netif, etc. The application event callback generally does not need to do anything.
  133. WIFI_EVENT_STA_CONNECTED
  134. ++++++++++++++++++++++++++++++++++++
  135. If :cpp:func:`esp_wifi_connect()` returns ESP_OK and the station successfully connects to the target AP, the connection event will arise. Upon receiving this event, the event task starts the DHCP client and begins the DHCP process of getting the IP address. Then, the Wi-Fi driver is ready for sending and receiving data. This moment is good for beginning the application work, provided that the application does not depend on LwIP, namely the IP address. However, if the application is LwIP-based, then you need to wait until the *got ip* event comes in.
  136. WIFI_EVENT_STA_DISCONNECTED
  137. ++++++++++++++++++++++++++++++++++++
  138. This event can be generated in the following scenarios:
  139. - When :cpp:func:`esp_wifi_disconnect()` or :cpp:func:`esp_wifi_stop()` is called and the station is already connected to the AP.
  140. - When :cpp:func:`esp_wifi_connect()` is called, but the Wi-Fi driver fails to set up a connection with the AP due to certain reasons, e.g., the scan fails to find the target AP or the authentication times out. If there are more than one AP with the same SSID, the disconnected event will be raised after the station fails to connect all of the found APs.
  141. - When the Wi-Fi connection is disrupted because of specific reasons, e.g., the station continuously loses N beacons, the AP kicks off the station, or the AP's authentication mode is changed.
  142. Upon receiving this event, the default behaviors of the event task are:
  143. - Shutting down the station's LwIP netif.
  144. - Notifying the LwIP task to clear the UDP/TCP connections which cause the wrong status to all sockets. For socket-based applications, the application callback can choose to close all sockets and re-create them, if necessary, upon receiving this event.
  145. The most common event handle code for this event in application is to call :cpp:func:`esp_wifi_connect()` to reconnect the Wi-Fi. However, if the event is raised because :cpp:func:`esp_wifi_disconnect()` is called, the application should not call :cpp:func:`esp_wifi_connect()` to reconnect. It is the application's responsibility to distinguish whether the event is caused by :cpp:func:`esp_wifi_disconnect()` or other reasons. Sometimes a better reconnection strategy is required. Refer to `Wi-Fi Reconnect`_ and `Scan When Wi-Fi Is Connecting`_.
  146. Another thing that deserves attention is that the default behavior of LwIP is to abort all TCP socket connections on receiving the disconnect. In mose cases, it is not a problem. However, for some special applications, this may not be what they want. Consider the following scenarios:
  147. - The application creates a TCP connection to maintain the application-level keep-alive data that is sent out every 60 seconds.
  148. - Due to certain reasons, the Wi-Fi connection is cut off, and the `WIFI_EVENT_STA_DISCONNECTED`_ is raised. According to the current implementation, all TCP connections will be removed and the keep-alive socket will be in a wrong status. However, since the application designer believes that the network layer should **ignore** this error at the Wi-Fi layer, the application does not close the socket.
  149. - Five seconds later, the Wi-Fi connection is restored because :cpp:func:`esp_wifi_connect()` is called in the application event callback function. **Moreover, the station connects to the same AP and gets the same IPV4 address as before**.
  150. - Sixty seconds later, when the application sends out data with the keep-alive socket, the socket returns an error and the application closes the socket and re-creates it when necessary.
  151. In above scenarios, ideally, the application sockets and the network layer should not be affected, since the Wi-Fi connection only fails temporarily and recovers very quickly. The application can enable "Keep TCP connections when IP changed" via LwIP menuconfig.
  152. IP_EVENT_STA_GOT_IP
  153. ++++++++++++++++++++++++++++++++++++
  154. This event arises when the DHCP client successfully gets the IPV4 address from the DHCP server, or when the IPV4 address is changed. The event means that everything is ready and the application can begin its tasks (e.g., creating sockets).
  155. The IPV4 may be changed because of the following reasons:
  156. - The DHCP client fails to renew/rebind the IPV4 address, and the station's IPV4 is reset to 0.
  157. - The DHCP client rebinds to a different address.
  158. - The static-configured IPV4 address is changed.
  159. Whether the IPV4 address is changed or not is indicated by the field ``ip_change`` of ``ip_event_got_ip_t``.
  160. The socket is based on the IPV4 address, which means that, if the IPV4 changes, all sockets relating to this IPV4 will become abnormal. Upon receiving this event, the application needs to close all sockets and recreate the application when the IPV4 changes to a valid one.
  161. IP_EVENT_GOT_IP6
  162. ++++++++++++++++++++++++++++++++++++
  163. This event arises when the IPV6 SLAAC support auto-configures an address for the {IDF_TARGET_NAME}, or when this address changes. The event means that everything is ready and the application can begin its tasks, e.g., creating sockets.
  164. IP_EVENT_STA_LOST_IP
  165. ++++++++++++++++++++++++++++++++++++
  166. This event arises when the IPV4 address becomes invalid.
  167. IP_EVENT_STA_LOST_IP does not arise immediately after the Wi-Fi disconnects. Instead, it starts an IPV4 address lost timer. If the IPV4 address is got before ip lost timer expires, IP_EVENT_STA_LOST_IP does not happen. Otherwise, the event arises when the IPV4 address lost timer expires.
  168. Generally, the application can ignore this event, because it is just a debug event to inform that the IPV4 address is lost.
  169. WIFI_EVENT_AP_START
  170. ++++++++++++++++++++++++++++++++++++
  171. Similar to `WIFI_EVENT_STA_START`_.
  172. WIFI_EVENT_AP_STOP
  173. ++++++++++++++++++++++++++++++++++++
  174. Similar to `WIFI_EVENT_STA_STOP`_.
  175. WIFI_EVENT_AP_STACONNECTED
  176. ++++++++++++++++++++++++++++++++++++
  177. Every time a station is connected to {IDF_TARGET_NAME} AP, the `WIFI_EVENT_AP_STACONNECTED`_ will arise. Upon receiving this event, the event task will do nothing, and the application callback can also ignore it. However, you may want to do something, for example, to get the info of the connected STA.
  178. WIFI_EVENT_AP_STADISCONNECTED
  179. ++++++++++++++++++++++++++++++++++++
  180. This event can happen in the following scenarios:
  181. - The application calls :cpp:func:`esp_wifi_disconnect()`, or :cpp:func:`esp_wifi_deauth_sta()`, to manually disconnect the station.
  182. - The Wi-Fi driver kicks off the station, e.g., because the AP has not received any packets in the past five minutes. The time can be modified by :cpp:func:`esp_wifi_set_inactive_time()`.
  183. - The station kicks off the AP.
  184. When this event happens, the event task will do nothing, but the application event callback needs to do something, e.g., close the socket which is related to this station.
  185. WIFI_EVENT_AP_PROBEREQRECVED
  186. ++++++++++++++++++++++++++++++++++++
  187. This event is disabled by default. The application can enable it via API :cpp:func:`esp_wifi_set_event_mask()`.
  188. When this event is enabled, it will be raised each time the AP receives a probe request.
  189. WIFI_EVENT_STA_BEACON_TIMEOUT
  190. ++++++++++++++++++++++++++++++++++++
  191. If the station does not receive the beacon of the connected AP within the inactive time, the beacon timeout happens, the `WIFI_EVENT_STA_BEACON_TIMEOUT`_ will arise. The application can set inactive time via API :cpp:func:`esp_wifi_set_inactive_time()`.
  192. {IDF_TARGET_NAME} Wi-Fi Station General Scenario
  193. ------------------------------------------------
  194. Below is a "big scenario" which describes some small scenarios in station mode:
  195. .. seqdiag::
  196. :caption: Sample Wi-Fi Event Scenarios in Station Mode
  197. :align: center
  198. seqdiag sample-scenarios-station-mode {
  199. activation = none;
  200. node_width = 80;
  201. node_height = 60;
  202. edge_length = 140;
  203. span_height = 5;
  204. default_shape = roundedbox;
  205. default_fontsize = 12;
  206. MAIN_TASK [label = "Main\ntask"];
  207. APP_TASK [label = "App\ntask"];
  208. EVENT_TASK [label = "Event\ntask"];
  209. LwIP_TASK [label = "LwIP\ntask"];
  210. WIFI_TASK [label = "Wi-Fi\ntask"];
  211. === 1. Init Phase ===
  212. MAIN_TASK -> LwIP_TASK [label="1.1> Create / init LwIP"];
  213. MAIN_TASK -> EVENT_TASK [label="1.2> Create / init event"];
  214. MAIN_TASK -> WIFI_TASK [label="1.3> Create / init Wi-Fi"];
  215. MAIN_TASK -> APP_TASK [label="1.4> Create app task"];
  216. === 2. Configure Phase ===
  217. MAIN_TASK -> WIFI_TASK [label="2> Configure Wi-Fi"];
  218. === 3. Start Phase ===
  219. MAIN_TASK -> WIFI_TASK [label="3.1> Start Wi-Fi"];
  220. EVENT_TASK <- WIFI_TASK [label="3.2> WIFI_EVENT_STA_START"];
  221. APP_TASK <- EVENT_TASK [label="3.3> WIFI_EVENT_STA_START"];
  222. === 4. Connect Phase ===
  223. APP_TASK -> WIFI_TASK [label="4.1> Connect Wi-Fi"];
  224. EVENT_TASK <- WIFI_TASK [label="4.2> WIFI_EVENT_STA_CONNECTED"];
  225. APP_TASK <- EVENT_TASK [label="4.3> WIFI_EVENT_STA_CONNECTED"];
  226. === 5. Got IP Phase ===
  227. EVENT_TASK -> LwIP_TASK [label="5.1> Start DHCP client"];
  228. EVENT_TASK <- LwIP_TASK [label="5.2> IP_EVENT_STA_GOT_IP"];
  229. APP_TASK <- EVENT_TASK [label="5.3> IP_EVENT_STA_GOT_IP"];
  230. APP_TASK -> APP_TASK [label="5.4> socket related init"];
  231. === 6. Disconnect Phase ===
  232. EVENT_TASK <- WIFI_TASK [label="6.1> WIFI_EVENT_STA_DISCONNECTED"];
  233. APP_TASK <- EVENT_TASK [label="6.2> WIFI_EVENT_STA_DISCONNECTED"];
  234. APP_TASK -> APP_TASK [label="6.3> disconnect handling"];
  235. === 7. IP Change Phase ===
  236. EVENT_TASK <- LwIP_TASK [label="7.1> IP_EVENT_STA_GOT_IP"];
  237. APP_TASK <- EVENT_TASK [label="7.2> IP_EVENT_STA_GOT_IP"];
  238. APP_TASK -> APP_TASK [label="7.3> Socket error handling"];
  239. === 8. Deinit Phase ===
  240. APP_TASK -> WIFI_TASK [label="8.1> Disconnect Wi-Fi"];
  241. APP_TASK -> WIFI_TASK [label="8.2> Stop Wi-Fi"];
  242. APP_TASK -> WIFI_TASK [label="8.3> Deinit Wi-Fi"];
  243. }
  244. 1. Wi-Fi/LwIP Init Phase
  245. ++++++++++++++++++++++++++++++
  246. - s1.1: The main task calls :cpp:func:`esp_netif_init()` to create an LwIP core task and initialize LwIP-related work.
  247. - s1.2: The main task calls :cpp:func:`esp_event_loop_create()` to create a system Event task and initialize an application event's callback function. In the scenario above, the application event's callback function does nothing but relaying the event to the application task.
  248. - s1.3: The main task calls :cpp:func:`esp_netif_create_default_wifi_ap()` or :cpp:func:`esp_netif_create_default_wifi_sta()` to create default network interface instance binding station or AP with TCP/IP stack.
  249. - s1.4: The main task calls :cpp:func:`esp_wifi_init()` to create the Wi-Fi driver task and initialize the Wi-Fi driver.
  250. - s1.5: The main task calls OS API to create the application task.
  251. Step 1.1 ~ 1.5 is a recommended sequence that initializes a Wi-Fi-/LwIP-based application. However, it is **NOT** a must-follow sequence, which means that you can create the application task in step 1.1 and put all other initializations in the application task. Moreover, you may not want to create the application task in the initialization phase if the application task depends on the sockets. Rather, you can defer the task creation until the IP is obtained.
  252. 2. Wi-Fi Configuration Phase
  253. +++++++++++++++++++++++++++++++
  254. Once the Wi-Fi driver is initialized, you can start configuring the Wi-Fi driver. In this scenario, the mode is station, so you may need to call :cpp:func:`esp_wifi_set_mode` (WIFI_MODE_STA) to configure the Wi-Fi mode as station. You can call other `esp_wifi_set_xxx` APIs to configure more settings, such as the protocol mode, the country code, and the bandwidth. Refer to `{IDF_TARGET_NAME} Wi-Fi Configuration`_.
  255. Generally, the Wi-Fi driver should be configured before the Wi-Fi connection is set up. But this is **NOT** mandatory, which means that you can configure the Wi-Fi connection anytime, provided that the Wi-Fi driver is initialized successfully. However, if the configuration does not need to change after the Wi-Fi connection is set up, you should configure the Wi-Fi driver at this stage, because the configuration APIs (such as :cpp:func:`esp_wifi_set_protocol()`) will cause the Wi-Fi to reconnect, which may not be desirable.
  256. If the Wi-Fi NVS flash is enabled by menuconfig, all Wi-Fi configuration in this phase, or later phases, will be stored into flash. When the board powers on/reboots, you do not need to configure the Wi-Fi driver from scratch. You only need to call esp_wifi_get_xxx APIs to fetch the configuration stored in flash previously. You can also configure the Wi-Fi driver if the previous configuration is not what you want.
  257. 3. Wi-Fi Start Phase
  258. ++++++++++++++++++++++++++++++++
  259. - s3.1: Call :cpp:func:`esp_wifi_start()` to start the Wi-Fi driver.
  260. - s3.2: The Wi-Fi driver posts `WIFI_EVENT_STA_START`_ to the event task; then, the event task will do some common things and will call the application event callback function.
  261. - s3.3: The application event callback function relays the `WIFI_EVENT_STA_START`_ to the application task. We recommend that you call :cpp:func:`esp_wifi_connect()`. However, you can also call :cpp:func:`esp_wifi_connect()` in other phrases after the `WIFI_EVENT_STA_START`_ arises.
  262. 4. Wi-Fi Connect Phase
  263. +++++++++++++++++++++++++++++++++
  264. - s4.1: Once :cpp:func:`esp_wifi_connect()` is called, the Wi-Fi driver will start the internal scan/connection process.
  265. - s4.2: If the internal scan/connection process is successful, the `WIFI_EVENT_STA_CONNECTED`_ will be generated. In the event task, it starts the DHCP client, which will finally trigger the DHCP process.
  266. - s4.3: In the above-mentioned scenario, the application event callback will relay the event to the application task. Generally, the application needs to do nothing, and you can do whatever you want, e.g., print a log.
  267. In step 4.2, the Wi-Fi connection may fail because, for example, the password is wrong, or the AP is not found. In a case like this, `WIFI_EVENT_STA_DISCONNECTED`_ will arise and the reason for such a failure will be provided. For handling events that disrupt Wi-Fi connection, please refer to phase 6.
  268. 5. Wi-Fi 'Got IP' Phase
  269. +++++++++++++++++++++++++++++++++
  270. - s5.1: Once the DHCP client is initialized in step 4.2, the *got IP* phase will begin.
  271. - s5.2: If the IP address is successfully received from the DHCP server, then `IP_EVENT_STA_GOT_IP`_ will arise and the event task will perform common handling.
  272. - s5.3: In the application event callback, `IP_EVENT_STA_GOT_IP`_ is relayed to the application task. For LwIP-based applications, this event is very special and means that everything is ready for the application to begin its tasks, e.g., creating the TCP/UDP socket. A very common mistake is to initialize the socket before `IP_EVENT_STA_GOT_IP`_ is received. **DO NOT start the socket-related work before the IP is received.**
  273. 6. Wi-Fi Disconnect Phase
  274. +++++++++++++++++++++++++++++++++
  275. - s6.1: When the Wi-Fi connection is disrupted, e.g., the AP is powered off or the RSSI is poor, `WIFI_EVENT_STA_DISCONNECTED`_ will arise. This event may also arise in phase 3. Here, the event task will notify the LwIP task to clear/remove all UDP/TCP connections. Then, all application sockets will be in a wrong status. In other words, no socket can work properly when this event happens.
  276. - s6.2: In the scenario described above, the application event callback function relays `WIFI_EVENT_STA_DISCONNECTED`_ to the application task. The recommended actions are: 1) call :cpp:func:`esp_wifi_connect()` to reconnect the Wi-Fi, 2) close all sockets, and 3) re-create them if necessary. For details, please refer to `WIFI_EVENT_STA_DISCONNECTED`_.
  277. 7. Wi-Fi IP Change Phase
  278. ++++++++++++++++++++++++++++++++++
  279. - s7.1: If the IP address is changed, the `IP_EVENT_STA_GOT_IP`_ will arise with "ip_change" set to true.
  280. - s7.2: **This event is important to the application. When it occurs, the timing is good for closing all created sockets and recreating them.**
  281. 8. Wi-Fi Deinit Phase
  282. ++++++++++++++++++++++++++++
  283. - s8.1: Call :cpp:func:`esp_wifi_disconnect()` to disconnect the Wi-Fi connectivity.
  284. - s8.2: Call :cpp:func:`esp_wifi_stop()` to stop the Wi-Fi driver.
  285. - s8.3: Call :cpp:func:`esp_wifi_deinit()` to unload the Wi-Fi driver.
  286. {IDF_TARGET_NAME} Wi-Fi AP General Scenario
  287. ---------------------------------------------
  288. Below is a "big scenario" which describes some small scenarios in AP mode:
  289. .. seqdiag::
  290. :caption: Sample Wi-Fi Event Scenarios in AP Mode
  291. :align: center
  292. seqdiag sample-scenarios-soft-ap-mode {
  293. activation = none;
  294. node_width = 80;
  295. node_height = 60;
  296. edge_length = 140;
  297. span_height = 5;
  298. default_shape = roundedbox;
  299. default_fontsize = 12;
  300. MAIN_TASK [label = "Main\ntask"];
  301. APP_TASK [label = "App\ntask"];
  302. EVENT_TASK [label = "Event\ntask"];
  303. LwIP_TASK [label = "LwIP\ntask"];
  304. WIFI_TASK [label = "Wi-Fi\ntask"];
  305. === 1. Init Phase ===
  306. MAIN_TASK -> LwIP_TASK [label="1.1> Create / init LwIP"];
  307. MAIN_TASK -> EVENT_TASK [label="1.2> Create / init event"];
  308. MAIN_TASK -> WIFI_TASK [label="1.3> Create / init Wi-Fi"];
  309. MAIN_TASK -> APP_TASK [label="1.4> Create app task"];
  310. === 2. Configure Phase ===
  311. MAIN_TASK -> WIFI_TASK [label="2> Configure Wi-Fi"];
  312. === 3. Start Phase ===
  313. MAIN_TASK -> WIFI_TASK [label="3.1> Start Wi-Fi"];
  314. EVENT_TASK <- WIFI_TASK [label="3.2> WIFI_EVENT_AP_START"];
  315. APP_TASK <- EVENT_TASK [label="3.3> WIFI_EVENT_AP_START"];
  316. === 4. Connect Phase ===
  317. EVENT_TASK <- WIFI_TASK [label="4.1> WIFI_EVENT_AP_STACONNECTED"];
  318. APP_TASK <- EVENT_TASK [label="4.2> WIFI_EVENT_AP_STACONNECTED"];
  319. === 5. Disconnect Phase ===
  320. EVENT_TASK <- WIFI_TASK [label="5.1> WIFI_EVENT_AP_STADISCONNECTED"];
  321. APP_TASK <- EVENT_TASK [label="5.2> WIFI_EVENT_AP_STADISCONNECTED"];
  322. APP_TASK -> APP_TASK [label="5.3> disconnect handling"];
  323. === 6. Deinit Phase ===
  324. APP_TASK -> WIFI_TASK [label="6.1> Disconnect Wi-Fi"];
  325. APP_TASK -> WIFI_TASK [label="6.2> Stop Wi-Fi"];
  326. APP_TASK -> WIFI_TASK [label="6.3> Deinit Wi-Fi"];
  327. }
  328. {IDF_TARGET_NAME} Wi-Fi Scan
  329. ----------------------------
  330. Currently, the :cpp:func:`esp_wifi_scan_start()` API is supported only in station or station/AP mode.
  331. Scan Type
  332. +++++++++++++++++++++++++
  333. .. list-table::
  334. :header-rows: 1
  335. :widths: 15 50
  336. * - Mode
  337. - Description
  338. * - Active Scan
  339. - Scan by sending a probe request. The default scan is an active scan.
  340. * - Passive Scan
  341. - No probe request is sent out. Just switch to the specific channel and wait for a beacon. Application can enable it via the scan_type field of wifi_scan_config_t.
  342. * - Foreground Scan
  343. - This scan is applicable when there is no Wi-Fi connection in station mode. Foreground or background scanning is controlled by the Wi-Fi driver and cannot be configured by the application.
  344. * - Background Scan
  345. - This scan is applicable when there is a Wi-Fi connection in station mode or in station/AP mode. Whether it is a foreground scan or background scan depends on the Wi-Fi driver and cannot be configured by the application.
  346. * - All-Channel Scan
  347. - It scans all of the channels. If the channel field of wifi_scan_config_t is set to 0, it is an all-channel scan.
  348. * - Specific Channel Scan
  349. - It scans specific channels only. If the channel field of wifi_scan_config_t set to 1-14, it is a specific-channel scan.
  350. The scan modes in above table can be combined arbitrarily, so there are in total 8 different scans:
  351. - All-Channel Background Active Scan
  352. - All-Channel Background Passive Scan
  353. - All-Channel Foreground Active Scan
  354. - All-Channel Foreground Passive Scan
  355. - Specific-Channel Background Active Scan
  356. - Specific-Channel Background Passive Scan
  357. - Specific-Channel Foreground Active Scan
  358. - Specific-Channel Foreground Passive Scan
  359. Scan Configuration
  360. +++++++++++++++++++++++++++++++++++++++
  361. The scan type and other per-scan attributes are configured by :cpp:func:`esp_wifi_scan_start()`. The table below provides a detailed description of wifi_scan_config_t.
  362. .. list-table::
  363. :header-rows: 1
  364. :widths: 15 50
  365. * - Field
  366. - Description
  367. * - ssid
  368. - If the SSID is not NULL, it is only the AP with the same SSID that can be scanned.
  369. * - bssid
  370. - If the BSSID is not NULL, it is only the AP with the same BSSID that can be scanned.
  371. * - channel
  372. - If “channel” is 0, there will be an all-channel scan; otherwise, there will be a specific-channel scan.
  373. * - show_hidden
  374. - If “show_hidden” is 0, the scan ignores the AP with a hidden SSID; otherwise, the scan considers the hidden AP a normal one.
  375. * - scan_type
  376. - If “scan_type” is WIFI_SCAN_TYPE_ACTIVE, the scan is “active”; otherwise, it is a “passive” one.
  377. * - scan_time
  378. - This field is used to control how long the scan dwells on each channel.
  379. For passive scans, scan_time.passive designates the dwell time for each channel.
  380. For active scans, dwell times for each channel are listed in the table below. Here, min is short for scan time.active.min and max is short for scan_time.active.max.
  381. - min=0, max=0: scan dwells on each channel for 120 ms.
  382. - min>0, max=0: scan dwells on each channel for 120 ms.
  383. - min=0, max>0: scan dwells on each channel for ``max`` ms.
  384. - min>0, max>0: the minimum time the scan dwells on each channel is ``min`` ms. If no AP is found during this time frame, the scan switches to the next channel. Otherwise, the scan dwells on the channel for ``max`` ms.
  385. If you want to improve the performance of the the scan, you can try to modify these two parameters.
  386. There are also some global scan attributes which are configured by API :cpp:func:`esp_wifi_set_config()`, refer to `Station Basic Configuration`_
  387. Scan All APs on All Channels (Foreground)
  388. +++++++++++++++++++++++++++++++++++++++++++++
  389. Scenario:
  390. .. seqdiag::
  391. :caption: Foreground Scan of all Wi-Fi Channels
  392. :align: center
  393. seqdiag foreground-scan-all-channels {
  394. activation = none;
  395. node_width = 80;
  396. node_height = 60;
  397. edge_length = 160;
  398. span_height = 5;
  399. default_shape = roundedbox;
  400. default_fontsize = 12;
  401. APP_TASK [label = "App\ntask"];
  402. EVENT_TASK [label = "Event\ntask"];
  403. WIFI_TASK [label = "Wi-Fi\ntask"];
  404. APP_TASK -> WIFI_TASK [label="1.1 > Configure country code"];
  405. APP_TASK -> WIFI_TASK [label="1.2 > Scan configuration"];
  406. WIFI_TASK -> WIFI_TASK [label="2.1 > Scan channel 1"];
  407. WIFI_TASK -> WIFI_TASK [label="2.2 > Scan channel 2"];
  408. WIFI_TASK -> WIFI_TASK [label="..."];
  409. WIFI_TASK -> WIFI_TASK [label="2.x > Scan channel N"];
  410. EVENT_TASK <- WIFI_TASK [label="3.1 > WIFI_EVENT_SCAN_DONE"];
  411. APP_TASK <- EVENT_TASK [label="3.2 > WIFI_EVENT_SCAN_DONE"];
  412. }
  413. The scenario above describes an all-channel, foreground scan. The foreground scan can only occur in station mode where the station does not connect to any AP. Whether it is a foreground or background scan is totally determined by the Wi-Fi driver, and cannot be configured by the application.
  414. Detailed scenario description:
  415. Scan Configuration Phase
  416. **************************
  417. - s1.1: Call :cpp:func:`esp_wifi_set_country()` to set the country info if the default country info is not what you want. Refer to `Wi-Fi Country Code`_.
  418. - s1.2: Call :cpp:func:`esp_wifi_scan_start()` to configure the scan. To do so, you can refer to `Scan Configuration`_. Since this is an all-channel scan, just set the SSID/BSSID/channel to 0.
  419. Wi-Fi Driver's Internal Scan Phase
  420. **************************************
  421. - s2.1: The Wi-Fi driver switches to channel 1. In this case, the scan type is WIFI_SCAN_TYPE_ACTIVE, and a probe request is broadcasted. Otherwise, the Wi-Fi will wait for a beacon from the APs. The Wi-Fi driver will stay in channel 1 for some time. The dwell time is configured in min/max time, with the default value being 120 ms.
  422. - s2.2: The Wi-Fi driver switches to channel 2 and performs the same operation as in step 2.1.
  423. - s2.3: The Wi-Fi driver scans the last channel N, where N is determined by the country code which is configured in step 1.1.
  424. Scan-Done Event Handling Phase
  425. *********************************
  426. - s3.1: When all channels are scanned, `WIFI_EVENT_SCAN_DONE`_ will arise.
  427. - s3.2: The application's event callback function notifies the application task that `WIFI_EVENT_SCAN_DONE`_ is received. :cpp:func:`esp_wifi_scan_get_ap_num()` is called to get the number of APs that have been found in this scan. Then, it allocates enough entries and calls :cpp:func:`esp_wifi_scan_get_ap_records()` to get the AP records. Please note that the AP records in the Wi-Fi driver will be freed once :cpp:func:`esp_wifi_scan_get_ap_records()` is called. Do not call :cpp:func:`esp_wifi_scan_get_ap_records()` twice for a single scan-done event. If :cpp:func:`esp_wifi_scan_get_ap_records()` is not called when the scan-done event occurs, the AP records allocated by the Wi-Fi driver will not be freed. So, make sure you call :cpp:func:`esp_wifi_scan_get_ap_records()`, yet only once.
  428. Scan All APs on All Channels (Background)
  429. ++++++++++++++++++++++++++++++++++++++++++
  430. Scenario:
  431. .. seqdiag::
  432. :caption: Background Scan of all Wi-Fi Channels
  433. :align: center
  434. seqdiag background-scan-all-channels {
  435. activation = none;
  436. node_width = 80;
  437. node_height = 60;
  438. edge_length = 160;
  439. span_height = 5;
  440. default_shape = roundedbox;
  441. default_fontsize = 12;
  442. APP_TASK [label = "App\ntask"];
  443. EVENT_TASK [label = "Event\ntask"];
  444. WIFI_TASK [label = "Wi-Fi\ntask"];
  445. APP_TASK -> WIFI_TASK [label="1.1 > Configure country code"];
  446. APP_TASK -> WIFI_TASK [label="1.2 > Scan configuration"];
  447. WIFI_TASK -> WIFI_TASK [label="2.1 > Scan channel 1"];
  448. WIFI_TASK -> WIFI_TASK [label="2.2 > Back to home channel H"];
  449. WIFI_TASK -> WIFI_TASK [label="2.3 > Scan channel 2"];
  450. WIFI_TASK -> WIFI_TASK [label="2.4 > Back to home channel H"];
  451. WIFI_TASK -> WIFI_TASK [label="..."];
  452. WIFI_TASK -> WIFI_TASK [label="2.x-1 > Scan channel N"];
  453. WIFI_TASK -> WIFI_TASK [label="2.x > Back to home channel H"];
  454. EVENT_TASK <- WIFI_TASK [label="3.1 > WIFI_EVENT_SCAN_DONE"];
  455. APP_TASK <- EVENT_TASK [label="3.2 > WIFI_EVENT_SCAN_DONE"];
  456. }
  457. The scenario above is an all-channel background scan. Compared to `Scan All APs on All Channels (Foreground)`_ , the difference in the all-channel background scan is that the Wi-Fi driver will scan the back-to-home channel for 30 ms before it switches to the next channel to give the Wi-Fi connection a chance to transmit/receive data.
  458. Scan for Specific AP on All Channels
  459. +++++++++++++++++++++++++++++++++++++++
  460. Scenario:
  461. .. seqdiag::
  462. :caption: Scan of specific Wi-Fi Channels
  463. :align: center
  464. seqdiag scan-specific-channels {
  465. activation = none;
  466. node_width = 80;
  467. node_height = 60;
  468. edge_length = 160;
  469. span_height = 5;
  470. default_shape = roundedbox;
  471. default_fontsize = 12;
  472. APP_TASK [label = "App\ntask"];
  473. EVENT_TASK [label = "Event\ntask"];
  474. WIFI_TASK [label = "Wi-Fi\ntask"];
  475. APP_TASK -> WIFI_TASK [label="1.1 > Configure country code"];
  476. APP_TASK -> WIFI_TASK [label="1.2 > Scan configuration"];
  477. WIFI_TASK -> WIFI_TASK [label="2.1 > Scan channel C1"];
  478. WIFI_TASK -> WIFI_TASK [label="2.2 > Scan channel C2"];
  479. WIFI_TASK -> WIFI_TASK [label="..."];
  480. WIFI_TASK -> WIFI_TASK [label="2.x > Scan channel CN, or the AP is found"];
  481. EVENT_TASK <- WIFI_TASK [label="3.1 > WIFI_EVENT_SCAN_DONE"];
  482. APP_TASK <- EVENT_TASK [label="3.2 > WIFI_EVENT_SCAN_DONE"];
  483. }
  484. This scan is similar to `Scan All APs on All Channels (Foreground)`_. The differences are:
  485. - s1.1: In step 1.2, the target AP will be configured to SSID/BSSID.
  486. - s2.1 ~ s2.N: Each time the Wi-Fi driver scans an AP, it will check whether it is a target AP or not. If the scan is WIFI_FAST_SCAN scan and the target AP is found, then the scan-done event will arise and scanning will end; otherwise, the scan will continue. Please note that the first scanned channel may not be channel 1, because the Wi-Fi driver optimizes the scanning sequence.
  487. It is a possible situation that there are multiple APs that match the target AP info, e.g., two APs with the SSID of "ap" are scanned. In this case, if the scan is WIFI_FAST_SCAN, then only the first scanned "ap" will be found. If the scan is WIFI_ALL_CHANNEL_SCAN, both "ap" will be found and the station will connect the "ap" according to the configured strategy. Refer to `Station Basic Configuration`_.
  488. You can scan a specific AP, or all of them, in any given channel. These two scenarios are very similar.
  489. Scan in Wi-Fi Connect
  490. +++++++++++++++++++++++++
  491. When :cpp:func:`esp_wifi_connect()` is called, the Wi-Fi driver will try to scan the configured AP first. The scan in "Wi-Fi Connect" is the same as `Scan for Specific AP On All Channels`_, except that no scan-done event will be generated when the scan is completed. If the target AP is found, the Wi-Fi driver will start the Wi-Fi connection; otherwise, `WIFI_EVENT_STA_DISCONNECTED`_ will be generated. Refer to `Scan for Specific AP On All Channels`_.
  492. Scan in Blocked Mode
  493. ++++++++++++++++++++
  494. If the block parameter of :cpp:func:`esp_wifi_scan_start()` is true, then the scan is a blocked one, and the application task will be blocked until the scan is done. The blocked scan is similar to an unblocked one, except that no scan-done event will arise when the blocked scan is completed.
  495. Parallel Scan
  496. +++++++++++++
  497. Two application tasks may call :cpp:func:`esp_wifi_scan_start()` at the same time, or the same application task calls :cpp:func:`esp_wifi_scan_start()` before it gets a scan-done event. Both scenarios can happen. **However, the Wi-Fi driver does not support multiple concurrent scans adequately. As a result, concurrent scans should be avoided.** Support for concurrent scan will be enhanced in future releases, as the {IDF_TARGET_NAME}'s Wi-Fi functionality improves continuously.
  498. Scan When Wi-Fi Is Connecting
  499. +++++++++++++++++++++++++++++++
  500. The :cpp:func:`esp_wifi_scan_start()` fails immediately if the Wi-Fi is connecting, because the connecting has higher priority than the scan. If scan fails because of connecting, the recommended strategy is to delay for some time and retry scan again. The scan will succeed once the connecting is completed.
  501. However, the retry/delay strategy may not work all the time. Considering the following scenarios:
  502. - The station is connecting a non-existing AP or it connects the existing AP with a wrong password, it always raises the event `WIFI_EVENT_STA_DISCONNECTED`_.
  503. - The application calls :cpp:func:`esp_wifi_connect()` to reconnect on receiving the disconnect event.
  504. - Another application task, e.g., the console task, calls :cpp:func:`esp_wifi_scan_start()` to do scan, the scan always fails immediately because the station keeps connecting.
  505. - When scan fails, the application simply delays for some time and retries the scan.
  506. In the above scenarios, the scan will never succeed because the connecting is in process. So if the application supports similar scenario, it needs to implement a better reconnection strategy. For example:
  507. - The application can choose to define a maximum continuous reconnection counter and stop reconnecting once the counter reaches the maximum.
  508. - The application can choose to reconnect immediately in the first N continuous reconnection, then give a delay sometime and reconnect again.
  509. The application can define its own reconnection strategy to avoid the scan starve to death. Refer to <`Wi-Fi Reconnect`_>.
  510. {IDF_TARGET_NAME} Wi-Fi Station Connecting Scenario
  511. ---------------------------------------------------
  512. This scenario depicts the case if only one target AP is found in the scan phase. For scenarios where more than one AP with the same SSID is found, refer to `{IDF_TARGET_NAME} Wi-Fi Station Connecting When Multiple APs Are Found`_.
  513. Generally, the application can ignore the connecting process. Below is a brief introduction to the process for those who are really interested.
  514. Scenario:
  515. .. seqdiag::
  516. :caption: Wi-Fi Station Connecting Process
  517. :align: center
  518. seqdiag station-connecting-process {
  519. activation = none;
  520. node_width = 80;
  521. node_height = 60;
  522. edge_length = 160;
  523. span_height = 5;
  524. default_shape = roundedbox;
  525. default_fontsize = 12;
  526. EVENT_TASK [label = "Event\ntask"];
  527. WIFI_TASK [label = "Wi-Fi\ntask"];
  528. AP [label = "AP"];
  529. === 1. Scan Phase ===
  530. WIFI_TASK -> WIFI_TASK [label="1.1 > Scan"];
  531. EVENT_TASK <- WIFI_TASK [label="1.2 > WIFI_EVENT_STA_DISCONNECTED"];
  532. === 2. Auth Phase ===
  533. WIFI_TASK -> AP [label="2.1 > Auth request"];
  534. EVENT_TASK <- WIFI_TASK [label="2.2 > WIFI_EVENT_STA_DISCONNECTED"];
  535. WIFI_TASK <- AP [label="2.3 > Auth response"];
  536. EVENT_TASK <- WIFI_TASK [label="2.4 > WIFI_EVENT_STA_DISCONNECTED"];
  537. === 3. Assoc Phase ===
  538. WIFI_TASK -> AP [label="3.1 > Assoc request"];
  539. EVENT_TASK <- WIFI_TASK [label="3.2 > WIFI_EVENT_STA_DISCONNECTED"];
  540. WIFI_TASK <- AP [label="3.3 > Assoc response"];
  541. EVENT_TASK <- WIFI_TASK [label="3.4 > WIFI_EVENT_STA_DISCONNECTED"];
  542. === 4. 4-way Handshake Phase ===
  543. EVENT_TASK <- WIFI_TASK [label="4.1 > WIFI_EVENT_STA_DISCONNECTED"];
  544. WIFI_TASK <- AP [label="4.2 > 1/4 EAPOL"];
  545. WIFI_TASK -> AP [label="4.3 > 2/4 EAPOL"];
  546. EVENT_TASK <- WIFI_TASK [label="4.4 > WIFI_EVENT_STA_DISCONNECTED"];
  547. WIFI_TASK <- AP [label="4.5 > 3/4 EAPOL"];
  548. WIFI_TASK -> AP [label="4.6 > 4/4 EAPOL"];
  549. EVENT_TASK <- WIFI_TASK [label="4.7 > WIFI_EVENT_STA_CONNECTED"];
  550. }
  551. Scan Phase
  552. +++++++++++++++++++++
  553. - s1.1: The Wi-Fi driver begins scanning in "Wi-Fi Connect". Refer to `Scan in Wi-Fi Connect`_ for more details.
  554. - s1.2: If the scan fails to find the target AP, `WIFI_EVENT_STA_DISCONNECTED`_ will arise and the reason code will be WIFI_REASON_NO_AP_FOUND. Refer to `Wi-Fi Reason Code`_.
  555. Auth Phase
  556. +++++++++++++++++++++
  557. - s2.1: The authentication request packet is sent and the auth timer is enabled.
  558. - s2.2: If the authentication response packet is not received before the authentication timer times out, `WIFI_EVENT_STA_DISCONNECTED`_ will arise and the reason code will be WIFI_REASON_AUTH_EXPIRE. Refer to `Wi-Fi Reason Code`_.
  559. - s2.3: The auth-response packet is received and the auth-timer is stopped.
  560. - s2.4: The AP rejects authentication in the response and `WIFI_EVENT_STA_DISCONNECTED`_ arises, while the reason code is WIFI_REASON_AUTH_FAIL or the reasons specified by the AP. Refer to `Wi-Fi Reason Code`_.
  561. Association Phase
  562. +++++++++++++++++++++
  563. - s3.1: The association request is sent and the association timer is enabled.
  564. - s3.2: If the association response is not received before the association timer times out, `WIFI_EVENT_STA_DISCONNECTED`_ will arise and the reason code will be WIFI_REASON_ASSOC_EXPIRE. Refer to `Wi-Fi Reason Code`_.
  565. - s3.3: The association response is received and the association timer is stopped.
  566. - s3.4: The AP rejects the association in the response and `WIFI_EVENT_STA_DISCONNECTED`_ arises, while the reason code is the one specified in the association response. Refer to `Wi-Fi Reason Code`_.
  567. Four-way Handshake Phase
  568. ++++++++++++++++++++++++++
  569. - s4.1: The handshake timer is enabled, and the 1/4 EAPOL is not received before the handshake timer expires. `WIFI_EVENT_STA_DISCONNECTED`_ will arise and the reason code will be WIFI_REASON_HANDSHAKE_TIMEOUT. Refer to `Wi-Fi Reason Code`_.
  570. - s4.2: The 1/4 EAPOL is received.
  571. - s4.3: The station replies 2/4 EAPOL.
  572. - s4.4: If the 3/4 EAPOL is not received before the handshake timer expires, `WIFI_EVENT_STA_DISCONNECTED`_ will arise and the reason code will be WIFI_REASON_HANDSHAKE_TIMEOUT. Refer to `Wi-Fi Reason Code`_.
  573. - s4.5: The 3/4 EAPOL is received.
  574. - s4.6: The station replies 4/4 EAPOL.
  575. - s4.7: The station raises `WIFI_EVENT_STA_CONNECTED`_.
  576. Wi-Fi Reason Code
  577. +++++++++++++++++++++
  578. The table below shows the reason code defined in {IDF_TARGET_NAME}. The first column is the macro name defined in esp_wifi_types.h. The common prefix *WIFI_REASON* is removed, which means that *UNSPECIFIED* actually stands for *WIFI_REASON_UNSPECIFIED* and so on. The second column is the value of the reason. The third column is the standard value to which this reason is mapped in section 8.4.1.7 of IEEE 802.11-2012 (For more information, refer to the standard mentioned above). The last column describes the reason.
  579. .. list-table::
  580. :header-rows: 1
  581. :widths: 5 10 12 40
  582. * - Reason code
  583. - Value
  584. - Mapped To
  585. - Description
  586. * - UNSPECIFIED
  587. - 1
  588. - 1
  589. - Generally, it means an internal failure, e.g., the memory runs out, the internal TX fails, or the reason is received from the remote side.
  590. * - AUTH_EXPIRE
  591. - 2
  592. - 2
  593. - The previous authentication is no longer valid.
  594. For the ESP station, this reason is reported when:
  595. - auth is timed out.
  596. - the reason is received from the AP.
  597. For the ESP AP, this reason is reported when:
  598. - the AP has not received any packets from the station in the past five minutes.
  599. - the AP is stopped by calling :cpp:func:`esp_wifi_stop()`.
  600. - the station is de-authed by calling :cpp:func:`esp_wifi_deauth_sta()`.
  601. * - AUTH_LEAVE
  602. - 3
  603. - 3
  604. - De-authenticated, because the sending station is leaving (or has left).
  605. For the ESP station, this reason is reported when:
  606. - it is received from the AP.
  607. * - ASSOC_EXPIRE
  608. - 4
  609. - 4
  610. - Disassociated due to inactivity.
  611. For the ESP station, this reason is reported when:
  612. - it is received from the AP.
  613. For the ESP AP, this reason is reported when:
  614. - the AP has not received any packets from the station in the past five minutes.
  615. - the AP is stopped by calling :cpp:func:`esp_wifi_stop()`.
  616. - the station is de-authed by calling :cpp:func:`esp_wifi_deauth_sta()`.
  617. * - ASSOC_TOOMANY
  618. - 5
  619. - 5
  620. - Disassociated, because the AP is unable to handle all currently associated STAs at the same time.
  621. For the ESP station, this reason is reported when:
  622. - it is received from the AP.
  623. For the ESP AP, this reason is reported when:
  624. - the stations associated with the AP reach the maximum number that the AP can support.
  625. * - NOT_AUTHED
  626. - 6
  627. - 6
  628. - Class-2 frame received from a non-authenticated STA.
  629. For the ESP station, this reason is reported when:
  630. - it is received from the AP.
  631. For the ESP AP, this reason is reported when:
  632. - the AP receives a packet with data from a non-authenticated station.
  633. * - NOT_ASSOCED
  634. - 7
  635. - 7
  636. - Class-3 frame received from a non-associated STA.
  637. For the ESP station, this reason is reported when:
  638. - it is received from the AP.
  639. For the ESP AP, this reason is reported when:
  640. - the AP receives a packet with data from a non-associated station.
  641. * - ASSOC_LEAVE
  642. - 8
  643. - 8
  644. - Disassociated, because the sending station is leaving (or has left) BSS.
  645. For the ESP station, this reason is reported when:
  646. - it is received from the AP.
  647. - the station is disconnected by :cpp:func:`esp_wifi_disconnect()` and other APIs.
  648. * - ASSOC_NOT_AUTHED
  649. - 9
  650. - 9
  651. - station requesting (re)association is not authenticated by the responding STA.
  652. For the ESP station, this reason is reported when:
  653. - it is received from the AP.
  654. For the ESP AP, this reason is reported when:
  655. - the AP receives packets with data from an associated, yet not authenticated, station.
  656. * - DISASSOC_PWRCAP_BAD
  657. - 10
  658. - 10
  659. - Disassociated, because the information in the Power Capability element is unacceptable.
  660. For the ESP station, this reason is reported when:
  661. - it is received from the AP.
  662. * - DISASSOC_SUPCHAN_BAD
  663. - 11
  664. - 11
  665. - Disassociated, because the information in the Supported Channels element is unacceptable.
  666. For the ESP station, this reason is reported when:
  667. - it is received from the AP.
  668. * - IE_INVALID
  669. - 13
  670. - 13
  671. - Invalid element, i.e., an element whose content does not meet the specifications of the Standard in frame formats clause.
  672. For the ESP station, this reason is reported when:
  673. - it is received from the AP.
  674. For the ESP AP, this reason is reported when:
  675. - the AP parses a wrong WPA or RSN IE.
  676. * - MIC_FAILURE
  677. - 14
  678. - 14
  679. - Message integrity code (MIC) failure.
  680. For the ESP station, this reason is reported when:
  681. - it is received from the AP.
  682. * - 4WAY_HANDSHAKE_TIMEOUT
  683. - 15
  684. - 15
  685. - Four-way handshake times out. For legacy reasons, in ESP this reason code is replaced with WIFI_REASON_HANDSHAKE_TIMEOUT.
  686. For the ESP station, this reason is reported when:
  687. - the handshake times out.
  688. - it is received from the AP.
  689. * - GROUP_KEY_UPDATE_TIMEOUT
  690. - 16
  691. - 16
  692. - Group-Key Handshake times out.
  693. For the ESP station, this reason is reported when:
  694. - it is received from the AP.
  695. * - IE_IN_4WAY_DIFFERS
  696. - 17
  697. - 17
  698. - The element in the four-way handshake is different from the (Re-)Association Request/Probe and Response/Beacon frame.
  699. For the ESP station, this reason is reported when:
  700. - it is received from the AP.
  701. - the station finds that the four-way handshake IE differs from the IE in the (Re-)Association Request/Probe and Response/Beacon frame.
  702. * - GROUP_CIPHER_INVALID
  703. - 18
  704. - 18
  705. - Invalid group cipher.
  706. For the ESP station, this reason is reported when:
  707. - it is received from the AP.
  708. * - PAIRWISE_CIPHER_INVALID
  709. - 19
  710. - 19
  711. - Invalid pairwise cipher.
  712. For the ESP station, this reason is reported when:
  713. - it is received from the AP.
  714. * - AKMP_INVALID
  715. - 20
  716. - 20
  717. - Invalid AKMP.
  718. For the ESP station, this reason is reported when:
  719. - it is received from the AP.
  720. * - UNSUPP_RSN_IE_VERSION
  721. - 21
  722. - 21
  723. - Unsupported RSNE version.
  724. For the ESP station, this reason is reported when:
  725. - it is received from the AP.
  726. * - INVALID_RSN_IE_CAP
  727. - 22
  728. - 22
  729. - Invalid RSNE capabilities.
  730. For the ESP station, this reason is reported when:
  731. - it is received from the AP.
  732. * - 802_1X_AUTH_FAILED
  733. - 23
  734. - 23
  735. - IEEE 802.1X. authentication failed.
  736. For the ESP station, this reason is reported when:
  737. - it is received from the AP.
  738. For the ESP AP, this reason is reported when:
  739. - IEEE 802.1X. authentication fails.
  740. * - CIPHER_SUITE_REJECTED
  741. - 24
  742. - 24
  743. - Cipher suite rejected due to security policies.
  744. For the ESP station, this reason is reported when:
  745. - it is received from the AP.
  746. * - BEACON_TIMEOUT
  747. - 200
  748. - reserved
  749. - Espressif-specific Wi-Fi reason code: when the station loses N beacons continuously, it will disrupt the connection and report this reason.
  750. * - NO_AP_FOUND
  751. - 201
  752. - reserved
  753. - Espressif-specific Wi-Fi reason code: when the station fails to scan the target AP, this reason code will be reported.
  754. * - AUTH_FAIL
  755. - 202
  756. - reserved
  757. - Espressif-specific Wi-Fi reason code: the authentication fails, but not because of a timeout.
  758. * - ASSOC_FAIL
  759. - 203
  760. - reserved
  761. - Espressif-specific Wi-Fi reason code: the association fails, but not because of ASSOC_EXPIRE or ASSOC_TOOMANY.
  762. * - HANDSHAKE_TIMEOUT
  763. - 204
  764. - reserved
  765. - Espressif-specific Wi-Fi reason code: the handshake fails for the same reason as that in WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT.
  766. * - CONNECTION_FAIL
  767. - 205
  768. - reserved
  769. - Espressif-specific Wi-Fi reason code: the connection to the AP has failed.
  770. {IDF_TARGET_NAME} Wi-Fi Station Connecting When Multiple APs Are Found
  771. ----------------------------------------------------------------------
  772. This scenario is similar as `{IDF_TARGET_NAME} Wi-Fi Station Connecting Scenario`_. The difference is that the station will not raise the event `WIFI_EVENT_STA_DISCONNECTED`_ unless it fails to connect all of the found APs.
  773. Wi-Fi Reconnect
  774. ---------------------------
  775. The station may disconnect due to many reasons, e.g., the connected AP is restarted. It is the application's responsibility to reconnect. The recommended reconnection strategy is to call :cpp:func:`esp_wifi_connect()` on receiving event `WIFI_EVENT_STA_DISCONNECTED`_.
  776. Sometimes the application needs more complex reconnection strategy:
  777. - If the disconnect event is raised because the :cpp:func:`esp_wifi_disconnect()` is called, the application may not want to do the reconnection.
  778. - If the :cpp:func:`esp_wifi_scan_start()` may be called at anytime, a better reconnection strategy is necessary. Refer to `Scan When Wi-Fi Is Connecting`_.
  779. Another thing that need to be considered is that the reconnection may not connect the same AP if there are more than one APs with the same SSID. The reconnection always select current best APs to connect.
  780. Wi-Fi Beacon Timeout
  781. ---------------------------
  782. The beacon timeout mechanism is used by {IDF_TARGET_NAME} station to detect whether the AP is alive or not. If the station does not receive the beacon of the connected AP within the inactive time, the beacon timeout happens. The application can set inactive time via API :cpp:func:`esp_wifi_set_inactive_time()`.
  783. After the beacon times out, the station sends 5 probe requests to the AP. If still no probe response or beacon is received from AP, the station disconnects from the AP and raises the event `WIFI_EVENT_STA_DISCONNECTED`_.
  784. It should be considered that the timer used for beacon timeout will be reset during the scanning process. It means that the scan process will affect the triggering of the event `WIFI_EVENT_STA_BEACON_TIMEOUT`_.
  785. {IDF_TARGET_NAME} Wi-Fi Configuration
  786. -------------------------------------
  787. All configurations will be stored into flash when the Wi-Fi NVS is enabled; otherwise, refer to `Wi-Fi NVS Flash`_.
  788. Wi-Fi Mode
  789. +++++++++++++++++++++++++
  790. Call :cpp:func:`esp_wifi_set_mode()` to set the Wi-Fi mode.
  791. .. list-table::
  792. :header-rows: 1
  793. :widths: 15 50
  794. * - Mode
  795. - Description
  796. * - WIFI_MODE_NULL
  797. - NULL mode: in this mode, the internal data struct is not allocated to the station and the AP, while both the station and AP interfaces are not initialized for RX/TX Wi-Fi data. Generally, this mode is used for Sniffer, or when you only want to stop both the station and the AP without calling :cpp:func:`esp_wifi_deinit()` to unload the whole Wi-Fi driver.
  798. * - WIFI_MODE_STA
  799. - Station mode: in this mode, :cpp:func:`esp_wifi_start()` will init the internal station data, while the station’s interface is ready for the RX and TX Wi-Fi data. After :cpp:func:`esp_wifi_connect()`, the station will connect to the target AP.
  800. * - WIFI_MODE_AP
  801. - AP mode: in this mode, :cpp:func:`esp_wifi_start()` will init the internal AP data, while the AP’s interface is ready for RX/TX Wi-Fi data. Then, the Wi-Fi driver starts broad-casting beacons, and the AP is ready to get connected to other stations.
  802. * - WIFI_MODE_APSTA
  803. - Station/AP coexistence mode: in this mode, :cpp:func:`esp_wifi_start()` will simultaneously init both the station and the AP. This is done in station mode and AP mode. Please note that the channel of the external AP, which the ESP station is connected to, has higher priority over the ESP AP channel.
  804. Station Basic Configuration
  805. +++++++++++++++++++++++++++++++++++++
  806. API esp_wifi_set_config() can be used to configure the station. The table below describes the fields in detail.
  807. .. list-table::
  808. :header-rows: 1
  809. :widths: 15 50
  810. * - Field
  811. - Description
  812. * - ssid
  813. - This is the SSID of the target AP, to which the station wants to connect.
  814. * - password
  815. - Password of the target AP.
  816. * - scan_method
  817. - For WIFI_FAST_SCAN scan, the scan ends when the first matched AP is found. For WIFI_ALL_CHANNEL_SCAN, the scan finds all matched APs on all channels. The default scan is WIFI_FAST_SCAN.
  818. * - bssid_set
  819. - If bssid_set is 0, the station connects to the AP whose SSID is the same as the field “ssid”, while the field “bssid” is ignored. In all other cases, the station connects to the AP whose SSID is the same as the “ssid” field, while its BSSID is the same the “bssid” field .
  820. * - bssid
  821. - This is valid only when bssid_set is 1; see field “bssid_set”.
  822. * - channel
  823. - If the channel is 0, the station scans the channel 1 ~ N to search for the target AP; otherwise, the station starts by scanning the channel whose value is the same as that of the “channel” field, and then scans others to find the target AP. If you do not know which channel the target AP is running on, set it to 0.
  824. * - sort_method
  825. - This field is only for WIFI_ALL_CHANNEL_SCAN.
  826. If the sort_method is WIFI_CONNECT_AP_BY_SIGNAL, all matched APs are sorted by signal, and the AP with the best signal will be connected firstly. For example, the station wants to connect an AP whose SSID is “apxx”. If the scan finds two APs whose SSID equals to “apxx”, and the first AP’s signal is -90 dBm while the second AP’s signal is -30 dBm, the station connects the second AP firstly, and it would not connect the first one unless it fails to connect the second one.
  827. If the sort_method is WIFI_CONNECT_AP_BY_SECURITY, all matched APs are sorted by security. For example, the station wants to connect an AP whose SSID is “apxx”. If the scan finds two APs whose SSID is “apxx”, and the security of the first found AP is open while the second one is WPA2, the station connects to the second AP firstly, and it would not connect the first one unless it fails to connect the second one.
  828. * - threshold
  829. - The threshold is used to filter the found AP. If the RSSI or security mode is less than the configured threshold, the AP will be discarded.
  830. If the RSSI is set to 0, it means the default threshold and the default RSSI threshold are -127 dBm. If the authmode threshold is set to 0, it means the default threshold and the default authmode threshold are open.
  831. .. attention::
  832. WEP/WPA security modes are deprecated in IEEE 802.11-2016 specifications and are recommended not to be used. These modes can be rejected using authmode threshold by setting threshold as WPA2 by threshold.authmode as WIFI_AUTH_WPA2_PSK.
  833. AP Basic Configuration
  834. +++++++++++++++++++++++++++++++++++++
  835. API esp_wifi_set_config() can be used to configure the AP. The table below describes the fields in detail.
  836. .. only:: esp32 or esp32s2 or esp32c3 or esp32s3
  837. .. list-table::
  838. :header-rows: 1
  839. :widths: 15 55
  840. * - Field
  841. - Description
  842. * - ssid
  843. - SSID of AP; if the ssid[0] is 0xFF and ssid[1] is 0xFF, the AP defaults the SSID to ESP_aabbcc, where “aabbcc” is the last three bytes of the AP MAC.
  844. * - password
  845. - Password of AP; if the auth mode is WIFI_AUTH_OPEN, this field will be ignored.
  846. * - ssid_len
  847. - Length of SSID; if ssid_len is 0, check the SSID until there is a termination character. If ssid_len > 32, change it to 32; otherwise, set the SSID length according to ssid_len.
  848. * - channel
  849. - Channel of AP; if the channel is out of range, the Wi-Fi driver defaults the channel to channel 1. So, please make sure the channel is within the required range. For more details, refer to `Wi-Fi Country Code`_.
  850. * - authmode
  851. - Auth mode of ESP AP; currently, ESP Wi-Fi does not support AUTH_WEP. If the authmode is an invalid value, AP defaults the value to WIFI_AUTH_OPEN.
  852. * - ssid_hidden
  853. - If ssid_hidden is 1, AP does not broadcast the SSID; otherwise, it does broadcast the SSID.
  854. * - max_connection
  855. - Currently, ESP Wi-Fi supports up to 10 Wi-Fi connections. If max_connection > 10, AP defaults the value to 10.
  856. * - beacon_interval
  857. - Beacon interval; the value is 100 ~ 60000 ms, with default value being 100 ms. If the value is out of range, AP defaults it to 100 ms.
  858. .. only:: esp32c2
  859. .. list-table::
  860. :header-rows: 1
  861. :widths: 15 55
  862. * - Field
  863. - Description
  864. * - ssid
  865. - SSID of AP; if the ssid[0] is 0xFF and ssid[1] is 0xFF, the AP defaults the SSID to ESP_aabbcc, where “aabbcc” is the last three bytes of the AP MAC.
  866. * - password
  867. - Password of AP; if the auth mode is WIFI_AUTH_OPEN, this field will be ignored.
  868. * - ssid_len
  869. - Length of SSID; if ssid_len is 0, check the SSID until there is a termination character. If ssid_len > 32, change it to 32; otherwise, set the SSID length according to ssid_len.
  870. * - channel
  871. - Channel of AP; if the channel is out of range, the Wi-Fi driver defaults the channel to channel 1. So, please make sure the channel is within the required range. For more details, refer to `Wi-Fi Country Code`_.
  872. * - authmode
  873. - Auth mode of ESP AP; currently, ESP Wi-Fi does not support AUTH_WEP. If the authmode is an invalid value, AP defaults the value to WIFI_AUTH_OPEN.
  874. * - ssid_hidden
  875. - If ssid_hidden is 1, AP does not broadcast the SSID; otherwise, it does broadcast the SSID.
  876. * - max_connection
  877. - Currently, ESP Wi-Fi supports up to 4 Wi-Fi connections. If max_connection > 4, AP defaults the value to 4.
  878. * - beacon_interval
  879. - Beacon interval; the value is 100 ~ 60000 ms, with default value being 100 ms. If the value is out of range, AP defaults it to 100 ms.
  880. Wi-Fi Protocol Mode
  881. +++++++++++++++++++++++++
  882. Currently, the ESP-IDF supports the following protocol modes:
  883. .. only:: esp32 or esp32s2 or esp32c3 or esp32s3
  884. .. list-table::
  885. :header-rows: 1
  886. :widths: 15 55
  887. * - Protocol Mode
  888. - Description
  889. * - 802.11b
  890. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B) to set the station/AP to 802.11b-only mode.
  891. * - 802.11bg
  892. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G) to set the station/AP to 802.11bg mode.
  893. * - 802.11g
  894. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G) and esp_wifi_config_11b_rate(ifx, true) to set the station/AP to 802.11g mode.
  895. * - 802.11bgn
  896. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B| WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N) to set the station/ AP to BGN mode.
  897. * - 802.11gn
  898. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N) and esp_wifi_config_11b_rate(ifx, true) to set the station/AP to 802.11gn mode.
  899. * - 802.11 BGNLR
  900. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B| WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N|WIFI_PROTOCOL_LR) to set the station/AP to BGN and the LR mode.
  901. * - 802.11 LR
  902. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_LR) to set the station/AP only to the LR mode.
  903. **This mode is an Espressif-patented mode which can achieve a one-kilometer line of sight range. Please make sure both the station and the AP are connected to an ESP device.**
  904. .. only:: esp32c2
  905. .. list-table::
  906. :header-rows: 1
  907. :widths: 15 55
  908. * - Protocol Mode
  909. - Description
  910. * - 802.11b
  911. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B) to set the station/AP to 802.11b-only mode.
  912. * - 802.11bg
  913. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G) to set the station/AP to 802.11bg mode.
  914. * - 802.11g
  915. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G) and esp_wifi_config_11b_rate(ifx, true) to set the station/AP to 802.11g mode.
  916. * - 802.11bgn
  917. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B| WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N) to set the station/ AP to BGN mode.
  918. * - 802.11gn
  919. - Call esp_wifi_set_protocol(ifx, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N) and esp_wifi_config_11b_rate(ifx, true) to set the station/AP to 802.11gn mode.
  920. .. only:: esp32 or esp32s2 or esp32c3 or esp32s3
  921. Long Range (LR)
  922. +++++++++++++++++++++++++
  923. Long Range (LR) mode is an Espressif-patented Wi-Fi mode which can achieve a one-kilometer line of sight range. Compared to the traditional 802.11b mode, it has better reception sensitivity, stronger anti-interference ability, and longer transmission distance.
  924. LR Compatibility
  925. *************************
  926. Since LR is Espressif-unique Wi-Fi mode, only {IDF_TARGET_NAME} devices can transmit and receive the LR data. In other words, the {IDF_TARGET_NAME} device should NOT transmit the data in LR data rate if the connected device does not support LR. The application can achieve this by configuring suitable Wi-Fi mode. If the negotiated mode supports LR, the {IDF_TARGET_NAME} may transmit data in LR rate. Otherwise, {IDF_TARGET_NAME} will transmit all data in traditional Wi-Fi data rate.
  927. The following table depicts the Wi-Fi mode negotiation:
  928. +-------+-----+----+---+-------+------+-----+----+
  929. |AP\STA | BGN | BG | B | BGNLR | BGLR | BLR | LR |
  930. +=======+=====+====+===+=======+======+=====+====+
  931. | BGN | BGN | BG | B | BGN | BG | B | - |
  932. +-------+-----+----+---+-------+------+-----+----+
  933. | BG | BG | BG | B | BG | BG | B | - |
  934. +-------+-----+----+---+-------+------+-----+----+
  935. | B | B | B | B | B | B | B | - |
  936. +-------+-----+----+---+-------+------+-----+----+
  937. | BGNLR | - | - | - | BGNLR | BGLR | BLR | LR |
  938. +-------+-----+----+---+-------+------+-----+----+
  939. | BGLR | - | - | - | BGLR | BGLR | BLR | LR |
  940. +-------+-----+----+---+-------+------+-----+----+
  941. | BLR | - | - | - | BLR | BLR | BLR | LR |
  942. +-------+-----+----+---+-------+------+-----+----+
  943. | LR | - | - | - | LR | LR | LR | LR |
  944. +-------+-----+----+---+-------+------+-----+----+
  945. In the above table, the row is the Wi-Fi mode of AP and the column is the Wi-Fi mode of station. The "-" indicates Wi-Fi mode of the AP and station are not compatible.
  946. According to the table, the following conclusions can be drawn:
  947. - For LR-enabled AP of {IDF_TARGET_NAME}, it is incompatible with traditional 802.11 mode, because the beacon is sent in LR mode.
  948. - For LR-enabled station of {IDF_TARGET_NAME} whose mode is NOT LR-only mode, it is compatible with traditional 802.11 mode.
  949. - If both station and AP are {IDF_TARGET_NAME} devices and both of them have enabled LR mode, the negotiated mode supports LR.
  950. If the negotiated Wi-Fi mode supports both traditional 802.11 mode and LR mode, it is the Wi-Fi driver's responsibility to automatically select the best data rate in different Wi-Fi modes and the application can ignore it.
  951. LR Impacts to Traditional Wi-Fi Device
  952. ***************************************
  953. The data transmission in LR rate has no impacts on the traditional Wi-Fi device because:
  954. - The CCA and backoff process in LR mode are consistent with 802.11 specification.
  955. - The traditional Wi-Fi device can detect the LR signal via CCA and do backoff.
  956. In other words, the transmission impact in LR mode is similar to that in 802.11b mode.
  957. LR Transmission Distance
  958. *************************
  959. The reception sensitivity gain of LR is about 4 dB larger than that of the traditional 802.11b mode. Theoretically, the transmission distance is about 2 to 2.5 times the distance of 11B.
  960. LR Throughput
  961. *************************
  962. The LR rate has very limited throughput, because the raw PHY data rate LR is 1/2 Mbps and 1/4 Mbps.
  963. When to Use LR
  964. *************************
  965. The general conditions for using LR are:
  966. - Both the AP and station are Espressif devices.
  967. - Long distance Wi-Fi connection and data transmission is required.
  968. - Data throughput requirements are very small, such as remote device control.
  969. Wi-Fi Country Code
  970. +++++++++++++++++++++++++
  971. Call :cpp:func:`esp_wifi_set_country()` to set the country info. The table below describes the fields in detail. Please consult local 2.4 GHz RF operating regulations before configuring these fields.
  972. .. list-table::
  973. :header-rows: 1
  974. :widths: 15 55
  975. * - Field
  976. - Description
  977. * - cc[3]
  978. - Country code string. This attribute identifies the country or noncountry entity in which the station/AP is operating. If it is a country, the first two octets of this string is the two-character country info as described in the document ISO/IEC3166-1. The third octect is one of the following:
  979. - an ASCII space character, which means the regulations under which the station/AP is operating encompass all environments for the current frequency band in the country.
  980. - an ASCII ‘O’ character, which means the regulations under which the station/AP is operating are for an outdoor environment only.
  981. - an ASCII ‘I’ character, which means the regulations under which the station/AP is operating are for an indoor environment only.
  982. - an ASCII ‘X’ character, which means the station/AP is operating under a noncountry entity. The first two octets of the noncountry entity is two ASCII ‘XX’ characters.
  983. - the binary representation of the Operating Class table number currently in use. Refer to Annex E of IEEE Std 802.11-2020.
  984. * - schan
  985. - Start channel. It is the minimum channel number of the regulations under which the station/AP can operate.
  986. * - nchan
  987. - Total number of channels as per the regulations. For example, if the schan=1, nchan=13, then the station/AP can send data from channel 1 to 13.
  988. * - policy
  989. - Country policy. This field controls which country info will be used if the configured country info is in conflict with the connected AP’s. For more details on related policies, see the following section.
  990. The default country info is::
  991. wifi_country_t config = {
  992. .cc = "01",
  993. .schan = 1,
  994. .nchan = 11,
  995. .policy = WIFI_COUNTRY_POLICY_AUTO,
  996. };
  997. If the Wi-Fi Mode is station/AP coexist mode, they share the same configured country info. Sometimes, the country info of AP, to which the station is connected, is different from the country info of configured. For example, the configured station has country info::
  998. wifi_country_t config = {
  999. .cc = "JP",
  1000. .schan = 1,
  1001. .nchan = 14,
  1002. .policy = WIFI_COUNTRY_POLICY_AUTO,
  1003. };
  1004. but the connected AP has country info::
  1005. wifi_country_t config = {
  1006. .cc = "CN",
  1007. .schan = 1,
  1008. .nchan = 13,
  1009. };
  1010. then country info of connected AP's is used.
  1011. The following table depicts which country info is used in different Wi-Fi modes and different country policies, and it also describes the impact on active scan.
  1012. .. list-table::
  1013. :header-rows: 1
  1014. :widths: 15 15 35
  1015. * - Wi-Fi Mode
  1016. - Policy
  1017. - Description
  1018. * - Station
  1019. - WIFI_COUNTRY_POLICY_AUTO
  1020. - If the connected AP has country IE in its beacon, the country info equals to the country info in beacon. Otherwise, use the default country info.
  1021. For scan:
  1022. - If schan+nchan-1 >11 :
  1023. Use active scan from schan to 11 and use passive scan from 12 to 14.
  1024. - If schan+nchan-1 <= 11 :
  1025. Use active scan from schan to schan+nchan-1 and use passive scan from schan+nchan to 14.
  1026. Always keep in mind that if an AP with hidden SSID and station is set to a passive scan channel, the passive scan will not find it. In other words, if the application hopes to find the AP with hidden SSID in every channel, the policy of country info should be configured to WIFI_COUNTRY_POLICY_MANUAL.
  1027. * - Station
  1028. - WIFI_COUNTRY_POLICY_MANUAL
  1029. - Always use the configured country info.
  1030. For scan:
  1031. - If schan+nchan-1 >11 :
  1032. Use active scan from schan to 11 and use passive scan from 12 to schan+nchan-1.
  1033. - If schan+nchan-1 <= 11 :
  1034. Use active scan from schan to schan+nchan-1.
  1035. * - AP
  1036. - WIFI_COUNTRY_POLICY_AUTO
  1037. - Always use the configured country info.
  1038. * - AP
  1039. - WIFI_COUNTRY_POLICY_MANUAL
  1040. - Always use the configured country info.
  1041. * - Station/AP-coexistence
  1042. - WIFI_COUNTRY_POLICY_AUTO
  1043. - If the station does not connect to any external AP, the AP uses the configured country info. If the station connects to an external AP, the AP has the same country info as the station.
  1044. Same as station mode with policy WIFI_COUNTRY_POLICY_AUTO.
  1045. Home Channel
  1046. *************************
  1047. In AP mode, the home channel is defined as the AP channel. In station mode, home channel is defined as the channel of AP which the station is connected to. In station/AP-coexistence mode, the home channel of AP and station must be the same, and if they are different, the station's home channel is always in priority. For example, assume that the AP is on channel 6, and the station connects to an AP whose channel is 9. Since the station's home channel has higher priority, the AP needs to switch its channel from 6 to 9 to make sure that it has the same home channel as the station. While switching channel, the {IDF_TARGET_NAME} in AP mode will notify the connected stations about the channel migration using a Channel Switch Announcement (CSA). Station that supports channel switching will transit without disconnecting and reconnecting to the AP.
  1048. Wi-Fi Vendor IE Configuration
  1049. +++++++++++++++++++++++++++++++++++
  1050. By default, all Wi-Fi management frames are processed by the Wi-Fi driver, and the application can ignore them. However, some applications may have to handle the beacon, probe request, probe response, and other management frames. For example, if you insert some vendor-specific IE into the management frames, it is only the management frames which contain this vendor-specific IE that will be processed. In {IDF_TARGET_NAME}, :cpp:func:`esp_wifi_set_vendor_ie()` and :cpp:func:`esp_wifi_set_vendor_ie_cb()` are responsible for this kind of tasks.
  1051. Wi-Fi Easy Connect™ (DPP)
  1052. --------------------------
  1053. Wi-Fi Easy Connect\ :sup:`TM` (or Device Provisioning Protocol) is a secure and standardized provisioning protocol for configuring Wi-Fi devices. More information can be found in :doc:`esp_dpp <../api-reference/network/esp_dpp>`.
  1054. WPA2-Enterprise
  1055. +++++++++++++++++++++++++++++++++
  1056. WPA2-Enterprise is the secure authentication mechanism for enterprise wireless networks. It uses RADIUS server for authentication of network users before connecting to the Access Point. The authentication process is based on 802.1X policy and comes with different Extended Authentication Protocol (EAP) methods such as TLS, TTLS, and PEAP. RADIUS server authenticates the users based on their credentials (username and password), digital certificates, or both. When {IDF_TARGET_NAME} in station mode tries to connect an AP in enterprise mode, it sends authentication request to AP which is sent to RADIUS server by AP for authenticating the station. Based on different EAP methods, the parameters can be set in configuration which can be opened using ``idf.py menuconfig``. WPA2_Enterprise is supported by {IDF_TARGET_NAME} only in station mode.
  1057. For establishing a secure connection, AP and station negotiate and agree on the best possible cipher suite to be used. {IDF_TARGET_NAME} supports 802.1X/EAP (WPA) method of AKM and Advanced encryption standard with Counter Mode Cipher Block Chaining Message Authentication protocol (AES-CCM) cipher suite. It also supports the cipher suites supported by mbedtls if `USE_MBEDTLS_CRYPTO` flag is set.
  1058. {IDF_TARGET_NAME} currently supports the following EAP methods:
  1059. - EAP-TLS: This is a certificate-based method and only requires SSID and EAP-IDF.
  1060. - PEAP: This is a Protected EAP method. Username and Password are mandatory.
  1061. - EAP-TTLS: This is a credential-based method. Only server authentication is mandatory while user authentication is optional. Username and Password are mandatory. It supports different Phase2 methods, such as:
  1062. - PAP: Password Authentication Protocol.
  1063. - CHAP: Challenge Handshake Authentication Protocol.
  1064. - MSCHAP and MSCHAP-V2.
  1065. - EAP-FAST: This is an authentication method based on Protected Access Credentials (PAC) which also uses identity and password. Currently, USE_MBEDTLS_CRYPTO flag should be disabled to use this feature.
  1066. Detailed information on creating certificates and how to run wpa2_enterprise example on {IDF_TARGET_NAME} can be found in :example:`wifi/wifi_enterprise`.
  1067. Wireless Network Management
  1068. ----------------------------
  1069. Wireless Network Management allows client devices to exchange information about the network topology, including information related to RF environment. This makes each client network-aware, facilitating overall improvement in the performace of the wireless network. It is part of 802.11v specification. It also enables the client to support Network assisted Roaming.
  1070. - Network assisted Roaming: Enables WLAN to send messages to associated clients, resulting clients to associate with APs with better link metrics. This is useful for both load balancing and in directing poorly connected clients.
  1071. Current implementation of 802.11v includes support for BSS transition management frames.
  1072. Radio Resource Measurement
  1073. ---------------------------
  1074. Radio Resource Measurement (802.11k) is intended to improve the way traffic is distributed within a network. In a WLAN, each device normally connects to the access point (AP) that provides the strongest signal. Depending on the number and geographic locations of the subscribers, this arrangement can sometimes lead to excessive demand on one AP and underutilization of others, resulting in degradation of overall network performance. In a network conforming to 802.11k, if the AP having the strongest signal is loaded to its full capacity, a wireless device can be moved to one of the underutilized APs. Even though the signal may be weaker, the overall throughput is greater because more efficient use is made of the network resources.
  1075. Current implementation of 802.11k includes support for beacon measurement report, link measurement report, and neighbor request.
  1076. Refer ESP-IDF example :idf_file:`examples/wifi/roaming/README.md` to set up and use these APIs. Example code only demonstrates how these APIs can be used, and the application should define its own algorithm and cases as required.
  1077. .. only:: esp32s2 or esp32c3
  1078. Wi-Fi Location
  1079. -------------------------------
  1080. Wi-Fi Location will improve the accuracy of a device's location data beyond the Access Point, which will enable creation of new and feature-rich applications and services such as geo-fencing, network management, and navigation. One of the protocols used to determine the device location with respect to the Access Point is Fine Timing Measurement which calculates Time-of-Flight of a Wi-Fi frame.
  1081. Fine Timing Measurement (FTM)
  1082. +++++++++++++++++++++++++++++
  1083. FTM is used to measure Wi-Fi Round Trip Time (Wi-Fi RTT) which is the time a Wi-Fi signal takes to travel from a device to another device and back again. Using Wi-Fi RTT, the distance between the devices can be calculated with a simple formula of `RTT * c / 2`, where c is the speed of light.
  1084. FTM uses timestamps given by Wi-Fi interface hardware at the time of arrival or departure of frames exchanged between a pair of devices. One entity called FTM Initiator (mostly a station device) discovers the FTM Responder (can be a station or an Access Point) and negotiates to start an FTM procedure. The procedure uses multiple Action frames sent in bursts and its ACK's to gather the timestamps data. FTM Initiator gathers the data in the end to calculate an average Round-Trip-Time.
  1085. {IDF_TARGET_NAME} supports FTM in below configuration:
  1086. - {IDF_TARGET_NAME} as FTM Initiator in station mode.
  1087. - {IDF_TARGET_NAME} as FTM Responder in AP mode.
  1088. Distance measurement using RTT is not accurate, and factors such as RF interference, multi-path travel, antenna orientation, and lack of calibration increase these inaccuracies. For better results, it is suggested to perform FTM between two {IDF_TARGET_NAME} devices as station and AP.
  1089. Refer to ESP-IDF example :idf_file:`examples/wifi/ftm/README.md` for steps on how to set up and perform FTM.
  1090. {IDF_TARGET_NAME} Wi-Fi Power-saving Mode
  1091. -----------------------------------------
  1092. Station Sleep
  1093. ++++++++++++++++++++++
  1094. Currently, {IDF_TARGET_NAME} Wi-Fi supports the Modem-sleep mode which refers to the legacy power-saving mode in the IEEE 802.11 protocol. Modem-sleep mode works in station-only mode and the station must connect to the AP first. If the Modem-sleep mode is enabled, station will switch between active and sleep state periodically. In sleep state, RF, PHY and BB are turned off in order to reduce power consumption. Station can keep connection with AP in modem-sleep mode.
  1095. Modem-sleep mode includes minimum and maximum power-saving modes. In minimum power-saving mode, station wakes up every DTIM to receive beacon. Broadcast data will not be lost because it is transmitted after DTIM. However, it cannot save much more power if DTIM is short for DTIM is determined by AP.
  1096. In maximum power-saving mode, station wakes up in every listen interval to receive beacon. This listen interval can be set to be longer than the AP DTIM period. Broadcast data may be lost because station may be in sleep state at DTIM time. If listen interval is longer, more power is saved, but broadcast data is more easy to lose. Listen interval can be configured by calling API :cpp:func:`esp_wifi_set_config()` before connecting to AP.
  1097. Call ``esp_wifi_set_ps(WIFI_PS_MIN_MODEM)`` to enable Modem-sleep minimum power-saving mode or ``esp_wifi_set_ps(WIFI_PS_MAX_MODEM)`` to enable Modem-sleep maximum power-saving mode after calling :cpp:func:`esp_wifi_init()`. When station connects to AP, Modem-sleep will start. When station disconnects from AP, Modem-sleep will stop.
  1098. Call ``esp_wifi_set_ps(WIFI_PS_NONE)`` to disable Modem-sleep entirely. This has much higher power consumption, but provides minimum latency for receiving Wi-Fi data in real time. When Modem-sleep is enabled, received Wi-Fi data can be delayed for as long as the DTIM period (minimum power-saving mode) or the listen interval (maximum power-saving mode). Disabling Modem-sleep entirely is not possible for Wi-Fi and Bluetooth coexist mode.
  1099. The default Modem-sleep mode is WIFI_PS_MIN_MODEM.
  1100. AP Sleep
  1101. +++++++++++++++++++++++++++++++
  1102. Currently, {IDF_TARGET_NAME} AP does not support all of the power-saving feature defined in Wi-Fi specification. To be specific, the AP only caches unicast data for the stations connect to this AP, but does not cache the multicast data for the stations. If stations connected to the {IDF_TARGET_NAME} AP are power-saving enabled, they may experience multicast packet loss.
  1103. In the future, all power-saving features will be supported on {IDF_TARGET_NAME} AP.
  1104. {IDF_TARGET_NAME} Wi-Fi Throughput
  1105. -----------------------------------
  1106. The table below shows the best throughput results gained in Espressif's lab and in a shielded box.
  1107. .. only:: esp32
  1108. .. list-table::
  1109. :header-rows: 1
  1110. :widths: 10 10 10 10 25
  1111. * - Type/Throughput
  1112. - Air In Lab
  1113. - Shield-box
  1114. - Test Tool
  1115. - IDF Version (commit ID)
  1116. * - Raw 802.11 Packet RX
  1117. - N/A
  1118. - **130 MBit/s**
  1119. - Internal tool
  1120. - NA
  1121. * - Raw 802.11 Packet TX
  1122. - N/A
  1123. - **130 MBit/s**
  1124. - Internal tool
  1125. - NA
  1126. * - UDP RX
  1127. - 30 MBit/s
  1128. - 85 MBit/s
  1129. - iperf example
  1130. - 15575346
  1131. * - UDP TX
  1132. - 30 MBit/s
  1133. - 75 MBit/s
  1134. - iperf example
  1135. - 15575346
  1136. * - TCP RX
  1137. - 20 MBit/s
  1138. - 65 MBit/s
  1139. - iperf example
  1140. - 15575346
  1141. * - TCP TX
  1142. - 20 MBit/s
  1143. - 75 MBit/s
  1144. - iperf example
  1145. - 15575346
  1146. When the throughput is tested by iperf example, the sdkconfig is :idf_file:`examples/wifi/iperf/sdkconfig.defaults.esp32`.
  1147. .. only:: esp32s2
  1148. .. list-table::
  1149. :header-rows: 1
  1150. :widths: 10 10 10 10 25
  1151. * - Type/Throughput
  1152. - Air In Lab
  1153. - Shield-box
  1154. - Test Tool
  1155. - IDF Version (commit ID)
  1156. * - Raw 802.11 Packet RX
  1157. - N/A
  1158. - **130 MBit/s**
  1159. - Internal tool
  1160. - NA
  1161. * - Raw 802.11 Packet TX
  1162. - N/A
  1163. - **130 MBit/s**
  1164. - Internal tool
  1165. - NA
  1166. * - UDP RX
  1167. - 30 MBit/s
  1168. - 70 MBit/s
  1169. - iperf example
  1170. - 15575346
  1171. * - UDP TX
  1172. - 30 MBit/s
  1173. - 50 MBit/s
  1174. - iperf example
  1175. - 15575346
  1176. * - TCP RX
  1177. - 20 MBit/s
  1178. - 32 MBit/s
  1179. - iperf example
  1180. - 15575346
  1181. * - TCP TX
  1182. - 20 MBit/s
  1183. - 37 MBit/s
  1184. - iperf example
  1185. - 15575346
  1186. When the throughput is tested by iperf example, the sdkconfig is :idf_file:`examples/wifi/iperf/sdkconfig.defaults.esp32s2`.
  1187. .. only:: esp32c3
  1188. .. list-table::
  1189. :header-rows: 1
  1190. :widths: 10 10 10 15 20
  1191. * - Type/Throughput
  1192. - Air In Lab
  1193. - Shield-box
  1194. - Test Tool
  1195. - IDF Version (commit ID)
  1196. * - Raw 802.11 Packet RX
  1197. - N/A
  1198. - **130 MBit/s**
  1199. - Internal tool
  1200. - NA
  1201. * - Raw 802.11 Packet TX
  1202. - N/A
  1203. - **130 MBit/s**
  1204. - Internal tool
  1205. - NA
  1206. * - UDP RX
  1207. - 30 MBit/s
  1208. - 50 MBit/s
  1209. - iperf example
  1210. - 15575346
  1211. * - UDP TX
  1212. - 30 MBit/s
  1213. - 40 MBit/s
  1214. - iperf example
  1215. - 15575346
  1216. * - TCP RX
  1217. - 20 MBit/s
  1218. - 35 MBit/s
  1219. - iperf example
  1220. - 15575346
  1221. * - TCP TX
  1222. - 20 MBit/s
  1223. - 37 MBit/s
  1224. - iperf example
  1225. - 15575346
  1226. When the throughput is tested by iperf example, the sdkconfig is :idf_file:`examples/wifi/iperf/sdkconfig.defaults.esp32c3`.
  1227. .. only:: esp32s3
  1228. .. list-table::
  1229. :header-rows: 1
  1230. :widths: 10 10 10 15 20
  1231. * - Type/Throughput
  1232. - Air In Lab
  1233. - Shield-box
  1234. - Test Tool
  1235. - IDF Version (commit ID)
  1236. * - Raw 802.11 Packet RX
  1237. - N/A
  1238. - **130 MBit/s**
  1239. - Internal tool
  1240. - NA
  1241. * - Raw 802.11 Packet TX
  1242. - N/A
  1243. - **130 MBit/s**
  1244. - Internal tool
  1245. - NA
  1246. * - UDP RX
  1247. - 30 MBit/s
  1248. - 88 MBit/s
  1249. - iperf example
  1250. - 15575346
  1251. * - UDP TX
  1252. - 30 MBit/s
  1253. - 98 MBit/s
  1254. - iperf example
  1255. - 15575346
  1256. * - TCP RX
  1257. - 20 MBit/s
  1258. - 73 MBit/s
  1259. - iperf example
  1260. - 15575346
  1261. * - TCP TX
  1262. - 20 MBit/s
  1263. - 83 MBit/s
  1264. - iperf example
  1265. - 15575346
  1266. When the throughput is tested by iperf example, the sdkconfig is :idf_file:`examples/wifi/iperf/sdkconfig.defaults.esp32s3`.
  1267. Wi-Fi 80211 Packet Send
  1268. ---------------------------
  1269. The :cpp:func:`esp_wifi_80211_tx()` API can be used to:
  1270. - Send the beacon, probe request, probe response, and action frame.
  1271. - Send the non-QoS data frame.
  1272. It cannot be used for sending encrypted or QoS frames.
  1273. Preconditions of Using :cpp:func:`esp_wifi_80211_tx()`
  1274. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1275. - The Wi-Fi mode is station, or AP, or station/AP.
  1276. - Either esp_wifi_set_promiscuous(true), or :cpp:func:`esp_wifi_start()`, or both of these APIs return ESP_OK. This is because Wi-Fi hardware must be initialized before :cpp:func:`esp_wifi_80211_tx()` is called. In {IDF_TARGET_NAME}, both esp_wifi_set_promiscuous(true) and :cpp:func:`esp_wifi_start()` can trigger the initialization of Wi-Fi hardware.
  1277. - The parameters of :cpp:func:`esp_wifi_80211_tx()` are hereby correctly provided.
  1278. Data Rate
  1279. +++++++++++++++++++++++++++++++++++++++++++++++
  1280. - The default data rate is 1 Mbps.
  1281. - Can set any rate through :cpp:func:`esp_wifi_config_80211_tx_rate()` API.
  1282. - Can set any bandwidth through :cpp:func:`esp_wifi_set_bandwidth()` API.
  1283. Side-Effects to Avoid in Different Scenarios
  1284. +++++++++++++++++++++++++++++++++++++++++++++++++++++
  1285. Theoretically, if the side-effects the API imposes on the Wi-Fi driver or other stations/APs are not considered, a raw 802.11 packet can be sent over the air with any destination MAC, any source MAC, any BSSID, or any other types of packet. However, robust or useful applications should avoid such side-effects. The table below provides some tips and recommendations on how to avoid the side-effects of :cpp:func:`esp_wifi_80211_tx()` in different scenarios.
  1286. .. list-table::
  1287. :header-rows: 1
  1288. :widths: 10 50
  1289. * - Scenario
  1290. - Description
  1291. * - No Wi-Fi connection
  1292. - In this scenario, no Wi-Fi connection is set up, so there are no side-effects on the Wi-Fi driver. If en_sys_seq==true, the Wi-Fi driver is responsible for the sequence control. If en_sys_seq==false, the application needs to ensure that the buffer has the correct sequence.
  1293. Theoretically, the MAC address can be any address. However, this may impact other stations/APs with the same MAC/BSSID.
  1294. Side-effect example#1 The application calls :cpp:func:`esp_wifi_80211_tx()` to send a beacon with BSSID == mac_x in AP mode, but the mac_x is not the MAC of the AP interface. Moreover, there is another AP, e.g., “other-AP”, whose BSSID is mac_x. If this happens, an “unexpected behavior” may occur, because the stations which connect to the “other-AP” cannot figure out whether the beacon is from the “other-AP” or the :cpp:func:`esp_wifi_80211_tx()`.
  1295. To avoid the above-mentioned side-effects, it is recommended that:
  1296. - If esp_wifi_80211_tx is called in station mode, the first MAC should be a multicast MAC or the exact target-device’s MAC, while the second MAC should be that of the station interface.
  1297. - If esp_wifi_80211_tx is called in AP mode, the first MAC should be a multicast MAC or the exact target-device’s MAC, while the second MAC should be that of the AP interface.
  1298. The recommendations above are only for avoiding side-effects and can be ignored when there are good reasons.
  1299. * - Have Wi-Fi connection
  1300. - When the Wi-Fi connection is already set up, and the sequence is controlled by the application, the latter may impact the sequence control of the Wi-Fi connection as a whole. So, the en_sys_seq need to be true, otherwise ESP_ERR_WIFI_ARG is returned.
  1301. The MAC-address recommendations in the “No Wi-Fi connection” scenario also apply to this scenario.
  1302. If the Wi-Fi mode is station mode, the MAC address1 is the MAC of AP to which the station is connected, and the MAC address2 is the MAC of station interface, it is said that the packet is sent from the station to AP. Otherwise, if the Wi-Fi is in AP mode, the MAC address1 is the MAC of the station that connects to this AP, and the MAC address2 is the MAC of AP interface, it is said that the packet is sent from the AP to station. To avoid conflicting with Wi-Fi connections, the following checks are applied:
  1303. - If the packet type is data and is sent from the station to AP, the ToDS bit in IEEE 80211 frame control should be 1 and the FromDS bit should be 0. Otherwise, the packet will be discarded by Wi-Fi driver.
  1304. - If the packet type is data and is sent from the AP to station, the ToDS bit in IEEE 80211 frame control should be 0 and the FromDS bit should be 1. Otherwise, the packet will be discarded by Wi-Fi driver.
  1305. - If the packet is sent from station to AP or from AP to station, the Power Management, More Data, and Re-Transmission bits should be 0. Otherwise, the packet will be discarded by Wi-Fi driver.
  1306. ESP_ERR_WIFI_ARG is returned if any check fails.
  1307. Wi-Fi Sniffer Mode
  1308. ---------------------------
  1309. The Wi-Fi sniffer mode can be enabled by esp_wifi_set_promiscuous(). If the sniffer mode is enabled, the following packets **can** be dumped to the application:
  1310. - 802.11 Management frame.
  1311. - 802.11 Data frame, including MPDU, AMPDU, and AMSDU.
  1312. - 802.11 MIMO frame, for MIMO frame, the sniffer only dumps the length of the frame.
  1313. - 802.11 Control frame.
  1314. - 802.11 CRC error frame.
  1315. The following packets will **NOT** be dumped to the application:
  1316. - Other 802.11 error frames.
  1317. For frames that the sniffer **can** dump, the application can additionally decide which specific type of packets can be filtered to the application by using :cpp:func:`esp_wifi_set_promiscuous_filter()` and :cpp:func:`esp_wifi_set_promiscuous_ctrl_filter()`. By default, it will filter all 802.11 data and management frames to the application.
  1318. The Wi-Fi sniffer mode can be enabled in the Wi-Fi mode of :cpp:enumerator:`WIFI_MODE_NULL`, :cpp:enumerator:`WIFI_MODE_STA`, :cpp:enumerator:`WIFI_MODE_AP`, or :cpp:enumerator:`WIFI_MODE_APSTA`. In other words, the sniffer mode is active when the station is connected to the AP, or when the AP has a Wi-Fi connection. Please note that the sniffer has a **great impact** on the throughput of the station or AP Wi-Fi connection. Generally, the sniffer should be enabled **only if** the station/AP Wi-Fi connection does not experience heavy traffic.
  1319. Another noteworthy issue about the sniffer is the callback wifi_promiscuous_cb_t. The callback will be called directly in the Wi-Fi driver task, so if the application has a lot of work to do for each filtered packet, the recommendation is to post an event to the application task in the callback and defer the real work to the application task.
  1320. Wi-Fi Multiple Antennas
  1321. --------------------------
  1322. The Wi-Fi multiple antennas selecting can be depicted as following picture::
  1323. __________
  1324. |Enabled |
  1325. ___|Antenna 0 |\\ _________
  1326. |__________| \\ GPIO[0] <----> antenna_select[0] ---| | --- antenna 0
  1327. RX/TX ___ \\____\ GPIO[1] <----> antenna_select[1] ---| Antenna | --- antenna 1
  1328. \ __________ // / GPIO[2] <----> antenna_select[2] ---| Switch | ... ...
  1329. \ ___|Enabled | // GPIO[3] <----> antenna_select[3] ---|_________| --- antenna 15
  1330. \ |Antenna 1 |//
  1331. |__________|
  1332. {IDF_TARGET_NAME} supports up to sixteen antennas through external antenna switch. The antenna switch can be controlled by up to four address pins - antenna_select[0:3]. Different input value of antenna_select[0:3] means selecting different antenna. For example, the value '0b1011' means the antenna 11 is selected. The default value of antenna_select[3:0] is '0b0000', which means the antenna 0 is selected by default.
  1333. Up to four GPIOs are connected to the four active high antenna_select pins. {IDF_TARGET_NAME} can select the antenna by control the GPIO[0:3]. The API :cpp:func:`esp_wifi_set_ant_gpio()` is used to configure which GPIOs are connected to antenna_selects. If GPIO[x] is connected to antenna_select[x], then gpio_config->gpio_cfg[x].gpio_select should be set to 1 and gpio_config->gpio_cfg[x].gpio_num should be provided.
  1334. For the specific implementation of the antenna switch, there may be illegal values in `antenna_select[0:3]`. It means that {IDF_TARGET_NAME} may support less than sixteen antennas through the switch. For example, ESP32-WROOM-DA which uses RTC6603SP as the antenna switch, supports two antennas. Two GPIOs are connected to two active high antenna selection inputs. The value '0b01' means the antenna 0 is selected, the value '0b10' means the antenna 1 is selected. Values '0b00' and '0b11' are illegal.
  1335. Although up to sixteen antennas are supported, only one or two antennas can be simultaneously enabled for RX/TX. The API :cpp:func:`esp_wifi_set_ant()` is used to configure which antennas are enabled.
  1336. The enabled antennas selecting algorithm is also configured by :cpp:func:`esp_wifi_set_ant()`. The RX/TX antenna mode can be :cpp:enumerator:`WIFI_ANT_MODE_ANT0`, :cpp:enumerator:`WIFI_ANT_MODE_ANT1`, or :cpp:enumerator:`WIFI_ANT_MODE_AUTO`. If the antenna mode is :cpp:enumerator:`WIFI_ANT_MODE_ANT0`, the enabled antenna 0 is selected for RX/TX data. If the antenna mode is :cpp:enumerator:`WIFI_ANT_MODE_ANT1`, the enabled antenna 1 is selected for RX/TX data. Otherwise, Wi-Fi automatically selects the enabled antenna that has better signal.
  1337. If the RX antenna mode is :cpp:enumerator:`WIFI_ANT_MODE_AUTO`, the default antenna mode also needs to be set, because the RX antenna switching only happens when some conditions are met. For example, the RX antenna starts to switch if the RSSI is lower than -65 dBm or another antenna has better signal. RX uses the default antenna if the conditions are not met. If the default antenna mode is :cpp:enumerator:`WIFI_ANT_MODE_ANT1`, the enabled antenna 1 is used as the default RX antenna, otherwise the enabled antenna 0 is used.
  1338. Some limitations need to be considered:
  1339. - The TX antenna can be set to :cpp:enumerator:`WIFI_ANT_MODE_AUTO` only if the RX antenna mode is :cpp:enumerator:`WIFI_ANT_MODE_AUTO`, because TX antenna selecting algorithm is based on RX antenna in :cpp:enumerator:`WIFI_ANT_MODE_AUTO` type.
  1340. - Currently, Bluetooth® does not support the multiple antennas feature, so please do not use multiple antennas related APIs.
  1341. Following is the recommended scenarios to use the multiple antennas:
  1342. - In Wi-Fi mode :cpp:enumerator:`WIFI_MODE_STA`, both RX/TX antenna modes are configured to :cpp:enumerator:`WIFI_ANT_MODE_AUTO`. The Wi-Fi driver selects the better RX/TX antenna automatically.
  1343. - - The RX antenna mode is configured to :cpp:enumerator:`WIFI_ANT_MODE_AUTO`. The TX antenna mode is configured to :cpp:enumerator:`WIFI_ANT_MODE_ANT0` or :cpp:enumerator:`WIFI_ANT_MODE_ANT1`. The applications can choose to always select a specified antenna for TX, or implement their own TX antenna selecting algorithm, e.g., selecting the TX antenna mode based on the channel switch information.
  1344. - Both RX/TX antenna modes are configured to WIFI_ANT_MODE_ANT0 or WIFI_ANT_MODE_ANT1.
  1345. Wi-Fi Multiple Antennas Configuration
  1346. +++++++++++++++++++++++++++++++++++++
  1347. Generally, following steps can be taken to configure the multiple antennas:
  1348. - Configure which GPIOs are connected to the antenna_selects. For example, if four antennas are supported and GPIO20/GPIO21 are connected to antenna_select[0]/antenna_select[1], the configurations look like::
  1349. wifi_ant_gpio_config_t config = {
  1350. { .gpio_select = 1, .gpio_num = 20 },
  1351. { .gpio_select = 1, .gpio_num = 21 }
  1352. };
  1353. - Configure which antennas are enabled and how RX/TX use the enabled antennas. For example, if antenna1 and antenna3 are enabled, the RX needs to select the better antenna automatically and uses antenna1 as its default antenna, the TX always selects the antenna3. The configuration looks like::
  1354. wifi_ant_config_t config = {
  1355. .rx_ant_mode = WIFI_ANT_MODE_AUTO,
  1356. .rx_ant_default = WIFI_ANT_ANT0,
  1357. .tx_ant_mode = WIFI_ANT_MODE_ANT1,
  1358. .enabled_ant0 = 1,
  1359. .enabled_ant1 = 3
  1360. };
  1361. .. only:: SOC_WIFI_CSI_SUPPORT
  1362. Wi-Fi Channel State Information
  1363. ------------------------------------
  1364. Channel state information (CSI) refers to the channel information of a Wi-Fi connection. In {IDF_TARGET_NAME}, this information consists of channel frequency responses of sub-carriers and is estimated when packets are received from the transmitter. Each channel frequency response of sub-carrier is recorded by two bytes of signed characters. The first one is imaginary part and the second one is real part. There are up to three fields of channel frequency responses according to the type of received packet. They are legacy long training field (LLTF), high throughput LTF (HT-LTF), and space time block code HT-LTF (STBC-HT-LTF). For different types of packets which are received on channels with different state, the sub-carrier index and total bytes of signed characters of CSI are shown in the following table.
  1365. +-------------+--------------------+-----------------------------------------+--------------------------------------------------------+----------------------------------------------------------+
  1366. | channel | secondary channel | none | below | above |
  1367. +-------------+--------------------+-------------+---------------------------+----------+---------------------------------------------+----------+-----------------------------------------------+
  1368. | packet | signal mode | non HT | HT | non HT | HT | non HT | HT |
  1369. + +--------------------+-------------+---------------------------+----------+-----------------+---------------------------+----------+-------------------+---------------------------+
  1370. | information | channel bandwidth | 20 MHz | 20 MHz | 20 MHz | 20 MHz | 40 MHz | 20 MHz | 20 MHz | 40 MHz |
  1371. + +--------------------+-------------+-------------+-------------+----------+----------+------+-------------+-------------+----------+----------+--------+-------------+-------------+
  1372. | | STBC | non STBC | non STBC | STBC | non STBC | non STBC | STBC | non STBC | STBC | non STBC | non STBC | STBC | non STBC | STBC |
  1373. +-------------+--------------------+-------------+-------------+-------------+----------+----------+------+-------------+-------------+----------+----------+--------+-------------+-------------+
  1374. | sub-carrier | LLTF | 0~31, -32~-1| 0~31, -32~-1| 0~31, -32~-1| 0~63 | 0~63 | 0~63 | 0~63 | 0~63 | -64~-1 | -64~-1 | -64~-1 | -64~-1 | -64~-1 |
  1375. + +--------------------+-------------+-------------+-------------+----------+----------+------+-------------+-------------+----------+----------+--------+-------------+-------------+
  1376. | index | HT-LTF | - | 0~31, -32~-1| 0~31, -32~-1| - | 0~63 | 0~62 | 0~63, -64~-1| 0~60, -60~-1| - | -64~-1 | -62~-1 | 0~63, -64~-1| 0~60, -60~-1|
  1377. + +--------------------+-------------+-------------+-------------+----------+----------+------+-------------+-------------+----------+----------+--------+-------------+-------------+
  1378. | | STBC-HT-LTF | - | - | 0~31, -32~-1| - | - | 0~62 | - | 0~60, -60~-1| - | - | -62~-1 | - | 0~60, -60~-1|
  1379. +-------------+--------------------+-------------+-------------+-------------+----------+----------+------+-------------+-------------+----------+----------+--------+-------------+-------------+
  1380. | total bytes | 128 | 256 | 384 | 128 | 256 | 380 | 384 | 612 | 128 | 256 | 376 | 384 | 612 |
  1381. +----------------------------------+-------------+-------------+-------------+----------+----------+------+-------------+-------------+----------+----------+--------+-------------+-------------+
  1382. All of the information in the table can be found in the structure wifi_csi_info_t.
  1383. - Secondary channel refers to secondary_channel field of rx_ctrl field.
  1384. - Signal mode of packet refers to sig_mode field of rx_ctrl field.
  1385. - Channel bandwidth refers to cwb field of rx_ctrl field.
  1386. - STBC refers to stbc field of rx_ctrl field.
  1387. - Total bytes refers to len field.
  1388. - The CSI data corresponding to each Long Training Field (LTF) type is stored in a buffer starting from the buf field. Each item is stored as two bytes: imaginary part followed by real part. The order of each item is the same as the sub-carrier in the table. The order of LTF is: LLTF, HT-LTF, STBC-HT-LTF. However, all 3 LTFs may not be present, depending on the channel and packet information (see above).
  1389. - If first_word_invalid field of wifi_csi_info_t is true, it means that the first four bytes of CSI data is invalid due to a hardware limitation in {IDF_TARGET_NAME}.
  1390. - More information like RSSI, noise floor of RF, receiving time and antenna is in the rx_ctrl field.
  1391. When imaginary part and real part data of sub-carrier are used, please refer to the table below.
  1392. +----------------+-------------------+------------------------------+-------------------------+
  1393. | PHY standard | Sub-carrier range | Pilot sub-carrier | Sub-carrier(total/data) |
  1394. +================+===================+==============================+=========================+
  1395. | 802.11a/g | -26 to +26 | -21, -7, +7, +21 | 52 total, 48 usable |
  1396. +----------------+-------------------+------------------------------+-------------------------+
  1397. | 802.11n, 20MHz | -28 to +28 | -21, -7, +7, +21 | 56 total, 52 usable |
  1398. +----------------+-------------------+------------------------------+-------------------------+
  1399. | 802.11n, 40MHz | -57 to +57 | -53, -25, -11, +11, +25, +53 | 114 total, 108 usable |
  1400. +----------------+-------------------+------------------------------+-------------------------+
  1401. .. note::
  1402. - For STBC packet, CSI is provided for every space-time stream without CSD (cyclic shift delay). As each cyclic shift on the additional chains shall be -200 ns, only the CSD angle of first space-time stream is recorded in sub-carrier 0 of HT-LTF and STBC-HT-LTF for there is no channel frequency response in sub-carrier 0. CSD[10:0] is 11 bits, ranging from -pi to pi.
  1403. - If LLTF, HT-LTF, or STBC-HT-LTF is not enabled by calling API :cpp:func:`esp_wifi_set_csi_config()`, the total bytes of CSI data will be fewer than that in the table. For example, if LLTF and HT-LTF is not enabled and STBC-HT-LTF is enabled, when a packet is received with the condition above/HT/40MHz/STBC, the total bytes of CSI data is 244 ((61 + 60) * 2 + 2 = 244. The result is aligned to four bytes, and the last two bytes are invalid).
  1404. Wi-Fi Channel State Information Configure
  1405. -------------------------------------------
  1406. To use Wi-Fi CSI, the following steps need to be done.
  1407. - Select Wi-Fi CSI in menuconfig. Go to ``Menuconfig`` > ``Components config`` > ``Wi-Fi`` > ``Wi-Fi CSI (Channel State Information)``.
  1408. - Set CSI receiving callback function by calling API :cpp:func:`esp_wifi_set_csi_rx_cb()`.
  1409. - Configure CSI by calling API :cpp:func:`esp_wifi_set_csi_config()`.
  1410. - Enable CSI by calling API :cpp:func:`esp_wifi_set_csi()`.
  1411. The CSI receiving callback function runs from Wi-Fi task. So, do not do lengthy operations in the callback function. Instead, post necessary data to a queue and handle it from a lower priority task. Because station does not receive any packet when it is disconnected and only receives packets from AP when it is connected, it is suggested to enable sniffer mode to receive more CSI data by calling :cpp:func:`esp_wifi_set_promiscuous()`.
  1412. Wi-Fi HT20/40
  1413. -------------------------
  1414. .. only:: esp32 or esp32s2 or esp32c3 or esp32s3
  1415. {IDF_TARGET_NAME} supports Wi-Fi bandwidth HT20 or HT40 and does not support HT20/40 coexist. :cpp:func:`esp_wifi_set_bandwidth()` can be used to change the default bandwidth of station or AP. The default bandwidth for {IDF_TARGET_NAME} station and AP is HT40.
  1416. In station mode, the actual bandwidth is firstly negotiated during the Wi-Fi connection. It is HT40 only if both the station and the connected AP support HT40, otherwise it is HT20. If the bandwidth of connected AP is changes, the actual bandwidth is negotiated again without Wi-Fi disconnecting.
  1417. Similarly, in AP mode, the actual bandwidth is negotiated between AP and the stations that connect to the AP. It is HT40 if the AP and one of the stations support HT40, otherwise it is HT20.
  1418. In station/AP coexist mode, the station/AP can configure HT20/40 seperately. If both station and AP are negotiated to HT40, the HT40 channel should be the channel of station because the station always has higher priority than AP in {IDF_TARGET_NAME}. For example, the configured bandwidth of AP is HT40, the configured primary channel is 6, and the configured secondary channel is 10. The station is connected to an router whose primary channel is 6 and secondary channel is 2, then the actual channel of AP is changed to primary 6 and secondary 2 automatically.
  1419. Theoretically, the HT40 can gain better throughput because the maximum raw physicial (PHY) data rate for HT40 is 150 Mbps while it is 72 Mbps for HT20. However, if the device is used in some special environment, e.g., there are too many other Wi-Fi devices around the {IDF_TARGET_NAME} device, the performance of HT40 may be degraded. So if the applications need to support same or similar scenarios, it is recommended that the bandwidth is always configured to HT20.
  1420. .. only:: esp32c2
  1421. {IDF_TARGET_NAME} supports Wi-Fi bandwidth HT20 and does not support Wi-Fi bandwidth HT40 or HT20/40 coexist.
  1422. Wi-Fi QoS
  1423. -------------------------
  1424. {IDF_TARGET_NAME} supports all the mandatory features required in WFA Wi-Fi QoS Certification.
  1425. Four ACs (Access Category) are defined in Wi-Fi specification, and each AC has its own priority to access the Wi-Fi channel. Moreover, a map rule is defined to map the QoS priority of other protocol, e.g., 802.11D or TCP/IP precedence is mapped to Wi-Fi AC.
  1426. The table below describes how the IP Precedences are mapped to Wi-Fi ACs in {IDF_TARGET_NAME}. It also indicates whether the AMPDU is supported for this AC. The table is sorted from high to low priority. That is to say, the AC_VO has the highest priority.
  1427. +------------------+------------------------+-----------------+
  1428. | IP Precedence | Wi-Fi AC | Support AMPDU? |
  1429. +==================+========================+=================+
  1430. | 6, 7 | AC_VO (Voice) | No |
  1431. +------------------+------------------------+-----------------+
  1432. | 4, 5 | AC_VI (Video) | Yes |
  1433. +------------------+------------------------+-----------------+
  1434. | 3, 0 | AC_BE (Best Effort) | Yes |
  1435. +------------------+------------------------+-----------------+
  1436. | 1, 2 | AC_BK (Background) | Yes |
  1437. +------------------+------------------------+-----------------+
  1438. The application can make use of the QoS feature by configuring the IP precedence via socket option IP_TOS. Here is an example to make the socket to use VI queue::
  1439. const int ip_precedence_vi = 4;
  1440. const int ip_precedence_offset = 5;
  1441. int priority = (ip_precedence_vi << ip_precedence_offset);
  1442. setsockopt(socket_id, IPPROTO_IP, IP_TOS, &priority, sizeof(priority));
  1443. Theoretically, the higher priority AC has better performance than the lower priority AC. However, it is not always true. Here are some suggestions about how to use the Wi-Fi QoS:
  1444. - Some really important application traffic can be put into the AC_VO queue. But avoid using the AC_VO queue for heavy traffic, as it may impact the management frames which also use this queue. Eventually, it is worth noting that the AC_VO queue does not support AMPDU, and its performance with heavy traffic is no better than other queues.
  1445. - Avoid using more than two precedences supported by different AMPDUs, e.g., when socket A uses precedence 0, socket B uses precedence 1, and socket C uses precedence 2. This can be a bad design because it may need much more memory. To be specific, the Wi-Fi driver may generate a Block Ack session for each precedence and it needs more memory if the Block Ack session is set up.
  1446. Wi-Fi AMSDU
  1447. -------------------------
  1448. {IDF_TARGET_NAME} supports receiving and transmitting AMSDU.
  1449. Wi-Fi Fragment
  1450. -------------------------
  1451. .. only:: esp32 or esp32s2
  1452. supports Wi-Fi receiving fragment, but does not support Wi-Fi transmitting fragment.
  1453. .. only:: esp32c3 or esp32s3
  1454. {IDF_TARGET_NAME} supports Wi-Fi receiving and transmitting fragment.
  1455. WPS Enrollee
  1456. -------------------------
  1457. {IDF_TARGET_NAME} supports WPS enrollee feature in Wi-Fi mode :cpp:enumerator:`WIFI_MODE_STA` or :cpp:enumerator:`WIFI_MODE_APSTA`. Currently, {IDF_TARGET_NAME} supports WPS enrollee type PBC and PIN.
  1458. .. _wifi-buffer-usage:
  1459. Wi-Fi Buffer Usage
  1460. --------------------------
  1461. This section is only about the dynamic buffer configuration.
  1462. Why Buffer Configuration Is Important
  1463. +++++++++++++++++++++++++++++++++++++++
  1464. In order to get a high-performance system, consider the memory usage/configuration carefully for the following reasons:
  1465. - the available memory in {IDF_TARGET_NAME} is limited.
  1466. - currently, the default type of buffer in LwIP and Wi-Fi drivers is "dynamic", **which means that both the LwIP and Wi-Fi share memory with the application**. Programmers should always keep this in mind; otherwise, they will face a memory issue, such as "running out of heap memory".
  1467. - it is very dangerous to run out of heap memory, as this will cause {IDF_TARGET_NAME} an "undefined behavior". Thus, enough heap memory should be reserved for the application, so that it never runs out of it.
  1468. - the Wi-Fi throughput heavily depends on memory-related configurations, such as the TCP window size and Wi-Fi RX/TX dynamic buffer number.
  1469. - the peak heap memory that the {IDF_TARGET_NAME} LwIP/Wi-Fi may consume depends on a number of factors, such as the maximum TCP/UDP connections that the application may have.
  1470. - the total memory that the application requires is also an important factor when considering memory configuration.
  1471. Due to these reasons, there is not a good-for-all application configuration. Rather, it is recommended to consider memory configurations separately for every different application.
  1472. Dynamic vs. Static Buffer
  1473. ++++++++++++++++++++++++++++++
  1474. The default type of buffer in Wi-Fi drivers is "dynamic". Most of the time the dynamic buffer can significantly save memory. However, it makes the application programming a little more difficult, because in this case the application needs to consider memory usage in Wi-Fi.
  1475. lwIP also allocates buffers at the TCP/IP layer, and this buffer allocation is also dynamic. See :ref:`lwIP documentation section about memory use and performance <lwip-performance>`.
  1476. Peak Wi-Fi Dynamic Buffer
  1477. ++++++++++++++++++++++++++++++
  1478. The Wi-Fi driver supports several types of buffer (refer to `Wi-Fi Buffer Configure`_). However, this section is about the usage of the dynamic Wi-Fi buffer only.
  1479. The peak heap memory that Wi-Fi consumes is the **theoretically-maximum memory** that the Wi-Fi driver consumes. Generally, the peak memory depends on:
  1480. - the number of dynamic RX buffers that are configured: wifi_rx_dynamic_buf_num
  1481. - the number of dynamic TX buffers that are configured: wifi_tx_dynamic_buf_num
  1482. - the maximum packet size that the Wi-Fi driver can receive: wifi_rx_pkt_size_max
  1483. - the maximum packet size that the Wi-Fi driver can send: wifi_tx_pkt_size_max
  1484. So, the peak memory that the Wi-Fi driver consumes can be calculated with the following formula:
  1485. wifi_dynamic_peek_memory = (wifi_rx_dynamic_buf_num * wifi_rx_pkt_size_max) + (wifi_tx_dynamic_buf_num * wifi_tx_pkt_size_max)
  1486. Generally, the dynamic TX long buffers and dynamic TX long long buffers can be ignored, because they are management frames which only have a small impact on the system.
  1487. .. _How-to-improve-Wi-Fi-performance:
  1488. How to Improve Wi-Fi Performance
  1489. ----------------------------------
  1490. The performance of {IDF_TARGET_NAME} Wi-Fi is affected by many parameters, and there are mutual constraints between each parameter. A proper configuration cannot only improve performance, but also increase available memory for applications and improve stability.
  1491. This section briefly explains the operating mode of the Wi-Fi/LwIP protocol stack and the role of each parameter. It also gives several recommended configuration ranks to help choose the appropriate rank according to the usage scenario.
  1492. Protocol Stack Operation Mode
  1493. ++++++++++++++++++++++++++++++++++
  1494. .. figure:: ../../_static/api-guides-WiFi-driver-how-to-improve-WiFi-performance.png
  1495. :align: center
  1496. {IDF_TARGET_NAME} datapath
  1497. The {IDF_TARGET_NAME} protocol stack is divided into four layers: Application, LwIP, Wi-Fi, and Hardware.
  1498. - During receiving, hardware puts the received packet into DMA buffer, and then transfers it into the RX buffer of Wi-Fi and LwIP in turn for related protocol processing, and finally to the application layer. The Wi-Fi RX buffer and the LwIP RX buffer shares the same buffer by default. In other words, the Wi-Fi forwards the packet to LwIP by reference by default.
  1499. - During sending, the application copies the messages to be sent into the TX buffer of the LwIP layer for TCP/IP encapsulation. The messages will then be passed to the TX buffer of the Wi-Fi layer for MAC encapsulation and wait to be sent.
  1500. Parameters
  1501. ++++++++++++++
  1502. Increasing the size or number of the buffers mentioned above properly can improve Wi-Fi performance. Meanwhile, it will reduce available memory to the application. The following is an introduction to the parameters that users need to configure:
  1503. **RX direction:**
  1504. - :ref:`CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM`
  1505. This parameter indicates the number of DMA buffer at the hardware layer. Increasing this parameter will increase the sender's one-time receiving throughput, thereby improving the Wi-Fi protocol stack ability to handle burst traffic.
  1506. - :ref:`CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM`
  1507. This parameter indicates the number of RX buffer in the Wi-Fi layer. Increasing this parameter will improve the performance of packet reception. This parameter needs to match the RX buffer size of the LwIP layer.
  1508. - :ref:`CONFIG_ESP32_WIFI_RX_BA_WIN`
  1509. This parameter indicates the size of the AMPDU BA Window at the receiving end. This parameter should be configured to the smaller value between twice of :ref:`CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM` and :ref:`CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM`.
  1510. - :ref:`CONFIG_LWIP_TCP_WND_DEFAULT`
  1511. This parameter represents the RX buffer size of the LwIP layer for each TCP stream. Its value should be configured to the value of WIFI_DYNAMIC_RX_BUFFER_NUM (KB) to reach a high and stable performance. Meanwhile, in case of multiple streams, this value needs to be reduced proportionally.
  1512. **TX direction:**
  1513. - :ref:`CONFIG_ESP32_WIFI_TX_BUFFER`
  1514. This parameter indicates the type of TX buffer, it is recommended to configure it as a dynamic buffer, which can make full use of memory.
  1515. - :ref:`CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM`
  1516. This parameter indicates the number of TX buffer on the Wi-Fi layer. Increasing this parameter will improve the performance of packet sending. The parameter value needs to match the TX buffer size of the LwIP layer.
  1517. - :ref:`CONFIG_LWIP_TCP_SND_BUF_DEFAULT`
  1518. This parameter represents the TX buffer size of the LwIP layer for each TCP stream. Its value should be configured to the value of WIFI_DYNAMIC_TX_BUFFER_NUM (KB) to reach a high and stable performance. In case of multiple streams, this value needs to be reduced proportionally.
  1519. **Throughput optimization by placing code in IRAM:**
  1520. .. only:: esp32 or esp32s2
  1521. - :ref:`CONFIG_ESP32_WIFI_IRAM_OPT`
  1522. If this option is enabled, some Wi-Fi functions are moved to IRAM, improving throughput. This increases IRAM usage by 15 kB.
  1523. - :ref:`CONFIG_ESP32_WIFI_RX_IRAM_OPT`
  1524. If this option is enabled, some Wi-Fi RX functions are moved to IRAM, improving throughput. This increases IRAM usage by 16 kB.
  1525. - :ref:`CONFIG_LWIP_IRAM_OPTIMIZATION`
  1526. If this option is enabled, some LwIP functions are moved to IRAM, improving throughput. This increases IRAM usage by 13 kB.
  1527. .. only:: esp32s2
  1528. **CACHE:**
  1529. - :ref:`CONFIG_ESP32S2_INSTRUCTION_CACHE_SIZE`
  1530. Configure the size of the instruction Cache.
  1531. - :ref:`CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_SIZE`
  1532. Configure the width of the instruction Cache bus.
  1533. .. only:: esp32s3
  1534. **CACHE:**
  1535. - :ref:`CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE`
  1536. Configure the size of the instruction Cache.
  1537. - :ref:`CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_SIZE`
  1538. Configure the size of the instruction Cache bus.
  1539. - :ref:`CONFIG_ESP32S3_ICACHE_ASSOCIATED_WAYS`
  1540. Configure the associated ways of the instruction Cache.
  1541. - :ref:`CONFIG_ESP32S3_DATA_CACHE_SIZE`
  1542. Configure the size of the Data Cache.
  1543. - :ref:`CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE`
  1544. Configure the line size of the Data Cache.
  1545. - :ref:`CONFIG_ESP32S3_DCACHE_ASSOCIATED_WAYS`
  1546. Configure the associated ways of the Data Cache.
  1547. .. note::
  1548. The buffer size mentioned above is fixed as 1.6 KB.
  1549. How to Configure Parameters
  1550. ++++++++++++++++++++++++++++
  1551. The memory of {IDF_TARGET_NAME} is shared by protocol stack and applications.
  1552. Here, several configuration ranks are given. In most cases, the user should select a suitable rank for parameter configuration according to the size of the memory occupied by the application.
  1553. The parameters not mentioned in the following table should be set to the default.
  1554. .. only:: esp32
  1555. .. list-table::
  1556. :header-rows: 1
  1557. :widths: 10 5 5 10 5 5 10 5
  1558. * - Rank
  1559. - Iperf
  1560. - TX prior
  1561. - High-performance
  1562. - RX prior
  1563. - Default
  1564. - Memory saving
  1565. - Minimum
  1566. * - Available memory (KB)
  1567. - 37.1
  1568. - 113.8
  1569. - 123.3
  1570. - 145.5
  1571. - 144.5
  1572. - 170.2
  1573. - 185.2
  1574. * - WIFI_STATIC_RX_BUFFER_NUM
  1575. - 16
  1576. - 6
  1577. - 6
  1578. - 6
  1579. - 6
  1580. - 6
  1581. - 4
  1582. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  1583. - 64
  1584. - 16
  1585. - 24
  1586. - 34
  1587. - 20
  1588. - 12
  1589. - 8
  1590. * - WIFI_DYNAMIC_TX_BUFFER_NUM
  1591. - 64
  1592. - 28
  1593. - 24
  1594. - 18
  1595. - 20
  1596. - 12
  1597. - 8
  1598. * - WIFI_RX_BA_WIN
  1599. - 32
  1600. - 8
  1601. - 12
  1602. - 12
  1603. - 10
  1604. - 6
  1605. - Disable
  1606. * - TCP_SND_BUF_DEFAULT (KB)
  1607. - 65
  1608. - 28
  1609. - 24
  1610. - 18
  1611. - 20
  1612. - 12
  1613. - 8
  1614. * - TCP_WND_DEFAULT (KB)
  1615. - 65
  1616. - 16
  1617. - 24
  1618. - 34
  1619. - 20
  1620. - 12
  1621. - 8
  1622. * - WIFI_IRAM_OPT
  1623. - 15
  1624. - 15
  1625. - 15
  1626. - 15
  1627. - 15
  1628. - 15
  1629. - 15
  1630. * - WIFI_RX_IRAM_OPT
  1631. - 16
  1632. - 16
  1633. - 16
  1634. - 16
  1635. - 16
  1636. - 16
  1637. - 16
  1638. * - LWIP_IRAM_OPTIMIZATION
  1639. - 13
  1640. - 13
  1641. - 13
  1642. - 13
  1643. - 13
  1644. - 13
  1645. - 13
  1646. * - TCP TX throughput (Mbit/s)
  1647. - 74.6
  1648. - 50.8
  1649. - 46.5
  1650. - 39.9
  1651. - 44.2
  1652. - 33.8
  1653. - 25.6
  1654. * - TCP RX throughput (Mbit/s)
  1655. - 63.6
  1656. - 35.5
  1657. - 42.3
  1658. - 48.5
  1659. - 40.5
  1660. - 30.1
  1661. - 27.8
  1662. * - UDP TX throughput (Mbit/s)
  1663. - 76.2
  1664. - 75.1
  1665. - 74.1
  1666. - 72.4
  1667. - 69.6
  1668. - 64.1
  1669. - 36.5
  1670. * - UDP RX throughput (Mbit/s)
  1671. - 83.1
  1672. - 66.3
  1673. - 75.1
  1674. - 75.6
  1675. - 73.1
  1676. - 65.3
  1677. - 54.7
  1678. .. only:: esp32s2
  1679. .. list-table::
  1680. :header-rows: 1
  1681. :widths: 10 10 10 10 10 10
  1682. * - Rank
  1683. - Iperf
  1684. - High-performance
  1685. - Default
  1686. - Memory saving
  1687. - Minimum
  1688. * - Available memory (KB)
  1689. - 4.1
  1690. - 24.2
  1691. - 78.4
  1692. - 86.5
  1693. - 116.4
  1694. * - WIFI_STATIC_RX_BUFFER_NUM
  1695. - 8
  1696. - 6
  1697. - 6
  1698. - 4
  1699. - 3
  1700. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  1701. - 24
  1702. - 18
  1703. - 12
  1704. - 8
  1705. - 6
  1706. * - WIFI_DYNAMIC_TX_BUFFER_NUM
  1707. - 24
  1708. - 18
  1709. - 12
  1710. - 8
  1711. - 6
  1712. * - WIFI_RX_BA_WIN
  1713. - 12
  1714. - 9
  1715. - 6
  1716. - 4
  1717. - 3
  1718. * - TCP_SND_BUF_DEFAULT (KB)
  1719. - 24
  1720. - 18
  1721. - 12
  1722. - 8
  1723. - 6
  1724. * - TCP_WND_DEFAULT (KB)
  1725. - 24
  1726. - 18
  1727. - 12
  1728. - 8
  1729. - 6
  1730. * - WIFI_IRAM_OPT
  1731. - 15
  1732. - 15
  1733. - 15
  1734. - 15
  1735. - 0
  1736. * - WIFI_RX_IRAM_OPT
  1737. - 16
  1738. - 16
  1739. - 16
  1740. - 0
  1741. - 0
  1742. * - LWIP_IRAM_OPTIMIZATION
  1743. - 13
  1744. - 13
  1745. - 0
  1746. - 0
  1747. - 0
  1748. * - INSTRUCTION_CACHE
  1749. - 16
  1750. - 16
  1751. - 16
  1752. - 16
  1753. - 8
  1754. * - INSTRUCTION_CACHE_LINE
  1755. - 16
  1756. - 16
  1757. - 16
  1758. - 16
  1759. - 16
  1760. * - TCP TX throughput (Mbit/s)
  1761. - 37.6
  1762. - 33.1
  1763. - 22.5
  1764. - 12.2
  1765. - 5.5
  1766. * - TCP RX throughput (Mbit/s)
  1767. - 31.5
  1768. - 28.1
  1769. - 20.1
  1770. - 13.1
  1771. - 7.2
  1772. * - UDP TX throughput (Mbit/s)
  1773. - 58.1
  1774. - 57.3
  1775. - 28.1
  1776. - 22.6
  1777. - 8.7
  1778. * - UDP RX throughput (Mbit/s)
  1779. - 78.1
  1780. - 66.7
  1781. - 65.3
  1782. - 53.8
  1783. - 28.5
  1784. .. only:: esp32c3
  1785. .. list-table::
  1786. :header-rows: 1
  1787. :widths: 10 10 10 15
  1788. * - Rank
  1789. - Iperf
  1790. - Default
  1791. - Minimum
  1792. * - Available memory (KB)
  1793. - 59
  1794. - 160
  1795. - 180
  1796. * - WIFI_STATIC_RX_BUFFER_NUM
  1797. - 20
  1798. - 8
  1799. - 3
  1800. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  1801. - 40
  1802. - 16
  1803. - 6
  1804. * - WIFI_DYNAMIC_TX_BUFFER_NUM
  1805. - 40
  1806. - 16
  1807. - 6
  1808. * - WIFI_RX_BA_WIN
  1809. - 32
  1810. - 16
  1811. - 6
  1812. * - TCP_SND_BUF_DEFAULT (KB)
  1813. - 40
  1814. - 16
  1815. - 6
  1816. * - TCP_WND_DEFAULT (KB)
  1817. - 40
  1818. - 16
  1819. - 6
  1820. * - LWIP_IRAM_OPTIMIZATION
  1821. - 13
  1822. - 13
  1823. - 0
  1824. * - TCP TX throughput (Mbit/s)
  1825. - 38.1
  1826. - 27.2
  1827. - 20.4
  1828. * - TCP RX throughput (Mbit/s)
  1829. - 35.3
  1830. - 24.2
  1831. - 17.4
  1832. * - UDP TX throughput (Mbit/s)
  1833. - 40.6
  1834. - 38.9
  1835. - 34.1
  1836. * - UDP RX throughput (Mbit/s)
  1837. - 52.4
  1838. - 44.5
  1839. - 44.2
  1840. .. only:: esp32c2
  1841. .. list-table::
  1842. :header-rows: 1
  1843. :widths: 10 10 10 15
  1844. * - Rank
  1845. - Iperf
  1846. - Default
  1847. - Minimum
  1848. * - Available memory (KB)
  1849. - 37
  1850. - 56
  1851. - 84
  1852. * - WIFI_STATIC_RX_BUFFER_NUM
  1853. - 14
  1854. - 7
  1855. - 3
  1856. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  1857. - 18
  1858. - 14
  1859. - 6
  1860. * - WIFI_DYNAMIC_TX_BUFFER_NUM
  1861. - 18
  1862. - 14
  1863. - 6
  1864. * - WIFI_RX_BA_WIN
  1865. - 16
  1866. - 12
  1867. - 6
  1868. * - TCP_SND_BUF_DEFAULT (KB)
  1869. - 18
  1870. - 14
  1871. - 6
  1872. * - TCP_WND_DEFAULT (KB)
  1873. - 18
  1874. - 14
  1875. - 6
  1876. * - LWIP_IRAM_OPTIMIZATION
  1877. - 13
  1878. - 13
  1879. - 0
  1880. * - TCP TX throughput (Mbit/s)
  1881. - 21.6
  1882. - 21.4
  1883. - 14.3
  1884. * - TCP RX throughput (Mbit/s)
  1885. - 19.1
  1886. - 17.9
  1887. - 12.4
  1888. * - UDP TX throughput (Mbit/s)
  1889. - 26.4
  1890. - 26.3
  1891. - 25.0
  1892. * - UDP RX throughput (Mbit/s)
  1893. - 32.3
  1894. - 31.5
  1895. - 27.7
  1896. .. only:: esp32s3
  1897. .. list-table::
  1898. :header-rows: 1
  1899. :widths: 25 20 25 25
  1900. * - Rank
  1901. - Iperf
  1902. - Default
  1903. - Minimum
  1904. * - Available memory (KB)
  1905. - 133.9
  1906. - 183.9
  1907. - 273.6
  1908. * - WIFI_STATIC_RX_BUFFER_NUM
  1909. - 24
  1910. - 8
  1911. - 3
  1912. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  1913. - 64
  1914. - 32
  1915. - 6
  1916. * - WIFI_DYNAMIC_TX_BUFFER_NUM
  1917. - 64
  1918. - 32
  1919. - 6
  1920. * - WIFI_RX_BA_WIN
  1921. - 32
  1922. - 16
  1923. - 6
  1924. * - TCP_SND_BUF_DEFAULT (KB)
  1925. - 64
  1926. - 32
  1927. - 6
  1928. * - TCP_WND_DEFAULT (KB)
  1929. - 64
  1930. - 32
  1931. - 6
  1932. * - WIFI_IRAM_OPT
  1933. - 15
  1934. - 15
  1935. - 15
  1936. * - WIFI_RX_IRAM_OPT
  1937. - 16
  1938. - 16
  1939. - 16
  1940. * - LWIP_IRAM_OPTIMIZATION
  1941. - 13
  1942. - 13
  1943. - 0
  1944. * - INSTRUCTION_CACHE
  1945. - 32
  1946. - 32
  1947. - 16
  1948. * - INSTRUCTION_CACHE_LINE
  1949. - 32
  1950. - 32
  1951. - 32
  1952. * - INSTRUCTION_CACHE_WAYS
  1953. - 8
  1954. - 8
  1955. - 4
  1956. * - TCP TX throughput (Mbit/s)
  1957. - 83.93
  1958. - 64.28
  1959. - 23.17
  1960. * - TCP RX throughput (Mbit/s)
  1961. - 73.98
  1962. - 60.39
  1963. - 18.11
  1964. * - UDP TX throughput (Mbit/s)
  1965. - 98.69
  1966. - 96.28
  1967. - 48.78
  1968. * - UDP RX throughput (Mbit/s)
  1969. - 88.58
  1970. - 86.57
  1971. - 59.45
  1972. .. only:: esp32 or esp32s3
  1973. .. note::
  1974. The test was performed with a single stream in a shielded box using an ASUS RT-N66U router.
  1975. {IDF_TARGET_NAME}'s CPU is dual core with 240 MHz. {IDF_TARGET_NAME}'s flash is in QIO mode with 80 MHz.
  1976. .. only:: esp32s2
  1977. .. note::
  1978. The test was performed with a single stream in a shielded box using an ASUS RT-N66U router.
  1979. {IDF_TARGET_NAME}'s CPU is single core with 240 MHz. {IDF_TARGET_NAME}'s flash is in QIO mode with 80 MHz.
  1980. .. only:: esp32c3
  1981. .. note::
  1982. The test was performed with a single stream in a shielded box using an ASUS RT-N66U router.
  1983. {IDF_TARGET_NAME}'s CPU is single core with 160 MHz. {IDF_TARGET_NAME}'s flash is in QIO mode with 80 MHz.
  1984. .. only:: esp32c2
  1985. .. note::
  1986. The test was performed with a single stream in a shielded box using an Redmi RM2100 router.
  1987. {IDF_TARGET_NAME}'s CPU is single core with 120 MHz. {IDF_TARGET_NAME}'s flash is in QIO mode with 60 MHz.
  1988. .. only:: esp32
  1989. **Ranks:**
  1990. - **Iperf rank**
  1991. {IDF_TARGET_NAME} extreme performance rank used to test extreme performance.
  1992. - **High-performance rank**
  1993. The {IDF_TARGET_NAME}'s high-performance configuration rank, suitable for scenarios where the application occupies less memory and has high-performance requirements. In this rank, users can choose to use the RX prior rank or the TX prior rank according to the usage scenario.
  1994. - **Default rank**
  1995. {IDF_TARGET_NAME}'s default configuration rank, the available memory, and performance are in balance.
  1996. - **Memory saving rank**
  1997. This rank is suitable for scenarios where the application requires a large amount of memory, and the transceiver performance will be reduced in this rank.
  1998. - **Minimum rank**
  1999. This is the minimum configuration rank of {IDF_TARGET_NAME}. The protocol stack only uses the necessary memory for running. It is suitable for scenarios where there is no requirement for performance and the application requires lots of space.
  2000. .. only:: esp32s2
  2001. **Ranks:**
  2002. - **Iperf rank**
  2003. {IDF_TARGET_NAME} extreme performance rank used to test extreme performance.
  2004. - **High-performance rank**
  2005. The {IDF_TARGET_NAME}'s high-performance configuration rank, suitable for scenarios where the application occupies less memory and has high-performance requirements.
  2006. - **Default rank**
  2007. {IDF_TARGET_NAME}'s default configuration rank, the available memory, and performance are in balance.
  2008. - **Memory saving rank**
  2009. This rank is suitable for scenarios where the application requires a large amount of memory, and the transceiver performance will be reduced in this rank.
  2010. - **Minimum rank**
  2011. This is the minimum configuration rank of {IDF_TARGET_NAME}. The protocol stack only uses the necessary memory for running. It is suitable for scenarios where there is no requirement for performance and the application requires lots of space.
  2012. .. only:: esp32c3 or esp32s3
  2013. **Ranks:**
  2014. - **Iperf rank**
  2015. {IDF_TARGET_NAME} extreme performance rank used to test extreme performance.
  2016. - **Default rank**
  2017. {IDF_TARGET_NAME}'s default configuration rank, the available memory, and performance are in balance.
  2018. - **Minimum rank**
  2019. This is the minimum configuration rank of {IDF_TARGET_NAME}. The protocol stack only uses the necessary memory for running. It is suitable for scenarios where there is no requirement for performance and the application requires lots of space.
  2020. .. only:: esp32 or esp32s2 or esp32s3
  2021. Using PSRAM
  2022. ++++++++++++++++++++++++++++
  2023. PSRAM is generally used when the application takes up a lot of memory. In this mode, the :ref:`CONFIG_ESP32_WIFI_TX_BUFFER` is forced to be static. :ref:`CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM` indicates the number of DMA buffers at the hardware layer, and increasing this parameter can improve performance.
  2024. The following are the recommended ranks for using PSRAM:
  2025. .. only:: esp32
  2026. .. list-table::
  2027. :header-rows: 1
  2028. :widths: 15 10 10 15 10
  2029. * - Rank
  2030. - Iperf
  2031. - Default
  2032. - Memory saving
  2033. - Minimum
  2034. * - Available memory (KB)
  2035. - 113.8
  2036. - 152.4
  2037. - 181.2
  2038. - 202.6
  2039. * - WIFI_STATIC_RX_BUFFER_NUM
  2040. - 16
  2041. - 8
  2042. - 4
  2043. - 2
  2044. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  2045. - 128
  2046. - 128
  2047. - 128
  2048. - 128
  2049. * - WIFI_STATIC_TX_BUFFER_NUM
  2050. - 16
  2051. - 8
  2052. - 4
  2053. - 2
  2054. * - WIFI_RX_BA_WIN
  2055. - 16
  2056. - 16
  2057. - 8
  2058. - Disable
  2059. * - TCP_SND_BUF_DEFAULT (KB)
  2060. - 65
  2061. - 65
  2062. - 65
  2063. - 65
  2064. * - TCP_WND_DEFAULT (KB)
  2065. - 65
  2066. - 65
  2067. - 65
  2068. - 65
  2069. * - WIFI_IRAM_OPT
  2070. - 15
  2071. - 15
  2072. - 15
  2073. - 0
  2074. * - WIFI_RX_IRAM_OPT
  2075. - 16
  2076. - 16
  2077. - 0
  2078. - 0
  2079. * - LWIP_IRAM_OPTIMIZATION
  2080. - 13
  2081. - 0
  2082. - 0
  2083. - 0
  2084. * - TCP TX throughput (Mbit/s)
  2085. - 37.5
  2086. - 31.7
  2087. - 21.7
  2088. - 14.6
  2089. * - TCP RX throughput (Mbit/s)
  2090. - 31.5
  2091. - 29.8
  2092. - 26.5
  2093. - 21.1
  2094. * - UDP TX throughput (Mbit/s)
  2095. - 69.1
  2096. - 31.5
  2097. - 27.1
  2098. - 24.1
  2099. * - UDP RX throughput (Mbit/s)
  2100. - 40.1
  2101. - 38.5
  2102. - 37.5
  2103. - 36.9
  2104. .. only:: esp32s2
  2105. .. list-table::
  2106. :header-rows: 1
  2107. :widths: 10 10 10 10 15
  2108. * - Rank
  2109. - Iperf
  2110. - Default
  2111. - Memory saving
  2112. - Minimum
  2113. * - Available memory (KB)
  2114. - 70.6
  2115. - 96.4
  2116. - 118.8
  2117. - 148.2
  2118. * - WIFI_STATIC_RX_BUFFER_NUM
  2119. - 8
  2120. - 8
  2121. - 6
  2122. - 4
  2123. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  2124. - 64
  2125. - 64
  2126. - 64
  2127. - 64
  2128. * - WIFI_STATIC_TX_BUFFER_NUM
  2129. - 16
  2130. - 8
  2131. - 6
  2132. - 4
  2133. * - WIFI_RX_BA_WIN
  2134. - 16
  2135. - 6
  2136. - 6
  2137. - Disable
  2138. * - TCP_SND_BUF_DEFAULT (KB)
  2139. - 32
  2140. - 32
  2141. - 32
  2142. - 32
  2143. * - TCP_WND_DEFAULT (KB)
  2144. - 32
  2145. - 32
  2146. - 32
  2147. - 32
  2148. * - WIFI_IRAM_OPT
  2149. - 15
  2150. - 15
  2151. - 15
  2152. - 0
  2153. * - WIFI_RX_IRAM_OPT
  2154. - 16
  2155. - 16
  2156. - 0
  2157. - 0
  2158. * - LWIP_IRAM_OPTIMIZATION
  2159. - 13
  2160. - 0
  2161. - 0
  2162. - 0
  2163. * - INSTRUCTION_CACHE
  2164. - 16
  2165. - 16
  2166. - 16
  2167. - 8
  2168. * - INSTRUCTION_CACHE_LINE
  2169. - 16
  2170. - 16
  2171. - 16
  2172. - 16
  2173. * - DATA_CACHE
  2174. - 8
  2175. - 8
  2176. - 8
  2177. - 8
  2178. * - DATA_CACHE_LINE
  2179. - 32
  2180. - 32
  2181. - 32
  2182. - 32
  2183. * - TCP TX throughput (Mbit/s)
  2184. - 40.1
  2185. - 29.2
  2186. - 20.1
  2187. - 8.9
  2188. * - TCP RX throughput (Mbit/s)
  2189. - 21.9
  2190. - 16.8
  2191. - 14.8
  2192. - 9.6
  2193. * - UDP TX throughput (Mbit/s)
  2194. - 50.1
  2195. - 25.7
  2196. - 22.4
  2197. - 10.2
  2198. * - UDP RX throughput (Mbit/s)
  2199. - 45.3
  2200. - 43.1
  2201. - 28.5
  2202. - 15.1
  2203. .. note::
  2204. Reaching peak performance may cause task watchdog. It is a normal phenomenon considering the CPU may have no time for lower priority tasks.
  2205. .. only:: esp32s3
  2206. **PSRAM with 4 lines:**
  2207. .. list-table::
  2208. :header-rows: 1
  2209. :widths: 25 20 25 25 25
  2210. * - Rank
  2211. - Iperf
  2212. - Default
  2213. - Memory saving
  2214. - Minimum
  2215. * - Available memory (KB)
  2216. - 50.3
  2217. - 158.7
  2218. - 198.2
  2219. - 228.9
  2220. * - WIFI_STATIC_RX_BUFFER_NUM
  2221. - 24
  2222. - 8
  2223. - 6
  2224. - 4
  2225. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  2226. - 85
  2227. - 64
  2228. - 32
  2229. - 32
  2230. * - WIFI_STATIC_TX_BUFFER_NUM
  2231. - 32
  2232. - 32
  2233. - 6
  2234. - 4
  2235. * - WIFI_RX_BA_WIN
  2236. - 32
  2237. - 16
  2238. - 12
  2239. - Disable
  2240. * - TCP_SND_BUF_DEFAULT (KB)
  2241. - 85
  2242. - 32
  2243. - 32
  2244. - 32
  2245. * - TCP_WND_DEFAULT (KB)
  2246. - 85
  2247. - 32
  2248. - 32
  2249. - 32
  2250. * - WIFI_IRAM_OPT
  2251. - 15
  2252. - 15
  2253. - 15
  2254. - 0
  2255. * - WIFI_RX_IRAM_OPT
  2256. - 16
  2257. - 16
  2258. - 0
  2259. - 0
  2260. * - LWIP_IRAM_OPTIMIZATION
  2261. - 13
  2262. - 0
  2263. - 0
  2264. - 0
  2265. * - LWIP_UDP_RECVMBOX_SIZE
  2266. - 16
  2267. - 16
  2268. - 16
  2269. - 16
  2270. * - INSTRUCTION_CACHE
  2271. - 32
  2272. - 16
  2273. - 16
  2274. - 16
  2275. * - INSTRUCTION_CACHE_LINE
  2276. - 32
  2277. - 16
  2278. - 16
  2279. - 16
  2280. * - INSTRUCTION_CACHE_WAYS
  2281. - 8
  2282. - 8
  2283. - 8
  2284. - 8
  2285. * - DATA_CACHE
  2286. - 64
  2287. - 16
  2288. - 16
  2289. - 16
  2290. * - DATA_CACHE_LINE
  2291. - 32
  2292. - 32
  2293. - 32
  2294. - 32
  2295. * - DATA_CACHE_WAYS
  2296. - 8
  2297. - 8
  2298. - 8
  2299. - 8
  2300. * - TCP TX throughput (Mbit/s)
  2301. - 93.1
  2302. - 62.5
  2303. - 41.3
  2304. - 42.7
  2305. * - TCP RX throughput (Mbit/s)
  2306. - 88.9
  2307. - 46.5
  2308. - 46.2
  2309. - 37.9
  2310. * - UDP TX throughput (Mbit/s)
  2311. - 106.4
  2312. - 106.2
  2313. - 60.7
  2314. - 50
  2315. * - UDP RX throughput (Mbit/s)
  2316. - 99.8
  2317. - 92.6
  2318. - 94.3
  2319. - 53.3
  2320. **PSRAM with 8 lines:**
  2321. .. list-table::
  2322. :header-rows: 1
  2323. :widths: 25 20 25 25 25
  2324. * - Rank
  2325. - Iperf
  2326. - Default
  2327. - Memory saving
  2328. - Minimum
  2329. * - Available memory (KB)
  2330. - 49.1
  2331. - 151.3
  2332. - 215.3
  2333. - 243.6
  2334. * - WIFI_STATIC_RX_BUFFER_NUM
  2335. - 24
  2336. - 8
  2337. - 6
  2338. - 4
  2339. * - WIFI_DYNAMIC_RX_BUFFER_NUM
  2340. - 85
  2341. - 64
  2342. - 32
  2343. - 32
  2344. * - WIFI_STATIC_TX_BUFFER_NUM
  2345. - 32
  2346. - 32
  2347. - 6
  2348. - 4
  2349. * - WIFI_RX_BA_WIN
  2350. - 32
  2351. - 16
  2352. - 12
  2353. - Disable
  2354. * - TCP_SND_BUF_DEFAULT (KB)
  2355. - 85
  2356. - 32
  2357. - 32
  2358. - 32
  2359. * - TCP_WND_DEFAULT (KB)
  2360. - 85
  2361. - 32
  2362. - 32
  2363. - 32
  2364. * - WIFI_IRAM_OPT
  2365. - 15
  2366. - 15
  2367. - 15
  2368. - 0
  2369. * - WIFI_RX_IRAM_OPT
  2370. - 16
  2371. - 16
  2372. - 0
  2373. - 0
  2374. * - LWIP_IRAM_OPTIMIZATION
  2375. - 13
  2376. - 0
  2377. - 0
  2378. - 0
  2379. * - LWIP_UDP_RECVMBOX_SIZE
  2380. - 16
  2381. - 16
  2382. - 16
  2383. - 16
  2384. * - INSTRUCTION_CACHE
  2385. - 32
  2386. - 16
  2387. - 16
  2388. - 16
  2389. * - INSTRUCTION_CACHE_LINE
  2390. - 32
  2391. - 16
  2392. - 16
  2393. - 16
  2394. * - INSTRUCTION_CACHE_WAYS
  2395. - 8
  2396. - 8
  2397. - 8
  2398. - 8
  2399. * - DATA_CACHE
  2400. - 64
  2401. - 16
  2402. - 16
  2403. - 16
  2404. * - DATA_CACHE_LINE
  2405. - 32
  2406. - 32
  2407. - 32
  2408. - 32
  2409. * - DATA_CACHE_WAYS
  2410. - 8
  2411. - 8
  2412. - 8
  2413. - 8
  2414. * - TCP TX throughput (Mbit/s)
  2415. - 93.3
  2416. - 58.4
  2417. - 37.1
  2418. - 35.6
  2419. * - TCP RX throughput (Mbit/s)
  2420. - 86.1
  2421. - 43.6
  2422. - 42.5
  2423. - 35
  2424. * - UDP TX throughput (Mbit/s)
  2425. - 104.7
  2426. - 82.2
  2427. - 60.4
  2428. - 47.9
  2429. * - UDP RX throughput (Mbit/s)
  2430. - 104.6
  2431. - 104.8
  2432. - 104
  2433. - 55.7
  2434. Wi-Fi Menuconfig
  2435. -----------------------
  2436. Wi-Fi Buffer Configure
  2437. +++++++++++++++++++++++
  2438. If you are going to modify the default number or type of buffer, it would be helpful to also have an overview of how the buffer is allocated/freed in the data path. The following diagram shows this process in the TX direction:
  2439. .. blockdiag::
  2440. :caption: TX Buffer Allocation
  2441. :align: center
  2442. blockdiag buffer_allocation_tx {
  2443. # global attributes
  2444. node_height = 60;
  2445. node_width = 100;
  2446. span_width = 50;
  2447. span_height = 20;
  2448. default_shape = roundedbox;
  2449. # labels of diagram nodes
  2450. APPL_TASK [label="Application\n task", fontsize=12];
  2451. LwIP_TASK [label="LwIP\n task", fontsize=12];
  2452. WIFI_TASK [label="Wi-Fi\n task", fontsize=12];
  2453. # labels of description nodes
  2454. APPL_DESC [label="1> User data", width=120, height=25, shape=note, color=yellow];
  2455. LwIP_DESC [label="2> Pbuf", width=120, height=25, shape=note, color=yellow];
  2456. WIFI_DESC [label="3> Dynamic (Static)\n TX Buffer", width=150, height=40, shape=note, color=yellow];
  2457. # node connections
  2458. APPL_TASK -> LwIP_TASK -> WIFI_TASK
  2459. APPL_DESC -> LwIP_DESC -> WIFI_DESC [style=none]
  2460. }
  2461. Description:
  2462. - The application allocates the data which needs to be sent out.
  2463. - The application calls TCPIP-/Socket-related APIs to send the user data. These APIs will allocate a PBUF used in LwIP, and make a copy of the user data.
  2464. - When LwIP calls a Wi-Fi API to send the PBUF, the Wi-Fi API will allocate a "Dynamic Tx Buffer" or "Static Tx Buffer", make a copy of the LwIP PBUF, and finally send the data.
  2465. The following diagram shows how buffer is allocated/freed in the RX direction:
  2466. .. blockdiag::
  2467. :caption: RX Buffer Allocation
  2468. :align: center
  2469. blockdiag buffer_allocation_rx {
  2470. # global attributes
  2471. node_height = 60;
  2472. node_width = 100;
  2473. span_width = 40;
  2474. span_height = 20;
  2475. default_shape = roundedbox;
  2476. # labels of diagram nodes
  2477. APPL_TASK [label="Application\n task", fontsize=12];
  2478. LwIP_TASK [label="LwIP\n task", fontsize=12];
  2479. WIFI_TASK [label="Wi-Fi\n task", fontsize=12];
  2480. WIFI_INTR [label="Wi-Fi\n interrupt", fontsize=12];
  2481. # labels of description nodes
  2482. APPL_DESC [label="4> User\n Data Buffer", height=40, shape=note, color=yellow];
  2483. LwIP_DESC [label="3> Pbuf", height=40, shape=note, color=yellow];
  2484. WIFI_DESC [label="2> Dynamic\n RX Buffer", height=40, shape=note, color=yellow];
  2485. INTR_DESC [label="1> Static\n RX Buffer", height=40, shape=note, color=yellow];
  2486. # node connections
  2487. APPL_TASK <- LwIP_TASK <- WIFI_TASK <- WIFI_INTR
  2488. APPL_DESC <- LwIP_DESC <- WIFI_DESC <- INTR_DESC [style=none]
  2489. }
  2490. Description:
  2491. - The Wi-Fi hardware receives a packet over the air and puts the packet content to the "Static Rx Buffer", which is also called "RX DMA Buffer".
  2492. - The Wi-Fi driver allocates a "Dynamic Rx Buffer", makes a copy of the "Static Rx Buffer", and returns the "Static Rx Buffer" to hardware.
  2493. - The Wi-Fi driver delivers the packet to the upper-layer (LwIP), and allocates a PBUF for holding the "Dynamic Rx Buffer".
  2494. - The application receives data from LwIP.
  2495. The diagram shows the configuration of the Wi-Fi internal buffer.
  2496. .. list-table::
  2497. :header-rows: 1
  2498. :widths: 10 10 10 10 25
  2499. * - Buffer Type
  2500. - Alloc Type
  2501. - Default
  2502. - Configurable
  2503. - Description
  2504. * - Static RX Buffer (Hardware RX Buffer)
  2505. - Static
  2506. - 10 * 1600 Bytes
  2507. - Yes
  2508. - This is a kind of DMA memory. It is initialized in :cpp:func:`esp_wifi_init()` and freed in :cpp:func:`esp_wifi_deinit()`. The ‘Static Rx Buffer’ forms the hardware receiving list. Upon receiving a frame over the air, hardware writes the frame into the buffer and raises an interrupt to the CPU. Then, the Wi-Fi driver reads the content from the buffer and returns the buffer back to the list.
  2509. If needs be, the application can reduce the memory statically allocated by Wi-Fi. It can reduce this value from 10 to 6 to save 6400 Bytes of memory. It is not recommended to reduce the configuration to a value less than 6 unless the AMPDU feature is disabled.
  2510. * - Dynamic RX Buffer
  2511. - Dynamic
  2512. - 32
  2513. - Yes
  2514. - The buffer length is variable and it depends on the received frames’ length. When the Wi-Fi driver receives a frame from the ‘Hardware Rx Buffer’, the ‘Dynamic Rx Buffer’ needs to be allocated from the heap. The number of the Dynamic Rx Buffer, configured in the menuconfig, is used to limit the total un-freed Dynamic Rx Buffer number.
  2515. * - Dynamic TX Buffer
  2516. - Dynamic
  2517. - 32
  2518. - Yes
  2519. - This is a kind of DMA memory. It is allocated to the heap. When the upper-layer (LwIP) sends packets to the Wi-Fi driver, it firstly allocates a ‘Dynamic TX Buffer’ and makes a copy of the upper-layer buffer.
  2520. The Dynamic and Static TX Buffers are mutually exclusive.
  2521. * - Static TX Buffer
  2522. - Static
  2523. - 16 * 1600Bytes
  2524. - Yes
  2525. - This is a kind of DMA memory. It is initialized in :cpp:func:`esp_wifi_init()` and freed in :cpp:func:`esp_wifi_deinit()`. When the upper-layer (LwIP) sends packets to the Wi-Fi driver, it firstly allocates a ‘Static TX Buffer’ and makes a copy of the upper-layer buffer.
  2526. The Dynamic and Static TX Buffer are mutually exclusive.
  2527. The TX buffer must be a DMA buffer. For this reason, if PSRAM is enabled, the TX buffer must be static.
  2528. * - Management Short Buffer
  2529. - Dynamic
  2530. - 8
  2531. - NO
  2532. - Wi-Fi driver’s internal buffer.
  2533. * - Management Long Buffer
  2534. - Dynamic
  2535. - 32
  2536. - NO
  2537. - Wi-Fi driver’s internal buffer.
  2538. * - Management Long Long Buffer
  2539. - Dynamic
  2540. - 32
  2541. - NO
  2542. - Wi-Fi driver’s internal buffer.
  2543. Wi-Fi NVS Flash
  2544. +++++++++++++++++++++
  2545. If the Wi-Fi NVS flash is enabled, all Wi-Fi configurations set via the Wi-Fi APIs will be stored into flash, and the Wi-Fi driver will start up with these configurations the next time it powers on/reboots. However, the application can choose to disable the Wi-Fi NVS flash if it does not need to store the configurations into persistent memory, or has its own persistent storage, or simply due to debugging reasons, etc.
  2546. Wi-Fi Aggregate MAC Protocol Data Unit (AMPDU)
  2547. ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2548. {IDF_TARGET_NAME} supports both receiving and transmitting AMPDU, and the AMPDU can greatly improve the Wi-Fi throughput.
  2549. Generally, the AMPDU should be enabled. Disabling AMPDU is usually for debugging purposes.
  2550. Troubleshooting
  2551. ---------------
  2552. Please refer to a separate document with :doc:`wireshark-user-guide`.
  2553. .. toctree::
  2554. :hidden:
  2555. wireshark-user-guide