/**************************************************************************** * * Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. * * This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics Ltd and MAY NOT * be copied by any method or incorporated into another program without * the express written consent of Aerospace C.Power. This Information or any portion * thereof remains the property of Aerospace C.Power. The Information contained herein * is believed to be accurate and Aerospace C.Power assumes no responsibility or * liability for its use in any way and conveys no license or title under * any patent or copyright and makes no representation or warranty that this * Information is free from patent or copyright infringement. * * ****************************************************************************/ /* os shim includes */ #include "os_types.h" #include "os_task.h" #include "os_utils.h" #include "iot_errno_api.h" /* common includes */ #include "iot_io.h" #include "iot_bitops.h" #include "iot_config.h" /* driver includes */ #include "iot_clock.h" #include "iot_uart.h" #include "iot_i2c_api.h" #include "iot_gpio_api.h" #include "i2c_slv_hw.h" /* cli includes */ #include "iot_cli.h" #include "iot_uart_h.h" /* debug includes*/ #include "dbg_io.h" #include "hw_reg_api.h" #include "mailbox.h" os_task_h test_init_handle; extern int platform_init(); uint32_t mb_get_current_cpu(void); uint32_t current_cpu; uint32_t *current_channel; uint32_t *target_channel; uint32_t target_index = 0; uint32_t target_chan_max = 0; uint32_t current_chan_max = 0; char print_buf[128]; uint8_t dtest_mailbox_uart = 0; uint32_t g_mb_group_1[] = {MB_CHANL_0,MB_CHANL_1,MB_CHANL_2}; uint32_t g_mb_group_0[] = {MB_CHANL_3,MB_CHANL_4,MB_CHANL_5}; int IRAM_ATTR uart_e_try_putc(int port, char c); int uart_e_init(int port); uint32_t os_boot_time32(); /* set *0x500000b0 = 0xffd9000 set *0x50000008 |= 0x20000 set *0x50000004 |= 0x20000 set *0x50000004 &= ~0x20000 */ void print_in_int(const char *err_msg) { while(*err_msg != '\0'){ uart_e_try_putc(dtest_mailbox_uart, *err_msg); ++err_msg; } } void mailbox_receiver(uint32_t ch) { uint32_t data, cnt; cnt = iot_mb_get_space_to_read(ch); iot_sprintf(print_buf, "\r\nMAILBOX-%p-%d:Receive CH=%d, CNT=%d.", (void *)mailbox_receiver, current_cpu, ch, cnt); print_in_int(print_buf); while(cnt--) { iot_mb_read(ch, &data); iot_sprintf(print_buf, "\r\nMAILBOX-%p-%d:DATA#%d=%d.", (void *)mailbox_receiver, current_cpu, cnt, data); print_in_int(print_buf); } return; } void mailbox_test_task() { uint32_t data; current_cpu = mb_get_current_cpu(); if(0 == current_cpu) { dtest_mailbox_uart = 0; current_channel = g_mb_group_0; target_channel = g_mb_group_1; current_chan_max = (sizeof(g_mb_group_0)/sizeof(g_mb_group_0[0])); target_chan_max = (sizeof(g_mb_group_1)/sizeof(g_mb_group_1[0])); } else { dtest_mailbox_uart = 1; current_channel = g_mb_group_1; target_channel = g_mb_group_0; current_chan_max = (sizeof(g_mb_group_1)/sizeof(g_mb_group_1[0])); target_chan_max = (sizeof(g_mb_group_0)/sizeof(g_mb_group_0[0])); } iot_mb_init(); iot_sprintf(print_buf, "\r\nMAILBOX-%p-%d:current_channel=%d,%d,%d, "\ "target_channel=%d,%d,%d.", (void *)mailbox_receiver, current_cpu, current_channel[0], current_channel[1], current_channel[2], target_channel[0], target_channel[1], target_channel[2]); print_in_int(print_buf); target_index = current_chan_max; while(target_index--) { data = iot_mb_receiver_create(current_channel[target_index], current_cpu, mailbox_receiver); if(ERR_OK != data) { iot_sprintf(print_buf, "\r\nMAILBOX-%p-%d:Init failed, CH=%d.", (void *)mailbox_receiver, current_cpu, current_channel[target_index]); } else { iot_sprintf(print_buf ,"\r\nMAILBOX-%p-%d:Init OK, CH=%d", (void *)mailbox_receiver, current_cpu, current_channel[target_index]); } print_in_int(print_buf); } target_index = 0; while(1) { data = os_boot_time32(); iot_sprintf(print_buf ,"\r\nMAILBOX-%p-%d:DATA=%d send to CH=%d.", (void *)mailbox_receiver, current_cpu, data, target_channel[target_index]); print_in_int(print_buf); iot_mb_write(target_channel[target_index], data); if(++target_index >= target_chan_max) { target_index = 0; } os_delay(2000); } return; } void mailbox_test_task_init() { os_task_h handle; iot_printf("mailbox_test_task_init entry....\n"); handle = os_create_task(mailbox_test_task, NULL, 6); if(handle != NULL) { iot_printf("mailbox_test_task create successfully...\n"); } } void mailbox_test_init() { /* init common modules */ iot_bitops_init(); /* init os related modules and utilities */ os_utils_init(); /* gpio matrix enable */ //gpio_mtx_enable(); mailbox_test_task_init(); } void mailbox_init_task(void *arg) { iot_printf("mailbox_init_task entry....\n"); for(;;) { mailbox_test_init(); os_delete_task(test_init_handle); } } int32_t mailbox_task_init() { //uart_e_init(DTEST_MAILBOX_UART); /* start plc lib task */ test_init_handle = os_create_task(mailbox_init_task, NULL, 9); //create the tasks; if(test_init_handle != NULL) { iot_printf("mailbox_init_task init successfully...\n"); } return 0; } int32_t test_task_start() { os_start_kernel(); return 0; } int32_t iot_platform_init() { platform_init(); system_clock_init(); system_uart_init(); dbg_uart_init(); return 0; } int32_t iot_module_init(void) { //platform intialization; iot_platform_init(); //create all the tasks; mailbox_task_init(); iot_printf("starting...\n"); return 0; } #ifdef BARE_METAL_ON_CUR_CPU void os_raw_delay(uint32_t ms) { uint32_t k = ms * 7500; while(k--) { __asm volatile ("nop\n"); } } int main(void) { uint32_t data, addr = (uint32_t)mailbox_receiver; iot_mb_init(); current_cpu = mb_get_current_cpu(); if(0 == current_cpu) { dtest_mailbox_uart = 0; current_channel = g_mb_group_0; target_channel = g_mb_group_1; current_chan_max = (sizeof(g_mb_group_0)/sizeof(g_mb_group_0[0])); target_chan_max = (sizeof(g_mb_group_1)/sizeof(g_mb_group_1[0])); } else { dtest_mailbox_uart = 1; current_channel = g_mb_group_1; target_channel = g_mb_group_0; current_chan_max = (sizeof(g_mb_group_1)/sizeof(g_mb_group_1[0])); target_chan_max = (sizeof(g_mb_group_0)/sizeof(g_mb_group_0[0])); } uart_e_init(dtest_mailbox_uart); iot_sprintf(print_buf, "\r\nMAILBOX-%p-%d:current_channel=%d,%d,%d, "\ "target_channel=%d,%d,%d.", (void *)mailbox_receiver, current_cpu, current_channel[0], current_channel[1], current_channel[2], target_channel[0], target_channel[1], target_channel[2]); print_in_int(print_buf); target_index = current_chan_max; while(target_index--) { data = iot_mb_receiver_create(current_channel[target_index], current_cpu, mailbox_receiver); if(ERR_OK != data) { iot_sprintf(print_buf, "\r\nMAILBOX-%p-%d:Init failed, CH=%d.", (void *)mailbox_receiver, current_cpu, current_channel[target_index]); } else { iot_sprintf(print_buf ,"\r\nMAILBOX-%p-%d:Init OK, CH=%d", (void *)mailbox_receiver, current_cpu, current_channel[target_index]); } print_in_int(print_buf); } print_in_int(print_buf); while(1) { os_raw_delay(2000); for(target_index = 0; target_index < target_chan_max; target_index++) { if(iot_mb_get_space_to_read(current_channel[target_index])) { data = *(uint32_t *)addr; addr += 4; mailbox_receiver(current_channel[target_index]); iot_sprintf(print_buf ,"\r\nMAILBOX-%p-%d:DATA=%d send to CH=%d.", (void *)mailbox_receiver, current_cpu, data, target_channel[target_index]); print_in_int(print_buf); iot_mb_write(target_channel[target_index], data); } else { iot_sprintf(print_buf ,"\r\nMAILBOX-%p-%d:No data. CH=%d", (void *)mailbox_receiver, current_cpu, current_channel[target_index]); print_in_int(print_buf); } } } } #else int main(void) { //module init; iot_module_init(); //module start; test_task_start(); return 0; } #endif