ulp.h 32 KB

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