summaryrefslogtreecommitdiffstats
path: root/firmware/calibration.h
blob: 92db5a42e319508407f144558e0bfe08b5907d8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef OPENPSU_CALIBRATION_H_
#define OPENPSU_CALIBRATION_H_

#include "util.h"

/****************************************************************************
 * Microcontroller CPU speed calibration                                    *
 ****************************************************************************/

/* System timer calibration. Calibrated to 1000Hz */
#define SYSTIMER_TIMERFREQ	((0 << CS10) | (1 << CS11) | (0 << CS12)) /* == F_CPU/8 */
#define SYSTIMER_CMPVAL		2500
#define JIFFIES_PER_SECOND	1000

/****************************************************************************
 * Other hardware calibration                                               *
 ****************************************************************************/

/* The maximum voltage or current value for the 12bit DAC. */
#define MAX_DAC_VALUE		((1 << 12) - 1)

/****************************************************************************
 * Output voltage offset calibration                                        *
 ****************************************************************************/

/* The maximum voltage value in millivolts. */
#define MAX_VOLTAGE		30000

/* Macro to convert an offset in millivolt to an offset in DAC steps. */
#define mV_offset(milli_offset)	\
	(int8_t)(((int8_t)(milli_offset) * (int32_t)MAX_DAC_VALUE) / (int32_t)MAX_VOLTAGE)

/* Output voltage calibration.
 * The desired mV value is passed to this function.
 * It returns the DAC offset that is required for a voltage value
 * to really reach it at the output pins. */
static inline int8_t calib_voltage_offset(uint16_t mV)
{
	if (mV <= 50)
		return mV_offset(0);
	if (mV <= 500)
		return mV_offset(27);
	if (mV <= 6000)
		return mV_offset(30);
	if (mV <= 10000)
		return mV_offset(20);
	if (mV <= 13000)
		return mV_offset(10);
	if (mV <= 15000)
		return mV_offset(0);
	if (mV <= 17000)
		return mV_offset(-10);
	if (mV <= 18000)
		return mV_offset(-20);
	if (mV <= 21000)
		return mV_offset(-30);
	if (mV <= 30000)
		return mV_offset(-40);
	BUG_ON(1);
	return 0;
}

/****************************************************************************
 * Output Current offset calibration                                        *
 ****************************************************************************/

/* The maximum current value in milliamps. */
#define MAX_CURRENT		3000

/* Macro to convert an offset in milliamps to an offset in DAC steps. */
#define mA_offset(milli_offset)	\
	(int8_t)(((int8_t)(milli_offset) * (int32_t)MAX_DAC_VALUE) / (int32_t)MAX_CURRENT)

/* Output Max-Current calibration.
 * The desired mA value is passed to this function.
 * It returns the DAC offset that is required for a Current value
 * to really reach it at the output pins. */
static inline int8_t calib_maxcurrent_offset(uint16_t mV)
{
	/* TODO */
	return mA_offset(0);
}


#endif /* OPENPSU_CALIBRATION_H_ */
bues.ch cgit interface