@ -39,12 +39,14 @@
* void Max7219_init ( ) ;
* void Max7219_init ( ) ;
* void Max7219_PutByte ( uint8_t data ) ;
* void Max7219_PutByte ( uint8_t data ) ;
* void Max7219 ( uint8_t reg , uint8_t data ) ;
* void Max7219 ( uint8_t reg , uint8_t data ) ;
* void Max7219_LED_On ( uint8_t row, uint8_t col ) ;
* void Max7219_LED_On ( uint8_t col, uint8_t row ) ;
* void Max7219_LED_Off ( uint8_t row, uint8_t col ) ;
* void Max7219_LED_Off ( uint8_t col, uint8_t row ) ;
* void Max7219_LED_Toggle ( uint8_t row, uint8_t col ) ;
* void Max7219_LED_Toggle ( uint8_t col, uint8_t row ) ;
* void Max7219_Clear_Row ( uint8_t row ) ;
* void Max7219_Clear_Row ( uint8_t row ) ;
* void Max7219_Clear_Column ( uint8_t col ) ;
* void Max7219_Clear_Column ( uint8_t col ) ;
* void Max7219_Set_Row ( uint8_t row , uint8_t val ) ;
* void Max7219_Set_Row ( uint8_t row , uint8_t val ) ;
* void Max7219_Set_2_Rows ( uint8_t row , uint16_t val ) ;
* void Max7219_Set_4_Rows ( uint8_t row , uint32_t val ) ;
* void Max7219_Set_Column ( uint8_t col , uint8_t val ) ;
* void Max7219_Set_Column ( uint8_t col , uint8_t val ) ;
* void Max7219_idle_tasks ( ) ;
* void Max7219_idle_tasks ( ) ;
*/
*/
@ -53,67 +55,155 @@
# if ENABLED(MAX7219_DEBUG)
# if ENABLED(MAX7219_DEBUG)
# include "Marlin.h"
# include "Max7219_Debug_LEDs.h"
# include "planner.h"
# include "planner.h"
# include "stepper.h"
# include "stepper.h"
# include "Max7219_Debug_LEDs .h"
# include "Marlin .h"
static uint8_t LEDs [ 8 ] = { 0 } ;
static uint8_t LEDs [ 8 ] = { 0 } ;
void Max7219_PutByte ( uint8_t data ) {
void Max7219_PutByte ( uint8_t data ) {
CRITICAL_SECTION_START
for ( uint8_t i = 8 ; i - - ; ) {
for ( uint8_t i = 8 ; i - - ; ) {
# ifdef CPU_32_BIT // The 32-bit processors are so fast, a small delay in the code is needed
delayMicroseconds ( 5 ) ; // to let the signal wires stabilize.
WRITE ( MAX7219_CLK_PIN , LOW ) ; // tick
WRITE ( MAX7219_CLK_PIN , LOW ) ; // tick
delayMicroseconds ( 5 ) ;
WRITE ( MAX7219_DIN_PIN , ( data & 0x80 ) ? HIGH : LOW ) ; // send 1 or 0 based on data bit
WRITE ( MAX7219_DIN_PIN , ( data & 0x80 ) ? HIGH : LOW ) ; // send 1 or 0 based on data bit
delayMicroseconds ( 5 ) ;
WRITE ( MAX7219_CLK_PIN , HIGH ) ; // tock
WRITE ( MAX7219_CLK_PIN , HIGH ) ; // tock
delayMicroseconds ( 5 ) ;
# else
WRITE ( MAX7219_CLK_PIN , LOW ) ; // tick
WRITE ( MAX7219_DIN_PIN , ( data & 0x80 ) ? HIGH : LOW ) ; // send 1 or 0 based on data bit
WRITE ( MAX7219_CLK_PIN , HIGH ) ; // tock
# endif
data < < = 1 ;
data < < = 1 ;
}
}
CRITICAL_SECTION_END
}
}
void Max7219 ( const uint8_t reg , const uint8_t data ) {
void Max7219 ( const uint8_t reg , const uint8_t data ) {
# ifdef CPU_32_BIT
delayMicroseconds ( 5 ) ;
# endif
CRITICAL_SECTION_START
WRITE ( MAX7219_LOAD_PIN , LOW ) ; // begin
WRITE ( MAX7219_LOAD_PIN , LOW ) ; // begin
# ifdef CPU_32_BIT // The 32-bit processors are so fast, a small delay in the code is needed
delayMicroseconds ( 5 ) ; // to let the signal wires stabilize.
# endif
Max7219_PutByte ( reg ) ; // specify register
Max7219_PutByte ( reg ) ; // specify register
# ifdef CPU_32_BIT
delayMicroseconds ( 5 ) ;
# endif
Max7219_PutByte ( data ) ; // put data
Max7219_PutByte ( data ) ; // put data
# ifdef CPU_32_BIT
delayMicroseconds ( 5 ) ;
# endif
WRITE ( MAX7219_LOAD_PIN , LOW ) ; // and tell the chip to load the data
WRITE ( MAX7219_LOAD_PIN , LOW ) ; // and tell the chip to load the data
# ifdef CPU_32_BIT
delayMicroseconds ( 5 ) ;
# endif
WRITE ( MAX7219_LOAD_PIN , HIGH ) ;
WRITE ( MAX7219_LOAD_PIN , HIGH ) ;
CRITICAL_SECTION_END
# ifdef CPU_32_BIT
delayMicroseconds ( 5 ) ;
# endif
}
}
void Max7219_LED_Set ( const uint8_t row , const uint8_t col , const bool on ) {
void Max7219_LED_Set ( const uint8_t row , const uint8_t col , const bool on ) {
if ( row > 7 | | col > 7 ) return ;
if ( row > 7 | | col > 7 ) {
int r , c ;
r = row ;
c = col ;
SERIAL_ECHOPAIR ( " ??? Max7219_LED_Set( " , r ) ;
SERIAL_ECHOPAIR ( " , " , c ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
if ( TEST ( LEDs [ row ] , col ) = = on ) return ; // if LED is already on/off, leave alone
if ( TEST ( LEDs [ row ] , col ) = = on ) return ; // if LED is already on/off, leave alone
if ( on ) SBI ( LEDs [ row ] , col ) ; else CBI ( LEDs [ row ] , col ) ;
if ( on ) SBI ( LEDs [ row ] , col ) ; else CBI ( LEDs [ row ] , col ) ;
Max7219 ( 8 - row , LEDs [ row ] ) ;
Max7219 ( 8 - row , LEDs [ row ] ) ;
}
}
void Max7219_LED_On ( const uint8_t row , const uint8_t col ) {
void Max7219_LED_On ( const uint8_t col , const uint8_t row ) {
Max7219_LED_Set ( row , col , true ) ;
if ( row > 7 | | col > 7 ) {
int r , c ;
r = row ;
c = col ;
SERIAL_ECHOPAIR ( " ??? Max7219_LED_On( " , c ) ;
SERIAL_ECHOPAIR ( " , " , r ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
Max7219_LED_Set ( col , row , true ) ;
}
}
void Max7219_LED_Off ( const uint8_t row , const uint8_t col ) {
void Max7219_LED_Off ( const uint8_t col , const uint8_t row ) {
Max7219_LED_Set ( row , col , false ) ;
if ( row > 7 | | col > 7 ) {
int r , c ;
r = row ;
c = col ;
SERIAL_ECHOPAIR ( " ??? Max7219_LED_Off( " , r ) ;
SERIAL_ECHOPAIR ( " , " , c ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
Max7219_LED_Set ( col , row , false ) ;
}
}
void Max7219_LED_Toggle ( const uint8_t row , const uint8_t col ) {
void Max7219_LED_Toggle ( const uint8_t col , const uint8_t row ) {
if ( row > 7 | | col > 7 ) return ;
if ( row > 7 | | col > 7 ) {
int r , c ;
r = row ;
c = col ;
SERIAL_ECHOPAIR ( " ??? Max7219_LED_Toggle( " , r ) ;
SERIAL_ECHOPAIR ( " , " , c ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
if ( TEST ( LEDs [ row ] , col ) )
if ( TEST ( LEDs [ row ] , col ) )
Max7219_LED_Off ( row , col ) ;
Max7219_LED_Off ( col , row ) ;
else
else
Max7219_LED_On ( row , col ) ;
Max7219_LED_On ( col , row ) ;
}
}
void Max7219_Clear_Column ( const uint8_t col ) {
void Max7219_Clear_Column ( const uint8_t col ) {
if ( col > 7 ) return ;
if ( col > 7 ) {
int c ;
c = col ;
SERIAL_ECHOPAIR ( " ??? Max7219_Clear_Column( " , c ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
LEDs [ col ] = 0 ;
LEDs [ col ] = 0 ;
Max7219 ( 8 - col , LEDs [ col ] ) ;
Max7219 ( 8 - col , LEDs [ col ] ) ;
}
}
void Max7219_Clear_Row ( const uint8_t row ) {
void Max7219_Clear_Row ( const uint8_t row ) {
if ( row > 7 ) return ;
if ( row > 7 ) {
int r ;
r = row ;
SERIAL_ECHOPAIR ( " ??? Max7219_Clear_Row( " , r ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
for ( uint8_t c = 0 ; c < = 7 ; c + + )
for ( uint8_t c = 0 ; c < = 7 ; c + + )
Max7219_LED_Off ( c , row ) ;
Max7219_LED_Off ( c , row ) ;
}
}
void Max7219_Set_Row ( const uint8_t row , const uint8_t val ) {
void Max7219_Set_Row ( const uint8_t row , const uint8_t val ) {
if ( row > 7 ) return ;
if ( row > 7 | | val > 255 ) {
int r , v ;
r = row ;
v = val ;
SERIAL_ECHOPAIR ( " ??? Max7219_Set_Row( " , r ) ;
SERIAL_ECHOPAIR ( " , " , v ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
for ( uint8_t b = 0 ; b < = 7 ; b + + )
for ( uint8_t b = 0 ; b < = 7 ; b + + )
if ( TEST ( val , b ) )
if ( TEST ( val , b ) )
Max7219_LED_On ( 7 - b , row ) ;
Max7219_LED_On ( 7 - b , row ) ;
@ -121,8 +211,47 @@
Max7219_LED_Off ( 7 - b , row ) ;
Max7219_LED_Off ( 7 - b , row ) ;
}
}
void Max7219_Set_2_Rows ( const uint8_t row , const uint16_t val ) {
if ( row > 6 | | val > 65535 ) {
int r , v ;
r = row ;
v = val ;
SERIAL_ECHOPAIR ( " ??? Max7219_Set_2_Rows( " , r ) ;
SERIAL_ECHOPAIR ( " , " , v ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
Max7219_Set_Row ( row + 1 , ( val & 0xff00 ) > > 8 ) ;
Max7219_Set_Row ( row + 0 , ( val & 0xff ) ) ;
}
void Max7219_Set_4_Rows ( const uint8_t row , const uint32_t val ) {
if ( row > 4 ) {
int r ;
long v ;
r = row ;
v = val ;
SERIAL_ECHOPAIR ( " ??? Max7219_Set_4_Rows( " , r ) ;
SERIAL_ECHOPAIR ( " , " , v ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
Max7219_Set_Row ( row + 3 , ( val & 0xff000000 ) > > 24 ) ;
Max7219_Set_Row ( row + 2 , ( val & 0xff0000 ) > > 16 ) ;
Max7219_Set_Row ( row + 1 , ( val & 0xff00 ) > > 8 ) ;
Max7219_Set_Row ( row + 0 , ( val & 0xff ) ) ;
}
void Max7219_Set_Column ( const uint8_t col , const uint8_t val ) {
void Max7219_Set_Column ( const uint8_t col , const uint8_t val ) {
if ( col > 7 ) return ;
if ( val > 255 | | col > 7 ) {
int v , c ;
v = val ;
c = col ;
SERIAL_ECHOPAIR ( " ??? Max7219_Column( " , c ) ;
SERIAL_ECHOPAIR ( " , " , v ) ;
SERIAL_ECHO ( " ) \n " ) ;
return ;
}
LEDs [ col ] = val ;
LEDs [ col ] = val ;
Max7219 ( 8 - col , LEDs [ col ] ) ;
Max7219 ( 8 - col , LEDs [ col ] ) ;
}
}
@ -134,6 +263,7 @@
SET_OUTPUT ( MAX7219_CLK_PIN ) ;
SET_OUTPUT ( MAX7219_CLK_PIN ) ;
OUT_WRITE ( MAX7219_LOAD_PIN , HIGH ) ;
OUT_WRITE ( MAX7219_LOAD_PIN , HIGH ) ;
delay ( 1 ) ;
//initiation of the max 7219
//initiation of the max 7219
Max7219 ( max7219_reg_scanLimit , 0x07 ) ;
Max7219 ( max7219_reg_scanLimit , 0x07 ) ;
@ -184,35 +314,63 @@
* or clear a row is not very significant .
* or clear a row is not very significant .
*/
*/
void Max7219_idle_tasks ( ) {
void Max7219_idle_tasks ( ) {
# if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
CRITICAL_SECTION_START
# if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_QUEUE
uint8_t head ;
head = planner . block_buffer_head ;
# endif
# if MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
uint8_t tail ;
tail = planner . block_buffer_tail ;
# endif
CRITICAL_SECTION_END
# endif
# if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
# if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
static int debug_cnt = 0 ;
static millis_t next_blink = 0 ;
if ( debug_cnt + + > 100 ) {
if ( ELAPSED ( millis ( ) , next_blink ) ) {
Max7219_LED_Toggle ( 7 , 7 ) ;
Max7219_LED_Toggle ( 7 , 7 ) ;
debug_cnt = 0 ;
next_blink = millis ( ) + 75 0;
}
}
# endif
# endif
# ifdef MAX7219_DEBUG_STEPPER_HEAD
# ifdef MAX7219_DEBUG_STEPPER_HEAD
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_HEAD ) ;
static int16_t last_head_cnt = 0 ;
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_HEAD + 1 ) ;
if ( last_head_cnt ! = head ) {
if ( planner . block_buffer_head < 8 )
if ( last_head_cnt < 8 )
Max7219_LED_On ( planner . block_buffer_head , MAX7219_DEBUG_STEPPER_HEAD ) ;
Max7219_LED_Off ( last_head_cnt , MAX7219_DEBUG_STEPPER_HEAD ) ;
else
Max7219_LED_Off ( last_head_cnt - 8 , MAX7219_DEBUG_STEPPER_HEAD + 1 ) ;
last_head_cnt = head ;
if ( head < 8 )
Max7219_LED_On ( head , MAX7219_DEBUG_STEPPER_HEAD ) ;
else
else
Max7219_LED_On ( planner . block_buffer_head - 8 , MAX7219_DEBUG_STEPPER_HEAD + 1 ) ;
Max7219_LED_On ( head - 8 , MAX7219_DEBUG_STEPPER_HEAD + 1 ) ;
}
# endif
# endif
# ifdef MAX7219_DEBUG_STEPPER_TAIL
# ifdef MAX7219_DEBUG_STEPPER_TAIL
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_TAIL ) ;
static int16_t last_tail_cnt = 0 ;
Max7219_Clear_Row ( MAX7219_DEBUG_STEPPER_TAIL + 1 ) ;
if ( last_tail_cnt ! = tail ) {
if ( planner. block_buffer_tail < 8 )
if ( last_tail_cnt < 8 )
Max7219_LED_O n( planner . block_buffer_tail , MAX7219_DEBUG_STEPPER_TAIL ) ;
Max7219_LED_O ff( last_tail_cnt , MAX7219_DEBUG_STEPPER_TAIL ) ;
else
else
Max7219_LED_On ( planner . block_buffer_tail - 8 , MAX7219_DEBUG_STEPPER_TAIL + 1 ) ;
Max7219_LED_Off ( last_tail_cnt - 8 , MAX7219_DEBUG_STEPPER_TAIL + 1 ) ;
last_tail_cnt = tail ;
if ( tail < 8 )
Max7219_LED_On ( tail , MAX7219_DEBUG_STEPPER_TAIL ) ;
else
Max7219_LED_On ( tail - 8 , MAX7219_DEBUG_STEPPER_TAIL + 1 ) ;
}
# endif
# endif
# ifdef MAX7219_DEBUG_STEPPER_QUEUE
# ifdef MAX7219_DEBUG_STEPPER_QUEUE
static int16_t last_depth = 0 ;
static int16_t last_depth = 0 ;
int16_t current_depth = planner . block_buffer_head - planner . block_buffer_tail ;
int16_t current_depth = head - tail;
if ( current_depth ! = last_depth ) { // usually, no update will be needed.
if ( current_depth ! = last_depth ) { // usually, no update will be needed.
if ( current_depth < 0 ) current_depth + = BLOCK_BUFFER_SIZE ;
if ( current_depth < 0 ) current_depth + = BLOCK_BUFFER_SIZE ;
NOMORE ( current_depth , BLOCK_BUFFER_SIZE ) ;
NOMORE ( current_depth , BLOCK_BUFFER_SIZE ) ;
@ -223,10 +381,10 @@
en = max ( current_depth , last_depth ) ;
en = max ( current_depth , last_depth ) ;
if ( current_depth < last_depth )
if ( current_depth < last_depth )
for ( uint8_t i = st ; i < = en ; i + + ) // clear the highest order LEDs
for ( uint8_t i = st ; i < = en ; i + + ) // clear the highest order LEDs
Max7219_LED_Off ( i > > 1 , MAX7219_DEBUG_STEPPER_QUEUE + ( i & 1 ) ) ;
Max7219_LED_Off ( i / 2 , MAX7219_DEBUG_STEPPER_QUEUE + ( i & 1 ) ) ;
else
else
for ( uint8_t i = st ; i < = en ; i + + ) // set the highest order LEDs
for ( uint8_t i = st ; i < = en ; i + + ) // set the LEDs to current depth
Max7219_LED_On ( i > > 1 , MAX7219_DEBUG_STEPPER_QUEUE + ( i & 1 ) ) ;
Max7219_LED_On ( i / 2 , MAX7219_DEBUG_STEPPER_QUEUE + ( i & 1 ) ) ;
last_depth = current_depth ;
last_depth = current_depth ;
}
}