First Publish. Works fine.

Dependents:   unzen_sample_nucleo_f746 unzen_delay_sample_nucleo_f746 skeleton_unzen_nucleo_f746 ifmag_noise_canceller ... more

Nucleo F746ZG用のオーディオ・フレームワークです。フレームワーク地震の詳細は『雲仙』オーディオ・フレームワークを参照してください。

参考リンク

  • skeleton_unzen_nucleo_f746 Nucleo F746ZGおよびUI基板を使う場合のスケルトンプログラム。F746を使う方はここから読み始めると良いでしょう。
Committer:
shorie
Date:
Sun Apr 10 12:37:10 2016 +0000
Revision:
0:5ac19c994288
Child:
1:9710fb328a08
HAL stable : considered STM32F4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shorie 0:5ac19c994288 1 #include "unzen.h"
shorie 0:5ac19c994288 2 #include "limits.h"
shorie 0:5ac19c994288 3 #include "unzen_hal.h"
shorie 0:5ac19c994288 4
shorie 0:5ac19c994288 5 namespace unzen
shorie 0:5ac19c994288 6 {
shorie 0:5ac19c994288 7 framework::framework()
shorie 0:5ac19c994288 8 {
shorie 0:5ac19c994288 9
shorie 0:5ac19c994288 10 tx_int_buffer[0] = NULL;
shorie 0:5ac19c994288 11 tx_int_buffer[1] = NULL;
shorie 0:5ac19c994288 12 rx_int_buffer[0] = NULL;
shorie 0:5ac19c994288 13 rx_int_buffer[1] = NULL;
shorie 0:5ac19c994288 14
shorie 0:5ac19c994288 15 tx_left_buffer = NULL;
shorie 0:5ac19c994288 16 tx_right_buffer = NULL;
shorie 0:5ac19c994288 17 rx_left_buffer = NULL;
shorie 0:5ac19c994288 18 rx_right_buffer = NULL;
shorie 0:5ac19c994288 19
shorie 0:5ac19c994288 20 buffer_index = 0;
shorie 0:5ac19c994288 21 tx_sample_index = 0;
shorie 0:5ac19c994288 22 rx_sample_index = 0;
shorie 0:5ac19c994288 23
shorie 0:5ac19c994288 24 pre_interrupt_callback = NULL;
shorie 0:5ac19c994288 25 post_interrupt_callback = NULL;
shorie 0:5ac19c994288 26 pre_process_callback = NULL;
shorie 0:5ac19c994288 27 post_process_callback = NULL;
shorie 0:5ac19c994288 28
shorie 0:5ac19c994288 29 process_callback = NULL;
shorie 0:5ac19c994288 30
shorie 0:5ac19c994288 31 set_block_size( 1 );
shorie 0:5ac19c994288 32
shorie 0:5ac19c994288 33 hal_i2s_setup();
shorie 0:5ac19c994288 34
shorie 0:5ac19c994288 35 NVIC_SetVector(hal_get_i2s_irq_id(), (uint32_t)i2s_irq_handler);
shorie 0:5ac19c994288 36 NVIC_EnableIRQ(hal_get_i2s_irq_id());
shorie 0:5ac19c994288 37
shorie 0:5ac19c994288 38 NVIC_SetVector(hal_get_process_irq_id(), (uint32_t)process_irq_handler);
shorie 0:5ac19c994288 39 NVIC_EnableIRQ(hal_get_process_irq_id());
shorie 0:5ac19c994288 40
shorie 0:5ac19c994288 41 // to enable block processing, I2S IRQ priority must be higher than process IRQ
shorie 0:5ac19c994288 42 set_i2s_irq_priority(hal_get_lowest_priority_level() - 1);
shorie 0:5ac19c994288 43 set_process_irq_priority(hal_get_lowest_priority_level() - 2);
shorie 0:5ac19c994288 44
shorie 0:5ac19c994288 45 }
shorie 0:5ac19c994288 46
shorie 0:5ac19c994288 47 error_type framework::set_block_size( unsigned int new_block_size )
shorie 0:5ac19c994288 48 {
shorie 0:5ac19c994288 49
shorie 0:5ac19c994288 50 delete [] tx_int_buffer[0];
shorie 0:5ac19c994288 51 delete [] tx_int_buffer[1];
shorie 0:5ac19c994288 52 delete [] rx_int_buffer[0];
shorie 0:5ac19c994288 53 delete [] rx_int_buffer[1];
shorie 0:5ac19c994288 54
shorie 0:5ac19c994288 55 delete [] tx_left_buffer;
shorie 0:5ac19c994288 56 delete [] tx_right_buffer;
shorie 0:5ac19c994288 57 delete [] rx_left_buffer;
shorie 0:5ac19c994288 58 delete [] rx_right_buffer;
shorie 0:5ac19c994288 59
shorie 0:5ac19c994288 60 block_size = new_block_size;
shorie 0:5ac19c994288 61
shorie 0:5ac19c994288 62 tx_int_buffer[0] = new int[ 2 * block_size ];
shorie 0:5ac19c994288 63 tx_int_buffer[1] = new int[ 2 * block_size ];
shorie 0:5ac19c994288 64 rx_int_buffer[0] = new int[ 2 * block_size ];
shorie 0:5ac19c994288 65 rx_int_buffer[1] = new int[ 2 * block_size ];
shorie 0:5ac19c994288 66
shorie 0:5ac19c994288 67 tx_left_buffer = new float[ block_size ];
shorie 0:5ac19c994288 68 tx_right_buffer = new float[ block_size ];
shorie 0:5ac19c994288 69 rx_left_buffer = new float[ block_size ];
shorie 0:5ac19c994288 70 rx_right_buffer = new float[ block_size ];
shorie 0:5ac19c994288 71
shorie 0:5ac19c994288 72 // error check
shorie 0:5ac19c994288 73 if ( rx_int_buffer[0] == NULL |
shorie 0:5ac19c994288 74 rx_int_buffer[1] == NULL |
shorie 0:5ac19c994288 75 tx_int_buffer[0] == NULL |
shorie 0:5ac19c994288 76 tx_int_buffer[1] == NULL |
shorie 0:5ac19c994288 77 rx_right_buffer == NULL |
shorie 0:5ac19c994288 78 tx_right_buffer == NULL |
shorie 0:5ac19c994288 79 rx_left_buffer == NULL |
shorie 0:5ac19c994288 80 tx_left_buffer == NULL )
shorie 0:5ac19c994288 81 { // if error, release all
shorie 0:5ac19c994288 82 delete [] tx_int_buffer[0];
shorie 0:5ac19c994288 83 delete [] tx_int_buffer[1];
shorie 0:5ac19c994288 84 delete [] rx_int_buffer[0];
shorie 0:5ac19c994288 85 delete [] rx_int_buffer[1];
shorie 0:5ac19c994288 86
shorie 0:5ac19c994288 87 delete [] tx_left_buffer;
shorie 0:5ac19c994288 88 delete [] tx_right_buffer;
shorie 0:5ac19c994288 89 delete [] rx_left_buffer;
shorie 0:5ac19c994288 90 delete [] rx_right_buffer;
shorie 0:5ac19c994288 91
shorie 0:5ac19c994288 92 tx_int_buffer[0] = NULL;
shorie 0:5ac19c994288 93 tx_int_buffer[1] = NULL;
shorie 0:5ac19c994288 94 rx_int_buffer[0] = NULL;
shorie 0:5ac19c994288 95 rx_int_buffer[1] = NULL;
shorie 0:5ac19c994288 96
shorie 0:5ac19c994288 97 tx_left_buffer = NULL;
shorie 0:5ac19c994288 98 tx_right_buffer = NULL;
shorie 0:5ac19c994288 99 rx_left_buffer = NULL;
shorie 0:5ac19c994288 100 rx_right_buffer = NULL;
shorie 0:5ac19c994288 101
shorie 0:5ac19c994288 102 return memory_allocation_error;
shorie 0:5ac19c994288 103 }
shorie 0:5ac19c994288 104
shorie 0:5ac19c994288 105 // clear blocks
shorie 0:5ac19c994288 106 for ( int i=0; i<block_size*2; i++ )
shorie 0:5ac19c994288 107 {
shorie 0:5ac19c994288 108
shorie 0:5ac19c994288 109 tx_int_buffer[0][i] = 0;
shorie 0:5ac19c994288 110 tx_int_buffer[1][i] = 0;
shorie 0:5ac19c994288 111 rx_int_buffer[0][i] = 0;
shorie 0:5ac19c994288 112 rx_int_buffer[1][i] = 0;
shorie 0:5ac19c994288 113
shorie 0:5ac19c994288 114 }
shorie 0:5ac19c994288 115 // clear blocks
shorie 0:5ac19c994288 116 for ( int i=0; i<block_size ; i++ )
shorie 0:5ac19c994288 117 {
shorie 0:5ac19c994288 118
shorie 0:5ac19c994288 119 tx_left_buffer[i] = 0;
shorie 0:5ac19c994288 120 tx_right_buffer[i] = 0;
shorie 0:5ac19c994288 121 rx_left_buffer[i] = 0;
shorie 0:5ac19c994288 122 rx_right_buffer[i] = 0;
shorie 0:5ac19c994288 123
shorie 0:5ac19c994288 124 }
shorie 0:5ac19c994288 125
shorie 0:5ac19c994288 126 return no_error;
shorie 0:5ac19c994288 127 }
shorie 0:5ac19c994288 128
shorie 0:5ac19c994288 129 void framework::start()
shorie 0:5ac19c994288 130 {
shorie 0:5ac19c994288 131 hal_i2s_start();
shorie 0:5ac19c994288 132 }
shorie 0:5ac19c994288 133
shorie 0:5ac19c994288 134 void framework::set_i2s_irq_priority( unsigned int pri )
shorie 0:5ac19c994288 135 {
shorie 0:5ac19c994288 136 NVIC_SetPriority(hal_get_process_irq_id(), pri); // must be higher than PendSV of mbed-RTOS
shorie 0:5ac19c994288 137
shorie 0:5ac19c994288 138 }
shorie 0:5ac19c994288 139
shorie 0:5ac19c994288 140 void framework::set_process_irq_priority( unsigned int pri )
shorie 0:5ac19c994288 141 {
shorie 0:5ac19c994288 142 NVIC_SetPriority(hal_get_i2s_irq_id(), pri); // must be higher than process IRQ
shorie 0:5ac19c994288 143
shorie 0:5ac19c994288 144 }
shorie 0:5ac19c994288 145
shorie 0:5ac19c994288 146 void framework::set_process_callback( void (* cb ) (float[], float[], float[], float[], unsigned int))
shorie 0:5ac19c994288 147 {
shorie 0:5ac19c994288 148 process_callback = cb;
shorie 0:5ac19c994288 149 }
shorie 0:5ac19c994288 150
shorie 0:5ac19c994288 151 void framework::set_pre_interrupt_callback( void (* cb ) (void))
shorie 0:5ac19c994288 152 {
shorie 0:5ac19c994288 153 pre_interrupt_callback = cb;
shorie 0:5ac19c994288 154 }
shorie 0:5ac19c994288 155
shorie 0:5ac19c994288 156 void framework::set_post_interrupt_callback( void (* cb ) (void))
shorie 0:5ac19c994288 157 {
shorie 0:5ac19c994288 158 post_interrupt_callback = cb;
shorie 0:5ac19c994288 159 }
shorie 0:5ac19c994288 160
shorie 0:5ac19c994288 161 void framework::set_pre_process_callback( void (* cb ) (void))
shorie 0:5ac19c994288 162 {
shorie 0:5ac19c994288 163 pre_process_callback = cb;
shorie 0:5ac19c994288 164 }
shorie 0:5ac19c994288 165
shorie 0:5ac19c994288 166 void framework::set_post_process_callback( void (* cb ) (void))
shorie 0:5ac19c994288 167 {
shorie 0:5ac19c994288 168 post_process_callback = cb;
shorie 0:5ac19c994288 169 }
shorie 0:5ac19c994288 170
shorie 0:5ac19c994288 171
shorie 0:5ac19c994288 172 void framework::do_i2s_irq(void)
shorie 0:5ac19c994288 173 {
shorie 0:5ac19c994288 174 if ( pre_interrupt_callback )
shorie 0:5ac19c994288 175 pre_interrupt_callback();
shorie 0:5ac19c994288 176
shorie 0:5ac19c994288 177 // irq is handled only when the buffer is correctly allocated
shorie 0:5ac19c994288 178 if (tx_left_buffer)
shorie 0:5ac19c994288 179 {
shorie 0:5ac19c994288 180 int sample;
shorie 0:5ac19c994288 181 // check how many data have to be transmimt per interrupt.
shorie 0:5ac19c994288 182 if ( hal_data_per_sample() >0 ) // In case single or multiple word per interrupt
shorie 0:5ac19c994288 183 {
shorie 0:5ac19c994288 184 for ( int i=0; i>hal_data_per_sample(); i++ )
shorie 0:5ac19c994288 185 {
shorie 0:5ac19c994288 186 // copy received data to buffer
shorie 0:5ac19c994288 187 hal_get_i2s_rx_data( sample );
shorie 0:5ac19c994288 188 rx_int_buffer[buffer_index][rx_sample_index++] = sample;
shorie 0:5ac19c994288 189 // copy buffer data to transmit register
shorie 0:5ac19c994288 190 hal_set_i2s_tx_data( tx_int_buffer[buffer_index][rx_sample_index++] );
shorie 0:5ac19c994288 191 }
shorie 0:5ac19c994288 192 }
shorie 0:5ac19c994288 193 else // if the hal_data_per_smple() is 0, the data have to push multiple time.
shorie 0:5ac19c994288 194 {
shorie 0:5ac19c994288 195 if ( hal_get_i2s_rx_data( sample ) )
shorie 0:5ac19c994288 196 {
shorie 0:5ac19c994288 197 // data have to be pushed to buffer only when the hal_get_i2s_rx_data() returns non-zero
shorie 0:5ac19c994288 198 rx_int_buffer[buffer_index][rx_sample_index++] = sample;
shorie 0:5ac19c994288 199 }
shorie 0:5ac19c994288 200 if ( hal_set_i2s_tx_data( tx_int_buffer[buffer_index][tx_sample_index] ))
shorie 0:5ac19c994288 201 // pointer can be updated only when hal_set_i2s_tx_data() returns non_zero.
shorie 0:5ac19c994288 202 tx_sample_index ++;
shorie 0:5ac19c994288 203
shorie 0:5ac19c994288 204 }
shorie 0:5ac19c994288 205 // Implementation of the double buffer algorithm.
shorie 0:5ac19c994288 206 // if buffer transfer is complete, swap the buffer
shorie 0:5ac19c994288 207 if (rx_sample_index > block_size * 2 & rx_sample_index > block_size * 2)
shorie 0:5ac19c994288 208 {
shorie 0:5ac19c994288 209 // index for the signal processing
shorie 0:5ac19c994288 210 process_index = buffer_index;
shorie 0:5ac19c994288 211 // swap buffer
shorie 0:5ac19c994288 212 buffer_index = 1 - buffer_index ; // reverse 1 <-> 0
shorie 0:5ac19c994288 213 tx_sample_index = 0;
shorie 0:5ac19c994288 214 rx_sample_index = 0;
shorie 0:5ac19c994288 215
shorie 0:5ac19c994288 216 NVIC->STIR = hal_get_process_irq_id(); // Trigger interrupt for signal processing
shorie 0:5ac19c994288 217 }
shorie 0:5ac19c994288 218 }
shorie 0:5ac19c994288 219
shorie 0:5ac19c994288 220 if ( post_interrupt_callback )
shorie 0:5ac19c994288 221 post_interrupt_callback();
shorie 0:5ac19c994288 222 }
shorie 0:5ac19c994288 223
shorie 0:5ac19c994288 224 void framework::do_process_irq(void)
shorie 0:5ac19c994288 225 {
shorie 0:5ac19c994288 226 if ( pre_process_callback )
shorie 0:5ac19c994288 227 pre_process_callback();
shorie 0:5ac19c994288 228
shorie 0:5ac19c994288 229 if ( process_callback )
shorie 0:5ac19c994288 230 {
shorie 0:5ac19c994288 231 int j = 0;
shorie 0:5ac19c994288 232
shorie 0:5ac19c994288 233 // convert from fixed point to floating point
shorie 0:5ac19c994288 234 // also scale down
shorie 0:5ac19c994288 235 for ( int i=0; i<block_size; i++ )
shorie 0:5ac19c994288 236 {
shorie 0:5ac19c994288 237 rx_left_buffer[i] = rx_int_buffer[process_index][j++]/ -(float)INT_MIN;
shorie 0:5ac19c994288 238 rx_right_buffer[i] = rx_int_buffer[process_index][j++]/ -(float)INT_MIN;
shorie 0:5ac19c994288 239 }
shorie 0:5ac19c994288 240
shorie 0:5ac19c994288 241 process_callback(
shorie 0:5ac19c994288 242 rx_left_buffer,
shorie 0:5ac19c994288 243 rx_right_buffer,
shorie 0:5ac19c994288 244 tx_left_buffer,
shorie 0:5ac19c994288 245 tx_right_buffer,
shorie 0:5ac19c994288 246 block_size
shorie 0:5ac19c994288 247 );
shorie 0:5ac19c994288 248
shorie 0:5ac19c994288 249 // convert from floating point to fixed point
shorie 0:5ac19c994288 250 // also scale up
shorie 0:5ac19c994288 251 j = 0;
shorie 0:5ac19c994288 252 for ( int i=0; i<block_size; i++ )
shorie 0:5ac19c994288 253 {
shorie 0:5ac19c994288 254 tx_int_buffer[process_index][j++] = - tx_left_buffer[i] * INT_MIN ;
shorie 0:5ac19c994288 255 tx_int_buffer[process_index][j++] = - tx_right_buffer[i] * INT_MIN ;
shorie 0:5ac19c994288 256 }
shorie 0:5ac19c994288 257
shorie 0:5ac19c994288 258 }
shorie 0:5ac19c994288 259
shorie 0:5ac19c994288 260 if ( post_process_callback )
shorie 0:5ac19c994288 261 post_process_callback();
shorie 0:5ac19c994288 262 }
shorie 0:5ac19c994288 263
shorie 0:5ac19c994288 264 void framework::process_irq_handler()
shorie 0:5ac19c994288 265 {
shorie 0:5ac19c994288 266 framework::get().do_process_irq();
shorie 0:5ac19c994288 267 }
shorie 0:5ac19c994288 268
shorie 0:5ac19c994288 269 void framework::i2s_irq_handler()
shorie 0:5ac19c994288 270 {
shorie 0:5ac19c994288 271 framework::get().do_i2s_irq();
shorie 0:5ac19c994288 272 }
shorie 0:5ac19c994288 273
shorie 0:5ac19c994288 274 }
shorie 0:5ac19c994288 275