can_driver.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. ******************************************************************************
  3. * @file eeprom.c
  4. * @author Vincent
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief AT24C02 eeprom read and write function.
  8. ******************************************************************************
  9. *
  10. * COPYRIGHT(c) 2015 STMicroelectronics
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "can_driver.h"
  38. /* Private variables ---------------接收缓存区------------------------------------------*/
  39. #if CAN_RxBufSize1 != 0
  40. CD_UINT8 CAN_RxBuf1[CAN_RxBufSize1];
  41. CAN_Buf_TypeDef CAN_RxBuf_Struct1={0x0715,CAN_RxBufSize1,0,0,0,0,CAN_RxBuf1};
  42. #endif
  43. /* Private variables ---------------------------------------------------------*/
  44. /* Private function prototypes -----------------------------------------------*/
  45. static void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,CD_UINT8 ucLength);
  46. /*******************************************************************************
  47. * Function:
  48. * Discrible:
  49. * Input:
  50. * Output:
  51. * Return:
  52. * Others:
  53. * date version author record
  54. * -----------------------------------------------
  55. * 20151107 V1.0 VINCENT Create
  56. *********************************************************************************/
  57. CD_UINT8 cd_GetChar(CAN_Buf_TypeDef * ptCANRx)
  58. {
  59. CD_UINT8 ucData;
  60. ucData = *((*ptCANRx).pcBufAddr + (*ptCANRx).ucBufRdInde);
  61. if ((*ptCANRx).ucBufCnt != 0)
  62. {
  63. if (++(*ptCANRx).ucBufRdInde == (*ptCANRx).ucBufSize)
  64. {
  65. (*ptCANRx).ucBufRdInde = 0;
  66. }
  67. __HAL_CAN_DISABLE_IT(&hcan, CAN_IT_FMP0);
  68. --(*ptCANRx).ucBufCnt;
  69. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
  70. }
  71. return ucData;
  72. }
  73. /*******************************************************************************
  74. * Function:
  75. * Discrible:
  76. * Input:
  77. * Output:
  78. * Return:
  79. * Others:
  80. * date version author record
  81. * -----------------------------------------------
  82. * 20151107 V1.0 VINCENT Create
  83. *********************************************************************************/
  84. CD_UINT8 cd_ReadChar(CAN_Buf_TypeDef * ptCANRx, CD_UINT16 uwNum)
  85. {
  86. CD_UINT8 ucData;
  87. CD_UINT16 i;
  88. i = uwNum;
  89. if ((*ptCANRx).ucBufCnt >= uwNum)
  90. {
  91. i += (*ptCANRx).ucBufRdInde;
  92. if (i >= (*ptCANRx).ucBufSize)
  93. {
  94. i -=((*ptCANRx).ucBufSize);
  95. }
  96. }
  97. else
  98. {
  99. i=0;
  100. }
  101. ucData = *((*ptCANRx).pcBufAddr + i);
  102. return ucData;
  103. }
  104. /*******************************************************************************
  105. * Function:
  106. * Discrible:
  107. * Input:
  108. * Output:
  109. * Return:
  110. * Others:
  111. * date version author record
  112. * -----------------------------------------------
  113. * 20151107 V1.0 VINCENT Create
  114. *********************************************************************************/
  115. void cd_DelChar(CAN_Buf_TypeDef * ptCANRx, CD_UINT16 uwNum)
  116. {
  117. CD_UINT16 i;
  118. if ((*ptCANRx).ucBufCnt >= uwNum)
  119. {
  120. __HAL_CAN_DISABLE_IT(&hcan, CAN_IT_FMP0);
  121. (*ptCANRx).ucBufCnt -= uwNum;
  122. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
  123. i = uwNum;
  124. i += (*ptCANRx).ucBufRdInde;
  125. if (i >= (*ptCANRx).ucBufSize)
  126. {
  127. i -= (*ptCANRx).ucBufSize;
  128. }
  129. (*ptCANRx).ucBufRdInde = i;
  130. }
  131. else
  132. {
  133. __HAL_CAN_DISABLE_IT(&hcan, CAN_IT_FMP0);
  134. i = (*ptCANRx).ucBufCnt;
  135. (*ptCANRx).ucBufCnt = 0;
  136. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
  137. i += (*ptCANRx).ucBufRdInde;
  138. if (i >= (*ptCANRx).ucBufSize)
  139. {
  140. i -= (*ptCANRx).ucBufSize;
  141. }
  142. (*ptCANRx).ucBufRdInde = i;
  143. }
  144. }
  145. /*******************************************************************************
  146. * Function:
  147. * Discrible:
  148. * Input:
  149. * Output:
  150. * Return:
  151. * Others:
  152. * date version author record
  153. * -----------------------------------------------
  154. * 20151107 V1.0 VINCENT Create
  155. *********************************************************************************/
  156. void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,CD_UINT8 ucLength)
  157. {
  158. for(CD_UINT8 i=0;i<ucLength;i++)
  159. {
  160. *((* ptCANRx).pcBufAddr + (* ptCANRx).ucBufWrInde) = hcan.pRxMsg->Data[i];
  161. if(++(* ptCANRx).ucBufWrInde >= (* ptCANRx).ucBufSize)
  162. {
  163. (* ptCANRx).ucBufWrInde = 0;
  164. }
  165. if(++(* ptCANRx).ucBufCnt > (* ptCANRx).ucBufSize)
  166. {
  167. (* ptCANRx).ucBufCnt = (* ptCANRx).ucBufSize;
  168. (* ptCANRx).ucBufOvf = 1;
  169. }
  170. }
  171. }
  172. /*******************************************************************************
  173. * Function:void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
  174. * Discrible:接收总段回调函数,进入中断后,中断就被清零了,所以在执行完回调函数后需要开中断
  175. * Input:
  176. * Output:
  177. * Return:
  178. * Others:
  179. * date version author record
  180. * -----------------------------------------------
  181. * 20151107 V1.0 VINCENT Create
  182. *********************************************************************************/
  183. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
  184. {
  185. if((CanHandle->pRxMsg->IDE != CAN_ID_STD)||(CanHandle->pRxMsg->DLC == 0))
  186. {
  187. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  188. return;
  189. }
  190. if(CanHandle->pRxMsg->StdId == 0x0751)
  191. {
  192. CAN_Rx_ISR(&CAN_RxBuf_Struct1,CanHandle->pRxMsg->DLC);
  193. }
  194. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  195. }
  196. void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
  197. {
  198. __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FMP0);
  199. }
  200. /************************ (C) END OF FILE *********************************/