paul@0 | 1 | /* |
paul@0 | 2 | * Export the keypad GPIOs on the Letux 400 as a data space accessible via the |
paul@0 | 3 | * "keypad" capability. |
paul@0 | 4 | * |
paul@285 | 5 | * Copyright (C) 2018, 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | * |
paul@0 | 7 | * This program is free software; you can redistribute it and/or |
paul@0 | 8 | * modify it under the terms of the GNU General Public License as |
paul@0 | 9 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 10 | * the License, or (at your option) any later version. |
paul@0 | 11 | * |
paul@0 | 12 | * This program is distributed in the hope that it will be useful, |
paul@0 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 15 | * GNU General Public License for more details. |
paul@0 | 16 | * |
paul@0 | 17 | * You should have received a copy of the GNU General Public License |
paul@0 | 18 | * along with this program; if not, write to the Free Software |
paul@0 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 20 | * Boston, MA 02110-1301, USA |
paul@0 | 21 | */ |
paul@0 | 22 | |
paul@0 | 23 | #include <l4/devices/gpio-jz4730.h> |
paul@0 | 24 | #include <l4/devices/dataspace.h> |
paul@0 | 25 | #include <l4/devices/memory.h> |
paul@0 | 26 | |
paul@153 | 27 | #include <l4/re/c/dataspace.h> |
paul@153 | 28 | #include <l4/re/env.h> |
paul@0 | 29 | |
paul@0 | 30 | #include <l4/sys/cache.h> |
paul@0 | 31 | |
paul@0 | 32 | #include <stdint.h> |
paul@0 | 33 | |
paul@153 | 34 | #include "keypad-event-loop.h" |
paul@153 | 35 | #include "keypad-run.h" |
paul@153 | 36 | |
paul@153 | 37 | |
paul@153 | 38 | |
paul@0 | 39 | enum Jz4730_keypad_gpio |
paul@0 | 40 | { |
paul@0 | 41 | Jz4730_keypad_gpio_inputs_count = 8, |
paul@0 | 42 | Jz4730_keypad_gpio_outputs_count = 17, |
paul@0 | 43 | }; |
paul@0 | 44 | |
paul@0 | 45 | /* Port A input pins. */ |
paul@0 | 46 | |
paul@0 | 47 | const uint8_t Jz4730_keypad_inputs[Jz4730_keypad_gpio_inputs_count] = { |
paul@0 | 48 | 0, 1, 2, 3, 4, 5, 6, 7 |
paul@0 | 49 | }; |
paul@0 | 50 | |
paul@0 | 51 | const Pin_slice Jz4730_keypad_inputs_mask = {0x000000ff, 0}; |
paul@0 | 52 | |
paul@0 | 53 | /* Port D output pins. */ |
paul@0 | 54 | |
paul@0 | 55 | const uint8_t Jz4730_keypad_outputs[Jz4730_keypad_gpio_outputs_count] = { |
paul@0 | 56 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 29 |
paul@0 | 57 | }; |
paul@0 | 58 | |
paul@0 | 59 | const Pin_slice Jz4730_keypad_outputs_mask = {0x2000ffff, 0}; |
paul@0 | 60 | |
paul@0 | 61 | /* Peripheral memory regions. */ |
paul@0 | 62 | |
paul@0 | 63 | static l4_addr_t gpio_virt_base = 0, gpio_virt_base_end = 0; |
paul@0 | 64 | |
paul@0 | 65 | /* GPIO abstractions. */ |
paul@0 | 66 | |
paul@0 | 67 | static void *gpio_port_a, *gpio_port_d; |
paul@0 | 68 | |
paul@0 | 69 | /* Keypad status: an array of keypad column values. */ |
paul@0 | 70 | |
paul@0 | 71 | uint32_t *keypad = 0; |
paul@0 | 72 | |
paul@0 | 73 | /* Imported keypad memory referenced by the array. */ |
paul@0 | 74 | |
paul@0 | 75 | void *keymem = 0; |
paul@0 | 76 | |
paul@11 | 77 | |
paul@11 | 78 | |
paul@11 | 79 | /* Set up access to memory. */ |
paul@0 | 80 | |
paul@11 | 81 | static int setup_memory() |
paul@11 | 82 | { |
paul@11 | 83 | if (get_memory("jz4730-gpio", &gpio_virt_base, &gpio_virt_base_end) < 0) |
paul@11 | 84 | return 1; |
paul@11 | 85 | |
paul@285 | 86 | gpio_port_a = jz4730_gpio_init(gpio_virt_base, 0); |
paul@285 | 87 | gpio_port_d = jz4730_gpio_init(gpio_virt_base, 3); |
paul@11 | 88 | |
paul@11 | 89 | return 0; |
paul@11 | 90 | } |
paul@0 | 91 | |
paul@0 | 92 | |
paul@0 | 93 | |
paul@0 | 94 | /* Initialise the pins for scanning the keypad. */ |
paul@0 | 95 | |
paul@11 | 96 | static void init_keyscan() |
paul@0 | 97 | { |
paul@0 | 98 | jz4730_gpio_multi_setup(gpio_port_a, &Jz4730_keypad_inputs_mask, Hw::Gpio_chip::Input, 0); |
paul@0 | 99 | jz4730_gpio_multi_config_pull(gpio_port_a, &Jz4730_keypad_inputs_mask, Hw::Gpio_chip::Pull_up); |
paul@0 | 100 | jz4730_gpio_multi_setup(gpio_port_d, &Jz4730_keypad_outputs_mask, Hw::Gpio_chip::Input, 0); |
paul@0 | 101 | } |
paul@0 | 102 | |
paul@0 | 103 | /* |
paul@0 | 104 | Scan the keypad by enabling each output column and inspecting each input row. |
paul@0 | 105 | Store each column bitmap in the keypad array. |
paul@0 | 106 | */ |
paul@0 | 107 | |
paul@11 | 108 | static void scan_keypad() |
paul@0 | 109 | { |
paul@0 | 110 | uint8_t column, row, value; |
paul@0 | 111 | |
paul@0 | 112 | for (column = 0; column < Jz4730_keypad_gpio_outputs_count; column++) |
paul@0 | 113 | { |
paul@0 | 114 | jz4730_gpio_setup(gpio_port_d, Jz4730_keypad_outputs[column], Hw::Gpio_chip::Output, 0); |
paul@0 | 115 | l4_sleep(1); |
paul@0 | 116 | |
paul@0 | 117 | value = 0; |
paul@0 | 118 | |
paul@0 | 119 | for (row = 0; row < Jz4730_keypad_gpio_inputs_count; row++) |
paul@0 | 120 | value = (value << 1) | (jz4730_gpio_get(gpio_port_a, Jz4730_keypad_inputs[row]) ? 0 : 1); |
paul@0 | 121 | |
paul@0 | 122 | keypad[column] = value; |
paul@0 | 123 | |
paul@0 | 124 | jz4730_gpio_setup(gpio_port_d, Jz4730_keypad_outputs[column], Hw::Gpio_chip::Input, 0); |
paul@0 | 125 | } |
paul@0 | 126 | |
paul@0 | 127 | l4_cache_clean_data((unsigned long) keypad, |
paul@0 | 128 | (unsigned long) keypad + Jz4730_keypad_gpio_outputs_count); |
paul@0 | 129 | } |
paul@0 | 130 | |
paul@0 | 131 | |
paul@0 | 132 | |
paul@0 | 133 | /* Main program. */ |
paul@0 | 134 | |
paul@0 | 135 | int main(void) |
paul@0 | 136 | { |
paul@153 | 137 | if (setup_memory()) |
paul@153 | 138 | return 1; |
paul@153 | 139 | |
paul@0 | 140 | /* Memory allocation capability for the keypad data. */ |
paul@0 | 141 | |
paul@0 | 142 | l4_size_t mem_size = Jz4730_keypad_gpio_outputs_count * sizeof(uint32_t); |
paul@153 | 143 | l4re_ds_t mem = allocate_data(mem_size, &keymem); |
paul@0 | 144 | |
paul@153 | 145 | if (l4_is_invalid_cap(mem)) |
paul@153 | 146 | return 1; |
paul@0 | 147 | |
paul@0 | 148 | keypad = (uint32_t *) keymem; |
paul@0 | 149 | |
paul@0 | 150 | /* Set up keypad access and start scanning. */ |
paul@0 | 151 | |
paul@0 | 152 | init_keyscan(); |
paul@0 | 153 | |
paul@0 | 154 | /* Set up a thread to scan the keypad concurrently with the server loop. */ |
paul@0 | 155 | |
paul@11 | 156 | Keypad_event_loop loop; |
paul@11 | 157 | loop.attach(scan_keypad); |
paul@11 | 158 | loop.start(); |
paul@0 | 159 | |
paul@153 | 160 | /* Start a server using the common server functionality. */ |
paul@0 | 161 | |
paul@153 | 162 | return run(mem); |
paul@0 | 163 | } |