Tapio Valli / Mbed 2 deprecated LoadCell_STM32_SDRAM_v1

Dependencies:   BSP_DISCO_F746NG DISCO-F746NG_SDRAMBlockDevice LCD_DISCO_F746NG mbed

Fork of LoadCell_STM32 by Tapio Valli

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // LoadCell_STM32_SDRAM main
00002 // (C) Tapio Valli 2018-02-24
00003 
00004 #include "mbed.h"
00005 #include "mbed_stats.h"
00006 
00007 #include "LCD_DISCO_F746NG.h"
00008 #include "SDRAM_DISCO_F746NG_BlockDevice.h"
00009 #include "FATFileSystem.h"
00010 
00011 // Physical block device, can be any device that supports the BlockDevice API
00012 #define BLOCK_SIZE 512
00013 
00014 // SDRAM Block Device
00015 SDRAM_DISCO_F746NG_BlockDevice bd;
00016 
00017 // File system declaration
00018 FATFileSystem fs("fs");
00019 
00020 // define the Serial object
00021 Serial pc(USBTX, USBRX);
00022 LCD_DISCO_F746NG lcd;
00023 
00024 typedef struct {
00025   uint16_t *array;
00026   size_t used;
00027   size_t size;
00028 } Array;
00029 
00030 // Prototypes
00031 
00032 extern void initArray(Array *a, size_t initialSize);
00033 extern void insertArray(Array *a, uint32_t newsize);
00034 extern void freeArray(Array *a);
00035 
00036 extern size_t string_parser(char *input, char ***word_array);
00037 extern void PlotData(uint32_t XCoordinate,uint32_t YCoordinate);
00038 extern void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor);
00039 extern void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlingMode);
00040 extern void CountDown(uint32_t millisecs);
00041 extern void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us);
00042 extern void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize);
00043 
00044 extern void BlockDeviceErase();
00045 extern void MountFS();
00046 extern void WriteData2FS(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber);
00047 extern void ReadFile2Data(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber);
00048 extern void DirListing(void);
00049 
00050 int main() {
00051     
00052     FMC_SDRAM_CommandTypeDef SDRAMCommandStructure;
00053     
00054     // Initialize SDRAM
00055     bd.init();
00056     
00057     // Issue self-refresh command to SDRAM device
00058     SDRAMCommandStructure.CommandMode            = FMC_SDRAM_CMD_SELFREFRESH_MODE;
00059     SDRAMCommandStructure.CommandTarget          = FMC_SDRAM_CMD_TARGET_BANK2;
00060     SDRAMCommandStructure.AutoRefreshNumber      = 1;
00061     SDRAMCommandStructure.ModeRegisterDefinition = 0;
00062     if (bd.Sendcmd(&SDRAMCommandStructure) != HAL_OK) 
00063     {
00064       error("BSP_SDRAM_Sendcmd FAILED\n");
00065     }
00066     
00067     // Erase in any case for start
00068     BlockDeviceErase();
00069 
00070     // Try to mount the filesystem
00071     MountFS();
00072     
00073     char CmdBuffer[30];
00074     char Arg[30]=" ";
00075     char Cmd[30]=" ";
00076     size_t n=0;
00077 
00078     uint32_t    NoOfPoints=19200;  
00079     uint32_t    AvgSize=10;
00080     uint32_t    Period_us=100;
00081     uint32_t    Count_ms=1000;
00082     uint32_t    MeasNumber=1;
00083     int err;
00084     
00085     mbed_stats_heap_t heap_stats;
00086         
00087     // Clear screen, set it up 
00088     InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
00089         
00090     //Initialize data storage
00091     Array Data;
00092     initArray(&Data,NoOfPoints);  // initially 19200 elements
00093     
00094     InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
00095     LCDWrite(5,"Ready.",CENTER_MODE);
00096     
00097     while (!((strcmp(Cmd,"quit")==0)&&(n==1))) {
00098         
00099         // Print Ready and current settings        
00100         pc.printf(
00101         "Ready. Settings are Points=%u, Avg=%u, Period_us=%u, "
00102         "Count_ms=%u. Sampling will take apprx. %.3f secs\r\n",
00103         NoOfPoints,AvgSize,Period_us,Count_ms,
00104         (float)(NoOfPoints*Period_us/1000000.0));
00105                    
00106         mbed_stats_heap_get(&heap_stats);
00107         pc.printf("Current heap = %lu Max heap = %lu\r\n", 
00108         heap_stats.current_size,heap_stats.max_size);
00109                                                                            
00110         pc.gets(CmdBuffer,30);
00111         pc.printf("I got %s \r\n", CmdBuffer);
00112         strcpy(Cmd," ");
00113         strcpy(Arg," ");
00114                 
00115         // Parse command and possible numeric arg
00116         char s[] = "Initial string";
00117         char ** word_array=NULL;
00118         
00119         strcpy(s,CmdBuffer);
00120         n=string_parser(s,&word_array);
00121 
00122         for ( size_t i=0;i<n;i++) {
00123             if (i==0) {strcpy(Cmd,word_array[i]);}
00124             if (i==1) {strcpy(Arg,word_array[i]);}
00125             if (i>1) {pc.printf("Wrong number of arguments \r\n");}
00126         }
00127         
00128         pc.printf("Cmd = %s Arg = %s \r\n",Cmd,Arg);
00129         for ( size_t i=0;i<n;i++) free( word_array[i] );
00130         free(word_array);
00131     
00132         // Branch based on command        
00133         // meas: Sample and plot a data set
00134         if((strcmp(Cmd,"meas")==0)&&(n==1)) {
00135                         
00136             // Countdown
00137             CountDown(Count_ms);
00138                                  
00139             // Sample & plot data one time
00140             SamplePoints(&Data,NoOfPoints,Period_us);
00141             AvgAndPlotPoints(&Data,NoOfPoints,AvgSize);
00142             WriteData2FS(&Data,NoOfPoints,MeasNumber);
00143             MeasNumber++;
00144             
00145             //-- implement http server with fs as storage
00146             //-- connect and pull data from there
00147             //-- implement disk init as functions & commands
00148         }
00149         
00150         // setpoints: Adjust sampled points                          
00151         else if((strcmp(Cmd,"setpoints")==0)&&(n==2)) {
00152             // Allocate more or less data space
00153             NoOfPoints=(uint32_t) strtol(Arg,NULL,10);
00154             pc.printf("Old Data size is %u New NoOfPOints = %u \r\n",Data.size,NoOfPoints);
00155             insertArray(&Data,NoOfPoints);
00156             pc.printf("New Array size is %u \r\n",Data.size);
00157         }
00158             
00159         // setavg: Adjust average amount in samples            
00160         else if((strcmp(Cmd,"setavg")==0)&&(n==2)) {
00161             AvgSize=(uint32_t) strtol(Arg,NULL,10);
00162         }
00163             
00164         // setperiod: Adjust sample period in us
00165         else if((strcmp(Cmd,"setperiod")==0)&&(n==2)) {
00166             Period_us=(uint32_t) strtol(Arg,NULL,10);
00167         }
00168         
00169         // setcount: Adjust countdown period in ms
00170         else if((strcmp(Cmd,"setcount")==0)&&(n==2)) {
00171             Count_ms=(uint32_t) strtol(Arg,NULL,10);
00172         }
00173         
00174         // dir: List files
00175         else if((strcmp(Cmd,"dir")==0)&&(n==1)) {
00176             DirListing();
00177         }
00178         
00179         // quit: Exit on next while
00180         else if((strcmp(Cmd,"quit")==0)&&(n==1)) {
00181             // Do nothing yet
00182         }
00183         else {
00184             pc.printf("Wrong command or argument \r\n");
00185         }
00186     }
00187     
00188     // Free memory after quit
00189     freeArray(&Data);
00190     
00191     // Tidy up filesystem
00192     pc.printf("Unmounting filesystem.");
00193     fflush(stdout);
00194     err = fs.unmount();
00195     pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00196     if (err < 0) {
00197         error("error: %s (%d)\n", strerror(-err), err);
00198     }
00199 
00200     // Print informative messages
00201     InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
00202     LCDWrite(5,"Stop.",CENTER_MODE);    
00203     pc.printf("Exit. Data freed. Stop. \r\n");
00204     
00205     // Infinite loop
00206     while(1) 
00207     {
00208         // Do nothing
00209     }
00210 }