pinebuds/platform/hal/hal_mcu2cp.c

568 lines
14 KiB
C
Raw Permalink Normal View History

2022-08-15 04:20:27 -05:00
/***************************************************************************
*
* Copyright 2015-2019 BES.
* All rights reserved. All unpublished rights reserved.
*
* No part of this work may be used or reproduced in any form or by any
* means, or stored in a database or retrieval system, without prior written
* permission of BES.
*
* Use of this work is governed by a license granted by BES.
* This work contains confidential and proprietary information of
* BES. which is protected by copyright, trade secret,
* trademark and other intellectual property rights.
*
****************************************************************************/
#ifdef CHIP_HAS_CP
#include "hal_mcu2cp.h"
2022-08-15 04:20:27 -05:00
#include "cmsis_nvic.h"
#include "hal_location.h"
#include "hal_sleep.h"
#include "hal_timer.h"
#include "hal_trace.h"
#include "plat_addr_map.h"
2022-08-15 04:20:27 -05:00
#include CHIP_SPECIFIC_HDR(reg_cmu)
#define MAX_SEND_RECORD_COUNT 3
2022-08-15 04:20:27 -05:00
#define HAL_SYS_WAKE_LOCK_USER_MCU2CP HAL_SYS_WAKE_LOCK_USER_4
2022-08-15 04:20:27 -05:00
#ifdef CP_API
#define API_POSTFIX _cp
#define MCU2CP_TEXT_LOC CP_TEXT_SRAM_LOC
#define MCU2CP_RODATA_LOC CP_DATA_LOC
#define MCU2CP_BSS_LOC CP_BSS_LOC
#define MCU2CP_BSS_DEF CP_BSS_DEF
2022-08-15 04:20:27 -05:00
#else
#define API_POSTFIX _mcu
2022-08-15 04:20:27 -05:00
#define MCU2CP_TEXT_LOC
#define MCU2CP_RODATA_LOC
#define MCU2CP_BSS_LOC
#define MCU2CP_BSS_DEF CP_BSS_DEF
2022-08-15 04:20:27 -05:00
#endif
#define MCU2CP_API_A(n, p) MCU2CP_TEXT_LOC n##p
#define MCU2CP_API_B(n, p) MCU2CP_API_A(n, p)
#define MCU2CP_API(n) MCU2CP_API_B(n, API_POSTFIX)
2022-08-15 04:20:27 -05:00
enum HAL_MCU2CP_IRQ_TYPE_T {
HAL_MCU2CP_IRQ_SEND_IND,
HAL_MCU2CP_IRQ_RECV_DONE,
2022-08-15 04:20:27 -05:00
HAL_MCU2CP_IRQ_TYPE_QTY
2022-08-15 04:20:27 -05:00
};
struct HAL_MCU2CP_MSG_T {
struct HAL_MCU2CP_MSG_T *next; // pointer to next element in the list
enum HAL_MCU2CP_MSG_TYPE_T type; // message type
unsigned int len; // message data length in bytes
const unsigned char *data; // pointer to the message data
2022-08-15 04:20:27 -05:00
};
struct HAL_MCU2CP_SEND_RECORD_T {
struct HAL_MCU2CP_MSG_T msg;
bool in_use;
2022-08-15 04:20:27 -05:00
};
static const IRQn_Type MCU2CP_RODATA_LOC rx_irq_id[HAL_MCU2CP_ID_QTY] = {
CP2MCU_DATA_IRQn,
CP2MCU_DATA1_IRQn,
};
static const IRQn_Type MCU2CP_RODATA_LOC tx_irq_id[HAL_MCU2CP_ID_QTY] = {
MCU2CP_DONE_IRQn,
MCU2CP_DONE1_IRQn,
};
static const struct HAL_MCU2CP_MSG_T **MCU2CP_BSS_LOC recv_msg_list_p;
2022-08-15 04:20:27 -05:00
static struct HAL_MCU2CP_MSG_T *MCU2CP_BSS_LOC
send_msg_list_p[HAL_MCU2CP_ID_QTY];
static struct HAL_MCU2CP_MSG_T *MCU2CP_BSS_LOC
send_pending_list_p[HAL_MCU2CP_ID_QTY];
2022-08-15 04:20:27 -05:00
static struct HAL_MCU2CP_MSG_T MCU2CP_BSS_LOC
recv_pending_head[HAL_MCU2CP_ID_QTY];
2022-08-15 04:20:27 -05:00
static struct HAL_MCU2CP_SEND_RECORD_T MCU2CP_BSS_LOC
send_msgs[HAL_MCU2CP_ID_QTY][MAX_SEND_RECORD_COUNT];
2022-08-15 04:20:27 -05:00
static HAL_MCU2CP_RX_IRQ_HANDLER MCU2CP_BSS_LOC
rx_irq_handler[HAL_MCU2CP_ID_QTY][HAL_MCU2CP_MSG_TYPE_QTY];
static HAL_MCU2CP_TX_IRQ_HANDLER MCU2CP_BSS_LOC
tx_irq_handler[HAL_MCU2CP_ID_QTY][HAL_MCU2CP_MSG_TYPE_QTY];
2022-08-15 04:20:27 -05:00
static uint8_t MCU2CP_BSS_LOC chan_opened[HAL_MCU2CP_ID_QTY] = {
0,
0,
};
STATIC_ASSERT(sizeof(chan_opened[0]) * 8 >= HAL_MCU2CP_MSG_TYPE_QTY,
"chan_opened size too small");
2022-08-15 04:20:27 -05:00
static bool MCU2CP_BSS_LOC need_flow_ctrl[HAL_MCU2CP_ID_QTY] = {
false,
false,
};
2022-08-15 04:20:27 -05:00
#ifndef CP_API
static bool MCU2CP_BSS_LOC chan_busy[HAL_MCU2CP_ID_QTY] = {
false,
false,
};
2022-08-15 04:20:27 -05:00
static bool MCU2CP_BSS_LOC busy_now = false;
#endif
static struct CMU_T *const MCU2CP_RODATA_LOC cmu = (struct CMU_T *)CMU_BASE;
2022-08-15 04:20:27 -05:00
static void MCU2CP_TEXT_LOC hal_mcu2cp_busy(enum HAL_MCU2CP_ID_T id,
bool busy) {
2022-08-15 04:20:27 -05:00
#ifndef CP_API
int i;
bool new_state;
if (chan_busy[id] == busy) {
return;
}
chan_busy[id] = busy;
if (busy_now == busy) {
return;
}
if (busy) {
hal_sys_wake_lock(HAL_SYS_WAKE_LOCK_USER_MCU2CP);
busy_now = true;
} else {
new_state = false;
for (i = 0; i < HAL_MCU2CP_ID_QTY; i++) {
if (chan_busy[i]) {
new_state = true;
break;
}
2022-08-15 04:20:27 -05:00
}
if (!new_state) {
hal_sys_wake_unlock(HAL_SYS_WAKE_LOCK_USER_MCU2CP);
busy_now = false;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
#endif
}
static int MCU2CP_TEXT_LOC hal_mcu2cp_peer_irq_set(
enum HAL_MCU2CP_ID_T id, enum HAL_MCU2CP_IRQ_TYPE_T type) {
uint32_t value;
2022-08-15 04:20:27 -05:00
#ifdef CP_API
if (id == HAL_MCU2CP_ID_0) {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_CP2MCU_DATA_IND_SET;
2022-08-15 04:20:27 -05:00
} else {
value = CMU_MCU2CP_DATA_DONE_SET;
2022-08-15 04:20:27 -05:00
}
} else {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_CP2MCU_DATA1_IND_SET;
} else {
value = CMU_MCU2CP_DATA1_DONE_SET;
}
}
2022-08-15 04:20:27 -05:00
cmu->CP2MCU_IRQ_SET = value;
2022-08-15 04:20:27 -05:00
#else
if (id == HAL_MCU2CP_ID_0) {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_MCU2CP_DATA_IND_SET;
2022-08-15 04:20:27 -05:00
} else {
value = CMU_CP2MCU_DATA_DONE_SET;
2022-08-15 04:20:27 -05:00
}
} else {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_MCU2CP_DATA1_IND_SET;
} else {
value = CMU_CP2MCU_DATA1_DONE_SET;
}
}
2022-08-15 04:20:27 -05:00
cmu->MCU2CP_IRQ_SET = value;
2022-08-15 04:20:27 -05:00
#endif
return 0;
2022-08-15 04:20:27 -05:00
}
static int MCU2CP_TEXT_LOC hal_mcu2cp_local_irq_clear(
enum HAL_MCU2CP_ID_T id, enum HAL_MCU2CP_IRQ_TYPE_T type) {
uint32_t value;
2022-08-15 04:20:27 -05:00
#ifdef CP_API
if (id == HAL_MCU2CP_ID_0) {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_MCU2CP_DATA_IND_CLR;
2022-08-15 04:20:27 -05:00
} else {
value = CMU_CP2MCU_DATA_DONE_CLR;
}
} else {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_MCU2CP_DATA1_IND_CLR;
} else {
value = CMU_CP2MCU_DATA1_DONE_CLR;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
cmu->MCU2CP_IRQ_CLR = value;
2022-08-15 04:20:27 -05:00
#else
if (id == HAL_MCU2CP_ID_0) {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_CP2MCU_DATA_IND_CLR;
2022-08-15 04:20:27 -05:00
} else {
value = CMU_MCU2CP_DATA_DONE_CLR;
}
} else {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_CP2MCU_DATA1_IND_CLR;
} else {
value = CMU_MCU2CP_DATA1_DONE_CLR;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
cmu->CP2MCU_IRQ_CLR = value;
2022-08-15 04:20:27 -05:00
#endif
return 0;
2022-08-15 04:20:27 -05:00
}
static int MCU2CP_TEXT_LOC hal_mcu2cp_local_irq_set(
enum HAL_MCU2CP_ID_T id, enum HAL_MCU2CP_IRQ_TYPE_T type) {
uint32_t value;
2022-08-15 04:20:27 -05:00
#ifdef CP_API
if (id == HAL_MCU2CP_ID_0) {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_MCU2CP_DATA_IND_SET;
2022-08-15 04:20:27 -05:00
} else {
value = CMU_CP2MCU_DATA_DONE_SET;
}
} else {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_MCU2CP_DATA1_IND_SET;
} else {
value = CMU_CP2MCU_DATA1_DONE_SET;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
cmu->MCU2CP_IRQ_SET = value;
2022-08-15 04:20:27 -05:00
#else
if (id == HAL_MCU2CP_ID_0) {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_CP2MCU_DATA_IND_SET;
2022-08-15 04:20:27 -05:00
} else {
value = CMU_MCU2CP_DATA_DONE_SET;
2022-08-15 04:20:27 -05:00
}
} else {
if (type == HAL_MCU2CP_IRQ_SEND_IND) {
value = CMU_CP2MCU_DATA1_IND_SET;
} else {
value = CMU_MCU2CP_DATA1_DONE_SET;
}
}
2022-08-15 04:20:27 -05:00
cmu->CP2MCU_IRQ_SET = value;
2022-08-15 04:20:27 -05:00
#endif
return 0;
2022-08-15 04:20:27 -05:00
}
static void MCU2CP_TEXT_LOC hal_mcu2cp_rx_irq(void) {
int id;
const struct HAL_MCU2CP_MSG_T *msg_ptr;
enum HAL_MCU2CP_MSG_TYPE_T type;
unsigned int processed;
for (id = HAL_MCU2CP_ID_0; id < HAL_MCU2CP_ID_QTY; id++) {
if (NVIC_GetActive(rx_irq_id[id])) {
hal_mcu2cp_local_irq_clear(id, HAL_MCU2CP_IRQ_SEND_IND);
if (recv_pending_head[id].data) {
// Previous unprocessed message
msg_ptr = &recv_pending_head[id];
} else {
// New message
msg_ptr = recv_msg_list_p[id];
}
while (msg_ptr) {
type = msg_ptr->type;
if (type >= HAL_MCU2CP_MSG_TYPE_QTY) {
// Error
ASSERT(false, "MCU2CP-RX: Invalid msg type: %d", type);
break;
2022-08-15 04:20:27 -05:00
}
if (rx_irq_handler[id][type]) {
processed = rx_irq_handler[id][type](msg_ptr->data, msg_ptr->len);
// Check if flow control needed
if (processed < msg_ptr->len) {
recv_pending_head[id].next = msg_ptr->next;
recv_pending_head[id].type = msg_ptr->type;
recv_pending_head[id].len = msg_ptr->len - processed;
recv_pending_head[id].data = msg_ptr->data + processed;
break;
}
} else {
// Error
ASSERT(false, "MCU2CP-RX: Handler missing");
break;
}
msg_ptr = msg_ptr->next;
}
if (msg_ptr == NULL) {
if (!need_flow_ctrl[id]) {
hal_mcu2cp_peer_irq_set(id, HAL_MCU2CP_IRQ_RECV_DONE);
}
recv_pending_head[id].data = NULL;
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
}
static void MCU2CP_TEXT_LOC hal_mcu2cp_tx_irq(void) {
int id;
struct HAL_MCU2CP_MSG_T *msg_ptr;
enum HAL_MCU2CP_MSG_TYPE_T type;
for (id = HAL_MCU2CP_ID_0; id < HAL_MCU2CP_ID_QTY; id++) {
if (NVIC_GetActive(tx_irq_id[id])) {
hal_mcu2cp_local_irq_clear(id, HAL_MCU2CP_IRQ_RECV_DONE);
msg_ptr = send_msg_list_p[id];
while (msg_ptr) {
type = msg_ptr->type;
if (type >= HAL_MCU2CP_MSG_TYPE_QTY) {
// Error
ASSERT(false, "MCU2CP-TX: Invalid msg type: %d", type);
break;
2022-08-15 04:20:27 -05:00
}
if (tx_irq_handler[id][type]) {
tx_irq_handler[id][type](msg_ptr->data, msg_ptr->len);
};
CONTAINER_OF(msg_ptr, struct HAL_MCU2CP_SEND_RECORD_T, msg)->in_use =
false;
msg_ptr = msg_ptr->next;
}
if (send_pending_list_p[id]) {
send_msg_list_p[id] = send_pending_list_p[id];
send_pending_list_p[id] = NULL;
hal_mcu2cp_peer_irq_set(id, HAL_MCU2CP_IRQ_SEND_IND);
} else {
send_msg_list_p[id] = NULL;
// Allow sleep
hal_mcu2cp_busy(id, false);
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
}
const struct HAL_MCU2CP_MSG_T **hal_mcu2cp_get_send_msg_list_mcu(void);
const struct HAL_MCU2CP_MSG_T **hal_mcu2cp_get_send_msg_list_cp(void);
2022-08-15 04:20:27 -05:00
#ifdef CP_API
// This is initialization code and should NOT be in CP text location
const struct HAL_MCU2CP_MSG_T **hal_mcu2cp_get_send_msg_list_cp(void)
2022-08-15 04:20:27 -05:00
#else
const struct HAL_MCU2CP_MSG_T **hal_mcu2cp_get_send_msg_list_mcu(void)
2022-08-15 04:20:27 -05:00
#endif
{
return (const struct HAL_MCU2CP_MSG_T **)&send_msg_list_p[0];
2022-08-15 04:20:27 -05:00
}
int MCU2CP_API(hal_mcu2cp_open)(enum HAL_MCU2CP_ID_T id,
enum HAL_MCU2CP_MSG_TYPE_T type,
HAL_MCU2CP_RX_IRQ_HANDLER rxhandler,
HAL_MCU2CP_TX_IRQ_HANDLER txhandler,
bool rx_flowctrl) {
int i;
if (id >= HAL_MCU2CP_ID_QTY) {
return 1;
}
if (type >= HAL_MCU2CP_MSG_TYPE_QTY) {
return 2;
}
if (chan_opened[id] == 0) {
hal_mcu2cp_local_irq_clear(id, HAL_MCU2CP_IRQ_SEND_IND);
hal_mcu2cp_local_irq_clear(id, HAL_MCU2CP_IRQ_RECV_DONE);
2022-08-15 04:20:27 -05:00
#ifdef CP_API
recv_msg_list_p = hal_mcu2cp_get_send_msg_list_mcu();
2022-08-15 04:20:27 -05:00
#else
recv_msg_list_p = hal_mcu2cp_get_send_msg_list_cp();
2022-08-15 04:20:27 -05:00
#endif
NVIC_SetVector(rx_irq_id[id], (uint32_t)hal_mcu2cp_rx_irq);
NVIC_SetPriority(rx_irq_id[id], IRQ_PRIORITY_NORMAL);
2022-08-15 04:20:27 -05:00
NVIC_SetVector(tx_irq_id[id], (uint32_t)hal_mcu2cp_tx_irq);
NVIC_SetPriority(tx_irq_id[id], IRQ_PRIORITY_NORMAL);
2022-08-15 04:20:27 -05:00
// Stop IRQs by default
NVIC_DisableIRQ(rx_irq_id[id]);
NVIC_DisableIRQ(tx_irq_id[id]);
2022-08-15 04:20:27 -05:00
send_msg_list_p[id] = NULL;
send_pending_list_p[id] = NULL;
recv_pending_head[id].data = NULL;
for (i = 0; i < MAX_SEND_RECORD_COUNT; i++) {
send_msgs[id][i].in_use = false;
2022-08-15 04:20:27 -05:00
}
need_flow_ctrl[id] = rx_flowctrl;
} else {
ASSERT(need_flow_ctrl[id] == rx_flowctrl,
"MCU2CP-OPEN: rx_flowctrl=%d (should be %d)", rx_flowctrl,
need_flow_ctrl[id]);
return 3;
}
chan_opened[id] |= (1 << type);
rx_irq_handler[id][type] = rxhandler;
tx_irq_handler[id][type] = txhandler;
return 0;
2022-08-15 04:20:27 -05:00
}
int MCU2CP_API(hal_mcu2cp_close)(enum HAL_MCU2CP_ID_T id,
enum HAL_MCU2CP_MSG_TYPE_T type) {
if (id >= HAL_MCU2CP_ID_QTY) {
return 1;
}
2022-08-15 04:20:27 -05:00
chan_opened[id] &= ~(1 << type);
rx_irq_handler[id][type] = NULL;
tx_irq_handler[id][type] = NULL;
2022-08-15 04:20:27 -05:00
if (chan_opened[id] == 0) {
// Stop IRQs by default
NVIC_DisableIRQ(rx_irq_id[id]);
NVIC_DisableIRQ(tx_irq_id[id]);
2022-08-15 04:20:27 -05:00
send_msg_list_p[id] = NULL;
send_pending_list_p[id] = NULL;
recv_pending_head[id].data = NULL;
need_flow_ctrl[id] = false;
}
2022-08-15 04:20:27 -05:00
return 0;
2022-08-15 04:20:27 -05:00
}
int MCU2CP_API(hal_mcu2cp_start_recv)(enum HAL_MCU2CP_ID_T id) {
if (id >= HAL_MCU2CP_ID_QTY) {
return 1;
}
NVIC_EnableIRQ(rx_irq_id[id]);
// Check if there is any previous unprocessed message
if (recv_pending_head[id].data) {
hal_mcu2cp_local_irq_set(id, HAL_MCU2CP_IRQ_SEND_IND);
}
return 0;
2022-08-15 04:20:27 -05:00
}
int MCU2CP_API(hal_mcu2cp_stop_recv)(enum HAL_MCU2CP_ID_T id) {
if (id >= HAL_MCU2CP_ID_QTY) {
return 1;
}
2022-08-15 04:20:27 -05:00
NVIC_DisableIRQ(rx_irq_id[id]);
return 0;
2022-08-15 04:20:27 -05:00
}
int MCU2CP_API(hal_mcu2cp_send)(enum HAL_MCU2CP_ID_T id,
enum HAL_MCU2CP_MSG_TYPE_T type,
const unsigned char *data, unsigned int len) {
uint32_t lock;
int ret;
struct HAL_MCU2CP_SEND_RECORD_T *record;
struct HAL_MCU2CP_MSG_T *next;
int i;
if (id >= HAL_MCU2CP_ID_QTY) {
return 1;
}
if (type >= HAL_MCU2CP_MSG_TYPE_QTY) {
return 2;
}
if ((chan_opened[id] & (1 << type)) == 0) {
return 3;
}
NVIC_EnableIRQ(tx_irq_id[id]);
ret = -1;
record = &send_msgs[id][0];
lock = int_lock();
for (i = 0; i < MAX_SEND_RECORD_COUNT; i++) {
if (record->in_use) {
record++;
continue;
2022-08-15 04:20:27 -05:00
}
record->in_use = true;
record->msg.next = NULL;
record->msg.type = type;
record->msg.len = len;
record->msg.data = data;
if (send_msg_list_p[id] == NULL) {
send_msg_list_p[id] = &record->msg;
hal_mcu2cp_peer_irq_set(id, HAL_MCU2CP_IRQ_SEND_IND);
} else if (send_pending_list_p[id] == NULL) {
send_pending_list_p[id] = &record->msg;
} else {
next = send_pending_list_p[id];
while (next->next) {
next = next->next;
}
next->next = &record->msg;
2022-08-15 04:20:27 -05:00
}
ret = 0;
// Prohibit sleep here
hal_mcu2cp_busy(id, true);
break;
}
int_unlock(lock);
return ret;
2022-08-15 04:20:27 -05:00
}
void MCU2CP_API(hal_mcu2cp_rx_done)(enum HAL_MCU2CP_ID_T id) {
hal_mcu2cp_peer_irq_set(id, HAL_MCU2CP_IRQ_RECV_DONE);
2022-08-15 04:20:27 -05:00
}
int MCU2CP_API(hal_mcu2cp_opened)(enum HAL_MCU2CP_ID_T id) {
return !!chan_opened[id];
2022-08-15 04:20:27 -05:00
}
int MCU2CP_API(hal_mcu2cp_local_irq_pending)(enum HAL_MCU2CP_ID_T id) {
uint32_t value;
2022-08-15 04:20:27 -05:00
#ifdef CP_API
if (id == HAL_MCU2CP_ID_0) {
value = CMU_MCU2CP_DATA_IND_SET | CMU_CP2MCU_DATA_DONE_SET;
} else {
value = CMU_MCU2CP_DATA1_IND_SET | CMU_CP2MCU_DATA1_DONE_SET;
}
2022-08-15 04:20:27 -05:00
return !!(cmu->MCU2CP_IRQ_SET & value);
2022-08-15 04:20:27 -05:00
#else
if (id == HAL_MCU2CP_ID_0) {
value = CMU_CP2MCU_DATA_IND_SET | CMU_MCU2CP_DATA_DONE_SET;
} else {
value = CMU_CP2MCU_DATA1_IND_SET | CMU_MCU2CP_DATA1_DONE_SET;
}
2022-08-15 04:20:27 -05:00
return !!(cmu->CP2MCU_IRQ_SET & value);
2022-08-15 04:20:27 -05:00
#endif
}
#endif