140 lines
3.9 KiB
C
140 lines
3.9 KiB
C
/*****************************************************************************/
|
|
/**
|
|
* Description: MPC uart driver implementation file.
|
|
*
|
|
* Date: 2023-05-06 10:59
|
|
* LastEditTime: 2023-05-06 21:56
|
|
* Revisions: Rev 0.0
|
|
* History:
|
|
* [2023-05-06]: Initial version.
|
|
*
|
|
* Copyright(c) 2022 Beijing Institute of Technology.
|
|
*
|
|
******************************************************************************/
|
|
|
|
/***************************** Include Files *******************************/
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include "mpc_hw.h"
|
|
#include "common.h"
|
|
|
|
// Control and status register
|
|
#define MODULE_VERSION 0
|
|
#define MPC_STATUS 4
|
|
#define MPC_SEND_CMD 8
|
|
#define MPC_RECV_CNT 12
|
|
|
|
// Status code
|
|
#define MPC_STATUS_TX_BUSY 0x00010000
|
|
#define MPC_STATUS_RX_BUSY 0x01000000
|
|
// Data buffer register
|
|
#define MPC_TXBUFF_MAX 512
|
|
#define MPC_RXBUFF_MAX 512
|
|
|
|
// Frame Data offsets
|
|
#define FRAME_HEAD_LOW 0x02
|
|
#define FRAME_HEAD_HIGH 0x53
|
|
#define FRAME_TAIL_LOW 0x32
|
|
#define FRAME_TAIL_HIGH 0x20
|
|
|
|
#define FRAME_HEAD_LEN 2
|
|
#define FRAME_TAIL_LEN 2
|
|
#define FRAME_DUMMY_LEN 1 // Dummy
|
|
#define FRAME_OVERHEAD_LEN 2 // CheckSum, Dummy and
|
|
|
|
#define MPC_STATUS_CH 1
|
|
#define MPC_CONFIG_CH 2
|
|
|
|
#define MPC_CONFIG_SEND_TRIG 0x00000002
|
|
/*************************** Function Definitions ****************************/
|
|
|
|
/*****************************************************************************/
|
|
/**
|
|
* @brief Instance initial
|
|
*
|
|
* @param instPtr A pointer to frame uart instance structure.
|
|
* @param config Base address of frame uart, refer to "xparameters.h"
|
|
*
|
|
* @return None.
|
|
*
|
|
******************************************************************************/
|
|
int MpcHw_Init(MPC_HW **instPtr, MPC_HW_CONFIG *config)
|
|
{
|
|
ASSERT(instPtr != NULL);
|
|
ASSERT(config != NULL);
|
|
uint32_t Status;
|
|
(*instPtr) = malloc(sizeof(MPC_HW));
|
|
if ((*instPtr) == NULL)
|
|
{
|
|
return FAILURE;
|
|
}
|
|
(*instPtr)->config.txBuffAddr = config->txBuffAddr;
|
|
(*instPtr)->config.rxBuffAddr = config->rxBuffAddr;
|
|
Status = XGpio_Initialize(&((*instPtr)->gpioDesc), config->gpioId);
|
|
if (Status != SUCCESS) {
|
|
DBG_ERROR("MPC gpio Initialization Failed.\n");
|
|
free(*instPtr);
|
|
return FAILURE;
|
|
}
|
|
XGpio_SetDataDirection(&((*instPtr)->gpioDesc), MPC_CONFIG_CH, 0x00000000);
|
|
XGpio_SetDataDirection(&((*instPtr)->gpioDesc), MPC_STATUS_CH, 0xFFFFFFFF);
|
|
return SUCCESS;
|
|
}
|
|
|
|
/*****************************************************************************/
|
|
/**
|
|
* @brief Send a MPC frame.
|
|
*
|
|
* @param instPtr A pointer to MPC_HW instance structure.
|
|
* @param buff A pointer to send data buffer
|
|
* @param dLen A integer indicate the send data buffer length in byte.
|
|
*
|
|
* @return
|
|
*
|
|
******************************************************************************/
|
|
int MpcHw_Send(MPC_HW *instPtr, MPC_HW_FRAME *frame)
|
|
{
|
|
ASSERT(instPtr != NULL);
|
|
ASSERT(frame != NULL);
|
|
uint8_t sendLen;
|
|
uint8_t *sendBuff = (uint8_t*) instPtr->config.txBuffAddr;
|
|
sendLen = frame->len + FRAME_OVERHEAD_LEN;
|
|
if (sendLen > MPC_TXBUFF_MAX || frame->len == 0)
|
|
{
|
|
return FAILURE;
|
|
}
|
|
frame->data[0] = FRAME_HEAD_LOW;
|
|
frame->data[1] = FRAME_HEAD_HIGH;
|
|
memcpy(sendBuff, frame->data, sendLen);
|
|
|
|
// Start Sending
|
|
uint32_t gpioVal = XGpio_DiscreteRead(&(instPtr->gpioDesc), MPC_CONFIG_CH);
|
|
gpioVal ^= MPC_CONFIG_SEND_TRIG;
|
|
XGpio_DiscreteWrite(&(instPtr->gpioDesc), MPC_CONFIG_CH, gpioVal);
|
|
return SUCCESS;
|
|
}
|
|
|
|
/*****************************************************************************/
|
|
/**
|
|
* @brief Get last MPC frame.
|
|
*
|
|
* @param instPtr A pointer to MPC_HW instance structure.
|
|
* @param buff A pointer to receive data buffer.
|
|
*
|
|
* @return
|
|
*
|
|
******************************************************************************/
|
|
int MpcHw_Recv(MPC_HW *instPtr, MPC_HW_FRAME *frame)
|
|
{
|
|
ASSERT(instPtr != NULL);
|
|
ASSERT(frame != NULL);
|
|
uint8_t *recvBuff = (uint8_t *)(instPtr->config.rxBuffAddr);
|
|
memcpy(frame->data, recvBuff, MPC_FRAME_MAX);
|
|
frame->len = MPC_FRAME_MAX;
|
|
frame->isValid = true;
|
|
return SUCCESS;
|
|
}
|
|
|