ulp.h 33 KB

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