Files
FireAlarmCtrlCn/FW/Core/my_src/HW_config.c
2026-04-06 19:02:09 +08:00

710 lines
17 KiB
C

#include "main.h"
#include "fdcan.h"
#include "fdcan_task.h"
#include "HW_Config.h"
#include "W25Qxx.h"
#include "internal_flash.h"
#include "user_norflash.h"
#include "load_gui_lib.h"
#include "uart_printer_drv.h"
#define USER_UART_NUM 8
extern void MX_FDCAN2_Init(void);
uid_union uid_value;
uart_info_8bit_struct uart_info_8bit[USER_UART_NUM];
//LPUART1--eth 57600,USART1--wifi 115200,USART2--fecbus 9600,USART3--canouter 57600,UART4--linkage 19200,UART5--memory 57600,USART6--print 9600,UART7--switch 57600
unsigned char * const p_user_uart[USER_UART_NUM] = {LPUART1,USART1,USART2,USART3,UART4,UART5,USART6,UART7};
const unsigned int uart_tx_complete_timeup[USER_UART_NUM] = {3,17,17,3,9,3,20,3};//1ms 16bytes FIFO
unsigned int lcd_update_flag = 0;
unsigned int usb_is_host = 0;
//unsigned char TEXT_Buffer[]={"ABC12365478903678913"};
#define SIZE sizeof(TEXT_Buffer)
/**
* @brief Perform the SDRAM exernal memory inialization sequence
* @param hsdram: SDRAM handle
* @param Command: Pointer to SDRAM command structure
* @retval None
*/
/**
* @brief Perform the SDRAM exernal memory inialization sequence
* @param hsdram: SDRAM handle
* @param Command: Pointer to SDRAM command structure
* @retval None
*/
void SDRAM_Initialization_Sequence(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command)
{
__IO uint32_t tmpmrd =0;
/* Step 1: Configure a clock configuration enable command */
Command->CommandMode = FMC_SDRAM_CMD_CLK_ENABLE;
Command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
Command->AutoRefreshNumber = 1;
Command->ModeRegisterDefinition = 0;
/* Send the command */
HAL_SDRAM_SendCommand(hsdram, Command, SDRAM_TIMEOUT);
/* Step 2: Insert 100 us minimum delay */
/* Inserted delay is equal to 1 ms due to systick time base unit (ms) */
HAL_Delay(1);
/* Step 3: Configure a PALL (precharge all) command */
Command->CommandMode = FMC_SDRAM_CMD_PALL;
Command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
Command->AutoRefreshNumber = 1;
Command->ModeRegisterDefinition = 0;
/* Send the command */
HAL_SDRAM_SendCommand(hsdram, Command, SDRAM_TIMEOUT);
/* Step 4 : Configure a Auto-Refresh command */
Command->CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
Command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
Command->AutoRefreshNumber = 8;
Command->ModeRegisterDefinition = 0;
/* Send the command */
HAL_SDRAM_SendCommand(hsdram, Command, SDRAM_TIMEOUT);
/* Step 5: Program the external memory mode register */
tmpmrd = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |
SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |
SDRAM_MODEREG_CAS_LATENCY_3 |
SDRAM_MODEREG_OPERATING_MODE_STANDARD |
SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
Command->CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
Command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
Command->AutoRefreshNumber = 1;
Command->ModeRegisterDefinition = tmpmrd;
/* Send the command */
HAL_SDRAM_SendCommand(hsdram, Command, SDRAM_TIMEOUT);
/* Step 6: Set the refresh rate counter */
/* Set the device refresh rate */
HAL_SDRAM_ProgramRefreshRate(hsdram, REFRESH_COUNT);
}
void FDCAN2_Initialization_Sequence(void)
{
FDCAN_FilterTypeDef sFilterConfig;
/*##-2- Configure the CAN Filter ###########################################*/
/* Configure Rx filter */
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = 0x0000;
sFilterConfig.FilterID2 = 0x0000;
if(HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
//HAL_FDCAN_Stop(p_fdcan_handle);
/*##-3- Start the CAN peripheral ###########################################*/
if (HAL_FDCAN_Start(&hfdcan2) != HAL_OK)
{
/* Start Error */
//Error_Handler();
HAL_Delay(100);
HAL_FDCAN_Start(&hfdcan2);
}
/*##-4- Activate CAN RX TX notification #######################################*/
if (HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
{
/* Notification Error */
Error_Handler();
}
if (HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_TX_FIFO_EMPTY, 0) != HAL_OK)
{
Error_Handler();
}
}
unsigned int FDCAN2_Reinit(void)
{
FDCAN_FilterTypeDef sFilterConfig;
MX_FDCAN2_Init();
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = 0x0000;
sFilterConfig.FilterID2 = 0x0000;
if(HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig) != HAL_OK)return 2;
HAL_FDCAN_Start(&hfdcan2);
HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_TX_FIFO_EMPTY, 0);
return 0;
}
void get_uuid(void)
{
uid_value.val_32[0] = LL_GetUID_Word0();
uid_value.val_32[1] = LL_GetUID_Word1();
uid_value.val_32[2] = LL_GetUID_Word2();
}
unsigned short get_open_password(void)
{
unsigned int pwd;
unsigned short res;
pwd = uid_value.val_32[0];
pwd^= uid_value.val_32[1];
pwd^= uid_value.val_32[2];
res = (pwd>>16)&0xFFFF;
res^= (pwd&0xFFFF);
return res;
}
void HW_init(void)
{
unsigned int addr = 0;
unsigned int zk_addr = 0;
unsigned int value = 0;
unsigned int i = 0,x = 0,y = 0, a = 0;
get_uuid();
factory_prm.pwr = get_open_password();
read_factory_prm();
//LCD RST
LL_GPIO_SetOutputPin(GPIOE,LL_GPIO_PIN_3);//not rst
//TIM2 relay
LL_TIM_CC_EnableChannel(TIM2,LL_TIM_CHANNEL_CH1);
LL_TIM_OC_SetCompareCH1(TIM2,0);
LL_TIM_EnableAllOutputs(TIM2);
//LL_TIM_EnableIT_UPDATE(TIM2);
LL_TIM_EnableCounter(TIM2);
//TIM3 LCD_BL
LL_TIM_CC_EnableChannel(TIM3,LL_TIM_CHANNEL_CH2);
LL_TIM_OC_SetCompareCH2(TIM3,255);//bl
LL_TIM_EnableAllOutputs(TIM3);
LL_TIM_EnableCounter(TIM3);
//TIM7 random
//LL_TIM_EnableIT_UPDATE(TIM7);
LL_TIM_EnableCounter(TIM7);
//TIM12 relay
LL_TIM_CC_EnableChannel(TIM12,LL_TIM_CHANNEL_CH1);
LL_TIM_OC_SetCompareCH1(TIM12,0);
LL_TIM_EnableAllOutputs(TIM12);
//LL_TIM_EnableIT_UPDATE(TIM2);
LL_TIM_EnableCounter(TIM12);
//LPUART1
LL_USART_EnableIT_RXNE(LPUART1);
LL_USART_ClearFlag_IDLE(LPUART1);
//LL_USART_EnableIT_IDLE(LPUART1);
uart_info_8bit[0].tx_complete = 1;
//UART1
LL_USART_EnableIT_RXNE(USART1);
LL_USART_ClearFlag_IDLE(USART1);
LL_USART_EnableIT_IDLE(USART1);
uart_info_8bit[1].tx_complete = 1;
//UART2
LL_USART_EnableIT_RXNE(USART2);
LL_USART_ClearFlag_IDLE(USART2);
//LL_USART_EnableIT_IDLE(USART2);
uart_info_8bit[2].tx_complete = 1;
//UART3
LL_USART_EnableIT_RXNE(USART3);
LL_USART_ClearFlag_IDLE(USART3);
LL_USART_EnableIT_IDLE(USART3);
uart_info_8bit[3].tx_complete = 1;
//UART4
LL_USART_EnableIT_RXNE(UART4);
LL_USART_ClearFlag_IDLE(UART4);
//LL_USART_EnableIT_IDLE(UART4);
uart_info_8bit[4].tx_complete = 1;
//UART5
LL_USART_EnableIT_RXNE(UART5);
LL_USART_ClearFlag_IDLE(UART5);
LL_USART_EnableIT_IDLE(UART5);
uart_info_8bit[5].tx_complete = 1;
//UART6
LL_USART_EnableIT_RXNE(USART6);
uart_info_8bit[6].tx_complete = 1;
//UART7
LL_USART_EnableIT_RXNE(UART7);
LL_USART_ClearFlag_IDLE(UART7);
LL_USART_EnableIT_IDLE(UART7);
uart_info_8bit[7].tx_complete = 1;
//fdcan2
FDCAN2_Initialization_Sequence();
can_prm_init();
//LTDC
LTDC_Init();
//LTDC->IER |= LTDC_IER_LIE;
//RTC
rtc_init();
//I2C
//write_user_eeprom(0, 0x1005, TEXT_Buffer, 16);
//write_user_eeprom(1, 0x10001, &TEXT_Buffer[1], 16);
//USBFS
usb_is_host = 1;
//QSPI-HW 24Mhz
nor_flash_init();
//W25QXX_Init();
//nor_erase_sector(0,0);
//nor_write_byte(0,0x403,TEXT_Buffer,16);
//nor_write_byte(1,0x207,TEXT_Buffer,16);
//LCD
LTDC_Layer_Switch(1,0);
//LTDC_Layer_Switch(0,1);
addr = 0xC0000000 + 480*800*4;
for(y=0;y<480;y++){
for(x=0;x<800;x++){
*(unsigned int*)addr = 0;
addr+=4;
}
}
pic_prm_init();
//device_init
printer_init();
//------------------------test-----------------------------------------
/*
for(i=0;i<480;i++){
addr = 50*4 + 0xC0000000 + (800*i*4);
*(unsigned int*)addr = 0xFF00FF00;
}
*/
/*
addr = 0xC0177000 + (10*800*4);
for(y=0;y<10;y++){
for(x=0;x<800;x++){
*(unsigned int*)addr = 0xFF000080;
addr+=4;
}
}
addr = 0xC0177000 + (20*800*4);
for(y=0;y<10;y++){
for(x=0;x<800;x++){
*(unsigned int*)addr = 0xFF000040;
addr+=4;
}
}
addr = 0xC0177000 + (30*800*4);
for(y=0;y<10;y++){
for(x=0;x<800;x++){
*(unsigned int*)addr = 0xFF000020;
addr+=4;
}
}
addr = 0xC0177000 + (40*800*4);
for(y=0;y<10;y++){
for(x=0;x<800;x++){
*(unsigned int*)addr = 0xFF000010;
addr+=4;
}
}
*/
/*
LCD_ShowString(0,240,200,24,24,"SD");
//while(1);
//erase_internal_one_sector(ADDR_FLASH_SECTOR_6_BANK2);
//write_internal_flash(ADDR_FLASH_SECTOR_6_BANK2,FlashWord);
LCD_ShowString(20,200,200,24,24,"SD");
Show_Str(20,100,720,24,"STM32H7¿ª0123456789abcuodij·¢111°å",24,0);//
gui_write_string24(20,130,0xFFFF0000,0xFF00FF00,"Óû§²Î845ÊýSWEIÉèawÖÃ");
load_pic_16x16_to_dispaly_layer(100,200,45);
load_pic_16x16_to_dispaly_layer(140,200,8);
load_pic_24x24_to_dispaly_layer(100,230,101);
load_pic_24x24_to_dispaly_layer(140,230,14);
load_pic_32x32_to_dispaly_layer(100,260,30);
load_pic_32x32_to_dispaly_layer(140,260,15);
*/
//nor_read_byte(0,0x009C0000,TEXT_Buffer,16);
//hw_sdram_test();
gbk_nor_load_to_sdram();
}
void uart_tx_trigger(unsigned int uart_ord)
{
if(uart_ord > 7)return;
uart_info_8bit[uart_ord].tx_index = 0;
uart_info_8bit[uart_ord].tx_complete = 0;
uart_info_8bit[uart_ord].tx_complete_delay_flag = 0;
uart_info_8bit[uart_ord].tx_complete_delay_tick = 0;
uart_info_8bit[uart_ord].tx_none_buf = 0;
LL_USART_ClearFlag_TC(p_user_uart[uart_ord]);
LL_USART_EnableIT_TC(p_user_uart[uart_ord]);
LL_USART_EnableIT_TXFE(p_user_uart[uart_ord]);
}
void uart_tx_load_data(unsigned int uart_ord, unsigned char * p_data, unsigned int len)
{
unsigned int i = 0;
unsigned char * pdata;
if(uart_ord > 7)return;
pdata = p_data;
uart_info_8bit[uart_ord].tx_index = 0;
uart_info_8bit[uart_ord].tx_complete = 0;
uart_info_8bit[uart_ord].tx_none_buf = 0;
uart_info_8bit[uart_ord].tx_len = len;
for(i=0;i<len;i++)
{
uart_info_8bit[uart_ord].tx_buf[i] = *pdata;
pdata++;
}
}
unsigned int get_uart_tx_complete(unsigned int uart_ord)
{
if(uart_ord > 7)return 0;
return uart_info_8bit[uart_ord].tx_complete;
}
unsigned int get_uart_tx_complete_delay_flag(unsigned int uart_ord)
{
if(uart_ord > 7)return 0;
return uart_info_8bit[uart_ord].tx_complete_delay_flag;
}
void uart_tx_complete_delay(void)//1ms
{
unsigned int i;
for(i=0;i<USER_UART_NUM;i++){
if(get_uart_tx_complete(i)){
if(0 == uart_info_8bit[i].tx_complete_delay_flag){
uart_info_8bit[i].tx_complete_delay_tick++;
if(uart_info_8bit[i].tx_complete_delay_tick > uart_tx_complete_timeup[i]){
uart_info_8bit[i].tx_complete_delay_tick = 0;
uart_info_8bit[i].tx_complete_delay_flag = 1;
}
}
}
}
}
void uart_tx_over(unsigned int uart_ord, unsigned char * p_data, unsigned int len)
{
unsigned int i = 0;
unsigned char * pdata;
if(uart_ord > 7)return;
pdata = p_data;
uart_info_8bit[uart_ord].tx_index = 0;
uart_info_8bit[uart_ord].tx_complete = 0;
uart_info_8bit[uart_ord].tx_complete_delay_flag = 0;
uart_info_8bit[uart_ord].tx_complete_delay_tick = 0;
uart_info_8bit[uart_ord].tx_none_buf = 0;
uart_info_8bit[uart_ord].tx_len = len;
for(i=0;i<len;i++)
{
uart_info_8bit[uart_ord].tx_buf[i] = *pdata;
pdata++;
}
uart_tx_trigger(uart_ord);
}
void uart_load_txbuf(unsigned int uart_ord, unsigned char * p_data, unsigned int len)
{
unsigned int i = 0;
unsigned char * pdata;
if(uart_ord > 7)return;
pdata = p_data;
uart_info_8bit[uart_ord].tx_index = 0;
uart_info_8bit[uart_ord].tx_complete = 0;
uart_info_8bit[uart_ord].tx_complete_delay_flag = 0;
uart_info_8bit[uart_ord].tx_complete_delay_tick = 0;
uart_info_8bit[uart_ord].tx_none_buf = 0;
uart_info_8bit[uart_ord].tx_len = len;
for(i=0;i<len;i++)
{
uart_info_8bit[uart_ord].tx_buf[i] = *pdata;
pdata++;
}
}
void uart_irq_handle(USART_TypeDef * USARTx)
{
unsigned short cc;
unsigned int i;
for(i=0;i<8;i++){
if(USARTx == p_user_uart[i])break;
}
if(LL_USART_IsActiveFlag_RXNE(USARTx))
{
cc = LL_USART_ReceiveData8(USARTx);
if(uart_info_8bit[i].rx_index < UART_RX_BUF_MAX)
{
uart_info_8bit[i].rx_buf[uart_info_8bit[i].rx_index] = cc;
uart_info_8bit[i].rx_index++;
}
}
if(LL_USART_IsActiveFlag_IDLE(USARTx))
{
LL_USART_ClearFlag_IDLE(USARTx);
if(uart_info_8bit[i].rx_index)
{
uart_info_8bit[i].rx_len = uart_info_8bit[i].rx_index;
uart_info_8bit[i].rx_index = 0;
uart_info_8bit[i].rx_complete = 1;
}
}
//--------------------------------------------------------------------------------------------------
if(LL_USART_IsEnabledIT_TC(USARTx))//LL_USART_IsActiveFlag_TC
{
LL_USART_ClearFlag_TC(USARTx);
if(uart_info_8bit[i].tx_index >= uart_info_8bit[i].tx_len)
{
LL_USART_DisableIT_TC(USARTx);
uart_info_8bit[i].tx_complete = 1;
}
}
if(LL_USART_IsEnabledIT_TXE_TXFNF(USARTx))//LL_USART_IsActiveFlag_TXE_TXFNF
{
if(uart_info_8bit[i].tx_index >= uart_info_8bit[i].tx_len)
{
LL_USART_DisableIT_TXE_TXFNF(USARTx);
}
else
{
LL_USART_TransmitData8(USARTx, uart_info_8bit[i].tx_buf[uart_info_8bit[i].tx_index]);
uart_info_8bit[i].tx_index++;
}
}
//---------------------------------------------------------------------------
LL_USART_ClearFlag_ORE(USARTx);
}
unsigned short crc16_data(unsigned char* p_data,unsigned int data_length)
{
unsigned int x = 0,y = 0;
unsigned short crc = 0xFFFF;
unsigned char data = 0;
for(x=0;x<data_length;x++){
data = (* p_data);
crc ^= data;
for(y=0;y<8;y++){
if(crc & 0x01){
crc>>=1;
crc ^= 0xA001;
}else{
crc>>=1;
}
}
p_data++;
}
return crc;
}
unsigned int check_crc(unsigned char* p_data,unsigned int data_length)
{
uint16_t crc_data = 0;
if(data_length < 2)return 0;
crc_data = *(p_data + data_length - 1);
crc_data<<=8;
crc_data |= *(p_data + data_length - 2);
if(crc_data == crc16_data(p_data,(data_length - 2))){
return 0;
}
return 1;
}
unsigned char sum8_data(unsigned char* p_data,unsigned int data_length)
{
unsigned int x = 0;
unsigned char sum = 0;
for(x=0;x<data_length;x++){
sum+= (* p_data);
p_data++;
}
return sum;
}
unsigned int check_sum8(unsigned char* p_data,unsigned int data_length)
{
uint8_t sum = 0;
sum = *(p_data + data_length);
if(sum == sum8_data(p_data,data_length)){
return 0;
}
return 1;
}
unsigned int check_open_password(unsigned int check_value)
{
if(check_value == factory_prm.pwr){
return 1;
}
return 0;
}
unsigned int check_open_password_isenable(void)
{
return factory_prm.open_password_isenable;
}
unsigned int check_auth_function_isenable(void)
{
return factory_prm.auth_function_isenable;
}
void set_open_password_isover(void)
{
factory_prm.open_password_isenable = 0;
write_factory_prm();
}
void set_auth_function_able(unsigned int able)
{
if(able > 1)return;
factory_prm.auth_function_isenable = able;
write_factory_prm();
}
void HAL_LTDC_LineEventCallback(LTDC_HandleTypeDef *hltdc)
{
lcd_update_flag = 1;
__HAL_LTDC_ENABLE_IT(hltdc, LTDC_IT_LI);
}
/*******************************************************************************
* Function Name : Crc32Cal
* Description : CRC
* Input : SourceData,Length
* Output : None
* Return : none
*******************************************************************************/
unsigned int Crc32Cal(unsigned int * SourceData, unsigned int Length)
{
unsigned int i;
CRC->CR |= CRC_CR_RESET;
for (i = 0; i < Length; ++i)
{
CRC->DR = *SourceData;
SourceData++;
}
return CRC->DR;
}
void Crc32Reset(void)
{
CRC->CR |= CRC_CR_RESET;
}
void Crc32Feed(unsigned int aVal)
{
CRC->DR = aVal;
}
unsigned int Crc32GetResult()
{
return CRC->DR;
}
void usb_host_to_device(void)
{
if(usb_is_host){
MX_FATFS_DeInit();
HAL_Delay(5);
HAL_NVIC_ClearPendingIRQ(OTG_FS_IRQn);
HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
MX_USB_HOST_DeInit();
HAL_Delay(10);
MX_USB_DEVICE_Init();
HAL_Delay(5);
usb_is_host = 0;
UsbConfigSetState(0);
}
}
void usb_device_to_host(void)
{
if(0 == usb_is_host){
HAL_NVIC_ClearPendingIRQ(OTG_FS_IRQn);
HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
MX_USB_DEVICE_DeInit();
HAL_Delay(5);
UsbConfigSetState(0);
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
HAL_NVIC_ClearPendingIRQ(OTG_FS_IRQn);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
MX_USB_HOST_Init();
MX_FATFS_Init();
HAL_Delay(5);
usb_is_host = 1;
}
}