Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.23 KB | None | 0 0
  1. #define FRAME_START           0xAA
  2. #define PREAMBLE_OFFSET       0
  3. #define ADDRESS_OFFSET        1
  4. #define SENDER_OFFSET         2
  5. #define MESSAGETYPE_OFFSET    3
  6. #define COMMAND_OFFSET        4
  7. #define LENGHTDATA_OFFSET     5
  8. #define DATA_OFFSET           7
  9.  
  10. #define RX_SIZE               32
  11. #define TX_SIZE               40
  12. #define UART_UI_TIMEOUT_MS    10
  13. #define UI_RX_SIZE            32
  14.  
  15. enum messageType
  16. {
  17.     messageType_Requst   = 0x00,
  18.     messageType_ACK      = 0x06,
  19.     messageType_NACK     = 0x15,
  20. };
  21.  
  22. typedef enum
  23. {
  24.    FrameError,
  25.    FrameNotReady,
  26.    FrameReady,
  27.  
  28. }UI_FrameError_t;
  29.  
  30. typedef enum
  31. {
  32.    ping              = 0x01,
  33.    setParameters     = 0x02,
  34.    getMeasurement    = 0x03,
  35.    getMeasurementRMS = 0x04,
  36.    debugOn           = 0x05,
  37.    setPWM            = 0x06,
  38.    getStat           = 0x07,
  39.    clearFlags        = 0x08,
  40.    resetProcessor    = 0x09,
  41.    setMotorStatus    = 0x10,
  42.    debugTest         = 0x20,
  43. }UI_Command_t;
  44.  
  45. typedef struct {
  46.     char *dataBuffer;
  47.     int head;
  48.     int tail;
  49.     int count;
  50.     int size;
  51. } UartBuffer;
  52.  
  53. typedef struct
  54. {
  55.    uint8_t praamble;
  56.    uint8_t address;
  57.    uint8_t sender;
  58.    uint8_t messageType;
  59.    UI_Command_t command;
  60.    uint16_t lenghtData;
  61.    uint8_t *data;
  62.    uint16_t crc;
  63. }COMM_Frame_t;
  64.  
  65. static uint8_t uartData;
  66.  
  67. static uint8_t rxBuffer[RX_SIZE];
  68. static uint8_t ui_frame[UI_RX_SIZE];
  69. static uint8_t txBuffer[TX_SIZE];
  70.  
  71. void COMMUNICATION_Init(void)
  72. {
  73.     USART1_UART_Init();
  74.     HAL_UART_Receive_IT(&huart1,&uartData,1);
  75.     __HAL_TIM_CLEAR_IT(&htim17,TIM_IT_UPDATE);
  76.     UartBuffer_Init(&uartBuffer, (char *)rxBuffer, RX_SIZE);
  77.     COMM_Message.data = ui_frame;
  78. }
  79.  
  80. void USART1_IRQHandler(void)
  81. {
  82.   HAL_UART_IRQHandler(&huart1);
  83. }
  84.  
  85. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  86. {
  87.     if(huart->Instance == USART1)
  88.     {
  89.         UartBuffer_PutChar(&uartBuffer, uartData);
  90.         HAL_TIM_Base_Start_IT(&htim17);
  91.         HAL_UART_Receive_IT(&huart1, &uartData, 1);
  92.     }
  93. }
  94.  
  95. bool UartBuffer_Init(UartBuffer *uartBuffer, char *dataBuffer, size_t dataBufferSize)
  96. {
  97.    assert(uartBuffer);
  98.    assert(dataBuffer);
  99.    assert(dataBufferSize > 0);
  100.  
  101.    if ((uartBuffer) && (dataBuffer) && (dataBufferSize > 0)) {
  102.        uartBuffer->dataBuffer = dataBuffer;
  103.        uartBuffer->head = 0;
  104.        uartBuffer->tail = 0;
  105.        uartBuffer->count = 0;
  106.        uartBuffer->size = dataBufferSize;
  107.       return true;
  108.    }
  109.  
  110.    return false;
  111. }
  112.  
  113. bool UartBuffer_IsEmpty(const UartBuffer *uartBuffer)
  114. {
  115.   assert(uartBuffer);
  116.  
  117.    if (uartBuffer->head == uartBuffer->tail && uartBuffer->count == 0)
  118.       return true;
  119.    else
  120.       return false;
  121.  
  122. }
  123.  
  124. bool UartBuffer_Clear(UartBuffer *uartBuffer)
  125. {
  126.    assert(uartBuffer);
  127.    if (uartBuffer) {
  128.        memset(uartBuffer->dataBuffer, 0, RX_SIZE);
  129.        uartBuffer->head = 0;
  130.        uartBuffer->tail = 0;
  131.        uartBuffer->count = 0;
  132.  
  133.       return true;
  134.    }
  135.    return false;
  136. }
  137.  
  138. bool UartBuffer_PutChar(UartBuffer *uartBuffer, char c)
  139. {
  140.     assert(uartBuffer);
  141.        if (uartBuffer) {
  142.           if (uartBuffer->count < uartBuffer->size)
  143.           {
  144.               uartBuffer->dataBuffer[uartBuffer->head++] = c;
  145.               uartBuffer->count++;
  146.              if (uartBuffer->head >= uartBuffer->size) uartBuffer->head = 0;
  147.              return true;
  148.           }
  149.        }
  150.        return false;
  151. }
  152.  
  153. bool UartBuffer_GetChar(UartBuffer *uartBuffer, char *c)
  154. {
  155.     assert(uartBuffer);
  156.        assert(c);
  157.        if ((uartBuffer) && (c)) {
  158.           if (uartBuffer->count > 0)
  159.           {
  160.  
  161.              *c = uartBuffer->dataBuffer[uartBuffer->tail++];
  162.              uartBuffer->count--;
  163.              if (uartBuffer->tail >= uartBuffer->size) uartBuffer->tail = 0;
  164.              return 0;
  165.  
  166.           }
  167.        }
  168.        return 1;
  169. }
  170.  
  171. UI_FrameError_t COMMUNICATION_ProcessByte(uint8_t byte)
  172. {
  173.    static uint8_t idx;
  174.  
  175.    if((idx > DATA_OFFSET) && (idx-DATA_OFFSET >= UI_RX_SIZE))
  176.    {
  177.       idx = 0;
  178.       return FrameError;
  179.    }
  180.  
  181.    if(idx == PREAMBLE_OFFSET)
  182.    {
  183.       if(byte == FRAME_START)
  184.          COMM_Message.praamble = byte;
  185.       else
  186.       {
  187.          idx = 0;
  188.          return FrameError;
  189.       }
  190.    }
  191.    else if(idx == ADDRESS_OFFSET)
  192.       COMM_Message.address = byte;
  193.    else if(idx == SENDER_OFFSET)
  194.       COMM_Message.sender = byte;
  195.    else if(idx == MESSAGETYPE_OFFSET)
  196.       COMM_Message.messageType = byte;
  197.    else if(idx == COMMAND_OFFSET)
  198.       COMM_Message.command = byte;
  199.    else if(idx == LENGHTDATA_OFFSET)
  200.       COMM_Message.lenghtData = byte;
  201.    else if(idx == LENGHTDATA_OFFSET + 1)
  202.       COMM_Message.lenghtData += byte << 8;
  203.    else
  204.    {
  205.       if(idx < DATA_OFFSET)
  206.       {
  207.          idx = 0;
  208.          return FrameError;
  209.       }
  210.  
  211.       if(idx < COMM_Message.lenghtData + DATA_OFFSET)
  212.       {
  213.          COMM_Message.data[idx - DATA_OFFSET] = byte;
  214.       }
  215.       else
  216.       {
  217.          if(idx == COMM_Message.lenghtData + DATA_OFFSET)
  218.             COMM_Message.crc = byte;
  219.          else if(idx == COMM_Message.lenghtData + DATA_OFFSET + 1)
  220.          {
  221.             COMM_Message.crc += byte << 8;
  222.             idx = 0;
  223.             return FrameReady;
  224.          }
  225.       }
  226.    }
  227.  
  228.    idx++;
  229.  
  230.    return FrameNotReady;
  231. }
  232.  
  233.  
  234. /******************************************************************************/
  235. void COMMUNICATION_ProcessMessage(void)
  236. {
  237.    if(COMMUNICATION_CalcCRC() != COMM_Message.crc)
  238.    {
  239.        PRINTF_HW("CRC BAD\n");
  240.    }
  241.    else if( COMM_Message.address == ADDRESS_UI &&
  242.             (COMM_Message.sender == ADDRESS_MOBO) &&
  243.             COMM_Message.messageType == messageType_ACK
  244.            )
  245.    {
  246.       switch (COMM_Message.command) {
  247.       case ping:
  248.          break;
  249.       case setParameters:
  250.           RESPOND_STER();
  251.          break;
  252.       case getMeasurement:
  253.           PARAM_PROCESS(COMM_Message.data,COMM_Message.lenghtData);
  254.          break;
  255.       case getMeasurementRMS:
  256.          break;
  257.       case debugOn:
  258.          break;
  259.       case setPWM:
  260.          RESPOND_PWM();
  261.          break;
  262.       case getStat:
  263.           RESPOND_STATUS();
  264.           PROCESS_STATUS(COMM_Message.data,COMM_Message.lenghtData);
  265.          break;
  266.       case clearFlags:
  267.           RESPOND_CLEAN();
  268.          break;
  269.       case resetProcessor:
  270.           RESPOND_RESET();
  271.          break;
  272.       case debugTest:
  273.          break;
  274.       case setMotorStatus:
  275.           RESPOND_MOTOR();
  276.          break;
  277.       default:
  278.          break;
  279.       }
  280.    }
  281. }
  282.  
  283. /******************************************************************************/
  284. void COMMUNICATION_Control(void)
  285. {
  286.       uint8_t byte;
  287.       if(!UartBuffer_IsEmpty(&uartBuffer))
  288.       {
  289.          while(!UartBuffer_GetChar(&uartBuffer, (char *)&byte))
  290.          {
  291.             UI_FrameError_t status = COMMUNICATION_ProcessByte(byte);
  292.             if(FrameReady == status)
  293.             {
  294.                PRINTF_COM("Frame Ready\n");
  295.                COMMUNICATION_ProcessMessage();
  296.             }
  297.             else if(FrameError == status)
  298.             {
  299.                 PRINTF_COM("Frame error\n");
  300.             }
  301.          }
  302.       }
  303.       COMM_Message.data[0] = 0;
  304.       COMM_Message.data[1] = 0;
  305.       COMM_Message.data[2] = 0;
  306.       COMM_Message.data[3] = 0;
  307.       COMM_Message.data[4] = 0;
  308.       COMM_Message.data[5] = 0;
  309.       COMM_Message.data[6] = 0;
  310.       COMM_Message.data[7] = 0;
  311.       UartBuffer_Clear(&uartBuffer);
  312.  
  313.       checkDone = true;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement