ulp.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 (not implemented yet) */
  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 cycles: 12; /*!< Cycles used to perform measurement */
  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. #define I_HALT() { .halt = {\
  233. .unused = 0, \
  234. .opcode = OPCODE_HALT } }
  235. /**
  236. * Map SoC peripheral register to periph_sel field of RD_REG and WR_REG
  237. * instructions.
  238. *
  239. * @param reg peripheral register in RTC_CNTL_, RTC_IO_, SENS_ peripherals.
  240. * @return periph_sel value for the peripheral to which this register belongs.
  241. */
  242. static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) {
  243. uint32_t ret = 3;
  244. if (reg < DR_REG_RTCCNTL_BASE) {
  245. assert(0 && "invalid register base");
  246. } else if (reg < DR_REG_RTCIO_BASE) {
  247. ret = RD_REG_PERIPH_RTC_CNTL;
  248. } else if (reg < DR_REG_SENS_BASE) {
  249. ret = RD_REG_PERIPH_RTC_IO;
  250. } else if (reg < DR_REG_RTCMEM0_BASE){
  251. ret = RD_REG_PERIPH_SENS;
  252. } else {
  253. assert(0 && "invalid register base");
  254. }
  255. return ret;
  256. }
  257. /**
  258. * Write literal value to a peripheral register
  259. *
  260. * reg[high_bit : low_bit] = val
  261. * This instruction can access RTC_CNTL_, RTC_IO_, and SENS_ peripheral registers.
  262. */
  263. #define I_WR_REG(reg, low_bit, high_bit, val) {.wr_reg = {\
  264. .addr = reg & 0xff, \
  265. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  266. .data = val, \
  267. .low = low_bit, \
  268. .high = high_bit, \
  269. .opcode = OPCODE_WR_REG } }
  270. /**
  271. * Read from peripheral register into R0
  272. *
  273. * R0 = reg[high_bit : low_bit]
  274. * This instruction can access RTC_CNTL_, RTC_IO_, and SENS_ peripheral registers.
  275. */
  276. #define I_RD_REG(reg, low_bit, high_bit, val) {.wr_reg = {\
  277. .addr = reg & 0xff, \
  278. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  279. .unused = 0, \
  280. .low = low_bit, \
  281. .high = high_bit, \
  282. .opcode = OPCODE_RD_REG } }
  283. /**
  284. * End program.
  285. *
  286. * If wake == 1, wake up main CPU.
  287. */
  288. #define I_END(wake) { .end = { \
  289. .wakeup = wake, \
  290. .unused = 0, \
  291. .sub_opcode = SUB_OPCODE_END, \
  292. .opcode = OPCODE_END } }
  293. /**
  294. * Store value from register reg_val into RTC memory.
  295. *
  296. * The value is written to an offset calculated by adding value of
  297. * reg_addr register and offset_ field (this offset is expressed in 32-bit words).
  298. * 32 bits written to RTC memory are built as follows:
  299. * - 5 MSBs are zero
  300. * - next 11 bits hold the PC of current instruction, expressed in 32-bit words
  301. * - next 16 bits hold the actual value to be written
  302. *
  303. * RTC_SLOW_MEM[addr + offset_] = { 5'b0, insn_PC[10:0], val[15:0] }
  304. */
  305. #define I_ST(reg_val, reg_addr, offset_) { .st = { \
  306. .dreg = reg_val, \
  307. .sreg = reg_addr, \
  308. .unused1 = 0, \
  309. .offset = offset_, \
  310. .unused2 = 0, \
  311. .sub_opcode = SUB_OPCODE_ST, \
  312. .opcode = OPCODE_ST } }
  313. /**
  314. * Load value from RTC memory into reg_dest register.
  315. *
  316. * Loads 16 LSBs from RTC memory word given by the sum of value in reg_addr and
  317. * value of offset_.
  318. */
  319. #define I_LD(reg_dest, reg_addr, offset_) { .ld = { \
  320. .dreg = reg_dest, \
  321. .sreg = reg_addr, \
  322. .unused1 = 0, \
  323. .offset = offset_, \
  324. .unused2 = 0, \
  325. .opcode = OPCODE_LD } }
  326. /**
  327. * Branch relative if R0 less than immediate value.
  328. *
  329. * pc_offset is expressed in words, and can be from -127 to 127
  330. * imm_value is a 16-bit value to compare R0 against
  331. */
  332. #define I_BL(pc_offset, imm_value) { .b = { \
  333. .imm = imm_value, \
  334. .cmp = B_CMP_L, \
  335. .offset = abs(pc_offset), \
  336. .sign = (pc_offset >= 0) ? 0 : 1, \
  337. .sub_opcode = SUB_OPCODE_B, \
  338. .opcode = OPCODE_BRANCH } }
  339. /**
  340. * Branch relative if R0 greater or equal than immediate value.
  341. *
  342. * pc_offset is expressed in words, and can be from -127 to 127
  343. * imm_value is a 16-bit value to compare R0 against
  344. */
  345. #define I_BGE(pc_offset, imm_value) { .b = { \
  346. .imm = imm_value, \
  347. .cmp = B_CMP_GE, \
  348. .offset = abs(pc_offset), \
  349. .sign = (pc_offset >= 0) ? 0 : 1, \
  350. .sub_opcode = SUB_OPCODE_B, \
  351. .opcode = OPCODE_BRANCH } }
  352. /**
  353. * Unconditional branch to absolute PC, address in register.
  354. *
  355. * reg_pc is the register which contains address to jump to.
  356. * Address is expressed in 32-bit words.
  357. */
  358. #define I_BXR(reg_pc) { .bx = { \
  359. .dreg = reg_pc, \
  360. .addr = 0, \
  361. .unused = 0, \
  362. .reg = 1, \
  363. .type = BX_JUMP_TYPE_DIRECT, \
  364. .sub_opcode = SUB_OPCODE_BX, \
  365. .opcode = OPCODE_BRANCH } }
  366. /**
  367. * Unconditional branch to absolute PC, immediate address.
  368. *
  369. * Address imm_pc is expressed in 32-bit words.
  370. */
  371. #define I_BXI(imm_pc) { .bx = { \
  372. .dreg = 0, \
  373. .addr = imm_pc, \
  374. .unused = 0, \
  375. .reg = 0, \
  376. .type = BX_JUMP_TYPE_DIRECT, \
  377. .sub_opcode = SUB_OPCODE_BX, \
  378. .opcode = OPCODE_BRANCH } }
  379. /**
  380. * Branch to absolute PC if ALU result is zero, address in register.
  381. *
  382. * reg_pc is the register which contains address to jump to.
  383. * Address is expressed in 32-bit words.
  384. */
  385. #define I_BXZR(reg_pc) { .bx = { \
  386. .dreg = reg_pc, \
  387. .addr = 0, \
  388. .unused = 0, \
  389. .reg = 1, \
  390. .type = BX_JUMP_TYPE_ZERO, \
  391. .sub_opcode = SUB_OPCODE_BX, \
  392. .opcode = OPCODE_BRANCH } }
  393. /**
  394. * Branch to absolute PC if ALU result is zero, immediate address.
  395. *
  396. * Address imm_pc is expressed in 32-bit words.
  397. */
  398. #define I_BXZI(imm_pc) { .bx = { \
  399. .dreg = 0, \
  400. .addr = imm_pc, \
  401. .unused = 0, \
  402. .reg = 0, \
  403. .type = BX_JUMP_TYPE_ZERO, \
  404. .sub_opcode = SUB_OPCODE_BX, \
  405. .opcode = OPCODE_BRANCH } }
  406. /**
  407. * Branch to absolute PC if ALU overflow, address in register
  408. *
  409. * reg_pc is the register which contains address to jump to.
  410. * Address is expressed in 32-bit words.
  411. */
  412. #define I_BXFR(reg_pc) { .bx = { \
  413. .dreg = reg_pc, \
  414. .addr = 0, \
  415. .unused = 0, \
  416. .reg = 1, \
  417. .type = BX_JUMP_TYPE_OVF, \
  418. .sub_opcode = SUB_OPCODE_BX, \
  419. .opcode = OPCODE_BRANCH } }
  420. /**
  421. * Branch to absolute PC if ALU overflow, immediate address
  422. *
  423. * Address imm_pc is expressed in 32-bit words.
  424. */
  425. #define I_BXFI(imm_pc) { .bx = { \
  426. .dreg = 0, \
  427. .addr = imm_pc, \
  428. .unused = 0, \
  429. .reg = 0, \
  430. .type = BX_JUMP_TYPE_OVF, \
  431. .sub_opcode = SUB_OPCODE_BX, \
  432. .opcode = OPCODE_BRANCH } }
  433. /**
  434. * Addition: dest = src1 + src2
  435. */
  436. #define I_ADDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  437. .dreg = reg_dest, \
  438. .sreg = reg_src1, \
  439. .treg = reg_src2, \
  440. .unused = 0, \
  441. .sel = ALU_SEL_ADD, \
  442. .sub_opcode = SUB_OPCODE_ALU_REG, \
  443. .opcode = OPCODE_ALU } }
  444. /**
  445. * Subtraction: dest = src1 - src2
  446. */
  447. #define I_SUBR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  448. .dreg = reg_dest, \
  449. .sreg = reg_src1, \
  450. .treg = reg_src2, \
  451. .unused = 0, \
  452. .sel = ALU_SEL_SUB, \
  453. .sub_opcode = SUB_OPCODE_ALU_REG, \
  454. .opcode = OPCODE_ALU } }
  455. /**
  456. * Logical AND: dest = src1 & src2
  457. */
  458. #define I_ANDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  459. .dreg = reg_dest, \
  460. .sreg = reg_src1, \
  461. .treg = reg_src2, \
  462. .unused = 0, \
  463. .sel = ALU_SEL_AND, \
  464. .sub_opcode = SUB_OPCODE_ALU_REG, \
  465. .opcode = OPCODE_ALU } }
  466. /**
  467. * Logical OR: dest = src1 | src2
  468. */
  469. #define I_ORR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  470. .dreg = reg_dest, \
  471. .sreg = reg_src1, \
  472. .treg = reg_src2, \
  473. .unused = 0, \
  474. .sel = ALU_SEL_OR, \
  475. .sub_opcode = SUB_OPCODE_ALU_REG, \
  476. .opcode = OPCODE_ALU } }
  477. /**
  478. * Copy: dest = src
  479. */
  480. #define I_MOVR(reg_dest, reg_src) { .alu_reg = { \
  481. .dreg = reg_dest, \
  482. .sreg = reg_src, \
  483. .treg = 0, \
  484. .unused = 0, \
  485. .sel = ALU_SEL_MOV, \
  486. .sub_opcode = SUB_OPCODE_ALU_REG, \
  487. .opcode = OPCODE_ALU } }
  488. /**
  489. * Logical shift left: dest = src << shift
  490. */
  491. #define I_LSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  492. .dreg = reg_dest, \
  493. .sreg = reg_src, \
  494. .treg = reg_shift, \
  495. .unused = 0, \
  496. .sel = ALU_SEL_LSH, \
  497. .sub_opcode = SUB_OPCODE_ALU_REG, \
  498. .opcode = OPCODE_ALU } }
  499. /**
  500. * Logical shift right: dest = src >> shift
  501. */
  502. #define I_RSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  503. .dreg = reg_dest, \
  504. .sreg = reg_src, \
  505. .treg = reg_shift, \
  506. .unused = 0, \
  507. .sel = ALU_SEL_RSH, \
  508. .sub_opcode = SUB_OPCODE_ALU_REG, \
  509. .opcode = OPCODE_ALU } }
  510. /**
  511. * Add register and an immediate value: dest = src1 + imm
  512. */
  513. #define I_ADDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  514. .dreg = reg_dest, \
  515. .sreg = reg_src, \
  516. .imm = imm_, \
  517. .unused = 0, \
  518. .sel = ALU_SEL_ADD, \
  519. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  520. .opcode = OPCODE_ALU } }
  521. /**
  522. * Subtract register and an immediate value: dest = src - imm
  523. */
  524. #define I_SUBI(reg_dest, reg_src, imm_) { .alu_imm = { \
  525. .dreg = reg_dest, \
  526. .sreg = reg_src, \
  527. .imm = imm_, \
  528. .unused = 0, \
  529. .sel = ALU_SEL_SUB, \
  530. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  531. .opcode = OPCODE_ALU } }
  532. /**
  533. * Logical AND register and an immediate value: dest = src & imm
  534. */
  535. #define I_ANDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  536. .dreg = reg_dest, \
  537. .sreg = reg_src, \
  538. .imm = imm_, \
  539. .unused = 0, \
  540. .sel = ALU_SEL_AND, \
  541. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  542. .opcode = OPCODE_ALU } }
  543. /**
  544. * Logical OR register and an immediate value: dest = src | imm
  545. */
  546. #define I_ORI(reg_dest, reg_src, imm_) { .alu_imm = { \
  547. .dreg = reg_dest, \
  548. .sreg = reg_src, \
  549. .imm = imm_, \
  550. .unused = 0, \
  551. .sel = ALU_SEL_OR, \
  552. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  553. .opcode = OPCODE_ALU } }
  554. /**
  555. * Copy an immediate value into register: dest = imm
  556. */
  557. #define I_MOVI(reg_dest, imm_) { .alu_imm = { \
  558. .dreg = reg_dest, \
  559. .sreg = 0, \
  560. .imm = imm_, \
  561. .unused = 0, \
  562. .sel = ALU_SEL_MOV, \
  563. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  564. .opcode = OPCODE_ALU } }
  565. /**
  566. * Logical shift left register value by an immediate: dest = src << imm
  567. */
  568. #define I_LSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  569. .dreg = reg_dest, \
  570. .sreg = reg_src, \
  571. .imm = imm_, \
  572. .unused = 0, \
  573. .sel = ALU_SEL_LSH, \
  574. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  575. .opcode = OPCODE_ALU } }
  576. /**
  577. * Logical shift right register value by an immediate: dest = val >> imm
  578. */
  579. #define I_RSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  580. .dreg = reg_dest, \
  581. .sreg = reg_src, \
  582. .imm = imm_, \
  583. .unused = 0, \
  584. .sel = ALU_SEL_RSH, \
  585. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  586. .opcode = OPCODE_ALU } }
  587. /**
  588. * Define a label with number label_num.
  589. *
  590. * This is a macro which doesn't generate a real instruction.
  591. * The token generated by this macro is removed by ulp_process_macros_and_load
  592. * function. Label defined using this macro can be used in branch macros defined
  593. * below.
  594. */
  595. #define M_LABEL(label_num) { .macro = { \
  596. .label = label_num, \
  597. .unused = 0, \
  598. .sub_opcode = SUB_OPCODE_MACRO_LABEL, \
  599. .opcode = OPCODE_MACRO } }
  600. /**
  601. * Token macro used by M_B and M_BX macros. Not to be used directly.
  602. */
  603. #define M_BRANCH(label_num) { .macro = { \
  604. .label = label_num, \
  605. .unused = 0, \
  606. .sub_opcode = SUB_OPCODE_MACRO_BRANCH, \
  607. .opcode = OPCODE_MACRO } }
  608. /**
  609. * Macro: branch to label label_num if R0 is less than immediate value.
  610. *
  611. * This macro generates two ulp_insn_t values separated by a comma, and should
  612. * be used when defining contents of ulp_insn_t arrays. First value is not a
  613. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  614. * function.
  615. */
  616. #define M_BL(label_num, imm_value) \
  617. M_BRANCH(label_num), \
  618. I_BL(0, imm_value)
  619. /**
  620. * Macro: branch to label label_num if R0 is greater or equal than immediate value
  621. *
  622. * This macro generates two ulp_insn_t values separated by a comma, and should
  623. * be used when defining contents of ulp_insn_t arrays. First value is not a
  624. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  625. * function.
  626. */
  627. #define M_BGE(label_num, imm_value) \
  628. M_BRANCH(label_num), \
  629. I_BGE(0, imm_value)
  630. /**
  631. * Macro: unconditional branch to label
  632. *
  633. * This macro generates two ulp_insn_t values separated by a comma, and should
  634. * be used when defining contents of ulp_insn_t arrays. First value is not a
  635. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  636. * function.
  637. */
  638. #define M_BX(label_num) \
  639. M_BRANCH(label_num), \
  640. I_BXI(0)
  641. /**
  642. * Macro: branch to label if ALU result is zero
  643. *
  644. * This macro generates two ulp_insn_t values separated by a comma, and should
  645. * be used when defining contents of ulp_insn_t arrays. First value is not a
  646. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  647. * function.
  648. */
  649. #define M_BXZ(label_num) \
  650. M_BRANCH(label_num), \
  651. I_BXZI(0)
  652. /**
  653. * Macro: branch to label if ALU overflow
  654. *
  655. * This macro generates two ulp_insn_t values separated by a comma, and should
  656. * be used when defining contents of ulp_insn_t arrays. First value is not a
  657. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  658. * function.
  659. */
  660. #define M_BXF(label_num) \
  661. M_BRANCH(label_num), \
  662. I_BXFI(0)
  663. #define RTC_SLOW_MEM ((uint32_t*) 0x50000000) /*!< RTC slow memory, 8k size */
  664. /**
  665. * @brief Resolve all macro references in a program and load it into RTC memory
  666. * @param load_addr address where the program should be loaded, expressed in 32-bit words
  667. * @param program ulp_insn_t array with the program
  668. * @param psize size of the program, expressed in 32-bit words
  669. * @return
  670. * - ESP_OK on success
  671. * - ESP_ERR_NO_MEM if auxiliary temporary structure can not be allocated
  672. * - one of ESP_ERR_ULP_xxx if program is not valid or can not be loaded
  673. */
  674. esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* program, size_t* psize);
  675. /**
  676. * @brief Run the program loaded into RTC memory
  677. * @param entry_point entry point, expressed in 32-bit words
  678. * @return ESP_OK on success
  679. */
  680. esp_err_t ulp_run(uint32_t entry_point);
  681. #ifdef __cplusplus
  682. }
  683. #endif