pub fn new(core: u8, scl_pin: (char, u8), sda_pin: (char, u8), pullup: bool) -> Result<Self, ProgError> {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}
    let rcc = &peripheral_ptr.RCC;
  
    if !I2C_MAP.scl_pins.iter().zip(I2C_MAP.sda_pins.iter()).zip(I2C_MAP.cores.iter()).any(|i| i == ((&scl_pin, &sda_pin), &core)) {
      rprintln!("These pins are not available for I2C communication! | I2C::new()");
      return Err(ProgError::InvalidConfiguration);
    }

    unsafe {
      if PIN_CONF.contains(&scl_pin) || PIN_CONF.contains(&sda_pin) {
        rprintln!("These pins are already configured for another function! | I2C::new()");
        return Err(ProgError::InvalidConfiguration);
      }
      else {
        PIN_CONF.push(scl_pin).expect("Could not store pin number! | I2C::new()");
        PIN_CONF.push(sda_pin).expect("Could not store pin number! | I2C::new()");
      }
    }

    let scl = match pinmode_alternate_function(scl_pin, 4) {
      Ok(value) => {
        open_drain(&value, true);
        value
      },
      Err(_) => return Err(ProgError::Internal)
    };

    let sda = match pinmode_alternate_function(sda_pin, 4) {
      Ok(value) => {
        open_drain(&value, true);
        value
      },
      Err(_) => return Err(ProgError::Internal)
    };

    if pullup {
      set_bias(&scl, Pullup);
      set_bias(&sda, Pullup);
    }

    let (ccr_t, rise_t) = calc_i2c_freq(I2C_FREQ);
    
    match core {
      1 => {
        let i2c1 = &peripheral_ptr.I2C1;
        if rcc.apb1enr.read().i2c1en().is_enabled() {
          rprintln!("I2C{} is already configured! | I2C::new()", core);
          return Err(ProgError::AlreadyConfigured);
        }
        rcc.apb1enr.modify(|_, w| w.i2c1en().enabled());
        i2c1.cr2.modify(|_, w| unsafe {w.freq().bits(BUS_FREQ as u8)});
        i2c1.ccr.modify(|_, w| unsafe {w.ccr().bits(ccr_t as u16)});
        i2c1.trise.write(|w| w.trise().bits(rise_t as u8));
        // if addr > 0 {i2c1.oar1.modify(|_, w| w.add().bits((addr << 1).into()));}
        i2c1.cr1.modify(|_, w| {
          w.ack().set_bit();
          w.pe().enabled()
        });
      },
      2 => {
        let i2c2 = &peripheral_ptr.I2C2;
        if rcc.apb1enr.read().i2c2en().is_enabled() {
          rprintln!("I2C{} is already configured! | I2C::new()", core);
          return Err(ProgError::AlreadyConfigured);
        }
        rcc.apb1enr.modify(|_, w| w.i2c2en().enabled());
        i2c2.cr2.modify(|_, w| unsafe {w.freq().bits(BUS_FREQ as u8)});
        i2c2.ccr.modify(|_, w| unsafe {w.ccr().bits(ccr_t as u16)});
        i2c2.trise.write(|w| w.trise().bits(rise_t as u8));
        // if addr > 0 {i2c2.oar1.modify(|_, w| w.add().bits((addr << 1).into()));}
        i2c2.cr1.modify(|_, w| {
          w.ack().set_bit();
          w.pe().enabled()
        });
      },
      3 => {
        let i2c3 = &peripheral_ptr.I2C3;
        if rcc.apb1enr.read().i2c3en().is_enabled() {
          rprintln!("I2C{} is already configured! | I2C::new()", core);
          return Err(ProgError::AlreadyConfigured);
        }
        rcc.apb1enr.modify(|_, w| w.i2c3en().enabled());
        i2c3.cr2.modify(|_, w| unsafe {w.freq().bits(BUS_FREQ as u8)});
        i2c3.ccr.modify(|_, w| unsafe {w.ccr().bits(ccr_t as u16)});
        i2c3.trise.write(|w| w.trise().bits(rise_t as u8));
        // if addr > 0 {i2c3.oar1.modify(|_, w| w.add().bits((addr << 1).into()));}
        i2c3.cr1.modify(|_, w| {
          w.ack().set_bit();
          w.pe().enabled()
        });
      },
      _ => panic!("I2C{} is not a valid core! | I2C::new()", core)
    };

    return Ok(Self {
      core,
      _scl_pin: scl,
      _sda_pin: sda,
      tx_buffer: Vec::new(),
      rx_buffer: Vec::new(),
      tx_addr: 0,
      transmitting: false
    });
  }
