Skip to content

Commit

Permalink
Add serialintrf read transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoan Hoang committed Jan 19, 2017
1 parent 7032bc4 commit ccd9f11
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
45 changes: 18 additions & 27 deletions ARM/Nordic/nRF52/EHAL/src/i2c_nrf52.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ bool nRF52I2CWaitRxComplete(NRF52_I2CDEV *pDev, int Timeout)
pDev->pReg->EVENTS_ERROR = 0;
pDev->pReg->TASKS_RESUME = 1;
pDev->pReg->TASKS_STOP;

return false;
}
if (pDev->pReg->EVENTS_LASTRX)
{
Expand All @@ -73,13 +75,6 @@ bool nRF52I2CWaitRxComplete(NRF52_I2CDEV *pDev, int Timeout)
//pDev->pReg->TASKS_STOP = 1;
return true;
}
/* if (pDev->pReg->EVENTS_STOPPED)
{
// Must wait for stop, other wise DMA count would
// not be updated with correct value
pDev->pReg->EVENTS_STOPPED = 0;
return true;
}*/
} while (Timeout-- > 0);

return false;
Expand All @@ -96,21 +91,15 @@ bool nRF52I2CWaitTxComplete(NRF52_I2CDEV *pDev, int Timeout)
pDev->pReg->EVENTS_ERROR = 0;
pDev->pReg->TASKS_RESUME = 1;
pDev->pReg->TASKS_STOP = 1;

return false;
}
if (pDev->pReg->EVENTS_LASTTX)
{
// Must wait for last DMA then issue a stop
pDev->pReg->EVENTS_LASTTX = 0;
// pDev->pReg->TASKS_STOP = 1;
return true;
}
/* if (pDev->pReg->EVENTS_STOPPED)
{
// Must wait for stop, other wise DMA count would
// not be updated with correct value
pDev->pReg->EVENTS_STOPPED = 0;
return true;
}*/
} while (Timeout-- > 0);

return false;
Expand All @@ -127,6 +116,8 @@ bool nRF52I2CWaitStop(NRF52_I2CDEV *pDev, int Timeout)
pDev->pReg->EVENTS_ERROR = 0;
pDev->pReg->TASKS_RESUME = 1;
pDev->pReg->TASKS_STOP = 1;

return false;
}
if (pDev->pReg->EVENTS_STOPPED)
{
Expand Down Expand Up @@ -212,10 +203,6 @@ int nRF52I2CRxData(SERINTRFDEV *pDev, uint8_t *pBuff, int BuffLen)
if (nRF52I2CWaitRxComplete(dev, 100000) == false)
break;

/* l = dev->pReg->RXD.AMOUNT;
if (l <= 0)
break;*/

BuffLen -= l;
pBuff += l;
cnt += l;
Expand Down Expand Up @@ -261,12 +248,6 @@ int nRF52I2CTxData(SERINTRFDEV *pDev, uint8_t *pData, int DataLen)
if (nRF52I2CWaitTxComplete(dev, 100000) == false)
break;

int rtry = 1000;

do {
l = dev->pReg->TXD.AMOUNT;
} while (l <= 0 && rtry-- > 0);

DataLen -= l;
pData += l;
cnt += l;
Expand All @@ -278,6 +259,10 @@ void nRF52I2CStopTx(SERINTRFDEV *pDev)
{
NRF52_I2CDEV *dev = (NRF52_I2CDEV*)pDev->pDevData;

if (dev->pReg->EVENTS_LASTTX == 1)
{
dev->pReg->EVENTS_LASTTX = 0;
}
dev->pReg->TASKS_STOP = 1;
nRF52I2CWaitStop(dev, 1000);
}
Expand Down Expand Up @@ -320,8 +305,14 @@ bool I2CInit(I2CDEV *pDev, const I2CCFG *pCfgData)
pDev->SerIntrf.Busy = false;
pDev->SerIntrf.MaxRetry = pCfgData->MaxRetry;

reg->EVENTS_ERROR = 0;
reg->ERRORSRC = reg->ERRORSRC;
// Clear all errors
if (reg->EVENTS_ERROR)
{
reg->ERRORSRC = reg->ERRORSRC;
reg->EVENTS_ERROR = 0;
reg->TASKS_RESUME = 1;
reg->TASKS_STOP = 1;
}
reg->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);

return true;
Expand Down
7 changes: 7 additions & 0 deletions include/serialintrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ static inline int SerialIntrfSetRate(SERINTRFDEV *pDev, int Rate) {

int SerialIntrfRx(SERINTRFDEV *pDev, int DevAddr, uint8_t *pBuff, int BuffLen);
int SerialIntrfTx(SERINTRFDEV *pDev, int DevAddr, uint8_t *pBuff, int BuffLen);
// Read transfer. Send setup data then read return data.
int SerialIntrfRead(SERINTRFDEV *pDev, int DevAddr, uint8_t *pTxData, int TxLen,
uint8_t *pRxBuff, int RxLen);

static inline bool SerialIntrfStartRx(SERINTRFDEV *pDev, int DevAddr) {
if (pDev->Busy)
Expand Down Expand Up @@ -327,6 +330,10 @@ class SerialIntrf {
virtual int Tx(int DevAddr, uint8_t *pData, int DataLen) {
return SerialIntrfTx(*this, DevAddr, pData, DataLen);
}
// Read transfer. Send setup data then read return data.
virtual int Read(int DevAddr, uint8_t *pTxData, int TxLen, uint8_t *pRxBuff, int RxLen) {
return SerialIntrfRead(*this, DevAddr, pTxData, TxLen, pRxBuff, RxLen);
}
// Initiate receive
virtual bool StartRx(int DevAddr) = 0;
// Receive Data only, no Start/Stop condition
Expand Down
24 changes: 24 additions & 0 deletions src/serialintrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ int SerialIntrfTx(SERINTRFDEV *pDev, int DevAddr, uint8_t *pBuff, int BuffLen)
return count;
}

int SerialIntrfRead(SERINTRFDEV *pDev, int DevAddr, uint8_t *pTxData, int TxLen,
uint8_t *pRxBuff, int RxLen)
{
int count = 0;
int nrtry = pDev->MaxRetry;

if (pRxBuff == NULL)
return 0;

do {
if (pDev->StartRx(pDev, DevAddr))
{
if (pTxData)
{
count = pDev->TxData(pDev, pTxData, TxLen);
}
count = pDev->RxData(pDev, pRxBuff, RxLen);
pDev->StopRx(pDev);
}
} while (count <= 0 && nrtry-- > 0);

return count;
}

/*
// Receive full frame
int SerialIntrf::Rx(int DevAddr, uint8_t *pBuff, int BuffLen)
Expand Down

0 comments on commit ccd9f11

Please sign in to comment.