Skip to content

Commit

Permalink
adding status control, error handling, essential types, pointer type
Browse files Browse the repository at this point in the history
  • Loading branch information
kanndil committed Jan 13, 2025
1 parent 5e66cc7 commit ed16526
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 115 deletions.
152 changes: 60 additions & 92 deletions fw/EF_I2C.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,79 +52,70 @@
* Function Definitions
******************************************************************************/

void EF_I2C_setGclkEnable (uint32_t i2c_base, int value){
EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_setGclkEnable (EF_I2C_PTR i2c, uint32_t value){

i2c->GCLK = value;
}

void EF_I2C_writeToAddress(uint32_t i2c_base, char addr, char data){
EF_I2C_writeData(i2c_base, data);
EF_I2C_writeCmd(i2c_base, addr);
EF_I2C_stopCmd(i2c_base);
EF_I2C_waitBusy(i2c_base);
}
EF_DRIVER_STATUS EF_I2C_writeToAddress(EF_I2C_PTR i2c, char addr, char data){

void EF_I2C_readCommand(uint32_t i2c_base, char addr){
EF_I2C_readCmd(i2c_base, addr);
EF_I2C_stopCmd(i2c_base);
EF_I2C_waitBusy(i2c_base);
EF_I2C_writeData(i2c, data);
EF_I2C_writeCmd(i2c, addr);
EF_I2C_stopCmd(i2c);
EF_I2C_waitBusy(i2c);
}
void EF_I2C_writeAddress(uint32_t i2c_base, char addr){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_readCommand(EF_I2C_PTR i2c, char addr){

EF_I2C_readCmd(i2c, addr);
EF_I2C_stopCmd(i2c);
EF_I2C_waitBusy(i2c);
}
EF_DRIVER_STATUS EF_I2C_writeAddress(EF_I2C_PTR i2c, char addr){

// Clear the field bits in the register using the defined mask
int command = i2c->COMMAND & ~EF_I2C_COMMAND_REG_CMD_ADDRESS_MASK;
uint32_t command = i2c->COMMAND & ~EF_I2C_COMMAND_REG_CMD_ADDRESS_MASK;

// Set the bits with the given value at the defined offset
i2c->COMMAND = command | ((addr << EF_I2C_COMMAND_REG_CMD_ADDRESS_BIT) & EF_I2C_COMMAND_REG_CMD_ADDRESS_MASK);
}

void EF_I2C_startCmd(uint32_t i2c_base){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_startCmd(EF_I2C_PTR i2c){

i2c->COMMAND |= ((1 << EF_I2C_COMMAND_REG_CMD_START_BIT) & EF_I2C_COMMAND_REG_CMD_START_MASK);
}

void EF_I2C_readCmd(uint32_t i2c_base, char addr){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_readCmd(EF_I2C_PTR i2c, char addr){

i2c->COMMAND = ((addr << EF_I2C_COMMAND_REG_CMD_ADDRESS_BIT ) | EF_I2C_COMMAND_REG_CMD_READ_MASK);
}

void EF_I2C_writeCmd(uint32_t i2c_base, char addr){
EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_writeCmd(EF_I2C_PTR i2c, char addr){

i2c->COMMAND = ((addr << EF_I2C_COMMAND_REG_CMD_ADDRESS_BIT) | EF_I2C_COMMAND_REG_CMD_WRITE_MASK);
}

void EF_I2C_writeMultipleCmd(uint32_t i2c_base){
EF_DRIVER_STATUS EF_I2C_writeMultipleCmd(EF_I2C_PTR i2c){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
i2c->COMMAND |= ((1 << EF_I2C_COMMAND_REG_CMD_WRITE_MULTIPLE_BIT) & EF_I2C_COMMAND_REG_CMD_WRITE_MULTIPLE_MASK);
}

void EF_I2C_stopCmd(uint32_t i2c_base){
EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_stopCmd(EF_I2C_PTR i2c){

i2c->COMMAND = ((1 << EF_I2C_COMMAND_REG_CMD_STOP_BIT) & EF_I2C_COMMAND_REG_CMD_STOP_MASK);
}

void EF_I2C_setCommandReg(uint32_t i2c_base, int value){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_setCommandReg(EF_I2C_PTR i2c, uint32_t value){

i2c->COMMAND = value;
}

int EF_I2C_getCommandReg(uint32_t i2c_base){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_getCommandReg(EF_I2C_PTR i2c, uint32_t* command_value){

return(i2c->COMMAND);
*command_value = i2c->COMMAND;
}

void EF_I2C_writeData(uint32_t i2c_base, char data){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_writeData(EF_I2C_PTR i2c, char data){

// Clear the field bits in the register using the defined mask
// i2c->DATA &= ~EF_I2C_DATA_REG_DATA_MASK;
Expand All @@ -134,112 +125,91 @@ void EF_I2C_writeData(uint32_t i2c_base, char data){
i2c->DATA = ((data << EF_I2C_DATA_REG_DATA_BIT) & EF_I2C_DATA_REG_DATA_MASK);
}

char EF_I2C_readData(uint32_t i2c_base){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
int data = i2c->DATA;
bool valid = (data & EF_I2C_DATA_REG_DATA_VALID_MASK) >> EF_I2C_DATA_REG_DATA_VALID_BIT;
if (valid)
return (data & EF_I2C_DATA_REG_DATA_MASK) >> EF_I2C_DATA_REG_DATA_BIT;
else
return -1;
EF_DRIVER_STATUS EF_I2C_readData(EF_I2C_PTR i2c, char *data){
*data = i2c->DATA;
bool valid = (*data & EF_I2C_DATA_REG_DATA_VALID_MASK) >> EF_I2C_DATA_REG_DATA_VALID_BIT;
if (valid){
*data = (*data & EF_I2C_DATA_REG_DATA_MASK) >> EF_I2C_DATA_REG_DATA_BIT;
}else{
*data = -1;
}
}

void EF_I2C_setDataValid(uint32_t i2c_base, bool valid){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_setDataValid(EF_I2C_PTR i2c, bool valid){

if (valid)
i2c->DATA |= ((1 << EF_I2C_DATA_REG_DATA_VALID_BIT) & EF_I2C_DATA_REG_DATA_VALID_MASK);
else
i2c->DATA &= ~EF_I2C_DATA_REG_DATA_VALID_MASK;
}

bool EF_I2C_getDataValid(uint32_t i2c_base){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_getDataValid(EF_I2C_PTR i2c, bool *data_valid){

if ((i2c->DATA & EF_I2C_DATA_REG_DATA_VALID_MASK) >> EF_I2C_DATA_REG_DATA_VALID_BIT)
return true;
*data_valid = true;
else
return false;
*data_valid = false;
}

void EF_I2C_setDataLast(uint32_t i2c_base, bool valid){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_setDataLast(EF_I2C_PTR i2c, bool valid){

if (valid)
i2c->DATA |= ((1 << EF_I2C_DATA_REG_DATA_LAST_BIT) & EF_I2C_DATA_REG_DATA_LAST_MASK);
else
i2c->DATA &= ~EF_I2C_DATA_REG_DATA_LAST_MASK;
}

bool EF_I2C_getDataLast(uint32_t i2c_base){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_getDataLast(EF_I2C_PTR i2c, bool *data_last){

if ((i2c->DATA & EF_I2C_DATA_REG_DATA_LAST_MASK) >> EF_I2C_DATA_REG_DATA_LAST_BIT)
return true;
*data_last = true;
else
return false;
*data_last = false;
}

void EF_I2C_setDataReg(uint32_t i2c_base, int value){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_setDataReg(EF_I2C_PTR i2c, uint32_t value){

i2c->DATA = value;
}

int EF_I2C_getDataReg(uint32_t i2c_base){
EF_DRIVER_STATUS EF_I2C_getDataReg(EF_I2C_PTR i2c, uint32_t* data_reg_value){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;

return(i2c->DATA);
*data_reg_value = i2c->DATA;
}

void EF_I2C_setPrescaler(uint32_t i2c_base, int value){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
EF_DRIVER_STATUS EF_I2C_setPrescaler(EF_I2C_PTR i2c, uint32_t value){

i2c->PR = value;
}

int EF_I2C_getPrescaler(uint32_t i2c_base){
EF_DRIVER_STATUS EF_I2C_getPrescaler(EF_I2C_PTR i2c, uint32_t* prescaler_value){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;

return(i2c->PR);
*prescaler_value = i2c->PR;
}

int EF_I2C_getRIS(uint32_t i2c_base){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
return (i2c->RIS);
EF_DRIVER_STATUS EF_I2C_getRIS(EF_I2C_PTR i2c, uint32_t* ris_value){

*ris_value = i2c->RIS;
}

int EF_I2C_getMIS(uint32_t i2c_base){
EF_DRIVER_STATUS EF_I2C_getMIS(EF_I2C_PTR i2c, uint32_t* mis_value){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
return (i2c->MIS);
*mis_value = i2c->MIS;
}

void EF_I2C_setIM(uint32_t i2c_base, int mask){
EF_DRIVER_STATUS EF_I2C_setIM(EF_I2C_PTR i2c, uint32_t mask){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
i2c->IM = mask;
}

int EF_I2C_getIM(uint32_t i2c_base){
EF_DRIVER_STATUS EF_I2C_getIM(EF_I2C_PTR i2c, uint32_t* im_value){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
return (i2c->IM);
*im_value = i2c->IM;
}

void EF_I2C_waitBusy(uint32_t i2c_base){
EF_DRIVER_STATUS EF_I2C_waitBusy(EF_I2C_PTR i2c){

EF_I2C_TYPE* i2c = (EF_I2C_TYPE*)i2c_base;
while(i2c->STATUS & 0b1 == 1);
}

Expand All @@ -251,8 +221,6 @@ void EF_I2C_waitBusy(uint32_t i2c_base){





#endif // EF_I2C_C

/******************************************************************************
Expand Down
46 changes: 23 additions & 23 deletions fw/EF_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@
******************************************************************************/


void EF_I2C_setGclkEnable (uint32_t i2c_base, int value);
void EF_I2C_writeAddress(uint32_t i2c_base, char addr);
void EF_I2C_startCmd(uint32_t i2c_base);
void EF_I2C_readCmd(uint32_t i2c_base, char addr);
void EF_I2C_writeCmd(uint32_t i2c_base, char addr);
void EF_I2C_writeMultipleCmd(uint32_t i2c_base);
void EF_I2C_stopCmd(uint32_t i2c_base);
void EF_I2C_setCommandReg(uint32_t i2c_base, int value);
int EF_I2C_getCommandReg(uint32_t i2c_base);
void EF_I2C_writeData(uint32_t i2c_base, char data);
char EF_I2C_readData(uint32_t i2c_base);
void EF_I2C_setDataValid(uint32_t i2c_base, bool valid);
bool EF_I2C_getDataValid(uint32_t i2c_base);
void EF_I2C_setDataLast(uint32_t i2c_base, bool valid);
bool EF_I2C_getDataLast(uint32_t i2c_base);
void EF_I2C_setDataReg(uint32_t i2c_base, int value);
int EF_I2C_getDataReg(uint32_t i2c_base);
void EF_I2C_setPrescaler(uint32_t i2c_base, int value);
int EF_I2C_getPrescaler(uint32_t i2c_base);
int EF_I2C_getRIS(uint32_t i2c_base);
int EF_I2C_getMIS(uint32_t i2c_base);
void EF_I2C_setIM(uint32_t i2c_base, int mask);
int EF_I2C_getIM(uint32_t i2c_base);
EF_DRIVER_STATUS EF_I2C_setGclkEnable (EF_I2C_PTR i2c, uint32_t value);
EF_DRIVER_STATUS EF_I2C_writeAddress(EF_I2C_PTR i2c, char addr);
EF_DRIVER_STATUS EF_I2C_startCmd(EF_I2C_PTR i2c);
EF_DRIVER_STATUS EF_I2C_readCmd(EF_I2C_PTR i2c, char addr);
EF_DRIVER_STATUS EF_I2C_writeCmd(EF_I2C_PTR i2c, char addr);
EF_DRIVER_STATUS EF_I2C_writeMultipleCmd(EF_I2C_PTR i2c);
EF_DRIVER_STATUS EF_I2C_stopCmd(EF_I2C_PTR i2c);
EF_DRIVER_STATUS EF_I2C_setCommandReg(EF_I2C_PTR i2c, uint32_t value);
EF_DRIVER_STATUS EF_I2C_getCommandReg(EF_I2C_PTR i2c, uint32_t* command_value);
EF_DRIVER_STATUS EF_I2C_writeData(EF_I2C_PTR i2c, char data);
EF_DRIVER_STATUS EF_I2C_readData(EF_I2C_PTR i2c, char *data);
EF_DRIVER_STATUS EF_I2C_setDataValid(EF_I2C_PTR i2c, bool valid);
EF_DRIVER_STATUS EF_I2C_getDataValid(EF_I2C_PTR i2c, bool *data_valid);
EF_DRIVER_STATUS EF_I2C_setDataLast(EF_I2C_PTR i2c, bool valid);
EF_DRIVER_STATUS EF_I2C_getDataLast(EF_I2C_PTR i2c, bool *data_last);
EF_DRIVER_STATUS EF_I2C_setDataReg(EF_I2C_PTR i2c, uint32_t value);
EF_DRIVER_STATUS EF_I2C_getDataReg(EF_I2C_PTR i2c, uint32_t* data_reg_value);
EF_DRIVER_STATUS EF_I2C_setPrescaler(EF_I2C_PTR i2c, uint32_t value);
EF_DRIVER_STATUS EF_I2C_getPrescaler(EF_I2C_PTR i2c, uint32_t* prescaler_value);
EF_DRIVER_STATUS EF_I2C_getRIS(EF_I2C_PTR i2c, uint32_t* ris_value);
EF_DRIVER_STATUS EF_I2C_getMIS(EF_I2C_PTR i2c, uint32_t* mis_value);
EF_DRIVER_STATUS EF_I2C_setIM(EF_I2C_PTR i2c, uint32_t mask);
EF_DRIVER_STATUS EF_I2C_getIM(EF_I2C_PTR i2c, uint32_t* im_value);

/******************************************************************************
* External Variables
Expand Down

0 comments on commit ed16526

Please sign in to comment.