Update Protocols_Details.md
[DIY-Multiprotocol-TX-Module.git] / docs / Compiling.md
blobcf2f9256baf8e11a2aa82caf4c52b2bed4f8ae90
1 # Compiling and Programming (ATmega328p)
3 Multiprotocol firmware is compiled using the Arduino IDE.  The guide below will walk you through all the steps to compile and upload your customized firmware.
5 **These instructions are for the Atmega328p version of the Multiprotocol module.**  If you are compling for the STM32 version please go to the dedicated [STM32](Compiling_STM32.md) page.
7 ## Index
8 1. [Tools Required](#tools-required)
9 1. [Preparation](#preparation)
10    1. [Install the Arduino IDE](#install-the-arduino-ide)
11    1. [Download the Multiprotocol source and open the project](#download-the-multiprotocol-source-and-open-the-project)
12    1. [Install the Multi 4-in-1 board](#install-the-multi-4-in-1-board)
13    1. [Configure the Arduino IDE](#configure-the-arduino-ide)
14 1. [Configure the firmware](#configure-the-firmware)
15    1. [Customize the firmware to match your hardware and your needs](#customize-the-firmware-to-match-your-hardware-and-your-needs)
16    1. [Verify the firmware](#verify-the-firmware)
17 1. [Compiling and uploading the firmware](#compiling-and-uploading-the-firmware)
18    1. [Connect the programmer](#connect-the-programmer)
19    1. [Burn bootloader and set fuses](#burn-bootloader-and-set-fuses)
20    1. [Upload the firmware](#upload-the-firmware)
21       1. [Flash from TX](#flash-from-tx)
22       1. [Upload using Arduino IDE](#upload-using-arduino-ide)
23 1. [Troubleshooting](#troubleshooting)
25 ## Tools required
27 | **3.3V USBasp Programmer** | **10-pin to 6-pin Adapter** | **6-pin header** |
28 |:---:|:---:|:---:|
29 | <img src="images/USBasp_Programmer.jpeg" width="200" height="200"/> | <img src="images/10pin_2_6pin.JPG" width="150" height="150"/> | <img src="images/6pin_header.jpg" width="100" height="100"/> |
30 | [(example aliexpress link)](https://www.aliexpress.com/item/USBasp-USB-ISP-3-3V-5V-AVR-Programmer-USB-ATMEGA8-ATMEGA128-New-10PIN-Wire-Support/2036402518.html?spm=2114.30010308.8.10.jIbHzs) | [(example ebay link)](http://www.ebay.fr/itm/10-Pin-a-6-Pin-Carte-Adaptateur-M-F-pour-AVRISP-USBASP-STK500-Noir-Bleu-WT-/291862396761?hash=item43f45abf59:g:gXsAAOSwMgdXyGnh) | [(example Digi-Key link)](http://www.digikey.com/products/en?keywords=3M%20961206-6404-AR) |
32 **Important:** The USBasp **must** be **3.3V**.  Using a 5V USBasp (most blue PCB type) will fry the RF modules as they are not 5V tolerant.
34 **Tip**: You can cut or remove the VCC line on your USBasp 6-pin adapter or ribbon cable and power the module from the radio when flashing to ensure that it receives the correct voltage.
36 The 6-pin header needs to be soldered onto the board as indicated by the red rectangle:
38 | **Banggood 4-in-1 Module** | **DIY Multiprotocol Module** | **Arduino Pro Mini Module** |
39 |:---:|:---:|:---:|
40 | <img src="images/V2b_ISP.jpeg" width="189" height="200"/> | <img src="images/MPTM_PCB_2.3d_ISP.png" width="486" height="201"/> | <img src="images/ProMini_ISP.png" width="195" height="200"/> |
42 ## Preparation
43 ### Install the Arduino IDE
44 1. Download and install the Arduino IDE. The currently supported Arduino version is 1.8.5, available for [Windows]( https://www.arduino.cc/download_handler.php?f=/arduino-1.8.5-windows.exe) and [Mac OSX](https://www.arduino.cc/download_handler.php?f=/arduino-1.8.5-macosx.zip)
45 1. It is recommended to upgrade Java to the [latest version](https://www.java.com/en/download/)
47 ### Download the Multiprotocol source and open the project
48 1. Either
49    1. Download the zip file with the Multiprotocol module source code from [here](https://github.com/pascallanger/DIY-Multiprotocol-TX-Module/archive/master.zip) and unzip and copy the source code folder **Multiprotocol** to a location of your choosing, or
50    1. Clone the project using Git or Github Desktop, then
51 1. Double-click the **Multiprotocol.ino** file in the **Multiprotocol** folder to open the project in the Arduino IDE
53 **Important note for Windows users:** You must download or unzip the Multiprotocol source in a folder which has no spaces in the path.  If you have spaces in your username **do not** use a sub-folder of your user directory.  This is due to a [bug in the Arduino IDE](https://github.com/arduino/arduino-builder/issues/316), caused by an [issue in Go](https://github.com/golang/go/issues/17149).
55 ### Install the Multi 4-in-1 board
56 1. Follow [these instructions](Arduino_IDE_Boards.md) to install the **Multi 4-in-1 AVR Board** in the Arduino IDE
58 ### Configure the Arduino IDE
59 1. Under **Tools -> Board** select **'Multi 4-in-1 (Atmega328p, 3.3V, 16MHz)**
60 1. Under **Tools -> Programmer** select **USBasp**
62 ## Configure the firmware
63 ### Customize the firmware to match your hardware and your needs
64 All customization is done by editing the ```_Config.h  ``` file in the Multiprotocol Arduino project.  
66 In the Arduino IDE, click on the down arrow on the far right of the tab bar to show a list of project files (see the red circle on the screenshot below).  Scroll down and select the _Config.h file. <br> <img src="images/Arduino.png" width="600" height="400" />
68 The file has different sections which are explained in details. The best is to go through them one by one carefully and apply the configuration which matches your needs.
70 Most of the default settings should get you started quickly. But on modules with ATMega microcontrollers, the memory required for all the protocols exceeds the available 32KB of flash memory.  You therefore need to select which protocols you wish to use in order to fit them into the available memory.
72 To fill in the "PROTOCOLS TO INCLUDE" section, it would be good to review all the available protocols on the [Protocol Details](../Protocols_Details.md) page and identify which one you would like to add on your module.  
74 ### Verify the firmware
76 To check that the program will compile correctly and fit in the Atmega click **Sketch -> Verify/Compile**, or press **Ctrl+R**.
78 If you see something like the following, your firmware is still too big and you need to deselect additional protocols:
79 ```
80 Sketch uses 42032 bytes (128%) of program storage space. Maximum is 32768 bytes.
82 Global variables use 1180 bytes (57%) of dynamic memory, leaving 868 bytes for local variables. Maximum is 2048 bytes.
83 Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.
84 Error compiling for board Multi 4-in-1 (Atmega328p, 3.3V, 16MHz).
85 ```
87 If there is another error carefully read it, go to the line number indicated and correct your typo.
89 If there are no errors and you see output like this:
90 ```
91 Sketch uses 31874 bytes (97%) of program storage space. Maximum is 32768 bytes.
92 Global variables use 1083 bytes (52%) of dynamic memory, leaving 965 bytes for local variables. Maximum is 2048 bytes.
93 ```
94 You can proceed to the next step.
96 ## Compiling and uploading the firmware
97 If you have already burned the bootloader, and are simply recompiling firmware to re-flash using your TX, you can skip straight to [Flash from TX](#flash-from-tx).
99 ### Connect the programmer
100 1. Before you connect the programmer make sure that you have selected the 3.3V mode and not 5V. The RF Modules are not 5V tolerant and you will break them with 5V.  On most programmers this is done by moving a jumper. <br> <img src="images/USBasp_Programmer_jumper.png" width="200" height="200" />
101 1. Please re-read item 1. above before going on, it's important.
102 1. If your module has a rotary switch, set it to the 0 position. The upload will not work if the switch is in any other position.
103 1. Connect the 6-pin programming connector to the 6-pin ASP IVR connector on the DIY Multiprotocol board. Be sure to match the ground pin of the programmer connector to the ground pin on the board.
105 The images below indicate the pin layout and the location of the ground pin on the board:
107 | **Banggood 4-in-1 Module** | **DIY Multiprotocol Module** | **Arduino Pro Mini Module** |
108 |:---:|:---:|:---:|
109 <img src="images/V2b_ISP.jpeg" width="189" height="200" /> | <img src="images/MPTM_PCB_2.3d_ISP.png" width="486" height="201" /> | <img src="images/ProMini_ISP.png" width="195" height="200" /> |
111 You are now ready to plug in the USB programmer to the computer.  If you are looking for a good working USBasp Windows driver, [use this one](http://www.protostack.com/download/USBasp-win-driver-x86-x64-v3.0.7.zip).
113 ### Burn bootloader and set fuses
114 The bootloader only needs to be burned once, unless you decide to switch from one option to the other (or it is accidentally erased).  If you have already burned the bootloader / set the fuses you can skip this step.
116 There are two bootloader options:
117    * **'No bootloader'** maximises flash space for protocols
118    * **'Flash from TX'** installs a small (512 byte) bootloader which allows flashing the module firmware using from a radio running ersky9x
120 **Note:** 'Burning the bootloader' is necessary even if the 'No bootloader' option is selected, as it sets the fuses on the AVR module.  This only needs to be once (unless you decide to change your bootloader choice later)
122 1. Under **Tools -> Bootloader** select a bootloader
123 1. Click on **Tools -> Burn Bootloader** 
125 ### Upload the firmware
126 You are now ready to upload the firmware to the multiprotocol module.  There are two methods for uploading the firmware:
127 * **Flash from TX** - uses the bootloader mode of radios running ersky9x or OpenTX to upload the firmware. The radio needs to run the latest bootloader with the Flash Multi app.
128 * **Upload using Arduino IDE** uses the Arduino IDE and the USBasp programmer to upload the firmware
130 #### Flash from TX
131 1. The MPM module must have the 'Flash from TX' bootloader installed
132 1. In the Arduino IDE click **Sketch -> Export compiled Binary**, or press **Ctrl+Alt+S**
133 1. Locate the file named **multi-avr-x.x.x.x.hex** in the **Multiprotocol source folder** (x.x.x.x is the multi version)
134 1. Follow the instructions [here](/docs/Flash_from_Tx.md) to upload the firmware using your radio
135 1. Once the previous step is completed your module is ready to be used
137 #### Upload using Arduino IDE
138 **Note:** If you have burned the 'Upload from TX' bootloader and you then upload firmware to your module using **Upload Using Programmer**, you will erase the bootloader.  That's just the way the Arduino IDE works - avrdude will erase the entire flash memory prior to writing the new code, *including the bootloader*, and the upload will not put it back.  If this happens you can [burn it again](#burn-bootloader-and-set-fuses).
140 1. In the Arduino IDE click **Sketch -> Upload Using Programmer**, or press **Ctrl+Shift+U**.
142 If the output indicates that the firmware has been uploaded successfully - give yourself a pat on the back.  Well done, you have successfully programmed your DIY Multiprotocol module. You can already go to the final step [Setting up your Transmitter](Transmitters.md#compatible-transmitters) and begin to fly!!!!
144 ## Troubleshooting
145 If you get an error that indicates "warning : Can not Set sck period . usbasp please check for firmware update ." just ignore it, everything is fine.  Don't be tempted to 'upgrade' your USBasp firmware to try to get rid of this message - if you do, you will find that the USBasp is no longer able to flash your multiprotocol module.
147 If you get an error that indicates a valid microcontroller was not found there is something wrong with:
148  * your connections, 
149  * your programmer, or
150  * your board