bool Network_init(NH * nh) { int i = 0; for(;;) { i++; printf("Registering %s... ", DEV_ETHER_NAME); NutSleep(500); if(NutRegisterDevice(&DEV_ETHER, 0, 0) == 0) { printf("ok\n"); break; } printf("Failed, retry\n"); if ( i > 2 ) return false; } int ip_end = rand()%128+1; char ip[12]; sprintf(ip, "10.10.7.%d", ip_end); uint8_t mac[6] = {0x00, 0x06, 0x98, 0x30, 0x00, ip_end}; printf("configuring... "); NutNetIfConfig(DEV_ETHER_NAME, mac, inet_addr(ip), inet_addr(NMASK)); printf("IP: %s\n\n\n", inet_ntoa(inet_addr(ip))); NutSleep(2000); printf("ok\n"); fflush(stdout); printf("Creating Udp socket... "); if ((nh->udpSock = NutUdpCreateSocket(SNMP_PORT)) == 0) { printf("failed\n"); } printf("ok\n"); printf("Creating Udp TRAP socket... "); if ((nh->udpTrapSock = NutUdpCreateSocket(TRAP_PORT)) == 0) { printf("failed\n"); } printf("ok\n"); //allocate room for packets and values SNMP_packetPool = (snmpPacket**)malloc(PACKETPOOL_SIZE * sizeof(snmpPacket*)); for(i = 0; i < PACKETPOOL_SIZE; i++) SNMP_packetPool[i] = (snmpPacket*)malloc(sizeof(snmpPacket)); SNMP_valuePool = (uint8_t*)malloc(VALUEPOOL_SIZE * sizeof(uint8_t)); PP_ptr = 0; VP_ptr = 0; VP_reserved = 0; PP_reserved = 0; SNMP_valuePool[215] = 'G'; //DEBUG //printf("PP_ptr: %d, valuepool[0]: %d, valuepool[215]: %c\n", PP_ptr, SNMP_valuePool[0], SNMP_valuePool[215]); //DEBUG NutThreadCreate("udpReceiveThread", udpReceiveThread, nh, 1024); NutThreadCreate("udpTrapReceiveThread", udpTrapReceiveThread, nh, 756); printf("ok\n"); return true; }
void buttonHandler(int keyvalue) { if(old != keyvalue){ //printf("%d",keyvalue); stateMenu(keyvalue); old = keyvalue; switch (keyvalue) { case KEY_01: NutThreadCreate("Bs", SleepMode, NULL, 512); break; case KEY_02: break; case KEY_03: alarmOn = 1; printf("DEBUG: Beep on\n"); NutThreadCreate("Bq", AlarmBeep,NULL,512); break; case KEY_04: break; case KEY_05: break; case KEY_ALT: printf("Stop alarm"); alarmOn=0; stopStream(); break; case KEY_ESC: break; case KEY_UP: break; case KEY_OK: break; case KEY_LEFT: break; case KEY_DOWN: break; case KEY_RIGHT: break; case KEY_RESET: // powerbutton +alt key printf("reset"); resetEEPROM(); reset(); break; default: break; } } }
/* ����������������������������������������������������������������������� */ int play(FILE *stream) { NutThreadCreate("Bg", StreamPlayer, stream, 512); printf("\nPlay thread created. Device is playing stream now !\n"); return OK; }
THREAD(SleepMode, arg) { struct _tm current_Time; getDateTime(¤t_Time); int temp = current_Time.tm_min; int volume = 0; setGVolume(volume); NutThreadCreate("fs", StreamMusic, NULL, 512); for (;;) { getDateTime(¤t_Time); if (temp > 45) { if (current_Time.tm_min >= temp - 45 && current_Time.tm_min < 45) { stopStream(); break; } } else { if (current_Time.tm_min >= (temp + 15)) { stopStream(); break; } } NutSleep(20000); volume+=1; setGVolume(volume); printf("test3 min:%d", current_Time.tm_min); } }
/*! * \brief Initialize the MP3 player. * * Initializes the decoder and the decoder buffer and starts the * player background thread. */ int ScannerInit(void) { /* Start scanner thread. */ if (NutThreadCreate("scanner", Scanner, 0, 512) == 0) return -1; return 0; }
/* * Main application routine. */ int main(void) { uint32_t baud = 115200; /* * Register the UART device, open it, assign stdout to it and set * the baudrate. */ NutRegisterDevice(&DEV_CONSOLE, 0, 0); freopen(DEV_CONSOLE_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); /* * Print title. */ puts("\nNut/OS Event Queue Demo"); puts("High Main Low "); /* * Post an initial event. This will put the queue into signaled * state and immediately grant the next call to NutEventWait(). */ NutEventPost(&mutex); /* * Start two background threads. */ NutThreadCreate("high", High, 0, 256); NutThreadCreate("low", Low, 0, 256); for(;;) { puts(" Request"); if (NutEventWait(&mutex, 1000)) { puts(" Timeout"); } else { puts(" Acquired"); NutSleep(1500); puts(" Release"); NutEventPost(&mutex); } NutSleep(1000); } return 0; }
static uint8_t dio_start_dev( dev_major* d ) { (void) d; //printf("dio_start_dev\n"); NutThreadCreate("IO", dio_proc, 0, 3024 ); return 0; }
/* ����������������������������������������������������������������������� */ void LcdWriteSecondLine(char text[]) { secondline = text; if(secondlinebool == 0) { NutThreadSetPriority(1); NutThreadCreate("LcdB", SecondLineThread, text, 1024); NutThreadSetPriority(0); } secondlinebool = 1; }
/* ����������������������������������������������������������������������� */ void LcdWriteTitle(char text[]) { title = text; if(titlebool == 0) { NutThreadSetPriority(1); NutThreadCreate("LcdA", TitleThread, text, 1024); NutThreadSetPriority(0); } titlebool = 1; }
THREAD(idle, arg) { NutTimerInit(); NutThreadCreate("main", WebDemo, 0, 768); NutThreadSetPriority(254); for (;;) { #ifdef HEARTBEAT_BIT HeartBeat(); #endif NutThreadYield(); } }
/* * \brief Nut/OS Initialization. * * Initializes the memory management and the thread system and starts * an idle thread, which in turn initializes the timer management. * Finally the application's main() function is called. * * Depending on the compiler, different methods are used to execute this * function before main() is called. * * For ICCAVR the default crtatmega.o startup file is replaced by * crtnut.o, which calls NutInit instead of main(). This is done * by adding the following compiler options in the project: * \code -ucrtnut.o nutinit.o \endcode * * For AVRGCC this function is located in section .init8, which is * called immediately before jumping to main(). NutInit is defined * as: * \code * void NutInit(void) __attribute__ ((naked)) __attribute__ ((section (".init8"))); * \endcode */ void NutInit(void) { /* * We can't use local variables in naked functions. */ #ifdef NUTDEBUG outp(7, UBRR); outp(BV(RXEN) | BV(TXEN), UCR); #endif #ifndef __GNUC__ // FIXME: move this line to an appropriate initialization section of ICCAVR (os) outp(BV(SRE) | BV(SRW), MCUCR); #endif /* First check, whether external RAM is available */ *(NUTRAMEND - 1) = 0x55; *NUTRAMEND = 0xAA; if (*(NUTRAMEND - 1) == 0x55 && *NUTRAMEND == 0xAA) { /* If we have external RAM, initialize stack pointer to * end of external RAM to avoid overwriting .data and .bss section */ SP = (u_short) NUTRAMEND; /* Then add the remaining RAM to heap */ if ((u_short) NUTRAMEND - (u_short) (&__heap_start) > 384) NutHeapAdd(&__heap_start, (u_short) NUTRAMEND - 256 - (u_short) (&__heap_start)); } else { /* No external RAM, so disable external memory interface to use the port pins for normal operation */ MCUCR = 0x00; /* Add the remaining internal RAM to heap */ if ((u_short) RAMEND - (u_short) (&__heap_start) > 384) NutHeapAdd(&__heap_start, (u_short) RAMEND - 256 - (u_short) (&__heap_start)); }; /* * Read eeprom configuration. */ if (NutLoadConfig()) { strcpy(confos.hostname, "ethernut"); NutSaveConfig(); } /* Create idle thread */ NutThreadCreate("idle", NutIdle, 0, 384); }
/* ����������������������������������������������������������������������� */ void enableFallingAsleepMode(void) { menuExit(); if(time > 0) { //Set variable of mode to 1, falling asleep mode enabled fallingAsleepMode = 1; //Set falling asleep time to 1 hour fallingAsleepTime = 60; //Create the thread for falling asleep NutThreadCreate("FallingAsleep", FallingAsleep, NULL, 1024); } }
void Sound_init(UI * ui, bool recording) { // init devices NutLoadConfig(); if (ConfigInit()) { /* No configuration memory, run with factory defaults. */ ConfigResetFactory(); } else { if (ConfigLoad()) { /* No configuration info, use factory defaults. */ ConfigResetFactory(); ConfigSave(); } } ResetDevice(); int i; SoundStruct *ss = malloc( sizeof(SoundStruct) ); ss->buffer = malloc( WIN_SIZE * CEPS_COUNT * sizeof(short) ); ss->ui = ui; ss->recording = recording; ss->ceps_count = 0; ss->dtwTable = malloc( (CEPS_COUNT+1)*sizeof(short*) ); for( i = 0; i< CEPS_COUNT+1; i++) ss->dtwTable[i] = malloc((CEPS_COUNT+1) * sizeof(short)); printf("Create Thread\n"); fflush(stdout); NutThreadCreate("SoundRecord", SoundRecord, (void *)(ss), 2048); NutThreadCreate("SoundFFT", SoundFFT, (void *)(ss), 1024); }
THREAD(AlarmCheckerThread, arg) { for (;;) { NutSleep(5000); if(CheckAlarm1()) { printf("Alarm should go off\n"); startStream(); NutSleep(61000); NutThreadExit(); printf("Exit thread and started new one...\n"); NutThreadCreate("Bl", AlarmCheckerThread, NULL, 512); break; }else{ printf("Alarm should not go off\n"); } if(CheckAlarm2()) { printf("Alarm should go off\n"); if(IMCconfig.alarm2.mode ==0) { NutThreadCreate("Bq", AlarmBeep, NULL, 512); } else{ startStream(); } NutSleep(61000); NutThreadExit(); printf("Exit thread and started new one...\n"); NutThreadCreate("Bl", AlarmCheckerThread, NULL, 512); break; }else{ printf("Alarm should not go off\n"); } } }
/*! * \brief Register discovery telegram responder. * * The first call will activate the responder thread. Any subsequent * calls will return a failure. * * \note Enabling a discovery responder is a potential security hole. * * \param ipmask Update datagrams from remote hosts, which do not fit to * this mask are ignored. Set to INADDR_BROADCAST to allow * any. If zero, no updates are allowed. * \param port The responder will listen to this UDP port. If zero, * the default port 9806 will be used. * \param flags Option value, may contain any combination of DISF_ flags. * * \return 0 if a handler thread had been started, -1 otherwise. */ int NutRegisterDiscovery(u_long ipmask, u_short port, u_int flags) { static HANDLE tid = NULL; if (tid == NULL) { disopt.disopt_ipmask = ipmask; disopt.disopt_port = port ? port : DISCOVERY_PORT; disopt.disopt_flags = flags; tid = NutThreadCreate("udisc", DiscoveryResponder, NULL, NUT_THREAD_DISTSTACK); if (tid) { return 0; } } return -1; }
int start_playing() { if(is_running == 0 ) { is_running = 1; playing = 1; NutThreadCreate("mp3Stream" , PlayMp3StreamThread, NULL, 256); } else if(is_running == 1) { printf("Already running + %s", Description ); return 0; } return 0; }
/*! * \brief Start background thread for display updates. * * \param name Display device name. * * \return 0 on success or -1 in case of a failure. */ int DisplayInit(char *name) { if ((lcd = fopen(name, "w")) == 0) return -1; if ((sline[0] = malloc(DISPLAY_VCOLUMNS + 1)) == 0 || (sline[1] = malloc(DISPLAY_VCOLUMNS + 1)) == 0 || (mline[0] = malloc(DISPLAY_VCOLUMNS + 1)) == 0 || (mline[1] = malloc(DISPLAY_VCOLUMNS + 1)) == 0) return -1; sline[0][DISPLAY_VCOLUMNS] = 0; sline[1][DISPLAY_VCOLUMNS] = 0; if (NutThreadCreate("displ", Displayer, 0, 512) == 0) return -1; DisplayStatus(DIST_NONE); return 0; }
struct sensorNode* initSensors(void) { SENSOR_BINARY(s1, "Przycisk 1", D, 4); SENSOR_BINARY(s2, "Przycisk 2", D, 5); SENSOR_BINARY(s3, "Przycisk 3", D, 6); SENSOR_BINARY(s4, "Przycisk 4", D, 7); SENSOR_ADC(s5, "LM35", 1); SENSOR_I2C(s6, "TC 74", 0x4B, 0x00); LIST_ADD_SENSOR(s1, s2); LIST_ADD_SENSOR(s2, s3); LIST_ADD_SENSOR(s3, s4); LIST_ADD_SENSOR(s4, s5); LIST_ADD_SENSOR(s5, s6); LIST_ADD_SENSOR(s6, NULL); NutThreadCreate("sensors", sensorsReader, s1, 512); return s1; }
int main(int argc, char *argv[]) { tcgetattr(fileno(stdout), &emulation_options.saved_termios); /* get command line options */ emulation_options_parse(argc, argv); /* * Register our Pseudo RAM */ NutHeapAdd(PSEUDO_RAM, PSEUDO_RAM_SIZE); /* Read OS configuration from non-volatile memory. */ NutLoadConfig(); /* * set stdio */ /* NutRegisterDevice(&devUart0, 0, 0); NUT_freopen("uart0", "w", __iob[1]); printf("OS Debug Mode, stdout opened in unix_nutinit.c\n"); // NutTraceOs( stdout, 1); */ /* * Init interrupt handling */ NutIRQInit(); /* * Init threading */ NutThreadInit(); /* * Create idle thread */ NutThreadCreate("idle", NutIdle, 0, NUT_THREAD_IDLESTACK); return 0; }
/* \fn NutIdle(void *arg) * \brief Idle thread. * * After initializing the timers, the idle thread switches to priority 254 * and enters an endless loop. */ THREAD(NutIdle, arg) { /* Initialize system timers. */ NutTimerInit(); /* Create the main application thread. */ NutThreadCreate("main", main, 0, 768); /* * Run in an idle loop at the lowest priority. We can still * do something useful here, like killing terminated threads * or putting the CPU into sleep mode. */ NutThreadSetPriority(254); for (;;) { NutThreadYield(); NutThreadDestroy(); } }
//menu void onMenu(int input) { switch(input) { case KEY_OK: NutThreadCreate("Bg", stream, NULL, 512); break; case KEY_LEFT: stateMenu = offMenu; break; case KEY_RIGHT: stateMenu = offMenu; break; default: break; } clearScreen(); drawMenu(); }
/*! * \brief Emulated idle thread. * * After initializing the timers, the idle thread switches to priority 254 * and enters an endless loop. */ THREAD(NutIdle, arg) { /* Initialize system timers. */ NutTimerInit(); /* Create the main application thread. */ NutThreadCreate("main", NutAppMain, 0, NUT_THREAD_MAINSTACK); // printf("main task created, idling now..\n"); /* * Run in an idle loop at the lowest priority. We can still * do something useful here, like killing terminated threads * or putting the CPU into sleep mode. */ NutThreadSetPriority(254); for (;;) { NutThreadYield(); NutThreadDestroy(); // sleep(); ... sleeping would be fine. } }
/* * Main application routine. * * Nut/OS automatically calls this entry after initialization. */ int main(void) { uint8_t my_mac[] = MY_MAC; /* * Initialize digital I/O. */ init_dio(); #ifdef DEV_ETHER /* * Register Realtek controller at address 8300 hex * and interrupt 5. */ NutRegisterDevice(&DEV_ETHER, 0x8300, 5); /* * Configure lan interface. */ if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000) && NutDhcpIfConfig("eth0", my_mac, 60000)) { /* * No DHCP server available. Use hard coded values. */ uint32_t ip_addr = inet_addr(MY_IP); /* ICCAVR fix. */ NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK)); } /* * Start another service thread to allow * two concurrent connections. */ NutThreadCreate("sback", service_thread, 0, 1384); for (;;) service(); #endif /* DEV_ETHER */ return 0; }
/* * Main application routine. * * Nut/OS automatically calls this entry after initialization. */ int main(void) { TCPSOCKET *sock; CHANNEL cd; uint32_t baud = 9600; /* * Register our devices. */ NutRegisterDevice(&DEV_UART, 0, 0); #ifndef DEV_ETHER for (;;); #else NutRegisterDevice(&DEV_ETHER, 0x8300, 5); /* * Setup the uart device. */ cd.cd_rs232 = fopen(DEV_UART_NAME, "r+b"); _ioctl(_fileno(cd.cd_rs232), UART_SETSPEED, &baud); /* * Setup the ethernet device. Try DHCP first. If this is * the first time boot with empty EEPROM and no DHCP server * was found, use hardcoded values. */ if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) { /* No valid EEPROM contents, use hard coded MAC. */ uint8_t my_mac[] = { 0x00, 0x06, 0x98, 0x20, 0x00, 0x00 }; if (NutDhcpIfConfig("eth0", my_mac, 60000)) { /* No DHCP server found, use hard coded IP address. */ uint32_t ip_addr = inet_addr("192.168.192.100"); uint32_t ip_mask = inet_addr("255.255.255.0"); NutNetIfConfig("eth0", my_mac, ip_addr, ip_mask); /* If not in a local network, we must also call NutIpRouteAdd() to configure the routing. */ } } /* * Start a RS232 receiver thread. */ NutThreadCreate("xmit", Receiver, &cd, 512); /* * Now loop endless for connections. */ cd.cd_connected = 0; for (;;) { /* * Create a socket and listen for a client. */ sock = NutTcpCreateSocket(); NutTcpAccept(sock, TCPPORT); /* * Open a stdio stream assigned to the connected socket. */ cd.cd_tcpip = _fdopen((int) sock, "r+b"); cd.cd_connected = 1; /* * Call RS232 transmit routine. On return we will be * disconnected again. */ StreamCopy(cd.cd_tcpip, cd.cd_rs232, &cd.cd_connected); /* * Close the stream. */ fclose(cd.cd_tcpip); /* * Close our socket. */ NutTcpCloseSocket(sock); } #endif return 0; }
/* * Main application routine. * */ int main(void) { uint32_t baud = 115200; UDPSOCKET *socket; uint32_t ip_addr; uint32_t ip_udp_echo; int rc; int error; int packet_nr; uint16_t length; /* * Initialize the uart device. */ NutRegisterDevice(&DEV_DEBUG, 0, 0); freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); puts("Demo for ICMP support in UDP sockets...\r\n"); #ifdef DEV_ETHER #ifndef NUT_UDP_ICMP_SUPPORT #warning ICMP support for UDP sockets not enabled in the configurator, please enable NUT_UDP_ICMP_SUPPORT puts("ICMP support for UDP sockets not enabled in the configurator\r\n"); puts("Please enable NUT_UDP_ICMP_SUPPORT\r\n"); #endif /* * Register the network device. */ puts("Configuring Ethernet interface"); NutRegisterDevice(&DEV_ETHER, 0, 0); ip_addr = inet_addr(MY_IP); NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK)); NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER); printf("%s ready\r\n", inet_ntoa(ip_addr)); socket = NutUdpCreateSocket(UDP_ECHO_PORT); if (socket == 0) { printf("Could not create UDP socket in port '%d'\r\n", UDP_ECHO_PORT); puts("Demo halted...\r\n"); while (1); } else { printf("Successfully created UDP socket on port '%d'\r\n", UDP_ECHO_PORT); length = UDP_BUFF_SIZE; if (NutUdpSetSockOpt(socket, SO_RCVBUF, &length, sizeof(length))) {; printf("Could not set UDP receive buffer size (%d)\r\n", length); puts("Demo halted...\r\n"); while (1); } } if (NutThreadCreate("RCV", UDPReceiver, (void*)socket, 1024) == 0) { puts("Could not start receiver thread\r\n"); puts("Demo halted...\r\n"); while (1); } else { puts("Receiver thread started\r\n"); } puts("Starting echo test (1 packet / second)\r\n"); ip_udp_echo = inet_addr(UDP_ECHO_IP); packet_nr = 0; while (1) { packet_nr ++; sprintf(send_buffer, "Packet: %d", packet_nr); rc = NutUdpSendTo(socket, ip_udp_echo, UDP_ECHO_PORT, send_buffer, length); printf("--> Sended packet: \"%s\", to %s, rc: %d\r\n", send_buffer, inet_ntoa(ip_udp_echo), rc); if (rc < 0) { uint32_t remote_ip; uint16_t remote_port; error = NutUdpError(socket, &remote_ip, &remote_port); print_udp_icmp_error(remote_ip, remote_port, error); } NutSleep(1000); } #endif /* DEV_ETHER */ return 0; }
int main(void) { //audioInit(); //setVolume(254); WatchDogDisable(); //disable watchdog NutDelay(100); //wait for it to disable SysInitIO(); //initialise input and output SPIinit(); //initialise SPI-registers (speed, mode) LedInit(); //initialise led initLCD(); //initialise lcd Uart0DriverInit(); //initialise Universal asynchronous receiver/transmitter Uart0DriverStart(); //start Uart printf("\n\n\n\nHardware initialization done\n\n"); LogInit(); //initialise ability to log CardInit(); //initialise cardreader printf("\n----------------------------------------------START OF PROGRAM------------------------------------------------------------\n"); if (NutSegBufInit(8192) == 0) { puts("NutSegBufInit: Fatal error"); } /* * Initialize the MP3 decoder hardware. */ if (VsPlayerInit() || VsPlayerReset(0)) { puts("VsPlayer: Fatal error"); } if(X12Init() == -1){ printf("error initializing RTC"); //initialise X12RTC } tm gmt; if (X12RtcGetClock(&gmt) == 0) { LogMsg_P(LOG_INFO, PSTR("local storage time [%02d:%02d:%02d] (in RTC)\n"), gmt.tm_hour, gmt.tm_min, gmt.tm_sec); } if (At45dbInit() == AT45DB041B) { // ...... } RcInit(); //Initialise the Remote Control module KbInit(); //Initialise keyboard initMenu(); //Initialise menu NutThreadCreate("Bh", ButtonHandlerThread, NULL, 512); //create thread to handle button presses printf("\nInitialization done.\n"); initAlarms(); SysControlMainBeat(ON); //enable 4.4 msecs heartbeat interrupt printf("Heartbeat on.\n"); NutThreadSetPriority(1); //Increase our priority so we can feed the watchdog. sei(); //enable global interrupts NutThreadCreate("Bg", TimeSyncThread, NULL, 512); NutThreadCreate("Bq", RefreshSceenThread, NULL, 512); NutThreadCreate("Bl", AlarmCheckerThread, NULL, 512); printf("\nEntering main loop.\n"); LcdBackLight(LCD_BACKLIGHT_ON); for(;;){ NutSleep(100); WatchDogRestart(); //restart watchdog } return(0); }
void playStream(FILE *stream) { NutThreadCreate("Bg", StreamPlayer, stream, 512); printf("Play thread created. Device is playing stream now !\n"); }
/*! \fn Service(void *arg) * \brief HTTP service thread. * * The endless loop in this thread waits for a client connect, * processes the HTTP request and disconnects. Nut/Net doesn't * support a server backlog. If one client has established a * connection, further connect attempts will be rejected. * Typically browsers open more than one connection in order * to load images concurrently. So we run this routine by * several threads. * */ THREAD(Service, arg) { TCPSOCKET *sock; FILE *stream; uint8_t id = (uint8_t) ((uintptr_t) arg); /* * Now loop endless for connections. */ for (;;) { /* * Create a socket. */ if ((sock = NutTcpCreateSocket()) == 0) { printf("[%u] Creating socket failed\n", id); NutSleep(5000); continue; } /* * Listen on port 80. This call will block until we get a connection * from a client. */ NutTcpAccept(sock, 80); #if defined(__AVR__) printf("[%u] Connected, %u bytes free\n", id, NutHeapAvailable()); #else printf("[%u] Connected, %lu bytes free\n", id, NutHeapAvailable()); #endif /* * Wait until at least 8 kByte of free RAM is available. This will * keep the client connected in low memory situations. */ #if defined(__AVR__) while (NutHeapAvailable() < 8192) { #else while (NutHeapAvailable() < 4096) { #endif printf("[%u] Low mem\n", id); NutSleep(1000); } /* * Associate a stream with the socket so we can use standard I/O calls. */ if ((stream = _fdopen((int) ((uintptr_t) sock), "r+b")) == 0) { printf("[%u] Creating stream device failed\n", id); } else { /* * This API call saves us a lot of work. It will parse the * client's HTTP request, send any requested file from the * registered file system or handle CGI requests by calling * our registered CGI routine. */ NutHttpProcessRequest(stream); /* * Destroy the virtual stream device. */ fclose(stream); } /* * Close our socket. */ NutTcpCloseSocket(sock); printf("[%u] Disconnected\n", id); } } #endif /* DEV_ETHER */ /*! * \brief Main application routine. * * Nut/OS automatically calls this entry after initialization. */ int main(void) { uint32_t baud = 115200; uint8_t i; /* * Initialize the uart device. */ NutRegisterDevice(&DEV_DEBUG, 0, 0); freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); NutSleep(200); printf("\n\nNut/OS %s HTTP Daemon...", NutVersionString()); #ifdef DEV_ETHER #ifdef NUTDEBUG NutTraceTcp(stdout, 0); NutTraceOs(stdout, 0); NutTraceHeap(stdout, 0); NutTracePPP(stdout, 0); #endif /* * Register Ethernet controller. */ if (NutRegisterDevice(&DEV_ETHER, 0, 0)) { puts("Registering device failed"); } printf("Configure %s...", DEV_ETHER_NAME); if (NutNetLoadConfig(DEV_ETHER_NAME)) { uint8_t mac[] = MY_MAC; printf("initial boot..."); #ifdef USE_DHCP if (NutDhcpIfConfig(DEV_ETHER_NAME, mac, 60000)) #endif { uint32_t ip_addr = inet_addr(MY_IPADDR); uint32_t ip_mask = inet_addr(MY_IPMASK); uint32_t ip_gate = inet_addr(MY_IPGATE); printf("No DHCP..."); if (NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask) == 0) { /* Without DHCP we had to set the default gateway manually.*/ if(ip_gate) { printf("hard coded gate..."); NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER); } puts("OK"); } else { puts("failed"); } } } else { #ifdef USE_DHCP if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) { puts("failed"); } else { puts("OK"); } #else if (NutNetIfConfig(DEV_ETHER_NAME, 0, 0, confnet.cdn_ip_mask)) { puts("failed"); } else { puts("OK"); } #endif } printf("%s ready\n", inet_ntoa(confnet.cdn_ip_addr)); #ifdef USE_DISCOVERY NutRegisterDiscovery((uint32_t)-1, 0, DISF_INITAL_ANN); #endif /* * Register our device for the file system. */ NutRegisterDevice(&MY_FSDEV, 0, 0); #ifdef MY_BLKDEV /* Register block device. */ printf("Registering block device '" MY_BLKDEV_NAME "'..."); if (NutRegisterDevice(&MY_BLKDEV, 0, 0)) { puts("failed"); for (;;); } puts("OK"); /* Mount partition. */ printf("Mounting block device '" MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME "'..."); if (_open(MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME, _O_RDWR | _O_BINARY) == -1) { puts("failed"); for (;;); } puts("OK"); #endif #ifdef MY_HTTPROOT /* Register root path. */ printf("Registering HTTP root '" MY_HTTPROOT "'..."); if (NutRegisterHttpRoot(MY_HTTPROOT)) { puts("failed"); for (;;); } puts("OK"); #endif NutRegisterCgiBinPath("cgi-bin/;user/cgi-bin/;admin/cgi-bin/"); /* * Register our CGI sample. This will be called * by http://host/cgi-bin/test.cgi?anyparams */ NutRegisterCgi("test.cgi", ShowQuery); #if defined(USE_SSI) /* * Register a cgi included by the ssi demo. This will show how dynamic * content is included in a ssi page and how the request parameters for * a site are passed down to the included cgi. */ NutRegisterCgi("ssi-demo.cgi", SSIDemoCGI); #endif /* * Register some CGI samples, which display interesting * system informations. */ NutRegisterCgi("threads.cgi", ShowThreads); NutRegisterCgi("timers.cgi", ShowTimers); NutRegisterCgi("sockets.cgi", ShowSockets); /* * Finally a CGI example to process a form. */ NutRegisterCgi("form.cgi", ShowForm); /* * Protect the cgi-bin directory with * user and password. */ NutRegisterAuth("admin", "root:root"); NutRegisterAuth("user", "user:user"); /* * Register SSI and ASP handler */ #if defined(USE_SSI) NutRegisterSsi(); #endif #if defined(USE_ASP) NutRegisterAsp(); NutRegisterAspCallback(ASPCallback); #endif /* * Start four server threads. */ for (i = 1; i <= 4; i++) { char thname[] = "httpd0"; thname[5] = '0' + i; NutThreadCreate(thname, Service, (void *) (uintptr_t) i, (HTTPD_SERVICE_STACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD); } #endif /* DEV_ETHER */ /* * We could do something useful here, like serving a watchdog. */ NutThreadSetPriority(254); for (;;) { NutSleep(60000); } return 0; }
/*! * \brief start the alarm check thread * * \author Matthijs */ void startAlarmThread(void) { NutThreadSetPriority(1); NutThreadCreate("AlarmThread1", AlarmThread, NULL, 1024); }
int main(void) { WatchDogDisable(); NutDelay(100); SysInitIO(); SPIinit(); LedInit(); LcdLowLevelInit(); LcdBackLight(1); PrintStr("Starting System"); Uart0DriverInit(); Uart0DriverStart(); LogInit(); CardInit(); char custom[48] = ALL; LoadCustomChars(custom, 6); X12Init(); if (X12RtcGetClock(&gmt) == 0) { LogMsg_P(LOG_INFO, PSTR("RTC time [%02d:%02d:%02d]"), gmt.tm_hour, gmt.tm_min, gmt.tm_sec ); } if (At45dbInit()==AT45DB041B) { // ...... } RcInit(); KbInit(); SysControlMainBeat(ON); // enable 4.4 msecs hartbeat interrupt ClearLcdScreen(); //Nieuwe threads aanmaken NutThreadCreate("t01", Thread1, NULL, 512); NutThreadCreate("time", TimeUpdater, NULL, 512); //Start netwerk int i = initNetworkDHCP(); LogMsg_P(LOG_INFO, PSTR("Ethernet Startup Message: [%d]"),i); //Haal Internet tijd op pgmt = getNTPTime(); X12RtcSetClock(&pgmt); //LogMsg_P(LOG_INFO, PSTR("New RTC time [%02d:%02d:%02d]"), gmt.tm_hour, gmt.tm_min, gmt.tm_sec ); /* * Increase our priority so we can feed the watchdog. */ NutThreadSetPriority(1); /* Enable global interrupts */ sei(); /******************NETWERK**TEST*****************************************************/ //Maak nieuwe socket TCPSOCKET *sock; sock = NutTcpCreateSocket(); //Connect met google.nl LogMsg_P(LOG_INFO, PSTR("Connecting client")); clientConnect(sock); //Zend http req clientSend(sock,buffer,sizeof(buffer)); //Ontvang response in buffer --> grotere buffer (1500)= crash-->heapsize??(8k ram) char rcvbuffer [500]; int rec; rec = clientReceive(sock,rcvbuffer,sizeof(rcvbuffer)); LogMsg_P(LOG_INFO, PSTR("received [%d]"),rec); //0 terminate buffer(string) //rcvbuffer[499] = 0; //Print buffer(http response enz) LogMsg_P(LOG_INFO, PSTR("received [%s]"),rcvbuffer); //Sluit connectie clientClose(sock); /***********************************************************************************/ //Main gui besturing?? for (;;) { NutSleep(100); WatchDogRestart(); } return(0); }