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.9 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_IOS_H
  48. #define AM_HAL_IOS_H
  49. #ifdef __cplusplus
  50. extern "C"
  51. {
  52. #endif
  53. //*****************************************************************************
  54. //
  55. //! @name Interface Configuration
  56. //! @brief Macro definitions for configuring the physical interface of the IO
  57. //! Slave
  58. //!
  59. //! These macros may be used with the am_hal_ios_config_t structure to set the
  60. //! physical parameters of the SPI/I2C slave module.
  61. //!
  62. //! @{
  63. //
  64. //*****************************************************************************
  65. #define AM_HAL_IOS_USE_SPI AM_REG_IOSLAVE_CFG_IFCSEL_SPI
  66. #define AM_HAL_IOS_SPIMODE_0 AM_REG_IOSLAVE_CFG_SPOL_SPI_MODES_0_3
  67. #define AM_HAL_IOS_SPIMODE_1 AM_REG_IOSLAVE_CFG_SPOL_SPI_MODES_1_2
  68. #define AM_HAL_IOS_SPIMODE_2 AM_REG_IOSLAVE_CFG_SPOL_SPI_MODES_1_2
  69. #define AM_HAL_IOS_SPIMODE_3 AM_REG_IOSLAVE_CFG_SPOL_SPI_MODES_0_3
  70. #define AM_HAL_IOS_USE_I2C AM_REG_IOSLAVE_CFG_IFCSEL_I2C
  71. #define AM_HAL_IOS_I2C_ADDRESS(n) AM_REG_IOSLAVE_CFG_I2CADDR(n)
  72. #define AM_HAL_IOS_LSB_FIRST AM_REG_IOSLAVE_CFG_LSB(1)
  73. //! @}
  74. //*****************************************************************************
  75. //
  76. //! @name Register Access Interrupts
  77. //! @brief Macro definitions for register access interrupts.
  78. //!
  79. //! These macros may be used with any of the
  80. //!
  81. //! @{
  82. //
  83. //*****************************************************************************
  84. #define AM_HAL_IOS_ACCESS_INT_00 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 31)
  85. #define AM_HAL_IOS_ACCESS_INT_01 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 30)
  86. #define AM_HAL_IOS_ACCESS_INT_02 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 29)
  87. #define AM_HAL_IOS_ACCESS_INT_03 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 28)
  88. #define AM_HAL_IOS_ACCESS_INT_04 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 27)
  89. #define AM_HAL_IOS_ACCESS_INT_05 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 26)
  90. #define AM_HAL_IOS_ACCESS_INT_06 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 25)
  91. #define AM_HAL_IOS_ACCESS_INT_07 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 24)
  92. #define AM_HAL_IOS_ACCESS_INT_08 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 23)
  93. #define AM_HAL_IOS_ACCESS_INT_09 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 22)
  94. #define AM_HAL_IOS_ACCESS_INT_0A AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 21)
  95. #define AM_HAL_IOS_ACCESS_INT_0B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 20)
  96. #define AM_HAL_IOS_ACCESS_INT_0C AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 19)
  97. #define AM_HAL_IOS_ACCESS_INT_0D AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 18)
  98. #define AM_HAL_IOS_ACCESS_INT_0E AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 17)
  99. #define AM_HAL_IOS_ACCESS_INT_0F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 16)
  100. #define AM_HAL_IOS_ACCESS_INT_13 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 15)
  101. #define AM_HAL_IOS_ACCESS_INT_17 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 14)
  102. #define AM_HAL_IOS_ACCESS_INT_1B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 13)
  103. #define AM_HAL_IOS_ACCESS_INT_1F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 12)
  104. #define AM_HAL_IOS_ACCESS_INT_23 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 11)
  105. #define AM_HAL_IOS_ACCESS_INT_27 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 10)
  106. #define AM_HAL_IOS_ACCESS_INT_2B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 9)
  107. #define AM_HAL_IOS_ACCESS_INT_2F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 8)
  108. #define AM_HAL_IOS_ACCESS_INT_33 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 7)
  109. #define AM_HAL_IOS_ACCESS_INT_37 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 6)
  110. #define AM_HAL_IOS_ACCESS_INT_3B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 5)
  111. #define AM_HAL_IOS_ACCESS_INT_3F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 4)
  112. #define AM_HAL_IOS_ACCESS_INT_43 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 3)
  113. #define AM_HAL_IOS_ACCESS_INT_47 AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 2)
  114. #define AM_HAL_IOS_ACCESS_INT_4B AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 1)
  115. #define AM_HAL_IOS_ACCESS_INT_4F AM_REG_IOSLAVE_REGACCINTEN_REGACC((uint32_t)1 << 0)
  116. #define AM_HAL_IOS_ACCESS_INT_ALL 0xFFFFFFFF
  117. //! @}
  118. //*****************************************************************************
  119. //
  120. //! @name I/O Slave Interrupts
  121. //! @brief Macro definitions for I/O slave (IOS) interrupts.
  122. //!
  123. //! These macros may be used with any of the
  124. //!
  125. //! @{
  126. //
  127. //*****************************************************************************
  128. #define AM_HAL_IOS_INT_FSIZE AM_REG_IOSLAVE_INTEN_FSIZE_M
  129. #define AM_HAL_IOS_INT_FOVFL AM_REG_IOSLAVE_INTEN_FOVFL_M
  130. #define AM_HAL_IOS_INT_FUNDFL AM_REG_IOSLAVE_INTEN_FUNDFL_M
  131. #define AM_HAL_IOS_INT_FRDERR AM_REG_IOSLAVE_INTEN_FRDERR_M
  132. #define AM_HAL_IOS_INT_GENAD AM_REG_IOSLAVE_INTEN_GENAD_M
  133. #define AM_HAL_IOS_INT_IOINTW AM_REG_IOSLAVE_INTEN_IOINTW_M
  134. #define AM_HAL_IOS_INT_XCMPWR AM_REG_IOSLAVE_INTEN_XCMPWR_M
  135. #define AM_HAL_IOS_INT_XCMPWF AM_REG_IOSLAVE_INTEN_XCMPWF_M
  136. #define AM_HAL_IOS_INT_XCMPRR AM_REG_IOSLAVE_INTEN_XCMPRR_M
  137. #define AM_HAL_IOS_INT_XCMPRF AM_REG_IOSLAVE_INTEN_XCMPRF_M
  138. #define AM_HAL_IOS_INT_ALL 0xFFFFFFFF
  139. //! @}
  140. //*****************************************************************************
  141. //
  142. //! @name I/O Slave Interrupts triggers
  143. //! @brief Macro definitions for I/O slave (IOS) interrupts.
  144. //!
  145. //! These macros may be used with am_hal_ios_int_set and am_hal_ios_int_clear
  146. //!
  147. //! @{
  148. //
  149. //*****************************************************************************
  150. #define AM_HAL_IOS_IOINTCTL_INT0 (0x01)
  151. #define AM_HAL_IOS_IOINTCTL_INT1 (0x02)
  152. #define AM_HAL_IOS_IOINTCTL_INT2 (0x04)
  153. #define AM_HAL_IOS_IOINTCTL_INT3 (0x08)
  154. #define AM_HAL_IOS_IOINTCTL_INT4 (0x10)
  155. #define AM_HAL_IOS_IOINTCTL_INT5 (0x20)
  156. //! @}
  157. //*****************************************************************************
  158. //
  159. // External variable definitions
  160. //
  161. //*****************************************************************************
  162. //*****************************************************************************
  163. //
  164. //! @brief LRAM pointer
  165. //!
  166. //! Pointer to the base of the IO Slave LRAM.
  167. //
  168. //*****************************************************************************
  169. extern volatile uint8_t * const am_hal_ios_pui8LRAM;
  170. //*****************************************************************************
  171. //
  172. //! @brief Configuration structure for the IO slave module.
  173. //!
  174. //! This structure may be used along with the am_hal_ios_config() function to
  175. //! select key parameters of the IO Slave module. See the descriptions of each
  176. //! parameter within this structure for more information on what they control.
  177. //
  178. //*****************************************************************************
  179. typedef struct
  180. {
  181. //
  182. //! Interface Selection
  183. //!
  184. //! This word selects the physical behavior of the IO Slave port. For SPI
  185. //! mode, this word should be the logical OR of one or more of the
  186. //! following:
  187. //!
  188. //! AM_HAL_IOS_USE_SPI
  189. //! AM_HAL_IOS_SPIMODE_0
  190. //! AM_HAL_IOS_SPIMODE_1
  191. //! AM_HAL_IOS_SPIMODE_2
  192. //! AM_HAL_IOS_SPIMODE_3
  193. //!
  194. //! For I2C mode, use the logical OR of one or more of these values instead
  195. //! (where n is the 7 or 10-bit I2C address to use):
  196. //!
  197. //! AM_HAL_IOS_USE_I2C
  198. //! AM_HAL_IOS_I2C_ADDRESS(n)
  199. //!
  200. //! Also, in any mode, you may OR in this value to reverse the order of
  201. //! incoming data bits.
  202. //!
  203. //! AM_HAL_IOS_LSB_FIRST
  204. //
  205. uint32_t ui32InterfaceSelect;
  206. //
  207. //! Read-Only section
  208. //!
  209. //! The IO Slave LRAM is split into three main sections. The first section
  210. //! is a "Direct Write" section, which may be accessed for reads or write
  211. //! either directly through the Apollo CPU, or over the SPI/I2C bus. The
  212. //! "Direct Write" section always begins at LRAM offset 0x0. At the end of
  213. //! the normal "Direct Write" space, there is a "Read Only" space, which is
  214. //! read/write accessible to the Apollo CPU, but read-only over the I2C/SPI
  215. //! Bus. This word selects the base address of this "Read Only" space.
  216. //!
  217. //! This value may be set to any multiple of 8 between 0x0 and 0x78,
  218. //! inclusive. For the configuration to be valid, \e ui32ROBase must also
  219. //! be less than or equal to \e ui32FIFOBase
  220. //!
  221. //! @note The address given here is in units of BYTES. Since the location
  222. //! of the "Read Only" space may only be set in 8-byte increments, this
  223. //! value must be a multiple of 8.
  224. //!
  225. //! For the avoidance of doubt this means 0x80 is 128 bytes. These functions
  226. //! will shift right by 8 internally.
  227. //
  228. uint32_t ui32ROBase;
  229. //
  230. //! FIFO section
  231. //!
  232. //! After the "Direct Access" and "Read Only" sections is a section of LRAM
  233. //! allocated to a FIFO. This section is accessible by the Apollo CPU
  234. //! through the FIFO control registers, and accessible on the SPI/I2C bus
  235. //! through the 0x7F address. This word selects the base address of the
  236. //! FIFO space. The FIFO will extend from the address specified here to the
  237. //! address specified in \e ui32RAMBase.
  238. //!
  239. //! This value may be set to any multiple of 8 between 0x0 and 0x78,
  240. //! inclusive. For the configuration to be valid, \e ui32FIFOBase must also
  241. //! be greater than or equal to \e ui32ROBase.
  242. //!
  243. //! @note The address given here is in units of BYTES. Since the location
  244. //! of the "FIFO" space may only be set in 8-byte increments, this value
  245. //! must be a multiple of 8.
  246. //!
  247. //! For the avoidance of doubt this means 0x80 is 128 bytes. These functions
  248. //! will shift right by 8 internally.
  249. //
  250. uint32_t ui32FIFOBase;
  251. //
  252. //! RAM section
  253. //!
  254. //! At the end of the IOS LRAM, the user may allocate a "RAM" space that
  255. //! can only be accessed by the Apollo CPU. This space will not interact
  256. //! with the SPI/I2C bus at all, and may be used as general-purpose memory.
  257. //! Unlike normal SRAM, this section of LRAM will retain its state through
  258. //! Deep Sleep, so it may be used as a data retention space for
  259. //! ultra-low-power applications.
  260. //!
  261. //! This value may be set to any multiple of 8 between 0x0 and 0x100,
  262. //! inclusive. For the configuration to be valid, \e ui32RAMBase must also
  263. //! be greater than or equal to \e ui32FIFOBase.
  264. //!
  265. //! @note The address given here is in units of BYTES. Since the location
  266. //! of the "FIFO" space may only be set in 8-byte increments, this value
  267. //! must be a multiple of 8.
  268. //!
  269. //! For the avoidance of doubt this means 0x80 is 128 bytes. These functions
  270. //! will shift right by 8 internally.
  271. //
  272. uint32_t ui32RAMBase;
  273. //
  274. //! FIFO threshold
  275. //!
  276. //! The IO Slave module will trigger an interrupt when the number of
  277. //! entries in the FIFO drops below this number of bytes.
  278. //
  279. uint32_t ui32FIFOThreshold;
  280. //
  281. // Pointer to an SRAM
  282. //
  283. uint8_t *pui8SRAMBuffer;
  284. }
  285. am_hal_ios_config_t;
  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. //*****************************************************************************