diff --git a/.vscode/settings.json b/.vscode/settings.json index 13b0224..02bb9b3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,6 +19,8 @@ "tuple": "cpp", "main.h": "c", "led.h": "c", - "stm32f730xx.h": "c" + "stm32f730xx.h": "c", + "keyboard.h": "c", + "gpio_helper.h": "c" } } \ No newline at end of file diff --git a/numworks_port/build/Laplace/gpio_helper.o b/numworks_port/build/Laplace/gpio_helper.o index 5e119b8..3a0b959 100644 Binary files a/numworks_port/build/Laplace/gpio_helper.o and b/numworks_port/build/Laplace/gpio_helper.o differ diff --git a/numworks_port/build/Laplace/keyboard.o b/numworks_port/build/Laplace/keyboard.o new file mode 100644 index 0000000..0e37f31 Binary files /dev/null and b/numworks_port/build/Laplace/keyboard.o differ diff --git a/numworks_port/build/Laplace/laplace.o b/numworks_port/build/Laplace/laplace.o index dd5dd23..2cb6cb8 100644 Binary files a/numworks_port/build/Laplace/laplace.o and b/numworks_port/build/Laplace/laplace.o differ diff --git a/numworks_port/build/Laplace/time.o b/numworks_port/build/Laplace/time.o new file mode 100644 index 0000000..46216b8 Binary files /dev/null and b/numworks_port/build/Laplace/time.o differ diff --git a/numworks_port/build/main.bin b/numworks_port/build/main.bin index ba87df0..4e1ba36 100755 Binary files a/numworks_port/build/main.bin and b/numworks_port/build/main.bin differ diff --git a/numworks_port/build/main.elf b/numworks_port/build/main.elf index ab1e7ff..a8a93d7 100755 Binary files a/numworks_port/build/main.elf and b/numworks_port/build/main.elf differ diff --git a/numworks_port/build/main.o b/numworks_port/build/main.o index 9b45cec..41168c9 100644 Binary files a/numworks_port/build/main.o and b/numworks_port/build/main.o differ diff --git a/numworks_port/src/Laplace/gpio_helper.c b/numworks_port/src/Laplace/gpio_helper.c index 7211792..d5ba4c2 100644 --- a/numworks_port/src/Laplace/gpio_helper.c +++ b/numworks_port/src/Laplace/gpio_helper.c @@ -59,6 +59,34 @@ void set_output_pin(uint8_t gpio_x, uint8_t pin, bool state){ } } +bool read_input_pin(uint8_t gpio_x, uint8_t pin){ + // Invert the IDR register since '0' means 'pressed'. + uint8_t idr_val = 0; + switch (gpio_x) + { + case GPIO_A: + idr_val = ~GPIOA->IDR; + break; + case GPIO_B: + idr_val = ~GPIOB->IDR; + break; + case GPIO_C: + idr_val = ~GPIOC->IDR; + break; + case GPIO_D: + idr_val = ~GPIOD->IDR; + break; + case GPIO_E: + idr_val = ~GPIOE->IDR; + break; + + default: + break; + } + + return idr_val & (1 << pin); +} + void enable_gpio_x_rcc(uint8_t gpio_x){ switch (gpio_x) { diff --git a/numworks_port/src/Laplace/gpio_helper.h b/numworks_port/src/Laplace/gpio_helper.h index 4f73d94..a09f88f 100644 --- a/numworks_port/src/Laplace/gpio_helper.h +++ b/numworks_port/src/Laplace/gpio_helper.h @@ -17,6 +17,7 @@ #define GPIO_E 4 void set_output_pin(uint8_t gpio_x, uint8_t pin, bool state); +bool read_input_pin(uint8_t gpio_x, uint8_t pin); void enable_gpio_x_rcc(uint8_t gpio_x); #endif \ No newline at end of file diff --git a/numworks_port/src/Laplace/keyboard.c b/numworks_port/src/Laplace/keyboard.c new file mode 100644 index 0000000..46f2e13 --- /dev/null +++ b/numworks_port/src/Laplace/keyboard.c @@ -0,0 +1,74 @@ +/* The keyboard is a matrix that is laid out as follow: + * (K_B2 and K_B3 are respectively specific to N0100 and N0110 + * + * | PC0 | PC1 | PC2 | PC3 | PC4 | PC5 | + * -----+------+------+------+------+------+------+ + * PE0 | K_A1 | K_A2 | K_A3 | K_A4 | K_A5 | K_A6 | + * -----+------+------+------+------+------+------+ + * PE1 | K_B1 |(K_B2)|(K_B3)| | | | + * -----+------+------+------+------+------+------+ + * PE2 | K_C1 | K_C2 | K_C3 | K_C4 | K_C5 | K_C6 | + * -----+------+------+------+------+------+------+ + * PE3 | K_D1 | K_D2 | K_D3 | K_D4 | K_D5 | K_D6 | + * -----+------+------+------+------+------+------+ + * PE4 | K_E1 | K_E2 | K_E3 | K_E4 | K_E5 | K_E6 | + * -----+------+------+------+------+------+------+ + * PE5 | K_F1 | K_F2 | K_F3 | K_F4 | K_F5 | | + * -----+------+------+------+------+------+------+ + * PE6 | K_G1 | K_G2 | K_G3 | K_G4 | K_G5 | | + * -----+------+------+------+------+------+------+ + * PE7 | K_H1 | K_H2 | K_H3 | K_H4 | K_H5 | | + * -----+------+------+------+------+------+------+ + * PE8 | K_I1 | K_I2 | K_I3 | K_I4 | K_I5 | | + * -----+------+------+------+------+------+------| + * + * We decide to drive the rows (PE0-8) and read the columns (PC0-5). + * + * To avoid short-circuits, the pins E0-E8 will not be standard outputs but + * only open-drain. Open drain means the pin is either driven low or left + * floating. + * When a user presses multiple keys, a connection between two rows can happen. + * If we don't use open drain outputs, this situation could trigger a short + * circuit between an output driving high and another driving low. + * + * If the outputs are open-drain, this means that the input must be pulled up. + * So if the input reads "1", this means the key is in fact *not* pressed, and + * if it reads "0" it means that there's a short to an open-drain output. Which + * means the corresponding key is pressed. + */ + +#include "keyboard.h" + +const uint8_t number_of_rows = 9; +const uint8_t number_of_columns = 6; + +void keyboard_init(){ + for (int i = 0; i < number_of_rows; i++){ + // setup rows (output open drain) + GPIOA->MODER &= ~(0x3 << (i*2)); + GPIOA->MODER |= (0x1 << (i*2)); + GPIOA->OTYPER |= (1 << i); + } + for (int i = 0; i < number_of_columns; i++){ + // setup columns (input pullup) + GPIOC->MODER &= ~(0x3 << (i*2)); + GPIOC->PUPDR &= ~(0x3 << (i*2)); + GPIOC->PUPDR |= (0x1 << (i*2)); + } +} + +bool keyboard_scan(){ + set_output_pin(GPIO_A, 6, true); + bool key_state = read_input_pin(GPIO_C, 3); + return key_state; + // A LOT TODO +} + +void activate_row(uint8_t row_nb){ + // set all row to 0 and then reenable selected row + for(int i =0; i < number_of_rows; i++){ + set_output_pin(GPIO_A, i, true); + } + set_output_pin(GPIO_A, row_nb, false); + us_wait(100); +} \ No newline at end of file diff --git a/numworks_port/src/Laplace/keyboard.h b/numworks_port/src/Laplace/keyboard.h new file mode 100644 index 0000000..32d7b97 --- /dev/null +++ b/numworks_port/src/Laplace/keyboard.h @@ -0,0 +1,27 @@ +/** @file keyboard.h + * + * @brief All action related to keyboard scan + * + */ + +#ifndef KEYBOARD_H +#define KEYBOARD_H + +enum PIN_ROW { + A = 1, + B = 0, + C = 2, + D = 3, + E = 4, + F = 5, + G = 6, + H = 7, + I = 8 +}; + +#include "gpio_helper.h" + +void keyboard_init(); +bool keyboard_scan(); + +#endif \ No newline at end of file diff --git a/numworks_port/src/Laplace/laplace.c b/numworks_port/src/Laplace/laplace.c index 4edfdd9..deb4ae3 100644 --- a/numworks_port/src/Laplace/laplace.c +++ b/numworks_port/src/Laplace/laplace.c @@ -4,4 +4,9 @@ void laplace_init(){ // Enable the GPIO peripheral in RCC. enable_gpio_x_rcc(GPIO_B); led_init(); + //col (in) + enable_gpio_x_rcc(GPIO_C); + //row (out) + enable_gpio_x_rcc(GPIO_A); + keyboard_init(); } \ No newline at end of file diff --git a/numworks_port/src/Laplace/laplace.h b/numworks_port/src/Laplace/laplace.h index d775efb..626bf7e 100644 --- a/numworks_port/src/Laplace/laplace.h +++ b/numworks_port/src/Laplace/laplace.h @@ -9,6 +9,7 @@ #include "gpio_helper.h" #include "led.h" +#include "keyboard.h" void laplace_init(); diff --git a/numworks_port/src/Laplace/time.c b/numworks_port/src/Laplace/time.c new file mode 100644 index 0000000..c257d18 --- /dev/null +++ b/numworks_port/src/Laplace/time.c @@ -0,0 +1,13 @@ +#include "time.h" + +void us_wait(uint8_t micro_seconds) { + for (volatile uint32_t i=0; i