First Publish. Works fine.

Dependents:   unzen_sample_lpcxpresso_4337_callbacks

Committer:
shorie
Date:
Thu May 12 22:13:04 2016 +0000
Revision:
12:5e73a102d809
Parent:
11:5f91f55065c1
Child:
13:845ba6aa3449
Refactored

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shorie 0:5ac19c994288 1 /**
shorie 0:5ac19c994288 2 * \brief header file for the unzen audio frame work
shorie 0:5ac19c994288 3 * \arthur SeiichiHorie
shorie 0:5ac19c994288 4 * \date 6/Apr/2016
shorie 0:5ac19c994288 5 */
shorie 0:5ac19c994288 6
shorie 0:5ac19c994288 7 #ifndef _unzen_h_
shorie 0:5ac19c994288 8 #define _unzen_h_
shorie 0:5ac19c994288 9
shorie 0:5ac19c994288 10 #include "mbed.h"
shorie 0:5ac19c994288 11 /**
shorie 0:5ac19c994288 12 \brief audio framework name space.
shorie 0:5ac19c994288 13 */
shorie 0:5ac19c994288 14 namespace unzen
shorie 0:5ac19c994288 15 {
shorie 0:5ac19c994288 16 /**
shorie 0:5ac19c994288 17 \brief error status type.
shorie 0:5ac19c994288 18 */
shorie 0:5ac19c994288 19 enum error_type {
shorie 0:5ac19c994288 20 no_error,
shorie 0:5ac19c994288 21 memory_allocation_error
shorie 0:5ac19c994288 22 };
shorie 0:5ac19c994288 23
shorie 0:5ac19c994288 24 /**
shorie 12:5e73a102d809 25 \brief adio frame work. Create a object and execute the \ref framework::start() method.
shorie 0:5ac19c994288 26 */
shorie 11:5f91f55065c1 27 class Framework
shorie 0:5ac19c994288 28 {
shorie 12:5e73a102d809 29 public:
shorie 0:5ac19c994288 30 /**
shorie 0:5ac19c994288 31 \constructor
shorie 0:5ac19c994288 32 \details
shorie 0:5ac19c994288 33 initialize the internal variables and set up all interrrupt / I2S related peripheral.
shorie 0:5ac19c994288 34 If needed, power up the peripheral, assign the clock and pins.
shorie 0:5ac19c994288 35 At the end of this constructor, the audio framework is ready to run, but still paused.
shorie 0:5ac19c994288 36 To start the real processing, call the \ref start method.
shorie 0:5ac19c994288 37
shorie 0:5ac19c994288 38 Note that this constructor set the block size ( interval count which audio processing
shorie 0:5ac19c994288 39 call back is called )
shorie 0:5ac19c994288 40 as 1. If it is needed to use other value, call \ref set_brock_size() method.
shorie 0:5ac19c994288 41 */
shorie 11:5f91f55065c1 42 Framework(void);
shorie 0:5ac19c994288 43
shorie 0:5ac19c994288 44 /**
shorie 0:5ac19c994288 45 \brief set the interval interrupt count for each time call back is called.
shorie 0:5ac19c994288 46 \param block_size An integer parameter > 1. If set to n, for each n interrupts, the audio call back is called.
shorie 0:5ac19c994288 47 \returns show the error status
shorie 0:5ac19c994288 48 \details
shorie 0:5ac19c994288 49 This method re-allocate the internal buffer. Then, the memory allocation error could occur. To detect the
shorie 0:5ac19c994288 50 memory allocation error, use \ref get_error() method.
shorie 0:5ac19c994288 51 */
shorie 0:5ac19c994288 52 error_type set_block_size( unsigned int new_block_size );
shorie 0:5ac19c994288 53
shorie 0:5ac19c994288 54 /**
shorie 0:5ac19c994288 55 \brief the real audio signal transfer. Trigger the I2S interrupt and call the call back.
shorie 12:5e73a102d809 56 \param init_cb initializer call back for signal processing. This is invoked only once before processing. Can be NUL
shorie 12:5e73a102d809 57 \param process_cb The call back function
shorie 0:5ac19c994288 58 \details
shorie 12:5e73a102d809 59 Set the call back function, then start the transer on I2S
shorie 12:5e73a102d809 60
shorie 12:5e73a102d809 61 Before the real data transaction on I2S, this method call init_cb(int) call back. The
shorie 12:5e73a102d809 62 parameter is block size (>0) which is set by set_block_size() method. If the init_cb is NUL,
shorie 12:5e73a102d809 63 this part is skipped.
shorie 12:5e73a102d809 64
shorie 12:5e73a102d809 65 This process_cb function is called for each time the rx buffer is filled. The call back has 5 parameters.
shorie 0:5ac19c994288 66 \li left_rx Received data from I2S rx port. Left data only.
shorie 0:5ac19c994288 67 \li right_rx Received data from I2S rx port. Right data only.
shorie 0:5ac19c994288 68 \li left_tx Buffer to fill the transmission data. This buffer mus be filled by call back. Left data only
shorie 0:5ac19c994288 69 \li right_tx Buffer to fill the transmission data. This buffer mus be filled by call back. Right data only
shorie 0:5ac19c994288 70 \li length length of above buffers.
shorie 0:5ac19c994288 71
shorie 12:5e73a102d809 72 The process call back is called for each time interrupt comes n times. Where n is the value which is specified by \ref set_block_size()
shorie 2:6613e62da521 73 function. This n is also passed to call back as above length parameter. By default, n is 1.
shorie 0:5ac19c994288 74
shorie 0:5ac19c994288 75 Note that the call back is called at interrupt context. Not the thread level context.
shorie 2:6613e62da521 76 That mean, it is better to avoid to call mbed API except the mbed-RTOS API for interrupt handler.
shorie 12:5e73a102d809 77 */
shorie 12:5e73a102d809 78 void start(
shorie 12:5e73a102d809 79 void (* init_cb ) (unsigned int),
shorie 12:5e73a102d809 80 void (* process_cb ) (float[], float[], float[], float[], unsigned int)
shorie 12:5e73a102d809 81 );
shorie 12:5e73a102d809 82
shorie 12:5e73a102d809 83
shorie 0:5ac19c994288 84 void set_pre_interrupt_callback( void (* cb ) (void));
shorie 0:5ac19c994288 85
shorie 0:5ac19c994288 86 /**
shorie 0:5ac19c994288 87 \brief Debug hook for interrupt handler.
shorie 0:5ac19c994288 88 \param cb A call back which is called at the end of I2S interrupt.
shorie 0:5ac19c994288 89 \details
shorie 0:5ac19c994288 90 Parameter cb is call at the end of the I2S interrupt. This call back can be
shorie 0:5ac19c994288 91 used to mesure the timing of interrupt by toggling the GPIO pin.
shorie 0:5ac19c994288 92
shorie 0:5ac19c994288 93 Passing 0 to cb parameter let the framwork ignore the callback.
shorie 0:5ac19c994288 94 */
shorie 0:5ac19c994288 95 void set_post_interrupt_callback( void (* cb ) (void));
shorie 0:5ac19c994288 96
shorie 0:5ac19c994288 97 /**
shorie 0:5ac19c994288 98 \brief Debug hook for processing handler.
shorie 0:5ac19c994288 99 \param cb A call back which is called at the beggining of processing.
shorie 0:5ac19c994288 100 \details
shorie 0:5ac19c994288 101 Parameter cb is call at the beggining of the signal processing. This call back can be
shorie 0:5ac19c994288 102 used to mesure the load of CPU by toggling the GPIO pin.
shorie 0:5ac19c994288 103
shorie 0:5ac19c994288 104 Passing 0 to cb parameter let the framwork ignore the callback.
shorie 0:5ac19c994288 105 */
shorie 0:5ac19c994288 106 void set_pre_process_callback( void (* cb ) (void));
shorie 0:5ac19c994288 107
shorie 0:5ac19c994288 108 /**
shorie 0:5ac19c994288 109 \brief Debug hook for processing handler.
shorie 0:5ac19c994288 110 \param cb A call back which is called at the end of processing.
shorie 0:5ac19c994288 111 \details
shorie 0:5ac19c994288 112 Parameter cb is call at the end of the signal processing. This call back can be
shorie 0:5ac19c994288 113 used to mesure the load of CPU by toggling the GPIO pin.
shorie 0:5ac19c994288 114
shorie 0:5ac19c994288 115 Passing 0 to cb parameter let the framwork ignore the callback.
shorie 0:5ac19c994288 116 */
shorie 0:5ac19c994288 117 void set_post_process_callback( void (* cb ) (void));
shorie 0:5ac19c994288 118
shorie 0:5ac19c994288 119 /**
shorie 0:5ac19c994288 120 \brief optional priority control for I2S IRQ.
shorie 0:5ac19c994288 121 \param pri Priority of IRQ.
shorie 0:5ac19c994288 122 \details
shorie 0:5ac19c994288 123 This is optional control. Usually, user doesn't need to call this method.
shorie 0:5ac19c994288 124 In case the framework has serious irq priority contention with other software,
shorie 0:5ac19c994288 125 this API help programmer to change the priority of the Unzen framework.
shorie 0:5ac19c994288 126
shorie 0:5ac19c994288 127 Value must be acceptable to CMSIS NVIC_SetPriority(). For LPC4337, it is range of 0..7. The 0 is highest priority.
shorie 0:5ac19c994288 128
shorie 0:5ac19c994288 129
shorie 0:5ac19c994288 130 By default, the framework set this priority as +2 higher than the lowest.
shorie 0:5ac19c994288 131
shorie 0:5ac19c994288 132 The priority set by this API must be higher than priority set by \ref set_process_irq_priority
shorie 0:5ac19c994288 133 */
shorie 0:5ac19c994288 134 void set_i2s_irq_priority( unsigned int pri );
shorie 0:5ac19c994288 135
shorie 0:5ac19c994288 136 /**
shorie 0:5ac19c994288 137 \brief optional priority control for Signal Processing IRQ.
shorie 0:5ac19c994288 138 \param pri Priority of IRQ.
shorie 0:5ac19c994288 139 \details
shorie 0:5ac19c994288 140 This is optional control. Usually, user doesn't need to call this method.
shorie 0:5ac19c994288 141 In case the framework has serious irq priority contention with other software,
shorie 0:5ac19c994288 142 this API help programmer to change the priority of the Unzen framework
shorie 0:5ac19c994288 143
shorie 0:5ac19c994288 144 Value must be acceptable to CMSIS NVIC_SetPriority(). For LPC4337, it is range of 0..7. The 0 is highest priority.
shorie 0:5ac19c994288 145
shorie 0:5ac19c994288 146 By default, the framework set this priority as +2 higher than the lowest.
shorie 0:5ac19c994288 147
shorie 0:5ac19c994288 148 */
shorie 0:5ac19c994288 149 void set_process_irq_priority( unsigned int pri );
shorie 12:5e73a102d809 150
shorie 12:5e73a102d809 151 private:
shorie 12:5e73a102d809 152 static Framework * _fw;
shorie 0:5ac19c994288 153 private:
shorie 10:45cbff9ee6e4 154 void (* _pre_interrupt_callback )(void);
shorie 10:45cbff9ee6e4 155 void (* _post_interrupt_callback )(void);
shorie 10:45cbff9ee6e4 156 void (* _pre_process_callback )(void);
shorie 10:45cbff9ee6e4 157 void (* _post_process_callback )(void);
shorie 0:5ac19c994288 158
shorie 10:45cbff9ee6e4 159 void (* _process_callback )( float left_in[], float right_in[], float left_out[], float right_out[], unsigned int length );
shorie 0:5ac19c994288 160
shorie 2:6613e62da521 161 // Size of the blocks ( interval of interrupt to call process_callback. 1 means every interrupt. 2 means every 2 interrupt )
shorie 10:45cbff9ee6e4 162 int _block_size;
shorie 0:5ac19c994288 163
shorie 2:6613e62da521 164 // Index for indentifying the buffer for interrupt. 0 or 1.
shorie 10:45cbff9ee6e4 165 int _buffer_index;
shorie 0:5ac19c994288 166
shorie 2:6613e62da521 167 // Index for indentifying the buffer for processing. 0 or 1.
shorie 10:45cbff9ee6e4 168 int _process_index;
shorie 2:6613e62da521 169
shorie 2:6613e62da521 170 // next transfer position in buffer
shorie 10:45cbff9ee6e4 171 int _sample_index;
shorie 0:5ac19c994288 172
shorie 0:5ac19c994288 173 // buffer for interrupt handler.
shorie 0:5ac19c994288 174 // data format is LRLR...
shorie 10:45cbff9ee6e4 175 int *_tx_int_buffer[2];
shorie 10:45cbff9ee6e4 176 int *_rx_int_buffer[2];
shorie 0:5ac19c994288 177
shorie 0:5ac19c994288 178 // buffers for passing
shorie 10:45cbff9ee6e4 179 float * _tx_left_buffer, * _tx_right_buffer;
shorie 10:45cbff9ee6e4 180 float * _rx_left_buffer, * _rx_right_buffer;
shorie 0:5ac19c994288 181
shorie 0:5ac19c994288 182 // real processing method.
shorie 10:45cbff9ee6e4 183 void _do_i2s_irq(void);
shorie 10:45cbff9ee6e4 184 void _do_process_irq(void);
shorie 0:5ac19c994288 185
shorie 0:5ac19c994288 186 // handler for NIVC
shorie 10:45cbff9ee6e4 187 static void _i2s_irq_handler();
shorie 10:45cbff9ee6e4 188 static void _process_irq_handler();
shorie 0:5ac19c994288 189 };
shorie 1:9710fb328a08 190
shorie 8:63e098b779e9 191
shorie 8:63e098b779e9 192
shorie 0:5ac19c994288 193 }
shorie 0:5ac19c994288 194
shorie 0:5ac19c994288 195 #endif