ulp.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include "esp_err.h"
  19. #include "soc/soc.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #define ULP_FSM_PREPARE_SLEEP_CYCLES 2 /*!< Cycles spent by FSM preparing ULP for sleep */
  24. #define ULP_FSM_WAKEUP_SLEEP_CYCLES 2 /*!< Cycles spent by FSM waking up ULP from sleep */
  25. /**
  26. * @defgroup ulp_registers ULP coprocessor registers
  27. * @{
  28. */
  29. #define R0 0 /*!< general purpose register 0 */
  30. #define R1 1 /*!< general purpose register 1 */
  31. #define R2 2 /*!< general purpose register 2 */
  32. #define R3 3 /*!< general purpose register 3 */
  33. /**@}*/
  34. /** @defgroup ulp_opcodes ULP coprocessor opcodes, sub opcodes, and various modifiers/flags
  35. *
  36. * These definitions are not intended to be used directly.
  37. * They are used in definitions of instructions later on.
  38. *
  39. * @{
  40. */
  41. #define OPCODE_WR_REG 1 /*!< Instruction: write peripheral register (RTC_CNTL/RTC_IO/SARADC) */
  42. #define OPCODE_RD_REG 2 /*!< Instruction: read peripheral register (RTC_CNTL/RTC_IO/SARADC) */
  43. #define RD_REG_PERIPH_RTC_CNTL 0 /*!< Identifier of RTC_CNTL peripheral for RD_REG and WR_REG instructions */
  44. #define RD_REG_PERIPH_RTC_IO 1 /*!< Identifier of RTC_IO peripheral for RD_REG and WR_REG instructions */
  45. #define RD_REG_PERIPH_SENS 2 /*!< Identifier of SARADC peripheral for RD_REG and WR_REG instructions */
  46. #define RD_REG_PERIPH_RTC_I2C 3 /*!< Identifier of RTC_I2C peripheral for RD_REG and WR_REG instructions */
  47. #define OPCODE_I2C 3 /*!< Instruction: read/write I2C */
  48. #define SUB_OPCODE_I2C_RD 0 /*!< I2C read */
  49. #define SUB_OPCODE_I2C_WR 1 /*!< I2C write */
  50. #define OPCODE_DELAY 4 /*!< Instruction: delay (nop) for a given number of cycles */
  51. #define OPCODE_ADC 5 /*!< Instruction: SAR ADC measurement */
  52. #define OPCODE_ST 6 /*!< Instruction: store indirect to RTC memory */
  53. #define SUB_OPCODE_ST 4 /*!< Store 32 bits, 16 MSBs contain PC, 16 LSBs contain value from source register */
  54. #define OPCODE_ALU 7 /*!< Arithmetic instructions */
  55. #define SUB_OPCODE_ALU_REG 0 /*!< Arithmetic instruction, both source values are in register */
  56. #define SUB_OPCODE_ALU_IMM 1 /*!< Arithmetic instruction, one source value is an immediate */
  57. #define SUB_OPCODE_ALU_CNT 2 /*!< Arithmetic instruction, stage counter and an immediate */
  58. #define ALU_SEL_ADD 0 /*!< Addition */
  59. #define ALU_SEL_SUB 1 /*!< Subtraction */
  60. #define ALU_SEL_AND 2 /*!< Logical AND */
  61. #define ALU_SEL_OR 3 /*!< Logical OR */
  62. #define ALU_SEL_MOV 4 /*!< Copy value (immediate to destination register or source register to destination register */
  63. #define ALU_SEL_LSH 5 /*!< Shift left by given number of bits */
  64. #define ALU_SEL_RSH 6 /*!< Shift right by given number of bits */
  65. #define ALU_SEL_SINC 0 /*!< Increment the stage counter */
  66. #define ALU_SEL_SDEC 1 /*!< Decrement the stage counter */
  67. #define ALU_SEL_SRST 2 /*!< Reset the stage counter */
  68. #define OPCODE_BRANCH 8 /*!< Branch instructions */
  69. #define SUB_OPCODE_BX 0 /*!< Branch to absolute PC (immediate or in register) */
  70. #define SUB_OPCODE_BR 1 /*!< Branch to relative PC, conditional on R0 */
  71. #define SUB_OPCODE_BS 2 /*!< Branch to relative PC, conditional on the stage counter */
  72. #define BX_JUMP_TYPE_DIRECT 0 /*!< Unconditional jump */
  73. #define BX_JUMP_TYPE_ZERO 1 /*!< Branch if last ALU result is zero */
  74. #define BX_JUMP_TYPE_OVF 2 /*!< Branch if last ALU operation caused and overflow */
  75. #define SUB_OPCODE_B 1 /*!< Branch to a relative offset */
  76. #define B_CMP_L 0 /*!< Branch if R0 is less than an immediate */
  77. #define B_CMP_GE 1 /*!< Branch if R0 is greater than or equal to an immediate */
  78. #define JUMPS_LT 0 /*!< Branch if the stage counter < */
  79. #define JUMPS_GE 1 /*!< Branch if the stage counter >= */
  80. #define JUMPS_LE 2 /*!< Branch if the stage counter <= */
  81. #define OPCODE_END 9 /*!< Stop executing the program */
  82. #define SUB_OPCODE_END 0 /*!< Stop executing the program and optionally wake up the chip */
  83. #define SUB_OPCODE_SLEEP 1 /*!< Stop executing the program and run it again after selected interval */
  84. #define OPCODE_TSENS 10 /*!< Instruction: temperature sensor measurement */
  85. #define OPCODE_HALT 11 /*!< Halt the coprocessor */
  86. #define OPCODE_LD 13 /*!< Indirect load lower 16 bits from RTC memory */
  87. #define OPCODE_MACRO 15 /*!< Not a real opcode. Used to identify labels and branches in the program */
  88. #define SUB_OPCODE_MACRO_LABEL 0 /*!< Label macro */
  89. #define SUB_OPCODE_MACRO_BRANCH 1 /*!< Branch macro */
  90. #define SUB_OPCODE_MACRO_LABELPC 2 /*!< Label pointer macro */
  91. /**@}*/
  92. /**@{*/
  93. #define ESP_ERR_ULP_BASE 0x1200 /*!< Offset for ULP-related error codes */
  94. #define ESP_ERR_ULP_SIZE_TOO_BIG (ESP_ERR_ULP_BASE + 1) /*!< Program doesn't fit into RTC memory reserved for the ULP */
  95. #define ESP_ERR_ULP_INVALID_LOAD_ADDR (ESP_ERR_ULP_BASE + 2) /*!< Load address is outside of RTC memory reserved for the ULP */
  96. #define ESP_ERR_ULP_DUPLICATE_LABEL (ESP_ERR_ULP_BASE + 3) /*!< More than one label with the same number was defined */
  97. #define ESP_ERR_ULP_UNDEFINED_LABEL (ESP_ERR_ULP_BASE + 4) /*!< Branch instructions references an undefined label */
  98. #define ESP_ERR_ULP_BRANCH_OUT_OF_RANGE (ESP_ERR_ULP_BASE + 5) /*!< Branch target is out of range of B instruction (try replacing with BX) */
  99. /**@}*/
  100. /**
  101. * @brief Instruction format structure
  102. *
  103. * All ULP instructions are 32 bit long.
  104. * This union contains field layouts used by all of the supported instructions.
  105. * This union also includes a special "macro" instruction layout.
  106. * This is not a real instruction which can be executed by the CPU. It acts
  107. * as a token which is removed from the program by the
  108. * ulp_process_macros_and_load function.
  109. *
  110. * These structures are not intended to be used directly.
  111. * Preprocessor definitions provided below fill the fields of these structure with
  112. * the right arguments.
  113. */
  114. typedef union {
  115. struct {
  116. uint32_t cycles : 16; /*!< Number of cycles to sleep */
  117. uint32_t unused : 12; /*!< Unused */
  118. uint32_t opcode : 4; /*!< Opcode (OPCODE_DELAY) */
  119. } delay; /*!< Format of DELAY instruction */
  120. struct {
  121. uint32_t dreg : 2; /*!< Register which contains data to store */
  122. uint32_t sreg : 2; /*!< Register which contains address in RTC memory (expressed in words) */
  123. uint32_t unused1 : 6; /*!< Unused */
  124. uint32_t offset : 11; /*!< Offset to add to sreg */
  125. uint32_t unused2 : 4; /*!< Unused */
  126. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ST) */
  127. uint32_t opcode : 4; /*!< Opcode (OPCODE_ST) */
  128. } st; /*!< Format of ST instruction */
  129. struct {
  130. uint32_t dreg : 2; /*!< Register where the data should be loaded to */
  131. uint32_t sreg : 2; /*!< Register which contains address in RTC memory (expressed in words) */
  132. uint32_t unused1 : 6; /*!< Unused */
  133. uint32_t offset : 11; /*!< Offset to add to sreg */
  134. uint32_t unused2 : 7; /*!< Unused */
  135. uint32_t opcode : 4; /*!< Opcode (OPCODE_LD) */
  136. } ld; /*!< Format of LD instruction */
  137. struct {
  138. uint32_t unused : 28; /*!< Unused */
  139. uint32_t opcode : 4; /*!< Opcode (OPCODE_HALT) */
  140. } halt; /*!< Format of HALT instruction */
  141. struct {
  142. uint32_t dreg : 2; /*!< Register which contains target PC, expressed in words (used if .reg == 1) */
  143. uint32_t addr : 11; /*!< Target PC, expressed in words (used if .reg == 0) */
  144. uint32_t unused : 8; /*!< Unused */
  145. uint32_t reg : 1; /*!< Target PC in register (1) or immediate (0) */
  146. uint32_t type : 3; /*!< Jump condition (BX_JUMP_TYPE_xxx) */
  147. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_BX) */
  148. uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */
  149. } bx; /*!< Format of BRANCH instruction (absolute address) */
  150. struct {
  151. uint32_t imm : 16; /*!< Immediate value to compare against */
  152. uint32_t cmp : 1; /*!< Comparison to perform: B_CMP_L or B_CMP_GE */
  153. uint32_t offset : 7; /*!< Absolute value of target PC offset w.r.t. current PC, expressed in words */
  154. uint32_t sign : 1; /*!< Sign of target PC offset: 0: positive, 1: negative */
  155. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_B) */
  156. uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */
  157. } b; /*!< Format of BRANCH instruction (relative address, conditional on R0) */
  158. struct {
  159. uint32_t imm : 8; /*!< Immediate value to compare against */
  160. uint32_t unused : 7; /*!< Unused */
  161. uint32_t cmp : 2; /*!< Comparison to perform: JUMPS_LT, JUMPS_GE or JUMPS_LE */
  162. uint32_t offset : 7; /*!< Absolute value of target PC offset w.r.t. current PC, expressed in words */
  163. uint32_t sign : 1; /*!< Sign of target PC offset: 0: positive, 1: negative */
  164. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_BS) */
  165. uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */
  166. } bs; /*!< Format of BRANCH instruction (relative address, conditional on the stage counter) */
  167. struct {
  168. uint32_t dreg : 2; /*!< Destination register */
  169. uint32_t sreg : 2; /*!< Register with operand A */
  170. uint32_t treg : 2; /*!< Register with operand B */
  171. uint32_t unused : 15; /*!< Unused */
  172. uint32_t sel : 4; /*!< Operation to perform, one of ALU_SEL_xxx */
  173. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ALU_REG) */
  174. uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */
  175. } alu_reg; /*!< Format of ALU instruction (both sources are registers) */
  176. struct {
  177. uint32_t unused1 : 4; /*!< Unused */
  178. uint32_t imm : 8; /*!< Immediate value of operand */
  179. uint32_t unused2 : 9; /*!< Unused */
  180. uint32_t sel : 4; /*!< Operation to perform, one of ALU_SEL_Sxxx */
  181. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ALU_CNT) */
  182. uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */
  183. } alu_reg_s; /*!< Format of ALU instruction (stage counter and an immediate) */
  184. struct {
  185. uint32_t dreg : 2; /*!< Destination register */
  186. uint32_t sreg : 2; /*!< Register with operand A */
  187. uint32_t imm : 16; /*!< Immediate value of operand B */
  188. uint32_t unused : 1; /*!< Unused */
  189. uint32_t sel : 4; /*!< Operation to perform, one of ALU_SEL_xxx */
  190. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ALU_IMM) */
  191. uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */
  192. } alu_imm; /*!< Format of ALU instruction (one source is an immediate) */
  193. struct {
  194. uint32_t addr : 8; /*!< Address within either RTC_CNTL, RTC_IO, or SARADC */
  195. uint32_t periph_sel : 2; /*!< Select peripheral: RTC_CNTL (0), RTC_IO(1), SARADC(2) */
  196. uint32_t data : 8; /*!< 8 bits of data to write */
  197. uint32_t low : 5; /*!< Low bit */
  198. uint32_t high : 5; /*!< High bit */
  199. uint32_t opcode : 4; /*!< Opcode (OPCODE_WR_REG) */
  200. } wr_reg; /*!< Format of WR_REG instruction */
  201. struct {
  202. uint32_t addr : 8; /*!< Address within either RTC_CNTL, RTC_IO, or SARADC */
  203. uint32_t periph_sel : 2; /*!< Select peripheral: RTC_CNTL (0), RTC_IO(1), SARADC(2) */
  204. uint32_t unused : 8; /*!< Unused */
  205. uint32_t low : 5; /*!< Low bit */
  206. uint32_t high : 5; /*!< High bit */
  207. uint32_t opcode : 4; /*!< Opcode (OPCODE_WR_REG) */
  208. } rd_reg; /*!< Format of RD_REG instruction */
  209. struct {
  210. uint32_t dreg : 2; /*!< Register where to store ADC result */
  211. uint32_t mux : 4; /*!< Select SARADC pad (mux + 1) */
  212. uint32_t sar_sel : 1; /*!< Select SARADC0 (0) or SARADC1 (1) */
  213. uint32_t unused1 : 1; /*!< Unused */
  214. uint32_t cycles : 16; /*!< TBD, cycles used for measurement */
  215. uint32_t unused2 : 4; /*!< Unused */
  216. uint32_t opcode: 4; /*!< Opcode (OPCODE_ADC) */
  217. } adc; /*!< Format of ADC instruction */
  218. struct {
  219. uint32_t dreg : 2; /*!< Register where to store temperature measurement result */
  220. uint32_t wait_delay: 14; /*!< Cycles to wait after measurement is done */
  221. uint32_t reserved: 12; /*!< Reserved, set to 0 */
  222. uint32_t opcode: 4; /*!< Opcode (OPCODE_TSENS) */
  223. } tsens; /*!< Format of TSENS instruction */
  224. struct {
  225. uint32_t i2c_addr : 8; /*!< I2C slave address */
  226. uint32_t data : 8; /*!< 8 bits of data for write operation */
  227. uint32_t low_bits : 3; /*!< low bit of range for write operation (lower bits are masked) */
  228. uint32_t high_bits : 3; /*!< high bit of range for write operation (higher bits are masked) */
  229. uint32_t i2c_sel : 4; /*!< index of slave address register [7:0] */
  230. uint32_t unused : 1; /*!< Unused */
  231. uint32_t rw : 1; /*!< Write (1) or read (0) */
  232. uint32_t opcode : 4; /*!< Opcode (OPCODE_I2C) */
  233. } i2c; /*!< Format of I2C instruction */
  234. struct {
  235. uint32_t wakeup : 1; /*!< Set to 1 to wake up chip */
  236. uint32_t unused : 24; /*!< Unused */
  237. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_WAKEUP) */
  238. uint32_t opcode : 4; /*!< Opcode (OPCODE_END) */
  239. } end; /*!< Format of END instruction with wakeup */
  240. struct {
  241. uint32_t cycle_sel : 4; /*!< Select which one of SARADC_ULP_CP_SLEEP_CYCx_REG to get the sleep duration from */
  242. uint32_t unused : 21; /*!< Unused */
  243. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_SLEEP) */
  244. uint32_t opcode : 4; /*!< Opcode (OPCODE_END) */
  245. } sleep; /*!< Format of END instruction with sleep */
  246. struct {
  247. uint32_t dreg : 2; /*!< Destination register (for SUB_OPCODE_MACRO_LABELPC) > */
  248. uint32_t label : 16; /*!< Label number */
  249. uint32_t unused : 6; /*!< Unused */
  250. uint32_t sub_opcode : 4; /*!< SUB_OPCODE_MACRO_LABEL or SUB_OPCODE_MACRO_BRANCH or SUB_OPCODE_MACRO_LABELPC */
  251. uint32_t opcode: 4; /*!< Opcode (OPCODE_MACRO) */
  252. } macro; /*!< Format of tokens used by MACROs */
  253. uint32_t instruction; /*!< Encoded instruction for ULP coprocessor */
  254. } ulp_insn_t;
  255. _Static_assert(sizeof(ulp_insn_t) == 4, "ULP coprocessor instruction size should be 4 bytes");
  256. /**
  257. * Delay (nop) for a given number of cycles
  258. */
  259. #define I_DELAY(cycles_) { .delay = {\
  260. .cycles = cycles_, \
  261. .unused = 0, \
  262. .opcode = OPCODE_DELAY } }
  263. /**
  264. * Halt the coprocessor.
  265. *
  266. * This instruction halts the coprocessor, but keeps ULP timer active.
  267. * As such, ULP program will be restarted again by timer.
  268. * To stop the program and prevent the timer from restarting the program,
  269. * use I_END(0) instruction.
  270. */
  271. #define I_HALT() { .halt = {\
  272. .unused = 0, \
  273. .opcode = OPCODE_HALT } }
  274. /**
  275. * Map SoC peripheral register to periph_sel field of RD_REG and WR_REG
  276. * instructions.
  277. *
  278. * @param reg peripheral register in RTC_CNTL_, RTC_IO_, SENS_, RTC_I2C peripherals.
  279. * @return periph_sel value for the peripheral to which this register belongs.
  280. */
  281. static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) {
  282. uint32_t ret = 3;
  283. if (reg < DR_REG_RTCCNTL_BASE) {
  284. assert(0 && "invalid register base");
  285. } else if (reg < DR_REG_RTCIO_BASE) {
  286. ret = RD_REG_PERIPH_RTC_CNTL;
  287. } else if (reg < DR_REG_SENS_BASE) {
  288. ret = RD_REG_PERIPH_RTC_IO;
  289. } else if (reg < DR_REG_RTC_I2C_BASE){
  290. ret = RD_REG_PERIPH_SENS;
  291. } else if (reg < DR_REG_IO_MUX_BASE){
  292. ret = RD_REG_PERIPH_RTC_I2C;
  293. } else {
  294. assert(0 && "invalid register base");
  295. }
  296. return ret;
  297. }
  298. /**
  299. * Write literal value to a peripheral register
  300. *
  301. * reg[high_bit : low_bit] = val
  302. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  303. */
  304. #define I_WR_REG(reg, low_bit, high_bit, val) {.wr_reg = {\
  305. .addr = (reg & 0xff) / sizeof(uint32_t), \
  306. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  307. .data = val, \
  308. .low = low_bit, \
  309. .high = high_bit, \
  310. .opcode = OPCODE_WR_REG } }
  311. /**
  312. * Read from peripheral register into R0
  313. *
  314. * R0 = reg[high_bit : low_bit]
  315. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  316. */
  317. #define I_RD_REG(reg, low_bit, high_bit) {.rd_reg = {\
  318. .addr = (reg & 0xff) / sizeof(uint32_t), \
  319. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  320. .unused = 0, \
  321. .low = low_bit, \
  322. .high = high_bit, \
  323. .opcode = OPCODE_RD_REG } }
  324. /**
  325. * Set or clear a bit in the peripheral register.
  326. *
  327. * Sets bit (1 << shift) of register reg to value val.
  328. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  329. */
  330. #define I_WR_REG_BIT(reg, shift, val) I_WR_REG(reg, shift, shift, val)
  331. /**
  332. * Wake the SoC from deep sleep.
  333. *
  334. * This instruction initiates wake up from deep sleep.
  335. * Use esp_deep_sleep_enable_ulp_wakeup to enable deep sleep wakeup
  336. * triggered by the ULP before going into deep sleep.
  337. * Note that ULP program will still keep running until the I_HALT
  338. * instruction, and it will still be restarted by timer at regular
  339. * intervals, even when the SoC is woken up.
  340. *
  341. * To stop the ULP program, use I_HALT instruction.
  342. *
  343. * To disable the timer which start ULP program, use I_END()
  344. * instruction. I_END instruction clears the
  345. * RTC_CNTL_ULP_CP_SLP_TIMER_EN_S bit of RTC_CNTL_STATE0_REG
  346. * register, which controls the ULP timer.
  347. */
  348. #define I_WAKE() { .end = { \
  349. .wakeup = 1, \
  350. .unused = 0, \
  351. .sub_opcode = SUB_OPCODE_END, \
  352. .opcode = OPCODE_END } }
  353. /**
  354. * Stop ULP program timer.
  355. *
  356. * This is a convenience macro which disables the ULP program timer.
  357. * Once this instruction is used, ULP program will not be restarted
  358. * anymore until ulp_run function is called.
  359. *
  360. * ULP program will continue running after this instruction. To stop
  361. * the currently running program, use I_HALT().
  362. */
  363. #define I_END() \
  364. I_WR_REG_BIT(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN_S, 0)
  365. /**
  366. * Select the time interval used to run ULP program.
  367. *
  368. * This instructions selects which of the SENS_SLEEP_CYCLES_Sx
  369. * registers' value is used by the ULP program timer.
  370. * When the ULP program stops at I_HALT instruction, ULP program
  371. * timer start counting. When the counter reaches the value of
  372. * the selected SENS_SLEEP_CYCLES_Sx register, ULP program
  373. * start running again from the start address (passed to the ulp_run
  374. * function).
  375. * There are 5 SENS_SLEEP_CYCLES_Sx registers, so 0 <= timer_idx < 5.
  376. *
  377. * By default, SENS_SLEEP_CYCLES_S0 register is used by the ULP
  378. * program timer.
  379. */
  380. #define I_SLEEP_CYCLE_SEL(timer_idx) { .sleep = { \
  381. .cycle_sel = timer_idx, \
  382. .unused = 0, \
  383. .sub_opcode = SUB_OPCODE_SLEEP, \
  384. .opcode = OPCODE_END } }
  385. /**
  386. * Perform temperature sensor measurement and store it into reg_dest.
  387. *
  388. * Delay can be set between 1 and ((1 << 14) - 1). Higher values give
  389. * higher measurement resolution.
  390. */
  391. #define I_TSENS(reg_dest, delay) { .tsens = { \
  392. .dreg = reg_dest, \
  393. .wait_delay = delay, \
  394. .reserved = 0, \
  395. .opcode = OPCODE_TSENS } }
  396. /**
  397. * Perform ADC measurement and store result in reg_dest.
  398. *
  399. * adc_idx selects ADC (0 or 1).
  400. * pad_idx selects ADC pad (0 - 7).
  401. */
  402. #define I_ADC(reg_dest, adc_idx, pad_idx) { .adc = {\
  403. .dreg = reg_dest, \
  404. .mux = pad_idx + 1, \
  405. .sar_sel = adc_idx, \
  406. .unused1 = 0, \
  407. .cycles = 0, \
  408. .unused2 = 0, \
  409. .opcode = OPCODE_ADC } }
  410. /**
  411. * Store value from register reg_val into RTC memory.
  412. *
  413. * The value is written to an offset calculated by adding value of
  414. * reg_addr register and offset_ field (this offset is expressed in 32-bit words).
  415. * 32 bits written to RTC memory are built as follows:
  416. * - bits [31:21] hold the PC of current instruction, expressed in 32-bit words
  417. * - bits [20:16] = 5'b1
  418. * - bits [15:0] are assigned the contents of reg_val
  419. *
  420. * RTC_SLOW_MEM[addr + offset_] = { 5'b0, insn_PC[10:0], val[15:0] }
  421. */
  422. #define I_ST(reg_val, reg_addr, offset_) { .st = { \
  423. .dreg = reg_val, \
  424. .sreg = reg_addr, \
  425. .unused1 = 0, \
  426. .offset = offset_, \
  427. .unused2 = 0, \
  428. .sub_opcode = SUB_OPCODE_ST, \
  429. .opcode = OPCODE_ST } }
  430. /**
  431. * Load value from RTC memory into reg_dest register.
  432. *
  433. * Loads 16 LSBs from RTC memory word given by the sum of value in reg_addr and
  434. * value of offset_.
  435. */
  436. #define I_LD(reg_dest, reg_addr, offset_) { .ld = { \
  437. .dreg = reg_dest, \
  438. .sreg = reg_addr, \
  439. .unused1 = 0, \
  440. .offset = offset_, \
  441. .unused2 = 0, \
  442. .opcode = OPCODE_LD } }
  443. /**
  444. * Branch relative if R0 less than immediate value.
  445. *
  446. * pc_offset is expressed in words, and can be from -127 to 127
  447. * imm_value is a 16-bit value to compare R0 against
  448. */
  449. #define I_BL(pc_offset, imm_value) { .b = { \
  450. .imm = imm_value, \
  451. .cmp = B_CMP_L, \
  452. .offset = abs(pc_offset), \
  453. .sign = (pc_offset >= 0) ? 0 : 1, \
  454. .sub_opcode = SUB_OPCODE_B, \
  455. .opcode = OPCODE_BRANCH } }
  456. /**
  457. * Branch relative if R0 greater or equal than immediate value.
  458. *
  459. * pc_offset is expressed in words, and can be from -127 to 127
  460. * imm_value is a 16-bit value to compare R0 against
  461. */
  462. #define I_BGE(pc_offset, imm_value) { .b = { \
  463. .imm = imm_value, \
  464. .cmp = B_CMP_GE, \
  465. .offset = abs(pc_offset), \
  466. .sign = (pc_offset >= 0) ? 0 : 1, \
  467. .sub_opcode = SUB_OPCODE_B, \
  468. .opcode = OPCODE_BRANCH } }
  469. /**
  470. * Unconditional branch to absolute PC, address in register.
  471. *
  472. * reg_pc is the register which contains address to jump to.
  473. * Address is expressed in 32-bit words.
  474. */
  475. #define I_BXR(reg_pc) { .bx = { \
  476. .dreg = reg_pc, \
  477. .addr = 0, \
  478. .unused = 0, \
  479. .reg = 1, \
  480. .type = BX_JUMP_TYPE_DIRECT, \
  481. .sub_opcode = SUB_OPCODE_BX, \
  482. .opcode = OPCODE_BRANCH } }
  483. /**
  484. * Unconditional branch to absolute PC, immediate address.
  485. *
  486. * Address imm_pc is expressed in 32-bit words.
  487. */
  488. #define I_BXI(imm_pc) { .bx = { \
  489. .dreg = 0, \
  490. .addr = imm_pc, \
  491. .unused = 0, \
  492. .reg = 0, \
  493. .type = BX_JUMP_TYPE_DIRECT, \
  494. .sub_opcode = SUB_OPCODE_BX, \
  495. .opcode = OPCODE_BRANCH } }
  496. /**
  497. * Branch to absolute PC if ALU result is zero, address in register.
  498. *
  499. * reg_pc is the register which contains address to jump to.
  500. * Address is expressed in 32-bit words.
  501. */
  502. #define I_BXZR(reg_pc) { .bx = { \
  503. .dreg = reg_pc, \
  504. .addr = 0, \
  505. .unused = 0, \
  506. .reg = 1, \
  507. .type = BX_JUMP_TYPE_ZERO, \
  508. .sub_opcode = SUB_OPCODE_BX, \
  509. .opcode = OPCODE_BRANCH } }
  510. /**
  511. * Branch to absolute PC if ALU result is zero, immediate address.
  512. *
  513. * Address imm_pc is expressed in 32-bit words.
  514. */
  515. #define I_BXZI(imm_pc) { .bx = { \
  516. .dreg = 0, \
  517. .addr = imm_pc, \
  518. .unused = 0, \
  519. .reg = 0, \
  520. .type = BX_JUMP_TYPE_ZERO, \
  521. .sub_opcode = SUB_OPCODE_BX, \
  522. .opcode = OPCODE_BRANCH } }
  523. /**
  524. * Branch to absolute PC if ALU overflow, address in register
  525. *
  526. * reg_pc is the register which contains address to jump to.
  527. * Address is expressed in 32-bit words.
  528. */
  529. #define I_BXFR(reg_pc) { .bx = { \
  530. .dreg = reg_pc, \
  531. .addr = 0, \
  532. .unused = 0, \
  533. .reg = 1, \
  534. .type = BX_JUMP_TYPE_OVF, \
  535. .sub_opcode = SUB_OPCODE_BX, \
  536. .opcode = OPCODE_BRANCH } }
  537. /**
  538. * Branch to absolute PC if ALU overflow, immediate address
  539. *
  540. * Address imm_pc is expressed in 32-bit words.
  541. */
  542. #define I_BXFI(imm_pc) { .bx = { \
  543. .dreg = 0, \
  544. .addr = imm_pc, \
  545. .unused = 0, \
  546. .reg = 0, \
  547. .type = BX_JUMP_TYPE_OVF, \
  548. .sub_opcode = SUB_OPCODE_BX, \
  549. .opcode = OPCODE_BRANCH } }
  550. /**
  551. * Addition: dest = src1 + src2
  552. */
  553. #define I_ADDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  554. .dreg = reg_dest, \
  555. .sreg = reg_src1, \
  556. .treg = reg_src2, \
  557. .unused = 0, \
  558. .sel = ALU_SEL_ADD, \
  559. .sub_opcode = SUB_OPCODE_ALU_REG, \
  560. .opcode = OPCODE_ALU } }
  561. /**
  562. * Subtraction: dest = src1 - src2
  563. */
  564. #define I_SUBR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  565. .dreg = reg_dest, \
  566. .sreg = reg_src1, \
  567. .treg = reg_src2, \
  568. .unused = 0, \
  569. .sel = ALU_SEL_SUB, \
  570. .sub_opcode = SUB_OPCODE_ALU_REG, \
  571. .opcode = OPCODE_ALU } }
  572. /**
  573. * Logical AND: dest = src1 & src2
  574. */
  575. #define I_ANDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  576. .dreg = reg_dest, \
  577. .sreg = reg_src1, \
  578. .treg = reg_src2, \
  579. .unused = 0, \
  580. .sel = ALU_SEL_AND, \
  581. .sub_opcode = SUB_OPCODE_ALU_REG, \
  582. .opcode = OPCODE_ALU } }
  583. /**
  584. * Logical OR: dest = src1 | src2
  585. */
  586. #define I_ORR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  587. .dreg = reg_dest, \
  588. .sreg = reg_src1, \
  589. .treg = reg_src2, \
  590. .unused = 0, \
  591. .sel = ALU_SEL_OR, \
  592. .sub_opcode = SUB_OPCODE_ALU_REG, \
  593. .opcode = OPCODE_ALU } }
  594. /**
  595. * Copy: dest = src
  596. */
  597. #define I_MOVR(reg_dest, reg_src) { .alu_reg = { \
  598. .dreg = reg_dest, \
  599. .sreg = reg_src, \
  600. .treg = 0, \
  601. .unused = 0, \
  602. .sel = ALU_SEL_MOV, \
  603. .sub_opcode = SUB_OPCODE_ALU_REG, \
  604. .opcode = OPCODE_ALU } }
  605. /**
  606. * Logical shift left: dest = src << shift
  607. */
  608. #define I_LSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  609. .dreg = reg_dest, \
  610. .sreg = reg_src, \
  611. .treg = reg_shift, \
  612. .unused = 0, \
  613. .sel = ALU_SEL_LSH, \
  614. .sub_opcode = SUB_OPCODE_ALU_REG, \
  615. .opcode = OPCODE_ALU } }
  616. /**
  617. * Logical shift right: dest = src >> shift
  618. */
  619. #define I_RSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  620. .dreg = reg_dest, \
  621. .sreg = reg_src, \
  622. .treg = reg_shift, \
  623. .unused = 0, \
  624. .sel = ALU_SEL_RSH, \
  625. .sub_opcode = SUB_OPCODE_ALU_REG, \
  626. .opcode = OPCODE_ALU } }
  627. /**
  628. * Add register and an immediate value: dest = src1 + imm
  629. */
  630. #define I_ADDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  631. .dreg = reg_dest, \
  632. .sreg = reg_src, \
  633. .imm = imm_, \
  634. .unused = 0, \
  635. .sel = ALU_SEL_ADD, \
  636. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  637. .opcode = OPCODE_ALU } }
  638. /**
  639. * Subtract register and an immediate value: dest = src - imm
  640. */
  641. #define I_SUBI(reg_dest, reg_src, imm_) { .alu_imm = { \
  642. .dreg = reg_dest, \
  643. .sreg = reg_src, \
  644. .imm = imm_, \
  645. .unused = 0, \
  646. .sel = ALU_SEL_SUB, \
  647. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  648. .opcode = OPCODE_ALU } }
  649. /**
  650. * Logical AND register and an immediate value: dest = src & imm
  651. */
  652. #define I_ANDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  653. .dreg = reg_dest, \
  654. .sreg = reg_src, \
  655. .imm = imm_, \
  656. .unused = 0, \
  657. .sel = ALU_SEL_AND, \
  658. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  659. .opcode = OPCODE_ALU } }
  660. /**
  661. * Logical OR register and an immediate value: dest = src | imm
  662. */
  663. #define I_ORI(reg_dest, reg_src, imm_) { .alu_imm = { \
  664. .dreg = reg_dest, \
  665. .sreg = reg_src, \
  666. .imm = imm_, \
  667. .unused = 0, \
  668. .sel = ALU_SEL_OR, \
  669. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  670. .opcode = OPCODE_ALU } }
  671. /**
  672. * Copy an immediate value into register: dest = imm
  673. */
  674. #define I_MOVI(reg_dest, imm_) { .alu_imm = { \
  675. .dreg = reg_dest, \
  676. .sreg = 0, \
  677. .imm = imm_, \
  678. .unused = 0, \
  679. .sel = ALU_SEL_MOV, \
  680. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  681. .opcode = OPCODE_ALU } }
  682. /**
  683. * Logical shift left register value by an immediate: dest = src << imm
  684. */
  685. #define I_LSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  686. .dreg = reg_dest, \
  687. .sreg = reg_src, \
  688. .imm = imm_, \
  689. .unused = 0, \
  690. .sel = ALU_SEL_LSH, \
  691. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  692. .opcode = OPCODE_ALU } }
  693. /**
  694. * Logical shift right register value by an immediate: dest = val >> imm
  695. */
  696. #define I_RSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  697. .dreg = reg_dest, \
  698. .sreg = reg_src, \
  699. .imm = imm_, \
  700. .unused = 0, \
  701. .sel = ALU_SEL_RSH, \
  702. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  703. .opcode = OPCODE_ALU } }
  704. /**
  705. * Define a label with number label_num.
  706. *
  707. * This is a macro which doesn't generate a real instruction.
  708. * The token generated by this macro is removed by ulp_process_macros_and_load
  709. * function. Label defined using this macro can be used in branch macros defined
  710. * below.
  711. */
  712. #define M_LABEL(label_num) { .macro = { \
  713. .dreg = 0, \
  714. .label = label_num, \
  715. .unused = 0, \
  716. .sub_opcode = SUB_OPCODE_MACRO_LABEL, \
  717. .opcode = OPCODE_MACRO } }
  718. /**
  719. * Token macro used by M_B and M_BX macros. Not to be used directly.
  720. */
  721. #define M_BRANCH(label_num) { .macro = { \
  722. .dreg = 0, \
  723. .label = label_num, \
  724. .unused = 0, \
  725. .sub_opcode = SUB_OPCODE_MACRO_BRANCH, \
  726. .opcode = OPCODE_MACRO } }
  727. /**
  728. * Token macro used by M_MOVL macro. Not to be used directly.
  729. */
  730. #define M_LABELPC(label_num) { .macro = { \
  731. .dreg = 0, \
  732. .label = label_num, \
  733. .unused = 0, \
  734. .sub_opcode = SUB_OPCODE_MACRO_LABELPC, \
  735. .opcode = OPCODE_MACRO } }
  736. /**
  737. * Macro: Move the program counter at the given label into the register.
  738. * This address can then be used with I_BXR, I_BXZR, I_BXFR, etc.
  739. *
  740. * This macro generates two ulp_insn_t values separated by a comma, and should
  741. * be used when defining contents of ulp_insn_t arrays. First value is not a
  742. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  743. * function.
  744. */
  745. #define M_MOVL(reg_dest, label_num) \
  746. M_LABELPC(label_num), \
  747. I_MOVI(reg_dest, 0)
  748. /**
  749. * Macro: branch to label label_num if R0 is less than immediate value.
  750. *
  751. * This macro generates two ulp_insn_t values separated by a comma, and should
  752. * be used when defining contents of ulp_insn_t arrays. First value is not a
  753. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  754. * function.
  755. */
  756. #define M_BL(label_num, imm_value) \
  757. M_BRANCH(label_num), \
  758. I_BL(0, imm_value)
  759. /**
  760. * Macro: branch to label label_num if R0 is greater or equal than immediate value
  761. *
  762. * This macro generates two ulp_insn_t values separated by a comma, and should
  763. * be used when defining contents of ulp_insn_t arrays. First value is not a
  764. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  765. * function.
  766. */
  767. #define M_BGE(label_num, imm_value) \
  768. M_BRANCH(label_num), \
  769. I_BGE(0, imm_value)
  770. /**
  771. * Macro: unconditional branch to label
  772. *
  773. * This macro generates two ulp_insn_t values separated by a comma, and should
  774. * be used when defining contents of ulp_insn_t arrays. First value is not a
  775. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  776. * function.
  777. */
  778. #define M_BX(label_num) \
  779. M_BRANCH(label_num), \
  780. I_BXI(0)
  781. /**
  782. * Macro: branch to label if ALU result is zero
  783. *
  784. * This macro generates two ulp_insn_t values separated by a comma, and should
  785. * be used when defining contents of ulp_insn_t arrays. First value is not a
  786. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  787. * function.
  788. */
  789. #define M_BXZ(label_num) \
  790. M_BRANCH(label_num), \
  791. I_BXZI(0)
  792. /**
  793. * Macro: branch to label if ALU overflow
  794. *
  795. * This macro generates two ulp_insn_t values separated by a comma, and should
  796. * be used when defining contents of ulp_insn_t arrays. First value is not a
  797. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  798. * function.
  799. */
  800. #define M_BXF(label_num) \
  801. M_BRANCH(label_num), \
  802. I_BXFI(0)
  803. /**
  804. * Increment the stage counter by immediate value
  805. */
  806. #define I_STAGE_INC(imm_) { .alu_reg_s = { \
  807. .unused1 = 0, \
  808. .imm = imm_, \
  809. .unused2 = 0, \
  810. .sel = ALU_SEL_SINC, \
  811. .sub_opcode = SUB_OPCODE_ALU_CNT, \
  812. .opcode = OPCODE_ALU } }
  813. /**
  814. * Decrement the stage counter by immediate value
  815. */
  816. #define I_STAGE_DEC(imm_) { .alu_reg_s = { \
  817. .unused1 = 0, \
  818. .imm = imm_, \
  819. .unused2 = 0, \
  820. .sel = ALU_SEL_SDEC, \
  821. .sub_opcode = SUB_OPCODE_ALU_CNT, \
  822. .opcode = OPCODE_ALU } }
  823. /**
  824. * Reset the stage counter
  825. */
  826. #define I_STAGE_RST() { .alu_reg_s = { \
  827. .unused1 = 0, \
  828. .imm = 0, \
  829. .unused2 = 0, \
  830. .sel = ALU_SEL_SRST, \
  831. .sub_opcode = SUB_OPCODE_ALU_CNT, \
  832. .opcode = OPCODE_ALU } }
  833. /**
  834. * Macro: branch to label if the stage counter is less than immediate value
  835. *
  836. * This macro generates two ulp_insn_t values separated by a comma, and should
  837. * be used when defining contents of ulp_insn_t arrays. First value is not a
  838. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  839. * function.
  840. */
  841. #define M_BSLT(label_num, imm_value) \
  842. M_BRANCH(label_num), \
  843. I_JUMPS(0, imm_value, JUMPS_LT)
  844. /**
  845. * Macro: branch to label if the stage counter is greater than or equal to immediate value
  846. *
  847. * This macro generates two ulp_insn_t values separated by a comma, and should
  848. * be used when defining contents of ulp_insn_t arrays. First value is not a
  849. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  850. * function.
  851. */
  852. #define M_BSGE(label_num, imm_value) \
  853. M_BRANCH(label_num), \
  854. I_JUMPS(0, imm_value, JUMPS_GE)
  855. /**
  856. * Macro: branch to label if the stage counter is less than or equal to immediate value
  857. *
  858. * This macro generates two ulp_insn_t values separated by a comma, and should
  859. * be used when defining contents of ulp_insn_t arrays. First value is not a
  860. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  861. * function.
  862. */
  863. #define M_BSLE(label_num, imm_value) \
  864. M_BRANCH(label_num), \
  865. I_JUMPS(0, imm_value, JUMPS_LE)
  866. /**
  867. * Macro: branch to label if the stage counter is equal to immediate value.
  868. * Implemented using two JUMPS instructions:
  869. * JUMPS next, imm_value, LT
  870. * JUMPS label_num, imm_value, LE
  871. *
  872. * This macro generates three ulp_insn_t values separated by commas, and should
  873. * be used when defining contents of ulp_insn_t arrays. Second value is not a
  874. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  875. * function.
  876. */
  877. #define M_BSEQ(label_num, imm_value) \
  878. I_JUMPS(2, imm_value, JUMPS_LT), \
  879. M_BRANCH(label_num), \
  880. I_JUMPS(0, imm_value, JUMPS_LE)
  881. /**
  882. * Macro: branch to label if the stage counter is greater than immediate value.
  883. * Implemented using two instructions:
  884. * JUMPS next, imm_value, LE
  885. * JUMPS label_num, imm_value, GE
  886. *
  887. * This macro generates three ulp_insn_t values separated by commas, and should
  888. * be used when defining contents of ulp_insn_t arrays. Second value is not a
  889. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  890. * function.
  891. */
  892. #define M_BSGT(label_num, imm_value) \
  893. I_JUMPS(2, imm_value, JUMPS_LE), \
  894. M_BRANCH(label_num), \
  895. I_JUMPS(0, imm_value, JUMPS_GE)
  896. /**
  897. * Branch relative if (stage counter [comp_type] [imm_value]) evaluates to true.
  898. *
  899. * pc_offset is expressed in words, and can be from -127 to 127
  900. * imm_value is an 8-bit value to compare the stage counter against
  901. * comp_type is the type of comparison to perform: JUMPS_LT (<), JUMPS_GE (>=) or JUMPS_LE (<=)
  902. */
  903. #define I_JUMPS(pc_offset, imm_value, comp_type) { .bs = { \
  904. .imm = imm_value, \
  905. .unused = 0, \
  906. .cmp = comp_type, \
  907. .offset = abs(pc_offset), \
  908. .sign = (pc_offset >= 0) ? 0 : 1, \
  909. .sub_opcode = SUB_OPCODE_BS, \
  910. .opcode = OPCODE_BRANCH } }
  911. /**
  912. * Perform an I2C transaction with a slave device.
  913. * I_I2C_READ and I_I2C_WRITE are provided for convenience, instead of using this directly.
  914. *
  915. * Slave address (in 7-bit format) has to be set in advance into SENS_I2C_SLAVE_ADDRx register field, where x == slave_sel.
  916. * For read operations, 8 bits of read result is stored into R0 register.
  917. * For write operations, val will be written to sub_addr at [high_bit:low_bit]. Bits outside of this range are masked.
  918. */
  919. #define I_I2C_RW(sub_addr, val, low_bit, high_bit, slave_sel, rw_bit) { .i2c = {\
  920. .i2c_addr = sub_addr, \
  921. .data = val, \
  922. .low_bits = low_bit, \
  923. .high_bits = high_bit, \
  924. .i2c_sel = slave_sel, \
  925. .unused = 0, \
  926. .rw = rw_bit, \
  927. .opcode = OPCODE_I2C } }
  928. /**
  929. * Read a byte from the sub address of an I2C slave, and store the result in R0.
  930. *
  931. * Slave address (in 7-bit format) has to be set in advance into SENS_I2C_SLAVE_ADDRx register field, where x == slave_sel.
  932. */
  933. #define I_I2C_READ(slave_sel, sub_addr) I_I2C_RW(sub_addr, 0, 0, 0, slave_sel, SUB_OPCODE_I2C_RD)
  934. /**
  935. * Write a byte to the sub address of an I2C slave.
  936. *
  937. * Slave address (in 7-bit format) has to be set in advance into SENS_I2C_SLAVE_ADDRx register field, where x == slave_sel.
  938. */
  939. #define I_I2C_WRITE(slave_sel, sub_addr, val) I_I2C_RW(sub_addr, val, 0, 7, slave_sel, SUB_OPCODE_I2C_WR)
  940. #define RTC_SLOW_MEM ((uint32_t*) 0x50000000) /*!< RTC slow memory, 8k size */
  941. /**
  942. * @brief Resolve all macro references in a program and load it into RTC memory
  943. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  944. * @param program ulp_insn_t array with the program
  945. * @param psize size of the program, expressed in 32-bit words
  946. * @return
  947. * - ESP_OK on success
  948. * - ESP_ERR_NO_MEM if auxiliary temporary structure can not be allocated
  949. * - one of ESP_ERR_ULP_xxx if program is not valid or can not be loaded
  950. */
  951. esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* program, size_t* psize);
  952. /**
  953. * @brief Load ULP program binary into RTC memory
  954. *
  955. * ULP program binary should have the following format (all values little-endian):
  956. *
  957. * 1. MAGIC, (value 0x00706c75, 4 bytes)
  958. * 2. TEXT_OFFSET, offset of .text section from binary start (2 bytes)
  959. * 3. TEXT_SIZE, size of .text section (2 bytes)
  960. * 4. DATA_SIZE, size of .data section (2 bytes)
  961. * 5. BSS_SIZE, size of .bss section (2 bytes)
  962. * 6. (TEXT_OFFSET - 12) bytes of arbitrary data (will not be loaded into RTC memory)
  963. * 7. .text section
  964. * 8. .data section
  965. *
  966. * Linker script in components/ulp/ld/esp32.ulp.ld produces ELF files which
  967. * correspond to this format. This linker script produces binaries with load_addr == 0.
  968. *
  969. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  970. * @param program_binary pointer to program binary
  971. * @param program_size size of the program binary
  972. * @return
  973. * - ESP_OK on success
  974. * - ESP_ERR_INVALID_ARG if load_addr is out of range
  975. * - ESP_ERR_INVALID_SIZE if program_size doesn't match (TEXT_OFFSET + TEXT_SIZE + DATA_SIZE)
  976. * - ESP_ERR_NOT_SUPPORTED if the magic number is incorrect
  977. */
  978. esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size);
  979. /**
  980. * @brief Run the program loaded into RTC memory
  981. * @param entry_point entry point, expressed in 32-bit words
  982. * @return ESP_OK on success
  983. */
  984. esp_err_t ulp_run(uint32_t entry_point);
  985. /**
  986. * @brief Set one of ULP wakeup period values
  987. *
  988. * ULP coprocessor starts running the program when the wakeup timer counts up
  989. * to a given value (called period). There are 5 period values which can be
  990. * programmed into SENS_ULP_CP_SLEEP_CYCx_REG registers, x = 0..4.
  991. * By default, wakeup timer will use the period set into SENS_ULP_CP_SLEEP_CYC0_REG,
  992. * i.e. period number 0. ULP program code can use SLEEP instruction to select
  993. * which of the SENS_ULP_CP_SLEEP_CYCx_REG should be used for subsequent wakeups.
  994. *
  995. * However, please note that SLEEP instruction issued (from ULP program) while the system
  996. * is in deep sleep mode does not have effect, and sleep cycle count 0 is used.
  997. *
  998. * @param period_index wakeup period setting number (0 - 4)
  999. * @param period_us wakeup period, us
  1000. * @note The ULP FSM requires two clock cycles to wakeup before being able to run the program.
  1001. * Then additional 16 cycles are reserved after wakeup waiting until the 8M clock is stable.
  1002. * The FSM also requires two more clock cycles to go to sleep after the program execution is halted.
  1003. * The minimum wakeup period that may be set up for the ULP
  1004. * is equal to the total number of cycles spent on the above internal tasks.
  1005. * For a default configuration of the ULP running at 150kHz it makes about 133us.
  1006. * @return
  1007. * - ESP_OK on success
  1008. * - ESP_ERR_INVALID_ARG if period_index is out of range
  1009. */
  1010. esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us);
  1011. #ifdef __cplusplus
  1012. }
  1013. #endif