python-on-a-chip online compiler

Dependencies:   mbed TSI

/media/uploads/va009039/p14p-f446re.png

more info: python-on-a-chip

Committer:
va009039
Date:
Thu Apr 14 22:32:57 2016 +0000
Revision:
15:94ca5c8003e5
Parent:
13:4ad84e922efd
update Nucleo-F401RE.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:65f1469d6bfb 1 /*
va009039 0:65f1469d6bfb 2 # This file is Copyright 2009 Dean Hall.
va009039 0:65f1469d6bfb 3 #
va009039 0:65f1469d6bfb 4 # This file is part of the Python-on-a-Chip program.
va009039 0:65f1469d6bfb 5 # Python-on-a-Chip is free software: you can redistribute it and/or modify
va009039 0:65f1469d6bfb 6 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
va009039 0:65f1469d6bfb 7 #
va009039 0:65f1469d6bfb 8 # Python-on-a-Chip is distributed in the hope that it will be useful,
va009039 0:65f1469d6bfb 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
va009039 0:65f1469d6bfb 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
va009039 0:65f1469d6bfb 11 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
va009039 0:65f1469d6bfb 12 # is seen in the file COPYING up one directory from this.
va009039 0:65f1469d6bfb 13 */
va009039 0:65f1469d6bfb 14
va009039 0:65f1469d6bfb 15
va009039 0:65f1469d6bfb 16 #undef __FILE_ID__
va009039 0:65f1469d6bfb 17 #define __FILE_ID__ 0x70
va009039 0:65f1469d6bfb 18
va009039 0:65f1469d6bfb 19
va009039 0:65f1469d6bfb 20 /** PyMite platform-specific routines for ARM7 target */
va009039 0:65f1469d6bfb 21
va009039 0:65f1469d6bfb 22
va009039 0:65f1469d6bfb 23 #include "mbed.h"
va009039 0:65f1469d6bfb 24 #include "pm.h"
va009039 0:65f1469d6bfb 25
va009039 0:65f1469d6bfb 26
va009039 0:65f1469d6bfb 27 #define CALLBACK_MS 10
va009039 0:65f1469d6bfb 28
va009039 0:65f1469d6bfb 29
va009039 13:4ad84e922efd 30 Serial serial(USBTX, USBRX);
va009039 0:65f1469d6bfb 31 Ticker ticker;
va009039 0:65f1469d6bfb 32
va009039 0:65f1469d6bfb 33
va009039 0:65f1469d6bfb 34 static void
va009039 0:65f1469d6bfb 35 ticker_callback(void)
va009039 0:65f1469d6bfb 36 {
va009039 0:65f1469d6bfb 37 PmReturn_t retval;
va009039 0:65f1469d6bfb 38
va009039 0:65f1469d6bfb 39 retval = pm_vmPeriodic(CALLBACK_MS * 1000);
va009039 0:65f1469d6bfb 40 PM_REPORT_IF_ERROR(retval);
va009039 0:65f1469d6bfb 41 }
va009039 0:65f1469d6bfb 42
va009039 0:65f1469d6bfb 43
va009039 0:65f1469d6bfb 44 PmReturn_t
va009039 0:65f1469d6bfb 45 plat_init(void)
va009039 0:65f1469d6bfb 46 {
va009039 13:4ad84e922efd 47 serial.baud(115200);
va009039 0:65f1469d6bfb 48
va009039 0:65f1469d6bfb 49 ticker.attach_us(ticker_callback, CALLBACK_MS * 1000);
va009039 0:65f1469d6bfb 50
va009039 0:65f1469d6bfb 51 return PM_RET_OK;
va009039 0:65f1469d6bfb 52 }
va009039 0:65f1469d6bfb 53
va009039 0:65f1469d6bfb 54
va009039 0:65f1469d6bfb 55 PmReturn_t
va009039 0:65f1469d6bfb 56 plat_deinit(void)
va009039 0:65f1469d6bfb 57 {
va009039 0:65f1469d6bfb 58 /* Detach the callback from the ticker */
va009039 0:65f1469d6bfb 59 ticker.detach();
va009039 0:65f1469d6bfb 60
va009039 0:65f1469d6bfb 61 return PM_RET_OK;
va009039 0:65f1469d6bfb 62 }
va009039 0:65f1469d6bfb 63
va009039 0:65f1469d6bfb 64
va009039 0:65f1469d6bfb 65 uint8_t
va009039 0:65f1469d6bfb 66 plat_memGetByte(PmMemSpace_t memspace, uint8_t const **paddr)
va009039 0:65f1469d6bfb 67 {
va009039 0:65f1469d6bfb 68 uint8_t b = 0;
va009039 0:65f1469d6bfb 69
va009039 0:65f1469d6bfb 70 switch (memspace)
va009039 0:65f1469d6bfb 71 {
va009039 0:65f1469d6bfb 72 case MEMSPACE_RAM:
va009039 0:65f1469d6bfb 73 case MEMSPACE_PROG:
va009039 0:65f1469d6bfb 74 b = **paddr;
va009039 0:65f1469d6bfb 75 *paddr += 1;
va009039 0:65f1469d6bfb 76 return b;
va009039 0:65f1469d6bfb 77
va009039 0:65f1469d6bfb 78 case MEMSPACE_EEPROM:
va009039 0:65f1469d6bfb 79 case MEMSPACE_SEEPROM:
va009039 0:65f1469d6bfb 80 case MEMSPACE_OTHER0:
va009039 0:65f1469d6bfb 81 case MEMSPACE_OTHER1:
va009039 0:65f1469d6bfb 82 case MEMSPACE_OTHER2:
va009039 0:65f1469d6bfb 83 case MEMSPACE_OTHER3:
va009039 0:65f1469d6bfb 84 default:
va009039 0:65f1469d6bfb 85 return 0;
va009039 0:65f1469d6bfb 86 }
va009039 0:65f1469d6bfb 87 }
va009039 0:65f1469d6bfb 88
va009039 0:65f1469d6bfb 89
va009039 0:65f1469d6bfb 90 PmReturn_t
va009039 0:65f1469d6bfb 91 plat_getByte(uint8_t *b)
va009039 0:65f1469d6bfb 92 {
va009039 0:65f1469d6bfb 93 int c;
va009039 0:65f1469d6bfb 94 PmReturn_t retval = PM_RET_OK;
va009039 0:65f1469d6bfb 95
va009039 0:65f1469d6bfb 96 c = serial.getc();
va009039 0:65f1469d6bfb 97 *b = c & 0xFF;
va009039 0:65f1469d6bfb 98
va009039 0:65f1469d6bfb 99 if (c > 0xFF)
va009039 0:65f1469d6bfb 100 {
va009039 0:65f1469d6bfb 101 PM_RAISE(retval, PM_RET_EX_IO);
va009039 0:65f1469d6bfb 102 }
va009039 0:65f1469d6bfb 103
va009039 0:65f1469d6bfb 104 return retval;
va009039 0:65f1469d6bfb 105 }
va009039 0:65f1469d6bfb 106
va009039 0:65f1469d6bfb 107
va009039 0:65f1469d6bfb 108 PmReturn_t
va009039 0:65f1469d6bfb 109 plat_putByte(uint8_t b)
va009039 0:65f1469d6bfb 110 {
va009039 0:65f1469d6bfb 111 while (!serial.writeable());
va009039 0:65f1469d6bfb 112 serial.putc(b);
va009039 0:65f1469d6bfb 113
va009039 0:65f1469d6bfb 114 return PM_RET_OK;
va009039 0:65f1469d6bfb 115 }
va009039 0:65f1469d6bfb 116
va009039 0:65f1469d6bfb 117
va009039 0:65f1469d6bfb 118 PmReturn_t
va009039 0:65f1469d6bfb 119 plat_getMsTicks(uint32_t *r_ticks)
va009039 0:65f1469d6bfb 120 {
va009039 0:65f1469d6bfb 121 *r_ticks = pm_timerMsTicks;
va009039 0:65f1469d6bfb 122
va009039 0:65f1469d6bfb 123 return PM_RET_OK;
va009039 0:65f1469d6bfb 124 }
va009039 0:65f1469d6bfb 125
va009039 0:65f1469d6bfb 126
va009039 0:65f1469d6bfb 127 void
va009039 0:65f1469d6bfb 128 plat_reportError(PmReturn_t result)
va009039 0:65f1469d6bfb 129 {
va009039 0:65f1469d6bfb 130
va009039 0:65f1469d6bfb 131 #ifdef HAVE_DEBUG_INFO
va009039 0:65f1469d6bfb 132 #define LEN_FNLOOKUP 26
va009039 0:65f1469d6bfb 133 #define LEN_EXNLOOKUP 18
va009039 0:65f1469d6bfb 134
va009039 0:65f1469d6bfb 135 uint8_t res;
va009039 0:65f1469d6bfb 136 pPmFrame_t pframe;
va009039 0:65f1469d6bfb 137 pPmObj_t pstr;
va009039 0:65f1469d6bfb 138 PmReturn_t retval;
va009039 0:65f1469d6bfb 139 uint16_t bcindex;
va009039 0:65f1469d6bfb 140 uint16_t bcsum;
va009039 0:65f1469d6bfb 141 uint16_t linesum;
va009039 0:65f1469d6bfb 142 uint16_t len_lnotab;
va009039 0:65f1469d6bfb 143 uint8_t const *plnotab;
va009039 0:65f1469d6bfb 144 uint16_t i;
va009039 0:65f1469d6bfb 145
va009039 0:65f1469d6bfb 146 /* This table should match src/vm/fileid.txt */
va009039 0:65f1469d6bfb 147 char const * const fnlookup[LEN_FNLOOKUP] = {
va009039 0:65f1469d6bfb 148 "<no file>",
va009039 0:65f1469d6bfb 149 "codeobj.c",
va009039 0:65f1469d6bfb 150 "dict.c",
va009039 0:65f1469d6bfb 151 "frame.c",
va009039 0:65f1469d6bfb 152 "func.c",
va009039 0:65f1469d6bfb 153 "global.c",
va009039 0:65f1469d6bfb 154 "heap.c",
va009039 0:65f1469d6bfb 155 "img.c",
va009039 0:65f1469d6bfb 156 "int.c",
va009039 0:65f1469d6bfb 157 "interp.c",
va009039 0:65f1469d6bfb 158 "pmstdlib_nat.c",
va009039 0:65f1469d6bfb 159 "list.c",
va009039 0:65f1469d6bfb 160 "main.c",
va009039 0:65f1469d6bfb 161 "mem.c",
va009039 0:65f1469d6bfb 162 "module.c",
va009039 0:65f1469d6bfb 163 "obj.c",
va009039 0:65f1469d6bfb 164 "seglist.c",
va009039 0:65f1469d6bfb 165 "sli.c",
va009039 0:65f1469d6bfb 166 "strobj.c",
va009039 0:65f1469d6bfb 167 "tuple.c",
va009039 0:65f1469d6bfb 168 "seq.c",
va009039 0:65f1469d6bfb 169 "pm.c",
va009039 0:65f1469d6bfb 170 "thread.c",
va009039 0:65f1469d6bfb 171 "float.c",
va009039 0:65f1469d6bfb 172 "class.c",
va009039 0:65f1469d6bfb 173 "bytearray.c",
va009039 0:65f1469d6bfb 174 };
va009039 0:65f1469d6bfb 175
va009039 0:65f1469d6bfb 176 /* This table should match src/vm/pm.h PmReturn_t */
va009039 0:65f1469d6bfb 177 char const * const exnlookup[LEN_EXNLOOKUP] = {
va009039 0:65f1469d6bfb 178 "Exception",
va009039 0:65f1469d6bfb 179 "SystemExit",
va009039 0:65f1469d6bfb 180 "IoError",
va009039 0:65f1469d6bfb 181 "ZeroDivisionError",
va009039 0:65f1469d6bfb 182 "AssertionError",
va009039 0:65f1469d6bfb 183 "AttributeError",
va009039 0:65f1469d6bfb 184 "ImportError",
va009039 0:65f1469d6bfb 185 "IndexError",
va009039 0:65f1469d6bfb 186 "KeyError",
va009039 0:65f1469d6bfb 187 "MemoryError",
va009039 0:65f1469d6bfb 188 "NameError",
va009039 0:65f1469d6bfb 189 "SyntaxError",
va009039 0:65f1469d6bfb 190 "SystemError",
va009039 0:65f1469d6bfb 191 "TypeError",
va009039 0:65f1469d6bfb 192 "ValueError",
va009039 0:65f1469d6bfb 193 "StopIteration",
va009039 0:65f1469d6bfb 194 "Warning",
va009039 0:65f1469d6bfb 195 "OverflowError",
va009039 0:65f1469d6bfb 196 };
va009039 0:65f1469d6bfb 197
va009039 0:65f1469d6bfb 198 /* Print traceback */
va009039 0:65f1469d6bfb 199 printf("Traceback (most recent call first):\n");
va009039 0:65f1469d6bfb 200
va009039 0:65f1469d6bfb 201 /* Get the top frame */
va009039 0:65f1469d6bfb 202 pframe = gVmGlobal.pthread->pframe;
va009039 0:65f1469d6bfb 203
va009039 0:65f1469d6bfb 204 /* If it's the native frame, print the native function name */
va009039 0:65f1469d6bfb 205 if (pframe == (pPmFrame_t)&(gVmGlobal.nativeframe))
va009039 0:65f1469d6bfb 206 {
va009039 0:65f1469d6bfb 207
va009039 0:65f1469d6bfb 208 /* The last name in the names tuple of the code obj is the name */
va009039 0:65f1469d6bfb 209 retval = tuple_getItem((pPmObj_t)gVmGlobal.nativeframe.nf_func->
va009039 0:65f1469d6bfb 210 f_co->co_names, -1, &pstr);
va009039 0:65f1469d6bfb 211 if ((retval) != PM_RET_OK)
va009039 0:65f1469d6bfb 212 {
va009039 0:65f1469d6bfb 213 printf(" Unable to get native func name.\n");
va009039 0:65f1469d6bfb 214 return;
va009039 0:65f1469d6bfb 215 }
va009039 0:65f1469d6bfb 216 else
va009039 0:65f1469d6bfb 217 {
va009039 0:65f1469d6bfb 218 printf(" %s() __NATIVE__\n", ((pPmString_t)pstr)->val);
va009039 0:65f1469d6bfb 219 }
va009039 0:65f1469d6bfb 220
va009039 0:65f1469d6bfb 221 /* Get the frame that called the native frame */
va009039 0:65f1469d6bfb 222 pframe = (pPmFrame_t)gVmGlobal.nativeframe.nf_back;
va009039 0:65f1469d6bfb 223 }
va009039 0:65f1469d6bfb 224
va009039 0:65f1469d6bfb 225 /* Print the remaining frame stack */
va009039 0:65f1469d6bfb 226 for (; pframe != C_NULL; pframe = pframe->fo_back)
va009039 0:65f1469d6bfb 227 {
va009039 0:65f1469d6bfb 228 /* The last name in the names tuple of the code obj is the name */
va009039 0:65f1469d6bfb 229 retval = tuple_getItem((pPmObj_t)pframe->fo_func->f_co->co_names,
va009039 0:65f1469d6bfb 230 -1,
va009039 0:65f1469d6bfb 231 &pstr);
va009039 0:65f1469d6bfb 232 if ((retval) != PM_RET_OK) break;
va009039 0:65f1469d6bfb 233
va009039 0:65f1469d6bfb 234 /*
va009039 0:65f1469d6bfb 235 * Get the line number of the current bytecode. Algorithm comes from:
va009039 0:65f1469d6bfb 236 * http://svn.python.org/view/python/trunk/Objects/lnotab_notes.txt?view=markup
va009039 0:65f1469d6bfb 237 */
va009039 0:65f1469d6bfb 238 bcindex = pframe->fo_ip - pframe->fo_func->f_co->co_codeaddr;
va009039 0:65f1469d6bfb 239 plnotab = pframe->fo_func->f_co->co_lnotab;
va009039 0:65f1469d6bfb 240 len_lnotab = mem_getWord(MEMSPACE_PROG, &plnotab);
va009039 0:65f1469d6bfb 241 bcsum = 0;
va009039 0:65f1469d6bfb 242 linesum = pframe->fo_func->f_co->co_firstlineno;
va009039 0:65f1469d6bfb 243 for (i = 0; i < len_lnotab; i += 2)
va009039 0:65f1469d6bfb 244 {
va009039 0:65f1469d6bfb 245 bcsum += mem_getByte(MEMSPACE_PROG, &plnotab);
va009039 0:65f1469d6bfb 246 if (bcsum > bcindex) break;
va009039 0:65f1469d6bfb 247 linesum += mem_getByte(MEMSPACE_PROG, &plnotab);
va009039 0:65f1469d6bfb 248 }
va009039 0:65f1469d6bfb 249 printf(" File \"%s\", line %d, in %s\n",
va009039 0:65f1469d6bfb 250 ((pPmFrame_t)pframe)->fo_func->f_co->co_filename,
va009039 0:65f1469d6bfb 251 linesum,
va009039 0:65f1469d6bfb 252 ((pPmString_t)pstr)->val);
va009039 0:65f1469d6bfb 253 }
va009039 0:65f1469d6bfb 254
va009039 0:65f1469d6bfb 255 /* Print error */
va009039 0:65f1469d6bfb 256 res = (uint8_t)result;
va009039 0:65f1469d6bfb 257 if ((res > 0) && ((res - PM_RET_EX) < LEN_EXNLOOKUP))
va009039 0:65f1469d6bfb 258 {
va009039 0:65f1469d6bfb 259 printf("%s", exnlookup[res - PM_RET_EX]);
va009039 0:65f1469d6bfb 260 }
va009039 0:65f1469d6bfb 261 else
va009039 0:65f1469d6bfb 262 {
va009039 0:65f1469d6bfb 263 printf("Error code 0x%02X", result);
va009039 0:65f1469d6bfb 264 }
va009039 0:65f1469d6bfb 265 printf(" detected by ");
va009039 0:65f1469d6bfb 266
va009039 0:65f1469d6bfb 267 if ((gVmGlobal.errFileId > 0) && (gVmGlobal.errFileId < LEN_FNLOOKUP))
va009039 0:65f1469d6bfb 268 {
va009039 0:65f1469d6bfb 269 printf("%s:", fnlookup[gVmGlobal.errFileId]);
va009039 0:65f1469d6bfb 270 }
va009039 0:65f1469d6bfb 271 else
va009039 0:65f1469d6bfb 272 {
va009039 0:65f1469d6bfb 273 printf("FileId 0x%02X line ", gVmGlobal.errFileId);
va009039 0:65f1469d6bfb 274 }
va009039 0:65f1469d6bfb 275 printf("%d\n", gVmGlobal.errLineNum);
va009039 0:65f1469d6bfb 276
va009039 0:65f1469d6bfb 277 #else /* HAVE_DEBUG_INFO */
va009039 0:65f1469d6bfb 278
va009039 0:65f1469d6bfb 279 /* Print error */
va009039 0:65f1469d6bfb 280 printf("Error: 0x%02X\n", result);
va009039 0:65f1469d6bfb 281 printf(" Release: 0x%02X\n", gVmGlobal.errVmRelease);
va009039 0:65f1469d6bfb 282 printf(" FileId: 0x%02X\n", gVmGlobal.errFileId);
va009039 0:65f1469d6bfb 283 printf(" LineNum: %d\n", gVmGlobal.errLineNum);
va009039 0:65f1469d6bfb 284
va009039 0:65f1469d6bfb 285 /* Print traceback */
va009039 0:65f1469d6bfb 286 {
va009039 0:65f1469d6bfb 287 pPmObj_t pframe;
va009039 0:65f1469d6bfb 288 pPmObj_t pstr;
va009039 0:65f1469d6bfb 289 PmReturn_t retval;
va009039 0:65f1469d6bfb 290
va009039 0:65f1469d6bfb 291 printf("Traceback (top first):\n");
va009039 0:65f1469d6bfb 292
va009039 0:65f1469d6bfb 293 /* Get the top frame */
va009039 0:65f1469d6bfb 294 pframe = (pPmObj_t)gVmGlobal.pthread->pframe;
va009039 0:65f1469d6bfb 295
va009039 0:65f1469d6bfb 296 /* If it's the native frame, print the native function name */
va009039 0:65f1469d6bfb 297 if (pframe == (pPmObj_t)&(gVmGlobal.nativeframe))
va009039 0:65f1469d6bfb 298 {
va009039 0:65f1469d6bfb 299
va009039 0:65f1469d6bfb 300 /* The last name in the names tuple of the code obj is the name */
va009039 0:65f1469d6bfb 301 retval = tuple_getItem((pPmObj_t)gVmGlobal.nativeframe.nf_func->
va009039 0:65f1469d6bfb 302 f_co->co_names, -1, &pstr);
va009039 0:65f1469d6bfb 303 if ((retval) != PM_RET_OK)
va009039 0:65f1469d6bfb 304 {
va009039 0:65f1469d6bfb 305 printf(" Unable to get native func name.\n");
va009039 0:65f1469d6bfb 306 return;
va009039 0:65f1469d6bfb 307 }
va009039 0:65f1469d6bfb 308 else
va009039 0:65f1469d6bfb 309 {
va009039 0:65f1469d6bfb 310 printf(" %s() __NATIVE__\n", ((pPmString_t)pstr)->val);
va009039 0:65f1469d6bfb 311 }
va009039 0:65f1469d6bfb 312
va009039 0:65f1469d6bfb 313 /* Get the frame that called the native frame */
va009039 0:65f1469d6bfb 314 pframe = (pPmObj_t)gVmGlobal.nativeframe.nf_back;
va009039 0:65f1469d6bfb 315 }
va009039 0:65f1469d6bfb 316
va009039 0:65f1469d6bfb 317 /* Print the remaining frame stack */
va009039 0:65f1469d6bfb 318 for (;
va009039 0:65f1469d6bfb 319 pframe != C_NULL;
va009039 0:65f1469d6bfb 320 pframe = (pPmObj_t)((pPmFrame_t)pframe)->fo_back)
va009039 0:65f1469d6bfb 321 {
va009039 0:65f1469d6bfb 322 /* The last name in the names tuple of the code obj is the name */
va009039 0:65f1469d6bfb 323 retval = tuple_getItem((pPmObj_t)((pPmFrame_t)pframe)->
va009039 0:65f1469d6bfb 324 fo_func->f_co->co_names, -1, &pstr);
va009039 0:65f1469d6bfb 325 if ((retval) != PM_RET_OK) break;
va009039 0:65f1469d6bfb 326
va009039 0:65f1469d6bfb 327 printf(" %s()\n", ((pPmString_t)pstr)->val);
va009039 0:65f1469d6bfb 328 }
va009039 0:65f1469d6bfb 329 printf(" <module>.\n");
va009039 0:65f1469d6bfb 330 }
va009039 0:65f1469d6bfb 331 #endif /* HAVE_DEBUG_INFO */
va009039 0:65f1469d6bfb 332 }