move protocol message to stack, disable battery in PWR's CR3

there is still issue with CC1/CC2 pull down resistor and vstate is not
correct.
This commit is contained in:
hathach
2023-06-09 18:15:28 +07:00
parent 8181d470e5
commit 912802456b
6 changed files with 162 additions and 75 deletions

View File

@@ -38,10 +38,28 @@ extern "C" {
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
enum {
TCD_EVENT_INVALID = 0,
TCD_EVENT_CC_CHANGED,
TCD_EVENT_RX_COMPLETE,
};
typedef struct {
uint8_t rhport;
uint8_t event_id;
union {
struct {
uint8_t cc_state[2];
} cc_changed;
struct {
uint16_t xferred_bytes;
uint8_t result;
} rx_complete;
};
} tcd_event_t;;
@@ -68,4 +86,38 @@ void tcd_int_handler(uint8_t rhport);
bool tcd_rx_start(uint8_t rhport, uint8_t* buffer, uint16_t total_bytes);
bool tcd_tx_start(uint8_t rhport, uint8_t const* buffer, uint16_t total_bytes);
//--------------------------------------------------------------------+
// Event API (implemented by stack)
// Called by TCD to notify stack
//--------------------------------------------------------------------+
extern void tcd_event_handler(tcd_event_t const * event, bool in_isr);
TU_ATTR_ALWAYS_INLINE static inline
void tcd_event_cc_changed(uint8_t rhport, uint8_t cc1, uint8_t cc2, bool in_isr) {
tcd_event_t event = {
.rhport = rhport,
.event_id = TCD_EVENT_CC_CHANGED,
.cc_changed = {
.cc_state = {cc1, cc2 }
}
};
tcd_event_handler(&event, in_isr);
}
TU_ATTR_ALWAYS_INLINE static inline
void tcd_event_rx_complete(uint8_t rhport, uint16_t xferred_bytes, uint8_t result, bool in_isr) {
tcd_event_t event = {
.rhport = rhport,
.event_id = TCD_EVENT_RX_COMPLETE,
.rx_complete = {
.xferred_bytes = xferred_bytes,
.result = result
}
};
tcd_event_handler(&event, in_isr);
}
#endif

View File

@@ -42,35 +42,38 @@
// Event queue
// utcd_int_set() is used as mutex in OS NONE config
void utcd_int_set(bool enabled);
OSAL_QUEUE_DEF(utcd_int_set, utcd_qdef, CFG_TUC_TASK_QUEUE_SZ, tcd_event_t);
tu_static osal_queue_t utcd_q;
OSAL_QUEUE_DEF(utcd_int_set, _utcd_qdef, CFG_TUC_TASK_QUEUE_SZ, tcd_event_t);
tu_static osal_queue_t _utcd_q;
// if stack is initialized
static bool utcd_inited = false;
static bool _utcd_inited = false;
// if port is initialized
static bool port_inited[TUP_TYPEC_RHPORTS_NUM];
static bool _port_inited[TUP_TYPEC_RHPORTS_NUM];
// Max possible PD size is 262 bytes
static uint8_t _rx_buf[262] TU_ATTR_ALIGNED(4);
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
bool tuc_inited(uint8_t rhport) {
return utcd_inited && port_inited[rhport];
return _utcd_inited && _port_inited[rhport];
}
bool tuc_init(uint8_t rhport, tusb_typec_port_type_t port_type) {
// Initialize stack
if (!utcd_inited) {
tu_memclr(port_inited, sizeof(port_inited));
if (!_utcd_inited) {
tu_memclr(_port_inited, sizeof(_port_inited));
utcd_q = osal_queue_create(&utcd_qdef);
TU_ASSERT(utcd_q != NULL);
_utcd_q = osal_queue_create(&_utcd_qdef);
TU_ASSERT(_utcd_q != NULL);
utcd_inited = true;
_utcd_inited = true;
}
// skip if port already initialized
if ( port_inited[rhport] ) {
if ( _port_inited[rhport] ) {
return true;
}
@@ -79,10 +82,42 @@ bool tuc_init(uint8_t rhport, tusb_typec_port_type_t port_type) {
TU_ASSERT(tcd_init(rhport, port_type));
tcd_int_enable(rhport);
port_inited[rhport] = true;
_port_inited[rhport] = true;
return true;
}
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
//bool parse_message(uint8_t const * data, uint16_t len, pd_msg_t * msg) {
// // TODO
// (void) data;
// (void) len;
// (void) msg;
// return false;
//}
void tcd_event_handler(tcd_event_t const * event, bool in_isr) {
(void) in_isr;
switch(event->event_id) {
case TCD_EVENT_CC_CHANGED:
if (event->cc_changed.cc_state[0] || event->cc_changed.cc_state[1]) {
// Attach
tcd_rx_start(event->rhport, _rx_buf, sizeof(_rx_buf));
}else {
// Detach
}
break;
case TCD_EVENT_RX_COMPLETE:
// TODO process message here in ISR, move to thread later
// start new rx
tcd_rx_start(event->rhport, _rx_buf, sizeof(_rx_buf));
break;
}
}
//--------------------------------------------------------------------+
//
@@ -90,7 +125,7 @@ bool tuc_init(uint8_t rhport, tusb_typec_port_type_t port_type) {
void utcd_int_set(bool enabled) {
// Disable all controllers since they shared the same event queue
for (uint8_t p = 0; p < TUP_TYPEC_RHPORTS_NUM; p++) {
if ( port_inited[p] ) {
if ( _port_inited[p] ) {
if (enabled) {
tcd_int_enable(p);
}else {

View File

@@ -38,7 +38,7 @@ extern "C" {
//--------------------------------------------------------------------+
#ifndef CFG_TUC_TASK_QUEUE_SZ
#define CFG_TUC_TASK_QUEUE_SZ 16
#define CFG_TUC_TASK_QUEUE_SZ 8
#endif