blob: 30099d3663954a63d63fb83f15dfaf23c61a5831 [file] [log] [blame]
Damiened656052013-10-13 00:42:20 +01001#include <stm32f4xx.h>
2#include <stm32f4xx_rcc.h>
Damien00ff04f2013-10-19 14:40:54 +01003#include <stm32f4xx_gpio.h>
4#include <stm_misc.h>
Damiened656052013-10-13 00:42:20 +01005#include "std.h"
6
Damien995b8aa2013-10-18 23:44:05 +01007#include "misc.h"
Damien00ff04f2013-10-19 14:40:54 +01008#include "systick.h"
Damien995b8aa2013-10-18 23:44:05 +01009#include "led.h"
Damien00ff04f2013-10-19 14:40:54 +010010#include "lcd.h"
Damien995b8aa2013-10-18 23:44:05 +010011#include "storage.h"
Damiened656052013-10-13 00:42:20 +010012
Damien4a175e12013-10-17 22:50:21 +010013static void impl02_c_version() {
Damiened656052013-10-13 00:42:20 +010014 int x = 0;
15 while (x < 400) {
16 int y = 0;
17 while (y < 400) {
18 volatile int z = 0;
19 while (z < 400) {
20 z = z + 1;
21 }
22 y = y + 1;
23 }
24 x = x + 1;
25 }
26}
27
28void set_bits(__IO uint32_t *addr, uint32_t shift, uint32_t mask, uint32_t value) {
29 uint32_t x = *addr;
30 x &= ~(mask << shift);
31 x |= (value << shift);
32 *addr = x;
33}
34
35void gpio_init() {
36 RCC->AHB1ENR |= RCC_AHB1ENR_CCMDATARAMEN | RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOAEN;
37}
38
Damien00ff04f2013-10-19 14:40:54 +010039/*
Damiened656052013-10-13 00:42:20 +010040void gpio_pin_af(GPIO_TypeDef *gpio, uint32_t pin, uint32_t af) {
41 // set the AF bits for the given pin
42 // pins 0-7 use low word of AFR, pins 8-15 use high word
43 set_bits(&gpio->AFR[pin >> 3], 4 * (pin & 0x07), 0xf, af);
44}
Damien00ff04f2013-10-19 14:40:54 +010045*/
Damiened656052013-10-13 00:42:20 +010046
Damien4a175e12013-10-17 22:50:21 +010047static void mma_init() {
Damien00ff04f2013-10-19 14:40:54 +010048 // XXX
Damiened656052013-10-13 00:42:20 +010049 RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // enable I2C1
Damien00ff04f2013-10-19 14:40:54 +010050 //gpio_pin_init(GPIOB, 6 /* B6 is SCL */, 2 /* AF mode */, 1 /* open drain output */, 1 /* 25 MHz */, 0 /* no pull up or pull down */);
51 //gpio_pin_init(GPIOB, 7 /* B7 is SDA */, 2 /* AF mode */, 1 /* open drain output */, 1 /* 25 MHz */, 0 /* no pull up or pull down */);
52 //gpio_pin_af(GPIOB, 6, 4 /* AF 4 for I2C1 */);
53 //gpio_pin_af(GPIOB, 7, 4 /* AF 4 for I2C1 */);
Damiened656052013-10-13 00:42:20 +010054
55 // get clock speeds
56 RCC_ClocksTypeDef rcc_clocks;
57 RCC_GetClocksFreq(&rcc_clocks);
58
59 // disable the I2C peripheral before we configure it
60 I2C1->CR1 &= ~I2C_CR1_PE;
61
62 // program peripheral input clock
63 I2C1->CR2 = 4; // no interrupts; 4 MHz (hopefully!) (could go up to 42MHz)
64
65 // configure clock control reg
66 uint32_t freq = rcc_clocks.PCLK1_Frequency / (100000 << 1); // want 100kHz, this is the formula for freq
67 I2C1->CCR = freq; // standard mode (speed), freq calculated as above
68
69 // configure rise time reg
70 I2C1->TRISE = (rcc_clocks.PCLK1_Frequency / 1000000) + 1; // formula for trise, gives maximum rise time
71
72 // enable the I2C peripheral
73 I2C1->CR1 |= I2C_CR1_PE;
74
75 // set START bit in CR1 to generate a start cond!
76}
77
Damien4a175e12013-10-17 22:50:21 +010078static uint32_t i2c_get_sr() {
Damiened656052013-10-13 00:42:20 +010079 // must read SR1 first, then SR2, as the read can clear some flags
80 uint32_t sr1 = I2C1->SR1;
81 uint32_t sr2 = I2C1->SR2;
82 return (sr2 << 16) | sr1;
83}
84
Damien4a175e12013-10-17 22:50:21 +010085static void mma_restart(uint8_t addr, int write) {
Damiened656052013-10-13 00:42:20 +010086 // send start condition
87 I2C1->CR1 |= I2C_CR1_START;
88
89 // wait for BUSY, MSL and SB --> Slave has acknowledged start condition
90 while ((i2c_get_sr() & 0x00030001) != 0x00030001) {
91 }
92
93 if (write) {
94 // send address and write bit
95 I2C1->DR = (addr << 1) | 0;
96 // wait for BUSY, MSL, ADDR, TXE and TRA
97 while ((i2c_get_sr() & 0x00070082) != 0x00070082) {
98 }
99 } else {
100 // send address and read bit
101 I2C1->DR = (addr << 1) | 1;
102 // wait for BUSY, MSL and ADDR flags
103 while ((i2c_get_sr() & 0x00030002) != 0x00030002) {
104 }
105 }
106}
107
Damien4a175e12013-10-17 22:50:21 +0100108static void mma_start(uint8_t addr, int write) {
Damiened656052013-10-13 00:42:20 +0100109 // wait until I2C is not busy
110 while (I2C1->SR2 & I2C_SR2_BUSY) {
111 }
112
113 // do rest of start
114 mma_restart(addr, write);
115}
116
Damien4a175e12013-10-17 22:50:21 +0100117static void mma_send_byte(uint8_t data) {
Damiened656052013-10-13 00:42:20 +0100118 // send byte
119 I2C1->DR = data;
120 // wait for TRA, BUSY, MSL, TXE and BTF (byte transmitted)
121 int timeout = 1000000;
122 while ((i2c_get_sr() & 0x00070084) != 0x00070084) {
123 if (timeout-- <= 0) {
124 printf("mma_send_byte timed out!\n");
125 break;
126 }
127 }
128}
129
Damien4a175e12013-10-17 22:50:21 +0100130static uint8_t mma_read_ack() {
Damiened656052013-10-13 00:42:20 +0100131 // enable ACK of received byte
132 I2C1->CR1 |= I2C_CR1_ACK;
133 // wait for BUSY, MSL and RXNE (byte received)
134 while ((i2c_get_sr() & 0x00030040) != 0x00030040) {
135 }
136 // read and return data
137 uint8_t data = I2C1->DR;
138 return data;
139}
140
Damien4a175e12013-10-17 22:50:21 +0100141static uint8_t mma_read_nack() {
Damiened656052013-10-13 00:42:20 +0100142 // disable ACK of received byte (to indicate end of receiving)
143 I2C1->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);
144 // last byte should apparently also generate a stop condition
145 I2C1->CR1 |= I2C_CR1_STOP;
146 // wait for BUSY, MSL and RXNE (byte received)
147 while ((i2c_get_sr() & 0x00030040) != 0x00030040) {
148 }
149 // read and return data
150 uint8_t data = I2C1->DR;
151 return data;
152}
153
Damien4a175e12013-10-17 22:50:21 +0100154static void mma_stop() {
Damiened656052013-10-13 00:42:20 +0100155 // send stop condition
156 I2C1->CR1 |= I2C_CR1_STOP;
157}
158
Damiened656052013-10-13 00:42:20 +0100159#define PYB_USRSW_PORT (GPIOA)
Damien00ff04f2013-10-19 14:40:54 +0100160#define PYB_USRSW_PIN (GPIO_Pin_13)
Damiened656052013-10-13 00:42:20 +0100161
162void sw_init() {
163 // make it an input with pull-up
Damien00ff04f2013-10-19 14:40:54 +0100164 GPIO_InitTypeDef GPIO_InitStructure;
165 GPIO_InitStructure.GPIO_Pin = PYB_USRSW_PIN;
166 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
167 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
168 GPIO_Init(PYB_USRSW_PORT, &GPIO_InitStructure);
Damiened656052013-10-13 00:42:20 +0100169}
170
171int sw_get() {
Damien00ff04f2013-10-19 14:40:54 +0100172 if (PYB_USRSW_PORT->IDR & PYB_USRSW_PIN) {
Damiened656052013-10-13 00:42:20 +0100173 // pulled high, so switch is not pressed
174 return 0;
175 } else {
176 // pulled low, so switch is pressed
177 return 1;
178 }
179}
180
Damiened656052013-10-13 00:42:20 +0100181void __fatal_error(const char *msg) {
182 lcd_print_strn("\nFATAL ERROR:\n", 14);
183 lcd_print_strn(msg, strlen(msg));
184
185 for (;;) {
Damien995b8aa2013-10-18 23:44:05 +0100186 led_state(PYB_LED_R1, 1);
187 led_state(PYB_LED_R2, 0);
Damien00ff04f2013-10-19 14:40:54 +0100188 sys_tick_delay_ms(150);
Damien995b8aa2013-10-18 23:44:05 +0100189 led_state(PYB_LED_R1, 0);
190 led_state(PYB_LED_R2, 1);
Damien00ff04f2013-10-19 14:40:54 +0100191 sys_tick_delay_ms(150);
Damiened656052013-10-13 00:42:20 +0100192 }
193}
194
195#include "misc.h"
196#include "lexer.h"
197#include "mpyconfig.h"
198#include "parse.h"
199#include "compile.h"
200#include "runtime.h"
201
Damiened656052013-10-13 00:42:20 +0100202py_obj_t pyb_delay(py_obj_t count) {
Damien00ff04f2013-10-19 14:40:54 +0100203 sys_tick_delay_ms(rt_get_int(count));
Damiened656052013-10-13 00:42:20 +0100204 return py_const_none;
205}
206
207py_obj_t pyb_led(py_obj_t state) {
Damien995b8aa2013-10-18 23:44:05 +0100208 led_state(PYB_LED_G1, rt_is_true(state));
Damiened656052013-10-13 00:42:20 +0100209 return state;
210}
211
212py_obj_t pyb_sw() {
213 if (sw_get()) {
214 return py_const_true;
215 } else {
216 return py_const_false;
217 }
218}
Damiened656052013-10-13 00:42:20 +0100219
220#include "ff.h"
221FATFS fatfs0;
222
Damien152568b2013-10-16 00:46:39 +0100223#include "nlr.h"
Damien4a175e12013-10-17 22:50:21 +0100224
225/*
Damien152568b2013-10-16 00:46:39 +0100226void g(uint i) {
227 printf("g:%d\n", i);
228 if (i & 1) {
229 nlr_jump((void*)(42 + i));
230 }
231}
232void f() {
233 nlr_buf_t nlr;
234 int i;
235 for (i = 0; i < 4; i++) {
236 printf("f:loop:%d:%p\n", i, &nlr);
237 if (nlr_push(&nlr) == 0) {
238 // normal
239 //printf("a:%p:%p %p %p %u\n", &nlr, nlr.ip, nlr.sp, nlr.prev, nlr.ret_val);
240 g(i);
241 printf("f:lp:%d:nrm\n", i);
242 nlr_pop();
243 } else {
244 // nlr
245 //printf("b:%p:%p %p %p %u\n", &nlr, nlr.ip, nlr.sp, nlr.prev, nlr.ret_val);
246 printf("f:lp:%d:nlr:%d\n", i, (int)nlr.ret_val);
247 }
248 }
249}
250void nlr_test() {
251 f(1);
252}
Damien4a175e12013-10-17 22:50:21 +0100253*/
254
Damien00ff04f2013-10-19 14:40:54 +0100255void fatality() {
256 led_state(PYB_LED_R1, 1);
257 led_state(PYB_LED_G1, 1);
258 led_state(PYB_LED_R2, 1);
259 led_state(PYB_LED_G2, 1);
260}
Damiened656052013-10-13 00:42:20 +0100261
Damien00ff04f2013-10-19 14:40:54 +0100262static const char *fresh_boot_py =
263"# boot.py -- run on boot-up\n"
264"# can run arbitrary Python, but best to keep it minimal\n"
265"\n"
266"pyb.source_dir('/src')\n"
267"pyb.main('main.py')\n"
268"#pyb.usb_usr('VCP')\n"
269"#pyb.usb_msd(True, 'dual partition')\n"
270"#pyb.flush_cache(False)\n"
271"#pyb.error_log('error.txt')\n"
272;
Damiened656052013-10-13 00:42:20 +0100273
Damien00ff04f2013-10-19 14:40:54 +0100274// get lots of info about the board
275static void board_info() {
Damiened656052013-10-13 00:42:20 +0100276 // get and print clock speeds
277 // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
Damiened656052013-10-13 00:42:20 +0100278 {
279 RCC_ClocksTypeDef rcc_clocks;
280 RCC_GetClocksFreq(&rcc_clocks);
Damien00ff04f2013-10-19 14:40:54 +0100281 printf("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n", rcc_clocks.SYSCLK_Frequency, rcc_clocks.HCLK_Frequency, rcc_clocks.PCLK1_Frequency, rcc_clocks.PCLK2_Frequency);
Damien4a175e12013-10-17 22:50:21 +0100282 }
283
Damien995b8aa2013-10-18 23:44:05 +0100284 // to print info about memory
Damien00ff04f2013-10-19 14:40:54 +0100285 {
Damien4a175e12013-10-17 22:50:21 +0100286 extern void *_sidata;
287 extern void *_sdata;
288 extern void *_edata;
289 extern void *_sbss;
290 extern void *_ebss;
291 extern void *_estack;
292 extern void *_etext;
293 extern void *_heap_start;
Damien00ff04f2013-10-19 14:40:54 +0100294 printf("_sidata=%p\n", &_sidata);
295 printf("_sdata=%p\n", &_sdata);
296 printf("_edata=%p\n", &_edata);
297 printf("_sbss=%p\n", &_sbss);
298 printf("_ebss=%p\n", &_ebss);
299 printf("_estack=%p\n", &_estack);
300 printf("_etext=%p\n", &_etext);
301 printf("_heap_start=%p\n", &_heap_start);
302 }
303
304 // free space on flash
305 {
306 DWORD nclst;
307 FATFS *fatfs;
308 f_getfree("0:", &nclst, &fatfs);
309 printf("free=%u\n", (uint)(nclst * fatfs->csize * 512));
310 }
311}
312
313int main() {
314 // TODO disable JTAG
315
316 // basic sub-system init
317 sys_tick_init();
318 gpio_init();
319 led_init();
320
321 // turn on LED to indicate bootup
322 led_state(PYB_LED_G1, 1);
323
324 // more sub-system init
325 sw_init();
326 lcd_init();
327 storage_init();
328
329 // Python init
330 qstr_init();
331 rt_init();
332
333 // print a message
334 printf(" micro py board\n");
335
336 // local filesystem init
337 {
338 // try to mount the flash
339 FRESULT res = f_mount(&fatfs0, "0:", 1);
340 if (res == FR_OK) {
341 // mount sucessful
342 } else if (res == FR_NO_FILESYSTEM) {
343 // no filesystem, so create a fresh one
344
345 // LED on to indicate creation of LFS
346 led_state(PYB_LED_R2, 1);
347 uint32_t stc = sys_tick_counter;
348
349 res = f_mkfs("0:", 0, 0);
350 if (res == FR_OK) {
351 // success creating fresh LFS
352 } else {
353 __fatal_error("could not create LFS");
354 }
355
356 // keep LED on for at least 100ms
357 sys_tick_wait_at_least(stc, 100);
358 led_state(PYB_LED_R2, 0);
359 } else {
360 __fatal_error("could not access LFS");
Damien4a175e12013-10-17 22:50:21 +0100361 }
Damien00ff04f2013-10-19 14:40:54 +0100362 }
363
364 // make sure we have a /boot.py
365 {
366 FILINFO fno;
367 FRESULT res = f_stat("0:/boot.py", &fno);
368 if (res == FR_OK) {
369 if (fno.fattrib & AM_DIR) {
370 // exists as a directory
371 // TODO handle this case
372 // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
373 } else {
374 // exists as a file, good!
375 }
376 } else {
377 // doesn't exist, create fresh file
378
379 // LED on to indicate creation of boot.py
380 led_state(PYB_LED_R2, 1);
381 uint32_t stc = sys_tick_counter;
382
383 FIL fp;
384 f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
385 UINT n;
386 f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py), &n);
387 // TODO check we could write n bytes
388 f_close(&fp);
389
390 // keep LED on for at least 100ms
391 sys_tick_wait_at_least(stc, 100);
392 led_state(PYB_LED_R2, 0);
393 }
394 }
395
396 // run /boot.py
397 if (0) {
398 FIL fp;
399 f_open(&fp, "0:/boot.py", FA_READ);
400 UINT n;
401 char buf[20];
402 f_read(&fp, buf, 18, &n);
403 buf[n + 1] = 0;
404 printf("read %d\n%s", n, buf);
405 f_close(&fp);
406 }
407
408 // turn boot-up LED off
409 led_state(PYB_LED_G1, 0);
410
411 /*
412 for (;;) {
413 led_state(PYB_LED_G2, 1);
414 sys_tick_wait_at_least(sys_tick_counter, 500);
415 led_state(PYB_LED_G2, 0);
416 sys_tick_wait_at_least(sys_tick_counter, 500);
Damien4a175e12013-10-17 22:50:21 +0100417 }
Damien995b8aa2013-10-18 23:44:05 +0100418 */
Damiened656052013-10-13 00:42:20 +0100419
Damien00ff04f2013-10-19 14:40:54 +0100420 // USB
421 if (0) {
422 void usb_init();
423 usb_init();
424 }
Damiened656052013-10-13 00:42:20 +0100425
Damien00ff04f2013-10-19 14:40:54 +0100426 //printf("init;al=%u\n", m_get_total_bytes_allocated()); // 1600, due to qstr_init
427 //sys_tick_delay_ms(1000);
428
429 #if 0
Damiened656052013-10-13 00:42:20 +0100430 // Python!
Damien4a175e12013-10-17 22:50:21 +0100431 if (0) {
Damiened656052013-10-13 00:42:20 +0100432 //const char *pysrc = "def f():\n x=x+1\nprint(42)\n";
433 const char *pysrc =
434 // impl01.py
435 /*
436 "x = 0\n"
437 "while x < 400:\n"
438 " y = 0\n"
439 " while y < 400:\n"
440 " z = 0\n"
441 " while z < 400:\n"
442 " z = z + 1\n"
443 " y = y + 1\n"
444 " x = x + 1\n";
445 */
446 // impl02.py
Damien152568b2013-10-16 00:46:39 +0100447 /*
Damiened656052013-10-13 00:42:20 +0100448 "#@micropython.native\n"
449 "def f():\n"
450 " x = 0\n"
451 " while x < 400:\n"
452 " y = 0\n"
453 " while y < 400:\n"
454 " z = 0\n"
455 " while z < 400:\n"
456 " z = z + 1\n"
457 " y = y + 1\n"
458 " x = x + 1\n"
459 "f()\n";
Damien152568b2013-10-16 00:46:39 +0100460 */
Damiened656052013-10-13 00:42:20 +0100461 /*
462 "print('in python!')\n"
463 "x = 0\n"
464 "while x < 4:\n"
465 " pyb_led(True)\n"
466 " pyb_delay(201)\n"
467 " pyb_led(False)\n"
468 " pyb_delay(201)\n"
469 " x = x + 1\n"
470 "print('press me!')\n"
471 "while True:\n"
472 " pyb_led(pyb_sw())\n";
473 */
474 /*
475 // impl16.py
476 "@micropython.asm_thumb\n"
477 "def delay(r0):\n"
478 " b(loop_entry)\n"
479 " label(loop1)\n"
480 " movw(r1, 55999)\n"
481 " label(loop2)\n"
482 " subs(r1, r1, 1)\n"
483 " cmp(r1, 0)\n"
484 " bgt(loop2)\n"
485 " subs(r0, r0, 1)\n"
486 " label(loop_entry)\n"
487 " cmp(r0, 0)\n"
488 " bgt(loop1)\n"
489 "print('in python!')\n"
490 "@micropython.native\n"
491 "def flash(n):\n"
492 " x = 0\n"
493 " while x < n:\n"
494 " pyb_led(True)\n"
495 " delay(249)\n"
496 " pyb_led(False)\n"
497 " delay(249)\n"
498 " x = x + 1\n"
499 "flash(20)\n";
500 */
Damien152568b2013-10-16 00:46:39 +0100501 // impl18.py
502 /*
503 "# basic exceptions\n"
504 "x = 1\n"
505 "try:\n"
506 " x.a()\n"
507 "except:\n"
508 " print(x)\n";
509 */
510 // impl19.py
511 "# for loop\n"
512 "def f():\n"
513 " for x in range(400):\n"
514 " for y in range(400):\n"
515 " for z in range(400):\n"
516 " pass\n"
517 "f()\n";
Damiened656052013-10-13 00:42:20 +0100518
519 py_lexer_t *lex = py_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
520
521 if (0) {
522 while (!py_lexer_is_kind(lex, PY_TOKEN_END)) {
523 py_token_show(py_lexer_cur(lex));
524 py_lexer_to_next(lex);
Damien00ff04f2013-10-19 14:40:54 +0100525 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100526 }
527 } else {
528 // nalloc=1740;6340;6836 -> 140;4600;496 bytes for lexer, parser, compiler
529 printf("lex; al=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100530 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100531 py_parse_node_t pn = py_parse(lex, 0);
532 //printf("----------------\n");
533 printf("pars;al=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100534 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100535 //parse_node_show(pn, 0);
Damien995b8aa2013-10-18 23:44:05 +0100536 py_compile(pn, false);
Damiened656052013-10-13 00:42:20 +0100537 printf("comp;al=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100538 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100539
540 if (1) {
541 // execute it!
542
543 // add some functions to the python namespace
544 rt_store_name(qstr_from_str_static("pyb_delay"), rt_make_function_1(pyb_delay));
545 rt_store_name(qstr_from_str_static("pyb_led"), rt_make_function_1(pyb_led));
546 rt_store_name(qstr_from_str_static("pyb_sw"), rt_make_function_0(pyb_sw));
547
548 py_obj_t module_fun = rt_make_function_from_id(1);
549
Damien152568b2013-10-16 00:46:39 +0100550 // flash once
Damien995b8aa2013-10-18 23:44:05 +0100551 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100552 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100553 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100554
Damien152568b2013-10-16 00:46:39 +0100555 nlr_buf_t nlr;
556 if (nlr_push(&nlr) == 0) {
557 py_obj_t ret = rt_call_function_0(module_fun);
558 printf("done! got: ");
559 py_obj_print(ret);
560 printf("\n");
561 nlr_pop();
562 } else {
563 // uncaught exception
564 printf("exception: ");
565 py_obj_print((py_obj_t)nlr.ret_val);
566 printf("\n");
567 }
568
569 // flash once
Damien995b8aa2013-10-18 23:44:05 +0100570 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100571 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100572 led_state(PYB_LED_G1, 0);
Damien152568b2013-10-16 00:46:39 +0100573
Damien00ff04f2013-10-19 14:40:54 +0100574 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100575 printf("nalloc=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100576 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100577 }
578 }
579 }
580 #endif
581
582 // benchmark C version of impl02.py
583 if (0) {
Damien995b8aa2013-10-18 23:44:05 +0100584 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100585 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100586 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100587 impl02_c_version();
Damien995b8aa2013-10-18 23:44:05 +0100588 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100589 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100590 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100591 }
592
593 // MMA testing
594 if (0) {
595 printf("1");
596 mma_init();
597 printf("2");
598 mma_start(0x4c, 1);
599 printf("3");
600 mma_send_byte(0);
601 printf("4");
602 mma_stop();
603 printf("5");
604 mma_start(0x4c, 1);
605 printf("6");
606 mma_send_byte(0);
607 printf("7");
608 mma_restart(0x4c, 0);
609 for (int i = 0; i <= 0xa; i++) {
610 int data;
611 if (i == 0xa) {
612 data = mma_read_nack();
613 } else {
614 data = mma_read_ack();
615 }
616 printf(" %02x", data);
617 }
618 printf("\n");
619
620 mma_start(0x4c, 1);
621 mma_send_byte(7); // mode
622 mma_send_byte(1); // active mode
623 mma_stop();
624
625 for (;;) {
Damien00ff04f2013-10-19 14:40:54 +0100626 sys_tick_delay_ms(500);
Damiened656052013-10-13 00:42:20 +0100627
628 mma_start(0x4c, 1);
629 mma_send_byte(0);
630 mma_restart(0x4c, 0);
631 for (int i = 0; i <= 3; i++) {
632 int data;
633 if (i == 3) {
634 data = mma_read_nack();
635 printf(" %02x\n", data);
636 } else {
637 data = mma_read_ack() & 0x3f;
638 if (data & 0x20) {
639 data |= 0xc0;
640 }
641 printf(" % 2d", data);
642 }
643 }
644 }
645 }
646
Damiened656052013-10-13 00:42:20 +0100647 // SD card testing
648 if (0) {
649 //sdio_init();
650 }
651
Damiened656052013-10-13 00:42:20 +0100652 int i = 0;
653 int n = 0;
Damien00ff04f2013-10-19 14:40:54 +0100654 uint32_t stc = sys_tick_counter;
Damiened656052013-10-13 00:42:20 +0100655
656 for (;;) {
Damien00ff04f2013-10-19 14:40:54 +0100657 sys_tick_delay_ms(10);
Damiened656052013-10-13 00:42:20 +0100658 if (sw_get()) {
Damien00ff04f2013-10-19 14:40:54 +0100659 led_state(PYB_LED_G1, 1);
Damiened656052013-10-13 00:42:20 +0100660 i = 1 - i;
661 if (i) {
662 printf(" angel %05x.\n", n);
663 //usb_vcp_send("hello!\r\n", 8);
664 } else {
665 printf(" mishka %4u.\n", n);
666 //usb_vcp_send("angel!\r\n", 8);
667 }
668 n += 1;
669 } else {
Damien00ff04f2013-10-19 14:40:54 +0100670 led_state(PYB_LED_G1, 0);
671 }
672 if (sys_tick_has_passed(stc, 500)) {
673 stc = sys_tick_counter;
674 led_toggle(PYB_LED_G2);
Damiened656052013-10-13 00:42:20 +0100675 }
676 }
677
678 return 0;
679}