ulp_instruction_set.rst 31 KB

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