First Publish. Works fine.
Dependents: unzen_sample_LPC4088_quickstart
Diff: unzen.cpp
- Revision:
- 10:45cbff9ee6e4
- Parent:
- 9:2da6ce640691
- Child:
- 11:5f91f55065c1
--- a/unzen.cpp Sun May 08 02:44:45 2016 +0000 +++ b/unzen.cpp Sun May 08 09:52:36 2016 +0000 @@ -10,27 +10,27 @@ { // Clear all buffers - tx_int_buffer[0] = NULL; - tx_int_buffer[1] = NULL; - rx_int_buffer[0] = NULL; - rx_int_buffer[1] = NULL; + _tx_int_buffer[0] = NULL; + _tx_int_buffer[1] = NULL; + _rx_int_buffer[0] = NULL; + _rx_int_buffer[1] = NULL; - tx_left_buffer = NULL; - tx_right_buffer = NULL; - rx_left_buffer = NULL; - rx_right_buffer = NULL; + _tx_left_buffer = NULL; + _tx_right_buffer = NULL; + _rx_left_buffer = NULL; + _rx_right_buffer = NULL; // Initialize all buffer - buffer_index = 0; - sample_index = 0; + _buffer_index = 0; + _sample_index = 0; // Clear all callbacks - pre_interrupt_callback = NULL; - post_interrupt_callback = NULL; - pre_process_callback = NULL; - post_process_callback = NULL; + _pre_interrupt_callback = NULL; + _post_interrupt_callback = NULL; + _pre_process_callback = NULL; + _post_process_callback = NULL; - process_callback = NULL; + _process_callback = NULL; // Initialy block(buffer) size is 1. set_block_size( 1 ); @@ -39,12 +39,12 @@ hal_i2s_setup(); // Setup the interrupt for the I2S - NVIC_SetVector(hal_get_i2s_irq_id(), (uint32_t)i2s_irq_handler); + NVIC_SetVector(hal_get_i2s_irq_id(), (uint32_t)_i2s_irq_handler); set_i2s_irq_priority(hal_get_i2s_irq_priority_level()); NVIC_EnableIRQ(hal_get_i2s_irq_id()); // Setup the interrupt for the process - NVIC_SetVector(hal_get_process_irq_id(), (uint32_t)process_irq_handler); + NVIC_SetVector(hal_get_process_irq_id(), (uint32_t)_process_irq_handler); set_process_irq_priority(hal_get_process_irq_priority_level()); NVIC_EnableIRQ(hal_get_process_irq_id()); @@ -53,79 +53,79 @@ error_type framework::set_block_size( unsigned int new_block_size ) { - delete [] tx_int_buffer[0]; - delete [] tx_int_buffer[1]; - delete [] rx_int_buffer[0]; - delete [] rx_int_buffer[1]; + delete [] _tx_int_buffer[0]; + delete [] _tx_int_buffer[1]; + delete [] _rx_int_buffer[0]; + delete [] _rx_int_buffer[1]; - delete [] tx_left_buffer; - delete [] tx_right_buffer; - delete [] rx_left_buffer; - delete [] rx_right_buffer; + delete [] _tx_left_buffer; + delete [] _tx_right_buffer; + delete [] _rx_left_buffer; + delete [] _rx_right_buffer; - block_size = new_block_size; + _block_size = new_block_size; - tx_int_buffer[0] = new int[ 2 * block_size ]; - tx_int_buffer[1] = new int[ 2 * block_size ]; - rx_int_buffer[0] = new int[ 2 * block_size ]; - rx_int_buffer[1] = new int[ 2 * block_size ]; + _tx_int_buffer[0] = new int[ 2 * _block_size ]; + _tx_int_buffer[1] = new int[ 2 * _block_size ]; + _rx_int_buffer[0] = new int[ 2 * _block_size ]; + _rx_int_buffer[1] = new int[ 2 * _block_size ]; - tx_left_buffer = new float[ block_size ]; - tx_right_buffer = new float[ block_size ]; - rx_left_buffer = new float[ block_size ]; - rx_right_buffer = new float[ block_size ]; + _tx_left_buffer = new float[ _block_size ]; + _tx_right_buffer = new float[ _block_size ]; + _rx_left_buffer = new float[ _block_size ]; + _rx_right_buffer = new float[ _block_size ]; // error check - if ( rx_int_buffer[0] == NULL | - rx_int_buffer[1] == NULL | - tx_int_buffer[0] == NULL | - tx_int_buffer[1] == NULL | - rx_right_buffer == NULL | - tx_right_buffer == NULL | - rx_left_buffer == NULL | - tx_left_buffer == NULL ) + if ( _rx_int_buffer[0] == NULL | + _rx_int_buffer[1] == NULL | + _tx_int_buffer[0] == NULL | + _tx_int_buffer[1] == NULL | + _rx_right_buffer == NULL | + _tx_right_buffer == NULL | + _rx_left_buffer == NULL | + _tx_left_buffer == NULL ) { // if error, release all - delete [] tx_int_buffer[0]; - delete [] tx_int_buffer[1]; - delete [] rx_int_buffer[0]; - delete [] rx_int_buffer[1]; + delete [] _tx_int_buffer[0]; + delete [] _tx_int_buffer[1]; + delete [] _rx_int_buffer[0]; + delete [] _rx_int_buffer[1]; - delete [] tx_left_buffer; - delete [] tx_right_buffer; - delete [] rx_left_buffer; - delete [] rx_right_buffer; + delete [] _tx_left_buffer; + delete [] _tx_right_buffer; + delete [] _rx_left_buffer; + delete [] _rx_right_buffer; - tx_int_buffer[0] = NULL; - tx_int_buffer[1] = NULL; - rx_int_buffer[0] = NULL; - rx_int_buffer[1] = NULL; + _tx_int_buffer[0] = NULL; + _tx_int_buffer[1] = NULL; + _rx_int_buffer[0] = NULL; + _rx_int_buffer[1] = NULL; - tx_left_buffer = NULL; - tx_right_buffer = NULL; - rx_left_buffer = NULL; - rx_right_buffer = NULL; + _tx_left_buffer = NULL; + _tx_right_buffer = NULL; + _rx_left_buffer = NULL; + _rx_right_buffer = NULL; return memory_allocation_error; } // clear blocks - for ( int i=0; i<block_size*2; i++ ) + for ( int i=0; i<_block_size*2; i++ ) { - tx_int_buffer[0][i] = 0; - tx_int_buffer[1][i] = 0; - rx_int_buffer[0][i] = 0; - rx_int_buffer[1][i] = 0; + _tx_int_buffer[0][i] = 0; + _tx_int_buffer[1][i] = 0; + _rx_int_buffer[0][i] = 0; + _rx_int_buffer[1][i] = 0; } // clear blocks - for ( int i=0; i<block_size ; i++ ) + for ( int i=0; i<_block_size ; i++ ) { - tx_left_buffer[i] = 0; - tx_right_buffer[i] = 0; - rx_left_buffer[i] = 0; - rx_right_buffer[i] = 0; + _tx_left_buffer[i] = 0; + _tx_right_buffer[i] = 0; + _rx_left_buffer[i] = 0; + _rx_right_buffer[i] = 0; } @@ -153,37 +153,37 @@ void framework::set_process_callback( void (* cb ) (float[], float[], float[], float[], unsigned int)) { - process_callback = cb; + _process_callback = cb; } void framework::set_pre_interrupt_callback( void (* cb ) (void)) { - pre_interrupt_callback = cb; + _pre_interrupt_callback = cb; } void framework::set_post_interrupt_callback( void (* cb ) (void)) { - post_interrupt_callback = cb; + _post_interrupt_callback = cb; } void framework::set_pre_process_callback( void (* cb ) (void)) { - pre_process_callback = cb; + _pre_process_callback = cb; } void framework::set_post_process_callback( void (* cb ) (void)) { - post_process_callback = cb; + _post_process_callback = cb; } - void framework::do_i2s_irq(void) + void framework::_do_i2s_irq(void) { // if needed, call pre-interrupt call back - if ( pre_interrupt_callback ) - pre_interrupt_callback(); + if ( _pre_interrupt_callback ) + _pre_interrupt_callback(); // irq is handled only when the buffer is correctly allocated - if (tx_left_buffer) + if (_tx_left_buffer) { int sample; @@ -192,31 +192,31 @@ { // copy received data to buffer hal_get_i2s_rx_data( sample ); - rx_int_buffer[buffer_index][sample_index] = sample; + _rx_int_buffer[_buffer_index][_sample_index] = sample; // copy buffer data to transmit register - sample = tx_int_buffer[buffer_index][sample_index]; + sample = _tx_int_buffer[_buffer_index][_sample_index]; hal_put_i2s_tx_data( sample ); // increment index - sample_index ++; + _sample_index ++; } // Implementation of the double buffer algorithm. // if buffer transfer is complete, swap the buffer - if (sample_index >= block_size * 2) + if (_sample_index >= _block_size * 2) { // index for the signal processing - process_index = buffer_index; + _process_index = _buffer_index; // swap buffer - if ( buffer_index == 0 ) - buffer_index = 1; + if ( _buffer_index == 0 ) + _buffer_index = 1; else - buffer_index = 0; + _buffer_index = 0; // rewind sample index - sample_index = 0; + _sample_index = 0; // Trigger interrupt for signal processing NVIC->STIR = hal_get_process_irq_id(); @@ -224,19 +224,19 @@ } // if needed, call post-interrupt call back - if ( post_interrupt_callback ) - post_interrupt_callback(); + if ( _post_interrupt_callback ) + _post_interrupt_callback(); } - void framework::do_process_irq(void) + void framework::_do_process_irq(void) { // If needed, call the pre-process hook - if ( pre_process_callback ) - pre_process_callback(); + if ( _pre_process_callback ) + _pre_process_callback(); // Only when the process_call back is registered. - if ( process_callback ) + if ( _process_callback ) { int j = 0; @@ -244,47 +244,47 @@ // -- premuted from LRLRLR... to LLL.., RRR... // -- convert from fixed point to floating point // -- scale down as range of [-1, 1) - for ( int i=0; i<block_size; i++ ) + for ( int i=0; i<_block_size; i++ ) { - rx_left_buffer[i] = rx_int_buffer[process_index][j++]/ -(float)INT_MIN; - rx_right_buffer[i] = rx_int_buffer[process_index][j++]/ -(float)INT_MIN; + _rx_left_buffer[i] = _rx_int_buffer[_process_index][j++]/ -(float)INT_MIN; + _rx_right_buffer[i] = _rx_int_buffer[_process_index][j++]/ -(float)INT_MIN; } - process_callback - ( - rx_left_buffer, - rx_right_buffer, - tx_left_buffer, - tx_right_buffer, - block_size - ); + _process_callback + ( + _rx_left_buffer, + _rx_right_buffer, + _tx_left_buffer, + _tx_right_buffer, + _block_size + ); // Format conversion. // -- premuted from LLL.., RRR... to LRLRLR... // -- convert from floating point to fixed point // -- scale up from range of [-1, 1) j = 0; - for ( int i=0; i<block_size; i++ ) + for ( int i=0; i<_block_size; i++ ) { - tx_int_buffer[process_index][j++] = tx_left_buffer[i] * -(float)INT_MIN ; - tx_int_buffer[process_index][j++] = tx_right_buffer[i] * -(float)INT_MIN ; + _tx_int_buffer[_process_index][j++] = _tx_left_buffer[i] * -(float)INT_MIN ; + _tx_int_buffer[_process_index][j++] = _tx_right_buffer[i] * -(float)INT_MIN ; } } // if needed, call post-process callback - if ( post_process_callback ) - post_process_callback(); + if ( _post_process_callback ) + _post_process_callback(); } - void framework::process_irq_handler() + void framework::_process_irq_handler() { - framework::get()->do_process_irq(); + framework::get()->_do_process_irq(); } - void framework::i2s_irq_handler() + void framework::_i2s_irq_handler() { - framework::get()->do_i2s_irq(); + framework::get()->_do_i2s_irq(); }