dw_gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file dw_gpio.c
  18. * @brief CSI Source File for GPIO Driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #include <stdbool.h>
  23. #include <stdio.h>
  24. #include "csi_core.h"
  25. #include "drv_gpio.h"
  26. #include "dw_gpio.h"
  27. #define ERR_GPIO(errno) (CSI_DRV_ERRNO_GPIO_BASE | errno)
  28. #define GPIO_NULL_PARAM_CHK(para) \
  29. do { \
  30. if (para == NULL) { \
  31. return ERR_GPIO(EDRV_PARAMETER); \
  32. } \
  33. } while (0)
  34. typedef struct {
  35. uint32_t base; ///< handle register base
  36. uint32_t irq; ///< irq of this handle
  37. uint32_t pin_num; ///< pin number of this handle
  38. uint32_t cb; ///< callback function
  39. gpio_mode_e mode; ///< gpio mode
  40. gpio_direction_e dir; ///< gpio direction
  41. uint32_t mask; ///< gpio mask bit
  42. uint32_t value; ///< gpio value
  43. } dw_gpio_priv_t;
  44. typedef struct {
  45. gpio_port_handle_t handle;
  46. uint8_t idx;
  47. pin_t pin_name;
  48. } dw_gpio_pin_priv_t;
  49. static dw_gpio_priv_t gpio_handle[CONFIG_GPIO_NUM];
  50. static dw_gpio_pin_priv_t gpio_pin_handle[CONFIG_GPIO_PIN_NUM];
  51. /* Driver Capabilities */
  52. static const gpio_capabilities_t driver_capabilities = {
  53. .interrupt_mode = 1, /* intrrupt mode */
  54. .pull_mode = 1 /* pull mode */
  55. };
  56. //
  57. // Functions
  58. //
  59. static dw_gpio_reg_t *gpio_reg = NULL;
  60. static dw_gpio_control_reg_t *gpio_control_reg = NULL;
  61. static int32_t gpio_set_direction(
  62. void *port,
  63. gpio_direction_e direction
  64. )
  65. {
  66. dw_gpio_priv_t *gpio_priv = port;
  67. if (direction == GPIO_DIRECTION_INPUT) {
  68. gpio_reg->SWPORT_DDR &= (~gpio_priv->mask);
  69. } else if (direction == GPIO_DIRECTION_OUTPUT) {
  70. gpio_reg->SWPORT_DDR |= gpio_priv->mask;
  71. } else {
  72. return ERR_GPIO(EDRV_PARAMETER);
  73. }
  74. return 0;
  75. }
  76. /*
  77. * Read the statu of the Port choosed.
  78. * Parameters:
  79. * port: use to choose a I/O port among Port A, B, or C.
  80. * return: the value of the corresponding Port.
  81. */
  82. int32_t gpio_read(uint32_t *value)
  83. {
  84. *value = gpio_control_reg->EXT_PORTA;
  85. return 0;
  86. }
  87. /*
  88. * Write an output value to corresponding Port.
  89. * Parameters:
  90. * port: use to choose a I/O port among Port A, B, or C.
  91. * output: value that will be written to the corresponding Port.
  92. * return: SUCCESS
  93. */
  94. static int32_t gpio_write(void *port, uint32_t mask)
  95. {
  96. dw_gpio_priv_t *gpio_priv = port;
  97. uint32_t value = gpio_reg->SWPORT_DR;
  98. value &= ~(mask);
  99. value |= gpio_priv->value;
  100. gpio_reg->SWPORT_DR = value;
  101. return 0;
  102. }
  103. /**
  104. * Configure a GPIO gpio_set_irq_mode.
  105. * @param[in] pin the addr store the pin num.
  106. * @param[in] _irqmode the irqmode of gpio
  107. * @return zero on success. -1 on falure.
  108. */
  109. static int32_t gpio_set_irq_mode(gpio_pin_handle_t pin, gpio_irq_mode_e irq_mode)
  110. {
  111. dw_gpio_pin_priv_t *gpio_pin_priv = pin;
  112. uint32_t offset = gpio_pin_priv->idx;
  113. uint32_t mask = 1 << offset;
  114. switch (irq_mode) {
  115. /* rising edge interrupt mode */
  116. case GPIO_IRQ_MODE_RISING_EDGE:
  117. gpio_control_reg->INTTYPE_LEVEL |= mask;
  118. gpio_control_reg->INT_POLARITY |= mask;
  119. break;
  120. /* falling edge interrupt mode */
  121. case GPIO_IRQ_MODE_FALLING_EDGE:
  122. gpio_control_reg->INTTYPE_LEVEL |= mask;
  123. gpio_control_reg->INT_POLARITY &= (~mask);
  124. break;
  125. /* low level interrupt mode */
  126. case GPIO_IRQ_MODE_LOW_LEVEL:
  127. gpio_control_reg->INTTYPE_LEVEL &= (~mask);
  128. gpio_control_reg->INT_POLARITY &= (~mask);
  129. break;
  130. /* high level interrupt mode */
  131. case GPIO_IRQ_MODE_HIGH_LEVEL:
  132. gpio_control_reg->INTTYPE_LEVEL &= (~mask);
  133. gpio_control_reg->INT_POLARITY |= mask;
  134. break;
  135. /* double edge interrupt mode */
  136. case GPIO_IRQ_MODE_DOUBLE_EDGE:
  137. return ERR_GPIO(EDRV_UNSUPPORTED);
  138. default:
  139. return ERR_GPIO(EDRV_PARAMETER);
  140. }
  141. return 0;
  142. }
  143. /*
  144. * Clear one or more interrupts of PortA.
  145. * Parameters:
  146. * pinno:
  147. * return: SUCCESS.
  148. */
  149. static void gpio_irq_clear(uint32_t idx)
  150. {
  151. gpio_control_reg->PORTA_EOI = idx;
  152. }
  153. /*
  154. * Enable one or more interrupts of PortA.
  155. * Parameters:
  156. * pinno:
  157. * return: SUCCESS.
  158. */
  159. static void gpio_irq_enable(gpio_pin_handle_t pin)
  160. {
  161. dw_gpio_pin_priv_t *gpio_pin_priv = pin;
  162. uint32_t offset = gpio_pin_priv->idx;
  163. uint32_t val = gpio_control_reg->INTEN;
  164. val |= (1 << offset);
  165. gpio_control_reg->INTEN = val;
  166. }
  167. /*
  168. * Disable one or more interrupts of PortA.
  169. * Parameters:
  170. * pinno:
  171. * return: SUCCESS.
  172. */
  173. static void gpio_irq_disable(gpio_pin_handle_t pin)
  174. {
  175. dw_gpio_pin_priv_t *gpio_pin_priv = pin;
  176. uint32_t offset = gpio_pin_priv->idx;
  177. uint32_t val = gpio_control_reg->INTEN;
  178. val &= ~(1 << offset);
  179. gpio_control_reg->INTEN = val;
  180. }
  181. void dw_gpio_irqhandler(int idx)
  182. {
  183. dw_gpio_priv_t *gpio_priv = &gpio_handle[idx];
  184. uint32_t value = gpio_control_reg->INTSTATUS;
  185. uint8_t i;
  186. /* find the interrput pin */
  187. for (i = 0; i < 32; i++) {
  188. if (value == (1 << i)) {
  189. break;
  190. }
  191. }
  192. uint32_t offset = i;
  193. uint32_t pin_idx = offset;
  194. for (i = 0; i < idx; i++) {
  195. pin_idx += gpio_handle[i].pin_num;
  196. }
  197. gpio_pin_handle_t pin = (gpio_pin_handle_t) &gpio_pin_handle[pin_idx];
  198. /* execute the callback function */
  199. if ((gpio_event_cb_t)(gpio_priv->cb)) {
  200. ((gpio_event_cb_t)(gpio_priv->cb))(pin);
  201. }
  202. gpio_irq_clear(value); //clear the gpio interrupt
  203. }
  204. int32_t __attribute__((weak)) target_gpio_port_init(port_name_t port, uint32_t *base, uint32_t *irq, uint32_t *pin_num)
  205. {
  206. return -1;
  207. }
  208. /**
  209. \brief Initialize GPIO module. 1. Initializes the resources needed for the GPIO handle 2.registers event callback function
  210. 3.get gpio_port_handle
  211. \param[in] port port_name.
  212. \param[in] cb_event Pointer to \ref gpio_event_cb_t
  213. \return gpio_port_handle
  214. */
  215. gpio_port_handle_t csi_gpio_port_initialize(port_name_t port, gpio_event_cb_t cb_event)
  216. {
  217. uint32_t i;
  218. dw_gpio_priv_t *gpio_priv;
  219. for (i = 0; i <= port; i++) {
  220. /* obtain the gpio port information */
  221. uint32_t base = 0u;
  222. uint32_t pin_num;
  223. uint32_t irq;
  224. uint32_t idx = target_gpio_port_init(i, &base, &irq, &pin_num);
  225. if (idx < 0 || idx >= CONFIG_GPIO_NUM) {
  226. return NULL;
  227. }
  228. gpio_priv = &gpio_handle[idx];
  229. gpio_priv->base = base;
  230. gpio_priv->irq = irq;
  231. gpio_priv->pin_num = pin_num;
  232. }
  233. gpio_reg = (dw_gpio_reg_t *)(gpio_priv->base);
  234. gpio_control_reg = (dw_gpio_control_reg_t *)(gpio_priv->base + 0x30);
  235. gpio_priv->cb = (uint32_t)cb_event;
  236. drv_nvic_enable_irq(gpio_priv->irq);
  237. return (gpio_port_handle_t)gpio_priv;
  238. }
  239. /**
  240. \brief De-initialize GPIO handle. stops operation and releases the software resources used by the handle
  241. \param[in] handle gpio port handle to operate.
  242. \return error code
  243. */
  244. int32_t csi_gpio_port_uninitialize(gpio_port_handle_t handle)
  245. {
  246. GPIO_NULL_PARAM_CHK(handle);
  247. dw_gpio_priv_t *gpio_priv = handle;
  248. gpio_priv->cb = NULL;
  249. drv_nvic_disable_irq(gpio_priv->irq);
  250. return 0;
  251. }
  252. /**
  253. \brief Get driver capabilities.
  254. \param[in] handle instance to operate.
  255. \return \ref gpio_capabilities_t
  256. */
  257. gpio_capabilities_t csi_gpio_get_io_capabilities(gpio_port_handle_t handle)
  258. {
  259. return driver_capabilities;
  260. }
  261. /**
  262. \brief config multiple pin within one handle
  263. \param[in] handle gpio port handle to operate.
  264. \param[in] mask the bitmask to identify which bits in the handle should be included (0 - ignore)
  265. \param[in] mode \ref gpio_mode_e
  266. \param[in] dir \ref gpio_direction_e
  267. \return error code
  268. */
  269. int32_t csi_gpio_port_config(gpio_port_handle_t handle, uint32_t mask, gpio_mode_e mode, gpio_direction_e dir)
  270. {
  271. if (mask < 0) {
  272. return ERR_GPIO(EDRV_PARAMETER);
  273. }
  274. GPIO_NULL_PARAM_CHK(handle);
  275. dw_gpio_priv_t *gpio_priv = handle;
  276. /*config the gpio mode direction mask bits */
  277. gpio_priv->mode = mode;
  278. gpio_priv->dir = dir;
  279. gpio_priv->mask = mask;
  280. uint32_t ret = gpio_set_direction(gpio_priv, dir);
  281. return ret;
  282. }
  283. /**
  284. \brief Write value to the handle(write value to multiple pins on one handle at the same time)
  285. \param[in] handle gpio port handle to operate.
  286. \param[in] mask The bitmask to identify which bits in the handle should be included (0 - ignore)
  287. \param[in] value the value to be set
  288. \return error code
  289. */
  290. int32_t csi_gpio_port_write(gpio_port_handle_t handle, uint32_t mask, uint32_t value)
  291. {
  292. if (mask < 0 || value < 0) {
  293. return ERR_GPIO(EDRV_PARAMETER);
  294. }
  295. GPIO_NULL_PARAM_CHK(handle);
  296. uint32_t port_value = mask & value;
  297. dw_gpio_priv_t *gpio_priv = handle;
  298. gpio_priv->value = port_value;
  299. gpio_write(gpio_priv, mask);
  300. return 0;
  301. }
  302. /**
  303. \brief Read the current value on the handle(read value of multiple pins on one handle at the same time)
  304. \param[in] handle gpio port handle to operate.
  305. \param[in] mask The bitmask to identify which bits in the handle should be included (0 - ignore)
  306. \param[out] value an integer with each bit corresponding to an associated handle pin setting
  307. \return error code
  308. */
  309. int32_t csi_gpio_port_read(gpio_port_handle_t handle, uint32_t mask, uint32_t *value)
  310. {
  311. if (mask < 0) {
  312. return ERR_GPIO(EDRV_PARAMETER);
  313. }
  314. GPIO_NULL_PARAM_CHK(handle);
  315. GPIO_NULL_PARAM_CHK(value);
  316. uint32_t port_value = 0;
  317. gpio_read(&port_value);
  318. *value = (mask & port_value);
  319. return 0;
  320. }
  321. int32_t __attribute__((weak)) target_gpio_pin_init(pin_t gpio_pin, uint32_t *port_idx)
  322. {
  323. return -1;
  324. }
  325. /**
  326. \brief Initialize GPIO handle.
  327. \param[in] gpio_pin Pointer to the pin_t.
  328. \return gpio_pin_handle
  329. */
  330. gpio_pin_handle_t csi_gpio_pin_initialize(pin_t gpio_pin)
  331. {
  332. /* obtain the gpio pin information */
  333. uint32_t port_idx;
  334. uint32_t pin_idx = target_gpio_pin_init(gpio_pin, &port_idx);
  335. dw_gpio_pin_priv_t *gpio_pin_priv = &(gpio_pin_handle[pin_idx]);
  336. gpio_pin_priv->handle = (gpio_port_handle_t)&gpio_handle[port_idx];
  337. uint32_t idx = pin_idx;
  338. uint32_t i;
  339. for (i = 0; i < port_idx; i++) {
  340. idx = pin_idx - (gpio_handle[i].pin_num);
  341. }
  342. gpio_pin_priv->idx = idx;
  343. return (gpio_pin_handle_t)gpio_pin_priv;
  344. }
  345. /**
  346. \brief config pin
  347. \param[in] pin gpio pin handle to operate.
  348. \param[in] mode \ref gpio_mode_e
  349. \param[in] dir \ref gpio_direction_e
  350. \return error code
  351. */
  352. int32_t csi_gpio_pin_config(gpio_pin_handle_t pin,
  353. gpio_mode_e mode,
  354. gpio_direction_e dir)
  355. {
  356. GPIO_NULL_PARAM_CHK(pin);
  357. /* config the gpio pin mode direction mask bits */
  358. dw_gpio_pin_priv_t *gpio_pin_priv = pin;
  359. dw_gpio_priv_t *gpio_priv = gpio_pin_priv->handle;
  360. gpio_priv->mode = mode;
  361. gpio_priv->dir = dir;
  362. gpio_priv->mask = 1 << gpio_pin_priv->idx;
  363. uint32_t ret = gpio_set_direction(gpio_priv, dir);
  364. if(ret) {
  365. return ret;
  366. }
  367. return 0;
  368. }
  369. /**
  370. \brief Set one or zero to the selected GPIO pin.
  371. \param[in] pin gpio pin handle to operate.
  372. \param[in] value the value to be set
  373. \return error code
  374. */
  375. int32_t csi_gpio_pin_write(gpio_pin_handle_t pin, bool value)
  376. {
  377. GPIO_NULL_PARAM_CHK(pin);
  378. if (value < 0) {
  379. return ERR_GPIO(EDRV_PARAMETER);
  380. }
  381. dw_gpio_pin_priv_t *gpio_pin_priv = pin;
  382. dw_gpio_priv_t *gpio_priv = gpio_pin_priv->handle;
  383. uint8_t offset = gpio_pin_priv->idx;
  384. uint32_t port_value = value << offset;
  385. gpio_priv->value = port_value;
  386. gpio_write(gpio_priv, (1 << offset));
  387. return 0;
  388. }
  389. /**
  390. \brief Get the value of selected GPIO pin.
  391. \param[in] pin gpio pin handle to operate.
  392. \param[out] value buf to store the pin value
  393. \return error code
  394. */
  395. int32_t csi_gpio_pin_read(gpio_pin_handle_t pin, bool *value)
  396. {
  397. GPIO_NULL_PARAM_CHK(pin);
  398. if (value <= 0) {
  399. return ERR_GPIO(EDRV_PARAMETER);
  400. }
  401. dw_gpio_pin_priv_t *gpio_pin_priv = pin;
  402. uint32_t port_value;
  403. uint8_t offset = gpio_pin_priv->idx;
  404. gpio_read(&port_value);
  405. *value = (port_value & (1 << offset)) >> offset;
  406. return 0;
  407. }
  408. /**
  409. \brief set GPIO interrupt mode.
  410. \param[in] pin gpio pin handle to operate.
  411. \param[in] mode the irq mode to be set
  412. \param[in] enable the enable flag
  413. \return error code
  414. */
  415. int32_t csi_gpio_pin_irq_set(gpio_pin_handle_t pin, gpio_irq_mode_e mode, bool enable)
  416. {
  417. GPIO_NULL_PARAM_CHK(pin);
  418. uint32_t ret = 0;
  419. if (enable) {
  420. ret = gpio_set_irq_mode(pin, mode);
  421. if (ret) {
  422. return ret;
  423. }
  424. gpio_irq_enable(pin);
  425. } else {
  426. gpio_irq_disable(pin);
  427. }
  428. return ret;
  429. }