void write_bit( bool x ){ scl.set( 0 ); wait_half_period(); sda.set( x ); scl.set( 1 ); wait_half_period(); }
/// write and read to (and from) a spi chip // /// This function performs an n byte write-and-read operation on the /// SPI chip activated by the (active low) sel pin. /// /// When either writing or reading is not needed the corresponding /// argument can be a nullptr. void write_and_read( pin_out & sel, int n, const byte data_out[], byte data_in[] ) override { sel.set( 0 ); for( int i = 0; i < n; ++i ){ byte d = ( data_out == nullptr ) ? 0 : *data_out++; for( int j = 0; j < 8; ++j ){ mosi.set( ( d & 0x80 ) != 0 ); wait_half_period(); sclk.set( 1 ); wait_half_period(); d = d << 1; if( miso.get() ){ d |= 0x01; } sclk.set( 0 ); } if( data_in != nullptr ){ *data_in++ = d; } } wait_half_period(); sel.set( 1 ); wait_half_period(); }
void write_stop(){ scl.set( 0 ); wait_half_period(); sda.set( 0 ); wait_half_period(); scl.set( 1 ); wait_half_period(); sda.set( 1 ); wait_half_period(); }
bool read_bit(){ scl.set( 0 ); sda.set( 1 ); wait_half_period(); scl.set( 1 ); wait_half_period(); bool result = sda.get(); wait_half_period(); return result; }
void write_start(){ sda.set( 0 ); wait_half_period(); scl.set( 0 ); wait_half_period(); }