Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. //To set high GPIO4 pin (GPFSEL0 and GPSET0 assumed to be adresses)
  2. #define GPIO4_GPFSEL 12
  3. #define GPIO4 4
  4. *GPFSEL0 |= (1 << GPIO4_GPFSEL);
  5. *GPSET0 |= (1 << 4);
  6.  
  7. gpio_set_mode(GPIO4, GPIO_OUTPUT);
  8. gpio_set_high(GPIO4);
  9.  
  10. void gpio_set_mode(uint32_t gpio, gpio_mode mode)
  11. {
  12. uint32_t gpfsel_addr = GPIO_BASE;
  13. uint32_t gpfsel_num = (uint32_t)(gpio / 10);
  14. uint32_t gpio_bit = ((((uint32_t)(gpio % 10)) * 3));
  15.  
  16. for (int i = 0; i < 6; i++)
  17. {
  18. if (i == gpfsel_num)
  19. break;
  20. else
  21. gpfsel_addr += 0x04;
  22. }
  23.  
  24. volatile uint32_t* GPFSEL = (volatile uint32_t*) gpfsel_addr;
  25. switch (mode)
  26. {
  27. case GPIO_OUTPUT:
  28. *GPFSEL |= (1 << gpio_bit);
  29. case GPIO_INPUT:
  30. {
  31. *GPFSEL &= ~(1 << gpio_bit);
  32. *GPFSEL &= ~(1 << (gpio_bit+1));
  33. *GPFSEL &= ~(1 << (gpio_bit+2));
  34. }
  35. }
  36. }
  37.  
  38. void gpio_set_high(uint32_t gpio)
  39. {
  40. volatile uint32_t* GPSET = ((gpio < 32) ? ( (volatile uint32_t*)(GPSET0)) : ( (volatile uint32_t*)(GPSET1) ));
  41. uint32_t gpio_bit = (gpio < 32) ? gpio : (gpio - 32);
  42. *GPSET |= (1 << gpio_bit);
  43. }
  44.  
  45.  
  46. void gpio_set_low(uint32_t gpio)
  47. {
  48. volatile uint32_t* GPCLR = ((gpio < 32) ? ( (volatile uint32_t*)(GPCLR0) ) : ( (volatile uint32_t*)(GPCLR1)));
  49. uint32_t gpio_bit = ((gpio < 32) ? gpio : (gpio - 32));
  50. *GPCLR |= (1 << gpio_bit);
  51. }
  52.  
  53. uint32_t gpio_status(uint32_t gpio)
  54. {
  55. volatile uint32_t* GPLEV = ((gpio < 32) ? ((volatile uint32_t*)(GPLEV0)) : ((volatile uint32_t*)(GPLEV1)));
  56. uint32_t gpio_bit = ((gpio < 32) ? gpio : (gpio - 32));
  57. return (*GPLEV & (1 << gpio_bit));
  58. }
  59.  
  60. -Wall -O2 -nostdlib -nostartfiles -ffreestanding
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement