ulp_instruction_set.rst 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. {IDF_TARGET_NAME} ULP Coprocessor Instruction Set
  2. =================================================
  3. This document provides details about the instructions used by {IDF_TARGET_NAME} ULP FSM coprocessor assembler.
  4. ULP FSM coprocessor has 4 16-bit general purpose registers, labeled R0, R1, R2, R3. It also has an 8-bit counter register (``stage_cnt``) which can be used to implement loops. Stage count register is accessed using special instructions.
  5. ULP coprocessor can access 8 K bytes of RTC_SLOW_MEM memory region. Memory is addressed in 32-bit word units. It can also access peripheral registers in ``RTC_CNTL``, ``RTC_IO``, and ``SENS`` peripherals.
  6. All instructions are 32-bit. Jump instructions, ALU instructions, peripheral register and memory access instructions are executed in 1 cycle. Instructions which work with peripherals (TSENS, ADC, I2C) take variable number of cycles, depending on peripheral operation.
  7. The instruction syntax is case insensitive. Upper and lower case letters can be used and intermixed arbitrarily. This is true both for register names and instruction names.
  8. .. _ulp-fsm-addressing:
  9. Note About Addressing
  10. ---------------------
  11. {IDF_TARGET_NAME} ULP FSM coprocessor's ``JUMP``, ``ST``, ``LD`` family of instructions expect the address argument to be expressed in the following way depending on the type of address argument used:
  12. - When the address argument is presented as a label then the instruction expects the address to be expressed as 32-bit words.
  13. Consider the following example program::
  14. entry:
  15. NOP
  16. NOP
  17. NOP
  18. NOP
  19. loop:
  20. MOVE R1, loop
  21. JUMP R1
  22. When this program is assembled and linked, address of label ``loop`` will be equal to 16 (expressed in bytes). However ``JUMP`` instruction expects the address stored in register ``R1`` to be expressed in 32-bit words. To account for this common use case, the assembler will convert the address of label ``loop`` from bytes to words when generating the ``MOVE`` instruction. Hence, the code generated code will be equivalent to::
  23. 0000 NOP
  24. 0004 NOP
  25. 0008 NOP
  26. 000c NOP
  27. 0010 MOVE R1, 4
  28. 0014 JUMP R1
  29. - The other case is when the argument of ``MOVE`` instruction is not a label but a constant. In this case, assembler will **use the value as is**, without any conversion::
  30. .set val, 0x10
  31. MOVE R1, val
  32. In this case, value loaded into ``R1`` will be ``0x10``.
  33. However, when an immediate value is used as an offset in ``LD`` and ``ST`` instructions, the assembler considers the address argument in bytes and converts it to 32-bit words before executing the instruction::
  34. ST R1, R2, 4 // offset = 4 bytes; Mem[R2 + 4 / 4] = R1
  35. In this case, the value in R1 is stored at the memory location pointed by [R2 + offset / 4]
  36. Consider the following code::
  37. .global array
  38. array: .long 0
  39. .long 0
  40. .long 0
  41. .long 0
  42. MOVE R1, array
  43. MOVE R2, 0x1234
  44. ST R2, R1, 0 // write value of R2 into the first array element,
  45. // i.e., array[0]
  46. ST R2, R1, 4 // write value of R2 into the second array element
  47. // (4 byte offset), i.e., array[1]
  48. ADD R1, R1, 2 // this increments address by 2 words (8 bytes)
  49. ST R2, R1, 0 // write value of R2 into the third array element,
  50. // i.e., array[2]
  51. Note About Instruction Execution Time
  52. -------------------------------------
  53. ULP coprocessor is clocked from ``RTC_FAST_CLK``, which is normally derived from the internal 8MHz oscillator. Applications which need to know exact ULP clock frequency can calibrate it against the main XTAL clock::
  54. #include "soc/rtc.h"
  55. // calibrate 8M/256 clock against XTAL, get 8M/256 clock period
  56. uint32_t rtc_8md256_period = rtc_clk_cal(RTC_CAL_8MD256, 100);
  57. uint32_t rtc_fast_freq_hz = 1000000ULL * (1 << RTC_CLK_CAL_FRACT) * 256 / rtc_8md256_period;
  58. ULP coprocessor needs certain number of clock cycles to fetch each instruction, plus certain number of cycles to execute it, depending on the instruction. See description of each instruction below for details on the execution time.
  59. Instruction fetch time is:
  60. - 2 clock cycles — for instructions following ALU and branch instructions.
  61. - 4 clock cycles — in other cases.
  62. Note that when accessing RTC memories and RTC registers, ULP coprocessor has lower priority than the main CPUs. This means that ULP coprocessor execution may be suspended while the main CPUs access same memory region as the ULP.
  63. .. only:: esp32s2 or esp32s3
  64. Difference Between ESP32 ULP and {IDF_TARGET_NAME} ULP Instruction Sets
  65. -----------------------------------------------------------------------
  66. Compared to the ESP32 ULP FSM coprocessor, the {IDF_TARGET_NAME} ULP FSM coprocessor has an extended instruction set. The {IDF_TARGET_NAME} ULP FSM is not binary compatible with ESP32 ULP FSM,
  67. but a ESP32 ULP FSM assembled program is expected to work on the {IDF_TARGET_NAME} ULP FSM after rebuilding.
  68. The list of the new instructions that was added to the {IDF_TARGET_NAME} ULP FSM is: ``LDL``, ``LDH``, ``STL``, ``STH``, ``ST32``, ``STO``, ``STI``, ``STI32``.
  69. The detailed description of all instructions is presented below:
  70. **NOP** - No Operation
  71. ----------------------
  72. **Syntax**
  73. **NOP**
  74. **Operands**
  75. None
  76. **Cycles**
  77. 2 cycle to execute, 4 cycles to fetch next instruction
  78. **Description**
  79. No operation is performed. Only the PC is incremented.
  80. **Example**::
  81. 1: NOP
  82. **ADD** - Add to Register
  83. -------------------------
  84. **Syntax**
  85. **ADD** **Rdst, Rsrc1, Rsrc2**
  86. **ADD** **Rdst, Rsrc1, imm**
  87. **Operands**
  88. - **Rdst** - Register R[0..3]
  89. - **Rsrc1** - Register R[0..3]
  90. - **Rsrc2** - Register R[0..3]
  91. - **Imm** - 16-bit signed value
  92. **Cycles**
  93. 2 cycles to execute, 4 cycles to fetch next instruction
  94. **Description**
  95. The instruction adds source register to another source register or to a 16-bit signed value and stores the result in the destination register.
  96. **Examples**::
  97. 1: ADD R1, R2, R3 // R1 = R2 + R3
  98. 2: Add R1, R2, 0x1234 // R1 = R2 + 0x1234
  99. 3: .set value1, 0x03 // constant value1=0x03
  100. Add R1, R2, value1 // R1 = R2 + value1
  101. 4: .global label // declaration of variable label
  102. add R1, R2, label // R1 = R2 + label
  103. ...
  104. label: nop // definition of variable label
  105. **SUB** - Subtract from Register
  106. --------------------------------
  107. **Syntax**
  108. **SUB** **Rdst, Rsrc1, Rsrc2**
  109. **SUB** **Rdst, Rsrc1, imm**
  110. **Operands**
  111. - **Rdst** - Register R[0..3]
  112. - **Rsrc1** - Register R[0..3]
  113. - **Rsrc2** - Register R[0..3]
  114. - **Imm** - 16-bit signed value
  115. **Cycles**
  116. 2 cycles to execute, 4 cycles to fetch next instruction
  117. **Description**
  118. The instruction subtracts the source register from another source register or subtracts a 16-bit signed value from a source register, and stores the result to the destination register.
  119. **Examples**::
  120. 1: SUB R1, R2, R3 // R1 = R2 - R3
  121. 2: sub R1, R2, 0x1234 // R1 = R2 - 0x1234
  122. 3: .set value1, 0x03 // constant value1=0x03
  123. SUB R1, R2, value1 // R1 = R2 - value1
  124. 4: .global label // declaration of variable label
  125. SUB R1, R2, label // R1 = R2 - label
  126. ....
  127. label: nop // definition of variable label
  128. **AND** - Bitwise Logical AND of Two Operands
  129. ---------------------------------------------
  130. **Syntax**
  131. **AND** **Rdst, Rsrc1, Rsrc2**
  132. **AND** **Rdst, Rsrc1, imm**
  133. **Operands**
  134. - **Rdst** - Register R[0..3]
  135. - **Rsrc1** - Register R[0..3]
  136. - **Rsrc2** - Register R[0..3]
  137. - **Imm** - 16-bit signed value
  138. **Cycles**
  139. 2 cycles to execute, 4 cycles to fetch next instruction
  140. **Description**
  141. The instruction does a bitwise logical AND of a source register and another source register or a 16-bit signed value and stores the result to the destination register.
  142. **Examples**::
  143. 1: AND R1, R2, R3 // R1 = R2 & R3
  144. 2: AND R1, R2, 0x1234 // R1 = R2 & 0x1234
  145. 3: .set value1, 0x03 // constant value1=0x03
  146. AND R1, R2, value1 // R1 = R2 & value1
  147. 4: .global label // declaration of variable label
  148. AND R1, R2, label // R1 = R2 & label
  149. ...
  150. label: nop // definition of variable label
  151. **OR** - Bitwise Logical OR of Two Operands
  152. -------------------------------------------
  153. **Syntax**
  154. **OR** **Rdst, Rsrc1, Rsrc2**
  155. **OR** **Rdst, Rsrc1, imm**
  156. **Operands**
  157. - **Rdst** - Register R[0..3]
  158. - **Rsrc1** - Register R[0..3]
  159. - **Rsrc2** - Register R[0..3]
  160. - **Imm** - 16-bit signed value
  161. **Cycles**
  162. 2 cycles to execute, 4 cycles to fetch next instruction
  163. **Description**
  164. The instruction does a bitwise logical OR of a source register and another source register or a 16-bit signed value and stores the result to the destination register.
  165. **Examples**::
  166. 1: OR R1, R2, R3 // R1 = R2 || R3
  167. 2: OR R1, R2, 0x1234 // R1 = R2 || 0x1234
  168. 3: .set value1, 0x03 // constant value1=0x03
  169. OR R1, R2, value1 // R1 = R2 || value1
  170. 4: .global label // declaration of variable label
  171. OR R1, R2, label // R1 = R2 || label
  172. ...
  173. label: nop // definition of variable label
  174. **LSH** - Logical Shift Left
  175. ----------------------------
  176. **Syntax**
  177. **LSH** **Rdst, Rsrc1, Rsrc2**
  178. **LSH** **Rdst, Rsrc1, imm**
  179. **Operands**
  180. - **Rdst** - Register R[0..3]
  181. - **Rsrc1** - Register R[0..3]
  182. - **Rsrc2** - Register R[0..3]
  183. - **Imm** - 16-bit signed value
  184. **Cycles**
  185. 2 cycles to execute, 4 cycles to fetch next instruction
  186. **Description**
  187. The instruction does a logical shift to left of the source register by the number of bits from another source register or a 16-bit signed value and stores the result to the destination register.
  188. .. note::
  189. Shift operations which are greater than 15 bits have undefined results.
  190. **Examples**::
  191. 1: LSH R1, R2, R3 // R1 = R2 << R3
  192. 2: LSH R1, R2, 0x03 // R1 = R2 << 0x03
  193. 3: .set value1, 0x03 // constant value1=0x03
  194. LSH R1, R2, value1 // R1 = R2 << value1
  195. 4: .global label // declaration of variable label
  196. LSH R1, R2, label // R1 = R2 << label
  197. ...
  198. label: nop // definition of variable label
  199. **RSH** - Logical Shift Right
  200. -----------------------------
  201. **Syntax**
  202. **RSH** **Rdst, Rsrc1, Rsrc2**
  203. **RSH** **Rdst, Rsrc1, imm**
  204. **Operands**
  205. **Rdst** - Register R[0..3]
  206. **Rsrc1** - Register R[0..3]
  207. **Rsrc2** - Register R[0..3]
  208. **Imm** - 16-bit signed value
  209. **Cycles**
  210. 2 cycles to execute, 4 cycles to fetch next instruction
  211. **Description**
  212. The instruction does a logical shift to right of a source register by the number of bits from another source register or a 16-bit signed value and stores the result to the destination register.
  213. .. note::
  214. Shift operations which are greater than 15 bits have undefined results.
  215. **Examples**::
  216. 1: RSH R1, R2, R3 // R1 = R2 >> R3
  217. 2: RSH R1, R2, 0x03 // R1 = R2 >> 0x03
  218. 3: .set value1, 0x03 // constant value1=0x03
  219. RSH R1, R2, value1 // R1 = R2 >> value1
  220. 4: .global label // declaration of variable label
  221. RSH R1, R2, label // R1 = R2 >> label
  222. label: nop // definition of variable label
  223. **MOVE** – Move to Register
  224. ---------------------------
  225. **Syntax**
  226. **MOVE** **Rdst, Rsrc**
  227. **MOVE** **Rdst, imm**
  228. **Operands**
  229. - **Rdst** – Register R[0..3]
  230. - **Rsrc** – Register R[0..3]
  231. - **Imm** – 16-bit signed value
  232. **Cycles**
  233. 2 cycles to execute, 4 cycles to fetch next instruction
  234. **Description**
  235. The instruction moves the value from the source register or a 16-bit signed value to the destination register.
  236. .. note::
  237. Note that when a label is used as an immediate, the address of the label will be converted from bytes to words. This is because LD, ST, and JUMP instructions expect the address register value to be expressed in words rather than bytes. See the section :ref:`ulp-fsm-addressing` for more details.
  238. **Examples**::
  239. 1: MOVE R1, R2 // R1 = R2
  240. 2: MOVE R1, 0x03 // R1 = 0x03
  241. 3: .set value1, 0x03 // constant value1=0x03
  242. MOVE R1, value1 // R1 = value1
  243. 4: .global label // declaration of label
  244. MOVE R1, label // R1 = address_of(label) / 4
  245. ...
  246. label: nop // definition of label
  247. **ST** – Store Data to the Memory
  248. ---------------------------------
  249. **Syntax**
  250. **ST** **Rsrc, Rdst, offset**
  251. **Operands**
  252. - **Rsrc** – Register R[0..3], holds the 16-bit value to store
  253. - **Rdst** – Register R[0..3], address of the destination, in 32-bit words
  254. - **Offset** – 13-bit signed value, offset in bytes
  255. **Cycles**
  256. 4 cycles to execute, 4 cycles to fetch next instruction
  257. **Description**
  258. The instruction stores the 16-bit value of Rsrc to the lower half-word of memory with address Rdst+offset. The upper half-word is written with the current program counter (PC) (expressed in words, shifted left by 5 bits) OR'd with Rdst (0..3)::
  259. Mem[Rdst + offset / 4]{31:0} = {PC[10:0], 3'b0, Rdst, Rsrc[15:0]}
  260. The application can use the higher 16 bits to determine which instruction in the ULP program has written any particular word into memory.
  261. .. note::
  262. Note that the offset specified in bytes is converted to a 32-bit word offset before execution. See the section :ref:`ulp-fsm-addressing` for more details.
  263. **Examples**::
  264. 1: ST R1, R2, 0x12 // MEM[R2 + 0x12 / 4] = R1
  265. 2: .data // Data section definition
  266. Addr1: .word 123 // Define label Addr1 16 bit
  267. .set offs, 0x00 // Define constant offs
  268. .text // Text section definition
  269. MOVE R1, 1 // R1 = 1
  270. MOVE R2, Addr1 // R2 = Addr1
  271. ST R1, R2, offs // MEM[R2 + 0 / 4] = R1
  272. // MEM[Addr1 + 0] will be 32'h600001
  273. .. only:: esp32s2 or esp32s3
  274. **STL** – Store Data to the Lower 16 Bits of 32-bit Memory
  275. ----------------------------------------------------------------
  276. **Syntax**
  277. **STL** **Rsrc, Rdst, offset, Label**
  278. **Operands**
  279. - **Rsrc** – Register R[0..3], holds the 16-bit value to store
  280. - **Rdst** – Register R[0..3], address of the destination, in 32-bit words
  281. - **Offset** – 11-bit signed value, offset in bytes
  282. - **Label** – 2-bit user defined unsigned value
  283. **Cycles**
  284. 4 cycles to execute, 4 cycles to fetch next instruction
  285. **Description**
  286. The instruction stores the 16-bit value of Rsrc to the lower half-word of the memory with address [Rdst + offset / 4]::
  287. Mem[Rdst + offset / 4]{15:0} = {Rsrc[15:0]}
  288. Mem[Rdst + offset / 4]{15:0} = {Label[1:0],Rsrc[13:0]}
  289. The ``ST`` and the ``STL`` commands can be used interchangeably and have been provided to maintain backward compatibility with previous versions of the ULP core.
  290. .. note::
  291. Note that the offset specified in bytes is converted to a 32-bit word offset before execution. See the section :ref:`ulp-fsm-addressing` for more details.
  292. **Examples**::
  293. 1: STL R1, R2, 0x12 // MEM[R2 + 0x12 / 4] = R1
  294. 2: .data // Data section definition
  295. Addr1: .word 123 // Define label Addr1 16 bit
  296. .set offs, 0x00 // Define constant offs
  297. .text // Text section definition
  298. MOVE R1, 1 // R1 = 1
  299. MOVE R2, Addr1 // R2 = Addr1
  300. STL R1, R2, offs // MEM[R2 + 0 / 4] = R1
  301. // MEM[Addr1 + 0] will be 32'hxxxx0001
  302. 3:
  303. MOVE R1, 1 // R1 = 1
  304. STL R1, R2, 0x12, 1 // MEM[R2 + 0x12 / 4] = 0xxxxx4001
  305. **STH** – Store data to the higher 16 bits of 32-bit memory
  306. ------------------------------------------------------------
  307. **Syntax**
  308. **STH** **Rsrc, Rdst, offset, Label**
  309. **Operands**
  310. - **Rsrc** – Register R[0..3], holds the 16-bit value to store
  311. - **Rdst** – Register R[0..3], address of the destination, in 32-bit words
  312. - **Offset** – 11-bit signed value, offset in bytes
  313. - **Label** – 2-bit user defined unsigned value
  314. **Cycles**
  315. 4 cycles to execute, 4 cycles to fetch next instruction
  316. **Description**
  317. The instruction stores the 16-bit value of Rsrc to the upper half-word of memory with address [Rdst + offset / 4]::
  318. Mem[Rdst + offset / 4]{31:16} = {Rsrc[15:0]}
  319. Mem[Rdst + offset / 4]{31:16} = {Label[1:0],Rsrc[13:0]}
  320. .. note::
  321. Note that the offset specified in bytes is converted to a 32-bit word offset before execution. See the section :ref:`ulp-fsm-addressing` for more details.
  322. **Examples**::
  323. 1: STH R1, R2, 0x12 // MEM[R2 + 0x12 / 4][31:16] = R1
  324. 2: .data // Data section definition
  325. Addr1: .word 123 // Define label Addr1 16 bit
  326. .set offs, 0x00 // Define constant offs
  327. .text // Text section definition
  328. MOVE R1, 1 // R1 = 1
  329. MOVE R2, Addr1 // R2 = Addr1
  330. STH R1, R2, offs // MEM[R2 + 0 / 4] = R1
  331. // MEM[Addr1 + 0] will be 32'h0001xxxx
  332. 3:
  333. MOVE R1, 1 // R1 = 1
  334. STH R1, R2, 0x12, 1 // MEM[R2 + 0x12 / 4] 0x4001xxxx
  335. **ST32** – Store 32-bits data to the 32-bits memory
  336. ---------------------------------------------------
  337. **Syntax**
  338. **ST32** **Rsrc, Rdst, offset, Label**
  339. **Operands**
  340. - **Rsrc** – Register R[0..3], holds the 16-bit value to store
  341. - **Rdst** – Register R[0..3], address of the destination, in 32-bit words
  342. - **Offset** – 11-bit signed value, offset in bytes
  343. - **Label** – 2-bit user defined unsigned value
  344. **Cycles**
  345. 4 cycles to execute, 4 cycles to fetch next instruction
  346. **Description**
  347. The instruction stores 11 bits of the PC value, label value and the 16-bit value of Rsrc to the 32-bit memory with address [Rdst + offset / 4]::
  348. Mem[Rdst + offset / 4]{31:0} = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  349. .. note::
  350. Note that the offset specified in bytes is converted to a 32-bit word offset before execution. See the section :ref:`ulp-fsm-addressing` for more details.
  351. **Examples**::
  352. 1: ST32 R1, R2, 0x12, 0 // MEM[R2 + 0x12 / 4][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  353. 2: .data // Data section definition
  354. Addr1: .word 123 // Define label Addr1 16 bit
  355. .set offs, 0x00 // Define constant offs
  356. .text // Text section definition
  357. MOVE R1, 1 // R1 = 1
  358. MOVE R2, Addr1 // R2 = Addr1
  359. ST32 R1, R2, offs, 1 // MEM[R2 + 0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  360. // MEM[Addr1 + 0] will be 32'h00010001
  361. **STO** – Set offset value for auto increment operation
  362. -------------------------------------------------------
  363. **Syntax**
  364. **STO** **offset**
  365. **Operands**
  366. - **Offset** – 11-bit signed value, offset in bytes
  367. **Cycles**
  368. 4 cycles to execute, 4 cycles to fetch next instruction
  369. **Description**
  370. The instruction sets the 16-bit value to the offset register::
  371. offset = value / 4
  372. .. note::
  373. Note that the offset specified in bytes is converted to a 32-bit word offset before execution. See the section :ref:`ulp-fsm-addressing` for more details.
  374. **Examples**::
  375. 1: STO 0x12 // Offset = 0x12 / 4
  376. 2: .data // Data section definition
  377. Addr1: .word 123 // Define label Addr1 16 bit
  378. .set offs, 0x00 // Define constant offs
  379. .text // Text section definition
  380. STO offs // Offset = 0x00
  381. **STI** – Store data to the 32-bits memory with auto increment of predefined offset address
  382. -------------------------------------------------------------------------------------------
  383. **Syntax**
  384. **STI** **Rsrc, Rdst, Label**
  385. **Operands**
  386. - **Rsrc** – Register R[0..3], holds the 16-bit value to store
  387. - **Rdst** – Register R[0..3], address of the destination, in 32-bit words
  388. - **Label** – 2-bit user defined unsigned value
  389. **Cycles**
  390. 4 cycles to execute, 4 cycles to fetch next instruction
  391. **Description**
  392. The instruction stores the 16-bit value of Rsrc to the lower and upper half-word of memory with address [Rdst + offset / 4].
  393. The offset value is auto incremented when the STI instruction is called twice. Make sure to execute the ``STO`` instruction
  394. to set the offset value before executing the STI instruction::
  395. Mem[Rdst + offset / 4]{15:0/31:16} = {Rsrc[15:0]}
  396. Mem[Rdst + offset / 4]{15:0/31:16} = {Label[1:0],Rsrc[13:0]}
  397. **Examples**::
  398. 1: STO 4 // Set offset to 4
  399. STI R1, R2 // MEM[R2 + 4 / 4][15:0] = R1
  400. STI R1, R2 // MEM[R2 + 4 / 4][31:16] = R1
  401. // offset += (1 * 4) //offset is incremented by 1 word
  402. STI R1, R2 // MEM[R2 + 8 / 4][15:0] = R1
  403. STI R1, R2 // MEM[R2 + 8 / 4][31:16] = R1
  404. **STI32** – Store 32-bits data to the 32-bits memory with auto increment of adress offset
  405. -----------------------------------------------------------------------------------------
  406. **Syntax**
  407. **STI32** **Rsrc, Rdst, Label**
  408. **Operands**
  409. - **Rsrc** – Register R[0..3], holds the 16-bit value to store
  410. - **Rdst** – Register R[0..3], address of the destination, in 32-bit words
  411. - **Label** – 2-bit user defined unsigned value
  412. **Cycles**
  413. 4 cycles to execute, 4 cycles to fetch next instruction
  414. **Description**
  415. The instruction stores 11 bits of the PC value, label value and the 16-bit value of Rsrc to the 32-bit memory with address [Rdst + offset / 4].
  416. The offset value is auto incremented each time the STI32 instruction is called. Make sure to execute the ``STO`` instruction
  417. to set the offset value before executing the STI32 instruction::
  418. Mem[Rdst + offset / 4]{31:0} = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  419. **Examples**::
  420. 1: STO 0x12
  421. STI32 R1, R2, 0 // MEM[R2 + 0x12 / 4][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  422. // offset += (1 * 4) //offset is incremented by 1 word
  423. STI32 R1, R2, 0 // MEM[R2 + 0x16 / 4][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  424. **LD** – Load Data from the Memory
  425. ----------------------------------
  426. **Syntax**
  427. **LD** **Rdst, Rsrc, offset**
  428. **Operands**
  429. - **Rdst** – Register R[0..3], destination
  430. - **Rsrc** – Register R[0..3], holds address of destination, in 32-bit words
  431. - **Offset** – 13-bit signed value, offset in bytes
  432. **Cycles**
  433. 4 cycles to execute, 4 cycles to fetch next instruction
  434. **Description**
  435. The instruction loads the lower 16-bit half-word from memory with address [Rsrc + offset / 4] into the destination register Rdst::
  436. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  437. .. note::
  438. Note that the offset specified in bytes is converted to a 32-bit word offset before execution. See the section :ref:`ulp-fsm-addressing` for more details.
  439. **Examples**::
  440. 1: LD R1, R2, 0x12 // R1 = MEM[R2 + 0x12 / 4]
  441. 2: .data // Data section definition
  442. Addr1: .word 123 // Define label Addr1 16 bit
  443. .set offs, 0x00 // Define constant offs
  444. .text // Text section definition
  445. MOVE R1, 1 // R1 = 1
  446. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  447. LD R1, R2, offs // R1 = MEM[R2 + 0]
  448. // R1 will be 123
  449. .. only:: esp32s2 or esp32s3
  450. **LDL** – Load Data from the Lower Half-Word of the 32-bit Memory
  451. ------------------------------------------------------------------
  452. **Syntax**
  453. **LDL** **Rdst, Rsrc, offset**
  454. **Operands**
  455. - **Rdst** – Register R[0..3], destination
  456. - **Rsrc** – Register R[0..3], holds address of destination, in 32-bit words
  457. - **Offset** – 13-bit signed value, offset in bytes
  458. **Cycles**
  459. 4 cycles to execute, 4 cycles to fetch next instruction
  460. **Description**
  461. The instruction loads the lower 16-bit half-word from memory with address [Rsrc + offset / 4] into the destination register Rdst::
  462. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  463. The ``LD`` and the ``LDL`` commands can be used interchangeably and have been provided to maintain backward compatibility with previous versions of the ULP core.
  464. .. note::
  465. Note that the offset specified in bytes is converted to a 32-bit word offset before execution. See the section :ref:`ulp-fsm-addressing` for more details.
  466. **Examples**::
  467. 1: LDL R1, R2, 0x12 // R1 = MEM[R2 + 0x12 / 4]
  468. 2: .data // Data section definition
  469. Addr1: .word 123 // Define label Addr1 16 bit
  470. .set offs, 0x00 // Define constant offs
  471. .text // Text section definition
  472. MOVE R1, 1 // R1 = 1
  473. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  474. LDL R1, R2, offs // R1 = MEM[R2 + 0]
  475. // R1 will be 123
  476. **LDH** – Load data from upper half-word of the 32-bit memory
  477. --------------------------------------------------------------
  478. **Syntax**
  479. **LDH** **Rdst, Rsrc, offset**
  480. **Operands**
  481. - **Rdst** – Register R[0..3], destination
  482. - **Rsrc** – Register R[0..3], holds address of destination, in 32-bit words
  483. - **Offset** – 13-bit signed value, offset in bytes
  484. **Cycles**
  485. 4 cycles to execute, 4 cycles to fetch next instruction
  486. **Description**
  487. The instruction loads the upper 16-bit half-word from memory with address [Rsrc + offset / 4] into the destination register Rdst::
  488. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  489. .. note::
  490. Note that the offset specified in bytes is converted to a 32-bit word offset before execution. See the section :ref:`ulp-fsm-addressing` for more details.
  491. **Examples**::
  492. 1: LDH R1, R2, 0x12 // R1 = MEM[R2 + 0x12 / 4]
  493. 2: .data // Data section definition
  494. Addr1: .word 0x12345678 // Define label Addr1 16 bit
  495. .set offs, 0x00 // Define constant offs
  496. .text // Text section definition
  497. MOVE R1, 1 // R1 = 1
  498. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  499. LDH R1, R2, offs // R1 = MEM[R2 + 0]
  500. // R1 will be 0x1234
  501. **JUMP** – Jump to an Absolute Address
  502. --------------------------------------
  503. **Syntax**
  504. **JUMP** **Rdst**
  505. **JUMP** **ImmAddr**
  506. **JUMP** **Rdst, Condition**
  507. **JUMP** **ImmAddr, Condition**
  508. **Operands**
  509. - **Rdst** – Register R[0..3] containing address to jump to (expressed in 32-bit words)
  510. - **ImmAddr** – 13 bits address (expressed in bytes), aligned to 4 bytes
  511. - **Condition**:
  512. - EQ – jump if last ALU operation result was zero
  513. - OV – jump if last ALU has set overflow flag
  514. **Cycles**
  515. 2 cycles to execute, 2 cycles to fetch next instruction
  516. **Description**
  517. The instruction makes jump to the specified address. Jump can be either unconditional or based on an ALU flag.
  518. **Examples**::
  519. 1: JUMP R1 // Jump to address in R1 (address in R1 is in 32-bit words)
  520. 2: JUMP 0x120, EQ // Jump to address 0x120 (in bytes) if ALU result is zero
  521. 3: JUMP label // Jump to label
  522. ...
  523. label: nop // Definition of label
  524. 4: .global label // Declaration of global label
  525. MOVE R1, label // R1 = label (value loaded into R1 is in words)
  526. JUMP R1 // Jump to label
  527. ...
  528. label: nop // Definition of label
  529. **JUMPR** – Jump to a Relative Offset (Condition Based on R0)
  530. -------------------------------------------------------------
  531. **Syntax**
  532. **JUMPR** **Step, Threshold, Condition**
  533. **Operands**
  534. - **Step** – relative shift from current position, in bytes
  535. - **Threshold** – threshold value for branch condition
  536. - **Condition**:
  537. - **EQ** (equal) – jump if value in R0 == threshold
  538. - **LT** (less than) – jump if value in R0 < threshold
  539. - **LE** (less or equal) – jump if value in R0 <= threshold
  540. - **GT** (greater than) – jump if value in R0 > threshold
  541. - **GE** (greater or equal) – jump if value in R0 >= threshold
  542. **Cycles**
  543. .. only:: esp32
  544. Conditions **LT**, **GE**, **LE** and **GT**: 2 cycles to execute, 2 cycles to fetch next instruction
  545. Conditions **LE** and **GT** are implemented in the assembler using one **JUMPR** instruction::
  546. // JUMPR target, threshold, GT is implemented as:
  547. JUMPR target, threshold+1, GE
  548. // JUMPR target, threshold, LE is implemented as:
  549. JUMPR target, threshold + 1, LT
  550. Conditions **EQ** is implemented in the assembler using two **JUMPR** instructions::
  551. // JUMPR target, threshold, EQ is implemented as:
  552. JUMPR next, threshold + 1, GE
  553. JUMPR target, threshold, GE
  554. next:
  555. .. only:: esp32s2 or esp32s3
  556. Conditions **EQ**, **GT** and **LT**: 2 cycles to execute, 2 cycles to fetch next instruction
  557. Conditions **LE** and **GE** are implemented in the assembler using two **JUMPR** instructions::
  558. // JUMPR target, threshold, LE is implemented as:
  559. JUMPR target, threshold, EQ
  560. JUMPR target, threshold, LT
  561. // JUMPR target, threshold, GE is implemented as:
  562. JUMPR target, threshold, EQ
  563. JUMPR target, threshold, GT
  564. Therefore the execution time depends on the branches taken: either 2 cycles to execute + 2 cycles to fetch, or 4 cycles to execute + 4 cycles to fetch.
  565. **Description**
  566. The instruction makes a jump to a relative address if condition is true. Condition is the result of comparison of R0 register value and the threshold value.
  567. **Examples**::
  568. 1:pos: JUMPR 16, 20, GE // Jump to address (position + 16 bytes) if value in R0 >= 20
  569. 2: // Down counting loop using R0 register
  570. MOVE R0, 16 // load 16 into R0
  571. label: SUB R0, R0, 1 // R0--
  572. NOP // do something
  573. JUMPR label, 1, GE // jump to label if R0 >= 1
  574. **JUMPS** – Jump to a Relative Address (Condition Based on Stage Count)
  575. -----------------------------------------------------------------------
  576. **Syntax**
  577. **JUMPS** **Step, Threshold, Condition**
  578. **Operands**
  579. - **Step** – relative shift from current position, in bytes
  580. - **Threshold** – threshold value for branch condition
  581. - **Condition**:
  582. - **EQ** (equal) – jump if value in stage_cnt == threshold
  583. - **LT** (less than) – jump if value in stage_cnt < threshold
  584. - **LE** (less or equal) - jump if value in stage_cnt <= threshold
  585. - **GT** (greater than) – jump if value in stage_cnt > threshold
  586. - **GE** (greater or equal) — jump if value in stage_cnt >= threshold
  587. **Cycles**
  588. .. only:: esp32
  589. Conditions **LE**, **LT**, **GE**: 2 cycles to execute, 2 cycles to fetch next instruction
  590. Conditions **EQ**, **GT** are implemented in the assembler using two **JUMPS** instructions::
  591. // JUMPS target, threshold, EQ is implemented as:
  592. JUMPS next, threshold, LT
  593. JUMPS target, threshold, LE
  594. next:
  595. // JUMPS target, threshold, GT is implemented as:
  596. JUMPS next, threshold, LE
  597. JUMPS target, threshold, GE
  598. next:
  599. Therefore the execution time depends on the branches taken: either 2 cycles to execute + 2 cycles to fetch, or 4 cycles to execute + 4 cycles to fetch.
  600. .. only:: esp32s2 or esp32s3
  601. 2 cycles to execute, 2 cycles to fetch next instruction
  602. **Description**
  603. The instruction makes a jump to a relative address if condition is true. Condition is the result of comparison of count register value and threshold value.
  604. **Examples**::
  605. 1:pos: JUMPS 16, 20, EQ // Jump to (position + 16 bytes) if stage_cnt == 20
  606. 2: // Up counting loop using stage count register
  607. STAGE_RST // set stage_cnt to 0
  608. label: STAGE_INC 1 // stage_cnt++
  609. NOP // do something
  610. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  611. **STAGE_RST** – Reset Stage Count Register
  612. ------------------------------------------
  613. **Syntax**
  614. **STAGE_RST**
  615. **Operands**
  616. No operands
  617. **Description**
  618. The instruction sets the stage count register to 0
  619. **Cycles**
  620. 2 cycles to execute, 4 cycles to fetch next instruction
  621. **Examples**::
  622. 1: STAGE_RST // Reset stage count register
  623. **STAGE_INC** – Increment Stage Count Register
  624. ----------------------------------------------
  625. **Syntax**
  626. **STAGE_INC** **Value**
  627. **Operands**
  628. - **Value** – 8 bits value
  629. **Cycles**
  630. 2 cycles to execute, 4 cycles to fetch next instruction
  631. **Description**
  632. The instruction increments the stage count register by the given value.
  633. **Examples**::
  634. 1: STAGE_INC 10 // stage_cnt += 10
  635. 2: // Up counting loop example:
  636. STAGE_RST // set stage_cnt to 0
  637. label: STAGE_INC 1 // stage_cnt++
  638. NOP // do something
  639. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  640. **STAGE_DEC** – Decrement Stage Count Register
  641. ----------------------------------------------
  642. **Syntax**
  643. **STAGE_DEC** **Value**
  644. **Operands**
  645. - **Value** – 8 bits value
  646. **Cycles**
  647. 2 cycles to execute, 4 cycles to fetch next instruction
  648. **Description**
  649. The instruction decrements the stage count register by the given value.
  650. **Examples**::
  651. 1: STAGE_DEC 10 // stage_cnt -= 10;
  652. 2: // Down counting loop example
  653. STAGE_RST // set stage_cnt to 0
  654. STAGE_INC 16 // increment stage_cnt to 16
  655. label: STAGE_DEC 1 // stage_cnt--;
  656. NOP // do something
  657. JUMPS label, 0, GT // jump to label if stage_cnt > 0
  658. **HALT** – End the Program
  659. --------------------------
  660. **Syntax**
  661. **HALT**
  662. **Operands**
  663. No operands
  664. **Cycles**
  665. 2 cycles to execute
  666. **Description**
  667. The instruction halts the ULP coprocessor and restarts the ULP wakeup timer, if it is enabled.
  668. **Examples**::
  669. 1: HALT // Halt the coprocessor
  670. **WAKE** – Wake up the Chip
  671. ---------------------------
  672. **Syntax**
  673. **WAKE**
  674. **Operands**
  675. No operands
  676. **Cycles**
  677. 2 cycles to execute, 4 cycles to fetch next instruction
  678. **Description**
  679. The instruction sends an interrupt from the ULP coprocessor to the RTC controller.
  680. - If the SoC is in deep sleep mode, and ULP wakeup is enabled, this causes the SoC to wake up.
  681. - If the SoC is not in deep sleep mode, and ULP interrupt bit (RTC_CNTL_ULP_CP_INT_ENA) is set in RTC_CNTL_INT_ENA_REG register, RTC interrupt will be triggered.
  682. .. note::
  683. Note that before using WAKE instruction, ULP program may need to wait until RTC controller is ready to wake up the main CPU. This is indicated using RTC_CNTL_RDY_FOR_WAKEUP bit of RTC_CNTL_LOW_POWER_ST_REG register. If WAKE instruction is executed while RTC_CNTL_RDY_FOR_WAKEUP is zero, it has no effect (wake up does not occur). If the WAKE instruction is intended to be used while the main CPU is not in sleep mode then the RTC_CNTL_MAIN_STATE_IN_IDLE (bit 27) of RTC_CNTL_LOW_POWER_ST_REG can be used to check whether main CPU is in normal mode or sleep mode.
  684. **Examples**::
  685. 1: is_rdy_for_wakeup: // Read RTC_CNTL_RDY_FOR_WAKEUP bit
  686. READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP)
  687. AND r0, r0, 1
  688. JUMP is_rdy_for_wakeup, eq // Retry until the bit is set
  689. WAKE // Trigger wake up
  690. REG_WR 0x006, 24, 24, 0 // Stop ULP timer (clear RTC_CNTL_ULP_CP_SLP_TIMER_EN)
  691. HALT // Stop the ULP program
  692. // After these instructions, SoC will wake up,
  693. // and ULP will not run again until started by the main program.
  694. 1: check_wakeup: // Read RTC_CNTL_RDY_FOR_WAKEUP and RTC_CNTL_MAIN_STATE_IN_IDLE bit
  695. READ_RTC_REG(RTC_CNTL_LOW_POWER_ST_REG, 27, 1)
  696. MOVE r1, r0 // Copy result in to r1
  697. READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP)
  698. OR r0, r0, r1
  699. JUMP check_wakeup, eq // Retry until either of the bit are set
  700. WAKE // Trigger wake up
  701. HALT // Stop the ULP program
  702. .. only:: esp32
  703. **SLEEP** – Set ULP Wakeup Timer Period
  704. ---------------------------------------
  705. **Syntax**
  706. **SLEEP** **sleep_reg**
  707. **Operands**
  708. - **sleep_reg** – 0..4, selects one of ``SENS_ULP_CP_SLEEP_CYCx_REG`` registers.
  709. **Cycles**
  710. 2 cycles to execute, 4 cycles to fetch next instruction
  711. **Description**
  712. The instruction selects which of the ``SENS_ULP_CP_SLEEP_CYCx_REG`` (x = 0..4) register values is to be used by the ULP wakeup timer as wakeup period. By default, the value from ``SENS_ULP_CP_SLEEP_CYC0_REG`` is used.
  713. **Examples**::
  714. 1: SLEEP 1 // Use period set in SENS_ULP_CP_SLEEP_CYC1_REG
  715. 2: .set sleep_reg, 4 // Set constant
  716. SLEEP sleep_reg // Use period set in SENS_ULP_CP_SLEEP_CYC4_REG
  717. **WAIT** – Wait Some Number of Cycles
  718. -------------------------------------
  719. **Syntax**
  720. **WAIT** **Cycles**
  721. **Operands**
  722. - **Cycles** – number of cycles for wait
  723. **Cycles**
  724. 2 + **Cycles** cycles to execute, 4 cycles to fetch next instruction
  725. **Description**
  726. The instruction delays for given number of cycles.
  727. **Examples**::
  728. 1: WAIT 10 // Do nothing for 10 cycles
  729. 2: .set wait_cnt, 10 // Set a constant
  730. WAIT wait_cnt // wait for 10 cycles
  731. .. only:: not esp32
  732. **TSENS** – Do Measurement with Temperature Sensor
  733. --------------------------------------------------
  734. **Syntax**
  735. - **TSENS** **Rdst, Wait_Delay**
  736. **Operands**
  737. - **Rdst** – Destination Register R[0..3], result will be stored to this register
  738. - **Wait_Delay** – number of cycles used to perform the measurement
  739. **Cycles**
  740. 2 + **Wait_Delay** + 3 * TSENS_CLK to execute, 4 cycles to fetch next instruction
  741. **Description**
  742. The instruction performs measurement using TSENS and stores the result into a general purpose register.
  743. **Examples**::
  744. 1: TSENS R1, 1000 // Measure temperature sensor for 1000 cycles,
  745. // and store result to R1
  746. **ADC** – Do Measurement with ADC
  747. ---------------------------------
  748. **Syntax**
  749. - **ADC** **Rdst, Sar_sel, Mux**
  750. - **ADC** **Rdst, Sar_sel, Mux, 0** — deprecated form
  751. **Operands**
  752. - **Rdst** – Destination Register R[0..3], result will be stored to this register
  753. - **Sar_sel** – Select ADC: 0 = SARADC1, 1 = SARADC2
  754. .. only:: esp32
  755. - **Mux** - Enable ADC channel. Channel number is [Mux-1]. If the user passes Mux value 1, then ADC channel 0 gets used.
  756. .. only:: esp32s2 or esp32s3
  757. - **Mux** - selected PAD, SARADC Pad[Mux-1] is enabled. If the user passes Mux value 1, then ADC pad 0 gets used.
  758. **Cycles**
  759. ``23 + max(1, SAR_AMP_WAIT1) + max(1, SAR_AMP_WAIT2) + max(1, SAR_AMP_WAIT3) + SARx_SAMPLE_CYCLE + SARx_SAMPLE_BIT`` cycles to execute, 4 cycles to fetch next instruction
  760. **Description**
  761. The instruction makes measurements from ADC.
  762. **Examples**::
  763. .. only:: esp32
  764. 1: ADC R1, 0, 1 // Measure value using ADC1 channel 0 and store result into R1
  765. .. only:: esp32s2 or esp32s3
  766. 1: ADC R1, 0, 1 // Measure value using ADC1 pad 2 and store result into R1
  767. .. only:: esp32
  768. **I2C_RD** - Read Single Byte from I2C Slave
  769. ----------------------------------------------
  770. **Syntax**
  771. - **I2C_RD** **Sub_addr, High, Low, Slave_sel**
  772. **Operands**
  773. - **Sub_addr** – Address within the I2C slave to read.
  774. - **High*, *Low** — Define range of bits to read. Bits outside of [High, Low] range are masked.
  775. - **Slave_sel** - Index of I2C slave address to use.
  776. **Cycles**
  777. Execution time mostly depends on I2C communication time. 4 cycles to fetch next instruction.
  778. **Description**
  779. ``I2C_RD`` instruction reads one byte from I2C slave with index ``Slave_sel``. Slave address (in 7-bit format) has to be set in advance into ``SENS_I2C_SLAVE_ADDRx`` register field, where ``x == Slave_sel``. 8 bits of read result is stored into ``R0`` register.
  780. **Examples**::
  781. 1: I2C_RD 0x10, 7, 0, 0 // Read byte from sub-address 0x10 of slave with address set in SENS_I2C_SLAVE_ADDR0
  782. **I2C_WR** - write single byte to I2C slave
  783. ----------------------------------------------
  784. **Syntax**
  785. - **I2C_WR** **Sub_addr, Value, High, Low, Slave_sel**
  786. **Operands**
  787. - **Sub_addr** – Address within the I2C slave to write.
  788. - **Value** – 8-bit value to be written.
  789. - **High**, **Low** — Define range of bits to write. Bits outside of [High, Low] range are masked.
  790. - **Slave_sel** - Index of I2C slave address to use.
  791. **Cycles**
  792. Execution time mostly depends on I2C communication time. 4 cycles to fetch next instruction.
  793. **Description**
  794. ``I2C_WR`` instruction writes one byte to I2C slave with index ``Slave_sel``. Slave address (in 7-bit format) has to be set in advance into ``SENS_I2C_SLAVE_ADDRx`` register field, where ``x == Slave_sel``.
  795. **Examples**::
  796. 1: I2C_WR 0x20, 0x33, 7, 0, 1 // Write byte 0x33 to sub-address 0x20 of slave with address set in SENS_I2C_SLAVE_ADDR1.
  797. **REG_RD** – Read from Peripheral Register
  798. ------------------------------------------
  799. **Syntax**
  800. **REG_RD** **Addr, High, Low**
  801. **Operands**
  802. - **Addr** – Register address, in 32-bit words
  803. - **High** – Register end bit number
  804. - **Low** – Register start bit number
  805. **Cycles**
  806. 4 cycles to execute, 4 cycles to fetch next instruction
  807. **Description**
  808. The instruction reads up to 16 bits from a peripheral register into a general purpose register: ``R0 = REG[Addr][High:Low]``.
  809. .. only:: esp32
  810. This instruction can access registers in RTC_CNTL, RTC_IO, SENS, and RTC_I2C peripherals. Address of the register, as seen from the ULP, can be calculated from the address of the same register on the DPORT bus as follows::
  811. addr_ulp = (addr_dport - DR_REG_RTCCNTL_BASE) / 4
  812. .. only:: esp32s2 or esp32s3
  813. This instruction can access registers in RTC_CNTL, RTC_IO, SENS, and RTC_I2C peripherals. Address of the register, as seen from the ULP, can be calculated from the address of the same register on the PeriBUS1 as follows::
  814. addr_ulp = (addr_peribus1 - DR_REG_RTCCNTL_BASE) / 4
  815. **Examples**::
  816. 1: REG_RD 0x120, 7, 4 // load 4 bits: R0 = {12'b0, REG[0x120][7:4]}
  817. **REG_WR** – Write to Peripheral Register
  818. -----------------------------------------
  819. **Syntax**
  820. **REG_WR** **Addr, High, Low, Data**
  821. **Operands**
  822. - **Addr** – Register address, in 32-bit words.
  823. - **High** – Register end bit number
  824. - **Low** – Register start bit number
  825. - **Data** – Value to write, 8 bits
  826. **Cycles**
  827. 8 cycles to execute, 4 cycles to fetch next instruction
  828. **Description**
  829. The instruction writes up to 8 bits from an immediate data value into a peripheral register: ``REG[Addr][High:Low] = data``.
  830. .. only:: esp32
  831. This instruction can access registers in RTC_CNTL, RTC_IO, SENS, and RTC_I2C peripherals. Address of the register, as seen from the ULP, can be calculated from the address of the same register on the DPORT bus as follows::
  832. addr_ulp = (addr_dport - DR_REG_RTCCNTL_BASE) / 4
  833. .. only:: esp32s2 or esp32s3
  834. This instruction can access registers in RTC_CNTL, RTC_IO, SENS, and RTC_I2C peripherals. Address of the register, as seen from the ULP, can be calculated from the address of the same register on the PeriBUS1 as follows::
  835. addr_ulp = (addr_peribus1 - DR_REG_RTCCNTL_BASE) / 4
  836. **Examples**::
  837. 1: REG_WR 0x120, 7, 0, 0x10 // set 8 bits: REG[0x120][7:0] = 0x10
  838. Convenience Macros for Peripheral Registers Access
  839. --------------------------------------------------
  840. ULP source files are passed through C preprocessor before the assembler. This allows certain macros to be used to facilitate access to peripheral registers.
  841. Some existing macros are defined in ``soc/soc_ulp.h`` header file. These macros allow access to the fields of peripheral registers by their names.
  842. Peripheral registers names which can be used with these macros are the ones defined in ``soc/rtc_cntl_reg.h``, ``soc/rtc_io_reg.h``, ``soc/sens_reg.h``, and ``soc/rtc_i2c_reg.h``.
  843. READ_RTC_REG(rtc_reg, low_bit, bit_width)
  844. Read up to 16 bits from rtc_reg[low_bit + bit_width - 1 : low_bit] into R0. For example::
  845. #include "soc/soc_ulp.h"
  846. #include "soc/rtc_cntl_reg.h"
  847. /* Read 16 lower bits of RTC_CNTL_TIME0_REG into R0 */
  848. READ_RTC_REG(RTC_CNTL_TIME0_REG, 0, 16)
  849. READ_RTC_FIELD(rtc_reg, field)
  850. Read from a field in rtc_reg into R0, up to 16 bits. For example::
  851. #include "soc/soc_ulp.h"
  852. #include "soc/sens_reg.h"
  853. /* Read 8-bit SENS_TSENS_OUT field of SENS_SAR_SLAVE_ADDR3_REG into R0 */
  854. READ_RTC_FIELD(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT)
  855. WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value)
  856. Write immediate value into rtc_reg[low_bit + bit_width - 1 : low_bit], bit_width <= 8. For example::
  857. #include "soc/soc_ulp.h"
  858. #include "soc/rtc_io_reg.h"
  859. /* Set BIT(2) of RTC_GPIO_OUT_DATA_W1TS field in RTC_GPIO_OUT_W1TS_REG */
  860. WRITE_RTC_REG(RTC_GPIO_OUT_W1TS_REG, RTC_GPIO_OUT_DATA_W1TS_S + 2, 1, 1)
  861. WRITE_RTC_FIELD(rtc_reg, field, value)
  862. Write immediate value into a field in rtc_reg, up to 8 bits. For example::
  863. #include "soc/soc_ulp.h"
  864. #include "soc/rtc_cntl_reg.h"
  865. /* Set RTC_CNTL_ULP_CP_SLP_TIMER_EN field of RTC_CNTL_STATE0_REG to 0 */
  866. WRITE_RTC_FIELD(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN, 0)