Browse Source

Changed PDO getters to take just the PDO

Previously, the PDO attribute getter macros took a capabilities message
and the PDO index.  This worked in a lot of cases, but not in some I
need now, and there was little advantage to them working they way they
did anyway.  Now they take just the PDO itself, which is more intuitive
and more useful.
Clara Hobbs 6 years ago
parent
commit
61992f5088
2 changed files with 30 additions and 30 deletions
  1. 5
    5
      lib/include/pd.h
  2. 25
    25
      src/device_policy_manager.c

+ 5
- 5
lib/include/pd.h View File

172
 #define PD_PDO_SRC_FIXED_CURRENT (0x3FF << PD_PDO_SRC_FIXED_CURRENT_SHIFT)
172
 #define PD_PDO_SRC_FIXED_CURRENT (0x3FF << PD_PDO_SRC_FIXED_CURRENT_SHIFT)
173
 
173
 
174
 /* PD Source Fixed PDO current */
174
 /* PD Source Fixed PDO current */
175
-#define PD_PDO_SRC_FIXED_CURRENT_GET(msg, i) (((msg)->obj[(i)] & PD_PDO_SRC_FIXED_CURRENT) >> PD_PDO_SRC_FIXED_CURRENT_SHIFT)
175
+#define PD_PDO_SRC_FIXED_CURRENT_GET(pdo) (((pdo) & PD_PDO_SRC_FIXED_CURRENT) >> PD_PDO_SRC_FIXED_CURRENT_SHIFT)
176
 
176
 
177
 /* PD Source Fixed PDO voltage */
177
 /* PD Source Fixed PDO voltage */
178
-#define PD_PDO_SRC_FIXED_VOLTAGE_GET(msg, i) (((msg)->obj[(i)] & PD_PDO_SRC_FIXED_VOLTAGE) >> PD_PDO_SRC_FIXED_VOLTAGE_SHIFT)
178
+#define PD_PDO_SRC_FIXED_VOLTAGE_GET(pdo) (((pdo) & PD_PDO_SRC_FIXED_VOLTAGE) >> PD_PDO_SRC_FIXED_VOLTAGE_SHIFT)
179
 
179
 
180
 /* PD Programmable Power Supply APDO */
180
 /* PD Programmable Power Supply APDO */
181
 #define PD_APDO_PPS_MAX_VOLTAGE_SHIFT 17
181
 #define PD_APDO_PPS_MAX_VOLTAGE_SHIFT 17
186
 #define PD_APDO_PPS_CURRENT (0x7F << PD_APDO_PPS_CURRENT_SHIFT)
186
 #define PD_APDO_PPS_CURRENT (0x7F << PD_APDO_PPS_CURRENT_SHIFT)
187
 
187
 
188
 /* PD Programmable Power Supply APDO voltages */
188
 /* PD Programmable Power Supply APDO voltages */
189
-#define PD_APDO_PPS_MAX_VOLTAGE_GET(msg, i) (((msg)->obj[(i)] & PD_APDO_PPS_MAX_VOLTAGE) >> PD_APDO_PPS_MAX_VOLTAGE_SHIFT)
190
-#define PD_APDO_PPS_MIN_VOLTAGE_GET(msg, i) (((msg)->obj[(i)] & PD_APDO_PPS_MIN_VOLTAGE) >> PD_APDO_PPS_MIN_VOLTAGE_SHIFT)
189
+#define PD_APDO_PPS_MAX_VOLTAGE_GET(pdo) (((pdo) & PD_APDO_PPS_MAX_VOLTAGE) >> PD_APDO_PPS_MAX_VOLTAGE_SHIFT)
190
+#define PD_APDO_PPS_MIN_VOLTAGE_GET(pdo) (((pdo) & PD_APDO_PPS_MIN_VOLTAGE) >> PD_APDO_PPS_MIN_VOLTAGE_SHIFT)
191
 
191
 
