Skip to content

Commit 23238f5

Browse files
committed
rx/tx leds
1 parent 25e8e1d commit 23238f5

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

cpu_map.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,16 +406,16 @@
406406
#define DIRECTION_MASK ((1<<X_DIRECTION_BIT)|(1<<Y_DIRECTION_BIT)|(1<<Z_DIRECTION_BIT)) // All direction bits
407407

408408
// Define stepper driver enable/disable output pin.
409-
#define STEPPERS_DISABLE_DDR DDRB
410-
#define STEPPERS_DISABLE_PORT PORTB
409+
#define STEPPERS_DISABLE_DDR DDRD
410+
#define STEPPERS_DISABLE_PORT PORTD
411411
#define STEPPERS_DISABLE_BIT 0 // Uno Digital Pin 8
412412
#define STEPPERS_DISABLE_MASK (1<<STEPPERS_DISABLE_BIT)
413413

414414
// Define homing/hard limit switch input pins and limit interrupt vectors.
415415
// NOTE: All limit bit pins must be on the same port, but not on a port with other input pins (pinout).
416-
#define LIMIT_DDR DDRB
417-
#define LIMIT_PIN PINB
418-
#define LIMIT_PORT PORTB
416+
#define LIMIT_DDR DDRD
417+
#define LIMIT_PIN PIND
418+
#define LIMIT_PORT PORTD
419419
#define X_LIMIT_BIT 1 // Uno Digital Pin 9
420420
#define Y_LIMIT_BIT 2 // Uno Digital Pin 10
421421
#ifdef VARIABLE_SPINDLE // Z Limit pin and spindle enabled swapped to access hardware PWM on Pin 11.
@@ -429,15 +429,15 @@
429429
#define LIMIT_PCMSK PCMSK0 // Pin change interrupt register
430430

431431
// Define spindle enable and spindle direction output pins.
432-
#define SPINDLE_ENABLE_DDR DDRB
433-
#define SPINDLE_ENABLE_PORT PORTB
432+
#define SPINDLE_ENABLE_DDR DDRD
433+
#define SPINDLE_ENABLE_PORT PORTD
434434
#ifdef VARIABLE_SPINDLE // Z Limit pin and spindle enabled swapped to access hardware PWM on Pin 11.
435435
#define SPINDLE_ENABLE_BIT 3 // Uno Digital Pin 11
436436
#else
437437
#define SPINDLE_ENABLE_BIT 4 // Uno Digital Pin 12
438438
#endif
439-
#define SPINDLE_DIRECTION_DDR DDRB
440-
#define SPINDLE_DIRECTION_PORT PORTB
439+
#define SPINDLE_DIRECTION_DDR DDRD
440+
#define SPINDLE_DIRECTION_PORT PORTD
441441
#define SPINDLE_DIRECTION_BIT 5 // Uno Digital Pin 13 (NOTE: D13 can't be pulled-high input due to LED.)
442442

443443
// Define flood and mist coolant enable output pins.

usb_serial.c

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@
118118
#define EP_TYPE_BULK_OUT 0x80
119119
#define EP_TYPE_INTERRUPT_IN 0xC1
120120

