ulp_instruction_set.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. Note about instruction execution time
  47. -------------------------------------
  48. 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::
  49. #include "soc/rtc.h"
  50. // calibrate 8M/256 clock against XTAL, get 8M/256 clock period
  51. uint32_t rtc_8md256_period = rtc_clk_cal(RTC_CAL_8MD256, 100);
  52. uint32_t rtc_fast_freq_hz = 1000000ULL * (1 << RTC_CLK_CAL_FRACT) * 256 / rtc_8md256_period;
  53. ULP coprocessor needs 2 clock cycle to fetch each instuction (fetching is not pipelined), plus certain number of cycles to execute, depending on the instruction. See description of each instruction for details on the execution time.
  54. 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.
  55. **NOP** - no operation
  56. ----------------------
  57. **Syntax**
  58. **NOP**
  59. **Operands**
  60. None
  61. **Cycles**
  62. 2 (fetch) + 1 (execute)
  63. **Description**
  64. No operation is performed. Only the PC is incremented.
  65. **Example**::
  66. 1: NOP
  67. **ADD** - Add to register
  68. -------------------------
  69. **Syntax**
  70. **ADD** *Rdst, Rsrc1, Rsrc2*
  71. **ADD** *Rdst, Rsrc1, imm*
  72. **Operands**
  73. - *Rdst* - Register R[0..3]
  74. - *Rsrc1* - Register R[0..3]
  75. - *Rsrc2* - Register R[0..3]
  76. - *Imm* - 16-bit signed value
  77. **Cycles**
  78. 2 (fetch) + 2 (execute)
  79. **Description**
  80. The instruction adds source register to another source register or to a 16-bit signed value and stores result to the destination register.
  81. **Examples**::
  82. 1: ADD R1, R2, R3 //R1 = R2 + R3
  83. 2: Add R1, R2, 0x1234 //R1 = R2 + 0x1234
  84. 3: .set value1, 0x03 //constant value1=0x03
  85. Add R1, R2, value1 //R1 = R2 + value1
  86. 4: .global label //declaration of variable label
  87. Add R1, R2, label //R1 = R2 + label
  88. ...
  89. label: nop //definition of variable label
  90. **SUB** - Subtract from register
  91. --------------------------------
  92. **Syntax**
  93. **SUB** *Rdst, Rsrc1, Rsrc2*
  94. **SUB** *Rdst, Rsrc1, imm*
  95. **Operands**
  96. - *Rdst* - Register R[0..3]
  97. - *Rsrc1* - Register R[0..3]
  98. - *Rsrc2* - Register R[0..3]
  99. - *Imm* - 16-bit signed value
  100. **Cycles**
  101. 2 (fetch) + 2 (execute)
  102. **Description**
  103. 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.
  104. **Examples**::
  105. 1: SUB R1, R2, R3 //R1 = R2 - R3
  106. 2: sub R1, R2, 0x1234 //R1 = R2 - 0x1234
  107. 3: .set value1, 0x03 //constant value1=0x03
  108. SUB R1, R2, value1 //R1 = R2 - value1
  109. 4: .global label //declaration of variable label
  110. SUB R1, R2, label //R1 = R2 - label
  111. ....
  112. label: nop //definition of variable label
  113. **AND** - Logical AND of two operands
  114. -------------------------------------
  115. **Syntax**
  116. **AND** *Rdst, Rsrc1, Rsrc2*
  117. **AND** *Rdst, Rsrc1, imm*
  118. **Operands**
  119. - *Rdst* - Register R[0..3]
  120. - *Rsrc1* - Register R[0..3]
  121. - *Rsrc2* - Register R[0..3]
  122. - *Imm* - 16-bit signed value
  123. **Cycles**
  124. 2 (fetch) + 2 (execute)
  125. **Description**
  126. 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.
  127. **Examples**::
  128. 1: AND R1, R2, R3 //R1 = R2 & R3
  129. 2: AND R1, R2, 0x1234 //R1 = R2 & 0x1234
  130. 3: .set value1, 0x03 //constant value1=0x03
  131. AND R1, R2, value1 //R1 = R2 & value1
  132. 4: .global label //declaration of variable label
  133. AND R1, R2, label //R1 = R2 & label
  134. ...
  135. label: nop //definition of variable label
  136. **OR** - Logical OR of two operands
  137. -----------------------------------
  138. **Syntax**
  139. **OR** *Rdst, Rsrc1, Rsrc2*
  140. **OR** *Rdst, Rsrc1, imm*
  141. **Operands**
  142. - *Rdst* - Register R[0..3]
  143. - *Rsrc1* - Register R[0..3]
  144. - *Rsrc2* - Register R[0..3]
  145. - *Imm* - 16-bit signed value
  146. **Cycles**
  147. 2 (fetch) + 2 (execute)
  148. **Description**
  149. 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.
  150. **Examples**::
  151. 1: OR R1, R2, R3 //R1 = R2 \| R3
  152. 2: OR R1, R2, 0x1234 //R1 = R2 \| 0x1234
  153. 3: .set value1, 0x03 //constant value1=0x03
  154. OR R1, R2, value1 //R1 = R2 \| value1
  155. 4: .global label //declaration of variable label
  156. OR R1, R2, label //R1 = R2 \|label
  157. ...
  158. label: nop //definition of variable label
  159. **LSH** - Logical Shift Left
  160. ----------------------------
  161. **Syntax**
  162. **LSH** *Rdst, Rsrc1, Rsrc2*
  163. **LSH** *Rdst, Rsrc1, imm*
  164. **Operands**
  165. - *Rdst* - Register R[0..3]
  166. - *Rsrc1* - Register R[0..3]
  167. - *Rsrc2* - Register R[0..3]
  168. - *Imm* - 16-bit signed value
  169. **Cycles**
  170. 2 (fetch) + 2 (execute)
  171. **Description**
  172. 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.
  173. **Examples**::
  174. 1: LSH R1, R2, R3 //R1 = R2 << R3
  175. 2: LSH R1, R2, 0x03 //R1 = R2 << 0x03
  176. 3: .set value1, 0x03 //constant value1=0x03
  177. LSH R1, R2, value1 //R1 = R2 << value1
  178. 4: .global label //declaration of variable label
  179. LSH R1, R2, label //R1 = R2 << label
  180. ...
  181. label: nop //definition of variable label
  182. **RSH** - Logical Shift Right
  183. -----------------------------
  184. **Syntax**
  185. **RSH** *Rdst, Rsrc1, Rsrc2*
  186. **RSH** *Rdst, Rsrc1, imm*
  187. **Operands**
  188. *Rdst* - Register R[0..3]
  189. *Rsrc1* - Register R[0..3]
  190. *Rsrc2* - Register R[0..3]
  191. *Imm* - 16-bit signed value
  192. **Cycles**
  193. 2 (fetch) + 2 (execute)
  194. **Description**
  195. 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.
  196. **Examples**::
  197. 1: RSH R1, R2, R3 //R1 = R2 >> R3
  198. 2: RSH R1, R2, 0x03 //R1 = R2 >> 0x03
  199. 3: .set value1, 0x03 //constant value1=0x03
  200. RSH R1, R2, value1 //R1 = R2 >> value1
  201. 4: .global label //declaration of variable label
  202. RSH R1, R2, label //R1 = R2 >> label
  203. label: nop //definition of variable label
  204. **MOVE** – Move to register
  205. ---------------------------
  206. **Syntax**
  207. **MOVE** *Rdst, Rsrc*
  208. **MOVE** *Rdst, imm*
  209. **Operands**
  210. - *Rdst* – Register R[0..3]
  211. - *Rsrc* – Register R[0..3]
  212. - *Imm* – 16-bit signed value
  213. **Cycles**
  214. 2 (fetch) + 2 (execute)
  215. **Description**
  216. The instruction move to destination register value from source register or 16-bit signed value.
  217. 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
  218. **Examples**::
  219. 1: MOVE R1, R2 //R1 = R2 >> R3
  220. 2: MOVE R1, 0x03 //R1 = R2 >> 0x03
  221. 3: .set value1, 0x03 //constant value1=0x03
  222. MOVE R1, value1 //R1 = value1
  223. 4: .global label //declaration of label
  224. MOVE R1, label //R1 = address_of(label) / 4
  225. ...
  226. label: nop //definition of label
  227. **ST** – Store data to the memory
  228. ---------------------------------
  229. **Syntax**
  230. **ST** *Rsrc, Rdst, offset*
  231. **Operands**
  232. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  233. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  234. - *Offset* – 10-bit signed value, offset in bytes
  235. **Cycles**
  236. 2 (fetch) + 4 (execute)
  237. **Description**
  238. 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::
  239. Mem[Rdst + offset / 4]{31:0} = {PC[10:0], 5'b0, Rsrc[15:0]}
  240. The application can use higher 16 bits to determine which instruction in the ULP program has written any particular word into memory.
  241. **Examples**::
  242. 1: ST R1, R2, 0x12 //MEM[R2+0x12] = R1
  243. 2: .data //Data section definition
  244. Addr1: .word 123 // Define label Addr1 16 bit
  245. .set offs, 0x00 // Define constant offs
  246. .text //Text section definition
  247. MOVE R1, 1 // R1 = 1
  248. MOVE R2, Addr1 // R2 = Addr1
  249. ST R1, R2, offs // MEM[R2 + 0] = R1
  250. // MEM[Addr1 + 0] will be 32'h600001
  251. **LD** – Load data from the memory
  252. ----------------------------------
  253. **Syntax**
  254. **LD** *Rdst, Rsrc, offset*
  255. **Operands**
  256. *Rdst* – Register R[0..3], destination
  257. *Rsrc* – Register R[0..3], holds address of destination, in 32-bit words
  258. *Offset* – 10-bit signed value, offset in bytes
  259. **Cycles**
  260. 2 (fetch) + 4 (execute)
  261. **Description**
  262. The instruction loads lower 16-bit half-word from memory with address Rsrc+offset into the destination register Rdst::
  263. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  264. **Examples**::
  265. 1: LD R1, R2, 0x12 //R1 = MEM[R2+0x12]
  266. 2: .data //Data section definition
  267. Addr1: .word 123 // Define label Addr1 16 bit
  268. .set offs, 0x00 // Define constant offs
  269. .text //Text section definition
  270. MOVE R1, 1 // R1 = 1
  271. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  272. LD R1, R2, offs // R1 = MEM[R2 + 0]
  273. // R1 will be 123
  274. **JUMP** – Jump to an absolute address
  275. --------------------------------------
  276. **Syntax**
  277. **JUMP** *Rdst*
  278. **JUMP** *ImmAddr*
  279. **JUMP** *Rdst, Condition*
  280. **JUMP** *ImmAddr, Condition*
  281. **Operands**
  282. - *Rdst* – Register R[0..3] containing address to jump to (expressed in 32-bit words)
  283. - *ImmAddr* – 13 bits address (expressed in bytes), aligned to 4 bytes
  284. - *Condition*:
  285. - EQ – jump if last ALU operation result was zero
  286. - OV – jump if last ALU has set overflow flag
  287. **Cycles**
  288. 2 (fetch) + 2 (execute)
  289. **Description**
  290. The instruction makes jump to the specified address. Jump can be either unconditional or based on an ALU flag.
  291. **Examples**::
  292. 1: JUMP R1 // Jump to address in R1 (address in R1 is in 32-bit words)
  293. 2: JUMP 0x120, EQ // Jump to address 0x120 (in bytes) if ALU result is zero
  294. 3: JUMP label // Jump to label
  295. ...
  296. label: nop // Definition of label
  297. 4: .global label // Declaration of global label
  298. MOVE R1, label // R1 = label (value loaded into R1 is in words)
  299. JUMP R1 // Jump to label
  300. ...
  301. label: nop // Definition of label
  302. **JUMPR** – Jump to a relative offset (condition based on R0)
  303. -------------------------------------------------------------
  304. **Syntax**
  305. **JUMPR** *Step, Threshold, Condition*
  306. **Operands**
  307. - *Step* – relative shift from current position, in bytes
  308. - *Threshold* – threshold value for branch condition
  309. - *Condition*:
  310. - *GE* (greater or equal) – jump if value in R0 >= threshold
  311. - *LT* (less than) – jump if value in R0 < threshold
  312. **Cycles**
  313. 2 (fetch) + 2 (execute)
  314. **Description**
  315. 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.
  316. **Examples**::
  317. 1:pos: JUMPR 16, 20, GE // Jump to address (position + 16 bytes) if value in R0 >= 20
  318. 2: // Down counting loop using R0 register
  319. MOVE R0, 16 // load 16 into R0
  320. label: SUB R0, R0, 1 // R0--
  321. NOP // do something
  322. JUMPR label, 1, GE // jump to label if R0 >= 1
  323. **JUMPS** – Jump to a relative address (condition based on stage count)
  324. -----------------------------------------------------------------------
  325. **Syntax**
  326. **JUMPS** *Step, Threshold, Condition*
  327. **Operands**
  328. - *Step* – relative shift from current position, in bytes
  329. - *Threshold* – threshold value for branch condition
  330. - *Condition*:
  331. - *EQ* (equal) – jump if value in stage_cnt == threshold
  332. - *LT* (less than) – jump if value in stage_cnt < threshold
  333. - *GT* (greater than) – jump if value in stage_cnt > threshold
  334. **Cycles**
  335. 2 (fetch) + 2 (execute)
  336. **Description**
  337. 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.
  338. **Examples**::
  339. 1:pos: JUMPS 16, 20, EQ // Jump to (position + 16 bytes) if stage_cnt == 20
  340. 2: // Up counting loop using stage count register
  341. STAGE_RST // set stage_cnt to 0
  342. label: STAGE_INC 1 // stage_cnt++
  343. NOP // do something
  344. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  345. **STAGE_RST** – Reset stage count register
  346. ------------------------------------------
  347. **Syntax**
  348. **STAGE_RST**
  349. **Operands**
  350. No operands
  351. **Description**
  352. The instruction sets the stage count register to 0
  353. **Cycles**
  354. 2 (fetch) + 2 (execute)
  355. **Examples**::
  356. 1: STAGE_RST // Reset stage count register
  357. **STAGE_INC** – Increment stage count register
  358. ----------------------------------------------
  359. **Syntax**
  360. **STAGE_INC** *Value*
  361. **Operands**
  362. - *Value* – 8 bits value
  363. **Cycles**
  364. 2 (fetch) + 2 (execute)
  365. **Description**
  366. The instruction increments stage count register by given value.
  367. **Examples**::
  368. 1: STAGE_INC 10 // stage_cnt += 10
  369. 2: // Up counting loop example:
  370. STAGE_RST // set stage_cnt to 0
  371. label: STAGE_INC 1 // stage_cnt++
  372. NOP // do something
  373. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  374. **STAGE_DEC** – Decrement stage count register
  375. ----------------------------------------------
  376. **Syntax**
  377. **STAGE_DEC** *Value*
  378. **Operands**
  379. - *Value* – 8 bits value
  380. **Cycles**
  381. 2 (fetch) + 2 (execute)
  382. **Description**
  383. The instruction decrements stage count register by given value.
  384. **Examples**::
  385. 1: STAGE_DEC 10 // stage_cnt -= 10;
  386. 2: // Down counting loop exaple
  387. STAGE_RST // set stage_cnt to 0
  388. STAGE_INC 16 // increment stage_cnt to 16
  389. label: STAGE_DEC 1 // stage_cnt--;
  390. NOP // do something
  391. JUMPS label, 0, GT // jump to label if stage_cnt > 0
  392. **HALT** – End the program
  393. --------------------------
  394. **Syntax**
  395. **HALT**
  396. **Operands**
  397. No operands
  398. **Cycles**
  399. 2 (fetch) + 2 (execute)
  400. **Description**
  401. The instruction halts the ULP coprocessor and restarts ULP wakeup timer, if it is enabled.
  402. **Examples**::
  403. 1: HALT // Halt the coprocessor
  404. **WAKE** – Wake up the chip
  405. ---------------------------
  406. **Syntax**
  407. **WAKE**
  408. **Operands**
  409. No operands
  410. **Cycles**
  411. 2 (fetch) + 2 (execute)
  412. **Description**
  413. The instruction sends an interrupt from ULP to RTC controller.
  414. - If the SoC is in deep sleep mode, and ULP wakeup is enabled, this causes the SoC to wake up.
  415. - 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.
  416. 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).
  417. **Examples**::
  418. 1: is_rdy_for_wakeup: // Read RTC_CNTL_RDY_FOR_WAKEUP bit
  419. READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP)
  420. AND r0, r0, 1
  421. JUMP is_rdy_for_wakeup, eq // Retry until the bit is set
  422. WAKE // Trigger wake up
  423. REG_WR 0x006, 24, 24, 0 // Stop ULP timer (clear RTC_CNTL_ULP_CP_SLP_TIMER_EN)
  424. HALT // Stop the ULP program
  425. // After these instructions, SoC will wake up,
  426. // and ULP will not run again until started by the main program.
  427. **SLEEP** – set ULP wakeup timer period
  428. ---------------------------------------
  429. **Syntax**
  430. **SLEEP** *sleep_reg*
  431. **Operands**
  432. - *sleep_reg* – 0..4, selects one of ``SENS_ULP_CP_SLEEP_CYCx_REG`` registers.
  433. **Cycles**
  434. 2 (fetch) + 2 (execute)
  435. **Description**
  436. 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.
  437. **Examples**::
  438. 1: SLEEP 1 // Use period set in SENS_ULP_CP_SLEEP_CYC1_REG
  439. 2: .set sleep_reg, 4 // Set constant
  440. SLEEP sleep_reg // Use period set in SENS_ULP_CP_SLEEP_CYC4_REG
  441. **WAIT** – wait some number of cycles
  442. -------------------------------------
  443. **Syntax**
  444. **WAIT** *Cycles*
  445. **Operands**
  446. - *Cycles* – number of cycles for wait
  447. **Cycles**
  448. 2 (fetch) + *Cycles* (execute)
  449. **Description**
  450. The instruction delays for given number of cycles.
  451. **Examples**::
  452. 1: WAIT 10 // Do nothing for 10 cycles
  453. 2: .set wait_cnt, 10 // Set a constant
  454. WAIT wait_cnt // wait for 10 cycles
  455. **TSENS** – do measurement with temperature sensor
  456. --------------------------------------------------
  457. **Syntax**
  458. - **TSENS** *Rdst, Wait_Delay*
  459. **Operands**
  460. - *Rdst* – Destination Register R[0..3], result will be stored to this register
  461. - *Wait_Delay* – number of cycles used to perform the measurement
  462. **Cycles**
  463. 2 (fetch) + *Wait_Delay* + 3 * TSENS_CLK
  464. **Description**
  465. The instruction performs measurement using TSENS and stores the result into a general purpose register.
  466. **Examples**::
  467. 1: TSENS R1, 1000 // Measure temperature sensor for 1000 cycles,
  468. // and store result to R1
  469. **ADC** – do measurement with ADC
  470. ---------------------------------
  471. **Syntax**
  472. - **ADC** *Rdst, Sar_sel, Mux*
  473. - **ADC** *Rdst, Sar_sel, Mux, 0* — deprecated form
  474. **Operands**
  475. - *Rdst* – Destination Register R[0..3], result will be stored to this register
  476. - *Sar_sel* – Select ADC: 0 = SARADC1, 1 = SARADC2
  477. - *Mux* - selected PAD, SARADC Pad[Mux+1] is enabled
  478. **Cycles**
  479. 2 (fetch) + 21 + max(1, SAR_AMP_WAIT1) + max(1, SAR_AMP_WAIT2) + max(1, SAR_AMP_WAIT3) + SARx_SAMPLE_CYCLE + SARx_SAMPLE_BIT
  480. **Description**
  481. The instruction makes measurements from ADC.
  482. **Examples**::
  483. 1: ADC R1, 0, 1 // Measure value using ADC1 pad 2 and store result into R1
  484. **I2C_RD** - read single byte from I2C slave
  485. ----------------------------------------------
  486. **Syntax**
  487. - **I2C_RD** *Sub_addr, High, Low, Slave_sel*
  488. **Operands**
  489. - *Sub_addr* – Address within the I2C slave to read.
  490. - *High*, *Low* — Define range of bits to read. Bits outside of [High, Low] range are masked.
  491. - *Slave_sel* - Index of I2C slave address to use.
  492. **Cycles**
  493. 2 (fetch) + I2C communication time
  494. **Description**
  495. ``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``.
  496. 8 bits of read result is stored into `R0` register.
  497. **Examples**::
  498. 1: I2C_RD 0x10, 7, 0, 0 // Read byte from sub-address 0x10 of slave with address set in SENS_I2C_SLAVE_ADDR0
  499. **I2C_WR** - write single byte to I2C slave
  500. ----------------------------------------------
  501. **Syntax**
  502. - **I2C_WR** *Sub_addr, Value, High, Low, Slave_sel*
  503. **Operands**
  504. - *Sub_addr* – Address within the I2C slave to write.
  505. - *Value* – 8-bit value to be written.
  506. - *High*, *Low* — Define range of bits to write. Bits outside of [High, Low] range are masked.
  507. - *Slave_sel* - Index of I2C slave address to use.
  508. **Cycles**
  509. 2 (fetch) + I2C communication time
  510. **Description**
  511. ``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``.
  512. **Examples**::
  513. 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.
  514. **REG_RD** – read from peripheral register
  515. ------------------------------------------
  516. **Syntax**
  517. **REG_RD** *Addr, High, Low*
  518. **Operands**
  519. - *Addr* – register address, in 32-bit words
  520. - *High* – High part of R0
  521. - *Low* – Low part of R0
  522. **Cycles**
  523. 2 (fetch) + 6 (execute)
  524. **Description**
  525. The instruction reads up to 16 bits from a peripheral register into a general purpose register: ``R0 = REG[Addr][High:Low]``.
  526. 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,
  527. can be calculated from the address of the same register on the DPORT bus as follows::
  528. addr_ulp = (addr_dport - DR_REG_RTCCNTL_BASE) / 4
  529. **Examples**::
  530. 1: REG_RD 0x120, 2, 0 // load 4 bits: R0 = {12'b0, REG[0x120][7:4]}
  531. **REG_WR** – write to peripheral register
  532. -----------------------------------------
  533. **Syntax**
  534. **REG_WR** *Addr, High, Low, Data*
  535. **Operands**
  536. - *Addr* – register address, in 32-bit words.
  537. - *High* – High part of R0
  538. - *Low* – Low part of R0
  539. - *Data* – value to write, 8 bits
  540. **Cycles**
  541. 2 (fetch) + 10 (execute)
  542. **Description**
  543. The instruction writes up to 8 bits from a general purpose register into a peripheral register. ``REG[Addr][High:Low] = data``
  544. 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,
  545. can be calculated from the address of the same register on the DPORT bus as follows::
  546. addr_ulp = (addr_dport - DR_REG_RTCCNTL_BASE) / 4
  547. **Examples**::
  548. 1: REG_WR 0x120, 7, 0, 0x10 // set 8 bits: REG[0x120][7:0] = 0x10
  549. Convenience macros for peripheral registers access
  550. --------------------------------------------------
  551. ULP source files are passed through C preprocessor before the assembler. This allows certain macros to be used to facilitate access to peripheral registers.
  552. 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.
  553. 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``.
  554. READ_RTC_REG(rtc_reg, low_bit, bit_width)
  555. Read up to 16 bits from rtc_reg[low_bit + bit_width - 1 : low_bit] into R0. For example::
  556. #include "soc/soc_ulp.h"
  557. #include "soc/rtc_cntl_reg.h"
  558. /* Read 16 lower bits of RTC_CNTL_TIME0_REG into R0 */
  559. READ_RTC_REG(RTC_CNTL_TIME0_REG, 0, 16)
  560. READ_RTC_FIELD(rtc_reg, field)
  561. Read from a field in rtc_reg into R0, up to 16 bits. For example::
  562. #include "soc/soc_ulp.h"
  563. #include "soc/sens_reg.h"
  564. /* Read 8-bit SENS_TSENS_OUT field of SENS_SAR_SLAVE_ADDR3_REG into R0 */
  565. READ_RTC_FIELD(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT)
  566. WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value)
  567. Write immediate value into rtc_reg[low_bit + bit_width - 1 : low_bit], bit_width <= 8. For example::
  568. #include "soc/soc_ulp.h"
  569. #include "soc/rtc_io_reg.h"
  570. /* Set BIT(2) of RTC_GPIO_OUT_DATA_W1TS field in RTC_GPIO_OUT_W1TS_REG */
  571. WRITE_RTC_REG(RTC_GPIO_OUT_W1TS_REG, RTC_GPIO_OUT_DATA_W1TS_S + 2, 1, 1)
  572. WRITE_RTC_FIELD(rtc_reg, field, value)
  573. Write immediate value into a field in rtc_reg, up to 8 bits. For example::
  574. #include "soc/soc_ulp.h"
  575. #include "soc/rtc_cntl_reg.h"
  576. /* Set RTC_CNTL_ULP_CP_SLP_TIMER_EN field of RTC_CNTL_STATE0_REG to 0 */
  577. WRITE_RTC_FIELD(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN, 0)