at91_i2c_gpio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * File : at91_i2c_gpio.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-04-25 weety first version
  23. */
  24. #include <rtdevice.h>
  25. #include <rthw.h>
  26. #include <at91sam926x.h>
  27. static void at91_i2c_gpio_init()
  28. {
  29. at91_sys_write(AT91_PMC_PCER, 1 << AT91SAM9260_ID_PIOA); //enable PIOA clock
  30. at91_sys_write(AT91_PIOA + PIO_PUER, (1 << 23));
  31. at91_sys_write(AT91_PIOA + PIO_PER, (1 << 23));
  32. at91_sys_write(AT91_PIOA + PIO_MDER, (1 << 23));
  33. at91_sys_write(AT91_PIOA + PIO_PUER, (1 << 24));
  34. at91_sys_write(AT91_PIOA + PIO_PER, (1 << 24));
  35. at91_sys_write(AT91_PIOA + PIO_MDER, (1 << 24));
  36. at91_sys_write(AT91_PIOA + PIO_OER, (1 << 23));
  37. at91_sys_write(AT91_PIOA + PIO_OER, (1 << 24));
  38. at91_sys_write(AT91_PIOA + PIO_SODR, (1 << 23));
  39. at91_sys_write(AT91_PIOA + PIO_SODR, (1 << 24));
  40. }
  41. static void at91_set_sda(void *data, rt_int32_t state)
  42. {
  43. if (state)
  44. {
  45. at91_sys_write(AT91_PIOA + PIO_SODR, (1 << 23));
  46. }
  47. else
  48. {
  49. at91_sys_write(AT91_PIOA + PIO_CODR, (1 << 23));
  50. }
  51. }
  52. static void at91_set_scl(void *data, rt_int32_t state)
  53. {
  54. if (state)
  55. {
  56. at91_sys_write(AT91_PIOA + PIO_SODR, (1 << 24));
  57. }
  58. else
  59. {
  60. at91_sys_write(AT91_PIOA + PIO_CODR, (1 << 24));
  61. }
  62. }
  63. static rt_int32_t at91_get_sda(void *data)
  64. {
  65. return at91_sys_read(AT91_PIOA + PIO_PDSR) & (1 << 23);
  66. }
  67. static rt_int32_t at91_get_scl(void *data)
  68. {
  69. return at91_sys_read(AT91_PIOA + PIO_PDSR) & (1 << 24);
  70. }
  71. static void at91_udelay (rt_uint32_t us)
  72. {
  73. rt_int32_t i;
  74. for (; us > 0; us--)
  75. {
  76. i = 50000;
  77. while(i > 0)
  78. {
  79. i--;
  80. }
  81. }
  82. }
  83. static const struct rt_i2c_bit_ops bit_ops = {
  84. RT_NULL,
  85. at91_set_sda,
  86. at91_set_scl,
  87. at91_get_sda,
  88. at91_get_scl,
  89. at91_udelay,
  90. 5,
  91. 100
  92. };
  93. int at91_i2c_init(void)
  94. {
  95. struct rt_i2c_bus_device *bus;
  96. bus = rt_malloc(sizeof(struct rt_i2c_bus_device));
  97. if (bus == RT_NULL)
  98. {
  99. rt_kprintf("rt_malloc failed\n");
  100. return -RT_ENOMEM;
  101. }
  102. rt_memset((void *)bus, 0, sizeof(struct rt_i2c_bus_device));
  103. bus->priv = (void *)&bit_ops;
  104. at91_i2c_gpio_init();
  105. rt_i2c_bit_add_bus(bus, "i2c0");
  106. return 0;
  107. }
  108. INIT_DEVICE_EXPORT(at91_i2c_init);