Skip to content

Commit

Permalink
added 12V and 9V profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Jana-Marie committed Oct 26, 2020
1 parent 2e7173b commit 1ffd13e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
6 changes: 6 additions & 0 deletions firmware/Inc/stusb4500.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#define STUSB4500_ADDR 0x28
#define RDO_REG_STATUS 0x91

#define ATTACHED_STATUS 0x01
#define PORT_STATUS 0x0E
#define TX_HEADER 0x51
#define CMD_CTRL 0x1A

typedef union {
uint32_t d32;
struct {
Expand Down Expand Up @@ -59,5 +64,6 @@ typedef union {
HAL_StatusTypeDef stusb_read_rdo(STUSB_GEN1S_RDO_REG_STATUS_RegTypeDef *Nego_RDO);
HAL_StatusTypeDef stusb_update_pdo(uint8_t pdo_number, uint16_t voltage_mv, uint16_t current_ma);
HAL_StatusTypeDef stusb_set_valid_pdo(uint8_t valid_count);
HAL_StatusTypeDef stusb_soft_reset();

#endif
23 changes: 16 additions & 7 deletions firmware/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ int main(void)
stusb_update_pdo(1, 5000, 500); // allows comms on standard 5 V
// 30 W and 80 W - ensures iron is well behaved and enumerates PD profile before drawing it
stusb_update_pdo(2, 15000, 1500);
stusb_update_pdo(3, 20000, 4000);
stusb_update_pdo(3, 20000, 2000);
stusb_set_valid_pdo(3);

HAL_Delay(50);
Expand Down Expand Up @@ -183,12 +183,21 @@ int main(void)
unsigned char line2[22];
STUSB_GEN1S_RDO_REG_STATUS_RegTypeDef Nego_RDO;

if (stusb_read_rdo(&Nego_RDO) == HAL_OK) {
s.imax = (float) Nego_RDO.b.MaxCurrent / 100.0;
s.pdo = Nego_RDO.b.Object_Pos;
} else {
s.pdo = 0;
}
if (stusb_read_rdo(&Nego_RDO) == HAL_OK && Nego_RDO.b.Object_Pos != 0) {
s.imax = (float) Nego_RDO.b.MaxCurrent / 100.0;
s.pdo = Nego_RDO.b.Object_Pos;
} else {
stusb_update_pdo(2, 9000, 900);
stusb_update_pdo(3, 12000, 1000);
stusb_soft_reset();
HAL_Delay(300);
if (stusb_read_rdo(&Nego_RDO) == HAL_OK) {
s.imax = (float) Nego_RDO.b.MaxCurrent / 100.0;
s.pdo = Nego_RDO.b.Object_Pos;
} else {
s.pdo = 0;
}
}

if (s.pdo > 0) {
sprintf((char * restrict) line1, "PD %s %1d", s.pdo > 3 ? "Adjust" : "Profile", s.pdo);
Expand Down
19 changes: 16 additions & 3 deletions firmware/Src/stusb4500.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
#include "stusb4500.h"

USB_PD_SNK_PDO_TypeDef pdo_profile[3];
extern I2C_HandleTypeDef hi2c1;
extern I2C_HandleTypeDef hi2c2;

static HAL_StatusTypeDef read_register(uint8_t device, uint8_t reg, uint8_t *data, uint8_t len)
{
return HAL_I2C_Mem_Read(&hi2c1, (device << 1), (uint16_t) reg, I2C_MEMADD_SIZE_8BIT, data, len, 1000);
return HAL_I2C_Mem_Read(&hi2c2, (device << 1), (uint16_t) reg, I2C_MEMADD_SIZE_8BIT, data, len, 1000);
}

static HAL_StatusTypeDef write_register(uint8_t device, uint8_t reg, uint8_t *data, uint8_t len)
{
return HAL_I2C_Mem_Write(&hi2c1, (device << 1), (uint16_t) reg, I2C_MEMADD_SIZE_8BIT, data, len, 1000);
return HAL_I2C_Mem_Write(&hi2c2, (device << 1), (uint16_t) reg, I2C_MEMADD_SIZE_8BIT, data, len, 1000);
}

HAL_StatusTypeDef stusb_read_rdo(STUSB_GEN1S_RDO_REG_STATUS_RegTypeDef *Nego_RDO) {
Expand Down Expand Up @@ -65,3 +65,16 @@ HAL_StatusTypeDef stusb_set_valid_pdo(uint8_t valid_count) {
}
return ret;
}

HAL_StatusTypeDef stusb_soft_reset() {
HAL_StatusTypeDef ret = -1;
uint8_t data;

// read CC pin Attachement status
ret = read_register(STUSB4500_ADDR, PORT_STATUS , &data, 1);
if((data & ATTACHED_STATUS) == 1) {
ret = write_register(STUSB4500_ADDR, TX_HEADER, (uint8_t *)0x000D, 2 );
ret = write_register(STUSB4500_ADDR, CMD_CTRL, (uint8_t *)0x26, 1 );
}
return ret;
}

0 comments on commit 1ffd13e

Please sign in to comment.