Jon Freeman / Mbed 2 deprecated Altera_FPGA_test

Dependencies:   MODSERIAL mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "MODSERIAL.h"
00004 #include "cnc.h"
00005 extern  void    i2c_handler    (void const *);
00006 extern  void    command_line_interpreter    (void const *) ;
00007 extern  fl_typ  feed_rate;      //  float type is 'float'
00008 extern  signed long spindle_rpm;
00009 
00010 const   int BAUD = 38400;
00011 MODSERIAL  pc(USBTX, USBRX);    //  tx, rx to pc via usb lead
00012 Ticker  msec;                   //  Ticker updating global millisecs counter
00013 
00014 bool    running         = false,
00015         new_run_pending = false,
00016         idle            = false,
00017         move_ended      = false;
00018 
00019 unsigned long   millisecs = 0L;        //  32 bit
00020 
00021 #if defined (TARGET_KL25Z)
00022     const char Target[] = "KL25Z";  //  Note need PTE0 (sda) and PTE1 (scl)
00023     DigitalOut intled               (PTD7); //(PTE1);     //J2p19, was 20
00024     DigitalOut charge_pumpD25pin1   (PTD6); //(PTE0);     //J2p17, was 18    
00025     DigitalIn     D25pin10_EStop    (PTE20);    //j10p1   KL25 J10 is KL46 j4
00026     DigitalIn       D25pin11_XLim     (PTE21);    //j10p3
00027     DigitalIn       D25pin12_YLim     (PTE22);    //j10p5
00028     DigitalIn       D25pin13_ZLim     (PTE23);    //j10p7
00029     DigitalIn       D25pin15_unkn     (PTE30);    //j10p11
00030 #if defined I2C_Enable
00031     I2CSlave slave(PTE0, PTE1); //  PTE0 sda, (yellow) PTE1 scl (blue)
00032 #endif
00033 #if defined SPI_Enable
00034     SPI spi(PTD2, PTD3, PTD1, PTD0); // mosi, miso, sclk (uses p11, p12, p13 on mbed LPC1768)
00035 #endif
00036     //                 J2p08,J2p10,J2p12, J2p06
00037     #define STEPPER_PORT    PortC
00038     const   int PortBitXSt   = 3,    //  Port bit num X Step    J1P05   D25pin 2
00039                 PortBitXDi   = 4,    //  Port bit num X Dir     J1P07   D25pin 3
00040                 PortBitYSt   = 5,    //  Port bit num Y Step    J1P09   D25pin 4
00041                 PortBitYDi   = 6,    //  Port bit num Y Dir     J1P11   D25pin 5
00042                 PortBitZSt   = 10,    //  Port bit num Z Step   J1P13   D25pin 6
00043                 PortBitZDi   = 11,    //  Port bit num Z Dir    J1P15   D25pin 7
00044                 PortBitASt   = 12,    //  Port bit num A Step   J2P01   D25pin 8
00045                 PortBitADi   = 13,    //  Port bit num A Dir    J2P03   D25pin 9
00046                 PortBitSSt   = 8,    //  Port bit num Spin Step   J1P14 D25pin 14
00047                 PortBitSDi   = 9;    //  Port bit num Spin Dir    J1P16 D25pin 16
00048 #endif
00049 
00050 
00051 
00052 #if defined (TARGET_KL46Z)
00053     const char Target[] = "KL46Z";
00054     DigitalOut intled               (PTE1);    //J2p20 checked
00055 
00056 
00057     DigitalOut charge_pumpD25pin1   (PTE0);    //J2p18 checked
00058 //    InterruptIn     D25pin10_EStop    (PTE20);  // j4p1  KL46 J4 is KL25 J10
00059     DigitalIn     D25pin10_EStop    (PTE20);  // j4p1  KL46 J4 is KL25 J10 checked
00060     DigitalIn       D25pin11_XLim     (PTE21);  // j4p3 checked
00061     DigitalIn       D25pin12_YLim     (PTE22);  // j4p5 checked
00062     DigitalIn       D25pin13_ZLim     (PTE23);  // j4p7 checked
00063     DigitalIn       D25pin15_unkn     (PTE30);  // j4p11 checked
00064 #if defined I2C_Enable
00065     I2CSlave slave(p9, p10);
00066 #endif
00067 #if defined SPI_Enable
00068     SPI spi(PTA16, PTA17, PTA15, PTA14); // mosi, miso, sclk, ssel (uses p11, p12, p13, p? on mbed LPC)
00069 #endif
00070     //                 J2p13, J2p15, J2p11, J2p09
00071                 // Easy way to allocate port bits for
00072                 // output of stepper motor Step and DIR sigs
00073     #define STEPPER_PORT    PortC
00074     const   int PortBitXSt   = 0,    //  Port bit num X Step    J1P05   D25pin 2 checked
00075                 PortBitXDi   = 4,    //  Port bit num X Dir     J1P07   D25pin 3 checked
00076                 PortBitYSt   = 6,    //  Port bit num Y Step    J1P09   D25pin 4 checked
00077                 PortBitYDi   = 7,    //  Port bit num Y Dir     J1P11   D25pin 5 checked
00078                 PortBitZSt   = 10,    //  Port bit num Z Step   J1P13   D25pin 6 checked
00079                 PortBitZDi   = 11,    //  Port bit num Z Dir    J1P15   D25pin 7 checked
00080                 PortBitASt   = 13,    //  Port bit num A Step   J2P01   D25pin 8 checked
00081                 PortBitADi   = 16,    //  Port bit num A Dir    J2P03   D25pin 9 checked
00082                 PortBitSSt   = 8,    //  Port bit num Spin Step J1P14   D25pin 14 checked
00083                 PortBitSDi   = 9;    //  Port bit num Spin Dir  J1P16   D25pin 16 checked
00084 #endif
00085 #if defined (TARGET_MBED_LPC1768)
00086     const char Target[] = "MBED LPC1768";
00087     DigitalOut intled(LED2);                    //  Correct
00088     DigitalOut charge_pumpD25pin1      (p25);    //
00089 //    InterruptIn D25pin10_EStop  (p26);    //P2.0
00090     DigitalIn D25pin10_EStop  (p26);    //P2.0
00091     DigitalIn   D25pin11_XLim   (p24);    //P2.2
00092     DigitalIn   D25pin12_YLim   (p23);    //P2.3
00093     DigitalIn   D25pin13_ZLim   (p19);    //P1.30
00094     DigitalIn   D25pin15_unkn   (p20);    //P1.31
00095 #if defined I2C_Enable
00096     I2CSlave slave(p9, p10);
00097 #endif
00098 //#if defined SPI_Enable
00099     SPI spi(p5, p6, p7);
00100 //#endif
00101                 // Easy way to allocate port bits
00102                 // output of stepper motor Step and DIR sigs
00103     #define STEPPER_PORT    Port0
00104     /* Port 0 bits routed to DIP pins as follows:-
00105         P0.00   p09 Reserve SDA
00106         P0.01   p10 Reserve SCL
00107         P0.04   p30 CAN rd  -   USE X Step  D25pin 2
00108         P0.05   p29 CAN td  -   USE X Dir   D25pin 3
00109         P0.10   p28 SDA     -   USE Y Step  D25pin 4
00110         P0.11   p27 SCL     -   USE Y Dir   D25pin 5
00111         P0.15   p13 Tx      -   USE Z Step  D25pin 6
00112         P0.16   p14 Rx      -   USE Z Dir   D25pin 7
00113         P0.17   p12 miso    -   USE A Step  D25pin 8
00114         P0.18   p11 mosi    -   Use A Dir   D25pin 9
00115         P0.23   p15 A In    -   Use S Step  D25pin 14
00116         P0.24   p16 A In    -   Use S Dir   D25pin 16
00117         P0.25   p17 Reserve A In
00118         P0.26   p18 Reserve A Out
00119     */
00120     const   int PortBitXSt  = 4,    //  Port bit num X Step
00121                 PortBitXDi  = 5,    //  Port bit num X Dir
00122                 PortBitYSt  = 10,    //  Port bit num Y Step
00123                 PortBitYDi  = 11,    //  Port bit num Y Dir
00124                 PortBitZSt  = 15,    //  Port bit num Z Step
00125                 PortBitZDi  = 16,    //  Port bit num Z Dir
00126                 PortBitASt  = 17,    //  Port bit num A Step
00127                 PortBitADi  = 18,    //  Port bit num A Dir
00128                 PortBitSSt  = 23,   //  Port bit num Spin Step
00129                 PortBitSDi  = 24;   //  Port bit num Spin Dir
00130 #endif
00131 
00132 const   long    //  Assemble mask bits from now known port bit positions
00133         XSt =   1 << PortBitXSt,    //  X axis Step signal
00134         XDi =   1 << PortBitXDi,    //  X axis Direction signal
00135         YSt =   1 << PortBitYSt,    //  Y axis Step, etc
00136         YDi =   1 << PortBitYDi,
00137         ZSt =   1 << PortBitZSt,    //  Z axis
00138         ZDi =   1 << PortBitZDi,
00139         ASt =   1 << PortBitASt,    //  A axis, not implemented in full, for e.g. rotary axis
00140         ADi =   1 << PortBitADi,
00141         SDi =   1 << PortBitSDi,     //  Spindle, also driven by Step and Dir signals up to 5kHz
00142         SSt =   1 << PortBitSSt,     //  for 5000 RPM
00143 
00144         SM_MASK = (XSt | XDi | YSt | YDi | ZSt | ZDi | ASt | ADi | SDi | SSt);
00145 //        direction_swappers = XDi | YDi | ZDi | SDi; //  include bit to swap direction
00146 
00147     PortOut Steppers    (STEPPER_PORT, SM_MASK);
00148 
00149 int freq_to_n   (int freq)  {
00150     const double factor = (1 << 25) / 390625.0;
00151 //    unsigned long long ll = ((1 << 31) * freq) / 50000000; 
00152     double ll = factor * (double)freq;
00153     return  (long) ll;
00154 }
00155 //  freq = (50E6 * 'n' / 2**BUS_WIDTH)
00156 //  'n' = freq * 2**BUS_WIDTH / 50000000
00157 //  'n' = freq * 2**31 / 25000000
00158 //  'n' = freq * 2**30 / 12500000
00159 //  'n' = freq * 2**29 / 6250000
00160 //  'n' = freq * 2**25 / 390625
00161 
00162 int pirs[8], dros[8];
00163 
00164 void    FPGA_bit    (int whichbit, int hiorlo)    {
00165     int port = Steppers;
00166     if  (hiorlo)    port |= whichbit;
00167     else            port &= ~whichbit;
00168     Steppers = port;
00169 }
00170 
00171 void    FPGA_setup  ()  {
00172     int port = Steppers;
00173     port |= sclr | clken;
00174     Steppers = port;
00175     port &= ~sclr;
00176     port &= ~clken;
00177     port &= ~ld_osr;
00178     port &= ~ld_pir;
00179     Steppers = port;
00180     for (int i = 0; i < 8; i++)
00181         pirs[i] = dros[i] = 0;
00182 }
00183 
00184 /*
00185 --    About Use of 16 bit Command Word
00186 --    bit 0   -   '1' causes zero reset of phase_inc_reg
00187 --    bit 1   -   '1' causes zero reset of dro_udcounter
00188 --    bit 2   -   '1' causes load of phase_inc_reg from input shift reg
00189 --    bit 3   -   '1' causes load of dro_udcounter from input shift reg 
00190 --    bit 4   -   '1' causes dro_udcounter --> shift reg ready to read dro value
00191 --    bit 5   -   '1' causes phase_inc_reg --> shift reg ready to read pir value
00192 --    bit 6   -   '1' causes reset everything to 0
00193 --
00194 --    bit 15  -   '1' causes reset of command_word to all 0 after one clock
00195 
00196 */
00197 #define zero_pir            0x8001
00198 #define zero_dro            0x8002
00199 #define load_pir_from_sr    0x8004
00200 #define load_dro_from_sr    0x8008
00201 #define load_dro_into_sr    0x8010
00202 #define load_pir_into_sr    0x8020
00203 
00204 void    FPGA_cmd    (int command_word)  {
00205     int port = Steppers;
00206     int spirx[8], command_copy = command_word;
00207 //    pc.printf("At FPGA_cmd, sending %d\r\n", command_word);
00208     port |= sclr;       //  cmdhi_datalo set to 1
00209     Steppers = port;    //  
00210     spirx[0] = spi.write(command_copy >> 8);
00211     spirx[1] = spi.write(command_copy);
00212     port &= ~sclr;       //  cmdhi_datalo set to 0
00213     Steppers = port;    //  
00214 //    pc.printf("Read spi %x %x\r\n", spirx[0], spirx[1]);
00215 }
00216 
00217 void    setcmd_cmd  (struct singleGparam * a)  {
00218     FPGA_cmd    (a[1].i);
00219 }
00220 
00221 //#define load_pir_from_sr    0x8004
00222 //#define load_dro_into_sr    0x8010
00223 
00224 //    DigitalIn   D25pin15_unkn   (p20);    //P1.31 use this to read osr
00225 int FPGA_rdandwr    (int tosend)  { //  send 32 bits to in_sr, read 32 bits from out_sr
00226     int torecv = 0, port = Steppers, tmp;
00227 //    tosend = freq_to_n(tosend);
00228     for (int j = 3; j >= 0; j--) {
00229         torecv <<= 8;
00230         tmp = tosend >> (j << 3);
00231         torecv |= spi.write(tmp);
00232     }
00233     return  torecv;
00234 }
00235 
00236 const int   numof_ncos = 4;
00237 
00238 void    setdro_cmd  (struct singleGparam * a)  {
00239     int recd[numof_ncos + 1];
00240     pc.printf("At setdro with values ");
00241     FPGA_cmd(load_dro_into_sr);
00242     for (int k = 0; k < numof_ncos; k++)    {
00243         recd[k] = FPGA_rdandwr    (a[k + 1].i);
00244         pc.printf("%d, ", a[k + 1].i);
00245     }
00246     FPGA_cmd(load_dro_from_sr);
00247     pc.printf("end\r\n");
00248 }
00249 
00250 void    setpir_cmd  (struct singleGparam * a)  {
00251     int recd[numof_ncos + 1];
00252     pc.printf("At setpir with values ");
00253     FPGA_cmd(load_dro_into_sr);
00254     for (int k = 0; k < numof_ncos; k++)    {
00255         recd[k] = FPGA_rdandwr    (a[k + 1].i);
00256         pc.printf("%d, ", a[k + 1].i);
00257     }
00258     FPGA_cmd(load_pir_from_sr);
00259     pc.printf("end\r\n");
00260 }
00261 
00262 void    getdro_cmd  (struct singleGparam * a)  {
00263     int read_dro[numof_ncos + 1];
00264     pc.printf("At rddro, retrieved values ");
00265     FPGA_cmd(load_dro_into_sr);
00266     for (int k = 0; k < numof_ncos; k++)    {
00267         read_dro[k] = FPGA_rdandwr (0);
00268         pc.printf("%d, ", read_dro[k]);
00269     }
00270     pc.printf(" end\r\n");
00271 }
00272 
00273 void    getpir_cmd  (struct singleGparam * a)  {
00274     int read_pir[numof_ncos + 1];
00275     pc.printf("At rdpir, retrieved values ");
00276     FPGA_cmd(load_pir_into_sr);
00277     for (int k = 0; k < numof_ncos; k++)    {
00278         read_pir[k] = FPGA_rdandwr (0);
00279         pc.printf("%d, ", read_pir[k]);
00280     }
00281     pc.printf(" end\r\n");
00282 }
00283 
00284 void    clrpir_cmd  (struct singleGparam * a)  {
00285     FPGA_cmd(zero_pir);
00286     getpir_cmd(a);    
00287 }
00288 
00289 void    clrdro_cmd  (struct singleGparam * a)  {
00290     FPGA_cmd(zero_dro);
00291     getdro_cmd(a);    
00292 }
00293 
00294 
00295 char const * target_str_addr  ()  {
00296     return  Target;
00297 }
00298 
00299 void    grain_clr   (struct singleGparam & g)  {
00300     g.flt = 0.0;
00301     g.ul = 0L;
00302     g.i = g.c = 0;
00303     g.changed = false;
00304 }
00305 void    Gparams_clr    (struct Gparams & p)   {
00306     grain_clr   (p.x);    grain_clr   (p.y);    grain_clr   (p.z);    grain_clr   (p.i);    grain_clr   (p.j);
00307     grain_clr   (p.r);    grain_clr   (p.a);    grain_clr   (p.b);    grain_clr   (p.c);    grain_clr   (p.d);
00308 }
00309 
00310 class digital_readout_stuff  {   //  class does not need to be named here
00311     private:
00312     char *  readout (char * txt, long p)         //  p has running subtotal of all pulses issued to stepper driver
00313     {
00314         txt[0] = '+';               //  constructs string e.g. "+123.456"
00315         txt[8] = 0;                 //  null terminated
00316         if  (p < 0)    {
00317             txt[0] = '-';
00318             p = -p;
00319         }
00320         p *= 1000;
00321 //??        p /= pulses_per_mm;
00322         if  (p > 999999)    {
00323             sprintf(txt + 1, "OVRANGE");
00324             return  txt;
00325         }
00326         for(int k = 7; k > 0; k--)  {
00327             if  (k == 4)
00328                 txt[k] = '.';
00329             else    {
00330                 txt[k] = '0' + (p % 10);
00331                 p /= 10;
00332             }
00333         }
00334         return  txt;    //  Returns pointer unaltered for subsequent use by e.g. cout
00335     }
00336         public:
00337     signed long x, y, z, a;    //  Could easily expand up to six or more dros
00338 //    bool    dro_output;             //  To enabe / disable output to terminal
00339     void    init    ()  {
00340         x = y = z = a = 0;   //  These dro registers count pulses delivered to stepper motor driver
00341 //        dro_output = true;
00342     }
00343     void    update  ()  {
00344         static  long    t = 300;    //  Prevent display immediately upon startup
00345         if  (millisecs < t)
00346             return;
00347 //        if(!idle && dro_output)  {
00348         if(!idle)  {
00349             char    txt[12];
00350             pc.printf("dros X %s,", readout(txt, x));    //  dro.n has running subtotal of all pulses issued to stepper driver.n
00351             pc.printf(" Y %s, Z ", readout(txt, y));
00352             pc.printf("%s, %s\r\n", readout(txt, z), running ? "R":"idle");
00353             if(!running)    idle = true;    //  Purpose of idle flag is to stop dro updates JUST AFTER run completes.
00354             t = millisecs + 350;    //  Schedule next update after this non-blocking delay
00355         }
00356     }
00357 }   dro_out ;   //  single instance of class digital_readout_stuff
00358 
00359 void    millisec_update_ISR ()  {
00360     millisecs++;
00361 }
00362 
00363 /*#define STEP_IDLE_HI    //  Choose IDLE_HI or LO to suit any power save function of stepper motor drive units
00364 //#define STEP_IDLE_LO
00365 void    Numerically_Controlled_Oscillators_ISR ()  {   // services Ticker 'NCO_gen' generated interrupts ***ISR***
00366     static  const   long    step_mask   = ASt | XSt | YSt | ZSt,    //  Added 6th Feb 14 Mask Does NOT include spindle bits
00367                             dir_mask    = ADi | XDi | YDi | ZDi;    //  Added 6th Feb 14 Mask Does NOT include spindle bits
00368     static  signed  long     //  27 Feb 14 changed from unsigned
00369 #if defined Fourth_Axis
00370         acc_a   = 0L,        pir_a   = 0L,
00371 #endif
00372         acc_x       = 0L,   //  acc Accumuloators
00373         pir_x       = 0L,   //  pir Phase Increment Registers
00374         acc_y       = 0L,        pir_y   = 0L,
00375         acc_z       = 0L,        pir_z   = 0L,
00376         acc_spin    = 0L,  //  separate acc for spindle rotation NCO
00377         inc_x       = 1L,   //  inc_x, y, z for updating DRO registers
00378         inc_y       = 1L,        inc_z   = 1L,
00379         dir_bits    = 0L,  //  direction flags for up to four axes
00380         oldSteps    = 0L;  //  
00381     long tmp, newSteps = 0L;
00382 
00383     intled = 1;     //  LED on for duration of interrupt service - point for scope probing
00384     ticks++;        //  count of interrupts serviced, vital to time end of movement
00385     charge_pumpD25pin1 = ticks & 0x01;  //  Can use 0x01 or 0x02 here to alter charge pump freq
00386     tmp = Steppers ^ direction_swappers;
00387 #if defined STEP_IDLE_LO
00388     tmp &= ~step_mask;   //  Step bits prepared for idle lo
00389 #endif
00390 #if defined STEP_IDLE_HI
00391     tmp |= step_mask;   //  Step bits prepared for idle hi
00392 #endif
00393     acc_spin += pir_spin;   //  Spindle NCO
00394     if  (acc_spin < 0)  tmp |= SSt;
00395     else                tmp &= ~SSt;
00396     if  (!running)      Steppers = tmp ^ direction_swappers;    //  Axes not moving, spindle may be turning or not
00397     else    {   //  running == true, Further manipulation of tmp follows, prior to rewriting to 'Steppers' IO Port
00398 //        newSteps = 0L;   //  Added 6th Feb 14
00399 #if defined Fourth_Axis
00400         acc_a   += pir_a;
00401         if  (acc_a < 0)    newSteps |= ASt;//  Added 6th Feb 14
00402 #endif
00403         acc_x   += pir_x;     //  Update phase of signals in accumulators
00404         if  (acc_x < 0)    newSteps |= XSt;//  Added 6th Feb 14
00405         acc_y   += pir_y;
00406         if  (acc_y < 0)    newSteps |= YSt;//  Added 6th Feb 14
00407         acc_z   += pir_z;
00408         if  (acc_z < 0)    newSteps |= ZSt;//  Added 6th Feb 14
00409         //  newSteps has copy of all 4 'acc' MSBs shifted into port bit positions
00410         oldSteps    ^= newSteps;  //  Any bit of stbits set to initiate a Step pulse
00411         tmp         ^= oldSteps;
00412         Steppers = tmp ^ direction_swappers; //  Output signals to stepper motor drivers, next update dros from 'clocked' bits CLOCK IDLES HIGH
00413         if(oldSteps & XSt)  dro_out.x += inc_x;       //  got clk edge for axis X
00414         if(oldSteps & YSt)  dro_out.y += inc_y;       //  got clk edge for axis Y
00415         if(oldSteps & ZSt)  dro_out.z += inc_z;       //  got clk edge for axis Z
00416         oldSteps    = newSteps;   //  Added 6th Feb 14
00417         if  (tickrun <= ticks & !new_run_pending)   {   //  End of a machine movement detected, start next move here if possible
00418                 running = false;
00419                 move_ended = true;
00420                 pir_x = 0L; //  stop all stepper motors
00421                 pir_y = 0L;
00422                 pir_z = 0L;
00423 #if defined Fourth_Axis
00424                 pir_a = 0L;
00425 #endif
00426     //          ticks = 0L; //  Simply to avoid having to think about overflow problems
00427         }       //  end of if   (tickrun <= ticks)  {
00428     }           //  end of else is   (running)   {
00429     if  (!running & new_run_pending)  { //  Start axis movement
00430         dir_bits= dir_bits_next;
00431 #if defined Fourth_Axis
00432         pir_a   = pir_a_next;
00433 #endif
00434         pir_x   = pir_x_next;
00435         pir_y   = pir_y_next;
00436         pir_z   = pir_z_next;
00437         inc_x   = inc_x_next;
00438         inc_y   = inc_y_next;
00439         inc_z   = inc_z_next;
00440         tmp     = Steppers ^ direction_swappers;  //  read output lines
00441         tmp     &= ~dir_mask;
00442         tmp     |= dir_bits;
00443         Steppers = tmp ^ direction_swappers;
00444         tickrun = ticks + ticks_next;
00445         running = true;     //  Start the new run
00446         new_run_pending = false;    //  Clear the flag which initiated this update
00447         idle = false;
00448     }       //  end of     else    {   //  Not running. Grab next data here when or if available
00449     intled = 0;         //  LED off
00450 }           //  end of interrupt handler
00451 */
00452 /*
00453 *   End of Interrupt Service Routine
00454 */
00455 /*bool    spindle_running ()  {
00456 */
00457 class   inputsreaderstuff   {
00458     private:
00459     long    ins_now;//,    ins_old,    ins_changed;
00460     public:
00461         void    init    ()  {   ins_now = 0L;}//ins_old = ins_changed = 0L;   }
00462         long    read    ()  {
00463             ins_now = 0;
00464             if  (D25pin10_EStop)    ins_now |= ESTOP;
00465             if  (D25pin11_XLim)     ins_now |= XLIM;
00466             if  (D25pin12_YLim)     ins_now |= YLIM;
00467             if  (D25pin13_ZLim)     ins_now |= ZLIM;
00468             if  (D25pin15_unkn)     ins_now |= UNKN;
00469 //            ins_changed = ins_now ^ ins_old;
00470 //            ins_old = ins_now;
00471             return  ins_now;
00472         }
00473 //        long    changed ()  {   return  ins_changed;    }
00474     }   Inputs_From_Machine;
00475 
00476 void    report_inputs   ()  {
00477     long    i = Inputs_From_Machine.read();
00478     pc.printf("Inputs: EStop %d, XLim %d, YLim %d, ", i & ESTOP ? 1:0, i & XLIM ? 1:0, i & YLIM ? 1:0);
00479     pc.printf("ZLim %d, unkn %d\r\n", i & ZLIM ? 1:0, i & UNKN ? 1:0);
00480 }
00481 
00482 int main() {
00483     long    ins, ins_old, ins_changed = 0;
00484     pc.baud(BAUD); //  comms to 'PuTTY' serial terminal via mbed usb
00485     spi.format  (8,0);          //  use 8 bit format for compatibility with Freescale KLxxZ
00486     spi.frequency(12000000);    //  12MHz, fast enough
00487     dro_out.init    ();
00488     FPGA_setup();
00489     pc.printf("\r\n*\n*\nFound Computer %s\r\n", Target);
00490 
00491     msec.attach_us(&millisec_update_ISR, 1001);
00492 
00493     Thread comlin (command_line_interpreter,    (void *)"cli"); //  Read any instructions arriving via serial port and act upon them
00494 //#if defined I2C_Enable
00495 //    Thread i2cstuff (i2c_handler, (void *)"i2c thing");
00496 //#endif
00497     ins = ins_old = Inputs_From_Machine.read    ();
00498     move_ended = true;  //  Needed to kickstart system
00499     
00500     while(1) {  //  Round Robin loop
00501         dro_out.update  ();             //  Update DRO readings if, and as often as needed
00502         ins = Inputs_From_Machine.read    ();
00503         ins_changed = ins ^ ins_old;
00504         ins_old = ins;
00505         if  (ins_changed)
00506             pc.printf("Inputs Have Changed 0x%x, read 0x%x\r\n", ins_changed, ins);
00507         osThreadYield();                //
00508     }   //  end of Round Robin loop
00509 }       //  end of int main()
00510