/// Configure a I2C connection with one of the internal I2C peripherals.
/// 
/// This Method expects the used I2C core, two [pin identifiers](crate::include::pins) for the scl and sda-pins
/// and if the internal pullup-resistors should be used as parameters and returns the [I2C Struct](crate::i2c::I2C).
/// Specitfy the buffer capapcity with the turbofish operator. Panics if the core or pins are already used or invalid.
Configure a I2C connection with one of the internal I2C peripherals.
Configure a I2C connection with one of the internal I2C peripherals .
pub fn new(core: u8, scl_pin: (char, u8), sda_pin: (char, u8), pullup: bool) -> Result<Self, ProgError> {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}
    let rcc = &peripheral_ptr.RCC;
    if !I2C_MAP.scl_pins.iter().zip(I2C_MAP.sda_pins.iter()).zip(I2C_MAP.cores.iter()).any(|i| i == ((&scl_pin, &sda_pin), &core)) {
      rprintln!("These pins are not available for I2C communication! | I2C::new()");
      return Err(ProgError::InvalidConfiguration);
    }
    unsafe {
      if PIN_CONF.contains(&scl_pin) || PIN_CONF.contains(&sda_pin) {
        rprintln!("These pins are already configured for another function! | I2C::new()");
        return Err(ProgError::InvalidConfiguration);
      }
      else {
        PIN_CONF.push(scl_pin).expect("Could not store pin number! | I2C::new()");
        PIN_CONF.push(sda_pin).expect("Could not store pin number! | I2C::new()");
      }
    }
    let scl = match pinmode_alternate_function(scl_pin, 4) {
      Ok(value) => {
        open_drain(&value, true);
        value
      },
      Err(_) => return Err(ProgError::Internal)
    };
    let sda = match pinmode_alternate_function(sda_pin, 4) {
      Ok(value) => {
        open_drain(&value, true);
        value
      },
      Err(_) => return Err(ProgError::Internal)
    };
    if pullup {
      set_bias(&scl, Pullup);
      set_bias(&sda, Pullup);
    }
    let (ccr_t, rise_t) = calc_i2c_freq(I2C_FREQ);
    match core {
      1 => {
        let i2c1 = &peripheral_ptr.I2C1;
        if rcc.apb1enr.read().i2c1en().is_enabled() {
          rprintln!("I2C{} is already configured! | I2C::new()", core);
          return Err(ProgError::AlreadyConfigured);
        }
        rcc.apb1enr.modify(|_, w| w.i2c1en().enabled());
        i2c1.cr2.modify(|_, w| unsafe {w.freq().bits(BUS_FREQ as u8)});
        i2c1.ccr.modify(|_, w| unsafe {w.ccr().bits(ccr_t as u16)});
        i2c1.trise.write(|w| w.trise().bits(rise_t as u8));
        // if addr > 0 {i2c1.oar1.modify(|_, w| w.add().bits((addr << 1).into()));}
        i2c1.cr1.modify(|_, w| {
          w.ack().set_bit();
          w.pe().enabled()
        });
      },
      2 => {
        let i2c2 = &peripheral_ptr.I2C2;
        if rcc.apb1enr.read().i2c2en().is_enabled() {
          rprintln!("I2C{} is already configured! | I2C::new()", core);
          return Err(ProgError::AlreadyConfigured);
        }
        rcc.apb1enr.modify(|_, w| w.i2c2en().enabled());
        i2c2.cr2.modify(|_, w| unsafe {w.freq().bits(BUS_FREQ as u8)});
        i2c2.ccr.modify(|_, w| unsafe {w.ccr().bits(ccr_t as u16)});
        i2c2.trise.write(|w| w.trise().bits(rise_t as u8));
        // if addr > 0 {i2c2.oar1.modify(|_, w| w.add().bits((addr << 1).into()));}
        i2c2.cr1.modify(|_, w| {
          w.ack().set_bit();
          w.pe().enabled()
        });
      },
      3 => {
        let i2c3 = &peripheral_ptr.I2C3;
        if rcc.apb1enr.read().i2c3en().is_enabled() {
          rprintln!("I2C{} is already configured! | I2C::new()", core);
          return Err(ProgError::AlreadyConfigured);
        }
        rcc.apb1enr.modify(|_, w| w.i2c3en().enabled());
        i2c3.cr2.modify(|_, w| unsafe {w.freq().bits(BUS_FREQ as u8)});
        i2c3.ccr.modify(|_, w| unsafe {w.ccr().bits(ccr_t as u16)});
        i2c3.trise.write(|w| w.trise().bits(rise_t as u8));
        // if addr > 0 {i2c3.oar1.modify(|_, w| w.add().bits((addr << 1).into()));}
        i2c3.cr1.modify(|_, w| {
          w.ack().set_bit();
          w.pe().enabled()
        });
      },
      _ => panic!("I2C{} is not a valid core! | I2C::new()", core)
    };
    return Ok(Self {
      core,
      _scl_pin: scl,
      _sda_pin: sda,
      tx_buffer: Vec::new(),
      rx_buffer: Vec::new(),
      tx_addr: 0,
      transmitting: false
    });
  }
