summaryrefslogtreecommitdiffstats
path: root/pressure_control/firmware
diff options
context:
space:
mode:
authorMichael Buesch <mb@bu3sch.de>2008-10-12 16:46:48 +0200
committerMichael Buesch <mb@bu3sch.de>2008-10-12 16:46:48 +0200
commit4a914e8c559cd48173bed088e2122d1d35c8de9b (patch)
treebd31fdcbe3fb8c645663760ef84ad24875b15dc9 /pressure_control/firmware
parent660677d70eeacb01304a547df5dacac1bda8d036 (diff)
downloadcnc-4a914e8c559cd48173bed088e2122d1d35c8de9b.tar.xz
cnc-4a914e8c559cd48173bed088e2122d1d35c8de9b.zip
Add code to the pressure control.
Signed-off-by: Michael Buesch <mb@bu3sch.de>
Diffstat (limited to 'pressure_control/firmware')
-rw-r--r--pressure_control/firmware/Makefile8
-rw-r--r--pressure_control/firmware/main.c73
-rw-r--r--pressure_control/firmware/sensor.c28
-rw-r--r--pressure_control/firmware/sensor.h9
-rw-r--r--pressure_control/firmware/valves.c82
-rw-r--r--pressure_control/firmware/valves.h16
6 files changed, 213 insertions, 3 deletions
diff --git a/pressure_control/firmware/Makefile b/pressure_control/firmware/Makefile
index 30eaaf6..4024f02 100644
--- a/pressure_control/firmware/Makefile
+++ b/pressure_control/firmware/Makefile
@@ -17,7 +17,7 @@ CFLAGS += "-Dinline=inline __attribute__((__always_inline__))"
LFUSE = 0xE0
HFUSE = 0xD9
-OBJECTS = main.o util.o
+OBJECTS = main.o util.o valves.o sensor.o
NAME = pressure_control
BIN = $(NAME).bin
@@ -26,10 +26,14 @@ EEP = $(NAME).eep.hex
all: $(HEX)
-main.o: util.h
+main.o: util.h calibration.h valves.h sensor.h
util.o: util.h calibration.h
+valves.o: valves.h
+
+sensor.o: sensor.h
+
%.s: %.c
$(CC) $(CFLAGS) -S $*.c
diff --git a/pressure_control/firmware/main.c b/pressure_control/firmware/main.c
index 949e5cb..02272ef 100644
--- a/pressure_control/firmware/main.c
+++ b/pressure_control/firmware/main.c
@@ -18,15 +18,86 @@
*/
#include "util.h"
+#include "calibration.h"
+#include "sensor.h"
+#include "valves.h"
+static inline void usart_tx(uint8_t data)
+{
+ while (!(UCSRA & (1 << UDRE)))
+ ;
+ UDR = data;
+}
+
+static void __print(const prog_char *msg)
+{
+ uint8_t c;
+
+ for ( ; ; msg++) {
+ c = pgm_read_byte(msg);
+ if (c == '\0')
+ break;
+ usart_tx(c);
+ }
+}
+#define print(msg) __print(PSTR(msg))
+
+#define ERXFE 1 /* USART RX frame error */
+#define ERXPE 2 /* USART RX parity error */
+#define ERXOV 3 /* USART RX hardware buffer overflow */
+#define ENODATA 4 /* No data available */
+
+static inline int8_t usart_rx(uint8_t *data)
+{
+ uint8_t status;
+
+ status = UCSRA;
+ if (!(status & (1 << RXC)))
+ return -ENODATA;
+ if (unlikely(status & ((1 << FE) | (1 << PE) | (1 << DOR)))) {
+ if (status & (1 << FE))
+ return -ERXFE;
+ if (status & (1 << PE))
+ return -ERXPE;
+ if (status & (1 << DOR))
+ return -ERXOV;
+ }
+ *data = UDR;
+
+ return 0;
+}
+
+#define BAUDRATE 9600
+
+static void usart_init(void)
+{
+ uint8_t dummy;
+
+ /* Set baud rate */
+ UBRRL = lo8((CPU_HZ / 16 / BAUDRATE) * 2);
+ UBRRH = hi8((CPU_HZ / 16 / BAUDRATE) * 2) & ~(1 << URSEL);
+ UCSRA = (1 << U2X);
+ /* 8 Data bits, 2 Stop bits, Even parity */
+ UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1) | (1 << UPM1) | (1 << USBS);
+ /* Enable transceiver and RX IRQs */
+ UCSRB = (1 << RXEN) | (1 << TXEN);// | (1 << RXCIE);
+ /* Drain the RX buffer */
+ while (usart_rx(&dummy) != -ENODATA)
+ mb();
+}
+
int main(void)
{
cli();
- //TODO
+
+ valves_init();
+ usart_init();
+
sei();
while (1) {
+ print("Hallo!\n");
//TODO
}
}
diff --git a/pressure_control/firmware/sensor.c b/pressure_control/firmware/sensor.c
new file mode 100644
index 0000000..e663d7a
--- /dev/null
+++ b/pressure_control/firmware/sensor.c
@@ -0,0 +1,28 @@
+/*
+ * Pneumatic pressure controller.
+ * Sensor input.
+ *
+ * Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "sensor.h"
+
+#include <avr/io.h>
+
+
+void sensor_init(void)
+{
+}
diff --git a/pressure_control/firmware/sensor.h b/pressure_control/firmware/sensor.h
new file mode 100644
index 0000000..dd03e29
--- /dev/null
+++ b/pressure_control/firmware/sensor.h
@@ -0,0 +1,9 @@
+#ifndef SENSOR_H_
+#define SENSOR_H_
+
+#include <stdint.h>
+
+
+void sensor_init(void);
+
+#endif /* SENSOR_H_ */
diff --git a/pressure_control/firmware/valves.c b/pressure_control/firmware/valves.c
new file mode 100644
index 0000000..f77d8b2
--- /dev/null
+++ b/pressure_control/firmware/valves.c
@@ -0,0 +1,82 @@
+/*
+ * Pneumatic pressure controller.
+ * Valve control.
+ *
+ * Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "valves.h"
+
+#include <avr/io.h>
+
+
+/*** Valve interface definitions ***/
+#define VALVE_DDR DDRD
+#define VALVE_PORT PORTD
+#define VALVE0_14 4 /* Pin for valve-0 position 14 */
+#define VALVE0_12 5 /* Pin for valve-0 position 12 */
+#define VALVE1_14 6 /* Pin for valve-1 position 14 */
+#define VALVE1_12 7 /* Pin for valve-1 position 12 */
+
+/* State for one valve. */
+enum valve_state {
+ VALVE_STATE_12,
+ VALVE_STATE_14,
+};
+
+
+static void valve0_switch(uint8_t state)
+{
+ VALVE_PORT &= ~((1 << VALVE0_12) | (1 << VALVE0_14));
+ if (state == VALVE_STATE_12)
+ VALVE_PORT |= (1 << VALVE0_12);
+ else
+ VALVE_PORT |= (1 << VALVE0_14);
+}
+
+static void valve1_switch(uint8_t state)
+{
+ VALVE_PORT &= ~((1 << VALVE1_12) | (1 << VALVE1_14));
+ if (state == VALVE_STATE_12)
+ VALVE_PORT |= (1 << VALVE1_12);
+ else
+ VALVE_PORT |= (1 << VALVE1_14);
+}
+
+void valves_global_switch(uint8_t state)
+{
+ switch (state) {
+ case VALVES_IDLE:
+ valve0_switch(VALVE_STATE_12);
+ valve1_switch(VALVE_STATE_12);
+ break;
+ case VALVES_FLOW_IN:
+ valve0_switch(VALVE_STATE_12);
+ valve1_switch(VALVE_STATE_14);
+ break;
+ case VALVES_FLOW_OUT:
+ valve1_switch(VALVE_STATE_12);
+ valve0_switch(VALVE_STATE_14);
+ break;
+ }
+}
+
+void valves_init(void)
+{
+ VALVE_DDR |= (1 << VALVE0_12) | (1 << VALVE0_14) |
+ (1 << VALVE1_12) | (1 << VALVE1_14);
+ valves_global_switch(VALVES_IDLE);
+}
diff --git a/pressure_control/firmware/valves.h b/pressure_control/firmware/valves.h
new file mode 100644
index 0000000..0f51a49
--- /dev/null
+++ b/pressure_control/firmware/valves.h
@@ -0,0 +1,16 @@
+#ifndef VALVES_H_
+#define VALVES_H_
+
+#include <stdint.h>
+
+
+enum valves_global_state {
+ VALVES_IDLE,
+ VALVES_FLOW_IN,
+ VALVES_FLOW_OUT,
+};
+
+void valves_init(void);
+void valves_global_switch(uint8_t state);
+
+#endif /* VALVES_H_ */
bues.ch cgit interface