Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum messageType
- {
- messageType_Requst = 0x00,
- messageType_ACK = 0x06,
- messageType_NACK = 0x15,
- };
- typedef enum
- {
- FrameError,
- FrameNotReady,
- FrameReady,
- }UI_FrameError_t;
- typedef enum
- {
- ping = 0x01,
- setParameters = 0x02,
- getMeasurement = 0x03,
- getMeasurementRMS = 0x04,
- debugOn = 0x05,
- setPWM = 0x06,
- getStat = 0x07,
- clearFlags = 0x08,
- resetProcessor = 0x09,
- setMotorStatus = 0x10,
- debugTest = 0x20,
- }UI_Command_t;
- typedef struct {
- char *dataBuffer;
- int head;
- int tail;
- int count;
- int size;
- } UartBuffer;
- typedef struct
- {
- uint8_t praamble;
- uint8_t address;
- uint8_t sender;
- uint8_t messageType;
- UI_Command_t command;
- uint16_t lenghtData;
- uint8_t *data;
- uint16_t crc;
- }COMM_Frame_t;
- static uint8_t rxBuffer[RX_SIZE];
- void COMMUNICATION_Init(void)
- {
- USART1_UART_Init();
- HAL_UART_Receive_IT(&huart1,&uartData,1);
- __HAL_TIM_CLEAR_IT(&htim17,TIM_IT_UPDATE);
- UartBuffer_Init(&uartBuffer, (char *)rxBuffer, RX_SIZE);
- COMM_Message.data = ui_frame;
- }
- void USART1_IRQHandler(void)
- {
- HAL_UART_IRQHandler(&huart1);
- }
- void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
- {
- if (huart->Instance == USART1)
- {
- UartReady = SET;
- }
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- if(huart->Instance == USART1)
- {
- UartBuffer_PutChar(&uartBuffer, uartData);
- HAL_TIM_Base_Start_IT(&htim17);
- HAL_UART_Receive_IT(&huart1, &uartData, 1);
- }
- }
- bool UartBuffer_Init(UartBuffer *uartBuffer, char *dataBuffer, size_t dataBufferSize)
- {
- assert(uartBuffer);
- assert(dataBuffer);
- assert(dataBufferSize > 0);
- if ((uartBuffer) && (dataBuffer) && (dataBufferSize > 0)) {
- uartBuffer->dataBuffer = dataBuffer;
- uartBuffer->head = 0;
- uartBuffer->tail = 0;
- uartBuffer->count = 0;
- uartBuffer->size = dataBufferSize;
- return true;
- }
- return false;
- }
- bool UartBuffer_IsEmpty(const UartBuffer *uartBuffer)
- {
- assert(uartBuffer);
- if (uartBuffer->head == uartBuffer->tail && uartBuffer->count == 0)
- return true;
- else
- return false;
- }
- bool UartBuffer_Clear(UartBuffer *uartBuffer)
- {
- assert(uartBuffer);
- if (uartBuffer) {
- uartBuffer->dataBuffer[0] = 0;
- uartBuffer->dataBuffer[1] = 0;
- uartBuffer->dataBuffer[2] = 0;
- uartBuffer->dataBuffer[3] = 0;
- uartBuffer->dataBuffer[4] = 0;
- uartBuffer->dataBuffer[5] = 0;
- uartBuffer->dataBuffer[6] = 0;
- uartBuffer->dataBuffer[7] = 0;
- uartBuffer->head = 0;
- uartBuffer->tail = 0;
- uartBuffer->count = 0;
- return true;
- }
- return false;
- }
- bool UartBuffer_PutChar(UartBuffer *uartBuffer, char c)
- {
- assert(uartBuffer);
- if (uartBuffer) {
- if (uartBuffer->count < uartBuffer->size)
- {
- uartBuffer->dataBuffer[uartBuffer->head++] = c;
- uartBuffer->count++;
- if (uartBuffer->head >= uartBuffer->size) uartBuffer->head = 0;
- return true;
- }
- }
- return false;
- }
- bool UartBuffer_GetChar(UartBuffer *uartBuffer, char *c)
- {
- assert(uartBuffer);
- assert(c);
- if ((uartBuffer) && (c)) {
- if (uartBuffer->count > 0)
- {
- *c = uartBuffer->dataBuffer[uartBuffer->tail++];
- uartBuffer->count--;
- if (uartBuffer->tail >= uartBuffer->size) uartBuffer->tail = 0;
- return 0;
- }
- }
- return 1;
- }
- UI_FrameError_t COMMUNICATION_ProcessByte(uint8_t byte)
- {
- static uint8_t idx;
- if((idx > DATA_OFFSET) && (idx-DATA_OFFSET >= UI_RX_SIZE))
- {
- idx = 0;
- return FrameError;
- }
- if(idx == PREAMBLE_OFFSET)
- {
- if(byte == FRAME_START)
- COMM_Message.praamble = byte;
- else
- {
- idx = 0;
- return FrameError;
- }
- }
- else if(idx == ADDRESS_OFFSET)
- COMM_Message.address = byte;
- else if(idx == SENDER_OFFSET)
- COMM_Message.sender = byte;
- else if(idx == MESSAGETYPE_OFFSET)
- COMM_Message.messageType = byte;
- else if(idx == COMMAND_OFFSET)
- COMM_Message.command = byte;
- else if(idx == LENGHTDATA_OFFSET)
- COMM_Message.lenghtData = byte;
- else if(idx == LENGHTDATA_OFFSET + 1)
- COMM_Message.lenghtData += byte << 8;
- else
- {
- if(idx < DATA_OFFSET)
- {
- idx = 0;
- return FrameError;
- }
- if(idx < COMM_Message.lenghtData + DATA_OFFSET)
- {
- COMM_Message.data[idx - DATA_OFFSET] = byte;
- }
- else
- {
- if(idx == COMM_Message.lenghtData + DATA_OFFSET)
- COMM_Message.crc = byte;
- else if(idx == COMM_Message.lenghtData + DATA_OFFSET + 1)
- {
- COMM_Message.crc += byte << 8;
- idx = 0;
- return FrameReady;
- }
- }
- }
- idx++;
- return FrameNotReady;
- }
- /******************************************************************************/
- void COMMUNICATION_ProcessMessage(void)
- {
- if(COMMUNICATION_CalcCRC() != COMM_Message.crc)
- {
- PRINTF_HW("CRC BAD\n");
- }
- else if( COMM_Message.address == ADDRESS_UI &&
- (COMM_Message.sender == ADDRESS_MOBO) &&
- COMM_Message.messageType == messageType_ACK
- )
- {
- switch (COMM_Message.command) {
- case ping:
- break;
- case setParameters:
- RESPOND_STER();
- break;
- case getMeasurement:
- PARAM_PROCESS(COMM_Message.data,COMM_Message.lenghtData);
- break;
- case getMeasurementRMS:
- break;
- case debugOn:
- break;
- case setPWM:
- RESPOND_PWM();
- break;
- case getStat:
- RESPOND_STATUS();
- PROCESS_STATUS(COMM_Message.data,COMM_Message.lenghtData);
- break;
- case clearFlags:
- RESPOND_CLEAN();
- break;
- case resetProcessor:
- RESPOND_RESET();
- break;
- case debugTest:
- break;
- case setMotorStatus:
- RESPOND_MOTOR();
- break;
- default:
- break;
- }
- }
- }
- /******************************************************************************/
- void COMMUNICATION_Control(void)
- {
- uint8_t byte;
- if(!UartBuffer_IsEmpty(&uartBuffer))
- {
- while(!UartBuffer_GetChar(&uartBuffer, (char *)&byte))
- {
- UI_FrameError_t status = COMMUNICATION_ProcessByte(byte);
- if(FrameReady == status)
- {
- PRINTF_COM("Frame Ready\n");
- COMMUNICATION_ProcessMessage();
- }
- else if(FrameError == status)
- {
- PRINTF_COM("Frame error\n");
- }
- }
- }
- COMM_Message.data[0] = 0;
- COMM_Message.data[1] = 0;
- COMM_Message.data[2] = 0;
- COMM_Message.data[3] = 0;
- COMM_Message.data[4] = 0;
- COMM_Message.data[5] = 0;
- COMM_Message.data[6] = 0;
- COMM_Message.data[7] = 0;
- UartBuffer_Clear(&uartBuffer);
- checkDone = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement