Skip to content

Commit

Permalink
save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
huaweiwx committed Jan 17, 2018
1 parent d5ee10a commit 87df5f9
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 66 deletions.
Expand Up @@ -24,8 +24,9 @@
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
#include <WireSoft.h>
#include <Wire.h>
#include <LED.h>
#include <Streaming.h> /*for Serial << */

TwoWire mywire(SDA, SCL);

Expand Down
Expand Up @@ -25,13 +25,14 @@
//
#include <LED.h>
#include <Wire.h>
TwoWire myWire(SDA,SCL);

//#include <WireSoft.h>
//TwoWire myWire(SDA,SCL);

//#define EE_SDA AT24CXX_SDA //PB7
//#define EE_SCL AT24CXX_SCL //PB6

//TwoWire Wire;
TwoWire myWire(SDA,SCL);

void setup() {
Serial.begin(115200);
Expand Down
73 changes: 36 additions & 37 deletions STM32/libraries/Wire/src/Wire.cpp
Expand Up @@ -44,26 +44,26 @@ void TwoWire::begin(void){
pdev->isMaster = 1;


#ifdef I2C1
#if defined(I2C1) && (USE_I2C1)
if (pdev->handle.Instance == I2C1) {
__HAL_RCC_I2C1_CLK_ENABLE();
}
#endif
#ifdef I2C2
#endif
#ifdef defined(I2C2) && (USE_I2C2)
if (pdev->handle.Instance == I2C2) {
__HAL_RCC_I2C2_CLK_ENABLE();
}
#endif
#ifdef I2C3
#endif
#if defined(I2C3) && (USE_I2C3)
if (pdev->handle.Instance == I2C3) {
__HAL_RCC_I2C3_CLK_ENABLE();
}
#endif
#ifdef I2C4
#endif
#if defined(I2C4) && (USE_I2C4)
if (pdev->handle.Instance == I2C4) {
__HAL_RCC_I2C4_CLK_ENABLE();
}
#endif
#endif

stm32AfI2CInit(pdev->handle.Instance,
variant_pin_list[pdev->sda].port,
Expand Down Expand Up @@ -93,38 +93,38 @@ void TwoWire::begin(uint8_t address) {
pdev->address = address << 1;


#ifdef I2C1
#if defined(I2C1) && (USE_I2C1)
if (pdev->handle.Instance == I2C1) {
slaveTwoWire[0] = this;
__HAL_RCC_I2C1_CLK_ENABLE();
HAL_NVIC_SetPriority(I2C1_EV_IRQn, I2C_PRIORITY, 0);
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
}
#endif
#ifdef I2C2
#endif
#if defined(I2C2) && (USE_I2C2)
if (pdev->handle.Instance == I2C2) {
slaveTwoWire[1] = this;
__HAL_RCC_I2C2_CLK_ENABLE();
HAL_NVIC_SetPriority(I2C2_EV_IRQn, I2C_PRIORITY, 0);
HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
}
#endif
#ifdef I2C3
#endif
#if defined(I2C3) && (USE_I2C3)
if (pdev->handle.Instance == I2C3) {
slaveTwoWire[2] = this;
__HAL_RCC_I2C3_CLK_ENABLE();
HAL_NVIC_SetPriority(I2C3_EV_IRQn, I2C_PRIORITY, 0);
HAL_NVIC_EnableIRQ(I2C3_EV_IRQn);
}
#endif
#ifdef I2C4
#endif
#if defined(I2C4) && (USE_I2C4)
if (pdev->handle.Instance == I2C4) {
slaveTwoWire[3] = this;
__HAL_RCC_I2C4_CLK_ENABLE();
HAL_NVIC_SetPriority(I2C4_EV_IRQn, I2C_PRIORITY, 0);
HAL_NVIC_EnableIRQ(I2C4_EV_IRQn);
}
#endif
#endif

stm32AfI2CInit (pdev->handle.Instance,
variant_pin_list[pdev->sda].port,
Expand All @@ -142,10 +142,10 @@ void TwoWire::begin(uint8_t address) {
setClock(100000);
HAL_I2C_Slave_Receive_IT(&pdev->handle, &pdev->slaveBuffer, 1);

//TODO rewrite IRQ handling to not use HAL_I2C_EV_IRQHandler, so F1 can also work
#ifndef STM32F1
//TODO rewrite IRQ handling to not use HAL_I2C_EV_IRQHandler, so F1 can also work L0?
#if !(defined(STM32F1)||defined(STM32L0))
HAL_I2C_EnableListen_IT(&pdev->handle);
#endif
#endif
}

void TwoWire::begin(int address) {
Expand All @@ -156,31 +156,30 @@ void TwoWire::end(void) {
HAL_I2C_DeInit(&pdev->handle);
}


void TwoWire::setClock(uint32_t frequency) {

#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || defined(STM32L1)
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || defined(STM32L1)
pdev->handle.Init.ClockSpeed = frequency;
pdev->handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
#else
#else

// I2C1_100KHZ_TIMING needs to be #defined in variant.h for these boards
// Open STM32CubeMX, select your chip, clock configuration according to systemclock_config.c
// Enable all I2Cs, go to I2Cx configuration, parameter settings, copy the Timing value.
#ifdef I2C1
#if defined(I2C1) && (USE_I2C1)
if (pdev->handle.Instance == I2C1) pdev->handle.Init.Timing = I2C1_100KHZ_TIMING;
#endif
#ifdef I2C2
#endif
#if defined(I2C2) && (USE_I2C2)
if (pdev->handle.Instance == I2C2) pdev->handle.Init.Timing = I2C2_100KHZ_TIMING;
#endif
#ifdef I2C3
#endif
#if defined(I2C3) && (USE_I2C3)
if (pdev->handle.Instance == I2C3) pdev->handle.Init.Timing = I2C3_100KHZ_TIMING;
#endif
#ifdef I2C4
#endif
#if defined(I2C4) && (USE_I2C4)
if (pdev->handle.Instance == I2C4) pdev->handle.Init.Timing = I2C4_100KHZ_TIMING;
#endif
#endif

#endif
#endif

HAL_I2C_Init(&pdev->handle);
}
Expand Down Expand Up @@ -407,25 +406,25 @@ void TwoWire::flush(void) {

TwoWire *interruptWire;

#ifdef I2C1
#if defined(I2C1) && (USE_I2C1)
extern "C" void I2C1_EV_IRQHandler(void ) {
interruptWire = slaveTwoWire[0];
HAL_I2C_EV_IRQHandler(&interruptWire->pdev->handle);
}
#endif
#ifdef I2C2
#if defined(I2C2) && (USE_I2C2)
extern "C" void I2C2_EV_IRQHandler(void ) {
interruptWire = slaveTwoWire[1];
HAL_I2C_EV_IRQHandler(&interruptWire->pdev->handle);
}
#endif
#ifdef I2C3
#if defined(I2C3) && (USE_I2C3)
extern "C" void I2C3_EV_IRQHandler(void ) {
interruptWire = slaveTwoWire[2];
HAL_I2C_EV_IRQHandler(&interruptWire->pdev->handle);
}
#endif
#ifdef I2C4
#if defined(I2C4) && (USE_I2C4)
extern "C" void I2C4_EV_IRQHandler(void ) {
interruptWire = slaveTwoWire[3];
HAL_I2C_EV_IRQHandler(&interruptWire->pdev->handle);
Expand Down Expand Up @@ -453,9 +452,9 @@ extern "C" void HAL_I2C_AddrCallback(I2C_HandleTypeDef *handle, uint8_t Transfer
interruptWire->pdev->txBufferIndex = 0;
interruptWire->pdev->txBufferLength = 0;

#ifdef I2C_IT_BUF
#ifdef I2C_IT_BUF
__HAL_I2C_ENABLE_IT(handle, I2C_IT_EVT | I2C_IT_BUF);
#endif
#endif
}
}

Expand Down
4 changes: 2 additions & 2 deletions STM32/libraries/Wire/src/Wire.h
Expand Up @@ -22,10 +22,10 @@
#ifndef TwoWire_h
#define TwoWire_h

#include "stm32_def.h"
#include <inttypes.h>
#include "Stream.h"
#include "stm32_def.h"

#include "util/toolschain.h" /*for __deprecated(x)*/
#if __has_include("configs/i2cEepromConfig.h")
# include "configs/i2cEepromConfig.h"
#endif
Expand Down
Expand Up @@ -41,7 +41,7 @@
#define AT24C512 0xffff


class EXTEEPROM : public CTwoWireSoft {
class EXTEEPROM : public TwoWireSoft {
public:
EXTEEPROM(uint8_t sda=SDA, uint8_t scl=SCL, uint8_t delay=SOFT_STANDARD);
void begin(uint8_t devAdr = 0x50, uint16_t devType = AT24C02);
Expand Down
Expand Up @@ -7,6 +7,7 @@

#include "WireSoft.h"
#include "at24cxx.h"
#include <Streaming.h> /*for Serial << */

//my vct6 board config, check and fix me

Expand Down
Expand Up @@ -24,14 +24,16 @@
// Devices with higher bit address might not be seen properly.
//
#include <LED.h>
//#include <Wire.h>
#include <WireSoft.h>
#include <Wire.h>
TwoWire myWire(SDA,SCL);

//#include <WireSoft.h>
//TwoWireSoft myWire(SDA,SCL);

//#define EE_SDA AT24CXX_SDA //PB7
//#define EE_SCL AT24CXX_SCL //PB6

//TwoWire Wire;
TwoWire myWire(SDA,SCL);

void setup() {
Serial.begin(115200);
Expand Down
32 changes: 16 additions & 16 deletions STM32/libraries/WireSoft/src/WireSoft.cpp
Expand Up @@ -51,7 +51,7 @@
* - always start with i2c_delay rather than end
*/

void CTwoWireSoft::sclPin(bool state) {
void TwoWireSoft::sclPin(bool state) {
I2C_DELAY(this->i2c_delay);
digitalWrite(this->scl_pin,state);
//Allow for clock stretching - dangerous currently
Expand All @@ -60,23 +60,23 @@ void CTwoWireSoft::sclPin(bool state) {
}
}

void CTwoWireSoft::sdaPin(bool state) {
void TwoWireSoft::sdaPin(bool state) {
I2C_DELAY(this->i2c_delay);
digitalWrite(this->sda_pin, state);
}

void CTwoWireSoft::i2c_start() {
void TwoWireSoft::i2c_start() {
sdaPin(LOW);
sclPin(LOW);
}

void CTwoWireSoft::i2c_stop() {
void TwoWireSoft::i2c_stop() {
sdaPin(LOW);
sclPin(HIGH);
sdaPin(HIGH);
}

bool CTwoWireSoft::i2c_get_ack() {
bool TwoWireSoft::i2c_get_ack() {
sclPin(LOW);
sdaPin(HIGH);
sclPin(HIGH);
Expand All @@ -86,19 +86,19 @@ bool CTwoWireSoft::i2c_get_ack() {
return ret;
}

void CTwoWireSoft::i2c_send_ack() {
void TwoWireSoft::i2c_send_ack() {
sdaPin(LOW);
sclPin(HIGH);
sclPin(LOW);
}

void CTwoWireSoft::i2c_send_nack() {
void TwoWireSoft::i2c_send_nack() {
sdaPin(HIGH);
sclPin(HIGH);
sclPin(LOW);
}

uint8_t CTwoWireSoft::i2c_shift_in() {
uint8_t TwoWireSoft::i2c_shift_in() {
uint8_t data = 0;
sdaPin(HIGH);

Expand All @@ -112,7 +112,7 @@ uint8_t CTwoWireSoft::i2c_shift_in() {
return data;
}

void CTwoWireSoft::i2c_shift_out(uint8_t val) {
void TwoWireSoft::i2c_shift_out(uint8_t val) {
int i;
for (i = 0; i < 8; i++) {
sdaPin(!!(val & (1 << (7 - i)) ) );
Expand All @@ -121,7 +121,7 @@ void CTwoWireSoft::i2c_shift_out(uint8_t val) {
}
}

uint8_t CTwoWireSoft::process() {
uint8_t TwoWireSoft::process() {
itc_msg.xferred = 0;

uint8_t sla_addr = (itc_msg.adr << 1);
Expand Down Expand Up @@ -168,12 +168,12 @@ uint8_t CTwoWireSoft::process() {

// TODO: Add in Error Handling if pins is out of range for other Maples
// TODO: Make delays more capable
CTwoWireSoft::CTwoWireSoft(uint8_t sda, uint8_t scl, uint8_t delay) : i2c_delay(delay) {
TwoWireSoft::TwoWireSoft(uint8_t sda, uint8_t scl, uint8_t delay) : i2c_delay(delay) {
this->scl_pin=scl;
this->sda_pin=sda;
}

void CTwoWireSoft::begin(uint8_t self_addr) {
void TwoWireSoft::begin(uint8_t self_addr) {
tx_buf_idx = 0;
tx_buf_overflow = false;
rx_buf_idx = 0;
Expand All @@ -184,7 +184,7 @@ void CTwoWireSoft::begin(uint8_t self_addr) {
sdaPin(HIGH);
}

void CTwoWireSoft::end()
void TwoWireSoft::end()
{
if (this->scl_pin)
{
Expand All @@ -196,11 +196,11 @@ void CTwoWireSoft::end()
}
}

CTwoWireSoft::~CTwoWireSoft() {
TwoWireSoft::~TwoWireSoft() {
this->scl_pin=0xff;
this->sda_pin=0xff;
}

// Declare the instance that the users of the library can use
//CTwoWireSoft WireSoft(SCL, SDA, SOFT_STANDARD);
//CTwoWireSoft WireSoft(PB6, PB7, SOFT_STANDARD);
//TwoWireSoft WireSoft(SCL, SDA, SOFT_STANDARD);
//TwoWireSoft WireSoft(PB6, PB7, SOFT_STANDARD);

0 comments on commit 87df5f9

Please sign in to comment.