Skip to content

Instantly share code, notes, and snippets.

@tur11ng
Last active May 29, 2023 16:32
Show Gist options
  • Save tur11ng/093d3219dda614d8adf4394d8bd9ca41 to your computer and use it in GitHub Desktop.
Save tur11ng/093d3219dda614d8adf4394d8bd9ca41 to your computer and use it in GitHub Desktop.
Helper for porting Arduino code to AT91
#include <fcntl.h>
#include <header.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define LOW 0
#define HIGH 1
#define OUTPUT 0
#define INPUT 1
#define INPUT_PULLUP 2
void digitalWrite(int pin, int value);
int digitalRead(int pin);
int pinMode(int pin, int mode);
void FIQ_handler(void);
void setup();
void loop();
/*void buttonUpdate();*/
void digitalWrite(int pin, int value) {
switch (value) {
case LOW:
pioa->CODR = pioa->CODR | (1 << pin) break;
case HIGH:
pioa->SODR = pioa->SODR | (1 << pin) break;
}
}
int digitalRead(int pin) { return pioa->PDSR & (1 << pin); }
int pinMode(int pin, int mode) {
pioa->PER = pioa->PER | (1 << pin);
switch (mode) {
case OUTPUT:
pioa->OER = pioa->OER | (1 << pin);
pioa->CODR = pioa->CODR | (1 << pin);
break;
case INPUT:
pioa->ODR = pioa->ODR | (1 << pin);
break;
case INPUT_PULLUP:
pioa->ODR = pioa->ODR | (1 << pin);
pioa->PUER = pioa->PUER | (1 << pin);
break;
}
}
void FIQ_handler() {
unsigned int data_in;
if (fiq & (1 << PIOA_ID)) {
data_in = pioa->ISR;
aic->ICCR = (1 << PIOA_ID);
data_in = pioa->PDSR;
if (data_in & 0x02) {
/*buttonUpdate();*/
}
}
}
int main() {
STARTUP;
setup();
while (getchar() != 'e') {
loop();
}
aic->IDCR = (1 << PIOA_ID); // Stop AIC interrupts
CLEANUP;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment