SPI slave program to enable communication between the FPGA and the STM32L432 board.

Dependencies:   mbed

Revision:
9:9ed9dffd602a
Parent:
8:e87027349167
Child:
10:5b96211275d4
--- a/main.cpp	Wed Feb 27 23:35:51 2019 +0000
+++ b/main.cpp	Thu Mar 07 01:16:48 2019 +0000
@@ -5,7 +5,7 @@
 #include "Quaternions.h"
 #include "DMA_SPI.h"
 
-DigitalOut myled(LED1);
+//DigitalOut myled(LED1);
 Serial pc(USBTX, USBRX); 
 
 int masterRx = 0;
@@ -51,10 +51,19 @@
     void calibrateOffset();
 
 
-float xx;
-float yy;
-float zz;
-
+int stateRXNE = 0;
+int stateTXE = 0;
+int stateBSY = 0;
+int DMA1_CH2_Transfer_Complete_Flag = 0;
+int DMA1_CH3_Transfer_Complete_Flag = 0;
+int DMA1_CH2_Transfer_Error_Flag = 0;
+int DMA1_CH3_Transfer_Error_Flag = 0;
+int stateDMAch2interrupt = 0;
+int stateDMAch3interrupt = 0;
+int flag = 0;
+int flag2 = 0;
+int rx_data = 0;
+int16_t rxx;
 
 
 /*
@@ -71,7 +80,7 @@
 //------------------Printing-All-values----------------------//
 
 //-------------Testing-Variables-Remove-Later----------------//
-int blinkCounter = 0;
+//int blinkCounter = 0;
 //-------------Testing-Variables-Remove-Later----------------//
 
 int main() {
@@ -89,9 +98,9 @@
     IDarray[10] = 19;
     IDarray[11] = 18;
     
-    init_spi1();
+    //init_spi1();
     t.start();
-    
+    SPI_DMA_init();
     gyroQ.w = 1;
     gyroQ.x = 0.0001;
     gyroQ.y = 0.0001;
@@ -102,65 +111,117 @@
     CF.y = 0.0001;
     CF.z = 0.0001;
     
-    
     gyroQ = normaliseQuaternion(gyroQ);
     CF = normaliseQuaternion(CF);
     
     vertical.x = 0.0f;
     vertical.y = 0.0f;
     vertical.z = 1.0f;
+
+   
+    data_to_transmit[0] = 1;
+    data_to_transmit[1] = 2;
+    data_to_transmit[2] = 3;
+    data_to_transmit[3] = 4;
+    data_to_transmit[4] = 5;
+    data_to_transmit[5] = 6;
+    data_to_transmit[6] = 7;
+    data_to_transmit[7] = 8;
+    data_to_transmit[8] = 9;
+    data_to_transmit[9] = 10;
+    data_to_transmit[10] = 11;
+    data_to_transmit[11] = 16;
+    data_to_transmit[12] = 13;
     
-    pc.printf("Begin \n\r");
-    
+
     while(1) {
-        if(i == 1) {
-        for(int x = 1; x < 128; x *= 2) { 
-        slaveRx = transfer_spi_slave(x);
-        //pc.putc(slaveRx); 
-        pc.printf("%d \n\r",slaveRx); 
+
+    
+
+     stateRXNE = SPI1->SR&0x01;
+     stateTXE = SPI1->SR&0x02;
+     stateBSY = SPI1->SR&(1u<<7);
+     
+    DMA1_CH2_Transfer_Complete_Flag = DMA1->ISR&(1u<<5);
+    DMA1_CH3_Transfer_Complete_Flag = DMA1->ISR&(1u<<9);
+    DMA1_CH2_Transfer_Error_Flag = DMA1->ISR&(1u<<7);
+    DMA1_CH3_Transfer_Error_Flag = DMA1->ISR&(1u<<11);
+    
+     pc.printf("RXNE: %d,       TXE: %d,        BSY: %d,        CH2 Transfer Complete: %d,      CH2 Transfer Complete: %d,       CH2 Transfer Error: %d,       CH3 Transfer Error: %d\n\r",stateRXNE, stateTXE, stateBSY, DMA1_CH2_Transfer_Complete_Flag, DMA1_CH3_Transfer_Complete_Flag, DMA1_CH2_Transfer_Error_Flag, DMA1_CH3_Transfer_Error_Flag);
+     
+     if(DMA1->ISR&(1u<<5)) {                                                    //Check whether data read transfer is complete
+        for(int x = 0; x <= 11; x++) {
+            data_to_transmit[x] = x;
         }
-        for(int x = 128; x > 1; x /= 2) { 
-        slaveRx = transfer_spi_slave(x);
-        //pc.putc(slaveRx);
-        pc.printf("%d \n\r",slaveRx);  
-        } 
-        }//if(i == 1) 
-        
-        
+        CLEAR_DMA1_CH2_IFCR_GFLAG();                                            //Clear global channel interrupt flag for channel 2
+     }
+     
+     if(DMA1->ISR&(1u<<9)) {                                                    //Check whteher data transmit transfer is complete  
+        //Read data from the array that stores received data
+        for(int x = 0; x <= 11; x++) {
+            IMUarray[x] = received_data[x];
+        }
+        CLEAR_DMA1_CH3_IFCR_GFLAG();                                            //Clear global channel interrupt flag for channel 3
+     }
+     /*
+     if(DMA1->ISR&(1u<<11) && flag == 0) {
+        if(SPI1->SR&(1u<<7) == 0) {                                                  //Check whether SPI is busy before disabling      
+            SPI1->CR1 &= ~SPI_CR1_SPE;                                              //Disable the SPI
+            DMA1->IFCR |= (1u << (4*(C3S - 1)));
+            DMA1_Channel3->CCR |= (0x01<<CCR_EN); 
+            SPI1->CR1 |= SPI_CR1_SPE;                                               //Enable SPI
+            flag = 1;
+        }
+     }
+     
+    if(DMA1->ISR&(1u<<7) && flag2 == 0) {
+        DMA1->IFCR |= (15u << (4*(C2S - 1)));
+        flag2 = 1;
+     }
+     
+    if(DMA1->ISR&(1u<<9) && flag == 0) {
+            DMA1->IFCR |= (1u << (4*(C3S - 1)));
         
-//------------------------------------------------------------------------------         
-        if(i == 0) { 
-        slaveRx = transfer_spi_slave(10);
-        countx++;
-        if(countx == 1) {
-            k = slaveRx; 
-            k &= ~(255 << 8);
-            k = k << 8;
-           // if(k == 19) {
-                zgHigher = k; 
-                //zgHigher = zgHigher << 8;
-            //}//if(k == 19)
-        }//if(count == 11)
-        
-        
-        else if(countx == 2) {
-            k = slaveRx;
-            k &= ~(255 << 8); 
-            //k = k << 8;
-            //if(k == 18) {
-                zgLower = k;
-                //zgLower = zgLower << 8;
-                zGyro = zgHigher + zgLower;
-                pc.printf("%+d \n\r",zGyro);
-            //}//if(k == 19)
-                countx = 0;
-        }//else if(count == 12)
-        
-        }//if(i == 0) 
+     }
+     
+    if(DMA1->ISR&(1u<<5) && flag2 == 0) {
+        DMA1->IFCR |= (15u << (4*(C2S - 1)));
+        for(int x = 0; x <= 10; x++) {
+            rxx = received_data[x];
+        }
+     }
+
+*/
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 //------------------------------------------------------------------------------ 
-        
+     /*   
         if(i == 2) {
-            slaveRx = transfer_spi_slave(10);                                   //get IMU data
+            //slaveRx = transfer_spi_slave(10);                                   //get IMU data
             
             
             id = slaveRx;                                                       //Save sample to id for id extraction
@@ -178,7 +239,6 @@
                     //reset idx and dataCount
                     dataCount = 0;
                     idx = 0;
-                  //ProcessAndPrint();
                   //calibrateOffset(); 
                   //IMU0.concatenateData(IMUarray);
                   Datt = IMU0.CalculateQCFAngles(IMUarray);
@@ -193,13 +253,13 @@
              else {                          
                                                                                 //-----Code-Used-For-Testing-----//
                 //pc.printf("Failed at: %d \n\r", dataCount);                   //Print an error if there is one
-                if(blinkCounter == 10) {                                        //Slow the blinking down to make it visible if there are errors
-                   myled = !myled;                                              //Change state of the LED if error occurs
-                   blinkCounter = 0;                                            //Reset Blink counter        
-                   }
-                else {
-                    blinkCounter++;   
-                }                                                           
+              //  if(blinkCounter == 10) {                                        //Slow the blinking down to make it visible if there are errors
+               //    myled = !myled;                                              //Change state of the LED if error occurs
+               //    blinkCounter = 0;                                            //Reset Blink counter        
+               //    }
+               // else {
+                //    blinkCounter++;   
+                //}                                                           
                                                                                 //-----Code-Used-For-Testing-----//
                                                                                 
                     dataCount = 0;                                              //ID sequence is worng so reset the counter
@@ -207,7 +267,7 @@
              }                                                                  
        }//if(i == 2)
 
-
+    */
        
     }
 }
