150 lines
5.2 KiB
C
150 lines
5.2 KiB
C
/********************************** (C) COPYRIGHT *******************************
|
||
* File Name : main.c
|
||
* Author : WCH
|
||
* Version : V1.0.0
|
||
* Date : 2022/05/31
|
||
* Description : Main program body.
|
||
*********************************************************************************
|
||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||
* Attention: This software (modified or not) and binary are used for
|
||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||
*******************************************************************************/
|
||
/*
|
||
*@Note
|
||
UDP Server example, demonstrating that the UDP Server receives data and loops back.
|
||
For details on the selection of engineering chips,
|
||
please refer to the "CH32V30x Evaluation Board Manual" under the CH32V307EVT\EVT\PUB folder.
|
||
*/
|
||
|
||
#include <interface_btm.h>
|
||
#include <interface_can.h>
|
||
#include <interface_etherent.h>
|
||
#include <interface_uart.h>
|
||
#include "string.h"
|
||
#include "wchnet.h"
|
||
#include "eth_driver.h"
|
||
#include "main.h"
|
||
#include "ch32v30x_rng.h"
|
||
#include "app_config.h"
|
||
#include "app_test.h"
|
||
#include "app_data_processing.h"
|
||
#include "app_request.h"
|
||
#include "app_param_manage.h"
|
||
//----------------------------------------全局变量
|
||
|
||
ubyte FlashTemp[432] = {0xAA,0xBB,0xCC};//写flash中间值
|
||
|
||
uword ArrWriteEepromBuf[250] = {0};//写flash缓存
|
||
ubyte ETHTxBuf[500] = {0};
|
||
|
||
|
||
//#pragma pack(1)//以1字节对齐,数据强制连续存储 20220926
|
||
|
||
volatile u8 RgCntFlag10ms = 0; /* 10ms标志量 */
|
||
/*********************************************************************
|
||
* @fn IWDG_Init
|
||
*
|
||
* @brief Initializes IWDG.
|
||
*
|
||
* @param IWDG_Prescaler - specifies the IWDG Prescaler value.
|
||
* IWDG_Prescaler_4 - IWDG prescaler set to 4.
|
||
* IWDG_Prescaler_8 - IWDG prescaler set to 8.
|
||
* IWDG_Prescaler_16 - IWDG prescaler set to 16.
|
||
* IWDG_Prescaler_32 - IWDG prescaler set to 32.
|
||
* IWDG_Prescaler_64 - IWDG prescaler set to 64.
|
||
* IWDG_Prescaler_128 - IWDG prescaler set to 128.
|
||
* IWDG_Prescaler_256 - IWDG prescaler set to 256.
|
||
* Reload - specifies the IWDG Reload value.
|
||
* This parameter must be a number between 0 and 0x0FFF.
|
||
*
|
||
* @return none
|
||
*/
|
||
void IWDG_Feed_Init(u16 prer, u16 rlr)
|
||
{
|
||
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
|
||
IWDG_SetPrescaler(prer);
|
||
IWDG_SetReload(rlr);
|
||
IWDG_ReloadCounter();
|
||
IWDG_Enable();
|
||
}
|
||
/*********************************************************************
|
||
* @fn User_GPIO_Init
|
||
*
|
||
* @brief Initializes User_GPIO_Init. IO
|
||
*
|
||
* @return none
|
||
*/
|
||
void User_GPIO_Init(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
||
//---------------------------------------------------------
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);//IO PE输出初始化
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All&0xFFF7;//PE3先不初始化
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_Init(GPIOE, &GPIO_InitStructure);
|
||
GPIO_WriteBit(GPIOE,GPIO_Pin_All&0xFFF7, DF_Low);//设置输出低
|
||
}
|
||
//上电初始化函数,上电时会调用该函数
|
||
//初始化各个模块
|
||
void InitUserMain(void)
|
||
{
|
||
u16 i;
|
||
//------------------------------------------------------------
|
||
WCHNET_GetMacAddr(MACAddr); //get the chip MAC address
|
||
TIM2_Init();
|
||
i = ETH_LibInit(IPAddr, GWIPAddr, IPMask, MACAddr); //Ethernet library initialize
|
||
mStopIfError(i);
|
||
if (i == WCHNET_ERR_SUCCESS)
|
||
WCHNET_CreateUdpSocket();
|
||
CAN1_Mode_Init( CAN_SJW_1tq, CAN_BS2_5tq, CAN_BS1_6tq, 20, CAN_Mode_Normal);//CAN初始化100k 20230704修改为250K,始终频率为系统始终120M/2/20/12=250K
|
||
CAN2_Mode_Init( CAN_SJW_1tq, CAN_BS2_5tq, CAN_BS1_6tq, 5, CAN_Mode_Normal);//CAN初始化1M
|
||
initUart();
|
||
iicInit(I2C2, 100000, 0x02);
|
||
|
||
// TIM3_Init();//步进电机定时器初始化
|
||
|
||
// CAN_Send_Msg(CAN1, 0x11111111, CAN_Id_Extended, CAN_RTR_Data, &UnSwSample_1.ArrData.Arr[0][0], 2);//复位
|
||
|
||
// IWDG_Feed_Init(IWDG_Prescaler_32, 1000); // 56ms内喂狗
|
||
}
|
||
/*********************************************************************
|
||
* @fn main
|
||
*
|
||
* @brief Main program
|
||
*
|
||
* @return none
|
||
*/
|
||
|
||
int main(void)
|
||
{
|
||
//----------------------------------------------------------------------
|
||
initFramework();
|
||
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断分组2.
|
||
Delay_Init();
|
||
USART_Printf_Init(230400); //USART initialize
|
||
printf("UdpServer Test\r\n");
|
||
printf("SystemClk:%d\r\n", SystemCoreClock);
|
||
printf("net version:%x\n", WCHNET_GetVer());
|
||
if ( WCHNET_LIB_VER != WCHNET_GetVer()) {
|
||
printf("version error.\n");
|
||
}
|
||
InitUserMain();//模块初始化
|
||
|
||
bootInterfaceInit();
|
||
appMonitorInit();
|
||
testAppInit();
|
||
feedWatchdog();
|
||
dataProcessingAppInit();
|
||
ethernetInterfaceInit();
|
||
interfaceUartInit();
|
||
requestAppInit();
|
||
// paramAppInit();
|
||
|
||
while(1)
|
||
{
|
||
processMessages();
|
||
}
|
||
}
|
||
|