Skip to content

Commit

Permalink
Merge pull request #6 from MaximumChungus/I2CCommit
Browse files Browse the repository at this point in the history
Update 03-09-2021
  • Loading branch information
Guilouz authored Sep 3, 2021
2 parents e2689ff + 2bd7078 commit 22c4184
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 53 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
// @section info

// Author info of this build printed to the host during boot and M115
#define STRING_CONFIG_H_AUTHOR "(Cyril Guislain, Super Racer Nano V3)" // Who made the changes.
#define STRING_CONFIG_H_AUTHOR "(Cyril Guislain, Brandon Salahat, Super Racer Nano V3)" // Who made the changes.
//#define CUSTOM_VERSION_FILE Version.h // Path from the root directory (no quotes)

/**
Expand Down
7 changes: 6 additions & 1 deletion Marlin/src/HAL/STM32/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ uint16_t HAL_adc_result;

// HAL initialization task
void HAL_init() {
FastIO_init();

// Ensure F_CPU is a constant expression.
// If the compiler breaks here, it means that delay code that should compute at compile time will not work.
// So better safe than sorry here.
constexpr int cpuFreq = F_CPU;
UNUSED(cpuFreq);

#if ENABLED(SDSUPPORT) && DISABLED(SDIO_SUPPORT) && (defined(SDSS) && SDSS != -1)
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/fastio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "../../inc/MarlinConfig.h"

GPIO_TypeDef* FastIOPortMap[LastPort + 1];
GPIO_TypeDef* FastIOPortMap[LastPort + 1] = { 0 };

void FastIO_init() {
LOOP_L_N(i, NUM_DIGITAL_PINS)
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/HAL/STM32/fastio.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern GPIO_TypeDef * FastIOPortMap[];
// ------------------------

void FastIO_init(); // Must be called before using fast io macros
#define FASTIO_INIT() FastIO_init()

// ------------------------
// Defines
Expand Down
6 changes: 6 additions & 0 deletions Marlin/src/HAL/STM32/inc/Conditionals_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@
#if defined(USBD_USE_CDC_MSC) && DISABLED(NO_SD_HOST_DRIVE)
#define HAS_SD_HOST_DRIVE 1
#endif

// Fix F_CPU not being a compile-time constant in STSTM32 framework
#ifdef BOARD_F_CPU
#undef F_CPU
#define F_CPU BOARD_F_CPU
#endif
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32F1/eeprom_if_iic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void eeprom_init() { BL24CXX::init(); }
// Public functions
// ------------------------

void eeprom_write_byte(uint8_t *pos, unsigned char value) {
void eeprom_write_byte(uint8_t *pos, uint8_t value) {
const unsigned eeprom_address = (unsigned)pos;
return BL24CXX::writeOneByte(eeprom_address, value);
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/shared/eeprom_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
// EEPROM
//
void eeprom_init();
void eeprom_write_byte(uint8_t *pos, unsigned char value);
void eeprom_write_byte(uint8_t *pos, uint8_t value);
uint8_t eeprom_read_byte(uint8_t *pos);
32 changes: 22 additions & 10 deletions Marlin/src/HAL/shared/eeprom_if_i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,28 @@ static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRE
// Public functions
// ------------------------

void eeprom_write_byte(uint8_t *pos, unsigned char value) {
#define SMALL_EEPROM (MARLIN_EEPROM_SIZE <= 2048)

// Combine Address high bits into the device address on <=16Kbit (2K) and >512Kbit (64K) EEPROMs.
// Note: MARLIN_EEPROM_SIZE is specified in bytes, whereas EEPROM model numbers refer to bits.
// e.g., The "16" in BL24C16 indicates a 16Kbit (2KB) size.
static uint8_t _eeprom_calc_device_address(uint8_t * const pos) {
const unsigned eeprom_address = (unsigned)pos;
return (SMALL_EEPROM || MARLIN_EEPROM_SIZE > 65536)
? uint8_t(eeprom_device_address | ((eeprom_address >> (SMALL_EEPROM ? 8 : 16)) & 0x07))
: eeprom_device_address;
}

Wire.beginTransmission(eeprom_device_address);
Wire.write(int(eeprom_address >> 8)); // MSB
Wire.write(int(eeprom_address & 0xFF)); // LSB
static void _eeprom_begin(uint8_t * const pos) {
const unsigned eeprom_address = (unsigned)pos;
Wire.beginTransmission(_eeprom_calc_device_address(pos));
if (!SMALL_EEPROM)
Wire.write(uint8_t((eeprom_address >> 8) & 0xFF)); // Address High, if needed
Wire.write(uint8_t(eeprom_address & 0xFF)); // Address Low
}

void eeprom_write_byte(uint8_t *pos, uint8_t value) {
_eeprom_begin(pos);
Wire.write(value);
Wire.endTransmission();

Expand All @@ -75,13 +91,9 @@ void eeprom_write_byte(uint8_t *pos, unsigned char value) {
}

uint8_t eeprom_read_byte(uint8_t *pos) {
const unsigned eeprom_address = (unsigned)pos;

Wire.beginTransmission(eeprom_device_address);
Wire.write(int(eeprom_address >> 8)); // MSB
Wire.write(int(eeprom_address & 0xFF)); // LSB
_eeprom_begin(pos);
Wire.endTransmission();
Wire.requestFrom(eeprom_device_address, (byte)1);
Wire.requestFrom(_eeprom_calc_device_address(pos), (byte)1);
return Wire.available() ? Wire.read() : 0xFF;
}

Expand Down
61 changes: 29 additions & 32 deletions Marlin/src/HAL/shared/eeprom_if_spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,45 +43,42 @@ void eeprom_init() {}
#define EEPROM_WRITE_DELAY 7
#endif

uint8_t eeprom_read_byte(uint8_t* pos) {
uint8_t v;
uint8_t eeprom_temp[3];

// set read location
// begin transmission from device
eeprom_temp[0] = CMD_READ;
eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; // addr High
eeprom_temp[2] = (unsigned)pos& 0xFF; // addr Low
WRITE(SPI_EEPROM1_CS, HIGH);
WRITE(SPI_EEPROM1_CS, LOW);
static void _eeprom_begin(uint8_t * const pos, const uint8_t cmd) {
const uint8_t eeprom_temp[3] = {
cmd,
(unsigned(pos) >> 8) & 0xFF, // Address High
unsigned(pos) & 0xFF // Address Low
};
WRITE(SPI_EEPROM1_CS, HIGH); // Usually free already
WRITE(SPI_EEPROM1_CS, LOW); // Activate the Bus
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
// Leave the Bus in-use
}

uint8_t eeprom_read_byte(uint8_t* pos) {
_eeprom_begin(pos, CMD_READ); // Set read location and begin transmission

const uint8_t v = spiRec(SPI_CHAN_EEPROM1); // After READ a value sits on the Bus

WRITE(SPI_EEPROM1_CS, HIGH); // Done with device

v = spiRec(SPI_CHAN_EEPROM1);
WRITE(SPI_EEPROM1_CS, HIGH);
return v;
}

void eeprom_write_byte(uint8_t* pos, uint8_t value) {
uint8_t eeprom_temp[3];

/*write enable*/
eeprom_temp[0] = CMD_WREN;
WRITE(SPI_EEPROM1_CS, LOW);
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
WRITE(SPI_EEPROM1_CS, HIGH);
delay(1);

