# Firmware for controllers of thermal sensors

Make regular scan of 8 sensors' pairs.
USART speed 115200. Code for ../../kicad/stm32

### Serial interface commands (ends with '\n'), small letter for only local processing:
- **0...7**  send message to Nth controller, not broadcast (after number should be CAN command)
- **a** get raw ADC values
- **B** send dummy CAN messages to broadcast address
- **c** show coefficients for all thermosensors
- **D** send dummy CAN messages to master (0) address
- **Ee** end temperature scan
- **Ff** turn sensors off
- **g** get last CAN address
- **Hh** switch I2C to high speed (100kHz)
- **i** reinit CAN with new address (if changed)
- **Jj** get MCU temperature
- **Kk** get values of U and I
- **Ll** switch I2C to low speed (default, 10kHz)
- **Mm** change master id to 0 (**m**) / broadcast (**M**)
- **Oo** turn onboard diagnostic LEDs **O**n or **o**ff (both commands are local!)
- **P** ping everyone over CAN
- **Rr** reinit I2C
- **Ss** start temperature scan
- **Tt** start single temperature measurement
- **u** check CAN bus status for errors
- **Vv** very low speed
- **Z** get sensors state over CAN

The command **M** allows to temporaly change master ID of all
controllers to broadcast ID. So all data they sent will be 
accessed @ any controller.

### PINOUT
- I2C: PB6 (SCL) & PB7 (SDA)
- USART1: PA9 (Tx) & PA10 (Rx)
- CAN bus: PB8 (Rx), PB9 (Tx)
- USB bus: PA11 (DM), PA12 (DP)
- I2C multiplexer: PB0..PB2 (0..2 address bits), PB12 (~EN)
- sensors' power: PB3 (in, overcurrent), PA8 (out, enable power)
- signal LEDs: PB10 (LED0), PB11 (LED1)
- ADC inputs: PA0 (V12/4.93), PA1 (V5/2), PA3 (I12 - 1V/A), PA6 (V3.3/2)
- controller CAN address: PA13..PA15 (0..2 bits), PB15 (3rd bit); 0 - master, other address - slave


### LEDS
- LED0 (nearest to sensors' connectors) - heartbeat
- LED1 (above LED0) - CAN bus OK

### CAN protocol
Variable data length: from 1 to 7 bytes.
First (number zero) byte of every sequence is command mark (0xA5) or data mark (0x5A).

Commands:
-    CMD_PING                request for PONG cmd
-    CMD_START_MEASUREMENT   start single temperature measurement
-    CMD_SENSORS_STATE       get sensors state
-    CMD_START_SCAN          run scan mode 
-    CMD_STOP_SCAN           stop scan mode
-    CMD_SENSORS_OFF         turn off power of sensors
-    CMD_LOWEST_SPEED        lowest I2C speed
-    CMD_LOW_SPEED           low I2C speed (10kHz)
-    CMD_HIGH_SPEED          high I2C speed (100kHz)
-    CMD_REINIT_I2C          reinit I2C with current speed

Dummy commands for test purposes:
-    CMD_DUMMY0 = 0xDA,
-    CMD_DUMMY1 = 0xAD

Data format:
- byte 1 - Controller number
- byte 2 - Command received
- bytes 3..7 - data

Thermal data format:
- byte 3 - Sensor number (10*N + M, where N is multiplexer number, M - number of sensor in pair, i.e. 0,1,10,11,20,21...70,71)
- byte 4 - thermal data H
- byte 5 - thermal data L

MCU temperature data format:
- byte 3 - data H
- byte 4 - data L

All temperature is in degrC/100

U and I data format:
- byte 2 - type of data (CMD_GETUIVAL0 - V12 and V5, CMD_GETUIVAL1 - I12 and V3.3)
case CMD_GETUIVAL0:
- bytes 3,4 - V12 H/L
- bytes 5,6 - V5 H/L
case CMD_GETUIVAL1:
- bytes 3,4 - I12 H/L
- bytes 5,6 - V33 H/L
Voltage is in V/100, Current is in mA
