Skip to content

Commit

Permalink
Merge pull request #4 from I-SYST/Test_Nordic_i2c
Browse files Browse the repository at this point in the history
nordic i2c revisited
  • Loading branch information
Hoan Hoang authored Jan 19, 2017
2 parents 742050e + ccd9f11 commit 6d4c473
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 60 deletions.
5 changes: 0 additions & 5 deletions ARM/NXP/LPC11xx/EHAL/.project
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,6 @@
<type>1</type>
<locationURI>PARENT-4-PROJECT_LOC/src/sdcard_impl.cpp</locationURI>
</link>
<link>
<name>src/seep.c</name>
<type>1</type>
<locationURI>PARENT-4-PROJECT_LOC/src/seep.c</locationURI>
</link>
<link>
<name>src/seep_impl.cpp</name>
<type>1</type>
Expand Down
2 changes: 2 additions & 0 deletions ARM/NXP/LPC11xx/EHAL/src/spi_lpc11uxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ bool SPIInit(SPIDEV *pDev, const SPICFG *pCfgData)
pDev->SerIntrf.StartTx = LpcSSPStartTx;
pDev->SerIntrf.TxData = LpcSSPTxData;
pDev->SerIntrf.StopTx = LpcSSPStopTx;
pDev->SerIntrf.Busy = false;
pDev->SerIntrf.MaxRetry = 0;

LpcSSPSetRate(&pDev->SerIntrf, pCfgData->Rate);

Expand Down
3 changes: 2 additions & 1 deletion ARM/NXP/LPC11xx/EHAL/src/uart_lpc11uxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,9 @@ bool UARTInit(UARTDEV *pDev, const UARTCFG *pCfg)
pDev->SerIntrf.StartTx = LpcUARTStartTx;
pDev->SerIntrf.TxData = LpcUARTTxData;
pDev->SerIntrf.StopTx = LpcUARTStopTx;
pDev->EvtCallback = pCfg->EvtCallback;
pDev->SerIntrf.Busy = false;
pDev->SerIntrf.MaxRetry = 0;
pDev->EvtCallback = pCfg->EvtCallback;

g_LpcUartDev[pCfg->DevNo].bTxReady = true;

Expand Down
84 changes: 56 additions & 28 deletions ARM/Nordic/nRF52/EHAL/src/i2c_nrf52.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,18 @@ bool nRF52I2CWaitRxComplete(NRF52_I2CDEV *pDev, int Timeout)
if (pDev->pReg->EVENTS_ERROR)
{
// Abort in case error
pDev->pReg->ERRORSRC = pDev->pReg->ERRORSRC;
pDev->pReg->EVENTS_ERROR = 0;
pDev->pReg->TASKS_RESUME = 1;
pDev->pReg->TASKS_STOP;

return false;
}
if (pDev->pReg->EVENTS_LASTRX)
{
// Must wait for last DMA then issue a stop
pDev->pReg->EVENTS_LASTRX = 0;
pDev->pReg->TASKS_STOP = 1;
}
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;
//pDev->pReg->TASKS_STOP = 1;
return true;
}
} while (Timeout-- > 0);
Expand All @@ -94,25 +91,46 @@ 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;
}
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;
}

bool nRF52I2CWaitStop(NRF52_I2CDEV *pDev, int Timeout)
{
uint32_t d;
do {
if (pDev->pReg->EVENTS_ERROR)
{
// Abort in case error
pDev->pReg->ERRORSRC = pDev->pReg->ERRORSRC;
pDev->pReg->EVENTS_ERROR = 0;
pDev->pReg->TASKS_RESUME = 1;
pDev->pReg->TASKS_STOP = 1;

return false;
}
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;
}

void nRF52I2CDisable(SERINTRFDEV *pDev)
{
NRF52_I2CDEV *dev = (NRF52_I2CDEV*)pDev->pDevData;
Expand Down Expand Up @@ -182,10 +200,7 @@ int nRF52I2CRxData(SERINTRFDEV *pDev, uint8_t *pBuff, int BuffLen)
dev->pReg->RXD.LIST = 0;
dev->pReg->TASKS_STARTRX = 1;

nRF52I2CWaitRxComplete(dev, 100000);

l = dev->pReg->RXD.AMOUNT;
if (l <= 0)
if (nRF52I2CWaitRxComplete(dev, 100000) == false)
break;

BuffLen -= l;
Expand All @@ -197,8 +212,9 @@ int nRF52I2CRxData(SERINTRFDEV *pDev, uint8_t *pBuff, int BuffLen)

void nRF52I2CStopRx(SERINTRFDEV *pDev)
{
// Nothing to do here since DMA has to be completed during
// RxData phase
NRF52_I2CDEV *dev = (NRF52_I2CDEV*)pDev->pDevData;
dev->pReg->TASKS_STOP = 1;
nRF52I2CWaitStop(dev, 1000);
}

bool nRF52I2CStartTx(SERINTRFDEV *pDev, int DevAddr)
Expand Down Expand Up @@ -229,11 +245,8 @@ int nRF52I2CTxData(SERINTRFDEV *pDev, uint8_t *pData, int DataLen)
dev->pReg->TXD.LIST = 0;
dev->pReg->TASKS_STARTTX = 1;

nRF52I2CWaitTxComplete(dev, 100000);

l = dev->pReg->TXD.AMOUNT;
if (l <= 0)
break;
if (nRF52I2CWaitTxComplete(dev, 100000) == false)
break;

DataLen -= l;
pData += l;
Expand All @@ -244,8 +257,14 @@ int nRF52I2CTxData(SERINTRFDEV *pDev, uint8_t *pData, int DataLen)

void nRF52I2CStopTx(SERINTRFDEV *pDev)
{
// Nothing to do here since DMA has to be completed during
// TxData phase
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);
}

bool I2CInit(I2CDEV *pDev, const I2CCFG *pCfgData)
Expand Down Expand Up @@ -284,7 +303,16 @@ bool I2CInit(I2CDEV *pDev, const I2CCFG *pCfgData)
pDev->SerIntrf.IntPrio = pCfgData->IntPrio;
pDev->SerIntrf.EvtCB = pCfgData->EvtCB;
pDev->SerIntrf.Busy = false;

pDev->SerIntrf.MaxRetry = pCfgData->MaxRetry;

// 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
1 change: 1 addition & 0 deletions ARM/Nordic/nRF52/EHAL/src/spi_nrf52.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ bool SPIInit(SPIDEV *pDev, const SPICFG *pCfgData)
pDev->SerIntrf.IntPrio = pCfgData->IntPrio;
pDev->SerIntrf.EvtCB = pCfgData->EvtCB;
pDev->SerIntrf.Busy = false;
pDev->SerIntrf.MaxRetry = 0;

reg->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos);
}
Expand Down
6 changes: 3 additions & 3 deletions ARM/Nordic/src/ble_periph_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
break; // BLE_GATTS_EVT_SYS_ATTR_MISSING