pub fn new ( core : u8 , scl_pin : ( char , u8 ) , sda_pin : ( char , u8 ) , pullup : bool ) -> Result < Self , ProgError > { let peripheral_ptr ; unsafe { peripheral_ptr = stm32f4 :: stm32f446 :: Peripherals :: steal ( ) ; } let rcc = & peripheral_ptr . RCC ; if ! I2C_MAP . scl_pins . iter ( ) . zip ( I2C_MAP . sda_pins . iter ( ) ) . zip ( I2C_MAP . cores . iter ( ) ) . any ( | i | i == ( ( & scl_pin , & sda_pin ) , & core ) ) { rprintln ! ( " " ) ; return Err ( ProgError :: InvalidConfiguration ) ; } unsafe { if PIN_CONF . contains ( & scl_pin ) || PIN_CONF . contains ( & sda_pin ) { rprintln ! ( " " ) ; return Err ( ProgError :: InvalidConfiguration ) ; } else { PIN_CONF . push ( scl_pin ) . expect ( " " ) ; PIN_CONF . push ( sda_pin ) . expect ( " " ) ; } } let scl = match pinmode_alternate_function ( scl_pin , 4 ) { Ok ( value ) => { open_drain ( & value , true ) ; value } , Err ( _ ) => return Err ( ProgError :: Internal ) } ; let sda = match pinmode_alternate_function ( sda_pin , 4 ) { Ok ( value ) => { open_drain ( & value , true ) ; value } , Err ( _ ) => return Err ( ProgError :: Internal ) } ; if pullup { set_bias ( & scl , Pullup ) ; set_bias ( & sda , Pullup ) ; } let ( ccr_t , rise_t ) = calc_i2c_freq ( I2C_FREQ ) ; match core { 1 => { let i2c1 = & peripheral_ptr . I2C1 ; if rcc . apb1enr . read ( ) . i2c1en ( ) . is_enabled ( ) { rprintln ! ( " " core ) ; return Err ( ProgError :: AlreadyConfigured ) ; } rcc . apb1enr . modify ( | _ , w | w . i2c1en ( ) . enabled ( ) ) ; i2c1 . cr2 . modify ( | _ , w | unsafe { w . freq ( ) . bits ( BUS_FREQ as u8 ) } ) ; i2c1 . ccr . modify ( | _ , w | unsafe { w . ccr ( ) . bits ( ccr_t as u16 ) } ) ; i2c1 . trise . write ( | w | w . trise ( ) . bits ( rise_t as u8 ) ) ; // if addr > 0 {i2c1.oar1.modify(|_, w| w.add().bits((addr << 1).into()));} i2c1 . cr1 . modify ( | _ , w | { w . ack ( ) . set_bit ( ) ; w . pe ( ) . enabled ( ) } ) ; } , 2 => { let i2c2 = & peripheral_ptr . I2C2 ; if rcc . apb1enr . read ( ) . i2c2en ( ) . is_enabled ( ) { rprintln ! ( " " core ) ; return Err ( ProgError :: AlreadyConfigured ) ; } rcc . apb1enr . modify ( | _ , w | w . i2c2en ( ) . enabled ( ) ) ; i2c2 . cr2 . modify ( | _ , w | unsafe { w . freq ( ) . bits ( BUS_FREQ as u8 ) } ) ; i2c2 . ccr . modify ( | _ , w | unsafe { w . ccr ( ) . bits ( ccr_t as u16 ) } ) ; i2c2 . trise . write ( | w | w . trise ( ) . bits ( rise_t as u8 ) ) ; // if addr > 0 {i2c2.oar1.modify(|_, w| w.add().bits((addr << 1).into()));} i2c2 . cr1 . modify ( | _ , w | { w . ack ( ) . set_bit ( ) ; w . pe ( ) . enabled ( ) } ) ; } , 3 => { let i2c3 = & peripheral_ptr . I2C3 ; if rcc . apb1enr . read ( ) . i2c3en ( ) . is_enabled ( ) { rprintln ! ( " " core ) ; return Err ( ProgError :: AlreadyConfigured ) ; } rcc . apb1enr . modify ( | _ , w | w . i2c3en ( ) . enabled ( ) ) ; i2c3 . cr2 . modify ( | _ , w | unsafe { w . freq ( ) . bits ( BUS_FREQ as u8 ) } ) ; i2c3 . ccr . modify ( | _ , w | unsafe { w . ccr ( ) . bits ( ccr_t as u16 ) } ) ; i2c3 . trise . write ( | w | w . trise ( ) . bits ( rise_t as u8 ) ) ; // if addr > 0 {i2c3.oar1.modify(|_, w| w.add().bits((addr << 1).into()));} i2c3 . cr1 . modify ( | _ , w | { w . ack ( ) . set_bit ( ) ; w . pe ( ) . enabled ( ) } ) ; } , _ => panic ! ( " " core ) } ; return Ok ( Self { core , _scl_pin : scl , _sda_pin : sda , tx_buffer : Vec :: new ( ) , rx_buffer : Vec :: new ( ) , tx_addr : 0 , transmitting : false } ) ; }
Configure a I2C connection with one of the internal I2C peripherals.
Configure a I2C connection with one of the internal I2C peripherals .
core
u8
scl_pin
(char, u8)
sda_pin
(char, u8)
pullup
bool
core
u8
scl_pin
(char, u8)
sda_pin
(char, u8)
pullup
bool