📄 main.c
字号:
/*** Sample application to toggle LED on KB9202* Notice that the kernel may provide LED access functions,* but the point of this program is to provide an example* on how to access the AT91RM9200 system controller for the purpose* of driving GPIO and other peripheral functions.** Copyright (C) 2008 KwikByte <www.kwikbyte.com>** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA* or visit http://www.gnu.org/copyleft/gpl.html** 26MAR2008 Initial creation (KwikByte)*/#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/shm.h>#include <sys/types.h>#include <sys/ioctl.h>#include <sys/mman.h>#include <sys/stat.h>#include <sys/time.h>#include <unistd.h>#include <fcntl.h>#include <strings.h>#include <string.h>#include <time.h>#include "AT91RM9200.h"/* ********************** LOCAL DEFINES ****************************** */#define LED_MAX 3/* D20 = top LED * D19 = middle LED * D18 = bottom LED * * Notice that D16..D31 are muxed with PC pins * * see AT91RM9200 datasheet for more information */static unsigned ledSignalMapping[] = { AT91C_PIO_PC20, AT91C_PIO_PC19, AT91C_PIO_PC18};#define GPIO_LED_MASK (AT91C_PIO_PC20 | AT91C_PIO_PC19 |\ AT91C_PIO_PC18)static int memMapFile;static AT91PS_SYS at91SysCtrlr;/* ************************ UTILITY FUNCTIONS ******************************* *//* * Set the LED output(s) */static void UpdateLED(unsigned ledIndex, unsigned on){ if (ledIndex >= LED_MAX) /* invalid index */ return ; /* remember that LED signals are active low */ if (on) { at91SysCtrlr->PIOC_CODR = (ledSignalMapping[ledIndex]); } else { at91SysCtrlr->PIOC_SODR = (ledSignalMapping[ledIndex]); } /* do not worry about sync */}/* *************** HARDWARE SUPPORT FUNCTIONS ************************ *//* * Access the hardware */static int OpenSystemController(void){ if ((memMapFile = open("/dev/mem", O_RDWR | O_SYNC)) < 0) { printf("ERROR: Unable to open /dev/mem\n"); return (errno); } at91SysCtrlr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, memMapFile, (unsigned)AT91C_BASE_SYS); if (at91SysCtrlr == MAP_FAILED) { printf("ERROR: Unable to mmap the system controller\n"); close (memMapFile); memMapFile = -1; return (errno); } /* set LED signals for output */ /* set digital output ports init value = all on (active low) */ at91SysCtrlr->PIOC_CODR = GPIO_LED_MASK; at91SysCtrlr->PIOC_PPUDR = GPIO_LED_MASK; at91SysCtrlr->PIOC_PER = GPIO_LED_MASK; at91SysCtrlr->PIOC_OER = GPIO_LED_MASK; /* do not worry about sync */ return (0);}/* ************************ MAIN LOOPS ******************************* */int main(int argc, char **argv){ unsigned ledIndex; if (OpenSystemController()) { printf("Unable to map hardware resources\n"); return (-1); } ledIndex = 0; while (1) { /* loop forever toggling LEDs at 1 second interval */ /* turn on */ UpdateLED(ledIndex, 1); printf ("LED %d is now on\n", ledIndex); /* wait for 0.5 seconds */ usleep(500000); /* turn off */ UpdateLED(ledIndex, 0); printf ("LED %d is now off\n", ledIndex); /* wait for 0.5 seconds */ usleep(500000); /* reset index to first LED if we finished all of them */ if (++ledIndex >= LED_MAX) ledIndex = 0; } return (0); /* never get here */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -