135 lines
6.0 KiB
C
135 lines
6.0 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
|
|
MAC RAW example, demonstrating that MAC RAW receives and sends data.
|
|
For details on the selection of engineering chips,
|
|
please refer to the "CH32V20x Evaluation Board Manual" under the CH32V20xEVT\EVT\PUB folder.
|
|
*/
|
|
#include "string.h"
|
|
#include "eth_driver.h"
|
|
|
|
extern volatile uint8_t LinkSta;
|
|
uint8_t MACAddr[6]; //MAC address
|
|
uint8_t ARPPackage[]=
|
|
{
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* destination MAC address */
|
|
0x84, 0xc2, 0xe4, 0x01, 0x02, 0x03, /* source MAC address */
|
|
0x08,0x06, /* Type:ARP */
|
|
0x00,0x01, /* Hardware type:Ethernet(1) */
|
|
0x08,0x00, /* Protocol type:IPv4 */
|
|
0x06,0x04, /* Hardware size and Protocol size*/
|
|
0x00,0x01, /* Opcode:request(1) */
|
|
0x84, 0xc2, 0xe4, 0x01, 0x02, 0x03, /* Sender MAC address */
|
|
0xc0,0xa8,0x1,0x0f, /* Sender IP address */
|
|
0x00,0x00,0x00,0x00,0x00,0x00, /* Target MAC address */
|
|
0xc0,0xa8,0x1,0x64, /* Target IP address */
|
|
#if 1 /* padding 16 bytes * line numbers */
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
|
|
0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
|
|
#endif
|
|
};
|
|
|
|
/*********************************************************************
|
|
* @fn TIM2_Init
|
|
*
|
|
* @brief Initializes TIM2.
|
|
*
|
|
* @return none
|
|
*/
|
|
void TIM2_Init(void)
|
|
{
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = { 0 };
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
|
|
|
TIM_TimeBaseStructure.TIM_Period = SystemCoreClock / 1000000;
|
|
TIM_TimeBaseStructure.TIM_Prescaler = WCHNETTIMERPERIOD * 1000 - 1;
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
|
|
|
TIM_Cmd(TIM2, ENABLE);
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
|
NVIC_EnableIRQ(TIM2_IRQn);
|
|
}
|
|
|
|
/*********************************************************************
|
|
* @fn main
|
|
*
|
|
* @brief Main program
|
|
*
|
|
* @return none
|
|
*/
|
|
int main(void)
|
|
{
|
|
u8 i;
|
|
uint32_t PktCnt = 0;
|
|
|
|
Delay_Init();
|
|
USART_Printf_Init(115200); //USART initialize
|
|
printf("MAC_RAW Test\r\n");
|
|
if((SystemCoreClock == 60000000) || (SystemCoreClock == 120000000))
|
|
printf("SystemClk:%d\r\n", SystemCoreClock);
|
|
else
|
|
printf("Error: Please choose 60MHz and 120MHz clock when using Ethernet!\r\n");
|
|
WCHNET_GetMacAddr(MACAddr); //get the chip MAC address
|
|
printf("mac addr:");
|
|
for(i = 0; i < 6; i++)
|
|
printf("%x ", MACAddr[i]);
|
|
printf("\n");
|
|
TIM2_Init();
|
|
ETH_Init(MACAddr);
|
|
/* change source MAC address */
|
|
memcpy(&ARPPackage[6], MACAddr, 6);
|
|
memcpy(&ARPPackage[22], MACAddr, 6);
|
|
|
|
while(1)
|
|
{
|
|
WCHNET_MainTask();
|
|
if(LinkSta)
|
|
{
|
|
MACRAW_Tx(ARPPackage, sizeof(ARPPackage));
|
|
if(++PktCnt % 100 == 0)
|
|
printf("PktCnt:%d\r\n",PktCnt);
|
|
Delay_Ms(100);
|
|
}
|
|
}
|
|
}
|