Files
E3_boot/boards/e3_176_ref/board.c
2025-11-07 10:05:24 +08:00

413 lines
13 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file board.c
* @brief board config source file.
*
* @copyright Copyright (c) 2022 Semidrive Semiconductor.
* All rights reserved.
*/
#include <clock_ip.h>
#include <debug.h>
#include <irq_num.h>
#include <param.h>
#include <part.h>
#include <regs_base.h>
#include <reset_ip.h>
#include <types.h>
#include "board.h"
//#include "app_config.h"
#include "reset_cfg.h"
#if CONFIG_ARCH_WITH_MPU
#include <armv7-r/mpu.h>
#endif
#ifndef CONFIG_CONSOLE_UART
#define CONSOLE_UART UART2
#else
#define CONSOLE_UART CONFIG_CONSOLE_UART
#endif
extern uint32_t __vector;
sdrv_uart_t g_console_uart;
///* Receive buff size */
//#define BUFFER_SIZE 256
///* User data size */
//#define USER_SIZE 64
///* Welcome buff size */
//#define WELCOME_SIZE 256
///* Sync transfer times out value */
//#define TIMES_OUT WaitForever
//// 定义并初始化 SBus 解析结构体
//typedef struct {
// uint16_t channels[16]; // 存储16个通道的解析结果
// uint8_t flags; // 标志字节
//} SBusData;
//
//SBusData sbus_data = {
// .channels = {0},
// .flags = 0
//};
//
//// SBus 数据解析函数
//SBusData* parseSBusData(const uint8_t *input_data)
//{
// // 解析16个通道的数据每个通道为11位
// sbus_data.channels[0] = (input_data[0] | input_data[1] << 8) & 0x07FF;
// sbus_data.channels[1] = (input_data[1] >> 3 | input_data[2] << 5) & 0x07FF;
// sbus_data.channels[2] = (input_data[2] >> 6 | input_data[3] << 2 | input_data[4] << 10) & 0x07FF;
// sbus_data.channels[3] = (input_data[4] >> 1 | input_data[5] << 7) & 0x07FF;
// sbus_data.channels[4] = (input_data[5] >> 4 | input_data[6] << 4) & 0x07FF;
// sbus_data.channels[5] = (input_data[6] >> 7 | input_data[7] << 1 | input_data[8] << 9) & 0x07FF;
// sbus_data.channels[6] = (input_data[8] >> 2 | input_data[9] << 6) & 0x07FF;
// sbus_data.channels[7] = (input_data[9] >> 5 | input_data[10] << 3) & 0x07FF;
// sbus_data.channels[8] = (input_data[11] | input_data[12] << 8) & 0x07FF;
// sbus_data.channels[9] = (input_data[12] >> 3 | input_data[13] << 5) & 0x07FF;
// sbus_data.channels[10] = (input_data[13] >> 6 | input_data[14] << 2 | input_data[15] << 10) & 0x07FF;
// sbus_data.channels[11] = (input_data[15] >> 1 | input_data[16] << 7) & 0x07FF;
// sbus_data.channels[12] = (input_data[16] >> 4 | input_data[17] << 4) & 0x07FF;
// sbus_data.channels[13] = (input_data[17] >> 7 | input_data[18] << 1 | input_data[19] << 9) & 0x07FF;
// sbus_data.channels[14] = (input_data[19] >> 2 | input_data[20] << 6) & 0x07FF;
// sbus_data.channels[15] = (input_data[20] >> 5 | input_data[21] << 3) & 0x07FF;
//
// // 解析标志字节
// sbus_data.flags = input_data[22];
// for(int i=0;i<16;i++)
// {
// ssdk_printf(SSDK_EMERG, "%04x\r\n",sbus_data.channels[i]);
// }
//// ssdk_printf(SSDK_EMERG, "%d\r\n",sbus_data.channels[0]);
// return &sbus_data;
//}
/* Buffer used to receive data from the console */
//uint8_t user_buffer[BUFFER_SIZE];
//
//char test_message[BUFFER_SIZE] =
// "1234567812345678123456781234567812345678123456781234567812345678";
static sdrv_uart_config_t console_uart_config = {
.base = DEVICE_BASE(CONSOLE_UART),
.irq = DEVICE_INTR(CONSOLE_UART),
.baud = 4608000,
.data_bits = SDRV_UART_CHAR_8_BITS,
.stop_bits = SDRV_UART_STOP_1_BIT,
.parity = SDRV_UART_NO_PARITY,
};
/* Set printf putchar interface */
void _putchar(char character)
{
char r_value = '\r';
if (character == '\n') {
sdrv_uart_sync_transmit(&g_console_uart, (uint8_t *)&r_value, 1, NULL,
WaitForever);
}
sdrv_uart_sync_transmit(&g_console_uart, (uint8_t *)&character, 1, NULL,
WaitForever);
}
void board_reset_init(void)
{
uint32_t i = 0;
for (i = 0; i < NUM_OF_RSTGEN_SIGS; i++) {
sdrv_rstgen_reset(board_reset_array[i]);
}
sdrv_recovery_module(&recovery_module_array);
}
/////* Buffer used to receive data from the console */
//typedef struct user_data {
// uint8_t user_buffer[BUFFER_SIZE];
// uint8_t *buff_ptr;
// uint32_t buff_remain;
//} user_data_t;
//static user_data_t user_data;
/////* Buffer used to transmit welcome message */
//char welcome_message[WELCOME_SIZE] =
// "Welcome to E3 SDRV Uart Demo, this demo display how to use the background receive with interrupt method.\r\n\
// Please send a message to E3 SDRV Uart node, the size of the message is not limited.\r\n";
//void callback(sdrv_uart_t *ctrl, sdrv_uart_callback_status_e status,
// void *userData)
//{
// uint8_t char_data[25] = 0;//接收一个字节缓存
// static uint8_t cnt_sbus = 0;
// static uint8_t state = 0;
// static uint8_t receive = 0;
// static uint8_t sbus_buff[25];
// SBusData *tmp_sbus_data;
//
//
// user_data_t *user_data = (user_data_t *)userData;
//
//// ssdk_printf(SSDK_NOTICE, "123456789\r\n");
//
// /* Uart receive data. */
// if (SDRV_UART_RxFWF == status)
// {
// size_t size = sdrv_uart_get_rxfifodata(ctrl, char_data,25);
//
// for(int i=0;i<size;i++)
// {
// switch(state)
// {
// case 0:
// if(0x0f == char_data[i])
// {
// receive = 0;
// cnt_sbus = 0;
// sbus_buff[cnt_sbus] = char_data[i];
// cnt_sbus++;
// state = 1;
// }
// else
// {
// state = 0;
// }
// break;
// case 1:
// if(cnt_sbus >= 24)
// {
// if(0x0 == char_data[i])
// {
// cnt_sbus = 0;
// receive = 0;
// sbus_buff[cnt_sbus] = char_data[i];
// state = 0;
// tmp_sbus_data = parseSBusData(sbus_buff);
// // ssdk_printf(SSDK_EMERG, "AAAr\n");
// }
// }
// else if(0x0f == char_data[i])
// {
// receive = 0;
// cnt_sbus = 0;
// sbus_buff[cnt_sbus] = char_data[i];
// cnt_sbus++;
// state = 1;
// }
// else
// {
// sbus_buff[cnt_sbus] = char_data[i];
// cnt_sbus ++;
// state = 1;
// }
// break;
//
// default:
// break;
// }
// }
// }
// else if (SDRV_UART_RxFifoOverFlow == status)
// {
// if (0 != user_data->buff_remain) {
// size_t size = sdrv_uart_get_rxfifodata(ctrl, user_data->user_buffer,
// user_data->buff_remain);
// }
// sdrv_uart_sync_transmit(ctrl, "\r\n", strlen("\r\n"), NULL, TIMES_OUT);
// sdrv_uart_sync_transmit(ctrl, user_data->user_buffer,
// BUFFER_SIZE - user_data->buff_remain, NULL,
// TIMES_OUT);
// sdrv_uart_sync_transmit(ctrl, "\r\n", strlen("\r\n"), NULL, TIMES_OUT);
//
// memset(user_data->user_buffer, '\0', BUFFER_SIZE);
// user_data->buff_ptr = user_data->user_buffer;
// user_data->buff_remain = BUFFER_SIZE;
//
// sdrv_uart_sync_transmit(
// ctrl, "\r\ncallback: SDRV_UART_RxFifoOverFlow!\r\n",
// strlen("\r\ncallback: SDRV_UART_RxFifoOverFlow!\r\n"), NULL,
// TIMES_OUT);
//
// /* User can stop or reset realtime receive if you want, when transfer
// * occur errors */
// sdrv_uart_stop_realtime_receive(ctrl);
// sdrv_uart_start_realtime_receive(ctrl);
//
// } else if (SDRV_UART_ParityError == status) {
// sdrv_uart_sync_transmit(
// ctrl, "\r\ncallback: SDRV_UART_ParityError!\r\n",
// strlen("\r\ncallback: SDRV_UART_ParityError!\r\n"), NULL,
// TIMES_OUT);
// } else if (SDRV_UART_BaudrateError == status) {
// sdrv_uart_sync_transmit(
// ctrl, "\r\ncallback: SDRV_UART_BaudrateError!\r\n",
// strlen("\r\ncallback: SDRV_UART_BaudrateError!\r\n"), NULL,
// TIMES_OUT);
// } else if (SDRV_UART_NoiseError == status) {
// sdrv_uart_sync_transmit(
// ctrl, "\r\ncallback: SDRV_UART_NoiseError!\r\n",
// strlen("\r\ncallback: SDRV_UART_NoiseError!\r\n"), NULL, TIMES_OUT);
// } else if (SDRV_UART_FramingError == status) {
// sdrv_uart_sync_transmit(
// ctrl, "\r\ncallback: SDRV_UART_FramingError!\r\n",
// strlen("\r\ncallback: SDRV_UART_FramingError!\r\n"), NULL,
// TIMES_OUT);
// } else
// {
//// ssdk_printf(SSDK_NOTICE, "234567891\r\n");
// }
//
//}
/* board debug console uart init */
void board_debug_console_init(void)
{
#if ((CONFIG_E3210) || (CONFIG_E3110))
/* get console uart clk freq */
if (console_uart_config.irq <= UART8_INTR_NUM) {
console_uart_config.clk_freq =
sdrv_ckgen_get_rate(CLK_NODE(g_ckgen_ip_uart_sf_1_to_8));
} else {
console_uart_config.clk_freq =
sdrv_ckgen_get_rate(CLK_NODE(g_ckgen_ip_uart_sf_9_to_16));
}
#else
/* get console uart clk freq */
if (console_uart_config.irq <= UART6_INTR_NUM)
{
console_uart_config.clk_freq =
sdrv_ckgen_get_rate(CLK_NODE(g_ckgen_ip_uart_sf_1_to_6));
}
else
{
console_uart_config.clk_freq =
sdrv_ckgen_get_rate(CLK_NODE(g_ckgen_ip_uart_sf_7_to_12));
}
#endif
/* Initializes sdrv uart controller. */
// sdrv_uart_t uart_ctrl;
// sdrv_uart_t *uart_ctrl_ptr = (sdrv_uart_t *)&uart_ctrl;
// user_data.buff_ptr = user_data.user_buffer;
// user_data.buff_remain = BUFFER_SIZE;
/* initializes uart controller */
sdrv_uart_controller_init(&g_console_uart, &console_uart_config, NULL,
NULL);
sdrv_uart_start_realtime_receive(&g_console_uart);
}
#if CONFIG_ARCH_WITH_MPU
__WEAK const mpu_config_t board_mpu_config[] = {
/* background region, 4GB size, not accessable. */
{0, 0x100000000, MPU_REGION_NO_ACCESS},
#if IRAM1_BASE
#if ((CONFIG_E3210) || (CONFIG_E3110))
/* iram region, normal */
{IRAM1_BASE, 0x100000, MPU_REGION_NORMAL},
#elif (CONFIG_E3104)
/* iram region, normal */
{IRAM1_BASE, 0x40000, MPU_REGION_NORMAL},
#else
/* iram region, normal */
{IRAM1_BASE, 0x80000, MPU_REGION_NORMAL},
#endif
/* iram ecc region, normal */
{IRAM1_ECC_BASE, 0x20000, MPU_REGION_NORMAL},
#endif
/* hsm reg region, device */
{SEIP_SEIP_BASE, 0x100000, MPU_REGION_DEVICE},
/* xspi1 region, for flash, read only */
{XSPI1_BASE, 0x10000000, MPU_REGION_NORMAL_RO},
#if ((CONFIG_E3210) || (CONFIG_E3110))
/* IP module, device */
{0xF0000000, 0x4000000, MPU_REGION_DEVICE},
#else
/* IP module, device */
{0xF0000000, 0x2000000, MPU_REGION_DEVICE},
#endif
/* tcm region, normal */
{CONFIG_ARMV7R_TCMB_BASE, 0x20000, MPU_REGION_NORMAL},
/* int vector, read only */
{(addr_t)&__vector, 0x40, MPU_REGION_NORMAL_RO},
/* int vector, read only */
{0, 0x40, MPU_REGION_NORMAL_RO},
};
__WEAK uint32_t num_of_mpu_cfg = ARRAY_SIZE(board_mpu_config);
void board_mpu_init(void)
{
uint32_t index = 0;
mpu_enable(false);
mpu_clear_region();
for (uint32_t i = 0; i < num_of_mpu_cfg; i++) {
const mpu_config_t *config = &board_mpu_config[i];
if (config->size > 0) {
mpu_add_region(index++, config->addr, config->size, config->type);
}
}
if (index > 0) {
mpu_enable(true);
}
}
#endif
/*
* Call constructor functions.
* This is required when the project has C++ modules.
*/
void call_constructors(void)
{
#if __GNUC__
extern void (*__ctor_list[])(void);
extern void (*__ctor_end[])(void);
void (**ctor)(void);
for (ctor = __ctor_list; ctor != __ctor_end; ctor++)
(*ctor)();
#elif __ICCARM__
extern void __iar_dynamic_initialization(void);
__iar_dynamic_initialization();
#endif
}
void copy_intvec(void)
{
#if ((CONFIG_ARMV7R_USE_TCMB) && (CONFIG_ARMV7R_TCMB_BASE == 0x0))
if ((uint32_t)(&__vector) != (uint32_t)0x0) {
/* copy intvec to tcm */
volatile uint64_t *p_dst = (uint64_t *)0x0;
volatile uint64_t *p_src = (uint64_t *)(&__vector);
for (int i = 0; i < 8; i++) {
*p_dst++ = *p_src++;
}
}
#endif
}