感觉可以正常接收ARP消息,但是正常响应ARP消息没有不知道该怎么验证
MCU:S_T_M_32F103C6T6
SPI链接:
A4(GPIO OUTPUT) CS
A5(SCK) SCK
A6(MISO) SO
A7(MOSI) SI
ENC28J60网口链接:
ENC28J60 -> 路由器 -> 电脑
第一次使用SPI接口,不确定是否是SPI接口链接的不对,或者代码有问题,请帮忙看下,非常感谢!
问题相关代码,请勿粘贴截图
问题代码:
while (1)
{
plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
if(plen==0)
{
continue;
}
print("ok");
if (eth_type_is_arp_and_my_ip(buf, plen))
{
print("arp package????");
make_arp_answer_from_request(buf);
print("arp send ok!^V^!");
continue;
}
if(eth_type_is_ip_and_my_ip(buf,plen)==0)
{
continue;
}
print("receive msg!!!!!!!!!");
if (buf[IP_PROTO_P] == IP_PROTO_ICMP_V) {
print("ICMP ........");
}
//print2("IP_PROTO_P:", (char*)buf);
if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
{
// a ping packet, let's send pong
make_echo_reply_from_request(buf, plen);
//USART_DMASendText(USART1,"make_echo_reply_from_request\n");
continue;
}
}
全部代码:
/**
* @brief The application entry point
* @retval int
*/
#define BUFFER_SIZE 1500//400
static unsigned char buf[BUFFER_SIZE+1];
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick*/
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
//simple_server();
for(int i = 0;i < 20;i++) {
//enc28j60PhyWrite(PHLCON,0x7a4);
HAL_Delay(500);
print("begin init enc28j60");
}
static unsigned char myip[4] = {192,168,1,25};
static unsigned char mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
print("start enc28j60 init");
enc28j60Init(mymac);
print("init enc28j60 success!!!!!!!!!!");
init_ip_arp_udp_tcp(mymac, myip, 80);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
unsigned int plen;
while (1)
{
plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
if(plen==0)
{
continue;
}
print("ok");
if (eth_type_is_arp_and_my_ip(buf, plen))
{
print("arp package????");
make_arp_answer_from_request(buf);
print("arp send ok!^V^!");
continue;
}
if(eth_type_is_ip_and_my_ip(buf,plen)==0)
{
continue;
}
print("receive msg!!!!!!!!!");
if (buf[IP_PROTO_P] == IP_PROTO_ICMP_V) {
print("ICMP ");
//print2("IP_PROTO_P:", (char*)buf);
if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
// a ping packet, let's send pong
make_echo_reply_from_request(buf, plen);
//USART_DMASendText(USART1,"make_echo_reply_from_request\n");
continue;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
运行结果及报错内容
请忽视ok消息,在接收到数据后就会发ok,(不管这个数据包是不是发给ip : 192 168 1 25(本机)的)
正常ping 192 168 1 25可以接收arp包,但是有没有正常响应arp包并不知道,请给一个验证方法,谢谢!
可以确定在同一个网段, 路由器已经关闭DHCP,并且网关设置成了192 168 1 1,电脑IP是: 192 168 1 3。enc28j60 ip:192 168 1 25
我的解答思路和尝试过的方法
ENC28J60驱动是从其他地方获取的,修改过SPI读写操作、CS片选信号高低(ENC28J60_CSL()、ENC28J60_CSH())
ENC28J60.h
#define ENC28J60_CS GPIO_PIN_4
#define ENC28J60_CSL() HAL_GPIO_WritePin(GPIO_ENC28J60, ENC28J60_CS, GPIO_PIN_RESET)
#define ENC28J60_CSH() HAL_GPIO_WritePin(GPIO_ENC28J60, ENC28J60_CS, GPIO_PIN_SET)
ENC28J60.c
static uint8_t Enc28j60Bank;
static uint16_t NextPacketPtr;
unsigned char SPI1_ReadWrite(unsigned char TxData) {
uint8_t Rxdata;
if(HAL_SPI_TransmitReceive(&hspi1, &TxData, &Rxdata, sizeof(TxData), 1000) != HAL_OK) {
//HAL_UART_Transmit(&huart1, (uint8_t*)"err!", 4, 1000);
return Rxdata;
//HAL_UART_Transmit(&huart1, (uint8_t*)"sus!", 4, 1000);
return Rxdata;
}
我想要达到的结果
希望可以正常ping通设备