blob: ca14d1ce9d0fd4d991a2977f9a8179004c2abfe8 [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"
Damienfb42ec12013-10-19 15:37:09 +010012#include "usb.h"
Damiened656052013-10-13 00:42:20 +010013
Damien4a175e12013-10-17 22:50:21 +010014static void impl02_c_version() {
Damiened656052013-10-13 00:42:20 +010015 int x = 0;
16 while (x < 400) {
17 int y = 0;
18 while (y < 400) {
19 volatile int z = 0;
20 while (z < 400) {
21 z = z + 1;
22 }
23 y = y + 1;
24 }
25 x = x + 1;
26 }
27}
28
29void set_bits(__IO uint32_t *addr, uint32_t shift, uint32_t mask, uint32_t value) {
30 uint32_t x = *addr;
31 x &= ~(mask << shift);
32 x |= (value << shift);
33 *addr = x;
34}
35
36void gpio_init() {
37 RCC->AHB1ENR |= RCC_AHB1ENR_CCMDATARAMEN | RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOAEN;
38}
39
Damien00ff04f2013-10-19 14:40:54 +010040/*
Damiened656052013-10-13 00:42:20 +010041void gpio_pin_af(GPIO_TypeDef *gpio, uint32_t pin, uint32_t af) {
42 // set the AF bits for the given pin
43 // pins 0-7 use low word of AFR, pins 8-15 use high word
44 set_bits(&gpio->AFR[pin >> 3], 4 * (pin & 0x07), 0xf, af);
45}
Damien00ff04f2013-10-19 14:40:54 +010046*/
Damiened656052013-10-13 00:42:20 +010047
Damien4a175e12013-10-17 22:50:21 +010048static void mma_init() {
Damien00ff04f2013-10-19 14:40:54 +010049 // XXX
Damiened656052013-10-13 00:42:20 +010050 RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // enable I2C1
Damien00ff04f2013-10-19 14:40:54 +010051 //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 */);
52 //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 */);
53 //gpio_pin_af(GPIOB, 6, 4 /* AF 4 for I2C1 */);
54 //gpio_pin_af(GPIOB, 7, 4 /* AF 4 for I2C1 */);
Damiened656052013-10-13 00:42:20 +010055
56 // get clock speeds
57 RCC_ClocksTypeDef rcc_clocks;
58 RCC_GetClocksFreq(&rcc_clocks);
59
60 // disable the I2C peripheral before we configure it
61 I2C1->CR1 &= ~I2C_CR1_PE;
62
63 // program peripheral input clock
64 I2C1->CR2 = 4; // no interrupts; 4 MHz (hopefully!) (could go up to 42MHz)
65
66 // configure clock control reg
67 uint32_t freq = rcc_clocks.PCLK1_Frequency / (100000 << 1); // want 100kHz, this is the formula for freq
68 I2C1->CCR = freq; // standard mode (speed), freq calculated as above
69
70 // configure rise time reg
71 I2C1->TRISE = (rcc_clocks.PCLK1_Frequency / 1000000) + 1; // formula for trise, gives maximum rise time
72
73 // enable the I2C peripheral
74 I2C1->CR1 |= I2C_CR1_PE;
75
76 // set START bit in CR1 to generate a start cond!
77}
78
Damien4a175e12013-10-17 22:50:21 +010079static uint32_t i2c_get_sr() {
Damiened656052013-10-13 00:42:20 +010080 // must read SR1 first, then SR2, as the read can clear some flags
81 uint32_t sr1 = I2C1->SR1;
82 uint32_t sr2 = I2C1->SR2;
83 return (sr2 << 16) | sr1;
84}
85
Damien4a175e12013-10-17 22:50:21 +010086static void mma_restart(uint8_t addr, int write) {
Damiened656052013-10-13 00:42:20 +010087 // send start condition
88 I2C1->CR1 |= I2C_CR1_START;
89
90 // wait for BUSY, MSL and SB --> Slave has acknowledged start condition
91 while ((i2c_get_sr() & 0x00030001) != 0x00030001) {
92 }
93
94 if (write) {
95 // send address and write bit
96 I2C1->DR = (addr << 1) | 0;
97 // wait for BUSY, MSL, ADDR, TXE and TRA
98 while ((i2c_get_sr() & 0x00070082) != 0x00070082) {
99 }
100 } else {
101 // send address and read bit
102 I2C1->DR = (addr << 1) | 1;
103 // wait for BUSY, MSL and ADDR flags
104 while ((i2c_get_sr() & 0x00030002) != 0x00030002) {
105 }
106 }
107}
108
Damien4a175e12013-10-17 22:50:21 +0100109static void mma_start(uint8_t addr, int write) {
Damiened656052013-10-13 00:42:20 +0100110 // wait until I2C is not busy
111 while (I2C1->SR2 & I2C_SR2_BUSY) {
112 }
113
114 // do rest of start
115 mma_restart(addr, write);
116}
117
Damien4a175e12013-10-17 22:50:21 +0100118static void mma_send_byte(uint8_t data) {
Damiened656052013-10-13 00:42:20 +0100119 // send byte
120 I2C1->DR = data;
121 // wait for TRA, BUSY, MSL, TXE and BTF (byte transmitted)
122 int timeout = 1000000;
123 while ((i2c_get_sr() & 0x00070084) != 0x00070084) {
124 if (timeout-- <= 0) {
125 printf("mma_send_byte timed out!\n");
126 break;
127 }
128 }
129}
130
Damien4a175e12013-10-17 22:50:21 +0100131static uint8_t mma_read_ack() {
Damiened656052013-10-13 00:42:20 +0100132 // enable ACK of received byte
133 I2C1->CR1 |= I2C_CR1_ACK;
134 // wait for BUSY, MSL and RXNE (byte received)
135 while ((i2c_get_sr() & 0x00030040) != 0x00030040) {
136 }
137 // read and return data
138 uint8_t data = I2C1->DR;
139 return data;
140}
141
Damien4a175e12013-10-17 22:50:21 +0100142static uint8_t mma_read_nack() {
Damiened656052013-10-13 00:42:20 +0100143 // disable ACK of received byte (to indicate end of receiving)
144 I2C1->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);
145 // last byte should apparently also generate a stop condition
146 I2C1->CR1 |= I2C_CR1_STOP;
147 // wait for BUSY, MSL and RXNE (byte received)
148 while ((i2c_get_sr() & 0x00030040) != 0x00030040) {
149 }
150 // read and return data
151 uint8_t data = I2C1->DR;
152 return data;
153}
154
Damien4a175e12013-10-17 22:50:21 +0100155static void mma_stop() {
Damiened656052013-10-13 00:42:20 +0100156 // send stop condition
157 I2C1->CR1 |= I2C_CR1_STOP;
158}
159
Damiened656052013-10-13 00:42:20 +0100160#define PYB_USRSW_PORT (GPIOA)
Damien00ff04f2013-10-19 14:40:54 +0100161#define PYB_USRSW_PIN (GPIO_Pin_13)
Damiened656052013-10-13 00:42:20 +0100162
163void sw_init() {
164 // make it an input with pull-up
Damien00ff04f2013-10-19 14:40:54 +0100165 GPIO_InitTypeDef GPIO_InitStructure;
166 GPIO_InitStructure.GPIO_Pin = PYB_USRSW_PIN;
167 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
168 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
169 GPIO_Init(PYB_USRSW_PORT, &GPIO_InitStructure);
Damiened656052013-10-13 00:42:20 +0100170}
171
172int sw_get() {
Damien00ff04f2013-10-19 14:40:54 +0100173 if (PYB_USRSW_PORT->IDR & PYB_USRSW_PIN) {
Damiened656052013-10-13 00:42:20 +0100174 // pulled high, so switch is not pressed
175 return 0;
176 } else {
177 // pulled low, so switch is pressed
178 return 1;
179 }
180}
181
Damiened656052013-10-13 00:42:20 +0100182void __fatal_error(const char *msg) {
183 lcd_print_strn("\nFATAL ERROR:\n", 14);
184 lcd_print_strn(msg, strlen(msg));
185
186 for (;;) {
Damien995b8aa2013-10-18 23:44:05 +0100187 led_state(PYB_LED_R1, 1);
188 led_state(PYB_LED_R2, 0);
Damien00ff04f2013-10-19 14:40:54 +0100189 sys_tick_delay_ms(150);
Damien995b8aa2013-10-18 23:44:05 +0100190 led_state(PYB_LED_R1, 0);
191 led_state(PYB_LED_R2, 1);
Damien00ff04f2013-10-19 14:40:54 +0100192 sys_tick_delay_ms(150);
Damiened656052013-10-13 00:42:20 +0100193 }
194}
195
196#include "misc.h"
197#include "lexer.h"
198#include "mpyconfig.h"
199#include "parse.h"
200#include "compile.h"
201#include "runtime.h"
202
Damienfb42ec12013-10-19 15:37:09 +0100203#if 0
Damiened656052013-10-13 00:42:20 +0100204py_obj_t pyb_delay(py_obj_t count) {
Damien00ff04f2013-10-19 14:40:54 +0100205 sys_tick_delay_ms(rt_get_int(count));
Damiened656052013-10-13 00:42:20 +0100206 return py_const_none;
207}
208
209py_obj_t pyb_led(py_obj_t state) {
Damien995b8aa2013-10-18 23:44:05 +0100210 led_state(PYB_LED_G1, rt_is_true(state));
Damiened656052013-10-13 00:42:20 +0100211 return state;
212}
213
214py_obj_t pyb_sw() {
215 if (sw_get()) {
216 return py_const_true;
217 } else {
218 return py_const_false;
219 }
220}
Damienfb42ec12013-10-19 15:37:09 +0100221#endif
Damiened656052013-10-13 00:42:20 +0100222
223#include "ff.h"
224FATFS fatfs0;
225
Damien4a175e12013-10-17 22:50:21 +0100226
227/*
Damienfb42ec12013-10-19 15:37:09 +0100228#include "nlr.h"
Damien152568b2013-10-16 00:46:39 +0100229void g(uint i) {
230 printf("g:%d\n", i);
231 if (i & 1) {
232 nlr_jump((void*)(42 + i));
233 }
234}
235void f() {
236 nlr_buf_t nlr;
237 int i;
238 for (i = 0; i < 4; i++) {
239 printf("f:loop:%d:%p\n", i, &nlr);
240 if (nlr_push(&nlr) == 0) {
241 // normal
242 //printf("a:%p:%p %p %p %u\n", &nlr, nlr.ip, nlr.sp, nlr.prev, nlr.ret_val);
243 g(i);
244 printf("f:lp:%d:nrm\n", i);
245 nlr_pop();
246 } else {
247 // nlr
248 //printf("b:%p:%p %p %p %u\n", &nlr, nlr.ip, nlr.sp, nlr.prev, nlr.ret_val);
249 printf("f:lp:%d:nlr:%d\n", i, (int)nlr.ret_val);
250 }
251 }
252}
253void nlr_test() {
254 f(1);
255}
Damien4a175e12013-10-17 22:50:21 +0100256*/
257
Damien00ff04f2013-10-19 14:40:54 +0100258void fatality() {
259 led_state(PYB_LED_R1, 1);
260 led_state(PYB_LED_G1, 1);
261 led_state(PYB_LED_R2, 1);
262 led_state(PYB_LED_G2, 1);
263}
Damiened656052013-10-13 00:42:20 +0100264
Damien00ff04f2013-10-19 14:40:54 +0100265static const char *fresh_boot_py =
266"# boot.py -- run on boot-up\n"
267"# can run arbitrary Python, but best to keep it minimal\n"
268"\n"
269"pyb.source_dir('/src')\n"
270"pyb.main('main.py')\n"
271"#pyb.usb_usr('VCP')\n"
272"#pyb.usb_msd(True, 'dual partition')\n"
273"#pyb.flush_cache(False)\n"
274"#pyb.error_log('error.txt')\n"
275;
Damiened656052013-10-13 00:42:20 +0100276
Damien00ff04f2013-10-19 14:40:54 +0100277// get lots of info about the board
278static void board_info() {
Damiened656052013-10-13 00:42:20 +0100279 // get and print clock speeds
280 // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
Damiened656052013-10-13 00:42:20 +0100281 {
282 RCC_ClocksTypeDef rcc_clocks;
283 RCC_GetClocksFreq(&rcc_clocks);
Damien00ff04f2013-10-19 14:40:54 +0100284 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 +0100285 }
286
Damien995b8aa2013-10-18 23:44:05 +0100287 // to print info about memory
Damien00ff04f2013-10-19 14:40:54 +0100288 {
Damien4a175e12013-10-17 22:50:21 +0100289 extern void *_sidata;
290 extern void *_sdata;
291 extern void *_edata;
292 extern void *_sbss;
293 extern void *_ebss;
294 extern void *_estack;
295 extern void *_etext;
296 extern void *_heap_start;
Damien00ff04f2013-10-19 14:40:54 +0100297 printf("_sidata=%p\n", &_sidata);
298 printf("_sdata=%p\n", &_sdata);
299 printf("_edata=%p\n", &_edata);
300 printf("_sbss=%p\n", &_sbss);
301 printf("_ebss=%p\n", &_ebss);
302 printf("_estack=%p\n", &_estack);
303 printf("_etext=%p\n", &_etext);
304 printf("_heap_start=%p\n", &_heap_start);
305 }
306
307 // free space on flash
308 {
309 DWORD nclst;
310 FATFS *fatfs;
311 f_getfree("0:", &nclst, &fatfs);
312 printf("free=%u\n", (uint)(nclst * fatfs->csize * 512));
313 }
314}
315
316int main() {
317 // TODO disable JTAG
318
319 // basic sub-system init
320 sys_tick_init();
321 gpio_init();
322 led_init();
323
324 // turn on LED to indicate bootup
325 led_state(PYB_LED_G1, 1);
326
327 // more sub-system init
328 sw_init();
329 lcd_init();
330 storage_init();
331
332 // Python init
Damienfb42ec12013-10-19 15:37:09 +0100333 //qstr_init();
334 //rt_init();
Damien00ff04f2013-10-19 14:40:54 +0100335
336 // print a message
337 printf(" micro py board\n");
338
339 // local filesystem init
340 {
341 // try to mount the flash
342 FRESULT res = f_mount(&fatfs0, "0:", 1);
343 if (res == FR_OK) {
344 // mount sucessful
345 } else if (res == FR_NO_FILESYSTEM) {
346 // no filesystem, so create a fresh one
347
348 // LED on to indicate creation of LFS
349 led_state(PYB_LED_R2, 1);
350 uint32_t stc = sys_tick_counter;
351
352 res = f_mkfs("0:", 0, 0);
353 if (res == FR_OK) {
354 // success creating fresh LFS
355 } else {
356 __fatal_error("could not create LFS");
357 }
358
359 // keep LED on for at least 100ms
360 sys_tick_wait_at_least(stc, 100);
361 led_state(PYB_LED_R2, 0);
362 } else {
363 __fatal_error("could not access LFS");
Damien4a175e12013-10-17 22:50:21 +0100364 }
Damien00ff04f2013-10-19 14:40:54 +0100365 }
366
367 // make sure we have a /boot.py
368 {
369 FILINFO fno;
370 FRESULT res = f_stat("0:/boot.py", &fno);
371 if (res == FR_OK) {
372 if (fno.fattrib & AM_DIR) {
373 // exists as a directory
374 // TODO handle this case
375 // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
376 } else {
377 // exists as a file, good!
378 }
379 } else {
380 // doesn't exist, create fresh file
381
382 // LED on to indicate creation of boot.py
383 led_state(PYB_LED_R2, 1);
384 uint32_t stc = sys_tick_counter;
385
386 FIL fp;
387 f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
388 UINT n;
389 f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py), &n);
390 // TODO check we could write n bytes
391 f_close(&fp);
392
393 // keep LED on for at least 100ms
394 sys_tick_wait_at_least(stc, 100);
395 led_state(PYB_LED_R2, 0);
396 }
397 }
398
399 // run /boot.py
400 if (0) {
401 FIL fp;
402 f_open(&fp, "0:/boot.py", FA_READ);
403 UINT n;
404 char buf[20];
405 f_read(&fp, buf, 18, &n);
406 buf[n + 1] = 0;
407 printf("read %d\n%s", n, buf);
408 f_close(&fp);
409 }
410
411 // turn boot-up LED off
412 led_state(PYB_LED_G1, 0);
413
Damien00ff04f2013-10-19 14:40:54 +0100414 // USB
Damienfb42ec12013-10-19 15:37:09 +0100415 if (1) {
Damien00ff04f2013-10-19 14:40:54 +0100416 usb_init();
417 }
Damiened656052013-10-13 00:42:20 +0100418
Damien00ff04f2013-10-19 14:40:54 +0100419 //printf("init;al=%u\n", m_get_total_bytes_allocated()); // 1600, due to qstr_init
420 //sys_tick_delay_ms(1000);
421
422 #if 0
Damiened656052013-10-13 00:42:20 +0100423 // Python!
Damien4a175e12013-10-17 22:50:21 +0100424 if (0) {
Damiened656052013-10-13 00:42:20 +0100425 //const char *pysrc = "def f():\n x=x+1\nprint(42)\n";
426 const char *pysrc =
427 // impl01.py
428 /*
429 "x = 0\n"
430 "while x < 400:\n"
431 " y = 0\n"
432 " while y < 400:\n"
433 " z = 0\n"
434 " while z < 400:\n"
435 " z = z + 1\n"
436 " y = y + 1\n"
437 " x = x + 1\n";
438 */
439 // impl02.py
Damien152568b2013-10-16 00:46:39 +0100440 /*
Damiened656052013-10-13 00:42:20 +0100441 "#@micropython.native\n"
442 "def f():\n"
443 " x = 0\n"
444 " while x < 400:\n"
445 " y = 0\n"
446 " while y < 400:\n"
447 " z = 0\n"
448 " while z < 400:\n"
449 " z = z + 1\n"
450 " y = y + 1\n"
451 " x = x + 1\n"
452 "f()\n";
Damien152568b2013-10-16 00:46:39 +0100453 */
Damiened656052013-10-13 00:42:20 +0100454 /*
455 "print('in python!')\n"
456 "x = 0\n"
457 "while x < 4:\n"
458 " pyb_led(True)\n"
459 " pyb_delay(201)\n"
460 " pyb_led(False)\n"
461 " pyb_delay(201)\n"
462 " x = x + 1\n"
463 "print('press me!')\n"
464 "while True:\n"
465 " pyb_led(pyb_sw())\n";
466 */
467 /*
468 // impl16.py
469 "@micropython.asm_thumb\n"
470 "def delay(r0):\n"
471 " b(loop_entry)\n"
472 " label(loop1)\n"
473 " movw(r1, 55999)\n"
474 " label(loop2)\n"
475 " subs(r1, r1, 1)\n"
476 " cmp(r1, 0)\n"
477 " bgt(loop2)\n"
478 " subs(r0, r0, 1)\n"
479 " label(loop_entry)\n"
480 " cmp(r0, 0)\n"
481 " bgt(loop1)\n"
482 "print('in python!')\n"
483 "@micropython.native\n"
484 "def flash(n):\n"
485 " x = 0\n"
486 " while x < n:\n"
487 " pyb_led(True)\n"
488 " delay(249)\n"
489 " pyb_led(False)\n"
490 " delay(249)\n"
491 " x = x + 1\n"
492 "flash(20)\n";
493 */
Damien152568b2013-10-16 00:46:39 +0100494 // impl18.py
495 /*
496 "# basic exceptions\n"
497 "x = 1\n"
498 "try:\n"
499 " x.a()\n"
500 "except:\n"
501 " print(x)\n";
502 */
503 // impl19.py
504 "# for loop\n"
505 "def f():\n"
506 " for x in range(400):\n"
507 " for y in range(400):\n"
508 " for z in range(400):\n"
509 " pass\n"
510 "f()\n";
Damiened656052013-10-13 00:42:20 +0100511
512 py_lexer_t *lex = py_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
513
514 if (0) {
515 while (!py_lexer_is_kind(lex, PY_TOKEN_END)) {
516 py_token_show(py_lexer_cur(lex));
517 py_lexer_to_next(lex);
Damien00ff04f2013-10-19 14:40:54 +0100518 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100519 }
520 } else {
521 // nalloc=1740;6340;6836 -> 140;4600;496 bytes for lexer, parser, compiler
522 printf("lex; al=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100523 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100524 py_parse_node_t pn = py_parse(lex, 0);
525 //printf("----------------\n");
526 printf("pars;al=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100527 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100528 //parse_node_show(pn, 0);
Damien995b8aa2013-10-18 23:44:05 +0100529 py_compile(pn, false);
Damiened656052013-10-13 00:42:20 +0100530 printf("comp;al=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100531 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100532
533 if (1) {
534 // execute it!
535
536 // add some functions to the python namespace
537 rt_store_name(qstr_from_str_static("pyb_delay"), rt_make_function_1(pyb_delay));
538 rt_store_name(qstr_from_str_static("pyb_led"), rt_make_function_1(pyb_led));
539 rt_store_name(qstr_from_str_static("pyb_sw"), rt_make_function_0(pyb_sw));
540
541 py_obj_t module_fun = rt_make_function_from_id(1);
542
Damien152568b2013-10-16 00:46:39 +0100543 // flash once
Damien995b8aa2013-10-18 23:44:05 +0100544 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100545 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100546 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100547
Damien152568b2013-10-16 00:46:39 +0100548 nlr_buf_t nlr;
549 if (nlr_push(&nlr) == 0) {
550 py_obj_t ret = rt_call_function_0(module_fun);
551 printf("done! got: ");
552 py_obj_print(ret);
553 printf("\n");
554 nlr_pop();
555 } else {
556 // uncaught exception
557 printf("exception: ");
558 py_obj_print((py_obj_t)nlr.ret_val);
559 printf("\n");
560 }
561
562 // flash once
Damien995b8aa2013-10-18 23:44:05 +0100563 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100564 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100565 led_state(PYB_LED_G1, 0);
Damien152568b2013-10-16 00:46:39 +0100566
Damien00ff04f2013-10-19 14:40:54 +0100567 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100568 printf("nalloc=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100569 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100570 }
571 }
572 }
573 #endif
574
575 // benchmark C version of impl02.py
576 if (0) {
Damien995b8aa2013-10-18 23:44:05 +0100577 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100578 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100579 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100580 impl02_c_version();
Damien995b8aa2013-10-18 23:44:05 +0100581 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100582 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100583 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100584 }
585
586 // MMA testing
587 if (0) {
588 printf("1");
589 mma_init();
590 printf("2");
591 mma_start(0x4c, 1);
592 printf("3");
593 mma_send_byte(0);
594 printf("4");
595 mma_stop();
596 printf("5");
597 mma_start(0x4c, 1);
598 printf("6");
599 mma_send_byte(0);
600 printf("7");
601 mma_restart(0x4c, 0);
602 for (int i = 0; i <= 0xa; i++) {
603 int data;
604 if (i == 0xa) {
605 data = mma_read_nack();
606 } else {
607 data = mma_read_ack();
608 }
609 printf(" %02x", data);
610 }
611 printf("\n");
612
613 mma_start(0x4c, 1);
614 mma_send_byte(7); // mode
615 mma_send_byte(1); // active mode
616 mma_stop();
617
618 for (;;) {
Damien00ff04f2013-10-19 14:40:54 +0100619 sys_tick_delay_ms(500);
Damiened656052013-10-13 00:42:20 +0100620
621 mma_start(0x4c, 1);
622 mma_send_byte(0);
623 mma_restart(0x4c, 0);
624 for (int i = 0; i <= 3; i++) {
625 int data;
626 if (i == 3) {
627 data = mma_read_nack();
628 printf(" %02x\n", data);
629 } else {
630 data = mma_read_ack() & 0x3f;
631 if (data & 0x20) {
632 data |= 0xc0;
633 }
634 printf(" % 2d", data);
635 }
636 }
637 }
638 }
639
Damiened656052013-10-13 00:42:20 +0100640 // SD card testing
641 if (0) {
642 //sdio_init();
643 }
644
Damiened656052013-10-13 00:42:20 +0100645 int i = 0;
646 int n = 0;
Damien00ff04f2013-10-19 14:40:54 +0100647 uint32_t stc = sys_tick_counter;
Damiened656052013-10-13 00:42:20 +0100648
649 for (;;) {
Damien00ff04f2013-10-19 14:40:54 +0100650 sys_tick_delay_ms(10);
Damiened656052013-10-13 00:42:20 +0100651 if (sw_get()) {
Damien00ff04f2013-10-19 14:40:54 +0100652 led_state(PYB_LED_G1, 1);
Damiened656052013-10-13 00:42:20 +0100653 i = 1 - i;
654 if (i) {
655 printf(" angel %05x.\n", n);
656 //usb_vcp_send("hello!\r\n", 8);
657 } else {
658 printf(" mishka %4u.\n", n);
659 //usb_vcp_send("angel!\r\n", 8);
660 }
661 n += 1;
662 } else {
Damien00ff04f2013-10-19 14:40:54 +0100663 led_state(PYB_LED_G1, 0);
664 }
665 if (sys_tick_has_passed(stc, 500)) {
666 stc = sys_tick_counter;
667 led_toggle(PYB_LED_G2);
Damiened656052013-10-13 00:42:20 +0100668 }
669 }
670
671 return 0;
672}