From 9d98a62699c92c05b8c142477951f27af2d71a43 Mon Sep 17 00:00:00 2001 From: etagle Date: Fri, 11 May 2018 02:27:36 -0300 Subject: [PATCH] Followup to HAL optimizations and delays - Cleanups, fixes for Due HAL code. - TC_IER is write-only. Use TC_IMR to test ISR state. --- Marlin/src/HAL/Delay.h | 2 ++ Marlin/src/HAL/HAL_DUE/HAL_spi_Due.cpp | 8 ++--- Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp | 14 +-------- .../HAL_DUE/u8g_com_HAL_DUE_st7920_sw_spi.cpp | 30 +++---------------- Marlin/src/HAL/HAL_LPC1768/include/Wire.cpp | 22 +++++++------- .../u8g_com_HAL_LPC1768_st7920_hw_spi.cpp | 4 +-- .../u8g_com_HAL_LPC1768_st7920_sw_spi.cpp | 4 +-- .../dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp | 2 +- 8 files changed, 27 insertions(+), 59 deletions(-) diff --git a/Marlin/src/HAL/Delay.h b/Marlin/src/HAL/Delay.h index 75619b23ef..e1e8fab7c8 100644 --- a/Marlin/src/HAL/Delay.h +++ b/Marlin/src/HAL/Delay.h @@ -33,6 +33,8 @@ #ifndef MARLIN_DELAY_H #define MARLIN_DELAY_H +#include "../core/macros.h" + #if defined(__arm__) || defined(__thumb__) /* https://blueprints.launchpad.net/gcc-arm-embedded/+spec/delay-cycles */ diff --git a/Marlin/src/HAL/HAL_DUE/HAL_spi_Due.cpp b/Marlin/src/HAL/HAL_DUE/HAL_spi_Due.cpp index 52ac1b9877..84c0f8f4bf 100644 --- a/Marlin/src/HAL/HAL_DUE/HAL_spi_Due.cpp +++ b/Marlin/src/HAL/HAL_DUE/HAL_spi_Due.cpp @@ -89,7 +89,7 @@ /* The software SPI routine */ __asm__ __volatile__( - ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax + A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax /* Bit 7 */ A("ubfx %[idx],%[txval],#7,#1") /* Place bit 7 in bit 0 of idx*/ @@ -170,7 +170,7 @@ /* The software SPI routine */ __asm__ __volatile__( - ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax + A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax /* bit 7 */ A("str %[sck_mask],[%[sck_port]]") /* SODR */ @@ -290,7 +290,7 @@ /* The software SPI routine */ __asm__ __volatile__( - ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax + A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax L("loop%=") A("ldrb.w %[txval], [%[ptr]], #1") /* Load value to send, increment buffer */ @@ -368,7 +368,7 @@ /* The software SPI routine */ __asm__ __volatile__( - ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax + A(".syntax unified") // is to prevent CM0,CM1 non-unified syntax L("loop%=") diff --git a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp index b522fbeb65..9f56bbeb58 100644 --- a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp +++ b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp @@ -137,19 +137,7 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num) { bool HAL_timer_interrupt_enabled(const uint8_t timer_num) { const tTimerConfig * const pConfig = &TimerConfig[timer_num]; - return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_IER == TC_IER_CPCS; + return (pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_IMR & TC_IMR_CPCS) != 0; } -#if 0 - void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare) { - const tTimerConfig * const pConfig = &TimerConfig[timer_num]; - TC_SetRC(pConfig->pTimerRegs, pConfig->channel, compare); - } - - void HAL_timer_isr_prologue(const uint8_t timer_num) { - const tTimerConfig * const pConfig = &TimerConfig[timer_num]; - TC_GetStatus(pConfig->pTimerRegs, pConfig->channel); - } -#endif - #endif // ARDUINO_ARCH_SAM diff --git a/Marlin/src/HAL/HAL_DUE/u8g_com_HAL_DUE_st7920_sw_spi.cpp b/Marlin/src/HAL/HAL_DUE/u8g_com_HAL_DUE_st7920_sw_spi.cpp index e8335788d5..7f2d1caf9d 100644 --- a/Marlin/src/HAL/HAL_DUE/u8g_com_HAL_DUE_st7920_sw_spi.cpp +++ b/Marlin/src/HAL/HAL_DUE/u8g_com_HAL_DUE_st7920_sw_spi.cpp @@ -58,6 +58,7 @@ #include #include #include "../../core/macros.h" +#include "../Delay.h" void u8g_SetPIOutput_DUE(u8g_t *u8g, uint8_t pin_index) { PIO_Configure(g_APinDescription[u8g->pin_list[pin_index]].pPort, PIO_OUTPUT_1, @@ -71,28 +72,6 @@ void u8g_SetPILevel_DUE(u8g_t *u8g, uint8_t pin_index, uint8_t level) { else port->PIO_CODR = mask; } -void __delay_4cycles(uint32_t cy) __attribute__ ((weak)); - -FORCE_INLINE void __delay_4cycles(uint32_t cy) { // +1 cycle - #if ARCH_PIPELINE_RELOAD_CYCLES<2 - #define EXTRA_NOP_CYCLES "nop" - #else - #define EXTRA_NOP_CYCLES "" - #endif - - __asm__ __volatile__( - ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax - - L("loop%=") - A("subs %[cnt],#1") - A(EXTRA_NOP_CYCLES) - A("bne loop%=") - : [cnt]"+r"(cy) // output: +r means input+output - : // input: - : "cc" // clobbers: - ); -} - Pio *SCK_pPio, *MOSI_pPio; uint32_t SCK_dwMask, MOSI_dwMask; @@ -102,9 +81,9 @@ static void spiSend_sw_DUE(uint8_t val) { // 800KHz MOSI_pPio->PIO_SODR = MOSI_dwMask; else MOSI_pPio->PIO_CODR = MOSI_dwMask; - __delay_4cycles(1); + DELAY_NS(48); SCK_pPio->PIO_SODR = SCK_dwMask; - __delay_4cycles(19); // 16 dead, 17 garbage, 18/0 900kHz, 19/1 825k, 20/1 800k, 21/2 725KHz + DELAY_NS(905); // 762 dead, 810 garbage, 858/0 900kHz, 905/1 825k, 953/1 800k, 1000/2 725KHz val <<= 1; SCK_pPio->PIO_CODR = SCK_dwMask; } @@ -125,8 +104,7 @@ static void u8g_com_DUE_st7920_write_byte_sw_spi(uint8_t rs, uint8_t val) { /* data */ spiSend_sw_DUE(0x0FA); - for (i = 0; i < 4; i++) // give the controller some time to process the data - u8g_10MicroDelay(); // 2 is bad, 3 is OK, 4 is safe + DELAY_US(40); // give the controller some time to process the data: 20 is bad, 30 is OK, 40 is safe } spiSend_sw_DUE(val & 0x0F0); diff --git a/Marlin/src/HAL/HAL_LPC1768/include/Wire.cpp b/Marlin/src/HAL/HAL_LPC1768/include/Wire.cpp index fa8149f535..28e1fa9b56 100644 --- a/Marlin/src/HAL/HAL_LPC1768/include/Wire.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/include/Wire.cpp @@ -118,12 +118,12 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) { // perform blocking read into buffer I2C_M_SETUP_Type transferMCfg; transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift - transferMCfg.tx_data = NULL; - transferMCfg.tx_length = 0; - transferMCfg.rx_data = rxBuffer; - transferMCfg.rx_length = quantity; - transferMCfg.retransmissions_max = 3; - I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING); + transferMCfg.tx_data = NULL; + transferMCfg.tx_length = 0; + transferMCfg.rx_data = rxBuffer; + transferMCfg.rx_length = quantity; + transferMCfg.retransmissions_max = 3; + I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING); // set rx buffer iterator vars rxBufferIndex = 0; @@ -154,11 +154,11 @@ uint8_t TwoWire::endTransmission(void) { // transmit buffer (blocking) I2C_M_SETUP_Type transferMCfg; transferMCfg.sl_addr7bit = txAddress >> 1; // not sure about the right shift - transferMCfg.tx_data = txBuffer; - transferMCfg.tx_length = txBufferLength; - transferMCfg.rx_data = NULL; - transferMCfg.rx_length = 0; - transferMCfg.retransmissions_max = 3; + transferMCfg.tx_data = txBuffer; + transferMCfg.tx_length = txBufferLength; + transferMCfg.rx_data = NULL; + transferMCfg.rx_length = 0; + transferMCfg.retransmissions_max = 3; Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING); // reset tx buffer iterator vars diff --git a/Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp b/Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp index 598715c1f0..a93ca221c6 100644 --- a/Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp @@ -60,6 +60,7 @@ //#include "Configuration.h" #include + #include "../Delay.h" #define SPI_FULL_SPEED 0 #define SPI_HALF_SPEED 1 @@ -92,8 +93,7 @@ /* data */ spiSend(0x0FA); - for( i = 0; i < 4; i++ ) // give the controller some time to process the data - u8g_10MicroDelay(); // 2 is bad, 3 is OK, 4 is safe + DELAY_US(40); // give the controller some time to process the data: 20 is bad, 30 is OK, 40 is safe } spiSend(val & 0x0F0); diff --git a/Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_st7920_sw_spi.cpp b/Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_st7920_sw_spi.cpp index 2b2d9880da..4112a1173f 100644 --- a/Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_st7920_sw_spi.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/u8g_com_HAL_LPC1768_st7920_sw_spi.cpp @@ -57,6 +57,7 @@ #include #include "SoftwareSPI.h" + #include "../Delay.h" #define SPI_SPEED 3 // About 1 MHz @@ -78,8 +79,7 @@ /* data */ swSpiTransfer(0x0FA, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL); - for( i = 0; i < 4; i++ ) // give the controller some time to process the data - u8g_10MicroDelay(); // 2 is bad, 3 is OK, 4 is safe + DELAY_US(40); // give the controller some time to process the data: 20 is bad, 30 is OK, 40 is safe } swSpiTransfer(val & 0x0F0, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL); diff --git a/Marlin/src/lcd/dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp b/Marlin/src/lcd/dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp index cae764e68c..f5926b020e 100644 --- a/Marlin/src/lcd/dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp +++ b/Marlin/src/lcd/dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp @@ -105,7 +105,7 @@ static void ST7920_SWSPI_SND_8BIT(uint8_t val) { #if DOGM_SPI_DELAY_US > 0 #define U8G_DELAY() DELAY_US(DOGM_SPI_DELAY_US) #else - #define U8G_DELAY() u8g_10MicroDelay() + #define U8G_DELAY() DELAY_US(10) #endif #define ST7920_CS() { WRITE(ST7920_CS_PIN,1); U8G_DELAY(); }