2020-09-23 15:30:05 +08:00
|
|
|
/*
|
2021-03-14 15:33:55 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2020-09-23 15:30:05 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2020-06-24 thread-liu first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
|
|
|
|
#ifdef BSP_USING_OPENAMP
|
|
|
|
|
2020-11-26 15:11:48 +08:00
|
|
|
#include <finsh.h>
|
2020-09-23 15:30:05 +08:00
|
|
|
#include <drv_openamp.h>
|
|
|
|
#include <openamp.h>
|
|
|
|
#include <virt_uart.h>
|
|
|
|
#include <openamp/rpmsg_virtio.h>
|
|
|
|
|
|
|
|
//#define DRV_DEBUG
|
|
|
|
#define LOG_TAG "drv.openamp"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
|
|
|
IPCC_HandleTypeDef hipcc;
|
|
|
|
static VIRT_UART_HandleTypeDef huart0;
|
|
|
|
|
|
|
|
static rt_uint8_t rx_buffer[MAX_BUFFER_SIZE];
|
|
|
|
static rt_uint8_t tx_buffer[MAX_BUFFER_SIZE];
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
struct rthw_openamp
|
|
|
|
{
|
|
|
|
struct rt_device parent;
|
|
|
|
struct rt_openamp serial;
|
|
|
|
struct rt_semaphore sema;
|
|
|
|
};
|
|
|
|
static struct rthw_openamp dev_openamp;
|
|
|
|
|
2021-03-14 15:33:55 +08:00
|
|
|
void IPCC_RX1_IRQHandler(void)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
|
|
|
rt_interrupt_enter();
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
HAL_IPCC_RX_IRQHandler(&hipcc);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPCC_TX1_IRQHandler(void)
|
|
|
|
{
|
|
|
|
rt_interrupt_enter();
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
HAL_IPCC_TX_IRQHandler(&hipcc);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
2021-03-14 15:33:55 +08:00
|
|
|
void VIRT_UART0_RxCpltCallback(VIRT_UART_HandleTypeDef *huart)
|
|
|
|
{
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_uint16_t rx_size = 0, i = 0;
|
|
|
|
rt_size_t count, size, offset;
|
|
|
|
rt_uint8_t *buf = RT_NULL;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
struct rthw_openamp *device;
|
|
|
|
device = (struct rthw_openamp *)rt_device_find("openamp");
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
|
|
|
buf = device->serial.rbuf;
|
2020-09-23 15:30:05 +08:00
|
|
|
count = device->serial.rbuf_count;
|
|
|
|
size = device->serial.rbuf_size;
|
|
|
|
offset = device->serial.rbuf_start + count;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_sem_take(&device->sema, RT_WAITING_FOREVER);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rx_size = (huart->RxXferSize < MAX_BUFFER_SIZE) ? huart->RxXferSize : MAX_BUFFER_SIZE - 1;
|
|
|
|
|
|
|
|
if (count < size)
|
|
|
|
{
|
|
|
|
if (offset >= size)
|
|
|
|
{
|
|
|
|
offset -= size;
|
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
for (i = 0; i < rx_size; i++)
|
|
|
|
{
|
|
|
|
buf[offset++] = huart->pRxBuffPtr[i];
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device->serial.rbuf_count = count;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_sem_release(&device->sema);
|
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
static rt_err_t _init(struct rt_device *dev)
|
|
|
|
{
|
|
|
|
struct rthw_openamp *device;
|
|
|
|
device = (struct rthw_openamp *)dev;
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
device->serial.rbuf_start = 0;
|
|
|
|
device->serial.rbuf_count = 0;
|
|
|
|
device->serial.tbuf_start = 0;
|
|
|
|
device->serial.tbuf_count = 0;
|
|
|
|
device->serial.rbuf_size = MAX_BUFFER_SIZE;
|
|
|
|
device->serial.tbuf_size = MAX_BUFFER_SIZE;
|
|
|
|
device->serial.rbuf = rx_buffer;
|
|
|
|
device->serial.tbuf = tx_buffer;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
if (rt_sem_init(&device->sema, "openamplock", 1, RT_IPC_FLAG_FIFO) != RT_EOK)
|
|
|
|
{
|
2023-03-17 12:12:16 +08:00
|
|
|
return -RT_ERROR;
|
2020-09-23 15:30:05 +08:00
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
|
|
|
rt_size_t count, rbsize, offset;
|
2021-03-14 15:33:55 +08:00
|
|
|
rt_uint8_t *buf = RT_NULL;
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_uint8_t *pBuffer = RT_NULL;
|
|
|
|
rt_uint16_t i = 0;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
struct rthw_openamp *device;
|
|
|
|
device = (struct rthw_openamp *)dev;
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
pBuffer = (unsigned char*)buffer;
|
|
|
|
count = device->serial.rbuf_count;
|
|
|
|
buf = device->serial.rbuf;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_sem_take(&device->sema, RT_WAITING_FOREVER);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
if (count >= size)
|
|
|
|
{
|
|
|
|
count = size;
|
2021-03-14 15:33:55 +08:00
|
|
|
}
|
2020-09-23 15:30:05 +08:00
|
|
|
|
|
|
|
offset = device->serial.rbuf_start;
|
|
|
|
rbsize = device->serial.rbuf_size;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
*pBuffer++ = buf[offset++];
|
|
|
|
if (offset > rbsize)
|
|
|
|
{
|
2021-03-14 15:33:55 +08:00
|
|
|
offset = 0;
|
2020-09-23 15:30:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device->serial.rbuf_start = offset;
|
|
|
|
device->serial.rbuf_count -= count;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_sem_release(&device->sema);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
return count;
|
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
|
|
|
rt_err_t result = VIRT_UART_OK;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
struct rthw_openamp *device;
|
|
|
|
device = (struct rthw_openamp *)dev;
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_sem_take(&device->sema, RT_WAITING_FOREVER);
|
2021-03-14 15:33:55 +08:00
|
|
|
result = VIRT_UART_Transmit(&huart0, (uint8_t *)buffer, size);
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_sem_release(&device->sema);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
if (result != VIRT_UART_OK)
|
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t rt_hw_openamp_register(struct rthw_openamp *openamp, const char *name, rt_uint32_t flag, void *data)
|
|
|
|
{
|
|
|
|
struct rt_device *device;
|
|
|
|
RT_ASSERT(openamp != RT_NULL);
|
|
|
|
|
|
|
|
device = &(openamp->parent);
|
|
|
|
|
|
|
|
device->type = RT_Device_Class_Char;
|
|
|
|
device->rx_indicate = RT_NULL;
|
|
|
|
device->tx_complete = RT_NULL;
|
|
|
|
|
|
|
|
device->init = _init;
|
|
|
|
device->open = RT_NULL;
|
|
|
|
device->close = RT_NULL;
|
|
|
|
device->read = _read;
|
|
|
|
device->write = _write;
|
|
|
|
device->control = RT_NULL;
|
|
|
|
device->user_data = data;
|
|
|
|
|
|
|
|
/* register a character device */
|
|
|
|
return rt_device_register(device, name, flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int openamp_init(void)
|
2021-03-14 15:33:55 +08:00
|
|
|
{
|
2020-09-23 15:30:05 +08:00
|
|
|
extern int MX_OPENAMP_Init(int RPMsgRole, rpmsg_ns_bind_cb ns_bind_cb);
|
|
|
|
|
|
|
|
/* IPCC init */
|
|
|
|
hipcc.Instance = IPCC;
|
|
|
|
if (HAL_IPCC_Init(&hipcc) != HAL_OK)
|
|
|
|
{
|
2023-03-17 12:12:16 +08:00
|
|
|
return -RT_ERROR;
|
2020-09-23 15:30:05 +08:00
|
|
|
}
|
|
|
|
/* openamp slave device */
|
|
|
|
MX_OPENAMP_Init(RPMSG_REMOTE, NULL);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
|
|
|
if (VIRT_UART_Init(&huart0) != VIRT_UART_OK)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
2023-03-17 12:12:16 +08:00
|
|
|
return -RT_ERROR;
|
2020-09-23 15:30:05 +08:00
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
if (VIRT_UART_RegisterCallback(&huart0, VIRT_UART_RXCPLT_CB_ID, VIRT_UART0_RxCpltCallback) != VIRT_UART_OK)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
2023-03-17 12:12:16 +08:00
|
|
|
return -RT_ERROR;
|
2020-09-23 15:30:05 +08:00
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
2020-09-23 15:30:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int rt_hw_openamp_init(void)
|
|
|
|
{
|
|
|
|
openamp_init();
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_hw_openamp_register(&dev_openamp, "openamp", 0, NULL);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2022-10-01 23:36:48 +08:00
|
|
|
if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "openamp") == 0)
|
2020-11-26 15:11:48 +08:00
|
|
|
{
|
|
|
|
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
|
|
|
|
}
|
2020-09-23 15:30:05 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
INIT_PREV_EXPORT(rt_hw_openamp_init);
|
|
|
|
|
2021-03-14 15:33:55 +08:00
|
|
|
static void openamp_thread_entry(void *parameter)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
|
|
|
rt_size_t size = 0;
|
|
|
|
struct rthw_openamp *device = RT_NULL;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
device = (struct rthw_openamp *)rt_device_find("openamp");
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
|
|
|
for (;;)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
|
|
|
OPENAMP_check_for_message();
|
|
|
|
|
|
|
|
size = device->serial.rbuf_count;
|
|
|
|
if (size > 0)
|
|
|
|
{
|
2021-03-14 15:33:55 +08:00
|
|
|
if (device->parent.rx_indicate != RT_NULL)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
|
|
|
device->parent.rx_indicate(&device->parent, size);
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_thread_mdelay(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int creat_openamp_thread(void)
|
|
|
|
{
|
|
|
|
rt_thread_t tid = RT_NULL;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
|
|
|
tid = rt_thread_create("OpenAMP",
|
|
|
|
openamp_thread_entry,
|
|
|
|
RT_NULL,
|
|
|
|
OPENAMP_THREAD_STACK_SIZE,
|
|
|
|
OPENAMP_THREAD_PRIORITY,
|
2020-09-23 15:30:05 +08:00
|
|
|
OPENAMP_THREAD_TIMESLICE);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
|
|
|
if (tid == RT_NULL)
|
2020-09-23 15:30:05 +08:00
|
|
|
{
|
|
|
|
LOG_E("openamp thread create failed!");
|
2023-03-17 12:12:16 +08:00
|
|
|
return -RT_ERROR;
|
2021-03-14 15:33:55 +08:00
|
|
|
}
|
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
rt_thread_startup(tid);
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
INIT_APP_EXPORT(creat_openamp_thread);
|
|
|
|
|
2020-11-26 15:11:48 +08:00
|
|
|
#ifdef FINSH_USING_MSH
|
|
|
|
|
|
|
|
static int console(int argc, char **argv)
|
|
|
|
{
|
|
|
|
rt_err_t result = RT_EOK;
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2020-11-26 15:11:48 +08:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (!strcmp(argv[1], "set"))
|
|
|
|
{
|
|
|
|
rt_kprintf("console change to %s\n", argv[2]);
|
|
|
|
rt_console_set_device(argv[2]);
|
|
|
|
finsh_set_device(argv[2]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("Unknown command. Please enter 'console' for help\n");
|
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("Usage: \n");
|
|
|
|
rt_kprintf("console set <name> - change console by name\n");
|
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
MSH_CMD_EXPORT(console, set console name);
|
|
|
|
|
|
|
|
#endif /* FINSH_USING_MSH */
|
|
|
|
|
2020-09-23 15:30:05 +08:00
|
|
|
#endif
|