ulp_instruction_set.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. ULP coprocessor instruction set
  2. ===============================
  3. This document provides details about the instructions used by ESP32 ULP coprocessor assembler.
  4. ULP 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 regiter 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. Note about addressing
  9. ---------------------
  10. ESP32 ULP coprocessor's JUMP, ST, LD instructions which take register as an argument (jump address, store/load base address) expect the argument to be expressed in 32-bit words.
  11. Consider the following example program::
  12. entry:
  13. NOP
  14. NOP
  15. NOP
  16. NOP
  17. loop:
  18. MOVE R1, loop
  19. JUMP R1
  20. 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 to be expressed in 32-bit words. To account for this common use case, assembler will convert the address of label `loop` from bytes to words, when generating ``MOVE`` instruction, so the code generated code will be equivalent to::
  21. 0000 NOP
  22. 0004 NOP
  23. 0008 NOP
  24. 000c NOP
  25. 0010 MOVE R1, 4
  26. 0014 JUMP R1
  27. 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::
  28. .set val, 0x10
  29. MOVE R1, val
  30. In this case, value loaded into R1 will be ``0x10``.
  31. Similar considerations apply to ``LD`` and ``ST`` instructions. Consider the following code::
  32. .global array
  33. array: .long 0
  34. .long 0
  35. .long 0
  36. .long 0
  37. MOVE R1, array
  38. MOVE R2, 0x1234
  39. ST R2, R1, 0 // write value of R2 into the first array element,
  40. // i.e. array[0]
  41. ST R2, R1, 4 // write value of R2 into the second array element
  42. // (4 byte offset), i.e. array[1]
  43. ADD R1, R1, 2 // this increments address by 2 words (8 bytes)
  44. ST R2, R1, 0 // write value of R2 into the third array element,
  45. // i.e. array[2]
  46. **NOP** - no operation
  47. ----------------------
  48. **Syntax:**
  49. **NOP**
  50. **Operands:**
  51. None
  52. **Description:**
  53. No operation is performed. Only the PC is incremented.
  54. **Example**::
  55. 1: NOP
  56. **ADD** - Add to register
  57. -------------------------
  58. **Syntax:**
  59. **ADD** *Rdst, Rsrc1, Rsrc2*
  60. **ADD** *Rdst, Rsrc1, imm*
  61. **Operands:**
  62. - *Rdst* - Register R[0..3]
  63. - *Rsrc1* - Register R[0..3]
  64. - *Rsrc2* - Register R[0..3]
  65. - *Imm* - 16-bit signed value
  66. **Description:**
  67. The instruction adds source register to another source register or to a 16-bit signed value and stores result to the destination register.
  68. **Examples**::
  69. 1: ADD R1, R2, R3 //R1 = R2 + R3
  70. 2: Add R1, R2, 0x1234 //R1 = R2 + 0x1234
  71. 3: .set value1, 0x03 //constant value1=0x03
  72. Add R1, R2, value1 //R1 = R2 + value1
  73. 4: .global label //declaration of variable label
  74. Add R1, R2, label //R1 = R2 + label
  75. ...
  76. label: nop //definition of variable label
  77. **SUB** - Subtract from register
  78. --------------------------------
  79. **Syntax:**
  80. **SUB** *Rdst, Rsrc1, Rsrc2*
  81. **SUB** *Rdst, Rsrc1, imm*
  82. **Operands:**
  83. - *Rdst* - Register R[0..3]
  84. - *Rsrc1* - Register R[0..3]
  85. - *Rsrc2* - Register R[0..3]
  86. - *Imm* - 16-bit signed value
  87. **Description:**
  88. The instruction subtracts the source register from another source register or subtracts 16-bit signed value from a source register, and stores result to the destination register.
  89. **Examples:**::
  90. 1: SUB R1, R2, R3 //R1 = R2 - R3
  91. 2: sub R1, R2, 0x1234 //R1 = R2 - 0x1234
  92. 3: .set value1, 0x03 //constant value1=0x03
  93. SUB R1, R2, value1 //R1 = R2 - value1
  94. 4: .global label //declaration of variable label
  95. SUB R1, R2, label //R1 = R2 - label
  96. ....
  97. label: nop //definition of variable label
  98. **AND** - Logical AND of two operands
  99. -------------------------------------
  100. **Syntax:**
  101. **AND** *Rdst, Rsrc1, Rsrc2*
  102. **AND** *Rdst, Rsrc1, imm*
  103. **Operands:**
  104. - *Rdst* - Register R[0..3]
  105. - *Rsrc1* - Register R[0..3]
  106. - *Rsrc2* - Register R[0..3]
  107. - *Imm* - 16-bit signed value
  108. **Description:**
  109. The instruction does logical AND of a source register and another source register or 16-bit signed value and stores result to the destination register.
  110. **Example**::
  111. 1: AND R1, R2, R3 //R1 = R2 & R3
  112. 2: AND R1, R2, 0x1234 //R1 = R2 & 0x1234
  113. 3: .set value1, 0x03 //constant value1=0x03
  114. AND R1, R2, value1 //R1 = R2 & value1
  115. 4: .global label //declaration of variable label
  116. AND R1, R2, label //R1 = R2 & label
  117. ...
  118. label: nop //definition of variable label
  119. **OR** - Logical OR of two operands
  120. -----------------------------------
  121. **Syntax**
  122. **OR** *Rdst, Rsrc1, Rsrc2*
  123. **OR** *Rdst, Rsrc1, imm*
  124. **Operands**
  125. - *Rdst* - Register R[0..3]
  126. - *Rsrc1* - Register R[0..3]
  127. - *Rsrc2* - Register R[0..3]
  128. - *Imm* - 16-bit signed value
  129. **Description**
  130. The instruction does logical OR of a source register and another source register or 16-bit signed value and stores result to the destination register.
  131. **Examples**::
  132. 1: OR R1, R2, R3 //R1 = R2 \| R3
  133. 2: OR R1, R2, 0x1234 //R1 = R2 \| 0x1234
  134. 3: .set value1, 0x03 //constant value1=0x03
  135. OR R1, R2, value1 //R1 = R2 \| value1
  136. 4: .global label //declaration of variable label
  137. OR R1, R2, label //R1 = R2 \|label
  138. ...
  139. label: nop //definition of variable label
  140. **LSH** - Logical Shift Left
  141. ----------------------------
  142. **Syntax**
  143. **LSH** *Rdst, Rsrc1, Rsrc2*
  144. **LSH** *Rdst, Rsrc1, imm*
  145. **Operands**
  146. - *Rdst* - Register R[0..3]
  147. - *Rsrc1* - Register R[0..3]
  148. - *Rsrc2* - Register R[0..3]
  149. - *Imm* - 16-bit signed value
  150. **Description**
  151. The instruction does logical shift to left of source register to number of bits from another source register or 16-bit signed value and store result to the destination register.
  152. **Examples**::
  153. 1: LSH R1, R2, R3 //R1 = R2 << R3
  154. 2: LSH R1, R2, 0x03 //R1 = R2 << 0x03
  155. 3: .set value1, 0x03 //constant value1=0x03
  156. LSH R1, R2, value1 //R1 = R2 << value1
  157. 4: .global label //declaration of variable label
  158. LSH R1, R2, label //R1 = R2 << label
  159. ...
  160. label: nop //definition of variable label
  161. **RSH** - Logical Shift Right
  162. -----------------------------
  163. **Syntax**
  164. **RSH** *Rdst, Rsrc1, Rsrc2*
  165. **RSH** *Rdst, Rsrc1, imm*
  166. **Operands**
  167. *Rdst* - Register R[0..3]
  168. *Rsrc1* - Register R[0..3]
  169. *Rsrc2* - Register R[0..3]
  170. *Imm* - 16-bit signed value
  171. **Description**
  172. The instruction does logical shift to right of source register to number of bits from another source register or 16-bit signed value and store result to the destination register.
  173. **Examples**::
  174. 1: RSH R1, R2, R3 //R1 = R2 >> R3
  175. 2: RSH R1, R2, 0x03 //R1 = R2 >> 0x03
  176. 3: .set value1, 0x03 //constant value1=0x03
  177. RSH R1, R2, value1 //R1 = R2 >> value1
  178. 4: .global label //declaration of variable label
  179. RSH R1, R2, label //R1 = R2 >> label
  180. label: nop //definition of variable label
  181. **MOVE** – Move to register
  182. ---------------------------
  183. **Syntax**
  184. **MOVE** *Rdst, Rsrc*
  185. **MOVE** *Rdst, imm*
  186. **Operands**
  187. - *Rdst* – Register R[0..3]
  188. - *Rsrc* – Register R[0..3]
  189. - *Imm* – 16-bit signed value
  190. **Description**
  191. The instruction move to destination register value from source register or 16-bit signed value.
  192. 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. To avoid using an extra instruction
  193. **Examples**::
  194. 1: MOVE R1, R2 //R1 = R2 >> R3
  195. 2: MOVE R1, 0x03 //R1 = R2 >> 0x03
  196. 3: .set value1, 0x03 //constant value1=0x03
  197. MOVE R1, value1 //R1 = value1
  198. 4: .global label //declaration of label
  199. MOVE R1, label //R1 = address_of(label) / 4
  200. ...
  201. label: nop //definition of label
  202. **ST** – Store data to the memory
  203. ---------------------------------
  204. **Syntax**
  205. **ST** *Rsrc, Rdst, offset*
  206. **Operands**
  207. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  208. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  209. - *Offset* – 10-bit signed value, offset in bytes
  210. **Description**
  211. 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::
  212. Mem[Rdst + offset / 4]{31:0} = {PC[10:0], 5'b0, Rsrc[15:0]}
  213. The application can use higher 16 bits to determine which instruction in the ULP program has written any particular word into memory.
  214. **Examples**::
  215. 1: ST R1, R2, 0x12 //MEM[R2+0x12] = R1
  216. 2: .data //Data section definition
  217. Addr1: .word 123 // Define label Addr1 16 bit
  218. .set offs, 0x00 // Define constant offs
  219. .text //Text section definition
  220. MOVE R1, 1 // R1 = 1
  221. MOVE R2, Addr1 // R2 = Addr1
  222. ST R1, R2, offs // MEM[R2 + 0] = R1
  223. // MEM[Addr1 + 0] will be 32'h600001
  224. **LD** – Load data from the memory
  225. ----------------------------------
  226. **Syntax**
  227. **LD** *Rdst, Rsrc, offset*
  228. **Operands**
  229. *Rdst* – Register R[0..3], destination
  230. *Rsrc* – Register R[0..3], holds address of destination, in 32-bit words
  231. *Offset* – 10-bit signed value, offset in bytes
  232. **Description**
  233. The instruction loads lower 16-bit half-word from memory with address Rsrc+offset into the destination register Rdst::
  234. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  235. **Examples**::
  236. 1: LD R1, R2, 0x12 //R1 = MEM[R2+0x12]
  237. 2: .data //Data section definition
  238. Addr1: .word 123 // Define label Addr1 16 bit
  239. .set offs, 0x00 // Define constant offs
  240. .text //Text section definition
  241. MOVE R1, 1 // R1 = 1
  242. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  243. LD R1, R2, offs // R1 = MEM[R2 + 0]
  244. // R1 will be 123
  245. **JUMP** – Jump to an absolute address
  246. --------------------------------------
  247. **Syntax**
  248. **JUMP** *Rdst*
  249. **JUMP** *ImmAddr*
  250. **JUMP** *Rdst, Condition*
  251. **JUMP** *ImmAddr, Condition*
  252. **Operands**
  253. - *Rdst* – Register R[0..3] containing address to jump to (expressed in 32-bit words)
  254. - *ImmAddr* – 13 bits address (expressed in bytes), aligned to 4 bytes
  255. - *Condition*:
  256. - EQ – jump if last ALU operation result was zero
  257. - OV – jump if last ALU has set overflow flag
  258. **Description**
  259. The instruction makes jump to the specified address. Jump can be either unconditional or based on an ALU flag.
  260. **Examples**::
  261. 1: JUMP R1 // Jump to address in R1 (address in R1 is in 32-bit words)
  262. 2: JUMP 0x120, EQ // Jump to address 0x120 (in bytes) if ALU result is zero
  263. 3: JUMP label // Jump to label
  264. ...
  265. label: nop // Definition of label
  266. 4: .global label // Declaration of global label
  267. MOVE R1, label // R1 = label (value loaded into R1 is in words)
  268. JUMP R1 // Jump to label
  269. ...
  270. label: nop // Definition of label
  271. **JUMPR** – Jump to a relative offset (condition based on R0)
  272. -------------------------------------------------------------
  273. **Syntax**
  274. **JUMPR** *Step, Threshold, Condition*
  275. **Operands**
  276. - *Step* – relative shift from current position, in bytes
  277. - *Threshold* – threshold value for branch condition
  278. - *Condition*:
  279. - *GE* (greater or equal) – jump if value in R0 >= threshold
  280. - *LT* (less than) – jump if value in R0 < threshold
  281. **Description**
  282. 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.
  283. **Examples**::
  284. 1:pos: JUMPR 16, 20, GE // Jump to address (position + 16 bytes) if value in R0 >= 20
  285. 2: // Down counting loop using R0 register
  286. MOVE R0, 16 // load 16 into R0
  287. label: SUB R0, R0, 1 // R0--
  288. NOP // do something
  289. JUMPR label, 1, GE // jump to label if R0 >= 1
  290. **JUMPS** – Jump to a relative address (condition based on stage count)
  291. -----------------------------------------------------------------------
  292. **Syntax**
  293. **JUMPS** *Step, Threshold, Condition*
  294. **Operands**
  295. - *Step* – relative shift from current position, in bytes
  296. - *Threshold* – threshold value for branch condition
  297. - *Condition*:
  298. - *EQ* (equal) – jump if value in stage_cnt == threshold
  299. - *LT* (less than) – jump if value in stage_cnt < threshold
  300. - *GT* (greater than) – jump if value in stage_cnt > threshold
  301. **Description**
  302. 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.
  303. **Examples**::
  304. 1:pos: JUMPS 16, 20, EQ // Jump to (position + 16 bytes) if stage_cnt == 20
  305. 2: // Up counting loop using stage count register
  306. STAGE_RST // set stage_cnt to 0
  307. label: STAGE_INC 1 // stage_cnt++
  308. NOP // do something
  309. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  310. **STAGE_RST** – Reset stage count register
  311. ------------------------------------------
  312. **Syntax**
  313. **STAGE_RST**
  314. **Operands**
  315. No operands
  316. **Description**
  317. The instruction sets the stage count register to 0
  318. **Examples**::
  319. 1: STAGE_RST // Reset stage count register
  320. **STAGE_INC** – Increment stage count register
  321. ----------------------------------------------
  322. **Syntax**
  323. **STAGE_INC** *Value*
  324. **Operands**
  325. - *Value* – 8 bits value
  326. **Description**
  327. The instruction increments stage count register by given value.
  328. **Examples**::
  329. 1: STAGE_INC 10 // stage_cnt += 10
  330. 2: // Up counting loop example:
  331. STAGE_RST // set stage_cnt to 0
  332. label: STAGE_INC 1 // stage_cnt++
  333. NOP // do something
  334. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  335. **STAGE_DEC** – Decrement stage count register
  336. ----------------------------------------------
  337. **Syntax**
  338. **STAGE_DEC** *Value*
  339. **Operands**
  340. - *Value* – 8 bits value
  341. **Description**
  342. The instruction decrements stage count register by given value.
  343. **Examples**::
  344. 1: STAGE_DEC 10 // stage_cnt -= 10;
  345. 2: // Down counting loop exaple
  346. STAGE_RST // set stage_cnt to 0
  347. STAGE_INC 16 // increment stage_cnt to 16
  348. label: STAGE_DEC 1 // stage_cnt--;
  349. NOP // do something
  350. JUMPS label, 0, GT // jump to label if stage_cnt > 0
  351. **HALT** – End the program
  352. --------------------------
  353. **Syntax**
  354. **HALT**
  355. **Operands**
  356. No operands
  357. **Description**
  358. The instruction halt the processor to the power down mode
  359. **Examples**::
  360. 1: HALT // Move chip to powerdown
  361. **WAKE** – wakeup the chip
  362. --------------------------
  363. **Syntax**
  364. **WAKE**
  365. **Operands**
  366. No operands
  367. **Description**
  368. The instruction sends an interrupt from ULP to RTC controller.
  369. - If the SoC is in deep sleep mode, and ULP wakeup is enabled, this causes the SoC to wake up.
  370. - 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.
  371. **Examples**::
  372. 1: WAKE // Trigger wake up
  373. REG_WR 0x006, 24, 24, 0 // Stop ULP timer (clear RTC_CNTL_ULP_CP_SLP_TIMER_EN)
  374. HALT // Stop the ULP program
  375. // After these instructions, SoC will wake up,
  376. // and ULP will not run again until started by the main program.
  377. **SLEEP** – set ULP wakeup timer period
  378. ---------------------------------------
  379. **Syntax**
  380. **SLEEP** *sleep_reg*
  381. **Operands**
  382. - *sleep_reg* – 0..4, selects one of ``SENS_ULP_CP_SLEEP_CYCx_REG`` registers.
  383. **Description**
  384. 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.
  385. **Examples**::
  386. 1: SLEEP 1 // Use period set in SENS_ULP_CP_SLEEP_CYC1_REG
  387. 2: .set sleep_reg, 4 // Set constant
  388. SLEEP sleep_reg // Use period set in SENS_ULP_CP_SLEEP_CYC4_REG
  389. **WAIT** – wait some number of cycles
  390. -------------------------------------
  391. **Syntax**
  392. **WAIT** *Cycles*
  393. **Operands**
  394. - *Cycles* – number of cycles for wait
  395. **Description**
  396. The instruction delays for given number of cycles.
  397. **Examples**::
  398. 1: WAIT 10 // Do nothing for 10 cycles
  399. 2: .set wait_cnt, 10 // Set a constant
  400. WAIT wait_cnt // wait for 10 cycles
  401. **TSENS** – do measurement with temperature sensor
  402. --------------------------------------------------
  403. **Syntax**
  404. - **TSENS** *Rdst, Wait_Delay*
  405. **Operands**
  406. - *Rdst* – Destination Register R[0..3], result will be stored to this register
  407. - *Wait_Delay* – number of cycles used to perform the measurement
  408. **Description**
  409. The instruction performs measurement using TSENS and stores the result into a general purpose register.
  410. **Examples**::
  411. 1: TSENS R1, 1000 // Measure temperature sensor for 1000 cycles,
  412. // and store result to R1
  413. **ADC** – do measurement with ADC
  414. ---------------------------------
  415. **Syntax**
  416. - **ADC** *Rdst, Sar_sel, Mux*
  417. - **ADC** *Rdst, Sar_sel, Mux, 0* — deprecated form
  418. **Operands**
  419. - *Rdst* – Destination Register R[0..3], result will be stored to this register
  420. - *Sar_sel* – Select ADC: 0 = SARADC1, 1 = SARADC2
  421. - *Mux* - selected PAD, SARADC Pad[Mux+1] is enabled
  422. **Description**
  423. The instruction makes measurements from ADC.
  424. **Examples**::
  425. 1: ADC R1, 0, 1 // Measure value using ADC1 pad 2 and store result into R1
  426. **REG_RD** – read from peripheral register
  427. ------------------------------------------
  428. **Syntax**
  429. **REG_RD** *Addr, High, Low*
  430. **Operands**
  431. - *Addr* – register address, in 32-bit words
  432. - *High* – High part of R0
  433. - *Low* – Low part of R0
  434. **Description**
  435. The instruction reads up to 16 bits from a peripheral register into a general purpose register: ``R0 = REG[Addr][High:Low]``.
  436. This instruction can access registers in RTC_CNTL, RTC_IO, and SENS peripherals. Address of the the register, as seen from the ULP,
  437. can be calculated from the address of the same register on the DPORT bus as follows::
  438. addr_ulp = (addr_dport - DR_REG_RTCCNTL_BASE) / 4
  439. **Examples**::
  440. 1: REG_RD 0x120, 2, 0 // load 4 bits: R0 = {12'b0, REG[0x120][7:4]}
  441. **REG_WR** – write to peripheral register
  442. -----------------------------------------
  443. **Syntax**
  444. **REG_WR** *Addr, High, Low, Data*
  445. **Operands**
  446. - *Addr* – register address, in 32-bit words.
  447. - *High* – High part of R0
  448. - *Low* – Low part of R0
  449. - *Data* – value to write, 8 bits
  450. **Description**
  451. The instruction writes up to 8 bits from a general purpose register into a peripheral register. ``REG[Addr][High:Low] = data``
  452. This instruction can access registers in RTC_CNTL, RTC_IO, and SENS peripherals. Address of the the register, as seen from the ULP,
  453. can be calculated from the address of the same register on the DPORT bus as follows::
  454. addr_ulp = (addr_dport - DR_REG_RTCCNTL_BASE) / 4
  455. **Examples**::
  456. 1: REG_WR 0x120, 7, 0, 0x10 // set 8 bits: REG[0x120][7:0] = 0x10
  457. Convenience macros for peripheral registers access
  458. --------------------------------------------------
  459. ULP source files are passed through C preprocessor before the assembler. This allows certain macros to be used to facilitate access to peripheral registers.
  460. 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.
  461. 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``, and ``soc/sens_reg.h``.
  462. READ_RTC_REG(rtc_reg, low_bit, bit_width)
  463. Read up to 16 bits from rtc_reg[low_bit + bit_width - 1 : low_bit] into R0. For example::
  464. #include "soc/soc_ulp.h"
  465. #include "soc/rtc_cntl_reg.h"
  466. /* Read 16 lower bits of RTC_CNTL_TIME0_REG into R0 */
  467. READ_RTC_REG(RTC_CNTL_TIME0_REG, 0, 16)
  468. READ_RTC_FIELD(rtc_reg, field)
  469. Read from a field in rtc_reg into R0, up to 16 bits. For example::
  470. #include "soc/soc_ulp.h"
  471. #include "soc/sens_reg.h"
  472. /* Read 8-bit SENS_TSENS_OUT field of SENS_SAR_SLAVE_ADDR3_REG into R0 */
  473. READ_RTC_FIELD(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT)
  474. WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value)
  475. Write immediate value into rtc_reg[low_bit + bit_width - 1 : low_bit], bit_width <= 8. For example::
  476. #include "soc/soc_ulp.h"
  477. #include "soc/rtc_io_reg.h"
  478. /* Set BIT(2) of RTC_GPIO_OUT_DATA_W1TS field in RTC_GPIO_OUT_W1TS_REG */
  479. WRITE_RTC_REG(RTC_GPIO_OUT_W1TS_REG, RTC_GPIO_OUT_DATA_W1TS_S + 2, 1, 1)
  480. WRITE_RTC_FIELD(rtc_reg, field, value)
  481. Write immediate value into a field in rtc_reg, up to 8 bits. For example::
  482. #include "soc/soc_ulp.h"
  483. #include "soc/rtc_cntl_reg.h"
  484. /* Set RTC_CNTL_ULP_CP_SLP_TIMER_EN field of RTC_CNTL_STATE0_REG to 0 */
  485. WRITE_RTC_FIELD(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN, 0)