Advertisement
Guest User

Untitled

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