Pulse mutex fixes

master
Lucjan Bryndza 2013-05-31 11:38:09 +02:00
rodzic 31ac1d913a
commit 04359c6a81
6 zmienionych plików z 98 dodań i 32 usunięć

Wyświetl plik

@ -22,8 +22,8 @@ class ham_digi
ham_digi& operator=(const ham_digi&) = delete;
static constexpr auto TX_QUELEN = 512;
static constexpr auto DEF_FREQ = 1000;
static constexpr auto SYS_CALLBACK_ID = -1;
public:
static constexpr auto SYS_CALLBACK_ID = -1;
/* Modulation structure */
enum class modulation : short
{
@ -48,7 +48,7 @@ public:
*/
typedef std::function <void( int chn, const event &ev )> handler_t;
/* Constructor with handler */
ham_digi( handler_t handler, trx_device_base *hw_device );
ham_digi( handler_t handler );
/* Activate transmission */
int enable( bool en );
/* Switch to TX RX */
@ -58,6 +58,11 @@ public:
/* New RX channel */
int rx_channel_add(); /* Extra channel ADD */
int rx_channel_remove( int chn_id ); /* Extra channel remove */
/* Wait to finish */
int join()
{
return m_iodev->join();
}
private:
class tx_proxy
{

Wyświetl plik

@ -64,8 +64,9 @@ private:
private:
const char *m_dev_name; //Device name
std::unique_ptr< std::thread > m_thread; //TX thread
std::mutex m_thrmutex; //Thread mutex
std::mutex m_codec_mutex; //Thread mutex
std::mutex m_sectrum_mutex; //Spectrum mutex
std::mutex m_pulse_mutex;
volatile bool m_thread_running {}; //Thread
volatile int m_thread_status {}; //Thread error status
pa_simple *m_pa_ctx {nullptr}; //PA CTX

Wyświetl plik

@ -107,9 +107,11 @@ void trx_device_base::adc_process( const sample_type *buf, size_t len )
{
//True if all samples completed
if( m_spectrum.copy_samples( buf, len ) )
{
m_spectrum_tmr = 0;
//Inform that user can calculate the FFT
callback_notify( event::type::spectrum );
//Inform that user can calculate the FFT
callback_notify( event::type::spectrum );
}
}
}
}

Wyświetl plik