/*write addr*/
eeprom_temp[0] = CMD_WRITE;
eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; //addr High
eeprom_temp[2] = (unsigned)pos & 0xFF; //addr Low
void eeprom_write_byte(uint8_t *pos, uint8_t value) {
const uint8_t eeprom_temp = CMD_WREN;
WRITE(SPI_EEPROM1_CS, LOW);
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
spiSend(SPI_CHAN_EEPROM1, &eeprom_temp, 1); // Write Enable

WRITE(SPI_EEPROM1_CS, HIGH); // Done with the Bus
delay(1); // For a small amount of time

_eeprom_begin(pos, CMD_WRITE); // Set write address and begin transmission

spiSend(SPI_CHAN_EEPROM1, value);
WRITE(SPI_EEPROM1_CS, HIGH);
delay(EEPROM_WRITE_DELAY); // wait for page write to complete
spiSend(SPI_CHAN_EEPROM1, value); // Send the value to be written
WRITE(SPI_EEPROM1_CS, HIGH); // Done with the Bus
delay(EEPROM_WRITE_DELAY); // Give page write time to complete
}

#endif // USE_SHARED_EEPROM
#endif // I2C_EEPROM
#endif // I2C_EEPROM
4 changes: 3 additions & 1 deletion Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,9 @@ inline void tmc_standby_setup() {
* • Max7219
*/
void setup() {

#ifdef FASTIO_INIT
FASTIO_INIT();
#endif
tmc_standby_setup(); // TMC Low Power Standby pins must be set early or they're not usable

#if ENABLED(MARLIN_DEV_MODE)
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/feature/powerloss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ void PrintJobRecovery::load() {//改动
void PrintJobRecovery::prepare() {//改动
card.getAbsFilename(info.sd_filename); // SD filename
sd_filename_size = strlen(info.sd_filename);//新增
persistentStore.write_data(983, (uint8_t*)&sd_filename_size, sizeof(sd_filename_size));
//persistentStore.write_data(983, (uint8_t*)&sd_filename_size, sizeof(sd_filename_size));
MSerial.println(sd_filename_size);
for(int i = 0;i < sd_filename_size;i++)
{
persistentStore.write_data(985 + i, (uint8_t*)&info.sd_filename[i], sizeof(info.sd_filename[i]));
//persistentStore.write_data(985 + i, (uint8_t*)&info.sd_filename[i], sizeof(info.sd_filename[i]));
}//新增
cmd_sdpos = 0;
}
Expand Down Expand Up @@ -247,7 +247,7 @@ void PrintJobRecovery::save(const bool force/*=false*/, const float zraise/*=0*/
void PrintJobRecovery::write_eeprom()//新增
{
int eeprom_pos_duandian = 900;
persistentStore.write_data(eeprom_pos_duandian, (uint8_t*)&info.valid_head, sizeof(info.valid_head));
/*persistentStore.write_data(eeprom_pos_duandian, (uint8_t*)&info.valid_head, sizeof(info.valid_head));
eeprom_pos_duandian += sizeof(info.valid_head);
persistentStore.write_data(eeprom_pos_duandian, (uint8_t*)&info.valid_foot, sizeof(info.valid_foot));
eeprom_pos_duandian += sizeof(info.valid_foot);
Expand Down Expand Up @@ -292,7 +292,7 @@ void PrintJobRecovery::write_eeprom()//新增
persistentStore.write_data(eeprom_pos_duandian, (uint8_t*)&info.sdpos, sizeof(info.sdpos));
eeprom_pos_duandian += sizeof(info.sdpos);
persistentStore.write_data(eeprom_pos_duandian, (uint8_t*)&info.flag_duandian, sizeof(info.flag_duandian));
eeprom_pos_duandian += sizeof(info.flag_duandian);
eeprom_pos_duandian += sizeof(info.flag_duandian);*/
}
#if PIN_EXISTS(POWER_LOSS)

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
//
// Limit Switches
//
#define X_DIAG_PIN PD15
#define X_DIAG_PIN PA15 //was PD15
#define Y_DIAG_PIN PD2
#define Z_DIAG_PIN PC8
#define E0_DIAG_PIN PC4
Expand Down
13 changes: 13 additions & 0 deletions buildroot/share/PlatformIO/scripts/common-cxxflags.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
#"-Wno-sign-compare"
])

#
# Add CPU frequency as a compile time constant instead of a runtime variable
#
def add_cpu_freq():
if 'BOARD_F_CPU' in env:
env['BUILD_FLAGS'].append('-DBOARD_F_CPU=' + env['BOARD_F_CPU'])

# Useful for JTAG debugging
#
# It will separe release and debug build folders.
Expand All @@ -20,3 +27,9 @@
#
if env.GetBuildType() == "debug":
env['BUILD_DIR'] = '$PROJECT_BUILD_DIR/$PIOENV/debug'

# On some platform, F_CPU is a runtime variable. Since it's used to convert from ns
# to CPU cycles, this adds overhead preventing small delay (in the order of less than
# 30 cycles) to be generated correctly. By using a compile time constant instead
# the compiler will perform the computation and this overhead will be avoided
add_cpu_freq()

0 comments on commit 22c4184

Please sign in to comment.