410 lines
11 KiB
C
410 lines
11 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2025 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
#include "i2c.h"
|
|
#include "spi.h"
|
|
#include "usart.h"
|
|
#include "gpio.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "retarget.h"
|
|
#include "oled.h"
|
|
#include "rc522.h"
|
|
#include "esp8266.h"
|
|
#include "OneNet.h"
|
|
/* USER CODE END Includes */
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* USER CODE BEGIN PTD */
|
|
|
|
/* USER CODE END PTD */
|
|
|
|
/* 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 -------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PM */
|
|
|
|
/* USER CODE END PM */
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
/* USER CODE BEGIN PV */
|
|
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;
|
|
uint8_t rf_card_type[3];
|
|
uint8_t rf_card_id[5];
|
|
uint8_t rf_data[16];
|
|
|
|
uint8_t ADDR = 0x01 * 4 + 0x03;
|
|
uint8_t KEY_A[6]= {0xff,0xff,0xff,0xff,0xff,0xff};
|
|
uint8_t KEY_B[6]= {0xff,0xff,0xff,0xff,0xff,0xff};
|
|
|
|
const char* SEP = "-------------------------\r\n";
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
/* USER CODE BEGIN PFP */
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* 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 */
|
|
|
|
/**
|
|
* @brief The application entry point.
|
|
* @retval int
|
|
*/
|
|
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_I2C1_Init();
|
|
MX_SPI1_Init();
|
|
MX_USART1_UART_Init();
|
|
MX_USART2_UART_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
// Retarget uart
|
|
RetargetInit(&huart1);
|
|
|
|
// Init OLED
|
|
OLED_Init();
|
|
OLED_Clear();
|
|
|
|
OLED_ShowString(0,0,"initializing...",12);
|
|
|
|
HAL_UARTEx_ReceiveToIdle_IT(&huart1, UART1_RxData, 40);
|
|
HAL_UART_Receive_IT(&huart2, &UART2_RxData, 1);
|
|
|
|
ESP8266_Init();
|
|
while (OneNet_DevLink()) {}
|
|
OneNET_Subscribe();
|
|
|
|
// Init RC522
|
|
MFRC_Init();
|
|
PCD_Reset();
|
|
|
|
OLED_ShowString(0,0,"OneNet online ",12);
|
|
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
KeyScan();
|
|
// if (!OneNet_DevLink())
|
|
// {
|
|
// OLED_ShowString(0,0,"OneNet online ",12);
|
|
// HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
|
|
// OneNet_SendData();
|
|
// HAL_Delay(1000);
|
|
// ESP8266_Clear();
|
|
// }
|
|
// else
|
|
// {
|
|
// 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();
|
|
|
|
Display_IM1281B();
|
|
|
|
// dataPtr = ESP8266_GetIPD(0);
|
|
// if (dataPtr != NULL)
|
|
// {
|
|
// OneNet_RevPro(dataPtr);
|
|
// }
|
|
|
|
// 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 */
|
|
}
|
|
/* USER CODE END 3 */
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|
|
|
/** Initializes the RCC Oscillators according to the specified parameters
|
|
* in the RCC_OscInitTypeDef structure.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
|
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
|
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Initializes the CPU, AHB and APB buses clocks
|
|
*/
|
|
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
|
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
|
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
// 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 (huart == &huart1)
|
|
{
|
|
// assign the received data to the IM_RxBuf
|
|
if (Size >= sizeof(UART1_RxData))
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
{
|
|
if (huart == &huart2)
|
|
{
|
|
if (esp8266_cnt >= sizeof(esp8266_buf))
|
|
{
|
|
esp8266_cnt = 0;
|
|
}
|
|
esp8266_buf[esp8266_cnt++] = UART2_RxData;
|
|
HAL_UART_Receive_IT(&huart2, (uint8_t *)&UART2_RxData, 1);
|
|
}
|
|
}
|
|
/* USER CODE END 4 */
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
/* USER CODE BEGIN Error_Handler_Debug */
|
|
/* User can add his own implementation to report the HAL error return state */
|
|
__disable_irq();
|
|
while (1)
|
|
{
|
|
HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
|
|
HAL_Delay(200);
|
|
}
|
|
/* USER CODE END Error_Handler_Debug */
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t *file, uint32_t line)
|
|
{
|
|
/* USER CODE BEGIN 6 */
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
/* USER CODE END 6 */
|
|
}
|
|
#endif /* USE_FULL_ASSERT */
|