Browse Source

Update requested power after writing configuration

The PD Buddy Sink now requests new power after configuration is written.
This means that in Setup mode, the voltage and current can be
re-negotiated on the fly.  Cool, huh?

It's still impossible to turn the output on and off from the shell.  New
commands will allow that soon enough, though.  Also, I'm seeing some
weird behavior when switching to/from 5 V (power shuts off entirely
sometimes), but I suspect that's a quirk of the source I'm using (Asus
USB 3.1 UPD Panel) and not the PD Buddy Sink itself.  I plan to make or
buy a USB power/data splitter to verify this.
Clara Hobbs 6 years ago
parent
commit
12ae6b5bea
3 changed files with 34 additions and 3 deletions
  1. 30
    3
      src/policy_engine.c
  2. 1
    0
      src/policy_engine.h
  3. 3
    0
      src/shell.c

+ 30
- 3
src/policy_engine.c View File

275
     /* Wait for an event */
275
     /* Wait for an event */
276
     if (min_power) {
276
     if (min_power) {
277
         evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
277
         evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
278
-                | PDB_EVT_PE_I_OVRTEMP, PD_T_SINK_REQUEST);
278
+                | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP,
279
+                PD_T_SINK_REQUEST);
279
     } else {
280
     } else {
280
         evt = chEvtWaitAny(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
281
         evt = chEvtWaitAny(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
281
-                | PDB_EVT_PE_I_OVRTEMP);
282
+                | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP);
282
     }
283
     }
283
 
284
 
284
     /* If we got reset signaling, transition to default */
285
     /* If we got reset signaling, transition to default */
291
         return PESinkHardReset;
292
         return PESinkHardReset;
292
     }
293
     }
293
 
294
 
295
+    /* If the DPM wants us to, send a Get_Source_Cap message */
296
+    if (evt & PDB_EVT_PE_GET_SOURCE_CAP) {
297
+        return PESinkGetSourceCap;
298
+    }
299
+
294
     /* If no event was received, the timer ran out. */
300
     /* If no event was received, the timer ran out. */
295
     if (evt == 0) {
301
     if (evt == 0) {
296
         /* Repeat our Request message */
302
         /* Repeat our Request message */
397
 
403
 
398
 static enum policy_engine_state pe_sink_get_source_cap(void)
404
 static enum policy_engine_state pe_sink_get_source_cap(void)
399
 {
405
 {
400
-    /* Stubbed until we actually have a need for this */
406
+    /* Get a message object */
407
+    union pd_msg *get_source_cap = chPoolAlloc(&pdb_msg_pool);
408
+    /* Make a Get_Source_Cap message */
409
+    get_source_cap->hdr = PD_MSGTYPE_GET_SOURCE_CAP | PD_DATAROLE_UFP
410
+        | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(0);
411
+    /* Transmit the Get_Source_Cap */
412
+    chMBPost(&pdb_prltx_mailbox, (msg_t) get_source_cap, TIME_IMMEDIATE);
413
+    chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
414
+    eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
415
+            | PDB_EVT_PE_RESET);
416
+    /* Free the sent message */
417
+    chPoolFree(&pdb_msg_pool, get_source_cap);
418
+    get_source_cap = NULL;
419
+    /* If we got reset signaling, transition to default */
420
+    if (evt & PDB_EVT_PE_RESET) {
421
+        return PESinkTransitionDefault;
422
+    }
423
+    /* If the message transmission failed, send a hard reset */
424
+    if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
425
+        return PESinkHardReset;
426
+    }
427
+
401
     return PESinkReady;
428
     return PESinkReady;
402
 }
429
 }
403
 
430
 

+ 1
- 0
src/policy_engine.h View File

29
 #define PDB_EVT_PE_TX_ERR EVENT_MASK(3)
29
 #define PDB_EVT_PE_TX_ERR EVENT_MASK(3)
30
 #define PDB_EVT_PE_HARD_SENT EVENT_MASK(4)
30
 #define PDB_EVT_PE_HARD_SENT EVENT_MASK(4)
31
 #define PDB_EVT_PE_I_OVRTEMP EVENT_MASK(5)
31
 #define PDB_EVT_PE_I_OVRTEMP EVENT_MASK(5)
32
+#define PDB_EVT_PE_GET_SOURCE_CAP EVENT_MASK(6)
32
 
33
 
33
 /* The Policy Engine thread object */
34
 /* The Policy Engine thread object */
34
 extern thread_t *pdb_pe_thread;
35
 extern thread_t *pdb_pe_thread;

+ 3
- 0
src/shell.c View File

42
 #include "usbcfg.h"
42
 #include "usbcfg.h"
43
 #include "storage.h"
43
 #include "storage.h"
44
 #include "led.h"
44
 #include "led.h"
45
+#include "policy_engine.h"
45
 #include "pd.h"
46
 #include "pd.h"
46
 
47
 
47
 
48
 
101
     }
102
     }
102
 
103
 
103
     pdb_config_flash_update(&tmpcfg);
104
     pdb_config_flash_update(&tmpcfg);
105
+
106
+    chEvtSignal(pdb_pe_thread, PDB_EVT_PE_GET_SOURCE_CAP);
104
 }
107
 }
105
 
108
 
106
 static void cmd_load(BaseSequentialStream *chp, int argc, char *argv[])
109
 static void cmd_load(BaseSequentialStream *chp, int argc, char *argv[])

Loading…
Cancel
Save