drv_pin.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * File : drv_pin.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2013, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2018-03-25 Tanek the first version.
  13. */
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include <drivers/pin.h>
  17. #include <SMM_MPS2.h>
  18. #ifdef RT_USING_PIN
  19. // pin 0 ~ 7 : switch 0 ~ 7
  20. // pin 8 ~ 15 : user led 0 ~ 7
  21. static void v2m_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  22. {
  23. return ;
  24. }
  25. static int v2m_pin_read(rt_device_t dev, rt_base_t pin)
  26. {
  27. RT_ASSERT(dev != RT_NULL);
  28. if (0 <= pin && pin <= 7)
  29. {
  30. return !!(MPS2_SCC->CFG_REG3 & (0x01 << pin)); // switchs
  31. }
  32. else if (8 <= pin && pin <= 15)
  33. {
  34. return !!(MPS2_SCC->CFG_REG1 & (0x01 << (pin - 8))); // leds
  35. }
  36. else
  37. {
  38. RT_ASSERT(RT_NULL);
  39. return 0;
  40. }
  41. }
  42. static void v2m_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  43. {
  44. RT_ASSERT(dev != RT_NULL);
  45. if (8 <= pin && pin <= 15)
  46. {
  47. rt_uint32_t reg = MPS2_SCC->CFG_REG1;
  48. if (value)
  49. reg |= 1 << (pin - 8);
  50. else
  51. reg &= ~(1 << (pin - 8));
  52. MPS2_SCC->CFG_REG1 = reg;
  53. }
  54. else
  55. {
  56. RT_ASSERT(RT_NULL);
  57. }
  58. }
  59. int rt_hw_pin_init(void)
  60. {
  61. int ret = RT_EOK;
  62. static const struct rt_pin_ops v2m_pin_ops =
  63. {
  64. v2m_pin_mode,
  65. v2m_pin_write,
  66. v2m_pin_read,
  67. RT_NULL,
  68. RT_NULL,
  69. RT_NULL
  70. };
  71. ret = rt_device_pin_register("pin", &v2m_pin_ops, RT_NULL);
  72. return ret;
  73. }
  74. INIT_BOARD_EXPORT(rt_hw_pin_init);
  75. #endif /*RT_USING_PIN */