123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include <stdio.h>
- #include <stdint.h>
- #include "main.h"
- #include "uart.h"
- #include "string.h"
- #include "Bluecell_operate.h"
- #include "CRC.h"
- #define DEBUG_PRINT 0
- UARTQUEUE TerminalQueue;
- UARTQUEUE WifiQueue;
- uart_hal_tx_type uart_hal_tx;
- extern volatile uint32_t UartRxTimerCnt;
- extern bool Bluecell_Operate(uint8_t* data);
- extern void MBIC_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_IT(&hTerminal, pQueue->Buffer + pQueue->head, 1) != HAL_OK)
- {
- printf("Comunication ERROR \r\n");
- }
-
-
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- pUARTQUEUE pQueue;
-
-
- 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);
- if (HAL_UART_Receive_IT(&hTerminal, pQueue->Buffer + pQueue->head, 1) != HAL_OK)
- {
- printf("Comunication ERROR \r\n");
-
- }
-
- }
- 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++;
-
- }
- volatile uint8_t uart_buf[QUEUE_BUFFER_LENGTH];
- void GetDataFromUartQueue(UART_HandleTypeDef *huart)
- {
- volatile static int cnt;
- bool chksumret = 0;
- uint16_t Length = 0;
- uint16_t CrcChk = 0;
- pUARTQUEUE pQueue = &TerminalQueue;
- uart_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);
- pQueue->tail++;
- pQueue->data--;
- if (pQueue->tail >= QUEUE_BUFFER_LENGTH) {
- pQueue->tail = 0;
- }
-
-
- if(pQueue->data == 0){
-
- #if DEBUG_PRINT
- printf("\r\n[RX]");
- for(int i = 0; i < cnt; i++){
- printf("%02x ",uart_buf[i]);
- }
- printf(ANSI_COLOR_GREEN"\r\n CNT : %d \r\n"ANSI_COLOR_RESET,cnt);
- #endif
- if(uart_buf[0] == 0xbe){
- Length = uart_buf[BLUECELL_LENGTH_H] << 8 | uart_buf[BLUECELL_LENGTH_L] ;
- CrcChk = uart_buf[Length + 1] << 8 | uart_buf[Length + 2] ;
- if(CRC16_Check(&uart_buf[BLUECELL_TYPE], Length,CrcChk) == NO_ERROR && Length != 0){
- 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 - 4,uart_buf[MBIC_CHECKSHUM_INDEX]))
- {
- Length = ((uart_buf[MBIC_LENGTH_0] << 8) | uart_buf[MBIC_LENGTH_1]);
- CrcChk = ((uart_buf[MBIC_PAYLOADSTART + Length ] << 8) | (uart_buf[MBIC_PAYLOADSTART + Length + 1]));
- if(CRC16_Check(&uart_buf[MBIC_PAYLOADSTART], Length,CrcChk) == NO_ERROR)
- MBIC_Operate(uart_buf);
- else
- printf("CRC ERROR \r\n");
- }
- else
- {
- printf("CHECK SUM ERR \r\n");
- }
- }
- memset(uart_buf,0x00,QUEUE_BUFFER_LENGTH);
- cnt = 0;
- }
- }
- 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[0],size);
-
- #if 0
- printf("\r\n [TX] : ");
- for(int i = 0; i< size; i++)
- printf("%02x ",data[i]);
- printf("\r\n");
- #endif
-
- }
|