1
0
forked from me/IronOS

Still testing

This commit is contained in:
Ben V. Brown
2020-07-21 13:39:50 +10:00
parent c70689df7d
commit b6c61cfb52
31 changed files with 674 additions and 557 deletions

View File

@@ -18,7 +18,7 @@
#include "fusb302b.h"
#include "I2CBB.hpp"
#include <pd.h>
#include "int_n.h"
/*
* Read a single byte from the FUSB302B
*
@@ -29,7 +29,9 @@
*/
static uint8_t fusb_read_byte(uint8_t addr) {
uint8_t data[1];
I2CBB::Mem_Read(FUSB302B_ADDR, addr, (uint8_t*) data, 1);
if (!I2CBB::Mem_Read(FUSB302B_ADDR, addr, (uint8_t*) data, 1)) {
asm("bkpt");
}
return data[0];
}
@@ -41,11 +43,8 @@ static uint8_t fusb_read_byte(uint8_t addr) {
* size: The number of bytes to read
* buf: The buffer into which data will be read
*/
static void fusb_read_buf(uint8_t addr, uint8_t size, uint8_t *buf) {
if(!I2CBB::Mem_Read(FUSB302B_ADDR, addr, (uint8_t*) buf, size)){
asm("bkpt");
}
static bool fusb_read_buf(uint8_t addr, uint8_t size, uint8_t *buf) {
return I2CBB::Mem_Read(FUSB302B_ADDR, addr, buf, size);
}
/*
@@ -55,11 +54,8 @@ static void fusb_read_buf(uint8_t addr, uint8_t size, uint8_t *buf) {
* addr: The memory address to which we will write
* byte: The value to write
*/
static void fusb_write_byte(uint8_t addr, uint8_t byte) {
if(!I2CBB::Mem_Write(FUSB302B_ADDR, addr, (uint8_t*) &byte, 1)){
asm("bkpt");
}
static bool fusb_write_byte(uint8_t addr, uint8_t byte) {
return I2CBB::Mem_Write(FUSB302B_ADDR, addr, (uint8_t*) &byte, 1);
}
/*
@@ -70,14 +66,12 @@ static void fusb_write_byte(uint8_t addr, uint8_t byte) {
* size: The number of bytes to write
* buf: The buffer to write
*/
static void fusb_write_buf(uint8_t addr, uint8_t size, const uint8_t *buf) {
if(!I2CBB::Mem_Write(FUSB302B_ADDR, addr, (uint8_t*) &buf, size)){
asm("bkpt");
}
static bool fusb_write_buf(uint8_t addr, uint8_t size, const uint8_t *buf) {
return I2CBB::Mem_Write(FUSB302B_ADDR, addr, buf, size);
}
void fusb_send_message(const union pd_msg *msg) {
/* Token sequences for the FUSB302B */
static uint8_t sop_seq[5] = {
FUSB_FIFO_TX_SOP1,
@@ -98,16 +92,23 @@ void fusb_send_message(const union pd_msg *msg) {
/* Set the number of bytes to be transmitted in the packet */
sop_seq[4] = FUSB_FIFO_TX_PACKSYM | msg_len;
if (!I2CBB::lock2()) {
asm("bkpt");
}
/* Write all three parts of the message to the TX FIFO */
fusb_write_buf( FUSB_FIFOS, 5, sop_seq);
fusb_write_buf( FUSB_FIFOS, msg_len, msg->bytes);
fusb_write_buf( FUSB_FIFOS, 4, eop_seq);
I2CBB::unlock2();
}
uint8_t fusb_read_message(union pd_msg *msg) {
uint8_t garbage[4];
if (!I2CBB::lock2()) {
asm("bkpt");
}
static uint8_t garbage[4];
uint8_t numobj;
/* If this isn't an SOP message, return error.
@@ -115,6 +116,8 @@ uint8_t fusb_read_message(union pd_msg *msg) {
* buffer is empty, and not try to read past a non-SOP message. */
if ((fusb_read_byte( FUSB_FIFOS) & FUSB_FIFO_RX_TOKEN_BITS)
!= FUSB_FIFO_RX_SOP) {
I2CBB::unlock2();
return 1;
}
/* Read the message header into msg */
@@ -128,33 +131,56 @@ uint8_t fusb_read_message(union pd_msg *msg) {
/* Throw the CRC32 in the garbage, since the PHY already checked it. */
fusb_read_buf( FUSB_FIFOS, 4, garbage);
I2CBB::unlock2();
return 0;
}
void fusb_send_hardrst() {
if (!I2CBB::lock2()) {
asm("bkpt");
}
/* Send a hard reset */
fusb_write_byte( FUSB_CONTROL3, 0x07 | FUSB_CONTROL3_SEND_HARD_RESET);
I2CBB::unlock2();
}
void fusb_setup() {
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 10, 0);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
if (!I2CBB::lock2()) {
asm("bkpt");
}
}
/* Fully reset the FUSB302B */
fusb_write_byte( FUSB_RESET, FUSB_RESET_SW_RES);
delay_ms(2);
if (!fusb_read_id()) {
asm("bkpt");
}
/* Turn on all power */
fusb_write_byte( FUSB_POWER, 0x0F);
/* Set interrupt masks */
//Setting to 0 so interrupts are allowed
fusb_write_byte( FUSB_MASK1, 0x00);
fusb_write_byte( FUSB_MASKA, 0x00);
fusb_write_byte( FUSB_MASKB, 0x00);
fusb_write_byte( FUSB_CONTROL0, 0x04);
fusb_write_byte( FUSB_CONTROL0, 0b11 << 2);
/* Enable automatic retransmission */
fusb_write_byte( FUSB_CONTROL3, 0x07);
//set defaults
fusb_write_byte( FUSB_CONTROL2, 0x00);
/* Flush the RX buffer */
fusb_write_byte( FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
@@ -184,29 +210,48 @@ void fusb_setup() {
fusb_write_byte( FUSB_SWITCHES0, 0x0B);
}
resetWatchdog();
/* Reset the PD logic */
fusb_write_byte( FUSB_RESET, FUSB_RESET_PD_RESET);
fusb_reset();
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
I2CBB::unlock2();
}
}
void fusb_get_status(union fusb_status *status) {
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
if (!I2CBB::lock2()) {
asm("bkpt");
}
}
/* Read the interrupt and status flags into status */
fusb_read_buf( FUSB_STATUS0A, 7, status->bytes);
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
I2CBB::unlock2();
}
}
enum fusb_typec_current fusb_get_typec_current() {
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
if (!I2CBB::lock2()) {
asm("bkpt");
}
}
/* Read the BC_LVL into a variable */
enum fusb_typec_current bc_lvl = (enum fusb_typec_current) (fusb_read_byte(
FUSB_STATUS0) & FUSB_STATUS0_BC_LVL);
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
I2CBB::unlock2();
}
return bc_lvl;
}
void fusb_reset() {
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
if (!I2CBB::lock2()) {
asm("bkpt");
}
}
/* Flush the TX buffer */
fusb_write_byte( FUSB_CONTROL0, 0x44);
@@ -214,5 +259,16 @@ void fusb_reset() {
fusb_write_byte( FUSB_CONTROL1, FUSB_CONTROL1_RX_FLUSH);
/* Reset the PD logic */
fusb_write_byte( FUSB_RESET, FUSB_RESET_PD_RESET);
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
I2CBB::unlock2();
}
}
bool fusb_read_id() {
//Return true if read of the revision ID is sane
uint8_t version = 0;
fusb_read_buf(FUSB_DEVICE_ID, 1, &version);
if (version == 0 || version == 0xFF)
return false;
return true;
}

