main.py

Go to the documentation of this file.
00001 # This file is Copyright 2007, 2009 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 ## @file
00014 #  @brief System test for the pic24/dspic33.
00015 #
00016 
00017 """__NATIVE__
00018 #include <pic24_all.h>
00019 #include <pps.h>
00020 #include "pyToC.h"
00021 
00022 /** Macro to convert a number to a string.
00023  *  Typical usage: <code>TOSTRING(__LINE__)</code>
00024  */
00025 #define TOSTRING(x) _TOSTRING(x)
00026 // A helper macro used by \\ref TOSTRING.
00027 #define _TOSTRING(x) #x
00028 
00029 #define EXCEPTION_ASSERT(a) EXCEPTION_UNLESS(a, PM_RET_EX_VAL, __FILE__ ":" TOSTRING(__LINE__) " - " #a "\\n")
00030 """
00031 
00032 ## Check results of pin config
00033 def testConfigDigitalPin1():
00034     """__NATIVE__
00035     PmReturn_t retval = PM_RET_OK;
00036 
00037     EXCEPTION_ASSERT(_PCFG3 == 1);
00038     EXCEPTION_ASSERT(_TRISB1 == 0);
00039     EXCEPTION_ASSERT(_ODCB1 == 0);
00040     EXCEPTION_ASSERT(_CN5PUE == 0);
00041     return retval;
00042     """
00043     pass
00044 
00045 ## Check results of pin config
00046 def testConfigDigitalPin2():
00047     """__NATIVE__
00048     PmReturn_t retval = PM_RET_OK;
00049 
00050     EXCEPTION_ASSERT(_PCFG3 == 1);
00051     EXCEPTION_ASSERT(_TRISB1 == 1);
00052     EXCEPTION_ASSERT(_ODCB1 == 1);
00053     EXCEPTION_ASSERT(_CN5PUE == 1);
00054     return retval;
00055     """
00056     pass
00057 
00058 ## Check results of analog pin config
00059 def testConfigAnalogPin():
00060     """__NATIVE__
00061     PmReturn_t retval = PM_RET_OK;
00062 
00063     EXCEPTION_ASSERT(_PCFG0 == 0);
00064     EXCEPTION_ASSERT(_TRISA0 == 1);
00065     EXCEPTION_ASSERT(_CN2PUE == 0);
00066     return retval;
00067     """
00068     pass
00069 
00070 ## Check results of analog pin config
00071 def testConfigPwm():
00072     """__NATIVE__
00073     PmReturn_t retval = PM_RET_OK;
00074 
00075     EXCEPTION_ASSERT(_PCFG0 == 0);
00076     EXCEPTION_ASSERT(_TRISA0 == 1);
00077     EXCEPTION_ASSERT(_CN2PUE == 0);
00078     EXCEPTION_ASSERT(OC2CON == (OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE));
00079 #if (FCY == 1000000L)
00080     EXCEPTION_ASSERT(T2CON == (T2_ON | T2_IDLE_CON | T2_GATE_OFF
00081           | T2_32BIT_MODE_OFF | T2_SOURCE_INT | T2_PS_1_1));
00082     EXCEPTION_ASSERT(PR2 == 999);
00083 #endif
00084     EXCEPTION_ASSERT(_RP0R == OUT_FN_PPS_OC2);
00085     EXCEPTION_ASSERT(OC2RS == 0);
00086     EXCEPTION_ASSERT(_TRISB0 == 0);
00087     EXCEPTION_ASSERT(_PCFG2 == 1);
00088     EXCEPTION_ASSERT(_CN4PUE == 0);
00089     return retval;
00090     """
00091     pass
00092 
00093 ## Check results of PWM setCounts method
00094 def testPwmSetCounts():
00095     """__NATIVE__
00096     PmReturn_t retval = PM_RET_OK;
00097     EXCEPTION_ASSERT(OC2RS == 600);
00098     return retval;
00099     """
00100     pass
00101 
00102 ## Check results of PWM setCounts method
00103 def testPwmSet():
00104     """__NATIVE__
00105     PmReturn_t retval = PM_RET_OK;
00106 #if (FCY == 1000000L)
00107     // At a PWM frequeny of 1000 Hz, PR2 = 999, so
00108     // half of (PR2 + 1) is 500.
00109     EXCEPTION_ASSERT(OC2RS == 500);
00110 #endif
00111     return retval;
00112     """
00113     pass
00114 
00115 
00116 import pic24_dspic33 as pic
00117 
00118 # Test digital I/O briefly
00119 # ------------------------
00120 #                    port pin isInput isOpenDrain pullDir
00121 dio = pic.digital_io(1,   1,  False,  False,      0)
00122 testConfigDigitalPin1()
00123 #                    port pin isInput isOpenDrain pullDir
00124 dio = pic.digital_io(1,   1,  True,   True,       1)
00125 testConfigDigitalPin2()
00126 
00127 #                    port pin isInput isOpenDrain pullDir
00128 dio = pic.digital_io(1,   1,  False,  False,      0)
00129 dio.set(True)
00130 assert(dio.get())
00131 assert(dio.getPin())
00132 assert(dio.getLatch())
00133 
00134 # Test default args
00135 dio = pic.digital_io(1, 1, False);
00136 
00137 # Test analog input
00138 # -----------------
00139 # Set up a digital input, then change to analog to
00140 # verify that the analog config reset everything
00141 # properly.
00142 dio = pic.digital_io(0,   0,  False,  False,      1)
00143 ain = pic.analog_input(0)
00144 testConfigAnalogPin()
00145 # Works only in Simulation mode. Follow the directions in
00146 # http://www.microchip.com/forums/tm.aspx?m=170556
00147 # to set everything up; this simulation assumes use of
00148 # system_test.sbs, which uses adc_injection.txt as input.
00149 # From this, the generated system_test.scl file provides
00150 # stimulus for the ADC.
00151 assert(ain.getCode() == 0x1A4)
00152 
00153 # Test PWM
00154 # --------
00155 #              freq  isTimer2 oc ocPin
00156 pwm1 = pic.pwm(1000, True,    2, 0)
00157 testConfigPwm()
00158 pwm1.setCounts(600)
00159 testPwmSetCounts()
00160 pwm1.set(0.5)
00161 testPwmSet()
00162 
00163 print "All tests passed.\n"

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