/** ****************************************************************************** * @file eeprom.c * @author Vincent * @version V1.0.0 * @date 23-12-2015 * @brief AT24C02 eeprom read and write function. ****************************************************************************** * * COPYRIGHT(c) 2015 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "can_driver.h" /* Private variables ---------------接收缓存区------------------------------------------*/ #if CAN_RxBufSize1 != 0 CD_UINT8 CAN_RxBuf1[CAN_RxBufSize1]; CAN_Buf_TypeDef CAN_RxBuf_Struct1={0x0715,CAN_RxBufSize1,0,0,0,0,CAN_RxBuf1}; #endif /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ static void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,CD_UINT8 ucLength); /******************************************************************************* * Function: * Discrible: * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20151107 V1.0 VINCENT Create *********************************************************************************/ CD_UINT8 cd_GetChar(CAN_Buf_TypeDef * ptCANRx) { CD_UINT8 ucData; ucData = *((*ptCANRx).pcBufAddr + (*ptCANRx).ucBufRdInde); if ((*ptCANRx).ucBufCnt != 0) { if (++(*ptCANRx).ucBufRdInde == (*ptCANRx).ucBufSize) { (*ptCANRx).ucBufRdInde = 0; } __HAL_CAN_DISABLE_IT(&hcan, CAN_IT_FMP0); --(*ptCANRx).ucBufCnt; __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0); } return ucData; } /******************************************************************************* * Function: * Discrible: * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20151107 V1.0 VINCENT Create *********************************************************************************/ CD_UINT8 cd_ReadChar(CAN_Buf_TypeDef * ptCANRx, CD_UINT16 uwNum) { CD_UINT8 ucData; CD_UINT16 i; i = uwNum; if ((*ptCANRx).ucBufCnt >= uwNum) { i += (*ptCANRx).ucBufRdInde; if (i >= (*ptCANRx).ucBufSize) { i -=((*ptCANRx).ucBufSize); } } else { i=0; } ucData = *((*ptCANRx).pcBufAddr + i); return ucData; } /******************************************************************************* * Function: * Discrible: * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20151107 V1.0 VINCENT Create *********************************************************************************/ void cd_DelChar(CAN_Buf_TypeDef * ptCANRx, CD_UINT16 uwNum) { CD_UINT16 i; if ((*ptCANRx).ucBufCnt >= uwNum) { __HAL_CAN_DISABLE_IT(&hcan, CAN_IT_FMP0); (*ptCANRx).ucBufCnt -= uwNum; __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0); i = uwNum; i += (*ptCANRx).ucBufRdInde; if (i >= (*ptCANRx).ucBufSize) { i -= (*ptCANRx).ucBufSize; } (*ptCANRx).ucBufRdInde = i; } else { __HAL_CAN_DISABLE_IT(&hcan, CAN_IT_FMP0); i = (*ptCANRx).ucBufCnt; (*ptCANRx).ucBufCnt = 0; __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0); i += (*ptCANRx).ucBufRdInde; if (i >= (*ptCANRx).ucBufSize) { i -= (*ptCANRx).ucBufSize; } (*ptCANRx).ucBufRdInde = i; } } /******************************************************************************* * Function: * Discrible: * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20151107 V1.0 VINCENT Create *********************************************************************************/ void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,CD_UINT8 ucLength) { for(CD_UINT8 i=0;iData[i]; if(++(* ptCANRx).ucBufWrInde >= (* ptCANRx).ucBufSize) { (* ptCANRx).ucBufWrInde = 0; } if(++(* ptCANRx).ucBufCnt > (* ptCANRx).ucBufSize) { (* ptCANRx).ucBufCnt = (* ptCANRx).ucBufSize; (* ptCANRx).ucBufOvf = 1; } } } /******************************************************************************* * Function:void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle) * Discrible:接收总段回调函数,进入中断后,中断就被清零了,所以在执行完回调函数后需要开中断 * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20151107 V1.0 VINCENT Create *********************************************************************************/ void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle) { if((CanHandle->pRxMsg->IDE != CAN_ID_STD)||(CanHandle->pRxMsg->DLC == 0)) { __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断 return; } if(CanHandle->pRxMsg->StdId == 0x0751) { CAN_Rx_ISR(&CAN_RxBuf_Struct1,CanHandle->pRxMsg->DLC); } __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断 } /************************ (C) END OF FILE *********************************/