# ATTiny co-processor

<abstract>
This article explains in detail the functions performed by the Microchip ATTiny88 co-processor present on the CM-Panel POE
</abstract>

Microchip ATTiny88 in an AVR RISC-based CMOS 8-bit microcontroller with:

* 8 KB ISP Flash memory
* 6 4B EEPROM
* 512B SRAM 
* 28 general purpose I/O lines 

More info are available on the Microchip ATTiny88 page at <https://www.microchip.com/en-us/product/attiny88>.

This processor is programmed in-factory to perform the following functions:

* Measure each 1s the on-board voltages and display a report on the serial debug port
* Manage the correct shutdown in case of power failure to avoid the microSD curruption
* Indicate the state of charge of the supercap using the RGB led
* Manage the state of display power supply circuits
* Send a signal via a GPIO line to shutdown Linux

The firmware used is this:

* [pwr.ino](./pwr.ino)

This article explains how to compile it and flash the ATTiny with your own version.

## Signals wired to the co-processor

These are the GPIO lines used by the ATTiny and their respective purposes:  

| PIN | DIR    | NAME          | USE  | ACTIVE | CMx GPIO |
|-----|--------|---------------|------|--------|----------|
| PA0 | ADC    | MeasBacklit   |      |
| PA1 | ADC    | MeasVSC       |      |
| PA2 |        | Main5V        |      |
| PA3 |        | Flag4         |      |
| PB0 |        | Flag1		   | N.C. |
| PB1 |        | Flag2         | N.C. |
| PB2 |        | Flag3         | N.C. |
| PB3 |        | MOSI          |      |
| PB4 |        | MISO          |      |
| PB5 |        | SCK           |      |
| PB6 | OUT    | Disp On       | Display ON   | High | 27 |
| PB7 | OUT    | KB            | RGB led blue | Low |
| PC0 | ADC    | Meas1V8       |      |
| PC1 | ADC    | Meas3V3       |      |
| PC2 | ADC    | Meas5VExt     |      |
| PC3 | ADC    | Meas5VInt     |      |
| PC4 |        | micro_SDA     |      |
| PC5 |        | micro_SCL     |      |
| PC6 |        | NRST          |      |
| PC7 | IN     | Touch_INT     |      | Low |
| PD0 | OUT    | GOSHDN        | Shutdown command to CMx module | Low  | 25 |
| PD1 | IN     | SHDNDONE      | Shutdown done by CMx module    | High | 26 |
| PD2 | IN     | INT1#         | Interrupt from RTC             | Low  |    |
| PD3 | IN     | Pushbutton    | P2 Button (Start/stop) | Low |
| PD4 | OUT    | KG            | RGB led green | Low |
| PD5 | OUT    | KR            | RGB led red   | Low |
| PD6 | OUT    | Txdata        | Debug port TX |
| PD7 | IN     | Rxdata        | Debug port RX |
     
## Install the Arduino IDE to program the ATTiny88 co-processor

Download and install the Arduino IDE from the official Arduino website. We used Legacy IDE 1.8.19 to write this article.

* <https://www.arduino.cc/en/software>

add the ATtiny support to the Arduino IDE ny navigating to <code>Arduino > Preferences... > Additional Board Manager URLs:</code> and
add the following URL for the ATtiny core by Spence Konde (DrAzzy):

	http://drazzy.com/package_drazzy.com_index.json

Click "OK" to close the Preferences window.

Open the Board Manager by navigating to <code>Tools > Board > Boards Manager...</code>.

In the Boards Manager window search for <code>ATtiny</code> and select <code>ATTinyCode by Spence Konde</code> then press <button>Install</button> then 
<button>Close</button>.

Now navigate to <code>Tools > Boards > ATTinyCore > ATtiny48/88 (Optiboot)</code>

Configure the processor settings

* <code>Tools > Chip > ATTiny88</code>
* <code>Tools > Clock Source > 4MHz (Internal)</code>

Copy inside the editor this sketch:

* [pwr.ino](./pwr.ino)

then select the menu <code>Sketch > Verify/Compile</code>

	Sketch uses 4342 bytes (53%) of program storage space. Maximum is 8192 bytes.
	Global variables use 292 bytes (57%) of dynamic memory, leaving 220 bytes for local variables. Maximum is 512 bytes.
	
