2021-10-13 10:09 PM
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stm32h750xx.h"
#include "ESP32.h"
#include "stm32h7xx_hal_uart.h"
#include "string.h"
extern uint8_t USART2_RX_BUF[USART2_MAX_RECV_LEN];
char *uarttx="AT\n\r";
uint8_t uartrx[5];
int OP_STATE;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_NVIC_Init(void);
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
esp32_init();
/* Initialize interrupts */
MX_NVIC_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_UART_Receive_IT (&huart2, USART2_RX_BUF, 512);
int OP_STATE_1(uint8_t * cmd, char * ack, uint16_t waittime){
int res1 = 0;
int len = strlen((const char * ) cmd);
int acklen = strlen((const char * ) ack);
memset(USART2_RX_BUF, 0, USART2_MAX_RECV_LEN);
HAL_UART_Transmit( & huart2, cmd, len, 200);
HAL_UART_Receive_IT( & huart2, USART2_RX_BUF, USART2_MAX_RECV_LEN);
HAL_Delay(500);
if (esp32_check_ack(ack))
res1 = 1;
return res1;
}
int OP_STATE_2(uint8_t * cmd, char * ack, uint16_t waittime) {
int res2 = 0;
int len = strlen((const char * ) cmd);
int acklen = strlen((const char * ) ack);
memset(USART2_RX_BUF, 0, USART2_MAX_RECV_LEN);
HAL_UART_Transmit( & huart2, cmd, len, 200);
HAL_UART_Receive_IT( & huart2, USART2_RX_BUF, USART2_MAX_RECV_LEN);
if (esp32_check_ack(ack))
res2 = 1;
return res2;
}
int OP_STATE_3(uint8_t * cmd, char * ack, uint16_t waittime) {
int res3 = 0;
int len = strlen((const char * ) cmd);
int acklen = strlen((const char * ) ack);
memset(USART2_RX_BUF, 0, USART2_MAX_RECV_LEN);
HAL_UART_Transmit( & huart2, cmd, len, 200);
HAL_UART_Receive_IT( & huart2, USART2_RX_BUF, USART2_MAX_RECV_LEN);
if (esp32_check_ack(ack))
res3 = 1;
return res3;
}
while (1)
{
..........
}
static void MX_NVIC_Init(void)
{
/* USART2_IRQn interrupt configuration */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Transmit(&huart2, USART2_RX_BUF, 12, 500);
HAL_UART_Receive_IT(&huart2, USART2_RX_BUF, 12);
if(huart->Instance == USART2)
{
HAL_UART_Receive_IT (&huart2, USART2_RX_BUF,1);
HAL_UART_Transmit (&huart2, USART2_RX_BUF, 1, 10);
if(esp32_check_ack("OK")){
HAL_UART_Transmit (&huart2, USART2_RX_BUF, 1, 10);
}else{
}
2021-10-13 10:18 PM
You can’t define a function within another function. Move your function definitions outside of main(). This is super basic C knowledge.
2021-10-13 10:24 PM
oh.. I'm so bad at coding thx!
BTW, Can I use global variable in this code?
I want to make a buffer and put the return value in it.
2021-10-14 06:37 AM
Yes, you can use global variables.
It may be hard to make a complex program with limited knowledge. Perhaps start with some known working examples to build your skill set.