121+
#define TX_RX_LED_INIT DDRD |= (1<<5), DDRB |= (1<<0)
122+
#define TXLED0 PORTD |= (1<<5)
123+
#define TXLED1 PORTD &= ~(1<<5)
124+
#define RXLED0 PORTB |= (1<<0)
125+
#define RXLED1 PORTB &= ~(1<<0)
126+
127+
#define TX_RX_LED_PULSE_MS 100
128+
121129
/**************************************************************************
122130
*
123131
* Descriptor Data
@@ -330,6 +338,8 @@ serial_init()
330338

331339
UDCON = 0; // enable attach resistor
332340
UDIEN = (1<<EORSTE)|(1<<SOFE);
341+
342+
TX_RX_LED_INIT;
333343
}
334344

335345

@@ -351,6 +361,7 @@ serial_reset_read_buffer()
351361
// Misc functions to wait for ready and send/receive packets
352362
#define usb_wait_in_ready() while (!(UEINTX & (1<<TXINI ))) ;
353363
#define usb_wait_receive_out() while (!(UEINTX & (1<<RXOUTI))) ;
364+
#define usb_wait_io() while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI)))) ;
354365
#define usb_send_in() UEINTX = ~(1<<TXINI );
355366
#define usb_ack_out() UEINTX = ~(1<<RXOUTI);
356367

@@ -359,11 +370,12 @@ static uint8_t transmit_previous_timeout=0;
359370

360371
#define TRANSMIT_TIMEOUT 25 /* in milliseconds */
361372
#define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
362-
373+
volatile uint8_t tx_led_pulse, rx_led_pulse;
363374
// transmit a character.
364375
void
365376
serial_write(uint8_t c)
366377
{
378+
#if 1
367379
uint8_t timeout, intr_state;
368380

369381
// if we're not online (enumerated and configured), error
@@ -413,6 +425,53 @@ serial_write(uint8_t c)
413425
UEINTX = 0x3A;
414426
transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
415427
SREG = intr_state;
428+
#else
429+
if (!usb_configuration)
430+
return;
431+
432+
u8 timeout = 250; // 250ms timeout on send? TODO
433+
while (len)
434+
{
435+
u8 n = USB_SendSpace(ep);
436+
if (n == 0)
437+
{
438+
if (!(--timeout))
439+
return -1;
440+
delay(1);
441+
continue;
442+
}
443+
444+
if (n > len)
445+
n = len;
446+
{
447+
LockEP lock(ep);
448+
// Frame may have been released by the SOF interrupt handler
449+
if (!ReadWriteAllowed())
450+
continue;
451+
len -= n;
452+
if (ep & TRANSFER_ZERO)
453+
{
454+
while (n--)
455+
Send8(0);
456+
}
457+
else if (ep & TRANSFER_PGM)
458+
{
459+
while (n--)
460+
Send8(pgm_read_byte(data++));
461+
}
462+
else
463+
{
464+
while (n--)
465+
Send8(*data++);
466+
}
467+
if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer
468+
ReleaseTX();
469+
}
470+
}
471+
472+
#endif
473+
TXLED1;
474+
tx_led_pulse = TX_RX_LED_PULSE_MS;
416475
}
417476

418477
// Fetches the first byte in the serial read buffer. Called by main program.
@@ -427,16 +486,20 @@ serial_read()
427486
intr_state = SREG;
428487
UENUM = CDC_RX_ENDPOINT;
429488

430-
while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI)))); // wait for rx/tx
489+
usb_wait_io();
431490
if ((UEBCLX == 0)) { // Empty buffer => flush it
432491
UEINTX = 0x6B;
433492
goto exit;
434493
}
435494

436495
// take one byte out of the buffer
437496
ret = UEDATX;
497+
498+
RXLED1;
499+
rx_led_pulse = TX_RX_LED_PULSE_MS;
438500
exit:
439501
SREG = intr_state;
502+
440503
return ret;
441504
}
442505

@@ -476,6 +539,12 @@ ISR(USB_GEN_vect)
476539
UEINTX = 0x3A;
477540
}
478541
}
542+
// check whether the one-shot period has elapsed. if so, turn off the LED
543+
if (tx_led_pulse && !(--tx_led_pulse))
544+
TXLED0;
545+
if (rx_led_pulse && !(--rx_led_pulse))
546+
RXLED0;
547+
479548
}
480549
}
481550

@@ -501,7 +570,7 @@ get_descriptor(uint16_t value, uint16_t index, uint16_t len)
501570
if (len > desc_length) len = desc_length;
502571
do {
503572
// wait for host ready for IN packet
504-
while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI)))); // wait for rx/tx
573+
usb_wait_io();
505574
// send IN packet
506575
n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
507576
for (i = n; i; i--) {

0 commit comments

Comments
 (0)