34 #define BCM2708_PERI_BASE 0x20000000
35 #define BCM2708_GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
36 #define BCM2709_PERI_BASE 0x3F000000
37 #define BCM2709_GPIO_BASE (BCM2709_PERI_BASE + 0x200000)
38 #define FSEL_OFFSET 0 // 0x0000
39 #define SET_OFFSET 7 // 0x001c / 4
40 #define CLR_OFFSET 10 // 0x0028 / 4
41 #define PINLEVEL_OFFSET 13 // 0x0034 / 4
42 #define EVENT_DETECT_OFFSET 16 // 0x0040 / 4
43 #define RISING_ED_OFFSET 19 // 0x004c / 4
44 #define FALLING_ED_OFFSET 22 // 0x0058 / 4
45 #define HIGH_DETECT_OFFSET 25 // 0x0064 / 4
46 #define LOW_DETECT_OFFSET 28 // 0x0070 / 4
47 #define PULLUPDN_OFFSET 37 // 0x0094 / 4
48 #define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
50 #define PAGE_SIZE (4*1024)
51 #define BLOCK_SIZE (4*1024)
79 if ((mem_fd = open(
"/dev/mem", O_RDWR|O_SYNC) ) < 0)
88 gpio_mem += PAGE_SIZE - ((uint32_t)gpio_mem % PAGE_SIZE);
104 int shift = (gpio%32);
114 *(
gpio_map+clk_offset) = 1 << shift;
123 if (
function ==
PWM) {
132 int shift = (gpio%10)*3;
135 *(
gpio_map+offset) = (*(
gpio_map+offset) & ~(7<<shift)) | (
function<<shift);
142 int shift = (gpio%10)*3;
155 int offset,
value, mask;
158 mask = (1 << gpio%32);
181 ts.tv_sec = period/1000;
182 ts.tv_nsec = (period%1000) * 1000000;
184 for (i=0; sequence[i] !=
'\0'; i++) {
185 if (sequence[i] ==
'1') {
192 nanosleep(&ts, NULL);
207 void pulseTS(
int gpio,
struct timespec *up,
struct timespec *down) {
208 if ((up->tv_sec > 0) || (up->tv_nsec > 0)) {
213 if ((down->tv_sec > 0) || (down->tv_nsec > 0)) {
215 nanosleep(down, NULL);
222 memcpy(&
gpio_tspairs[gpio].up, up,
sizeof(
struct timespec));
223 memcpy(&
gpio_tspairs[gpio].down, down,
sizeof(
struct timespec));
232 struct timespec tsUP, tsDOWN;
234 tsUP.tv_sec = up/1000;
235 tsUP.tv_nsec = (up%1000) * 1000000;
237 tsDOWN.tv_sec = down/1000;
238 tsDOWN.tv_nsec = (down%1000) * 1000000;
244 int up = ratio*width;
245 int down = width - up;
251 struct timespec tsUP, tsDOWN;
254 tsUP.tv_nsec = up * 1000;
257 tsDOWN.tv_nsec = down * 1000;
263 int up = ratio*width;
264 int down = width - up;
272 int up = 1520 + (angle*400)/45;
281 int up = ratio * 20000;
282 int down = 20000 - up;
292 int gpio = (int)data;
302 if (thread != NULL) {
308 thread = (pthread_t*) malloc(
sizeof(pthread_t));
309 pthread_create(thread, NULL,
pwmLoop, (
void*)gpio);
316 if (thread == NULL) {
320 pthread_cancel(*thread);
344 if( (fp = fopen(
"/proc/cpuinfo",
"r")) )
346 while(fgets(str,
sizeof str, fp))
347 if( !memcmp(str,
"processor", 9) ) procCount++;
352 printf(
"Unable to get proc count. Defaulting to 2");
#define BCM2708_GPIO_BASE
void pulseOrSaveTS(int gpio, struct timespec *up, struct timespec *down)
#define PULLUPDNCLK_OFFSET
void * pwmLoop(void *data)
void output(int gpio, int value)
#define BCM2709_GPIO_BASE
static volatile uint32_t * gpio_map
void pulseMicro(int gpio, int up, int down)
#define SETUP_DEVMEM_FAIL
void disablePWM(int gpio)
struct pulse * getPulse(int gpio)
void pulseMicroRatio(int gpio, int width, float ratio)
void set_pullupdn(int gpio, int pud)
void pulseTS(int gpio, struct timespec *up, struct timespec *down)
int get_rpi_revision(void)
#define SETUP_MALLOC_FAIL
void outputSequence(int gpio, int period, char *sequence)
static struct pulse gpio_pulses[GPIO_COUNT]
void pulseMilli(int gpio, int up, int down)
static struct tspair gpio_tspairs[GPIO_COUNT]
int number_of_cores(void)
int isPWMEnabled(int gpio)
void pulseMilliRatio(int gpio, int width, float ratio)
void set_function(int gpio, int function, int pud)
void pulseRatio(int gpio, float ratio)
void pulseAngle(int gpio, float angle)
static pthread_t * gpio_threads[GPIO_COUNT]
int get_function(int gpio)