error_t ksz8051Init(NetInterface *interface) { //Debug message TRACE_INFO("Initializing KSZ8051...\r\n"); //Initialize external interrupt line driver if(interface->extIntDriver != NULL) interface->extIntDriver->init(); //Reset PHY transceiver ksz8051WritePhyReg(interface, KSZ8051_PHY_REG_BMCR, BMCR_RESET); //Wait for the reset to complete while(ksz8051ReadPhyReg(interface, KSZ8051_PHY_REG_BMCR) & BMCR_RESET); //Dump PHY registers for debugging purpose ksz8051DumpPhyReg(interface); //The PHY will generate interrupts when link status changes are detected ksz8051WritePhyReg(interface, KSZ8051_PHY_REG_ICSR, ICSR_LINK_DOWN_IE | ICSR_LINK_UP_IE); //Force the TCP/IP stack to poll the link state at startup interface->phyEvent = TRUE; //Notify the TCP/IP stack of the event osSetEvent(&netEvent); //Successful initialization return NO_ERROR; }
error_t ksz8051Init(NetInterface *interface) { GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //Debug message TRACE_INFO("Initializing KSZ8051...\r\n"); //Enable GPIOB clock RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Enable SYSCFG clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); //Configure PB2 pin as an input GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOB, &GPIO_InitStructure); //Connect EXTI Line2 to PB2 pin SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource2); //Configure EXTI Line2 EXTI_InitStructure.EXTI_Line = EXTI_Line2; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); //Enable EXTI2 interrupts NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //Reset PHY transceiver ksz8051WritePhyReg(interface, KSZ8051_PHY_REG_BMCR, BMCR_RESET); //Wait for the reset to complete while(ksz8051ReadPhyReg(interface, KSZ8051_PHY_REG_BMCR) & BMCR_RESET); //Dump PHY registers for debugging purpose ksz8051DumpPhyReg(interface); //The PHY will generate interrupts when link status changes are detected ksz8051WritePhyReg(interface, KSZ8051_PHY_REG_ICSR, ICSR_LINK_DOWN_IE | ICSR_LINK_UP_IE); //Successful initialization return NO_ERROR; }
error_t ksz8051Init(NetInterface *interface) { //SAM4E-EK evaluation board? #if defined(USE_SAM4E_EK) volatile uint32_t status; //Debug message TRACE_INFO("Initializing KSZ8051...\r\n"); //Enable PIO peripheral clock PMC->PMC_PCER0 = (1 << ID_PIOD); //Enable pull-up resistor on PHY IRQ pin PIOD->PIO_PUER = PIO_PD28; //Configure the corresponding pin as an input PIOD->PIO_ODR = PIO_PD28; PIOD->PIO_PER = PIO_PD28; //Enable interrupts-on-change PIOD->PIO_IDR = 0xFFFFFFFF; PIOD->PIO_IER = PIO_PD28; //Reset PHY transceiver by asserting NRST pin //RSTC->RSTC_MR = RSTC_MR_KEY(0xA5) | RSTC_MR_ERSTL(4); //RSTC->RSTC_CR = RSTC_CR_KEY(0xA5) | RSTC_CR_EXTRST; //Wait for the reset to complete //while(!(RSTC->RSTC_SR & RSTC_SR_NRSTL)); //Delay before accessing PHY transceiver sleep(10); //Read PIO ISR register to clear any pending interrupt status = PIOD->PIO_ISR; //Set priority grouping (2 bits for pre-emption priority, 2 bits for subpriority) NVIC_SetPriorityGrouping(5); //Configure PIOD interrupt priority NVIC_SetPriority(PIOD_IRQn, NVIC_EncodePriority(5, 2, 0)); #else GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; //Debug message TRACE_INFO("Initializing KSZ8051...\r\n"); //Enable GPIOB clock RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Enable SYSCFG clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); //Configure PB2 pin as an input GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOB, &GPIO_InitStructure); //Connect EXTI Line2 to PB2 pin SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource2); //Configure EXTI Line2 EXTI_InitStructure.EXTI_Line = EXTI_Line2; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); //Set priority grouping (2 bits for pre-emption priority, 2 bits for subpriority) NVIC_SetPriorityGrouping(5); //Configure EXTI2 interrupt priority NVIC_SetPriority(EXTI2_IRQn, NVIC_EncodePriority(5, 2, 0)); #endif //Reset PHY transceiver ksz8051WritePhyReg(interface, KSZ8051_PHY_REG_BMCR, BMCR_RESET); //Wait for the reset to complete while(ksz8051ReadPhyReg(interface, KSZ8051_PHY_REG_BMCR) & BMCR_RESET); //Dump PHY registers for debugging purpose ksz8051DumpPhyReg(interface); //The PHY will generate interrupts when link status changes are detected ksz8051WritePhyReg(interface, KSZ8051_PHY_REG_ICSR, ICSR_LINK_DOWN_IE | ICSR_LINK_UP_IE); //Successful initialization return NO_ERROR; }