Skip to content
Snippets Groups Projects
Commit acd5f82f authored by Per Lindgren's avatar Per Lindgren
Browse files

dma works, now working on network order

parent c5876c19
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,23 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "attach",
"name": "Debug x/nested.elf",
"gdbpath": "/usr/bin/arm-none-eabi-gdb",
"executable": "./target/x/debug/examples/nested.elf",
"target": ":3333",
"remote": true,
"autorun": [
"monitor reset init",
"monitor arm semihosting enable",
"monitor tpiu config internal /tmp/itm.log uart off 16000000",
"monitor itm port 0 on",
"load"
],
"cwd": "${workspaceRoot}"
},
{
"type": "gdb",
"request": "attach",
......
......@@ -10,6 +10,7 @@
#![feature(attr_literals)]
//extern crate bare_metal;
//#[macro_use]
use core::ops::Deref;
extern crate cortex_m;
#[macro_use]
......@@ -32,7 +33,8 @@ const E_BLOCK: Block = [0; 4];
// Number of blocks in each buffer
const NR_BLOCKS: usize = 1;
const SIZE_BLOCKS: usize = (mem::size_of::<Block>() as u32 * (NR_BLOCKS as u32)) as usize;
const SIZE_BYTES: usize = (mem::size_of::<Block>() as u32 * (NR_BLOCKS as u32)) as usize;
const SIZE_WORDS: usize = (SIZE_BYTES as u32 / 4) as usize;
app! {
device: stm32f413,
......@@ -76,6 +78,13 @@ fn write_key(aes: &stm32f413::AES, key: &Key) {
aes.keyr3.write(|w| unsafe { w.bits(key[3]) });
}
fn write_key_little_endian(aes: &stm32f413::AES, key: &Key) {
aes.keyr0.write(|w| unsafe { w.bits(key[3]) });
aes.keyr1.write(|w| unsafe { w.bits(key[2]) });
aes.keyr2.write(|w| unsafe { w.bits(key[1]) });
aes.keyr3.write(|w| unsafe { w.bits(key[0]) });
}
fn pr(p: Buffer<[Block; NR_BLOCKS], Dma2Channel6>) {}
// Interrupt for AES_IN (transfer from memory to peripheral)
fn aes_in_done(t: &mut Threshold, r: DMA2_STREAM6::Resources) {
......@@ -89,13 +98,12 @@ fn aes_in_done(t: &mut Threshold, r: DMA2_STREAM6::Resources) {
dma2.s6cr.modify(|_, w| w.en().clear_bit());
});
// r.AES_IN_BUFFER.claim(_t, |aes_in, _| {
// let b_in: &mut [Block; NR_BLOCKS] = &mut aes_in.borrow_mut();
// let q: &mut [u8; SIZE_BLOCKS] = unsafe { mem::transmute::<_, _>(b_in.as_mut_ptr()) };
// ipln!(" in {:?}", core::str::from_utf8(&q[..]));
// });
r.AES_IN_BUFFER.claim(t, |aes_in, _| {
let q: &[u8; SIZE_BYTES] =
unsafe { mem::transmute::<_, _>(aes_in.deref().borrow().as_ptr()) };
ipln!("q {:?}", q.len());
ipln!("q {:?}", core::str::from_utf8(&q[..]));
});
}
// Interrupt for AES_OUT (transfer from peripheral to memory)
......@@ -108,7 +116,26 @@ fn aes_out_done(t: &mut Threshold, r: DMA2_STREAM5::Resources) {
// disable stream 5
dma2.s5cr.modify(|_, w| w.en().clear_bit());
});
r.AES_OUT_BUFFER.claim(t, |aes_in, _| {
let q: &[u8; SIZE_BYTES] =
unsafe { mem::transmute::<_, _>(aes_in.deref().borrow().as_ptr()) };
ip!(" q.len {:?} : ", q.len());
for d in q {
ip!("{:02x}", d);
}
ipln!();
let q: &[u32; SIZE_WORDS] =
unsafe { mem::transmute::<_, _>(aes_in.deref().borrow().as_ptr()) };
ip!(" out.len {:?} : ", q.len());
for d in q {
ip!("{:08x}", d);
}
ipln!();
});
}
// ea0f6c92 5fd6af05 9ad23159 9c3f6b9c
fn encrypt_ecb_dma<B>(
aes: &stm32f413::AES,
......@@ -134,7 +161,7 @@ fn encrypt_ecb_dma<B>(
// AES_IN (transfer from memory to peripheral)
let b_in: &[Block] = b_in.lock();
dma2.s6ndtr.write(|w| unsafe { w.ndt().bits(8) });
dma2.s6ndtr.write(|w| unsafe { w.ndt().bits(4) });
dma2.s6par
.write(|w| unsafe { w.bits(&aes.dinr as *const _ as u32) });
dma2.s6m0ar
......@@ -144,7 +171,7 @@ fn encrypt_ecb_dma<B>(
// AES_OUT (transfer from peripheral to memory)
let b_out: &[Block] = b_out.lock();
dma2.s5ndtr.write(|w| unsafe { w.ndt().bits(8) });
dma2.s5ndtr.write(|w| unsafe { w.ndt().bits(4) });
dma2.s5par
.write(|w| unsafe { w.bits(&aes.doutr as *const _ as u32) });
dma2.s5m0ar
......@@ -158,32 +185,33 @@ fn encrypt_ecb_dma<B>(
.modify(|_, w| w.dmainen().set_bit().dmaouten().set_bit().en().set_bit());
}
// fn copy_to_blocks(b_in: &mut) {
// }
// dma version
fn init(p: init::Peripherals, r: init::Resources) {
fn init_dma(p: init::Peripherals, r: init::Resources) {
ipln!("start");
// enable clocking of AES
p.RCC.ahb2enr.write(|w| w.crypen().set_bit());
// errata, device might not be ready
// rtfm::dsb(); // not implemented/exported, so we wait instead
wait(1000);
// r.AES_IN_BUFFER
// .borrow_mut()
// .clone_from_slice(&[E_BLOCK; NR_BLOCKS]);
{
let s: &[u8] = "Merry X-Mas 2017".as_bytes();
let b_in: &mut [Block; NR_BLOCKS] = &mut r.AES_IN_BUFFER.borrow_mut();
let q: &mut [u8; SIZE_BLOCKS] = unsafe { mem::transmute::<_, _>(b_in.as_mut_ptr()) };
//let s: &[u8] = b"aaaaaaaaaaaaaaaa";
//let s: &[u8] = &[0; 16];
let b_in: &mut [Block; NR_BLOCKS] = &mut r.AES_IN_BUFFER.borrow_mut();
let q: &mut [u8; SIZE_BYTES] = unsafe { mem::transmute::<_, _>(b_in.as_mut_ptr()) };
q[..s.len()].clone_from_slice(s);
}
{
let q: &[u8; SIZE_BYTES] =
unsafe { mem::transmute::<_, _>(r.AES_IN_BUFFER.borrow().as_ptr()) };
ipln!(" in.len {:?}", q.len());
ipln!(" in {:?}", core::str::from_utf8(&q[..]));
}
// enable clocking of DMA2, RM0430 Table 31. DMA2 request mapping
p.RCC.ahb1enr.modify(|_, w| w.dma2en().set_bit());
let dma2 = p.DMA2;
......@@ -291,7 +319,7 @@ fn _process(aes: &stm32f413::AES, b_in: &[Block], b_out: &mut [Block]) {
fn _encrypt_ecb(aes: &stm32f413::AES, key: &Key, b_in: &[Block], b_out: &mut [Block]) {
// clear complete and disable AES;
aes.cr.write(|w| w.ccfc().set_bit());
write_key(aes, key);
write_key_little_endian(aes, key);
// start encrytion, mode 1, ECB by default
aes.cr.modify(|_, w| w.en().set_bit());
......@@ -303,7 +331,7 @@ fn _decrypt_ecb(aes: &stm32f413::AES, key: &Key, b_in: &[Block], b_out: &mut [Bl
aes.cr.write(|w| w.ccfc().set_bit());
// mode 4. ECB
aes.cr.write(|w| unsafe { w.mode().bits(0b11) });
write_key(aes, key);
write_key_little_endian(aes, key);
// start encrytion, mode 1, ECB by default
aes.cr.modify(|_, w| w.en().set_bit());
......@@ -317,13 +345,34 @@ fn _init(p: init::Peripherals, _r: init::Resources) {
// errata, device might not be ready
// rtfm::dsb(); // not implemented/exported
wait(1000);
let _key: Block = [0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef];
let key = [0; 4];
// stored with MSW first for simple comparison with http://aes.online-domain-tools.com/
// writes key with write_key_little_endian
let key = [0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef];
let key = [0; 4];
p.AES.cr.modify(|_, w| w.en().set_bit());
//let mut b1 = [[0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]];
let mut b1 = [[0; 4]];
// b1 is initially the plain data
let mut b1 = [[0; 4]]; // works
let mut b1 = [[0x1111_1111; 4]]; // works
let mut b1 = [[0x1111_1111, 0x1111_1111, 0x1111_1111, 0]]; // works
let mut b1 = [[0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]]; // works
{
let s: &[u8] = "Merry X-Mas 2017".as_bytes();
let s: &[u8] = "rreM-X y saM7102".as_bytes();
//let s: &[u8] = b"aaaaaaaaaaaaaaaa";
//let s: &[u8] = &[0; 16];
let q: &mut [u8; SIZE_BYTES] = unsafe { mem::transmute::<_, _>(b1.as_mut_ptr()) };
q[..s.len()].clone_from_slice(s);
}
let q: &[u8; SIZE_BYTES] = unsafe { mem::transmute::<_, _>(b1.as_ptr()) };
ipln!("q {:?}", q.len());
ipln!("q {:?}", core::str::from_utf8(&q[..]));
let mut b2 = [[0; 4]];
_encrypt_ecb(&p.AES, &key, &b1, &mut b2);
......@@ -336,9 +385,15 @@ fn _init(p: init::Peripherals, _r: init::Resources) {
#[inline(never)]
fn idle(_t: &mut Threshold, _r: idle::Resources) -> ! {
//use rtfm::Resource;
ipln!("idle");
rtfm::bkpt();
loop {
rtfm::wfi();
}
}
fn init(p: init::Peripherals, r: init::Resources) {
//init_dma(p, r);
_init(p, r);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment