char TWI_SendAddress ( char Address, char TWI_RW ) { char twst; int timer; TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); timer = CLOCK_RegisterCoundowntimer(); if( timer == CLOCK_FAILED ) return FALSE; CLOCK_SetCountdownTimer( timer , TWITIMEOUT, MSECOUND ); while (!(TWCR & (1<<TWINT))) { if ( CLOCK_GetCountdownTimer( timer ) == 0 ) { CLOCK_ReleaseCountdownTimer( timer ); return FALSE; } } CLOCK_ReleaseCountdownTimer( timer ); twst = TWSR & 0xF8; if ((twst != TWI_START) && (twst != TWI_REP_START)) return FALSE; TWDR = (Address<<1) + TWI_RW; TWCR = (1<<TWINT)|(1<<TWEN); timer = CLOCK_RegisterCoundowntimer(); if( timer == CLOCK_FAILED ) return FALSE; CLOCK_SetCountdownTimer( timer , TWITIMEOUT, MSECOUND ); while (!(TWCR & (1<<TWINT))) { if ( CLOCK_GetCountdownTimer( timer ) == 0 ) { CLOCK_ReleaseCountdownTimer( timer ); return FALSE; } } CLOCK_ReleaseCountdownTimer( timer ); twst = TWSR & 0xF8; if ((twst != TWI_MTX_ADR_ACK) && (twst != TWI_MRX_ADR_ACK)) return FALSE; return TRUE; }
char TWI_Write ( char Data ) { int timer; char twst; TWDR = Data; TWCR = (1<<TWINT)|(1<<TWEN); timer = CLOCK_RegisterCoundowntimer(); if( timer == CLOCK_FAILED ) return FALSE; CLOCK_SetCountdownTimer( timer , TWITIMEOUT, MSECOUND ); while (!(TWCR & (1<<TWINT))) { if ( CLOCK_GetCountdownTimer( timer ) == 0 ) { CLOCK_ReleaseCountdownTimer( timer ); return FALSE; } } CLOCK_ReleaseCountdownTimer( timer ); twst = TWSR & 0xF8; if (twst != TWI_MTX_DATA_ACK) return 1; return 0; }
void TWI_SendStart(void) { int timer; TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //make start timer = CLOCK_RegisterCoundowntimer(); if( timer == CLOCK_FAILED ) return; CLOCK_SetCountdownTimer( timer , TWITIMEOUT, MSECOUND ); while (!(TWCR & (1<<TWINT))) { if ( CLOCK_GetCountdownTimer( timer ) == 0 ) { CLOCK_ReleaseCountdownTimer( timer ); return; } } CLOCK_ReleaseCountdownTimer( timer ); if (( TWSR != TWI_START) && ( TWSR != TWI_REP_START)) return; return; }
/*------------------------------------------------------------------------------------------------------------*/ void mp3client_init( void ) { MP3_SOCKET = NO_SOCKET_USED; mp3timer = CLOCK_RegisterCoundowntimer(); MP3_FIFO = Get_FIFO ( mp3_buffer, mp3_buffer_size ); timer0_init( 25 ); timer0_RegisterCallbackFunction( mp3client_stream ); printf_P( PSTR("Tsumani v0.1.3 MP3-streamingengine gestartet \r\n$Id: mp3-streaming.c 67 2008-09-07 11:43:32Z sharan $.\r\n")); }
void TWI_SendStop ( void ) { int timer; TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); timer = CLOCK_RegisterCoundowntimer(); if( timer == CLOCK_FAILED ) return; CLOCK_SetCountdownTimer( timer , TWITIMEOUT, MSECOUND ); while (!(TWCR & (1<<TWINT))) { if ( CLOCK_GetCountdownTimer( timer ) == 0 ) { CLOCK_ReleaseCountdownTimer( timer ); return; } } CLOCK_ReleaseCountdownTimer( timer ); }
char TWI_ReadNack ( void ) { int timer; TWCR = (1<<TWINT)|(1<<TWEN); timer = CLOCK_RegisterCoundowntimer(); if( timer == CLOCK_FAILED ) return FALSE; CLOCK_SetCountdownTimer( timer , TWITIMEOUT, MSECOUND ); while (!(TWCR & (1<<TWINT))) { if ( CLOCK_GetCountdownTimer( timer ) == 0 ) { CLOCK_ReleaseCountdownTimer( timer ); return FALSE; } } CLOCK_ReleaseCountdownTimer( timer ); return TWDR; }
/* Download CEPT-Hypertext Page from Ulm */ int applicationBtxHttpGet(char *url, unsigned long ulmIp ,char *data, int *status) { volatile unsigned int httpServerSocket = NO_SOCKET_USED; char dataByte; char *dataPointer = data; unsigned char timeoutTimerHandle; int dataCount = 0; /* Counter to prevent buffer overflows */ /* Pass 1: Establish server connection */ httpServerSocket = Connect2IP(ulmIp,BTX_ULM_PORT); /* Connect http-server (Ulm) */ if(httpServerSocket == NO_SOCKET_USED) return -1; /* Connection failed */ /* Pass 2: Perform HTTP-Request */ STDOUT_Set_TCP_Socket(httpServerSocket); printf_P(PSTR("GET %s HTTP/1.0\r\nHost: " BTX_ULM_HOST "\r\n\r\n"),url); STDOUT_Flush(); if(httpServerSocket == NO_SOCKET_USED) return -1; /* Connection closed unexpectetly */ timeoutTimerHandle = CLOCK_RegisterCoundowntimer(); /* Register timer */ while(GetBytesInSocketData(httpServerSocket) == 0); /* Trigger on incoming data */ while(GetBytesInSocketData(httpServerSocket) > 0) { dataByte = GetByteFromSocketData(httpServerSocket); if(dataCount < BTX_CEPT_HYPERTEXT_BUFFERSIZE-1) { *dataPointer = dataByte; dataPointer++; dataCount++; } /* Handle connection timeout (3 retrys) */ if(GetBytesInSocketData(httpServerSocket) == 0) { CLOCK_SetCountdownTimer (timeoutTimerHandle, 8, MSECOUND ); while(CLOCK_GetCountdownTimer(timeoutTimerHandle) > 0); if(GetBytesInSocketData(httpServerSocket) == 0) { CLOCK_SetCountdownTimer (timeoutTimerHandle, 16, MSECOUND ); while(CLOCK_GetCountdownTimer(timeoutTimerHandle) > 0); if(GetBytesInSocketData(httpServerSocket) == 0) { CLOCK_SetCountdownTimer (timeoutTimerHandle, 32, MSECOUND ); while(CLOCK_GetCountdownTimer(timeoutTimerHandle) > 0); if(GetBytesInSocketData(httpServerSocket) == 0) break; } } } } CLOCK_ReleaseCountdownTimer(timeoutTimerHandle); /* Free timer */ *dataPointer = '\0'; /* Add a 0 to make the string functions happy */ *status = atoi(&data[8]); /* Calculate status code */ CloseTCPSocket (httpServerSocket); /* Close connection */ STDOUT_Set_RS232(); /* Set STDOUT to console */ return 0; }
/*! \brief Holt von einen Hostname die IP-Adressen * \warning Es ist drauf zu achten das genug Speicher vorgesehen ist fuer die Anworten und das auch Packete mit entsprechender groesse * vom Ethernetmodul empfagen werden koennen und nicht verworfen werden. Siehe MAX_FRAMELEN in enc28j60.h . * \param HOSTNAME Zeiger auf den Hostnamestring der mit 0 teminiert ist. * \retval IP Die IP des Hostname, wenn IP = DNS_NO_ANSWER ist war die Anfrage nicht erfolgreich. Es sollte der DNSserver Eintrag ueberprueft werden * oder die richtigkeit des Hostname. */ unsigned long DNS_ResolveName( char * HOSTNAME ) { int i,UDP_socket; int timer; // udp-puffer anlegen unsigned char * udpbuffer; udpbuffer = (unsigned char*) __builtin_alloca (( size_t ) DNS_BUFFER_LENGHT ); // DNS-struct in udp-puffer anlegen struct DNS_header * DNS_question; DNS_question = ( struct DNS_header *) udpbuffer; // DNS anfrage bauen DNS_question->TransactionID = 0x1acd; DNS_question->Flags = ChangeEndian16bit( 0x0100 ); DNS_question->Questions = ChangeEndian16bit( 1 ); DNS_question->Answer_RRs = 0; DNS_question->Authority_RRs = 0; DNS_question->Additional_RRs = 0; // Hostename für DNS umwandeln, in i steht die länge des neuen strings i = DNS_convertHostName( HOSTNAME, DNS_question->Queries ); DNS_question->Queries[i + 1] = '\0'; DNS_question->Queries[i + 2] = 1; DNS_question->Queries[i + 3] = '\0'; DNS_question->Queries[i + 4] = 1; i = i + 5; // Antwortstruct anlegen struct DNS_answer * DNS_ans; DNS_ans = ( void * ) &DNS_question->Queries[i]; // UDP-Paccket senden UDP_socket = UDP_RegisterSocket( DNSserver , DNS_SERVER_PORT, DNS_BUFFER_LENGHT , udpbuffer); if ( UDP_socket == UDP_SOCKET_ERROR ) return( DNS_NO_ANSWER ); UDP_SendPacket( UDP_socket, DNS_HEADER_LENGHT + i , udpbuffer); // empfang des der DNS-Atwort abwarten timer = CLOCK_RegisterCoundowntimer(); if ( timer == CLOCK_FAILED ) return ( DNS_NO_ANSWER ); CLOCK_SetCountdownTimer ( timer, DNS_REQUEST_TIMEOUT, MSECOUND ); while ( 1 ) { if ( UDP_GetSocketState( UDP_socket ) == UDP_SOCKET_BUSY ) { CLOCK_ReleaseCountdownTimer( timer ); UDP_CloseSocket( UDP_socket ); if ( ( ChangeEndian16bit( DNS_question->Flags ) & 0x000f ) != 0 ) return ( DNS_NO_ANSWER ); break; } if ( CLOCK_GetCountdownTimer( timer ) == 0 ) { CLOCK_ReleaseCountdownTimer( timer ); UDP_CloseSocket( UDP_socket ); return( DNS_NO_ANSWER ); } } // Antwortpacket auseinander nehmen while ( 1 ) { // Wenn noch nicht der Hosteintrag dann nächsten DNS-Answer Datensatz if ( ChangeEndian16bit( DNS_ans->Type ) != A_HOSTNAME ) { i = i + ChangeEndian16bit( DNS_ans->Datalenght ) + DNS_ANSWER_HEADER_LENGHT; DNS_ans = ( void * ) &DNS_question->Queries[i]; } else break; } return( DNS_ans->Adress ); }