View File

@@ -300,4 +300,6 @@ void fusb_setup();
*/
void fusb_reset();
bool fusb_read_id();
#endif /* PDB_FUSB302B_H */

View File

@@ -23,6 +23,7 @@ uint8_t fusb302_detect() {
void fusb302_start_processing() {
/* Initialize the FUSB302B */
fusb_setup();
resetWatchdog();
/* Create the policy engine thread. */
PolicyEngine::init();
@@ -30,7 +31,7 @@ void fusb302_start_processing() {
ProtocolReceive::init();
ProtocolTransmit::init();
ResetHandler::init();
resetWatchdog();
/* Create the INT_N thread. */
InterruptHandler::init();
}

View File

@@ -34,20 +34,23 @@ ResetHandler::hardrst_state ResetHandler::hardrst_reset_layer() {
/* First, wait for the signal to run a hard reset. */
eventmask_t evt = waitForEvent(
PDB_EVT_HARDRST_RESET | PDB_EVT_HARDRST_I_HARDRST);
/* Reset the Protocol RX machine */
ProtocolReceive::notify( PDB_EVT_PRLRX_RESET);
taskYIELD();
/* Reset the Protocol TX machine */
ProtocolTransmit::notify(PDB_EVT_PRLTX_RESET);
taskYIELD();
/* Continue the process based on what event started the reset. */
if (evt & PDB_EVT_HARDRST_RESET) {
/* Policy Engine started the reset. */
return PRLHRRequestHardReset;
if (evt & (PDB_EVT_HARDRST_RESET | PDB_EVT_HARDRST_I_HARDRST)) {
/* Reset the Protocol RX machine */
ProtocolReceive::notify( PDB_EVT_PRLRX_RESET);
taskYIELD();
/* Reset the Protocol TX machine */
ProtocolTransmit::notify( ProtocolTransmit::Notifications::PDB_EVT_PRLTX_RESET);
taskYIELD();
/* Continue the process based on what event started the reset. */
if (evt & PDB_EVT_HARDRST_RESET) {
/* Policy Engine started the reset. */
return PRLHRRequestHardReset;
} else {
/* PHY started the reset */
return PRLHRIndicateHardReset;
}
} else {
/* PHY started the reset */
return PRLHRIndicateHardReset;
return PRLHRResetLayer;
}
}
@@ -100,8 +103,7 @@ void ResetHandler::init() {
}
void ResetHandler::notify(uint32_t notification) {
xTaskNotify(TaskHandle, notification,
eNotifyAction::eSetBits);
xTaskNotify(TaskHandle, notification, eNotifyAction::eSetBits);
}
void ResetHandler::Thread(const void *arg) {
@@ -134,6 +136,7 @@ void ResetHandler::Thread(const void *arg) {
default:
/* This is an error. It really shouldn't happen. We might
* want to handle it anyway, though. */
state = PRLHRResetLayer;
break;
}
}

View File

@@ -33,7 +33,7 @@ public:
private:
static void Thread(const void *arg);
static osThreadId TaskHandle;
static const size_t TaskStackSize = 1024 / 4;
static const size_t TaskStackSize = 1536 / 4;
static uint32_t TaskBuffer[TaskStackSize];
static osStaticThreadDef_t TaskControlBlock;
static uint32_t waitForEvent(uint32_t mask, uint32_t ticksToWait =

View File

@@ -25,6 +25,7 @@
#include "policy_engine.h"
#include "protocol_rx.h"
#include "protocol_tx.h"
#include "task.h"
#include "BSP.h"
osThreadId InterruptHandler::TaskHandle;
@@ -32,39 +33,40 @@ uint32_t InterruptHandler::TaskBuffer[InterruptHandler::TaskStackSize];
osStaticThreadDef_t InterruptHandler::TaskControlBlock;
void InterruptHandler::init() {
osThreadStaticDef(Task, Thread, PDB_PRIO_PE, 0, TaskStackSize, TaskBuffer,
&TaskControlBlock);
osThreadStaticDef(Task, Thread, PDB_PRIO_PRL_INT_N, 0, TaskStackSize,
TaskBuffer, &TaskControlBlock);
TaskHandle = osThreadCreate(osThread(Task), NULL);
}
void InterruptHandler::Thread(const void *arg) {
(void) arg;
union fusb_status status;
eventmask_t events;
volatile uint32_t events;
while (true) {
/* If the INT_N line is low */
xTaskNotifyWait(0x00, 0x0F, NULL, 5);
xTaskNotifyWait(0x00, 0x0F, NULL, 100);
/* Read the FUSB302B status and interrupt registers */
fusb_get_status(&status);
//Check for rx alerts
/* If the I_GCRCSENT flag is set, tell the Protocol RX thread */
//This means a message was recieved with a good CRC
if (status.interruptb & FUSB_INTERRUPTB_I_GCRCSENT) {
ProtocolReceive::notify(PDB_EVT_PRLRX_I_GCRCSENT);
}
if ((status.status1 & FUSB_STATUS1_RX_EMPTY) == 0) {
ProtocolReceive::notify(PDB_EVT_PRLRX_I_GCRCSENT);
}
/* If the I_TXSENT or I_RETRYFAIL flag is set, tell the Protocol TX
* thread */
events = 0;
if (status.interrupta & FUSB_INTERRUPTA_I_RETRYFAIL) {
events |= PDB_EVT_PRLTX_I_RETRYFAIL;
}
if (status.interrupta & FUSB_INTERRUPTA_I_TXSENT) {
events |= PDB_EVT_PRLTX_I_TXSENT;
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_I_TXSENT);
}
if (events) {
ProtocolTransmit::notify(events);
if (status.interrupta & FUSB_INTERRUPTA_I_RETRYFAIL) {
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_I_RETRYFAIL);
}
/* If the I_HARDRST or I_HARDSENT flag is set, tell the Hard Reset
@@ -72,25 +74,25 @@ void InterruptHandler::Thread(const void *arg) {
events = 0;
if (status.interrupta & FUSB_INTERRUPTA_I_HARDRST) {
events |= PDB_EVT_HARDRST_I_HARDRST;
}
if (status.interrupta & FUSB_INTERRUPTA_I_HARDSENT) {
// events |= PDB_EVT_HARDRST_I_HARDRST;
} else if (status.interrupta & FUSB_INTERRUPTA_I_HARDSENT) {
events |= PDB_EVT_HARDRST_I_HARDSENT;
}
if (events) {
ResetHandler::notify(events);
}
/* If the I_OCP_TEMP and OVRTEMP flags are set, tell the Policy
* Engine thread */
if (status.interrupta & FUSB_INTERRUPTA_I_OCP_TEMP
&& status.status1 & FUSB_STATUS1_OVRTEMP) {
PolicyEngine::notify(PDB_EVT_PE_I_OVRTEMP);
}
}
}
volatile uint8_t irqs = 0;
void InterruptHandler::irqCallback() {
xTaskNotify(TaskHandle, 0x0F, eNotifyAction::eSetBits);
irqs++;
BaseType_t taskWoke = pdFALSE;
xTaskNotifyFromISR(TaskHandle, 0x0F, eNotifyAction::eSetBits, &taskWoke);
portYIELD_FROM_ISR(taskWoke);
}

View File

@@ -30,7 +30,7 @@ public:
private:
static void Thread(const void *arg);
static osThreadId TaskHandle;
static const size_t TaskStackSize = 1024 / 4;
static const size_t TaskStackSize = 1536 / 4;
static uint32_t TaskBuffer[TaskStackSize];
static osStaticThreadDef_t TaskControlBlock;
/*

View File

@@ -273,18 +273,18 @@
* Where a range is specified, the middle of the range (rounded down to the
* nearest millisecond) is used.
*/
#define PD_T_CHUNKING_NOT_SUPPORTED (45)
#define PD_T_HARD_RESET_COMPLETE (4)
#define PD_T_PS_TRANSITION (500)
#define PD_T_SENDER_RESPONSE (27)
#define PD_T_SINK_REQUEST (100)
#define PD_T_TYPEC_SINK_WAIT_CAP (465)
#define PD_T_PD_DEBOUNCE (15)
#define PD_T_CHUNKING_NOT_SUPPORTED (450)
#define PD_T_HARD_RESET_COMPLETE (400)
#define PD_T_PS_TRANSITION (5000)
#define PD_T_SENDER_RESPONSE (2700)
#define PD_T_SINK_REQUEST (1000)
#define PD_T_TYPEC_SINK_WAIT_CAP (5000)
#define PD_T_PD_DEBOUNCE (2000)
/*
* Counter maximums
*/
#define PD_N_HARD_RESET_COUNT 10
#define PD_N_HARD_RESET_COUNT 20
/*
* Value parameters

View File

@@ -181,13 +181,13 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_wait_cap() {
if ((hdr_template & PD_HDR_SPECREV) == PD_SPECREV_1_0) {
/* If the other end is using at least version 3.0, we'll
* use version 3.0. */
if ((tempMessage.hdr & PD_HDR_SPECREV) >= PD_SPECREV_3_0) {
hdr_template |= PD_SPECREV_3_0;
/* Otherwise, use 2.0. Don't worry about the 1.0 case
* because we don't have hardware for PD 1.0 signaling. */
} else {
hdr_template |= PD_SPECREV_2_0;
}
// if ((tempMessage.hdr & PD_HDR_SPECREV) >= PD_SPECREV_3_0) {
// hdr_template |= PD_SPECREV_3_0;
// /* Otherwise, use 2.0. Don't worry about the 1.0 case
// * because we don't have hardware for PD 1.0 signaling. */
// } else {
hdr_template |= PD_SPECREV_2_0;
// }
}
return PESinkEvalCap;
/* If the message was a Soft_Reset, do the soft reset procedure */
@@ -247,7 +247,8 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_select_cap() {
/* Transmit the request */
ProtocolTransmit::pushMessage(&_last_dpm_request);
//Send indication that there is a message pending
ProtocolTransmit::notify( PDB_EVT_PRLTX_MSG_TX);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_MSG_TX);
eventmask_t evt = waitForEvent(
PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR | PDB_EVT_PE_RESET);
/* Don't free the request; we might need it again */
@@ -396,7 +397,8 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_ready() {
/* If the DPM wants us to, send a Get_Source_Cap message */
if (evt & PDB_EVT_PE_GET_SOURCE_CAP) {
/* Tell the protocol layer we're starting an AMS */
ProtocolTransmit::notify( PDB_EVT_PRLTX_START_AMS);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_START_AMS);
return PESinkGetSourceCap;
}
@@ -406,14 +408,16 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_ready() {
* design of this firmware. */
if (evt & PDB_EVT_PE_NEW_POWER) {
/* Tell the protocol layer we're starting an AMS */
ProtocolTransmit::notify( PDB_EVT_PRLTX_START_AMS);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_START_AMS);
return PESinkEvalCap;
}
/* If SinkPPSPeriodicTimer ran out, send a new request */
if (evt & PDB_EVT_PE_PPS_REQUEST) {
/* Tell the protocol layer we're starting an AMS */
ProtocolTransmit::notify( PDB_EVT_PRLTX_START_AMS);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_START_AMS);
return PESinkSelectCap;
}
@@ -538,7 +542,8 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_get_source_cap() {
| PD_NUMOBJ(0);
/* Transmit the Get_Source_Cap */
ProtocolTransmit::pushMessage(get_source_cap);
ProtocolTransmit::notify( PDB_EVT_PRLTX_MSG_TX);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_MSG_TX);
eventmask_t evt = waitForEvent(
PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR | PDB_EVT_PE_RESET);
/* Free the sent message */
@@ -562,7 +567,8 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_give_sink_cap() {
/* Transmit our capabilities */
ProtocolTransmit::pushMessage(snk_cap);
ProtocolTransmit::notify( PDB_EVT_PRLTX_MSG_TX);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_MSG_TX);
eventmask_t evt = waitForEvent(
PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR | PDB_EVT_PE_RESET);
@@ -623,7 +629,8 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_soft_reset() {
accept.hdr = hdr_template | PD_MSGTYPE_ACCEPT | PD_NUMOBJ(0);
/* Transmit the Accept */
ProtocolTransmit::pushMessage(&accept);
ProtocolTransmit::notify( PDB_EVT_PRLTX_MSG_TX);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_MSG_TX);
eventmask_t evt = waitForEvent(
PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR | PDB_EVT_PE_RESET);
/* Free the sent message */
@@ -650,7 +657,8 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_send_soft_reset() {
softrst->hdr = hdr_template | PD_MSGTYPE_SOFT_RESET | PD_NUMOBJ(0);
/* Transmit the soft reset */
ProtocolTransmit::pushMessage(softrst);
ProtocolTransmit::notify( PDB_EVT_PRLTX_MSG_TX);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_MSG_TX);
eventmask_t evt = waitForEvent(
PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR | PDB_EVT_PE_RESET);
/* If we got reset signaling, transition to default */
@@ -711,7 +719,8 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_send_not_supported() {
/* Transmit the message */
ProtocolTransmit::pushMessage(not_supported);
ProtocolTransmit::notify( PDB_EVT_PRLTX_MSG_TX);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_MSG_TX);
eventmask_t evt = waitForEvent(
PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR | PDB_EVT_PE_RESET);

View File

@@ -116,7 +116,7 @@ private:
portMAX_DELAY);
//Task resources
static osThreadId TaskHandle;
static const size_t TaskStackSize = 1024 / 4;
static const size_t TaskStackSize = 2048 / 4;
static uint32_t TaskBuffer[TaskStackSize];
static osStaticThreadDef_t TaskControlBlock;
static union pd_msg tempMessage;

