Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / libraries / PyMite / platform / openpilot_sitl / plat.c
blob2539c6fc9c5a14e35f1f7c20996d057c8e48d55f
1 /**
2 * @file plat.c
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 *****************************************************************************/
9 /*
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
18 * for more details.
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
25 #include "pm.h"
26 #include "openpilot.h"
28 PmReturn_t plat_init(void)
30 return PM_RET_OK;
33 PmReturn_t plat_deinit(void)
35 return PM_RET_OK;
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)
44 uint8_t b = 0;
46 switch (memspace)
48 case MEMSPACE_RAM:
49 case MEMSPACE_PROG:
50 b = **paddr;
51 *paddr += 1;
52 return b;
53 case MEMSPACE_EEPROM:
54 case MEMSPACE_SEEPROM:
55 case MEMSPACE_OTHER0:
56 case MEMSPACE_OTHER1:
57 case MEMSPACE_OTHER2:
58 case MEMSPACE_OTHER3:
59 default:
60 return 0;
64 PmReturn_t plat_getByte(uint8_t *b)
66 int c;
67 PmReturn_t retval = PM_RET_OK;
69 c = getchar();
70 *b = c & 0xFF;
72 if (c == EOF)
74 PM_RAISE(retval, PM_RET_EX_IO);
77 return retval;
80 PmReturn_t plat_putByte(uint8_t b)
82 int i;
83 PmReturn_t retval = PM_RET_OK;
85 i = putchar(b);
86 fflush(stdout);
88 if ((i != b) || (i == EOF))
90 PM_RAISE(retval, PM_RET_EX_IO);
93 return retval;
96 PmReturn_t plat_getMsTicks(uint32_t *r_ticks)
98 *r_ticks = xTaskGetTickCount() * portTICK_RATE_MS;
99 return PM_RET_OK;
102 void plat_reportError(PmReturn_t result)
104 #ifdef HAVE_DEBUG_INFO
105 #define LEN_FNLOOKUP 26
106 #define LEN_EXNLOOKUP 17
108 uint8_t res;
109 pPmFrame_t pframe;
110 pPmObj_t pstr;
111 PmReturn_t retval;
112 uint8_t bcindex;
113 uint16_t bcsum;
114 uint16_t linesum;
115 uint16_t len_lnotab;
116 uint8_t const *plnotab;
117 uint16_t i;
119 /* This table should match src/vm/fileid.txt */
120 char const * const fnlookup[LEN_FNLOOKUP] = {
121 "<no file>",
122 "codeobj.c",
123 "dict.c",
124 "frame.c",
125 "func.c",
126 "global.c",
127 "heap.c",
128 "img.c",
129 "int.c",
130 "interp.c",
131 "pmstdlib_nat.c",
132 "list.c",
133 "main.c",
134 "mem.c",
135 "module.c",
136 "obj.c",
137 "seglist.c",
138 "sli.c",
139 "strobj.c",
140 "tuple.c",
141 "seq.c",
142 "pm.c",
143 "thread.c",
144 "float.c",
145 "class.c",
146 "bytearray.c",
149 /* This table should match src/vm/pm.h PmReturn_t */
150 char const * const exnlookup[LEN_EXNLOOKUP] = {
151 "Exception",
152 "SystemExit",
153 "IoError",
154 "ZeroDivisionError",
155 "AssertionError",
156 "AttributeError",
157 "ImportError",
158 "IndexError",
159 "KeyError",
160 "MemoryError",
161 "NameError",
162 "SyntaxError",
163 "SystemError",
164 "TypeError",
165 "ValueError",
166 "StopIteration",
167 "Warning",
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");
186 return;
188 else
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,
203 &pstr);
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);
213 bcsum = 0;
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,
223 linesum,
224 ((pPmString_t)pstr)->val);
227 /* Print error */
228 res = (uint8_t)result;
229 if ((res > 0) && ((res - PM_RET_EX) < LEN_EXNLOOKUP))
231 printf("%s", exnlookup[res - PM_RET_EX]);
233 else
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]);
243 else
245 printf("FileId 0x%02X line ", gVmGlobal.errFileId);
247 printf("%d\n", gVmGlobal.errLineNum);
249 #else /* HAVE_DEBUG_INFO */
251 /* Print error */
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 */
259 pPmObj_t pframe;
260 pPmObj_t pstr;
261 PmReturn_t retval;
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");
278 return;
280 else
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 */
290 for (;
291 pframe != C_NULL;
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 */