Skip to content

Commit

Permalink
Apply bit macros where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Mar 10, 2016
1 parent 9f7ee7b commit 1c152bf
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 154 deletions.
19 changes: 6 additions & 13 deletions Marlin.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ typedef unsigned long millis_t;

#include "MarlinSerial.h"

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

#include "WString.h"

#ifdef USBCON
Expand Down Expand Up @@ -217,12 +210,12 @@ void Stop();
* Debug flags - not yet widely applied
*/
enum DebugFlags {
DEBUG_ECHO = BIT(0),
DEBUG_INFO = BIT(1),
DEBUG_ERRORS = BIT(2),
DEBUG_DRYRUN = BIT(3),
DEBUG_COMMUNICATION = BIT(4),
DEBUG_LEVELING = BIT(5)
DEBUG_ECHO = _BV(0),
DEBUG_INFO = _BV(1),
DEBUG_ERRORS = _BV(2),
DEBUG_DRYRUN = _BV(3),
DEBUG_COMMUNICATION = _BV(4),
DEBUG_LEVELING = _BV(5)
};
extern uint8_t marlin_debug_flags;

Expand Down
14 changes: 7 additions & 7 deletions MarlinSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void MarlinSerial::begin(long baud) {
#endif

if (useU2X) {
M_UCSRxA = BIT(M_U2Xx);
M_UCSRxA = _BV(M_U2Xx);
baud_setting = (F_CPU / 4 / baud - 1) / 2;
}
else {
Expand All @@ -88,15 +88,15 @@ void MarlinSerial::begin(long baud) {
M_UBRRxH = baud_setting >> 8;
M_UBRRxL = baud_setting;

sbi(M_UCSRxB, M_RXENx);
sbi(M_UCSRxB, M_TXENx);
sbi(M_UCSRxB, M_RXCIEx);
SBI(M_UCSRxB, M_RXENx);
SBI(M_UCSRxB, M_TXENx);
SBI(M_UCSRxB, M_RXCIEx);
}

void MarlinSerial::end() {
cbi(M_UCSRxB, M_RXENx);
cbi(M_UCSRxB, M_TXENx);
cbi(M_UCSRxB, M_RXCIEx);
CBI(M_UCSRxB, M_RXENx);
CBI(M_UCSRxB, M_TXENx);
CBI(M_UCSRxB, M_RXCIEx);
}


Expand Down
32 changes: 16 additions & 16 deletions Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,11 @@ void setup() {

// Check startup - does nothing if bootloader sets MCUSR to 0
byte mcu = MCUSR;
if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
if (TEST(mcu, 0)) SERIAL_ECHOLNPGM(MSG_POWERUP);
if (TEST(mcu, 1)) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
if (TEST(mcu, 2)) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
if (TEST(mcu, 3)) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
if (TEST(mcu, 5)) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
MCUSR = 0;

SERIAL_ECHOPGM(MSG_MARLIN);
Expand Down Expand Up @@ -1601,8 +1601,8 @@ static void setup_for_endstop_move() {

enum ProbeAction {
ProbeStay = 0,
ProbeDeploy = BIT(0),
ProbeStow = BIT(1),
ProbeDeploy = _BV(0),
ProbeStow = _BV(1),
ProbeDeployAndStow = (ProbeDeploy | ProbeStow)
};

Expand Down Expand Up @@ -3680,7 +3680,7 @@ inline void gcode_M42() {
millis_t ms = millis();
double radius = ms % (X_MAX_LENGTH / 4), // limit how far out to go
theta = RADIANS(ms % 360L);
float dir = (ms & 0x0001) ? 1 : -1; // clockwise or counter clockwise
float dir = TEST(ms, 0) ? 1 : -1; // clockwise or counter clockwise

//SERIAL_ECHOPAIR("starting radius: ",radius);
//SERIAL_ECHOPAIR(" theta: ",theta);
Expand Down Expand Up @@ -6408,33 +6408,33 @@ void mesh_plan_buffer_line(float x, float y, float z, const float e, float feed_
return;
}
float nx, ny, ne, normalized_dist;
if (ix > pix && (x_splits) & BIT(ix)) {
if (ix > pix && TEST(x_splits, ix)) {
nx = mbl.get_x(ix);
normalized_dist = (nx - current_position[X_AXIS]) / (x - current_position[X_AXIS]);
ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
x_splits ^= BIT(ix);
CBI(x_splits, ix);
}
else if (ix < pix && (x_splits) & BIT(pix)) {
else if (ix < pix && TEST(x_splits, pix)) {
nx = mbl.get_x(pix);
normalized_dist = (nx - current_position[X_AXIS]) / (x - current_position[X_AXIS]);
ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
x_splits ^= BIT(pix);
CBI(x_splits, pix);
}
else if (iy > piy && (y_splits) & BIT(iy)) {
else if (iy > piy && TEST(y_splits, iy)) {
ny = mbl.get_y(iy);
normalized_dist = (ny - current_position[Y_AXIS]) / (y - current_position[Y_AXIS]);
nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
y_splits ^= BIT(iy);
CBI(y_splits, iy);
}
else if (iy < piy && (y_splits) & BIT(piy)) {
else if (iy < piy && TEST(y_splits, piy)) {
ny = mbl.get_y(piy);
normalized_dist = (ny - current_position[Y_AXIS]) / (y - current_position[Y_AXIS]);
nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
y_splits ^= BIT(piy);
CBI(y_splits, piy);
}
else {
// Already split on a border
Expand Down
4 changes: 2 additions & 2 deletions Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
*/
static void spiInit(uint8_t spiRate) {
// See avr processor documentation
SPCR = BIT(SPE) | BIT(MSTR) | (spiRate >> 1);
SPSR = spiRate & 1 || spiRate == 6 ? 0 : BIT(SPI2X);
SPCR = _BV(SPE) | _BV(MSTR) | (spiRate >> 1);
SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X);
}
//------------------------------------------------------------------------------
/** SPI receive a byte */
Expand Down
8 changes: 4 additions & 4 deletions Sd2PinMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@ static inline __attribute__((always_inline))
void setPinMode(uint8_t pin, uint8_t mode) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
if (mode) {
*digitalPinMap[pin].ddr |= BIT(digitalPinMap[pin].bit);
SBI(*digitalPinMap[pin].ddr, digitalPinMap[pin].bit);
}
else {
*digitalPinMap[pin].ddr &= ~BIT(digitalPinMap[pin].bit);
CBI(*digitalPinMap[pin].ddr, digitalPinMap[pin].bit);
}
}
else {
Expand All @@ -428,10 +428,10 @@ static inline __attribute__((always_inline))
void fastDigitalWrite(uint8_t pin, uint8_t value) {
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
if (value) {
*digitalPinMap[pin].port |= BIT(digitalPinMap[pin].bit);
SBI(*digitalPinMap[pin].port, digitalPinMap[pin].bit);
}
else {
*digitalPinMap[pin].port &= ~BIT(digitalPinMap[pin].bit);
CBI(*digitalPinMap[pin].port, digitalPinMap[pin].bit);
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion SdVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ bool SdVolume::init(Sd2Card* dev, uint8_t part) {
blocksPerCluster_ = fbs->sectorsPerCluster;
// determine shift that is same as multiply by blocksPerCluster_
clusterSizeShift_ = 0;
while (blocksPerCluster_ != BIT(clusterSizeShift_)) {
while (blocksPerCluster_ != _BV(clusterSizeShift_)) {
// error if not power of 2
if (clusterSizeShift_++ > 7) goto fail;
}
Expand Down
6 changes: 3 additions & 3 deletions dogm_lcd_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#define BLEN_A 0
#define BLEN_B 1
#define BLEN_C 2
#define EN_A BIT(BLEN_A)
#define EN_B BIT(BLEN_B)
#define EN_C BIT(BLEN_C)
#define EN_A (_BV(BLEN_A))
#define EN_B (_BV(BLEN_B))
#define EN_C (_BV(BLEN_C))
#define LCD_CLICKED (buttons&EN_C)
#endif

Expand Down
7 changes: 4 additions & 3 deletions macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#define MACROS_H

// Macros for bit masks
#define BIT(b) (1<<(b))
#define TEST(n,b) (((n)&BIT(b))!=0)
#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b))
#define TEST(n,b) (((n)&_BV(b))!=0)
#define SBI(n,b) (n |= _BV(b))
#define CBI(n,b) (n &= ~_BV(b))
#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (_BV(b))

// Macros for maths shortcuts
#define RADIANS(d) ((d)*M_PI/180.0)
Expand Down
32 changes: 16 additions & 16 deletions planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,23 +580,23 @@ float junction_deviation = 0.1;
// Compute direction bits for this block
uint8_t db = 0;
#if ENABLED(COREXY)
if (dx < 0) db |= BIT(X_HEAD); // Save the real Extruder (head) direction in X Axis
if (dy < 0) db |= BIT(Y_HEAD); // ...and Y
if (dz < 0) db |= BIT(Z_AXIS);
if (dx + dy < 0) db |= BIT(A_AXIS); // Motor A direction
if (dx - dy < 0) db |= BIT(B_AXIS); // Motor B direction
if (dx < 0) SBI(db, X_HEAD); // Save the real Extruder (head) direction in X Axis
if (dy < 0) SBI(db, Y_HEAD); // ...and Y
if (dz < 0) SBI(db, Z_AXIS);
if (dx + dy < 0) SBI(db, A_AXIS); // Motor A direction
if (dx - dy < 0) SBI(db, B_AXIS); // Motor B direction
#elif ENABLED(COREXZ)
if (dx < 0) db |= BIT(X_HEAD); // Save the real Extruder (head) direction in X Axis
if (dy < 0) db |= BIT(Y_AXIS);
if (dz < 0) db |= BIT(Z_HEAD); // ...and Z
if (dx + dz < 0) db |= BIT(A_AXIS); // Motor A direction
if (dx - dz < 0) db |= BIT(C_AXIS); // Motor B direction
if (dx < 0) SBI(db, X_HEAD); // Save the real Extruder (head) direction in X Axis
if (dy < 0) SBI(db, Y_AXIS);
if (dz < 0) SBI(db, Z_HEAD); // ...and Z
if (dx + dz < 0) SBI(db, A_AXIS); // Motor A direction
if (dx - dz < 0) SBI(db, C_AXIS); // Motor B direction
#else
if (dx < 0) db |= BIT(X_AXIS);
if (dy < 0) db |= BIT(Y_AXIS);
if (dz < 0) db |= BIT(Z_AXIS);
if (dx < 0) SBI(db, X_AXIS);
if (dy < 0) SBI(db, Y_AXIS);
if (dz < 0) SBI(db, Z_AXIS);
#endif
if (de < 0) db |= BIT(E_AXIS);
if (de < 0) SBI(db, E_AXIS);
block->direction_bits = db;

block->active_extruder = extruder;
Expand Down Expand Up @@ -824,14 +824,14 @@ float junction_deviation = 0.1;
ys1 = axis_segment_time[Y_AXIS][1],
ys2 = axis_segment_time[Y_AXIS][2];

if ((direction_change & BIT(X_AXIS)) != 0) {
if (TEST(direction_change, X_AXIS)) {
xs2 = axis_segment_time[X_AXIS][2] = xs1;
xs1 = axis_segment_time[X_AXIS][1] = xs0;
xs0 = 0;
}
xs0 = axis_segment_time[X_AXIS][0] = xs0 + segment_time;

if ((direction_change & BIT(Y_AXIS)) != 0) {
if (TEST(direction_change, Y_AXIS)) {
ys2 = axis_segment_time[Y_AXIS][2] = axis_segment_time[Y_AXIS][1];
ys1 = axis_segment_time[Y_AXIS][1] = axis_segment_time[Y_AXIS][0];
ys0 = 0;
Expand Down
38 changes: 20 additions & 18 deletions servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ static void initISR(timer16_Sequence_t timer) {
TCCR1B = _BV(CS11); // set prescaler of 8
TCNT1 = 0; // clear the timer count
#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__)
TIFR |= _BV(OCF1A); // clear any pending interrupts;
TIMSK |= _BV(OCIE1A); // enable the output compare interrupt
SBI(TIFR, OCF1A); // clear any pending interrupts;
SBI(TIMSK, OCIE1A); // enable the output compare interrupt
#else
// here if not ATmega8 or ATmega128
TIFR1 |= _BV(OCF1A); // clear any pending interrupts;
TIMSK1 |= _BV(OCIE1A); // enable the output compare interrupt
SBI(TIFR1, OCF1A); // clear any pending interrupts;
SBI(TIMSK1, OCIE1A); // enable the output compare interrupt
#endif
#ifdef WIRING
timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service);
Expand All @@ -158,8 +158,8 @@ static void initISR(timer16_Sequence_t timer) {
TCCR3B = _BV(CS31); // set prescaler of 8
TCNT3 = 0; // clear the timer count
#ifdef __AVR_ATmega128__
TIFR |= _BV(OCF3A); // clear any pending interrupts;
ETIMSK |= _BV(OCIE3A); // enable the output compare interrupt
SBI(TIFR, OCF3A); // clear any pending interrupts;
SBI(ETIMSK, OCIE3A); // enable the output compare interrupt
#else
TIFR3 = _BV(OCF3A); // clear any pending interrupts;
TIMSK3 = _BV(OCIE3A) ; // enable the output compare interrupt
Expand Down Expand Up @@ -195,21 +195,23 @@ static void finISR(timer16_Sequence_t timer) {
// Disable use of the given timer
#ifdef WIRING
if (timer == _timer1) {
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
TIMSK1
#else
TIMSK
#endif
&= ~_BV(OCIE1A); // disable timer 1 output compare interrupt
CBI(
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
TIMSK1
#else
TIMSK
#endif
, OCIE1A); // disable timer 1 output compare interrupt
timerDetach(TIMER1OUTCOMPAREA_INT);
}
else if (timer == _timer3) {
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
TIMSK3
#else
ETIMSK
#endif
&= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt
CBI(
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
TIMSK3
#else
ETIMSK
#endif
, OCIE3A); // disable the timer3 output compare A interrupt
timerDetach(TIMER3OUTCOMPAREA_INT);
}
#else //!WIRING
Expand Down
Loading

0 comments on commit 1c152bf

Please sign in to comment.