Contiki 2.5
asm.c
1 /*
2  * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
3  * to the MC1322x project (http://mc1322x.devl.org)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * This file is part of libmc1322x: see http://mc1322x.devl.org
31  * for details.
32  *
33  *
34  */
35 
36 #include <mc1322x.h>
37 #include <board.h>
38 #include <stdio.h>
39 
40 #include "tests.h"
41 #include "config.h"
42 
43 void asm_isr(void) {
44  printf("asm isr\n\r");
45  ASM->CONTROL0bits.CLEAR_IRQ = 1;
46 }
47 
48 
49 void main(void) {
50  volatile int i;
51 
52  /* trim the reference osc. to 24MHz */
53  trim_xtal();
54 
55  uart_init(INC, MOD, SAMP);
56 
57  vreg_init();
58 
59  enable_irq(ASM);
60 
61  print_welcome("asm");
62 
63  printf("ASM Control 0: %08x\n\r", (unsigned int)ASM->CONTROL0);
64  printf("ASM Control 1: %08x\n\r", (unsigned int)ASM->CONTROL1);
65  printf("ASM Status: %08x\n\r", (unsigned int)ASM->STATUS);
66  printf("ASM Test pass: %d\n\r", ASM->STATUSbits.TEST_PASS);
67 
68  /* ASM module is disabled until self-test passes */
69  printf("\n\r*** ASM self-test ***\n\r");
70 
71  ASM->CONTROL1bits.ON = 1;
72  ASM->CONTROL1bits.SELF_TEST = 1;
73  ASM->CONTROL0bits.START = 1;
74 
75  /* Self test takes 3330 periph. clocks (default 24Mhz) */
76  /* to complete. This doesn't wait 3330 exactly, but should be long enough */
77 
78  for(i = 0; i < 3330; i++) { continue; }
79 
80  printf("ASM Test pass: %d\n\r", ASM->STATUSbits.TEST_PASS);
81 
82  /* must clear the self test bit when done */
83  ASM->CONTROL1bits.SELF_TEST = 0;
84 
85 
86  /* ASM starts in "BOOT" mode which uses an internal secret key
87  * to load encrypted data from an external source */
88  /* must set to NORMAL mode */
89  ASM->CONTROL1bits.NORMAL_MODE = 1;
90 
91  /* setting the bypass bit will disable the encryption */
92  /* bypass defaults to off */
93  ASM->CONTROL1bits.BYPASS = 0;
94 
95  printf("\n\r*** set ASM key ***\n\r");
96  ASM->KEY0 = 0xccddeeff;
97  ASM->KEY1 = 0x8899aabb;
98  ASM->KEY2 = 0x44556677;
99  ASM->KEY3 = 0x00112233;
100 
101  /* KEY registers appear to be write-only (which is a good thing) */
102  /* even though the datasheet says you can read them */
103  printf("ASM Key [3,2,1,0] : 0x%08x%08x%08x%08x\n",
104  (unsigned int) ASM->KEY3,
105  (unsigned int) ASM->KEY2,
106  (unsigned int) ASM->KEY1,
107  (unsigned int) ASM->KEY0);
108 
109  printf("\n\r*** CTR test ***\n\r");
110  printf("Encrypt\n\r");
111  ASM->CONTROL1bits.CTR = 1;
112 
113  ASM->DATA0 = 0xdeaddead;
114  ASM->DATA1 = 0xbeefbeef;
115  ASM->DATA2 = 0xfaceface;
116  ASM->DATA3 = 0x01234567;
117 
118  printf("ASM Data [3,2,1,0] : 0x%08x%08x%08x%08x\n",
119  (unsigned int) ASM->DATA3,
120  (unsigned int) ASM->DATA2,
121  (unsigned int) ASM->DATA1,
122  (unsigned int) ASM->DATA0);
123 
124  ASM->CTR0 = 0x33333333;
125  ASM->CTR1 = 0x22222222;
126  ASM->CTR2 = 0x11111111;
127  ASM->CTR3 = 0x00000000;
128 
129  printf("ASM CTR [3,2,1,0] : 0x%08x%08x%08x%08x\n",
130  (unsigned int) ASM->CTR3,
131  (unsigned int) ASM->CTR2,
132  (unsigned int) ASM->CTR1,
133  (unsigned int) ASM->CTR0);
134 
135  ASM->CONTROL0bits.START = 1;
136  while(ASM->STATUSbits.DONE == 0) { continue; }
137 
138  printf("ASM CTR RESULT [3,2,1,0]: 0x%08x%08x%08x%08x\n",
139  (unsigned int) ASM->CTR3_RESULT,
140  (unsigned int) ASM->CTR2_RESULT,
141  (unsigned int) ASM->CTR1_RESULT,
142  (unsigned int) ASM->CTR0_RESULT);
143 
144  printf("Decrypt\n\r");
145 
146  ASM->DATA0 = ASM->CTR0_RESULT;
147  ASM->DATA1 = ASM->CTR1_RESULT;
148  ASM->DATA2 = ASM->CTR2_RESULT;
149  ASM->DATA3 = ASM->CTR3_RESULT;
150 
151  ASM->CONTROL0bits.START = 1;
152  while(ASM->STATUSbits.DONE == 0) { continue; }
153 
154  printf("ASM CTR RESULT [3,2,1,0]: 0x%08x%08x%08x%08x\n",
155  (unsigned int) ASM->CTR3_RESULT,
156  (unsigned int) ASM->CTR2_RESULT,
157  (unsigned int) ASM->CTR1_RESULT,
158  (unsigned int) ASM->CTR0_RESULT);
159 
160  printf("\n\r*** CBC MAC generation ***\n\r");
161 
162  ASM->CONTROL1bits.CTR = 0;
163  ASM->CONTROL1bits.CBC = 1;
164 
165  /* CBC is like a hash */
166  /* it doesn't use the CTR data */
167  /* the accumulated MAC is in the MAC registers */
168  /* you must use the CLEAR bit to reset the MAC state */
169 
170  ASM->DATA0 = 0xdeaddead;
171  ASM->DATA1 = 0xbeefbeef;
172  ASM->DATA2 = 0xfaceface;
173  ASM->DATA3 = 0x01234567;
174 
175  ASM->CONTROL0bits.CLEAR = 1;
176 
177  ASM->CONTROL0bits.START = 1;
178  while(ASM->STATUSbits.DONE == 0) { continue; }
179 
180  printf("ASM CBC RESULT [3,2,1,0]: 0x%08x%08x%08x%08x\n",
181  (unsigned int) ASM->CBC3_RESULT,
182  (unsigned int) ASM->CBC2_RESULT,
183  (unsigned int) ASM->CBC1_RESULT,
184  (unsigned int) ASM->CBC0_RESULT);
185 
186  printf("\n\r*** CCM (CTR+CBC) ***\n\r");
187 
188  ASM->CONTROL1bits.CTR = 1;
189  ASM->CONTROL1bits.CBC = 1;
190  ASM->CONTROL0bits.CLEAR = 1;
191 
192  ASM->CONTROL0bits.START = 1;
193  while(ASM->STATUSbits.DONE == 0) { continue; }
194 
195  printf("ASM CTR RESULT [3,2,1,0]: 0x%08x%08x%08x%08x\n",
196  (unsigned int) ASM->CTR3_RESULT,
197  (unsigned int) ASM->CTR2_RESULT,
198  (unsigned int) ASM->CTR1_RESULT,
199  (unsigned int) ASM->CTR0_RESULT);
200 
201  printf("ASM CBC RESULT [3,2,1,0]: 0x%08x%08x%08x%08x\n",
202  (unsigned int) ASM->CBC3_RESULT,
203  (unsigned int) ASM->CBC2_RESULT,
204  (unsigned int) ASM->CBC1_RESULT,
205  (unsigned int) ASM->CBC0_RESULT);
206 
207  while(1) {
208  }
209 }