314 lines
11 KiB
C
314 lines
11 KiB
C
|
/**
|
||
|
******************************************************************************
|
||
|
* @file at32_msp.c
|
||
|
* @author Artery Technology
|
||
|
* @version V1.0.0
|
||
|
* @date 2020-01-10
|
||
|
* @brief Msp source file
|
||
|
******************************************************************************
|
||
|
* @attention
|
||
|
*
|
||
|
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
||
|
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
||
|
* TIME. AS A RESULT, ARTERYTEK SHALL NOT BE HELD LIABLE FOR ANY
|
||
|
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
||
|
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
||
|
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
||
|
*
|
||
|
* <h2><center>© COPYRIGHT 2018 ArteryTek</center></h2>
|
||
|
******************************************************************************
|
||
|
*/
|
||
|
|
||
|
#include <at32f4xx.h>
|
||
|
#include <rtthread.h>
|
||
|
#include "at32_msp.h"
|
||
|
|
||
|
#ifdef BSP_USING_SERIAL
|
||
|
void at32_msp_usart_init(void *Instance)
|
||
|
{
|
||
|
GPIO_InitType GPIO_InitStruct;
|
||
|
USART_Type *USARTx = (USART_Type *)Instance;
|
||
|
|
||
|
GPIO_StructInit(&GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
|
||
|
#ifdef BSP_USING_UART1
|
||
|
if(USART1 == USARTx)
|
||
|
{
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_USART1, ENABLE);
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_9;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_10;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
}
|
||
|
#endif
|
||
|
#ifdef BSP_USING_UART2
|
||
|
if(USART2 == USARTx)
|
||
|
{
|
||
|
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_USART2, ENABLE);
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_2;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_3;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
}
|
||
|
#endif
|
||
|
/* Add others */
|
||
|
}
|
||
|
#endif /* BSP_USING_SERIAL */
|
||
|
|
||
|
#ifdef BSP_USING_SPI
|
||
|
void at32_msp_spi_init(void *Instance)
|
||
|
{
|
||
|
GPIO_InitType GPIO_InitStruct;
|
||
|
SPI_Type *SPIx = (SPI_Type *)Instance;
|
||
|
|
||
|
GPIO_StructInit(&GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
|
||
|
#ifdef BSP_USING_SPI1
|
||
|
if(SPI1 == SPIx)
|
||
|
{
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_SPI1, ENABLE);
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
|
||
|
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_4;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_5 | GPIO_Pins_7;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_6;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
}
|
||
|
#endif
|
||
|
#ifdef BSP_USING_SPI2
|
||
|
if(SPI2 == SPIx)
|
||
|
{
|
||
|
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_SPI2, ENABLE);
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB, ENABLE);
|
||
|
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_12;
|
||
|
GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_13 | GPIO_Pins_15;
|
||
|
GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_14;
|
||
|
GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||
|
}
|
||
|
#endif
|
||
|
/* Add others */
|
||
|
}
|
||
|
#endif /* BSP_USING_SPI */
|
||
|
|
||
|
#ifdef BSP_USING_SDIO
|
||
|
void at32_msp_sdio_init(void *Instance)
|
||
|
{
|
||
|
GPIO_InitType GPIO_InitStructure;
|
||
|
SDIO_Type *SDIOx = (SDIO_Type *)Instance;
|
||
|
|
||
|
GPIO_StructInit(&GPIO_InitStructure);
|
||
|
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
|
||
|
|
||
|
if(SDIO1 == SDIOx)
|
||
|
{
|
||
|
/* if used dma ... */
|
||
|
RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_DMA2, ENABLE);
|
||
|
|
||
|
RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_SDIO1, ENABLE);
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOC | RCC_APB2PERIPH_GPIOD, ENABLE);
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_8 | GPIO_Pins_9 | GPIO_Pins_10 | GPIO_Pins_11 | GPIO_Pins_12;
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_2;
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||
|
}
|
||
|
}
|
||
|
#endif /* BSP_USING_SDIO */
|
||
|
|
||
|
#ifdef BSP_USING_PWM
|
||
|
void at32_msp_tmr_init(void *Instance)
|
||
|
{
|
||
|
GPIO_InitType GPIO_InitStructure;
|
||
|
TMR_Type *TMRx = (TMR_Type *)Instance;
|
||
|
|
||
|
if(TMRx == TMR1)
|
||
|
{
|
||
|
/* TMR1 clock enable */
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_TMR1, ENABLE);
|
||
|
/* GPIOA clock enable */
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
|
||
|
|
||
|
/* GPIOA Configuration:TMR1 Channel1 as alternate function push-pull */
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_8 | GPIO_Pins_11;
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
|
||
|
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
}
|
||
|
|
||
|
if(TMRx == TMR2)
|
||
|
{
|
||
|
/* TMR2 clock enable */
|
||
|
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR2, ENABLE);
|
||
|
/* GPIOA clock enable */
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
|
||
|
|
||
|
/* GPIOA Configuration:TMR1 Channel1 as alternate function push-pull */
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0;
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
|
||
|
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
}
|
||
|
|
||
|
if(TMRx == TMR3)
|
||
|
{
|
||
|
/* TMR3 clock enable */
|
||
|
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR3, ENABLE);
|
||
|
/* GPIOA clock enable */
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOB, ENABLE);
|
||
|
|
||
|
/* TMR1 Channel1, 2, 3 and 4 as alternate function push-pull */
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_6 | GPIO_Pins_7;
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
|
||
|
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1;
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
|
||
|
|
||
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||
|
}
|
||
|
/* Add others */
|
||
|
}
|
||
|
#endif /* BSP_USING_PWM */
|
||
|
|
||
|
#if defined (BSP_USING_SRAM)
|
||
|
void at32_msp_xmc_init(void *Instance)
|
||
|
{
|
||
|
XMC_Bank1_Type *XMC = (XMC_Bank1_Type *)Instance;
|
||
|
GPIO_InitType GPIO_InitStructure;
|
||
|
(void)XMC;
|
||
|
|
||
|
/* Enable the XMC Clock */
|
||
|
RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_XMC, ENABLE);
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOD | RCC_APB2PERIPH_GPIOG | RCC_APB2PERIPH_GPIOE |
|
||
|
RCC_APB2PERIPH_GPIOF, ENABLE);
|
||
|
/*-- GPIO Configuration ------------------------------------------------------*/
|
||
|
/*!< SRAM Data lines configuration */
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_8 | GPIO_Pins_9 |
|
||
|
GPIO_Pins_10 | GPIO_Pins_14 | GPIO_Pins_15;
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
|
||
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||
|
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_7 | GPIO_Pins_8 | GPIO_Pins_9 | GPIO_Pins_10 |
|
||
|
GPIO_Pins_11 | GPIO_Pins_12 | GPIO_Pins_13 | GPIO_Pins_14 |
|
||
|
GPIO_Pins_15;
|
||
|
GPIO_Init(GPIOE, &GPIO_InitStructure);
|
||
|
|
||
|
/*!< SRAM Address lines configuration */
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3 |
|
||
|
GPIO_Pins_4 | GPIO_Pins_5 | GPIO_Pins_12 | GPIO_Pins_13 |
|
||
|
GPIO_Pins_14 | GPIO_Pins_15;
|
||
|
GPIO_Init(GPIOF, &GPIO_InitStructure);
|
||
|
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3 |
|
||
|
GPIO_Pins_4 | GPIO_Pins_5;
|
||
|
GPIO_Init(GPIOG, &GPIO_InitStructure);
|
||
|
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_11 | GPIO_Pins_12 | GPIO_Pins_13;
|
||
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||
|
|
||
|
/*!< NOE and NWE configuration */
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_4 |GPIO_Pins_5;
|
||
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
||
|
|
||
|
/*!< NE3 configuration */
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_10;
|
||
|
GPIO_Init(GPIOG, &GPIO_InitStructure);
|
||
|
|
||
|
/*!< NBL0, NBL1 configuration */
|
||
|
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1;
|
||
|
GPIO_Init(GPIOE, &GPIO_InitStructure);
|
||
|
|
||
|
}
|
||
|
#endif /* BSP_USING_SRAM */
|
||
|
|
||
|
#ifdef BSP_USING_ADC
|
||
|
void at32_msp_adc_init(void *Instance)
|
||
|
{
|
||
|
GPIO_InitType GPIO_InitStruct;
|
||
|
ADC_Type *ADCx = (ADC_Type *)Instance;
|
||
|
|
||
|
#ifdef BSP_USING_ADC1
|
||
|
if(ADCx == ADC1)
|
||
|
{
|
||
|
/* ADC1 & GPIO clock enable */
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_ADC1 | RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOB | RCC_APB2PERIPH_GPIOC,ENABLE);
|
||
|
|
||
|
/* Configure ADC Channel as analog input */
|
||
|
GPIO_StructInit(&GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3 | GPIO_Pins_4 | GPIO_Pins_5 | GPIO_Pins_6 | GPIO_Pins_7;
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_ANALOG;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
|
||
|
GPIO_StructInit(&GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1;
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_ANALOG;
|
||
|
GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||
|
|
||
|
GPIO_StructInit(&GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3 | GPIO_Pins_4 | GPIO_Pins_5;
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_ANALOG;
|
||
|
GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||
|
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
#ifdef BSP_USING_ADC2
|
||
|
if(ADCx == ADC2)
|
||
|
{
|
||
|
/* ADC2 & GPIO clock enable */
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_ADC2 | RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOB | RCC_APB2PERIPH_GPIOC,ENABLE);
|
||
|
|
||
|
/* Configure ADC Channel as analog input */
|
||
|
GPIO_StructInit(&GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3 | GPIO_Pins_4 | GPIO_Pins_5 | GPIO_Pins_6 | GPIO_Pins_7;
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_ANALOG;
|
||
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
|
||
|
GPIO_StructInit(&GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1;
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_ANALOG;
|
||
|
GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||
|
|
||
|
GPIO_StructInit(&GPIO_InitStruct);
|
||
|
GPIO_InitStruct.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3 | GPIO_Pins_4 | GPIO_Pins_5;
|
||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_ANALOG;
|
||
|
GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
#ifdef BSP_USING_ADC3
|
||
|
if(ADCx == ADC3)
|
||
|
{
|
||
|
/* Add others */
|
||
|
}
|
||
|
#endif
|
||
|
}
|
||
|
#endif /* BSP_USING_ADC */
|