This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from EESC-USP-TUPA/issue-3
Issue 3
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "I2C.h" | ||
|
||
I2C::I2C(I2C_HandleTypeDef* handler, uint16_t endereco, uint32_t delay){ | ||
this->handler = handler; | ||
this->endereco = endereco; | ||
this->delay = delay; | ||
} | ||
|
||
|
||
I2C::~I2C() { | ||
HAL_I2C_DeInit(this->handler); | ||
} | ||
|
||
|
||
uint8_t *I2C::ler(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho) { | ||
uint8_t *dados = (uint8_t*)malloc(tamanho * sizeof(uint8_t)); | ||
HAL_I2C_Mem_Read(this->handler, this->endereco, registrador, tam_registrador, dados, tamanho, this->delay); | ||
return dados; | ||
} | ||
|
||
|
||
bool I2C::escrever(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho, uint8_t* dados) { | ||
return (HAL_I2C_Mem_Write(this->handler, this->endereco, registrador, tam_registrador, dados, tamanho, this->delay) == HAL_OK); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef I2C_H | ||
#define I2C_H | ||
|
||
#include "main.h" | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
class I2C { | ||
private: | ||
uint32_t delay; | ||
I2C_HandleTypeDef *handler; | ||
uint16_t endereco; | ||
|
||
public: | ||
I2C(I2C_HandleTypeDef* handler, uint16_t endereco, uint32_t delay); | ||
~I2C(); | ||
uint8_t *ler(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho); | ||
bool escrever(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho, uint8_t* dados); | ||
|
||
}; | ||
|
||
#endif |