1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#include <stdint.h>
14#include <platsupport/io.h>
15#include <platsupport/tmu.h>
16#include "xadc.h"
17
18/* Macros for converting from the raw sensor value into degrees kelvin.
19 * For a full description of this process, see
20 * 7 Series FPGAs and Zynq-7000 All Programmable SoC XADC
21 * Dual 12-bit 1 MSPS Analog-to-Digital Converter User Guide,
22 * CH. 2: Analog-to-Digital Converter, Temperature Sensor (p32)
23 */
24
25/* The temperature is encoded in the 12 MSBs of 16 LSBs of the reported value */
26#define TEMPERATURE_RAW_TO_CODE(x) (((x) & MASK(16)) >> 4u)
27
28/* Converts a word representation of a temperature obtained from the
29 * temperature sensor into degrees kelvin */
30#define TEMPERATURE_RAW_TO_MILLIKELVIN(x) \
31        ((TEMPERATURE_RAW_TO_CODE((x)) * 503975) / 4096)
32
33static uint32_t get_raw_temperature(ps_tmu_t* tmu) {
34    return xadc_read_register(XADC_ADDRESS_TEMPERATURE);
35}
36
37static temperature_t get_temperature_millikelvin(ps_tmu_t* tmu) {
38    return TEMPERATURE_RAW_TO_MILLIKELVIN(get_raw_temperature(tmu));
39}
40
41int ps_tmu_init(enum tmu_id id, ps_io_ops_t* ops, ps_tmu_t* dev) {
42
43    xadc_init(ops);
44
45    dev->get_temperature = get_temperature_millikelvin;
46    dev->get_raw_temperature = get_raw_temperature;
47
48    return 0;
49}
50