/*
 * TMP144.h
 *
 *  Created on: Mar 22, 2022
 *      Author: Jasper
 */

#ifndef INC_TMP144_H_
#define INC_TMP144_H_

#include "common_defs.h"

#include "stm32f4xx_hal.h"

extern volatile uint16_t temperatures[N_CELLS];

typedef enum {
	TMP144_IDLE, TMP144_RESETTING, TMP144_INITIALIZING, TMP144_READING_TEMP
} TMP144State;

typedef struct {
	UART_HandleTypeDef* handle;
	TMP144State state;

	uint8_t rxbuf[34];

	size_t n_sensors;
	uint8_t sensor_mappings[16];
} TMP144Bus;

HAL_StatusTypeDef tmp144_init(UART_HandleTypeDef* busbar_side, UART_HandleTypeDef* other_side);

HAL_StatusTypeDef tmp144_init_reset(TMP144Bus* bus);
HAL_StatusTypeDef tmp144_init_post_reset(TMP144Bus* bus);
HAL_StatusTypeDef tmp144_init_post_addr(TMP144Bus* bus);

HAL_StatusTypeDef tmp144_read_temps();
HAL_StatusTypeDef tmp144_send_read_temps(TMP144Bus* bus);
HAL_StatusTypeDef tmp144_recv_temps(TMP144Bus* bus);

#endif /* INC_TMP144_H_ */
