3 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
4 * @brief PyMite platform definitions for OpenPilot
6 * @see The GNU Public License (GPL) Version 3
8 *****************************************************************************/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "openpilot.h"
28 PmReturn_t
plat_init(void)
33 PmReturn_t
plat_deinit(void)
39 * Gets a byte from the address in the designated memory space
40 * Post-increments *paddr.
42 uint8_t plat_memGetByte(PmMemSpace_t memspace
, uint8_t const **paddr
)
54 case MEMSPACE_SEEPROM
:
64 PmReturn_t
plat_getByte(uint8_t *b
)
67 PmReturn_t retval
= PM_RET_OK
;
74 PM_RAISE(retval
, PM_RET_EX_IO
);
80 PmReturn_t
plat_putByte(uint8_t b
)
83 PmReturn_t retval
= PM_RET_OK
;
88 if ((i
!= b
) || (i
== EOF
))
90 PM_RAISE(retval
, PM_RET_EX_IO
);
96 PmReturn_t
plat_getMsTicks(uint32_t *r_ticks
)
98 *r_ticks
= xTaskGetTickCount() * portTICK_RATE_MS
;
102 void plat_reportError(PmReturn_t result
)
104 #ifdef HAVE_DEBUG_INFO
105 #define LEN_FNLOOKUP 26
106 #define LEN_EXNLOOKUP 17
116 uint8_t const *plnotab
;
119 /* This table should match src/vm/fileid.txt */
120 char const * const fnlookup
[LEN_FNLOOKUP
] = {
149 /* This table should match src/vm/pm.h PmReturn_t */
150 char const * const exnlookup
[LEN_EXNLOOKUP
] = {
170 /* Print traceback */
171 printf("Traceback (most recent call first):\n");
173 /* Get the top frame */
174 pframe
= gVmGlobal
.pthread
->pframe
;
176 /* If it's the native frame, print the native function name */
177 if (pframe
== (pPmFrame_t
)&(gVmGlobal
.nativeframe
))
180 /* The last name in the names tuple of the code obj is the name */
181 retval
= tuple_getItem((pPmObj_t
)gVmGlobal
.nativeframe
.nf_func
->
182 f_co
->co_names
, -1, &pstr
);
183 if ((retval
) != PM_RET_OK
)
185 printf(" Unable to get native func name.\n");
190 printf(" %s() __NATIVE__\n", ((pPmString_t
)pstr
)->val
);
193 /* Get the frame that called the native frame */
194 pframe
= (pPmFrame_t
)gVmGlobal
.nativeframe
.nf_back
;
197 /* Print the remaining frame stack */
198 for (; pframe
!= C_NULL
; pframe
= pframe
->fo_back
)
200 /* The last name in the names tuple of the code obj is the name */
201 retval
= tuple_getItem((pPmObj_t
)pframe
->fo_func
->f_co
->co_names
,
204 if ((retval
) != PM_RET_OK
) break;
207 * Get the line number of the current bytecode. Algorithm comes from:
208 * http://svn.python.org/view/python/trunk/Objects/lnotab_notes.txt?view=markup
210 bcindex
= pframe
->fo_ip
- pframe
->fo_func
->f_co
->co_codeaddr
;
211 plnotab
= pframe
->fo_func
->f_co
->co_lnotab
;
212 len_lnotab
= mem_getWord(MEMSPACE_PROG
, &plnotab
);
214 linesum
= pframe
->fo_func
->f_co
->co_firstlineno
;
215 for (i
= 0; i
< len_lnotab
; i
+= 2)
217 bcsum
+= mem_getByte(MEMSPACE_PROG
, &plnotab
);
218 if (bcsum
> bcindex
) break;
219 linesum
+= mem_getByte(MEMSPACE_PROG
, &plnotab
);
221 printf(" File \"%s\", line %d, in %s\n",
222 ((pPmFrame_t
)pframe
)->fo_func
->f_co
->co_filename
,
224 ((pPmString_t
)pstr
)->val
);
228 res
= (uint8_t)result
;
229 if ((res
> 0) && ((res
- PM_RET_EX
) < LEN_EXNLOOKUP
))
231 printf("%s", exnlookup
[res
- PM_RET_EX
]);
235 printf("Error code 0x%02X", result
);
237 printf(" detected by ");
239 if ((gVmGlobal
.errFileId
> 0) && (gVmGlobal
.errFileId
< LEN_FNLOOKUP
))
241 printf("%s:", fnlookup
[gVmGlobal
.errFileId
]);
245 printf("FileId 0x%02X line ", gVmGlobal
.errFileId
);
247 printf("%d\n", gVmGlobal
.errLineNum
);
249 #else /* HAVE_DEBUG_INFO */
252 printf("Error: 0x%02X\n", result
);
253 printf(" Release: 0x%02X\n", gVmGlobal
.errVmRelease
);
254 printf(" FileId: 0x%02X\n", gVmGlobal
.errFileId
);
255 printf(" LineNum: %d\n", gVmGlobal
.errLineNum
);
257 /* Print traceback */
263 printf("Traceback (top first):\n");
265 /* Get the top frame */
266 pframe
= (pPmObj_t
)gVmGlobal
.pthread
->pframe
;
268 /* If it's the native frame, print the native function name */
269 if (pframe
== (pPmObj_t
)&(gVmGlobal
.nativeframe
))
272 /* The last name in the names tuple of the code obj is the name */
273 retval
= tuple_getItem((pPmObj_t
)gVmGlobal
.nativeframe
.nf_func
->
274 f_co
->co_names
, -1, &pstr
);
275 if ((retval
) != PM_RET_OK
)
277 printf(" Unable to get native func name.\n");
282 printf(" %s() __NATIVE__\n", ((pPmString_t
)pstr
)->val
);
285 /* Get the frame that called the native frame */
286 pframe
= (pPmObj_t
)gVmGlobal
.nativeframe
.nf_back
;
289 /* Print the remaining frame stack */
292 pframe
= (pPmObj_t
)((pPmFrame_t
)pframe
)->fo_back
)
294 /* The last name in the names tuple of the code obj is the name */
295 retval
= tuple_getItem((pPmObj_t
)((pPmFrame_t
)pframe
)->
296 fo_func
->f_co
->co_names
, -1, &pstr
);
297 if ((retval
) != PM_RET_OK
) break;
299 printf(" %s()\n", ((pPmString_t
)pstr
)->val
);
301 printf(" <module>.\n");
303 #endif /* HAVE_DEBUG_INFO */
305 /* TODO: Copy error information to UAVObject */
306 /* gVmGlobal.errVmRelease gVmGlobal.errFileId gVmGlobal.errLineNum */