void timer0_init(const Timer0Registers *regs, const Timer0Prescale _prescale) { registers = regs; prescale = _prescale; // halt all timers timer0_stop(); }
void timer0_init(uint8_t prescaler) { if(prescaler > 5) return; uint8_t temp, mask; cs0[0] = (prescaler & (1 << 0)) >> 0; cs0[1] = (prescaler & (1 << 1)) >> 1; cs0[2] = (prescaler & (1 << 2)) >> 2; timer0_stop(); TCCR0A = (0 << TCW0) | // !enable 16 bit mode (0 << ICEN0) | // !enable capture mode (0 << ICNC0) | // !enable capture mode noise canceller (0 << ICES0) | // !enable capture mode edge select (0 << ACIC0) | // !enable capture mode by analog compare (0 << 2) | // reserved (0 << 1) | // reserved (0 << WGM00); // !waveform generation mode => normal mode mask = (0 << OCIE1D) | // !enable output compare match 1 d interrupt (0 << OCIE1A) | // !enable output compare match 1 a interrupt (0 << OCIE1B) | // !enable output compare match 1 b interrupt (1 << OCIE0A) | // enable output compare match 0 a interrupt (1 << OCIE0B) | // enable output compare match 0 b interrupt (0 << TOIE1) | // !enable timer 1 overflow interrupt (1 << TOIE0) | // enable timer 0 overflow interrupt (1 << TICIE0); // enable timer 0 capture interrupt temp = TIMSK & ~mask; temp |= (0 << OCIE1D) | // !enable output compare match 1 d interrupt (0 << OCIE1A) | // !enable output compare match 1 a interrupt (0 << OCIE1B) | // !enable output compare match 1 b interrupt (1 << OCIE0A) | // enable output compare match 0 a interrupt (1 << OCIE0B) | // enable output compare match 0 b interrupt (0 << TOIE1) | // !enable timer 1 overflow interrupt (1 << TOIE0) | // enable timer 0 overflow interrupt (0 << TICIE0); // !enable timer 0 capture interrupt TIMSK = temp; TIFR = (0 << OCF1D) | // !clear output compare flag 1 d (0 << OCF1A) | // !clear output compare flag 1 a (0 << OCF1B) | // !clear output compare flag 1 b (1 << OCF0A) | // clear output compare flag 0 a (1 << OCF0B) | // clear output compare flag 0 b (0 << TOV1) | // !clear timer 1 overflow flag (1 << TOV0) | // clear timer 0 overflow flag (1 << ICF0); // clear timer 0 input capture flag }
void timer0_start(void) { timer0_stop(); timer0_reset_counter(); TCCR0B = (0 << 7) | // reserved (0 << 6) | // reserved (0 << 5) | // reserved (0 << TSM) | // !timer synchronisation mode (0 << PSR0) | // !prescaler reset (cs0[2] << CS02) | // prescaler (cs0[1] << CS01) | // start timer (cs0[0] << CS00); // }
/*------------------------------------------------------------------------------------------------------------*/ void mp3client_stopstreaming( void ) { timer0_stop(); // stream offen ? if ( MP3_SOCKET != NO_SOCKET_USED ) CloseTCPSocket ( MP3_SOCKET ); // Meldung ausgeben printf_P( PSTR("Connection closed.\r\n") ); // MP3-Puffer leeren Flush_FIFO( MP3_FIFO ); printf_P( PSTR("FIFO flushed.\r\n") ); STDOUT_Flush(); // socket ungültig setzen MP3_SOCKET = NO_SOCKET_USED; }
void timer0_init(void *func, uint32 par) #endif { #if DEBUGSOO > 3 os_printf("timer0_init(%d)\n", flg); #endif timer0_stop(); timer0_cb = func; timer0_arg = par; #ifdef TIMER0_USE_NMI_VECTOR if(nmi_flg) { DPORT_BASE[0] |= 0x0F; } else #endif { DPORT_BASE[0] = 0; ets_isr_attach(ETS_FRC_TIMER0_INUM, _timer0_isr, NULL); } ets_isr_unmask(BIT(ETS_FRC_TIMER0_INUM)); }
/*------------------------------------------------------------------------------------------------------------*/ void mp3client_stream( void ) { static unsigned int temp; char sreg_tmp; timer0_stop(); // Wenn slowstart auf 0 steht Decoder nicht befüttern, sonst warten bis 7/8 voll if ( slowstart == 0 ) VS10xx_flush_from_FIFO( MP3_FIFO ); else if ( Get_Bytes_in_FIFO ( MP3_FIFO ) > ( mp3_buffer_size / 8 ) * 7 ) slowstart = 0; // Verbindung wirklich noch offen if( CheckSocketState( MP3_SOCKET ) == SOCKET_NOT_USE ) { CloseTCPSocket( MP3_SOCKET ); MP3_SOCKET = NO_SOCKET_USED; } else { sreg_tmp = SREG; sei(); // Datenpuffer auffrischen mit neuen daten von TCP-Verbindung while ( GetBytesInSocketData( MP3_SOCKET ) >= MP3TCPtranslen && Get_FIFOrestsize ( MP3_FIFO ) > MP3TCPtranslen && metainfolen == 0 ) { // Wenn die transferlänge zum nächsten Metaint kleiner als translen zwischen tcp und mp3 buffer ist, nur diese // kopieren damit danach der metaint eingelesen werden kann if ( metaint - ( metaintcounter%metaint ) < MP3TCPtranslen && metaint != 0) { LED_on(1); temp = GetSocketDataToFIFO ( MP3_SOCKET, MP3_FIFO, metaint - ( metaintcounter%metaint ) ); totalbytes = totalbytes + temp; metaintcounter = metaintcounter + temp; LED_off(1); } else { LED_on(1); temp = GetSocketDataToFIFO ( MP3_SOCKET, MP3_FIFO, MP3TCPtranslen ); totalbytes = totalbytes + temp; metaintcounter = metaintcounter + temp; LED_off(1); } if ( metaintcounter%metaint == 0 && metaint != 0 ) { // checkt und wartet ob mehr als 1 Byte im Puffer ist while ( GetBytesInSocketData( MP3_SOCKET ) <= 0 ); // lese Byte und errechne die metalänge metainfolen = GetByteFromSocketData ( MP3_SOCKET ) * 16 ; break; } } SREG = sreg_tmp; } timer0_free(); }
/*------------------------------------------------------------------------------------------------------------*/ void mp3client_startstreaming( unsigned char * streamingPROTO, unsigned char * streamingURL, unsigned long streamingIP, unsigned int streamingPORT, unsigned char * streamingFILE, unsigned int socket ) { unsigned int TEMP_MP3_SOCKET = NO_SOCKET_USED; int REQUESTCODE; STDOUT_Set_TCP_Socket ( socket ); // mindestens IP und PORT gesetzt ? if ( streamingIP == 0 || streamingPORT == 0 ) return( 0 ); // schon ein stream offen ? Wenn ja killen if ( MP3_SOCKET != NO_SOCKET_USED ) { printf_P( PSTR("Highjack current stream and kill them on Socket %d\r\n"), MP3_SOCKET ); mp3client_stopstreaming ( ); } printf_P( PSTR("Connect to %s"), streamingURL ); // neue Verbindung auf machen TEMP_MP3_SOCKET = Connect2IP( streamingIP , streamingPORT ); // neue Verbindung geöffnet ? if ( TEMP_MP3_SOCKET != NO_SOCKET_USED ) { printf_P( PSTR(" connected ;-D\r\n"), streamingURL ); } else { printf_P( PSTR(" fail to connect ;-(\r\n"), streamingURL ); } STDOUT_Flush(); // Wenn Verbindung geöffnet, weiter weiter :-) if ( TEMP_MP3_SOCKET != NO_SOCKET_USED ) { STDOUT_Set_TCP_Socket ( TEMP_MP3_SOCKET ); // Request senden printf_P( PSTR("GET /%s HTTP/1.1\r\n" \ "Host: %s\r\n" \ "User-Agent: mp3-streamingclient v0.1a (ccc-berlin)\r\n" \ "Icy-MetaData: %d\r\n" \ "Keep-Alive: \r\n" \ "Connection: TE, Keep-Alive\r\n" \ "TE: trailers\r\n\r\n" ), streamingFILE , streamingFILE , metainfo ); STDOUT_Set_TCP_Socket ( socket ); printf_P( PSTR("Request on TCP-Socket %d\r\n"), TEMP_MP3_SOCKET ); // Stream setzen MP3_SOCKET = TEMP_MP3_SOCKET; // Lautstärke sichern timer0_stop(); vol = VS10xx_read( VS10xx_Register_VOL ); // VS10xx reseten VS10xx_reset( SOFT_RESET ); // gesicherte Lautstärke setzen VS10xx_write( VS10xx_Register_VOL, vol ); printf_P( PSTR("MP3 buffering ( FIFO = %d with %dBytes )\r\n"), MP3_FIFO, mp3_buffer_size ); // Timeout für pufferauffüllen setzen CLOCK_SetCountdownTimer ( mp3timer, 1000, MSECOUND ); // alten MP3-Puffer löschen Flush_FIFO ( MP3_FIFO ); STDOUT_Flush(); STDOUT_Set_RS232 (); REQUESTCODE = mp3client_pharseheader( MP3_SOCKET ); STDOUT_Set_TCP_Socket ( socket ); if ( REQUESTCODE != 200 ) { switch ( REQUESTCODE ) { case -1: printf_P( PSTR("Timeout:") ); break; case 400: printf_P( PSTR("Requestcode: %d Bad Request:"), REQUESTCODE ); break; case 401: printf_P( PSTR("Requestcode: %d Unauthorized:"), REQUESTCODE ); break; case 403: printf_P( PSTR("Requestcode: %d Forbidden:"), REQUESTCODE ); break; case 404: printf_P( PSTR("Requestcode: %d Not Found:"), REQUESTCODE ); break; case 405: printf_P( PSTR("Requestcode: %d Method Not Allowed:"), REQUESTCODE ); break; case 500: printf_P( PSTR("Requestcode: %d Internal Server Error:"), REQUESTCODE ); break; default: printf_P( PSTR("Requestcode: %d Unknow:"), REQUESTCODE ); break; } printf_P( PSTR("Stream failed\r\n") ); STDOUT_Flush(); MP3_SOCKET = NO_SOCKET_USED; return; } // wenn alles schick mit dem stream dann: printf_P( PSTR("Requestcode: %d - Stream okay, Tsunami can highjack them totaly\r\n"), REQUESTCODE ); metaintcounter = 0; slowstart = 1; // Counter setzen für die statsupdates CLOCK_SetCountdownTimer ( mp3timer, 50, MSECOUND ); } STDOUT_Flush(); }
/*------------------------------------------------------------------------------------------------------------*/ void mp3client_thread( void ) { #define bandwidthhis 16 unsigned char i; static unsigned long totalbytesold[bandwidthhis],bandwidth; static unsigned char y; static unsigned int temp; timer0_stop(); STDOUT_Set_RS232 (); if ( MP3_SOCKET == NO_SOCKET_USED ) { // keine Verbindung offen? Trafficcounter zurücksetzen y=0; metaintcounter=0; slowstart = 1; } else { // Verbindung wirklich noch offen if( CheckSocketState( MP3_SOCKET ) == SOCKET_NOT_USE ) { CloseTCPSocket( MP3_SOCKET ); MP3_SOCKET = NO_SOCKET_USED; } else { // Wenn Puffer auf unter 1/8 gelaufen ist slowstart setzen if ( Get_Bytes_in_FIFO ( MP3_FIFO ) < mp3_buffer_size / 8 ) slowstart = 1; /* // Datenpuffer auffrischen mit neuen daten von TCP-Verbindung while ( GetBytesInSocketData( MP3_SOCKET ) >= MP3TCPtranslen && Get_FIFOrestsize ( MP3_FIFO ) > MP3TCPtranslen && metainfolen == 0 ) { // Wenn die transferlänge zum nächsten Metaint kleiner als translen zwischen tcp und mp3 buffer ist, nur diese // kopieren damit danach der metaint eingelesen werden kann if ( metaint - ( metaintcounter%metaint ) < MP3TCPtranslen && metaint != 0) { LED_on(1); temp = GetSocketDataToFIFO ( MP3_SOCKET, MP3_FIFO, metaint - ( metaintcounter%metaint ) ); totalbytes = totalbytes + temp; metaintcounter = metaintcounter + temp; LED_off(1); } else { LED_on(1); temp = GetSocketDataToFIFO ( MP3_SOCKET, MP3_FIFO, MP3TCPtranslen ); totalbytes = totalbytes + temp; metaintcounter = metaintcounter + temp; LED_off(1); } if ( metaintcounter%metaint == 0 && metaint != 0 ) { // checkt und wartet ob mehr als 1 Byte im Puffer ist while ( GetBytesInSocketData( MP3_SOCKET ) <= 0 ); // lese Byte und errechne die metalänge metainfolen = GetByteFromSocketData ( MP3_SOCKET ) * 16 ; break; } } */ // Wenn der metaintcounter auf einen metaint steht, den metaint lesen und ausgeben if ( metainfolen != 0 ) { mp3client_readmetainfo( MP3_SOCKET, metainfolen ); metainfolen = 0; } // Counter schon bei 0, wenn ja stats updaten if ( CLOCK_GetCountdownTimer ( mp3timer ) == 0 ) { bandwidth = 0; // traffic aufaddieren for ( i = 0 ; i < bandwidthhis ; i++) bandwidth = bandwidth + totalbytesold[ i ]; // bandwidth berechnen alle 0,5 sekunden bandwidth = ( bandwidth / bandwidthhis ) * 2; // stats sichern mp3clientupdate( bandwidth, GetBytesInSocketData( MP3_SOCKET ) , Get_Bytes_in_FIFO ( MP3_FIFO ), VS10xx_get_decodetime( ), verboselevel ); // wenn verboselevel gesetzt ausgeben if ( verboselevel != 0) printf_P( PSTR("\r%5d(TCP %4d)Bytes on FIFO, bandwidth:%3ldkbit/s(%2dkb/s)"), Get_Bytes_in_FIFO ( MP3_FIFO ), GetBytesInSocketData( MP3_SOCKET ), ( bandwidth * 8 ) / 1024, ( bandwidth * 8 ) / 8192 ); // zähler für bandwidth zurücksetzten if ( y < bandwidthhis ) { totalbytesold[y] = totalbytes; } else { y = 0; totalbytesold[y] = totalbytes; } y++; totalbytes = 0; // Counter neu starten 0,5 Sekunden CLOCK_SetCountdownTimer ( mp3timer, 47, MSECOUND ); } } } timer0_free(); return; }
/** **************************************************************************************** * @brief Releases TIMER0. Does not disable the TIM clock, as it is shared with TIMER2 * * @param[in] timer2_duty_cycle: PWM2 duty cycle **************************************************************************************** */ inline void timer0_release(void) { timer0_stop(); }
void main (void) { //unsigned char i; unsigned char my_order; //static char count_burst; init_ioport(); spi_init(); rf_init(); //msp_init(); //timer0_init(); adc_init(); timer0_init(); timer0_start(); //regCallbackFunc(); //led_turnOn(); BUF_RDY = 0; rf_config(RX); rf_rxOn(); /* initial blinking */ //for(my_order=0; my_order<=MyID; my_order++) for(my_order=0; my_order<=1; my_order++) { led_turnOn(); delayms(10); led_turnOff(); delayms(1000); } /**/ for(;;) { rf_rxPacket(Buffer); if (Buffer[0] != MyID && Buffer[1] != MyID && Buffer[2] != MyID) { rf_rxOn(); continue; } if(Buffer[0] == MyID) my_order = 0; else if(Buffer[1] == MyID) my_order = 1; else my_order = 2; Buffer[0] = Buffer[1] = Buffer[2] = -1; // xmitdata() uses 'Buffer' to send data --> reset! //PWR_UP = 0; MSP_CS = 1; switch(my_order) { case 0: //_delay2us(150); _delay2us(170); //350 us break; case 1: delayms(1); _delay2us(245); //500 us //_delay2us(185); _delay2us(195); //400 break; case 2: delayms(2); _delay2us(245); // 5oo us //_delay2us(185); _delay2us(205); // 420 us break; } timer0_stop(); MSP_CS = 0; xmitdata(); MSP_CS = 1; led_toggle(); rf_config(RX); rf_rxOn(); timer0_start(); MSP_CS = 0; } // for } // main