@@ -215,133 +275,10 @@
 
 
 
-/*
-    -The purpose of this function
-*/
-void ProcessAndPrint() {
-    int16_t MSB = 0;                                    //Store Most Significant Byte of data piece in this variable for processing
-    int16_t LSB = 0;                                    //Store Least Significant Byte of data piece in this variable for processing
-    char    arrPointer = 0;                                //Array Pointer
-    
-//-----------Concatentated-Data-Pieces------------------------------------------
-    int16_t gyroX = 0;
-    int16_t gyroY = 0;
-    int16_t gyroZ = 0;
-    
-    int16_t accelX = 0;
-    int16_t accelY = 0;
-    int16_t accelZ = 0;
-    
-    float   faccelX = 0.0f;
-    float   faccelY = 0.0f;
-    float   faccelZ = 0.0f;
-    
-    float   fgyroX = 0.0f;
-    float   fgyroY = 0.0f;
-    float   fgyroZ = 0.0f;
-
-//-----------Concatentated-Data-Pieces------------------------------------------
-    for(char x = 0; x <= 5; x++) {
-        MSB = IMUarray[arrPointer];                                             //Odd data pieces are MSBs
-        MSB &= ~(255<<8);                                                       //Mask the MSB bits as they are not part of data
-        MSB = MSB << 8;                                                         //Shift the Value as its the MSB of the data piece
-        arrPointer++;                                                           //Increment array pointer
-        LSB = IMUarray[arrPointer];                                             //Even data pieces are LSBs
-        LSB &= ~(255 << 8);                                                     //Mask the MSB bits as they are not part of data
-        arrPointer++;                                                           //Increment array pointer
-        
-        switch(x) {
-            case 0:
-                accelX = (MSB + LSB) + OffsetXA;                                             //Combine Accelerometer x-axis data
-                faccelX = (float)accelX * 0.00006103515625f;                    //Multiply the acceleration by sensitivity scale factor to get it into g  1/16,384
-            break;
-            case 1:
-                accelY = (MSB + LSB) + OffsetYA;                                             //Combine Accelerometer y-axis data
-                faccelY = (float)accelY * 0.00006103515625f;                    //Multiply the acceleration by sensitivity scale factor to get it into g  1/16,384
-            break;
-            case 2:
-                accelZ = (MSB + LSB) + OffsetZA;                                              //Combine Accelerometer z-axis data
-                faccelZ = (float)accelZ * 0.00006103515625f;                    //Multiply the acceleration by sensitivity scale factor to get it into g  1/16,384
-            break;
-            case 3:
-                gyroX = (MSB + LSB) + OffsetX;                                              //Combine Gyroscope x-axis data
-                fgyroX = (float)gyroX * SSF;    //Multiply the sample by sensitivity scale factor to get it into degress per second  1/16.4
-                
-            break;
-            case 4:
-                gyroY = (MSB + LSB) + OffsetY;                                              //Combine Gyroscope y-axis data
-                fgyroY = (float)gyroY * SSF;    //Multiply the sample by sensitivity scale factor to get it into degress per second   1/16.4
-
-            break;
-            case 5:
-                gyroZ = (MSB + LSB) + OffsetZ;                                              //Combine Gyroscope z-axis data
-                fgyroZ = (float)gyroZ * SSF;    //Multiply the sample by sensitivity scale factor to get it into degress per second   1/16.4
-            break;
-            default:
-            break; 
-        }//switch(x) 
-    }//for(char x = 0; x <= 5; x++)
-       
-//Quaternion-Gyro-Integration--------------------------------------------------------------    
-    //Get the Gyro and Accel values--------------------------------------------------------    
-        //Convert these vales to radians per second and store them in the vector
-        GyroVals.x = fgyroX * 0.0174533f;
-        GyroVals.y = fgyroY * 0.0174533f;
-        GyroVals.z =  fgyroZ * 0.0174533f;
-        
-        intGyro.x = (fgyroX * dTime);
-        intGyro.y = (fgyroY * dTime);
-        intGyro.z = (fgyroZ * dTime);
-        
-        AccelVals.x = faccelX;
-        AccelVals.y = faccelY;
-        AccelVals.z = faccelZ;
-    //Get the Gyro and Accel values--------------------------------------------------------  
-    
-        //CF = updateQuaternion(CF, GyroVals, dTime); 
-        //CF = normaliseQuaternion(CF);
-        //Eangles = eulerA(CF);
-//Quaternion-Gyro-Integration--------------------------------------------------------------
-
-
-//Angle-Calculations-Based-on-Accalerometer------------------------------------------------
-    //https://www.dfrobot.com/wiki/index.php/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
-    
-    accelTilt.x = (atan2f(AccelVals.y, AccelVals.z) * 57.29577951f); 
-    accelTilt.y = (atan2f((-AccelVals.x), sqrt(pow(AccelVals.y, 2) + pow(AccelVals.z, 2) )) * 57.29577951f);
-    //pc.printf("%+6f,     %+6f       ", accelTilt.x, accelTilt.y); 
-    //Canntot calculate accel tilt for Z. Gyro date in combination with magnetometer have to be used to obtain reliable z-axis tilt.
-//Angle-Calculations-Based-on-Accalerometer------------------------------------------------ 
-    //pc.printf("%+6f,     %+6f,     %+6f     ", CF.x, CF.y, CF.z); 
-    
-    
-//Complementary-Filter---------------------------------------------------------------------
-    //CF.x = 0.98f*(Eangles.x) + 0.02f*accelTilt.x;
-    //CF.y = 0.98f*(Eangles.y) + 0.02f*accelTilt.y;
-    //CF.z = 0.98f*(Eangles.z) + 0.02f*accelTilt.z;
-    
-    CF.x = 0.98f*(CF.x + intGyro.x) + 0.02f*accelTilt.x;
-    CF.y = 0.98f*(CF.y + intGyro.y) + 0.02f*accelTilt.y;
-    CF.z = 0.98f*(CF.z + intGyro.z) + 0.02f*accelTilt.z;
-//Complementary-Filter---------------------------------------------------------------------        
-
-    //Add the correction to gyro readings and update the quaternion------------------------     
-        //pc.printf("%+6f,     %+6f,    %+6f      %+6f\n\r ", xx, yy, zz, dTime); 
-        //pc.printf("%+6f,     %+6f,     %+6f,    %+6f,    %+6f,    %+6f,    %+6f\n\r ", gyroQ.w, gyroQ.x, gyroQ.y, gyroQ.z, CF.x, CF.y, Eangles.z); 
-        //pc.printf("%+6f,     %+6f,     %+6f     \n\r", CF.x, CF.y, CF.z); 
-        //CF.x *= 0.0174533f;
-        //CF.y *= 0.0174533f;
-       // CF.z *= 0.0174533f;
-       //CF = euler2Quaternion(CF);
-       //CF = normaliseQuaternion(CF);
-        //pc.printf("%+6f,     %+6f,     %+6f\n\r ",Eangles.x, Eangles.y, Eangles.z); 
-        pc.printf("Accel X: %+6f,    Accel Y: %+6f,    Accel Z: %+6f,    Gyro X: %+6f,    Gyro Y: %+6f,    Gyro Z: %+6f\n\r", faccelX, faccelY, faccelZ, fgyroX, fgyroY, fgyroZ);
-        //pc.printf("Accel X: %+6f,    Accel Y: %+6f,    Accel Z: %+6f,    Gyro X: %+6d,    Gyro Y: %+6d,    Gyro Z: %+6d\n\r", faccelX, faccelY, faccelZ, gyroX, gyroY, gyroZ);
-   
-}//void ProcessAndPrint()
 
 
 
+//---------------------------------------------------------------------------OFFSET-CALIBRATION--------------------------------------------------------------------
 void calibrateOffset()  {
         
     int16_t MSB = 0;                                    //Store Most Significant Byte of data piece in this variable for processing
@@ -460,7 +397,7 @@
     sampleCounter++;
 //Print Messages-------------------------------------------------------------------------------     
 }    
-
+//---------------------------------------------------------------------------OFFSET-CALIBRATION--------------------------------------------------------------------