case BLE_GAP_EVT_TIMEOUT:
if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
/* if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
{
ble_advertising_start(BLE_ADV_MODE_SLOW);
}
}*/
break;

case BLE_GATTC_EVT_TIMEOUT:
Expand Down Expand Up @@ -694,7 +694,7 @@ void BlePeriphAppAdvInit(const BLEAPP_CFG *pCfg)
memset(&advdata, 0, sizeof(advdata));
advdata.name_type = BLE_ADVDATA_FULL_NAME;
advdata.include_appearance = false;
advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
advdata.p_manuf_specific_data = &mdata;

memset(&scanrsp, 0, sizeof(scanrsp));
Expand Down
1 change: 1 addition & 0 deletions ARM/Nordic/src/uart_nrf5x.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ bool UARTInit(UARTDEV *pDev, const UARTCFG *pCfg)
pDev->SerIntrf.TxData = nRFUARTTxData;
pDev->SerIntrf.StopTx = nRFUARTStopTx;
pDev->SerIntrf.Busy = false;
pDev->SerIntrf.MaxRetry = 0;

NRF_UART0->TASKS_STARTTX = 1;
NRF_UART0->TASKS_STARTRX = 1;
Expand Down
40 changes: 17 additions & 23 deletions include/serialintrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ struct _serialintrf_dev {
int IntPrio; // Interrupt priority. Value is implementation specific
SERINTRFEVCB EvtCB; // Interrupt based event callback function pointer. Must be set to NULL if not used
volatile bool Busy; // Busy flag to be set check and set at start and reset at end of transmission
int MaxRetry; // Max retry when data could not be transfered (Rx/Tx returns zero count)

// Bellow are all mandatory functions to implement
// On init, all implementation must fill these function, no NULL allowed
Expand Down Expand Up @@ -265,26 +266,11 @@ static inline int SerialIntrfSetRate(SERINTRFDEV *pDev, int Rate) {
return pDev->SetRate(pDev, Rate);
}

static inline int SerialIntrfRx(SERINTRFDEV *pDev, int DevAddr, uint8_t *pBuff, int BuffLen) {
int retval = 0;

if (pBuff && pDev->StartRx(pDev, DevAddr)) {
retval = pDev->RxData(pDev, pBuff, BuffLen);
pDev->StopRx(pDev);
}

return retval;
}

static inline int SerialIntrfTx(SERINTRFDEV *pDev, int DevAddr, uint8_t *pBuff, int BuffLen) {
int retval = 0;

if (pBuff && pDev->StartTx(pDev, DevAddr)) {
retval = pDev->TxData(pDev, pBuff, BuffLen);
pDev->StopTx(pDev);
}
return retval;
}
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 @@ -336,10 +322,18 @@ class SerialIntrf {
virtual void Disable(void) = 0;
// Enable device
virtual void Enable(void) = 0;
// Transmit full frame
virtual int Tx(int DevAddr, uint8_t *pData, int DataLen);
// Receive full frame
virtual int Rx(int DevAddr, uint8_t *pBuff, int BuffLen);
virtual int Rx(int DevAddr, uint8_t *pBuff, int BuffLen) {
return SerialIntrfRx(*this,DevAddr, pBuff, BuffLen);
}
// Transmit full frame
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
56 changes: 56 additions & 0 deletions src/serialintrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,61 @@ Modified by Date Description
----------------------------------------------------------------------------*/
#include "serialintrf.h"

int SerialIntrfRx(SERINTRFDEV *pDev, int DevAddr, uint8_t *pBuff, int BuffLen)
{
int count = 0;
int nrtry = pDev->MaxRetry;

do {
if (pBuff && pDev->StartRx(pDev, DevAddr)) {
count = pDev->RxData(pDev, pBuff, BuffLen);
pDev->StopRx(pDev);
}
} while(count <= 0 && nrtry-- > 0);

return count;
}

int SerialIntrfTx(SERINTRFDEV *pDev, int DevAddr, uint8_t *pBuff, int BuffLen)
{
int count = 0;
int nrtry = pDev->MaxRetry;

do {
if (pBuff && pDev->StartTx(pDev, DevAddr)) {
count = pDev->TxData(pDev, pBuff, BuffLen);
pDev->StopTx(pDev);
}
} while (count <= 0 && nrtry-- > 0);

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 Expand Up @@ -62,3 +117,4 @@ int SerialIntrf::Tx(int DevAddr, uint8_t *pData, int DataLen)
return count;
}
*/

0 comments on commit 6d4c473

Please sign in to comment.