diff --git a/src/draw.c b/src/draw.c index 286a40f..7bec443 100644 --- a/src/draw.c +++ b/src/draw.c @@ -7,7 +7,7 @@ #define BYTES_PER_PIXEL 2 -uint16_t bitmap_draw( unsigned int width, unsigned int height, +int8_t bitmap_draw( unsigned int width, unsigned int height, unsigned int bytes_per_pixel, const unsigned char *pixel_data){ diff --git a/src/draw.h b/src/draw.h index 12d13af..db8efe1 100644 --- a/src/draw.h +++ b/src/draw.h @@ -7,11 +7,10 @@ * @param width Bitmap width * @param height Bitmap height * @param bytes_per_pixel Bytes per pixel - * @param color 16-Bit color bitmap - * @param *pixel_data uint16_t Bitmap data array + * @param pixel_data uint16_t Bitmap data array * @return -1 if error 0 else * */ -uint16_t bitmap_draw(unsigned int width, unsigned int height, unsigned int bytes_per_pixel, const unsigned char *pixel_data); +int8_t bitmap_draw(unsigned int width, unsigned int height, unsigned int bytes_per_pixel, const unsigned char *pixel_data); #endif /* DRAW_H */ diff --git a/src/io.h b/src/io.h index aa9199c..30b944b 100644 --- a/src/io.h +++ b/src/io.h @@ -34,8 +34,13 @@ bool io_button_has_edge(uint8_t btnnumber); uint16_t read_adc(); -#define ADC_MASK 0x3FF +//Mask for the ADC value (to cut away bits which cannot be valid) +#define ADC_MASK 0x3FF + +//Value that should be threated as maximum #define ADC_MAX 0x3A0 + +//Tolerance value. Changes below this value will be ignored. #define ADC_TOLERANCE 0x08 diff --git a/src/io.s b/src/io.s index ce732bf..ef31c7c 100644 --- a/src/io.s +++ b/src/io.s @@ -11,10 +11,10 @@ typedef struct pin_s { .set OFFSET_PIN_PINMASK, 4 -//--------Local Functions ------------------ +//--------Functions ------------------ +//See the doxygen comments in the headerfiles for the parameter documentation of the following functions .text - //void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr) .global pin_create @@ -28,7 +28,7 @@ pin_create: .set GPIO_PuPd_UP, 1 .set GPIO_OType_OD, 1 - PUSH {r4,r5,r6,lr} + PUSH {r4,r5,r6,lr} //Save registers LSL r4, r2, #1 //r4 = pinnr*2 MOV r5, #3 //r5 = 3 @@ -72,7 +72,7 @@ pin_create: //pin->pinmask=0x01<GPIO->IDR & pin->pinmask) > 0); + + //C-Code (one liner) return ((pin->GPIO->IDR & pin->pinmask) > 0); + LDR r1, [r0, #OFFSET_PIN_GPIO] //r1 = pin->GPIO LDR r1, [r1,#GPIO_IDR] // r1 = pin->GPIO->IDR LDRH r2, [r0, #OFFSET_PIN_PINMASK] // r2 = pin->pinmask @@ -93,13 +95,14 @@ pin_get: MOV r0, #1 //return 1 MOV pc, lr + pin_get_eq: MOV r0, #0 //return 0 MOV pc, lr + end_pin_get: -//-------Global Functions------------------- //void adc_init(void); .global adc_init diff --git a/src/io_c.c b/src/io_c.c index 915372e..a0eae4d 100644 --- a/src/io_c.c +++ b/src/io_c.c @@ -2,42 +2,44 @@ #include #include "io.h" - +//Structure which holds the configuration for one (input) pin (GPIO). typedef struct pin_s { - GPIO_TypeDef* GPIO; - uint16_t pinmask; + GPIO_TypeDef* GPIO; //GPIO port which is used + uint16_t pinmask; //Pin Nr (0..15) } pin_t; // Local variables +// Pin structs for the pins T0..T3 static pin_t pin_t0; static pin_t pin_t1; static pin_t pin_t2; static pin_t pin_t3; -static uint8_t new = 0; -static uint8_t old = 0; -static volatile uint8_t edg = 0; + +//Variables for the edge detection +static uint8_t old = 0; //old state of the buttons +static volatile uint8_t edg = 0; //detected edges //Function prototypes of functions that are implemented in asm -void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr); -bool pin_get(pin_t* pin); -bool adc_init(); - +void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr); //Initializes the pin structure and the gpio with the passed data. +bool pin_get(pin_t* pin); //Returns the current state of the pin (true = pin is "high") +bool adc_init(); //Initializes the adc and configures the channel void io_process(void) { - new = pin_get(&pin_t0) | - pin_get(&pin_t1) << 1 | - pin_get(&pin_t2) << 2 | - pin_get(&pin_t3) << 3; + //Save the current state of the 4 pins in the lower nibble + uint8_t new = pin_get(&pin_t0) | + pin_get(&pin_t1) << 1 | + pin_get(&pin_t2) << 2 | + pin_get(&pin_t3) << 3; edg |= (new ^ old) & new; // detect positive edge - old = new; + old = new; //store current state, for later edge detection } bool io_button_has_edge(uint8_t btnnumber) { uint8_t bm = (1 << btnnumber); // create bitmask bool status = ((edg & bm) > 0); // check if button is pressed - if(status){ + if(status) { // if button is pressed edg &= ~bm; // clear edge bit } @@ -45,14 +47,16 @@ bool io_button_has_edge(uint8_t btnnumber) { } void io_init(void){ + //Enable clock for gpio //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE); // Enable gpio clock source RCC->AHB1ENR|= RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOI; + // Create player pins pin_create(&pin_t0, GPIOC, 7); // create pin_t0 pin_create(&pin_t1, GPIOB, 15); // create pin_t1 pin_create(&pin_t2, GPIOB, 14); // create pin_t2 pin_create(&pin_t3, GPIOI, 0); // create pin_t3 - adc_init(); + adc_init(); //Initialize adc } diff --git a/src/main.c b/src/main.c index c1d0b6a..7f09a7b 100644 --- a/src/main.c +++ b/src/main.c @@ -31,18 +31,21 @@ #include "game.h" #include "uart.h" +//Game Object, which contains the entire state of the game +//Exchange this object via Uart, to have remote players game_t gameobj; -uint64_t ticks; -uint64_t lastTicks; +volatile uint64_t ticks = 0; //Ticks since reset +uint64_t lastTicks = 0; //Last tick +//SysTick Handler that will be called several times per second (see TICKS_PER_SECOND). This is an interrupt. void SysTick_Handler() { - ticks++; - io_process(); + ticks++; + io_process(); //Process button presses } -#define SYSCLK 168e6 -#define TICKS_PER_SECOND 1000 +#define SYSCLK 168e6 //System frequency +#define TICKS_PER_SECOND 1000 //Number of SysTick interrupts that should occour ( = number of times SysTick_Handler calls per sec) int main(void) { @@ -50,13 +53,14 @@ int main(void) while(1); //sleep forever } - game_init(&gameobj, TICKS_PER_SECOND); + game_init(&gameobj, TICKS_PER_SECOND); //Init game state + while(1) { uint64_t curTicks = ticks; - if(game_step(&gameobj,curTicks-lastTicks)) { //calculate next game step, and pass it the delta time - lastTicks = ticks; + if(game_step(&gameobj,curTicks-lastTicks)) { //calculate next game step, and pass it the delta time. Returns true if function was blocking + lastTicks = ticks; //Save actual ticks } else { - lastTicks = curTicks; + lastTicks = curTicks; //Save ticks from before calling game_step } }