Browse Source

Merge pull request #379 from KurtE/SerialEvents_main

Serial events main
main
Paul Stoffregen 5 years ago
parent
commit
36c270865a
No account linked to committer's email address
8 changed files with 85 additions and 9 deletions
  1. +1
    -0
      teensy4/EventResponder.h
  2. +15
    -1
      teensy4/HardwareSerial.h
  3. +37
    -0
      teensy4/HardwareSerial1.cpp
  4. +5
    -4
      teensy4/startup.c
  5. +2
    -2
      teensy4/usb_inst.cpp
  6. +18
    -1
      teensy4/usb_serial.c
  7. +1
    -0
      teensy4/usb_serial.h
  8. +6
    -1
      teensy4/yield.cpp

+ 1
- 0
teensy4/EventResponder.h View File

@@ -170,6 +170,7 @@ public:
EventResponder * waitForEvent(EventResponder *list, int listsize, int timeout);

static void runFromYield() {
if (!firstYield) return;
// First, check if yield was called from an interrupt
// never call normal handler functions from any interrupt context
uint32_t ipsr;

+ 15
- 1
teensy4/HardwareSerial.h View File

@@ -181,6 +181,7 @@ public:
void enableSerialEvents();
void disableSerialEvents();
static void processSerialEvents();
static uint8_t serial_event_handlers_active;

using Print::write;
// Only overwrite some of the virtualWrite functions if we are going to optimize them over Print version
@@ -236,7 +237,6 @@ private:
#else
static SerialEventCheckingFunctionPointer serial_event_handler_checks[7];
#endif
static uint8_t serial_event_handlers_active;



@@ -265,6 +265,20 @@ extern void serialEvent8(void);
#endif // __cplusplus


// c functions to call c++ code in case some programs call the old functions
// Defined under extern "C" {}

#ifdef __cplusplus
extern "C" {
#endif
extern void serial_print(const char *p);
extern void serial_phex(uint32_t n);
extern void serial_phex16(uint32_t n);
extern void serial_phex32(uint32_t n);

#ifdef __cplusplus
}
#endif


// TODO: replace with proper divisor+oversample calculation

+ 37
- 0
teensy4/HardwareSerial1.cpp View File

@@ -71,3 +71,40 @@ HardwareSerial Serial1(&IMXRT_LPUART6, &UART6_Hardware, tx_buffer1, SERIAL1_TX_B

void serialEvent1() __attribute__((weak));
void serialEvent1() {Serial1.disableSerialEvents(); } // No use calling this so disable if called...

// C wrapper functions to help take care of places that used to call these from standard C
void serial_print(const char *p)
{
Serial1.write(p);
}

static void serial_phex1(uint32_t n)
{
n &= 15;
if (n < 10) {
Serial1.write('0' + n);
} else {
Serial1.write('A' - 10 + n);
}
}

void serial_phex(uint32_t n)
{
serial_phex1(n >> 4);
serial_phex1(n);
}

void serial_phex16(uint32_t n)
{
serial_phex(n >> 8);
serial_phex(n);
}

void serial_phex32(uint32_t n)
{
serial_phex(n >> 24);
serial_phex(n >> 16);
serial_phex(n >> 8);
serial_phex(n);
}


+ 5
- 4
teensy4/startup.c View File

@@ -112,15 +112,16 @@ void ResetHandler(void)
tempmon_init();

while (millis() < 300) ; // wait at least 300ms before calling user code
printf("before C++ constructors\n");
//printf("before C++ constructors\n");
__libc_init_array();
printf("after C++ constructors\n");
printf("before setup\n");
//printf("after C++ constructors\n");
//printf("before setup\n");
setup();
printf("after setup\n");
//printf("after setup\n");
while (1) {
//printf("loop\n");
loop();
yield();
}
}


+ 2
- 2
teensy4/usb_inst.cpp View File

@@ -84,6 +84,6 @@ usb_seremu_class Serial;
#endif

#endif // F_CPU
uint8_t usb_enable_serial_event_processing = 1;
void serialEvent() __attribute__((weak));
void serialEvent() {}
void serialEvent() {usb_enable_serial_event_processing = 0;}

+ 18
- 1
teensy4/usb_serial.c View File

@@ -141,6 +141,10 @@ int usb_serial_getchar(void)
// peek at the next character, or -1 if nothing received
int usb_serial_peekchar(void)
{
if (rx_index[0] < rx_count[0]) {
return rx_buffer[rx_index[0]];
}

#if 0
if (!rx_packet) {
if (!usb_configuration) return -1;
@@ -169,7 +173,20 @@ int usb_serial_available(void)
// read a block of bytes to a buffer
int usb_serial_read(void *buffer, uint32_t size)
{
#if 0
#if 1
// Quick and dirty to make it at least limp...
uint8_t *p = (uint8_t *)buffer;
uint32_t count=0;

while (size) {
int ch = usb_serial_getchar();
if (ch == -1) break;
*p++ = (uint8_t)ch;
size--;
count++;
}
return count;
#else
uint8_t *p = (uint8_t *)buffer;
uint32_t qty, count=0;


+ 1
- 0
teensy4/usb_serial.h View File

@@ -57,6 +57,7 @@ extern volatile uint32_t systick_millis_count;
extern volatile uint8_t usb_cdc_line_rtsdtr;
extern volatile uint8_t usb_cdc_transmit_flush_timer;
extern volatile uint8_t usb_configuration;
extern uint8_t usb_enable_serial_event_processing;
#ifdef __cplusplus
}
#endif

+ 6
- 1
teensy4/yield.cpp View File

@@ -39,9 +39,14 @@ void yield(void)
if (running) return; // TODO: does this need to be atomic?
running = 1;


// USB Serail - Add hack to minimize impact...
if (usb_enable_serial_event_processing && Serial.available()) serialEvent();

// Current workaround until integrate with EventResponder.
HardwareSerial::processSerialEvents();
if (HardwareSerial::serial_event_handlers_active) HardwareSerial::processSerialEvents();

running = 0;
EventResponder::runFromYield();
};

Loading…
Cancel
Save