From 9033d86877fa1e8c312240d58e97c4f6764f285f Mon Sep 17 00:00:00 2001 From: Nico Tonnhofer Date: Sun, 26 Feb 2017 20:23:47 +0100 Subject: [PATCH] fast integer: uint_fast8_t and uint_fast16_t for some vars. This will decrease the flash size and should increase performance. In some cases this will increase the used ram slightly. In total this path costs 28 bytes RAM and saves 88 bytes of Flash on a STM32. AVRs are not affected by this commit. --- analog-stm32.c | 6 +++--- clock.c | 12 ++++++------ dda_queue.c | 8 ++++---- dda_queue.h | 4 ++-- temp.c | 8 ++++---- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/analog-stm32.c b/analog-stm32.c index 959e204..897d64c 100644 --- a/analog-stm32.c +++ b/analog-stm32.c @@ -78,7 +78,7 @@ void init_analog() { // subt line is to keep compiler happy #define DEFINE_TEMP_SENSOR(name, type, pin, additional) \ if (NUM_TEMP_SENSORS) { \ - uint8_t subt = (pin ## _ADC >= 10) ? 10 : 0; \ + uint32_t subt = (pin ## _ADC >= 10) ? 10 : 0; \ if (pin ## _ADC >= 10) { \ ADC1->SMPR1 |= (uint32_t)0x06 << (3 * ((pin ## _ADC) - subt)); \ } else { \ @@ -186,8 +186,8 @@ uint16_t analog_read(uint8_t index) { if (NUM_TEMP_SENSORS > 0) { uint16_t r = 0; uint16_t temp; - uint16_t max_temp = 0; - uint16_t min_temp = 0xffff; + uint32_t max_temp = 0; + uint32_t min_temp = UINT32_MAX; for (uint8_t i = 0; i < OVERSAMPLE; i++) { temp = adc_buffer[i][index]; diff --git a/clock.c b/clock.c index 01a8ab1..0af23fe 100644 --- a/clock.c +++ b/clock.c @@ -30,16 +30,16 @@ Every time our clock fires we increment this, so we know when 10ms/250ms/1s has elapsed. */ -static uint8_t clock_counter_10ms = 0; -static uint8_t clock_counter_250ms = 0; -static uint8_t clock_counter_1s = 0; +static uint_fast8_t clock_counter_10ms = 0; +static uint_fast8_t clock_counter_250ms = 0; +static uint_fast8_t clock_counter_1s = 0; /** Flags to tell clock() when above have elapsed. */ -static volatile uint8_t clock_flag_10ms = 0; -static volatile uint8_t clock_flag_250ms = 0; -static volatile uint8_t clock_flag_1s = 0; +static volatile uint_fast8_t clock_flag_10ms = 0; +static volatile uint_fast8_t clock_flag_250ms = 0; +static volatile uint_fast8_t clock_flag_1s = 0; /** Advance our clock by a tick. diff --git a/dda_queue.c b/dda_queue.c index 0ddd35c..d77efb4 100644 --- a/dda_queue.c +++ b/dda_queue.c @@ -21,11 +21,11 @@ is used both in and out of interrupts, but is only written outside of interrupts. */ -static uint8_t mb_head = 0; +static uint_fast8_t mb_head = 0; /// movebuffer tail pointer. Points to the currently executing move /// this variable is read/written both in and out of interrupts. -uint8_t mb_tail = 0; +uint_fast8_t mb_tail = 0; /// move buffer. /// holds move queue @@ -46,7 +46,7 @@ DDA *mb_tail_dda; #define MB_NEXT(x) ((x) < MOVEBUFFER_SIZE - 1 ? (x) + 1 : 0) /// check if the queue is completely full -uint8_t queue_full() { +uint_fast8_t queue_full() { MEMORY_BARRIER(); return MB_NEXT(mb_head) == mb_tail; } @@ -87,7 +87,7 @@ void enqueue_home(TARGET *t, uint8_t endstop_check, uint8_t endstop_stop_cond) { while (queue_full()) delay_us(100); - uint8_t h = MB_NEXT(mb_head); + uint_fast8_t h = MB_NEXT(mb_head); DDA* new_movebuffer = &(movebuffer[h]); diff --git a/dda_queue.h b/dda_queue.h index 888124b..914f0ce 100644 --- a/dda_queue.h +++ b/dda_queue.h @@ -11,7 +11,7 @@ */ // this is the ringbuffer that holds the current and pending moves. -extern uint8_t mb_tail; +extern uint_fast8_t mb_tail; extern DDA movebuffer[MOVEBUFFER_SIZE]; extern DDA *mb_tail_dda; @@ -21,7 +21,7 @@ extern DDA *mb_tail_dda; */ // queue status methods -uint8_t queue_full(void); +uint_fast8_t queue_full(void); // take one step void queue_step(void); diff --git a/temp.c b/temp.c index 1784c91..37ad925 100644 --- a/temp.c +++ b/temp.c @@ -75,12 +75,12 @@ static const temp_sensor_definition_t temp_sensors[NUM_TEMP_SENSORS] = /// this struct holds the runtime sensor data- read temperatures, targets, etc static struct { - uint16_t last_read_temp; ///< last received reading - uint16_t target_temp; ///< manipulate attached heater to attempt to achieve this value + uint_fast16_t last_read_temp; ///< last received reading + uint_fast16_t target_temp; ///< manipulate attached heater to attempt to achieve this value - uint16_t temp_residency; ///< how long have we been close to target temperature in temp ticks? + uint_fast16_t temp_residency; ///< how long have we been close to target temperature in temp ticks? - uint8_t active; ///< State machine tracker for readers that need it. + uint_fast8_t active; ///< State machine tracker for readers that need it. } temp_sensors_runtime[NUM_TEMP_SENSORS]; /** \def TEMP_EWMA