Hello,
I am new to this rust language for embedded targets and I was trying out ways it can panic and return the information back to the file the function is called from (in my case it is .c file).
What I am trying to achieve is, if you try to access an element in an array that doesn't exist, usually rust will panic and give error. But, if the index is being passed at runtime, then rust will compile the code, but will enter into panic and stay forever, when you flash the software.
Is there a way to know, why it panicked or some error information that can be provided back to C and then may be reset the target?
below is the rust code that I am trying to work with panics
#[no_mangle]
pub extern "C" fn rust_fn(idx: *const u8) -> u8
{
let mut data: [u8; 32] = [0; 32];
data[0] = 42; // hex val - 2A
data[7] = 26; // hex val - 1A
if idx.is_null()
{
return E_PARAM;
}
let idx_deref: usize = unsafe { *idx as usize };
unsafe
{
core::ptr::write_volatile(&mut data[idx_deref], 64); // hex val - 40
}
return E_OK;
}
#[no_mangle]
pub extern "C" fn get_runtime_idx() -> usize
{
let mut n: usize = 0;
for i in 0..5
{
n += i;
}
return n+50; // value of n is 10
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> !
{
LAST_ERROR.store(E_PANIC_OOB, Ordering::Relaxed);
loop {} // infinite loop to halt execution on panic
}
Below is the C code where these 2 functions are being called:
uint8 idx = get_runtime_idx();
volatile int ret_code = rust_fn(&idx);
Since the code goes into panic and never returns, I cannot read the ret_code or any error to know the cause.
Any ideas or help into the topic will be really helpful and is greatly appreciated.
Thanks