ulp.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. /**
  24. * @defgroup ulp_registers ULP coprocessor registers
  25. * @{
  26. */
  27. #define R0 0 /*!< general purpose register 0 */
  28. #define R1 1 /*!< general purpose register 1 */
  29. #define R2 2 /*!< general purpose register 2 */
  30. #define R3 3 /*!< general purpose register 3 */
  31. /**@}*/
  32. /** @defgroup ulp_opcodes ULP coprocessor opcodes, sub opcodes, and various modifiers/flags
  33. *
  34. * These definitions are not intended to be used directly.
  35. * They are used in definitions of instructions later on.
  36. *
  37. * @{
  38. */
  39. #define OPCODE_WR_REG 1 /*!< Instruction: write peripheral register (RTC_CNTL/RTC_IO/SARADC) (not implemented yet) */
  40. #define OPCODE_RD_REG 2 /*!< Instruction: read peripheral register (RTC_CNTL/RTC_IO/SARADC) (not implemented yet) */
  41. #define RD_REG_PERIPH_RTC_CNTL 0 /*!< Identifier of RTC_CNTL peripheral for RD_REG and WR_REG instructions */
  42. #define RD_REG_PERIPH_RTC_IO 1 /*!< Identifier of RTC_IO peripheral for RD_REG and WR_REG instructions */
  43. #define RD_REG_PERIPH_SENS 2 /*!< Identifier of SARADC peripheral for RD_REG and WR_REG instructions */
  44. #define RD_REG_PERIPH_RTC_I2C 3 /*!< Identifier of RTC_I2C peripheral for RD_REG and WR_REG instructions */
  45. #define OPCODE_I2C 3 /*!< Instruction: read/write I2C (not implemented yet) */
  46. #define OPCODE_DELAY 4 /*!< Instruction: delay (nop) for a given number of cycles */
  47. #define OPCODE_ADC 5 /*!< Instruction: SAR ADC measurement (not implemented yet) */
  48. #define OPCODE_ST 6 /*!< Instruction: store indirect to RTC memory */
  49. #define SUB_OPCODE_ST 4 /*!< Store 32 bits, 16 MSBs contain PC, 16 LSBs contain value from source register */
  50. #define OPCODE_ALU 7 /*!< Arithmetic instructions */
  51. #define SUB_OPCODE_ALU_REG 0 /*!< Arithmetic instruction, both source values are in register */
  52. #define SUB_OPCODE_ALU_IMM 1 /*!< Arithmetic instruction, one source value is an immediate */
  53. #define SUB_OPCODE_ALU_CNT 2 /*!< Arithmetic instruction between counter register and an immediate (not implemented yet)*/
  54. #define ALU_SEL_ADD 0 /*!< Addition */
  55. #define ALU_SEL_SUB 1 /*!< Subtraction */
  56. #define ALU_SEL_AND 2 /*!< Logical AND */
  57. #define ALU_SEL_OR 3 /*!< Logical OR */
  58. #define ALU_SEL_MOV 4 /*!< Copy value (immediate to destination register or source register to destination register */
  59. #define ALU_SEL_LSH 5 /*!< Shift left by given number of bits */
  60. #define ALU_SEL_RSH 6 /*!< Shift right by given number of bits */
  61. #define OPCODE_BRANCH 8 /*!< Branch instructions */
  62. #define SUB_OPCODE_BX 0 /*!< Branch to absolute PC (immediate or in register) */
  63. #define BX_JUMP_TYPE_DIRECT 0 /*!< Unconditional jump */
  64. #define BX_JUMP_TYPE_ZERO 1 /*!< Branch if last ALU result is zero */
  65. #define BX_JUMP_TYPE_OVF 2 /*!< Branch if last ALU operation caused and overflow */
  66. #define SUB_OPCODE_B 1 /*!< Branch to a relative offset */
  67. #define B_CMP_L 0 /*!< Branch if R0 is less than an immediate */
  68. #define B_CMP_GE 1 /*!< Branch if R0 is greater than or equal to an immediate */
  69. #define OPCODE_END 9 /*!< Stop executing the program */
  70. #define SUB_OPCODE_END 0 /*!< Stop executing the program and optionally wake up the chip */
  71. #define SUB_OPCODE_SLEEP 1 /*!< Stop executing the program and run it again after selected interval */
  72. #define OPCODE_TSENS 10 /*!< Instruction: temperature sensor measurement (not implemented yet) */
  73. #define OPCODE_HALT 11 /*!< Halt the coprocessor */
  74. #define OPCODE_LD 13 /*!< Indirect load lower 16 bits from RTC memory */
  75. #define OPCODE_MACRO 15 /*!< Not a real opcode. Used to identify labels and branches in the program */
  76. #define SUB_OPCODE_MACRO_LABEL 0 /*!< Label macro */
  77. #define SUB_OPCODE_MACRO_BRANCH 1 /*!< Branch macro */
  78. /**@}*/
  79. /**@{*/
  80. #define ESP_ERR_ULP_BASE 0x1200 /*!< Offset for ULP-related error codes */
  81. #define ESP_ERR_ULP_SIZE_TOO_BIG (ESP_ERR_ULP_BASE + 1) /*!< Program doesn't fit into RTC memory reserved for the ULP */
  82. #define ESP_ERR_ULP_INVALID_LOAD_ADDR (ESP_ERR_ULP_BASE + 2) /*!< Load address is outside of RTC memory reserved for the ULP */
  83. #define ESP_ERR_ULP_DUPLICATE_LABEL (ESP_ERR_ULP_BASE + 3) /*!< More than one label with the same number was defined */
  84. #define ESP_ERR_ULP_UNDEFINED_LABEL (ESP_ERR_ULP_BASE + 4) /*!< Branch instructions references an undefined label */
  85. #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) */
  86. /**@}*/
  87. /**
  88. * @brief Instruction format structure
  89. *
  90. * All ULP instructions are 32 bit long.
  91. * This union contains field layouts used by all of the supported instructions.
  92. * This union also includes a special "macro" instruction layout.
  93. * This is not a real instruction which can be executed by the CPU. It acts
  94. * as a token which is removed from the program by the
  95. * ulp_process_macros_and_load function.
  96. *
  97. * These structures are not intended to be used directly.
  98. * Preprocessor definitions provided below fill the fields of these structure with
  99. * the right arguments.
  100. */
  101. typedef union {
  102. struct {
  103. uint32_t cycles : 16; /*!< Number of cycles to sleep */
  104. uint32_t unused : 12; /*!< Unused */
  105. uint32_t opcode : 4; /*!< Opcode (OPCODE_DELAY) */
  106. } delay; /*!< Format of DELAY instruction */
  107. struct {
  108. uint32_t dreg : 2; /*!< Register which contains data to store */
  109. uint32_t sreg : 2; /*!< Register which contains address in RTC memory (expressed in words) */
  110. uint32_t unused1 : 6; /*!< Unused */
  111. uint32_t offset : 11; /*!< Offset to add to sreg */
  112. uint32_t unused2 : 4; /*!< Unused */
  113. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ST) */
  114. uint32_t opcode : 4; /*!< Opcode (OPCODE_ST) */
  115. } st; /*!< Format of ST instruction */
  116. struct {
  117. uint32_t dreg : 2; /*!< Register where the data should be loaded to */
  118. uint32_t sreg : 2; /*!< Register which contains address in RTC memory (expressed in words) */
  119. uint32_t unused1 : 6; /*!< Unused */
  120. uint32_t offset : 11; /*!< Offset to add to sreg */
  121. uint32_t unused2 : 7; /*!< Unused */
  122. uint32_t opcode : 4; /*!< Opcode (OPCODE_LD) */
  123. } ld; /*!< Format of LD instruction */
  124. struct {
  125. uint32_t unused : 28; /*!< Unused */
  126. uint32_t opcode : 4; /*!< Opcode (OPCODE_HALT) */
  127. } halt; /*!< Format of HALT instruction */
  128. struct {
  129. uint32_t dreg : 2; /*!< Register which contains target PC, expressed in words (used if .reg == 1) */
  130. uint32_t addr : 11; /*!< Target PC, expressed in words (used if .reg == 0) */
  131. uint32_t unused : 8; /*!< Unused */
  132. uint32_t reg : 1; /*!< Target PC in register (1) or immediate (0) */
  133. uint32_t type : 3; /*!< Jump condition (BX_JUMP_TYPE_xxx) */
  134. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_BX) */
  135. uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */
  136. } bx; /*!< Format of BRANCH instruction (absolute address) */
  137. struct {
  138. uint32_t imm : 16; /*!< Immediate value to compare against */
  139. uint32_t cmp : 1; /*!< Comparison to perform: B_CMP_L or B_CMP_GE */
  140. uint32_t offset : 7; /*!< Absolute value of target PC offset w.r.t. current PC, expressed in words */
  141. uint32_t sign : 1; /*!< Sign of target PC offset: 0: positive, 1: negative */
  142. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_B) */
  143. uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */
  144. } b; /*!< Format of BRANCH instruction (relative address) */
  145. struct {
  146. uint32_t dreg : 2; /*!< Destination register */
  147. uint32_t sreg : 2; /*!< Register with operand A */
  148. uint32_t treg : 2; /*!< Register with operand B */
  149. uint32_t unused : 15; /*!< Unused */
  150. uint32_t sel : 4; /*!< Operation to perform, one of ALU_SEL_xxx */
  151. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ALU_REG) */
  152. uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */
  153. } alu_reg; /*!< Format of ALU instruction (both sources are registers) */
  154. struct {
  155. uint32_t dreg : 2; /*!< Destination register */
  156. uint32_t sreg : 2; /*!< Register with operand A */
  157. uint32_t imm : 16; /*!< Immediate value of operand B */
  158. uint32_t unused : 1; /*!< Unused */
  159. uint32_t sel : 4; /*!< Operation to perform, one of ALU_SEL_xxx */
  160. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ALU_IMM) */
  161. uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */
  162. } alu_imm; /*!< Format of ALU instruction (one source is an immediate) */
  163. struct {
  164. uint32_t addr : 8; /*!< Address within either RTC_CNTL, RTC_IO, or SARADC */
  165. uint32_t periph_sel : 2; /*!< Select peripheral: RTC_CNTL (0), RTC_IO(1), SARADC(2) */
  166. uint32_t data : 8; /*!< 8 bits of data to write */
  167. uint32_t low : 5; /*!< Low bit */
  168. uint32_t high : 5; /*!< High bit */
  169. uint32_t opcode : 4; /*!< Opcode (OPCODE_WR_REG) */
  170. } wr_reg; /*!< Format of WR_REG instruction */
  171. struct {
  172. uint32_t addr : 8; /*!< Address within either RTC_CNTL, RTC_IO, or SARADC */
  173. uint32_t periph_sel : 2; /*!< Select peripheral: RTC_CNTL (0), RTC_IO(1), SARADC(2) */
  174. uint32_t unused : 8; /*!< Unused */
  175. uint32_t low : 5; /*!< Low bit */
  176. uint32_t high : 5; /*!< High bit */
  177. uint32_t opcode : 4; /*!< Opcode (OPCODE_WR_REG) */
  178. } rd_reg; /*!< Format of RD_REG instruction */
  179. struct {
  180. uint32_t dreg : 2; /*!< Register where to store ADC result */
  181. uint32_t mux : 4; /*!< Select SARADC pad (mux + 1) */
  182. uint32_t sar_sel : 1; /*!< Select SARADC0 (0) or SARADC1 (1) */
  183. uint32_t unused1 : 1; /*!< Unused */
  184. uint32_t cycles : 16; /*!< TBD, cycles used for measurement */
  185. uint32_t unused2 : 4; /*!< Unused */
  186. uint32_t opcode: 4; /*!< Opcode (OPCODE_ADC) */
  187. } adc; /*!< Format of ADC instruction */
  188. struct {
  189. uint32_t dreg : 2; /*!< Register where to store temperature measurement result */
  190. uint32_t wait_delay: 14; /*!< Cycles to wait after measurement is done */
  191. uint32_t reserved: 12; /*!< Reserved, set to 0 */
  192. uint32_t opcode: 4; /*!< Opcode (OPCODE_TSENS) */
  193. } tsens; /*!< Format of TSENS instruction */
  194. struct {
  195. uint32_t i2c_addr : 8; /*!< I2C slave address */
  196. uint32_t data : 8; /*!< Data to read or write */
  197. uint32_t low_bits : 3; /*!< TBD */
  198. uint32_t high_bits : 3; /*!< TBD */
  199. uint32_t i2c_sel : 4; /*!< TBD, select reg_i2c_slave_address[7:0] */
  200. uint32_t unused : 1; /*!< Unused */
  201. uint32_t rw : 1; /*!< Write (1) or read (0) */
  202. uint32_t opcode : 4; /*!< Opcode (OPCODE_I2C) */
  203. } i2c; /*!< Format of I2C instruction */
  204. struct {
  205. uint32_t wakeup : 1; /*!< Set to 1 to wake up chip */
  206. uint32_t unused : 24; /*!< Unused */
  207. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_WAKEUP) */
  208. uint32_t opcode : 4; /*!< Opcode (OPCODE_END) */
  209. } end; /*!< Format of END instruction with wakeup */
  210. struct {
  211. uint32_t cycle_sel : 4; /*!< Select which one of SARADC_ULP_CP_SLEEP_CYCx_REG to get the sleep duration from */
  212. uint32_t unused : 21; /*!< Unused */
  213. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_SLEEP) */
  214. uint32_t opcode : 4; /*!< Opcode (OPCODE_END) */
  215. } sleep; /*!< Format of END instruction with sleep */
  216. struct {
  217. uint32_t label : 16; /*!< Label number */
  218. uint32_t unused : 8; /*!< Unused */
  219. uint32_t sub_opcode : 4; /*!< SUB_OPCODE_MACRO_LABEL or SUB_OPCODE_MACRO_BRANCH */
  220. uint32_t opcode: 4; /*!< Opcode (OPCODE_MACRO) */
  221. } macro; /*!< Format of tokens used by LABEL and BRANCH macros */
  222. } ulp_insn_t;
  223. _Static_assert(sizeof(ulp_insn_t) == 4, "ULP coprocessor instruction size should be 4 bytes");
  224. /**
  225. * Delay (nop) for a given number of cycles
  226. */
  227. #define I_DELAY(cycles_) { .delay = {\
  228. .cycles = cycles_, \
  229. .unused = 0, \
  230. .opcode = OPCODE_DELAY } }
  231. /**
  232. * Halt the coprocessor.
  233. *
  234. * This instruction halts the coprocessor, but keeps ULP timer active.
  235. * As such, ULP program will be restarted again by timer.
  236. * To stop the program and prevent the timer from restarting the program,
  237. * use I_END(0) instruction.
  238. */
  239. #define I_HALT() { .halt = {\
  240. .unused = 0, \
  241. .opcode = OPCODE_HALT } }
  242. /**
  243. * Map SoC peripheral register to periph_sel field of RD_REG and WR_REG
  244. * instructions.
  245. *
  246. * @param reg peripheral register in RTC_CNTL_, RTC_IO_, SENS_, RTC_I2C peripherals.
  247. * @return periph_sel value for the peripheral to which this register belongs.
  248. */
  249. static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) {
  250. uint32_t ret = 3;
  251. if (reg < DR_REG_RTCCNTL_BASE) {
  252. assert(0 && "invalid register base");
  253. } else if (reg < DR_REG_RTCIO_BASE) {
  254. ret = RD_REG_PERIPH_RTC_CNTL;
  255. } else if (reg < DR_REG_SENS_BASE) {
  256. ret = RD_REG_PERIPH_RTC_IO;
  257. } else if (reg < DR_REG_RTC_I2C_BASE){
  258. ret = RD_REG_PERIPH_SENS;
  259. } else if (reg < DR_REG_IO_MUX_BASE){
  260. ret = RD_REG_PERIPH_RTC_I2C;
  261. } else {
  262. assert(0 && "invalid register base");
  263. }
  264. return ret;
  265. }
  266. /**
  267. * Write literal value to a peripheral register
  268. *
  269. * reg[high_bit : low_bit] = val
  270. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  271. */
  272. #define I_WR_REG(reg, low_bit, high_bit, val) {.wr_reg = {\
  273. .addr = (reg & 0xff) / sizeof(uint32_t), \
  274. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  275. .data = val, \
  276. .low = low_bit, \
  277. .high = high_bit, \
  278. .opcode = OPCODE_WR_REG } }
  279. /**
  280. * Read from peripheral register into R0
  281. *
  282. * R0 = reg[high_bit : low_bit]
  283. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  284. */
  285. #define I_RD_REG(reg, low_bit, high_bit) {.rd_reg = {\
  286. .addr = (reg & 0xff) / sizeof(uint32_t), \
  287. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  288. .unused = 0, \
  289. .low = low_bit, \
  290. .high = high_bit, \
  291. .opcode = OPCODE_RD_REG } }
  292. /**
  293. * Set or clear a bit in the peripheral register.
  294. *
  295. * Sets bit (1 << shift) of register reg to value val.
  296. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  297. */
  298. #define I_WR_REG_BIT(reg, shift, val) I_WR_REG(reg, shift, shift, val)
  299. /**
  300. * Wake the SoC from deep sleep.
  301. *
  302. * This instruction initiates wake up from deep sleep.
  303. * Use esp_deep_sleep_enable_ulp_wakeup to enable deep sleep wakeup
  304. * triggered by the ULP before going into deep sleep.
  305. * Note that ULP program will still keep running until the I_HALT
  306. * instruction, and it will still be restarted by timer at regular
  307. * intervals, even when the SoC is woken up.
  308. *
  309. * To stop the ULP program, use I_HALT instruction.
  310. *
  311. * To disable the timer which start ULP program, use I_END()
  312. * instruction. I_END instruction clears the
  313. * RTC_CNTL_ULP_CP_SLP_TIMER_EN_S bit of RTC_CNTL_STATE0_REG
  314. * register, which controls the ULP timer.
  315. */
  316. #define I_WAKE() { .end = { \
  317. .wakeup = 1, \
  318. .unused = 0, \
  319. .sub_opcode = SUB_OPCODE_END, \
  320. .opcode = OPCODE_END } }
  321. /**
  322. * Stop ULP program timer.
  323. *
  324. * This is a convenience macro which disables the ULP program timer.
  325. * Once this instruction is used, ULP program will not be restarted
  326. * anymore until ulp_run function is called.
  327. *
  328. * ULP program will continue running after this instruction. To stop
  329. * the currently running program, use I_HALT().
  330. */
  331. #define I_END() \
  332. I_WR_REG_BIT(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN_S, 0)
  333. /**
  334. * Select the time interval used to run ULP program.
  335. *
  336. * This instructions selects which of the SENS_SLEEP_CYCLES_Sx
  337. * registers' value is used by the ULP program timer.
  338. * When the ULP program stops at I_HALT instruction, ULP program
  339. * timer start counting. When the counter reaches the value of
  340. * the selected SENS_SLEEP_CYCLES_Sx register, ULP program
  341. * start running again from the start address (passed to the ulp_run
  342. * function).
  343. * There are 5 SENS_SLEEP_CYCLES_Sx registers, so 0 <= timer_idx < 5.
  344. *
  345. * By default, SENS_SLEEP_CYCLES_S0 register is used by the ULP
  346. * program timer.
  347. */
  348. #define I_SLEEP_CYCLE_SEL(timer_idx) { .sleep = { \
  349. .cycle_sel = timer_idx, \
  350. .unused = 0, \
  351. .sub_opcode = SUB_OPCODE_SLEEP, \
  352. .opcode = OPCODE_END } }
  353. /**
  354. * Perform temperature sensor measurement and store it into reg_dest.
  355. *
  356. * Delay can be set between 1 and ((1 << 14) - 1). Higher values give
  357. * higher measurement resolution.
  358. */
  359. #define I_TSENS(reg_dest, delay) { .tsens = { \
  360. .dreg = reg_dest, \
  361. .wait_delay = delay, \
  362. .reserved = 0, \
  363. .opcode = OPCODE_TSENS } }
  364. /**
  365. * Perform ADC measurement and store result in reg_dest.
  366. *
  367. * adc_idx selects ADC (0 or 1).
  368. * pad_idx selects ADC pad (0 - 7).
  369. */
  370. #define I_ADC(reg_dest, adc_idx, pad_idx) { .adc = {\
  371. .dreg = reg_dest, \
  372. .mux = pad_idx + 1, \
  373. .sar_sel = adc_idx, \
  374. .unused1 = 0, \
  375. .cycles = 0, \
  376. .unused2 = 0, \
  377. .opcode = OPCODE_ADC } }
  378. /**
  379. * Store value from register reg_val into RTC memory.
  380. *
  381. * The value is written to an offset calculated by adding value of
  382. * reg_addr register and offset_ field (this offset is expressed in 32-bit words).
  383. * 32 bits written to RTC memory are built as follows:
  384. * - bits [31:21] hold the PC of current instruction, expressed in 32-bit words
  385. * - bits [20:16] = 5'b1
  386. * - bits [15:0] are assigned the contents of reg_val
  387. *
  388. * RTC_SLOW_MEM[addr + offset_] = { 5'b0, insn_PC[10:0], val[15:0] }
  389. */
  390. #define I_ST(reg_val, reg_addr, offset_) { .st = { \
  391. .dreg = reg_val, \
  392. .sreg = reg_addr, \
  393. .unused1 = 0, \
  394. .offset = offset_, \
  395. .unused2 = 0, \
  396. .sub_opcode = SUB_OPCODE_ST, \
  397. .opcode = OPCODE_ST } }
  398. /**
  399. * Load value from RTC memory into reg_dest register.
  400. *
  401. * Loads 16 LSBs from RTC memory word given by the sum of value in reg_addr and
  402. * value of offset_.
  403. */
  404. #define I_LD(reg_dest, reg_addr, offset_) { .ld = { \
  405. .dreg = reg_dest, \
  406. .sreg = reg_addr, \
  407. .unused1 = 0, \
  408. .offset = offset_, \
  409. .unused2 = 0, \
  410. .opcode = OPCODE_LD } }
  411. /**
  412. * Branch relative if R0 less than immediate value.
  413. *
  414. * pc_offset is expressed in words, and can be from -127 to 127
  415. * imm_value is a 16-bit value to compare R0 against
  416. */
  417. #define I_BL(pc_offset, imm_value) { .b = { \
  418. .imm = imm_value, \
  419. .cmp = B_CMP_L, \
  420. .offset = abs(pc_offset), \
  421. .sign = (pc_offset >= 0) ? 0 : 1, \
  422. .sub_opcode = SUB_OPCODE_B, \
  423. .opcode = OPCODE_BRANCH } }
  424. /**
  425. * Branch relative if R0 greater or equal than immediate value.
  426. *
  427. * pc_offset is expressed in words, and can be from -127 to 127
  428. * imm_value is a 16-bit value to compare R0 against
  429. */
  430. #define I_BGE(pc_offset, imm_value) { .b = { \
  431. .imm = imm_value, \
  432. .cmp = B_CMP_GE, \
  433. .offset = abs(pc_offset), \
  434. .sign = (pc_offset >= 0) ? 0 : 1, \
  435. .sub_opcode = SUB_OPCODE_B, \
  436. .opcode = OPCODE_BRANCH } }
  437. /**
  438. * Unconditional branch to absolute PC, address in register.
  439. *
  440. * reg_pc is the register which contains address to jump to.
  441. * Address is expressed in 32-bit words.
  442. */
  443. #define I_BXR(reg_pc) { .bx = { \
  444. .dreg = reg_pc, \
  445. .addr = 0, \
  446. .unused = 0, \
  447. .reg = 1, \
  448. .type = BX_JUMP_TYPE_DIRECT, \
  449. .sub_opcode = SUB_OPCODE_BX, \
  450. .opcode = OPCODE_BRANCH } }
  451. /**
  452. * Unconditional branch to absolute PC, immediate address.
  453. *
  454. * Address imm_pc is expressed in 32-bit words.
  455. */
  456. #define I_BXI(imm_pc) { .bx = { \
  457. .dreg = 0, \
  458. .addr = imm_pc, \
  459. .unused = 0, \
  460. .reg = 0, \
  461. .type = BX_JUMP_TYPE_DIRECT, \
  462. .sub_opcode = SUB_OPCODE_BX, \
  463. .opcode = OPCODE_BRANCH } }
  464. /**
  465. * Branch to absolute PC if ALU result is zero, address in register.
  466. *
  467. * reg_pc is the register which contains address to jump to.
  468. * Address is expressed in 32-bit words.
  469. */
  470. #define I_BXZR(reg_pc) { .bx = { \
  471. .dreg = reg_pc, \
  472. .addr = 0, \
  473. .unused = 0, \
  474. .reg = 1, \
  475. .type = BX_JUMP_TYPE_ZERO, \
  476. .sub_opcode = SUB_OPCODE_BX, \
  477. .opcode = OPCODE_BRANCH } }
  478. /**
  479. * Branch to absolute PC if ALU result is zero, immediate address.
  480. *
  481. * Address imm_pc is expressed in 32-bit words.
  482. */
  483. #define I_BXZI(imm_pc) { .bx = { \
  484. .dreg = 0, \
  485. .addr = imm_pc, \
  486. .unused = 0, \
  487. .reg = 0, \
  488. .type = BX_JUMP_TYPE_ZERO, \
  489. .sub_opcode = SUB_OPCODE_BX, \
  490. .opcode = OPCODE_BRANCH } }
  491. /**
  492. * Branch to absolute PC if ALU overflow, address in register
  493. *
  494. * reg_pc is the register which contains address to jump to.
  495. * Address is expressed in 32-bit words.
  496. */
  497. #define I_BXFR(reg_pc) { .bx = { \
  498. .dreg = reg_pc, \
  499. .addr = 0, \
  500. .unused = 0, \
  501. .reg = 1, \
  502. .type = BX_JUMP_TYPE_OVF, \
  503. .sub_opcode = SUB_OPCODE_BX, \
  504. .opcode = OPCODE_BRANCH } }
  505. /**
  506. * Branch to absolute PC if ALU overflow, immediate address
  507. *
  508. * Address imm_pc is expressed in 32-bit words.
  509. */
  510. #define I_BXFI(imm_pc) { .bx = { \
  511. .dreg = 0, \
  512. .addr = imm_pc, \
  513. .unused = 0, \
  514. .reg = 0, \
  515. .type = BX_JUMP_TYPE_OVF, \
  516. .sub_opcode = SUB_OPCODE_BX, \
  517. .opcode = OPCODE_BRANCH } }
  518. /**
  519. * Addition: dest = src1 + src2
  520. */
  521. #define I_ADDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  522. .dreg = reg_dest, \
  523. .sreg = reg_src1, \
  524. .treg = reg_src2, \
  525. .unused = 0, \
  526. .sel = ALU_SEL_ADD, \
  527. .sub_opcode = SUB_OPCODE_ALU_REG, \
  528. .opcode = OPCODE_ALU } }
  529. /**
  530. * Subtraction: dest = src1 - src2
  531. */
  532. #define I_SUBR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  533. .dreg = reg_dest, \
  534. .sreg = reg_src1, \
  535. .treg = reg_src2, \
  536. .unused = 0, \
  537. .sel = ALU_SEL_SUB, \
  538. .sub_opcode = SUB_OPCODE_ALU_REG, \
  539. .opcode = OPCODE_ALU } }
  540. /**
  541. * Logical AND: dest = src1 & src2
  542. */
  543. #define I_ANDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  544. .dreg = reg_dest, \
  545. .sreg = reg_src1, \
  546. .treg = reg_src2, \
  547. .unused = 0, \
  548. .sel = ALU_SEL_AND, \
  549. .sub_opcode = SUB_OPCODE_ALU_REG, \
  550. .opcode = OPCODE_ALU } }
  551. /**
  552. * Logical OR: dest = src1 | src2
  553. */
  554. #define I_ORR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  555. .dreg = reg_dest, \
  556. .sreg = reg_src1, \
  557. .treg = reg_src2, \
  558. .unused = 0, \
  559. .sel = ALU_SEL_OR, \
  560. .sub_opcode = SUB_OPCODE_ALU_REG, \
  561. .opcode = OPCODE_ALU } }
  562. /**
  563. * Copy: dest = src
  564. */
  565. #define I_MOVR(reg_dest, reg_src) { .alu_reg = { \
  566. .dreg = reg_dest, \
  567. .sreg = reg_src, \
  568. .treg = 0, \
  569. .unused = 0, \
  570. .sel = ALU_SEL_MOV, \
  571. .sub_opcode = SUB_OPCODE_ALU_REG, \
  572. .opcode = OPCODE_ALU } }
  573. /**
  574. * Logical shift left: dest = src << shift
  575. */
  576. #define I_LSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  577. .dreg = reg_dest, \
  578. .sreg = reg_src, \
  579. .treg = reg_shift, \
  580. .unused = 0, \
  581. .sel = ALU_SEL_LSH, \
  582. .sub_opcode = SUB_OPCODE_ALU_REG, \
  583. .opcode = OPCODE_ALU } }
  584. /**
  585. * Logical shift right: dest = src >> shift
  586. */
  587. #define I_RSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  588. .dreg = reg_dest, \
  589. .sreg = reg_src, \
  590. .treg = reg_shift, \
  591. .unused = 0, \
  592. .sel = ALU_SEL_RSH, \
  593. .sub_opcode = SUB_OPCODE_ALU_REG, \
  594. .opcode = OPCODE_ALU } }
  595. /**
  596. * Add register and an immediate value: dest = src1 + imm
  597. */
  598. #define I_ADDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  599. .dreg = reg_dest, \
  600. .sreg = reg_src, \
  601. .imm = imm_, \
  602. .unused = 0, \
  603. .sel = ALU_SEL_ADD, \
  604. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  605. .opcode = OPCODE_ALU } }
  606. /**
  607. * Subtract register and an immediate value: dest = src - imm
  608. */
  609. #define I_SUBI(reg_dest, reg_src, imm_) { .alu_imm = { \
  610. .dreg = reg_dest, \
  611. .sreg = reg_src, \
  612. .imm = imm_, \
  613. .unused = 0, \
  614. .sel = ALU_SEL_SUB, \
  615. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  616. .opcode = OPCODE_ALU } }
  617. /**
  618. * Logical AND register and an immediate value: dest = src & imm
  619. */
  620. #define I_ANDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  621. .dreg = reg_dest, \
  622. .sreg = reg_src, \
  623. .imm = imm_, \
  624. .unused = 0, \
  625. .sel = ALU_SEL_AND, \
  626. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  627. .opcode = OPCODE_ALU } }
  628. /**
  629. * Logical OR register and an immediate value: dest = src | imm
  630. */
  631. #define I_ORI(reg_dest, reg_src, imm_) { .alu_imm = { \
  632. .dreg = reg_dest, \
  633. .sreg = reg_src, \
  634. .imm = imm_, \
  635. .unused = 0, \
  636. .sel = ALU_SEL_OR, \
  637. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  638. .opcode = OPCODE_ALU } }
  639. /**
  640. * Copy an immediate value into register: dest = imm
  641. */
  642. #define I_MOVI(reg_dest, imm_) { .alu_imm = { \
  643. .dreg = reg_dest, \
  644. .sreg = 0, \
  645. .imm = imm_, \
  646. .unused = 0, \
  647. .sel = ALU_SEL_MOV, \
  648. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  649. .opcode = OPCODE_ALU } }
  650. /**
  651. * Logical shift left register value by an immediate: dest = src << imm
  652. */
  653. #define I_LSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  654. .dreg = reg_dest, \
  655. .sreg = reg_src, \
  656. .imm = imm_, \
  657. .unused = 0, \
  658. .sel = ALU_SEL_LSH, \
  659. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  660. .opcode = OPCODE_ALU } }
  661. /**
  662. * Logical shift right register value by an immediate: dest = val >> imm
  663. */
  664. #define I_RSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  665. .dreg = reg_dest, \
  666. .sreg = reg_src, \
  667. .imm = imm_, \
  668. .unused = 0, \
  669. .sel = ALU_SEL_RSH, \
  670. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  671. .opcode = OPCODE_ALU } }
  672. /**
  673. * Define a label with number label_num.
  674. *
  675. * This is a macro which doesn't generate a real instruction.
  676. * The token generated by this macro is removed by ulp_process_macros_and_load
  677. * function. Label defined using this macro can be used in branch macros defined
  678. * below.
  679. */
  680. #define M_LABEL(label_num) { .macro = { \
  681. .label = label_num, \
  682. .unused = 0, \
  683. .sub_opcode = SUB_OPCODE_MACRO_LABEL, \
  684. .opcode = OPCODE_MACRO } }
  685. /**
  686. * Token macro used by M_B and M_BX macros. Not to be used directly.
  687. */
  688. #define M_BRANCH(label_num) { .macro = { \
  689. .label = label_num, \
  690. .unused = 0, \
  691. .sub_opcode = SUB_OPCODE_MACRO_BRANCH, \
  692. .opcode = OPCODE_MACRO } }
  693. /**
  694. * Macro: branch to label label_num if R0 is less than immediate value.
  695. *
  696. * This macro generates two ulp_insn_t values separated by a comma, and should
  697. * be used when defining contents of ulp_insn_t arrays. First value is not a
  698. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  699. * function.
  700. */
  701. #define M_BL(label_num, imm_value) \
  702. M_BRANCH(label_num), \
  703. I_BL(0, imm_value)
  704. /**
  705. * Macro: branch to label label_num if R0 is greater or equal than immediate value
  706. *
  707. * This macro generates two ulp_insn_t values separated by a comma, and should
  708. * be used when defining contents of ulp_insn_t arrays. First value is not a
  709. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  710. * function.
  711. */
  712. #define M_BGE(label_num, imm_value) \
  713. M_BRANCH(label_num), \
  714. I_BGE(0, imm_value)
  715. /**
  716. * Macro: unconditional branch to label
  717. *
  718. * This macro generates two ulp_insn_t values separated by a comma, and should
  719. * be used when defining contents of ulp_insn_t arrays. First value is not a
  720. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  721. * function.
  722. */
  723. #define M_BX(label_num) \
  724. M_BRANCH(label_num), \
  725. I_BXI(0)
  726. /**
  727. * Macro: branch to label if ALU result is zero
  728. *
  729. * This macro generates two ulp_insn_t values separated by a comma, and should
  730. * be used when defining contents of ulp_insn_t arrays. First value is not a
  731. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  732. * function.
  733. */
  734. #define M_BXZ(label_num) \
  735. M_BRANCH(label_num), \
  736. I_BXZI(0)
  737. /**
  738. * Macro: branch to label if ALU overflow
  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_BXF(label_num) \
  746. M_BRANCH(label_num), \
  747. I_BXFI(0)
  748. #define RTC_SLOW_MEM ((uint32_t*) 0x50000000) /*!< RTC slow memory, 8k size */
  749. /**
  750. * @brief Resolve all macro references in a program and load it into RTC memory
  751. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  752. * @param program ulp_insn_t array with the program
  753. * @param psize size of the program, expressed in 32-bit words
  754. * @return
  755. * - ESP_OK on success
  756. * - ESP_ERR_NO_MEM if auxiliary temporary structure can not be allocated
  757. * - one of ESP_ERR_ULP_xxx if program is not valid or can not be loaded
  758. */
  759. esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* program, size_t* psize);
  760. /**
  761. * @brief Load ULP program binary into RTC memory
  762. *
  763. * ULP program binary should have the following format (all values little-endian):
  764. *
  765. * 1. MAGIC, (value 0x00706c75, 4 bytes)
  766. * 2. TEXT_OFFSET, offset of .text section from binary start (2 bytes)
  767. * 3. TEXT_SIZE, size of .text section (2 bytes)
  768. * 4. DATA_SIZE, size of .data section (2 bytes)
  769. * 5. BSS_SIZE, size of .bss section (2 bytes)
  770. * 6. (TEXT_OFFSET - 12) bytes of arbitrary data (will not be loaded into RTC memory)
  771. * 7. .text section
  772. * 8. .data section
  773. *
  774. * Linker script in components/ulp/ld/esp32.ulp.ld produces ELF files which
  775. * correspond to this format. This linker script produces binaries with load_addr == 0.
  776. *
  777. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  778. * @param program_binary pointer to program binary
  779. * @param program_size size of the program binary
  780. * @return
  781. * - ESP_OK on success
  782. * - ESP_ERR_INVALID_ARG if load_addr is out of range
  783. * - ESP_ERR_INVALID_SIZE if program_size doesn't match (TEXT_OFFSET + TEXT_SIZE + DATA_SIZE)
  784. * - ESP_ERR_NOT_SUPPORTED if the magic number is incorrect
  785. */
  786. esp_err_t ulp_load_binary(uint32_t load_addr, const uint8_t* program_binary, size_t program_size);
  787. /**
  788. * @brief Run the program loaded into RTC memory
  789. * @param entry_point entry point, expressed in 32-bit words
  790. * @return ESP_OK on success
  791. */
  792. esp_err_t ulp_run(uint32_t entry_point);
  793. /**
  794. * @brief Set one of ULP wakeup period values
  795. *
  796. * ULP coprocessor starts running the program when the wakeup timer counts up
  797. * to a given value (called period). There are 5 period values which can be
  798. * programmed into SENS_ULP_CP_SLEEP_CYCx_REG registers, x = 0..4.
  799. * By default, wakeup timer will use the period set into SENS_ULP_CP_SLEEP_CYC0_REG,
  800. * i.e. period number 0. ULP program code can use SLEEP instruction to select
  801. * which of the SENS_ULP_CP_SLEEP_CYCx_REG should be used for subsequent wakeups.
  802. *
  803. * @param period_index wakeup period setting number (0 - 4)
  804. * @param period_us wakeup period, us
  805. * @return
  806. * - ESP_OK on success
  807. * - ESP_ERR_INVALID_ARG if period_index is out of range
  808. */
  809. esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us);
  810. #ifdef __cplusplus
  811. }
  812. #endif