SpiManager.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * @file SpiManager.h
  3. *
  4. * @date: 8. 1. 2023
  5. * @author: juraj
  6. */
  7. #ifndef _SPI_MANAGER__H_
  8. #define _SPI_MANAGER__H_
  9. #include "icm_datatypes.h"
  10. /* Defines */
  11. #define READ 0x80
  12. #define WRITE 0x00
  13. typedef enum
  14. {
  15. ub_NONE = -1,
  16. ub_0 = 0 << 4,
  17. ub_1 = 1 << 4,
  18. ub_2 = 2 << 4,
  19. ub_3 = 3 << 4
  20. } userbank;
  21. #define MAX_SLAVES 4
  22. /* ICM-20948 Registers */
  23. #define ICM20948_ID 0xEA
  24. #define REG_BANK_SEL 0x7F
  25. // USER BANK 0
  26. #define B0_WHO_AM_I 0x00
  27. #define B0_USER_CTRL 0x03
  28. #define B0_LP_CONFIG 0x05
  29. #define B0_PWR_MGMT_1 0x06
  30. #define B0_PWR_MGMT_2 0x07
  31. #define B0_INT_PIN_CFG 0x0F
  32. #define B0_INT_ENABLE 0x10
  33. #define B0_INT_ENABLE_1 0x11
  34. #define B0_INT_ENABLE_2 0x12
  35. #define B0_INT_ENABLE_3 0x13
  36. #define B0_I2C_MST_STATUS 0x17
  37. #define B0_INT_STATUS 0x19
  38. #define B0_INT_STATUS_1 0x1A
  39. #define B0_INT_STATUS_2 0x1B
  40. #define B0_INT_STATUS_3 0x1C
  41. #define B0_DELAY_TIMEH 0x28
  42. #define B0_DELAY_TIMEL 0x29
  43. #define B0_ACCEL_XOUT_H 0x2D
  44. #define B0_ACCEL_XOUT_L 0x2E
  45. #define B0_ACCEL_YOUT_H 0x2F
  46. #define B0_ACCEL_YOUT_L 0x30
  47. #define B0_ACCEL_ZOUT_H 0x31
  48. #define B0_ACCEL_ZOUT_L 0x32
  49. #define B0_GYRO_XOUT_H 0x33
  50. #define B0_GYRO_XOUT_L 0x34
  51. #define B0_GYRO_YOUT_H 0x35
  52. #define B0_GYRO_YOUT_L 0x36
  53. #define B0_GYRO_ZOUT_H 0x37
  54. #define B0_GYRO_ZOUT_L 0x38
  55. #define B0_TEMP_OUT_H 0x39
  56. #define B0_TEMP_OUT_L 0x3A
  57. #define B0_EXT_SLV_SENS_DATA_00 0x3B
  58. #define B0_EXT_SLV_SENS_DATA_01 0x3C
  59. #define B0_EXT_SLV_SENS_DATA_02 0x3D
  60. #define B0_EXT_SLV_SENS_DATA_03 0x3E
  61. #define B0_EXT_SLV_SENS_DATA_04 0x3F
  62. #define B0_EXT_SLV_SENS_DATA_05 0x40
  63. #define B0_EXT_SLV_SENS_DATA_06 0x41
  64. #define B0_EXT_SLV_SENS_DATA_07 0x42
  65. #define B0_EXT_SLV_SENS_DATA_08 0x43
  66. #define B0_EXT_SLV_SENS_DATA_09 0x44
  67. #define B0_EXT_SLV_SENS_DATA_10 0x45
  68. #define B0_EXT_SLV_SENS_DATA_11 0x46
  69. #define B0_EXT_SLV_SENS_DATA_12 0x47
  70. #define B0_EXT_SLV_SENS_DATA_13 0x48
  71. #define B0_EXT_SLV_SENS_DATA_14 0x49
  72. #define B0_EXT_SLV_SENS_DATA_15 0x4A
  73. #define B0_EXT_SLV_SENS_DATA_16 0x4B
  74. #define B0_EXT_SLV_SENS_DATA_17 0x4C
  75. #define B0_EXT_SLV_SENS_DATA_18 0x4D
  76. #define B0_EXT_SLV_SENS_DATA_19 0x4E
  77. #define B0_EXT_SLV_SENS_DATA_20 0x4F
  78. #define B0_EXT_SLV_SENS_DATA_21 0x50
  79. #define B0_EXT_SLV_SENS_DATA_22 0x51
  80. #define B0_EXT_SLV_SENS_DATA_23 0x52
  81. #define B0_FIFO_EN_1 0x66
  82. #define B0_FIFO_EN_2 0x67
  83. #define B0_FIFO_RST 0x68
  84. #define B0_FIFO_MODE 0x69
  85. #define B0_FIFO_COUNTH 0X70
  86. #define B0_FIFO_COUNTL 0X71
  87. #define B0_FIFO_R_W 0x72
  88. #define B0_DATA_RDY_STATUS 0x74
  89. #define B0_FIFO_CFG 0x76
  90. // USER BANK 1
  91. #define B1_SELF_TEST_X_GYRO 0x02
  92. #define B1_SELF_TEST_Y_GYRO 0x03
  93. #define B1_SELF_TEST_Z_GYRO 0x04
  94. #define B1_SELF_TEST_X_ACCEL 0x0E
  95. #define B1_SELF_TEST_Y_ACCEL 0x0F
  96. #define B1_SELF_TEST_Z_ACCEL 0x10
  97. #define B1_XA_OFFS_H 0x14
  98. #define B1_XA_OFFS_L 0x15
  99. #define B1_YA_OFFS_H 0x17
  100. #define B1_YA_OFFS_L 0x18
  101. #define B1_ZA_OFFS_H 0x1A
  102. #define B1_ZA_OFFS_L 0x1B
  103. #define B1_TIMEBASE_CORRECTION_PLL 0x28
  104. // USER BANK 2
  105. #define B2_GYRO_SMPLRT_DIV 0x00
  106. #define B2_GYRO_CONFIG_1 0x01
  107. #define B2_GYRO_CONFIG_2 0x02
  108. #define B2_XG_OFFS_USRH 0x03
  109. #define B2_XG_OFFS_USRL 0x04
  110. #define B2_YG_OFFS_USRH 0x05
  111. #define B2_YG_OFFS_USRL 0x06
  112. #define B2_ZG_OFFS_USRH 0x07
  113. #define B2_ZG_OFFS_USRL 0x08
  114. #define B2_ODR_ALIGN_EN 0x09
  115. #define B2_ACCEL_SMPLRT_DIV_1 0x10
  116. #define B2_ACCEL_SMPLRT_DIV_2 0x11
  117. #define B2_ACCEL_INTEL_CTRL 0x12
  118. #define B2_ACCEL_WOM_THR 0x13
  119. #define B2_ACCEL_CONFIG 0x14
  120. #define B2_ACCEL_CONFIG_2 0x15
  121. #define B2_FSYNC_CONFIG 0x52
  122. #define B2_TEMP_CONFIG 0x53
  123. #define B2_MOD_CTRL_USR 0X54
  124. // USER BANK 3
  125. #define B3_I2C_MST_ODR_CONFIG 0x00
  126. #define B3_I2C_MST_CTRL 0x01
  127. #define B3_I2C_MST_DELAY_CTRL 0x02
  128. #define B3_I2C_SLV0_ADDR 0x03
  129. #define B3_I2C_SLV0_REG 0x04
  130. #define B3_I2C_SLV0_CTRL 0x05
  131. #define B3_I2C_SLV0_DO 0x06
  132. #define B3_I2C_SLV1_ADDR 0x07
  133. #define B3_I2C_SLV1_REG 0x08
  134. #define B3_I2C_SLV1_CTRL 0x09
  135. #define B3_I2C_SLV1_DO 0x0A
  136. #define B3_I2C_SLV2_ADDR 0x0B
  137. #define B3_I2C_SLV2_REG 0x0C
  138. #define B3_I2C_SLV2_CTRL 0x0D
  139. #define B3_I2C_SLV2_DO 0x0E
  140. #define B3_I2C_SLV3_ADDR 0x0F
  141. #define B3_I2C_SLV3_REG 0x10
  142. #define B3_I2C_SLV3_CTRL 0x11
  143. #define B3_I2C_SLV3_DO 0x12
  144. #define B3_I2C_SLV4_ADDR 0x13
  145. #define B3_I2C_SLV4_REG 0x14
  146. #define B3_I2C_SLV4_CTRL 0x15
  147. #define B3_I2C_SLV4_DO 0x16
  148. #define B3_I2C_SLV4_DI 0x17
  149. /* AK09916 Registers */
  150. #define AK09916_ID 0x09
  151. #define MAG_SLAVE_ADDR 0x0C
  152. #define MAG_WIA2 0x01
  153. #define MAG_ST1 0x10
  154. #define MAG_HXL 0x11
  155. #define MAG_HXH 0x12
  156. #define MAG_HYL 0x13
  157. #define MAG_HYH 0x14
  158. #define MAG_HZL 0x15
  159. #define MAG_HZH 0x16
  160. #define MAG_ST2 0x18
  161. #define MAG_CNTL2 0x31
  162. #define MAG_CNTL3 0x32
  163. #define MAG_TS1 0x33
  164. #define MAG_TS2 0x34
  165. // B0_INT_PIN_CFG params
  166. #define INT_PIN_CFG_ACTL (1<<7) // The logic level for INT1 pin is active low.
  167. #define INT_PIN_CFG_OPEN (1<<6) // INT1 pin is configured as open drain.
  168. #define INT_PIN_CFG_LATCH (1<<5) // INT1 pin level held until interrupt status is cleared.
  169. #define INT_PIN_CFG_2CLEAR (1<<4) //Interrupt status in INT_STATUS is cleared (set to 0) if any read operation isperformed.
  170. #define INT_PIN_CFG_FSYNC (1<<3) // The logic level for the FSYNC pin as an interrupt to the ICM-20948 is active low.
  171. #define INT_PIN_CFG_FSYNC_EN (1<<2) // This enables the FSYNC pin to be used as an interrupt. A transition to the active level described by the ACTL_FSYNC bit will cause an interrupt. The status of the interrupt is read in the I2C Master Status register PASS_THROUGH bit.
  172. #define INT_PIN_CFG_BYPASS (1<<1) // When asserted, the I2C_MASTER interface pins (ES_CL and ES_DA) will go into ‘bypass mode’ when the I2C master interface is disabled.
  173. class SpiManager{
  174. private:
  175. uint8_t _numDevices;
  176. volatile int8_t _activeDevice;
  177. /** Internal buffer. Size=12B */
  178. uint8_t _reg_val[12];
  179. SPI_HandleTypeDef *_spi;
  180. McuPin_typeDef *_pinCS[MAX_SLAVES];
  181. volatile userbank _active_bank;
  182. void select_user_bank(uint8_t sensorNum, userbank ub);
  183. void cs_high(uint8_t index);
  184. void cs_low(uint8_t index);
  185. void takeBus(uint8_t index);
  186. void releaseBus();
  187. public:
  188. SpiManager(SPI_HandleTypeDef* hspi);
  189. uint8_t addSlave(McuPin_typeDef *pin);
  190. uint8_t* read_multiple_reg(uint8_t index, userbank ub, uint8_t reg, uint8_t len);
  191. uint8_t read_single_reg(uint8_t sensorNum, userbank ub, uint8_t reg);
  192. void write_single_reg(uint8_t sensorNum, userbank ub, uint8_t reg, uint8_t val);
  193. void write_multiple_reg(uint8_t sensorNum, userbank ub, uint8_t reg, uint8_t* val, uint8_t len);
  194. uint8_t read_single_external_reg(uint8_t sensorNum, uint8_t reg);
  195. void write_single_external_reg(uint8_t sensorNum, uint8_t reg, uint8_t val);
  196. uint8_t* read_multiple_external_reg(uint8_t sensorNum, uint8_t reg, uint8_t len);
  197. };
  198. #endif