1663 lines
50 KiB
C
1663 lines
50 KiB
C
|
|
/*
|
|
@NOT_ACT_USED :@NEED_CHECK_FUNC
|
|
|
|
|
|
void tuner_config_power_on(void)
|
|
void tuner_config_power_reset(void)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
版权所有 (C), 2001-2011, 上海全波通信技术有限公司
|
|
|
|
******************************************************************************
|
|
文 件 名 : sys_config.c
|
|
版 本 号 : 初稿
|
|
作 者 : 郭金涛
|
|
生成日期 : 2013年6月3日
|
|
最近修改 :
|
|
功能描述 : 系统配置函数
|
|
函数列表 :
|
|
timer_init
|
|
uart_com_init
|
|
watchdog_init
|
|
修改历史 :
|
|
1.日 期 : 2013年6月3日
|
|
作 者 : 郭金涛
|
|
修改内容 : 创建文件
|
|
|
|
******************************************************************************/
|
|
#include <stdio.h>
|
|
#include "type.h"
|
|
#include "sys_config.h"
|
|
#include "os_task.h"
|
|
#include "delay.h"
|
|
#include "rtc.h"
|
|
#include "lcd_task.h"
|
|
#include "stm32f4xx.h"
|
|
|
|
#include "stm32f4xx_hal.h"
|
|
#if 0
|
|
//#include "misc.h"
|
|
//#include "stm32f4xx_exti.h"
|
|
//#include "stm32f4xx_gpio.h"
|
|
#endif
|
|
|
|
|
|
#define SYS_DEV_UNIQUE_ID_L ((uint32_t)0x1FFF7A10)
|
|
#define SYS_DEV_UNIQUE_ID_H ((uint32_t)0x1FFF7A14)
|
|
|
|
SPI_HandleTypeDef hspi1;
|
|
SPI_HandleTypeDef hspi2;
|
|
EXTI_HandleTypeDef hexti;
|
|
WWDG_HandleTypeDef hwwdg;
|
|
|
|
#if USE_ETH_DBG
|
|
s_USR_DBG_ETH udbg_eth={0};
|
|
|
|
void inc_eth_dbg_cnt(en_USR_ETH_DBG type)
|
|
{
|
|
switch(type)
|
|
{
|
|
case USR_ETH_INT:
|
|
udbg_eth.eth_int_in_cnt++;
|
|
break;
|
|
case USR_ETH_RECV_SEM:
|
|
udbg_eth.eth_recv_sem_cnt++;//ethernetif_input
|
|
break;
|
|
case USR_ETH_RECV_TCPIP:
|
|
udbg_eth.eth_recv_tcpip_cnt++;//tcpip_input ethernet_input
|
|
break;
|
|
case USR_ETH_IP4_INPUT://ip4_input
|
|
udbg_eth.eth_ip4_in_cnt++;
|
|
break;
|
|
case USR_ETH_TCP_INPUT://tcp_input
|
|
udbg_eth.eth_tcp_in_cnt++;
|
|
break;
|
|
case USR_ETH_UDP_INPUT:
|
|
udbg_eth.eth_udp_in_cnt++;
|
|
break;
|
|
case USR_ETH_ICMP_INPUT:
|
|
udbg_eth.eth_icmp_in_cnt++;
|
|
break;
|
|
|
|
case USR_ETH_TCP_LISTEN:
|
|
udbg_eth.eth_tcp_listen_cnt++;//tcp_listen_input
|
|
break;
|
|
case USR_ETH_HTTP_ACCEPT://http_accept
|
|
udbg_eth.eth_http_accept_c++;
|
|
break;
|
|
case USR_ETH_HTTP_RECV://http_recv
|
|
udbg_eth.eth_http_recv_c++;
|
|
break;
|
|
case USR_ETH_HTTP_PARSE://http_parse_request
|
|
udbg_eth.eth_http_parse_c++;
|
|
break;
|
|
case USR_ETH_HTTP_SEND://http_sent
|
|
udbg_eth.eth_http_send_c++;
|
|
break;
|
|
case USR_ETH_TCP_OUT://tcp_output
|
|
udbg_eth.eth_tcp_out_c++;
|
|
break;
|
|
|
|
case USR_LVL_INPUT_ERR://tcp_output
|
|
udbg_eth.low_level_in_alloc_err++;
|
|
break;
|
|
|
|
case USR_MEMP_TCP_SEG_RESET:
|
|
udbg_eth.memp_tcp_seg_reset_c++;
|
|
break;
|
|
default:break;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if I2C_SOFT_GPIO //软件模拟,不使用mutex,直接关中断
|
|
extern S_I2C_GPIO i2c_gpio[];
|
|
#endif
|
|
extern MUTEX_HANDLE i2c_mutex[];
|
|
|
|
extern void delay_us(uint32 nus);
|
|
extern void Error_Handler(void);
|
|
//获取当前芯片的序列ID(低32bit)
|
|
uint32 sys_get_dev_unique_id(void)
|
|
{
|
|
return *(__IO uint32 *) SYS_DEV_UNIQUE_ID_L;
|
|
}
|
|
|
|
//初始化GPIO,包括使能和时钟
|
|
void sys_gpio_init(void)
|
|
{
|
|
/*RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);
|
|
*/
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
|
__HAL_RCC_GPIOF_CLK_ENABLE();
|
|
__HAL_RCC_GPIOG_CLK_ENABLE();
|
|
|
|
}
|
|
|
|
//初始化内部RCC组件,包括使能和时钟配置
|
|
void sys_rcc_init(void)
|
|
{
|
|
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
|
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
|
}
|
|
//初始化外围器件,包括使能和时钟
|
|
void sys_peripheral_clk_init(void)
|
|
{
|
|
|
|
}
|
|
|
|
void sys_pvd_init(void)
|
|
{
|
|
EXTI_ConfigTypeDef ExtiConfig;
|
|
PWR_PVDTypeDef sConfigPVD;
|
|
|
|
/* NVIC_InitTypeDef NVIC_InitStructure;
|
|
EXTI_InitTypeDef EXTI_InitStructure;
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //置为最高优先级
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
*/
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
|
HAL_NVIC_SetPriority(PVD_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(PVD_IRQn);
|
|
|
|
/* EXTI_ClearITPendingBit(EXTI_Line16);
|
|
EXTI_InitStructure.EXTI_Line = EXTI_Line16;
|
|
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
|
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //只检测上升沿(掉电)
|
|
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
|
EXTI_Init(&EXTI_InitStructure);
|
|
|
|
PWR_PVDLevelConfig(PWR_PVDLevel_4);
|
|
|
|
//Enable the PVD Output
|
|
PWR_PVDCmd(ENABLE);
|
|
*/
|
|
#if 1//_USE_RVL_ //上升沿触发回调是否应该直接定义
|
|
#endif
|
|
|
|
hexti.Line = EXTI_LINE_16;
|
|
HAL_EXTI_ClearPending(&hexti, EXTI_TRIGGER_RISING);
|
|
ExtiConfig.Line = EXTI_LINE_16;
|
|
ExtiConfig.Mode = EXTI_MODE_INTERRUPT;
|
|
ExtiConfig.Trigger = EXTI_TRIGGER_RISING;
|
|
HAL_EXTI_SetConfigLine(&hexti, &ExtiConfig);
|
|
|
|
PWR_PVDLevelConfig(PWR_PVDLEVEL_4);//HAL_PWR_ConfigPVD(&sConfigPVD);
|
|
|
|
HAL_PWR_EnablePVD();
|
|
}
|
|
void PVD_IRQ_Handler(void)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
//实际情况不一定全是系统关闭. 后续在增加逻辑判断
|
|
//if(EXTI_GetITStatus(EXTI_LINE_16) != RESET)//EXTI_Line16
|
|
if(HAL_EXTI_GetPending(&hexti,EXTI_LINE_16) != RESET)//EXTI_Line16
|
|
{
|
|
rtc_update_close_time();
|
|
//EXTI_ClearITPendingBit(EXTI_Line16);
|
|
hexti.Line = EXTI_LINE_16;
|
|
HAL_EXTI_ClearPending(&hexti, EXTI_TRIGGER_RISING);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
//-------------------->>>---------------------
|
|
|
|
|
|
void WWDG_IRQHandler(void)
|
|
{
|
|
#if WATCHDOG_ENABLE
|
|
os_int_enter();
|
|
os_dead_wwdg();
|
|
os_int_exit();
|
|
#else
|
|
//走不到这.
|
|
#endif
|
|
}
|
|
#if WATCHDOG_ENABLE
|
|
void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
|
|
{
|
|
static uint16 test=0;
|
|
test++;
|
|
os_dead_wwdg();
|
|
test = test+20;
|
|
|
|
}
|
|
|
|
//看门狗用那个 IWDG 还是WWDG?
|
|
void watchdog_init(void)
|
|
{
|
|
|
|
/*RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG,ENABLE);
|
|
WWDG_SetPrescaler(WWDG_Prescaler_8); //单周期780us
|
|
WWDG_SetWindowValue(0x7f-7); //由tick中断喂狗,1个tick喂一次10ms 780*7 =5ms左右
|
|
WWDG_EnableIT(); //最大喂狗周期,780*64 =49.9ms 最小喂狗周期 780*7 = 5ms
|
|
DBGMCU_APB1PeriphConfig(DBGMCU_WWDG_STOP,ENABLE); //为了方便调试,进入debug模式时,WWDG时钟挂起.
|
|
WWDG_Enable(0x7f);*/
|
|
|
|
__HAL_RCC_WWDG_CLK_ENABLE();
|
|
|
|
//hwwdg.EwiCallback = WWDG_IRQHandler;//Reset call back
|
|
hwwdg.Instance = WWDG;
|
|
hwwdg.Init.Counter = 0x7f-7;
|
|
hwwdg.Init.Window = 0x7f-7;
|
|
hwwdg.Init.Prescaler = WWDG_PRESCALER_8;
|
|
hwwdg.Init.EWIMode = WWDG_EWI_ENABLE;
|
|
#if 1//_USE_RVL_
|
|
HAL_WWDG_Init(&hwwdg);
|
|
#endif
|
|
__HAL_WWDG_ENABLE_IT(&hwwdg,WWDG_IT_EWI);
|
|
__HAL_DBGMCU_FREEZE_WWDG();
|
|
__HAL_WWDG_ENABLE(&hwwdg);
|
|
}
|
|
#endif
|
|
|
|
|
|
//--------------------<<<---------------------
|
|
|
|
|
|
|
|
TIM_HandleTypeDef htim5;
|
|
extern uint32_t SystemCoreClock;
|
|
|
|
//创建一个1ms定时器,用于检测串口报文等
|
|
uint32 create_uart_timer(void)
|
|
{
|
|
uint32 reload;
|
|
|
|
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
|
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
|
|
|
__HAL_RCC_TIM5_CLK_ENABLE();//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
|
|
|
|
HAL_NVIC_SetPriority(TIM5_IRQn, 13 ,0);
|
|
HAL_NVIC_EnableIRQ(TIM5_IRQn);
|
|
|
|
reload = ((SystemCoreClock/2) /1000) - 1; //周期1ms
|
|
|
|
htim5.Instance = TIM5;
|
|
htim5.Init.Period = reload;
|
|
htim5.Init.Prescaler = 0;
|
|
htim5.Init.ClockDivision = 0;
|
|
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
|
|
|
|
if(HAL_TIM_Base_Init(&htim5) == HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
|
if (HAL_TIM_ConfigClockSource(&htim5, &sClockSourceConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_TIM_OnePulse_Init(&htim5, TIM_OPMODE_REPETITIVE) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* Start the TIM time Base generation in interrupt mode */
|
|
HAL_TIM_Base_Start_IT(&htim5);
|
|
|
|
return 0;
|
|
}
|
|
|
|
UART_HandleTypeDef husart[UART_PORT_NUM];
|
|
|
|
|
|
static S_UART_RX_CTRL uart_ctrl[UART_PORT_NUM];
|
|
static USART_TypeDef* COM_USART[UART_PORT_NUM] = {USART1,USART2,USART6,USART3};
|
|
static uint8 NVIC_UART_IRQ[UART_PORT_NUM] = {USART1_IRQn,USART2_IRQn,USART6_IRQn,USART3_IRQn};
|
|
/*********************************串口部分配置函数**************************************/
|
|
/*
|
|
串口硬件连接说明:
|
|
序号 对应硬件原理图上的名称 芯片管脚 TX RX
|
|
0 USART1 PB6, PB7 GPS
|
|
1 UART2 .. PD5, PD6 RS232
|
|
2 UART6 .. PC6, PC7 J1 RS485 PC8为控制管脚
|
|
3 USART3 PD8, PD9 连接外部dryloop
|
|
*/
|
|
|
|
/*****************************************************************************
|
|
函 数 名 : uart_init_hw
|
|
功能描述 : 初始化串口底层
|
|
输入参数 : void
|
|
输出参数 : 无
|
|
返 回 值 : static
|
|
调用函数 :
|
|
被调函数 :
|
|
|
|
修改历史 :
|
|
1.日 期 : 2013年6月3日
|
|
作 者 : 郭金涛
|
|
修改内容 : 新生成函数
|
|
|
|
*****************************************************************************/
|
|
static uint32 uart_init_bsp(uint32 port,UART_InitTypeDef* puart_init,uint32 int_sw)
|
|
{
|
|
static const uint32 COM_TX_PORT_CLK[UART_PORT_NUM] = {
|
|
RCC_AHB1Periph_GPIOB,RCC_AHB1Periph_GPIOD,RCC_AHB1Periph_GPIOC,RCC_AHB1Periph_GPIOD};
|
|
|
|
static const uint32 COM_RX_PORT_CLK[UART_PORT_NUM] = {
|
|
RCC_AHB1Periph_GPIOB,RCC_AHB1Periph_GPIOD,RCC_AHB1Periph_GPIOC,RCC_AHB1Periph_GPIOD};
|
|
|
|
static const uint32 COM_USART_CLK[UART_PORT_NUM] = {
|
|
RCC_APB2Periph_USART1,RCC_APB1Periph_USART2,RCC_APB2Periph_USART6,RCC_APB1Periph_USART3};
|
|
|
|
static GPIO_TypeDef* COM_TX_PORT[UART_PORT_NUM] = {GPIOB,GPIOD,GPIOC,GPIOD};
|
|
static GPIO_TypeDef* COM_RX_PORT[UART_PORT_NUM] = {GPIOB,GPIOD,GPIOC,GPIOD};
|
|
|
|
static const uint16 COM_TX_PIN[UART_PORT_NUM] = {GPIO_Pin_6,GPIO_Pin_5, GPIO_Pin_6,GPIO_Pin_8};
|
|
static const uint16 COM_RX_PIN[UART_PORT_NUM] = {GPIO_Pin_7,GPIO_Pin_6, GPIO_Pin_7,GPIO_Pin_9};
|
|
|
|
static const uint16 COM_TX_PIN_SOURCE[UART_PORT_NUM] = {GPIO_PinSource6,GPIO_PinSource5, GPIO_PinSource6,GPIO_PinSource8};
|
|
static const uint16 COM_RX_PIN_SOURCE[UART_PORT_NUM] = {GPIO_PinSource7,GPIO_PinSource6, GPIO_PinSource7,GPIO_PinSource9};
|
|
|
|
//static const uint16 COM_AF[UART_PORT_NUM] = {GPIO_AF_USART1,GPIO_AF_USART2,GPIO_AF_USART6,GPIO_AF_USART3};
|
|
static const uint16 COM_AF[UART_PORT_NUM] = {GPIO_AF7_USART1,GPIO_AF7_USART2,GPIO_AF8_USART6,GPIO_AF7_USART3};
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
//NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
//串口时钟使能
|
|
RCC_AHB1PeriphClockCmd(COM_TX_PORT_CLK[port] | COM_RX_PORT_CLK[port], ENABLE);
|
|
if((port==0)||(port==2))
|
|
{
|
|
RCC_APB2PeriphClockCmd(COM_USART_CLK[port], ENABLE);
|
|
}
|
|
else
|
|
{
|
|
RCC_APB1PeriphClockCmd(COM_USART_CLK[port], ENABLE);
|
|
}
|
|
|
|
//RS485控制引脚配置
|
|
if(port==2)
|
|
{
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
|
|
|
|
GPIO_InitStructure.Pull = GPIO_PULLUP;
|
|
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStructure.Pin = GPIO_Pin_8;
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
GPIO_ResetBits(GPIOC,GPIO_Pin_8);
|
|
}
|
|
|
|
//GPIO_PinAFConfig(COM_TX_PORT[port], COM_TX_PIN_SOURCE[port], COM_AF[port]);
|
|
//GPIO_PinAFConfig(COM_RX_PORT[port], COM_RX_PIN_SOURCE[port], COM_AF[port]);
|
|
|
|
//串口收发引脚配置
|
|
GPIO_InitStructure.Pull = GPIO_PULLUP;
|
|
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStructure.Pin = COM_TX_PIN[port];
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.Alternate = COM_AF[port];
|
|
HAL_GPIO_Init(COM_TX_PORT[port], &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.Pull = GPIO_PULLUP;
|
|
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStructure.Pin = COM_RX_PIN[port];
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.Alternate = COM_AF[port];
|
|
HAL_GPIO_Init(COM_RX_PORT[port], &GPIO_InitStructure);
|
|
|
|
//串口基本参数配置
|
|
husart[port].Instance = COM_USART[port];
|
|
husart[port].Init.BaudRate = puart_init->BaudRate;
|
|
husart[port].Init.WordLength = puart_init->WordLength;
|
|
husart[port].Init.StopBits = puart_init->StopBits;
|
|
husart[port].Init.Parity = puart_init->Parity;
|
|
husart[port].Init.Mode = puart_init->Mode;
|
|
husart[port].Init.HwFlowCtl = puart_init->HwFlowCtl;
|
|
husart[port].Init.OverSampling= puart_init->OverSampling;
|
|
|
|
|
|
|
|
__HAL_USART_DISABLE(&husart[port]);//USART_Cmd(COM_USART[port], DISABLE);
|
|
HAL_UART_Init(&husart[port]);
|
|
|
|
//串口中断配置
|
|
if(int_sw)
|
|
{
|
|
/*NVIC_InitStructure.NVIC_IRQChannel = NVIC_UART_IRQ[port];
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 13;
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
USART_ITConfig(COM_USART[port], USART_IT_RXNE, ENABLE); //开启接收中断.
|
|
*/
|
|
|
|
HAL_NVIC_SetPriority(NVIC_UART_IRQ[port], 13, 0);
|
|
HAL_NVIC_EnableIRQ(NVIC_UART_IRQ[port]);
|
|
__HAL_UART_ENABLE_IT(&husart[port],UART_IT_RXNE);
|
|
}
|
|
/* Enable USART */
|
|
__HAL_USART_ENABLE(&husart[port]);//USART_Cmd(COM_USART[port], ENABLE);
|
|
|
|
return ERR_NO_ERR;
|
|
}
|
|
|
|
|
|
/*****************************************************************************
|
|
函 数 名 : uart_com_init
|
|
功能描述 : 初始化串口
|
|
输入参数 : uint32 com_num 串口号
|
|
uint32 baud 波特率
|
|
uint32 verify 只可配置奇偶校验 其他固定
|
|
uint32 int_sw 配置中断开关
|
|
输出参数 : 无
|
|
返 回 值 :
|
|
调用函数 :
|
|
被调函数 :
|
|
|
|
修改历史 :
|
|
1.日 期 : 2013年6月3日
|
|
作 者 : 郭金涛
|
|
修改内容 : 新生成函数
|
|
|
|
*****************************************************************************/
|
|
uint32 uart_com_init(uint32 com_num,uint32 baud,UART_PARITY verify,uint32 int_sw)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
//static const uint32 parity[3] = {USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
|
|
static const uint32 parity[3] = {USART_PARITY_NONE,USART_PARITY_EVEN,USART_PARITY_ODD};
|
|
|
|
UART_InitTypeDef UART_InitStructure;
|
|
|
|
if(com_num>=UART_PORT_NUM)
|
|
return ERR_NO_ERR;
|
|
|
|
if((uint32)verify>2)
|
|
verify = PARITY_NO;
|
|
|
|
if(int_sw>1)
|
|
int_sw = 0;
|
|
|
|
uart_ctrl[com_num].valid = 1; //是否需要判断 中断使能与否?
|
|
get_current_task_id(&uart_ctrl[com_num].task_id);
|
|
|
|
if (uart_ctrl[com_num].mutex == NULL)
|
|
{
|
|
uart_ctrl[com_num].mutex = create_mutex();
|
|
if(uart_ctrl[com_num].mutex==NULL)
|
|
{
|
|
return ERR_CREATE_MUTEX_FAILED;
|
|
}
|
|
}
|
|
|
|
UART_InitStructure.BaudRate = baud;
|
|
UART_InitStructure.WordLength = USART_WORDLENGTH_8B;
|
|
UART_InitStructure.StopBits = USART_STOPBITS_1;
|
|
UART_InitStructure.Parity = parity[(uint32)verify];
|
|
UART_InitStructure.Mode = USART_MODE_TX_RX;
|
|
UART_InitStructure.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
UART_InitStructure.OverSampling = UART_OVERSAMPLING_16;
|
|
//USART_InitStructure.HardwareFlowControl = USART_HardwareFlowControl_None; //默认关闭
|
|
|
|
return uart_init_bsp(com_num,&UART_InitStructure,int_sw);
|
|
#endif//_USE_RVL_
|
|
}
|
|
|
|
uint32 uart_com_init_task_id(uint32 com_num,uint32 baud,UART_PARITY verify,uint32 int_sw,uint32 task_id)
|
|
{
|
|
static const uint32 parity[3] = {USART_PARITY_NONE,USART_PARITY_EVEN,USART_PARITY_ODD};
|
|
static UART_InitTypeDef UART_InitStructure;
|
|
|
|
if(com_num>=UART_PORT_NUM)
|
|
return ERR_NO_ERR;
|
|
|
|
if((uint32)verify>2)
|
|
verify = PARITY_NO;
|
|
|
|
if(int_sw>1)
|
|
int_sw = 0;
|
|
|
|
uart_ctrl[com_num].valid = 1; //是否需要判断 中断使能与否?
|
|
uart_ctrl[com_num].task_id = task_id;
|
|
|
|
if(uart_ctrl[com_num].mutex == NULL)
|
|
{
|
|
uart_ctrl[com_num].mutex = create_mutex();
|
|
if(uart_ctrl[com_num].mutex==NULL)
|
|
{
|
|
return ERR_CREATE_MUTEX_FAILED;
|
|
}
|
|
}
|
|
|
|
//串口配置
|
|
UART_InitStructure.BaudRate = baud;
|
|
UART_InitStructure.WordLength = USART_WORDLENGTH_8B;
|
|
UART_InitStructure.StopBits = USART_STOPBITS_1;
|
|
UART_InitStructure.Parity = parity[(uint32)verify];
|
|
//USART_InitStructure.HardwareFlowControl = USART_HardwareFlowControl_None; //默认关闭
|
|
UART_InitStructure.Mode = USART_MODE_TX_RX;
|
|
UART_InitStructure.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
UART_InitStructure.OverSampling = UART_OVERSAMPLING_16;
|
|
|
|
return uart_init_bsp(com_num,&UART_InitStructure,int_sw);
|
|
}
|
|
|
|
|
|
static void uart2_set_send_control(void)
|
|
{
|
|
GPIO_SetBits(GPIOC,GPIO_Pin_8); //发送时,高电平
|
|
}
|
|
static void uart2_clear_send_control(void)
|
|
{
|
|
GPIO_ResetBits(GPIOC,GPIO_Pin_8);
|
|
}
|
|
|
|
void uart_send_string(uint32 port,const uint8 *pbuf,uint32 len)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
|
|
if(port>UART_PORT_NUM)
|
|
return;
|
|
|
|
if((!pbuf)||(!len))
|
|
return;
|
|
|
|
take_mutex(uart_ctrl[port].mutex);
|
|
|
|
if(port==2)
|
|
{
|
|
uart2_set_send_control();
|
|
}
|
|
|
|
#if USE_STD_USART_SEND_RECV
|
|
while(len--)
|
|
{
|
|
USART_SendData(COM_USART[port], *pbuf++);
|
|
|
|
//while (USART_GetFlagStatus(COM_USART[port], USART_FLAG_TC) == RESET)
|
|
while(__HAL_UART_GET_FLAG(&husart[port], UART_FLAG_TC) == RESET)
|
|
{
|
|
;
|
|
}
|
|
}
|
|
#else
|
|
HAL_USART_Transmit(&husart[port], (uint8_t*)pbuf, len, HAL_MAX_DELAY);
|
|
#endif
|
|
|
|
if(port==2)
|
|
{
|
|
uart2_clear_send_control();
|
|
}
|
|
give_mutex(uart_ctrl[port].mutex);
|
|
#endif
|
|
}
|
|
|
|
//任务起来前,要先执行此函数,将485口配置为输入状态.防止串口初始化前一直处于输出状态,影响485总线
|
|
void uart_hw_pre_init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
|
|
|
|
GPIO_InitStructure.Pull = GPIO_PULLUP;
|
|
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStructure.Pin = GPIO_PIN_8;
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
|
|
uart2_clear_send_control();
|
|
}
|
|
|
|
#if USE_DEBUG_UART
|
|
void debug_uart_init(void)
|
|
{
|
|
uart_com_init(DEBUG_UART,115200,PARITY_NO,INT_OFF); //调试串口,初始化为115200,无奇偶校验,无中断
|
|
}
|
|
#endif
|
|
|
|
/*****************************************************************************
|
|
函 数 名 : fputc
|
|
功能描述 : 实现printf的接口函数
|
|
输入参数 : int ch
|
|
FILE *f
|
|
输出参数 : int ch
|
|
返 回 值 :
|
|
调用函数 :
|
|
被调函数 :
|
|
|
|
修改历史 :
|
|
1.日 期 : 2013年6月3日
|
|
作 者 : 郭金涛
|
|
修改内容 : 新生成函数
|
|
|
|
*****************************************************************************/
|
|
int fputc(int ch, FILE *f)
|
|
{
|
|
char ch_r='\r';
|
|
#if USE_DEBUG_UART
|
|
#if USE_STD_USART_SEND_RECV
|
|
if(ch=='\n')
|
|
{
|
|
USART_SendData(COM_USART[DEBUG_UART],'\r');
|
|
//while (USART_GetFlagStatus(COM_USART[DEBUG_UART], USART_FLAG_TC) == RESET)
|
|
while(__HAL_UART_GET_FLAG(&husart[DEBUG_UART], UART_FLAG_TC) == RESET)
|
|
{
|
|
;
|
|
}
|
|
}
|
|
USART_SendData(COM_USART[DEBUG_UART],ch);
|
|
//while (USART_GetFlagStatus(COM_USART[DEBUG_UART], USART_FLAG_TC) == RESET)
|
|
while (__HAL_UART_GET_FLAG(&husart[DEBUG_UART], UART_FLAG_TC) == RESET)
|
|
{
|
|
;
|
|
}
|
|
#else
|
|
if(ch=='\n')
|
|
{
|
|
HAL_USART_Transmit(&husart[DEBUG_UART], (uint8_t*)&ch_r, 1, HAL_MAX_DELAY);
|
|
}
|
|
HAL_USART_Transmit(&husart[DEBUG_UART], (uint8_t*)&ch, 1, HAL_MAX_DELAY);
|
|
#endif
|
|
|
|
#endif
|
|
return ch;
|
|
}
|
|
|
|
|
|
/*****************************************************************************
|
|
函 数 名 : USART_IRQ_Handler
|
|
功能描述 : 串口中断函数,所有串口均用此函数,接收完成后,向注册的任务发送消息.
|
|
输入参数 : void
|
|
输出参数 : 无
|
|
返 回 值 :
|
|
调用函数 :
|
|
被调函数 :
|
|
|
|
修改历史 :
|
|
1.日 期 : 2013年6月3日
|
|
作 者 : 郭金涛
|
|
修改内容 : 新生成函数
|
|
|
|
*****************************************************************************/
|
|
void USART_IRQ_Handler(void)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
uint32 i = 0;
|
|
S_UART_RX_CTRL *puart = &uart_ctrl[0];
|
|
MSG *pmsg;
|
|
|
|
os_int_enter();
|
|
for(i=0;i<UART_PORT_NUM;i++,puart++)
|
|
{
|
|
if(!puart->valid)
|
|
{
|
|
continue;
|
|
}
|
|
//串口未使能
|
|
if(0==(COM_USART[i]->CR1&USART_CR1_UE))
|
|
{
|
|
continue;
|
|
}
|
|
//if(SET!=USART_GetFlagStatus(COM_USART[i], USART_FLAG_RXNE))
|
|
if(SET!=__HAL_USART_GET_FLAG(&husart[i], USART_FLAG_RXNE))
|
|
continue;
|
|
|
|
|
|
|
|
//USART_ClearFlag(COM_USART[i],USART_FLAG_RXNE);
|
|
__HAL_UART_CLEAR_FLAG(&husart[i], USART_FLAG_RXNE);
|
|
|
|
puart->buffer[puart->buf_cnt][puart->index++]= USART_ReceiveData(COM_USART[i]);
|
|
|
|
if(puart->index>UART_RCV_BUF_LEN-32)
|
|
{
|
|
if(puart->task_id)
|
|
{
|
|
pmsg = &puart->msg[puart->buf_cnt];
|
|
pmsg->type = MSG_RS232;
|
|
pmsg->need_free = MSG_NO_NEED_FREE;
|
|
pmsg->wPara = puart->index;
|
|
pmsg->rPara = i;
|
|
pmsg->lPara = (uint32)&puart->buffer[puart->buf_cnt];
|
|
send_msg(puart->task_id, pmsg);
|
|
}
|
|
puart->buf_cnt++;
|
|
puart->buf_cnt&=(UART_REC_BUF_BAK-1);
|
|
puart->index = 0;
|
|
}
|
|
}
|
|
os_int_exit();
|
|
#endif
|
|
}
|
|
|
|
/*****************************************************************************
|
|
函 数 名 : uart_timer_check
|
|
功能描述 : 串口缓冲区周期检查函数,有tick中断调用. 两帧命令最小间隔为20ms!! 2个tick
|
|
输入参数 : void 使用tick检查,效率太低,需要额外定义一个快速的定时器,间隔1ms左右,bps为115200,20ms检查过长
|
|
输出参数 : 无
|
|
返 回 值 :
|
|
调用函数 :
|
|
被调函数 :
|
|
|
|
修改历史 :
|
|
1.日 期 : 2013年6月3日
|
|
作 者 : 郭金涛
|
|
修改内容 : 新生成函数
|
|
|
|
*****************************************************************************/
|
|
static __IO uint32 uart_timer_num = 0;
|
|
uint32 get_uart_timer_num(void)
|
|
{
|
|
return uart_timer_num;
|
|
}
|
|
static void uart_timer_check(void)
|
|
{
|
|
static uint8 checkcnt[UART_PORT_NUM]={0,};
|
|
static uint16 len[UART_PORT_NUM]={0,};
|
|
uint32 i = 0;
|
|
S_UART_RX_CTRL *puart = &uart_ctrl[0];
|
|
MSG *pmsg;
|
|
|
|
uart_timer_num++;
|
|
for(i=0;i<UART_PORT_NUM;i++,puart++)
|
|
{
|
|
if(!puart->valid)
|
|
continue;
|
|
if(!puart->index)
|
|
continue;
|
|
if(len[i]==puart->index)
|
|
{
|
|
checkcnt[i]++;
|
|
if(checkcnt[i]>=5) //5ms
|
|
{
|
|
if(puart->task_id)
|
|
{
|
|
pmsg = &puart->msg[puart->buf_cnt];
|
|
pmsg->type = MSG_RS232;
|
|
pmsg->need_free = MSG_NO_NEED_FREE;
|
|
pmsg->wPara = puart->index;
|
|
pmsg->rPara = i;
|
|
pmsg->lPara = (uint32)&puart->buffer[puart->buf_cnt];
|
|
send_msg(puart->task_id, pmsg);
|
|
}
|
|
puart->buf_cnt++;
|
|
puart->buf_cnt&=(UART_REC_BUF_BAK-1);
|
|
puart->index = 0;
|
|
|
|
checkcnt[i] = 0;
|
|
len[i] = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
len[i] = puart->index;
|
|
checkcnt[i] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/******************************I2C部分配置函数***************************************/
|
|
#if I2C_SOFT_GPIO==0
|
|
static uint32 i2c_init_bsp(uint32 i2c_num)
|
|
{
|
|
I2C_HandleTypeDef hi2c;
|
|
#if 1//_USE_RVL_
|
|
|
|
I2C_InitTypeDef I2C_InitStructure;
|
|
static I2C_TypeDef* i2c_base[] = {I2C1,I2C2,I2C3};
|
|
|
|
if(i2c_num>2)
|
|
return ERR_I2C_NUM_ERR;
|
|
|
|
hi2c.Instance = i2c_base[i2c_num];
|
|
|
|
/* I2C configuration */
|
|
/*I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
|
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
|
|
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
|
|
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
|
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
|
I2C_InitStructure.I2C_ClockSpeed = I2C_NORMAL_SPEED;*/
|
|
|
|
HAL_I2C_DeInit(i2c_base[i2c_num]);
|
|
|
|
hi2c.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
|
hi2c.Init.OwnAddress1 = 0x00;
|
|
hi2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
hi2c.Init.ClockSpeed = I2C_NORMAL_SPEED;
|
|
hi2c.Init.GeneralCallMode = I2C_GENERALCALL_ENABLE;
|
|
hi2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
|
|
|
//hi2c.Mode = HAL_I2C_MODE_MASTER;
|
|
|
|
HAL_I2C_Init(i2c_base[i2c_num], &I2C_InitStructure);
|
|
__HAL_I2C_ENABLE(&hi2c);//I2C_Cmd(i2c_base[i2c_num],ENABLE);
|
|
|
|
#endif
|
|
return ERR_NO_ERR;
|
|
}
|
|
#endif
|
|
|
|
//i2c初始化,要分别配置时钟,GPIO口,最后在配置IO口I2C模式.
|
|
//I2C 序号为0,1,2
|
|
/*
|
|
硬件上,现在使用了两个I2C口
|
|
序号 原理图名称 芯片端口 SCL SDA
|
|
0 I2C1 PB8,PB9 连接外部接口板,扩充使用(ts接口板等)
|
|
1 I2C2 PF1,PF0 连接FPGA
|
|
2 I2C3 PA8,PC9 连接E2
|
|
3 I2C4(只能模拟) pe3,pe4 连接tuner
|
|
4 I2C5 PD0,PD1 连接dryloop,
|
|
*/
|
|
uint32 i2c_com_init(uint32 i2c_num)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
#if I2C_SOFT_GPIO
|
|
static const uint32 i2c_scl_pin[I2C_MAX_NUM] = {GPIO_Pin_8,GPIO_Pin_1,GPIO_Pin_8,GPIO_Pin_3,GPIO_Pin_0};
|
|
static GPIO_TypeDef* i2c_scl_pin_port[I2C_MAX_NUM] = {GPIOB,GPIOF,GPIOA,GPIOE,GPIOD};
|
|
static const uint32 i2c_scl_gpio_clk[I2C_MAX_NUM] = {RCC_AHB1Periph_GPIOB,RCC_AHB1Periph_GPIOF,RCC_AHB1Periph_GPIOA,RCC_AHB1Periph_GPIOE,RCC_AHB1Periph_GPIOD};
|
|
|
|
static const uint32 i2c_sda_pin[I2C_MAX_NUM] = {GPIO_Pin_9,GPIO_Pin_0,GPIO_Pin_9,GPIO_Pin_4,GPIO_Pin_1};
|
|
static GPIO_TypeDef* i2c_sda_pin_port[I2C_MAX_NUM] = {GPIOB,GPIOF,GPIOC,GPIOE,GPIOD};
|
|
static const uint32 i2c_sda_gpio_clk[I2C_MAX_NUM] = {RCC_AHB1Periph_GPIOB,RCC_AHB1Periph_GPIOF,RCC_AHB1Periph_GPIOC,RCC_AHB1Periph_GPIOE,RCC_AHB1Periph_GPIOD};
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
if(i2c_num>=I2C_MAX_NUM)
|
|
{
|
|
return ERR_I2C_NUM_ERR;
|
|
}
|
|
#if 1//_USE_RVL_
|
|
i2c_mutex[i2c_num] = create_mutex();
|
|
#endif
|
|
|
|
RCC_AHB1PeriphClockCmd(i2c_scl_gpio_clk[i2c_num] | i2c_sda_gpio_clk[i2c_num],ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
|
|
|
GPIO_InitStructure.Pin = i2c_scl_pin[i2c_num];
|
|
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(i2c_scl_pin_port[i2c_num], &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.Pin = i2c_sda_pin[i2c_num];
|
|
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(i2c_sda_pin_port[i2c_num], &GPIO_InitStructure);
|
|
|
|
i2c_gpio[i2c_num].scl_port = i2c_scl_pin_port[i2c_num];
|
|
i2c_gpio[i2c_num].sda_port = i2c_sda_pin_port[i2c_num];
|
|
i2c_gpio[i2c_num].scl_pin = i2c_scl_pin[i2c_num];
|
|
i2c_gpio[i2c_num].sda_pin = i2c_sda_pin[i2c_num];
|
|
|
|
GPIO_SetBits(i2c_scl_pin_port[i2c_num],i2c_scl_pin[i2c_num]);
|
|
GPIO_SetBits(i2c_sda_pin_port[i2c_num],i2c_sda_pin[i2c_num]); //空闲状态,都拉高
|
|
|
|
return 0;
|
|
#else
|
|
static const uint32 i2c_clk[3] = {RCC_APB1Periph_I2C1,RCC_APB1Periph_I2C2,RCC_APB1Periph_I2C3};
|
|
static const uint32 i2c_scl_pin[3] = {GPIO_Pin_8,GPIO_Pin_1,GPIO_Pin_8};
|
|
static GPIO_TypeDef* i2c_scl_pin_port[3] = {GPIOB,GPIOF,GPIOA};
|
|
static const uint32 i2c_scl_pin_source[3] = {GPIO_PinSource8,GPIO_PinSource1,GPIO_PinSource8};
|
|
static const uint32 i2c_scl_gpio_clk[3] = {RCC_AHB1Periph_GPIOB,RCC_AHB1Periph_GPIOF,RCC_AHB1Periph_GPIOA};
|
|
|
|
static const uint32 i2c_sda_pin[3] = {GPIO_Pin_9,GPIO_Pin_0,GPIO_Pin_9};
|
|
static GPIO_TypeDef* i2c_sda_pin_port[3] = {GPIOB,GPIOF,GPIOC};
|
|
static const uint32 i2c_sda_pin_source[3] = {GPIO_PinSource9,GPIO_PinSource0,GPIO_PinSource9};
|
|
static const uint32 i2c_sda_gpio_clk[3] = {RCC_AHB1Periph_GPIOB,RCC_AHB1Periph_GPIOF,RCC_AHB1Periph_GPIOC};
|
|
static const uint32 i2c_af[3] = {GPIO_AF4_I2C1,GPIO_AF4_I2C2,GPIO_AF4_I2C3};
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
#error "STM32F407 i2c must in gpio soft mode!"
|
|
if(i2c_num>I2C_MAX_NUM)
|
|
{
|
|
return ERR_I2C_NUM_ERR;
|
|
}
|
|
i2c_mutex[i2c_num] = create_mutex();
|
|
RCC_APB1PeriphClockCmd(i2c_clk[i2c_num], ENABLE);
|
|
RCC_AHB1PeriphClockCmd(i2c_scl_gpio_clk[i2c_num] | i2c_sda_gpio_clk[i2c_num],ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
|
|
|
RCC_APB1PeriphResetCmd(i2c_clk[i2c_num], ENABLE);
|
|
RCC_APB1PeriphResetCmd(i2c_clk[i2c_num], DISABLE);
|
|
|
|
GPIO_PinAFConfig(i2c_scl_pin_port[i2c_num], i2c_scl_pin_source[i2c_num], i2c_af[i2c_num]);
|
|
GPIO_PinAFConfig(i2c_sda_pin_port[i2c_num], i2c_sda_pin_source[i2c_num], i2c_af[i2c_num]);
|
|
|
|
/*GPIO_InitStructure.GPIO_Pin = i2c_scl_pin[i2c_num];
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;*/
|
|
|
|
GPIO_InitStructure.Pin = i2c_scl_pin[i2c_num];
|
|
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
|
|
GPIO_InitStructure.Speed = GPIO_Speed_2MHz;
|
|
GPIO_InitStructure.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(i2c_scl_pin_port[i2c_num], &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.Pin = i2c_sda_pin[i2c_num];
|
|
HAL_GPIO_Init(i2c_sda_pin_port[i2c_num], &GPIO_InitStructure);
|
|
|
|
return i2c_init_bsp(i2c_num);
|
|
#endif
|
|
|
|
#endif
|
|
}
|
|
|
|
void TIM5_IRQ_Handler(void)
|
|
{
|
|
os_int_enter();
|
|
__HAL_TIM_CLEAR_IT(&htim5,TIM_IT_UPDATE);//TIM_ClearFlag(TIM5,TIM_IT_Update);
|
|
#if 1//_USE_RVL_
|
|
uart_timer_check();
|
|
#endif
|
|
os_int_exit();
|
|
}
|
|
|
|
/*初始化系统LED灯用到的gpio口,
|
|
|
|
LED PE1 配置为输出口,低电平亮
|
|
*/
|
|
void sys_led_gpio_init(void)
|
|
{
|
|
GPIO_InitTypeDef gpio_init_struct;
|
|
|
|
gpio_init_struct.Pin = GPIO_Pin_1;
|
|
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
gpio_init_struct.Speed = GPIO_Speed_2MHz;
|
|
gpio_init_struct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOE, &gpio_init_struct);
|
|
}
|
|
|
|
/*初始化系统按键用到的gpio口,配置为输入口,拉高
|
|
low --> key press
|
|
*/
|
|
void sys_key_gpio_init(void)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
|
|
GPIO_InitTypeDef gpio_init_struct;
|
|
|
|
/*gpio_init_struct.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
|
|
gpio_init_struct.GPIO_Mode = GPIO_Mode_IN;
|
|
gpio_init_struct.GPIO_OType = GPIO_OType_PP;
|
|
gpio_init_struct.GPIO_Speed = GPIO_Speed_2MHz;
|
|
gpio_init_struct.GPIO_PuPd = GPIO_PuPd_UP;*/
|
|
|
|
gpio_init_struct.Pin = GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
|
|
gpio_init_struct.Mode = GPIO_MODE_INPUT;
|
|
gpio_init_struct.Speed = GPIO_Speed_2MHz;
|
|
gpio_init_struct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOF, &gpio_init_struct);
|
|
|
|
/*gpio_init_struct.GPIO_Pin = GPIO_Pin_0;
|
|
gpio_init_struct.GPIO_Mode = GPIO_Mode_IN;
|
|
gpio_init_struct.GPIO_OType = GPIO_OType_PP;
|
|
gpio_init_struct.GPIO_Speed = GPIO_Speed_2MHz;
|
|
gpio_init_struct.GPIO_PuPd = GPIO_PuPd_UP;*/
|
|
|
|
gpio_init_struct.Pin = GPIO_Pin_0;
|
|
gpio_init_struct.Mode = GPIO_MODE_INPUT;
|
|
gpio_init_struct.Speed = GPIO_Speed_2MHz;
|
|
gpio_init_struct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOG, &gpio_init_struct);
|
|
|
|
#endif//_USE_RVL_
|
|
}
|
|
|
|
/*初始化系统LCD用到的gpio口,配置为输入口,拉高
|
|
配置的灯有 管脚
|
|
LCD_E PG3
|
|
CTL PG2
|
|
RS PG4
|
|
RW PG5
|
|
D0~D7 PE7~PE14
|
|
*/
|
|
void sys_lcd_gpio_init(void)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
|
|
GPIO_InitTypeDef gpio_init_struct;
|
|
|
|
/*gpio_init_struct.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3 |GPIO_Pin_4|GPIO_Pin_5;
|
|
gpio_init_struct.GPIO_Mode = GPIO_Mode_OUT;
|
|
gpio_init_struct.GPIO_OType = GPIO_OType_PP;
|
|
gpio_init_struct.GPIO_Speed = GPIO_Speed_50MHz;
|
|
gpio_init_struct.GPIO_PuPd = GPIO_PuPd_UP;*/
|
|
|
|
gpio_init_struct.Pin = GPIO_Pin_2|GPIO_Pin_3 |GPIO_Pin_4|GPIO_Pin_5;
|
|
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
gpio_init_struct.Speed = GPIO_Speed_50MHz;
|
|
gpio_init_struct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOG, &gpio_init_struct);
|
|
|
|
/*gpio_init_struct.GPIO_Pin = GPIO_Pin_7 |GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 |GPIO_Pin_11 |GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14;
|
|
gpio_init_struct.GPIO_Mode = GPIO_Mode_OUT;
|
|
gpio_init_struct.GPIO_OType = GPIO_OType_PP;
|
|
gpio_init_struct.GPIO_Speed = GPIO_Speed_50MHz;
|
|
gpio_init_struct.GPIO_PuPd = GPIO_PuPd_UP;*/
|
|
|
|
gpio_init_struct.Pin = GPIO_Pin_7 |GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 |GPIO_Pin_11 |GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14;
|
|
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
gpio_init_struct.Speed = GPIO_Speed_50MHz;
|
|
gpio_init_struct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOE, &gpio_init_struct);
|
|
|
|
#endif//_USE_RVL_
|
|
}
|
|
|
|
void tuner_gpio_init(void)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
|
|
GPIO_InitTypeDef gpio_init_struct;
|
|
|
|
/*gpio_init_struct.GPIO_Pin = GPIO_Pin_1;
|
|
gpio_init_struct.GPIO_Mode = GPIO_Mode_OUT;
|
|
gpio_init_struct.GPIO_OType = GPIO_OType_PP;
|
|
gpio_init_struct.GPIO_Speed = GPIO_Speed_50MHz;
|
|
gpio_init_struct.GPIO_PuPd = GPIO_PuPd_UP;*/
|
|
|
|
gpio_init_struct.Pin = GPIO_Pin_1;
|
|
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
gpio_init_struct.Speed = GPIO_Speed_50MHz;
|
|
gpio_init_struct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOG, &gpio_init_struct);
|
|
|
|
#endif//_USE_RVL_
|
|
}
|
|
//控制运行灯,有主任务秒消息驱动
|
|
void run_led_ctl(void)
|
|
{
|
|
//GPIO_ToggleBits(GPIOE,GPIO_Pin_1);
|
|
HAL_GPIO_TogglePin(GPIOE,GPIO_Pin_1);
|
|
}
|
|
|
|
//static int u32Val=0;
|
|
#define USER_SET_UPDATE_ADDRESS (RTC_BKP_DR19)
|
|
#define USER_UPDATE_FLAG (0X3366CC77) //要和boot一致
|
|
void SoftwareUpdateBegin(void)
|
|
{
|
|
#if 0//ETH_UPGRADE
|
|
RTC_WriteBackupRegister(USER_SET_UPDATE_ADDRESS,USER_UPDATE_FLAG);
|
|
|
|
RTC_ReadBackupRegister(USER_SET_UPDATE_ADDRESS);
|
|
NVIC_SystemReset();
|
|
#endif
|
|
}
|
|
|
|
//初始化与FLASH连接的SPI口,主要用于FPGA存储曲线
|
|
//4.3
|
|
//名称 cs clk miso mosi
|
|
//管脚 pa4 pa5 pa6 pb5
|
|
void spi_flash_com_init(void)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
|
|
#if USE_SPI_FLASH
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
//SPI_InitTypeDef SPI_InitStructure;
|
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
|
|
|
|
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
|
|
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
|
|
|
|
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
|
|
/*GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
|
|
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);*/
|
|
/*GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);*/
|
|
|
|
/*GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;*/
|
|
|
|
GPIO_InitStructure.Pin = GPIO_Pin_5|GPIO_Pin_6;
|
|
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
|
|
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.Pin = GPIO_Pin_5;
|
|
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
/*GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;*/
|
|
|
|
GPIO_InitStructure.Pin = GPIO_Pin_4;
|
|
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStructure.Pull = GPIO_PULLUP;
|
|
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
GPIO_SetBits(GPIOA, GPIO_Pin_4); //CS默认为高
|
|
|
|
/*SPI_I2S_DeInit(FLASH_SPI);
|
|
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
|
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
|
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
|
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
|
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
|
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
|
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //APB2 84M/4 21M
|
|
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
|
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
|
|
|
SPI_Cmd(FLASH_SPI,DISABLE);
|
|
SPI_Init(FLASH_SPI, &SPI_InitStructure);*/
|
|
|
|
/*SPI_CalculateCRC(FLASH_SPI,DISABLE); //禁用CRC功能
|
|
SPI_SSOutputCmd(FLASH_SPI,ENABLE);
|
|
SPI_Cmd(FLASH_SPI,ENABLE);*/
|
|
|
|
|
|
|
|
hspi1.Instance = FLASH_SPI;
|
|
HAL_SPI_DeInit(&hspi1);
|
|
|
|
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
|
hspi1.Init.Mode = SPI_MODE_MASTER;
|
|
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
|
|
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; //与原先6911平台保持一致
|
|
hspi1.Init.NSS = SPI_NSS_SOFT;
|
|
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; //APB1 42M/256 160k左右
|
|
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
hspi1.Init.CRCPolynomial = 7;
|
|
|
|
|
|
__HAL_SPI_DISABLE(&hspi1);
|
|
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
HAL_SPI_Init(&hspi1);
|
|
__HAL_SPI_ENABLE(&hspi1);
|
|
|
|
#endif
|
|
|
|
#endif//_USE_RVL_
|
|
}
|
|
|
|
//初始化与FPGA连接的SPI口,主要用于升级FPGA代码
|
|
//名称 cs clk miso mosi
|
|
//管脚 pb12 pb13 pb14 pb15
|
|
void spi_fpga_com_init(void)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
//SPI_InitTypeDef SPI_InitStructure;
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
|
|
|
|
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
|
|
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
|
|
|
|
GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF5_SPI2);
|
|
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF5_SPI2);
|
|
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF5_SPI2);
|
|
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF5_SPI2);
|
|
|
|
GPIO_InitStructure.Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
|
|
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
|
|
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.Pin = GPIO_Pin_12;
|
|
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStructure.Pull = GPIO_PULLUP;
|
|
GPIO_InitStructure.Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
GPIO_SetBits(GPIOB, GPIO_Pin_12); //CS默认为高
|
|
|
|
//SPI_I2S_DeInit(FPGA_SPI);
|
|
|
|
hspi2.Instance = FPGA_SPI;
|
|
HAL_SPI_DeInit(&hspi2);
|
|
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
|
|
hspi2.Init.Mode = SPI_MODE_MASTER;
|
|
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
|
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE; //与原先6911平台保持一致
|
|
hspi2.Init.NSS = SPI_NSS_SOFT;
|
|
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; //APB1 42M/256 160k左右
|
|
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
hspi2.Init.CRCPolynomial = 7;
|
|
|
|
/*SPI_Cmd(FPGA_SPI,DISABLE);
|
|
SPI_Init(FPGA_SPI, &SPI_InitStructure);
|
|
SPI_CalculateCRC(FPGA_SPI,DISABLE); //禁用CRC功能
|
|
SPI_SSOutputCmd(FPGA_SPI,ENABLE);
|
|
SPI_Cmd(FPGA_SPI,ENABLE);*/
|
|
|
|
__HAL_SPI_DISABLE(&hspi2);
|
|
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
HAL_SPI_Init(&hspi2);
|
|
__HAL_SPI_ENABLE(&hspi2);
|
|
|
|
#endif//_USE_RVL_
|
|
}
|
|
|
|
void spi_fpga_send_buf(uint8 *puc,uint32 length)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
//portENTER_CRITICAL();
|
|
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
|
|
while(length--)
|
|
{
|
|
//while (SPI_I2S_GetFlagStatus(FPGA_SPI, SPI_I2S_FLAG_TXE) == RESET);
|
|
while (__HAL_SPI_GET_FLAG(&hspi2, SPI_FLAG_TXE) == RESET);
|
|
SPI_I2S_SendData(FPGA_SPI, *puc++);
|
|
//while (SPI_I2S_GetFlagStatus(FPGA_SPI, SPI_I2S_FLAG_RXNE) == RESET);
|
|
while (__HAL_SPI_GET_FLAG(&hspi2, SPI_FLAG_RXNE) == RESET);
|
|
(void)SPI_I2S_ReceiveData(FPGA_SPI);
|
|
}
|
|
//while (SPI_I2S_GetFlagStatus(FPGA_SPI, SPI_I2S_FLAG_TXE) == RESET);
|
|
while (__HAL_SPI_GET_FLAG(&hspi2, SPI_FLAG_TXE) == RESET);
|
|
delay_us(5);
|
|
GPIO_SetBits(GPIOB, GPIO_Pin_12);
|
|
//portEXIT_CRITICAL();
|
|
|
|
#endif
|
|
}
|
|
|
|
|
|
void spect_spi_read(uint32 len,void *pdata,uint32 restart)
|
|
{
|
|
#if 1//_USE_RVL_
|
|
uint8 *pchar = (uint8*)pdata;
|
|
|
|
if((len==0)||(pdata==0))
|
|
return;
|
|
|
|
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
|
|
if(restart==1)
|
|
{
|
|
//while (SPI_I2S_GetFlagStatus(FPGA_SPI, SPI_I2S_FLAG_TXE) == RESET);
|
|
while (__HAL_SPI_GET_FLAG(&hspi2, SPI_FLAG_TXE) == RESET);
|
|
SPI_I2S_SendData(FPGA_SPI, 0xA5); //若需要重新读取数据,发送a5
|
|
|
|
//while (SPI_I2S_GetFlagStatus(FPGA_SPI, SPI_I2S_FLAG_RXNE) == RESET);
|
|
while (__HAL_SPI_GET_FLAG(&hspi2, SPI_FLAG_RXNE) == RESET);
|
|
(void)SPI_I2S_ReceiveData(FPGA_SPI);
|
|
}
|
|
|
|
while(len--)
|
|
{
|
|
//while (SPI_I2S_GetFlagStatus(FPGA_SPI, SPI_I2S_FLAG_TXE) == RESET);
|
|
while (__HAL_SPI_GET_FLAG(&hspi2, SPI_FLAG_TXE) == RESET);
|
|
SPI_I2S_SendData(FPGA_SPI, 0xFF);
|
|
|
|
//while (SPI_I2S_GetFlagStatus(FPGA_SPI, SPI_I2S_FLAG_RXNE) == RESET);
|
|
while (__HAL_SPI_GET_FLAG(&hspi2, SPI_FLAG_RXNE) == RESET);
|
|
*pchar++ = SPI_I2S_ReceiveData(FPGA_SPI);
|
|
}
|
|
//while (SPI_I2S_GetFlagStatus(FPGA_SPI, SPI_I2S_FLAG_TXE) == RESET);
|
|
while (__HAL_SPI_GET_FLAG(&hspi2, SPI_FLAG_TXE) == RESET);
|
|
|
|
delay_us(5);
|
|
GPIO_SetBits(GPIOB, GPIO_Pin_12);
|
|
|
|
#endif
|
|
}
|
|
|
|
void tuner_config_power_on(void)
|
|
{//低电平复位tuner,
|
|
#if 0//@TMP_UNUSED_TUNER
|
|
GPIO_ResetBits(GPIOG, GPIO_Pin_1);
|
|
delay_ticks(5);
|
|
GPIO_SetBits(GPIOG, GPIO_Pin_1);
|
|
#endif
|
|
}
|
|
|
|
void tuner_config_power_reset(void)
|
|
{
|
|
#if 0//@TMP_UNUSED_TUNER
|
|
GPIO_ResetBits(GPIOG, GPIO_Pin_1);
|
|
delay_ticks(300);
|
|
GPIO_SetBits(GPIOG, GPIO_Pin_1);
|
|
#endif
|
|
}
|
|
|
|
void set_exc_reset(void)
|
|
{
|
|
NVIC_SystemReset();
|
|
}
|
|
|
|
|
|
#define USE_STD_LIB 1
|
|
#if USE_STD_LIB
|
|
|
|
#define HAL_STD_GPIO 1
|
|
#if HAL_STD_GPIO
|
|
|
|
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_GPIO_PIN(GPIO_Pin));
|
|
|
|
GPIOx->BSRR = GPIO_Pin;
|
|
}
|
|
|
|
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_GPIO_PIN(GPIO_Pin));
|
|
|
|
GPIOx->BSRR = GPIO_Pin << 16;
|
|
}
|
|
|
|
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
|
|
{
|
|
return ((uint16_t)GPIOx->IDR);
|
|
}
|
|
|
|
#define CR_PLS_MASK PWR_CR_PLS
|
|
void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel)
|
|
{
|
|
uint32_t tmpreg = 0;
|
|
|
|
/* Check the parameters */
|
|
assert_param(IS_PWR_PVD_LEVEL(PWR_PVDLevel));
|
|
|
|
tmpreg = PWR->CR;
|
|
|
|
/* Clear PLS[7:5] bits */
|
|
tmpreg &= CR_PLS_MASK;
|
|
|
|
/* Set PLS[7:5] bits according to PWR_PVDLevel value */
|
|
tmpreg |= PWR_PVDLevel;
|
|
|
|
/* Store the new value */
|
|
PWR->CR = tmpreg;
|
|
}
|
|
|
|
#endif
|
|
|
|
void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_AHB1_CLOCK_PERIPH(RCC_AHB1Periph));
|
|
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->AHB1ENR |= RCC_AHB1Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->AHB1ENR &= ~RCC_AHB1Periph;
|
|
}
|
|
}
|
|
|
|
void RCC_AHB2PeriphClockCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->AHB2ENR |= RCC_AHB2Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->AHB2ENR &= ~RCC_AHB2Periph;
|
|
}
|
|
}
|
|
|
|
void RCC_AHB3PeriphClockCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_AHB3_PERIPH(RCC_AHB3Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->AHB3ENR |= RCC_AHB3Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->AHB3ENR &= ~RCC_AHB3Periph;
|
|
}
|
|
}
|
|
|
|
|
|
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->APB2ENR |= RCC_APB2Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->APB2ENR &= ~RCC_APB2Periph;
|
|
}
|
|
}
|
|
|
|
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->APB1ENR |= RCC_APB1Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->APB1ENR &= ~RCC_APB1Periph;
|
|
}
|
|
}
|
|
|
|
void RCC_AHB1PeriphResetCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_AHB1_RESET_PERIPH(RCC_AHB1Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->AHB1RSTR |= RCC_AHB1Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->AHB1RSTR &= ~RCC_AHB1Periph;
|
|
}
|
|
}
|
|
|
|
void RCC_AHB2PeriphResetCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->AHB2RSTR |= RCC_AHB2Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->AHB2RSTR &= ~RCC_AHB2Periph;
|
|
}
|
|
}
|
|
|
|
void RCC_AHB3PeriphResetCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_AHB3_PERIPH(RCC_AHB3Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->AHB3RSTR |= RCC_AHB3Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->AHB3RSTR &= ~RCC_AHB3Periph;
|
|
}
|
|
}
|
|
|
|
void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->APB1RSTR |= RCC_APB1Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->APB1RSTR &= ~RCC_APB1Periph;
|
|
}
|
|
}
|
|
|
|
void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_RCC_APB2_RESET_PERIPH(RCC_APB2Periph));
|
|
assert_param(IS_FUNCTIONAL_STATE(NewState));
|
|
if (NewState != DISABLE)
|
|
{
|
|
RCC->APB2RSTR |= RCC_APB2Periph;
|
|
}
|
|
else
|
|
{
|
|
RCC->APB2RSTR &= ~RCC_APB2Periph;
|
|
}
|
|
}
|
|
|
|
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
|
|
{
|
|
uint32_t temp = 0x00;
|
|
uint32_t temp_2 = 0x00;
|
|
|
|
/* Check the parameters */
|
|
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
|
|
assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
|
|
assert_param(IS_GPIO_AF(GPIO_AF));
|
|
|
|
temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
|
|
GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
|
|
temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
|
|
GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
|
|
}
|
|
|
|
|
|
|
|
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_USART_ALL_PERIPH(USARTx));
|
|
//assert_param(IS_USART_DATA(Data));
|
|
|
|
/* Transmit Data */
|
|
USARTx->DR = (Data & (uint16_t)0x01FF);
|
|
}
|
|
|
|
uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_USART_ALL_PERIPH(USARTx));
|
|
|
|
/* Receive Data */
|
|
return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
|
|
}
|
|
|
|
|
|
uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
|
|
|
|
/* Return the data in the DR register */
|
|
return SPIx->DR;
|
|
}
|
|
|
|
void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)
|
|
{
|
|
/* Check the parameters */
|
|
assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
|
|
|
|
/* Write in the DR register the data to be sent */
|
|
SPIx->DR = Data;
|
|
}
|
|
|
|
void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
|
|
{
|
|
__IO uint32_t tmp = 0;
|
|
|
|
/* Check the parameters */
|
|
assert_param(IS_RTC_BKP(RTC_BKP_DR));
|
|
|
|
tmp = RTC_BASE + 0x50;
|
|
tmp += (RTC_BKP_DR * 4);
|
|
|
|
/* Write the specified register */
|
|
*(__IO uint32_t *)tmp = (uint32_t)Data;
|
|
}
|
|
|
|
uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
|
|
{
|
|
__IO uint32_t tmp = 0;
|
|
|
|
/* Check the parameters */
|
|
assert_param(IS_RTC_BKP(RTC_BKP_DR));
|
|
|
|
tmp = RTC_BASE + 0x50;
|
|
tmp += (RTC_BKP_DR * 4);
|
|
|
|
/* Read the specified register */
|
|
return (*(__IO uint32_t *)tmp);
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|