diff --git a/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp b/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp index 79867d39a2..8b6ac025db 100644 --- a/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/SoftwareSerial.cpp @@ -54,8 +54,7 @@ unsigned char SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF]; volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0; volatile uint8_t SoftwareSerial::_receive_buffer_head = 0; -typedef struct _DELAY_TABLE -{ +typedef struct _DELAY_TABLE { long baud; uint16_t rx_delay_centering; uint16_t rx_delay_intrabit; @@ -64,16 +63,12 @@ typedef struct _DELAY_TABLE } DELAY_TABLE; // rough delay estimation -static const DELAY_TABLE table[] = -{ - //baud |rxcenter|rxintra |rxstop |tx - { 250000, 2, 4, 4, 4, }, //Done but not good due to instruction cycle error - { 115200, 4, 8, 8, 8, }, //Done but not good due to instruction cycle error +static const DELAY_TABLE table[] = { + //baud |rxcenter|rxintra |rxstop |tx { 250000, 2, 4, 4, 4, }, //Done but not good due to instruction cycle error { 115200, 4, 8, 8, 8, }, //Done but not good due to instruction cycle error //{ 74880, 69, 139, 62, 162, }, // estimation // { 57600, 100, 185, 1, 208, }, // Done but not good due to instruction cycle error //{ 38400, 13, 26, 26, 26, }, // Done - //{ 19200, 26, 52, 52, 52, }, // Done - { 9600, 52, 104, 104, 104, }, // Done + //{ 19200, 26, 52, 52, 52, }, // Done { 9600, 52, 104, 104, 104, }, // Done //{ 4800, 104, 208, 208, 208, }, //{ 2400, 208, 417, 417, 417, }, //{ 1200, 416, 833, 833, 833,}, @@ -85,7 +80,7 @@ static const DELAY_TABLE table[] = #if 0 /* static */ -inline void SoftwareSerial::tunedDelay(uint32_t count) { +inline void SoftwareSerial::tunedDelay(const uint32_t count) { asm volatile( @@ -101,20 +96,18 @@ inline void SoftwareSerial::tunedDelay(uint32_t count) { } #else -inline void SoftwareSerial::tunedDelay(uint32_t count) { +inline void SoftwareSerial::tunedDelay(const uint32_t count) { delayMicroseconds(count); } #endif // This function sets the current object as the "listening" // one and returns true if it replaces another -bool SoftwareSerial::listen() -{ +bool SoftwareSerial::listen() { if (!_rx_delay_stopbit) return false; - if (active_object != this) - { + if (active_object != this) { if (active_object) active_object->stopListening(); @@ -130,10 +123,8 @@ bool SoftwareSerial::listen() } // Stop listening. Returns true if we were actually listening. -bool SoftwareSerial::stopListening() -{ - if (active_object == this) - { +bool SoftwareSerial::stopListening() { + if (active_object == this) { setRxIntMsk(false); active_object = NULL; return true; @@ -144,14 +135,12 @@ bool SoftwareSerial::stopListening() // // The receive routine called by the interrupt handler // -void SoftwareSerial::recv() -{ +void SoftwareSerial::recv() { uint8_t d = 0; // If RX line is high, then we don't see any start bit // so interrupt is probably not for us - if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) - { + if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) { // Disable further interrupts during reception, this prevents // triggering another interrupt directly after we return, which can // cause problems at higher baudrates. @@ -160,38 +149,31 @@ void SoftwareSerial::recv() // Wait approximately 1/2 of a bit width to "center" the sample tunedDelay(_rx_delay_centering); // Read each of the 8 bits - for (uint8_t i=8; i > 0; --i) - { - tunedDelay(_rx_delay_intrabit); + for (uint8_t i=8; i > 0; --i) { + tunedDelay(_rx_delay_intrabit); d >>= 1; - if (rx_pin_read()) - d |= 0x80; + if (rx_pin_read()) d |= 0x80; } - if (_inverse_logic) - d = ~d; + if (_inverse_logic) d = ~d; // if buffer full, set the overflow flag and return uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; - if (next != _receive_buffer_head) - { + if (next != _receive_buffer_head) { // save new data in buffer: tail points to where byte goes _receive_buffer[_receive_buffer_tail] = d; // save new byte _receive_buffer_tail = next; } - else - { + else { _buffer_overflow = true; } - tunedDelay(_rx_delay_stopbit); + tunedDelay(_rx_delay_stopbit); // Re-enable interrupts when we're sure to be inside the stop bit - setRxIntMsk(true);//__enable_irq();// - + setRxIntMsk(true); //__enable_irq();// } } -uint32_t SoftwareSerial::rx_pin_read() -{ +uint32_t SoftwareSerial::rx_pin_read() { return digitalRead(_receivePin); } @@ -200,12 +182,9 @@ uint32_t SoftwareSerial::rx_pin_read() // /* static */ -inline void SoftwareSerial::handle_interrupt() -{ +inline void SoftwareSerial::handle_interrupt() { if (active_object) - { active_object->recv(); - } } extern "C" void intWrapper() { SoftwareSerial::handle_interrupt(); @@ -219,23 +198,19 @@ SoftwareSerial::SoftwareSerial(pin_t receivePin, pin_t transmitPin, bool inverse _rx_delay_stopbit(0), _tx_delay(0), _buffer_overflow(false), - _inverse_logic(inverse_logic) -{ + _inverse_logic(inverse_logic) { setTX(transmitPin); setRX(receivePin); - } // // Destructor // -SoftwareSerial::~SoftwareSerial() -{ +SoftwareSerial::~SoftwareSerial() { end(); } -void SoftwareSerial::setTX(pin_t tx) -{ +void SoftwareSerial::setTX(pin_t tx) { // First write, then set output. If we do this the other way around, // the pin would be output low for a short while before switching to // output hihg. Now, it is input with pullup for a short while, which @@ -244,36 +219,30 @@ void SoftwareSerial::setTX(pin_t tx) digitalWrite(tx, _inverse_logic ? LOW : HIGH); pinMode(tx,OUTPUT); _transmitPin = tx; - } -void SoftwareSerial::setRX(pin_t rx) -{ +void SoftwareSerial::setRX(pin_t rx) { pinMode(rx, INPUT_PULLUP); // pullup for normal logic! //if (!_inverse_logic) // digitalWrite(rx, HIGH); _receivePin = rx; _receivePort = LPC1768_PIN_PORT(rx); _receivePortPin = LPC1768_PIN_PIN(rx); -/* GPIO_T * rxPort = digitalPinToPort(rx); + /* GPIO_T * rxPort = digitalPinToPort(rx); _receivePortRegister = portInputRegister(rxPort); _receiveBitMask = digitalPinToBitMask(rx);*/ - } // // Public methods // -void SoftwareSerial::begin(long speed) -{ +void SoftwareSerial::begin(long speed) { _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0; - for(uint8_t i = 0; i < sizeof(table)/sizeof(table[0]); ++i) - { + for(uint8_t i = 0; i < sizeof(table)/sizeof(table[0]); ++i) { long baud = table[i].baud; - if(baud == speed) - { + if (baud == speed) { _rx_delay_centering = table[i].rx_delay_centering; _rx_delay_intrabit = table[i].rx_delay_intrabit; _rx_delay_stopbit = table[i].rx_delay_stopbit; @@ -289,29 +258,24 @@ void SoftwareSerial::begin(long speed) } -void SoftwareSerial::setRxIntMsk(bool enable) -{ +void SoftwareSerial::setRxIntMsk(bool enable) { if (enable) GpioEnableInt(_receivePort,_receivePin,CHANGE); else GpioDisableInt(_receivePort,_receivePin); } -void SoftwareSerial::end() -{ +void SoftwareSerial::end() { stopListening(); } // Read data from buffer -int SoftwareSerial::read() -{ - if (!isListening()) - return -1; +int SoftwareSerial::read() { + if (!isListening()) return -1; // Empty buffer? - if (_receive_buffer_head == _receive_buffer_tail) - return -1; + if (_receive_buffer_head == _receive_buffer_tail) return -1; // Read from "head" uint8_t d = _receive_buffer[_receive_buffer_head]; // grab next byte @@ -319,16 +283,13 @@ int SoftwareSerial::read() return d; } -int SoftwareSerial::available() -{ - if (!isListening()) - return 0; +int SoftwareSerial::available() { + if (!isListening()) return 0; return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF; } -size_t SoftwareSerial::write(uint8_t b) -{ +size_t SoftwareSerial::write(uint8_t b) { // By declaring these as local variables, the compiler will put them // in registers _before_ disabling interrupts and entering the // critical timing sections below, which makes it a lot easier to @@ -337,36 +298,25 @@ size_t SoftwareSerial::write(uint8_t b) bool inv = _inverse_logic; uint16_t delay = _tx_delay; - if(inv) - b = ~b; + if (inv) b = ~b; cli(); // turn off interrupts for a clean txmit // Write the start bit - if (inv) - digitalWrite(_transmitPin, 1); - else - digitalWrite(_transmitPin, 0); + digitalWrite(_transmitPin, !!inv); tunedDelay(delay); // Write each of the 8 bits - for (uint8_t i = 8; i > 0; --i) - { - if (b & 1) // choose bit - digitalWrite(_transmitPin, 1); // send 1 //(GPIO_Desc[_transmitPin].P)->DOUT |= GPIO_Desc[_transmitPin].bit; - else - digitalWrite(_transmitPin, 0); // send 0 //(GPIO_Desc[_transmitPin].P)->DOUT &= ~GPIO_Desc[_transmitPin].bit; - + for (uint8_t i = 8; i > 0; --i) { + digitalWrite(_transmitPin, b & 1); // send 1 //(GPIO_Desc[_transmitPin].P)->DOUT |= GPIO_Desc[_transmitPin].bit; + // send 0 //(GPIO_Desc[_transmitPin].P)->DOUT &= ~GPIO_Desc[_transmitPin].bit; tunedDelay(delay); b >>= 1; } // restore pin to natural state - if (inv) - digitalWrite(_transmitPin, 0); - else - digitalWrite(_transmitPin, 1); + digitalWrite(_transmitPin, !inv); sei(); // turn interrupts back on tunedDelay(delay); @@ -374,18 +324,15 @@ size_t SoftwareSerial::write(uint8_t b) return 1; } -void SoftwareSerial::flush() -{ - if (!isListening()) - return; +void SoftwareSerial::flush() { + if (!isListening()) return; cli(); _receive_buffer_head = _receive_buffer_tail = 0; sei(); } -int SoftwareSerial::peek() -{ +int SoftwareSerial::peek() { if (!isListening()) return -1; diff --git a/Marlin/src/HAL/HAL_LPC1768/arduino.cpp b/Marlin/src/HAL/HAL_LPC1768/arduino.cpp index 7a027f6d25..d18c77247c 100644 --- a/Marlin/src/HAL/HAL_LPC1768/arduino.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/arduino.cpp @@ -165,9 +165,9 @@ void eeprom_write_byte(unsigned char *pos, unsigned char value) { unsigned char eeprom_read_byte(uint8_t * pos) { return '\0'; } -void eeprom_read_block (void *__dst, const void *__src, size_t __n) { } +void eeprom_read_block(void *__dst, const void *__src, size_t __n) { } -void eeprom_update_block (const void *__src, void *__dst, size_t __n) { } +void eeprom_update_block(const void *__src, void *__dst, size_t __n) { } char *dtostrf (double __val, signed char __width, unsigned char __prec, char *__s) { char format_string[20]; diff --git a/Marlin/src/HAL/HAL_STM32F7/EmulatedEeprom.cpp b/Marlin/src/HAL/HAL_STM32F7/EmulatedEeprom.cpp index 71140d18e6..c3cf6ee7ca 100644 --- a/Marlin/src/HAL/HAL_STM32F7/EmulatedEeprom.cpp +++ b/Marlin/src/HAL/HAL_STM32F7/EmulatedEeprom.cpp @@ -80,23 +80,18 @@ static bool eeprom_initialised = false; void eeprom_init() { - if(!eeprom_initialised) { + if (!eeprom_initialised) { HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); /* EEPROM Init */ - if(EE_Initialise() != EE_OK) - { - while(1) { - HAL_Delay(1); - } - } + if (EE_Initialise() != EE_OK) + for (;;) HAL_Delay(1); // Spin forever until watchdog reset HAL_FLASH_Lock(); eeprom_initialised = true; } - } void eeprom_write_byte(unsigned char *pos, unsigned char value) { @@ -106,39 +101,38 @@ void eeprom_write_byte(unsigned char *pos, unsigned char value) { HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); - if(EE_WriteVariable(eeprom_address, (uint16_t) value) != EE_OK) { - while(1) { - HAL_Delay(1); - } - } + + if (EE_WriteVariable(eeprom_address, (uint16_t) value) != EE_OK) + for (;;) HAL_Delay(1); // Spin forever until watchdog reset + HAL_FLASH_Lock(); } unsigned char eeprom_read_byte(unsigned char *pos) { uint16_t data = 0xFF; - uint16_t eeprom_address = (unsigned) pos; + uint16_t eeprom_address = (unsigned)pos; eeprom_init(); - if(EE_ReadVariable(eeprom_address, &data) != EE_OK) { - return (char) data; + if (EE_ReadVariable(eeprom_address, &data) != EE_OK) { + return (unsigned char)data; } - return (char)data; + return (unsigned char)data; } -void eeprom_read_block (void *__dst, const void *__src, size_t __n) { +void eeprom_read_block(void *__dst, const void *__src, size_t __n) { uint16_t data = 0xFF; uint16_t eeprom_address = (unsigned) __src; eeprom_init(); - for(uint8_t c = 0; c < __n; c++) { + for (uint8_t c = 0; c < __n; c++) { EE_ReadVariable(eeprom_address+c, &data); *((uint8_t*)__dst + c) = data; } } -void eeprom_update_block (const void *__src, void *__dst, size_t __n) { +void eeprom_update_block(const void *__src, void *__dst, size_t __n) { } diff --git a/Marlin/src/HAL/I2cEeprom.cpp b/Marlin/src/HAL/I2cEeprom.cpp index 15518fc43f..27fa0062d8 100644 --- a/Marlin/src/HAL/I2cEeprom.cpp +++ b/Marlin/src/HAL/I2cEeprom.cpp @@ -72,10 +72,10 @@ // Public functions // -------------------------------------------------------------------------- -static bool eeprom_initialised = false; static uint8_t eeprom_device_address = 0x50; static void eeprom_init(void) { + static bool eeprom_initialised = false; if (!eeprom_initialised) { Wire.begin(); eeprom_initialised = true; @@ -100,27 +100,25 @@ void eeprom_write_byte(unsigned char *pos, unsigned char value) { // WARNING: address is a page address, 6-bit end will wrap around // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes -void eeprom_update_block(const void* pos, void* eeprom_address, size_t n) { - uint8_t eeprom_temp[32] = {0}; - uint8_t flag = 0; - +void eeprom_update_block(const void *pos, void* eeprom_address, size_t n) { eeprom_init(); Wire.beginTransmission(eeprom_device_address); Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB Wire.endTransmission(); + + uint8_t *ptr = (uint8_t*)pos; + uint8_t flag = 0; Wire.requestFrom(eeprom_device_address, (byte)n); - for (byte c = 0; c < n; c++) { - if (Wire.available()) eeprom_temp[c] = Wire.read(); - flag |= (eeprom_temp[c] ^ *((uint8_t*)pos + c)); - } + for (byte c = 0; c < n && Wire.available(); c++) + flag |= Wire.read() ^ ptr[c]; if (flag) { Wire.beginTransmission(eeprom_device_address); Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB - Wire.write((uint8_t*)(pos), n); + Wire.write((uint8_t*)pos, n); Wire.endTransmission(); // wait for write cycle to complete @@ -132,18 +130,16 @@ void eeprom_update_block(const void* pos, void* eeprom_address, size_t n) { unsigned char eeprom_read_byte(unsigned char *pos) { byte data = 0xFF; - unsigned eeprom_address = (unsigned) pos; + unsigned eeprom_address = (unsigned)pos; - eeprom_init (); + eeprom_init(); Wire.beginTransmission(eeprom_device_address); Wire.write((int)(eeprom_address >> 8)); // MSB Wire.write((int)(eeprom_address & 0xFF)); // LSB Wire.endTransmission(); Wire.requestFrom(eeprom_device_address, (byte)1); - if (Wire.available()) - data = Wire.read(); - return data; + return Wire.available() ? Wire.read() : 0xFF; } // maybe let's not read more than 30 or 32 bytes at a time! diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp index 5b83f49ce1..bf458a88c2 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp @@ -541,7 +541,7 @@ // increment to first segment destination LOOP_XYZE(i) raw[i] += diff[i]; - for(;;) { // for each mesh cell encountered during the move + for (;;) { // for each mesh cell encountered during the move // Compute mesh cell invariants that remain constant for all segments within cell. // Note for cell index, if point is outside the mesh grid (in MESH_INSET perimeter) @@ -591,7 +591,7 @@ const float z_sxy0 = z_xmy0 * diff[X_AXIS], // per-segment adjustment to z_cxy0 z_sxym = (z_xmy1 - z_xmy0) * (1.0 / (MESH_Y_DIST)) * diff[X_AXIS]; // per-segment adjustment to z_cxym - for(;;) { // for all segments within this mesh cell + for (;;) { // for all segments within this mesh cell if (--segments == 0) // if this is last segment, use rtarget for exact COPY(raw, rtarget);