添加低功耗串口LPUART1驱动,使用内部16M时钟
This commit is contained in:
parent
bcd0c963d7
commit
d92e4d08a7
|
@ -19,6 +19,7 @@ typedef u_int32_t device_handle;
|
|||
extern device_handle g_term_uart_handle;
|
||||
extern device_handle g_rs485_uart_handle;
|
||||
extern device_handle g_ec801_uart_handle;
|
||||
extern device_handle g_rain_uart_handle;
|
||||
|
||||
#define RS485_MAX_PACK_DATA_LEN 30
|
||||
|
||||
|
@ -42,6 +43,7 @@ void uart_dev_write(device_handle device, void *data, int len);
|
|||
void init_term_uart();
|
||||
void init_rs485_uart();
|
||||
void init_ec801_uart();
|
||||
void init_rain_uart();
|
||||
void uart_close(uartIndex_e uart_index);
|
||||
void term_printf(char *format, ...);
|
||||
int term_uart_readln(u_int8_t *buff, int buff_size, u_int32_t timeout_ms);
|
||||
|
|
|
@ -14,15 +14,18 @@ static u_int8_t uart_putchar(device_handle device, char ch);
|
|||
|
||||
device_handle g_term_uart_handle;
|
||||
device_handle g_rs485_uart_handle;
|
||||
device_handle g_rain_uart_handle;
|
||||
device_handle g_ec801_uart_handle;
|
||||
|
||||
device_handle uart_dev_init(uartIndex_e uart_index, uint8_t *buff, int buff_size);
|
||||
static u_int8_t term_in_buff[200];
|
||||
static u_int8_t ec801_in_buff[200];
|
||||
static u_int8_t rs485_in_buff[300];
|
||||
static u_int8_t rain_in_buff[50];
|
||||
|
||||
u_int8_t rs485_out_buff[100];
|
||||
u_int8_t ec801_out_buff[100];
|
||||
u_int8_t rain_out_buff[50];
|
||||
static u_int8_t term_out_buff[100];
|
||||
|
||||
/**
|
||||
|
@ -47,6 +50,11 @@ uart_device_info uart_devices[]={
|
|||
.uart_index = EC801_UART_INDEX,
|
||||
.uart_baudrate = 115200,
|
||||
},
|
||||
[3] = {
|
||||
.init = 0,
|
||||
.uart_index = RAIN_RS485_UART_INDEX,
|
||||
.uart_baudrate = 4800,
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -92,6 +100,10 @@ static void uart_init(uartIndex_e uart_index, int baud)
|
|||
MX_UART5_Init(baud);
|
||||
// MX_USART5_UART_Init();
|
||||
}
|
||||
else if(uart_index == RAIN_RS485_UART_INDEX){
|
||||
MX_LPUART1_UART_Init(baud);
|
||||
// MX_LPUART1_UART_Init();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,6 +119,8 @@ void uart_close(uartIndex_e uart_index)
|
|||
HAL_UART_MspDeInit(&huart3);
|
||||
}else if(uart_index == EC801_UART_INDEX){
|
||||
HAL_UART_MspDeInit(&huart5);
|
||||
}else if(uart_index == RAIN_RS485_UART_INDEX){
|
||||
HAL_UART_MspDeInit(&hlpuart1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,6 +146,8 @@ static u_int8_t uart_putchar(device_handle device, char ch)
|
|||
ret= HAL_UART_Transmit(&huart3,(uint8_t*)&ch,1,10);
|
||||
}else if(device_info->uart_index == EC801_UART_INDEX){
|
||||
ret= HAL_UART_Transmit(&huart5,(uint8_t*)&ch,1,10);
|
||||
}else if(device_info->uart_index == RAIN_RS485_UART_INDEX){
|
||||
ret= HAL_UART_Transmit(&hlpuart1,(uint8_t*)&ch,1,10);
|
||||
}
|
||||
|
||||
if(ret == HAL_OK){
|
||||
|
@ -331,3 +347,12 @@ void init_ec801_uart()
|
|||
g_ec801_uart_handle = uart_dev_init(EC801_UART_INDEX, ec801_in_buff, sizeof(ec801_in_buff));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ³õʼ»¯ÓêÁ¿´®¿Ú.
|
||||
* @retval None
|
||||
*/
|
||||
void init_rain_uart()
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIO_RAIN_PWR_CTRL_GPIO_Port, GPIO_RAIN_PWR_CTRL_Pin, GPIO_PIN_SET);
|
||||
g_rain_uart_handle = uart_dev_init(RAIN_RS485_UART_INDEX, rain_in_buff, sizeof(rain_in_buff));
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ extern UART_HandleTypeDef huart3;
|
|||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_LPUART1_UART_Init(void);
|
||||
void MX_LPUART1_UART_Init(int baud);
|
||||
void MX_UART5_Init(int baud);
|
||||
void MX_USART1_UART_Init(int baud);
|
||||
void MX_USART3_UART_Init(int baud);
|
||||
|
|
|
@ -152,7 +152,7 @@ void Flash_EnableReadProtection(void)
|
|||
MX_GPIO_Init();
|
||||
MX_DMA_Init();
|
||||
MX_ADC1_Init();
|
||||
MX_LPUART1_UART_Init();
|
||||
// MX_LPUART1_UART_Init(4800);
|
||||
// MX_USART1_UART_Init();
|
||||
// MX_USART3_UART_Init();
|
||||
MX_TIM2_Init();
|
||||
|
@ -167,6 +167,7 @@ void Flash_EnableReadProtection(void)
|
|||
/* USER CODE BEGIN 2 */
|
||||
init_term_uart();
|
||||
init_rs485_uart();
|
||||
init_rain_uart();uart_dev_write(g_rain_uart_handle, "Hello", 5);
|
||||
// init_ec801_uart();
|
||||
term_printf("Micro Climate Station Sensor.\r\n");
|
||||
term_printf("Version 1.0.0 Build: %s %s\r\n",__DATE__,__TIME__);
|
||||
|
@ -215,8 +216,10 @@ void SystemClock_Config(void)
|
|||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_HSI;
|
||||
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
||||
RCC_OscInitStruct.PLL.PLLM = 1;
|
||||
|
|
|
@ -279,13 +279,20 @@ void UART5_IRQHandler(void)
|
|||
*/
|
||||
void LPUART1_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN LPUART1_IRQn 0 */
|
||||
|
||||
/* USER CODE END LPUART1_IRQn 0 */
|
||||
/* USER CODE BEGIN USART3_IRQn 0 */
|
||||
uint8_t c = 0;
|
||||
/* USER CODE END USART3_IRQn 0 */
|
||||
HAL_UART_IRQHandler(&hlpuart1);
|
||||
/* USER CODE BEGIN LPUART1_IRQn 1 */
|
||||
/* USER CODE BEGIN USART3_IRQn 1 */
|
||||
uart_device_info *dev = (uart_device_info *)g_rain_uart_handle;
|
||||
HAL_UART_Receive_IT(&hlpuart1, rx_uart2_buf,1);
|
||||
c = rx_uart2_buf[0];
|
||||
|
||||
/* USER CODE END LPUART1_IRQn 1 */
|
||||
if(!RingQueueFull(&dev->uart_ring_queue))
|
||||
InRingQueue(&dev->uart_ring_queue, c);
|
||||
|
||||
HAL_UART_Receive_IT(&hlpuart1, rx_uart2_buf,1);
|
||||
/* USER CODE END USART3_IRQn 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
|
|
@ -34,7 +34,7 @@ UART_HandleTypeDef huart3;
|
|||
|
||||
/* LPUART1 init function */
|
||||
|
||||
void MX_LPUART1_UART_Init(void)
|
||||
void MX_LPUART1_UART_Init(int baud)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LPUART1_Init 0 */
|
||||
|
@ -45,8 +45,8 @@ void MX_LPUART1_UART_Init(void)
|
|||
|
||||
/* USER CODE END LPUART1_Init 1 */
|
||||
hlpuart1.Instance = LPUART1;
|
||||
hlpuart1.Init.BaudRate = 209700;
|
||||
hlpuart1.Init.WordLength = UART_WORDLENGTH_7B;
|
||||
hlpuart1.Init.BaudRate = baud;
|
||||
hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
hlpuart1.Init.StopBits = UART_STOPBITS_1;
|
||||
hlpuart1.Init.Parity = UART_PARITY_NONE;
|
||||
hlpuart1.Init.Mode = UART_MODE_TX_RX;
|
||||
|
@ -58,7 +58,7 @@ void MX_LPUART1_UART_Init(void)
|
|||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN LPUART1_Init 2 */
|
||||
//HAL_UART_Receive_IT(&hlpuart1, rx_uart2_buf,1);
|
||||
HAL_UART_Receive_IT(&hlpuart1, rx_uart2_buf,1);
|
||||
/* USER CODE END LPUART1_Init 2 */
|
||||
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
|||
/** Initializes the peripherals clock
|
||||
*/
|
||||
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
|
||||
PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
|
||||
PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
|
@ -192,7 +192,8 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
|||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN LPUART1_MspInit 1 */
|
||||
|
||||
HAL_NVIC_SetPriority(LPUART1_IRQn, 5, 0);
|
||||
HAL_NVIC_EnableIRQ(LPUART1_IRQn);
|
||||
/* USER CODE END LPUART1_MspInit 1 */
|
||||
}
|
||||
else if(uartHandle->Instance==UART5)
|
||||
|
@ -338,7 +339,7 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
|||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11);
|
||||
|
||||
/* USER CODE BEGIN LPUART1_MspDeInit 1 */
|
||||
|
||||
HAL_NVIC_DisableIRQ(LPUART1_IRQn);
|
||||
/* USER CODE END LPUART1_MspDeInit 1 */
|
||||
}
|
||||
else if(uartHandle->Instance==UART5)
|
||||
|
|
Loading…
Reference in New Issue