214 lines
6.2 KiB
C
214 lines
6.2 KiB
C
/*
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
* Copyright (c) 2022, Xiaohua Semiconductor Co., Ltd.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Change Logs:
|
|
* Date Author Notes
|
|
* 2022-04-28 CDT first version
|
|
*/
|
|
|
|
#include <rtdevice.h>
|
|
#include "board_config.h"
|
|
#include "tca9539.h"
|
|
|
|
/**
|
|
* The below functions will initialize HC32 board.
|
|
*/
|
|
|
|
#if defined RT_USING_SERIAL
|
|
rt_err_t rt_hw_board_uart_init(CM_USART_TypeDef *USARTx)
|
|
{
|
|
rt_err_t result = RT_EOK;
|
|
|
|
switch ((rt_uint32_t)USARTx)
|
|
{
|
|
#if defined(BSP_USING_UART1)
|
|
case (rt_uint32_t)CM_USART1:
|
|
/* Configure USART RX/TX pin. */
|
|
GPIO_SetFunc(USART1_RX_PORT, USART1_RX_PIN, GPIO_FUNC_33);
|
|
GPIO_SetFunc(USART1_TX_PORT, USART1_TX_PIN, GPIO_FUNC_32);
|
|
break;
|
|
#endif
|
|
#if defined(BSP_USING_UART6)
|
|
case (rt_uint32_t)CM_USART6:
|
|
/* Configure USART RX/TX pin. */
|
|
GPIO_SetFunc(USART6_RX_PORT, USART6_RX_PIN, GPIO_FUNC_37);
|
|
GPIO_SetFunc(USART6_TX_PORT, USART6_TX_PIN, GPIO_FUNC_36);
|
|
break;
|
|
#endif
|
|
default:
|
|
result = -RT_ERROR;
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
#if defined(RT_USING_ADC)
|
|
rt_err_t rt_hw_board_adc_init(CM_ADC_TypeDef *ADCx)
|
|
{
|
|
rt_err_t result = RT_EOK;
|
|
stc_gpio_init_t stcGpioInit;
|
|
|
|
(void)GPIO_StructInit(&stcGpioInit);
|
|
stcGpioInit.u16PinAttr = PIN_ATTR_ANALOG;
|
|
switch ((rt_uint32_t)ADCx)
|
|
{
|
|
#if defined(BSP_USING_ADC1)
|
|
case (rt_uint32_t)CM_ADC1:
|
|
(void)GPIO_Init(ADC1_CH_PORT, ADC1_CH_PIN, &stcGpioInit);
|
|
break;
|
|
#endif
|
|
#if defined(BSP_USING_ADC2)
|
|
case (rt_uint32_t)CM_ADC2:
|
|
(void)GPIO_Init(ADC2_CH_PORT, ADC2_CH_PIN, &stcGpioInit);
|
|
break;
|
|
#endif
|
|
#if defined(BSP_USING_ADC3)
|
|
case (rt_uint32_t)CM_ADC3:
|
|
(void)GPIO_Init(ADC3_CH_PORT, ADC3_CH_PIN, &stcGpioInit);
|
|
break;
|
|
#endif
|
|
default:
|
|
result = -RT_ERROR;
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
#if defined(RT_USING_CAN)
|
|
void CanPhyEnable(void)
|
|
{
|
|
TCA9539_WritePin(CAN_STB_PORT, CAN_STB_PIN, TCA9539_PIN_RESET);
|
|
TCA9539_ConfigPin(CAN_STB_PORT, CAN_STB_PIN, TCA9539_DIR_OUT);
|
|
}
|
|
rt_err_t rt_hw_board_can_init(CM_CAN_TypeDef *CANx)
|
|
{
|
|
rt_err_t result = RT_EOK;
|
|
|
|
switch ((rt_uint32_t)CANx)
|
|
{
|
|
#if defined(BSP_USING_CAN1)
|
|
case (rt_uint32_t)CM_CAN1:
|
|
GPIO_SetFunc(CAN1_TX_PORT, CAN1_TX_PIN, CAN1_TX_PIN_FUNC);
|
|
GPIO_SetFunc(CAN1_RX_PORT, CAN1_RX_PIN, CAN1_RX_PIN_FUNC);
|
|
break;
|
|
#endif
|
|
#if defined(BSP_USING_CAN2)
|
|
case (rt_uint32_t)CM_CAN2:
|
|
GPIO_SetFunc(CAN2_TX_PORT, CAN2_TX_PIN, CAN2_TX_PIN_FUNC);
|
|
GPIO_SetFunc(CAN2_RX_PORT, CAN2_RX_PIN, CAN2_RX_PIN_FUNC);
|
|
break;
|
|
#endif
|
|
default:
|
|
result = -RT_ERROR;
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
|
|
#if defined (RT_USING_SPI)
|
|
rt_err_t rt_hw_spi_board_init(CM_SPI_TypeDef *CM_SPIx)
|
|
{
|
|
rt_err_t result = RT_EOK;
|
|
|
|
switch ((rt_uint32_t)CM_SPIx)
|
|
{
|
|
#if defined(BSP_USING_SPI1)
|
|
case (rt_uint32_t)CM_SPI1:
|
|
GPIO_SetFunc(GPIO_PORT_C, GPIO_PIN_06, GPIO_FUNC_40); /* SCK */
|
|
GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_13, GPIO_FUNC_41); /* MOSI */
|
|
GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_12, GPIO_FUNC_42); /* MISO */
|
|
break;
|
|
#endif
|
|
default:
|
|
result = -RT_ERROR;
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
#if defined(BSP_USING_ETH)
|
|
/* PHY hardware reset time */
|
|
#define PHY_HW_RST_DELAY (0x40U)
|
|
|
|
rt_err_t rt_hw_eth_phy_reset(CM_ETH_TypeDef *CM_ETHx)
|
|
{
|
|
TCA9539_ConfigPin(TCA9539_IO_PORT1, EIO_ETH_RST, TCA9539_DIR_OUT);
|
|
TCA9539_WritePin(TCA9539_IO_PORT1, EIO_ETH_RST, TCA9539_PIN_RESET);
|
|
rt_thread_mdelay(PHY_HW_RST_DELAY);
|
|
TCA9539_WritePin(TCA9539_IO_PORT1, EIO_ETH_RST, TCA9539_PIN_SET);
|
|
rt_thread_mdelay(PHY_HW_RST_DELAY);
|
|
return RT_EOK;
|
|
}
|
|
|
|
rt_err_t rt_hw_eth_board_init(CM_ETH_TypeDef *CM_ETHx)
|
|
{
|
|
#if defined(ETH_USING_INTERFACE_RMII)
|
|
/*
|
|
ETH_SMI_MDIO ----------------> PA2
|
|
ETH_SMI_MDC -----------------> PC1
|
|
ETH_RMII_TX_EN --------------> PG11
|
|
ETH_RMII_TXD0 ---------------> PG13
|
|
ETH_RMII_TXD1 ---------------> PG14
|
|
ETH_RMII_REF_CLK ------------> PA1
|
|
ETH_RMII_CRS_DV -------------> PA7
|
|
ETH_RMII_RXD0 ---------------> PC4
|
|
ETH_RMII_RXD1 ---------------> PC5
|
|
ETH_RMII_RX_ER --------------> PI10
|
|
*/
|
|
/* Configure PA1, PA2 and PA7 */
|
|
GPIO_SetFunc(GPIO_PORT_A, (GPIO_PIN_01 | GPIO_PIN_02 | GPIO_PIN_07), GPIO_FUNC_11);
|
|
/* Configure PC1, PC4 and PC5 */
|
|
GPIO_SetFunc(GPIO_PORT_C, (GPIO_PIN_01 | GPIO_PIN_04 | GPIO_PIN_05), GPIO_FUNC_11);
|
|
/* Configure PG11, PG13 and PG14 */
|
|
GPIO_SetFunc(GPIO_PORT_G, (GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14), GPIO_FUNC_11);
|
|
/* Configure PI10 */
|
|
GPIO_SetFunc(GPIO_PORT_I, GPIO_PIN_10, GPIO_FUNC_11);
|
|
#else
|
|
/*
|
|
ETH_SMI_MDIO ----------------> PA2
|
|
ETH_SMI_MDC -----------------> PC1
|
|
ETH_MII_TX_CLK --------------> PB6
|
|
ETH_MII_TX_EN ---------------> PG11
|
|
ETH_MII_TXD0 ----------------> PG13
|
|
ETH_MII_TXD1 ----------------> PG14
|
|
ETH_MII_TXD2 ----------------> PB9
|
|
ETH_MII_TXD3 ----------------> PB8
|
|
ETH_MII_RX_CLK --------------> PA1
|
|
ETH_MII_RX_DV ---------------> PA7
|
|
ETH_MII_RXD0 ----------------> PC4
|
|
ETH_MII_RXD1 ----------------> PC5
|
|
ETH_MII_RXD2 ----------------> PB0
|
|
ETH_MII_RXD3 ----------------> PB1
|
|
ETH_MII_RX_ER ---------------> PI10
|
|
ETH_MII_CRS -----------------> PH2
|
|
ETH_MII_COL -----------------> PH3
|
|
*/
|
|
/* Configure PA1, PA2 and PA7 */
|
|
GPIO_SetFunc(GPIO_PORT_A, (GPIO_PIN_01 | GPIO_PIN_02 | GPIO_PIN_07), GPIO_FUNC_11);
|
|
/* Configure PB0, PB1, PB6, PB8 and PB9 */
|
|
GPIO_SetFunc(GPIO_PORT_B, (GPIO_PIN_00 | GPIO_PIN_01 | GPIO_PIN_06 | GPIO_PIN_08 | GPIO_PIN_09), GPIO_FUNC_11);
|
|
/* Configure PC1, PC4 and PC5 */
|
|
GPIO_SetFunc(GPIO_PORT_C, (GPIO_PIN_01 | GPIO_PIN_04 | GPIO_PIN_05), GPIO_FUNC_11);
|
|
/* Configure PG11, PG13 and PG14 */
|
|
GPIO_SetFunc(GPIO_PORT_G, (GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14), GPIO_FUNC_11);
|
|
/* Configure PH2, PH3 */
|
|
GPIO_SetFunc(GPIO_PORT_H, (GPIO_PIN_02 | GPIO_PIN_03), GPIO_FUNC_11);
|
|
/* Configure PI10 */
|
|
GPIO_SetFunc(GPIO_PORT_I, GPIO_PIN_10, GPIO_FUNC_11);
|
|
#endif
|
|
return RT_EOK;
|
|
}
|
|
#endif
|