gpio.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file gpio.h
  30. * @brief Contains all functions support for gpio and iomux driver
  31. * @version 0.0
  32. * @date 19. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #ifndef __GPIO_H__
  40. #define __GPIO_H__
  41. #include <types.h>
  42. #include "ap_cp.h"
  43. #define NUMBER_OF_PINS 35
  44. #define NUMBER_OF_IRQ_PINS 18
  45. #define gpio_write_reg(addr,data) (*(volatile unsigned long *)(addr)=data)
  46. #define gpio_read_reg(addr) (*(volatile unsigned long *)(addr))
  47. #define reg_gpio_ioe_porta (volatile unsigned int *)0x40008004
  48. #define reg_gpio_ioe_portb (volatile unsigned int *)0x40008010
  49. #define reg_gpio_swporta_dr (volatile unsigned int *)0x40008000
  50. #define reg_gpio_swportb_dr (volatile unsigned int *)0x4000800c
  51. #define reg_gpio_ext_porta (volatile unsigned int *)0x40008050
  52. #define reg_gpio_ext_portb (volatile unsigned int *)0x40008054
  53. #define FMUX_EN_ADD_BASE (AP_IOMUX_BASE+full_mux_en)
  54. #define DMUX_EN_ADD_BASE (AP_IOMUX_BASE+debug_mux_en)
  55. #define FMUX_FUNIO_ADD_BASE (AP_IOMUX_BASE+gpio_base_sel)
  56. #define IO_PULL_ADD_BASE 0x4000f008
  57. #define IO_WAKEUP_ADD_BASE 0x4000f0a0
  58. #define PAD_PE_ADD_BASE (AP_IOMUX_BASE+pad_pe)
  59. #define PAD_DS_ADD_BASE (AP_IOMUX_BASE+pad_ds)
  60. #define FMUX_FUNIO_SELECT(Pin,Type) gpio_write_reg(FMUX_FUNIO_ADD_BASE+((Pin >>2) << 2),((gpio_read_reg(FMUX_FUNIO_ADD_BASE+((Pin >> 2) << 2)))& (~(0xFF << ((Pin & 0x03) << 3))))|(Type << ((Pin & 0x03) << 3)))
  61. //#define reg_gpio_oen 0x40008004
  62. //#define GPIO_REG_ADDR_8(addr) (volatile unsigned char *)(GPIO_REG_BASE+addr)
  63. //AP_IOMUX->full_mux0_en |= BIT(pin);
  64. #define REG_FMUX_EN_FUC(i) (volatile unsigned int *)(FMUX_EN_ADD_BASE + ((i >> 5) << 2))
  65. #define REG_DMUX_EN_FUC *(volatile unsigned int *)(DMUX_EN_ADD_BASE)
  66. #define REG_IO_WAKEUP_EN(i) (volatile unsigned int *)(IO_WAKEUP_ADD_BASE + ((i >> 5) << 2))
  67. #define REG_IO_Wakeuo_Pol(i) (volatile unsigned int *)(IO_PULL_ADD_BASE + ((i/10) << 2))
  68. #define REG_ANALOG_IO (volatile unsigned int *)(AP_IOMUX_BASE)
  69. #define REG_IOPULL_IO(i) (volatile unsigned int *)(IO_PULL_ADD_BASE + ((i/10) << 2))
  70. #define REG_PAD_EN(i) (volatile unsigned int *)(PAD_PE_ADD_BASE + ((i >> 5) << 2))
  71. #define REG_PAD_DS(i) (volatile unsigned int *)(PAD_DS_ADD_BASE + ((i >> 5) << 2))
  72. #define REG_SWPORT_DR(i) (volatile unsigned int *)(AP_GPIOA_BASE + ((i >> 5) << 2)*3)
  73. typedef enum{
  74. IE, //input
  75. OEN, //output
  76. }GPIO_ioe;
  77. typedef enum{
  78. GPIO_P00 = 0, P0 = 0,
  79. GPIO_P01 = 1, P1 = 1,
  80. GPIO_P02 = 2, P2 = 2,
  81. GPIO_P03 = 3, P3 = 3,
  82. GPIO_P04 = 4, P4 = 4,
  83. GPIO_P05 = 5, P5 = 5,
  84. GPIO_P06 = 6, P6 = 6,
  85. GPIO_P07 = 7, P7 = 7,
  86. TEST_MODE = 8, P8 = 8,
  87. GPIO_P09 = 9, P9 = 9,
  88. GPIO_P10 = 10, P10 = 10,
  89. GPIO_P11 = 11, P11 = 11, Analog_IO_0 = 11,
  90. GPIO_P12 = 12, P12 = 12, Analog_IO_1 = 12,
  91. GPIO_P13 = 13, P13 = 13, Analog_IO_2 = 13,
  92. GPIO_P14 = 14, P14 = 14, Analog_IO_3 = 14,
  93. GPIO_P15 = 15, P15 = 15, Analog_IO_4 = 15,
  94. GPIO_P16 = 16, P16 = 16, XTALI = 16,
  95. GPIO_P17 = 17, P17 = 17, XTALO = 17,
  96. GPIO_P18 = 18, P18 = 18, Analog_IO_7 = 18,
  97. GPIO_P19 = 19, P19 = 19, Analog_IO_8 = 19,
  98. GPIO_P20 = 20, P20 = 20, Analog_IO_9 = 20,
  99. GPIO_P21 = 21, P21 = 21,
  100. GPIO_P22 = 22, P22 = 22,
  101. GPIO_P23 = 23, P23 = 23,
  102. GPIO_P24 = 24, P24 = 24,
  103. GPIO_P25 = 25, P25 = 25,
  104. GPIO_P26 = 26, P26 = 26,
  105. GPIO_P27 = 27, P27 = 27,
  106. GPIO_P28 = 28, P28 = 28,
  107. GPIO_P29 = 29, P29 = 29,
  108. GPIO_P30 = 30, P30 = 30,
  109. GPIO_P31 = 31, P31 = 31,
  110. GPIO_P32 = 32, P32 = 32,
  111. GPIO_P33 = 33, P33 = 33,
  112. GPIO_P34 = 34, P34 = 34,
  113. GPIO_DUMMY = 0xff,
  114. }GPIO_Pin_e;
  115. typedef enum{
  116. Bit_DISABLE = 0,
  117. Bit_ENABLE,
  118. }BitAction_e;
  119. typedef enum{
  120. FLOATING = 0x00,// 悬空
  121. WEAK_PULL_UP = 0x01,// 弱上拉
  122. STRONG_PULL_UP = 0x02,// 强上拉
  123. PULL_DOWN = 0x03,// 下拉
  124. }IO_Pull_Type_e;
  125. typedef enum{
  126. POSEDGE,
  127. NEGEDGE
  128. }IO_Wakeup_Pol_e;
  129. typedef enum {
  130. IIC0_SCL = 0,
  131. IIC0_SDA = 1,
  132. IIC1_SCL = 2,
  133. IIC1_SDA = 3,
  134. I2S_SCK = 4,
  135. I2S_WS = 5,
  136. I2S_SDO_0 = 6,
  137. I2S_SDI_0 = 7,
  138. UART_TX = 8,
  139. UART_RX = 9,
  140. PWM0,
  141. PWM1,
  142. PWM2,
  143. PWM3,
  144. PWM4,
  145. PWM5,
  146. SPI_0_SCK,
  147. SPI_0_SSN,
  148. SPI_0_TX,
  149. SPI_0_RX,
  150. SPI_1_SCK,
  151. SPI_1_SSN,
  152. SPI_1_TX,
  153. SPI_1_RX,
  154. CHAX ,
  155. CHBX,
  156. CHIX,
  157. CHAY ,
  158. CHBY,
  159. CHIY,
  160. CHAZ,
  161. CHBZ,
  162. CHIZ,
  163. CLK1P28M,
  164. ADCC,
  165. I2S_SDO_1 = 35,
  166. I2S_SDO_2 = 36,
  167. I2S_SDO_3 = 37,
  168. I2S_SDI_1 = 38,
  169. I2S_SDI_2 = 39,
  170. I2S_SDI_3 = 40
  171. }Fmux_Type_e;
  172. #define Analog_IO_en 0x00
  173. #define debug_mux_en 0x08
  174. #define full_mux_en 0x0c // 0xc
  175. #define gpio_pad_en 0x14
  176. #define gpio_base_sel 0x18
  177. #define pad_pe 0x3c
  178. #define pad_ds 0x44
  179. typedef void (*gpioin_Hdl_t)(GPIO_Pin_e pin,IO_Wakeup_Pol_e type);
  180. bool hal_gpio_read(GPIO_Pin_e pin);
  181. int hal_gpio_write(GPIO_Pin_e pin, uint8_t en);
  182. void hal_gpio_fast_write(GPIO_Pin_e pin, uint8_t en);
  183. int hal_gpio_toggle(GPIO_Pin_e pin);
  184. int hal_gpio_pin_init(GPIO_Pin_e pin,GPIO_ioe type);
  185. int hal_gpio_cfg_analog_io(GPIO_Pin_e pin, BitAction_e value);
  186. int gpio_pin0to3_pin31to34_control(GPIO_Pin_e pin, uint8_t en);
  187. int hal_gpio_DS_control(GPIO_Pin_e pin, BitAction_e value);
  188. int hal_gpio_fmux(GPIO_Pin_e pin, BitAction_e value);
  189. int hal_gpio_fmux_set(GPIO_Pin_e pin,Fmux_Type_e type);
  190. int hal_gpio_pull_set(GPIO_Pin_e pin,IO_Pull_Type_e type);
  191. int hal_gpio_wakeup_set(GPIO_Pin_e pin,IO_Wakeup_Pol_e type);
  192. int hal_gpioin_enable(GPIO_Pin_e pin);
  193. int hal_gpioin_disable(GPIO_Pin_e pin);
  194. int hal_gpioin_register(GPIO_Pin_e pin, gpioin_Hdl_t posedgeHdl, gpioin_Hdl_t negedgeHdl);
  195. int hal_gpioin_unregister(GPIO_Pin_e pin);
  196. int hal_gpio_init(void);
  197. void __attribute__((weak)) hal_GPIO_IRQHandler(void);
  198. void hal_gpio_p00_to_hclk_div8_enable(void);
  199. void hal_gpio_p00_to_hclk_div8_disable(void);
  200. void hal_gpio_p01_to_pclk_div4_enable(void);
  201. void hal_gpio_p01_to_pclk_div4_disable(void);
  202. void hal_gpio_p24_to_rc32k_enable(void);
  203. void hal_gpio_p24_to_rc32k_disable(void);
  204. void hal_gpio_p25_to_xtal_clk32k_enable(void);
  205. void hal_gpio_p25_to_xtal_clk32k_disable(void);
  206. #endif