feature: state controlled by received data on CDC

This commit is contained in:
2022-12-12 17:11:37 +01:00
parent 3731265cde
commit ac391abcb0
3 changed files with 100 additions and 8 deletions

View File

@@ -38,6 +38,11 @@ extern "C" {
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* state enum type */
typedef enum {
run, read_spi, read_i2c, read_d28, read_d32, write_spi, write_i2c, write_d28, write_d32, invalid
} state;
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/

View File

@@ -23,7 +23,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usbd_cdc.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -47,6 +47,10 @@ SPI_HandleTypeDef hspi1;
/* USER CODE BEGIN PV */
state st = run;
uint16_t i2c_address = 0;
uint32_t limit_address = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -55,7 +59,7 @@ static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */
extern uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
@@ -96,14 +100,41 @@ int main(void)
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
uint8_t txbuff [1024] = {0};
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_Delay(1000);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
while (1) {
switch (st) {
case read_i2c:
st = run;
break;
case read_spi:
st = run;
break;
case read_d28:
st = run;
break;
case read_d32:
st = run;
break;
case write_i2c:
st = run;
break;
case write_spi:
st = run;
break;
case write_d28:
st = run;
break;
case write_d32:
st = run;
break;
default:;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */

View File

@@ -22,7 +22,7 @@
#include "usbd_cdc_if.h"
/* USER CODE BEGIN INCLUDE */
#include <stdio.h>
/* USER CODE END INCLUDE */
/* Private typedef -----------------------------------------------------------*/
@@ -31,7 +31,9 @@
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
extern state st;
extern uint16_t i2c_address;
extern uint32_t limit_address;
/* USER CODE END PV */
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
@@ -261,6 +263,60 @@ static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
/* USER CODE BEGIN 6 */
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
if (st == run) {
if (Buf[0] == 'r') {
switch (Buf[1]) {
case 'i':
st = read_i2c;
break;
case 's':
st = read_spi;
break;
case '2':
st = read_d28;
break;
case '3':
st = read_d32;
break;
default:
st = invalid;
return (USBD_OK);
}
}
else if (Buf[0] == 'w') {
switch (Buf[1]) {
case 'i':
st = write_i2c;
break;
case 's':
st = write_spi;
break;
case '2':
st = write_d28;
break;
case '3':
st = write_d32;
break;
default:
st = invalid;
return (USBD_OK);
}
}
else {
st = invalid;
return (USBD_OK);
}
if (sscanf((char *)Buf + 2, "%lx", &limit_address) != 1) {
st = invalid;
return (USBD_OK);
}
if (st == read_i2c || st == write_i2c) {
if (sscanf((char *)Buf + 2, "%*lx %hx", &i2c_address) != 1) {
st = invalid;
return (USBD_OK);
}
}
}
return (USBD_OK);
/* USER CODE END 6 */
}