Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bus error #363

Open
kalyanbhetwal opened this issue Apr 1, 2024 · 0 comments
Open

Bus error #363

kalyanbhetwal opened this issue Apr 1, 2024 · 0 comments

Comments

@kalyanbhetwal
Copy link

kalyanbhetwal commented Apr 1, 2024

I am trying to interface stm32f3discovery with I2C F-RAM(MB85RC256V).
Below is the snippet of the code.

//! Example of using I2C.
//! Scans available I2C devices on bus and print the result.
//! Appropriate pull-up registers should be installed on I2C bus.
//! Target board: STM32F3DISCOVERY

#![no_std]
#![no_main]

use panic_halt as _;
use core::ops::Range;
use cortex_m_rt::entry;
use cortex_m_semihosting::{hprint, hprintln};

use stm32f3xx_hal_v2::{self as hal, pac, prelude::*, i2c::Error};

const VALID_ADDR_RANGE: Range = 0x08..0x78;
const MEMORY_ADDRESS: u16 = 0x0000; // Address to write/read data to/from

fn check_i2c_error(error: Error) -> Result<(), ()> {
match error {
Error::Arbitration => {
hprintln!("Arbitration loss occurred").unwrap();
Err(())
}
Error::Bus => {
hprintln!("Bus error occurred").unwrap();
Err(())
}
Error::Busy => {
hprintln!("Bus is busy").unwrap();
Err(())
}
Error::Nack => {
hprintln!("NACK received during transfer").unwrap();
Err(())
}
_ => {
hprintln!("Unknown error occurred").unwrap();
Err(())
}

}

}

#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);

// Configure I2C1
let pins = (
    gpiob.pb8.into_af4(&mut gpiob.moder, &mut gpiob.afrh), // SCL
    gpiob.pb9.into_af4(&mut gpiob.moder, &mut gpiob.afrh), // SDA
);
let mut i2c = hal::i2c::I2c::new(dp.I2C1, pins, 100.khz(), clocks, &mut rcc.apb1);
let fram_address = 0x50;

let data_to_write = [0x21, 0x22, 0x23];

if let Err(_) = i2c.write(fram_address, &data_to_write) {
    check_i2c_error(Error::Bus).unwrap();
} else {
    hprintln!("Write operation successful").unwrap();
}

// Example read operation
let mut data_read = [0u8; 3];
if let Err(e) = i2c.read(fram_address, &mut data_read) {
hprintln!("Error reading data: {:?}", e).unwrap();
}else{
hprintln!("Success!!").unwrap();
}

hprintln!().unwrap();
hprintln!("Done!").unwrap();

loop {
   
}

}

I always receive "Bus error occurred" while trying to read data.
I have used Arduino and F-RAM which works as expected.

I would truly appreciate any help in this matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant