Advertisement
Guest User

Untitled

a guest
May 8th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. /*
  2.  * Copyright 2011-2012 Haiku, Inc. All rights reserved.
  3.  * Distributed under the terms of the MIT License.
  4.  *
  5.  * Authors:
  6.  *      Alexander von Gluck, kallisti5@unixzen.com
  7.  */
  8.  
  9.  
  10. #include "gpio.h"
  11.  
  12.  
  13. #if 0
  14. #define GPIO_IN(g) *(GPIO_BASE + ((g) / 10)) &= ~(7 << (((g) % 10) * 3))
  15. #define GPIO_OUT(g) *(GPIO_BASE + ((g) / 10)) |= (1 << (((g) % 10) * 3))
  16. #define GPIO_SET(g) *(GPIO_BASE + 7) = (1 << g)
  17. #define GPIO_CLR(g) *(GPIO_BASE + 10) = (1 << g)
  18.  
  19. #define GPIO_ALT(g, a) *(GPIO_BASE + (((g) / 10))) \
  20.     |= (((a) <= 3 ? (a) + 4 : (a) == 4 ? 3 : 2) << (((g)%10) * 3))
  21. #endif
  22.  
  23.  
  24. /*!
  25.  * At GPIO (base) (pin) directon to input
  26.  */
  27. void
  28. gpio_in(addr_t base, int pin)
  29. {
  30.     *(volatile unsigned char *)(base + (pin / 10))
  31.         &= ~(7 << ((pin % 10) * 3));
  32. }
  33.  
  34.  
  35. /*!
  36.  * At GPIO (base) (pin) directon to output
  37.  */
  38. void
  39. gpio_out(addr_t base, int pin)
  40. {
  41.     *(volatile unsigned char *)(base + (pin / 10))
  42.         |= (1 << ((pin % 10) * 3));
  43. }
  44.  
  45.  
  46.  
  47. /*!
  48.  * At GPIO (base) (pin) state set (high)
  49.  */
  50. void
  51. gpio_set(addr_t base, int pin)
  52. {
  53.     *(volatile unsigned char *)(base + 7) = (1 << pin);
  54. }
  55.  
  56.  
  57. /*!
  58.  * At GPIO (base) (pin) state clear (low)
  59.  */
  60. void
  61. gpio_clear(addr_t base, int pin)
  62. {
  63.     *(volatile unsigned char *)(base + 10) = (1 << pin);
  64. }
  65.  
  66.  
  67.  
  68. /*!
  69.  * At GPIO (base) (pin) alternate function (value)
  70.  */
  71. void
  72. gpio_alt(addr_t base, int pin, int value)
  73. {
  74.     *(volatile unsigned char *)(base + (pin / 10))
  75.         |= ((value <= 3 ? value + 4 : value == 4 ? 3 : 2) << ((pin % 10) * 3));
  76.  
  77. }
  78.  
  79.  
  80. void
  81. gpio_init()
  82. {
  83.     // ** Take control of ok uart, led, and general use pins
  84.     int pin = 0;
  85.     for (pin = 14; pin <= 25; pin++) {
  86.         gpio_in(GPIO_BASE, pin);
  87.         gpio_out(GPIO_BASE, pin);
  88.     }
  89.  
  90.     // ** Prepare UART pins for serial communication
  91.     // Set alternate function 0 on UART pins
  92.     gpio_alt(GPIO_BASE, 14, 4);
  93.     gpio_alt(GPIO_BASE, 15, 4);
  94.     // Pull UART pins to initial state low
  95.     gpio_clear(GPIO_BASE, 14);
  96.     gpio_clear(GPIO_BASE, 15);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement