LoadCell_STM32_SDRAM_v1

Dependencies:   BSP_DISCO_F746NG DISCO-F746NG_SDRAMBlockDevice LCD_DISCO_F746NG mbed

Fork of LoadCell_STM32 by Tapio Valli

Committer:
tapiov
Date:
Thu Mar 08 20:17:32 2018 +0000
Revision:
7:30df32ea2d03
Parent:
6:641b171407c9
First commit to publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tapiov 6:641b171407c9 1 // LoadCell_STM32_SDRAM main
tapiov 4:5fe86291ba03 2 // (C) Tapio Valli 2018-02-24
tapiov 0:b3e6088c873f 3
tapiov 1:c3c61d08f31b 4 #include "mbed.h"
tapiov 2:386f65563144 5 #include "mbed_stats.h"
tapiov 1:c3c61d08f31b 6
tapiov 1:c3c61d08f31b 7 #include "LCD_DISCO_F746NG.h"
tapiov 6:641b171407c9 8 #include "SDRAM_DISCO_F746NG_BlockDevice.h"
tapiov 6:641b171407c9 9 #include "FATFileSystem.h"
tapiov 0:b3e6088c873f 10
tapiov 6:641b171407c9 11 // Physical block device, can be any device that supports the BlockDevice API
tapiov 6:641b171407c9 12 #define BLOCK_SIZE 512
tapiov 0:b3e6088c873f 13
tapiov 6:641b171407c9 14 // SDRAM Block Device
tapiov 6:641b171407c9 15 SDRAM_DISCO_F746NG_BlockDevice bd;
tapiov 6:641b171407c9 16
tapiov 6:641b171407c9 17 // File system declaration
tapiov 6:641b171407c9 18 FATFileSystem fs("fs");
tapiov 0:b3e6088c873f 19
tapiov 1:c3c61d08f31b 20 // define the Serial object
tapiov 6:641b171407c9 21 Serial pc(USBTX, USBRX);
tapiov 6:641b171407c9 22 LCD_DISCO_F746NG lcd;
tapiov 6:641b171407c9 23
tapiov 6:641b171407c9 24 typedef struct {
tapiov 6:641b171407c9 25 uint16_t *array;
tapiov 6:641b171407c9 26 size_t used;
tapiov 6:641b171407c9 27 size_t size;
tapiov 6:641b171407c9 28 } Array;
tapiov 6:641b171407c9 29
tapiov 6:641b171407c9 30 // Prototypes
tapiov 6:641b171407c9 31
tapiov 6:641b171407c9 32 extern void initArray(Array *a, size_t initialSize);
tapiov 6:641b171407c9 33 extern void insertArray(Array *a, uint32_t newsize);
tapiov 6:641b171407c9 34 extern void freeArray(Array *a);
tapiov 6:641b171407c9 35
tapiov 6:641b171407c9 36 extern size_t string_parser(char *input, char ***word_array);
tapiov 6:641b171407c9 37 extern void PlotData(uint32_t XCoordinate,uint32_t YCoordinate);
tapiov 6:641b171407c9 38 extern void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor);
tapiov 6:641b171407c9 39 extern void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlingMode);
tapiov 6:641b171407c9 40 extern void CountDown(uint32_t millisecs);
tapiov 6:641b171407c9 41 extern void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us);
tapiov 6:641b171407c9 42 extern void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize);
tapiov 6:641b171407c9 43
tapiov 6:641b171407c9 44 extern void BlockDeviceErase();
tapiov 6:641b171407c9 45 extern void MountFS();
tapiov 6:641b171407c9 46 extern void WriteData2FS(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber);
tapiov 6:641b171407c9 47 extern void ReadFile2Data(Array *Data,uint32_t NoOfPoints,uint32_t FileNumber);
tapiov 6:641b171407c9 48 extern void DirListing(void);
tapiov 0:b3e6088c873f 49
tapiov 0:b3e6088c873f 50 int main() {
tapiov 6:641b171407c9 51
tapiov 6:641b171407c9 52 FMC_SDRAM_CommandTypeDef SDRAMCommandStructure;
tapiov 6:641b171407c9 53
tapiov 6:641b171407c9 54 // Initialize SDRAM
tapiov 6:641b171407c9 55 bd.init();
tapiov 6:641b171407c9 56
tapiov 6:641b171407c9 57 // Issue self-refresh command to SDRAM device
tapiov 6:641b171407c9 58 SDRAMCommandStructure.CommandMode = FMC_SDRAM_CMD_SELFREFRESH_MODE;
tapiov 6:641b171407c9 59 SDRAMCommandStructure.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
tapiov 6:641b171407c9 60 SDRAMCommandStructure.AutoRefreshNumber = 1;
tapiov 6:641b171407c9 61 SDRAMCommandStructure.ModeRegisterDefinition = 0;
tapiov 6:641b171407c9 62 if (bd.Sendcmd(&SDRAMCommandStructure) != HAL_OK)
tapiov 6:641b171407c9 63 {
tapiov 6:641b171407c9 64 error("BSP_SDRAM_Sendcmd FAILED\n");
tapiov 6:641b171407c9 65 }
tapiov 6:641b171407c9 66
tapiov 6:641b171407c9 67 // Erase in any case for start
tapiov 6:641b171407c9 68 BlockDeviceErase();
tapiov 1:c3c61d08f31b 69
tapiov 6:641b171407c9 70 // Try to mount the filesystem
tapiov 6:641b171407c9 71 MountFS();
tapiov 6:641b171407c9 72
tapiov 1:c3c61d08f31b 73 char CmdBuffer[30];
tapiov 1:c3c61d08f31b 74 char Arg[30]=" ";
tapiov 1:c3c61d08f31b 75 char Cmd[30]=" ";
tapiov 2:386f65563144 76 size_t n=0;
tapiov 0:b3e6088c873f 77
tapiov 1:c3c61d08f31b 78 uint32_t NoOfPoints=19200;
tapiov 1:c3c61d08f31b 79 uint32_t AvgSize=10;
tapiov 1:c3c61d08f31b 80 uint32_t Period_us=100;
tapiov 1:c3c61d08f31b 81 uint32_t Count_ms=1000;
tapiov 6:641b171407c9 82 uint32_t MeasNumber=1;
tapiov 6:641b171407c9 83 int err;
tapiov 2:386f65563144 84
tapiov 2:386f65563144 85 mbed_stats_heap_t heap_stats;
tapiov 1:c3c61d08f31b 86
tapiov 1:c3c61d08f31b 87 // Clear screen, set it up
tapiov 1:c3c61d08f31b 88 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 2:386f65563144 89
tapiov 1:c3c61d08f31b 90 //Initialize data storage
tapiov 1:c3c61d08f31b 91 Array Data;
tapiov 1:c3c61d08f31b 92 initArray(&Data,NoOfPoints); // initially 19200 elements
tapiov 0:b3e6088c873f 93
tapiov 5:f8d3bcb187de 94 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 5:f8d3bcb187de 95 LCDWrite(5,"Ready.",CENTER_MODE);
tapiov 5:f8d3bcb187de 96
tapiov 2:386f65563144 97 while (!((strcmp(Cmd,"quit")==0)&&(n==1))) {
tapiov 1:c3c61d08f31b 98
tapiov 6:641b171407c9 99 // Print Ready and current settings
tapiov 6:641b171407c9 100 pc.printf(
tapiov 5:f8d3bcb187de 101 "Ready. Settings are Points=%u, Avg=%u, Period_us=%u, "
tapiov 5:f8d3bcb187de 102 "Count_ms=%u. Sampling will take apprx. %.3f secs\r\n",
tapiov 5:f8d3bcb187de 103 NoOfPoints,AvgSize,Period_us,Count_ms,
tapiov 5:f8d3bcb187de 104 (float)(NoOfPoints*Period_us/1000000.0));
tapiov 2:386f65563144 105
tapiov 2:386f65563144 106 mbed_stats_heap_get(&heap_stats);
tapiov 6:641b171407c9 107 pc.printf("Current heap = %lu Max heap = %lu\r\n",
tapiov 6:641b171407c9 108 heap_stats.current_size,heap_stats.max_size);
tapiov 6:641b171407c9 109
tapiov 6:641b171407c9 110 pc.gets(CmdBuffer,30);
tapiov 6:641b171407c9 111 pc.printf("I got %s \r\n", CmdBuffer);
tapiov 1:c3c61d08f31b 112 strcpy(Cmd," ");
tapiov 1:c3c61d08f31b 113 strcpy(Arg," ");
tapiov 1:c3c61d08f31b 114
tapiov 1:c3c61d08f31b 115 // Parse command and possible numeric arg
tapiov 1:c3c61d08f31b 116 char s[] = "Initial string";
tapiov 2:386f65563144 117 char ** word_array=NULL;
tapiov 1:c3c61d08f31b 118
tapiov 1:c3c61d08f31b 119 strcpy(s,CmdBuffer);
tapiov 2:386f65563144 120 n=string_parser(s,&word_array);
tapiov 1:c3c61d08f31b 121
tapiov 2:386f65563144 122 for ( size_t i=0;i<n;i++) {
tapiov 1:c3c61d08f31b 123 if (i==0) {strcpy(Cmd,word_array[i]);}
tapiov 1:c3c61d08f31b 124 if (i==1) {strcpy(Arg,word_array[i]);}
tapiov 6:641b171407c9 125 if (i>1) {pc.printf("Wrong number of arguments \r\n");}
tapiov 0:b3e6088c873f 126 }
tapiov 0:b3e6088c873f 127
tapiov 6:641b171407c9 128 pc.printf("Cmd = %s Arg = %s \r\n",Cmd,Arg);
tapiov 2:386f65563144 129 for ( size_t i=0;i<n;i++) free( word_array[i] );
tapiov 2:386f65563144 130 free(word_array);
tapiov 1:c3c61d08f31b 131
tapiov 1:c3c61d08f31b 132 // Branch based on command
tapiov 1:c3c61d08f31b 133 // meas: Sample and plot a data set
tapiov 2:386f65563144 134 if((strcmp(Cmd,"meas")==0)&&(n==1)) {
tapiov 6:641b171407c9 135
tapiov 1:c3c61d08f31b 136 // Countdown
tapiov 1:c3c61d08f31b 137 CountDown(Count_ms);
tapiov 1:c3c61d08f31b 138
tapiov 1:c3c61d08f31b 139 // Sample & plot data one time
tapiov 1:c3c61d08f31b 140 SamplePoints(&Data,NoOfPoints,Period_us);
tapiov 1:c3c61d08f31b 141 AvgAndPlotPoints(&Data,NoOfPoints,AvgSize);
tapiov 6:641b171407c9 142 WriteData2FS(&Data,NoOfPoints,MeasNumber);
tapiov 6:641b171407c9 143 MeasNumber++;
tapiov 6:641b171407c9 144
tapiov 6:641b171407c9 145 //-- implement http server with fs as storage
tapiov 6:641b171407c9 146 //-- connect and pull data from there
tapiov 6:641b171407c9 147 //-- implement disk init as functions & commands
tapiov 1:c3c61d08f31b 148 }
tapiov 0:b3e6088c873f 149
tapiov 1:c3c61d08f31b 150 // setpoints: Adjust sampled points
tapiov 2:386f65563144 151 else if((strcmp(Cmd,"setpoints")==0)&&(n==2)) {
tapiov 1:c3c61d08f31b 152 // Allocate more or less data space
tapiov 1:c3c61d08f31b 153 NoOfPoints=(uint32_t) strtol(Arg,NULL,10);
tapiov 6:641b171407c9 154 pc.printf("Old Data size is %u New NoOfPOints = %u \r\n",Data.size,NoOfPoints);
tapiov 5:f8d3bcb187de 155 insertArray(&Data,NoOfPoints);
tapiov 6:641b171407c9 156 pc.printf("New Array size is %u \r\n",Data.size);
tapiov 1:c3c61d08f31b 157 }
tapiov 1:c3c61d08f31b 158
tapiov 1:c3c61d08f31b 159 // setavg: Adjust average amount in samples
tapiov 2:386f65563144 160 else if((strcmp(Cmd,"setavg")==0)&&(n==2)) {
tapiov 1:c3c61d08f31b 161 AvgSize=(uint32_t) strtol(Arg,NULL,10);
tapiov 1:c3c61d08f31b 162 }
tapiov 1:c3c61d08f31b 163
tapiov 1:c3c61d08f31b 164 // setperiod: Adjust sample period in us
tapiov 2:386f65563144 165 else if((strcmp(Cmd,"setperiod")==0)&&(n==2)) {
tapiov 1:c3c61d08f31b 166 Period_us=(uint32_t) strtol(Arg,NULL,10);
tapiov 1:c3c61d08f31b 167 }
tapiov 1:c3c61d08f31b 168
tapiov 1:c3c61d08f31b 169 // setcount: Adjust countdown period in ms
tapiov 2:386f65563144 170 else if((strcmp(Cmd,"setcount")==0)&&(n==2)) {
tapiov 1:c3c61d08f31b 171 Count_ms=(uint32_t) strtol(Arg,NULL,10);
tapiov 2:386f65563144 172 }
tapiov 2:386f65563144 173
tapiov 6:641b171407c9 174 // dir: List files
tapiov 6:641b171407c9 175 else if((strcmp(Cmd,"dir")==0)&&(n==1)) {
tapiov 6:641b171407c9 176 DirListing();
tapiov 6:641b171407c9 177 }
tapiov 6:641b171407c9 178
tapiov 2:386f65563144 179 // quit: Exit on next while
tapiov 2:386f65563144 180 else if((strcmp(Cmd,"quit")==0)&&(n==1)) {
tapiov 2:386f65563144 181 // Do nothing yet
tapiov 2:386f65563144 182 }
tapiov 2:386f65563144 183 else {
tapiov 6:641b171407c9 184 pc.printf("Wrong command or argument \r\n");
tapiov 2:386f65563144 185 }
tapiov 1:c3c61d08f31b 186 }
tapiov 1:c3c61d08f31b 187
tapiov 1:c3c61d08f31b 188 // Free memory after quit
tapiov 1:c3c61d08f31b 189 freeArray(&Data);
tapiov 6:641b171407c9 190
tapiov 6:641b171407c9 191 // Tidy up filesystem
tapiov 6:641b171407c9 192 pc.printf("Unmounting filesystem.");
tapiov 6:641b171407c9 193 fflush(stdout);
tapiov 6:641b171407c9 194 err = fs.unmount();
tapiov 6:641b171407c9 195 pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
tapiov 6:641b171407c9 196 if (err < 0) {
tapiov 6:641b171407c9 197 error("error: %s (%d)\n", strerror(-err), err);
tapiov 6:641b171407c9 198 }
tapiov 5:f8d3bcb187de 199
tapiov 5:f8d3bcb187de 200 // Print informative messages
tapiov 5:f8d3bcb187de 201 InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
tapiov 5:f8d3bcb187de 202 LCDWrite(5,"Stop.",CENTER_MODE);
tapiov 6:641b171407c9 203 pc.printf("Exit. Data freed. Stop. \r\n");
tapiov 1:c3c61d08f31b 204
tapiov 1:c3c61d08f31b 205 // Infinite loop
tapiov 1:c3c61d08f31b 206 while(1)
tapiov 1:c3c61d08f31b 207 {
tapiov 1:c3c61d08f31b 208 // Do nothing
tapiov 0:b3e6088c873f 209 }
tapiov 0:b3e6088c873f 210 }