am_hal_ios.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //*****************************************************************************
  2. //
  3. // am_hal_ios.h
  4. //! @file
  5. //!
  6. //! @brief Functions for interfacing with the IO Slave module
  7. //!
  8. //! @addtogroup ios2 IO Slave (SPI/I2C)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_IOS_H
  48. #define AM_HAL_IOS_H
  49. //*****************************************************************************
  50. //
  51. //! @name Interface Configuration
  52. //! @brief Macro definitions for configuring the physical interface of the IO
  53. //! Slave
  54. //!
  55. //! These macros may be used with the am_hal_ios_config_t structure to set the
  56. //! physical parameters of the SPI/I2C slave module.
  57. //!
  58. //! @{
  59. //
  60. //*****************************************************************************
  61. #define AM_HAL_IOS_USE_SPI AM_REG_IOSLAVE_CFG_IFCSEL_SPI
  62. #define AM_HAL_IOS_SPIMODE_0 AM_REG_IOSLAVE_CFG_SPOL_SPI_MODES_0_3
  63. #define AM_HAL_IOS_SPIMODE_1 AM_REG_IOSLAVE_CFG_SPOL_SPI_MODES_1_2
  64. #define AM_HAL_IOS_SPIMODE_2 AM_REG_IOSLAVE_CFG_SPOL_SPI_MODES_1_2
  65. #define AM_HAL_IOS_SPIMODE_3 AM_REG_IOSLAVE_CFG_SPOL_SPI_MODES_0_3
  66. #define AM_HAL_IOS_USE_I2C AM_REG_IOSLAVE_CFG_IFCSEL_I2C
  67. #define AM_HAL_IOS_I2C_ADDRESS(n) AM_REG_IOSLAVE_CFG_I2CADDR(n)
  68. #define AM_HAL_IOS_LSB_FIRST AM_REG_IOSLAVE_CFG_LSB(1)
  69. //! @}
  70. //*****************************************************************************
  71. //
  72. //! @name Register Access Interrupts
  73. //! @brief Macro definitions for register access interrupts.
  74. //!
  75. //! These macros may be used with any of the
  76. //!
  77. //! @{
  78. //
  79. //*****************************************************************************
  80. #define AM_HAL_IOS_ACCESS_INT_00 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 31)
  81. #define AM_HAL_IOS_ACCESS_INT_01 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 30)
  82. #define AM_HAL_IOS_ACCESS_INT_02 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 29)
  83. #define AM_HAL_IOS_ACCESS_INT_03 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 28)
  84. #define AM_HAL_IOS_ACCESS_INT_04 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 27)
  85. #define AM_HAL_IOS_ACCESS_INT_05 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 26)
  86. #define AM_HAL_IOS_ACCESS_INT_06 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 25)
  87. #define AM_HAL_IOS_ACCESS_INT_07 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 24)
  88. #define AM_HAL_IOS_ACCESS_INT_08 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 23)
  89. #define AM_HAL_IOS_ACCESS_INT_09 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 22)
  90. #define AM_HAL_IOS_ACCESS_INT_0A AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 21)
  91. #define AM_HAL_IOS_ACCESS_INT_0B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 20)
  92. #define AM_HAL_IOS_ACCESS_INT_0C AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 19)
  93. #define AM_HAL_IOS_ACCESS_INT_0D AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 18)
  94. #define AM_HAL_IOS_ACCESS_INT_0E AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 17)
  95. #define AM_HAL_IOS_ACCESS_INT_0F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 16)
  96. #define AM_HAL_IOS_ACCESS_INT_13 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 15)
  97. #define AM_HAL_IOS_ACCESS_INT_17 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 14)
  98. #define AM_HAL_IOS_ACCESS_INT_1B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 13)
  99. #define AM_HAL_IOS_ACCESS_INT_1F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 12)
  100. #define AM_HAL_IOS_ACCESS_INT_23 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 11)
  101. #define AM_HAL_IOS_ACCESS_INT_27 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 10)
  102. #define AM_HAL_IOS_ACCESS_INT_2B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 9)
  103. #define AM_HAL_IOS_ACCESS_INT_2F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 8)
  104. #define AM_HAL_IOS_ACCESS_INT_33 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 7)
  105. #define AM_HAL_IOS_ACCESS_INT_37 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 6)
  106. #define AM_HAL_IOS_ACCESS_INT_3B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 5)
  107. #define AM_HAL_IOS_ACCESS_INT_3F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 4)
  108. #define AM_HAL_IOS_ACCESS_INT_43 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 3)
  109. #define AM_HAL_IOS_ACCESS_INT_47 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 2)
  110. #define AM_HAL_IOS_ACCESS_INT_4B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 1)
  111. #define AM_HAL_IOS_ACCESS_INT_4F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 0)
  112. #define AM_HAL_IOS_ACCESS_INT_ALL 0xFFFFFFFF
  113. //! @}
  114. //*****************************************************************************
  115. //
  116. //! @name I/O Slave Interrupts
  117. //! @brief Macro definitions for I/O slave (IOS) interrupts.
  118. //!
  119. //! These macros may be used with any of the
  120. //!
  121. //! @{
  122. //
  123. //*****************************************************************************
  124. #define AM_HAL_IOS_INT_FSIZE AM_REG_IOSLAVE_INTEN_FSIZE_M
  125. #define AM_HAL_IOS_INT_FOVFL AM_REG_IOSLAVE_INTEN_FOVFL_M
  126. #define AM_HAL_IOS_INT_FUNDFL AM_REG_IOSLAVE_INTEN_FUNDFL_M
  127. #define AM_HAL_IOS_INT_FRDERR AM_REG_IOSLAVE_INTEN_FRDERR_M
  128. #define AM_HAL_IOS_INT_GENAD AM_REG_IOSLAVE_INTEN_GENAD_M
  129. #define AM_HAL_IOS_INT_IOINTW AM_REG_IOSLAVE_INTEN_IOINTW_M
  130. #define AM_HAL_IOS_INT_XCMPWR AM_REG_IOSLAVE_INTEN_XCMPWR_M
  131. #define AM_HAL_IOS_INT_XCMPWF AM_REG_IOSLAVE_INTEN_XCMPWF_M
  132. #define AM_HAL_IOS_INT_XCMPRR AM_REG_IOSLAVE_INTEN_XCMPRR_M
  133. #define AM_HAL_IOS_INT_XCMPRF AM_REG_IOSLAVE_INTEN_XCMPRF_M
  134. #define AM_HAL_IOS_INT_ALL 0xFFFFFFFF
  135. //! @}
  136. //*****************************************************************************
  137. //
  138. //! @name I/O Slave Interrupts triggers
  139. //! @brief Macro definitions for I/O slave (IOS) interrupts.
  140. //!
  141. //! These macros may be used with am_hal_ios_int_set and am_hal_ios_int_clear
  142. //!
  143. //! @{
  144. //
  145. //*****************************************************************************
  146. #define AM_HAL_IOS_IOINTCTL_INT0 (0x01)
  147. #define AM_HAL_IOS_IOINTCTL_INT1 (0x02)
  148. #define AM_HAL_IOS_IOINTCTL_INT2 (0x04)
  149. #define AM_HAL_IOS_IOINTCTL_INT3 (0x08)
  150. #define AM_HAL_IOS_IOINTCTL_INT4 (0x10)
  151. #define AM_HAL_IOS_IOINTCTL_INT5 (0x20)
  152. //! @}
  153. //*****************************************************************************
  154. //
  155. // External variable definitions
  156. //
  157. //*****************************************************************************
  158. //*****************************************************************************
  159. //
  160. //! @brief LRAM pointer
  161. //!
  162. //! Pointer to the base of the IO Slave LRAM.
  163. //
  164. //*****************************************************************************
  165. extern volatile uint8_t * const am_hal_ios_pui8LRAM;
  166. //*****************************************************************************
  167. //
  168. //! @brief Configuration structure for the IO slave module.
  169. //!
  170. //! This structure may be used along with the am_hal_ios_config() function to
  171. //! select key parameters of the IO Slave module. See the descriptions of each
  172. //! parameter within this structure for more information on what they control.
  173. //
  174. //*****************************************************************************
  175. typedef struct
  176. {
  177. //
  178. //! Interface Selection
  179. //!
  180. //! This word selects the physical behavior of the IO Slave port. For SPI
  181. //! mode, this word should be the logical OR of one or more of the
  182. //! following:
  183. //!
  184. //! AM_HAL_IOS_USE_SPI
  185. //! AM_HAL_IOS_SPIMODE_0
  186. //! AM_HAL_IOS_SPIMODE_1
  187. //! AM_HAL_IOS_SPIMODE_2
  188. //! AM_HAL_IOS_SPIMODE_3
  189. //!
  190. //! For I2C mode, use the logical OR of one or more of these values instead
  191. //! (where n is the 7 or 10-bit I2C address to use):
  192. //!
  193. //! AM_HAL_IOS_USE_I2C
  194. //! AM_HAL_IOS_I2C_ADDRESS(n)
  195. //!
  196. //! Also, in any mode, you may OR in this value to reverse the order of
  197. //! incoming data bits.
  198. //!
  199. //! AM_HAL_IOS_LSB_FIRST
  200. //
  201. uint32_t ui32InterfaceSelect;
  202. //
  203. //! Read-Only section
  204. //!
  205. //! The IO Slave LRAM is split into three main sections. The first section
  206. //! is a "Direct Write" section, which may be accessed for reads or write
  207. //! either directly through the Apollo CPU, or over the SPI/I2C bus. The
  208. //! "Direct Write" section always begins at LRAM offset 0x0. At the end of
  209. //! the normal "Direct Write" space, there is a "Read Only" space, which is
  210. //! read/write accessible to the Apollo CPU, but read-only over the I2C/SPI
  211. //! Bus. This word selects the base address of this "Read Only" space.
  212. //!
  213. //! This value may be set to any multiple of 8 between 0x0 and 0x78,
  214. //! inclusive. For the configuration to be valid, \e ui32ROBase must also
  215. //! be less than or equal to \e ui32FIFOBase
  216. //!
  217. //! @note The address given here is in units of BYTES. Since the location
  218. //! of the "Read Only" space may only be set in 8-byte increments, this
  219. //! value must be a multiple of 8.
  220. //!
  221. //! For the avoidance of doubt this means 0x80 is 128 bytes. These functions
  222. //! will shift right by 8 internally.
  223. //
  224. uint32_t ui32ROBase;
  225. //
  226. //! FIFO section
  227. //!
  228. //! After the "Direct Access" and "Read Only" sections is a section of LRAM
  229. //! allocated to a FIFO. This section is accessible by the Apollo CPU
  230. //! through the FIFO control registers, and accessible on the SPI/I2C bus
  231. //! through the 0x7F address. This word selects the base address of the
  232. //! FIFO space. The FIFO will extend from the address specified here to the
  233. //! address specified in \e ui32RAMBase.
  234. //!
  235. //! This value may be set to any multiple of 8 between 0x0 and 0x78,
  236. //! inclusive. For the configuration to be valid, \e ui32FIFOBase must also
  237. //! be greater than or equal to \e ui32ROBase.
  238. //!
  239. //! @note The address given here is in units of BYTES. Since the location
  240. //! of the "FIFO" space may only be set in 8-byte increments, this value
  241. //! must be a multiple of 8.
  242. //!
  243. //! For the avoidance of doubt this means 0x80 is 128 bytes. These functions
  244. //! will shift right by 8 internally.
  245. //
  246. uint32_t ui32FIFOBase;
  247. //
  248. //! RAM section
  249. //!
  250. //! At the end of the IOS LRAM, the user may allocate a "RAM" space that
  251. //! can only be accessed by the Apollo CPU. This space will not interact
  252. //! with the SPI/I2C bus at all, and may be used as general-purpose memory.
  253. //! Unlike normal SRAM, this section of LRAM will retain its state through
  254. //! Deep Sleep, so it may be used as a data retention space for
  255. //! ultra-low-power applications.
  256. //!
  257. //! This value may be set to any multiple of 8 between 0x0 and 0x100,
  258. //! inclusive. For the configuration to be valid, \e ui32RAMBase must also
  259. //! be greater than or equal to \e ui32FIFOBase.
  260. //!
  261. //! @note The address given here is in units of BYTES. Since the location
  262. //! of the "FIFO" space may only be set in 8-byte increments, this value
  263. //! must be a multiple of 8.
  264. //!
  265. //! For the avoidance of doubt this means 0x80 is 128 bytes. These functions
  266. //! will shift right by 8 internally.
  267. //
  268. uint32_t ui32RAMBase;
  269. //
  270. //! FIFO threshold
  271. //!
  272. //! The IO Slave module will trigger an interrupt when the number of
  273. //! entries in the FIFO drops below this number of bytes.
  274. //
  275. uint32_t ui32FIFOThreshold;
  276. //
  277. // Pointer to an SRAM
  278. //
  279. uint8_t *pui8SRAMBuffer;
  280. }
  281. am_hal_ios_config_t;
  282. #ifdef __cplusplus
  283. extern "C"
  284. {
  285. #endif
  286. //*****************************************************************************
  287. //
  288. // External function definitions
  289. //
  290. //*****************************************************************************
  291. extern void am_hal_ios_enable(uint32_t ui32Module);
  292. extern void am_hal_ios_disable(uint32_t ui32Module);
  293. // these interrupts drive the HOST side IOS interrupt pins
  294. extern void am_hal_ios_host_int_set(uint32_t ui32Interrupt);
  295. extern void am_hal_ios_host_int_clear(uint32_t ui32Interrupt);
  296. extern uint32_t am_hal_ios_host_int_get(void);
  297. extern uint32_t am_hal_ios_host_int_enable_get(void);
  298. extern void am_hal_ios_lram_write(uint32_t ui32Offset, uint8_t ui8Value);
  299. extern uint8_t am_hal_ios_lram_read(uint32_t ui32Offset);
  300. // the following interrupts go back to the NVIC
  301. extern void am_hal_ios_config(am_hal_ios_config_t *psConfig);
  302. extern void am_hal_ios_access_int_enable(uint32_t ui32Interrupt);
  303. extern uint32_t am_hal_ios_access_int_enable_get(void);
  304. extern void am_hal_ios_access_int_disable(uint32_t ui32Interrupt);
  305. extern void am_hal_ios_access_int_clear(uint32_t ui32Interrupt);
  306. extern void am_hal_ios_access_int_set(uint32_t ui32Interrupt);
  307. extern uint32_t am_hal_ios_access_int_status_get(bool bEnabledOnly);
  308. extern void am_hal_ios_int_enable(uint32_t ui32Interrupt);
  309. extern uint32_t am_hal_ios_int_enable_get(void);
  310. extern void am_hal_ios_int_disable(uint32_t ui32Interrupt);
  311. extern void am_hal_ios_int_clear(uint32_t ui32Interrupt);
  312. extern void am_hal_ios_int_set(uint32_t ui32Interrupt);
  313. extern uint32_t am_hal_ios_int_status_get(bool bEnabledOnly);
  314. extern void am_hal_ios_fifo_buffer_init(uint8_t *pui8Buffer, uint32_t ui32NumBytes);
  315. extern uint32_t am_hal_ios_fifo_space_left(void);
  316. extern uint32_t am_hal_ios_fifo_space_used(void);
  317. extern void am_hal_ios_fifo_service(uint32_t ui32Status);
  318. // Returns the number of bytes actually written
  319. extern uint32_t am_hal_ios_fifo_write(uint8_t *pui8Data, uint32_t ui32NumBytes);
  320. extern void am_hal_ios_fifo_write_simple(uint8_t *pui8Data,
  321. uint32_t ui32NumBytes);
  322. extern void am_hal_ios_fifo_ptr_set(uint32_t ui32Offset);
  323. extern void am_hal_ios_update_fifoctr(void);
  324. extern void am_hal_ios_read_poll_complete(void);
  325. extern void am_hal_ios_pwrctrl_enable(void);
  326. extern void am_hal_ios_pwrctrl_disable(void);
  327. #ifdef __cplusplus
  328. }
  329. #endif
  330. #endif // AM_HAL_IOS_H
  331. //*****************************************************************************
  332. //
  333. // End Doxygen group.
  334. //! @}
  335. //
  336. //*****************************************************************************