then <code>Sketch > Export compiled Binary</code>.

then <code>Sketch > Show sketch folder</code> and check that the file <code>pwr.ino.hex</code> exist.

If you have a Tiny USB programmer you code inject the code directly by hardware to the co-processor using the J4 Prog port on CM-Panel POE.

If not transfer the file <code>pwr.ino.hex</code> to the CM-Panel POE microSH using SSH and program the co-processor using <code>avrdude</code>.

## How to inject the code inside the ATTiny88 co-processor

There are some ways to transfer the binary code inside the ATTiny flash memory:

1. Using the Tiny USB cheap ISP programmer and Arduino IDE
1. Using the Tiny USB cheap ISP programmer and the CM-Panel itself
1. Using an USB to serial converter and the Arduino IDE
1. Using an USB to serial converter and the CM-Panel itself

Methods 3 and 4 are possible because the ATTiny become already programmed with the ATTiny firmware + Optiboot loader 
<br/>
Methods 1 and 2 works also with a fully blank ATTiny

<img src="tinyusb.jpg" width="420px"> 

In case of ISP using the CM-Panel itself install <code>avrdude</code> by typing:

	$ sudo apt update
	$ sudo apt install avrdude

Download the avrdude configuration file from our site by typing:

	$ wget https://www.acmesystems.it/www/cmpanel-poe_attiny/avrdude.conf	

Download the ATTiny firmware from our site by typing:

	$ wget https://www.acmesystems.it/www/cmpanel-poe_attiny/pwr.ino.hex

Insert the Tiny USB programmer between one of the USB port of CM-Panel and the <code>J4 PROG</code> port
and type:

	$ sudo avrdude -Cavrdude.conf -v -pattiny88 -cusbtiny -e -Uefuse:w:0xfe:m -Uhfuse:w:0b11010111:m -Ulfuse:w:0xE2:m -Uflash:w:pwr.ino.hex:i

or insert an USB to serial adapted between an USB port on CM-Panel POE and the ATTiny debug port available on EXP2
header, open the PROG BYPASS jumper, then type: 

	$ sudo avrdude -Cavrdude.conf -v -pattiny88 -carduino -P/dev/ttyUSB0 -b19200 -D -Uflash:w:pwr.ino.hex:i

and reset the ATTiny by pressing the reset button near the supercap (@article='cmpanel-poe_jumpers_headers_pushbuttons').

## ATTiny debug port

The coprocessor has a debug serial port whose signals are exposed on the EXP2 connector as shown in the figure. 
<br/>
These signals are at 3.3 volts with the following serial parameters: <code>19200 baud,N,8,1 no flow control</code>.

<br/>

<img src="./attiny_debug_port.jpg" style="max-width:740px; width:100%;">


## Notes

Get the stand alone optiboot binary file:

	$ wget https://www.acmesystems.it/www/cmpanel-poe_attiny/optiboot_attiny88_8000000L.hex

Programming just the Optiboot bootloader width Atmel ICE

	$ sudo avrdude -Cavrdude.conf -v -pattiny88 -catmelice_isp -Pusb -e -Uefuse:w:0xfe:m -Uhfuse:w:0b11010111:m -Ulfuse:w:0xE2:m -Uflash:w:optiboot_attiny88_8000000L.hex:i 

Programming just the bootloader with USBTiny

	$ sudo avrdude -Cavrdude.conf -v -pattiny88 -cusbtiny -e -Uefuse:w:0xfe:m -Uhfuse:w:0b11010111:m -Ulfuse:w:0xE2:m -Uflash:w:optiboot_attiny88_8000000L.hex:i 

## Downloads

* ATTiny co-processor firmware: [pwr.ino](./pwr.ino)
* ATTiny co-processor binary code (includes optiboot): [pwr.ino.hex](./pwr.ino.hex)
* Stand alone optiboot binary code: [optiboot_attiny88_8000000L.hex](./optiboot_attiny88_8000000L.hex)
* Avrdude configuration file: [avrdude.conf](./avrdude.conf)


## Links

* Optiboot <https://github.com/Optiboot/optiboot> 

<br/>
@include='adv_cmpanel' 	
	
