Skip to content
  • Martin Roukala's avatar
    WIP sensor: expose a list of sensors · 17063f85
    Martin Roukala authored and Martin Roukala's avatar Martin Roukala committed
    The typical way of exposing sensors is simply to expose the output from
    the ADC in a register, and let the driver deal with it. For example,
    in LiteX, the driver is supposed to know that at the register address
    X lies the temperature raw value which needs to be converted to degrees
    celcius with formula (1). However, the following three registers
    contain voltages should be converted to volts following the formula (2)
    
    [1] temp = raw value x 503.975 / 4096 - 273.15
    [2] volt = raw value x 3 / 4096
    
    To make sensors' discovery possible, we create a discoverable Sensors
    module which lists in its header how many sensors are present. Then, we
    create 4 registers per sensor that contain the unit of the data as a
    single character, along with a multiplication factor and offset to be
    applied on the raw value to transform it into the wanted unit.
    
    Since offsets and factors are unlikely to be integers, they are
    represented as a floating point: 8 bits for the exponent, and 16 bits
    for the significant.
    
    The exponents for both the offset and the factor are packed in the
    first register (header) along with the unit, the significant for the
    offset and factor are found in the second register (calibration),
    and the third register of course contains the raw value.
    
    To get the calibrated value, the driver simply needs to apply the
    following formula:
    
    value = (raw * ${f_sig}) >> ${f_exp} + (${o_sig} / 2^${o_exp})
    
    Finally, the last register contains the min/max values sampled since
    the last time it was read, enabling detection of brownouts or
    peak over-temperature after the fact.
    17063f85