From 5299036409128b7ccaab492d083f7bdf63555688 Mon Sep 17 00:00:00 2001 From: EduardoCavValenca Date: Thu, 10 Feb 2022 21:00:29 -0300 Subject: [PATCH 1/3] VScode implementation --- I2C.cpp | 26 ++++++++++++++++++++++++++ I2C.h | 20 ++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 I2C.cpp create mode 100644 I2C.h diff --git a/I2C.cpp b/I2C.cpp new file mode 100644 index 0000000..de80212 --- /dev/null +++ b/I2C.cpp @@ -0,0 +1,26 @@ +#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[tamanho]; + if(HAL_I2C_Mem_Read(this->handler, this->endereco, registrador, tam_registrador, dados, tamanho, this->delay) == HAL_OK) + return dados; + else + throw 0; //Resolve exception +} + + +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) +} diff --git a/I2C.h b/I2C.h new file mode 100644 index 0000000..6044867 --- /dev/null +++ b/I2C.h @@ -0,0 +1,20 @@ +#ifndef I2C_H +#define I2C_H + +#include + +class I2C { + private: + uint32_t delay; + I2C_HandleTypeDef *handler; + uint16_t endereco; + + public: + I2C(I2C_HandleTypeDef* handler, uint16_t endereco, uint32_t delay); //Definir i2c_config + ~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 \ No newline at end of file From 5748ba3b3c117f2c5ed5068a6741e3726c667884 Mon Sep 17 00:00:00 2001 From: EduardoCavValenca Date: Thu, 17 Feb 2022 10:16:18 -0300 Subject: [PATCH 2/3] After cube IDE test --- I2C.cpp | 12 +++++------- I2C.h | 4 +++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/I2C.cpp b/I2C.cpp index de80212..ee96093 100644 --- a/I2C.cpp +++ b/I2C.cpp @@ -13,14 +13,12 @@ I2C::~I2C() { uint8_t *I2C::ler(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho) { - uint8_t dados[tamanho]; - if(HAL_I2C_Mem_Read(this->handler, this->endereco, registrador, tam_registrador, dados, tamanho, this->delay) == HAL_OK) - return dados; - else - throw 0; //Resolve exception + 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) -} + return (HAL_I2C_Mem_Write(this->handler, this->endereco, registrador, tam_registrador, dados, tamanho, this->delay) == HAL_OK); +} \ No newline at end of file diff --git a/I2C.h b/I2C.h index 6044867..3c7e624 100644 --- a/I2C.h +++ b/I2C.h @@ -1,6 +1,8 @@ #ifndef I2C_H #define I2C_H +#include "main.h" +#include #include class I2C { @@ -10,7 +12,7 @@ class I2C { uint16_t endereco; public: - I2C(I2C_HandleTypeDef* handler, uint16_t endereco, uint32_t delay); //Definir i2c_config + 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); From d0f3b6b0adfb170cdb6a0e3fbcdf6ba41cef12a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Murilo=20Tod=C3=A3o?= <70351552+murilo-toddy@users.noreply.github.com> Date: Thu, 17 Feb 2022 12:03:16 -0300 Subject: [PATCH 3/3] Update identation --- I2C.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/I2C.cpp b/I2C.cpp index ee96093..176f585 100644 --- a/I2C.cpp +++ b/I2C.cpp @@ -14,8 +14,8 @@ I2C::~I2C() { 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; + HAL_I2C_Mem_Read(this->handler, this->endereco, registrador, tam_registrador, dados, tamanho, this->delay); + return dados; }