# TOSR-04
## The board :
![](/images/git/TOSR-04.jpg)

**The purpose of this project is to write and flash a custom firmware in the MCU powering this board.
Indeed, this relay board was meant to be controled by sending very poorly designed serial commands which I want to get rid of !**

## The manual :
**The user guide is available [here](/git/Th3maz1ng/TOSR-04/src/branch/master/documentation/TOSR-0X%20USER%20MANUAL.pdf) if you want to learn more about this module's capabilities**

## The schematic :
**The schematic is available [here](/git/Th3maz1ng/TOSR-04/src/branch/master/schematic/TOSR-04.pdf).**

## Project folder architecture :
``` json
/
|_src                   This folder contains all the C/C++ file sources.
| \_app                 This folder contains the main app and it's dependencies.
| |_libs                This folder contains all the required 3rd party libraries that should be put in "Arduino\libraries" folder in order for the app to compile.
| |_test                This folder contains a test program which was used to test the dependencies.
|
|_schematic             This folder contains all the files associated with the schematic 
| \_KiCad               This folder contains the KiCad files associated with the project.
| |_TOSR-04.pdf         Which is the most recent schematic exported as a pdf for quick viewing.
|
|_documentation         This folder contains all documentation and resources to work on the project.
| 
|_.gitignore
|_LICENSE.md
|_README.md             The content of this page
```

## What I am planning to do :
1. Reverse engineer the schematic of this board (because it is not available) (DONE !)
    - The [schematic](/git/Th3maz1ng/TOSR-04/src/branch/master/schematic/TOSR-04.pdf) is now available as a pdf file in the **schematic** folder
2. Find the best way to flash a custom firmware on the ATMEGA328PU (DONE !)
    - Check the section : **The best way to flash a firmware** section hereunder 
3. Write a custom firmware for it (TO DO)

## The best way to flash a firmware
To be honest, the best way to flash our own firmware on this board is to replace the ATMEGA328PU with one which has the Arduino bootloader burnt on it :-(.<br />
Indeed after looking at the reverse engineered schematic, the SPI pins needed for an ISP are not exposed (they are not even used...).<br />
Fortunately enough, the onboard FTDI seems to be connected with the DTR line which is connected through a capacitor to the ATMEGA's reset pin that is required by the Arduino bootloader.<br />
I will test it this weekend and keep you informed.<br />
**The test is done and IT WORKS !!!**. 
1. I took an Arduino pro mini 3.3V 8MHz. I desoldered the ATMEGA328PU that was on it and soldered it on the relay board.
2. I then connected the relay board to the PC with an USB cable and started the Arduino IDE.
3. In the tools menu of the IDE I chose the Arduino UNO board and I flashed the blink sketch.
4. The download process worked and the led L (which is pin 13 luckily) started blinking as expected.
5. We can now write our own firmware.

## Could the onboard ATMEGA be saved ?
I will try to restore the chip to it's factory state by programming it with a HVPP (High Voltage Parallel Programmer) in order to reset the fuse bytes as well if they are set of course ...<br />
If I succeed, I'll explain how I did it here.

## FAQ
1) Why is it not possible to reprogram the chip or to burn a bootloader on the relay board ?
    - The first issue is that the pins required for an ISP are not exposed :(. The way around it would be to parallel program it, but for that you need to desolder the chip anyway.
    - The second issue is that the fuse bytes are probably set (I dont actually know for sure) in a way to prevent easy access to the chip. Only a High Voltage Reprogramming of the chip could "unlock it".

***If you have any questions, do not hesitate to contact me at : bugreport[at]laposte[dot]net***
