ulp_instruction_set.rst 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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 8k 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. **Examples**::
  189. 1: LSH R1, R2, R3 // R1 = R2 << R3
  190. 2: LSH R1, R2, 0x03 // R1 = R2 << 0x03
  191. 3: .set value1, 0x03 // constant value1=0x03
  192. LSH R1, R2, value1 // R1 = R2 << value1
  193. 4: .global label // declaration of variable label
  194. LSH R1, R2, label // R1 = R2 << label
  195. ...
  196. label: nop // definition of variable label
  197. **RSH** - Logical Shift Right
  198. -----------------------------
  199. **Syntax**
  200. **RSH** *Rdst, Rsrc1, Rsrc2*
  201. **RSH** *Rdst, Rsrc1, imm*
  202. **Operands**
  203. *Rdst* - Register R[0..3]
  204. *Rsrc1* - Register R[0..3]
  205. *Rsrc2* - Register R[0..3]
  206. *Imm* - 16-bit signed value
  207. **Cycles**
  208. 2 cycles to execute, 4 cycles to fetch next instruction
  209. **Description**
  210. 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.
  211. **Examples**::
  212. 1: RSH R1, R2, R3 // R1 = R2 >> R3
  213. 2: RSH R1, R2, 0x03 // R1 = R2 >> 0x03
  214. 3: .set value1, 0x03 // constant value1=0x03
  215. RSH R1, R2, value1 // R1 = R2 >> value1
  216. 4: .global label // declaration of variable label
  217. RSH R1, R2, label // R1 = R2 >> label
  218. label: nop // definition of variable label
  219. **MOVE** – Move to register
  220. ---------------------------
  221. **Syntax**
  222. **MOVE** *Rdst, Rsrc*
  223. **MOVE** *Rdst, imm*
  224. **Operands**
  225. - *Rdst* – Register R[0..3]
  226. - *Rsrc* – Register R[0..3]
  227. - *Imm* – 16-bit signed value
  228. **Cycles**
  229. 2 cycles to execute, 4 cycles to fetch next instruction
  230. **Description**
  231. The instruction moves the value from the source register or a 16-bit signed value to the destination register.
  232. .. note::
  233. 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.
  234. **Examples**::
  235. 1: MOVE R1, R2 // R1 = R2
  236. 2: MOVE R1, 0x03 // R1 = 0x03
  237. 3: .set value1, 0x03 // constant value1=0x03
  238. MOVE R1, value1 // R1 = value1
  239. 4: .global label // declaration of label
  240. MOVE R1, label // R1 = address_of(label) / 4
  241. ...
  242. label: nop // definition of label
  243. **ST** – Store data to the memory
  244. ---------------------------------
  245. **Syntax**
  246. **ST** *Rsrc, Rdst, offset*
  247. **Operands**
  248. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  249. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  250. - *Offset* – 13-bit signed value, offset in bytes
  251. **Cycles**
  252. 4 cycles to execute, 4 cycles to fetch next instruction
  253. **Description**
  254. 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)::
  255. Mem[Rdst + offset / 4]{31:0} = {PC[10:0], 3'b0, Rdst, Rsrc[15:0]}
  256. The application can use the higher 16 bits to determine which instruction in the ULP program has written any particular word into memory.
  257. .. note::
  258. 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.
  259. **Examples**::
  260. 1: ST R1, R2, 0x12 // MEM[R2 + 0x12 / 4] = R1
  261. 2: .data // Data section definition
  262. Addr1: .word 123 // Define label Addr1 16 bit
  263. .set offs, 0x00 // Define constant offs
  264. .text // Text section definition
  265. MOVE R1, 1 // R1 = 1
  266. MOVE R2, Addr1 // R2 = Addr1
  267. ST R1, R2, offs // MEM[R2 + 0 / 4] = R1
  268. // MEM[Addr1 + 0] will be 32'h600001
  269. .. only:: esp32s2 or esp32s3
  270. **STL** – Store data to the lower 16 bits of 32-bit memory
  271. ----------------------------------------------------------------
  272. **Syntax**
  273. **STL** *Rsrc, Rdst, offset, Label*
  274. **Operands**
  275. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  276. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  277. - *Offset* – 11-bit signed value, offset in bytes
  278. - *Label* – 2-bit user defined unsigned value
  279. **Cycles**
  280. 4 cycles to execute, 4 cycles to fetch next instruction
  281. **Description**
  282. The instruction stores the 16-bit value of Rsrc to the lower half-word of the memory with address [Rdst + offset / 4]::
  283. Mem[Rdst + offset / 4]{15:0} = {Rsrc[15:0]}
  284. Mem[Rdst + offset / 4]{15:0} = {Label[1:0],Rsrc[13:0]}
  285. 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.
  286. .. note::
  287. 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.
  288. **Examples**::
  289. 1: STL R1, R2, 0x12 // MEM[R2 + 0x12 / 4] = R1
  290. 2: .data // Data section definition
  291. Addr1: .word 123 // Define label Addr1 16 bit
  292. .set offs, 0x00 // Define constant offs
  293. .text // Text section definition
  294. MOVE R1, 1 // R1 = 1
  295. MOVE R2, Addr1 // R2 = Addr1
  296. STL R1, R2, offs // MEM[R2 + 0 / 4] = R1
  297. // MEM[Addr1 + 0] will be 32'hxxxx0001
  298. 3:
  299. MOVE R1, 1 // R1 = 1
  300. STL R1, R2, 0x12, 1 // MEM[R2 + 0x12 / 4] = 0xxxxx4001
  301. **STH** – Store data to the higher 16 bits of 32-bit memory
  302. ------------------------------------------------------------
  303. **Syntax**
  304. **STH** *Rsrc, Rdst, offset, Label*
  305. **Operands**
  306. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  307. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  308. - *Offset* – 11-bit signed value, offset in bytes
  309. - *Label* – 2-bit user defined unsigned value
  310. **Cycles**
  311. 4 cycles to execute, 4 cycles to fetch next instruction
  312. **Description**
  313. The instruction stores the 16-bit value of Rsrc to the upper half-word of memory with address [Rdst + offset / 4]::
  314. Mem[Rdst + offset / 4]{31:16} = {Rsrc[15:0]}
  315. Mem[Rdst + offset / 4]{31:16} = {Label[1:0],Rsrc[13:0]}
  316. .. note::
  317. 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.
  318. **Examples**::
  319. 1: STH R1, R2, 0x12 // MEM[R2 + 0x12 / 4][31:16] = R1
  320. 2: .data // Data section definition
  321. Addr1: .word 123 // Define label Addr1 16 bit
  322. .set offs, 0x00 // Define constant offs
  323. .text // Text section definition
  324. MOVE R1, 1 // R1 = 1
  325. MOVE R2, Addr1 // R2 = Addr1
  326. STH R1, R2, offs // MEM[R2 + 0 / 4] = R1
  327. // MEM[Addr1 + 0] will be 32'h0001xxxx
  328. 3:
  329. MOVE R1, 1 // R1 = 1
  330. STH R1, R2, 0x12, 1 // MEM[R2 + 0x12 / 4] 0x4001xxxx
  331. **ST32** – Store 32-bits data to the 32-bits memory
  332. ---------------------------------------------------
  333. **Syntax**
  334. **ST32** *Rsrc, Rdst, offset, Label*
  335. **Operands**
  336. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  337. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  338. - *Offset* – 11-bit signed value, offset in bytes
  339. - *Label* – 2-bit user defined unsigned value
  340. **Cycles**
  341. 4 cycles to execute, 4 cycles to fetch next instruction
  342. **Description**
  343. 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]::
  344. Mem[Rdst + offset / 4]{31:0} = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  345. .. note::
  346. 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.
  347. **Examples**::
  348. 1: ST32 R1, R2, 0x12, 0 // MEM[R2 + 0x12 / 4][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  349. 2: .data // Data section definition
  350. Addr1: .word 123 // Define label Addr1 16 bit
  351. .set offs, 0x00 // Define constant offs
  352. .text // Text section definition
  353. MOVE R1, 1 // R1 = 1
  354. MOVE R2, Addr1 // R2 = Addr1
  355. ST32 R1, R2, offs, 1 // MEM[R2 + 0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  356. // MEM[Addr1 + 0] will be 32'h00010001
  357. **STO** – Set offset value for auto increment operation
  358. -------------------------------------------------------
  359. **Syntax**
  360. **STO** *offset*
  361. **Operands**
  362. - *Offset* – 11-bit signed value, offset in bytes
  363. **Cycles**
  364. 4 cycles to execute, 4 cycles to fetch next instruction
  365. **Description**
  366. The instruction sets the 16-bit value to the offset register::
  367. offset = value / 4
  368. .. note::
  369. 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.
  370. **Examples**::
  371. 1: STO 0x12 // Offset = 0x12 / 4
  372. 2: .data // Data section definition
  373. Addr1: .word 123 // Define label Addr1 16 bit
  374. .set offs, 0x00 // Define constant offs
  375. .text // Text section definition
  376. STO offs // Offset = 0x00
  377. **STI** – Store data to the 32-bits memory with auto increment of predefined offset address
  378. -------------------------------------------------------------------------------------------
  379. **Syntax**
  380. **STI** *Rsrc, Rdst, Label*
  381. **Operands**
  382. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  383. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  384. - *Label* – 2-bit user defined unsigned value
  385. **Cycles**
  386. 4 cycles to execute, 4 cycles to fetch next instruction
  387. **Description**
  388. The instruction stores the 16-bit value of Rsrc to the lower and upper half-word of memory with address [Rdst + offset / 4].
  389. The offset value is auto incremented when the STI instruction is called twice. Make sure to execute the ``STO`` instruction
  390. to set the offset value before executing the STI instruction::
  391. Mem[Rdst + offset / 4]{15:0/31:16} = {Rsrc[15:0]}
  392. Mem[Rdst + offset / 4]{15:0/31:16} = {Label[1:0],Rsrc[13:0]}
  393. **Examples**::
  394. 1: STO 4 // Set offset to 4
  395. STI R1, R2 // MEM[R2 + 4 / 4][15:0] = R1
  396. STI R1, R2 // MEM[R2 + 4 / 4][31:16] = R1
  397. // offset += (1 * 4) //offset is incremented by 1 word
  398. STI R1, R2 // MEM[R2 + 8 / 4][15:0] = R1
  399. STI R1, R2 // MEM[R2 + 8 / 4][31:16] = R1
  400. **STI32** – Store 32-bits data to the 32-bits memory with auto increment of adress offset
  401. -----------------------------------------------------------------------------------------
  402. **Syntax**
  403. **STI32** *Rsrc, Rdst, Label*
  404. **Operands**
  405. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  406. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  407. - *Label* – 2-bit user defined unsigned value
  408. **Cycles**
  409. 4 cycles to execute, 4 cycles to fetch next instruction
  410. **Description**
  411. 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].
  412. The offset value is auto incremented each time the STI32 instruction is called. Make sure to execute the ``STO`` instruction
  413. to set the offset value before executing the STI32 instruction::
  414. Mem[Rdst + offset / 4]{31:0} = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  415. **Examples**::
  416. 1: STO 0x12
  417. STI32 R1, R2, 0 // MEM[R2 + 0x12 / 4][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  418. // offset += (1 * 4) //offset is incremented by 1 word
  419. STI32 R1, R2, 0 // MEM[R2 + 0x16 / 4][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  420. **LD** – Load data from the memory
  421. ----------------------------------
  422. **Syntax**
  423. **LD** *Rdst, Rsrc, offset*
  424. **Operands**
  425. - *Rdst* – Register R[0..3], destination
  426. - *Rsrc* – Register R[0..3], holds address of destination, in 32-bit words
  427. - *Offset* – 13-bit signed value, offset in bytes
  428. **Cycles**
  429. 4 cycles to execute, 4 cycles to fetch next instruction
  430. **Description**
  431. The instruction loads the lower 16-bit half-word from memory with address [Rsrc + offset / 4] into the destination register Rdst::
  432. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  433. .. note::
  434. 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.
  435. **Examples**::
  436. 1: LD R1, R2, 0x12 // R1 = MEM[R2 + 0x12 / 4]
  437. 2: .data // Data section definition
  438. Addr1: .word 123 // Define label Addr1 16 bit
  439. .set offs, 0x00 // Define constant offs
  440. .text // Text section definition
  441. MOVE R1, 1 // R1 = 1
  442. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  443. LD R1, R2, offs // R1 = MEM[R2 + 0]
  444. // R1 will be 123
  445. .. only:: esp32s2 or esp32s3
  446. **LDL** – Load data from the lower half-word of the 32-bit memory
  447. ------------------------------------------------------------------
  448. **Syntax**
  449. **LDL** *Rdst, Rsrc, offset*
  450. **Operands**
  451. - *Rdst* – Register R[0..3], destination
  452. - *Rsrc* – Register R[0..3], holds address of destination, in 32-bit words
  453. - *Offset* – 13-bit signed value, offset in bytes
  454. **Cycles**
  455. 4 cycles to execute, 4 cycles to fetch next instruction
  456. **Description**
  457. The instruction loads the lower 16-bit half-word from memory with address [Rsrc + offset / 4] into the destination register Rdst::
  458. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  459. 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.
  460. .. note::
  461. 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.
  462. **Examples**::
  463. 1: LDL R1, R2, 0x12 // R1 = MEM[R2 + 0x12 / 4]
  464. 2: .data // Data section definition
  465. Addr1: .word 123 // Define label Addr1 16 bit
  466. .set offs, 0x00 // Define constant offs
  467. .text // Text section definition
  468. MOVE R1, 1 // R1 = 1
  469. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  470. LDL R1, R2, offs // R1 = MEM[R2 + 0]
  471. // R1 will be 123
  472. **LDH** – Load data from upper half-word of the 32-bit memory
  473. --------------------------------------------------------------
  474. **Syntax**
  475. **LDH** *Rdst, Rsrc, offset*
  476. **Operands**
  477. - *Rdst* – Register R[0..3], destination
  478. - *Rsrc* – Register R[0..3], holds address of destination, in 32-bit words
  479. - *Offset* – 13-bit signed value, offset in bytes
  480. **Cycles**
  481. 4 cycles to execute, 4 cycles to fetch next instruction
  482. **Description**
  483. The instruction loads the upper 16-bit half-word from memory with address [Rsrc + offset / 4] into the destination register Rdst::
  484. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  485. .. note::
  486. 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.
  487. **Examples**::
  488. 1: LDH R1, R2, 0x12 // R1 = MEM[R2 + 0x12 / 4]
  489. 2: .data // Data section definition
  490. Addr1: .word 0x12345678 // Define label Addr1 16 bit
  491. .set offs, 0x00 // Define constant offs
  492. .text // Text section definition
  493. MOVE R1, 1 // R1 = 1
  494. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  495. LDH R1, R2, offs // R1 = MEM[R2 + 0]
  496. // R1 will be 0x1234
  497. **JUMP** – Jump to an absolute address
  498. --------------------------------------
  499. **Syntax**
  500. **JUMP** *Rdst*
  501. **JUMP** *ImmAddr*
  502. **JUMP** *Rdst, Condition*
  503. **JUMP** *ImmAddr, Condition*
  504. **Operands**
  505. - *Rdst* – Register R[0..3] containing address to jump to (expressed in 32-bit words)
  506. - *ImmAddr* – 13 bits address (expressed in bytes), aligned to 4 bytes
  507. - *Condition*:
  508. - EQ – jump if last ALU operation result was zero
  509. - OV – jump if last ALU has set overflow flag
  510. **Cycles**
  511. 2 cycles to execute, 2 cycles to fetch next instruction
  512. **Description**
  513. The instruction makes jump to the specified address. Jump can be either unconditional or based on an ALU flag.
  514. **Examples**::
  515. 1: JUMP R1 // Jump to address in R1 (address in R1 is in 32-bit words)
  516. 2: JUMP 0x120, EQ // Jump to address 0x120 (in bytes) if ALU result is zero
  517. 3: JUMP label // Jump to label
  518. ...
  519. label: nop // Definition of label
  520. 4: .global label // Declaration of global label
  521. MOVE R1, label // R1 = label (value loaded into R1 is in words)
  522. JUMP R1 // Jump to label
  523. ...
  524. label: nop // Definition of label
  525. **JUMPR** – Jump to a relative offset (condition based on R0)
  526. -------------------------------------------------------------
  527. **Syntax**
  528. **JUMPR** *Step, Threshold, Condition*
  529. **Operands**
  530. - *Step* – relative shift from current position, in bytes
  531. - *Threshold* – threshold value for branch condition
  532. - *Condition*:
  533. - *EQ* (equal) – jump if value in R0 == threshold
  534. - *LT* (less than) – jump if value in R0 < threshold
  535. - *LE* (less or equal) – jump if value in R0 <= threshold
  536. - *GT* (greater than) – jump if value in R0 > threshold
  537. - *GE* (greater or equal) – jump if value in R0 >= threshold
  538. **Cycles**
  539. Conditions *EQ*, *GT* and *LT*: 2 cycles to execute, 2 cycles to fetch next instruction
  540. Conditions *LE* and *GE* are implemented in the assembler using two **JUMPR** instructions::
  541. // JUMPR target, threshold, LE is implemented as:
  542. JUMPR target, threshold, EQ
  543. JUMPR target, threshold, LT
  544. // JUMPR target, threshold, GE is implemented as:
  545. JUMPR target, threshold, EQ
  546. JUMPR target, threshold, GT
  547. Therefore the execution time will depend on the branches taken: either 2 cycles to execute + 2 cycles to fetch, or 4 cycles to execute + 4 cycles to fetch.
  548. **Description**
  549. 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.
  550. **Examples**::
  551. 1:pos: JUMPR 16, 20, GE // Jump to address (position + 16 bytes) if value in R0 >= 20
  552. 2: // Down counting loop using R0 register
  553. MOVE R0, 16 // load 16 into R0
  554. label: SUB R0, R0, 1 // R0--
  555. NOP // do something
  556. JUMPR label, 1, GE // jump to label if R0 >= 1
  557. **JUMPS** – Jump to a relative address (condition based on stage count)
  558. -----------------------------------------------------------------------
  559. **Syntax**
  560. **JUMPS** *Step, Threshold, Condition*
  561. **Operands**
  562. - *Step* – relative shift from current position, in bytes
  563. - *Threshold* – threshold value for branch condition
  564. - *Condition*:
  565. - *EQ* (equal) – jump if value in stage_cnt == threshold
  566. - *LT* (less than) – jump if value in stage_cnt < threshold
  567. - *LE* (less or equal) - jump if value in stage_cnt <= threshold
  568. - *GT* (greater than) – jump if value in stage_cnt > threshold
  569. - *GE* (greater or equal) — jump if value in stage_cnt >= threshold
  570. **Cycles**
  571. 2 cycles to execute, 2 cycles to fetch next instruction::
  572. // JUMPS target, threshold, EQ is implemented as:
  573. JUMPS next, threshold, LT
  574. JUMPS target, threshold, LE
  575. next:
  576. // JUMPS target, threshold, GT is implemented as:
  577. JUMPS next, threshold, LE
  578. JUMPS target, threshold, GE
  579. next:
  580. Therefore the execution time will depend on the branches taken: either 2 cycles to execute + 2 cycles to fetch, or 4 cycles to execute + 4 cycles to fetch.
  581. **Description**
  582. 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.
  583. **Examples**::
  584. 1:pos: JUMPS 16, 20, EQ // Jump to (position + 16 bytes) if stage_cnt == 20
  585. 2: // Up counting loop using stage count register
  586. STAGE_RST // set stage_cnt to 0
  587. label: STAGE_INC 1 // stage_cnt++
  588. NOP // do something
  589. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  590. **STAGE_RST** – Reset stage count register
  591. ------------------------------------------
  592. **Syntax**
  593. **STAGE_RST**
  594. **Operands**
  595. No operands
  596. **Description**
  597. The instruction sets the stage count register to 0
  598. **Cycles**
  599. 2 cycles to execute, 4 cycles to fetch next instruction
  600. **Examples**::
  601. 1: STAGE_RST // Reset stage count register
  602. **STAGE_INC** – Increment stage count register
  603. ----------------------------------------------
  604. **Syntax**
  605. **STAGE_INC** *Value*
  606. **Operands**
  607. - *Value* – 8 bits value
  608. **Cycles**
  609. 2 cycles to execute, 4 cycles to fetch next instruction
  610. **Description**
  611. The instruction increments the stage count register by the given value.
  612. **Examples**::
  613. 1: STAGE_INC 10 // stage_cnt += 10
  614. 2: // Up counting loop example:
  615. STAGE_RST // set stage_cnt to 0
  616. label: STAGE_INC 1 // stage_cnt++
  617. NOP // do something
  618. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  619. **STAGE_DEC** – Decrement stage count register
  620. ----------------------------------------------
  621. **Syntax**
  622. **STAGE_DEC** *Value*
  623. **Operands**
  624. - *Value* – 8 bits value
  625. **Cycles**
  626. 2 cycles to execute, 4 cycles to fetch next instruction
  627. **Description**
  628. The instruction decrements the stage count register by the given value.
  629. **Examples**::
  630. 1: STAGE_DEC 10 // stage_cnt -= 10;
  631. 2: // Down counting loop example
  632. STAGE_RST // set stage_cnt to 0
  633. STAGE_INC 16 // increment stage_cnt to 16
  634. label: STAGE_DEC 1 // stage_cnt--;
  635. NOP // do something
  636. JUMPS label, 0, GT // jump to label if stage_cnt > 0
  637. **HALT** – End the program
  638. --------------------------
  639. **Syntax**
  640. **HALT**
  641. **Operands**
  642. No operands
  643. **Cycles**
  644. 2 cycles to execute
  645. **Description**
  646. The instruction halts the ULP coprocessor and restarts the ULP wakeup timer, if it is enabled.
  647. **Examples**::
  648. 1: HALT // Halt the coprocessor
  649. **WAKE** – Wake up the chip
  650. ---------------------------
  651. **Syntax**
  652. **WAKE**
  653. **Operands**
  654. No operands
  655. **Cycles**
  656. 2 cycles to execute, 4 cycles to fetch next instruction
  657. **Description**
  658. The instruction sends an interrupt from the ULP coprocessor to the RTC controller.
  659. - If the SoC is in deep sleep mode, and ULP wakeup is enabled, this causes the SoC to wake up.
  660. - 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.
  661. Note that before using WAKE instruction, ULP program may needs 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).
  662. **Examples**::
  663. 1: is_rdy_for_wakeup: // Read RTC_CNTL_RDY_FOR_WAKEUP bit
  664. READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP)
  665. AND r0, r0, 1
  666. JUMP is_rdy_for_wakeup, eq // Retry until the bit is set
  667. WAKE // Trigger wake up
  668. REG_WR 0x006, 24, 24, 0 // Stop ULP timer (clear RTC_CNTL_ULP_CP_SLP_TIMER_EN)
  669. HALT // Stop the ULP program
  670. // After these instructions, SoC will wake up,
  671. // and ULP will not run again until started by the main program.
  672. .. only:: esp32
  673. **SLEEP** – set ULP wakeup timer period
  674. ---------------------------------------
  675. **Syntax**
  676. **SLEEP** *sleep_reg*
  677. **Operands**
  678. - *sleep_reg* – 0..4, selects one of ``SENS_ULP_CP_SLEEP_CYCx_REG`` registers.
  679. **Cycles**
  680. 2 cycles to execute, 4 cycles to fetch next instruction
  681. **Description**
  682. 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.
  683. **Examples**::
  684. 1: SLEEP 1 // Use period set in SENS_ULP_CP_SLEEP_CYC1_REG
  685. 2: .set sleep_reg, 4 // Set constant
  686. SLEEP sleep_reg // Use period set in SENS_ULP_CP_SLEEP_CYC4_REG
  687. **WAIT** – wait some number of cycles
  688. -------------------------------------
  689. **Syntax**
  690. **WAIT** *Cycles*
  691. **Operands**
  692. - *Cycles* – number of cycles for wait
  693. **Cycles**
  694. 2 + *Cycles* cycles to execute, 4 cycles to fetch next instruction
  695. **Description**
  696. The instruction delays for given number of cycles.
  697. **Examples**::
  698. 1: WAIT 10 // Do nothing for 10 cycles
  699. 2: .set wait_cnt, 10 // Set a constant
  700. WAIT wait_cnt // wait for 10 cycles
  701. .. only:: not esp32
  702. **TSENS** – do measurement with temperature sensor
  703. --------------------------------------------------
  704. **Syntax**
  705. - **TSENS** *Rdst, Wait_Delay*
  706. **Operands**
  707. - *Rdst* – Destination Register R[0..3], result will be stored to this register
  708. - *Wait_Delay* – number of cycles used to perform the measurement
  709. **Cycles**
  710. 2 + *Wait_Delay* + 3 * TSENS_CLK to execute, 4 cycles to fetch next instruction
  711. **Description**
  712. The instruction performs measurement using TSENS and stores the result into a general purpose register.
  713. **Examples**::
  714. 1: TSENS R1, 1000 // Measure temperature sensor for 1000 cycles,
  715. // and store result to R1
  716. **ADC** – do measurement with ADC
  717. ---------------------------------
  718. **Syntax**
  719. - **ADC** *Rdst, Sar_sel, Mux*
  720. - **ADC** *Rdst, Sar_sel, Mux, 0* — deprecated form
  721. **Operands**
  722. - *Rdst* – Destination Register R[0..3], result will be stored to this register
  723. - *Sar_sel* – Select ADC: 0 = SARADC1, 1 = SARADC2
  724. .. only:: esp32
  725. - *Mux* - Enable ADC channel. Channel number is [Mux-1]. If the user passes Mux value 1, then ADC channel 0 gets used.
  726. .. only:: esp32s2 or esp32s3
  727. - *Mux* - selected PAD, SARADC Pad[Mux-1] is enabled. If the user passes Mux value 1, then ADC pad 0 gets used.
  728. **Cycles**
  729. ``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
  730. **Description**
  731. The instruction makes measurements from ADC.
  732. **Examples**::
  733. .. only:: esp32
  734. 1: ADC R1, 0, 1 // Measure value using ADC1 channel 0 and store result into R1
  735. .. only:: esp32s2 or esp32s3
  736. 1: ADC R1, 0, 1 // Measure value using ADC1 pad 2 and store result into R1
  737. .. only:: esp32
  738. **I2C_RD** - read single byte from I2C slave
  739. ----------------------------------------------
  740. **Syntax**
  741. - **I2C_RD** *Sub_addr, High, Low, Slave_sel*
  742. **Operands**
  743. - *Sub_addr* – Address within the I2C slave to read.
  744. - *High*, *Low* — Define range of bits to read. Bits outside of [High, Low] range are masked.
  745. - *Slave_sel* - Index of I2C slave address to use.
  746. **Cycles**
  747. Execution time mostly depends on I2C communication time. 4 cycles to fetch next instruction.
  748. **Description**
  749. ``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``.
  750. 8 bits of read result is stored into `R0` register.
  751. **Examples**::
  752. 1: I2C_RD 0x10, 7, 0, 0 // Read byte from sub-address 0x10 of slave with address set in SENS_I2C_SLAVE_ADDR0
  753. **I2C_WR** - write single byte to I2C slave
  754. ----------------------------------------------
  755. **Syntax**
  756. - **I2C_WR** *Sub_addr, Value, High, Low, Slave_sel*
  757. **Operands**
  758. - *Sub_addr* – Address within the I2C slave to write.
  759. - *Value* – 8-bit value to be written.
  760. - *High*, *Low* — Define range of bits to write. Bits outside of [High, Low] range are masked.
  761. - *Slave_sel* - Index of I2C slave address to use.
  762. **Cycles**
  763. Execution time mostly depends on I2C communication time. 4 cycles to fetch next instruction.
  764. **Description**
  765. ``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``.
  766. **Examples**::
  767. 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.
  768. **REG_RD** – read from peripheral register
  769. ------------------------------------------
  770. **Syntax**
  771. **REG_RD** *Addr, High, Low*
  772. **Operands**
  773. - *Addr* – Register address, in 32-bit words
  774. - *High* – Register end bit number
  775. - *Low* – Register start bit number
  776. **Cycles**
  777. 4 cycles to execute, 4 cycles to fetch next instruction
  778. **Description**
  779. The instruction reads up to 16 bits from a peripheral register into a general purpose register: ``R0 = REG[Addr][High:Low]``.
  780. .. only:: esp32
  781. 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::
  782. addr_ulp = (addr_dport - DR_REG_RTCCNTL_BASE) / 4
  783. .. only:: esp32s2 or esp32s3
  784. 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::
  785. addr_ulp = (addr_peribus1 - DR_REG_RTCCNTL_BASE) / 4
  786. **Examples**::
  787. 1: REG_RD 0x120, 7, 4 // load 4 bits: R0 = {12'b0, REG[0x120][7:4]}
  788. **REG_WR** – write to peripheral register
  789. -----------------------------------------
  790. **Syntax**
  791. **REG_WR** *Addr, High, Low, Data*
  792. **Operands**
  793. - *Addr* – Register address, in 32-bit words.
  794. - *High* – Register end bit number
  795. - *Low* – Register start bit number
  796. - *Data* – Value to write, 8 bits
  797. **Cycles**
  798. 8 cycles to execute, 4 cycles to fetch next instruction
  799. **Description**
  800. The instruction writes up to 8 bits from an immediate data value into a peripheral register: ``REG[Addr][High:Low] = data``.
  801. .. only:: esp32
  802. 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::
  803. addr_ulp = (addr_dport - DR_REG_RTCCNTL_BASE) / 4
  804. .. only:: esp32s2 or esp32s3
  805. This instruction can access registers in RTC_CNTL, RTC_IO, SENS, and RTC_I2C peripherals. Address of the the register, as seen from the ULP, can be calculated from the address of the same register on the PeriBUS1 as follows::
  806. addr_ulp = (addr_peribus1 - DR_REG_RTCCNTL_BASE) / 4
  807. **Examples**::
  808. 1: REG_WR 0x120, 7, 0, 0x10 // set 8 bits: REG[0x120][7:0] = 0x10
  809. Convenience macros for peripheral registers access
  810. --------------------------------------------------
  811. ULP source files are passed through C preprocessor before the assembler. This allows certain macros to be used to facilitate access to peripheral registers.
  812. 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.
  813. 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``.
  814. READ_RTC_REG(rtc_reg, low_bit, bit_width)
  815. Read up to 16 bits from rtc_reg[low_bit + bit_width - 1 : low_bit] into R0. For example::
  816. #include "soc/soc_ulp.h"
  817. #include "soc/rtc_cntl_reg.h"
  818. /* Read 16 lower bits of RTC_CNTL_TIME0_REG into R0 */
  819. READ_RTC_REG(RTC_CNTL_TIME0_REG, 0, 16)
  820. READ_RTC_FIELD(rtc_reg, field)
  821. Read from a field in rtc_reg into R0, up to 16 bits. For example::
  822. #include "soc/soc_ulp.h"
  823. #include "soc/sens_reg.h"
  824. /* Read 8-bit SENS_TSENS_OUT field of SENS_SAR_SLAVE_ADDR3_REG into R0 */
  825. READ_RTC_FIELD(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT)
  826. WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value)
  827. Write immediate value into rtc_reg[low_bit + bit_width - 1 : low_bit], bit_width <= 8. For example::
  828. #include "soc/soc_ulp.h"
  829. #include "soc/rtc_io_reg.h"
  830. /* Set BIT(2) of RTC_GPIO_OUT_DATA_W1TS field in RTC_GPIO_OUT_W1TS_REG */
  831. WRITE_RTC_REG(RTC_GPIO_OUT_W1TS_REG, RTC_GPIO_OUT_DATA_W1TS_S + 2, 1, 1)
  832. WRITE_RTC_FIELD(rtc_reg, field, value)
  833. Write immediate value into a field in rtc_reg, up to 8 bits. For example::
  834. #include "soc/soc_ulp.h"
  835. #include "soc/rtc_cntl_reg.h"
  836. /* Set RTC_CNTL_ULP_CP_SLP_TIMER_EN field of RTC_CNTL_STATE0_REG to 0 */
  837. WRITE_RTC_FIELD(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN, 0)