Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/dev/mpr121.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ class Mpr121I2CTransport
return I2CHandle::Result::OK
!= i2c_.ReceiveBlocking(config_.dev_addr, data, size, 10);
}

bool ReadMem(uint8_t *data, uint8_t memAddress, uint16_t memSize)
{
return I2CHandle::Result::OK
!= i2c_.Mem_read(
config_.dev_addr,
memAddress,
1,
data,
memSize,
10);
}
private:
I2CHandle i2c_;
Config config_;
Expand Down Expand Up @@ -228,24 +238,18 @@ class Mpr121
\param reg the register address to read from
\returns the 8 bit value that was read.
*/
uint8_t ReadRegister8(uint8_t reg)
uint8_t ReadRegister8(uint8_t reg)
{
uint8_t buff;
SetTransportErr(transport_.Write(&reg, 1));
SetTransportErr(transport_.Read(&buff, 1));

SetTransportErr (transport_.ReadMem (&buff, reg, 1));
return buff;
}

/** Read the contents of a 16 bit device register.
\param reg the register address to read from
\returns the 16 bit value that was read.
*/
uint16_t ReadRegister16(uint8_t reg)
{
uint16_t buff;
SetTransportErr(transport_.Write(&reg, 1));
SetTransportErr(transport_.Read((uint8_t *)&buff, 2));
SetTransportErr (transport_.ReadMem ((uint8_t*)&buff, reg, 2));


return buff;
}
Expand Down
63 changes: 60 additions & 3 deletions src/per/i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class I2CHandle::Impl
uint8_t* data,
uint16_t data_size,
uint32_t timeout);

// =========================================================
// scheduling and global functions
struct DmaJob
Expand Down Expand Up @@ -92,11 +92,20 @@ class I2CHandle::Impl
uint16_t size,
I2CHandle::CallbackFunctionPtr callback,
void* callback_context);


I2CHandle::Result Mem_read(
uint16_t devAddress,
uint16_t memAddress,
uint16_t memAddSize,
uint8_t* pData,
uint16_t size,
uint32_t timeout);


void InitPins();
void DeinitPins();
};

};
// ======================================================================
// Error handler
// ======================================================================
Expand Down Expand Up @@ -281,6 +290,42 @@ I2CHandle::Result I2CHandle::Impl::Init(const I2CHandle::Config& config)
return I2CHandle::Result::OK;
}

I2CHandle::Result I2CHandle::Impl::Mem_read(
uint16_t devAddress,
uint16_t memAddress,
uint16_t memAddSize,
uint8_t* pData,
uint16_t size,
uint32_t timeout)
{
// wait for previous transfer to be finished
while(HAL_I2C_GetState(&i2c_hal_handle_) != HAL_I2C_STATE_READY) {};

HAL_StatusTypeDef status;
if(config_.mode == I2CHandle::Config::Mode::I2C_MASTER)
{
// status = HAL_I2C_Master_Transmit(
// &i2c_hal_handle_, address << 1, data, size, timeout);
status = HAL_I2C_Mem_Read (
&i2c_hal_handle_,
devAddress << 1,
memAddress,
memAddSize,
pData,
size,
timeout
);
}
else
{
//status = HAL_I2C_Slave_Transmit(&i2c_hal_handle_, data, size, timeout);
}
if(status != HAL_OK)
return I2CHandle::Result::ERR;

return I2CHandle::Result::OK;
}

I2CHandle::Result I2CHandle::Impl::TransmitBlocking(uint16_t address,
uint8_t* data,
uint16_t size,
Expand Down Expand Up @@ -855,6 +900,18 @@ I2CHandle::Result I2CHandle::ReadDataAtAddress(uint16_t address,
address, mem_address, mem_address_size, data, data_size, timeout);
}

I2CHandle::Result I2CHandle::Mem_read(
uint16_t devAddress,
uint16_t memAddress,
uint16_t memAddSize,
uint8_t* pData,
uint16_t size,
uint32_t timeout)
{
return pimpl->Mem_read(devAddress, memAddress, memAddSize, pdata, size, timeout);
}


I2CHandle::Result I2CHandle::WriteDataAtAddress(uint16_t address,
uint16_t mem_address,
uint16_t mem_address_size,
Expand Down
8 changes: 7 additions & 1 deletion src/per/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ class I2CHandle
uint8_t* data,
uint16_t data_size,
uint32_t timeout);

Result Mem_read(
uint16_t devAddress,
uint16_t memAddress,
uint16_t memAddSize,
uint8_t* pData,
uint16_t size,
uint32_t timeout);

class Impl; /**< & */

Expand Down
Loading