usart.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. ******************************************************************************
  3. * @file usart.h
  4. * @author
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief usart function.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __USART_H
  39. #define __USART_H
  40. /* Includes ------------------------------------------------------------------*/
  41. //#include "at32f421.h"
  42. #include <stdbool.h>
  43. #include "typedefine.h"
  44. #include "bikethrottle.h"
  45. #include "typedefine.h"
  46. /* Public Function declare---------------------------------------------------*/
  47. typedef _Bool BOOL;
  48. // 天腾
  49. #define ID_MC_BC 0x710
  50. #define ID_MC_TO_BMS 0x712
  51. #define ID_MC_TO_PBU 0x713
  52. #define ID_MC_TO_HMI 0x714
  53. #define ID_MC_TO_CDL 0x715
  54. #define ID_BMS_BC 0x720
  55. #define ID_BMS_TO_MC 0x721
  56. #define ID_BMS_TO_PBU 0x723
  57. #define ID_BMS_TO_HMI 0x724
  58. #define ID_BMS_TO_CDL 0x725
  59. #define ID_PBU_BC 0x730
  60. #define ID_PBU_TO_MC 0x731
  61. #define ID_PBU_TO_BMS 0x732
  62. #define ID_PBU_TO_CDL 0x735
  63. #define ID_HMI_BC 0x740
  64. #define ID_HMI_TO_MC 0x741
  65. #define ID_HMI_TO_BMS 0x742
  66. #define ID_HMI_TO_PBU 0x743
  67. #define ID_HMI_TO_CDL 0x745
  68. #define ID_CDL_BC 0x750
  69. #define ID_CDL_TO_MC 0x751
  70. #define ID_CDL_TO_BMS 0x752
  71. #define ID_CDL_TO_PBU 0x753
  72. #define ID_CDL_TO_HMI 0x754
  73. #define MODE_READ 0x11
  74. #define MODE_WRITE 0x16
  75. #define MODE_REPORT 0x0C
  76. #define UART_BUFF_SIZE 512//256
  77. #define FRAME_SIZE 8
  78. #define FRAMETIMEOUT_Count 10//20 //ms
  79. #define UARTFRAME_BEGIN (UBYTE)0x3A
  80. #define UARTFRAME_END1 (UBYTE)0x0D
  81. #define UARTFRAME_END2 (UBYTE)0x0A
  82. #define UARTADDR_CDL (UBYTE)0xFA
  83. #define UARTADDR_BMS (UBYTE)0x16
  84. #define UARTADDR_OBC (UBYTE)0x1A
  85. #define UARTMODE_READ (UBYTE)0x11
  86. #define UARTMODE_WRITE (UBYTE)0x16
  87. #define UARTMODE_REPORT (UBYTE)0x0C
  88. /* Define to variables declare---------------------------------------------------*/
  89. //低字节在前, 高字节在后
  90. typedef struct
  91. {
  92. UWORD ucBufSize;
  93. UWORD ucBufWrInde;
  94. UWORD ucBufRdInde;
  95. UWORD ucBufCnt;
  96. UBYTE ucBufOvf;
  97. UBYTE * pcBufAddr;
  98. // usart_type* usart_periph;
  99. ULONG DelayTimeCnt; //超时计时
  100. BOOL IsWaitRX_Flag; //超时等待数据标志
  101. ULONG ul_UartComTimeOutCount;
  102. } USART_Buf_TypeDef;
  103. typedef struct
  104. {
  105. UWORD uwFramecount;
  106. UBYTE ucBufWrIndex;
  107. UBYTE ucBufRdIndex;
  108. UWORD uwDataCount[FRAME_SIZE];
  109. } USART_FrameBuf_TypeDef;
  110. typedef enum
  111. {
  112. Assist_Forward = 0x00,
  113. Assist_Backward = 0x01,
  114. } AssistDir_Struct_t;
  115. typedef struct
  116. {
  117. UBYTE ucPWM;
  118. UBYTE ucLightSwitch;
  119. UBYTE ucLowPower;
  120. UBYTE ucWalk;
  121. UBYTE ucCruise;
  122. UBYTE ucOverSpeed;
  123. } OBC_RunInfo_Struct_t;
  124. typedef struct
  125. {
  126. AssistDir_Struct_t AssistDir;
  127. UBYTE ucAssistedMagnetSteel;
  128. UBYTE ucAssistRatio;
  129. UBYTE ucThrottleGt;
  130. UBYTE ucThrottleLimited;
  131. UBYTE ucSlowStartMode;
  132. UBYTE ucSpeedSensorPulses;
  133. SWORD swUnderVoltageBias;//0.1V
  134. UWORD uwUnderVoltageBase;
  135. UBYTE ucCurrentLimiation;//0.5A
  136. UBYTE ucShakingCode;
  137. UBYTE ucSpeedLimiation;
  138. UBYTE ucWheelDiameterCode;
  139. } OBC_SetInfo_Struct_t;
  140. typedef struct
  141. {
  142. UBYTE Stage;
  143. UBYTE Current;
  144. UWORD Speed;
  145. UBYTE ErrorCode;
  146. } MC2OBC_RunInfo_Struct_t;
  147. /* Public variables declare---------------------------------------------------*/
  148. extern UBYTE UART_RxBuff_OBC[UART_BUFF_SIZE];
  149. extern USART_Buf_TypeDef UART_RxBuff_Struct_OBC;
  150. extern UBYTE UART_TxBuff_OBC[UART_BUFF_SIZE];
  151. extern USART_Buf_TypeDef UART_TxBuff_Struct_OBC;
  152. extern UBYTE UART_RxBuff_BMS[UART_BUFF_SIZE];
  153. extern USART_Buf_TypeDef UART_RxBuff_Struct_BMS ;
  154. extern UBYTE UART_TxBuff_BMS[UART_BUFF_SIZE];
  155. extern USART_Buf_TypeDef UART_TxBuff_Struct_BMS;
  156. extern USART_FrameBuf_TypeDef stUSART_FrameBuf_OBC;
  157. extern USART_FrameBuf_TypeDef stUSART_FrameBuf_BMS;
  158. extern OBC_RunInfo_Struct_t stOBC_RunInfo;
  159. //extern OBC_SetInfo_Struct_t stOBC_SetInfo;
  160. //extern MC2OBC_RunInfo_Struct_t stMC2OBC_RRunInfo;
  161. extern UBYTE ucUartAnalyseID;
  162. extern UBYTE ucCommType;
  163. extern BIKECRUISE_COF bikeCruise;
  164. /*Private function declare---------------------------------------------------*/
  165. UBYTE UART_ReadChar(USART_Buf_TypeDef * ptRx, UBYTE ucNum);
  166. void UART_DelChar(USART_Buf_TypeDef * ptRx, UBYTE ucNum);
  167. void UART_PutChar(USART_Buf_TypeDef * ptTx, UBYTE ucData);
  168. void UsartRx_Process(USART_Buf_TypeDef *USARTx_RxBuf_Struct, USART_Buf_TypeDef *USARTx_TxBuf_Struct, USART_FrameBuf_TypeDef *USART_RxFrameBuf_Struct);
  169. void SendData(USART_Buf_TypeDef* ptUartTx, UWORD ID, UBYTE Mode, UWORD Command, UBYTE *Data);
  170. void KM5S_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount);
  171. void KM5S_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UWORD Addr, UBYTE Mode, UBYTE Length, UBYTE* Data);
  172. void DataProcess(USART_Buf_TypeDef* ptUartTx, UWORD ID, UBYTE Mode, UWORD Cmd, UBYTE *Data);
  173. void KM5S_SendData_OBC(USART_Buf_TypeDef* ptUartTx, UBYTE Mode, UBYTE Length, UBYTE checknum);
  174. void USARTx_Rx_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct, USART_FrameBuf_TypeDef *USART_RxFrameBuf_Struct);
  175. void USARTx_Tx_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct);
  176. void KM5S_OBCSetPara(UBYTE* buf);
  177. void USARTx_RxTX_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct,USART_Buf_TypeDef *USARTx_TxBuf_Struct,USART_FrameBuf_TypeDef *USART_RxFrameBuf_Struct);
  178. #endif /*__USART_H */
  179. /*---------------------------- file end -------------------------------------*/