Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <signal.h>
- #include <sys/mman.h>
- #include <native/task.h>
- #include <native/timer.h>
- #define TIMESLEEP 10000
- static volatile uint32_t *gpio;
- RT_TASK blink_task;
- int fd;
- void blink(void *arg __attribute__((__unused__)))
- {
- int iomask = 0;
- rt_task_set_periodic(NULL, TM_NOW, TIMESLEEP);
- while(1) {
- rt_task_wait_period(NULL);
- if( iomask == 0 ) {
- *gpio &= ~(1 << 8);
- iomask = 1;
- }
- else {
- *gpio |= (1 << 8);
- iomask = 0;
- }
- }
- }
- void catch_signal() {}
- int main(void)
- {
- signal(SIGTERM, catch_signal);
- signal(SIGINT, catch_signal);
- /* Avoids memory swapping for this program */
- mlockall(MCL_CURRENT|MCL_FUTURE);
- //Obtain handle to physical memory
- if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
- printf("Unable to open /dev/mem: %s\n", strerror(errno));
- return -1;
- }
- gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x20ac000);
- if ((int32_t)gpio < 0){
- printf("Mmap failed: %s\n", strerror(errno));
- return -1;
- }
- //set gpio8 as an output
- //increment the pointer to 0x20ac004
- *(gpio + 4) |= (1 << 8);
- /* Task Creation */
- rt_task_create(&blink_task, "blinkLed", 0, 99, 0);
- rt_task_start(&blink_task, &blink, NULL);
- getchar();
- rt_task_delete(&blink_task);
- //close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment