mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			_acquire() function added & no duplication in format/freq calls
1. Private _acquire() function is added to avoid multiple locking/unlocking 2. format and frequency functions updated to use appropriate function calls instead of a aquire()pull/4635/head
							parent
							
								
									cbfb234f07
								
							
						
					
					
						commit
						de89be35f8
					
				| 
						 | 
				
			
			@ -36,23 +36,23 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
 | 
			
		|||
    // No lock needed in the constructor
 | 
			
		||||
 | 
			
		||||
    spi_init(&_spi, mosi, miso, sclk, ssel);
 | 
			
		||||
    aquire();
 | 
			
		||||
    _acquire();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SPI::format(int bits, int mode) {
 | 
			
		||||
    lock();
 | 
			
		||||
    _bits = bits;
 | 
			
		||||
    _mode = mode;
 | 
			
		||||
    SPI::_owner = NULL; // Not that elegant, but works. rmeyer
 | 
			
		||||
    aquire();
 | 
			
		||||
    spi_format(&_spi, _bits, _mode, 0);
 | 
			
		||||
    _owner = this;
 | 
			
		||||
    unlock();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SPI::frequency(int hz) {
 | 
			
		||||
    lock();
 | 
			
		||||
    _hz = hz;
 | 
			
		||||
    SPI::_owner = NULL; // Not that elegant, but works. rmeyer
 | 
			
		||||
    aquire();
 | 
			
		||||
    spi_frequency(&_spi, _hz);
 | 
			
		||||
    _owner = this;
 | 
			
		||||
    unlock();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -70,9 +70,18 @@ void SPI::aquire() {
 | 
			
		|||
    unlock();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Note: Private function with no locking
 | 
			
		||||
void SPI::_acquire() {
 | 
			
		||||
     if (_owner != this) {
 | 
			
		||||
        spi_format(&_spi, _bits, _mode, 0);
 | 
			
		||||
        spi_frequency(&_spi, _hz);
 | 
			
		||||
        _owner = this;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int SPI::write(int value) {
 | 
			
		||||
    lock();
 | 
			
		||||
    aquire();
 | 
			
		||||
    _acquire();
 | 
			
		||||
    int ret = spi_master_write(&_spi, value);
 | 
			
		||||
    unlock();
 | 
			
		||||
    return ret;
 | 
			
		||||
| 
						 | 
				
			
			@ -80,7 +89,7 @@ int SPI::write(int value) {
 | 
			
		|||
 | 
			
		||||
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
 | 
			
		||||
    lock();
 | 
			
		||||
    aquire();
 | 
			
		||||
    _acquire();
 | 
			
		||||
    int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
 | 
			
		||||
    unlock();
 | 
			
		||||
    return ret;
 | 
			
		||||
| 
						 | 
				
			
			@ -167,7 +176,7 @@ int SPI::queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, i
 | 
			
		|||
 | 
			
		||||
void SPI::start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event)
 | 
			
		||||
{
 | 
			
		||||
    aquire();
 | 
			
		||||
    _acquire();
 | 
			
		||||
    _callback = callback;
 | 
			
		||||
    _irq.callback(&SPI::irq_handler_asynch);
 | 
			
		||||
    spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -270,6 +270,12 @@ protected:
 | 
			
		|||
    int _bits;
 | 
			
		||||
    int _mode;
 | 
			
		||||
    int _hz;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    /* Private acquire fucntion without locking/unlocking
 | 
			
		||||
     * Implemented in order to avoid duplicate locking and boost performance
 | 
			
		||||
     */
 | 
			
		||||
    void _acquire(void);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace mbed
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue