Selaa lähdekoodia

Comm protocol improvement

Nagaev Aleksandr 2 vuotta sitten
vanhempi
commit
0bb5fa095e

+ 2 - 2
stm32workspace/dosim-v1/Core/Inc/commprt.h

@@ -11,9 +11,9 @@
 #include <stdint.h>
 #include <stdint.h>
 
 
 typedef enum { COMM_BT, COMM_USB } COMM_IfaceTypeDef;
 typedef enum { COMM_BT, COMM_USB } COMM_IfaceTypeDef;
-typedef enum { COMM_OK, COMM_FAIL } COMM_StatusTypeDef;
+typedef enum { COMM_OK, COMM_FAIL, COMM_TIMEOUT, COMM_CHECKSUM_MISTMATCH } COMM_StatusTypeDef;
 
 
-COMM_StatusTypeDef COMM_Receive(uint8_t *buf, COMM_IfaceTypeDef iface);
+COMM_StatusTypeDef COMM_Receive(uint8_t *buf, COMM_IfaceTypeDef iface, uint32_t timeout);
 
 
 COMM_StatusTypeDef COMM_Transmit(uint8_t *data, uint8_t len, COMM_IfaceTypeDef iface);
 COMM_StatusTypeDef COMM_Transmit(uint8_t *data, uint8_t len, COMM_IfaceTypeDef iface);
 
 

+ 1 - 0
stm32workspace/dosim-v1/Core/Inc/fifo.h

@@ -24,5 +24,6 @@ void fifo_init(FIFO_HandleTypeDef *fifo, uint8_t *buffer, uint32_t size);
 FIFO_StateTypeDef fifo_state(FIFO_HandleTypeDef *fifo);
 FIFO_StateTypeDef fifo_state(FIFO_HandleTypeDef *fifo);
 int fifo_read(FIFO_HandleTypeDef *fifo, uint8_t *buf, uint32_t len);
 int fifo_read(FIFO_HandleTypeDef *fifo, uint8_t *buf, uint32_t len);
 int fifo_write(FIFO_HandleTypeDef *fifo, uint8_t *data, uint32_t len);
 int fifo_write(FIFO_HandleTypeDef *fifo, uint8_t *data, uint32_t len);
+int fifo_size(FIFO_HandleTypeDef *fifo);
 
 
 #endif /* INC_FIFO_H_ */
 #endif /* INC_FIFO_H_ */

+ 26 - 0
stm32workspace/dosim-v1/Core/Inc/menu.h

@@ -0,0 +1,26 @@
+/*
+ * menu.h
+ *
+ *  Created on: Jan 26, 2022
+ *      Author: radioman
+ */
+
+#ifndef INC_MENU_H_
+#define INC_MENU_H_
+
+#include <stdint.h>
+
+typedef enum { ENTRY_DIR, ENTRY_DISPLAY, ENTRY_NUMBER, ENTRY_CHECKBOX } entry_type;
+
+typedef struct {
+	uint8_t id;
+	entry_type type;
+	uint8_t selected;
+	uint8_t active;
+	uint8_t parent_id;
+	char *title;
+} menu_entry_td;
+
+void MenuShow(uint8_t id);
+
+#endif /* INC_MENU_H_ */

+ 1 - 0
stm32workspace/dosim-v1/Core/Inc/stm32l1xx_it.h

@@ -57,6 +57,7 @@ void DebugMon_Handler(void);
 void PendSV_Handler(void);
 void PendSV_Handler(void);
 void SysTick_Handler(void);
 void SysTick_Handler(void);
 void DMA1_Channel1_IRQHandler(void);
 void DMA1_Channel1_IRQHandler(void);
+void DMA1_Channel3_IRQHandler(void);
 void DMA1_Channel4_IRQHandler(void);
 void DMA1_Channel4_IRQHandler(void);
 void DMA1_Channel5_IRQHandler(void);
 void DMA1_Channel5_IRQHandler(void);
 void USB_LP_IRQHandler(void);
 void USB_LP_IRQHandler(void);

+ 3 - 3
stm32workspace/dosim-v1/Core/Inc/uartio.h

@@ -12,8 +12,8 @@
 #define UART_TX_BUF_SIZE 256
 #define UART_TX_BUF_SIZE 256
 
 
 void UARTIO_Init(UART_HandleTypeDef *huart);
 void UARTIO_Init(UART_HandleTypeDef *huart);
-uint32_t UARTIO_DataAvail(UART_HandleTypeDef *huart);
-uint32_t UARTIO_Receive(UART_HandleTypeDef *huart, uint8_t *buf);
-uint32_t UARTIO_Transmit(UART_HandleTypeDef *huart, uint8_t *buf, uint32_t len);
+uint32_t UARTIO_DataAvailable(void);
+int UARTIO_Receive(uint8_t *buf, uint32_t len);
+int UARTIO_Transmit(uint8_t *buf, uint32_t len);
 
 
 #endif /* INC_UARTIO_H_ */
 #endif /* INC_UARTIO_H_ */

+ 67 - 19
stm32workspace/dosim-v1/Core/Src/commprt.c

@@ -11,12 +11,13 @@
 #include "uartio.h"
 #include "uartio.h"
 #include "usbd_cdc_if.h"
 #include "usbd_cdc_if.h"
 
 
-#define COMM_HEADER_BYTE	0x55;
+#define COMM_HEADER_BYTE	0x55
 #define COMM_BUFFER_SIZE	260
 #define COMM_BUFFER_SIZE	260
 
 
 extern UART_HandleTypeDef huart1;
 extern UART_HandleTypeDef huart1;
 
 
-static uint8_t comm_buffer[COMM_BUFFER_SIZE];
+static uint8_t comm_txbuffer[COMM_BUFFER_SIZE];
+static uint8_t comm_rxbuffer[COMM_BUFFER_SIZE];
 
 
 uint8_t COMM_Checksum(const uint8_t *data, uint8_t len)
 uint8_t COMM_Checksum(const uint8_t *data, uint8_t len)
 {
 {
@@ -26,28 +27,75 @@ uint8_t COMM_Checksum(const uint8_t *data, uint8_t len)
 	return chk;
 	return chk;
 }
 }
 
 
-COMM_StatusTypeDef COMM_Receive(uint8_t *buf, COMM_IfaceTypeDef iface)
+COMM_StatusTypeDef COMM_Receive(uint8_t *buf, COMM_IfaceTypeDef iface, uint32_t timeout)
 {
 {
-	return COMM_OK;
-}
+	uint8_t header = 0, psize = 0;
+	uint32_t stime = HAL_GetTick();
+	int (*read_data)(uint8_t*, uint32_t);
+	uint32_t (*data_avail)(void);
 
 
-COMM_StatusTypeDef COMM_Transmit(uint8_t *data, uint8_t len, COMM_IfaceTypeDef iface)
-{
-	comm_buffer[0] = COMM_HEADER_BYTE;
-	comm_buffer[1] = len;
-	memcpy(&comm_buffer[2], data, len);
-	comm_buffer[len + 2] = COMM_Checksum(comm_buffer, len + 2);
+	if(iface == COMM_BT)
+	{
+		read_data = UARTIO_Receive;
+		data_avail = UARTIO_DataAvailable;
+	}
+
+	if(iface == COMM_USB)
+	{
+		read_data = CDC_ReadData;
+		data_avail = CDC_DataAvailable;
+	}
 
 
-	switch(iface)
+	while((header != COMM_HEADER_BYTE))
 	{
 	{
-		case COMM_BT:
-			UARTIO_Transmit(&huart1, comm_buffer, len + 3);
-			break;
-		case COMM_USB:
-			CDC_Transmit_FS(comm_buffer, len + 3);
-			break;
-		default:
+		if(read_data(&header, 1) <= 0)
 			return COMM_FAIL;
 			return COMM_FAIL;
+		if((HAL_GetTick() - stime) > timeout)
+			return COMM_TIMEOUT;
 	}
 	}
+
+	while(read_data(&psize, 1) != 1)
+	{
+		if((HAL_GetTick() - stime) > timeout)
+			return COMM_TIMEOUT;
+	}
+
+	if(psize == 0)
+		return COMM_FAIL;
+
+	while(data_avail() < (psize + 1))
+	{
+		if((HAL_GetTick() - stime) > timeout)
+			return COMM_TIMEOUT;
+	}
+
+	read_data(comm_rxbuffer, psize + 1);
+
+	uint8_t checksum = (COMM_HEADER_BYTE ^ psize) ^ COMM_Checksum(comm_rxbuffer, psize);
+	if(checksum != comm_rxbuffer[psize])
+		return COMM_CHECKSUM_MISTMATCH;
+
+	memcpy(buf, comm_rxbuffer, psize);
+
 	return COMM_OK;
 	return COMM_OK;
 }
 }
+
+COMM_StatusTypeDef COMM_Transmit(uint8_t *data, uint8_t len, COMM_IfaceTypeDef iface)
+{
+	comm_txbuffer[0] = COMM_HEADER_BYTE;
+	comm_txbuffer[1] = len;
+	memcpy(&comm_txbuffer[2], data, len);
+	comm_txbuffer[len + 2] = COMM_Checksum(comm_txbuffer, len + 2);
+	int (*write_data)(uint8_t*, uint32_t);
+
+	if(iface == COMM_BT)
+		write_data = UARTIO_Transmit;
+
+	if(iface == COMM_USB)
+		write_data = CDC_WriteData;
+
+	if(write_data(data, len) > 0)
+		return COMM_OK;
+	else
+		return COMM_FAIL;
+}

+ 14 - 1
stm32workspace/dosim-v1/Core/Src/fifo.c

@@ -21,6 +21,20 @@ FIFO_StateTypeDef fifo_state(FIFO_HandleTypeDef *fifo)
 	return fifo->state;
 	return fifo->state;
 }
 }
 
 
+int fifo_size(FIFO_HandleTypeDef *fifo)
+{
+	if(fifo->state == FIFO_FULL)
+		return fifo->size;
+
+	if(fifo->state == FIFO_EMPTY)
+		return 0;
+
+	if(fifo->head >= fifo->tail)
+		return (fifo->head - fifo->tail);
+	else
+		return (fifo->size - fifo->tail + fifo->head);
+}
+
 int fifo_read(FIFO_HandleTypeDef *fifo, uint8_t *buf, uint32_t len)
 int fifo_read(FIFO_HandleTypeDef *fifo, uint8_t *buf, uint32_t len)
 {
 {
 	if(fifo->state != FIFO_EMPTY)
 	if(fifo->state != FIFO_EMPTY)
@@ -108,4 +122,3 @@ int fifo_write(FIFO_HandleTypeDef *fifo, uint8_t *data, uint32_t len)
 		return (-1);
 		return (-1);
 	}
 	}
 }
 }
-

+ 97 - 74
stm32workspace/dosim-v1/Core/Src/main.c

@@ -26,6 +26,8 @@
 #include "usbd_cdc_if.h"
 #include "usbd_cdc_if.h"
 #include "uartio.h"
 #include "uartio.h"
 #include "commprt.h"
 #include "commprt.h"
+#include "menu.h"
+#include "ssd1306_tests.h"
 #include <stdlib.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdio.h>
 /* USER CODE END Includes */
 /* USER CODE END Includes */
@@ -59,10 +61,10 @@ DAC_HandleTypeDef hdac;
 I2C_HandleTypeDef hi2c1;
 I2C_HandleTypeDef hi2c1;
 I2C_HandleTypeDef hi2c2;
 I2C_HandleTypeDef hi2c2;
 
 
-TIM_HandleTypeDef htim2;
 TIM_HandleTypeDef htim3;
 TIM_HandleTypeDef htim3;
 TIM_HandleTypeDef htim4;
 TIM_HandleTypeDef htim4;
 TIM_HandleTypeDef htim6;
 TIM_HandleTypeDef htim6;
+DMA_HandleTypeDef hdma_tim3_ch4_up;
 
 
 UART_HandleTypeDef huart1;
 UART_HandleTypeDef huart1;
 DMA_HandleTypeDef hdma_usart1_rx;
 DMA_HandleTypeDef hdma_usart1_rx;
@@ -86,6 +88,41 @@ volatile struct {
 
 
 volatile uint32_t pulses = 0;
 volatile uint32_t pulses = 0;
 
 
+uint32_t Wave_LUT[] = {
+		0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,
+		0x98,0x9b,0x9e,0xa2,0xa5,0xa7,0xaa,0xad,
+		0xb0,0xb3,0xb6,0xb9,0xbc,0xbe,0xc1,0xc4,
+		0xc6,0xc9,0xcb,0xce,0xd0,0xd3,0xd5,0xd7,
+		0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,
+		0xea,0xeb,0xed,0xee,0xf0,0xf1,0xf3,0xf4,
+		0xf5,0xf6,0xf8,0xf9,0xfa,0xfa,0xfb,0xfc,
+		0xfd,0xfd,0xfe,0xfe,0xfe,0xff,0xff,0xff,
+		0xff,0xff,0xff,0xff,0xfe,0xfe,0xfe,0xfd,
+		0xfd,0xfc,0xfb,0xfa,0xfa,0xf9,0xf8,0xf6,
+		0xf5,0xf4,0xf3,0xf1,0xf0,0xee,0xed,0xeb,
+		0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
+		0xda,0xd7,0xd5,0xd3,0xd0,0xce,0xcb,0xc9,
+		0xc6,0xc4,0xc1,0xbe,0xbc,0xb9,0xb6,0xb3,
+		0xb0,0xad,0xaa,0xa7,0xa5,0xa2,0x9e,0x9b,
+		0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
+		0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,
+		0x67,0x64,0x61,0x5d,0x5a,0x58,0x55,0x52,
+		0x4f,0x4c,0x49,0x46,0x43,0x41,0x3e,0x3b,
+		0x39,0x36,0x34,0x31,0x2f,0x2c,0x2a,0x28,
+		0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,
+		0x15,0x14,0x12,0x11,0xf,0xe,0xc,0xb,
+		0xa,0x9,0x7,0x6,0x5,0x5,0x4,0x3,
+		0x2,0x2,0x1,0x1,0x1,0x0,0x0,0x0,
+		0x0,0x0,0x0,0x0,0x1,0x1,0x1,0x2,
+		0x2,0x3,0x4,0x5,0x5,0x6,0x7,0x9,
+		0xa,0xb,0xc,0xe,0xf,0x11,0x12,0x14,
+		0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
+		0x25,0x28,0x2a,0x2c,0x2f,0x31,0x34,0x36,
+		0x39,0x3b,0x3e,0x41,0x43,0x46,0x49,0x4c,
+		0x4f,0x52,0x55,0x58,0x5a,0x5d,0x61,0x64,
+		0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c,
+};
+
 /* USER CODE END PV */
 /* USER CODE END PV */
 
 
 /* Private function prototypes -----------------------------------------------*/
 /* Private function prototypes -----------------------------------------------*/
@@ -100,7 +137,6 @@ static void MX_USART1_UART_Init(void);
 static void MX_TIM6_Init(void);
 static void MX_TIM6_Init(void);
 static void MX_TIM4_Init(void);
 static void MX_TIM4_Init(void);
 static void MX_TIM3_Init(void);
 static void MX_TIM3_Init(void);
-static void MX_TIM2_Init(void);
 static void MX_I2C1_Init(void);
 static void MX_I2C1_Init(void);
 static void MX_CRC_Init(void);
 static void MX_CRC_Init(void);
 /* USER CODE BEGIN PFP */
 /* USER CODE BEGIN PFP */
@@ -160,8 +196,9 @@ void SwitchPeriphSupply(OnOff_t state)
 
 
 void Beep()
 void Beep()
 {
 {
-    HAL_TIM_Base_Stop(&htim2);
-    HAL_TIM_Base_Start(&htim2);
+	//HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_4, Wave_LUT, sizeof(Wave_LUT) / 4);
+    //HAL_TIM_Base_Stop(&htim2);
+    //HAL_TIM_Base_Start(&htim2);
 }
 }
 
 
 /* USER CODE END 0 */
 /* USER CODE END 0 */
@@ -202,7 +239,6 @@ int main(void)
   MX_TIM6_Init();
   MX_TIM6_Init();
   MX_TIM4_Init();
   MX_TIM4_Init();
   MX_TIM3_Init();
   MX_TIM3_Init();
-  MX_TIM2_Init();
   MX_I2C1_Init();
   MX_I2C1_Init();
   MX_USB_DEVICE_Init();
   MX_USB_DEVICE_Init();
   MX_CRC_Init();
   MX_CRC_Init();
@@ -232,7 +268,7 @@ int main(void)
   HAL_COMP_Start_IT(&hcomp2);
   HAL_COMP_Start_IT(&hcomp2);
   HAL_TIM_Base_Start_IT(&htim6);
   HAL_TIM_Base_Start_IT(&htim6);
   HAL_TIM_Base_Start_IT(&htim4);
   HAL_TIM_Base_Start_IT(&htim4);
-  HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_4);
+  //HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_4);
 
 
   UARTIO_Init(&huart1);
   UARTIO_Init(&huart1);
 
 
@@ -242,6 +278,8 @@ int main(void)
   /* USER CODE BEGIN WHILE */
   /* USER CODE BEGIN WHILE */
   char tempstr[32];
   char tempstr[32];
 
 
+  uint8_t imgbuffer[172];
+
   uint32_t pps = 0;
   uint32_t pps = 0;
   uint32_t pps_avg = 0;
   uint32_t pps_avg = 0;
   uint32_t total_dose = 0;
   uint32_t total_dose = 0;
@@ -249,8 +287,31 @@ int main(void)
   uint8_t window = 0;
   uint8_t window = 0;
 
 
   Flags.Exti = 0;
   Flags.Exti = 0;
+
+  //ssd1306_SetContrast(60);
+  //ssd1306_TestAll();
+  Beep();
+
   while (1)
   while (1)
   {
   {
+	  if(window == 1)
+	  {
+		  if(COMM_Receive(imgbuffer, COMM_USB, 200) == COMM_OK)
+		  {
+			  ssd1306_Fill(Black);
+			  for(int y = 0; y < 32; y++)
+			  {
+				  for(int x = 0; x < 43; x++)
+				  {
+					  uint16_t pos = (y * 43) + x;
+					  if(imgbuffer[pos / 8] & (1 << (pos % 8)))
+						  ssd1306_DrawPixel(x + 43, y, White);
+				  }
+			  }
+			  ssd1306_UpdateScreen();
+		  }
+	  }
+
     if(Flags.Exti)
     if(Flags.Exti)
     {
     {
     	uint16_t btn = Flags.Exti;
     	uint16_t btn = Flags.Exti;
@@ -258,11 +319,21 @@ int main(void)
     	if(btn == GPIO_PIN_15)
     	if(btn == GPIO_PIN_15)
     	{
     	{
     		window ^= 1;
     		window ^= 1;
+    		ssd1306_Fill(Black);
+    		ssd1306_UpdateScreen();
     	}
     	}
     }
     }
 
 
     if(Flags.Tim6)
     if(Flags.Tim6)
     {
     {
+      int rcvlen = CDC_ReadData(imgbuffer, sizeof(imgbuffer));
+      if(rcvlen > 0)
+    	  UARTIO_Transmit(imgbuffer, rcvlen);
+
+      rcvlen = UARTIO_Receive(imgbuffer, sizeof(imgbuffer));
+      if(rcvlen > 0)
+    	  CDC_WriteData(imgbuffer, rcvlen);
+
       Flags.Tim6 = 0;
       Flags.Tim6 = 0;
       HAL_ADC_Stop_DMA(&hadc);
       HAL_ADC_Stop_DMA(&hadc);
 
 
@@ -295,28 +366,26 @@ int main(void)
         pps_avg = (pps + pps_avg) / 2;
         pps_avg = (pps + pps_avg) / 2;
       }
       }
 
 
-      ssd1306_Fill(Black);
-	  ssd1306_DrawRectangle(0, 0, SSD1306_WIDTH - 1, SSD1306_HEIGHT - 1, White);
-
       if(window == 0)
       if(window == 0)
       {
       {
+    	  ssd1306_DrawRectangle(0, 0, SSD1306_WIDTH - 1, SSD1306_HEIGHT - 1, White);
     	  ssd1306_SetCursor(1, 7);
     	  ssd1306_SetCursor(1, 7);
 		  sprintf(tempstr, "%5.2f uSv/h", (float)pps_avg / 100.0);
 		  sprintf(tempstr, "%5.2f uSv/h", (float)pps_avg / 100.0);
 		  ssd1306_WriteString(tempstr, Font_11x18, White);
 		  ssd1306_WriteString(tempstr, Font_11x18, White);
+		  ssd1306_UpdateScreen();
       }
       }
       else
       else
       {
       {
-    	  ssd1306_SetCursor(2, 2);
-    	  ssd1306_WriteString(tempstr, Font_7x10, White);
+    	  //ssd1306_SetCursor(2, 2);
+    	  //ssd1306_WriteString(tempstr, Font_7x10, White);
       }
       }
-      ssd1306_UpdateScreen();
 
 
       SetDACVoltage(DAC_CHANNEL_1, SIPM_BIAS_VOLTAGE_OFFSET);
       SetDACVoltage(DAC_CHANNEL_1, SIPM_BIAS_VOLTAGE_OFFSET);
       SetDACVoltage(DAC_CHANNEL_2, COMP_THRESHOLD_VOLTAGE);
       SetDACVoltage(DAC_CHANNEL_2, COMP_THRESHOLD_VOLTAGE);
       HAL_ADC_Start_DMA(&hadc, (uint32_t*)&adc_meas, 3);
       HAL_ADC_Start_DMA(&hadc, (uint32_t*)&adc_meas, 3);
 
 
-      sprintf(tempstr, "%5.2f uSv/h\r\n", (float)pps_avg / 100.0);
-      COMM_Transmit((uint8_t*)&pps_avg, sizeof(pps_avg), COMM_USB);
+      //sprintf(tempstr, "%5.2f uSv/h\r\n", (float)pps_avg / 100.0);
+      //COMM_Transmit((uint8_t*)&pps_avg, sizeof(pps_avg), COMM_USB);
     }
     }
 
 
     if(pulses > 50 || Flags.Tim4 > 3)
     if(pulses > 50 || Flags.Tim4 > 3)
@@ -331,7 +400,8 @@ int main(void)
       htim4.Instance->CNT = 0;
       htim4.Instance->CNT = 0;
       HAL_TIM_Base_Start(&htim4);
       HAL_TIM_Base_Start(&htim4);
 
 
-      Beep();
+      //if(window == 0)
+    	//  Beep();
     }
     }
 
 
     /* USER CODE END WHILE */
     /* USER CODE END WHILE */
@@ -623,55 +693,6 @@ static void MX_I2C2_Init(void)
 
 
 }
 }
 
 
-/**
-  * @brief TIM2 Initialization Function
-  * @param None
-  * @retval None
-  */
-static void MX_TIM2_Init(void)
-{
-
-  /* USER CODE BEGIN TIM2_Init 0 */
-
-  /* USER CODE END TIM2_Init 0 */
-
-  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
-  TIM_MasterConfigTypeDef sMasterConfig = {0};
-
-  /* USER CODE BEGIN TIM2_Init 1 */
-
-  /* USER CODE END TIM2_Init 1 */
-  htim2.Instance = TIM2;
-  htim2.Init.Prescaler = 600;
-  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
-  htim2.Init.Period = 1000;
-  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
-  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
-  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
-  {
-    Error_Handler();
-  }
-  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
-  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
-  {
-    Error_Handler();
-  }
-  if (HAL_TIM_OnePulse_Init(&htim2, TIM_OPMODE_SINGLE) != HAL_OK)
-  {
-    Error_Handler();
-  }
-  sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
-  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
-  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
-  {
-    Error_Handler();
-  }
-  /* USER CODE BEGIN TIM2_Init 2 */
-
-  /* USER CODE END TIM2_Init 2 */
-
-}
-
 /**
 /**
   * @brief TIM3 Initialization Function
   * @brief TIM3 Initialization Function
   * @param None
   * @param None
@@ -685,7 +706,6 @@ static void MX_TIM3_Init(void)
   /* USER CODE END TIM3_Init 0 */
   /* USER CODE END TIM3_Init 0 */
 
 
   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
-  TIM_SlaveConfigTypeDef sSlaveConfig = {0};
   TIM_MasterConfigTypeDef sMasterConfig = {0};
   TIM_MasterConfigTypeDef sMasterConfig = {0};
   TIM_OC_InitTypeDef sConfigOC = {0};
   TIM_OC_InitTypeDef sConfigOC = {0};
 
 
@@ -693,9 +713,9 @@ static void MX_TIM3_Init(void)
 
 
   /* USER CODE END TIM3_Init 1 */
   /* USER CODE END TIM3_Init 1 */
   htim3.Instance = TIM3;
   htim3.Instance = TIM3;
-  htim3.Init.Prescaler = 48;
+  htim3.Init.Prescaler = 0;
   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
-  htim3.Init.Period = 1000;
+  htim3.Init.Period = 255;
   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
   htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
   htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
   if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
   if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
@@ -711,12 +731,6 @@ static void MX_TIM3_Init(void)
   {
   {
     Error_Handler();
     Error_Handler();
   }
   }
-  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
-  sSlaveConfig.InputTrigger = TIM_TS_ITR1;
-  if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
-  {
-    Error_Handler();
-  }
   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
@@ -867,6 +881,9 @@ static void MX_DMA_Init(void)
   /* DMA1_Channel1_IRQn interrupt configuration */
   /* DMA1_Channel1_IRQn interrupt configuration */
   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
+  /* DMA1_Channel3_IRQn interrupt configuration */
+  HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
+  HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
   /* DMA1_Channel4_IRQn interrupt configuration */
   /* DMA1_Channel4_IRQn interrupt configuration */
   HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
   HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
   HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
@@ -957,7 +974,13 @@ void Error_Handler(void)
 {
 {
   /* USER CODE BEGIN Error_Handler_Debug */
   /* USER CODE BEGIN Error_Handler_Debug */
   /* User can add his own implementation to report the HAL error return state */
   /* User can add his own implementation to report the HAL error return state */
-  __disable_irq();
+  //__disable_irq();
+
+  ssd1306_Fill(Black);
+  ssd1306_SetCursor(1, 1);
+  ssd1306_WriteString("HAL Fault", Font_11x18, White);
+  ssd1306_UpdateScreen();
+
   while (1)
   while (1)
   {
   {
   }
   }

+ 90 - 0
stm32workspace/dosim-v1/Core/Src/menu.c

@@ -0,0 +1,90 @@
+/*
+ * menu.c
+ *
+ *  Created on: Jan 26, 2022
+ *      Author: radioman
+ */
+
+/*
+Menu Structure:
+
+Dose rate measure
+
+Total dose measure
+	Reset total dose
+
+Time
+	Reset timer
+
+Settings:
+	Measurement settings:
+		Reset total dose
+
+	Sound settings:
+		Sound volume
+		Sound type
+		Buttons volume
+		Back
+
+	Alarm settings:
+		Alarm at dose rate
+		Alarm at dose
+		Alarm sound type
+		Alarm volume
+		Back
+
+	Energy-saving settings
+		Display auto-shutdown
+		Device auto-shutdown
+		Back
+
+About
+*/
+
+#include "menu.h"
+#include "ssd1306.h"
+
+static menu_entry_td menu[] = { \
+	{ .title = "Dose rate measure",  .id = 1, .parent_id = 0 },
+	{ .title = "Total dose measure", .id = 2, .parent_id = 0 },
+	{ .title = "Up Time",            .id = 3, .parent_id = 0 },
+	{ .title = "Settings",			 .id = 4, .parent_id = 0 },
+		{ .title = "Sound",				.id = 5, .parent_id = 3 },
+			{ .title = "Sound volume",		 .id = 6, .parent_id = 4 },
+			{ .title = "Sound type",		 .id = 7, .parent_id = 4 },
+			{ .title = "Button sound",		 .id = 8, .parent_id = 4 },
+			{ .title = "Back",				 .id = 9, .parent_id = 4 },
+		{ .title = "Alarm",				.id = 10, .parent_id = 3 },
+			{ .title = "Alarm at dose rate", .id = 11, .parent_id = 9 },
+			{ .title = "Alarm at dose",		 .id = 12, .parent_id = 9 },
+			{ .title = "Alarm sound type",	 .id = 13, .parent_id = 9 },
+			{ .title = "Alarm volume",		 .id = 14, .parent_id = 9 },
+			{ .title = "Back",				 .id = 15, .parent_id = 9 },
+		{ .title = "Energy-saving",		.id = 16, .parent_id = 3 },
+			{ .title = "Display auto-shutdown", .id = 17, .parent_id = 15 },
+			{ .title = "Device auto-shutdown",	.id = 18, .parent_id = 15 },
+			{ .title = "Back",					.id = 19, .parent_id = 15 },
+	{ .title = "Information",		.id = 20, .parent_id = 0 }
+};
+
+menu_entry_td* GetMenuEntry(uint8_t id)
+{
+	return &menu[id];
+}
+
+void MenuShow(uint8_t id)
+{
+	int x = 0, y = 0;
+	SSD1306_COLOR color = White;
+	menu[1].selected = 1;
+	for(int i = 0; i < (sizeof(menu) / sizeof(menu_entry_td)); i++)
+	{
+		if(menu[i].parent_id == 0)
+		{
+			ssd1306_SetCursor(x, y);
+			(menu[i].selected) ? (color = Black) : (color = White);
+			ssd1306_WriteString(menu[i].title, Font_7x10, color);
+			y += 11;
+		}
+	}
+}

+ 29 - 24
stm32workspace/dosim-v1/Core/Src/stm32l1xx_hal_msp.c

@@ -26,6 +26,8 @@
 /* USER CODE END Includes */
 /* USER CODE END Includes */
 extern DMA_HandleTypeDef hdma_adc;
 extern DMA_HandleTypeDef hdma_adc;
 
 
+extern DMA_HandleTypeDef hdma_tim3_ch4_up;
+
 extern DMA_HandleTypeDef hdma_usart1_rx;
 extern DMA_HandleTypeDef hdma_usart1_rx;
 
 
 extern DMA_HandleTypeDef hdma_usart1_tx;
 extern DMA_HandleTypeDef hdma_usart1_tx;
@@ -453,24 +455,34 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
 */
 */
 void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
 void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
 {
 {
-  if(htim_base->Instance==TIM2)
-  {
-  /* USER CODE BEGIN TIM2_MspInit 0 */
-
-  /* USER CODE END TIM2_MspInit 0 */
-    /* Peripheral clock enable */
-    __HAL_RCC_TIM2_CLK_ENABLE();
-  /* USER CODE BEGIN TIM2_MspInit 1 */
-
-  /* USER CODE END TIM2_MspInit 1 */
-  }
-  else if(htim_base->Instance==TIM3)
+  if(htim_base->Instance==TIM3)
   {
   {
   /* USER CODE BEGIN TIM3_MspInit 0 */
   /* USER CODE BEGIN TIM3_MspInit 0 */
 
 
   /* USER CODE END TIM3_MspInit 0 */
   /* USER CODE END TIM3_MspInit 0 */
     /* Peripheral clock enable */
     /* Peripheral clock enable */
     __HAL_RCC_TIM3_CLK_ENABLE();
     __HAL_RCC_TIM3_CLK_ENABLE();
+
+    /* TIM3 DMA Init */
+    /* TIM3_CH4_UP Init */
+    hdma_tim3_ch4_up.Instance = DMA1_Channel3;
+    hdma_tim3_ch4_up.Init.Direction = DMA_MEMORY_TO_PERIPH;
+    hdma_tim3_ch4_up.Init.PeriphInc = DMA_PINC_DISABLE;
+    hdma_tim3_ch4_up.Init.MemInc = DMA_MINC_ENABLE;
+    hdma_tim3_ch4_up.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
+    hdma_tim3_ch4_up.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
+    hdma_tim3_ch4_up.Init.Mode = DMA_CIRCULAR;
+    hdma_tim3_ch4_up.Init.Priority = DMA_PRIORITY_LOW;
+    if (HAL_DMA_Init(&hdma_tim3_ch4_up) != HAL_OK)
+    {
+      Error_Handler();
+    }
+
+    /* Several peripheral DMA handle pointers point to the same DMA handle.
+     Be aware that there is only one channel to perform all the requested DMAs. */
+    __HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_CC4],hdma_tim3_ch4_up);
+    __HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_UPDATE],hdma_tim3_ch4_up);
+
   /* USER CODE BEGIN TIM3_MspInit 1 */
   /* USER CODE BEGIN TIM3_MspInit 1 */
 
 
   /* USER CODE END TIM3_MspInit 1 */
   /* USER CODE END TIM3_MspInit 1 */
@@ -540,24 +552,17 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
 */
 */
 void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
 void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
 {
 {
-  if(htim_base->Instance==TIM2)
-  {
-  /* USER CODE BEGIN TIM2_MspDeInit 0 */
-
-  /* USER CODE END TIM2_MspDeInit 0 */
-    /* Peripheral clock disable */
-    __HAL_RCC_TIM2_CLK_DISABLE();
-  /* USER CODE BEGIN TIM2_MspDeInit 1 */
-
-  /* USER CODE END TIM2_MspDeInit 1 */
-  }
-  else if(htim_base->Instance==TIM3)
+  if(htim_base->Instance==TIM3)
   {
   {
   /* USER CODE BEGIN TIM3_MspDeInit 0 */
   /* USER CODE BEGIN TIM3_MspDeInit 0 */
 
 
   /* USER CODE END TIM3_MspDeInit 0 */
   /* USER CODE END TIM3_MspDeInit 0 */
     /* Peripheral clock disable */
     /* Peripheral clock disable */
     __HAL_RCC_TIM3_CLK_DISABLE();
     __HAL_RCC_TIM3_CLK_DISABLE();
+
+    /* TIM3 DMA DeInit */
+    HAL_DMA_DeInit(htim_base->hdma[TIM_DMA_ID_CC4]);
+    HAL_DMA_DeInit(htim_base->hdma[TIM_DMA_ID_UPDATE]);
   /* USER CODE BEGIN TIM3_MspDeInit 1 */
   /* USER CODE BEGIN TIM3_MspDeInit 1 */
 
 
   /* USER CODE END TIM3_MspDeInit 1 */
   /* USER CODE END TIM3_MspDeInit 1 */

+ 15 - 0
stm32workspace/dosim-v1/Core/Src/stm32l1xx_it.c

@@ -59,6 +59,7 @@
 extern PCD_HandleTypeDef hpcd_USB_FS;
 extern PCD_HandleTypeDef hpcd_USB_FS;
 extern DMA_HandleTypeDef hdma_adc;
 extern DMA_HandleTypeDef hdma_adc;
 extern COMP_HandleTypeDef hcomp2;
 extern COMP_HandleTypeDef hcomp2;
+extern DMA_HandleTypeDef hdma_tim3_ch4_up;
 extern TIM_HandleTypeDef htim4;
 extern TIM_HandleTypeDef htim4;
 extern TIM_HandleTypeDef htim6;
 extern TIM_HandleTypeDef htim6;
 extern DMA_HandleTypeDef hdma_usart1_rx;
 extern DMA_HandleTypeDef hdma_usart1_rx;
@@ -220,6 +221,20 @@ void DMA1_Channel1_IRQHandler(void)
   /* USER CODE END DMA1_Channel1_IRQn 1 */
   /* USER CODE END DMA1_Channel1_IRQn 1 */
 }
 }
 
 
+/**
+  * @brief This function handles DMA1 channel3 global interrupt.
+  */
+void DMA1_Channel3_IRQHandler(void)
+{
+  /* USER CODE BEGIN DMA1_Channel3_IRQn 0 */
+
+  /* USER CODE END DMA1_Channel3_IRQn 0 */
+  HAL_DMA_IRQHandler(&hdma_tim3_ch4_up);
+  /* USER CODE BEGIN DMA1_Channel3_IRQn 1 */
+
+  /* USER CODE END DMA1_Channel3_IRQn 1 */
+}
+
 /**
 /**
   * @brief This function handles DMA1 channel4 global interrupt.
   * @brief This function handles DMA1 channel4 global interrupt.
   */
   */

+ 47 - 28
stm32workspace/dosim-v1/Core/Src/uartio.c

@@ -12,58 +12,77 @@
 static uint8_t uart_rx_buffer[UART_RX_BUF_SIZE];
 static uint8_t uart_rx_buffer[UART_RX_BUF_SIZE];
 static uint8_t uart_tx_buffer[UART_TX_BUF_SIZE];
 static uint8_t uart_tx_buffer[UART_TX_BUF_SIZE];
 
 
-static uint32_t rd_ptr;
+static uint32_t tail;
 static uint8_t tx_cplt = 1;
 static uint8_t tx_cplt = 1;
+static UART_HandleTypeDef *uart_handle;
 
 
 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
 {
 {
-	tx_cplt = 1;
+	if(huart == uart_handle)
+		tx_cplt = 1;
 }
 }
 
 
 void UARTIO_Init(UART_HandleTypeDef *huart)
 void UARTIO_Init(UART_HandleTypeDef *huart)
 {
 {
-	HAL_UART_Receive_DMA(huart, uart_rx_buffer, UART_RX_BUF_SIZE);
-	rd_ptr = 0;
+	uart_handle = huart;
+	tail = 0;
+	HAL_UART_Receive_DMA(uart_handle, uart_rx_buffer, UART_RX_BUF_SIZE);
 }
 }
 
 
-uint32_t UARTIO_DataAvail(UART_HandleTypeDef *huart)
+uint32_t UARTIO_DataAvailable(void)
 {
 {
-	if((UART_RX_BUF_SIZE - huart->hdmarx->Instance->CNDTR) >= rd_ptr)
-		return (UART_RX_BUF_SIZE - huart->hdmarx->Instance->CNDTR) - rd_ptr;
-	else
-		return (2 * UART_RX_BUF_SIZE) - rd_ptr - huart->hdmarx->Instance->CNDTR;
+	uint32_t head = UART_RX_BUF_SIZE - uart_handle->hdmarx->Instance->CNDTR;
 
 
+	if(head >= tail)
+		return (head - tail);
+	else
+		return (UART_RX_BUF_SIZE - tail + head);
 }
 }
 
 
-uint32_t UARTIO_Receive(UART_HandleTypeDef *huart, uint8_t *buf)
+int UARTIO_Receive(uint8_t *buf, uint32_t len)
 {
 {
-	uint32_t dma_ptr = (UART_RX_BUF_SIZE - huart->hdmarx->Instance->CNDTR);
-	if(dma_ptr > rd_ptr)
-	{
-		uint32_t data_avail = dma_ptr - rd_ptr;
-		memcpy(&buf[0], &uart_rx_buffer[rd_ptr], data_avail);
-		rd_ptr = dma_ptr;
-		return (data_avail);
-	}
-	else if(dma_ptr < rd_ptr)
+	//uint32_t dma_ptr = (UART_RX_BUF_SIZE - huart->hdmarx->Instance->CNDTR);
+	uint32_t head = UART_RX_BUF_SIZE - uart_handle->hdmarx->Instance->CNDTR;
+	if(tail == head)
+		return (-1);
+
+	if(tail > head)
 	{
 	{
-		uint32_t blk1_sz = UART_RX_BUF_SIZE - rd_ptr;
-		memcpy(&buf[0], &uart_rx_buffer[rd_ptr], blk1_sz);
-		memcpy(&buf[blk1_sz], &uart_rx_buffer[0], dma_ptr);
-		rd_ptr = dma_ptr;
-		return (blk1_sz + dma_ptr);
+		uint32_t cap_avail = UART_RX_BUF_SIZE - tail + head;
+		if((UART_RX_BUF_SIZE - tail) >= len)
+		{
+			memcpy(buf, &uart_rx_buffer[tail], len);
+			tail = (tail + len) % UART_RX_BUF_SIZE;
+		}
+		else
+		{
+			if(len > cap_avail)
+				len = cap_avail;
+			uint32_t blk1_sz = UART_RX_BUF_SIZE - tail;
+			memcpy(buf, &uart_rx_buffer[tail], blk1_sz);
+			tail = 0;
+			memcpy(&buf[blk1_sz], &uart_rx_buffer[tail], (len - blk1_sz));
+			tail = (len - blk1_sz) % UART_RX_BUF_SIZE;
+		}
 	}
 	}
 	else
 	else
 	{
 	{
-		return 0;
+		if(len > (head - tail))
+			len = head - tail;
+		memcpy(buf, &uart_rx_buffer[tail], len);
+		tail = (tail + len) % UART_RX_BUF_SIZE;
 	}
 	}
+
+	return (len);
 }
 }
 
 
-uint32_t UARTIO_Transmit(UART_HandleTypeDef *huart, uint8_t *buf, uint32_t len)
+int UARTIO_Transmit(uint8_t *buf, uint32_t len)
 {
 {
+	if(!tx_cplt)
+		return (-1);
+
 	memcpy(uart_tx_buffer, buf, len);
 	memcpy(uart_tx_buffer, buf, len);
-	while(!tx_cplt);
-	HAL_UART_Transmit_DMA(huart, uart_tx_buffer, len);
+	HAL_UART_Transmit_DMA(uart_handle, uart_tx_buffer, len);
 	tx_cplt = 0;
 	tx_cplt = 0;
 	return len;
 	return len;
 }
 }

+ 17 - 4
stm32workspace/dosim-v1/USB_DEVICE/App/usbd_cdc_if.c

@@ -76,6 +76,7 @@
 
 
 /* USER CODE BEGIN PRIVATE_MACRO */
 /* USER CODE BEGIN PRIVATE_MACRO */
 #define RX_FIFO_SIZE	500
 #define RX_FIFO_SIZE	500
+
 /* USER CODE END PRIVATE_MACRO */
 /* USER CODE END PRIVATE_MACRO */
 
 
 /**
 /**
@@ -97,6 +98,7 @@ uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
 /* USER CODE BEGIN PRIVATE_VARIABLES */
 /* USER CODE BEGIN PRIVATE_VARIABLES */
 FIFO_HandleTypeDef RxFIFO;
 FIFO_HandleTypeDef RxFIFO;
 uint8_t RxFIFOBuffer[RX_FIFO_SIZE];
 uint8_t RxFIFOBuffer[RX_FIFO_SIZE];
+
 /* USER CODE END PRIVATE_VARIABLES */
 /* USER CODE END PRIVATE_VARIABLES */
 
 
 /**
 /**
@@ -289,10 +291,13 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
   uint8_t result = USBD_OK;
   uint8_t result = USBD_OK;
   /* USER CODE BEGIN 7 */
   /* USER CODE BEGIN 7 */
   USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
   USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
+
   if (hcdc->TxState != 0){
   if (hcdc->TxState != 0){
-    return USBD_BUSY;
+     return USBD_BUSY;
   }
   }
-  USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
+
+  memcpy(UserTxBufferFS, Buf, Len);
+  USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, Len);
   result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
   result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
   /* USER CODE END 7 */
   /* USER CODE END 7 */
   return result;
   return result;
@@ -300,9 +305,17 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
 
 
 /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
 /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
 
 
-uint8_t CDC_DataAvailable(void)
+uint32_t CDC_DataAvailable(void)
+{
+	return fifo_size(&RxFIFO);
+}
+
+int CDC_WriteData(uint8_t* buf, uint32_t len)
 {
 {
-	return (fifo_state(&RxFIFO) != FIFO_EMPTY);
+	if(CDC_Transmit_FS(buf, len) == USBD_OK)
+		return len;
+	else
+		return (-1);
 }
 }
 
 
 int CDC_ReadData(uint8_t* buf, uint32_t len)
 int CDC_ReadData(uint8_t* buf, uint32_t len)

+ 2 - 1
stm32workspace/dosim-v1/USB_DEVICE/App/usbd_cdc_if.h

@@ -109,8 +109,9 @@ extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
 uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
 uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
 
 
 /* USER CODE BEGIN EXPORTED_FUNCTIONS */
 /* USER CODE BEGIN EXPORTED_FUNCTIONS */
-uint8_t CDC_DataAvailable(void);
+uint32_t CDC_DataAvailable(void);
 int CDC_ReadData(uint8_t *buf, uint32_t len);
 int CDC_ReadData(uint8_t *buf, uint32_t len);
+int CDC_WriteData(uint8_t *buf, uint32_t len);
 /* USER CODE END EXPORTED_FUNCTIONS */
 /* USER CODE END EXPORTED_FUNCTIONS */
 
 
 /**
 /**

+ 27 - 36
stm32workspace/dosim-v1/dosim-v1.ioc

@@ -33,7 +33,17 @@ Dma.ADC.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignm
 Dma.Request0=ADC
 Dma.Request0=ADC
 Dma.Request1=USART1_RX
 Dma.Request1=USART1_RX
 Dma.Request2=USART1_TX
 Dma.Request2=USART1_TX
-Dma.RequestsNb=3
+Dma.Request3=TIM3_CH4/UP
+Dma.RequestsNb=4
+Dma.TIM3_CH4/UP.3.Direction=DMA_MEMORY_TO_PERIPH
+Dma.TIM3_CH4/UP.3.Instance=DMA1_Channel3
+Dma.TIM3_CH4/UP.3.MemDataAlignment=DMA_MDATAALIGN_HALFWORD
+Dma.TIM3_CH4/UP.3.MemInc=DMA_MINC_ENABLE
+Dma.TIM3_CH4/UP.3.Mode=DMA_CIRCULAR
+Dma.TIM3_CH4/UP.3.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD
+Dma.TIM3_CH4/UP.3.PeriphInc=DMA_PINC_DISABLE
+Dma.TIM3_CH4/UP.3.Priority=DMA_PRIORITY_LOW
+Dma.TIM3_CH4/UP.3.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority
 Dma.USART1_RX.1.Direction=DMA_PERIPH_TO_MEMORY
 Dma.USART1_RX.1.Direction=DMA_PERIPH_TO_MEMORY
 Dma.USART1_RX.1.Instance=DMA1_Channel5
 Dma.USART1_RX.1.Instance=DMA1_Channel5
 Dma.USART1_RX.1.MemDataAlignment=DMA_MDATAALIGN_BYTE
 Dma.USART1_RX.1.MemDataAlignment=DMA_MDATAALIGN_BYTE
@@ -62,13 +72,12 @@ KeepUserPlacement=false
 Mcu.Family=STM32L1
 Mcu.Family=STM32L1
 Mcu.IP0=ADC
 Mcu.IP0=ADC
 Mcu.IP1=COMP2
 Mcu.IP1=COMP2
-Mcu.IP10=TIM2
-Mcu.IP11=TIM3
-Mcu.IP12=TIM4
-Mcu.IP13=TIM6
-Mcu.IP14=USART1
-Mcu.IP15=USB
-Mcu.IP16=USB_DEVICE
+Mcu.IP10=TIM3
+Mcu.IP11=TIM4
+Mcu.IP12=TIM6
+Mcu.IP13=USART1
+Mcu.IP14=USB
+Mcu.IP15=USB_DEVICE
 Mcu.IP2=CRC
 Mcu.IP2=CRC
 Mcu.IP3=DAC
 Mcu.IP3=DAC
 Mcu.IP4=DMA
 Mcu.IP4=DMA
@@ -77,7 +86,7 @@ Mcu.IP6=I2C2
 Mcu.IP7=NVIC
 Mcu.IP7=NVIC
 Mcu.IP8=RCC
 Mcu.IP8=RCC
 Mcu.IP9=SYS
 Mcu.IP9=SYS
-Mcu.IPNb=17
+Mcu.IPNb=16
 Mcu.Name=STM32L151C(6-8-B)TxA
 Mcu.Name=STM32L151C(6-8-B)TxA
 Mcu.Package=LQFP48
 Mcu.Package=LQFP48
 Mcu.Pin0=PH0-OSC_IN
 Mcu.Pin0=PH0-OSC_IN
@@ -99,22 +108,18 @@ Mcu.Pin22=VP_ADC_Vref_Input
 Mcu.Pin23=VP_COMP2_VS_DACOUT2
 Mcu.Pin23=VP_COMP2_VS_DACOUT2
 Mcu.Pin24=VP_CRC_VS_CRC
 Mcu.Pin24=VP_CRC_VS_CRC
 Mcu.Pin25=VP_SYS_VS_Systick
 Mcu.Pin25=VP_SYS_VS_Systick
-Mcu.Pin26=VP_TIM2_VS_ClockSourceINT
-Mcu.Pin27=VP_TIM2_VS_OPM
-Mcu.Pin28=VP_TIM3_VS_ControllerModeGated
-Mcu.Pin29=VP_TIM3_VS_ClockSourceINT
+Mcu.Pin26=VP_TIM3_VS_ClockSourceINT
+Mcu.Pin27=VP_TIM4_VS_ClockSourceINT
+Mcu.Pin28=VP_TIM6_VS_ClockSourceINT
+Mcu.Pin29=VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS
 Mcu.Pin3=PA4
 Mcu.Pin3=PA4
-Mcu.Pin30=VP_TIM3_VS_ClockSourceITR
-Mcu.Pin31=VP_TIM4_VS_ClockSourceINT
-Mcu.Pin32=VP_TIM6_VS_ClockSourceINT
-Mcu.Pin33=VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS
 Mcu.Pin4=PA5
 Mcu.Pin4=PA5
 Mcu.Pin5=PA7
 Mcu.Pin5=PA7
 Mcu.Pin6=PB0
 Mcu.Pin6=PB0
 Mcu.Pin7=PB1
 Mcu.Pin7=PB1
 Mcu.Pin8=PB10
 Mcu.Pin8=PB10
 Mcu.Pin9=PB11
 Mcu.Pin9=PB11
-Mcu.PinsNb=34
+Mcu.PinsNb=30
 Mcu.ThirdPartyNb=0
 Mcu.ThirdPartyNb=0
 Mcu.UserConstants=
 Mcu.UserConstants=
 Mcu.UserName=STM32L151CBTxA
 Mcu.UserName=STM32L151CBTxA
@@ -123,6 +128,7 @@ MxDb.Version=DB.6.0.40
 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
 NVIC.COMP_IRQn=true\:0\:0\:false\:false\:true\:false\:true
 NVIC.COMP_IRQn=true\:0\:0\:false\:false\:true\:false\:true
 NVIC.DMA1_Channel1_IRQn=true\:0\:0\:false\:false\:true\:false\:true
 NVIC.DMA1_Channel1_IRQn=true\:0\:0\:false\:false\:true\:false\:true
+NVIC.DMA1_Channel3_IRQn=true\:0\:0\:false\:false\:true\:false\:true
 NVIC.DMA1_Channel4_IRQn=true\:0\:0\:false\:false\:true\:false\:true
 NVIC.DMA1_Channel4_IRQn=true\:0\:0\:false\:false\:true\:false\:true
 NVIC.DMA1_Channel5_IRQn=true\:0\:0\:false\:false\:true\:false\:true
 NVIC.DMA1_Channel5_IRQn=true\:0\:0\:false\:false\:true\:false\:true
 NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false
 NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false
@@ -220,7 +226,7 @@ ProjectManager.StackSize=0x400
 ProjectManager.TargetToolchain=STM32CubeIDE
 ProjectManager.TargetToolchain=STM32CubeIDE
 ProjectManager.ToolChainLocation=
 ProjectManager.ToolChainLocation=
 ProjectManager.UnderRoot=true
 ProjectManager.UnderRoot=true
-ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_DMA_Init-DMA-false-HAL-true,3-MX_GPIO_Init-GPIO-false-HAL-true,4-MX_ADC_Init-ADC-false-HAL-true,5-MX_DAC_Init-DAC-false-HAL-true,6-MX_I2C2_Init-I2C2-true-HAL-true,7-MX_COMP2_Init-COMP2-false-HAL-true,8-MX_USART1_UART_Init-USART1-false-HAL-true,9-MX_TIM6_Init-TIM6-false-HAL-true,10-MX_TIM4_Init-TIM4-false-HAL-true,11-MX_TIM3_Init-TIM3-false-HAL-true,12-MX_TIM2_Init-TIM2-false-HAL-true,13-MX_I2C1_Init-I2C1-false-HAL-true,14-MX_USB_DEVICE_Init-USB_DEVICE-false-HAL-false
+ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_DMA_Init-DMA-false-HAL-true,3-MX_GPIO_Init-GPIO-false-HAL-true,4-MX_ADC_Init-ADC-false-HAL-true,5-MX_DAC_Init-DAC-false-HAL-true,6-MX_I2C2_Init-I2C2-true-HAL-true,7-MX_COMP2_Init-COMP2-false-HAL-true,8-MX_USART1_UART_Init-USART1-false-HAL-true,9-MX_TIM6_Init-TIM6-false-HAL-true,10-MX_TIM4_Init-TIM4-false-HAL-true,11-MX_TIM3_Init-TIM3-false-HAL-true,12-MX_I2C1_Init-I2C1-false-HAL-true,13-MX_USB_DEVICE_Init-USB_DEVICE-false-HAL-false,14-MX_CRC_Init-CRC-false-HAL-true
 RCC.48MHZClocksFreq_Value=48000000
 RCC.48MHZClocksFreq_Value=48000000
 RCC.AHBFreq_Value=24000000
 RCC.AHBFreq_Value=24000000
 RCC.APB1Freq_Value=24000000
 RCC.APB1Freq_Value=24000000
@@ -259,20 +265,13 @@ SH.GPXTI15.0=GPIO_EXTI15
 SH.GPXTI15.ConfNb=1
 SH.GPXTI15.ConfNb=1
 SH.S_TIM3_CH4.0=TIM3_CH4,PWM Generation4 CH4
 SH.S_TIM3_CH4.0=TIM3_CH4,PWM Generation4 CH4
 SH.S_TIM3_CH4.ConfNb=1
 SH.S_TIM3_CH4.ConfNb=1
-TIM2.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_DISABLE
-TIM2.CounterMode=TIM_COUNTERMODE_UP
-TIM2.IPParameters=Prescaler,Period,TIM_MasterOutputTrigger,TIM_MasterSlaveMode,AutoReloadPreload,CounterMode
-TIM2.Period=1000
-TIM2.Prescaler=600
-TIM2.TIM_MasterOutputTrigger=TIM_TRGO_ENABLE
-TIM2.TIM_MasterSlaveMode=TIM_MASTERSLAVEMODE_DISABLE
 TIM3.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4
 TIM3.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4
 TIM3.IPParameters=Prescaler,Period,Channel-PWM Generation4 CH4,Pulse-PWM Generation4 CH4,OCMode_PWM-PWM Generation4 CH4,TIM_MasterSlaveMode,OC4Preload_PWM,OCPolarity_4
 TIM3.IPParameters=Prescaler,Period,Channel-PWM Generation4 CH4,Pulse-PWM Generation4 CH4,OCMode_PWM-PWM Generation4 CH4,TIM_MasterSlaveMode,OC4Preload_PWM,OCPolarity_4
 TIM3.OC4Preload_PWM=ENABLE
 TIM3.OC4Preload_PWM=ENABLE
 TIM3.OCMode_PWM-PWM\ Generation4\ CH4=TIM_OCMODE_PWM1
 TIM3.OCMode_PWM-PWM\ Generation4\ CH4=TIM_OCMODE_PWM1
 TIM3.OCPolarity_4=TIM_OCPOLARITY_HIGH
 TIM3.OCPolarity_4=TIM_OCPOLARITY_HIGH
-TIM3.Period=1000
-TIM3.Prescaler=48
+TIM3.Period=255
+TIM3.Prescaler=0
 TIM3.Pulse-PWM\ Generation4\ CH4=10
 TIM3.Pulse-PWM\ Generation4\ CH4=10
 TIM3.TIM_MasterSlaveMode=TIM_MASTERSLAVEMODE_DISABLE
 TIM3.TIM_MasterSlaveMode=TIM_MASTERSLAVEMODE_DISABLE
 TIM4.IPParameters=Prescaler,TIM_MasterOutputTrigger,Period
 TIM4.IPParameters=Prescaler,TIM_MasterOutputTrigger,Period
@@ -304,16 +303,8 @@ VP_CRC_VS_CRC.Mode=CRC_Activate
 VP_CRC_VS_CRC.Signal=CRC_VS_CRC
 VP_CRC_VS_CRC.Signal=CRC_VS_CRC
 VP_SYS_VS_Systick.Mode=SysTick
 VP_SYS_VS_Systick.Mode=SysTick
 VP_SYS_VS_Systick.Signal=SYS_VS_Systick
 VP_SYS_VS_Systick.Signal=SYS_VS_Systick
-VP_TIM2_VS_ClockSourceINT.Mode=Internal
-VP_TIM2_VS_ClockSourceINT.Signal=TIM2_VS_ClockSourceINT
-VP_TIM2_VS_OPM.Mode=OPM_bit
-VP_TIM2_VS_OPM.Signal=TIM2_VS_OPM
 VP_TIM3_VS_ClockSourceINT.Mode=Internal
 VP_TIM3_VS_ClockSourceINT.Mode=Internal
 VP_TIM3_VS_ClockSourceINT.Signal=TIM3_VS_ClockSourceINT
 VP_TIM3_VS_ClockSourceINT.Signal=TIM3_VS_ClockSourceINT
-VP_TIM3_VS_ClockSourceITR.Mode=TriggerSource_ITR1
-VP_TIM3_VS_ClockSourceITR.Signal=TIM3_VS_ClockSourceITR
-VP_TIM3_VS_ControllerModeGated.Mode=Gated Mode
-VP_TIM3_VS_ControllerModeGated.Signal=TIM3_VS_ControllerModeGated
 VP_TIM4_VS_ClockSourceINT.Mode=Internal
 VP_TIM4_VS_ClockSourceINT.Mode=Internal
 VP_TIM4_VS_ClockSourceINT.Signal=TIM4_VS_ClockSourceINT
 VP_TIM4_VS_ClockSourceINT.Signal=TIM4_VS_ClockSourceINT
 VP_TIM6_VS_ClockSourceINT.Mode=Enable_Timer
 VP_TIM6_VS_ClockSourceINT.Mode=Enable_Timer

+ 0 - 1
stm32workspace/dosimeter-fw/dosimeter-fw.ioc

@@ -392,4 +392,3 @@ VP_TIM4_VS_OPM.Signal=TIM4_VS_OPM
 VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS.Mode=CDC_FS
 VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS.Mode=CDC_FS
 VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS.Signal=USB_DEVICE_VS_USB_DEVICE_CDC_FS
 VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS.Signal=USB_DEVICE_VS_USB_DEVICE_CDC_FS
 board=custom
 board=custom
-isbadioc=false