This code needs to be modified a bit, as follows. @harris.shallcross @heikki728
1, the wakeup pin has to pull down the delay by about 3ms before sending data to the serial port
2, If you want faster data response, you can modify the connection interval a bit via the AT command. You can send this data to modify the parameters, do not immediately pull up after sending the data, also delay 1~3ms.
TTM:CIT-30ms\r\n\0
54 54 4D 3A 43 49 54 2D 33 30 6D 73 0D 0A 00
void U_TxC(char c){
huart2.Instance->TDR = (char)c;
while(!__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TC));
}
void U_TxS(const char *s, uint8_t null){
HAL_GPIO_WritePin(BL_WKP_GPIO_Port, BL_WKP_Pin, 0);
HAL_Delay(2);
while(*s){
U_TxC(*s);
s++;
}
if(null) U_TxC('\0');
HAL_GPIO_WritePin(BL_WKP_GPIO_Port, BL_WKP_Pin, 1);
}
void U_TxSL(const char *s, uint8_t l){
while(l--){
U_TxC(*s);
s++;
}
}
#define BL_BUFLEN 256
volatile char ubuf[BL_BUFLEN] = {0};
volatile uint8_t newbldata = 0;
volatile uint16_t cc = 0;
void UART_Handler(void){
char c;
if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE)){
c = (char)huart2.Instance->RDR;
if(cc<BL_BUFLEN){
ubuf[cc++] = c;
}
if(c=='\0'){
newbldata = 1;
cc = 0;
}
}
}
/* USER CODE END 0 */
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();
MX_ADC_Init();
MX_TIM21_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
//Wakeup bluetooth module
HAL_GPIO_WritePin(BL_PDN_GPIO_Port, BL_PDN_Pin, 1);
HAL_Delay(1);
HAL_GPIO_WritePin(BL_PDN_GPIO_Port, BL_PDN_Pin, 0);
HAL_Delay(1);
__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
__HAL_UART_ENABLE(&huart2);
newbldata = 0;
U_TxS("TTM:NAM-?\r\n", 1);
while(!newbldata);
newbldata = 0; //Breakpoint to check for received name
uint8_t isconnect = 0, n=0;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(newbldata){
if(strstr(ubuf, "TTM:CONNECT") != NULL){
isconnect = 1; //Successfully connected
}
else if(strstr(ubuf, "TTM:DISCONNECT") != NULL){
isconnect = 0; //Successfully disconnected
}
else{
n++;
}
newbldata = 0;
}
//When connected, send string every second
if(isconnect){
HAL_Delay(1000);
U_TxS("hi\r\n",1);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}