Add Zadig 101 to docs (#6585)
[jackhill/qmk/firmware.git] / docs / hardware_avr.md
CommitLineData
7b0356d1 1# Keyboards with AVR Processors
67cc5ceb 2
3This page describes the support for for AVR processors in QMK. AVR processors include the atmega32u4, atmega32u2, at90usb1286, and other processors from Atmel Corporation. AVR processors are 8-bit MCU's that are designed to be easy to work with. The most common AVR processors in keyboards have on-board USB and plenty of GPIO for supporting large keyboard matrices. They are the most popular MCU for use in keyboards today.
4
5If you have not yet you should read the [Keyboard Guidelines](hardware_keyboard_guidelines.md) to get a sense of how keyboards fit into QMK.
6
7## Adding Your AVR Keyboard to QMK
8
53c51f1d 9QMK has a number of features to simplify working with AVR keyboards. For most keyboards you don't have to write a single line of code. To get started, run the `util/new_keyboard.sh` script:
10
11```
12$ ./util/new_keyboard.sh
13Generating a new QMK keyboard directory
14
15Keyboard Name: mycoolkb
16Keyboard Type [avr]:
17Your Name [John Smith]:
18
19Copying base template files... done
20Copying avr template files... done
21Renaming keyboard files... done
22Replacing %KEYBOARD% with mycoolkb... done
23Replacing %YOUR_NAME% with John Smith... done
24
25Created a new keyboard called mycoolkb.
26
27To start working on things, cd into keyboards/mycoolkb,
28or open the directory in your favourite text editor.
67cc5ceb 29```
30
31This will create all the files needed to support your new keyboard, and populate the settings with default values. Now you just need to customize it for your keyboard.
32
33## `readme.md`
34
35This is where you'll describe your keyboard. Please follow the [Keyboard Readme Template](documentation_templates.md#keyboard-readmemd-template) when writing your `readme.md`. You're encouraged to place an image at the top of your `readme.md`, please use an external service such as [Imgur](http://imgur.com) to host the images.
36
37## `<keyboard>.c`
38
39This is where all the custom logic for your keyboard goes. Many keyboards do not need to put anything at all in here. You can learn more about writing custom logic in [Custom Quantum Functions](custom_quantum_functions.md).
40
41## `<keyboard>.h`
42
af37bb2f 43This is the file you define your [Layout Macro(s)](feature_layouts.md) in. At minimum you should have a `#define LAYOUT` for your keyboard that looks something like this:
67cc5ceb 44
0e437404 45```c
67cc5ceb 46#define LAYOUT( \
47 k00, k01, k02, \
48 k10, k11 \
49) { \
50 { k00, k01, k02 }, \
51 { k10, KC_NO, k11 }, \
52}
53```
54
55The first half of the `LAYOUT` pre-processor macro defines the physical arrangement of keys. The second half of the macro defines the matrix the switches are connected to. This allows you to have a physical arrangement of keys that differs from the wiring matrix.
56
57Each of the `k__` variables needs to be unique, and typically they follow the format `k<row><col>`.
58
af37bb2f 59The physical matrix (the second half) must have a number of rows equaling `MATRIX_ROWS`, and each row must have exactly `MATRIX_COLS` elements in it. If you do not have this many physical keys you can use `KC_NO` to fill in the blank spots.
67cc5ceb 60
61## `config.h`
62
63The `config.h` file is where you configure the hardware and feature set for your keyboard. There are a lot of options that can be placed in that file, too many to list there. For a complete overview of available options see the [Config Options](config_options.md) page.
64
65### Hardware Configuration
66
67
68At the top of the `config.h` you'll find USB related settings. These control how your keyboard appears to the Operating System. If you don't have a good reason to change you should leave the `VENDOR_ID` as `0xFEED`. For the `PRODUCT_ID` you should pick a number that is not yet in use.
69
70Do change the `MANUFACTURER`, `PRODUCT`, and `DESCRIPTION` lines to accurately reflect your keyboard.
71
0e437404 72```c
67cc5ceb 73#define VENDOR_ID 0xFEED
74#define PRODUCT_ID 0x6060
75#define DEVICE_VER 0x0001
76#define MANUFACTURER You
77#define PRODUCT my_awesome_keyboard
78#define DESCRIPTION A custom keyboard
79```
80
ddaf37ff 81?> Windows and macOS will display the `MANUFACTURER` and `PRODUCT` in the list of USB devices. `lsusb` on Linux instead takes these from the list maintained by the [USB ID Repository](http://www.linux-usb.org/usb-ids.html) by default. `lsusb -v` will show the values reported by the device, and they are also present in kernel logs after plugging it in.
67cc5ceb 82
83### Keyboard Matrix Configuration
84
85The next section of the `config.h` file deals with your keyboard's matrix. The first thing you should set is the matrix's size. This is usually, but not always, the same number of rows and columns as the physical key arrangement.
86
0e437404 87```c
67cc5ceb 88#define MATRIX_ROWS 2
89#define MATRIX_COLS 3
90```
91
92Once you've defined the size of your matrix you need to define which pins on your MCU are connected to rows and columns. To do so simply specify the names of those pins:
93
0e437404 94```c
67cc5ceb 95#define MATRIX_ROW_PINS { D0, D5 }
96#define MATRIX_COL_PINS { F1, F0, B0 }
97#define UNUSED_PINS
98```
99
100The number of `MATRIX_ROW_PINS` entries must be the same as the number you assigned to `MATRIX_ROWS`, and likewise for `MATRIX_COL_PINS` and `MATRIX_COLS`. You do not have to specify `UNUSED_PINS`, but you can if you want to document what pins are open.
101
fc069869 102Finally, you can specify the direction your diodes point. This can be `COL2ROW` or `ROW2COL`.
67cc5ceb 103
0e437404 104```c
67cc5ceb 105#define DIODE_DIRECTION COL2ROW
106```
107
0137b023 108#### Direct Pin Matrix
109To configure a keyboard where each switch is connected to a separate pin and ground instead of sharing row and column pins, use `DIRECT_PINS`. The mapping defines the pins of each switch in rows and columns, from left to right. Must conform to the sizes within `MATRIX_ROWS` and `MATRIX_COLS`, use `NO_PIN` to fill in blank spaces. Overrides the behaviour of `DIODE_DIRECTION`, `MATRIX_ROW_PINS` and `MATRIX_COL_PINS`.
110
111```c
112// #define MATRIX_ROW_PINS { D0, D5 }
113// #define MATRIX_COL_PINS { F1, F0, B0 }
114#define DIRECT_PINS { \
115 { F1, E6, B0, B2, B3 }, \
116 { F5, F0, B1, B7, D2 }, \
117 { F6, F7, C7, D5, D3 }, \
118 { B5, C6, B6, NO_PIN, NO_PIN } \
119}
120#define UNUSED_PINS
121
122/* COL2ROW, ROW2COL */
123//#define DIODE_DIRECTION
124```
125
67cc5ceb 126### Backlight Configuration
127
4d72aa42 128QMK supports backlighting on most GPIO pins. A select few of these can be driven by the MCU in hardware. For more details see the [Backlight Documentation](feature_backlight.md).
67cc5ceb 129
0e437404 130```c
67cc5ceb 131#define BACKLIGHT_PIN B7
67cc5ceb 132#define BACKLIGHT_LEVELS 3
4931510a
BG
133#define BACKLIGHT_BREATHING
134#define BREATHING_PERIOD 6
67cc5ceb 135```
136
67cc5ceb 137### Other Configuration Options
138
139There are a lot of features that can be configured or tuned in `config.h`. You should see the [Config Options](config_options.md) page for more details.
140
141## `rules.mk`
142
143You use the `rules.mk` file to tell QMK what files to build and what features to enable. If you are building around an atmega32u4 you can largely leave these defaults alone. If you are using another MCU you may have to tweak some parameters.
144
145### MCU Options
146
147These options tell the build system what CPU to build for. Be very careful if you change any of these settings, you can render your keyboard inoperable.
148
0e437404 149```make
67cc5ceb 150MCU = atmega32u4
151F_CPU = 16000000
152ARCH = AVR8
153F_USB = $(F_CPU)
154OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
155```
156
0e437404 157### Bootloaders
67cc5ceb 158
0e437404 159The bootloader is a special section of your MCU that allows you to upgrade the code stored on the MCU. Think of it like a Rescue Partition for your keyboard.
67cc5ceb 160
0e437404 161#### Teensy Bootloader Example
67cc5ceb 162
0e437404
DJ
163```make
164BOOTLOADER = halfkay
67cc5ceb 165```
166
0e437404 167#### Atmel DFU Loader Example
67cc5ceb 168
0e437404
DJ
169```make
170BOOTLOADER = atmel-dfu
67cc5ceb 171```
172
0e437404 173#### Pro Micro Bootloader Example
67cc5ceb 174
0e437404
DJ
175```make
176BOOTLOADER = caterina
67cc5ceb 177```
178
179### Build Options
180
181There are a number of features that can be turned on or off in `rules.mk`. See the [Config Options](config_options.md#feature-options) page for a detailed list and description.