C Library for mbedWSE project based single board computer for hardware peripherals

Fork of mbedWSEsbc by Joseph Bradshaw

Revision:
4:1aa4a75f6885
Parent:
1:85cd6d500385
Child:
7:740d112d934d
Child:
11:acf3fcc0d085
--- a/mbedWSEsbc.h	Fri Sep 12 16:25:12 2014 +0000
+++ b/mbedWSEsbc.h	Thu Sep 25 13:36:45 2014 +0000
@@ -1,7 +1,12 @@
-// C Library for the WSE-PROJ-SBC
-// J Bradshaw
-// 20140912
+/* C Library for the WSE-PROJ-SBC
+ J Bradshaw
+ 20140912
+ 20140918 J Bradshaw - Found CS mistake in Encoder routines
+    Added comments in Init function, encoder functions
+*/
 
+
+// LS7366 ENCODER IC DEFINITIONS
 //=============================================================================
 // Four commands for the Instruction Register (B7,B6) - LS7366
 //=============================================================================
@@ -25,13 +30,13 @@
 // Set-up hardwired IO
 SPI spi_max1270(p5, p6, p7);
 SPI spi(p5, p6, p7);
-DigitalOut max1270_cs(p8);  //MAX1270 ADC CS
-DigitalOut max522_cs(p11);  //MAX522 DAC CS
+DigitalOut max1270_cs(p8);  //CS for MAX1270 ADC (U3)
+DigitalOut max522_cs(p11);  //CS for MAX522 DAC (U5)
 
-DigitalOut ls7166_cs1(p19);    //CS for LS7366
-DigitalOut ls7166_cs2(p20);    //CS for LS7366
+DigitalOut ls7166_cs1(p19); //CS for LS7366-1 (U8)
+DigitalOut ls7166_cs2(p20); //CS for LS7366-2 (U9)
 
-DigitalOut mot1_ph1(p21);
+DigitalOut mot1_ph1(p21);       
 DigitalOut mot1_ph2(p22);
 PwmOut mot_en1(p23);
 
@@ -228,6 +233,7 @@
     }    
     wait_us(1);
     LS7366_cmd(WR,MDR0);// Write to the MDR0 register
+    wait_us(1);
     spi.write(0x03); // X4 quadrature count mode
     if(chan_num!=2){
         ls7166_cs1 = 1;    
@@ -237,84 +243,83 @@
     }
 }
 
-void LS7366_reset_counter(int chan_num){
+void LS7366_reset_counter(int chan_num){    
+    spi.format(8, 0);           // set up SPI for 8 data bits, mode 0
+    spi.frequency(2000000);     // 2MHz SPI clock
     
-    spi.format(8, 0);
-    spi.frequency(2000000);
-    
-    if(chan_num!=2){
+    if(chan_num!=2){            // activate chip select
         ls7166_cs1 = 0;    
     }
     else{
         ls7166_cs2 = 0;    
     }    
-    wait_us(1);
-    LS7366_cmd(CLR,CNTR);//Clear the counter register
-    if(chan_num!=2){
+    wait_us(1);                 // short delay
+    LS7366_cmd(CLR,CNTR);       // Clear the counter register
+    if(chan_num!=2){            // de-activate chip select
         ls7166_cs1 = 1;    
     }            
     else{
-        ls7166_cs2 = 1;    
+        ls7166_cs2 = 1;
     }
-    wait_us(1);
+    wait_us(1);                 // short delay
     
-    if(chan_num!=2){
+    if(chan_num!=2){            // activate chip select
         ls7166_cs1 = 0;    
     }            
     else{
         ls7166_cs2 = 0;    
     }        
-    wait_us(1);
-    LS7366_cmd(LOAD,CNTR);//
-    if(chan_num!=2){
+    wait_us(1);                 // short delay
+    LS7366_cmd(LOAD,CNTR);      // load counter reg
+    if(chan_num!=2){            // de-activate chip select
         ls7166_cs1 = 1;    
     }            
     else{
-        ls7166_cs1 = 1;    
+        ls7166_cs2 = 1;    
     }
 }
 
-void LS7366_write_DTR(int chan_num,long enc_value){
-    union bytes
+void LS7366_write_DTR(int chan_num, long enc_value){
+    union bytes                // Union to speed up byte writes
     {
         char byte_enc[4];
         long long_enc;
     }counter;
     
-    spi.format(8, 0);
-    spi.frequency(2000000);
+    spi.format(8, 0);           // set up SPI for 8 data bits, mode 0
+    spi.frequency(2000000);     // 2MHz SPI clock
     
-    counter.long_enc = enc_value;
+    counter.long_enc = enc_value; // pass enc_value to Union
     
-    if(chan_num!=2){
+    if(chan_num!=2){              // activate chip select
         ls7166_cs1 = 0;    
     }            
     else{
         ls7166_cs2 = 0;    
     }   
-    wait_us(1);
-    LS7366_cmd(WR,DTR);//
-    spi.write(counter.byte_enc[3]);
+    wait_us(1);                 // short delay
+    LS7366_cmd(WR,DTR);         // Write to the Data Transfer Register
+    spi.write(counter.byte_enc[3]); // Write the 32-bit encoder value
     spi.write(counter.byte_enc[2]);
     spi.write(counter.byte_enc[1]);
     spi.write(counter.byte_enc[0]);
-    if(chan_num!=2){
+    if(chan_num!=2){            // de-activate the chip select
         ls7166_cs1 = 1;    
     }            
     else{
         ls7166_cs2 = 1;    
     }     
     
-    wait_us(1);
-    if(chan_num!=2){
+    wait_us(1);                 // short delay
+    if(chan_num!=2){            // activate chip select
         ls7166_cs1 = 0;    
     }            
     else{
         ls7166_cs2 = 0;    
     }
-    wait_us(1);
-    LS7366_cmd(LOAD,CNTR);//
-    if(chan_num!=2){
+    wait_us(1);                 // short delay
+    LS7366_cmd(LOAD,CNTR);      // load command to the counter register from DTR
+    if(chan_num!=2){            // de-activate chip select
         ls7166_cs1 = 1;    
     }            
     else{
@@ -342,20 +347,22 @@
 }
 
 void mbedWSEsbcInit(unsigned long pcbaud){
-    led4 = 0;         
-    max1270_cs = 1;
+    led1 = 0;           //Initialize all LEDs as off
+    led2 = 0;
+    led3 = 0;
+    led4 = 0;
+    max1270_cs = 1;     //Initialize all chip selects as off
     max522_cs = 1;
     ls7166_cs1 = 1;
     ls7166_cs2 = 1;
     
     wait(.2);   //delay at beginning for voltage settle purposes
     
-    mot_en1.period_us(50);   
-    //pc.baud(921600); //Set up serial port baud rate
+    mot_en1.period_us(50);   //20KHz for DC motor control PWM
     pc.baud(pcbaud); //Set up serial port baud rate
+    pc.printf("\r\n");
     xbee.baud(9600);
     
-    spi.frequency(5000000);
     LS7366_reset_counter(1);
     LS7366_quad_mode_x4(1);       
     LS7366_write_DTR(1,0);