From 34ae57ee58ea2cb1be3241f1bb505baa8fb5656c Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 12 Jun 2020 21:10:03 +1000 Subject: [PATCH] Bitbang I2C setup + detect test --- workspace/TS100/Core/BSP/BSP.h | 4 +- workspace/TS100/Core/BSP/Miniware/I2CBB.cpp | 232 ++++ workspace/TS100/Core/BSP/Miniware/I2CBB.hpp | 42 + workspace/TS100/Core/BSP/Miniware/Pins.h | 4 + workspace/TS100/Core/BSP/Miniware/Power.cpp | 13 +- workspace/TS100/Core/BSP/Miniware/preRTOS.cpp | 3 +- .../FUSB302/{FUSB302.c => FUSB302.cpp} | 81 +- .../TS100/Core/Drivers/FUSB302/FUSB302.h | 47 +- ...{usb_pd_protocol.c => usb_pd_protocol.cpp} | 1042 ++++++-------- .../Core/Drivers/FUSB302/tcpm_driver.cpp | 25 +- .../Core/Drivers/FUSB302/usb_pd_driver.h | 6 +- .../TS100/Core/Drivers/FUSB302/usb_pd_tcpm.h | 354 ----- workspace/TS100/Core/Inc/main.hpp | 2 +- workspace/TS100/Core/Src/main.cpp | 5 +- workspace/TS100/Core/Threads/GUIThread.cpp | 1205 +++++++++-------- 15 files changed, 1435 insertions(+), 1630 deletions(-) create mode 100644 workspace/TS100/Core/BSP/Miniware/I2CBB.cpp create mode 100644 workspace/TS100/Core/BSP/Miniware/I2CBB.hpp rename workspace/TS100/Core/Drivers/FUSB302/{FUSB302.c => FUSB302.cpp} (93%) rename workspace/TS100/Core/Drivers/FUSB302/USBC_PD/{usb_pd_protocol.c => usb_pd_protocol.cpp} (84%) delete mode 100644 workspace/TS100/Core/Drivers/FUSB302/usb_pd_tcpm.h diff --git a/workspace/TS100/Core/BSP/BSP.h b/workspace/TS100/Core/BSP/BSP.h index a558baad..8c8ce624 100644 --- a/workspace/TS100/Core/BSP/BSP.h +++ b/workspace/TS100/Core/BSP/BSP.h @@ -47,8 +47,10 @@ void reboot(); // If the user has programmed in a bootup logo, draw it to the screen from flash // Returns 1 if the logo was printed so that the unit waits for the timeout or button uint8_t showBootLogoIfavailable(); - +//delay wrapper for delay using the hardware timer (used before RTOS) void delay_ms(uint16_t count); +//Used to allow knowledge of if usb_pd is being used +uint8_t usb_pd_detect(); #ifdef __cplusplus } #endif diff --git a/workspace/TS100/Core/BSP/Miniware/I2CBB.cpp b/workspace/TS100/Core/BSP/Miniware/I2CBB.cpp new file mode 100644 index 00000000..44db8cb2 --- /dev/null +++ b/workspace/TS100/Core/BSP/Miniware/I2CBB.cpp @@ -0,0 +1,232 @@ +/* + * I2CBB.cpp + * + * Created on: 12 Jun 2020 + * Author: Ralim + */ + +#include + +#define SCL_HIGH() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_SET) +#define SCL_LOW() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_RESET) +#define SDA_HIGH() HAL_GPIO_WritePin(SDA2_GPIO_Port, SDA2_Pin, GPIO_PIN_SET) +#define SDA_LOW() HAL_GPIO_WritePin(SDA2_GPIO_Port, SDA2_Pin, GPIO_PIN_RESET) +#define SDA_READ() (HAL_GPIO_ReadPin(SDA2_GPIO_Port,SDA2_Pin)==GPIO_PIN_SET?1:0) +#define SCL_READ() (HAL_GPIO_ReadPin(SCL2_GPIO_Port,SCL2_Pin)==GPIO_PIN_SET?1:0) +#define I2C_DELAY() HAL_Delay(1); +void I2CBB::init() { + //Set GPIO's to output open drain + GPIO_InitTypeDef GPIO_InitStruct; + __HAL_RCC_GPIOA_CLK_ENABLE(); + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + GPIO_InitStruct.Pin = SDA2_Pin | SCL2_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(SDA2_GPIO_Port, &GPIO_InitStruct); + SDA_HIGH(); + SCL_HIGH(); +} + +bool I2CBB::probe(uint8_t address) { + start(); + bool ack = send(address); + stop(); + return ack; +} + +bool I2CBB::Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, + uint16_t Size) { + start(); + bool ack = send(DevAddress | 1); + if (!ack) { + stop(); + return false; + } + ack = send(MemAddress); + if (!ack) { + stop(); + return false; + } + while (Size) { + pData[0] = read(Size > 1); + pData++; + Size--; + } + stop(); + return true; +} + +bool I2CBB::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, + uint16_t Size) { + start(); + bool ack = send(DevAddress); + if (!ack) { + stop(); + return false; + } + ack = send(MemAddress); + if (!ack) { + stop(); + return false; + } + while (Size) { + bool ack = send(pData[0]); + if (!ack) { + stop(); + return false; + } + pData++; + Size--; + } + stop(); + return true; +} + +void I2CBB::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { + start(); + bool ack = send(DevAddress); + if (!ack) { + stop(); + return; + } + while (Size) { + bool ack = send(pData[0]); + if (!ack) { + stop(); + return; + } + pData++; + Size--; + } + stop(); + +} + +void I2CBB::Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { + start(); + bool ack = send(DevAddress | 1); + if (!ack) { + stop(); + return; + } + while (Size) { + pData[0] = read(Size > 1); + pData++; + Size--; + } + stop(); +} + +void I2CBB::TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, + uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx) { + if (Size_tx == 0 && Size_rx == 0) + return; + if (Size_tx) { + start(); + bool ack = send(DevAddress); + if (!ack) { + stop(); + return; + } + while (Size_tx) { + bool ack = send(pData_tx[0]); + if (!ack) { + stop(); + return; + } + pData_tx++; + Size_tx--; + } + } + if (Size_rx) { + start(); + bool ack = send(DevAddress | 1); + if (!ack) { + stop(); + return; + } + while (Size_rx) { + pData_rx[0] = read(Size_rx > 1); + pData_rx++; + Size_rx--; + } + } + stop(); +} + +void I2CBB::start() { + /* I2C Start condition, data line goes low when clock is high */ + SCL_HIGH(); + SDA_HIGH(); + I2C_DELAY(); + SDA_LOW(); + I2C_DELAY(); + SCL_LOW(); + I2C_DELAY(); +} + +void I2CBB::stop() { + /* I2C Stop condition, clock goes high when data is low */ + SDA_LOW(); + I2C_DELAY(); + SCL_HIGH(); + I2C_DELAY(); + SDA_HIGH(); + I2C_DELAY(); +} + +bool I2CBB::send(uint8_t value) { + + for (uint8_t i = 0; i < 8; i++) { + write_bit(value & 0x80); // write the most-significant bit + value <<= 1; + } + + bool ack = read_bit()==0; + return ack; +} + +uint8_t I2CBB::read(bool ack) { + uint8_t B = 0; + + uint8_t i; + for (i = 0; i < 8; i++) { + B <<= 1; + B |= read_bit(); + } + + if (ack) + write_bit(0); + else + write_bit(1); + return B; +} + +uint8_t I2CBB::read_bit() { + uint8_t b; + + SDA_HIGH(); + I2C_DELAY(); + SCL_HIGH(); + I2C_DELAY(); + + if (SDA_READ()) + b = 1; + else + b = 0; + + SCL_LOW(); + return b; +} + +void I2CBB::write_bit(uint8_t val) { + if (val > 0) + SDA_HIGH(); + else + SDA_LOW(); + + I2C_DELAY(); + SCL_HIGH(); + I2C_DELAY(); + SCL_LOW(); +} diff --git a/workspace/TS100/Core/BSP/Miniware/I2CBB.hpp b/workspace/TS100/Core/BSP/Miniware/I2CBB.hpp new file mode 100644 index 00000000..5be764d8 --- /dev/null +++ b/workspace/TS100/Core/BSP/Miniware/I2CBB.hpp @@ -0,0 +1,42 @@ +/* + * I2CBB.hpp + * + * Created on: 12 Jun 2020 + * Author: Ralim + */ + +#ifndef BSP_MINIWARE_I2CBB_HPP_ +#define BSP_MINIWARE_I2CBB_HPP_ +#include "BSP.h" +#include "Setup.h" +#include "Pins.h" +/* + * Simple static I2C bit-bang class used on the TS80P + * SCL = PA5 + * SDA = PA1 + */ +class I2CBB { +public: + static void init(); + //Probe if device ACK's address or not + static bool probe(uint8_t address); + //Issues a complete 8bit register read + static bool Mem_Read(uint16_t DevAddress, uint16_t MemAddress, + uint8_t *pData, uint16_t Size); + //Implements a register write + static bool Mem_Write(uint16_t DevAddress, uint16_t MemAddress, + uint8_t *pData, uint16_t Size); + static void Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size); + static void Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size); + static void TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, + uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx); +private: + static void start(); + static void stop(); + static bool send(uint8_t value); + static uint8_t read(bool ack); + static uint8_t read_bit(); + static void write_bit(uint8_t val); +}; + +#endif /* BSP_MINIWARE_I2CBB_HPP_ */ diff --git a/workspace/TS100/Core/BSP/Miniware/Pins.h b/workspace/TS100/Core/BSP/Miniware/Pins.h index 1f0ddfa9..ea30222f 100644 --- a/workspace/TS100/Core/BSP/Miniware/Pins.h +++ b/workspace/TS100/Core/BSP/Miniware/Pins.h @@ -77,6 +77,10 @@ #define SCL_GPIO_Port GPIOB #define SDA_Pin GPIO_PIN_7 #define SDA_GPIO_Port GPIOB +#define SCL2_Pin GPIO_PIN_5 +#define SCL2_GPIO_Port GPIOA +#define SDA2_Pin GPIO_PIN_1 +#define SDA2_GPIO_Port GPIOA #endif diff --git a/workspace/TS100/Core/BSP/Miniware/Power.cpp b/workspace/TS100/Core/BSP/Miniware/Power.cpp index 30386c83..c72e6207 100644 --- a/workspace/TS100/Core/BSP/Miniware/Power.cpp +++ b/workspace/TS100/Core/BSP/Miniware/Power.cpp @@ -2,10 +2,14 @@ #include "BSP_Power.h" #include "QC3.h" #include "Settings.h" +#include "FUSB302.h" +bool FUSB302_present = false; void power_probe() { // If TS80 probe for QC // If TS100 - noop #ifdef MODEL_TS80 + + startQC(systemSettings.voltageDiv); seekQC((systemSettings.cutoutSetting) ? 120 : 90, @@ -18,4 +22,11 @@ void power_check() { #ifdef MODEL_TS80 QC_resync(); #endif -} \ No newline at end of file +} +uint8_t usb_pd_detect(){ +#ifdef MODEL_TS80 + FUSB302_present=fusb302_detect(); + return FUSB302_present; +#endif + return false; +} diff --git a/workspace/TS100/Core/BSP/Miniware/preRTOS.cpp b/workspace/TS100/Core/BSP/Miniware/preRTOS.cpp index 6a16c44f..674a850c 100644 --- a/workspace/TS100/Core/BSP/Miniware/preRTOS.cpp +++ b/workspace/TS100/Core/BSP/Miniware/preRTOS.cpp @@ -9,6 +9,7 @@ #include "BSP.h" #include "Setup.h" #include "Pins.h" +#include "I2CBB.hpp" void preRToSInit() { /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ @@ -18,5 +19,5 @@ void preRToSInit() { HAL_Delay(50); HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET); HAL_Delay(50); - + I2CBB::init(); } diff --git a/workspace/TS100/Core/Drivers/FUSB302/FUSB302.c b/workspace/TS100/Core/Drivers/FUSB302/FUSB302.cpp similarity index 93% rename from workspace/TS100/Core/Drivers/FUSB302/FUSB302.c rename to workspace/TS100/Core/Drivers/FUSB302/FUSB302.cpp index 2d9a0c3a..44739e8d 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/FUSB302.c +++ b/workspace/TS100/Core/Drivers/FUSB302/FUSB302.cpp @@ -6,13 +6,16 @@ */ #include "FUSB302.h" -#include "usb_pd_tcpm.h" +#include "USBC_TCPM/usb_pd_tcpm.h" #include "USBC_TCPM/tcpm.h" #include "USBC_PD/usb_pd.h" - +#include +#include "cmsis_os.h" +#include "I2C_Wrapper.hpp" #define PACKET_IS_GOOD_CRC(head) (PD_HEADER_TYPE(head) == PD_CTRL_GOOD_CRC && \ PD_HEADER_CNT(head) == 0) - +const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_COUNT] = { { 0, + fusb302_I2C_SLAVE_ADDR, &fusb302_tcpm_drv, TCPC_ALERT_ACTIVE_LOW }, }; static struct fusb302_chip_state { int cc_polarity; int vconn_enabled; @@ -44,7 +47,7 @@ static void fusb302_flush_rx_fifo(int port) { * then we'll have to keep a shadow of what this register * value should be so we don't clobber it here! */ - i2c_master_lock(tcpc_config[port].i2c_host_port); + tcpc_write(port, TCPC_REG_CONTROL1, TCPC_REG_CONTROL1_RX_FLUSH); } @@ -52,7 +55,6 @@ static void fusb302_flush_rx_fifo(int port) { static void fusb302_flush_tx_fifo(int port) { int reg; - i2c_master_lock(tcpc_config[port].i2c_host_port); tcpc_read(port, TCPC_REG_CONTROL0, ®); reg |= TCPC_REG_CONTROL0_TX_FLUSH; tcpc_write(port, TCPC_REG_CONTROL0, reg); @@ -62,7 +64,6 @@ static void fusb302_flush_tx_fifo(int port) { static void fusb302_auto_goodcrc_enable(int port, int enable) { int reg; - i2c_master_lock(tcpc_config[port].i2c_host_port); tcpc_read(port, TCPC_REG_SWITCHES1, ®); if (enable) @@ -101,8 +102,6 @@ static int measure_cc_pin_source(int port, int cc_measure) { int reg; int cc_lvl; - i2c_master_lock(tcpc_config[port].i2c_host_port); - /* Read status register */ tcpc_read(port, TCPC_REG_SWITCHES0, ®); /* Save current value */ @@ -181,8 +180,6 @@ static void detect_cc_pin_sink(int port, int *cc1, int *cc2) { int bc_lvl_cc1; int bc_lvl_cc2; - i2c_master_lock(tcpc_config[port].i2c_host_port); - /* * Measure CC1 first. */ @@ -317,7 +314,7 @@ static int fusb302_send_message(int port, uint16_t header, const uint32_t *data, buf[buf_pos++] = fusb302_TKN_TXON; /* burst write for speed! */ - i2c_master_lock(tcpc_config[port].i2c_host_port); + rv = tcpc_xfer(port, buf, buf_pos, 0, 0, I2C_XFER_SINGLE); return rv; @@ -328,8 +325,6 @@ static int fusb302_tcpm_select_rp_value(int port, int rp) { int rv; uint8_t vnc, rd; - i2c_master_lock(tcpc_config[port].i2c_host_port); - rv = tcpc_read(port, TCPC_REG_CONTROL0, ®); if (rv) return rv; @@ -373,8 +368,6 @@ static int fusb302_tcpm_init(int port) { /* all other variables assumed to default to 0 */ - i2c_master_lock(tcpc_config[port].i2c_host_port); - /* Restore default settings */ tcpc_write(port, TCPC_REG_RESET, TCPC_REG_RESET_SW_RESET); @@ -448,8 +441,6 @@ static int fusb302_tcpm_get_cc(int port, int *cc1, int *cc2) { static int fusb302_tcpm_set_cc(int port, int pull) { int reg; - i2c_master_lock(tcpc_config[port].i2c_host_port); - /* NOTE: FUSB302 toggles a single pull-up between CC1 and CC2 */ /* NOTE: FUSB302 Does not support Ra. */ switch (pull) { @@ -523,8 +514,6 @@ static int fusb302_tcpm_set_polarity(int port, int polarity) { /* Port polarity : 0 => CC1 is CC line, 1 => CC2 is CC line */ int reg; - i2c_master_lock(tcpc_config[port].i2c_host_port); - tcpc_read(port, TCPC_REG_SWITCHES0, ®); /* clear VCONN switch bits */ @@ -590,8 +579,6 @@ static int fusb302_tcpm_set_vconn(int port, int enable) { tcpm_set_polarity(port, state[port].cc_polarity); } else { - i2c_master_lock(tcpc_config[port].i2c_host_port); - tcpc_read(port, TCPC_REG_SWITCHES0, ®); /* clear VCONN switch bits */ @@ -605,7 +592,8 @@ static int fusb302_tcpm_set_vconn(int port, int enable) { return 0; } -static int fusb302_tcpm_set_msg_header(int port, int power_role, int data_role) { +static int fusb302_tcpm_set_msg_header(int port, int power_role, + int data_role) { int reg; tcpc_read(port, TCPC_REG_SWITCHES1, ®); @@ -628,8 +616,6 @@ static int fusb302_tcpm_set_rx_enable(int port, int enable) { state[port].rx_enable = enable; - i2c_master_lock(tcpc_config[port].i2c_host_port); - /* Get current switch state */ tcpc_read(port, TCPC_REG_SWITCHES0, ®); @@ -678,8 +664,6 @@ static int fusb302_tcpm_set_rx_enable(int port, int enable) { static int fusb302_rx_fifo_is_empty(int port) { int reg, ret; - i2c_master_lock(tcpc_config[port].i2c_host_port); - ret = (!tcpc_read(port, TCPC_REG_STATUS1, ®)) && (reg & TCPC_REG_STATUS1_RX_EMPTY); @@ -705,7 +689,6 @@ static int fusb302_tcpm_get_message(int port, uint32_t *payload, int *head) { /* Read until we have a non-GoodCRC packet or an empty FIFO */ do { buf[0] = TCPC_REG_FIFOS; - i2c_master_lock(tcpc_config[port].i2c_host_port); /* * PART 1 OF BURST READ: Write in register address. @@ -794,10 +777,11 @@ static int fusb302_tcpm_transmit(int port, enum tcpm_transmit_type type, fusb302_send_message(port, header, data, buf, buf_pos); // wait for the GoodCRC to come back before we let the rest // of the code do stuff like change polarity and miss it - delay_us(600); - return; +// delay_us(600); + osDelay(1); + break; case TCPC_TX_HARD_RESET: - i2c_master_lock(tcpc_config[port].i2c_host_port); + /* Simply hit the SEND_HARD_RESET bit */ tcpc_read(port, TCPC_REG_CONTROL3, ®); reg |= TCPC_REG_CONTROL3_SEND_HARDRESET; @@ -805,7 +789,7 @@ static int fusb302_tcpm_transmit(int port, enum tcpm_transmit_type type, break; case TCPC_TX_BIST_MODE_2: - i2c_master_lock(tcpc_config[port].i2c_host_port); + /* Hit the BIST_MODE2 bit and start TX */ tcpc_read(port, TCPC_REG_CONTROL1, ®); reg |= TCPC_REG_CONTROL1_BIST_MODE2; @@ -836,7 +820,7 @@ static int fusb302_tcpm_get_vbus_level(int port) int reg; /* Read status register */ - i2c_master_lock(tcpc_config[port].i2c_host_port); + tcpc_read(port, TCPC_REG_STATUS0, ®); @@ -852,7 +836,6 @@ void fusb302_tcpc_alert(int port) { /* reading interrupt registers clears them */ - i2c_master_lock(tcpc_config[port].i2c_host_port); tcpc_read(port, TCPC_REG_INTERRUPT, &interrupt); tcpc_read(port, TCPC_REG_INTERRUPTA, &interrupta); tcpc_read(port, TCPC_REG_INTERRUPTB, &interruptb); @@ -924,8 +907,6 @@ void fusb302_tcpc_alert(int port) { void tcpm_set_bist_test_data(int port) { int reg; - i2c_master_lock(tcpc_config[port].i2c_host_port); - /* Read control3 register */ tcpc_read(port, TCPC_REG_CONTROL3, ®); @@ -937,16 +918,20 @@ void tcpm_set_bist_test_data(int port) { } -const struct tcpm_drv fusb302_tcpm_drv = { .init = &fusb302_tcpm_init, - .release = &fusb302_tcpm_release, .get_cc = &fusb302_tcpm_get_cc, -#ifdef CONFIG_USB_PD_VBUS_DETECT_TCPC - .get_vbus_level = &fusb302_tcpm_get_vbus_level, -#endif - .select_rp_value = &fusb302_tcpm_select_rp_value, .set_cc = - &fusb302_tcpm_set_cc, - .set_polarity = &fusb302_tcpm_set_polarity, .set_vconn = - &fusb302_tcpm_set_vconn, .set_msg_header = - &fusb302_tcpm_set_msg_header, .set_rx_enable = - &fusb302_tcpm_set_rx_enable, .get_message = - &fusb302_tcpm_get_message, .transmit = &fusb302_tcpm_transmit, - .tcpc_alert = &fusb302_tcpc_alert, }; +const struct tcpm_drv fusb302_tcpm_drv = { &fusb302_tcpm_init, //init + &fusb302_tcpm_release, //.release + &fusb302_tcpm_get_cc, //get_cc + NULL, //get_vbus_level + &fusb302_tcpm_select_rp_value, //select_rp_value + &fusb302_tcpm_set_cc, //set_cc + &fusb302_tcpm_set_polarity, //set_polarity + &fusb302_tcpm_set_vconn, //set_vconn + &fusb302_tcpm_set_msg_header, //set_msg_header + &fusb302_tcpm_set_rx_enable, //set_rx_enable + &fusb302_tcpm_get_message, //get_message + &fusb302_tcpm_transmit, //transmit + &fusb302_tcpc_alert, //tcpc_alert + NULL, //tcpc_discharge_vbus + NULL, //get_chip_info + }; + diff --git a/workspace/TS100/Core/Drivers/FUSB302/FUSB302.h b/workspace/TS100/Core/Drivers/FUSB302/FUSB302.h index b6404659..5a0455aa 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/FUSB302.h +++ b/workspace/TS100/Core/Drivers/FUSB302/FUSB302.h @@ -9,7 +9,7 @@ #define fusb302_H #include -#include "usb_pd_tcpm.h" +#include "USBC_TCPM/usb_pd_tcpm.h" #include "USBC_PD/usb_pd.h" /* Chip Device ID - 302A or 302B */ @@ -18,13 +18,13 @@ /* I2C slave address varies by part number */ /* FUSB302BUCX / FUSB302BMPX */ -#define fusb302_I2C_SLAVE_ADDR 0x22 // 7-bit address for Arduino +#define fusb302_I2C_SLAVE_ADDR 0x22<<1 // 7-bit address /* FUSB302B01MPX */ -#define fusb302_I2C_SLAVE_ADDR_B01 0x23 +#define fusb302_I2C_SLAVE_ADDR_B01 0x23<<1 /* FUSB302B10MPX */ -#define fusb302_I2C_SLAVE_ADDR_B10 0x24 +#define fusb302_I2C_SLAVE_ADDR_B10 0x24<<1 /* FUSB302B11MPX */ -#define fusb302_I2C_SLAVE_ADDR_B11 0x25 +#define fusb302_I2C_SLAVE_ADDR_B11 0x25<<1 /* Default retry count for transmitting */ #define PD_RETRY_COUNT 3 @@ -210,41 +210,8 @@ enum fusb302_txfifo_tokens { extern const struct tcpm_drv fusb302_tcpm_drv; -/* -// Common methods for TCPM implementations -int fusb302_init(void); -int fusb302_get_cc(int *cc1, int *cc2); -int fusb302_get_vbus_level(void); -int fusb302_select_rp_value(int rp); -int fusb302_set_cc(int pull); -int fusb302_set_polarity(int polarity); -int fusb302_set_vconn(int enable); -int fusb302_set_msg_header(int power_role, int data_role); -int fusb302_set_rx_enable(int enable); -int fusb302_get_message(uint32_t *payload, int *head); -int fusb302_transmit(enum tcpm_transmit_type type, - uint16_t header, const uint32_t *data); -//int alert(void); -void fusb302_pd_reset(int port); -void fusb302_auto_goodcrc_enable(int enable); -int fusb302_convert_bc_lvl(int bc_lvl); -void fusb302_detect_cc_pin_source_manual(int *cc1_lvl, int *cc2_lvl); -int fusb302_measure_cc_pin_source(int cc_measure); -void fusb302_detect_cc_pin_sink(int *cc1, int *cc2); -int fusb302_send_message(uint16_t header, const uint32_t *data, - uint8_t *buf, int buf_pos); -void fusb302_flush_rx_fifo(int port); -void fusb302_flush_tx_fifo(int port); -void fusb302_clear_int_pin(void); -void fusb302_set_bist_test_data(void); -int fusb302_get_chip_id(int *id); -uint32_t fusb302_get_interrupt_reason(void); -int fusb302_tcpc_write(int reg, int val); -int fusb302_tcpc_read(int reg, int *val); -int fusb302_tcpc_xfer(const uint8_t *out, - int out_size, uint8_t *in, - int in_size, int flags); -*/ +//returns 1 if the FUSB302 is on the I2C bus +uint8_t fusb302_detect(); #endif /* fusb302_H */ diff --git a/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.c b/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.cpp similarity index 84% rename from workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.c rename to workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.cpp index d08379ba..1b1cc383 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.c +++ b/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.cpp @@ -4,11 +4,11 @@ */ #include "usb_pd.h" -#include "usb_pd_tcpm.h" +#include "USBC_TCPM/usb_pd_tcpm.h" #include "USBC_TCPM/tcpm.h" #include "usb_pd_driver.h" #include - +#include #ifdef CONFIG_COMMON_RUNTIME #define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args) #define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args) @@ -42,7 +42,7 @@ static uint8_t pd_comm_enabled[CONFIG_USB_PD_PORT_COUNT]; #else /* CONFIG_COMMON_RUNTIME */ #define CPRINTF(format, args...) #define CPRINTS(format, args...) -static const int debug_level; +static const int debug_level=0; #endif #ifdef CONFIG_USB_PD_DUAL_ROLE @@ -116,7 +116,7 @@ static const uint8_t vdo_ver[] = { static int head; static int port = TASK_ID_TO_PD_PORT(task_get_current()); static uint32_t payload[7]; -static int timeout = 10*MSEC; +static int timeout = 10 * MSEC; static int cc1, cc2; static int res, incoming_packet = 0; static int hard_reset_count = 0; @@ -248,8 +248,7 @@ BUILD_ASSERT(ARRAY_SIZE(pd_state_names) == PD_STATE_COUNT); static struct ec_params_usb_pd_rw_hash_entry rw_hash_table[RW_HASH_ENTRIES]; #endif -int pd_comm_is_enabled(int port) -{ +int pd_comm_is_enabled(int port) { #ifdef CONFIG_COMMON_RUNTIME return pd_comm_enabled[port]; #else @@ -257,10 +256,8 @@ int pd_comm_is_enabled(int port) #endif } -static inline void set_state_timeout(int port, - uint64_t timeout, - enum pd_states timeout_state) -{ +static inline void set_state_timeout(int port, uint64_t timeout, + enum pd_states timeout_state) { pd[port].timeout = timeout; pd[port].timeout_state = timeout_state; } @@ -278,8 +275,7 @@ int pd_get_vdo_ver(int port) #endif /* Return flag for pd state is connected */ -int pd_is_connected(int port) -{ +int pd_is_connected(int port) { if (pd[port].task_state == PD_STATE_DISABLED) return 0; @@ -289,31 +285,26 @@ int pd_is_connected(int port) #endif return DUAL_ROLE_IF_ELSE(port, - /* sink */ - pd[port].task_state != PD_STATE_SNK_DISCONNECTED && - pd[port].task_state != PD_STATE_SNK_DISCONNECTED_DEBOUNCE, - /* source */ - pd[port].task_state != PD_STATE_SRC_DISCONNECTED && - pd[port].task_state != PD_STATE_SRC_DISCONNECTED_DEBOUNCE); + /* sink */ + pd[port].task_state != PD_STATE_SNK_DISCONNECTED && pd[port].task_state != PD_STATE_SNK_DISCONNECTED_DEBOUNCE, + /* source */ + pd[port].task_state != PD_STATE_SRC_DISCONNECTED && pd[port].task_state != PD_STATE_SRC_DISCONNECTED_DEBOUNCE); } /* * Return true if partner port is a DTS or TS capable of entering debug * mode (eg. is presenting Rp/Rp or Rd/Rd). */ -int pd_ts_dts_plugged(int port) -{ +int pd_ts_dts_plugged(int port) { return pd[port].flags & PD_FLAGS_TS_DTS_PARTNER; } #ifdef CONFIG_USB_PD_DUAL_ROLE -void pd_vbus_low(int port) -{ +void pd_vbus_low(int port) { pd[port].flags &= ~PD_FLAGS_VBUS_NEVER_LOW; } -static inline int pd_is_vbus_present(int port) -{ +static inline int pd_is_vbus_present(int port) { #ifdef CONFIG_USB_PD_VBUS_DETECT_TCPC return tcpm_get_vbus_level(port); #else @@ -322,14 +313,13 @@ static inline int pd_is_vbus_present(int port) } #endif -static inline void set_state(int port, enum pd_states next_state) -{ +static inline void set_state(int port, enum pd_states next_state) { enum pd_states last_state = pd[port].task_state; #ifdef CONFIG_LOW_POWER_IDLE int i; #endif - set_state_timeout(port, 0, 0); + set_state_timeout(port, 0, PD_STATE_DISABLED); pd[port].task_state = next_state; if (last_state == next_state) @@ -343,14 +333,14 @@ static inline void set_state(int port, enum pd_states next_state) #endif /* Ignore dual-role toggling between sink and source */ - if ((last_state == PD_STATE_SNK_DISCONNECTED && - next_state == PD_STATE_SRC_DISCONNECTED) || - (last_state == PD_STATE_SRC_DISCONNECTED && - next_state == PD_STATE_SNK_DISCONNECTED)) + if ((last_state == PD_STATE_SNK_DISCONNECTED + && next_state == PD_STATE_SRC_DISCONNECTED) + || (last_state == PD_STATE_SRC_DISCONNECTED + && next_state == PD_STATE_SNK_DISCONNECTED)) return; - if (next_state == PD_STATE_SRC_DISCONNECTED || - next_state == PD_STATE_SNK_DISCONNECTED) { + if (next_state == PD_STATE_SRC_DISCONNECTED + || next_state == PD_STATE_SNK_DISCONNECTED) { /* Clear the input current limit */ pd_set_input_current_limit(port, 0, 0); #ifdef CONFIG_CHARGE_MANAGER @@ -418,14 +408,13 @@ static inline void set_state(int port, enum pd_states next_state) if (debug_level >= 1) CPRINTF("C%d st%d %s\n", port, next_state, - pd_state_names[next_state]); + pd_state_names[next_state]); else CPRINTF("C%d st%d\n", port, next_state); } /* increment message ID counter */ -static void inc_id(int port) -{ +static void inc_id(int port) { pd[port].msg_id = (pd[port].msg_id + 1) & PD_MESSAGE_ID_COUNT; } @@ -442,8 +431,7 @@ static inline void pd_ca_reset(int port) } #endif -void pd_transmit_complete(int port, int status) -{ +void pd_transmit_complete(int port, int status) { if (status == TCPC_TX_COMPLETE_SUCCESS) inc_id(port); @@ -452,9 +440,8 @@ void pd_transmit_complete(int port, int status) pd_task_set_event(PD_EVENT_TX, 0); } -static int pd_transmit(int port, enum tcpm_transmit_type type, - uint16_t header, const uint32_t *data) -{ +static int pd_transmit(int port, enum tcpm_transmit_type type, uint16_t header, + const uint32_t *data) { int evt; /* If comms are disabled, do not transmit, return error */ @@ -518,7 +505,6 @@ static int pd_transmit(int port, enum tcpm_transmit_type type, /* Wait until TX is complete */ // Would wait, except that we're making tcpm_transmit blocking //evt = task_wait_event_mask(PD_EVENT_TX, PD_T_TCPC_TX_TIMEOUT); - #ifdef CONFIG_USB_PD_REV30 /* * If the source just completed a transmit, tell @@ -533,7 +519,7 @@ static int pd_transmit(int port, enum tcpm_transmit_type type, // removing task-based stuff from the library //if (evt & TASK_EVENT_TIMER) - // return -1; + // return -1; /* TODO: give different error condition for failed vs discarded */ return pd[port].tx_status == TCPC_TX_COMPLETE_SUCCESS ? 1 : -1; @@ -562,18 +548,15 @@ static void pd_ca_send_pending(int port) } #endif -static void pd_update_roles(int port) -{ +static void pd_update_roles(int port) { /* Notify TCPC of role update */ tcpm_set_msg_header(port, pd[port].power_role, pd[port].data_role); } -static int send_control(int port, int type) -{ +static int send_control(int port, int type) { int bit_len; - uint16_t header = PD_HEADER(type, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, 0, - pd_get_rev(port), 0); + uint16_t header = PD_HEADER(type, pd[port].power_role, pd[port].data_role, + pd[port].msg_id, 0, pd_get_rev(port), 0); bit_len = pd_transmit(port, TCPC_TX_SOP, header, NULL); if (debug_level >= 2) @@ -582,8 +565,7 @@ static int send_control(int port, int type) return bit_len; } -static int send_source_cap(int port) -{ +static int send_source_cap(int port) { int bit_len; #if defined(CONFIG_USB_PD_DYNAMIC_SRC_CAP) || \ defined(CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT) @@ -598,12 +580,11 @@ static int send_source_cap(int port) if (src_pdo_cnt == 0) /* No source capabilities defined, sink only */ header = PD_HEADER(PD_CTRL_REJECT, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, 0, - pd_get_rev(port), 0); + pd[port].data_role, pd[port].msg_id, 0, pd_get_rev(port), 0); else header = PD_HEADER(PD_DATA_SOURCE_CAP, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, src_pdo_cnt, - pd_get_rev(port), 0); + pd[port].data_role, pd[port].msg_id, src_pdo_cnt, + pd_get_rev(port), 0); bit_len = pd_transmit(port, TCPC_TX_SOP, header, src_pdo); if (debug_level >= 2) @@ -768,8 +749,7 @@ static int send_battery_status(int port, uint32_t *payload) #endif #ifdef CONFIG_USB_PD_DUAL_ROLE -static void send_sink_cap(int port) -{ +static void send_sink_cap(int port) { int bit_len; uint16_t header = PD_HEADER(PD_DATA_SINK_CAP, pd[port].power_role, pd[port].data_role, pd[port].msg_id, pd_snk_pdo_cnt, @@ -780,12 +760,10 @@ static void send_sink_cap(int port) CPRINTF("snkCAP>%d\n", bit_len); } -static int send_request(int port, uint32_t rdo) -{ +static int send_request(int port, uint32_t rdo) { int bit_len; uint16_t header = PD_HEADER(PD_DATA_REQUEST, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, 1, - pd_get_rev(port), 0); + pd[port].data_role, pd[port].msg_id, 1, pd_get_rev(port), 0); bit_len = pd_transmit(port, TCPC_TX_SOP, header, &rdo); if (debug_level >= 2) @@ -833,8 +811,7 @@ static int send_bist_cmd(int port) #endif static void queue_vdm(int port, uint32_t *header, const uint32_t *data, - int data_cnt) -{ + int data_cnt) { pd[port].vdo_count = data_cnt + 1; pd[port].vdo_data[0] = header[0]; memcpy(&pd[port].vdo_data[1], data, sizeof(uint32_t) * data_cnt); @@ -842,8 +819,7 @@ static void queue_vdm(int port, uint32_t *header, const uint32_t *data, pd[port].vdm_state = VDM_STATE_READY; } -static void handle_vdm_request(int port, int cnt, uint32_t *payload) -{ +static void handle_vdm_request(int port, int cnt, uint32_t *payload) { int rlen = 0; uint32_t *rdata; @@ -851,10 +827,10 @@ static void handle_vdm_request(int port, int cnt, uint32_t *payload) /* If UFP responded busy retry after timeout */ if (PD_VDO_CMDT(payload[0]) == CMDT_RSP_BUSY) { pd[port].vdm_timeout.val = get_time().val + - PD_T_VDM_BUSY; + PD_T_VDM_BUSY; pd[port].vdm_state = VDM_STATE_WAIT_RSP_BUSY; pd[port].vdo_retry = (payload[0] & ~VDO_CMDT_MASK) | - CMDT_INIT; + CMDT_INIT; return; } else { pd[port].vdm_state = VDM_STATE_DONE; @@ -872,11 +848,10 @@ static void handle_vdm_request(int port, int cnt, uint32_t *payload) } if (debug_level >= 2) CPRINTF("Unhandled VDM VID %04x CMD %04x\n", - PD_VDO_VID(payload[0]), payload[0] & 0xFFFF); + PD_VDO_VID(payload[0]), payload[0] & 0xFFFF); } -void pd_execute_hard_reset(int port) -{ +void pd_execute_hard_reset(int port) { if (pd[port].last_state == PD_STATE_HARD_RESET_SEND) CPRINTF("C%d HARD RST TX\n", port); else @@ -902,8 +877,8 @@ void pd_execute_hard_reset(int port) * If we are swapping to a source and have changed to Rp, restore back * to Rd and turn off vbus to match our power_role. */ - if (pd[port].task_state == PD_STATE_SNK_SWAP_STANDBY || - pd[port].task_state == PD_STATE_SNK_SWAP_COMPLETE) { + if (pd[port].task_state == PD_STATE_SNK_SWAP_STANDBY + || pd[port].task_state == PD_STATE_SNK_SWAP_COMPLETE) { tcpm_set_cc(port, TYPEC_CC_RD); pd_power_supply_reset(port); } @@ -928,16 +903,15 @@ void pd_execute_hard_reset(int port) set_state(port, PD_STATE_SRC_HARD_RESET_RECOVER); } -static void execute_soft_reset(int port) -{ +static void execute_soft_reset(int port) { pd[port].msg_id = 0; - set_state(port, DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_DISCOVERY, - PD_STATE_SRC_DISCOVERY)); + set_state(port, + DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_DISCOVERY, + PD_STATE_SRC_DISCOVERY)); CPRINTF("C%d Soft Rst\n", port); } -void pd_soft_reset(void) -{ +void pd_soft_reset(void) { int i; for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; ++i) @@ -953,8 +927,7 @@ void pd_soft_reset(void) * Request desired charge voltage from source. * Returns EC_SUCCESS on success or non-zero on failure. */ -static int pd_send_request_msg(int port, int always_send_request) -{ +static int pd_send_request_msg(int port, int always_send_request) { uint32_t rdo, curr_limit, supply_voltage; int res; @@ -980,7 +953,7 @@ static int pd_send_request_msg(int port, int always_send_request) * request the max voltage, then select vSafe5V */ res = pd_build_request(port, &rdo, &curr_limit, &supply_voltage, - charging && max_request_allowed ? + charging && max_request_allowed ? PD_REQUEST_MAX : PD_REQUEST_VSAFE5V); if (res != EC_SUCCESS) @@ -1002,10 +975,9 @@ static int pd_send_request_msg(int port, int always_send_request) } CPRINTF("Req C%d [%d] %dmV %dmA", port, RDO_POS(rdo), - supply_voltage, curr_limit); + supply_voltage, curr_limit); if (rdo & RDO_CAP_MISMATCH) - CPRINTF(" Mismatch"); - CPRINTF("\n"); + CPRINTF(" Mismatch"); CPRINTF("\n"); pd[port].curr_limit = curr_limit; pd[port].supply_voltage = supply_voltage; @@ -1018,8 +990,7 @@ static int pd_send_request_msg(int port, int always_send_request) } #endif -static void pd_update_pdo_flags(int port, uint32_t pdo) -{ +static void pd_update_pdo_flags(int port, uint32_t pdo) { #ifdef CONFIG_CHARGE_MANAGER #ifdef CONFIG_USB_PD_ALT_MODE_DFP int charge_whitelisted = @@ -1065,19 +1036,17 @@ static void pd_update_pdo_flags(int port, uint32_t pdo) * charging white-list. */ /* - if (!(pd[port].flags & PD_FLAGS_PARTNER_DR_POWER) || - (pd[port].flags & PD_FLAGS_PARTNER_EXTPOWER) || - charge_whitelisted) - charge_manager_update_dualrole(port, CAP_DEDICATED); - else - charge_manager_update_dualrole(port, CAP_DUALROLE); - */ + if (!(pd[port].flags & PD_FLAGS_PARTNER_DR_POWER) || + (pd[port].flags & PD_FLAGS_PARTNER_EXTPOWER) || + charge_whitelisted) + charge_manager_update_dualrole(port, CAP_DEDICATED); + else + charge_manager_update_dualrole(port, CAP_DUALROLE); + */ #endif } -static void handle_data_request(int port, uint16_t head, - uint32_t *payload) -{ +static void handle_data_request(int port, uint16_t head, uint32_t *payload) { int type = PD_HEADER_TYPE(head); int cnt = PD_HEADER_CNT(head); @@ -1085,12 +1054,12 @@ static void handle_data_request(int port, uint16_t head, #ifdef CONFIG_USB_PD_DUAL_ROLE case PD_DATA_SOURCE_CAP: if ((pd[port].task_state == PD_STATE_SNK_DISCOVERY) - || (pd[port].task_state == PD_STATE_SNK_TRANSITION) + || (pd[port].task_state == PD_STATE_SNK_TRANSITION) #ifdef CONFIG_USB_PD_VBUS_DETECT_NONE || (pd[port].task_state == PD_STATE_SNK_HARD_RESET_RECOVER) #endif - || (pd[port].task_state == PD_STATE_SNK_READY)) { + || (pd[port].task_state == PD_STATE_SNK_READY)) { #ifdef CONFIG_USB_PD_REV30 /* * Only adjust sink rev if source rev is higher. @@ -1108,7 +1077,7 @@ static void handle_data_request(int port, uint16_t head, /* Source will resend source cap on failure */ pd_send_request_msg(port, 1); - + // We call the callback after we send the request // because the timing on Request seems to be sensitive // User code can take the time until PS_RDY to do stuff @@ -1162,18 +1131,17 @@ static void handle_data_request(int port, uint16_t head, break; case PD_DATA_BIST: /* If not in READY state, then don't start BIST */ - if (DUAL_ROLE_IF_ELSE(port, - pd[port].task_state == PD_STATE_SNK_READY, + if (DUAL_ROLE_IF_ELSE(port, pd[port].task_state == PD_STATE_SNK_READY, pd[port].task_state == PD_STATE_SRC_READY)) { /* currently only support sending bist carrier mode 2 */ if ((payload[0] >> 28) == 5) { /* bist data object mode is 2 */ pd_transmit(port, TCPC_TX_BIST_MODE_2, 0, - NULL); + NULL); /* Set to appropriate port disconnected state */ - set_state(port, DUAL_ROLE_IF_ELSE(port, - PD_STATE_SNK_DISCONNECTED, - PD_STATE_SRC_DISCONNECTED)); + set_state(port, + DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_DISCONNECTED, + PD_STATE_SRC_DISCONNECTED)); } } break; @@ -1197,8 +1165,7 @@ static void handle_data_request(int port, uint16_t head, } #ifdef CONFIG_USB_PD_DUAL_ROLE -void pd_request_power_swap(int port) -{ +void pd_request_power_swap(int port) { if (pd[port].task_state == PD_STATE_SRC_READY) set_state(port, PD_STATE_SRC_SWAP_INIT); else if (pd[port].task_state == PD_STATE_SNK_READY) @@ -1231,18 +1198,15 @@ void pd_try_vconn_src(int port) #endif #endif /* CONFIG_USB_PD_DUAL_ROLE */ -void pd_request_data_swap(int port) -{ - if (DUAL_ROLE_IF_ELSE(port, - pd[port].task_state == PD_STATE_SNK_READY, - pd[port].task_state == PD_STATE_SRC_READY)) +void pd_request_data_swap(int port) { + if (DUAL_ROLE_IF_ELSE(port, pd[port].task_state == PD_STATE_SNK_READY, + pd[port].task_state == PD_STATE_SRC_READY)) set_state(port, PD_STATE_DR_SWAP); // getting rid of task stuff //task_wake(PD_PORT_TO_TASK_ID(port)); } -static void pd_set_data_role(int port, int role) -{ +static void pd_set_data_role(int port, int role) { pd[port].data_role = role; pd_execute_data_swap(port, role); @@ -1266,15 +1230,12 @@ static void pd_set_data_role(int port, int role) pd_update_roles(port); } -static void pd_dr_swap(int port) -{ +static void pd_dr_swap(int port) { pd_set_data_role(port, !pd[port].data_role); pd[port].flags |= PD_FLAGS_CHECK_IDENTITY; } -static void handle_ctrl_request(int port, uint16_t head, - uint32_t *payload) -{ +static void handle_ctrl_request(int port, uint16_t head, uint32_t *payload) { int type = PD_HEADER_TYPE(head); int res; @@ -1287,8 +1248,7 @@ static void handle_ctrl_request(int port, uint16_t head, break; case PD_CTRL_GET_SOURCE_CAP: res = send_source_cap(port); - if ((res >= 0) && - (pd[port].task_state == PD_STATE_SRC_DISCOVERY)) + if ((res >= 0) && (pd[port].task_state == PD_STATE_SRC_DISCOVERY)) set_state(port, PD_STATE_SRC_NEGOCIATE); break; case PD_CTRL_GET_SINK_CAP: @@ -1342,7 +1302,7 @@ static void handle_ctrl_request(int port, uint16_t head, } else if (pd[port].power_role == PD_ROLE_SINK) { set_state(port, PD_STATE_SNK_READY); pd_set_input_current_limit(port, pd[port].curr_limit, - pd[port].supply_voltage); + pd[port].supply_voltage); #ifdef CONFIG_CHARGE_MANAGER /* Set ceiling based on what's negotiated */ //charge_manager_set_ceil(port, @@ -1401,8 +1361,7 @@ static void handle_ctrl_request(int port, uint16_t head, * after PD_T_SINK_REQUEST ms. */ set_state_timeout(port, get_time().val + - PD_T_SINK_REQUEST, - PD_STATE_SNK_READY); + PD_T_SINK_REQUEST, PD_STATE_SNK_READY); } else { /* The request was rejected */ set_state(port, PD_STATE_SNK_READY); @@ -1465,9 +1424,8 @@ static void handle_ctrl_request(int port, uint16_t head, */ pd[port].flags &= ~PD_FLAGS_CHECK_PR_ROLE; set_state(port, - DUAL_ROLE_IF_ELSE(port, - PD_STATE_SNK_SWAP_SNK_DISABLE, - PD_STATE_SRC_SWAP_SNK_DISABLE)); + DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_SWAP_SNK_DISABLE, + PD_STATE_SRC_SWAP_SNK_DISABLE)); } else { send_control(port, REFUSE(pd[port].rev)); } @@ -1534,19 +1492,16 @@ static void handle_ext_request(int port, uint16_t head, uint32_t *payload) } #endif -static void handle_request(int port, uint16_t head, - uint32_t *payload) -{ +static void handle_request(int port, uint16_t head, uint32_t *payload) { int cnt = PD_HEADER_CNT(head); int p; /* dump received packet content (only dump ping at debug level 3) */ - if ((debug_level == 2 && PD_HEADER_TYPE(head) != PD_CTRL_PING) || - debug_level >= 3) { + if ((debug_level == 2 && PD_HEADER_TYPE(head) != PD_CTRL_PING) + || debug_level >= 3) { CPRINTF("RECV %04x/%d ", head, cnt); for (p = 0; p < cnt; p++) - CPRINTF("[%d]%08x ", p, payload[p]); - CPRINTF("\n"); + CPRINTF("[%d]%08x ", p, payload[p]); CPRINTF("\n"); } /* @@ -1570,16 +1525,17 @@ static void handle_request(int port, uint16_t head, } void pd_send_vdm(int port, uint32_t vid, int cmd, const uint32_t *data, - int count) -{ + int count) { if (count > VDO_MAX_SIZE - 1) { CPRINTF("VDM over max size\n"); return; } /* set VDM header with VID & CMD */ - pd[port].vdo_data[0] = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ? - 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd); + pd[port].vdo_data[0] = + VDO(vid, + ((vid & USB_SID_PD) == USB_SID_PD) ? 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), + cmd); #ifdef CONFIG_USB_PD_REV30 pd[port].vdo_data[0] |= VDO_SVDM_VERS(vdo_ver[pd[port].rev]); #endif @@ -1589,8 +1545,7 @@ void pd_send_vdm(int port, uint32_t vid, int cmd, const uint32_t *data, //task_wake(PD_PORT_TO_TASK_ID(port)); } -static inline int pdo_busy(int port) -{ +static inline int pdo_busy(int port) { /* * Note, main PDO state machine (pd_task) uses READY state exclusively * to denote port partners have successfully negociated a contract. All @@ -1603,14 +1558,13 @@ static inline int pdo_busy(int port) return rv; } -static uint64_t vdm_get_ready_timeout(uint32_t vdm_hdr) -{ +static uint64_t vdm_get_ready_timeout(uint32_t vdm_hdr) { uint64_t timeout; int cmd = PD_VDO_CMD(vdm_hdr); /* its not a structured VDM command */ if (!PD_VDO_SVDM(vdm_hdr)) - return 500*MSEC; + return 500 * MSEC; switch (PD_VDO_CMDT(vdm_hdr)) { case CMDT_INIT: @@ -1629,8 +1583,7 @@ static uint64_t vdm_get_ready_timeout(uint32_t vdm_hdr) return timeout; } -static void pd_vdm_send_state_machine(int port) -{ +static void pd_vdm_send_state_machine(int port) { int res; uint16_t header; @@ -1651,17 +1604,15 @@ static void pd_vdm_send_state_machine(int port) /* Prepare and send VDM */ header = PD_HEADER(PD_DATA_VENDOR_DEF, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, - (int)pd[port].vdo_count, - pd_get_rev(port), 0); - res = pd_transmit(port, TCPC_TX_SOP, header, - pd[port].vdo_data); + pd[port].data_role, pd[port].msg_id, (int )pd[port].vdo_count, + pd_get_rev(port), 0); + res = pd_transmit(port, TCPC_TX_SOP, header, pd[port].vdo_data); if (res < 0) { pd[port].vdm_state = VDM_STATE_ERR_SEND; } else { pd[port].vdm_state = VDM_STATE_BUSY; - pd[port].vdm_timeout.val = get_time().val + - vdm_get_ready_timeout(pd[port].vdo_data[0]); + pd[port].vdm_timeout.val = get_time().val + + vdm_get_ready_timeout(pd[port].vdo_data[0]); } break; case VDM_STATE_WAIT_RSP_BUSY: @@ -1674,8 +1625,8 @@ static void pd_vdm_send_state_machine(int port) break; case VDM_STATE_BUSY: /* Wait for VDM response or timeout */ - if (pd[port].vdm_timeout.val && - (get_time().val > pd[port].vdm_timeout.val)) { + if (pd[port].vdm_timeout.val + && (get_time().val > pd[port].vdm_timeout.val)) { pd[port].vdm_state = VDM_STATE_ERR_TMOUT; } break; @@ -1699,8 +1650,7 @@ static inline void pd_dev_dump_info(uint16_t dev_id, uint8_t *hash) #endif /* CONFIG_CMD_PD_DEV_DUMP_INFO */ int pd_dev_store_rw_hash(int port, uint16_t dev_id, uint32_t *rw_hash, - uint32_t current_image) -{ + uint32_t current_image) { #ifdef CONFIG_COMMON_RUNTIME int i; #endif @@ -1729,8 +1679,7 @@ int pd_dev_store_rw_hash(int port, uint16_t dev_id, uint32_t *rw_hash, } #ifdef CONFIG_USB_PD_DUAL_ROLE -enum pd_dual_role_states pd_get_dual_role(void) -{ +enum pd_dual_role_states pd_get_dual_role(void) { return drp_state; } @@ -1773,8 +1722,7 @@ static void pd_update_try_source(void) DECLARE_HOOK(HOOK_BATTERY_SOC_CHANGE, pd_update_try_source, HOOK_PRIO_DEFAULT); #endif -void pd_set_dual_role(enum pd_dual_role_states state) -{ +void pd_set_dual_role(enum pd_dual_role_states state) { int i; drp_state = state; @@ -1790,18 +1738,17 @@ void pd_set_dual_role(enum pd_dual_role_states state) ; } -void pd_update_dual_role_config(int port) -{ +void pd_update_dual_role_config(int port) { /* * Change to sink if port is currently a source AND (new DRP * state is force sink OR new DRP state is either toggle off * or debug accessory toggle only and we are in the source * disconnected state). */ - if (pd[port].power_role == PD_ROLE_SOURCE && - ((drp_state == PD_DRP_FORCE_SINK && !pd_ts_dts_plugged(port)) || - (drp_state == PD_DRP_TOGGLE_OFF - && pd[port].task_state == PD_STATE_SRC_DISCONNECTED))) { + if (pd[port].power_role == PD_ROLE_SOURCE + && ((drp_state == PD_DRP_FORCE_SINK && !pd_ts_dts_plugged(port)) + || (drp_state == PD_DRP_TOGGLE_OFF + && pd[port].task_state == PD_STATE_SRC_DISCONNECTED))) { pd[port].power_role = PD_ROLE_SINK; set_state(port, PD_STATE_SNK_DISCONNECTED); tcpm_set_cc(port, TYPEC_CC_RD); @@ -1813,8 +1760,8 @@ void pd_update_dual_role_config(int port) * Change to source if port is currently a sink and the * new DRP state is force source. */ - if (pd[port].power_role == PD_ROLE_SINK && - drp_state == PD_DRP_FORCE_SOURCE) { + if (pd[port].power_role == PD_ROLE_SINK + && drp_state == PD_DRP_FORCE_SOURCE) { pd[port].power_role = PD_ROLE_SOURCE; set_state(port, PD_STATE_SRC_DISCONNECTED); tcpm_set_cc(port, TYPEC_CC_RP); @@ -1827,29 +1774,26 @@ void pd_update_dual_role_config(int port) #endif } -int pd_get_role(int port) -{ +int pd_get_role(int port) { return pd[port].power_role; } -static int pd_is_power_swapping(int port) -{ +static int pd_is_power_swapping(int port) { /* return true if in the act of swapping power roles */ - return pd[port].task_state == PD_STATE_SNK_SWAP_SNK_DISABLE || - pd[port].task_state == PD_STATE_SNK_SWAP_SRC_DISABLE || - pd[port].task_state == PD_STATE_SNK_SWAP_STANDBY || - pd[port].task_state == PD_STATE_SNK_SWAP_COMPLETE || - pd[port].task_state == PD_STATE_SRC_SWAP_SNK_DISABLE || - pd[port].task_state == PD_STATE_SRC_SWAP_SRC_DISABLE || - pd[port].task_state == PD_STATE_SRC_SWAP_STANDBY; + return pd[port].task_state == PD_STATE_SNK_SWAP_SNK_DISABLE + || pd[port].task_state == PD_STATE_SNK_SWAP_SRC_DISABLE + || pd[port].task_state == PD_STATE_SNK_SWAP_STANDBY + || pd[port].task_state == PD_STATE_SNK_SWAP_COMPLETE + || pd[port].task_state == PD_STATE_SRC_SWAP_SNK_DISABLE + || pd[port].task_state == PD_STATE_SRC_SWAP_SRC_DISABLE + || pd[port].task_state == PD_STATE_SRC_SWAP_STANDBY; } /* * Provide Rp to ensure the partner port is in a known state (eg. not * PD negotiated, not sourcing 20V). */ -static void pd_partner_port_reset(int port) -{ +static void pd_partner_port_reset(int port) { uint64_t timeout; #ifdef CONFIG_BBRAM @@ -1876,13 +1820,11 @@ static void pd_partner_port_reset(int port) } #endif /* CONFIG_USB_PD_DUAL_ROLE */ -int pd_get_polarity(int port) -{ +int pd_get_polarity(int port) { return pd[port].polarity; } -int pd_get_partner_data_swap_capable(int port) -{ +int pd_get_partner_data_swap_capable(int port) { /* return data swap capable status of port partner */ return pd[port].flags & PD_FLAGS_PARTNER_DR_DATA; } @@ -1910,8 +1852,7 @@ void pd_comm_enable(int port, int enable) } #endif -void pd_ping_enable(int port, int enable) -{ +void pd_ping_enable(int port, int enable) { if (enable) pd[port].flags |= PD_FLAGS_PING_ENABLED; else @@ -1921,10 +1862,9 @@ void pd_ping_enable(int port, int enable) /** * Returns whether the sink has detected a Rp resistor on the other side. */ -static inline int cc_is_rp(int cc) -{ - return (cc == TYPEC_CC_VOLT_SNK_DEF) || (cc == TYPEC_CC_VOLT_SNK_1_5) || - (cc == TYPEC_CC_VOLT_SNK_3_0); +static inline int cc_is_rp(int cc) { + return (cc == TYPEC_CC_VOLT_SNK_DEF) || (cc == TYPEC_CC_VOLT_SNK_1_5) + || (cc == TYPEC_CC_VOLT_SNK_3_0); } /* @@ -1938,13 +1878,12 @@ static inline int cc_is_rp(int cc) * DTS Default USB Power Rp3A0 Rp1A5 * DTS USB-C @ 1.5 A Rp1A5 RpUSB * DTS USB-C @ 3 A Rp3A0 RpUSB -*/ + */ /** * Returns the polarity of a Sink. */ -static inline int get_snk_polarity(int cc1, int cc2) -{ +static inline int get_snk_polarity(int cc1, int cc2) { /* the following assumes: * TYPEC_CC_VOLT_SNK_3_0 > TYPEC_CC_VOLT_SNK_1_5 * TYPEC_CC_VOLT_SNK_1_5 > TYPEC_CC_VOLT_SNK_DEF @@ -1957,8 +1896,7 @@ static inline int get_snk_polarity(int cc1, int cc2) /** * Returns type C current limit (mA) based upon cc_voltage (mV). */ -static typec_current_t get_typec_current_limit(int polarity, int cc1, int cc2) -{ +static typec_current_t get_typec_current_limit(int polarity, int cc1, int cc2) { typec_current_t charge; int cc = polarity ? cc2 : cc1; int cc_alt = polarity ? cc1 : cc2; @@ -1979,8 +1917,7 @@ static typec_current_t get_typec_current_limit(int polarity, int cc1, int cc2) /** * Signal power request to indicate a charger update that affects the port. */ -void pd_set_new_power_request(int port) -{ +void pd_set_new_power_request(int port) { pd[port].new_power_request = 1; // getting rid of task stuff //task_wake(PD_PORT_TO_TASK_ID(port)); @@ -2035,8 +1972,7 @@ static void pd_init_tasks(void) #endif /* CONFIG_COMMON_RUNTIME */ #ifndef CONFIG_USB_PD_TCPC -static int pd_restart_tcpc(int port) -{ +static int pd_restart_tcpc(int port) { if (board_set_tcpc_power_mode) { /* force chip reset */ board_set_tcpc_power_mode(port, 0); @@ -2045,8 +1981,7 @@ static int pd_restart_tcpc(int port) } #endif -void pd_init(int port) -{ +void pd_init(int port) { #ifdef CONFIG_COMMON_RUNTIME pd_init_tasks(); #endif @@ -2073,8 +2008,8 @@ void pd_init(int port) struct ec_response_pd_chip_info *info; tcpm_get_chip_info(port, 0, &info); CPRINTS("TCPC p%d VID:0x%x PID:0x%x DID:0x%x FWV:0x%lx", - port, info->vendor_id, info->product_id, - info->device_id, info->fw_version_number); + port, info->vendor_id, info->product_id, + info->device_id, info->fw_version_number); } #endif @@ -2112,8 +2047,9 @@ void pd_init(int port) #else tcpm_select_rp_value(port, CONFIG_USB_PD_PULLUP); #endif - tcpm_set_cc(port, PD_ROLE_DEFAULT(port) == PD_ROLE_SOURCE ? - TYPEC_CC_RP : TYPEC_CC_RD); + tcpm_set_cc(port, + PD_ROLE_DEFAULT(port) == PD_ROLE_SOURCE ? + TYPEC_CC_RP : TYPEC_CC_RD); #ifdef CONFIG_USB_PD_ALT_MODE_DFP /* Initialize PD Policy engine */ @@ -2128,8 +2064,7 @@ void pd_init(int port) #endif } -void pd_run_state_machine(int port) -{ +void pd_run_state_machine(int port) { #ifdef CONFIG_USB_PD_REV30 /* send any pending messages */ pd_ca_send_pending(port); @@ -2149,7 +2084,6 @@ void pd_run_state_machine(int port) /* wait for next event/packet or timeout expiration */ // getting rid of task stuff //evt = task_wait_event(timeout); - #ifdef CONFIG_USB_PD_DUAL_ROLE if (evt & PD_EVENT_UPDATE_DUAL_ROLE) pd_update_dual_role_config(port); @@ -2175,25 +2109,26 @@ void pd_run_state_machine(int port) #endif /* Ensure CC termination is default */ tcpm_set_cc(port, PD_ROLE_DEFAULT(port) == - PD_ROLE_SOURCE ? TYPEC_CC_RP : TYPEC_CC_RD); + PD_ROLE_SOURCE ? TYPEC_CC_RP : TYPEC_CC_RD); /* - * If we have a stable contract in the default role, - * then simply update TCPC with some missing info - * so that we can continue without resetting PD comms. - * Otherwise, go to the default disconnected state - * and force renegotiation. - */ - if (pd[port].vdm_state == VDM_STATE_DONE && ( + * If we have a stable contract in the default role, + * then simply update TCPC with some missing info + * so that we can continue without resetting PD comms. + * Otherwise, go to the default disconnected state + * and force renegotiation. + */ + if (pd[port].vdm_state == VDM_STATE_DONE + && ( #ifdef CONFIG_USB_PD_DUAL_ROLE - (PD_ROLE_DEFAULT(port) == PD_ROLE_SINK && - pd[port].task_state == PD_STATE_SNK_READY) || + (PD_ROLE_DEFAULT(port) == PD_ROLE_SINK + && pd[port].task_state == PD_STATE_SNK_READY) + || #endif - (PD_ROLE_DEFAULT(port) == PD_ROLE_SOURCE && - pd[port].task_state == PD_STATE_SRC_READY))) { + (PD_ROLE_DEFAULT(port) == PD_ROLE_SOURCE + && pd[port].task_state == PD_STATE_SRC_READY))) { tcpm_set_polarity(port, pd[port].polarity); - tcpm_set_msg_header(port, pd[port].power_role, - pd[port].data_role); + tcpm_set_msg_header(port, pd[port].power_role, pd[port].data_role); tcpm_set_rx_enable(port, 1); } else { /* Ensure state variables are at default */ @@ -2207,8 +2142,8 @@ void pd_run_state_machine(int port) /* process any potential incoming message */ incoming_packet = evt & PD_EVENT_RX; //if (incoming_packet) { - if (!tcpm_get_message(port, payload, &head)) - handle_request(port, head, payload); + if (!tcpm_get_message(port, payload, &head)) + handle_request(port, head, payload); //} if (pd[port].req_suspend_state) @@ -2216,13 +2151,13 @@ void pd_run_state_machine(int port) /* if nothing to do, verify the state of the world in 500ms */ this_state = pd[port].task_state; - timeout = 500*MSEC; + timeout = 500 * MSEC; switch (this_state) { case PD_STATE_DISABLED: /* Nothing to do */ break; case PD_STATE_SRC_DISCONNECTED: - timeout = 10*MSEC; + timeout = 10 * MSEC; tcpm_get_cc(port, &cc1, &cc2); #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE /* @@ -2241,66 +2176,59 @@ void pd_run_state_machine(int port) #endif /* Vnc monitoring */ - if ((cc1 == TYPEC_CC_VOLT_RD || - cc2 == TYPEC_CC_VOLT_RD) || - (cc1 == TYPEC_CC_VOLT_RA && - cc2 == TYPEC_CC_VOLT_RA)) { + if ((cc1 == TYPEC_CC_VOLT_RD || cc2 == TYPEC_CC_VOLT_RD) + || (cc1 == TYPEC_CC_VOLT_RA && cc2 == TYPEC_CC_VOLT_RA)) { #ifdef CONFIG_USBC_BACKWARDS_COMPATIBLE_DFP /* Enable VBUS */ if (pd_set_power_supply_ready(port)) break; #endif pd[port].cc_state = PD_CC_NONE; - set_state(port, - PD_STATE_SRC_DISCONNECTED_DEBOUNCE); + set_state(port, PD_STATE_SRC_DISCONNECTED_DEBOUNCE); } #if defined(CONFIG_USB_PD_DUAL_ROLE) /* - * Try.SRC state is embedded here. Wait for SNK - * detect, or if timer expires, transition to - * SNK_DISCONNETED. - * - * If Try.SRC state is not active, then this block - * handles the normal DRP toggle from SRC->SNK - */ - else if ((pd[port].flags & PD_FLAGS_TRY_SRC && - get_time().val >= pd[port].try_src_marker) || - (!(pd[port].flags & PD_FLAGS_TRY_SRC) && - drp_state != PD_DRP_FORCE_SOURCE && - drp_state != PD_DRP_FREEZE && - get_time().val >= next_role_swap)) { + * Try.SRC state is embedded here. Wait for SNK + * detect, or if timer expires, transition to + * SNK_DISCONNETED. + * + * If Try.SRC state is not active, then this block + * handles the normal DRP toggle from SRC->SNK + */ + else if ((pd[port].flags & PD_FLAGS_TRY_SRC + && get_time().val >= pd[port].try_src_marker) + || (!(pd[port].flags & PD_FLAGS_TRY_SRC) + && drp_state != PD_DRP_FORCE_SOURCE + && drp_state != PD_DRP_FREEZE + && get_time().val >= next_role_swap)) { pd[port].power_role = PD_ROLE_SINK; set_state(port, PD_STATE_SNK_DISCONNECTED); tcpm_set_cc(port, TYPEC_CC_RD); next_role_swap = get_time().val + PD_T_DRP_SNK; - pd[port].try_src_marker = get_time().val - + PD_T_TRY_WAIT; + pd[port].try_src_marker = get_time().val + PD_T_TRY_WAIT; /* Swap states quickly */ - timeout = 2*MSEC; + timeout = 2 * MSEC; } #endif break; case PD_STATE_SRC_DISCONNECTED_DEBOUNCE: - timeout = 20*MSEC; + timeout = 20 * MSEC; tcpm_get_cc(port, &cc1, &cc2); - if (cc1 == TYPEC_CC_VOLT_RD && - cc2 == TYPEC_CC_VOLT_RD) { + if (cc1 == TYPEC_CC_VOLT_RD && cc2 == TYPEC_CC_VOLT_RD) { /* Debug accessory */ new_cc_state = PD_CC_DEBUG_ACC; - } else if (cc1 == TYPEC_CC_VOLT_RD || - cc2 == TYPEC_CC_VOLT_RD) { + } else if (cc1 == TYPEC_CC_VOLT_RD || cc2 == TYPEC_CC_VOLT_RD) { /* UFP attached */ new_cc_state = PD_CC_UFP_ATTACHED; - } else if (cc1 == TYPEC_CC_VOLT_RA && - cc2 == TYPEC_CC_VOLT_RA) { + } else if (cc1 == TYPEC_CC_VOLT_RA && cc2 == TYPEC_CC_VOLT_RA) { /* Audio accessory */ new_cc_state = PD_CC_AUDIO_ACC; } else { /* No UFP */ set_state(port, PD_STATE_SRC_DISCONNECTED); - timeout = 5*MSEC; + timeout = 5 * MSEC; break; } /* If in Try.SRC state, then don't need to debounce */ @@ -2308,19 +2236,18 @@ void pd_run_state_machine(int port) /* Debounce the cc state */ if (new_cc_state != pd[port].cc_state) { pd[port].cc_debounce = get_time().val + - PD_T_CC_DEBOUNCE; + PD_T_CC_DEBOUNCE; pd[port].cc_state = new_cc_state; break; - } else if (get_time().val < - pd[port].cc_debounce) { + } else if (get_time().val < pd[port].cc_debounce) { break; } } /* Debounce complete */ /* UFP is attached */ - if (new_cc_state == PD_CC_UFP_ATTACHED || - new_cc_state == PD_CC_DEBUG_ACC) { + if (new_cc_state == PD_CC_UFP_ATTACHED + || new_cc_state == PD_CC_DEBUG_ACC) { pd[port].polarity = (cc1 != TYPEC_CC_VOLT_RD); tcpm_set_polarity(port, pd[port].polarity); @@ -2329,7 +2256,7 @@ void pd_run_state_machine(int port) if (new_cc_state == PD_CC_DEBUG_ACC) pd[port].flags |= - PD_FLAGS_TS_DTS_PARTNER; + PD_FLAGS_TS_DTS_PARTNER; #ifndef CONFIG_USBC_BACKWARDS_COMPATIBLE_DFP /* Enable VBUS */ @@ -2352,25 +2279,25 @@ void pd_run_state_machine(int port) #endif pd[port].flags |= PD_FLAGS_CHECK_PR_ROLE | - PD_FLAGS_CHECK_DR_ROLE; + PD_FLAGS_CHECK_DR_ROLE; hard_reset_count = 0; - timeout = 5*MSEC; + timeout = 5 * MSEC; set_state(port, PD_STATE_SRC_STARTUP); } /* - * AUDIO_ACC will remain in this state indefinitely - * until disconnect. - */ + * AUDIO_ACC will remain in this state indefinitely + * until disconnect. + */ break; case PD_STATE_SRC_HARD_RESET_RECOVER: /* Do not continue until hard reset recovery time */ if (get_time().val < pd[port].src_recover) { - timeout = 50*MSEC; + timeout = 50 * MSEC; break; } /* Enable VBUS */ - timeout = 10*MSEC; + timeout = 10 * MSEC; if (pd_set_power_supply_ready(port)) { set_state(port, PD_STATE_SRC_DISCONNECTED); break; @@ -2395,8 +2322,7 @@ void pd_run_state_machine(int port) caps_count = 0; pd[port].msg_id = 0; snk_cap_count = 0; - set_state_timeout( - port, + set_state_timeout(port, #ifdef CONFIG_USBC_BACKWARDS_COMPATIBLE_DFP /* * delay for power supply to start up. @@ -2410,26 +2336,25 @@ void pd_run_state_machine(int port) PD_STATE_SRC_DISCONNECTED_DEBOUNCE ? PD_T_CC_DEBOUNCE : 0), #else - get_time().val + - PD_POWER_SUPPLY_TURN_ON_DELAY, + get_time().val + + PD_POWER_SUPPLY_TURN_ON_DELAY, #endif - PD_STATE_SRC_DISCOVERY); + PD_STATE_SRC_DISCOVERY); } break; case PD_STATE_SRC_DISCOVERY: if (pd[port].last_state != pd[port].task_state) { /* - * If we have had PD connection with this port - * partner, then start NoResponseTimer. - */ + * If we have had PD connection with this port + * partner, then start NoResponseTimer. + */ if (pd[port].flags & PD_FLAGS_PREVIOUS_PD_CONN) - set_state_timeout(port, - get_time().val + - PD_T_NO_RESPONSE, - hard_reset_count < + set_state_timeout(port, get_time().val + + PD_T_NO_RESPONSE, + hard_reset_count < PD_HARD_RESET_COUNT ? - PD_STATE_HARD_RESET_SEND : - PD_STATE_SRC_DISCONNECTED); + PD_STATE_HARD_RESET_SEND : + PD_STATE_SRC_DISCONNECTED); } /* Send source cap some minimum number of times */ @@ -2438,14 +2363,13 @@ void pd_run_state_machine(int port) res = send_source_cap(port); /* packet was acked => PD capable device) */ if (res >= 0) { - set_state(port, - PD_STATE_SRC_NEGOCIATE); - timeout = 10*MSEC; + set_state(port, PD_STATE_SRC_NEGOCIATE); + timeout = 10 * MSEC; hard_reset_count = 0; caps_count = 0; /* Port partner is PD capable */ pd[port].flags |= - PD_FLAGS_PREVIOUS_PD_CONN; + PD_FLAGS_PREVIOUS_PD_CONN; } else { /* failed, retry later */ timeout = PD_T_SEND_SOURCE_CAP; caps_count++; @@ -2455,36 +2379,28 @@ void pd_run_state_machine(int port) case PD_STATE_SRC_NEGOCIATE: /* wait for a "Request" message */ if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, - get_time().val + - PD_T_SENDER_RESPONSE, - PD_STATE_HARD_RESET_SEND); + set_state_timeout(port, get_time().val + + PD_T_SENDER_RESPONSE, PD_STATE_HARD_RESET_SEND); break; case PD_STATE_SRC_ACCEPTED: /* Accept sent, wait for enabling the new voltage */ if (pd[port].last_state != pd[port].task_state) - set_state_timeout( - port, - get_time().val + - PD_T_SINK_TRANSITION, - PD_STATE_SRC_POWERED); + set_state_timeout(port, get_time().val + + PD_T_SINK_TRANSITION, PD_STATE_SRC_POWERED); break; case PD_STATE_SRC_POWERED: /* Switch to the new requested voltage */ if (pd[port].last_state != pd[port].task_state) { pd_transition_voltage(pd[port].requested_idx); - set_state_timeout( - port, - get_time().val + - PD_POWER_SUPPLY_TURN_ON_DELAY, - PD_STATE_SRC_TRANSITION); + set_state_timeout(port, get_time().val + + PD_POWER_SUPPLY_TURN_ON_DELAY, PD_STATE_SRC_TRANSITION); } break; case PD_STATE_SRC_TRANSITION: /* the voltage output is good, notify the source */ res = send_control(port, PD_CTRL_PS_RDY); if (res >= 0) { - timeout = 10*MSEC; + timeout = 10 * MSEC; /* it'a time to ping regularly the sink */ set_state(port, PD_STATE_SRC_READY); } else { @@ -2496,22 +2412,19 @@ void pd_run_state_machine(int port) timeout = PD_T_SOURCE_ACTIVITY; /* - * Don't send any PD traffic if we woke up due to - * incoming packet or if VDO response pending to avoid - * collisions. - */ - if (incoming_packet || - (pd[port].vdm_state == VDM_STATE_BUSY)) + * Don't send any PD traffic if we woke up due to + * incoming packet or if VDO response pending to avoid + * collisions. + */ + if (incoming_packet || (pd[port].vdm_state == VDM_STATE_BUSY)) break; /* Send updated source capabilities to our partner */ if (pd[port].flags & PD_FLAGS_UPDATE_SRC_CAPS) { res = send_source_cap(port); if (res >= 0) { - set_state(port, - PD_STATE_SRC_NEGOCIATE); - pd[port].flags &= - ~PD_FLAGS_UPDATE_SRC_CAPS; + set_state(port, PD_STATE_SRC_NEGOCIATE); + pd[port].flags &= ~PD_FLAGS_UPDATE_SRC_CAPS; } break; } @@ -2523,34 +2436,32 @@ void pd_run_state_machine(int port) send_control(port, PD_CTRL_GET_SINK_CAP); set_state(port, PD_STATE_SRC_GET_SINK_CAP); break; - } else if (debug_level >= 2 && - snk_cap_count == PD_SNK_CAP_RETRIES+1) { + } else if (debug_level >= 2 + && snk_cap_count == PD_SNK_CAP_RETRIES + 1) { CPRINTF("ERR SNK_CAP\n"); } } /* Check power role policy, which may trigger a swap */ if (pd[port].flags & PD_FLAGS_CHECK_PR_ROLE) { - pd_check_pr_role(port, PD_ROLE_SOURCE, - pd[port].flags); + pd_check_pr_role(port, PD_ROLE_SOURCE, pd[port].flags); pd[port].flags &= ~PD_FLAGS_CHECK_PR_ROLE; break; } /* Check data role policy, which may trigger a swap */ if (pd[port].flags & PD_FLAGS_CHECK_DR_ROLE) { - pd_check_dr_role(port, pd[port].data_role, - pd[port].flags); + pd_check_dr_role(port, pd[port].data_role, pd[port].flags); pd[port].flags &= ~PD_FLAGS_CHECK_DR_ROLE; break; } /* Send discovery SVDMs last */ - if (pd[port].data_role == PD_ROLE_DFP && - (pd[port].flags & PD_FLAGS_CHECK_IDENTITY)) { + if (pd[port].data_role == PD_ROLE_DFP + && (pd[port].flags & PD_FLAGS_CHECK_IDENTITY)) { #ifndef CONFIG_USB_PD_SIMPLE_DFP pd_send_vdm(port, USB_SID_PD, - CMD_DISCOVER_IDENT, NULL, 0); + CMD_DISCOVER_IDENT, NULL, 0); #endif pd[port].flags &= ~PD_FLAGS_CHECK_IDENTITY; break; @@ -2570,31 +2481,27 @@ void pd_run_state_machine(int port) break; case PD_STATE_SRC_GET_SINK_CAP: if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, - get_time().val + - PD_T_SENDER_RESPONSE, - PD_STATE_SRC_READY); + set_state_timeout(port, get_time().val + + PD_T_SENDER_RESPONSE, PD_STATE_SRC_READY); break; case PD_STATE_DR_SWAP: if (pd[port].last_state != pd[port].task_state) { res = send_control(port, PD_CTRL_DR_SWAP); if (res < 0) { - timeout = 10*MSEC; + timeout = 10 * MSEC; /* - * If failed to get goodCRC, send - * soft reset, otherwise ignore - * failure. - */ - set_state(port, res == -1 ? - PD_STATE_SOFT_RESET : - READY_RETURN_STATE(port)); + * If failed to get goodCRC, send + * soft reset, otherwise ignore + * failure. + */ + set_state(port, + res == -1 ? + PD_STATE_SOFT_RESET : READY_RETURN_STATE(port)); break; } /* Wait for accept or reject */ - set_state_timeout(port, - get_time().val + - PD_T_SENDER_RESPONSE, - READY_RETURN_STATE(port)); + set_state_timeout(port, get_time().val + + PD_T_SENDER_RESPONSE, READY_RETURN_STATE(port)); } break; #ifdef CONFIG_USB_PD_DUAL_ROLE @@ -2602,40 +2509,33 @@ void pd_run_state_machine(int port) if (pd[port].last_state != pd[port].task_state) { res = send_control(port, PD_CTRL_PR_SWAP); if (res < 0) { - timeout = 10*MSEC; + timeout = 10 * MSEC; /* - * If failed to get goodCRC, send - * soft reset, otherwise ignore - * failure. - */ - set_state(port, res == -1 ? - PD_STATE_SOFT_RESET : - PD_STATE_SRC_READY); + * If failed to get goodCRC, send + * soft reset, otherwise ignore + * failure. + */ + set_state(port, + res == -1 ? PD_STATE_SOFT_RESET : PD_STATE_SRC_READY); break; } /* Wait for accept or reject */ - set_state_timeout(port, - get_time().val + - PD_T_SENDER_RESPONSE, - PD_STATE_SRC_READY); + set_state_timeout(port, get_time().val + + PD_T_SENDER_RESPONSE, PD_STATE_SRC_READY); } break; case PD_STATE_SRC_SWAP_SNK_DISABLE: /* Give time for sink to stop drawing current */ if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, - get_time().val + - PD_T_SINK_TRANSITION, - PD_STATE_SRC_SWAP_SRC_DISABLE); + set_state_timeout(port, get_time().val + + PD_T_SINK_TRANSITION, PD_STATE_SRC_SWAP_SRC_DISABLE); break; case PD_STATE_SRC_SWAP_SRC_DISABLE: /* Turn power off */ if (pd[port].last_state != pd[port].task_state) { pd_power_supply_reset(port); - set_state_timeout(port, - get_time().val + - PD_POWER_SUPPLY_TURN_OFF_DELAY, - PD_STATE_SRC_SWAP_STANDBY); + set_state_timeout(port, get_time().val + + PD_POWER_SUPPLY_TURN_OFF_DELAY, PD_STATE_SRC_SWAP_STANDBY); } break; case PD_STATE_SRC_SWAP_STANDBY: @@ -2644,19 +2544,16 @@ void pd_run_state_machine(int port) /* Send PS_RDY */ res = send_control(port, PD_CTRL_PS_RDY); if (res < 0) { - timeout = 10*MSEC; - set_state(port, - PD_STATE_SRC_DISCONNECTED); + timeout = 10 * MSEC; + set_state(port, PD_STATE_SRC_DISCONNECTED); break; } /* Switch to Rd and swap roles to sink */ tcpm_set_cc(port, TYPEC_CC_RD); pd[port].power_role = PD_ROLE_SINK; /* Wait for PS_RDY from new source */ - set_state_timeout(port, - get_time().val + - PD_T_PS_SOURCE_ON, - PD_STATE_SNK_DISCONNECTED); + set_state_timeout(port, get_time().val + + PD_T_PS_SOURCE_ON, PD_STATE_SNK_DISCONNECTED); } break; case PD_STATE_SUSPENDED: { @@ -2682,8 +2579,7 @@ void pd_run_state_machine(int port) pd_hw_init(port, PD_ROLE_DEFAULT(port)); CPRINTS("TCPC p%d resumed!", port); #else - if (rstatus != EC_ERROR_UNIMPLEMENTED && - pd_restart_tcpc(port) != 0) { + if (rstatus != EC_ERROR_UNIMPLEMENTED && pd_restart_tcpc(port) != 0) { /* stay in PD_STATE_SUSPENDED */ CPRINTS("TCPC p%d restart failed!", port); break; @@ -2698,7 +2594,7 @@ void pd_run_state_machine(int port) timeout = drp_state != PD_DRP_TOGGLE_ON ? SECOND : 10*MSEC; #else - timeout = 10*MSEC; + timeout = 10 * MSEC; #endif tcpm_get_cc(port, &cc1, &cc2); @@ -2719,25 +2615,23 @@ void pd_run_state_machine(int port) #endif /* Source connection monitoring */ - if (cc1 != TYPEC_CC_VOLT_OPEN || - cc2 != TYPEC_CC_VOLT_OPEN) { + if (cc1 != TYPEC_CC_VOLT_OPEN || cc2 != TYPEC_CC_VOLT_OPEN) { pd[port].cc_state = PD_CC_NONE; hard_reset_count = 0; new_cc_state = PD_CC_NONE; pd[port].cc_debounce = get_time().val + - PD_T_CC_DEBOUNCE; - set_state(port, - PD_STATE_SNK_DISCONNECTED_DEBOUNCE); - timeout = 10*MSEC; + PD_T_CC_DEBOUNCE; + set_state(port, PD_STATE_SNK_DISCONNECTED_DEBOUNCE); + timeout = 10 * MSEC; break; } /* - * If Try.SRC is active and failed to detect a SNK, - * then it transitions to TryWait.SNK. Need to prevent - * normal dual role toggle until tDRPTryWait timer - * expires. - */ + * If Try.SRC is active and failed to detect a SNK, + * then it transitions to TryWait.SNK. Need to prevent + * normal dual role toggle until tDRPTryWait timer + * expires. + */ if (pd[port].flags & PD_FLAGS_TRY_SRC) { if (get_time().val > pd[port].try_src_marker) pd[port].flags &= ~PD_FLAGS_TRY_SRC; @@ -2745,8 +2639,7 @@ void pd_run_state_machine(int port) } /* If no source detected, check for role toggle. */ - if (drp_state == PD_DRP_TOGGLE_ON && - get_time().val >= next_role_swap) { + if (drp_state == PD_DRP_TOGGLE_ON && get_time().val >= next_role_swap) { /* Swap roles to source */ pd[port].power_role = PD_ROLE_SOURCE; set_state(port, PD_STATE_SRC_DISCONNECTED); @@ -2754,7 +2647,7 @@ void pd_run_state_machine(int port) next_role_swap = get_time().val + PD_T_DRP_SRC; /* Swap states quickly */ - timeout = 2*MSEC; + timeout = 2 * MSEC; } break; case PD_STATE_SNK_DISCONNECTED_DEBOUNCE: @@ -2768,36 +2661,33 @@ void pd_run_state_machine(int port) } else { /* No connection any more */ set_state(port, PD_STATE_SNK_DISCONNECTED); - timeout = 5*MSEC; + timeout = 5 * MSEC; break; } - timeout = 20*MSEC; + timeout = 20 * MSEC; /* Debounce the cc state */ if (new_cc_state != pd[port].cc_state) { pd[port].cc_debounce = get_time().val + - PD_T_CC_DEBOUNCE; + PD_T_CC_DEBOUNCE; pd[port].cc_state = new_cc_state; break; } /* Wait for CC debounce and VBUS present */ - if (get_time().val < pd[port].cc_debounce || - !pd_is_vbus_present(port)) + if (get_time().val < pd[port].cc_debounce || !pd_is_vbus_present(port)) break; - if (pd_try_src_enable && - !(pd[port].flags & PD_FLAGS_TRY_SRC)) { + if (pd_try_src_enable && !(pd[port].flags & PD_FLAGS_TRY_SRC)) { /* - * If TRY_SRC is enabled, but not active, - * then force attempt to connect as source. - */ - pd[port].try_src_marker = get_time().val - + PD_T_TRY_SRC; + * If TRY_SRC is enabled, but not active, + * then force attempt to connect as source. + */ + pd[port].try_src_marker = get_time().val + PD_T_TRY_SRC; /* Swap roles to source */ pd[port].power_role = PD_ROLE_SOURCE; tcpm_set_cc(port, TYPEC_CC_RP); - timeout = 2*MSEC; + timeout = 2 * MSEC; set_state(port, PD_STATE_SRC_DISCONNECTED); /* Set flag after the state change */ pd[port].flags |= PD_FLAGS_TRY_SRC; @@ -2812,8 +2702,7 @@ void pd_run_state_machine(int port) /* initial data role for sink is UFP */ pd_set_data_role(port, PD_ROLE_UFP); #if defined(CONFIG_CHARGE_MANAGER) - typec_curr = get_typec_current_limit(pd[port].polarity, - cc1, cc2); + typec_curr = get_typec_current_limit(pd[port].polarity, cc1, cc2); //typec_set_input_current_limit( // port, typec_curr, TYPE_C_VOLTAGE); #endif @@ -2822,17 +2711,17 @@ void pd_run_state_machine(int port) tcpm_set_rx_enable(port, 1); /* DFP is attached */ - if (new_cc_state == PD_CC_DFP_ATTACHED || - new_cc_state == PD_CC_DEBUG_ACC) { + if (new_cc_state == PD_CC_DFP_ATTACHED + || new_cc_state == PD_CC_DEBUG_ACC) { pd[port].flags |= PD_FLAGS_CHECK_PR_ROLE | - PD_FLAGS_CHECK_DR_ROLE | - PD_FLAGS_CHECK_IDENTITY; + PD_FLAGS_CHECK_DR_ROLE | + PD_FLAGS_CHECK_IDENTITY; if (new_cc_state == PD_CC_DEBUG_ACC) pd[port].flags |= - PD_FLAGS_TS_DTS_PARTNER; + PD_FLAGS_TS_DTS_PARTNER; send_control(port, PD_CTRL_GET_SOURCE_CAP); set_state(port, PD_STATE_SNK_DISCOVERY); - timeout = 10*MSEC; + timeout = 10 * MSEC; //hook_call_deferred( // &pd_usb_billboard_deferred_data, // PD_T_AME); @@ -2856,27 +2745,21 @@ void pd_run_state_machine(int port) /* Wait for VBUS to go low and then high*/ if (pd[port].last_state != pd[port].task_state) { snk_hard_reset_vbus_off = 0; - set_state_timeout(port, - get_time().val + - PD_T_SAFE_0V, - hard_reset_count < - PD_HARD_RESET_COUNT ? - PD_STATE_HARD_RESET_SEND : - PD_STATE_SNK_DISCOVERY); + set_state_timeout(port, get_time().val + + PD_T_SAFE_0V, + hard_reset_count < + PD_HARD_RESET_COUNT ? + PD_STATE_HARD_RESET_SEND : PD_STATE_SNK_DISCOVERY); } - if (!pd_is_vbus_present(port) && - !snk_hard_reset_vbus_off) { + if (!pd_is_vbus_present(port) && !snk_hard_reset_vbus_off) { /* VBUS has gone low, reset timeout */ snk_hard_reset_vbus_off = 1; - set_state_timeout(port, - get_time().val + - PD_T_SRC_RECOVER_MAX + - PD_T_SRC_TURN_ON, - PD_STATE_SNK_DISCONNECTED); + set_state_timeout(port, get_time().val + + PD_T_SRC_RECOVER_MAX + + PD_T_SRC_TURN_ON, PD_STATE_SNK_DISCONNECTED); } - if (pd_is_vbus_present(port) && - snk_hard_reset_vbus_off) { + if (pd_is_vbus_present(port) && snk_hard_reset_vbus_off) { #ifdef CONFIG_USB_PD_TCPM_TCPCI /* * After transmitting hard reset, TCPM writes @@ -2889,56 +2772,49 @@ void pd_run_state_machine(int port) /* VBUS went high again */ set_state(port, PD_STATE_SNK_DISCOVERY); - timeout = 10*MSEC; + timeout = 10 * MSEC; } /* - * Don't need to set timeout because VBUS changing - * will trigger an interrupt and wake us up. - */ + * Don't need to set timeout because VBUS changing + * will trigger an interrupt and wake us up. + */ #endif break; case PD_STATE_SNK_DISCOVERY: /* Wait for source cap expired only if we are enabled */ if ((pd[port].last_state != pd[port].task_state) - && pd_comm_is_enabled(port)) { + && pd_comm_is_enabled(port)) { /* - * If VBUS has never been low, and we timeout - * waiting for source cap, try a soft reset - * first, in case we were already in a stable - * contract before this boot. - */ + * If VBUS has never been low, and we timeout + * waiting for source cap, try a soft reset + * first, in case we were already in a stable + * contract before this boot. + */ if (pd[port].flags & PD_FLAGS_VBUS_NEVER_LOW) - set_state_timeout(port, - get_time().val + - PD_T_SINK_WAIT_CAP, - PD_STATE_SOFT_RESET); + set_state_timeout(port, get_time().val + + PD_T_SINK_WAIT_CAP, PD_STATE_SOFT_RESET); /* - * If we haven't passed hard reset counter, - * start SinkWaitCapTimer, otherwise start - * NoResponseTimer. - */ + * If we haven't passed hard reset counter, + * start SinkWaitCapTimer, otherwise start + * NoResponseTimer. + */ else if (hard_reset_count < PD_HARD_RESET_COUNT) - set_state_timeout(port, - get_time().val + - PD_T_SINK_WAIT_CAP, - PD_STATE_HARD_RESET_SEND); + set_state_timeout(port, get_time().val + + PD_T_SINK_WAIT_CAP, PD_STATE_HARD_RESET_SEND); else if (pd[port].flags & - PD_FLAGS_PREVIOUS_PD_CONN) + PD_FLAGS_PREVIOUS_PD_CONN) /* ErrorRecovery */ - set_state_timeout(port, - get_time().val + - PD_T_NO_RESPONSE, - PD_STATE_SNK_DISCONNECTED); + set_state_timeout(port, get_time().val + + PD_T_NO_RESPONSE, PD_STATE_SNK_DISCONNECTED); #if defined(CONFIG_CHARGE_MANAGER) /* - * If we didn't come from disconnected, must - * have come from some path that did not set - * typec current limit. So, set to 0 so that - * we guarantee this is revised below. - */ - if (pd[port].last_state != - PD_STATE_SNK_DISCONNECTED_DEBOUNCE) + * If we didn't come from disconnected, must + * have come from some path that did not set + * typec current limit. So, set to 0 so that + * we guarantee this is revised below. + */ + if (pd[port].last_state != PD_STATE_SNK_DISCONNECTED_DEBOUNCE) typec_curr = 0; #endif } @@ -2948,13 +2824,13 @@ void pd_run_state_machine(int port) /* Check if CC pull-up has changed */ tcpm_get_cc(port, &cc1, &cc2); - if (typec_curr != get_typec_current_limit( - pd[port].polarity, cc1, cc2)) { + if (typec_curr + != get_typec_current_limit(pd[port].polarity, cc1, cc2)) { /* debounce signal by requiring two reads */ if (typec_curr_change) { /* set new input current limit */ - typec_curr = get_typec_current_limit( - pd[port].polarity, cc1, cc2); + typec_curr = get_typec_current_limit(pd[port].polarity, cc1, + cc2); //typec_set_input_current_limit( // port, typec_curr, TYPE_C_VOLTAGE); } else { @@ -2971,30 +2847,25 @@ void pd_run_state_machine(int port) /* Wait for ACCEPT or REJECT */ if (pd[port].last_state != pd[port].task_state) { hard_reset_count = 0; - set_state_timeout(port, - get_time().val + - PD_T_SENDER_RESPONSE, - PD_STATE_HARD_RESET_SEND); + set_state_timeout(port, get_time().val + + PD_T_SENDER_RESPONSE, PD_STATE_HARD_RESET_SEND); } break; case PD_STATE_SNK_TRANSITION: /* Wait for PS_RDY */ if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, - get_time().val + - PD_T_PS_TRANSITION, - PD_STATE_HARD_RESET_SEND); + set_state_timeout(port, get_time().val + + PD_T_PS_TRANSITION, PD_STATE_HARD_RESET_SEND); break; case PD_STATE_SNK_READY: - timeout = 20*MSEC; + timeout = 20 * MSEC; /* - * Don't send any PD traffic if we woke up due to - * incoming packet or if VDO response pending to avoid - * collisions. - */ - if (incoming_packet || - (pd[port].vdm_state == VDM_STATE_BUSY)) + * Don't send any PD traffic if we woke up due to + * incoming packet or if VDO response pending to avoid + * collisions. + */ + if (incoming_packet || (pd[port].vdm_state == VDM_STATE_BUSY)) break; /* Check for new power to request */ @@ -3006,52 +2877,47 @@ void pd_run_state_machine(int port) /* Check power role policy, which may trigger a swap */ if (pd[port].flags & PD_FLAGS_CHECK_PR_ROLE) { - pd_check_pr_role(port, PD_ROLE_SINK, - pd[port].flags); + pd_check_pr_role(port, PD_ROLE_SINK, pd[port].flags); pd[port].flags &= ~PD_FLAGS_CHECK_PR_ROLE; break; } /* Check data role policy, which may trigger a swap */ if (pd[port].flags & PD_FLAGS_CHECK_DR_ROLE) { - pd_check_dr_role(port, pd[port].data_role, - pd[port].flags); + pd_check_dr_role(port, pd[port].data_role, pd[port].flags); pd[port].flags &= ~PD_FLAGS_CHECK_DR_ROLE; break; } /* If DFP, send discovery SVDMs */ - if (pd[port].data_role == PD_ROLE_DFP && - (pd[port].flags & PD_FLAGS_CHECK_IDENTITY)) { + if (pd[port].data_role == PD_ROLE_DFP + && (pd[port].flags & PD_FLAGS_CHECK_IDENTITY)) { pd_send_vdm(port, USB_SID_PD, - CMD_DISCOVER_IDENT, NULL, 0); + CMD_DISCOVER_IDENT, NULL, 0); pd[port].flags &= ~PD_FLAGS_CHECK_IDENTITY; break; } /* Sent all messages, don't need to wake very often */ - timeout = 200*MSEC; + timeout = 200 * MSEC; break; case PD_STATE_SNK_SWAP_INIT: if (pd[port].last_state != pd[port].task_state) { res = send_control(port, PD_CTRL_PR_SWAP); if (res < 0) { - timeout = 10*MSEC; + timeout = 10 * MSEC; /* - * If failed to get goodCRC, send - * soft reset, otherwise ignore - * failure. - */ - set_state(port, res == -1 ? - PD_STATE_SOFT_RESET : - PD_STATE_SNK_READY); + * If failed to get goodCRC, send + * soft reset, otherwise ignore + * failure. + */ + set_state(port, + res == -1 ? PD_STATE_SOFT_RESET : PD_STATE_SNK_READY); break; } /* Wait for accept or reject */ - set_state_timeout(port, - get_time().val + - PD_T_SENDER_RESPONSE, - PD_STATE_SNK_READY); + set_state_timeout(port, get_time().val + + PD_T_SENDER_RESPONSE, PD_STATE_SNK_READY); } break; case PD_STATE_SNK_SWAP_SNK_DISABLE: @@ -3064,15 +2930,13 @@ void pd_run_state_machine(int port) // CHARGE_CEIL_NONE); #endif set_state(port, PD_STATE_SNK_SWAP_SRC_DISABLE); - timeout = 10*MSEC; + timeout = 10 * MSEC; break; case PD_STATE_SNK_SWAP_SRC_DISABLE: /* Wait for PS_RDY */ if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, - get_time().val + - PD_T_PS_SOURCE_OFF, - PD_STATE_HARD_RESET_SEND); + set_state_timeout(port, get_time().val + + PD_T_PS_SOURCE_OFF, PD_STATE_HARD_RESET_SEND); break; case PD_STATE_SNK_SWAP_STANDBY: if (pd[port].last_state != pd[port].task_state) { @@ -3081,17 +2945,13 @@ void pd_run_state_machine(int port) if (pd_set_power_supply_ready(port)) { /* Restore Rd */ tcpm_set_cc(port, TYPEC_CC_RD); - timeout = 10*MSEC; - set_state(port, - PD_STATE_SNK_DISCONNECTED); + timeout = 10 * MSEC; + set_state(port, PD_STATE_SNK_DISCONNECTED); break; } /* Wait for power supply to turn on */ - set_state_timeout( - port, - get_time().val + - PD_POWER_SUPPLY_TURN_ON_DELAY, - PD_STATE_SNK_SWAP_COMPLETE); + set_state_timeout(port, get_time().val + + PD_POWER_SUPPLY_TURN_ON_DELAY, PD_STATE_SNK_SWAP_COMPLETE); } break; case PD_STATE_SNK_SWAP_COMPLETE: @@ -3107,13 +2967,13 @@ void pd_run_state_machine(int port) } /* Don't send GET_SINK_CAP on swap */ - snk_cap_count = PD_SNK_CAP_RETRIES+1; + snk_cap_count = PD_SNK_CAP_RETRIES + 1; caps_count = 0; pd[port].msg_id = 0; pd[port].power_role = PD_ROLE_SOURCE; pd_update_roles(port); set_state(port, PD_STATE_SRC_DISCOVERY); - timeout = 10*MSEC; + timeout = 10 * MSEC; break; #ifdef CONFIG_USBC_VCONN_SWAP case PD_STATE_VCONN_SWAP_SEND: @@ -3192,16 +3052,13 @@ void pd_run_state_machine(int port) /* if soft reset failed, try hard reset. */ if (res < 0) { - set_state(port, - PD_STATE_HARD_RESET_SEND); - timeout = 5*MSEC; + set_state(port, PD_STATE_HARD_RESET_SEND); + timeout = 5 * MSEC; break; } - set_state_timeout( - port, - get_time().val + PD_T_SENDER_RESPONSE, - PD_STATE_HARD_RESET_SEND); + set_state_timeout(port, get_time().val + PD_T_SENDER_RESPONSE, + PD_STATE_HARD_RESET_SEND); } break; case PD_STATE_HARD_RESET_SEND: @@ -3209,19 +3066,19 @@ void pd_run_state_machine(int port) if (pd[port].last_state != pd[port].task_state) hard_reset_sent = 0; #ifdef CONFIG_CHARGE_MANAGER - if (pd[port].last_state == PD_STATE_SNK_DISCOVERY || - (pd[port].last_state == PD_STATE_SOFT_RESET && - (pd[port].flags & PD_FLAGS_VBUS_NEVER_LOW))) { + if (pd[port].last_state == PD_STATE_SNK_DISCOVERY + || (pd[port].last_state == PD_STATE_SOFT_RESET + && (pd[port].flags & PD_FLAGS_VBUS_NEVER_LOW))) { pd[port].flags &= ~PD_FLAGS_VBUS_NEVER_LOW; /* - * If discovery timed out, assume that we - * have a dedicated charger attached. This - * may not be a correct assumption according - * to the specification, but it generally - * works in practice and the harmful - * effects of a wrong assumption here - * are minimal. - */ + * If discovery timed out, assume that we + * have a dedicated charger attached. This + * may not be a correct assumption according + * to the specification, but it generally + * works in practice and the harmful + * effects of a wrong assumption here + * are minimal. + */ //charge_manager_update_dualrole(port, // CAP_DEDICATED); } @@ -3229,42 +3086,39 @@ void pd_run_state_machine(int port) /* try sending hard reset until it succeeds */ if (!hard_reset_sent) { - if (pd_transmit(port, TCPC_TX_HARD_RESET, - 0, NULL) < 0) { - timeout = 10*MSEC; + if (pd_transmit(port, TCPC_TX_HARD_RESET, 0, NULL) < 0) { + timeout = 10 * MSEC; break; } /* successfully sent hard reset */ hard_reset_sent = 1; /* - * If we are source, delay before cutting power - * to allow sink time to get hard reset. - */ + * If we are source, delay before cutting power + * to allow sink time to get hard reset. + */ if (pd[port].power_role == PD_ROLE_SOURCE) { - set_state_timeout(port, - get_time().val + PD_T_PS_HARD_RESET, - PD_STATE_HARD_RESET_EXECUTE); - } else { - set_state(port, + set_state_timeout(port, get_time().val + PD_T_PS_HARD_RESET, PD_STATE_HARD_RESET_EXECUTE); - timeout = 10*MSEC; + } else { + set_state(port, PD_STATE_HARD_RESET_EXECUTE); + timeout = 10 * MSEC; } } break; case PD_STATE_HARD_RESET_EXECUTE: #ifdef CONFIG_USB_PD_DUAL_ROLE /* - * If hard reset while in the last stages of power - * swap, then we need to restore our CC resistor. - */ + * If hard reset while in the last stages of power + * swap, then we need to restore our CC resistor. + */ if (pd[port].last_state == PD_STATE_SNK_SWAP_STANDBY) tcpm_set_cc(port, TYPEC_CC_RD); #endif /* reset our own state machine */ pd_execute_hard_reset(port); - timeout = 10*MSEC; + timeout = 10 * MSEC; break; #ifdef CONFIG_COMMON_RUNTIME case PD_STATE_BIST_RX: @@ -3352,15 +3206,15 @@ void pd_run_state_machine(int port) pd[port].last_state = this_state; /* - * Check for state timeout, and if not check if need to adjust - * timeout value to wake up on the next state timeout. - */ + * Check for state timeout, and if not check if need to adjust + * timeout value to wake up on the next state timeout. + */ now = get_time(); if (pd[port].timeout) { if (now.val >= pd[port].timeout) { set_state(port, pd[port].timeout_state); /* On a state timeout, run next state soon */ - timeout = timeout < 10*MSEC ? timeout : 10*MSEC; + timeout = timeout < 10 * MSEC ? timeout : 10 * MSEC; } else if (pd[port].timeout - now.val < timeout) { timeout = pd[port].timeout - now.val; } @@ -3381,23 +3235,21 @@ void pd_run_state_machine(int port) if (cc1 == TYPEC_CC_VOLT_OPEN) { set_state(port, PD_STATE_SRC_DISCONNECTED); /* Debouncing */ - timeout = 10*MSEC; + timeout = 10 * MSEC; #ifdef CONFIG_USB_PD_DUAL_ROLE /* - * If Try.SRC is configured, then ATTACHED_SRC - * needs to transition to TryWait.SNK. Change - * power role to SNK and start state timer. - */ + * If Try.SRC is configured, then ATTACHED_SRC + * needs to transition to TryWait.SNK. Change + * power role to SNK and start state timer. + */ if (pd_try_src_enable) { /* Swap roles to sink */ pd[port].power_role = PD_ROLE_SINK; tcpm_set_cc(port, TYPEC_CC_RD); /* Set timer for TryWait.SNK state */ - pd[port].try_src_marker = get_time().val - + PD_T_TRY_WAIT; + pd[port].try_src_marker = get_time().val + PD_T_TRY_WAIT; /* Advance to TryWait.SNK state */ - set_state(port, - PD_STATE_SNK_DISCONNECTED); + set_state(port, PD_STATE_SNK_DISCONNECTED); /* Mark state as TryWait.SNK */ pd[port].flags |= PD_FLAGS_TRY_SRC; } @@ -3406,55 +3258,53 @@ void pd_run_state_machine(int port) } #ifdef CONFIG_USB_PD_DUAL_ROLE /* - * Sink disconnect if VBUS is low and we are not recovering - * a hard reset. - */ - if (pd[port].power_role == PD_ROLE_SINK && - !pd_is_vbus_present(port) && - pd[port].task_state != PD_STATE_SNK_HARD_RESET_RECOVER && - pd[port].task_state != PD_STATE_HARD_RESET_EXECUTE) { + * Sink disconnect if VBUS is low and we are not recovering + * a hard reset. + */ + if (pd[port].power_role == PD_ROLE_SINK && !pd_is_vbus_present(port) + && pd[port].task_state != PD_STATE_SNK_HARD_RESET_RECOVER + && pd[port].task_state != PD_STATE_HARD_RESET_EXECUTE) { /* Sink: detect disconnect by monitoring VBUS */ set_state(port, PD_STATE_SNK_DISCONNECTED); /* set timeout small to reconnect fast */ - timeout = 5*MSEC; + timeout = 5 * MSEC; } #endif /* CONFIG_USB_PD_DUAL_ROLE */ } #ifdef CONFIG_USB_PD_DUAL_ROLE -static void dual_role_on(void) -{ - int i; - - for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; i++) { -#ifdef CONFIG_CHARGE_MANAGER - //if (charge_manager_get_active_charge_port() != i) -#endif - pd[i].flags |= PD_FLAGS_CHECK_PR_ROLE | - PD_FLAGS_CHECK_DR_ROLE; - - pd[i].flags |= PD_FLAGS_CHECK_IDENTITY; - } - - pd_set_dual_role(PD_DRP_TOGGLE_ON); - CPRINTS("chipset -> S0"); -} -DECLARE_HOOK(HOOK_CHIPSET_RESUME, dual_role_on, HOOK_PRIO_DEFAULT); - -static void dual_role_off(void) -{ - pd_set_dual_role(PD_DRP_TOGGLE_OFF); - CPRINTS("chipset -> S3"); -} -DECLARE_HOOK(HOOK_CHIPSET_SUSPEND, dual_role_off, HOOK_PRIO_DEFAULT); -DECLARE_HOOK(HOOK_CHIPSET_STARTUP, dual_role_off, HOOK_PRIO_DEFAULT); - -static void dual_role_force_sink(void) -{ - pd_set_dual_role(PD_DRP_FORCE_SINK); - CPRINTS("chipset -> S5"); -} -DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, dual_role_force_sink, HOOK_PRIO_DEFAULT); +//extern "C" { +//static void dual_role_on(void) { +// int i; +// +// for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; i++) { +//#ifdef CONFIG_CHARGE_MANAGER +// //if (charge_manager_get_active_charge_port() != i) +//#endif +// pd[i].flags |= PD_FLAGS_CHECK_PR_ROLE | +// PD_FLAGS_CHECK_DR_ROLE; +// +// pd[i].flags |= PD_FLAGS_CHECK_IDENTITY; +// } +// +// pd_set_dual_role(PD_DRP_TOGGLE_ON); +// CPRINTS("chipset -> S0"); +//} +//} +//DECLARE_HOOK(HOOK_CHIPSET_RESUME, dual_role_on, HOOK_PRIO_DEFAULT); +// +//static void dual_role_off(void) { +// pd_set_dual_role(PD_DRP_TOGGLE_OFF); +// CPRINTS("chipset -> S3"); +//} +//DECLARE_HOOK(HOOK_CHIPSET_SUSPEND, dual_role_off, HOOK_PRIO_DEFAULT); +//DECLARE_HOOK(HOOK_CHIPSET_STARTUP, dual_role_off, HOOK_PRIO_DEFAULT); +// +//static void dual_role_force_sink(void) { +// pd_set_dual_role(PD_DRP_FORCE_SINK); +// CPRINTS("chipset -> S5"); +//} +//DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, dual_role_force_sink, HOOK_PRIO_DEFAULT); #endif /* CONFIG_USB_PD_DUAL_ROLE */ diff --git a/workspace/TS100/Core/Drivers/FUSB302/tcpm_driver.cpp b/workspace/TS100/Core/Drivers/FUSB302/tcpm_driver.cpp index b32d7075..d133ebb4 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/tcpm_driver.cpp +++ b/workspace/TS100/Core/Drivers/FUSB302/tcpm_driver.cpp @@ -7,13 +7,13 @@ #include "tcpm_driver.h" #include "I2C_Wrapper.hpp" +#include "I2CBB.hpp" extern const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_COUNT]; #define STATUS_OK 0 /* I2C wrapper functions - get I2C port / slave addr from config struct. */ int tcpc_write(int port, int reg, int val) { - FRToSI2C::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) &val, - 1); + I2CBB::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) &val, 1); return STATUS_OK; } @@ -21,16 +21,14 @@ int tcpc_write16(int port, int reg, int val) { uint8_t data[2]; data[0] = (0xFF) & val; data[1] = (0xFF) & (val >> 8); - FRToSI2C::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, - 2); + I2CBB::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, 2); return STATUS_OK; } int tcpc_read(int port, int reg, int *val) { uint8_t data[1]; - FRToSI2C::Mem_Read(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, - 1); + I2CBB::Mem_Read(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, 1); *val = data[0]; @@ -39,8 +37,7 @@ int tcpc_read(int port, int reg, int *val) { int tcpc_read16(int port, int reg, int *val) { uint8_t data[2]; - FRToSI2C::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, - 2); + I2CBB::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, 2); *val = data[0]; *val |= (data[1] << 8); @@ -57,13 +54,19 @@ int tcpc_xfer(int port, const uint8_t *out, int out_size, uint8_t *in, if (flags & I2C_XFER_STOP) { //Issuing a stop between the requests //Send as a Tx followed by a Rx - FRToSI2C::Transmit(tcpc_config[port].i2c_slave_addr, (uint8_t*)out, out_size); - FRToSI2C::Receive(tcpc_config[port].i2c_slave_addr, in, in_size); + I2CBB::Transmit(tcpc_config[port].i2c_slave_addr, (uint8_t*) out, + out_size); + I2CBB::Receive(tcpc_config[port].i2c_slave_addr, in, in_size); } else { //issue as a continious transmit & recieve - FRToSI2C::TransmitReceive(tcpc_config[port].i2c_slave_addr, (uint8_t*)out, + I2CBB::TransmitReceive(tcpc_config[port].i2c_slave_addr, (uint8_t*) out, out_size, in, in_size); } return STATUS_OK; } + +uint8_t fusb302_detect() { + //Probe the I2C bus for its address + return I2CBB::probe(fusb302_I2C_SLAVE_ADDR); +} diff --git a/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.h b/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.h index 1cb14e60..1a560b1a 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.h +++ b/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.h @@ -10,7 +10,7 @@ #define USB_PD_DRIVER_H_ #include "USBC_PD/usb_pd.h" - +#include "cmsis_os.h" #include //#define CONFIG_BBRAM @@ -64,8 +64,8 @@ PDO_FIXED_COMM_CAP) #define CONFIG_USB_PD_IDENTITY_HW_VERS 1 #define CONFIG_USB_PD_IDENTITY_SW_VERS 1 -#define usleep(us) (delay_us(us)) -#define msleep(us) (delay_ms(us)) +#define usleep(us) (osDelay(1)) +#define msleep(us) (osDelay(us)) typedef union { uint64_t val; diff --git a/workspace/TS100/Core/Drivers/FUSB302/usb_pd_tcpm.h b/workspace/TS100/Core/Drivers/FUSB302/usb_pd_tcpm.h deleted file mode 100644 index 947a88c9..00000000 --- a/workspace/TS100/Core/Drivers/FUSB302/usb_pd_tcpm.h +++ /dev/null @@ -1,354 +0,0 @@ -/* Copyright 2015 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -/* USB Power delivery port management */ - -#ifndef __CROS_EC_USB_PD_TCPM_H -#define __CROS_EC_USB_PD_TCPM_H - -/* List of common error codes that can be returned */ -enum ec_error_list { - /* Success - no error */ - EC_SUCCESS = 0, - /* Unknown error */ - EC_ERROR_UNKNOWN = 1, - /* Function not implemented yet */ - EC_ERROR_UNIMPLEMENTED = 2, - /* Overflow error; too much input provided. */ - EC_ERROR_OVERFLOW = 3, - /* Timeout */ - EC_ERROR_TIMEOUT = 4, - /* Invalid argument */ - EC_ERROR_INVAL = 5, - /* Already in use, or not ready yet */ - EC_ERROR_BUSY = 6, - /* Access denied */ - EC_ERROR_ACCESS_DENIED = 7, - /* Failed because component does not have power */ - EC_ERROR_NOT_POWERED = 8, - /* Failed because component is not calibrated */ - EC_ERROR_NOT_CALIBRATED = 9, - /* Failed because CRC error */ - EC_ERROR_CRC = 10, - /* Invalid console command param (PARAMn means parameter n is bad) */ - EC_ERROR_PARAM1 = 11, - EC_ERROR_PARAM2 = 12, - EC_ERROR_PARAM3 = 13, - EC_ERROR_PARAM4 = 14, - EC_ERROR_PARAM5 = 15, - EC_ERROR_PARAM6 = 16, - EC_ERROR_PARAM7 = 17, - EC_ERROR_PARAM8 = 18, - EC_ERROR_PARAM9 = 19, - /* Wrong number of params */ - EC_ERROR_PARAM_COUNT = 20, - /* Interrupt event not handled */ - EC_ERROR_NOT_HANDLED = 21, - /* Data has not changed */ - EC_ERROR_UNCHANGED = 22, - /* Memory allocation */ - EC_ERROR_MEMORY_ALLOCATION = 23, - - /* Verified boot errors */ - EC_ERROR_VBOOT_SIGNATURE = 0x1000, /* 4096 */ - EC_ERROR_VBOOT_SIG_MAGIC = 0x1001, - EC_ERROR_VBOOT_SIG_SIZE = 0x1002, - EC_ERROR_VBOOT_SIG_ALGORITHM = 0x1003, - EC_ERROR_VBOOT_HASH_ALGORITHM = 0x1004, - EC_ERROR_VBOOT_SIG_OFFSET = 0x1005, - EC_ERROR_VBOOT_DATA_SIZE = 0x1006, - - /* Verified boot key errors */ - EC_ERROR_VBOOT_KEY = 0x1100, - EC_ERROR_VBOOT_KEY_MAGIC = 0x1101, - EC_ERROR_VBOOT_KEY_SIZE = 0x1102, - - /* Verified boot data errors */ - EC_ERROR_VBOOT_DATA = 0x1200, - EC_ERROR_VBOOT_DATA_VERIFY = 0x1201, - - /* Module-internal error codes may use this range. */ - EC_ERROR_INTERNAL_FIRST = 0x10000, - EC_ERROR_INTERNAL_LAST = 0x1FFFF -}; - -/* Flags for i2c_xfer() */ -#define I2C_XFER_START (1 << 0) /* Start smbus session from idle state */ -#define I2C_XFER_STOP (1 << 1) /* Terminate smbus session with stop bit */ -#define I2C_XFER_SINGLE (I2C_XFER_START | I2C_XFER_STOP) /* One transaction */ - -/* Default retry count for transmitting */ -#define PD_RETRY_COUNT 3 - -/* Time to wait for TCPC to complete transmit */ -#define PD_T_TCPC_TX_TIMEOUT (100*MSEC) - -enum tcpc_cc_voltage_status { - TYPEC_CC_VOLT_OPEN = 0, - TYPEC_CC_VOLT_RA = 1, - TYPEC_CC_VOLT_RD = 2, - TYPEC_CC_VOLT_SNK_DEF = 5, - TYPEC_CC_VOLT_SNK_1_5 = 6, - TYPEC_CC_VOLT_SNK_3_0 = 7, -}; - -enum tcpc_cc_pull { - TYPEC_CC_RA = 0, - TYPEC_CC_RP = 1, - TYPEC_CC_RD = 2, - TYPEC_CC_OPEN = 3, -}; - -enum tcpc_rp_value { - TYPEC_RP_USB = 0, - TYPEC_RP_1A5 = 1, - TYPEC_RP_3A0 = 2, - TYPEC_RP_RESERVED = 3, -}; - -enum tcpm_transmit_type { - TCPC_TX_SOP = 0, - TCPC_TX_SOP_PRIME = 1, - TCPC_TX_SOP_PRIME_PRIME = 2, - TCPC_TX_SOP_DEBUG_PRIME = 3, - TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4, - TCPC_TX_HARD_RESET = 5, - TCPC_TX_CABLE_RESET = 6, - TCPC_TX_BIST_MODE_2 = 7 -}; - -enum tcpc_transmit_complete { - TCPC_TX_COMPLETE_SUCCESS = 0, - TCPC_TX_COMPLETE_DISCARDED = 1, - TCPC_TX_COMPLETE_FAILED = 2, -}; - -struct tcpm_drv { - /** - * Initialize TCPM driver and wait for TCPC readiness. - * - * @param port Type-C port number - * - * @return EC_SUCCESS or error - */ - int (*init)(int port); - - /** - * Release the TCPM hardware and disconnect the driver. - * Only .init() can be called after .release(). - * - * @param port Type-C port number - * - * @return EC_SUCCESS or error - */ - int (*release)(int port); - - /** - * Read the CC line status. - * - * @param port Type-C port number - * @param cc1 pointer to CC status for CC1 - * @param cc2 pointer to CC status for CC2 - * - * @return EC_SUCCESS or error - */ - int (*get_cc)(int port, int *cc1, int *cc2); - - /** - * Read VBUS - * - * @param port Type-C port number - * - * @return 0 => VBUS not detected, 1 => VBUS detected - */ - int (*get_vbus_level)(int port); - - /** - * Set the value of the CC pull-up used when we are a source. - * - * @param port Type-C port number - * @param rp One of enum tcpc_rp_value - * - * @return EC_SUCCESS or error - */ - int (*select_rp_value)(int port, int rp); - - /** - * Set the CC pull resistor. This sets our role as either source or sink. - * - * @param port Type-C port number - * @param pull One of enum tcpc_cc_pull - * - * @return EC_SUCCESS or error - */ - int (*set_cc)(int port, int pull); - - /** - * Set polarity - * - * @param port Type-C port number - * @param polarity 0=> transmit on CC1, 1=> transmit on CC2 - * - * @return EC_SUCCESS or error - */ - int (*set_polarity)(int port, int polarity); - - /** - * Set Vconn. - * - * @param port Type-C port number - * @param polarity Polarity of the CC line to read - * - * @return EC_SUCCESS or error - */ - int (*set_vconn)(int port, int enable); - - /** - * Set PD message header to use for goodCRC - * - * @param port Type-C port number - * @param power_role Power role to use in header - * @param data_role Data role to use in header - * - * @return EC_SUCCESS or error - */ - int (*set_msg_header)(int port, int power_role, int data_role); - - /** - * Set RX enable flag - * - * @param port Type-C port number - * @enable true for enable, false for disable - * - * @return EC_SUCCESS or error - */ - int (*set_rx_enable)(int port, int enable); - - /** - * Read last received PD message. - * - * @param port Type-C port number - * @param payload Pointer to location to copy payload of message - * @param header of message - * - * @return EC_SUCCESS or error - */ - int (*get_message)(int port, uint32_t *payload, int *head); - - /** - * Transmit PD message - * - * @param port Type-C port number - * @param type Transmit type - * @param header Packet header - * @param cnt Number of bytes in payload - * @param data Payload - * - * @return EC_SUCCESS or error - */ - int (*transmit)(int port, enum tcpm_transmit_type type, uint16_t header, - const uint32_t *data); - - /** - * TCPC is asserting alert - * - * @param port Type-C port number - */ - void (*tcpc_alert)(int port); - - /** - * Discharge PD VBUS on src/sink disconnect & power role swap - * - * @param port Type-C port number - * @param enable Discharge enable or disable - */ - void (*tcpc_discharge_vbus)(int port, int enable); - -#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE - /** - * Enable TCPC auto DRP toggling. - * - * @param port Type-C port number - * @param enable 1: Enable 0: Disable - * - * @return EC_SUCCESS or error - */ - int (*drp_toggle)(int port, int enable); -#endif - - /** - * Get firmware version. - * - * @param port Type-C port number - * @param renew Force renewal - * @param info Pointer to pointer to PD chip info - * - * @return EC_SUCCESS or error - */ - int (*get_chip_info)(int port, int renew, - struct ec_response_pd_chip_info **info); -}; - -enum tcpc_alert_polarity { - TCPC_ALERT_ACTIVE_LOW, - TCPC_ALERT_ACTIVE_HIGH, -}; - -struct tcpc_config_t { - int i2c_host_port; - int i2c_slave_addr; - const struct tcpm_drv *drv; - enum tcpc_alert_polarity pol; -}; - -/** - * Returns the PD_STATUS_TCPC_ALERT_* mask corresponding to the TCPC ports - * that are currently asserting ALERT. - * - * @return PD_STATUS_TCPC_ALERT_* mask. - */ -uint16_t tcpc_get_alert_status(void); - -/** - * Optional, set the TCPC power mode. - * - * @param port Type-C port number - * @param mode 0: off/sleep, 1: on/awake - */ -void board_set_tcpc_power_mode(int port, int mode) __attribute__((weak)); - -/** - * Initialize TCPC. - * - * @param port Type-C port number - */ -void tcpc_init(int port); - -/** - * TCPC is asserting alert - * - * @param port Type-C port number - */ -void tcpc_alert_clear(int port); - -/** - * Run TCPC task once. This checks for incoming messages, processes - * any outgoing messages, and reads CC lines. - * - * @param port Type-C port number - * @param evt Event type that woke up this task - */ -int tcpc_run(int port, int evt); - -/** - * Initialize board specific TCPC functions post TCPC initialization. - * - * @param port Type-C port number - * - * @return EC_SUCCESS or error - */ -int board_tcpc_post_init(int port) __attribute__((weak)); - -#endif /* __CROS_EC_USB_PD_TCPM_H */ diff --git a/workspace/TS100/Core/Inc/main.hpp b/workspace/TS100/Core/Inc/main.hpp index 606d920b..edfde783 100644 --- a/workspace/TS100/Core/Inc/main.hpp +++ b/workspace/TS100/Core/Inc/main.hpp @@ -6,7 +6,7 @@ extern uint8_t PCBVersion; extern uint32_t currentTempTargetDegC; extern bool settingsWereReset; - +extern bool usb_pd_available; #ifdef __cplusplus extern "C" { #endif diff --git a/workspace/TS100/Core/Src/main.cpp b/workspace/TS100/Core/Src/main.cpp index 3141a5d2..e8258523 100644 --- a/workspace/TS100/Core/Src/main.cpp +++ b/workspace/TS100/Core/Src/main.cpp @@ -13,7 +13,7 @@ #include "cmsis_os.h" uint8_t PCBVersion = 0; // File local variables - +bool usb_pd_available = false; bool settingsWereReset = false; // FreeRTOS variables @@ -57,7 +57,8 @@ int main(void) { systemSettings.ShutdownTime = 0; // No accel -> disable sleep systemSettings.sensitivity = 0; } - + resetWatchdog(); + usb_pd_available = usb_pd_detect(); resetWatchdog(); /* Create the thread(s) */ diff --git a/workspace/TS100/Core/Threads/GUIThread.cpp b/workspace/TS100/Core/Threads/GUIThread.cpp index 13e94ea0..5ed33dc1 100644 --- a/workspace/TS100/Core/Threads/GUIThread.cpp +++ b/workspace/TS100/Core/Threads/GUIThread.cpp @@ -22,7 +22,7 @@ extern "C" { #include "stdlib.h" #include "string.h" #include "unit.h" - +#include "I2CBB.hpp" // File local variables extern uint32_t currentTempTargetDegC; extern uint8_t accelInit; @@ -36,720 +36,781 @@ extern osThreadId PIDTaskHandle; #define BUTTON_INACTIVITY_TIME (60 * configTICK_RATE_HZ) static uint16_t min(uint16_t a, uint16_t b) { - if (a > b) - return b; - else - return a; + if (a > b) + return b; + else + return a; } void printVoltage() { - uint32_t volt = getInputVoltageX10(systemSettings.voltageDiv, 0); - OLED::printNumber(volt / 10, 2); - OLED::print(SymbolDot); - OLED::printNumber(volt % 10, 1); + uint32_t volt = getInputVoltageX10(systemSettings.voltageDiv, 0); + OLED::printNumber(volt / 10, 2); + OLED::print(SymbolDot); + OLED::printNumber(volt % 10, 1); } void GUIDelay() { - // Called in all UI looping tasks, - // This limits the re-draw rate to the LCD and also lets the DMA run - // As the gui task can very easily fill this bus with transactions, which will - // prevent the movement detection from running - osDelay(50); + // Called in all UI looping tasks, + // This limits the re-draw rate to the LCD and also lets the DMA run + // As the gui task can very easily fill this bus with transactions, which will + // prevent the movement detection from running + osDelay(50); } void gui_drawTipTemp(bool symbol) { - // Draw tip temp handling unit conversion & tolerance near setpoint - uint16_t Temp = 0; + // Draw tip temp handling unit conversion & tolerance near setpoint + uint16_t Temp = 0; #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) Temp = TipThermoModel::getTipInF(); else #endif - Temp = TipThermoModel::getTipInC(); + Temp = TipThermoModel::getTipInC(); - OLED::printNumber(Temp, 3); // Draw the tip temp out finally - if (symbol) { - if (OLED::getFont() == 0) { - // Big font, can draw nice symbols + OLED::printNumber(Temp, 3); // Draw the tip temp out finally + if (symbol) { + if (OLED::getFont() == 0) { + // Big font, can draw nice symbols #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) OLED::drawSymbol(0); else #endif - OLED::drawSymbol(1); - } else { - // Otherwise fall back to chars + OLED::drawSymbol(1); + } else { + // Otherwise fall back to chars #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) OLED::print(SymbolDegF); else #endif - OLED::print(SymbolDegC); - } - } + OLED::print(SymbolDegC); + } + } } #ifdef MODEL_TS100 // returns true if undervoltage has occured static bool checkVoltageForExit() { - uint16_t v = getInputVoltageX10(systemSettings.voltageDiv, 0); + uint16_t v = getInputVoltageX10(systemSettings.voltageDiv, 0); - // Dont check for first 1.5 seconds while the ADC stabilizes and the DMA fills the buffer - if (xTaskGetTickCount() > 150) { - if ((v < lookupVoltageLevel(systemSettings.cutoutSetting))) { - GUIDelay(); - OLED::clearScreen(); - OLED::setCursor(0, 0); - if (systemSettings.detailedSoldering) { - OLED::setFont(1); - OLED::print(UndervoltageString); - OLED::setCursor(0, 8); - OLED::print(InputVoltageString); - printVoltage(); - OLED::print(SymbolVolts); + // Dont check for first 1.5 seconds while the ADC stabilizes and the DMA fills the buffer + if (xTaskGetTickCount() > 150) { + if ((v < lookupVoltageLevel(systemSettings.cutoutSetting))) { + GUIDelay(); + OLED::clearScreen(); + OLED::setCursor(0, 0); + if (systemSettings.detailedSoldering) { + OLED::setFont(1); + OLED::print(UndervoltageString); + OLED::setCursor(0, 8); + OLED::print(InputVoltageString); + printVoltage(); + OLED::print(SymbolVolts); - } else { - OLED::setFont(0); - OLED::print(UVLOWarningString); - } + } else { + OLED::setFont(0); + OLED::print(UVLOWarningString); + } - OLED::refresh(); - currentTempTargetDegC = 0; - waitForButtonPress(); - return true; - } - } - return false; + OLED::refresh(); + currentTempTargetDegC = 0; + waitForButtonPress(); + return true; + } + } + return false; } #endif static void gui_drawBatteryIcon() { #ifdef MODEL_TS100 - if (systemSettings.cutoutSetting) { - // User is on a lithium battery - // we need to calculate which of the 10 levels they are on - uint8_t cellCount = systemSettings.cutoutSetting + 2; - uint32_t cellV = getInputVoltageX10(systemSettings.voltageDiv, 0) / cellCount; - // Should give us approx cell voltage X10 - // Range is 42 -> 33 = 9 steps therefore we will use battery 1-10 - if (cellV < 33) cellV = 33; - cellV -= 33; // Should leave us a number of 0-9 - if (cellV > 9) cellV = 9; - OLED::drawBattery(cellV + 1); - } else - OLED::drawSymbol(15); // Draw the DC Logo + if (systemSettings.cutoutSetting) { + // User is on a lithium battery + // we need to calculate which of the 10 levels they are on + uint8_t cellCount = systemSettings.cutoutSetting + 2; + uint32_t cellV = getInputVoltageX10(systemSettings.voltageDiv, 0) + / cellCount; + // Should give us approx cell voltage X10 + // Range is 42 -> 33 = 9 steps therefore we will use battery 1-10 + if (cellV < 33) + cellV = 33; + cellV -= 33; // Should leave us a number of 0-9 + if (cellV > 9) + cellV = 9; + OLED::drawBattery(cellV + 1); + } else + OLED::drawSymbol(15); // Draw the DC Logo #else - // On TS80 we replace this symbol with the voltage we are operating on - // If <9V then show single digit, if not show duals - uint8_t V = getInputVoltageX10(systemSettings.voltageDiv, 0); - if (V % 10 >= 5) - V = V / 10 + 1; // round up - else - V = V / 10; - if (V >= 10) { - int16_t xPos = OLED::getCursorX(); - OLED::setFont(1); - OLED::printNumber(1, 1); - OLED::setCursor(xPos, 8); - OLED::printNumber(V % 10, 1); - OLED::setFont(0); - OLED::setCursor(xPos + 12, 0); // need to reset this as if we drew a wide char - } else { - OLED::printNumber(V, 1); - } + // On TS80 we replace this symbol with the voltage we are operating on + // If <9V then show single digit, if not show duals + uint8_t V = getInputVoltageX10(systemSettings.voltageDiv, 0); + if (V % 10 >= 5) + V = V / 10 + 1; // round up + else + V = V / 10; + if (V >= 10) { + int16_t xPos = OLED::getCursorX(); + OLED::setFont(1); + OLED::printNumber(1, 1); + OLED::setCursor(xPos, 8); + OLED::printNumber(V % 10, 1); + OLED::setFont(0); + OLED::setCursor(xPos + 12, 0); // need to reset this as if we drew a wide char + } else { + OLED::printNumber(V, 1); + } #endif } static void gui_solderingTempAdjust() { - uint32_t lastChange = xTaskGetTickCount(); - currentTempTargetDegC = 0; - uint32_t autoRepeatTimer = 0; - uint8_t autoRepeatAcceleration = 0; - for (;;) { - OLED::setCursor(0, 0); - OLED::clearScreen(); - OLED::setFont(0); - ButtonState buttons = getButtonState(); - if (buttons) lastChange = xTaskGetTickCount(); - switch (buttons) { - case BUTTON_NONE: - // stay - break; - case BUTTON_BOTH: - // exit - return; - break; - case BUTTON_B_LONG: - if (xTaskGetTickCount() - autoRepeatTimer + autoRepeatAcceleration > PRESS_ACCEL_INTERVAL_MAX) { - if (systemSettings.ReverseButtonTempChangeEnabled) { - systemSettings.SolderingTemp += systemSettings.TempChangeLongStep; - } else - systemSettings.SolderingTemp -= systemSettings.TempChangeLongStep; + uint32_t lastChange = xTaskGetTickCount(); + currentTempTargetDegC = 0; + uint32_t autoRepeatTimer = 0; + uint8_t autoRepeatAcceleration = 0; + for (;;) { + OLED::setCursor(0, 0); + OLED::clearScreen(); + OLED::setFont(0); + ButtonState buttons = getButtonState(); + if (buttons) + lastChange = xTaskGetTickCount(); + switch (buttons) { + case BUTTON_NONE: + // stay + break; + case BUTTON_BOTH: + // exit + return; + break; + case BUTTON_B_LONG: + if (xTaskGetTickCount() - autoRepeatTimer + + autoRepeatAcceleration> PRESS_ACCEL_INTERVAL_MAX) { + if (systemSettings.ReverseButtonTempChangeEnabled) { + systemSettings.SolderingTemp += + systemSettings.TempChangeLongStep; + } else + systemSettings.SolderingTemp -= + systemSettings.TempChangeLongStep; - autoRepeatTimer = xTaskGetTickCount(); - autoRepeatAcceleration += PRESS_ACCEL_STEP; - } - break; - case BUTTON_B_SHORT: - if (systemSettings.ReverseButtonTempChangeEnabled) { - systemSettings.SolderingTemp += systemSettings.TempChangeShortStep; - } else - systemSettings.SolderingTemp -= systemSettings.TempChangeShortStep; - break; - case BUTTON_F_LONG: - if (xTaskGetTickCount() - autoRepeatTimer + autoRepeatAcceleration > PRESS_ACCEL_INTERVAL_MAX) { - if (systemSettings.ReverseButtonTempChangeEnabled) { - systemSettings.SolderingTemp -= systemSettings.TempChangeLongStep; - } else - systemSettings.SolderingTemp += systemSettings.TempChangeLongStep; - autoRepeatTimer = xTaskGetTickCount(); - autoRepeatAcceleration += PRESS_ACCEL_STEP; - } - break; - case BUTTON_F_SHORT: - if (systemSettings.ReverseButtonTempChangeEnabled) { - systemSettings.SolderingTemp -= systemSettings.TempChangeShortStep; // add 10 - } else - systemSettings.SolderingTemp += systemSettings.TempChangeShortStep; // add 10 - break; - default: - break; - } - if ((PRESS_ACCEL_INTERVAL_MAX - autoRepeatAcceleration) < PRESS_ACCEL_INTERVAL_MIN) { - autoRepeatAcceleration = PRESS_ACCEL_INTERVAL_MAX - PRESS_ACCEL_INTERVAL_MIN; - } - // constrain between 10-450 C + autoRepeatTimer = xTaskGetTickCount(); + autoRepeatAcceleration += PRESS_ACCEL_STEP; + } + break; + case BUTTON_B_SHORT: + if (systemSettings.ReverseButtonTempChangeEnabled) { + systemSettings.SolderingTemp += + systemSettings.TempChangeShortStep; + } else + systemSettings.SolderingTemp -= + systemSettings.TempChangeShortStep; + break; + case BUTTON_F_LONG: + if (xTaskGetTickCount() - autoRepeatTimer + + autoRepeatAcceleration> PRESS_ACCEL_INTERVAL_MAX) { + if (systemSettings.ReverseButtonTempChangeEnabled) { + systemSettings.SolderingTemp -= + systemSettings.TempChangeLongStep; + } else + systemSettings.SolderingTemp += + systemSettings.TempChangeLongStep; + autoRepeatTimer = xTaskGetTickCount(); + autoRepeatAcceleration += PRESS_ACCEL_STEP; + } + break; + case BUTTON_F_SHORT: + if (systemSettings.ReverseButtonTempChangeEnabled) { + systemSettings.SolderingTemp -= + systemSettings.TempChangeShortStep; // add 10 + } else + systemSettings.SolderingTemp += + systemSettings.TempChangeShortStep; // add 10 + break; + default: + break; + } + if ((PRESS_ACCEL_INTERVAL_MAX - autoRepeatAcceleration) + < PRESS_ACCEL_INTERVAL_MIN) { + autoRepeatAcceleration = PRESS_ACCEL_INTERVAL_MAX + - PRESS_ACCEL_INTERVAL_MIN; + } + // constrain between 10-450 C #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) { if (systemSettings.SolderingTemp > 850) systemSettings.SolderingTemp = 850; if (systemSettings.SolderingTemp < 60) systemSettings.SolderingTemp = 60; } else #endif - { - if (systemSettings.SolderingTemp > 450) systemSettings.SolderingTemp = 450; - if (systemSettings.SolderingTemp < 10) systemSettings.SolderingTemp = 10; - } + { + if (systemSettings.SolderingTemp > 450) + systemSettings.SolderingTemp = 450; + if (systemSettings.SolderingTemp < 10) + systemSettings.SolderingTemp = 10; + } - if (xTaskGetTickCount() - lastChange > 200) return; // exit if user just doesn't press anything for a bit + if (xTaskGetTickCount() - lastChange > 200) + return; // exit if user just doesn't press anything for a bit #ifdef MODEL_TS80 - if (!OLED::getRotation()) { + if (!OLED::getRotation()) { #else - if (OLED::getRotation()) { + if (OLED::getRotation()) { #endif - OLED::print(systemSettings.ReverseButtonTempChangeEnabled ? SymbolPlus : SymbolMinus); - } else { - OLED::print(systemSettings.ReverseButtonTempChangeEnabled ? SymbolMinus : SymbolPlus); - } + OLED::print( + systemSettings.ReverseButtonTempChangeEnabled ? + SymbolPlus : SymbolMinus); + } else { + OLED::print( + systemSettings.ReverseButtonTempChangeEnabled ? + SymbolMinus : SymbolPlus); + } - OLED::print(SymbolSpace); - OLED::printNumber(systemSettings.SolderingTemp, 3); + OLED::print(SymbolSpace); + OLED::printNumber(systemSettings.SolderingTemp, 3); #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) OLED::drawSymbol(0); else #endif - { - OLED::drawSymbol(1); - } - OLED::print(SymbolSpace); + { + OLED::drawSymbol(1); + } + OLED::print(SymbolSpace); #ifdef MODEL_TS80 - if (!OLED::getRotation()) { + if (!OLED::getRotation()) { #else - if (OLED::getRotation()) { + if (OLED::getRotation()) { #endif - OLED::print(systemSettings.ReverseButtonTempChangeEnabled ? SymbolMinus : SymbolPlus); - } else { - OLED::print(systemSettings.ReverseButtonTempChangeEnabled ? SymbolPlus : SymbolMinus); - } - OLED::refresh(); - GUIDelay(); - } + OLED::print( + systemSettings.ReverseButtonTempChangeEnabled ? + SymbolMinus : SymbolPlus); + } else { + OLED::print( + systemSettings.ReverseButtonTempChangeEnabled ? + SymbolPlus : SymbolMinus); + } + OLED::refresh(); + GUIDelay(); + } } static int gui_SolderingSleepingMode(bool stayOff) { - // Drop to sleep temperature and display until movement or button press + // Drop to sleep temperature and display until movement or button press - for (;;) { - ButtonState buttons = getButtonState(); - if (buttons) return 0; - if ((xTaskGetTickCount() > 100) && ((accelInit && (xTaskGetTickCount() - lastMovementTime < 100)) || (xTaskGetTickCount() - lastButtonTime < 100))) return 0; // user moved or pressed a button, go back to soldering + for (;;) { + ButtonState buttons = getButtonState(); + if (buttons) + return 0; + if ((xTaskGetTickCount() > 100) + && ((accelInit && (xTaskGetTickCount() - lastMovementTime < 100)) + || (xTaskGetTickCount() - lastButtonTime < 100))) + return 0; // user moved or pressed a button, go back to soldering #ifdef MODEL_TS100 - if (checkVoltageForExit()) return 1; // return non-zero on error + if (checkVoltageForExit()) + return 1; // return non-zero on error #endif #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) { currentTempTargetDegC = stayOff ? 0 : TipThermoModel::convertFtoC(min(systemSettings.SleepTemp, systemSettings.SolderingTemp)); } else #endif - { - currentTempTargetDegC = stayOff ? 0 : min(systemSettings.SleepTemp, systemSettings.SolderingTemp); - } - // draw the lcd - uint16_t tipTemp; + { + currentTempTargetDegC = + stayOff ? + 0 : + min(systemSettings.SleepTemp, + systemSettings.SolderingTemp); + } + // draw the lcd + uint16_t tipTemp; #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) tipTemp = TipThermoModel::getTipInF(); else #endif - { - tipTemp = TipThermoModel::getTipInC(); - } + { + tipTemp = TipThermoModel::getTipInC(); + } - OLED::clearScreen(); - OLED::setCursor(0, 0); - if (systemSettings.detailedSoldering) { - OLED::setFont(1); - OLED::print(SleepingAdvancedString); - OLED::setCursor(0, 8); - OLED::print(SleepingTipAdvancedString); - OLED::printNumber(tipTemp, 3); + OLED::clearScreen(); + OLED::setCursor(0, 0); + if (systemSettings.detailedSoldering) { + OLED::setFont(1); + OLED::print(SleepingAdvancedString); + OLED::setCursor(0, 8); + OLED::print(SleepingTipAdvancedString); + OLED::printNumber(tipTemp, 3); #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) OLED::print(SymbolDegF); else #endif - { - OLED::print(SymbolDegC); - } + { + OLED::print(SymbolDegC); + } - OLED::print(SymbolSpace); - printVoltage(); - OLED::print(SymbolVolts); - } else { - OLED::setFont(0); - OLED::print(SleepingSimpleString); - OLED::printNumber(tipTemp, 3); + OLED::print(SymbolSpace); + printVoltage(); + OLED::print(SymbolVolts); + } else { + OLED::setFont(0); + OLED::print(SleepingSimpleString); + OLED::printNumber(tipTemp, 3); #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) OLED::drawSymbol(0); else #endif - { - OLED::drawSymbol(1); - } - } - if (systemSettings.ShutdownTime) // only allow shutdown exit if time > 0 - if (lastMovementTime) - if (((uint32_t)(xTaskGetTickCount() - lastMovementTime)) > (uint32_t)(systemSettings.ShutdownTime * 60 * 100)) { - // shutdown - currentTempTargetDegC = 0; - return 1; // we want to exit soldering mode - } - OLED::refresh(); - GUIDelay(); - } - return 0; + { + OLED::drawSymbol(1); + } + } + if (systemSettings.ShutdownTime) // only allow shutdown exit if time > 0 + if (lastMovementTime) + if (((uint32_t) (xTaskGetTickCount() - lastMovementTime)) + > (uint32_t) (systemSettings.ShutdownTime * 60 * 100)) { + // shutdown + currentTempTargetDegC = 0; + return 1; // we want to exit soldering mode + } + OLED::refresh(); + GUIDelay(); + } + return 0; } static void display_countdown(int sleepThres) { - /* - * Print seconds or minutes (if > 99 seconds) until sleep - * mode is triggered. - */ - int lastEventTime = lastButtonTime < lastMovementTime ? lastMovementTime : lastButtonTime; - int downCount = sleepThres - xTaskGetTickCount() + lastEventTime; - if (downCount > 9900) { - OLED::printNumber(downCount / 6000 + 1, 2); - OLED::print(SymbolMinutes); - } else { - OLED::printNumber(downCount / 100 + 1, 2); - OLED::print(SymbolSeconds); - } + /* + * Print seconds or minutes (if > 99 seconds) until sleep + * mode is triggered. + */ + int lastEventTime = + lastButtonTime < lastMovementTime ? + lastMovementTime : lastButtonTime; + int downCount = sleepThres - xTaskGetTickCount() + lastEventTime; + if (downCount > 9900) { + OLED::printNumber(downCount / 6000 + 1, 2); + OLED::print(SymbolMinutes); + } else { + OLED::printNumber(downCount / 100 + 1, 2); + OLED::print(SymbolSeconds); + } } static void gui_solderingMode(uint8_t jumpToSleep) { - /* - * * Soldering (gui_solderingMode) - * -> Main loop where we draw temp, and animations - * --> User presses buttons and they goto the temperature adjust screen - * ---> Display the current setpoint temperature - * ---> Use buttons to change forward and back on temperature - * ---> Both buttons or timeout for exiting - * --> Long hold front button to enter boost mode - * ---> Just temporarily sets the system into the alternate temperature for - * PID control - * --> Long hold back button to exit - * --> Double button to exit - */ - bool boostModeOn = false; + /* + * * Soldering (gui_solderingMode) + * -> Main loop where we draw temp, and animations + * --> User presses buttons and they goto the temperature adjust screen + * ---> Display the current setpoint temperature + * ---> Use buttons to change forward and back on temperature + * ---> Both buttons or timeout for exiting + * --> Long hold front button to enter boost mode + * ---> Just temporarily sets the system into the alternate temperature for + * PID control + * --> Long hold back button to exit + * --> Double button to exit + */ + bool boostModeOn = false; - uint32_t sleepThres = 0; - if (systemSettings.SleepTime < 6) - sleepThres = systemSettings.SleepTime * 10 * 100; - else - sleepThres = (systemSettings.SleepTime - 5) * 60 * 100; - if (jumpToSleep) { - if (gui_SolderingSleepingMode(jumpToSleep == 2)) { - lastButtonTime = xTaskGetTickCount(); - return; // If the function returns non-0 then exit - } - } - for (;;) { - ButtonState buttons = getButtonState(); - switch (buttons) { - case BUTTON_NONE: - // stay - boostModeOn = false; - break; - case BUTTON_BOTH: - // exit - return; - break; - case BUTTON_B_LONG: - return; // exit on back long hold - break; - case BUTTON_F_LONG: - // if boost mode is enabled turn it on - if (systemSettings.boostModeEnabled) boostModeOn = true; - break; - case BUTTON_F_SHORT: - case BUTTON_B_SHORT: { - uint16_t oldTemp = systemSettings.SolderingTemp; - gui_solderingTempAdjust(); // goto adjust temp mode - if (oldTemp != systemSettings.SolderingTemp) { - saveSettings(); // only save on change - } - } break; - default: - break; - } - // else we update the screen information - OLED::setCursor(0, 0); - OLED::clearScreen(); - OLED::setFont(0); - // Draw in the screen details - if (systemSettings.detailedSoldering) { - OLED::setFont(1); - OLED::print(SolderingAdvancedPowerPrompt); // Power: - OLED::printNumber(x10WattHistory.average() / 10, 2); - OLED::print(SymbolDot); - OLED::printNumber(x10WattHistory.average() % 10, 1); - OLED::print(SymbolWatts); + uint32_t sleepThres = 0; + if (systemSettings.SleepTime < 6) + sleepThres = systemSettings.SleepTime * 10 * 100; + else + sleepThres = (systemSettings.SleepTime - 5) * 60 * 100; + if (jumpToSleep) { + if (gui_SolderingSleepingMode(jumpToSleep == 2)) { + lastButtonTime = xTaskGetTickCount(); + return; // If the function returns non-0 then exit + } + } + for (;;) { + ButtonState buttons = getButtonState(); + switch (buttons) { + case BUTTON_NONE: + // stay + boostModeOn = false; + break; + case BUTTON_BOTH: + // exit + return; + break; + case BUTTON_B_LONG: + return; // exit on back long hold + break; + case BUTTON_F_LONG: + // if boost mode is enabled turn it on + if (systemSettings.boostModeEnabled) + boostModeOn = true; + break; + case BUTTON_F_SHORT: + case BUTTON_B_SHORT: { + uint16_t oldTemp = systemSettings.SolderingTemp; + gui_solderingTempAdjust(); // goto adjust temp mode + if (oldTemp != systemSettings.SolderingTemp) { + saveSettings(); // only save on change + } + } + break; + default: + break; + } + // else we update the screen information + OLED::setCursor(0, 0); + OLED::clearScreen(); + OLED::setFont(0); + // Draw in the screen details + if (systemSettings.detailedSoldering) { + OLED::setFont(1); + OLED::print(SolderingAdvancedPowerPrompt); // Power: + OLED::printNumber(x10WattHistory.average() / 10, 2); + OLED::print(SymbolDot); + OLED::printNumber(x10WattHistory.average() % 10, 1); + OLED::print(SymbolWatts); - if (systemSettings.sensitivity && systemSettings.SleepTime) { - OLED::print(SymbolSpace); - display_countdown(sleepThres); - } + if (systemSettings.sensitivity && systemSettings.SleepTime) { + OLED::print(SymbolSpace); + display_countdown(sleepThres); + } - OLED::setCursor(0, 8); - OLED::print(SleepingTipAdvancedString); - // OLED::printNumber( - // TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0)), 5); // Draw the tip temp out finally + OLED::setCursor(0, 8); + OLED::print(SleepingTipAdvancedString); + // OLED::printNumber( + // TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0)), 5); // Draw the tip temp out finally - gui_drawTipTemp(true); - OLED::print(SymbolSpace); - printVoltage(); - OLED::print(SymbolVolts); - } else { - // We switch the layout direction depending on the orientation of the - // OLED:: - if (OLED::getRotation()) { - // battery - gui_drawBatteryIcon(); - OLED::print(SymbolSpace); // Space out gap between battery <-> temp - gui_drawTipTemp(true); // Draw current tip temp + gui_drawTipTemp(true); + OLED::print(SymbolSpace); + printVoltage(); + OLED::print(SymbolVolts); + } else { + // We switch the layout direction depending on the orientation of the + // OLED:: + if (OLED::getRotation()) { + // battery + gui_drawBatteryIcon(); + OLED::print(SymbolSpace); // Space out gap between battery <-> temp + gui_drawTipTemp(true); // Draw current tip temp - // We draw boost arrow if boosting, or else gap temp <-> heat - // indicator - if (boostModeOn) - OLED::drawSymbol(2); - else - OLED::print(SymbolSpace); + // We draw boost arrow if boosting, or else gap temp <-> heat + // indicator + if (boostModeOn) + OLED::drawSymbol(2); + else + OLED::print(SymbolSpace); - // Draw heating/cooling symbols - OLED::drawHeatSymbol(X10WattsToPWM(x10WattHistory.average())); - } else { - // Draw heating/cooling symbols - OLED::drawHeatSymbol(X10WattsToPWM(x10WattHistory.average())); - // We draw boost arrow if boosting, or else gap temp <-> heat - // indicator - if (boostModeOn) - OLED::drawSymbol(2); - else - OLED::print(SymbolSpace); - gui_drawTipTemp(true); // Draw current tip temp + // Draw heating/cooling symbols + OLED::drawHeatSymbol(X10WattsToPWM(x10WattHistory.average())); + } else { + // Draw heating/cooling symbols + OLED::drawHeatSymbol(X10WattsToPWM(x10WattHistory.average())); + // We draw boost arrow if boosting, or else gap temp <-> heat + // indicator + if (boostModeOn) + OLED::drawSymbol(2); + else + OLED::print(SymbolSpace); + gui_drawTipTemp(true); // Draw current tip temp - OLED::print(SymbolSpace); // Space out gap between battery <-> temp + OLED::print(SymbolSpace); // Space out gap between battery <-> temp - gui_drawBatteryIcon(); - } - } - OLED::refresh(); + gui_drawBatteryIcon(); + } + } + OLED::refresh(); - // Update the setpoints for the temperature - if (boostModeOn) { + // Update the setpoints for the temperature + if (boostModeOn) { #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) currentTempTargetDegC = TipThermoModel::convertFtoC(systemSettings.BoostTemp); else #endif - { - currentTempTargetDegC = (systemSettings.BoostTemp); - } - } else { + { + currentTempTargetDegC = (systemSettings.BoostTemp); + } + } else { #ifdef ENABLED_FAHRENHEIT_SUPPORT if (systemSettings.temperatureInF) currentTempTargetDegC = TipThermoModel::convertFtoC(systemSettings.SolderingTemp); else #endif - { - currentTempTargetDegC = (systemSettings.SolderingTemp); - } - } + { + currentTempTargetDegC = (systemSettings.SolderingTemp); + } + } #ifdef MODEL_TS100 - // Undervoltage test - if (checkVoltageForExit()) { - lastButtonTime = xTaskGetTickCount(); - return; - } + // Undervoltage test + if (checkVoltageForExit()) { + lastButtonTime = xTaskGetTickCount(); + return; + } #else - // on the TS80 we only want to check for over voltage to prevent tip damage - /*if (getInputVoltageX10(systemSettings.voltageDiv, 1) > 150) { - lastButtonTime = xTaskGetTickCount(); - currentlyActiveTemperatureTarget = 0; - return; // Over voltage - }*/ + // on the TS80 we only want to check for over voltage to prevent tip damage + /*if (getInputVoltageX10(systemSettings.voltageDiv, 1) > 150) { + lastButtonTime = xTaskGetTickCount(); + currentlyActiveTemperatureTarget = 0; + return; // Over voltage + }*/ #endif - if (systemSettings.sensitivity && systemSettings.SleepTime) - if (xTaskGetTickCount() - lastMovementTime > sleepThres && xTaskGetTickCount() - lastButtonTime > sleepThres) { - if (gui_SolderingSleepingMode(false)) { - return; // If the function returns non-0 then exit - } - } - // slow down ui update rate - GUIDelay(); - } + if (systemSettings.sensitivity && systemSettings.SleepTime) + if (xTaskGetTickCount() - lastMovementTime > sleepThres + && xTaskGetTickCount() - lastButtonTime > sleepThres) { + if (gui_SolderingSleepingMode(false)) { + return; // If the function returns non-0 then exit + } + } + // slow down ui update rate + GUIDelay(); + } } void showDebugMenu(void) { - uint8_t screen = 0; - ButtonState b; - for (;;) { - OLED::clearScreen(); // Ensure the buffer starts clean - OLED::setCursor(0, 0); // Position the cursor at the 0,0 (top left) - OLED::setFont(1); // small font - OLED::print(SymbolVersionNumber); // Print version number - OLED::setCursor(0, 8); // second line - OLED::print(DebugMenu[screen]); - switch (screen) { - case 0: // Just prints date - break; - case 1: - // High water mark for GUI - OLED::printNumber(uxTaskGetStackHighWaterMark(GUITaskHandle), 5); - break; - case 2: - // High water mark for the Movement task - OLED::printNumber(uxTaskGetStackHighWaterMark(MOVTaskHandle), 5); - break; - case 3: - // High water mark for the PID task - OLED::printNumber(uxTaskGetStackHighWaterMark(PIDTaskHandle), 5); - break; - case 4: - // system up time stamp - OLED::printNumber(xTaskGetTickCount() / 100, 5); - break; - case 5: - // Movement time stamp - OLED::printNumber(lastMovementTime / 100, 5); - break; - case 6: - // Raw Tip - { - uint32_t temp = systemSettings.CalibrationOffset; - systemSettings.CalibrationOffset = 0; - OLED::printNumber(TipThermoModel::convertTipRawADCTouV(getTipRawTemp(1)), 6); - systemSettings.CalibrationOffset = temp; - } - break; - case 7: - // Temp in C - OLED::printNumber(TipThermoModel::getTipInC(1), 5); - break; - case 8: - // Handle Temp - OLED::printNumber(getHandleTemperature(), 3); - break; - case 9: - // Voltage input - printVoltage(); - break; - case 10: - // Print PCB ID number - OLED::printNumber(PCBVersion, 1); - break; - default: - break; - } + uint8_t screen = 0; + ButtonState b; + for (;;) { + OLED::clearScreen(); // Ensure the buffer starts clean + OLED::setCursor(0, 0); // Position the cursor at the 0,0 (top left) + OLED::setFont(1); // small font + OLED::print(SymbolVersionNumber); // Print version number + OLED::setCursor(0, 8); // second line + OLED::print(DebugMenu[screen]); + switch (screen) { + case 0: // Just prints date + break; + case 1: + // High water mark for GUI + OLED::printNumber(uxTaskGetStackHighWaterMark(GUITaskHandle), 5); + break; + case 2: + // High water mark for the Movement task + OLED::printNumber(uxTaskGetStackHighWaterMark(MOVTaskHandle), 5); + break; + case 3: + // High water mark for the PID task + OLED::printNumber(uxTaskGetStackHighWaterMark(PIDTaskHandle), 5); + break; + case 4: + // system up time stamp + OLED::printNumber(xTaskGetTickCount() / 100, 5); + break; + case 5: + // Movement time stamp + OLED::printNumber(lastMovementTime / 100, 5); + break; + case 6: + // Raw Tip + { + uint32_t temp = systemSettings.CalibrationOffset; + systemSettings.CalibrationOffset = 0; + OLED::printNumber( + TipThermoModel::convertTipRawADCTouV(getTipRawTemp(1)), 6); + systemSettings.CalibrationOffset = temp; + } + break; + case 7: + // Temp in C + OLED::printNumber(TipThermoModel::getTipInC(1), 5); + break; + case 8: + // Handle Temp + OLED::printNumber(getHandleTemperature(), 3); + break; + case 9: + // Voltage input + printVoltage(); + break; + case 10: + // Print PCB ID number + OLED::printNumber(PCBVersion, 1); + break; + default: + break; + } - OLED::refresh(); - b = getButtonState(); - if (b == BUTTON_B_SHORT) - return; - else if (b == BUTTON_F_SHORT) { - screen++; - screen = screen % 11; - } - GUIDelay(); - } + OLED::refresh(); + b = getButtonState(); + if (b == BUTTON_B_SHORT) + return; + else if (b == BUTTON_F_SHORT) { + screen++; + screen = screen % 11; + } + GUIDelay(); + } } /* StartGUITask function */ void startGUITask(void const *argument __unused) { - uint8_t tempWarningState = 0; - bool buttonLockout = false; - bool tempOnDisplay = false; - getTipRawTemp(1); // reset filter - OLED::setRotation(systemSettings.OrientationMode & 1); - uint32_t ticks = xTaskGetTickCount(); - ticks += 400; // 4 seconds from now - while (xTaskGetTickCount() < ticks) { - if (showBootLogoIfavailable() == false) ticks = xTaskGetTickCount(); - ButtonState buttons = getButtonState(); - if (buttons) ticks = xTaskGetTickCount(); // make timeout now so we will exit - GUIDelay(); - } + uint8_t tempWarningState = 0; + bool buttonLockout = false; + bool tempOnDisplay = false; + getTipRawTemp(1); // reset filter + OLED::setRotation(systemSettings.OrientationMode & 1); + uint32_t ticks = xTaskGetTickCount(); + ticks += 400; // 4 seconds from now + while (xTaskGetTickCount() < ticks) { + if (showBootLogoIfavailable() == false) + ticks = xTaskGetTickCount(); + ButtonState buttons = getButtonState(); + if (buttons) + ticks = xTaskGetTickCount(); // make timeout now so we will exit + GUIDelay(); + } - if (settingsWereReset) { - // Display alert settings were reset - OLED::clearScreen(); - OLED::setFont(1); - OLED::setCursor(0, 0); - OLED::print(SettingsResetMessage); - OLED::refresh(); - waitForButtonPressOrTimeout(1000); - } + if (settingsWereReset) { + // Display alert settings were reset + OLED::clearScreen(); + OLED::setFont(1); + OLED::setCursor(0, 0); + OLED::print(SettingsResetMessage); + OLED::refresh(); + waitForButtonPressOrTimeout(1000); + } - if (systemSettings.autoStartMode) { - // jump directly to the autostart mode - if (systemSettings.autoStartMode == 1) { - gui_solderingMode(0); - buttonLockout = true; - } else if (systemSettings.autoStartMode == 2) { - gui_solderingMode(1); - buttonLockout = true; - } else if (systemSettings.autoStartMode == 3) { - gui_solderingMode(2); - buttonLockout = true; - } - } + if (usb_pd_available) { + OLED::clearScreen(); + OLED::setFont(1); + OLED::setCursor(0, 0); + OLED::print(InputVoltageString); + OLED::refresh(); + waitForButtonPressOrTimeout(1000); + } - for (;;) { - ButtonState buttons = getButtonState(); - if (buttons != BUTTON_NONE) { - OLED::setDisplayState(OLED::DisplayState::ON); - OLED::setFont(0); - } - if (tempWarningState == 2) buttons = BUTTON_F_SHORT; - if (buttons != BUTTON_NONE && buttonLockout) - buttons = BUTTON_NONE; - else - buttonLockout = false; + if (systemSettings.autoStartMode) { + // jump directly to the autostart mode + if (systemSettings.autoStartMode == 1) { + gui_solderingMode(0); + buttonLockout = true; + } else if (systemSettings.autoStartMode == 2) { + gui_solderingMode(1); + buttonLockout = true; + } else if (systemSettings.autoStartMode == 3) { + gui_solderingMode(2); + buttonLockout = true; + } + } - switch (buttons) { - case BUTTON_NONE: - // Do nothing - break; - case BUTTON_BOTH: - // Not used yet - // In multi-language this might be used to reset language on a long hold - // or some such - break; + for (;;) { + ButtonState buttons = getButtonState(); + if (buttons != BUTTON_NONE) { + OLED::setDisplayState(OLED::DisplayState::ON); + OLED::setFont(0); + } + if (tempWarningState == 2) + buttons = BUTTON_F_SHORT; + if (buttons != BUTTON_NONE && buttonLockout) + buttons = BUTTON_NONE; + else + buttonLockout = false; - case BUTTON_B_LONG: - // Show the version information - showDebugMenu(); - break; - case BUTTON_F_LONG: - gui_solderingTempAdjust(); - saveSettings(); - break; - case BUTTON_F_SHORT: - gui_solderingMode(0); // enter soldering mode - buttonLockout = true; - break; - case BUTTON_B_SHORT: - enterSettingsMenu(); // enter the settings menu - buttonLockout = true; - break; - default: - break; - } + switch (buttons) { + case BUTTON_NONE: + // Do nothing + break; + case BUTTON_BOTH: + // Not used yet + // In multi-language this might be used to reset language on a long hold + // or some such + break; - currentTempTargetDegC = 0; // ensure tip is off - getInputVoltageX10(systemSettings.voltageDiv, 0); - uint16_t tipTemp = TipThermoModel::getTipInC(); + case BUTTON_B_LONG: + // Show the version information + showDebugMenu(); + break; + case BUTTON_F_LONG: + gui_solderingTempAdjust(); + saveSettings(); + break; + case BUTTON_F_SHORT: + gui_solderingMode(0); // enter soldering mode + buttonLockout = true; + break; + case BUTTON_B_SHORT: + enterSettingsMenu(); // enter the settings menu + buttonLockout = true; + break; + default: + break; + } - // Preemptively turn the display on. Turn it off if and only if - // the tip temperature is below 50 degrees C *and* motion sleep - // detection is enabled *and* there has been no activity (movement or - // button presses) in a while. - OLED::setDisplayState(OLED::DisplayState::ON); + currentTempTargetDegC = 0; // ensure tip is off + getInputVoltageX10(systemSettings.voltageDiv, 0); + uint16_t tipTemp = TipThermoModel::getTipInC(); - if ((tipTemp < 50) && systemSettings.sensitivity && (((xTaskGetTickCount() - lastMovementTime) > MOVEMENT_INACTIVITY_TIME) && ((xTaskGetTickCount() - lastButtonTime) > BUTTON_INACTIVITY_TIME))) { - OLED::setDisplayState(OLED::DisplayState::OFF); - } + // Preemptively turn the display on. Turn it off if and only if + // the tip temperature is below 50 degrees C *and* motion sleep + // detection is enabled *and* there has been no activity (movement or + // button presses) in a while. + OLED::setDisplayState(OLED::DisplayState::ON); - // Clear the lcd buffer - OLED::clearScreen(); - OLED::setCursor(0, 0); - if (systemSettings.detailedIDLE) { - OLED::setFont(1); - if (tipTemp > 470) { - OLED::print(TipDisconnectedString); - } else { - OLED::print(IdleTipString); - gui_drawTipTemp(false); - OLED::print(IdleSetString); - OLED::printNumber(systemSettings.SolderingTemp, 3); - } - OLED::setCursor(0, 8); + if ((tipTemp < 50) && systemSettings.sensitivity + && (((xTaskGetTickCount() - lastMovementTime) + > MOVEMENT_INACTIVITY_TIME) + && ((xTaskGetTickCount() - lastButtonTime) + > BUTTON_INACTIVITY_TIME))) { + OLED::setDisplayState(OLED::DisplayState::OFF); + } - OLED::print(InputVoltageString); - printVoltage(); + // Clear the lcd buffer + OLED::clearScreen(); + OLED::setCursor(0, 0); + if (systemSettings.detailedIDLE) { + OLED::setFont(1); + if (tipTemp > 470) { + OLED::print(TipDisconnectedString); + } else { + OLED::print(IdleTipString); + gui_drawTipTemp(false); + OLED::print(IdleSetString); + OLED::printNumber(systemSettings.SolderingTemp, 3); + } + OLED::setCursor(0, 8); - } else { - OLED::setFont(0); + OLED::print(InputVoltageString); + printVoltage(); + + } else { + OLED::setFont(0); #ifdef MODEL_TS80 - if (!OLED::getRotation()) { + if (!OLED::getRotation()) { #else - if (OLED::getRotation()) { + if (OLED::getRotation()) { #endif - OLED::drawArea(12, 0, 84, 16, idleScreenBG); - OLED::setCursor(0, 0); - gui_drawBatteryIcon(); - } else { - OLED::drawArea(0, 0, 84, 16, idleScreenBGF); // Needs to be flipped so button ends up - // on right side of screen - OLED::setCursor(84, 0); - gui_drawBatteryIcon(); - } - if (tipTemp > 55) - tempOnDisplay = true; - else if (tipTemp < 45) - tempOnDisplay = false; - if (tempOnDisplay) { - // draw temp over the start soldering button - // Location changes on screen rotation + OLED::drawArea(12, 0, 84, 16, idleScreenBG); + OLED::setCursor(0, 0); + gui_drawBatteryIcon(); + } else { + OLED::drawArea(0, 0, 84, 16, idleScreenBGF); // Needs to be flipped so button ends up + // on right side of screen + OLED::setCursor(84, 0); + gui_drawBatteryIcon(); + } + if (tipTemp > 55) + tempOnDisplay = true; + else if (tipTemp < 45) + tempOnDisplay = false; + if (tempOnDisplay) { + // draw temp over the start soldering button + // Location changes on screen rotation #ifdef MODEL_TS80 - if (!OLED::getRotation()) { + if (!OLED::getRotation()) { #else - if (OLED::getRotation()) { + if (OLED::getRotation()) { #endif - // in right handed mode we want to draw over the first part - OLED::fillArea(55, 0, 41, 16, 0); // clear the area for the temp - OLED::setCursor(56, 0); + // in right handed mode we want to draw over the first part + OLED::fillArea(55, 0, 41, 16, 0); // clear the area for the temp + OLED::setCursor(56, 0); - } else { - OLED::fillArea(0, 0, 41, 16, 0); // clear the area - OLED::setCursor(0, 0); - } - // draw in the temp - if (!(systemSettings.coolingTempBlink && (xTaskGetTickCount() % 25 < 16))) gui_drawTipTemp(false); // draw in the temp - } - } - OLED::refresh(); - GUIDelay(); - } + } else { + OLED::fillArea(0, 0, 41, 16, 0); // clear the area + OLED::setCursor(0, 0); + } + // draw in the temp + if (!(systemSettings.coolingTempBlink + && (xTaskGetTickCount() % 25 < 16))) + gui_drawTipTemp(false); // draw in the temp + } + } + OLED::refresh(); + GUIDelay(); + } }