econotag.py

00001 # This file is Copyright 2010 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 #
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING up one directory from this.
00012 
00013 """__NATIVE__
00014 #include "mc1322x.h"
00015 #include "board.h"
00016 
00017 #ifndef HAVE_BYTEARRAY
00018 #error HAVE_BYTEARRAY must be defined for the Econotag packet implementation
00019 #endif
00020 """
00021 
00022 
00023 def gpio_func_sel(gpio, mode):
00024     """__NATIVE__
00025     PmReturn_t retval = PM_RET_OK;
00026     pPmObj_t pgpio, pmode;
00027     int32_t gpio, mode;
00028     uint32_t mask;
00029     uint8_t shift;
00030     volatile uint32_t *func_sel_regs[] = {GPIO_FUNC_SEL0, GPIO_FUNC_SEL1,
00031                                           GPIO_FUNC_SEL2, GPIO_FUNC_SEL3};
00032     volatile uint32_t *func_sel_reg;
00033 
00034     /* Raise TypeError if wrong number of args */
00035     if (NATIVE_GET_NUM_ARGS() != 2)
00036     {
00037         PM_RAISE(retval, PM_RET_EX_TYPE);
00038         return retval;
00039     }
00040 
00041     /* Raise TypeError if args are not ints */
00042     pgpio = NATIVE_GET_LOCAL(0);
00043     pmode = NATIVE_GET_LOCAL(1);
00044     if ((OBJ_GET_TYPE(pgpio) != OBJ_TYPE_INT)
00045         || (OBJ_GET_TYPE(pmode) != OBJ_TYPE_INT))
00046     {
00047         PM_RAISE(retval, PM_RET_EX_TYPE);
00048         return retval;
00049     }
00050 
00051     /* Raise ValueError if either value is out of range */
00052     gpio = ((pPmInt_t)pgpio)->val;
00053     mode = ((pPmInt_t)pmode)->val;
00054     if ((gpio < 0) || (gpio > 63) || (mode < 0) || (mode > 3))
00055     {
00056         PM_RAISE(retval, PM_RET_EX_VAL);
00057         return retval;
00058     }
00059 
00060     /* Choose the register and calculate the shift needed for the gpio */
00061     func_sel_reg = func_sel_regs[gpio >> 4];
00062     shift = (gpio & 0x0F) << 1;
00063     mask = 0x03 << shift;
00064 
00065     /* Clear the old mode, apply the new mode */
00066     *func_sel_reg &= ~mask;
00067     *func_sel_reg |= (mode << shift);
00068 
00069     NATIVE_SET_TOS(PM_NONE);
00070     return retval;
00071     """
00072     pass
00073 
00074 
00075 def gpio_pad_dir(lo32, hi32):
00076     """__NATIVE__
00077     PmReturn_t retval = PM_RET_OK;
00078     pPmObj_t plo, phi;
00079 
00080     /* Raise TypeError if wrong number of args */
00081     if (NATIVE_GET_NUM_ARGS() != 2)
00082     {
00083         PM_RAISE(retval, PM_RET_EX_TYPE);
00084         return retval;
00085     }
00086 
00087     /* Raise TypeError if args are not ints */
00088     plo = NATIVE_GET_LOCAL(0);
00089     phi = NATIVE_GET_LOCAL(1);
00090     if ((OBJ_GET_TYPE(plo) != OBJ_TYPE_INT)
00091         || (OBJ_GET_TYPE(phi) != OBJ_TYPE_INT))
00092     {
00093         PM_RAISE(retval, PM_RET_EX_TYPE);
00094         return retval;
00095     }
00096 
00097     *GPIO_PAD_DIR0 = ((pPmInt_t)plo)->val;
00098     *GPIO_PAD_DIR1 = ((pPmInt_t)phi)->val;
00099     NATIVE_SET_TOS(PM_NONE);
00100     return retval;
00101     """
00102     pass
00103 
00104 
00105 def gpio_data(lo32, hi32):
00106     """__NATIVE__
00107     PmReturn_t retval = PM_RET_OK;
00108     pPmObj_t plo, phi;
00109 
00110     /* Raise TypeError if wrong number of args */
00111     if (NATIVE_GET_NUM_ARGS() != 2)
00112     {
00113         PM_RAISE(retval, PM_RET_EX_TYPE);
00114         return retval;
00115     }
00116 
00117     /* Raise TypeError if args are not ints */
00118     plo = NATIVE_GET_LOCAL(0);
00119     phi = NATIVE_GET_LOCAL(1);
00120     if ((OBJ_GET_TYPE(plo) != OBJ_TYPE_INT)
00121         || (OBJ_GET_TYPE(phi) != OBJ_TYPE_INT))
00122     {
00123         PM_RAISE(retval, PM_RET_EX_TYPE);
00124         return retval;
00125     }
00126 
00127     *GPIO_DATA0 = ((pPmInt_t)plo)->val;
00128     *GPIO_DATA1 = ((pPmInt_t)phi)->val;
00129     NATIVE_SET_TOS(PM_NONE);
00130     return retval;
00131     """
00132     pass
00133 
00134 
00135 def gpio_data_get_hi():
00136     """__NATIVE__
00137     PmReturn_t retval = PM_RET_OK;
00138     pPmObj_t pn;
00139 
00140     /* Raise TypeError if wrong number of args */
00141     if (NATIVE_GET_NUM_ARGS() != 0)
00142     {
00143         PM_RAISE(retval, PM_RET_EX_TYPE);
00144         return retval;
00145     }
00146 
00147         retval = int_new(*GPIO_DATA1, &pn);
00148     NATIVE_SET_TOS(pn);
00149     return retval;
00150     """
00151     pass
00152 
00153 
00154 def gpio_data_get_lo():
00155     """__NATIVE__
00156     PmReturn_t retval = PM_RET_OK;
00157     pPmObj_t pn;
00158 
00159     /* Raise TypeError if wrong number of args */
00160     if (NATIVE_GET_NUM_ARGS() != 0)
00161     {
00162         PM_RAISE(retval, PM_RET_EX_TYPE);
00163         return retval;
00164     }
00165 
00166         retval = int_new(*GPIO_DATA0, &pn);
00167     NATIVE_SET_TOS(pn);
00168     return retval;
00169     """
00170     pass
00171 
00172 
00173 def set_channel(n):
00174     """__NATIVE__
00175     PmReturn_t retval = PM_RET_OK;
00176     pPmObj_t pn;
00177     uint8_t n;
00178 
00179     /* Raise TypeError if wrong number of args */
00180     if (NATIVE_GET_NUM_ARGS() != 1)
00181     {
00182         PM_RAISE(retval, PM_RET_EX_TYPE);
00183         return retval;
00184     }
00185 
00186     /* Raise TypeError if arg is not an int */
00187     pn = NATIVE_GET_LOCAL(0);
00188     if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
00189     {
00190         PM_RAISE(retval, PM_RET_EX_TYPE);
00191         return retval;
00192     }
00193 
00194     /* Raise ValueError if arg is not within 0..15 inclusive */
00195     n = (uint8_t)(((pPmInt_t)pn)->val);
00196     if (n >= (uint8_t)16)
00197     {
00198         PM_RAISE(retval, PM_RET_EX_VAL);
00199         return retval;
00200     }
00201 
00202     maca_off();
00203     set_channel(n);
00204     maca_on();
00205 
00206     NATIVE_SET_TOS(PM_NONE);
00207     return retval;
00208     """
00209     pass
00210 
00211 
00212 def set_power(n):
00213     """__NATIVE__
00214     PmReturn_t retval = PM_RET_OK;
00215     pPmObj_t pn;
00216     uint8_t n;
00217 
00218     /* Raise TypeError if wrong number of args */
00219     if (NATIVE_GET_NUM_ARGS() != 1)
00220     {
00221         PM_RAISE(retval, PM_RET_EX_TYPE);
00222         return retval;
00223     }
00224 
00225     /* Raise TypeError if arg is not an int */
00226     pn = NATIVE_GET_LOCAL(0);
00227     if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
00228     {
00229         PM_RAISE(retval, PM_RET_EX_TYPE);
00230         return retval;
00231     }
00232 
00233     /* Raise ValueError if arg is not within 0..18 inclusive */
00234     n = (uint8_t)(((pPmInt_t)pn)->val);
00235     if (n > (uint8_t)18)
00236     {
00237         PM_RAISE(retval, PM_RET_EX_VAL);
00238         return retval;
00239     }
00240 
00241     maca_off();
00242     set_power(n);
00243     maca_on();
00244 
00245     NATIVE_SET_TOS(PM_NONE);
00246     return retval;
00247     """
00248     pass
00249 
00250 
00251 def check_maca():
00252     """__NATIVE__
00253     PmReturn_t retval = PM_RET_OK;
00254 
00255     /* Raise TypeError if wrong number of args */
00256     if (NATIVE_GET_NUM_ARGS() != 0)
00257     {
00258         PM_RAISE(retval, PM_RET_EX_TYPE);
00259         return retval;
00260     }
00261 
00262     check_maca();
00263 
00264     NATIVE_SET_TOS(PM_NONE);
00265     return retval;
00266     """
00267     pass
00268 
00269 
00270 def tx_packet(p):
00271     """__NATIVE__
00272     PmReturn_t retval = PM_RET_OK;
00273         volatile packet_t *p;
00274         pPmObj_t ppkt;
00275         pPmBytearray_t pba;
00276         pPmString_t pstr;
00277         pPmInt_t pn;
00278 
00279     /* Raise TypeError if wrong number of args */
00280     if (NATIVE_GET_NUM_ARGS() != 1)
00281     {
00282         PM_RAISE(retval, PM_RET_EX_TYPE);
00283         return retval;
00284     }
00285 
00286     ppkt = NATIVE_GET_LOCAL(0);
00287 
00288     /* If object is an instance, get the thing it is containing */
00289     if (OBJ_GET_TYPE(ppkt) == OBJ_TYPE_CLI)
00290     {
00291         retval = dict_getItem((pPmObj_t)((pPmInstance_t)ppkt)->cli_attrs,
00292                               PM_NONE,
00293                               (pPmObj_t *)&pba);
00294         PM_RETURN_IF_ERROR(retval);
00295         ppkt = (pPmObj_t)pba;
00296     }
00297 
00298     /* Raise TypeError if arg is not a string or bytearray */
00299     if ((OBJ_GET_TYPE(ppkt) != OBJ_TYPE_STR)
00300         && (OBJ_GET_TYPE(ppkt) != OBJ_TYPE_BYA))
00301     {
00302         NATIVE_SET_TOS(PM_NONE);
00303         PM_RAISE(retval, PM_RET_EX_TYPE);
00304         return retval;
00305     }
00306 
00307     p = get_free_packet();
00308     if (!p)
00309     {
00310         /* TODO: raise an IoError (when VM can catch exceptions) */
00311         NATIVE_SET_TOS(PM_NONE);
00312         return retval;
00313     }
00314 
00315     /* Copy bytes from string arg to packet */
00316     p->offset = 0;
00317     if (OBJ_GET_TYPE(ppkt) == OBJ_TYPE_STR)
00318     {
00319         pstr = (pPmString_t)ppkt;
00320         sli_memcpy((unsigned char *)&(p->data[0]),
00321                    (unsigned char *)&(pstr->val[0]),
00322                    (unsigned int)pstr->length);
00323         p->length = pstr->length;
00324     }
00325 
00326     /* Copy bytes from bytearray arg to packet */
00327     else
00328     {
00329         pba = (pPmBytearray_t)ppkt;
00330         sli_memcpy((unsigned char *)&(p->data[0]),
00331                    (unsigned char *)&(pba->val->val[0]),
00332                    (unsigned int)pba->length);
00333         p->length = pba->length;
00334     }
00335 
00336     retval = int_new(p->length, &pn);
00337     NATIVE_SET_TOS(pn);
00338 
00339     tx_packet(p);
00340 
00341     return retval;
00342     """
00343     pass
00344 
00345 
00346 def rx_packet():
00347     """__NATIVE__
00348     PmReturn_t retval = PM_RET_OK;
00349     volatile packet_t *p;
00350     pPmBytearray_t pba;
00351     pPmInt_t pn;
00352     uint8_t objid, objid2;
00353     pPmObj_t pbaclass;
00354     pPmObj_t pcli;
00355 
00356     p = rx_packet();
00357     if (!p)
00358     {
00359         NATIVE_SET_TOS(PM_NONE);
00360         return retval;
00361     }
00362 
00363     /* Allocate a bytearray */
00364     retval = int_new(p->length, (pPmObj_t *)&pn);
00365     if (retval != PM_RET_OK)
00366     {
00367         free_packet(p);
00368         return retval;
00369     }
00370 
00371     heap_gcPushTempRoot((pPmObj_t)pn, &objid);
00372     retval = bytearray_new(pn, (pPmObj_t *)&pba);
00373     heap_gcPopTempRoot(objid);
00374     if (retval != PM_RET_OK)
00375     {
00376         free_packet(p);
00377         return retval;
00378     }
00379     heap_freeChunk(pn);
00380 
00381     /* Copy packet payload to bytearray */
00382     sli_memcpy((unsigned char *)&(pba->val->val[0]),
00383                (unsigned char *)&(p->data[p->offset]),
00384                (unsigned int)p->length);
00385     free_packet(p);
00386 
00387     /* Create an instance of bytearray to hold the bytearray struct */
00388     retval = dict_getItem(PM_PBUILTINS, PM_BYTEARRAY_STR, &pbaclass);
00389     PM_RETURN_IF_ERROR(retval);
00390     heap_gcPushTempRoot((pPmObj_t)pba, &objid);
00391     retval = class_instantiate(pbaclass, &pcli);
00392     if (retval != PM_RET_OK)
00393     {
00394         heap_gcPopTempRoot(objid);
00395         return retval;
00396     }
00397 
00398     /* Store bytearray struct in the instance's None attribute */
00399     heap_gcPushTempRoot(pcli, &objid2);
00400     retval = dict_setItem((pPmObj_t)((pPmInstance_t)pcli)->cli_attrs,
00401                           PM_NONE,
00402                           (pPmObj_t)pba);
00403     heap_gcPopTempRoot(objid);
00404 
00405     NATIVE_SET_TOS(pcli);
00406     return retval;
00407     """
00408     pass
00409 
00410 
00411 def random():
00412     """__NATIVE__
00413     PmReturn_t retval;
00414     pPmObj_t pr = C_NULL;
00415 
00416     /* If wrong number of args, raise TypeError */
00417     if (NATIVE_GET_NUM_ARGS() != 0)
00418     {
00419         PM_RAISE(retval, PM_RET_EX_TYPE);
00420         return retval;
00421     }
00422 
00423     retval = int_new(*MACA_RANDOM, &pr);
00424     NATIVE_SET_TOS(pr);
00425     return retval;
00426     """
00427     pass
00428 
00429 
00430 # :mode=c:

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9