@ -6,7 +6,11 @@
*/
/* ------------------------------------------------------------------------- */
#include "ham/ham_digi.hpp"
#ifndef COMPILED_UNDER_ISIX
#include "port/pulse/pulse_device.hpp"
#else
#error not implemented yet
#endif
#include "psk/decoder.hpp"
#include "psk/modulator.hpp"
#include <functional>
@ -14,11 +18,23 @@
namespace ham {
namespace psk {
/* ------------------------------------------------------------------------- */
namespace
{
#ifndef COMPILED_UNDER_ISIX
inline trx_device_base* create_default_device( ham_digi::handler_t h )
{
static const int sys_idx = ham_digi::SYS_CALLBACK_ID;
return new pulse_device( std::bind(h, sys_idx, std::placeholders::_1) );
}
#else
#endif
}
/* ------------------------------------------------------------------------- */
//Default ham digi constructor
ham_digi::ham_digi( handler_t handler, trx_device_base *hw_device )
: m_iodev( hw_device ), m_callback( handler )
ham_digi::ham_digi( handler_t handler )
: m_iodev( create_default_device( handler ) )
, m_callback( handler )
{
}
/* ------------------------------------------------------------------------- */
@ -47,16 +63,15 @@ int ham_digi::set_tx( bool tx )
{
return err_no_modulation;
}
/* Not working yet */
//if( m_iodev->get_tx_codec()->is_transmitting() )
//{
// return err_tx_busy;
//}
if( tx && m_iodev->get_mode()==trx_device_base::mode::on )
{
if( m_iodev->get_tx_codec()->is_transmitting() )
{
return err_tx_busy;
}
else
{
m_iodev->set_mode( trx_device_base::mode::transmit );
}
m_iodev->set_mode( trx_device_base::mode::transmit );
}
else
{

Wyświetl plik

@ -28,6 +28,7 @@ int pulse_device::setup_sound_hardware( trx_device_base::mode m )
//Hardware SETUP state
if( m == trx_device_base::mode::on && get_mode()!=trx_device_base::mode::on )
{
if( get_mode()==trx_device_base::mode::transmit )
{
const int ret = disable_hw_tx();
@ -60,7 +61,7 @@ int pulse_device::setup_sound_hardware( trx_device_base::mode m )
m_thread.reset( new std::thread( &pulse_device::hardware_sound_thread, this ) );
return 0;
}
return trx_device_base::INVALID;
return 0;
}
/* ------------------------------------------------------------------------- */
//Hardware sound thread func
@ -69,10 +70,9 @@ void pulse_device::hardware_sound_thread()
int errcode = 0;
for(; m_thread_running && !errcode ;)
{
m_thrmutex.lock();
if ( get_mode()==trx_device_base::mode::on ) errcode = receive_thread();
else if( get_mode()==trx_device_base::mode::transmit ) errcode = transmit_thread();
m_thrmutex.unlock();
}
if( get_mode()==trx_device_base::mode::on )
{
@ -94,12 +94,18 @@ void pulse_device::hardware_sound_thread()
int pulse_device::receive_thread()
{
int error = 0;
/* Record some data ... */
if (pa_simple_read(m_pa_ctx, &m_audio_buf[0], audio_buf_len*sizeof(short), &error) < 0)
{
return error;
std::lock_guard<std::mutex> lock( m_pulse_mutex );
/* Record some data ... */
if (pa_simple_read(m_pa_ctx, &m_audio_buf[0], audio_buf_len*sizeof(short), &error) < 0)
{
return error;
}
}
{
std::lock_guard<std::mutex> lock( m_codec_mutex );
adc_process( &m_audio_buf[0], audio_buf_len );
}
adc_process( &m_audio_buf[0], audio_buf_len );
return error;
}
/* ------------------------------------------------------------------------- */
@ -107,10 +113,18 @@ int pulse_device::receive_thread()
int pulse_device::transmit_thread()
{
int error = 0;
if ( !dac_process( &m_audio_buf[0], audio_buf_len ) )
bool status;
{
std::lock_guard<std::mutex> lock( m_codec_mutex );
status = dac_process( &m_audio_buf[0], audio_buf_len );
}
if( !status )
{
std::lock_guard<std::mutex> lock( m_pulse_mutex );
if (pa_simple_write(m_pa_ctx,&m_audio_buf[0], audio_buf_len*sizeof(short), &error) < 0)
{
return error;
}
}
else
{
@ -118,7 +132,6 @@ int pulse_device::transmit_thread()
return error;
if( (error = enable_hw_rx()) )
return error;
}
return error;
}
@ -126,6 +139,7 @@ int pulse_device::transmit_thread()
//Enable hardware receive
int pulse_device::enable_hw_rx()
{
std::lock_guard<std::mutex> lock( m_pulse_mutex );
/* Pulse receive config */
const pa_sample_spec pconfig
{
@ -144,6 +158,7 @@ int pulse_device::enable_hw_rx()
//Disable hardware receive
int pulse_device::disable_hw_rx()
{
std::lock_guard<std::mutex> lock( m_pulse_mutex );
pa_simple_free(m_pa_ctx);
m_pa_ctx = nullptr;
return 0;
@ -152,6 +167,7 @@ int pulse_device::disable_hw_rx()
//Enable hardware trasmit
int pulse_device::enable_hw_tx()
{
std::lock_guard<std::mutex> lock( m_pulse_mutex );
/* Pulse receive config */
const pa_sample_spec pconfig
{
@ -171,6 +187,7 @@ int pulse_device::enable_hw_tx()
//Disable hardware tramsmit
int pulse_device::disable_hw_tx()
{
std::lock_guard<std::mutex> lock( m_pulse_mutex );
int error = 0;
/* Make sure that every single sample was played */
if (pa_simple_drain(m_pa_ctx, &error) < 0)
@ -186,7 +203,7 @@ int pulse_device::disable_hw_tx()
void pulse_device::lock( int id )
{
if( id == trx_device_base::lock_object )
m_thrmutex.lock();
m_codec_mutex.lock();
else if( id == trx_device_base::lock_spectrum )
m_sectrum_mutex.lock();
}
@ -194,7 +211,7 @@ void pulse_device::lock( int id )
void pulse_device::unlock( int id )
{
if( id == trx_device_base::lock_object )
m_thrmutex.unlock();
m_codec_mutex.unlock();
else if( id == trx_device_base::lock_spectrum )
m_sectrum_mutex.unlock();
}
@ -202,7 +219,7 @@ void pulse_device::unlock( int id )
bool pulse_device::try_lock( int id )
{
if( id == trx_device_base::lock_object )
return m_thrmutex.try_lock();
return m_codec_mutex.try_lock();
else if( id == trx_device_base::lock_spectrum )
return m_sectrum_mutex.try_lock();
return false;

Wyświetl plik

@ -508,7 +508,7 @@ int encoder_main( const char *filename )
/* *************************** MAIN LOOP ********************************** */
#if 0 /* Disabled for real pulse testing */
#if 0 /* Disabled for real pulse testing */
int main(int argc, const char * const *argv )
{
if(argc < 2)
@ -547,10 +547,11 @@ int main(int argc, const char * const *argv )
void decoder_callback( const ham::psk::event &ev )
void decoder_callback( int idx, const ham::psk::event &ev )
{
using namespace ham::psk;
using namespace std;
cout << idx << " ";
switch( ev.evt )
{
case event::type::rx_char:
@ -568,13 +569,16 @@ void decoder_callback( const ham::psk::event &ev )
case event::type::tx_end:
cout << "TRANSMIT FINISHED" << endl;
break;
case event::type::spectrum:
cout << "SPECTRUM AVAIL " << endl;
break;
default:
cout << "Unknown evt" << endl;
cout << "Unknown evt " << int( ev.evt ) << endl;
}
}
#if 1
#if 0
int main(int /*argc*/, const char * const */*argv*/ )
{
namespace psk = ham::psk;
@ -598,6 +602,28 @@ int main(int /*argc*/, const char * const */*argv*/ )
}
#endif
#if 1
int main(int /*argc*/, const char * const */*argv*/ )
{
namespace psk = ham::psk;
psk::ham_digi ham_digi( decoder_callback );
std:: cout << ham_digi.set_modulation( psk::ham_digi::modulation::psk ) << std::endl;
ham_digi.rx()->set_frequency( 2125 );
ham_digi.rx()->set_afc_limit( 100 );
ham_digi.rx()->set_mode( psk::mod_psk_config(psk::mod_psk_config::mode::qpsku, psk::mod_psk_config::baud::b31) );
ham_digi.tx()->set_mode( psk::mod_psk_config(psk::mod_psk_config::mode::qpsku, psk::mod_psk_config::baud::b31));
ham_digi.tx()->set_freqency( 2125 );
const char txt[] = "Ala ma kota a KOT ma ale teraz bedzie nieco dluzszy tekst a im tekst dluzszy tym lepszy";
for(size_t i=0;i<sizeof txt -1; i++)
ham_digi.tx()->put_tx( txt[i] );
std::cout << ham_digi.enable( true ) << std::endl;
::sleep(2);
std::cout << "TRANSMIT " << std::endl;
std::cout << ham_digi.set_tx( true ) << std::endl;
std::cout << ham_digi.join() << std::endl;
}
#endif
#else /*ARM defined test only */
void decoder_callback( int event, int param, int param2 )