123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /*
- * uart.c
- *
- * Created on: 2019. 5. 27.
- * Author: parkyj
- */
- #include <stdio.h>
- #include "main.h"
- #include "uart.h"
- #include "string.h"
- #include "Bluecell_operate.h"
- UARTQUEUE TerminalQueue;
- UARTQUEUE WifiQueue;
- uart_hal_tx_type uart_hal_tx;
- extern volatile uint32_t UartRxTimerCnt;
- extern bool Bluecell_Operate(uint8_t* data);
- void InitUartQueue(pUARTQUEUE pQueue)
- {
- pQueue->data = pQueue->head = pQueue->tail = 0;
- uart_hal_tx.output_p = uart_hal_tx.input_p = 0;
- if (HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1) != HAL_OK)
- {
- // _Error_Handler(__FILE__, __LINE__);
- }
- //HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1);
- //HAL_UART_Receive_IT(hTerminal, pQueue->Buffer + pQueue->head, 1);
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- pUARTQUEUE pQueue;
- // printf("Function : %s : \r\n",__func__);
- UartRxTimerCnt = 0;
- pQueue = &TerminalQueue;
- pQueue->head++;
- if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
- pQueue->data++;
- if (pQueue->data >= QUEUE_BUFFER_LENGTH)
- GetDataFromUartQueue(huart);
- HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 1);
- // Set_UartRcv(true);
- }
- void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
- {
- pUARTQUEUE pQueue = &TerminalQueue;
- if (pQueue->data >= QUEUE_BUFFER_LENGTH)
- GetDataFromUartQueue(huart);
- pQueue->Buffer[pQueue->head++] = data;
- if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
- pQueue->data++;
- // HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 10);
- }
- volatile uint8_t uart_buf[QUEUE_BUFFER_LENGTH];
- void GetDataFromUartQueue(UART_HandleTypeDef *huart)
- {
- volatile static int cnt;
-
- // UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
- // UART_HandleTypeDef *dst = &hTerminal;
- pUARTQUEUE pQueue = &TerminalQueue;
- // if (HAL_UART_Transmit(dst, pQueue->Buffer + pQueue->tail, 1, 3000) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- uart_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);
- //#ifdef DEBUG_PRINT
- // printf("%02x ",*(pQueue->Buffer + pQueue->tail)) ;
- //#endif /* DEBUG_PRINT */
- pQueue->tail++;
- if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
- pQueue->data--;
-
- if(pQueue->data == 0){
- // printf("data cnt zero !!! \r\n");
- //RF_Ctrl_Main(&uart_buf[Header]);
- // HAL_UART_Transmit(dst, &temp_buf[BLUECELL_HEADER00], 11, 3000);
- #if 0 // PYJ.2019.07.15_BEGIN --
- for(int i = 0; i < cnt; i++){
- printf("%02x ",uart_buf[i]);
- }
- printf("CNT : %d",cnt);
- #endif // PYJ.2019.07.15_END --
- if(uart_buf[0] == 0xbe){
- Bluecell_Operate(uart_buf);
- }else if(uart_buf[0] == MBIC_PREAMBLE0
- &&uart_buf[1] == MBIC_PREAMBLE1
- &&uart_buf[2] == MBIC_PREAMBLE2
- &&uart_buf[3] == MBIC_PREAMBLE3){
- if(Chksum_Check(uart_buf,MBIC_HEADER_SIZE - 1,uart_buf[MBIC_CHECKSHUM_INDEX])){
- MBIC_Operate(uart_buf);
- }
- else{
- printf("CHECK SUM ERR \r\n");
- }
- }
- memset(uart_buf,0x00,cnt);
- // for(int i = 0; i < cnt; i++)
- // uart_buf[i] = 0;
- cnt = 0;
- // HAL_Delay(1);
- }
- }
- void Uart_Check(void){
- while (TerminalQueue.data > 0 && UartRxTimerCnt > 50) GetDataFromUartQueue(&hTerminal);
- }
- void Uart1_Data_Send(uint8_t* data,uint16_t size){
- HAL_UART_Transmit_DMA(&hTerminal, data,size);
- }
|