ARMEBS4  revision-26.06.2015
stm32_manageable_psu.c
Go to the documentation of this file.
1 /**
2  * \file heivs/stm32_manageable_psu.c
3  *
4  * \author marc dot pignat at hevs dot ch
5  */
6 
8 #include "heivs/stm32_mcp4552.h"
9 #include "heivs/bsp.h"
10 
11 static const struct mcp4552_t mcp4552[] =
12 {
13  {
14  .bus = &bus_i2c[0],
15  .i2c_address = 0x2c
16  },
17  {
18  .bus = &bus_i2c[0],
19  .i2c_address = 0x2d
20  },
21 };
22 
23 static const struct gpio_t gpio_enable_not0 = DEF_GPIOG(14, GPIO_OUTPUT_1);
24 static const struct gpio_t gpio_enable_not1 = DEF_GPIOG(13, GPIO_OUTPUT_1);
25 
26 struct manageable_psu_t
27 {
28  const struct mcp4552_t *potentiometer;
29  const struct gpio_t *enable_not;
30 };
31 
32 static const struct manageable_psu_t psu[] =
33 {
34  {
35  .potentiometer = &mcp4552[0],
36  .enable_not = &gpio_enable_not0,
37  },
38  {
39  .potentiometer = &mcp4552[1],
40  .enable_not = &gpio_enable_not1,
41  },
42 };
43 
44 static status_e psu_set(const struct manageable_psu_t *psu, uint32_t mv)
45 {
46  /**
47  * Output formula
48  * Ra = ((Vout / 0.6663) - 1) * Rb
49  * Vout should be < Vin
50  *
51  * Rb is 13k
52  * Vin = 3.3V
53  *
54  * Ra = 50k*value/256
55  * -> value = 256 * Ra / 50k
56  * -> value = 256 * ((mv/1000 / 0.6663)- 1) * 13k / 50k
57  * -> value = 256 * ((mv / 666.3) - 1) * 13 / 50
58  */
59  uint64_t value = mv;
60  uint32_t f = 1000;
61 
62  gpio_setup(psu->enable_not);
63  gpio_set(psu->enable_not, 1);
64 
65  if (mv > 3300)
66  {
67  return ERROR_BAD_PARAM;
68  }
69 
70  if (mv < 666)
71  {
72  return ERROR_BAD_PARAM;
73  }
74 
75  value *= 10 * f;
76  value /= 6663;
77  value -= f;
78 
79  value *= 13 * 256;
80  value /= 50;
81  value /= f;
82 
83  value = min(value, (uint32_t)256);
84 
85  gpio_set(psu->enable_not, 0);
86 
87  mcp4552_set(psu->potentiometer, value);
88 
89  return NO_ERROR;
90 }
91 
92 status_e psu_aux0_set(uint32_t mv)
93 {
94  return psu_set(&psu[0], mv);
95 }
96 
97 status_e psu_aux1_set(uint32_t mv)
98 {
99  return psu_set(&psu[1], mv);
100 }
BSP - Board Support Package.
Output (default 1)
Definition: stm32_gpio.h:53
Manageable PSU with an enable and a digital potentiometer.
static void gpio_set(const struct gpio_t *gpio, uint32_t value)
Set a gpio.
Definition: stm32_gpio.h:260
Parameter unsupported.
Definition: error.h:56
#define DEF_GPIOG(__PIN, __MODE)
same as DEF_GPIOA for port G
Definition: stm32_gpio.h:365
Driver for mcp4552 digital potentiometer.
GPIO control structure.
Definition: stm32_gpio.h:96
const struct heivs_bus_t bus_i2c[BSP_I2C_BUS_COUNT]
All i2c busses.
Definition: stm32_i2c.c:424
status_e gpio_setup(const struct gpio_t *gpio)
Setup a gpio.
Definition: stm32_gpio.c:9
status_e
Known errors.
Definition: error.h:21
No error.
Definition: error.h:28