aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/avr/Makefile4
-rw-r--r--src/avr/serial.c119
-rw-r--r--src/generic/serial_irq.c120
-rw-r--r--src/generic/serial_irq.h11
-rw-r--r--src/sam3x8e/Makefile2
-rw-r--r--src/sam3x8e/serial.c125
-rw-r--r--src/stm32f1/Makefile4
-rw-r--r--src/stm32f1/serial.c124
8 files changed, 166 insertions, 343 deletions
diff --git a/src/avr/Makefile b/src/avr/Makefile
index 371bd961..9f814a44 100644
--- a/src/avr/Makefile
+++ b/src/avr/Makefile
@@ -3,7 +3,7 @@
# Use the avr toolchain
CROSS_PREFIX=avr-
-dirs-y += src/avr lib/pjrc_usb_serial
+dirs-y += src/avr src/generic lib/pjrc_usb_serial
CFLAGS += -mmcu=$(CONFIG_MCU)
@@ -11,7 +11,7 @@ CFLAGS += -mmcu=$(CONFIG_MCU)
src-y += avr/main.c avr/timer.c avr/gpio.c
src-$(CONFIG_AVR_WATCHDOG) += avr/watchdog.c
src-$(CONFIG_AVR_USBSERIAL) += avr/usbserial.c ../lib/pjrc_usb_serial/usb_serial.c
-src-$(CONFIG_AVR_SERIAL) += avr/serial.c
+src-$(CONFIG_AVR_SERIAL) += avr/serial.c generic/serial_irq.c
# Suppress broken "misspelled signal handler" warnings on gcc 4.8.1
CFLAGS_klipper.elf := $(CFLAGS_klipper.elf) $(if $(filter 4.8.1, $(shell $(CC) -dumpversion)), -w)
diff --git a/src/avr/serial.c b/src/avr/serial.c
index 5fcf54d6..4b72bf76 100644
--- a/src/avr/serial.c
+++ b/src/avr/serial.c
@@ -1,31 +1,14 @@
// AVR serial port code.
//
-// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <avr/interrupt.h> // USART0_RX_vect
-#include <string.h> // memmove
#include "autoconf.h" // CONFIG_SERIAL_BAUD
-#include "board/io.h" // readb
-#include "board/misc.h" // console_sendf
-#include "command.h" // DECL_CONSTANT
-#include "irq.h" // irq_save
-#include "pgm.h" // READP
+#include "board/serial_irq.h" // serial_rx_byte
#include "sched.h" // DECL_INIT
-static char receive_buf[192];
-static uint8_t receive_pos;
-static char transmit_buf[96];
-static uint8_t transmit_pos, transmit_max;
-
-
-/****************************************************************
- * Serial hardware
- ****************************************************************/
-
-DECL_CONSTANT(SERIAL_BAUD, CONFIG_SERIAL_BAUD);
-
// Define serial port registers on AT90USB1286
#if !defined(UCSR0A) && defined(UCSR1A)
#define UCSR0A UCSR1A
@@ -64,107 +47,23 @@ DECL_INIT(serial_init);
// Rx interrupt - data available to be read.
ISR(USART0_RX_vect)
{
- uint8_t data = UDR0;
- if (data == MESSAGE_SYNC)
- sched_wake_tasks();
- if (receive_pos >= sizeof(receive_buf))
- // Serial overflow - ignore it as crc error will force retransmit
- return;
- receive_buf[receive_pos++] = data;
+ serial_rx_byte(UDR0);
}
// Tx interrupt - data can be written to serial.
ISR(USART0_UDRE_vect)
{
- if (transmit_pos >= transmit_max)
+ uint8_t data;
+ int ret = serial_get_tx_byte(&data);
+ if (ret)
UCSR0B &= ~(1<<UDRIE0);
else
- UDR0 = transmit_buf[transmit_pos++];
+ UDR0 = data;
}
// Enable tx interrupts
-static void
-enable_tx_irq(void)
-{
- UCSR0B |= 1<<UDRIE0;
-}
-
-
-/****************************************************************
- * Console access functions
- ****************************************************************/
-
-// Remove from the receive buffer the given number of bytes
-static void
-console_pop_input(uint8_t len)
-{
- uint8_t copied = 0;
- for (;;) {
- uint8_t rpos = readb(&receive_pos);
- uint8_t needcopy = rpos - len;
- if (needcopy) {
- memmove(&receive_buf[copied], &receive_buf[copied + len]
- , needcopy - copied);
- copied = needcopy;
- sched_wake_tasks();
- }
- irqstatus_t flag = irq_save();
- if (rpos != readb(&receive_pos)) {
- // Raced with irq handler - retry
- irq_restore(flag);
- continue;
- }
- receive_pos = needcopy;
- irq_restore(flag);
- break;
- }
-}
-
-// Process any incoming commands
void
-console_task(void)
+serial_enable_tx_irq(void)
{
- uint8_t pop_count, rpos = readb(&receive_pos);
- int8_t ret = command_find_block(receive_buf, rpos, &pop_count);
- if (ret > 0)
- command_dispatch(receive_buf, pop_count);
- if (ret)
- console_pop_input(pop_count);
-}
-DECL_TASK(console_task);
-
-// Encode and transmit a "response" message
-void
-console_sendf(const struct command_encoder *ce, va_list args)
-{
- // Verify space for message
- uint8_t tpos = readb(&transmit_pos), tmax = readb(&transmit_max);
- if (tpos >= tmax) {
- tpos = tmax = 0;
- writeb(&transmit_max, 0);
- writeb(&transmit_pos, 0);
- }
- uint8_t max_size = READP(ce->max_size);
- if (tmax + max_size > sizeof(transmit_buf)) {
- if (tmax + max_size - tpos > sizeof(transmit_buf))
- // Not enough space for message
- return;
- // Disable TX irq and move buffer
- writeb(&transmit_max, 0);
- tpos = readb(&transmit_pos);
- tmax -= tpos;
- memmove(&transmit_buf[0], &transmit_buf[tpos], tmax);
- writeb(&transmit_pos, 0);
- writeb(&transmit_max, tmax);
- enable_tx_irq();
- }
-
- // Generate message
- char *buf = &transmit_buf[tmax];
- uint8_t msglen = command_encodef(buf, ce, args);
- command_add_frame(buf, msglen);
-
- // Start message transmit
- writeb(&transmit_max, tmax + msglen);
- enable_tx_irq();
+ UCSR0B |= 1<<UDRIE0;
}
diff --git a/src/generic/serial_irq.c b/src/generic/serial_irq.c
new file mode 100644
index 00000000..79eaad7f
--- /dev/null
+++ b/src/generic/serial_irq.c
@@ -0,0 +1,120 @@
+// Generic interrupt based serial uart helper code
+//
+// Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include <string.h> // memmove
+#include "autoconf.h" // CONFIG_SERIAL_BAUD
+#include "board/io.h" // readb
+#include "board/irq.h" // irq_save
+#include "board/misc.h" // console_sendf
+#include "board/pgm.h" // READP
+#include "command.h" // DECL_CONSTANT
+#include "sched.h" // sched_wake_tasks
+#include "serial_irq.h" // serial_enable_tx_irq
+
+static char receive_buf[192];
+static uint8_t receive_pos;
+static char transmit_buf[96];
+static uint8_t transmit_pos, transmit_max;
+
+DECL_CONSTANT(SERIAL_BAUD, CONFIG_SERIAL_BAUD);
+
+// Rx interrupt - store read data
+void
+serial_rx_byte(uint_fast8_t data)
+{
+ if (data == MESSAGE_SYNC)
+ sched_wake_tasks();
+ if (receive_pos >= sizeof(receive_buf))
+ // Serial overflow - ignore it as crc error will force retransmit
+ return;
+ receive_buf[receive_pos++] = data;
+}
+
+// Tx interrupt - get next byte to transmit
+int
+serial_get_tx_byte(uint8_t *pdata)
+{
+ if (transmit_pos >= transmit_max)
+ return -1;
+ *pdata = transmit_buf[transmit_pos++];
+ return 0;
+}
+
+// Remove from the receive buffer the given number of bytes
+static void
+console_pop_input(uint_fast8_t len)
+{
+ uint_fast8_t copied = 0;
+ for (;;) {
+ uint_fast8_t rpos = readb(&receive_pos);
+ uint_fast8_t needcopy = rpos - len;
+ if (needcopy) {
+ memmove(&receive_buf[copied], &receive_buf[copied + len]
+ , needcopy - copied);
+ copied = needcopy;
+ sched_wake_tasks();
+ }
+ irqstatus_t flag = irq_save();
+ if (rpos != readb(&receive_pos)) {
+ // Raced with irq handler - retry
+ irq_restore(flag);
+ continue;
+ }
+ receive_pos = needcopy;
+ irq_restore(flag);
+ break;
+ }
+}
+
+// Process any incoming commands
+void
+console_task(void)
+{
+ uint_fast8_t rpos = readb(&receive_pos);
+ uint8_t pop_count;
+ int8_t ret = command_find_block(receive_buf, rpos, &pop_count);
+ if (ret > 0)
+ command_dispatch(receive_buf, pop_count);
+ if (ret)
+ console_pop_input(pop_count);
+}
+DECL_TASK(console_task);
+
+// Encode and transmit a "response" message
+void
+console_sendf(const struct command_encoder *ce, va_list args)
+{
+ // Verify space for message
+ uint_fast8_t tpos = readb(&transmit_pos), tmax = readb(&transmit_max);
+ if (tpos >= tmax) {
+ tpos = tmax = 0;
+ writeb(&transmit_max, 0);
+ writeb(&transmit_pos, 0);
+ }
+ uint_fast8_t max_size = READP(ce->max_size);
+ if (tmax + max_size > sizeof(transmit_buf)) {
+ if (tmax + max_size - tpos > sizeof(transmit_buf))
+ // Not enough space for message
+ return;
+ // Disable TX irq and move buffer
+ writeb(&transmit_max, 0);
+ tpos = readb(&transmit_pos);
+ tmax -= tpos;
+ memmove(&transmit_buf[0], &transmit_buf[tpos], tmax);
+ writeb(&transmit_pos, 0);
+ writeb(&transmit_max, tmax);
+ serial_enable_tx_irq();
+ }
+
+ // Generate message
+ char *buf = &transmit_buf[tmax];
+ uint8_t msglen = command_encodef(buf, ce, args);
+ command_add_frame(buf, msglen);
+
+ // Start message transmit
+ writeb(&transmit_max, tmax + msglen);
+ serial_enable_tx_irq();
+}
diff --git a/src/generic/serial_irq.h b/src/generic/serial_irq.h
new file mode 100644
index 00000000..470c9dfa
--- /dev/null
+++ b/src/generic/serial_irq.h
@@ -0,0 +1,11 @@
+#ifndef __GENERIC_SERIAL_IRQ_H
+#define __GENERIC_SERIAL_IRQ_H
+
+// callback provided by board specific code
+void serial_enable_tx_irq(void);
+
+// serial_irq.c
+void serial_rx_byte(uint_fast8_t data);
+int serial_get_tx_byte(uint8_t *pdata);
+
+#endif // serial_irq.h
diff --git a/src/sam3x8e/Makefile b/src/sam3x8e/Makefile
index fb42fc61..43aef609 100644
--- a/src/sam3x8e/Makefile
+++ b/src/sam3x8e/Makefile
@@ -20,7 +20,7 @@ src-y += generic/crc16_ccitt.c generic/alloc.c
src-y += generic/armcm_irq.c generic/timer_irq.c
src-y += ../lib/cmsis-sam3x8e/source/system_sam3xa.c
src-y += ../lib/cmsis-sam3x8e/source/gcc/startup_sam3xa.c
-src-$(CONFIG_SERIAL) += sam3x8e/serial.c
+src-$(CONFIG_SERIAL) += sam3x8e/serial.c generic/serial_irq.c
# Build the additional hex output file
target-y += $(OUT)klipper.bin
diff --git a/src/sam3x8e/serial.c b/src/sam3x8e/serial.c
index f4a65aa0..b10e8d8b 100644
--- a/src/sam3x8e/serial.c
+++ b/src/sam3x8e/serial.c
@@ -1,32 +1,15 @@
// sam3x8e serial port
//
-// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
-#include <string.h> // memmove
#include "autoconf.h" // CONFIG_SERIAL_BAUD
#include "board/gpio.h" // gpio_peripheral
-#include "board/io.h" // readl
-#include "board/irq.h" // irq_save
-#include "board/misc.h" // console_sendf
-#include "command.h" // DECL_CONSTANT
+#include "board/serial_irq.h" // serial_rx_data
#include "sam3x8e.h" // UART
#include "sched.h" // DECL_INIT
-#define SERIAL_BUFFER_SIZE 96
-static char receive_buf[SERIAL_BUFFER_SIZE];
-static uint32_t receive_pos;
-static char transmit_buf[SERIAL_BUFFER_SIZE];
-static uint32_t transmit_pos, transmit_max;
-
-
-/****************************************************************
- * Serial hardware
- ****************************************************************/
-
-DECL_CONSTANT(SERIAL_BAUD, CONFIG_SERIAL_BAUD);
-
void
serial_init(void)
{
@@ -54,108 +37,20 @@ void __visible
UART_Handler(void)
{
uint32_t status = UART->UART_SR;
- if (status & UART_SR_RXRDY) {
- uint8_t data = UART->UART_RHR;
- if (data == MESSAGE_SYNC)
- sched_wake_tasks();
- if (receive_pos >= sizeof(receive_buf))
- // Serial overflow - ignore it as crc error will force retransmit
- return;
- receive_buf[receive_pos++] = data;
- return;
- }
+ if (status & UART_SR_RXRDY)
+ serial_rx_byte(UART->UART_RHR);
if (status & UART_SR_TXRDY) {
- if (transmit_pos >= transmit_max)
+ uint8_t data;
+ int ret = serial_get_tx_byte(&data);
+ if (ret)
UART->UART_IDR = UART_IDR_TXRDY;
else
- UART->UART_THR = transmit_buf[transmit_pos++];
- }
-}
-
-// Enable tx interrupts
-static void
-enable_tx_irq(void)
-{
- UART->UART_IER = UART_IDR_TXRDY;
-}
-
-
-/****************************************************************
- * Console access functions
- ****************************************************************/
-
-// Remove from the receive buffer the given number of bytes
-static void
-console_pop_input(uint32_t len)
-{
- uint32_t copied = 0;
- for (;;) {
- uint32_t rpos = readl(&receive_pos);
- uint32_t needcopy = rpos - len;
- if (needcopy) {
- memmove(&receive_buf[copied], &receive_buf[copied + len]
- , needcopy - copied);
- copied = needcopy;
- sched_wake_tasks();
- }
- irqstatus_t flag = irq_save();
- if (rpos != readl(&receive_pos)) {
- // Raced with irq handler - retry
- irq_restore(flag);
- continue;
- }
- receive_pos = needcopy;
- irq_restore(flag);
- break;
+ UART->UART_THR = data;
}
}
-// Process any incoming commands
-void
-console_task(void)
-{
- uint8_t pop_count;
- uint32_t rpos = readl(&receive_pos);
- int8_t ret = command_find_block(receive_buf, rpos, &pop_count);
- if (ret > 0)
- command_dispatch(receive_buf, pop_count);
- if (ret)
- console_pop_input(pop_count);
-}
-DECL_TASK(console_task);
-
-// Encode and transmit a "response" message
void
-console_sendf(const struct command_encoder *ce, va_list args)
+serial_enable_tx_irq(void)
{
- // Verify space for message
- uint32_t tpos = readl(&transmit_pos), tmax = readl(&transmit_max);
- if (tpos >= tmax) {
- tpos = tmax = 0;
- writel(&transmit_max, 0);
- writel(&transmit_pos, 0);
- }
- uint32_t max_size = ce->max_size;
- if (tmax + max_size > sizeof(transmit_buf)) {
- if (tmax + max_size - tpos > sizeof(transmit_buf))
- // Not enough space for message
- return;
- // Disable TX irq and move buffer
- writel(&transmit_max, 0);
- tpos = readl(&transmit_pos);
- tmax -= tpos;
- memmove(&transmit_buf[0], &transmit_buf[tpos], tmax);
- writel(&transmit_pos, 0);
- writel(&transmit_max, tmax);
- enable_tx_irq();
- }
-
- // Generate message
- char *buf = &transmit_buf[tmax];
- uint32_t msglen = command_encodef(buf, ce, args);
- command_add_frame(buf, msglen);
-
- // Start message transmit
- writel(&transmit_max, tmax + msglen);
- enable_tx_irq();
+ UART->UART_IER = UART_IDR_TXRDY;
}
diff --git a/src/stm32f1/Makefile b/src/stm32f1/Makefile
index d7d20744..9fca3561 100644
--- a/src/stm32f1/Makefile
+++ b/src/stm32f1/Makefile
@@ -8,7 +8,7 @@ dirs-y += lib/cmsis-stm32f1/source
dirs-y += lib/hal-stm32f1/source
CFLAGS += -mthumb -mcpu=cortex-m3
-CFLAGS += -Ilib/cmsis-stm32f1/include -Ilib/cmsis-stm32f1/cmsis-include
+CFLAGS += -Ilib/cmsis-stm32f1/include -Ilib/cmsis-stm32f1/cmsis-include
CFLAGS += -Ilib/hal-stm32f1/include
CFLAGS += -DSTM32F103xB
CFLAGS += -O3
@@ -28,7 +28,7 @@ src-y += $(addprefix ../, $(wildcard lib/hal-stm32f1/source/stm32f1xx_ll_*.c))
src-y += generic/crc16_ccitt.c generic/armcm_irq.c generic/timer_irq.c
src-y += ../lib/cmsis-stm32f1/source/system_stm32f1xx.c
src-ys = ../lib/cmsis-stm32f1/source/startup_stm32f103xb.s
-src-$(CONFIG_SERIAL) += stm32f1/serial.c
+src-$(CONFIG_SERIAL) += stm32f1/serial.c generic/serial_irq.c
# Build the additional hex output file
target-y += $(OUT)klipper.bin
diff --git a/src/stm32f1/serial.c b/src/stm32f1/serial.c
index d79c74f8..bc6ebb94 100644
--- a/src/stm32f1/serial.c
+++ b/src/stm32f1/serial.c
@@ -4,30 +4,15 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.
-#include <string.h> // memmove
+#include <stdint.h>
#include "autoconf.h" // CONFIG_SERIAL_BAUD
-#include "command.h" // DECL_CONSTANT
+#include "board/serial_irq.h" // serial_rx_byte
+#include "sched.h" // DECL_INIT
#include "stm32f1xx.h" // UART
#include "stm32f1xx_ll_bus.h"
#include "stm32f1xx_ll_rcc.h"
#include "stm32f1xx_ll_usart.h"
#include "stm32f1xx_ll_gpio.h"
-#include "board/irq.h"
-#include "board/io.h"
-#include "sched.h" // DECL_INIT
-
-#define SERIAL_BUFFER_SIZE 96
-static char receive_buf[SERIAL_BUFFER_SIZE];
-static uint32_t receive_pos;
-static char transmit_buf[SERIAL_BUFFER_SIZE];
-static uint32_t transmit_pos, transmit_max;
-
-
-/****************************************************************
- * Serial hardware
- ****************************************************************/
-
-DECL_CONSTANT(SERIAL_BAUD, CONFIG_SERIAL_BAUD);
void
serial_init(void)
@@ -60,107 +45,20 @@ DECL_INIT(serial_init);
void __visible
USART1_IRQHandler(void)
{
- if (LL_USART_IsActiveFlag_RXNE(USART1) || LL_USART_IsActiveFlag_ORE(USART1)) {
- uint8_t data = LL_USART_ReceiveData8(USART1);
- if (data == MESSAGE_SYNC)
- sched_wake_tasks();
- if (receive_pos >= sizeof(receive_buf))
- // Serial overflow - ignore it as crc error will force retransmit
- return;
- receive_buf[receive_pos++] = data;
- return;
- }
+ if (LL_USART_IsActiveFlag_RXNE(USART1) || LL_USART_IsActiveFlag_ORE(USART1))
+ serial_rx_byte(LL_USART_ReceiveData8(USART1));
if (LL_USART_IsActiveFlag_TXE(USART1)) {
- if (transmit_pos >= transmit_max)
+ uint8_t data;
+ int ret = serial_get_tx_byte(&data);
+ if (ret)
LL_USART_DisableIT_TXE(USART1);
else
- LL_USART_TransmitData8(USART1, transmit_buf[transmit_pos++]);
+ LL_USART_TransmitData8(USART1, data);
}
}
-// Enable tx interrupts
-static void
-enable_tx_irq(void)
-{
- LL_USART_EnableIT_TXE(USART1);
-}
-
-/****************************************************************
- * Console access functions
- ****************************************************************/
-
-// Remove from the receive buffer the given number of bytes
-static void
-console_pop_input(uint32_t len)
-{
- uint32_t copied = 0;
- for (;;) {
- uint32_t rpos = readl(&receive_pos);
- uint32_t needcopy = rpos - len;
- if (needcopy) {
- memmove(&receive_buf[copied], &receive_buf[copied + len]
- , needcopy - copied);
- copied = needcopy;
- sched_wake_tasks();
- }
- irqstatus_t flag = irq_save();
- if (rpos != readl(&receive_pos)) {
- // Raced with irq handler - retry
- irq_restore(flag);
- continue;
- }
- receive_pos = needcopy;
- irq_restore(flag);
- break;
- }
-}
-
-// Process any incoming commands
-void
-console_task(void)
-{
- uint8_t pop_count;
- uint32_t rpos = readl(&receive_pos);
- int8_t ret = command_find_block(receive_buf, rpos, &pop_count);
- if (ret > 0)
- command_dispatch(receive_buf, pop_count);
- if (ret)
- console_pop_input(pop_count);
-}
-DECL_TASK(console_task);
-
-// Encode and transmit a "response" message
void
-console_sendf(const struct command_encoder *ce, va_list args)
+serial_enable_tx_irq(void)
{
- // Verify space for message
- uint32_t tpos = readl(&transmit_pos), tmax = readl(&transmit_max);
- if (tpos >= tmax) {
- tpos = tmax = 0;
- writel(&transmit_max, 0);
- writel(&transmit_pos, 0);
- }
- uint32_t max_size = ce->max_size;
- if (tmax + max_size > sizeof(transmit_buf)) {
- if (tmax + max_size - tpos > sizeof(transmit_buf))
- // Not enough space for message
- return;
- // Disable TX irq and move buffer
- writel(&transmit_max, 0);
- tpos = readl(&transmit_pos);
- tmax -= tpos;
- memmove(&transmit_buf[0], &transmit_buf[tpos], tmax);
- writel(&transmit_pos, 0);
- writel(&transmit_max, tmax);
- enable_tx_irq();
- }
-
- // Generate message
- char *buf = &transmit_buf[tmax];
- uint32_t msglen = command_encodef(buf, ce, args);
- command_add_frame(buf, msglen);
-
- // Start message transmit
- writel(&transmit_max, tmax + msglen);
- enable_tx_irq();
+ LL_USART_EnableIT_TXE(USART1);
}