Skip to content

Commit

Permalink
Refactor I2C_SOFT to new #define
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralim committed Jun 3, 2023
1 parent 286afad commit a06d6cf
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion source/Core/BSP/MHP30/Software_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "BSP.h"
#include "configuration.h"
#include "stm32f1xx_hal.h"
#ifdef I2C_SOFT
#ifdef I2C_SOFT_PD

#define SOFT_SCL_HIGH() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_SET)
#define SOFT_SCL_LOW() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_RESET)
Expand Down
2 changes: 1 addition & 1 deletion source/Core/BSP/MHP30/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@

#define POW_PD 1
#define TEMP_NTC
#define I2C_SOFT
#define I2C_SOFT_PD
#define BATTFILTERDEPTH 8
#define OLED_I2CBB
#define ACCEL_EXITS_ON_MOVEMENT
Expand Down
15 changes: 8 additions & 7 deletions source/Core/BSP/Miniware/Software_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#include "BSP.h"
#include "configuration.h"
#include "stm32f1xx_hal.h"
#ifdef I2C_SOFT
#ifdef I2C_SOFT_PD

#define SOFT_SCL2_HIGH() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_SET)
#define SOFT_SCL2_LOW() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_RESET)
#define SOFT_SDA2_HIGH() HAL_GPIO_WritePin(SDA2_GPIO_Port, SDA2_Pin, GPIO_PIN_SET)
#define SOFT_SDA2_LOW() HAL_GPIO_WritePin(SDA2_GPIO_Port, SDA2_Pin, GPIO_PIN_RESET)
#define SOFT_SDA2_READ() (HAL_GPIO_ReadPin(SDA2_GPIO_Port, SDA2_Pin) == GPIO_PIN_SET ? 1 : 0)
#define SOFT_SCL2_READ() (HAL_GPIO_ReadPin(SCL2_GPIO_Port, SCL2_Pin) == GPIO_PIN_SET ? 1 : 0)

#define SOFT_SCL_HIGH() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_SET)
#define SOFT_SCL_LOW() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_RESET)
#define SOFT_SDA_HIGH() HAL_GPIO_WritePin(SDA2_GPIO_Port, SDA2_Pin, GPIO_PIN_SET)
#define SOFT_SDA_LOW() HAL_GPIO_WritePin(SDA2_GPIO_Port, SDA2_Pin, GPIO_PIN_RESET)
#define SOFT_SDA_READ() (HAL_GPIO_ReadPin(SDA2_GPIO_Port, SDA2_Pin) == GPIO_PIN_SET ? 1 : 0)
#define SOFT_SCL_READ() (HAL_GPIO_ReadPin(SCL2_GPIO_Port, SCL2_Pin) == GPIO_PIN_SET ? 1 : 0)
#define SOFT_I2C_DELAY() \
{ \
for (int xx = 0; xx < 15; xx++) { \
Expand Down
2 changes: 1 addition & 1 deletion source/Core/BSP/Miniware/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
#define POW_PD 1
#define POW_QC 1
#define TEMP_NTC
#define I2C_SOFT
#define I2C_SOFT_PD
#define SC7_ORI_FLIP
#endif
#endif
Expand Down
3 changes: 2 additions & 1 deletion source/Core/BSP/Miniware/preRTOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "BSP.h"
#include "I2CBB.hpp"
#include "Pins.h"
#include "configuration.h"
#include "Setup.h"
#include <I2C_Wrapper.hpp>

Expand All @@ -17,7 +18,7 @@ void preRToSInit() {
HAL_Init();
Setup_HAL(); // Setup all the HAL objects
BSPInit();
#ifdef I2C_SOFT
#ifdef I2C_SOFT_PD
I2CBB::init();
#endif
/* Init the IPC objects */
Expand Down
2 changes: 1 addition & 1 deletion source/Core/BSP/Sequre_S60/Software_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "BSP.h"
#include "configuration.h"
#include "stm32f1xx_hal.h"
#ifdef I2C_SOFT
#ifdef I2C_SOFT_PD

#define SOFT_SCL_HIGH() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_SET)
#define SOFT_SCL_LOW() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_RESET)
Expand Down
2 changes: 1 addition & 1 deletion source/Core/BSP/Sequre_S60/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
#define POW_PD_EXT 1
#define HAS_POWER_DEBUG_MENU
#define TEMP_NTC
#define I2C_SOFT // For now we are doing software I2C to get around hardware chip issues
#define I2C_SOFT_PD // For now we are doing software I2C to get around hardware chip issues
#define OLED_I2CBB

#define MODEL_HAS_DCDC // We dont have DC/DC but have reallly fast PWM that gets us roughly the same place
Expand Down
2 changes: 1 addition & 1 deletion source/Core/BSP/Sequre_S60/preRTOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void preRToSInit() {
HAL_Init();
Setup_HAL(); // Setup all the HAL objects
BSPInit();
#ifdef I2C_SOFT
#ifdef I2C_SOFT_PD
I2CBB::init();
#endif
/* Init the IPC objects */
Expand Down
48 changes: 24 additions & 24 deletions source/Core/Drivers/I2CBB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Author: Ralim
*/
#include "configuration.h"
#ifdef I2C_SOFT
#ifdef I2C_SOFT_PD
#include "FreeRTOS.h"
#include <I2CBB.hpp>
SemaphoreHandle_t I2CBB::I2CSemaphore = NULL;
Expand All @@ -24,16 +24,16 @@ void I2CBB::init() {
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(SCL2_GPIO_Port, &GPIO_InitStruct);
SOFT_SDA_HIGH();
SOFT_SCL_HIGH();
SOFT_SDA2_HIGH();
SOFT_SCL2_HIGH();
// To ensure bus is unlocked; we toggle the Clock a bunch of times to make things error out
for (int i = 0; i < 128; i++) {
SOFT_SCL_LOW();
SOFT_SCL2_LOW();
asm("nop");
asm("nop");
asm("nop");
asm("nop");
SOFT_SCL_HIGH();
SOFT_SCL2_HIGH();
asm("nop");
asm("nop");
asm("nop");
Expand Down Expand Up @@ -69,7 +69,7 @@ bool I2CBB::Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, u
unlock();
return false;
}
SOFT_SCL_LOW();
SOFT_SCL2_LOW();
SOFT_I2C_DELAY();
// stop();
start();
Expand Down Expand Up @@ -208,23 +208,23 @@ void I2CBB::TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, uint16_t Siz

void I2CBB::start() {
/* I2C Start condition, data line goes low when clock is high */
SOFT_SCL_HIGH();
SOFT_SDA_HIGH();
SOFT_SCL2_HIGH();
SOFT_SDA2_HIGH();
SOFT_I2C_DELAY();
SOFT_SDA_LOW();
SOFT_SDA2_LOW();
SOFT_I2C_DELAY();
SOFT_SCL_LOW();
SOFT_SCL2_LOW();
SOFT_I2C_DELAY();
SOFT_SDA_HIGH();
SOFT_SDA2_HIGH();
}

void I2CBB::stop() {
/* I2C Stop condition, clock goes high when data is low */
SOFT_SDA_LOW();
SOFT_SDA2_LOW();
SOFT_I2C_DELAY();
SOFT_SCL_HIGH();
SOFT_SCL2_HIGH();
SOFT_I2C_DELAY();
SOFT_SDA_HIGH();
SOFT_SDA2_HIGH();
SOFT_I2C_DELAY();
}

Expand All @@ -235,7 +235,7 @@ bool I2CBB::send(uint8_t value) {
value <<= 1;
}

SOFT_SDA_HIGH();
SOFT_SDA2_HIGH();
bool ack = (read_bit() == 0);
return ack;
}
Expand All @@ -249,7 +249,7 @@ uint8_t I2CBB::read(bool ack) {
B |= read_bit();
}

SOFT_SDA_HIGH();
SOFT_SDA2_HIGH();
if (ack)
write_bit(0);
else
Expand All @@ -260,17 +260,17 @@ uint8_t I2CBB::read(bool ack) {
uint8_t I2CBB::read_bit() {
uint8_t b;

SOFT_SDA_HIGH();
SOFT_SDA2_HIGH();
SOFT_I2C_DELAY();
SOFT_SCL_HIGH();
SOFT_SCL2_HIGH();
SOFT_I2C_DELAY();

if (SOFT_SDA_READ())
if (SOFT_SDA2_READ())
b = 1;
else
b = 0;

SOFT_SCL_LOW();
SOFT_SCL2_LOW();
return b;
}

Expand All @@ -292,15 +292,15 @@ uint8_t I2CBB::I2C_RegisterRead(uint8_t address, uint8_t reg) {

void I2CBB::write_bit(uint8_t val) {
if (val) {
SOFT_SDA_HIGH();
SOFT_SDA2_HIGH();
} else {
SOFT_SDA_LOW();
SOFT_SDA2_LOW();
}

SOFT_I2C_DELAY();
SOFT_SCL_HIGH();
SOFT_SCL2_HIGH();
SOFT_I2C_DELAY();
SOFT_SCL_LOW();
SOFT_SCL2_LOW();
}

bool I2CBB::writeRegistersBulk(const uint8_t address, const I2C_REG *registers, const uint8_t registersLength) {
Expand Down
2 changes: 1 addition & 1 deletion source/Core/Drivers/I2CBB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef BSP_MINIWARE_I2CBB_HPP_
#define BSP_MINIWARE_I2CBB_HPP_
#include "configuration.h"
#ifdef I2C_SOFT
#ifdef I2C_SOFT_PD
#include "BSP.h"
#include "FreeRTOS.h"
#include "Pins.h"
Expand Down

0 comments on commit a06d6cf

Please sign in to comment.