Helpful ReplyHot!PIC32MZ - Harmony-less, DMA and Crypto questions

Author
Edocecrous
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/07/13 13:25:16
  • Location: 0
  • Status: offline
2017/09/07 06:14:49 (permalink)
0

PIC32MZ - Harmony-less, DMA and Crypto questions

Hi,
 
I'm new to PIC, but not new to MCU-s and x86 system programming.Got a task to create a device with the PIC32MZ2048EFM144 starter kit (with LAN and Crypto).
 
While plenty documentation can be found, it is nowhere as detailed as other manufacturer's documents. You download the reference manual for your particular device and it keeps mentioning for further info you should look up the reference document...
 
Took me some time to write my own drivers/libraries, UART, I2C, Timers, I/O, Keypad and Oled display all working fine, however i run into some issues with DMA and Crypto engine and hoping for a PIC expert to enlighten me...
 
Problems:
1, I set up the DMA to transfer few bytes from text buffer A -> B, no interrupts, chaining or pattern match. This works fine, however if i try to do the same where source buffer is in Flash (static keyword at defining), it does not transfer anything.
 
2, This is a general problem, i'd like to use the Crypto Engine to do a simple SHA1 of a text string. Would it hurt Microchip to provide an example for each type of cypher and hash? Could someone show me some code that does an SHA1 without using interrupts (and Harmony)?
 
Thank you,
 
Edo
#1
Larry.Standage
Super Member
  • Total Posts : 724
  • Reward points : 0
  • Joined: 2011/12/30 09:50:47
  • Location: 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/07 13:57:22 (permalink) ☄ Helpfulby Edocecrous 2017/09/09 05:24:24
3.5 (2)
Can you provide your setup of the DMA for reading from Flash?
 
In the FRM, I put some snippets of setting up the Security Association and the Buffer Descriptors. The only real difference between executing any of the algorithms is the ALGO field in the Security Association, to tell it what to execute. That, and setting up the correct pointers in the Buffer Descriptor for where to put the output.
 
The hard part is making sure the SAs and BDs don't end up getting cached, or that you flush the cache, so that the engine can pick up the change and execute it. I'll see if I can set up a "simple" example for the starter kit.
#2
RISC
Super Member
  • Total Posts : 4404
  • Reward points : 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/07 16:54:52 (permalink)
0
Hi Edocecrous,
Did you mention that you also want to implement LAN connection (TCP/IP) without Harmony ?
Regards
#3
Howard Long
Super Member
  • Total Posts : 316
  • Reward points : 0
  • Joined: 2005/04/04 08:50:32
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/08 00:49:27 (permalink)
3.5 (2)
Sounds like a cache coherency problem? Can you share a small code example demonstrating the problem?
 
Are you sure your data is being accessed directly from ROM? You mention "static", but not "const". If it's static only, it'll apparently be "in RAM", initialised by the C startup code, but from a hardware perspective it may well not have been flushed from data cache to RAM yet. The DMA engine only accesses RAM directly, not through dcache.
 
Look up AN1600: http://ww1.microchip.com/...AppNotes/00001600C.pdf
#4
Edocecrous
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/07/13 13:25:16
  • Location: 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/08 03:39:10 (permalink)
0
Hello,
First of all, thank you for the answers and instant help!
 
@Howard: Thanks for the document, i'll study it and try different settings. I wrote static, but i meant static const.
So my code tries to use DMA to copy from FLASH to RAM i guess. My source operand is at 9D0030E8, that gets translated via & 0x1FFFFFFF to 1D0030E8, my destination is 80000C34 that gets translated to 00000C34. So then CACHE (if enabled) is used with RAM only, or also with FLASH?
 
@RISC: No, i just mentioned that i have a board which has the LAN daughterboard on it. At this moment i don't have to implement networking (but if i do, it will go without Harmony).
 
@Larry: Tried to attach a file but got a 'No permission to access to server..' error and my post also got lost. My DMA 'Copy' function is as follows:
[code]
void DMACopy(void * dst, void * src, unsigned int size){
unsigned int srcAddr;
unsigned int dstAddr;
srcAddr = (unsigned int) src & 0x1FFFFFFF;     //Physical address of src
dstAddr = (unsigned int) dst & 0x1FFFFFFF;     //Physical address of dst
//srcAddr=KVA_TO_PA(src);
//dstAddr=KVA_TO_PA(dst);
IEC1CLR=0x00010000;         // disable DMA channel 0 interrupts
IFS1CLR=0x00010000;         // clear existing DMA channel 0 interrupt flag
DMACON = 0x8000;            // dma module on
DCH0CON = 0x3;       //DMA Channel Disable, turns disabled after block transfer complete, priority 3 (highest)
DCH0ECON = 0;          // dma transfer is not triggered by irq, has to be forced, no pattern match
DCRCCON = 0;                // crc module off
DCH0INT = 0;                // interrupts disabled
DCH0SSA = srcAddr;       // source start address
DCH0DSA = dstAddr;        // destination start address
DCH0SSIZ = size;               // source size
DCH0DSIZ = size;               // destination size
DCH0CSIZ = size;               // cell size bytes transfered per event
DCH0INTCLR=0x00ff00ff;      //clear events and disable interrupts
DCH0CONSET = 0x80;      //channel on
DCH0ECONSET = 0x80;     //transfer on (CFORCE=1)
while (DCH0INT&0x4);
int DMAFlags=DCH0INT;
}
[/code]
And my global variables are set up like this:
[code]
static uint8_t screen[1024];
static const uint8_t background[4*1024] = {0x44, 0x1C, ...}
 [/code]
I'll look up FRM once figure out what is it. And waiting eagerly for your 'simple' example regarding caching for the starter kit.
 
To all, thank you for the help!
 
Csaba Bacskay
(Edocecrous)
 
 
 
post edited by Edocecrous - 2017/09/08 03:49:16
#5
andersm
Super Member
  • Total Posts : 2174
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/08 06:37:54 (permalink) ☄ Helpfulby Edocecrous 2017/09/09 05:27:59
3.5 (2)
EdocecrousSo then CACHE (if enabled) is used with RAM only, or also with FLASH?

Simplified, everything outside the KSEG1 memory range uses the cache. In further detail, the K0 bits in the CP0.Config register controls whether KSEG0 is cached or not, and page table attributes controls coherency for the mapped memory ranges.
#6
friesen
Super Member
  • Total Posts : 1645
  • Reward points : 0
  • Joined: 2008/05/08 05:23:35
  • Location: Indiana, USA
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/08 07:24:44 (permalink)
0
Don't some of the MZ devices have an errata regarding Flash and crypto?

Erik Friesen
#7
Larry.Standage
Super Member
  • Total Posts : 724
  • Reward points : 0
  • Joined: 2011/12/30 09:50:47
  • Location: 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/08 12:26:19 (permalink)
3 (1)
On EC, the output was in big-endian format, so you had to swap the output in code. On EF a bit was added so that the engine would do the swap automatically.
 
On DA, there is an issue with the bus when the crypto engine's DMA accesses Flash, and the only way I could work around it was to increase the Flash wait states.
#8
mrpackethead
Super Member
  • Total Posts : 595
  • Reward points : 0
  • Joined: 2007/04/01 23:33:39
  • Location: 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/09 00:15:53 (permalink)
0
This is an interesting thread.  Glad Larry is following, because his input on this topic might be useful.

I want to set up an encrypted bootloader on a PIC32MZ ( EFM Variants ).  These have the hardware crypto in them. Each PIC32 has some uniqueness built in ( The mac address for a start ) that coudl be used for some key information.  This would make distriubtion of the hex file to update devices much more secure..

I'm trying to work out though if there is enough nvram in the bootloader space to pull this off.
 
#9
Edocecrous
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/07/13 13:25:16
  • Location: 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/11 00:39:26 (permalink)
4.5 (2)
Hi,
 
The DMA problem was caused by the cache. The copy was from Flash to RAM, destination was 80000C34 virtual, C34 physical.The destination data didn't match the cache contents, so when checking the variable it showed the previous state.
 
Once i allocated the destination to be in KSEG1 [used __attribute__((coherent))], the copy was successful. I also would like to experiment with a cache invalidate routine, but first i have to find out which caching scheme is used in my setup right now and need more reading-up on the subject.
 
Regarding the Crypto engine, i know that the ALGO field in the SA controls what to use, but setting up all the buffers for the different algorithms and dealing with the caching issue Larry stated does pose a problem, so i hope he can write the examples he mentioned.
 
Right now the only thing i'd need from the Crypto engine is to do the SHA-1.
 
thx,
Edo
 
#10
Larry.Standage
Super Member
  • Total Posts : 724
  • Reward points : 0
  • Joined: 2011/12/30 09:50:47
  • Location: 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/12 17:00:59 (permalink)
3 (1)
Here's what I came up with for running a SHA-1 on a string.
 
Note that the engine seems to have to be reset every time a new hash is run. I tried just updating the buffer descriptor and rerunning it, but it always came up with a different result.
#11
Edocecrous
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/07/13 13:25:16
  • Location: 0
  • Status: offline
Re: PIC32MZ - Harmony-less, DMA and Crypto questions 2017/09/13 22:49:52 (permalink)
0
Thanks, that is great!!!
 
It works fine on my 32MZ :P
 
Poking through the wolfSSL it seems they also reset the engine after hashing.
 
Edo
 
#12
Jump to:
© 2017 APG vNext Commercial Version 4.5