WL55JC_AS923/Core/Src/main.c

312 lines
8.1 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2021 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 "app_lorawan.h"
#include "gpio.h"
#include "usart.h"
#include "stdio.h"
#include "ev1527.h"
#include "tim.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
// #include "i2c.h"
// #include "app_tof.h"
/* USER CODE END Includes */
// #include "sts_aq_o3.h"
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
TIM_HandleTypeDef htim1, htim2;
//uint8_t rf_payload[3]={0xF8,0xCD,0x07}, rf_length=3;
uint8_t rf_payload[3]={0x1F,0xB3,0xE0}, rf_length=3; // RC_PROJECTOR
uint8_t sos_rf_payload[3]={0x82,0x73,0xA0}, sos_rf_length=3; // sos_button
enum rf_cmd_enum
{ BUTTON_NONE=0,
BUTTON_ON,
BUTTON_OFF,
BUTTON_FIRST,
BUTTON_NEXT,
BUTTON_5S,
BUTTON_10S,
BUTTON_15S,
BUTTON_30S,
BUTTON_9,BUTTON_10,BUTTON_11,BUTTON_12,BUTTON_13,BUTTON_14,BUTTON_15,
};
uint8_t rf_cmd[16]={0x00, 0x8,0xC,0x4,0x6,0x1,0x9,0x2,0x3}; // cmd 1 = 1, cmd2=4, cmd3=3, cmd4=2
uint8_t sos_rf_cmd[16]={0x00, 0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; // cmd 1 = 1
void sts_rc_key(uint8_t key);
void sts_rc_decoder(void);
uint8_t sts_rc_decodedx(void);
volatile uint8_t codexx=0, code_vt=0;
/* USER CODE END PTD */
uint8_t rc_cmd[9]={
RC_NONE,
RC_POWER_ON,
RC_POWER_OFF,
RC_PIC_FIRST,
RC_PIC_NEXT,
RC_SHOW_5S,
RC_SHOW_10S,
RC_SHOW_15S,
RC_SHOW_30S
};
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* 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 */
int _write(int file, char *ptr, int len)
{
(void) file;
HAL_UART_Transmit (&huart2, (uint8_t*)ptr, len, 0xFFFF);
return len;
}
/* 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_I2C2_Init();
MX_GPIO_Init();
// MX_LoRaWAN_Init();
/* USER CODE BEGIN 2 */
MX_USART2_UART_Init();
// MX_USART1_UART_Init();
// MX_TIM1_Init();
MX_TIM2_Init();
if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
printf("start \r\n");
while(1)
{
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
MX_LoRaWAN_Process();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
uint8_t single_button =0;
switch(GPIO_Pin)
{
case BUT1_Pin:
#if 1
printf("Button 1 pressed, sending cmd #1 \r\n");
//STS_RF_Send_AddressBit_and_CmdBit(rf_payload, rf_length);
// STS_RF_Send_Multi_Times(rf_payload, 3, 8);
single_button = rf_cmd[BUTTON_ON];
STS_RF_Send_Button_Multi_Times(rf_payload, single_button, 3, 8);
#endif
// for SOS BUTTON
#if 0
printf("SOS Button pressed, sending cmd #1 \r\n");
//STS_RF_Send_AddressBit_and_CmdBit(rf_payload, rf_length);
// STS_RF_Send_Multi_Times(rf_payload, 3, 8);
single_button = sos_rf_cmd[BUTTON_ON];
STS_RF_Send_Button_Multi_Times(sos_rf_payload, single_button, 3, 8);
#endif
break;
case BUT2_Pin:
printf("Button 2 pressed, sending cmd #2 \r\n");
single_button = rf_cmd[BUTTON_OFF];
// STS_RF_Send_Multi_Times(rf_payload, 3, 5);
STS_RF_Send_Button_Multi_Times(rf_payload, single_button, 3, 8);
break;
case BUT3_Pin:
printf("Button 3 pressed, sending cmd #3 \r\n");
single_button = rf_cmd[BUTTON_NEXT];
// STS_RF_Send_Multi_Times(rf_payload, 3, 5);
STS_RF_Send_Button_Multi_Times(rf_payload, single_button, 3, 8);
break;
case DATA_433_PIN:
// printf("^");
//HAL_TIM_IC_CaptureCallback(&htim1);
//RF_Signal_Decode();
break;
default:
break;
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK
|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* 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 */
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET);
__disable_irq();
while (1)
{
}
/* 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) */
while (1)
{
}
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */