error_t tcpIpStackConfigInterface(NetInterface *interface) { error_t error; //IPv6 specific variables #if (IPV6_SUPPORT == ENABLED) Ipv6Addr solicitedNodeAddr; #endif //Disable Ethernet controller interrupts interface->nicDriver->disableIrq(interface); //Start of exception handling block do { //Receive notifications when the transmitter is ready to send interface->nicTxEvent = osEventCreate(FALSE, FALSE); //Out of resources? if(interface->nicTxEvent == OS_INVALID_HANDLE) { //Report an error error = ERROR_OUT_OF_RESOURCES; //Stop immediately break; } //Receive notifications when a Ethernet frame has been received, //or the link status has changed interface->nicRxEvent = osEventCreate(FALSE, FALSE); //Out of resources? if(interface->nicRxEvent == OS_INVALID_HANDLE) { //Report an error error = ERROR_OUT_OF_RESOURCES; //Stop immediately break; } //Create a mutex to prevent simultaneous access to the NIC driver interface->nicDriverMutex = osMutexCreate(FALSE); //Out of resources? if(interface->nicDriverMutex == OS_INVALID_HANDLE) { //Report an error error = ERROR_OUT_OF_RESOURCES; //Stop immediately break; } //Ethernet controller configuration error = interface->nicDriver->init(interface); //Any error to report? if(error) break; //Ethernet related initialization error = ethInit(interface); //Any error to report? if(error) break; //IPv4 specific initialization #if (IPV4_SUPPORT == ENABLED) //Network layer initialization error = ipv4Init(interface); //Any error to report? if(error) break; //ARP cache initialization error = arpInit(interface); //Any error to report? if(error) break; #if (IGMP_SUPPORT == ENABLED) //IGMP related initialization error = igmpInit(interface); //Any error to report? if(error) break; //Join the all-systems group error = ipv4JoinMulticastGroup(interface, IGMP_ALL_SYSTEMS_ADDR); //Any error to report? if(error) break; #endif #endif //IPv6 specific initialization #if (IPV6_SUPPORT == ENABLED) //Network layer initialization error = ipv6Init(interface); //Any error to report? if(error) break; //Neighbor cache initialization error = ndpInit(interface); //Any error to report? if(error) break; #if (MLD_SUPPORT == ENABLED) ///MLD related initialization error = mldInit(interface); //Any error to report? if(error) break; #endif //Join the All-Nodes multicast address error = ipv6JoinMulticastGroup(interface, &IPV6_LINK_LOCAL_ALL_NODES_ADDR); //Any error to report? if(error) break; //Form the Solicited-Node address for the link-local address error = ipv6ComputeSolicitedNodeAddr(&interface->ipv6Config.linkLocalAddr, &solicitedNodeAddr); //Any error to report? if(error) break; //Join the Solicited-Node multicast group for each assigned address error = ipv6JoinMulticastGroup(interface, &solicitedNodeAddr); //Any error to report? if(error) break; #endif //Create a task to process incoming frames interface->rxTask = osTaskCreate("TCP/IP Stack (RX)", tcpIpStackRxTask, interface, TCP_IP_RX_STACK_SIZE, TCP_IP_RX_PRIORITY); //Unable to create the task? if(interface->rxTask == OS_INVALID_HANDLE) error = ERROR_OUT_OF_RESOURCES; //End of exception handling block } while(0); //Check whether the interface is fully configured if(!error) { //Successful interface configuration interface->configured = TRUE; //Interrupts can be safely enabled interface->nicDriver->enableIrq(interface); } else { //Clean up side effects before returning osEventClose(interface->nicTxEvent); osEventClose(interface->nicRxEvent); osMutexClose(interface->nicDriverMutex); } //Return status code return error; }
void main() { SCB->VTOR = 0x08000000; // set clock source as HSI / 2 * (PLL) 4 RCC->CFGR |= RCC_CFGR_PLLMULL8 | RCC_CFGR_PLLXTPRE_HSE_Div2 /*| RCC_CFGR_PLLSRC*/; RCC->CR |= RCC_CR_PLLON; while (!(RCC->CR & RCC_CR_PLLRDY)); RCC->CFGR |= RCC_CFGR_SW_PLL; while (!(RCC->CFGR & RCC_CFGR_SWS_PLL)); // enable peripherals RCC->APB1ENR = RCC_APB1ENR_TIM2EN; RCC->APB2ENR = RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_ADC1EN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN; AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE; // configure USART IO_ALT_PUSH_PULL(UART_TX); USART1->BRR = USART_BRR(230400); USART1->CR1 = USART_CR1_UE | USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; ENABLE_INTERRUPT(USART1_IRQn); // enable systick SysTick->LOAD = SysTick->VAL = (F_CPU / 1000) / 8; SysTick->CTRL = /*SysTick_CTRL_CLKSOURCE |*/ SysTick_CTRL_ENABLE | SysTick_CTRL_TICKINT; _delay_init(); IO_PUSH_PULL(LED); IO_HIGH(LED); #ifndef ETHERNET_MODULE OW_UART_init(); tempInit(); ioInit(); irInit(); #endif #ifdef ETHERNET ethInit(); #endif uint8_t b; uint32_t lastCheck = 0; for (;;) { // if (IO_IS_LOW(IN1)) dodump = 1; // else dodump = 0; // dodump=1; #ifndef ETHERNET_MODULE ioProcess(); tempProcess(); irProcess(); #endif provTmr(); #ifdef ETHERNET ethProcess(); #endif } }