Thomas Chef преди 3 години
родител
ревизия
b56029829b
променени са 3 файла, в които са добавени 55 реда и са изтрити 9 реда
  1. 1 8
      STM32/Core/Inc/uart.h
  2. 50 0
      STM32/Core/Src/cmdRx.c
  3. 4 1
      STM32/Core/Src/main.c

+ 1 - 8
STM32/Core/Inc/uart.h

@@ -17,13 +17,6 @@
 void uart_send_byte(uint8_t byte);
 
 
-// UART data receive function
-//  - checks if data exists in the receive sw buffer
-//  - if data exists, it returns the oldest element contained in the buffer 
-//  - automatically handles "uart_rx_buffer_full_flag"
-//  - if no data exists, it clears the uart_rx_flag
-uint8_t uart_get_byte(void);
-
 volatile extern uint8_t uart_rx_fifo_not_empty_flag; // this flag is automatically set and cleared by the software buffer
 volatile extern uint8_t uart_rx_fifo_full_flag;      // this flag is automatically set and cleared by the software buffer
 volatile extern uint8_t uart_rx_fifo_ovf_flag;       // this flag is not automatically cleared by the software buffer
@@ -36,4 +29,4 @@ void usartInterrupt(void);
 int serialAvailable();
 unsigned char serialRead(void);
 
-#endif
+#endif

+ 50 - 0
STM32/Core/Src/cmdRx.c

@@ -0,0 +1,50 @@
+#include "uart.h"
+#include "stdio.h"
+#include "main.h"
+
+#define MAX_LEN 100
+
+void handleCmdRx() {
+
+	static int mode = -1;
+	static unsigned char str[MAX_LEN];
+	static unsigned char *valP;
+	static int reset = 1;
+
+    if( reset == 1 ) {
+            valP = str;
+            mode = 0;
+            reset = 0;
+    }
+
+	while( serialAvailable() ) {
+
+		const unsigned char c = serialRead();
+
+		if(  !(c>='0' && c<='9') &&
+			 !(c>='a' && c<='z') &&
+			 !(c>='A' && c<='Z') &&
+			 c!='\"' && c!=',' && c!='.' && c!='{' && c!='}') {
+			reset = 1;
+		}
+		else {
+			if( mode == 0 && c=='{' ) {
+				*(valP)++ = c;
+				mode = 1;
+			}
+			else if( mode == 1 && c=='}' ) {
+				*(valP)++ = c;
+				*(valP)++ = '\0';
+				int val=-1;
+				const int no = sscanf((const char *)str,"{%d}",&val);
+				if( no == 1 ) {
+					HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, (val==1)?GPIO_PIN_RESET:GPIO_PIN_SET);
+				}
+				reset=1;
+			}
+			else if( mode==1 ){
+				*(valP)++ = c;
+			}
+		}
+	}
+}

+ 4 - 1
STM32/Core/Src/main.c

@@ -63,6 +63,7 @@ static void MX_ADC1_Init(void);
 static void MX_TIM1_Init(void);
 static void MX_NVIC_Init(void);
 /* USER CODE BEGIN PFP */
+void handleCmdRx();
 
 /* USER CODE END PFP */
 
@@ -183,7 +184,7 @@ int main(void)
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
-	  HAL_GPIO_TogglePin (GPIOC, GPIO_PIN_13);
+
 
 	  //adr = 0x080C25372F3D253F;
 
@@ -201,6 +202,8 @@ int main(void)
 	  printf("{\"type\":\"vpp\",\"id\":\"0");
 	  printf("\",\"value1\":%u}\n",adcDiff);
 
+	  handleCmdRx();
+
 	  HAL_Delay(1000);
   }
   /* USER CODE END 3 */