192
 #define PD_APDO_PPS_MAX_VOLTAGE_SET(v) (((v) << PD_APDO_PPS_MAX_VOLTAGE_SHIFT) & PD_APDO_PPS_MAX_VOLTAGE)
192
 #define PD_APDO_PPS_MAX_VOLTAGE_SET(v) (((v) << PD_APDO_PPS_MAX_VOLTAGE_SHIFT) & PD_APDO_PPS_MAX_VOLTAGE)
193
 #define PD_APDO_PPS_MIN_VOLTAGE_SET(v) (((v) << PD_APDO_PPS_MIN_VOLTAGE_SHIFT) & PD_APDO_PPS_MIN_VOLTAGE)
193
 #define PD_APDO_PPS_MIN_VOLTAGE_SET(v) (((v) << PD_APDO_PPS_MIN_VOLTAGE_SHIFT) & PD_APDO_PPS_MIN_VOLTAGE)
194
 
194
 
195
 /* PD Programmable Power Supply APDO current */
195
 /* PD Programmable Power Supply APDO current */
196
-#define PD_APDO_PPS_CURRENT_GET(msg, i) (((msg)->obj[(i)] & PD_APDO_PPS_CURRENT) >> PD_APDO_PPS_CURRENT_SHIFT)
196
+#define PD_APDO_PPS_CURRENT_GET(pdo) (((pdo) & PD_APDO_PPS_CURRENT) >> PD_APDO_PPS_CURRENT_SHIFT)
197
 
197
 
198
 #define PD_APDO_PPS_CURRENT_SET(i) (((i) << PD_APDO_PPS_CURRENT_SHIFT) & PD_APDO_PPS_CURRENT)
198
 #define PD_APDO_PPS_CURRENT_SET(i) (((i) << PD_APDO_PPS_CURRENT_SHIFT) & PD_APDO_PPS_CURRENT)
199
 
199
 

+ 25
- 25
src/device_policy_manager.c View File

38
  *
38
  *
39
  * If there is no such PDO, returns -1 instead.
39
  * If there is no such PDO, returns -1 instead.
40
  */
40
  */
41
-static int8_t dpm_get_range_fixed_pdo_index(const union pd_msg *capabilities,
41
+static int8_t dpm_get_range_fixed_pdo_index(const union pd_msg *caps,
42
         struct pdbs_config *scfg)
42
         struct pdbs_config *scfg)
43
 {
43
 {
44
     /* Get the number of PDOs */
44
     /* Get the number of PDOs */
45
-    uint8_t numobj = PD_NUMOBJ_GET(capabilities);
45
+    uint8_t numobj = PD_NUMOBJ_GET(caps);
46
 
46
 
47
     /* Get ready to iterate over the PDOs */
47
     /* Get ready to iterate over the PDOs */
48
     int8_t i;
48
     int8_t i;
59
     while (0 <= i && i < numobj) {
59
     while (0 <= i && i < numobj) {
60
         /* If we have a fixed PDO, its V is within our range, and its I is at
60
         /* If we have a fixed PDO, its V is within our range, and its I is at
61
          * least our desired I */
61
          * least our desired I */
62
-        if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED
63
-                && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i
64
-                && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) >= PD_MV2PDV(scfg->vmin)
65
-                && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) <= PD_MV2PDV(scfg->vmax)) {
62
+        if ((caps->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED
63
+                && PD_PDO_SRC_FIXED_CURRENT_GET(caps->obj[i]) >= scfg->i
64
+                && PD_PDO_SRC_FIXED_VOLTAGE_GET(caps->obj[i]) >= PD_MV2PDV(scfg->vmin)
65
+                && PD_PDO_SRC_FIXED_VOLTAGE_GET(caps->obj[i]) <= PD_MV2PDV(scfg->vmax)) {
66
             return i;
66
             return i;
67
         }
67
         }
68
         i += step;
68
         i += step;
71
 }
71
 }
72
 
72
 
73
 bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
73
 bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
74
-        const union pd_msg *capabilities, union pd_msg *request)
74
+        const union pd_msg *caps, union pd_msg *request)
75
 {
75
 {
76
     /* Cast the dpm_data to the right type */
76
     /* Cast the dpm_data to the right type */
77
     struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
77
     struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
78
 
78
 
79
     /* Update the stored Source_Capabilities */
79
     /* Update the stored Source_Capabilities */
80
-    if (capabilities != NULL) {
80
+    if (caps != NULL) {
81
         if (dpm_data->capabilities != NULL) {
81
         if (dpm_data->capabilities != NULL) {
82
             chPoolFree(&pdb_msg_pool, (union pd_msg *) dpm_data->capabilities);
82
             chPoolFree(&pdb_msg_pool, (union pd_msg *) dpm_data->capabilities);
83
         }
83
         }
84
-        dpm_data->capabilities = capabilities;
84
+        dpm_data->capabilities = caps;
85
     } else {
85
     } else {
86
         /* No new capabilities; use a shorter name for the stored ones. */
86
         /* No new capabilities; use a shorter name for the stored ones. */
87
-        capabilities = dpm_data->capabilities;
87
+        caps = dpm_data->capabilities;
88
     }
88
     }
89
 
89
 
90
     /* Get the current configuration */
90
     /* Get the current configuration */
91
     struct pdbs_config *scfg = pdbs_config_flash_read();
91
     struct pdbs_config *scfg = pdbs_config_flash_read();
92
     /* Get the number of PDOs */
92
     /* Get the number of PDOs */
93
-    uint8_t numobj = PD_NUMOBJ_GET(capabilities);
93
+    uint8_t numobj = PD_NUMOBJ_GET(caps);
94
 
94
 
95
     /* Make the LED blink to indicate ongoing power negotiations */
95
     /* Make the LED blink to indicate ongoing power negotiations */
96
     if (dpm_data->led_pd_status) {
96
     if (dpm_data->led_pd_status) {
98
     }
98
     }
99
 
99
 
100
     /* Get whether or not the power supply is constrained */
100
     /* Get whether or not the power supply is constrained */
101
-    dpm_data->_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
101
+    dpm_data->_unconstrained_power = caps->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
102
 
102
 
103
     /* Make sure we have configuration */
103
     /* Make sure we have configuration */
104
     if (scfg != NULL && dpm_data->output_enabled) {
104
     if (scfg != NULL && dpm_data->output_enabled) {
106
         for (uint8_t i = 0; i < numobj; i++) {
106
         for (uint8_t i = 0; i < numobj; i++) {
107
             /* If we have a fixed PDO, its V equals our desired V, and its I is
107
             /* If we have a fixed PDO, its V equals our desired V, and its I is
108
              * at least our desired I */
108
              * at least our desired I */
109
-            if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED
110
-                    && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == PD_MV2PDV(scfg->v)
111
-                    && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i) {
109
+            if ((caps->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED
110
+                    && PD_PDO_SRC_FIXED_VOLTAGE_GET(caps->obj[i]) == PD_MV2PDV(scfg->v)
111
+                    && PD_PDO_SRC_FIXED_CURRENT_GET(caps->obj[i]) >= scfg->i) {
112
                 /* We got what we wanted, so build a request for that */
112
                 /* We got what we wanted, so build a request for that */
113
                 request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
113
                 request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
114
                     | PD_NUMOBJ(1);
114
                     | PD_NUMOBJ(1);
136
             }
136
             }
137
             /* If we have a PPS APDO, our desired V lies within its range, and
137
             /* If we have a PPS APDO, our desired V lies within its range, and
138
              * its I is at least our desired I */
138
              * its I is at least our desired I */
139
-            if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_AUGMENTED
140
-                    && (capabilities->obj[i] & PD_APDO_TYPE) == PD_APDO_TYPE_PPS
141
-                    && PD_APDO_PPS_MAX_VOLTAGE_GET(capabilities, i) >= PD_MV2PAV(scfg->v)
142
-                    && PD_APDO_PPS_MIN_VOLTAGE_GET(capabilities, i) <= PD_MV2PAV(scfg->v)
143
-                    && PD_APDO_PPS_CURRENT_GET(capabilities, i) >= PD_CA2PAI(scfg->i)) {
139
+            if ((caps->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_AUGMENTED
140
+                    && (caps->obj[i] & PD_APDO_TYPE) == PD_APDO_TYPE_PPS
141
+                    && PD_APDO_PPS_MAX_VOLTAGE_GET(caps->obj[i]) >= PD_MV2PAV(scfg->v)
142
+                    && PD_APDO_PPS_MIN_VOLTAGE_GET(caps->obj[i]) <= PD_MV2PAV(scfg->v)
143
+                    && PD_APDO_PPS_CURRENT_GET(caps->obj[i]) >= PD_CA2PAI(scfg->i)) {
144
                 /* We got what we wanted, so build a request for that */
144
                 /* We got what we wanted, so build a request for that */
145
                 request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
145
                 request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
146
                     | PD_NUMOBJ(1);
146
                     | PD_NUMOBJ(1);
161
             }
161
             }
162
         }
162
         }
163
         /* If there's a PDO in the voltage range, use it */
163
         /* If there's a PDO in the voltage range, use it */
164
-        int8_t i = dpm_get_range_fixed_pdo_index(capabilities, scfg);
164
+        int8_t i = dpm_get_range_fixed_pdo_index(caps, scfg);
165
         if (i >= 0) {
165
         if (i >= 0) {
166
             /* We got what we wanted, so build a request for that */
166
             /* We got what we wanted, so build a request for that */
167
             request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
167
             request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
183
             }
183
             }
184
 
184
 
185
             /* Update requested voltage */
185
             /* Update requested voltage */
186
-            dpm_data->_requested_voltage = PD_PDV2MV(PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i));
186
+            dpm_data->_requested_voltage = PD_PDV2MV(PD_PDO_SRC_FIXED_VOLTAGE_GET(caps->obj[i]));
187
 
187
 
188
             dpm_data->_capability_match = true;
188
             dpm_data->_capability_match = true;
189
             return true;
189
             return true;
252
 
252
 
253
             /* If the range PDO is a different voltage than the preferred
253
             /* If the range PDO is a different voltage than the preferred
254
              * voltage, add it to the array. */
254
              * voltage, add it to the array. */
255
-            if (i > 0 && PD_PDO_SRC_FIXED_VOLTAGE_GET(dpm_data->capabilities, i) != PD_MV2PDV(scfg->v)) {
255
+            if (i > 0 && PD_PDO_SRC_FIXED_VOLTAGE_GET(dpm_data->capabilities->obj[i]) != PD_MV2PDV(scfg->v)) {
256
                 cap->obj[numobj++] = PD_PDO_TYPE_FIXED
256
                 cap->obj[numobj++] = PD_PDO_TYPE_FIXED
257
-                    | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_PDO_SRC_FIXED_VOLTAGE_GET(dpm_data->capabilities, i))
258
-                    | PD_PDO_SNK_FIXED_CURRENT_SET(PD_PDO_SRC_FIXED_CURRENT_GET(dpm_data->capabilities, i));
257
+                    | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_PDO_SRC_FIXED_VOLTAGE_GET(dpm_data->capabilities->obj[i]))
258
+                    | PD_PDO_SNK_FIXED_CURRENT_SET(PD_PDO_SRC_FIXED_CURRENT_GET(dpm_data->capabilities->obj[i]));
259
             }
259
             }
260
 
260
 
261
             /* If we have three PDOs at this point, make sure the last two are
261
             /* If we have three PDOs at this point, make sure the last two are

Loading…
Cancel
Save