240 lines
7.3 KiB
C
240 lines
7.3 KiB
C
/********************************** (C) COPYRIGHT *******************************
|
|
* File Name : peripheral.C
|
|
* Author : WCH
|
|
* Version : v1.0
|
|
* Date : 2020/11/26
|
|
* Description :
|
|
*********************************************************************************
|
|
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
|
* Attention: This software (modified or not) and binary are used for
|
|
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
|
*******************************************************************************/
|
|
|
|
/*********************************************************************
|
|
* INCLUDES
|
|
*/
|
|
#include "CONFIG.h"
|
|
#include "devinfoservice.h"
|
|
#include "gattprofile.h"
|
|
#include "peripheral.h"
|
|
#include "app_uart.h"
|
|
|
|
/*********************************************************************
|
|
* MACROS
|
|
*/
|
|
//The buffer length should be a power of 2
|
|
#define APP_UART_TX_BUFFER_LENGTH 512U
|
|
#define APP_UART_RX_BUFFER_LENGTH 2048U
|
|
|
|
/*********************************************************************
|
|
* CONSTANTS
|
|
*/
|
|
|
|
/*********************************************************************
|
|
* TYPEDEFS
|
|
*/
|
|
|
|
/*********************************************************************
|
|
* GLOBAL VARIABLES
|
|
*/
|
|
uint8_t to_test_buffer[BLE_BUFF_MAX_LEN - 4 - 3];
|
|
|
|
app_drv_fifo_t app_uart_tx_fifo;
|
|
app_drv_fifo_t app_uart_rx_fifo;
|
|
|
|
//interupt uart rx flag ,clear at main loop
|
|
bool uart_rx_flag = false;
|
|
|
|
//for interrupt rx blcak hole ,when uart rx fifo full
|
|
uint8_t for_uart_rx_black_hole = 0;
|
|
|
|
//fifo length less that MTU-3, retry times
|
|
uint32_t uart_to_ble_send_evt_cnt = 0;
|
|
|
|
/*********************************************************************
|
|
* EXTERNAL VARIABLES
|
|
*/
|
|
|
|
/*********************************************************************
|
|
* EXTERNAL FUNCTIONS
|
|
*/
|
|
|
|
/*********************************************************************
|
|
* LOCAL VARIABLES
|
|
*/
|
|
//
|
|
|
|
//The tx buffer and rx buffer for app_drv_fifo
|
|
//length should be a power of 2
|
|
static uint8_t app_uart_tx_buffer[APP_UART_TX_BUFFER_LENGTH] = {0};
|
|
static uint8_t app_uart_rx_buffer[APP_UART_RX_BUFFER_LENGTH] = {0};
|
|
|
|
|
|
/*********************************************************************
|
|
* LOCAL FUNCTIONS
|
|
*/
|
|
|
|
/*********************************************************************
|
|
* PROFILE CALLBACKS
|
|
*/
|
|
|
|
/*********************************************************************
|
|
* PUBLIC FUNCTIONS
|
|
*/
|
|
|
|
/*********************************************************************
|
|
* @fn app_uart_process
|
|
*
|
|
* @brief process uart data
|
|
*
|
|
* @return NULL
|
|
*/
|
|
void app_uart_process(void)
|
|
{
|
|
uint8_t data;
|
|
__disable_irq();
|
|
if(uart_rx_flag)
|
|
{
|
|
tmos_start_task(Peripheral_TaskID, UART_TO_BLE_SEND_EVT, 2);
|
|
uart_rx_flag = false;
|
|
}
|
|
__enable_irq();
|
|
|
|
|
|
while(app_drv_fifo_length(&app_uart_tx_fifo))
|
|
{
|
|
app_drv_fifo_read_to_same_addr(&app_uart_tx_fifo, (uint8_t *)&data, 1);
|
|
USART_SendData(USART3, data);
|
|
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
|
|
{
|
|
}
|
|
}
|
|
}
|
|
/*********************************************************************
|
|
* @fn app_uart_init
|
|
*
|
|
* @brief init uart
|
|
*
|
|
* @return NULL
|
|
*/
|
|
void app_uart_init()
|
|
{
|
|
//tx fifo and tx fifo
|
|
//The buffer length should be a power of 2
|
|
app_drv_fifo_init(&app_uart_tx_fifo, app_uart_tx_buffer, APP_UART_TX_BUFFER_LENGTH);
|
|
app_drv_fifo_init(&app_uart_rx_fifo, app_uart_rx_buffer, APP_UART_RX_BUFFER_LENGTH);
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
|
USART_InitTypeDef USART_InitStructure = {0};
|
|
NVIC_InitTypeDef NVIC_InitStructure = {0};
|
|
|
|
//uart3 init
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
|
|
/* USART3 TX-->B.10 RX-->B.11 */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
USART_InitStructure.USART_BaudRate = 115200;
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
|
|
|
|
USART_Init(USART3, &USART_InitStructure);
|
|
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
USART_Cmd(USART3, ENABLE);
|
|
}
|
|
|
|
/*********************************************************************
|
|
* @fn app_uart_tx_data
|
|
*
|
|
* @brief app_uart_tx_data
|
|
*
|
|
* @return NULL
|
|
*/
|
|
void app_uart_tx_data(uint8_t *data, uint16_t length)
|
|
{
|
|
uint16_t write_length = length;
|
|
app_drv_fifo_write(&app_uart_tx_fifo, data, &write_length);
|
|
}
|
|
|
|
/*********************************************************************
|
|
* @fn UART3_IRQHandler
|
|
*
|
|
* @brief Not every uart reception will end with a UART_II_RECV_TOUT
|
|
* UART_II_RECV_TOUT can only be triggered when R8_UARTx_RFC is not 0
|
|
* Here we cannot rely UART_II_RECV_TOUT as the end of a uart reception
|
|
*
|
|
* @return NULL
|
|
*/
|
|
__attribute__((interrupt("WCH-Interrupt-fast")))
|
|
void USART3_IRQHandler(void)
|
|
{
|
|
uint16_t error;
|
|
uint8_t data;
|
|
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
|
|
{
|
|
data = USART_ReceiveData(USART3);
|
|
error = app_drv_fifo_write_from_same_addr(&app_uart_rx_fifo, (uint8_t *)&data, 1);
|
|
if(error != APP_DRV_FIFO_RESULT_SUCCESS)
|
|
{
|
|
PRINT("APP_DRV_FIFO_RESULT_NOT_MEM\r\n");
|
|
}
|
|
uart_rx_flag = true;
|
|
}
|
|
}
|
|
|
|
/*********************************************************************
|
|
* @fn on_bleuartServiceEvt
|
|
*
|
|
* @brief ble uart service callback handler
|
|
*
|
|
* @return NULL
|
|
*/
|
|
void on_bleuartServiceEvt(uint16_t connection_handle, ble_uart_evt_t *p_evt)
|
|
{
|
|
switch(p_evt->type)
|
|
{
|
|
case BLE_UART_EVT_TX_NOTI_DISABLED:
|
|
PRINT("%02x:bleuart_EVT_TX_NOTI_DISABLED\r\n", connection_handle);
|
|
break;
|
|
case BLE_UART_EVT_TX_NOTI_ENABLED:
|
|
PRINT("%02x:bleuart_EVT_TX_NOTI_ENABLED\r\n", connection_handle);
|
|
break;
|
|
case BLE_UART_EVT_BLE_DATA_RECIEVED:
|
|
PRINT("BLE RX DATA len:%d\r\n", p_evt->data.length);
|
|
|
|
//for notify back test
|
|
//to ble
|
|
uint16_t to_write_length = p_evt->data.length;
|
|
app_drv_fifo_write(&app_uart_rx_fifo, (uint8_t *)p_evt->data.p_data, &to_write_length);
|
|
tmos_start_task(Peripheral_TaskID, UART_TO_BLE_SEND_EVT, 2);
|
|
//end of nofify back test
|
|
|
|
//ble to uart
|
|
app_uart_tx_data((uint8_t *)p_evt->data.p_data, p_evt->data.length);
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*********************************************************************
|
|
*********************************************************************/
|