123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * uart.c
- *
- * Created on: 2020. 8. 3.
- * Author: parkyj
- */
- #include <stdio.h>
- #include <stdint.h>
- #include "main.h"
- #include "string.h"
- #include "crc.h"
- #include "uart.h"
- #include "NessLab.h"
- extern void NessLab_Operate(uint8_t* data);
- UARTQUEUE TerminalQueue;
- UARTQUEUE MainQueue;
- uart_hal_tx_type uart_hal_tx;
- extern volatile uint32_t UartRxTimerCnt;
- volatile uint8_t uart_buf[QUEUE_BUFFER_LENGTH];
- extern bool Bluecell_Operate(uint8_t* data);
- extern void MBIC_Operate(uint8_t * data);
- extern bool NessLab_CheckSum_Check(uint8_t* data,uint8_t size,uint8_t checksum);
- void InitUartQueue(pUARTQUEUE pQueue)
- {
- pQueue->data = pQueue->head = pQueue->tail = 0;
- uart_hal_tx.output_p = uart_hal_tx.input_p = 0;
- // HAL_UART_Receive_IT(&huart2,rxBuf,5);
- if (HAL_UART_Receive_DMA(&hMain, MainQueue.Buffer, 1) != HAL_OK)
- //if (HAL_UART_Receive_IT(&hTerminal, TerminalQueue.Buffer, 1) != HAL_OK)
- {
- // _Error_Handler(__FILE__, __LINE__);
- }
- // if (HAL_UART_Receive_DMA(&hTest, 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)
- {
- // UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hTest:&hTerminal);
- pUARTQUEUE pQueue;
- // printf("Function : %s : \r\n",__func__);
- //printf("%02x ",uart_buf[i]);
- UartRxTimerCnt = 0;
- pQueue = &MainQueue;
- pQueue->head++;
- if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
- pQueue->data++;
- if (pQueue->data >= QUEUE_BUFFER_LENGTH)
- GetDataFromUartQueue(huart);
- HAL_UART_Receive_IT(&hMain, pQueue->Buffer + pQueue->head, 1);
- // HAL_UART_Receive_DMA(&hTest, pQueue->Buffer + pQueue->head, 1);
- // Set_UartRcv(true);
- }
- void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
- {
- pUARTQUEUE pQueue = &MainQueue;
- 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);
- }
- void GetDataFromUartQueue(UART_HandleTypeDef *huart)
- {
- volatile static int cnt;
- bool ret = 0;
- /* bool chksumret = 0;
- uint16_t Length = 0;
- uint16_t CrcChk = 0;
- UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hTest:&hTerminal);*/
- // UART_HandleTypeDef *dst = &hTerminal;
- pUARTQUEUE pQueue = &MainQueue;
- // 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 1// PYJ.2019.07.15_BEGIN --
- printf("\r\n[RX]");
- for(int i = 0; i < cnt; i++){
- printf("%02x ",uart_buf[i]);
- }
- // printf("Checksum Index : %d %x\r\n",uart_buf[NessLab_Req_DataLength] + NessLab_Req_DataLength + 1,uart_buf[uart_buf[NessLab_Req_DataLength] + NessLab_Req_DataLength + 1]);
- // printf(ANSI_COLOR_GREEN"\r\n CNT : %d \r\n"ANSI_COLOR_RESET,cnt);
- #endif // PYJ.2019.07.15_END --
- ret = NessLab_CheckSum_Check(&uart_buf[NessLab_Req_MsgID0],uart_buf[NessLab_DataLength] + 5 ,uart_buf[uart_buf[NessLab_Req_DataLength] + NessLab_Req_DataLength + 1]);
- if(ret == true){
- NessLab_Operate(&uart_buf[0]);
- printf("Checksum OK \r\n");
- }else{
- printf("Checksum Error \r\n");
- printf("uart_buf[NessLab_Req_DataLength] : %x \r\n",uart_buf[NessLab_Req_DataLength]);
- printf("NessLab_Req_DataLength : %d \r\n",NessLab_Req_DataLength);
- printf("Checksum Index : %d %x\r\n",uart_buf[NessLab_Req_DataLength] + NessLab_Req_DataLength + 1,uart_buf[uart_buf[NessLab_Req_DataLength] + NessLab_Req_DataLength + 1]);
- }
- cnt = 0;
- }
- }
- void Uart_Check(void){
- while (MainQueue.data > 0 && UartRxTimerCnt > 50) GetDataFromUartQueue(&hMain);
- }
- void Uart1_Data_Send(uint8_t* data,uint16_t size){
- HAL_UART_Transmit_DMA(&hMain, &data[0],size);
- //HAL_UART_Transmit_IT(&hTerminal, &data[0],size);
- // printf("data[278] : %x \r\n",data[278]);
- //// HAL_Delay(1);
- #if 1 // PYJ.2020.07.19_BEGIN --
- printf("\r\n [TX] : ");
- for(int i = 0; i< size; i++)
- printf("%02x ",data[i]);
- // printf("};\r\n\tCOUNT : %d \r\n",size);
- printf("\r\n");
- #endif // PYJ.2020.07.19_END --
- // printf("\r\n [TX] : {");
- // for(int i = 0; i< size; i++){
- // printf(",%02x ",data[i]);
- // data[i] = 0;
- // }
- // printf("};\r\n\tCOUNT : %d \r\n",size);
- // printf("\r\n");
-
- }
|