View File

@@ -39,7 +39,7 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_wait_phy() {
/* If we got a reset event, reset */
if (evt & PDB_EVT_PRLRX_RESET) {
waitForEvent(PDB_EVT_PRLRX_RESET, 0);
// waitForEvent(PDB_EVT_PRLRX_RESET, 0);
return PRLRxWaitPHY;
}
/* If we got an I_GCRCSENT event, read the message and decide what to do */
@@ -73,7 +73,8 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_reset() {
_rx_messageid = -1;
/* TX transitions to its reset state */
ProtocolTransmit::notify( PDB_EVT_PRLTX_RESET);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_RESET);
taskYIELD();
/* If we got a RESET signal, reset the machine */
@@ -84,7 +85,7 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_reset() {
/* Go to the Check_MessageID state */
return PRLRxCheckMessageID;
}
volatile uint32_t rxCounter = 0;
/*
* PRL_Rx_Check_MessageID state
*/
@@ -99,9 +100,9 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_check_messageid(
/* Otherwise, there's either no stored ID or this message has an ID we
* haven't just seen. Transition to the Store_MessageID state. */
/*if (PD_MESSAGEID_GET(&tempMessage) == _rx_messageid) {
return PRLRxWaitPHY;
} else*/
return PRLRxWaitPHY;
} else*/
rxCounter++;
{
return PRLRxStoreMessageID;
}
@@ -113,7 +114,8 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_check_messageid(
ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_store_messageid() {
/* Tell ProtocolTX to discard the message being transmitted */
ProtocolTransmit::notify( PDB_EVT_PRLTX_DISCARD);
ProtocolTransmit::notify(
ProtocolTransmit::Notifications::PDB_EVT_PRLTX_DISCARD);
taskYIELD();
/* Update the stored MessageID */
@@ -122,7 +124,7 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_store_messageid(
/* Pass the message to the policy engine. */
PolicyEngine::handleMessage(&tempMessage);
PolicyEngine::notify( PDB_EVT_PE_MSG_RX);
PolicyEngine::notify(PDB_EVT_PE_MSG_RX);
/* Don't check if we got a RESET because we'd do nothing different. */
@@ -163,6 +165,9 @@ void ProtocolReceive::thread(const void *args) {
}
void ProtocolReceive::notify(uint32_t notification) {
if (notification == PDB_EVT_PRLRX_I_GCRCSENT) {
// asm("bkpt");
}
xTaskNotify(TaskHandle, notification, eNotifyAction::eSetBits);
}

View File

@@ -34,7 +34,7 @@ private:
static void thread(const void *args);
static osThreadId TaskHandle;
static const size_t TaskStackSize = 512 / 4;
static const size_t TaskStackSize = 1024 / 4;
static uint32_t TaskBuffer[TaskStackSize];
static osStaticThreadDef_t TaskControlBlock;
/*

View File

@@ -59,18 +59,20 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_phy_reset() {
*/
ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_wait_message() {
/* Wait for an event */
eventmask_t evt = waitForEvent(
PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD | PDB_EVT_PRLTX_MSG_TX);
ProtocolTransmit::Notifications evt = waitForEvent(
(uint32_t) Notifications::PDB_EVT_PRLTX_RESET
| (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD
| (uint32_t) Notifications::PDB_EVT_PRLTX_MSG_TX);
if (evt & PDB_EVT_PRLTX_RESET) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_RESET) {
return PRLTxPHYReset;
}
if (evt & PDB_EVT_PRLTX_DISCARD) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD) {
return PRLTxDiscardMessage;
}
/* If the policy engine is trying to send a message */
if (evt & PDB_EVT_PRLTX_MSG_TX) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_MSG_TX) {
/* Get the message */
getMessage();
@@ -104,13 +106,14 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_reset() {
*/
ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_construct_message() {
/* Make sure nobody wants us to reset */
eventmask_t evt = waitForEvent(
PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD, 0);
ProtocolTransmit::Notifications evt = waitForEvent(
(uint32_t) Notifications::PDB_EVT_PRLTX_RESET
| (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD, 0);
if (evt & PDB_EVT_PRLTX_RESET) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_RESET) {
return PRLTxPHYReset;
}
if (evt & PDB_EVT_PRLTX_DISCARD) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD) {
return PRLTxDiscardMessage;
}
@@ -121,8 +124,9 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_construct_mess
/* PD 3.0 collision avoidance */
if (PolicyEngine::isPD3_0()) {
/* If we're starting an AMS, wait for permission to transmit */
evt = waitForEvent(PDB_EVT_PRLTX_START_AMS, 0);
if (evt & PDB_EVT_PRLTX_START_AMS) {
evt = waitForEvent((uint32_t) Notifications::PDB_EVT_PRLTX_START_AMS,
0);
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_START_AMS) {
while (fusb_get_typec_current() != fusb_sink_tx_ok) {
osDelay(1);
}
@@ -141,23 +145,25 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_construct_mess
ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_wait_response() {
/* Wait for an event. There is no need to run CRCReceiveTimer, since the
* FUSB302B handles that as part of its retry mechanism. */
eventmask_t evt = waitForEvent(
PDB_EVT_PRLTX_RESET | PDB_EVT_PRLTX_DISCARD | PDB_EVT_PRLTX_I_TXSENT
| PDB_EVT_PRLTX_I_RETRYFAIL);
ProtocolTransmit::Notifications evt = waitForEvent(
(uint32_t) Notifications::PDB_EVT_PRLTX_RESET
| (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD
| (uint32_t) Notifications::PDB_EVT_PRLTX_I_TXSENT
| (uint32_t) Notifications::PDB_EVT_PRLTX_I_RETRYFAIL);
if (evt & PDB_EVT_PRLTX_RESET) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_RESET) {
return PRLTxPHYReset;
}
if (evt & PDB_EVT_PRLTX_DISCARD) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD) {
return PRLTxDiscardMessage;
}
/* If the message was sent successfully */
if (evt & PDB_EVT_PRLTX_I_TXSENT) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_I_TXSENT) {
return PRLTxMatchMessageID;
}
/* If the message failed to be sent */
if (evt & PDB_EVT_PRLTX_I_RETRYFAIL) {
if ((uint32_t)evt & (uint32_t) Notifications::PDB_EVT_PRLTX_I_RETRYFAIL) {
return PRLTxTransmissionError;
}
@@ -253,16 +259,16 @@ void ProtocolTransmit::thread(const void *args) {
}
}
void ProtocolTransmit::notify(uint32_t notification) {
xTaskNotify(TaskHandle, notification, eNotifyAction::eSetBits);
void ProtocolTransmit::notify(ProtocolTransmit::Notifications notification) {
xTaskNotify(TaskHandle, (uint32_t )notification, eNotifyAction::eSetBits);
}
void ProtocolTransmit::init() {
messagesWaiting = xQueueCreateStatic(PDB_MSG_POOL_SIZE,
sizeof(union pd_msg), ucQueueStorageArea, &xStaticQueue);
osThreadStaticDef(pd_txTask, thread, PDB_PRIO_PRL, 0,
TaskStackSize, TaskBuffer, &TaskControlBlock);
osThreadStaticDef(pd_txTask, thread, PDB_PRIO_PE, 0, TaskStackSize,
TaskBuffer, &TaskControlBlock);
TaskHandle = osThreadCreate(osThread(pd_txTask), NULL);
}
@@ -279,8 +285,9 @@ void ProtocolTransmit::getMessage() {
xQueueReceive(messagesWaiting, &temp_msg, 1);
}
uint32_t ProtocolTransmit::waitForEvent(uint32_t mask, uint32_t ticksToWait) {
ProtocolTransmit::Notifications ProtocolTransmit::waitForEvent(uint32_t mask,
uint32_t ticksToWait) {
uint32_t pulNotificationValue;
xTaskNotifyWait(0x00, mask, &pulNotificationValue, ticksToWait);
return pulNotificationValue & mask;
return (Notifications) (pulNotificationValue & mask);
}

View File

@@ -24,24 +24,28 @@
#include <pd.h>
/* Events for the Protocol TX thread */
#define PDB_EVT_PRLTX_RESET EVENT_MASK(0)
#define PDB_EVT_PRLTX_I_TXSENT EVENT_MASK(1)
#define PDB_EVT_PRLTX_I_RETRYFAIL EVENT_MASK(2)
#define PDB_EVT_PRLTX_DISCARD EVENT_MASK(3)
#define PDB_EVT_PRLTX_MSG_TX EVENT_MASK(4)
#define PDB_EVT_PRLTX_START_AMS EVENT_MASK(5)
class ProtocolTransmit {
public:
static void init();
//Push a message to the queue to be sent out the pd comms bus
static void pushMessage(union pd_msg *msg);
static void notify(uint32_t notification);
enum class Notifications {
PDB_EVT_PRLTX_RESET = EVENT_MASK(0), //
PDB_EVT_PRLTX_I_TXSENT = EVENT_MASK(1), //
PDB_EVT_PRLTX_I_RETRYFAIL = EVENT_MASK(2), //
PDB_EVT_PRLTX_DISCARD = EVENT_MASK(3), //
PDB_EVT_PRLTX_MSG_TX = EVENT_MASK(4), //
PDB_EVT_PRLTX_START_AMS = EVENT_MASK(5), //
};
static void notify(Notifications notification);
private:
static void thread(const void *args);
static osThreadId TaskHandle;
static const size_t TaskStackSize = 512 / 4;
static const size_t TaskStackSize = 1024 / 4;
static uint32_t TaskBuffer[TaskStackSize];
static osStaticThreadDef_t TaskControlBlock;
/*
@@ -83,8 +87,8 @@ private:
//Reads a message off the queue into the temp message
static void getMessage();
static union pd_msg temp_msg;
static uint32_t waitForEvent(uint32_t mask, uint32_t ticksToWait =
portMAX_DELAY);
static Notifications waitForEvent(uint32_t mask, uint32_t ticksToWait =
portMAX_DELAY);
};

View File

@@ -213,7 +213,7 @@ void OLED::useSecondaryFramebuffer(bool useSecondary) {
}
void OLED::setRotation(bool leftHanded) {
#ifdef MODEL_TS80
#if defined( MODEL_TS80) +defined( MODEL_TS80P) > 0
leftHanded = !leftHanded;
#endif
if (inLeftHandedMode == leftHanded) {

View File

@@ -27,12 +27,11 @@
* This was bought to my attention by <Kuba Sztandera>
*/
uint32_t TipThermoModel::convertTipRawADCTouV(uint16_t rawADC) {
// This takes the raw ADC samples, converts these to uV
// Then divides this down by the gain to convert to the uV on the input to the op-amp (A+B terminals)
// Then remove the calibration value that is stored as a tip offset
uint32_t vddRailmVX10 = 33000; //The vreg is +-2%, but we have no higher accuracy available
uint32_t vddRailmVX10 = 33000;//The vreg is +-2%, but we have no higher accuracy available
// 4096 * 8 readings for full scale
// Convert the input ADC reading back into mV times 10 format.
uint32_t rawInputmVX10 = (rawADC * vddRailmVX10) / (4096 * 8);
@@ -76,7 +75,7 @@ uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) {
tipuVDelta *= 10;
tipuVDelta /= systemSettings.TipGain;
#ifdef MODEL_TS80
#if defined( MODEL_TS80)+defined( MODEL_TS80P)>0
tipuVDelta /= OP_AMP_GAIN_STAGE_TS100 / OP_AMP_GAIN_STAGE_TS80;
#endif