feat: update USART1 baud rate and implement IM1281B communication

This commit is contained in:
2025-03-29 22:37:31 +08:00
parent 98463aa9f1
commit 05668ca262
11 changed files with 1663 additions and 208 deletions

View File

@ -26,6 +26,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include "retarget.h"
#include "oled.h"
@ -41,7 +42,10 @@
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define K1 HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)
#define K2 HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin)
#define K3 HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin)
#define K4 HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin)
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
@ -55,6 +59,7 @@
extern unsigned short esp8266_cnt;
extern unsigned char esp8266_buf[128];
uint8_t* dataPtr;
uint8_t* UART1_RxData[40];
uint8_t UART2_RxData;
uint8_t rf_status;
@ -77,7 +82,68 @@ void SystemClock_Config(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void KeyScan(void)
{
if (K1 == GPIO_PIN_RESET)
{
HAL_Delay(5);
if (K1 == GPIO_PIN_RESET)
{
HAL_GPIO_TogglePin(RELAY1_GPIO_Port, RELAY1_Pin);
}
while (K1 == GPIO_PIN_RESET);
}
if (K2 == GPIO_PIN_RESET)
{
HAL_Delay(5);
if (K2 == GPIO_PIN_RESET)
{
HAL_GPIO_TogglePin(RELAY1_GPIO_Port, RELAY1_Pin);
}
while (K2 == GPIO_PIN_RESET);
}
if (K3 == GPIO_PIN_RESET)
{
HAL_Delay(5);
if (K3 == GPIO_PIN_RESET)
{
HAL_GPIO_TogglePin(RELAY1_GPIO_Port, RELAY1_Pin);
}
while (K3 == GPIO_PIN_RESET);
}
if (K4 == GPIO_PIN_RESET)
{
HAL_Delay(5);
if (K4 == GPIO_PIN_RESET)
{
HAL_GPIO_TogglePin(RELAY1_GPIO_Port, RELAY1_Pin);
}
while (K4 == GPIO_PIN_RESET);
}
}
void Display_IM1281B(void)
{
OLED_ShowString(0, 0, "Volt:", 12);
OLED_ShowString(0, 2, "Curr:", 12);
OLED_ShowString(0, 4, "Pwr: ", 12);
OLED_ShowString(0, 6, "Eng: ", 12);
char buffer[16];
snprintf(buffer, sizeof(buffer), "%.2fV", IM_Volt * 0.0001);
OLED_ShowString(40, 0, buffer, 12);
snprintf(buffer, sizeof(buffer), "%.2fA", IM_Curr * 0.0001);
OLED_ShowString(40, 2, buffer, 12);
snprintf(buffer, sizeof(buffer), "%.2fW", IM_Power * 0.0001);
OLED_ShowString(40, 4, buffer, 12);
snprintf(buffer, sizeof(buffer), "%.3fKWh", IM_Energy * 0.0001);
OLED_ShowString(40, 6, buffer, 12);
}
/* USER CODE END 0 */
/**
@ -124,7 +190,9 @@ int main(void)
OLED_ShowString(0,0,"initializing...",12);
HAL_UART_Receive_IT(&huart2, (uint8_t *) &UART2_RxData, 1);
HAL_UARTEx_ReceiveToIdle_IT(&huart1, UART1_RxData, 40);
HAL_UART_Receive_IT(&huart2, &UART2_RxData, 1);
ESP8266_Init();
while (OneNet_DevLink()) {}
OneNET_Subscribe();
@ -141,6 +209,7 @@ int main(void)
/* USER CODE BEGIN WHILE */
while (1)
{
KeyScan();
// if (!OneNet_DevLink())
// {
// OLED_ShowString(0,0,"OneNet online ",12);
@ -154,88 +223,62 @@ int main(void)
// OLED_ShowString(0,0,"OneNet offline ",12);
// HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
// }
IM_ReadFlag = 1; //抄读标志
IM_Read();
HAL_Delay(500);
OneNet_SendData();
HAL_Delay(1000);
ESP8266_Clear();
dataPtr = ESP8266_GetIPD(0);
if (dataPtr != NULL)
{
OneNet_RevPro(dataPtr);
}
Display_IM1281B();
rf_status = PCD_Request(PICC_REQALL, rf_card_type);
// dataPtr = ESP8266_GetIPD(0);
// if (dataPtr != NULL)
// {
// OneNet_RevPro(dataPtr);
// }
if (!rf_status) {
rf_status = PCD_ERR;
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
rf_status = PCD_AntiColl(rf_card_id);
}
if (!rf_status) {
rf_status = PCD_ERR;
// printf(SEP);
// printf("[i] Card Type: %02X %02X %02X\r\n", rf_card_type[0], rf_card_type[1], rf_card_type[2]);
// printf("[i] Card ID : %02X-%02X-%02X-%02X\r\n", rf_card_id[0], rf_card_id[1], rf_card_id[2], rf_card_id[3]);
OLED_ShowString(0, 2, "Typ:", 12);
uint8_t card_type_buffer[9];
snprintf(card_type_buffer, sizeof card_type_buffer, "%02X %02X %02X", rf_card_type[0], rf_card_type[1], rf_card_type[2]);
OLED_ShowString(38, 2, card_type_buffer, 12);
OLED_ShowString(0, 4, "ID :", 12);
uint8_t card_id_buffer[12];
snprintf(card_id_buffer, sizeof card_id_buffer, "%02X-%02X-%02X-%02X", rf_card_id[0], rf_card_id[1], rf_card_id[2], rf_card_id[3]);
OLED_ShowString(36, 4, card_id_buffer, 12);
rf_status = PCD_Select(rf_card_id);
}
if (!rf_status) {
rf_status = PCD_ERR;
rf_status = PCD_AuthState(PICC_AUTHENT1A, ADDR, KEY_A, rf_card_id);
// if(rf_status == PCD_OK)
// {
// printf("[*] Key A pass\r\n");
// }
// else
// {
// printf("[!] Key A verification failed\r\n");
// }
rf_status = PCD_AuthState(PICC_AUTHENT1B, ADDR, KEY_B, rf_card_id);
// if(rf_status == PCD_OK)
// {
// printf("[*] Key B pass\r\n");
// }
// else
// {
// printf("[!] Key B verification failed\r\n");
// }
}
if(rf_status == PCD_OK)
{
rf_status = PCD_ERR;
rf_status = PCD_ReadBlock(ADDR, rf_data);
// if(rf_status == PCD_OK)
// {
// printf("[+] Data in sector %d block %d: ", ADDR/4, ADDR%4);
// for(int i = 0; i < 16; i++)
// {
// printf("%02x", rf_data[i]);
// }
// printf("\r\n");
// }
// else
// {
// printf("Read card failed\r\n");
// }
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
HAL_Delay(3000);
}
// rf_status = PCD_Request(PICC_REQALL, rf_card_type);
//
// if (!rf_status) {
// rf_status = PCD_ERR;
// HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
// rf_status = PCD_AntiColl(rf_card_id);
// }
//
// if (!rf_status) {
// rf_status = PCD_ERR;
//
// OLED_ShowString(0, 2, "Typ:", 12);
// uint8_t card_type_buffer[9];
// snprintf(card_type_buffer, sizeof card_type_buffer, "%02X %02X %02X", rf_card_type[0], rf_card_type[1], rf_card_type[2]);
// OLED_ShowString(38, 2, card_type_buffer, 12);
//
// OLED_ShowString(0, 4, "ID :", 12);
// uint8_t card_id_buffer[12];
// snprintf(card_id_buffer, sizeof card_id_buffer, "%02X-%02X-%02X-%02X", rf_card_id[0], rf_card_id[1], rf_card_id[2], rf_card_id[3]);
// OLED_ShowString(36, 4, card_id_buffer, 12);
//
// rf_status = PCD_Select(rf_card_id);
// }
//
// if (!rf_status) {
// rf_status = PCD_ERR;
// rf_status = PCD_AuthState(PICC_AUTHENT1A, ADDR, KEY_A, rf_card_id);
// rf_status = PCD_AuthState(PICC_AUTHENT1B, ADDR, KEY_B, rf_card_id);
// }
//
// if(rf_status == PCD_OK)
// {
// rf_status = PCD_ERR;
// rf_status = PCD_ReadBlock(ADDR, rf_data);
//
// HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
// HAL_Delay(3000);
// }
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
@ -283,19 +326,37 @@ void SystemClock_Config(void)
}
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
// void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
// {
// if(GPIO_Pin == KEY1_Pin || GPIO_Pin == KEY2_Pin || GPIO_Pin == KEY3_Pin || GPIO_Pin == KEY4_Pin)
// {
// HAL_GPIO_TogglePin(RELAY1_GPIO_Port, RELAY1_Pin);
// if(HAL_GPIO_ReadPin(RELAY1_GPIO_Port, RELAY1_Pin) == GPIO_PIN_RESET)
// {
// OLED_ShowString(0, 6, "Relay1: ON ", 12);
// }
// else
// {
// OLED_ShowString(0, 6, "Relay1: OFF", 12);
// }
// }
// }
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if(GPIO_Pin == KEY1_Pin || GPIO_Pin == KEY2_Pin || GPIO_Pin == KEY3_Pin || GPIO_Pin == KEY4_Pin)
if (huart == &huart1)
{
HAL_GPIO_TogglePin(RELAY1_GPIO_Port, RELAY1_Pin);
if(HAL_GPIO_ReadPin(RELAY1_GPIO_Port, RELAY1_Pin) == GPIO_PIN_RESET)
// assign the received data to the IM_RxBuf
if (Size >= sizeof(UART1_RxData))
{
OLED_ShowString(0, 6, "Relay1: ON ", 12);
}
else
{
OLED_ShowString(0, 6, "Relay1: OFF", 12);
Size = sizeof(UART1_RxData) - 1;
}
// IM_RecvLen = Size;
memcpy(IM_RxBuf, UART1_RxData, Size);
IM_RecvLen = Size;
IM_RecvDone = 1;
IM_Analyze();
HAL_UARTEx_ReceiveToIdle_IT(&huart1, UART1_RxData, 40);
}
}
@ -324,6 +385,8 @@ void Error_Handler(void)
__disable_irq();
while (1)
{
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
HAL_Delay(200);
}
/* USER CODE END Error_Handler_Debug */
}