STM32L476

Dependencies:   MbedJSONValue SDFileSystem WConstants mbed-dev DS1820 TinyGPSPlus epd1in54

Fork of A_SSL_Main by SilentSensors

Revision:
2:b14aac0ec647
Parent:
0:eafcd0445e97
Child:
3:bc4495101c7b
--- a/main.cpp	Wed Apr 11 13:36:00 2018 +0200
+++ b/main.cpp	Tue Aug 21 13:14:51 2018 +0000
@@ -1,87 +1,227 @@
 #include "mbed.h"
+#include "stm32l496g_discovery.h"
 #include "stm32l496g_discovery_lcd.h"
-#include "stm32l496g_discovery_ts.h"
+#include "stm32l496g_discovery_sd.h"
+#include "DS1820.h"
+#include "epd1in54.h"
+#include "stdio.h"
+
+#define NUM_OF_BLOCKS            5
+#define BLOCK_START_ADDR         0
+#define BLOCK_SIZE               512
+#define BLOCK_END_ADDR           (BLOCK_SIZE * NUM_OF_BLOCKS)
+#define BUFFER_WORDS_SIZE        ((BLOCK_SIZE * NUM_OF_BLOCKS) >> 2) // Total data size in bytes
+
+//DS18B20 temperature sensor
+ Serial serial(USBTX, USBRX);
+ DS1820  ds1820(PG_15);    //pin name connected to the DS1820 data pin
+ float t;
+ 
+//E-ink Display
+// Control
+PinName rst;
+PinName dc;
+PinName busy;
+//SPI communication
+PinName mosi;
+PinName miso;
+PinName sclk;
+PinName cs;
+unsigned char frame_black[EPD_HEIGHT*EPD_WIDTH/8];
+
+uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
+uint32_t aRxBuffer[BUFFER_WORDS_SIZE];
+
+static void print_demo_title(void);
+static void print_PASS(void);
+static void print_FAIL(void);
+static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset);
+static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength);
+
+float getTemp(float t)
+{
+
+    // getTemp(tRead);
+
+    if(ds1820.begin()) {
+        ds1820.startConversion();
+        wait(1.0);
+        t = ds1820.read();         // read temperature
+        serial.printf("temp = %3.1f\r\n", t);     // Display temp on terminal
+
+    } else
+        serial.printf("No DS1820 sensor found!\r\n");
+
+
+
+    
+    mosi = PB_5;
+    miso = PB_4;
+    sclk = PA_5;
+    cs = PA_15;
+    rst = PI_6;
+    dc = PG_6;
+    busy = PI_11;
+
+    char cVal[32];
+    //char* t_str;
+    sprintf(cVal,"%.2f", t);
+
+    memset(frame_black, 0xFF, sizeof(unsigned char)*EPD_HEIGHT*EPD_WIDTH/8);
+
+    Epd epd = Epd(mosi, miso, sclk, cs, dc, rst, busy);
+    if (epd.Init(lut_full_update) != 0) {
+        return -1;
+    }
+
+    /*Write strings to the buffer */
+    epd.DrawStringAt(frame_black, 30, 30, cVal, &Font16, COLORED);
+    epd.DrawStringAt(frame_black, 28, 10, "Temperature", &Font16, COLORED);
+
+    /* Display the frame_buffer */
+    epd.SetFrameMemory(frame_black, 0, 0, epd.width, epd.height);
+    epd.DisplayFrame();
+    epd.Sleep();
+
+    
+    return t;
+}
 
 int main()
 {
-    TS_StateTypeDef TS_State;
-    uint16_t x, y;
-    uint8_t text[30];
-    uint8_t idx;
-    uint8_t cleared = 0;
-    uint8_t prev_nb_touches = 0;
-
-    BSP_LCD_Init();
-    BSP_LCD_SetFont(&Font20);
-    BSP_LCD_Clear(LCD_COLOR_BLUE);
-    BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
-    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
-    BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"DISCO_L496AG", CENTER_MODE);
-    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"LCDTS DEMO", CENTER_MODE);
-    wait(1);
-
-    if (BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize()) != TS_OK)
-    {
-      BSP_LCD_Clear(LCD_COLOR_RED);
-      BSP_LCD_SetBackColor(LCD_COLOR_RED);
-      BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
-      BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TS INIT FAIL", CENTER_MODE);
+    uint8_t status;
+    print_demo_title();
+    wait(0.2);
+    
+    // Initialization
+    status = BSP_SD_Init();
+    if (status == MSD_OK) {
+      BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD INIT OK", LEFT_MODE);
+    }
+    else if (status == MSD_ERROR_SD_NOT_PRESENT) {
+      BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD CARD NOT FOUND", LEFT_MODE);
+      print_FAIL();
     }
-    else
-    {
-      BSP_LCD_Clear(LCD_COLOR_GREEN);
-      BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
-      BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
-      BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TS INIT OK", CENTER_MODE);
+    else {
+      BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD INIT FAIL", LEFT_MODE);
+      print_FAIL();
+    }
+    
+    // Erase
+    status = BSP_SD_Erase(BLOCK_START_ADDR, BLOCK_END_ADDR);
+    if (status == MSD_OK) {
+      BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SD ERASE OK", LEFT_MODE);
+    }
+    else {
+      BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SD ERASE FAIL", LEFT_MODE);
+      print_FAIL();
     }
-    wait(0.5);
-
-    BSP_LCD_SetFont(&Font16);
-    BSP_LCD_Clear(LCD_COLOR_BLUE);
-    BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
-    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
-
-    while(1) {
-
-      BSP_TS_GetState(&TS_State);
-
-      if (TS_State.touchDetected)
-      {
-        // Clear lines corresponding to old touches coordinates
-        if (TS_State.touchDetected < prev_nb_touches)
-        {
-          for (idx = (TS_State.touchDetected + 1); idx <= TS_MAX_NB_TOUCH; idx++)
-          {
-            BSP_LCD_ClearStringLine(idx);
-          }
-        }
-        prev_nb_touches = TS_State.touchDetected;
-
-        cleared = 0;
-
-        sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
-        BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
-
-        for (idx = 0; idx < TS_State.touchDetected; idx++)
-        {
-          x = TS_State.touchX[idx];
-          y = TS_State.touchY[idx];
-          sprintf((char*)text, "Touch %d: x=%d y=%d", idx+1, x, y);
-          BSP_LCD_DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
-          BSP_LCD_DrawHLine(x-10, y, 20);
-          BSP_LCD_DrawVLine(x, y-10, 20);
-        }
-      }
-      else
-      {
-        if (!cleared)
-        {
-          sprintf((char*)text, "Touches: 0");
-          BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
-          cleared = 1;
-        }
-      }
-
+    
+    // Prepare the buffer to write
+    Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
+    
+    // Write
+    status = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT);
+    if (status == MSD_OK) {
+      BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SD WRITE OK", LEFT_MODE);
+    }
+    else {
+      BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SD WRITE FAIL", LEFT_MODE);
+      print_FAIL();
+    }
+    
+    // Read
+    status = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT);
+    if (status == MSD_OK) {
+      BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SD READ OK", LEFT_MODE);
+    }
+    else {
+      BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SD READ FAIL", LEFT_MODE);
+      print_FAIL();
+    }
+    
+    // Compare data
+    if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) == 0) {
+      BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)"SD DATA OK", LEFT_MODE);
+    }
+    else {
+      BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)"SD DATA FAIL", LEFT_MODE);
+      print_FAIL();
     }
 
+    print_PASS();
+    
+     float x;
+  
+  while(1){
+    x = getTemp(t);
+    serial.printf("temp = %3.1f\r\n", x);
+    wait(5);
+  }
 }
+
+static void print_demo_title(void)
+{
+    BSP_LCD_Init();
+    BSP_LCD_Clear(LCD_COLOR_WHITE);
+    BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
+    BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80);
+    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+    BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
+    BSP_LCD_SetFont(&Font24);
+    BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"SDCARD", CENTER_MODE);
+    BSP_LCD_SetFont(&Font12);
+    BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE);
+    BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the SDCard", CENTER_MODE);
+    BSP_LCD_SetFont(&Font20);
+}
+
+static void print_PASS(void)
+{
+    BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+    BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
+    BSP_LCD_DisplayStringAt(0, LINE(11), (uint8_t *)"Demo OK", CENTER_MODE);
+    //while(1);
+}
+
+static void print_FAIL(void)
+{
+    BSP_LCD_SetBackColor(LCD_COLOR_RED);
+    BSP_LCD_DisplayStringAt(0, LINE(11), (uint8_t *)"Demo FAILED", CENTER_MODE);
+   // while(1);
+}
+
+/**
+  * @brief  Fills buffer with user predefined data.
+  * @param  pBuffer: pointer on the buffer to fill
+  * @param  uwBufferLenght: size of the buffer to fill
+  * @param  uwOffset: first value to fill on the buffer
+  * @retval None
+  */
+static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
+{
+  uint32_t tmpIndex = 0;
+  /* Put in global buffer different values */
+  for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ ) {
+    pBuffer[tmpIndex] = tmpIndex + uwOffset;
+  }
+}
+ 
+/**
+  * @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
+  */
+static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
+{
+  while (BufferLength--) {
+    if (*pBuffer1 != *pBuffer2) {
+      return 1;
+    }
+    pBuffer1++;
+    pBuffer2++;
+  }
+  return 0;
+}