2019-2020 LIS2MDL Magnetometer Project

Dependencies:   X_NUCLEO_IKS01A3

Files at this revision

API Documentation at this revision

Comitter:
martlefebvre94
Date:
Mon Sep 30 22:14:36 2019 +0000
Parent:
10:0f471469279a
Commit message:
Modification of the Flash data acquisition

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 0f471469279a -r b16d3b1a061e main.cpp
--- a/main.cpp	Mon Sep 23 13:07:49 2019 +0000
+++ b/main.cpp	Mon Sep 30 22:14:36 2019 +0000
@@ -49,15 +49,18 @@
 #include "stm32l0xx_hal_flash.h"
 
 /* Defines */
-#define FS                      12.5    // Readout frequency (Hz)
+#define FS                      5.0     // Readout frequency (Hz) - /!\ Must be below 100Hz
+#define FLASH_WRITE_TIME        0.00328 // Flash write time (s)
 
-#define LIS2MDL_ODR             50.0    // Output data rate (10, 20, 50 or 100 Hz)
-#define LIS2MDL_LP              0       // Power mode (0 for high-resolution mode, 1 for low-power mode)
+#define LIS2MDL_ODR             20.0    // Output data rate (10, 20, 50 or 100 Hz)
+#define LIS2MDL_LP              1       // Power mode (0 for high-resolution mode, 1 for low-power mode)
 #define LIS2MDL_LPF             0       // Bandwidth (0 for ODR/2, 1 for ODR/4)
 #define LIS2MDL_COMP_TEMP_EN    1       // Temperature compensation (0 disabled, 1 enabled)
-#define LIS2MDL_OFF_CANC        1       // Offset cancellation (0 for no offset cancellation, 1 for offset cancellation, 2 for set pulse only at power-on)
+#define LIS2MDL_OFF_CANC        0       // Offset cancellation (0 disabled, 1 enabled, 2 for set pulse only at power-on)
 #define LIS2MDL_DATA_SIZE       12      // Number of bytes for LIS2MDL magnetometer data
 
+#define TS                      (1/FS)-((LIS2MDL_DATA_SIZE/4)*FLASH_WRITE_TIME)
+
 /* Functions definition */
 bool acquisition_task(bool verbose);
 void read_task();
@@ -173,29 +176,15 @@
 bool acquisition_task(bool verbose)
 {
     uint32_t Flash_addr = FLASH_BANK2_BASE;
-    uint32_t buffer_size = FLASH_PAGE_SIZE/4;
-    uint32_t data_ind = 0;
-    
     int32_t m_axes[3];
-    uint32_t data_buffer[buffer_size];
 
     while (Flash_addr <= FLASH_BANK2_END-FLASH_PAGE_SIZE+1) {
         // Read magnetometer data
         magnetometer->get_m_axes(m_axes);
         
-        // Save data to Flash memory
-        for (int i=0; i<3; i++) {
-            // Write page in Flash memory
-            if (data_ind >= buffer_size) {
-                write_flash(Flash_addr, &data_buffer[0], buffer_size, false);
-                Flash_addr += FLASH_PAGE_SIZE;
-                data_ind = 0;
-            }
-            
-            // Write data to buffer
-            data_buffer[data_ind] = (uint32_t) m_axes[i];
-            data_ind++;
-        }
+        // Write page in Flash memory
+        write_flash(Flash_addr, (uint32_t*) &m_axes[0], 3, false);
+        Flash_addr += LIS2MDL_DATA_SIZE;
         
         // Print data in terminal
         if (verbose) {
@@ -203,13 +192,11 @@
         }
         
         // Wait for acquisition period
-        wait(1/FS);
+        wait(TS);
         
         // Stop saving data when button is pushed
         if (button1_pressed) {
             button1_pressed = false;
-            // Save remaining data to Flash memory
-            write_flash(Flash_addr, &data_buffer[0], data_ind, false);
             printf("Data acquisition stopped\r\n");
             printf("Press 'R' to read the data\r\n");
             return false;