Data sample and LCD plot
Dependencies: BSP_DISCO_F746NG LCD_DISCO_F746NG SDRAM_DISCO_F746NG mbed
Revision 1:c3c61d08f31b, committed 2018-02-23
- Comitter:
- tapiov
- Date:
- Fri Feb 23 16:56:03 2018 +0000
- Parent:
- 0:b3e6088c873f
- Child:
- 2:386f65563144
- Commit message:
- First version. Parametrized, mainly working. Not fully tested.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/functions.cpp Fri Feb 23 16:56:03 2018 +0000
@@ -0,0 +1,214 @@
+// LoadCell_STM32_RAM v2 functions
+// (C) Tapio Valli 2018-02-17
+
+#include "mbed.h"
+#include "LCD_DISCO_F746NG.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <ctype.h>
+
+#include "main.h"
+
+// define the Serial object
+Serial pc2(USBTX, USBRX);
+
+LCD_DISCO_F746NG lcd2;
+
+void initArray(Array *a, size_t initialSize) {
+ a->array = (uint32_t *)malloc(initialSize * sizeof(uint32_t));
+ a->used = 0;
+ a->size = initialSize;
+}
+
+void insertArray(Array *a, uint32_t element) {
+ // a->used is the number of used entries, because a->array[a->used++]
+ // updates a->used only *after* the array has been accessed.
+ // Therefore a->used can go up to a->size
+
+ if (a->used == a->size) {
+ a->size *= 2;
+ a->array = (uint32_t *)realloc(a->array, a->size * sizeof(uint32_t));
+ }
+ a->array[a->used++] = element;
+}
+
+void freeArray(Array *a) {
+ free(a->array);
+ a->array = NULL;
+ a->used = a->size = 0;
+}
+
+size_t string_parser(char *input, char ***word_array)
+{
+ size_t n = 0;
+ const char *p = input;
+
+ while ( *p )
+ {
+ while ( isspace( ( unsigned char )*p ) ) ++p;
+ n += *p != '\0';
+ while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
+ }
+
+ if ( n )
+ {
+ size_t i = 0;
+
+ *word_array = (char**)malloc( n * sizeof( char * ) );
+
+ p = input;
+
+ while ( *p )
+ {
+ while ( isspace( ( unsigned char )*p ) ) ++p;
+ if ( *p )
+ {
+ const char *q = p;
+ while ( *p && !isspace( ( unsigned char )*p ) ) ++p;
+
+ size_t length = p - q;
+
+ ( *word_array )[i] = ( char * )malloc( length + 1 );
+
+ strncpy( ( *word_array )[i], q, length );
+ ( *word_array )[i][length] = '\0';
+
+ ++i;
+ }
+ }
+ }
+
+ return n;
+}
+
+void PlotData(uint32_t XCoordinate,uint32_t YCoordinate)
+{
+ // Plot at x,y
+ lcd2.DrawHLine(XCoordinate,YCoordinate,1);
+}
+
+void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor)
+{
+
+// #define LCD_COLOR_BLUE ((uint32_t)0xFF0000FF)
+// #define LCD_COLOR_GREEN ((uint32_t)0xFF00FF00)
+// #define LCD_COLOR_RED ((uint32_t)0xFFFF0000)
+// #define LCD_COLOR_CYAN ((uint32_t)0xFF00FFFF)
+// #define LCD_COLOR_MAGENTA ((uint32_t)0xFFFF00FF)
+// #define LCD_COLOR_YELLOW ((uint32_t)0xFFFFFF00)
+// #define LCD_COLOR_LIGHTBLUE ((uint32_t)0xFF8080FF)
+// #define LCD_COLOR_LIGHTGREEN ((uint32_t)0xFF80FF80)
+// #define LCD_COLOR_LIGHTRED ((uint32_t)0xFFFF8080)
+// #define LCD_COLOR_LIGHTCYAN ((uint32_t)0xFF80FFFF)
+// #define LCD_COLOR_LIGHTMAGENTA ((uint32_t)0xFFFF80FF)
+// #define LCD_COLOR_LIGHTYELLOW ((uint32_t)0xFFFFFF80)
+// #define LCD_COLOR_DARKBLUE ((uint32_t)0xFF000080)
+// #define LCD_COLOR_DARKGREEN ((uint32_t)0xFF008000)
+// #define LCD_COLOR_DARKRED ((uint32_t)0xFF800000)
+// #define LCD_COLOR_DARKCYAN ((uint32_t)0xFF008080)
+// #define LCD_COLOR_DARKMAGENTA ((uint32_t)0xFF800080)
+// #define LCD_COLOR_DARKYELLOW ((uint32_t)0xFF808000)
+// #define LCD_COLOR_WHITE ((uint32_t)0xFFFFFFFF)
+// #define LCD_COLOR_LIGHTGRAY ((uint32_t)0xFFD3D3D3)
+// #define LCD_COLOR_GRAY ((uint32_t)0xFF808080)
+// #define LCD_COLOR_DARKGRAY ((uint32_t)0xFF404040)
+// #define LCD_COLOR_BLACK ((uint32_t)0xFF000000)
+// #define LCD_COLOR_BROWN ((uint32_t)0xFFA52A2A)
+// #define LCD_COLOR_ORANGE ((uint32_t)0xFFFFA500)
+// #define LCD_COLOR_TRANSPARENT ((uint32_t)0xFF000000)
+
+ lcd2.Clear(BackGroundColor);
+ lcd2.SetBackColor(BackGroundColor);
+ lcd2.SetTextColor(ForeGroundColor);
+ lcd2.SetFont(&Font20);
+}
+
+void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlignMode)
+{
+ char IntStr[50];
+
+// InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE,Font20);
+
+ lcd2.ClearStringLine(Line);
+ snprintf(IntStr,50,Str);
+ lcd2.DisplayStringAt(0, LINE(Line), (uint8_t *)IntStr, AlignMode);
+}
+
+void CountDown(uint32_t millisecs)
+{
+ InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
+
+ LCDWrite(5," ",CENTER_MODE);
+ wait_ms(1);
+
+ LCDWrite(5,"Starting in 3... ",CENTER_MODE);
+ wait_ms(millisecs);
+
+ LCDWrite(5,"Starting in 2... ",CENTER_MODE);
+ wait_ms(millisecs);
+
+ LCDWrite(5,"Starting in 1... ",CENTER_MODE);
+ wait_ms(millisecs);
+
+ InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
+ LCDWrite(5,"GO!",CENTER_MODE);
+}
+
+void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us)
+{
+ AnalogIn ain(A0);
+ uint32_t i;
+
+ // Measure NoOfPoints values (f.ex. 19200)
+ for(i=0;i<NoOfPoints;i++) {
+ Data->array[i]=(uint32_t)((0x0000 << 16) | ain.read_u16());
+ wait_us(Period_us);
+ //if ((i%100)==0) {
+ // pc2.printf("index i = %u",i);
+ //}
+ }
+
+ InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
+ LCDWrite(5,"DONE!",CENTER_MODE);
+
+ pc2.printf("\r\nSampling done, index i = %u \r\n",i);
+}
+
+void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize) {
+
+ uint32_t i1,i2;
+
+ uint32_t BufferSum,BufferAvg;
+ uint32_t XCoord,YCoord;
+ char MyStr[50];
+
+ pc2.printf("Start reading... \r\n");
+ InitScreen(LCD_COLOR_BLACK,LCD_COLOR_RED);
+
+ for(i1=0;i1<NoOfPoints;i1++) {
+ BufferSum=0;
+
+ // Read AvgSize samples
+ for(i2=i1;i2<i1+AvgSize;i2++) {
+ BufferSum=BufferSum+Data->array[i2];
+ }
+
+ BufferAvg=BufferSum/AvgSize;
+
+ // Calculate two coords and plot
+ XCoord=((i1*480.0)/NoOfPoints);
+ YCoord=(272.0*(BufferAvg/65536.0));
+
+ PlotData(XCoord,YCoord);
+ }
+
+ pc2.printf("Done all, Points = %u Avg = %u \r\n", i1,AvgSize);
+
+ LCDWrite(0,"",CENTER_MODE);
+ snprintf(MyStr,50,"Pnts = %d Avg = %d",NoOfPoints,AvgSize);
+ LCDWrite(0,MyStr,RIGHT_MODE);
+}
--- a/main.cpp Sat Feb 10 20:19:36 2018 +0000
+++ b/main.cpp Fri Feb 23 16:56:03 2018 +0000
@@ -1,159 +1,139 @@
-#include "mbed.h"
-#include <string.h>
-#include "LCD_DISCO_F746NG.h"
-#include "SDRAM_DISCO_F746NG.h"
-
-LCD_DISCO_F746NG lcd;
-SDRAM_DISCO_F746NG sdram;
+// LoadCell_STM32_RAM v2 main
+// (C) Tapio Valli 2018-02-17
-#define BUFFER_SIZE ((uint32_t)0x0001)
-#define WRITE_READ_ADDR ((uint32_t)0x0800)
-
-void FillBuffer(uint32_t *pBuffer, uint32_t BufferLength, uint32_t Offset);
-uint8_t CompareBuffer(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength);
-
-Ticker ms_tick;
-uint32_t idx;
-uint32_t WRITE_READ_ADDR; (()0x0800)
+#include "mbed.h"
+#include <mbed_stats.h>
+#include "mbed_mem_trace.h"
+
+#include "LCD_DISCO_F746NG.h"
-void onTimingEventTicker(void)
-{
- // this code will run every timing event
- uint32_t result;
- uint32_t y_coord;
-
- AnalogIn ain(A0);
-
- result = (0x0000 << 16) | ain.read_u16();
-
- // Fill the write buffer
- FillBuffer(WriteBuffer, BUFFER_SIZE, result);
-
- // Write buffer
- sdram.WriteData(SDRAM_DEVICE_ADDR + WRITE_READ_ADDR + idx, WriteBuffer, BUFFER_SIZE);
-
- idx++;
-}
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <ctype.h>
-uint32_t PlotData(void)
-{
- // this code will run every timing event
- uint32_t result;
- uint32_t y_coord;
-
- AnalogIn ain(A0);
+#include "main.h"
- if (x_coord == 0) { lcd.Clear(LCD_COLOR_BLUE); }
-
- result = (0x0000 << 16) | ain.read_u16();
-
- y_coord=(272.0*result)/16384.0;
+// define the Serial object
+Serial pc1(USBTX, USBRX);
+LCD_DISCO_F746NG lcd1;
- if (x_coord < 480) { x_coord++; } else { x_coord=0; }
-
- //snprintf(my_str, 50,"Result = %f\0", result/4096.0);
- //snprintf(my_str, 50,"Loops = %i\0", loops);
-
- //lcd.DisplayStringAt(0, LINE(5), (uint8_t *)my_str, CENTER_MODE);
-
- // Plot loops,y_coord
- lcd.DrawHLine(loops,y_coord,1);
-
-}
+// Global heap stats
+mbed_stats_heap_t heap_stats;
+//mbed_stats_stack_t stack_stats;
+//mbed_mem_trace_cb_t mem_trace_result;
+
+// Mem trace callback
+// mbed_mem_trace_cb_t mem_trace(void)
+// {
+// pc1.printf("Heap = %lu, Max = %lu Percentage = %lu \r\n",
+// heap_stats.current_size,heap_stats.max_size,
+// (100*(heap_stats.current_size/heap_stats.max_size)));
+
+// pc1.printf("Stack = %lu, Max = %lu Percentage = %lu \r\n",
+// stack_stats.reserved_size,stack_stats.max_size,
+// (100*(stack_stats.reserved_size/stack_stats.max_size)));
+//}
int main() {
-
- uint32_t WriteBuffer[BUFFER_SIZE];
- uint32_t ReadBuffer[BUFFER_SIZE];
- FMC_SDRAM_CommandTypeDef SDRAMCommandStructure;
+
+ char CmdBuffer[30];
+ char Arg[30]=" ";
+ char Cmd[30]=" ";
- // # of samples
- idx=0;
+ uint32_t NoOfPoints=19200;
+ uint32_t AvgSize=10;
+ uint32_t Period_us=100;
+ uint32_t Count_ms=1000;
+
+// mbed_mem_trace_set_callback(mem_trace);
+
+ // Clear screen, set it up
+ InitScreen(LCD_COLOR_BLACK,LCD_COLOR_WHITE);
- // turn on 100 us interrupt
- ms_tick.attach_us(onTimingEventTicker,100);
+ //Initialize data storage
+ Array Data;
+ initArray(&Data,NoOfPoints); // initially 19200 elements
- lcd.Clear(LCD_COLOR_BLUE);
- lcd.SetBackColor(LCD_COLOR_BLUE);
- lcd.SetTextColor(LCD_COLOR_WHITE);
-
- // Issue self-refresh command to SDRAM device
- SDRAMCommandStructure.CommandMode=FMC_SDRAM_CMD_SELFREFRESH_MODE;
- SDRAMCommandStructure.CommandTarget=FMC_SDRAM_CMD_TARGET_BANK2;
- SDRAMCommandStructure.AutoRefreshNumber=1;
- SDRAMCommandStructure.ModeRegisterDefinition = 0;
-
- while(1) {
+ while (Cmd!="quit") {
+
+ // Print Ready and current settings
+ pc1.printf("Ready. Settings are Points = %u, Avg = %u, Period_us = %u, Count_ms = %u \r\n",
+ NoOfPoints,AvgSize,Period_us,Count_ms);
- // Measure 19200 values (1.920 s time)
- while(idx<19200) {
- // Wait while the data is collected
+ mbed_stats_heap_get(&heap_stats);
+ pc1.printf("Heap = %lu, Max = %lu Percentage = %lu \r\n",
+ heap_stats.current_size,heap_stats.max_size,
+ (100*(heap_stats.current_size/heap_stats.max_size)));
+
+ pc1.gets(CmdBuffer,30);
+ // pc1.printf("I got %s \r\n", CmdBuffer);
+ strcpy(Cmd," ");
+ strcpy(Arg," ");
+
+ // Parse command and possible numeric arg
+ char s[] = "Initial string";
+ char ** word_array = NULL;
+
+ strcpy(s,CmdBuffer);
+ size_t n = string_parser(s, &word_array );
+
+ for ( size_t i = 0; i < n; i++ ) {
+ if (i==0) {strcpy(Cmd,word_array[i]);}
+ if (i==1) {strcpy(Arg,word_array[i]);}
+ if (i>1) {pc1.printf("Wrong number of arguments \r\n");}
}
- // Read back data from the SDRAM memory
- sdram.ReadData(SDRAM_DEVICE_ADDR + WRITE_READ_ADDR + idx, ReadBuffer, BUFFER_SIZE);
- pc.printf("\nRead data DONE\n");
+ // pc1.printf("Cmd = %s Arg = %s \r\n",Cmd,Arg);
+ for ( size_t i = 0; i < n; i++ ) free( word_array[i] );
+ free( word_array );
+
+ // Branch based on command
+ // meas: Sample and plot a data set
+ if(strcmp(Cmd,"meas") == 0) {
+
+ // Countdown
+ CountDown(Count_ms);
+
+ // Sample & plot data one time
+ SamplePoints(&Data,NoOfPoints,Period_us);
+
+ AvgAndPlotPoints(&Data,NoOfPoints,AvgSize);
+ }
-
- // Read back data from the SDRAM memory
-// sdram.ReadData(SDRAM_DEVICE_ADDR + WRITE_READ_ADDR, ReadBuffer, BUFFER_SIZE);
-// pc.printf("\nRead data DONE\n");
-
- // Checking data integrity
-// if (CompareBuffer(WriteBuffer, ReadBuffer, BUFFER_SIZE) != 0)
-// {
-// led1 = !led1;
-// pc.printf("Write/Read buffers are different\n");
-// }
-// else
-// {
-// led1 = !led1;
-// pc.printf("Write/Read buffers are identical\n");
-// }
-
-// wait(1);
-
+ // setpoints: Adjust sampled points
+ else if(strcmp(Cmd,"setpoints") == 0) {
+ // Allocate more or less data space
+ NoOfPoints=(uint32_t) strtol(Arg,NULL,10);
+ insertArray(&Data,NoOfPoints);
+ }
+
+ // setavg: Adjust average amount in samples
+ else if(strcmp(Cmd,"setavg") == 0) {
+ AvgSize=(uint32_t) strtol(Arg,NULL,10);
+ }
+
+ // setperiod: Adjust sample period in us
+ else if(strcmp(Cmd,"setperiod") == 0) {
+ Period_us=(uint32_t) strtol(Arg,NULL,10);
+ }
+
+ // setcount: Adjust countdown period in ms
+ else if(strcmp(Cmd,"setcount") == 0) {
+ Count_ms=(uint32_t) strtol(Arg,NULL,10);
+ }
+ }
+
+ // Free memory after quit
+ freeArray(&Data);
+ pc1.printf("Exit. Data freed. \r\n", CmdBuffer);
+
+ // Infinite loop
+ while(1)
+ {
+ // Do nothing
}
}
-
-/**
- * @brief Fills buffer with user predefined data.
- * @param pBuffer: pointer on the buffer to fill
- * @param BufferLength: size of the buffer to fill
- * @param Value: first value to fill on the buffer
- * @retval None
- */
-void FillBuffer(uint32_t *pBuffer, uint32_t BufferLength, uint32_t Value)
-{
- uint32_t tmpIndex = 0;
-
- /* Put in global buffer different values */
- for (tmpIndex = 0; tmpIndex < BufferLength; tmpIndex++ )
- {
- pBuffer[tmpIndex] = tmpIndex + Value;
- }
-}
-
-/**
- * @brief Compares two buffers.
- * @param pBuffer1, pBuffer2: buffers to be compared.
- * @param BufferLength: buffer's length
- * @retval 0: pBuffer2 identical to pBuffer1
- * 1: pBuffer2 differs from pBuffer1
- */
-uint8_t CompareBuffer(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
-{
- while (BufferLength--)
- {
- if (*pBuffer1 != *pBuffer2)
- {
- return 1;
- }
-
- pBuffer1++;
- pBuffer2++;
- }
-
- return 0;
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h Fri Feb 23 16:56:03 2018 +0000
@@ -0,0 +1,22 @@
+// LoadCell_STM32_RAM v2 main header
+// (C) Tapio Valli 2018-02-20
+
+typedef struct {
+ uint32_t *array;
+ size_t used;
+ size_t size;
+} Array;
+
+// Prototypes
+
+extern void initArray(Array *a, size_t initialSize);
+extern void insertArray(Array *a, uint32_t element);
+extern void freeArray(Array *a);
+
+extern size_t string_parser(char *input, char ***word_array);
+extern void PlotData(uint32_t XCoordinate,uint32_t YCoordinate);
+extern void InitScreen(uint32_t BackGroundColor,uint32_t ForeGroundColor);
+extern void LCDWrite(uint32_t Line,char Str[],Text_AlignModeTypdef AlingMode);
+extern void CountDown(uint32_t millisecs);
+extern void SamplePoints(Array *Data,uint32_t NoOfPoints,uint32_t Period_us);
+extern void AvgAndPlotPoints(Array *Data,uint32_t NoOfPoints, uint32_t AvgSize);
--- a/mbed.bld Sat Feb 10 20:19:36 2018 +0000 +++ b/mbed.bld Fri Feb 23 16:56:03 2018 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/7130f322cb7e \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/5571c4ff569f \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json Fri Feb 23 16:56:03 2018 +0000
@@ -0,0 +1,3 @@
+{
+ "macros": ["MBED_MEM_TRACING_ENABLED"]
+}
\ No newline at end of file