Bitbang I2C setup + detect test
This commit is contained in:
232
workspace/TS100/Core/BSP/Miniware/I2CBB.cpp
Normal file
232
workspace/TS100/Core/BSP/Miniware/I2CBB.cpp
Normal file
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
* I2CBB.cpp
|
||||
*
|
||||
* Created on: 12 Jun 2020
|
||||
* Author: Ralim
|
||||
*/
|
||||
|
||||
#include <I2CBB.hpp>
|
||||
|
||||
#define SCL_HIGH() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_SET)
|
||||
#define SCL_LOW() HAL_GPIO_WritePin(SCL2_GPIO_Port, SCL2_Pin, GPIO_PIN_RESET)
|
||||
#define SDA_HIGH() HAL_GPIO_WritePin(SDA2_GPIO_Port, SDA2_Pin, GPIO_PIN_SET)
|
||||
#define SDA_LOW() HAL_GPIO_WritePin(SDA2_GPIO_Port, SDA2_Pin, GPIO_PIN_RESET)
|
||||
#define SDA_READ() (HAL_GPIO_ReadPin(SDA2_GPIO_Port,SDA2_Pin)==GPIO_PIN_SET?1:0)
|
||||
#define SCL_READ() (HAL_GPIO_ReadPin(SCL2_GPIO_Port,SCL2_Pin)==GPIO_PIN_SET?1:0)
|
||||
#define I2C_DELAY() HAL_Delay(1);
|
||||
void I2CBB::init() {
|
||||
//Set GPIO's to output open drain
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
|
||||
GPIO_InitStruct.Pin = SDA2_Pin | SCL2_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(SDA2_GPIO_Port, &GPIO_InitStruct);
|
||||
SDA_HIGH();
|
||||
SCL_HIGH();
|
||||
}
|
||||
|
||||
bool I2CBB::probe(uint8_t address) {
|
||||
start();
|
||||
bool ack = send(address);
|
||||
stop();
|
||||
return ack;
|
||||
}
|
||||
|
||||
bool I2CBB::Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData,
|
||||
uint16_t Size) {
|
||||
start();
|
||||
bool ack = send(DevAddress | 1);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
ack = send(MemAddress);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
while (Size) {
|
||||
pData[0] = read(Size > 1);
|
||||
pData++;
|
||||
Size--;
|
||||
}
|
||||
stop();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool I2CBB::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData,
|
||||
uint16_t Size) {
|
||||
start();
|
||||
bool ack = send(DevAddress);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
ack = send(MemAddress);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
while (Size) {
|
||||
bool ack = send(pData[0]);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
pData++;
|
||||
Size--;
|
||||
}
|
||||
stop();
|
||||
return true;
|
||||
}
|
||||
|
||||
void I2CBB::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) {
|
||||
start();
|
||||
bool ack = send(DevAddress);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
while (Size) {
|
||||
bool ack = send(pData[0]);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
pData++;
|
||||
Size--;
|
||||
}
|
||||
stop();
|
||||
|
||||
}
|
||||
|
||||
void I2CBB::Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size) {
|
||||
start();
|
||||
bool ack = send(DevAddress | 1);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
while (Size) {
|
||||
pData[0] = read(Size > 1);
|
||||
pData++;
|
||||
Size--;
|
||||
}
|
||||
stop();
|
||||
}
|
||||
|
||||
void I2CBB::TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx,
|
||||
uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx) {
|
||||
if (Size_tx == 0 && Size_rx == 0)
|
||||
return;
|
||||
if (Size_tx) {
|
||||
start();
|
||||
bool ack = send(DevAddress);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
while (Size_tx) {
|
||||
bool ack = send(pData_tx[0]);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
pData_tx++;
|
||||
Size_tx--;
|
||||
}
|
||||
}
|
||||
if (Size_rx) {
|
||||
start();
|
||||
bool ack = send(DevAddress | 1);
|
||||
if (!ack) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
while (Size_rx) {
|
||||
pData_rx[0] = read(Size_rx > 1);
|
||||
pData_rx++;
|
||||
Size_rx--;
|
||||
}
|
||||
}
|
||||
stop();
|
||||
}
|
||||
|
||||
void I2CBB::start() {
|
||||
/* I2C Start condition, data line goes low when clock is high */
|
||||
SCL_HIGH();
|
||||
SDA_HIGH();
|
||||
I2C_DELAY();
|
||||
SDA_LOW();
|
||||
I2C_DELAY();
|
||||
SCL_LOW();
|
||||
I2C_DELAY();
|
||||
}
|
||||
|
||||
void I2CBB::stop() {
|
||||
/* I2C Stop condition, clock goes high when data is low */
|
||||
SDA_LOW();
|
||||
I2C_DELAY();
|
||||
SCL_HIGH();
|
||||
I2C_DELAY();
|
||||
SDA_HIGH();
|
||||
I2C_DELAY();
|
||||
}
|
||||
|
||||
bool I2CBB::send(uint8_t value) {
|
||||
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
write_bit(value & 0x80); // write the most-significant bit
|
||||
value <<= 1;
|
||||
}
|
||||
|
||||
bool ack = read_bit()==0;
|
||||
return ack;
|
||||
}
|
||||
|
||||
uint8_t I2CBB::read(bool ack) {
|
||||
uint8_t B = 0;
|
||||
|
||||
uint8_t i;
|
||||
for (i = 0; i < 8; i++) {
|
||||
B <<= 1;
|
||||
B |= read_bit();
|
||||
}
|
||||
|
||||
if (ack)
|
||||
write_bit(0);
|
||||
else
|
||||
write_bit(1);
|
||||
return B;
|
||||
}
|
||||
|
||||
uint8_t I2CBB::read_bit() {
|
||||
uint8_t b;
|
||||
|
||||
SDA_HIGH();
|
||||
I2C_DELAY();
|
||||
SCL_HIGH();
|
||||
I2C_DELAY();
|
||||
|
||||
if (SDA_READ())
|
||||
b = 1;
|
||||
else
|
||||
b = 0;
|
||||
|
||||
SCL_LOW();
|
||||
return b;
|
||||
}
|
||||
|
||||
void I2CBB::write_bit(uint8_t val) {
|
||||
if (val > 0)
|
||||
SDA_HIGH();
|
||||
else
|
||||
SDA_LOW();
|
||||
|
||||
I2C_DELAY();
|
||||
SCL_HIGH();
|
||||
I2C_DELAY();
|
||||
SCL_LOW();
|
||||
}
|
||||
42
workspace/TS100/Core/BSP/Miniware/I2CBB.hpp
Normal file
42
workspace/TS100/Core/BSP/Miniware/I2CBB.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* I2CBB.hpp
|
||||
*
|
||||
* Created on: 12 Jun 2020
|
||||
* Author: Ralim
|
||||
*/
|
||||
|
||||
#ifndef BSP_MINIWARE_I2CBB_HPP_
|
||||
#define BSP_MINIWARE_I2CBB_HPP_
|
||||
#include "BSP.h"
|
||||
#include "Setup.h"
|
||||
#include "Pins.h"
|
||||
/*
|
||||
* Simple static I2C bit-bang class used on the TS80P
|
||||
* SCL = PA5
|
||||
* SDA = PA1
|
||||
*/
|
||||
class I2CBB {
|
||||
public:
|
||||
static void init();
|
||||
//Probe if device ACK's address or not
|
||||
static bool probe(uint8_t address);
|
||||
//Issues a complete 8bit register read
|
||||
static bool Mem_Read(uint16_t DevAddress, uint16_t MemAddress,
|
||||
uint8_t *pData, uint16_t Size);
|
||||
//Implements a register write
|
||||
static bool Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
|
||||
uint8_t *pData, uint16_t Size);
|
||||
static void Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size);
|
||||
static void Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size);
|
||||
static void TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx,
|
||||
uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx);
|
||||
private:
|
||||
static void start();
|
||||
static void stop();
|
||||
static bool send(uint8_t value);
|
||||
static uint8_t read(bool ack);
|
||||
static uint8_t read_bit();
|
||||
static void write_bit(uint8_t val);
|
||||
};
|
||||
|
||||
#endif /* BSP_MINIWARE_I2CBB_HPP_ */
|
||||
@@ -77,6 +77,10 @@
|
||||
#define SCL_GPIO_Port GPIOB
|
||||
#define SDA_Pin GPIO_PIN_7
|
||||
#define SDA_GPIO_Port GPIOB
|
||||
#define SCL2_Pin GPIO_PIN_5
|
||||
#define SCL2_GPIO_Port GPIOA
|
||||
#define SDA2_Pin GPIO_PIN_1
|
||||
#define SDA2_GPIO_Port GPIOA
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
#include "BSP_Power.h"
|
||||
#include "QC3.h"
|
||||
#include "Settings.h"
|
||||
#include "FUSB302.h"
|
||||
bool FUSB302_present = false;
|
||||
void power_probe() {
|
||||
// If TS80 probe for QC
|
||||
// If TS100 - noop
|
||||
#ifdef MODEL_TS80
|
||||
|
||||
|
||||
startQC(systemSettings.voltageDiv);
|
||||
|
||||
seekQC((systemSettings.cutoutSetting) ? 120 : 90,
|
||||
@@ -18,4 +22,11 @@ void power_check() {
|
||||
#ifdef MODEL_TS80
|
||||
QC_resync();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
uint8_t usb_pd_detect(){
|
||||
#ifdef MODEL_TS80
|
||||
FUSB302_present=fusb302_detect();
|
||||
return FUSB302_present;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "BSP.h"
|
||||
#include "Setup.h"
|
||||
#include "Pins.h"
|
||||
#include "I2CBB.hpp"
|
||||
void preRToSInit() {
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick.
|
||||
*/
|
||||
@@ -18,5 +19,5 @@ void preRToSInit() {
|
||||
HAL_Delay(50);
|
||||
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(50);
|
||||
|
||||
I2CBB::init();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user