ulp.h 36 KB

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