Advertisement
Guest User

Raspberry Pi LED blinking

a guest
Dec 9th, 2019
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. #include <stdint.h>
  2.  
  3. #define RPI_SYSTIMER_BASE       0x20003000UL
  4.  
  5. /* The base address of the GPIO peripheral (ARM Physical Address) */
  6. #define GPIO_BASE       0x20200000UL
  7.  
  8. // define used GPIO pin
  9. #define LED_GPFSEL      GPIO_GPFSEL1
  10. #define LED_GPFBIT      18
  11. #define LED_GPSET       GPIO_GPSET0
  12. #define LED_GPCLR       GPIO_GPCLR0
  13. #define LED_GPIO_BIT    16
  14.  
  15.  
  16. #define GPIO_GPFSEL0    0
  17. #define GPIO_GPFSEL1    1
  18. #define GPIO_GPFSEL2    2
  19. #define GPIO_GPFSEL3    3
  20. #define GPIO_GPFSEL4    4
  21. #define GPIO_GPFSEL5    5
  22.  
  23. #define GPIO_GPSET0     7
  24. #define GPIO_GPSET1     8
  25.  
  26. #define GPIO_GPCLR0     10
  27. #define GPIO_GPCLR1     11
  28.  
  29. #define GPIO_GPLEV0     13
  30. #define GPIO_GPLEV1     14
  31.  
  32. #define GPIO_GPEDS0     16
  33. #define GPIO_GPEDS1     17
  34.  
  35. #define GPIO_GPREN0     19
  36. #define GPIO_GPREN1     20
  37.  
  38. #define GPIO_GPFEN0     22
  39. #define GPIO_GPFEN1     23
  40.  
  41. #define GPIO_GPHEN0     25
  42. #define GPIO_GPHEN1     26
  43.  
  44. #define GPIO_GPLEN0     28
  45. #define GPIO_GPLEN1     29
  46.  
  47. #define GPIO_GPAREN0    31
  48. #define GPIO_GPAREN1    32
  49.  
  50. #define GPIO_GPAFEN0    34
  51. #define GPIO_GPAFEN1    35
  52.  
  53. #define GPIO_GPPUD      37
  54. #define GPIO_GPPUDCLK0  38
  55. #define GPIO_GPPUDCLK1  39
  56.  
  57. typedef struct {
  58.     volatile uint32_t control_status;
  59.     volatile uint32_t counter_lo;
  60.     volatile uint32_t counter_hi;
  61.     volatile uint32_t compare0;
  62.     volatile uint32_t compare1;
  63.     volatile uint32_t compare2;
  64.     volatile uint32_t compare3;
  65. } rpi_sys_timer_t;
  66.  
  67. void RPI_WaitMicroSeconds( uint32_t us );
  68.  
  69. static rpi_sys_timer_t* rpiSystemTimer = (rpi_sys_timer_t*)RPI_SYSTIMER_BASE;
  70.  
  71. /** GPIO Register set */
  72. volatile unsigned int* gpio;
  73.  
  74. /** Main function - we'll never return from here */
  75. int main(void)
  76. {
  77.     /* Assign the address of the GPIO peripheral (Using ARM Physical Address) */
  78.     gpio = (unsigned int*)GPIO_BASE;
  79.  
  80.     /* Write 1 to the GPIO16 init nibble in the Function Select 1 GPIO
  81.        peripheral register to enable GPIO16 as an output */
  82.     gpio[LED_GPFSEL] |= (1 << LED_GPFBIT);
  83.  
  84.     /* Never exit as there is no OS to exit to! */
  85.     while(1)
  86.     {
  87.         RPI_WaitMicroSeconds( 500000 );
  88.  
  89.         /* Set the LED GPIO pin low ( Turn OK LED on for original Pi, and off
  90.             for plus models )*/
  91.         gpio[LED_GPCLR] = (1 << LED_GPIO_BIT);
  92.  
  93.         RPI_WaitMicroSeconds( 500000 );
  94.  
  95.         /* Set the LED GPIO pin high ( Turn OK LED off for original Pi, and on
  96.             for plus models )*/
  97.         gpio[LED_GPSET] = (1 << LED_GPIO_BIT);
  98.     }
  99. }
  100.  
  101. void RPI_WaitMicroSeconds( uint32_t us )
  102. {
  103.     volatile uint32_t ts = rpiSystemTimer->counter_lo;
  104.    
  105.     while( ( rpiSystemTimer->counter_lo - ts ) < us )
  106.     {
  107.         /* Wait */
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement