Converted to C++ project
authorGraham <dev@netlinux.org.uk>
Mon, 4 Sep 2017 16:56:04 +0000 (17:56 +0100)
committerGraham <dev@netlinux.org.uk>
Mon, 4 Sep 2017 16:56:04 +0000 (17:56 +0100)
.indent.pro [new file with mode: 0644]
ATmega328P.h [new file with mode: 0644]
Makefile [new file with mode: 0644]
UnoBLDC.cpp [new file with mode: 0644]
arduino.cpp [new file with mode: 0644]
arduino.h [new file with mode: 0644]
uart.cpp [new file with mode: 0644]
uart.h [new file with mode: 0644]

diff --git a/.indent.pro b/.indent.pro
new file mode 100644 (file)
index 0000000..253ccd7
--- /dev/null
@@ -0,0 +1,15 @@
+--blank-lines-after-procedures
+--braces-after-if-line
+--dont-cuddle-else
+--dont-break-procedure-type
+--indent-level4
+--indent-label0
+--no-tabs
+--brace-indent0
+--tab-size4
+--blank-lines-after-commas
+--ignore-newlines
+--preprocessor-indentation1
+--leave-optional-blank-lines
+--case-indentation4
+--line-length120
diff --git a/ATmega328P.h b/ATmega328P.h
new file mode 100644 (file)
index 0000000..874364f
--- /dev/null
@@ -0,0 +1,105 @@
+// For page references see p423 (Register Summary)
+
+// For Timers 0, 1 and 2
+
+// Interrupt mask
+#define TIMSKxTOIE      0b00000001  // Overflow interrupt enable
+#define TIMSKxOCIEA     0b00000010  // Output interrupt enable
+#define TIMSKxOCIEB     0b00000100  // Output interrupt enable
+
+// Tabel 14-2 to 14-6 p106, 15-1 to 15-3 p134, 17-2 to 17-7 p158 : On Compare Match
+#define TCCRxA_COMA_NC      0b00000000
+#define TCCRxA_COMA_TOGGLE  0b01000000
+#define TCCRxA_COMA_CLEAR   0b10000000
+#define TCCRxA_COMA_SET     0b11000000
+#define TCCRxA_COMB_NC      0b00000000
+#define TCCRxA_COMB_TOGGLE  0b00010000
+#define TCCRxA_COMB_CLEAR   0b00100000
+#define TCCRxA_COMB_SET     0b00110000
+
+
+// p143 (Timer0/1) and p165 (Timer2) : Timer reset
+#define GTCCR_TSM_ON    0b10000000  // Set to hold timers/Reset to release
+#define GTCCR_PSRASY    0b00000010  // Reset Timer0/1 (Bit set/reset by hardware)
+#define GTCCR_PSRSYNC   0b00000001  // Reset Timer2 (Bit set/reset by hardware)
+
+// For Timers 0 and 1
+
+//  Table 14-8 p110 and 15-5 p137 : Prescaler
+#define CSx_STOP    0b00000000
+#define CSx_NOSCALE 0b00000001
+#define CSx_0008    0b00000010
+#define CSx_0064    0b00000011
+#define CSx_0256    0b00000100
+#define CSx_1024    0b00000101
+#define CSx_EXTFALL 0b00000110
+#define CSx_EXTRISE 0b00000111
+
+//  Table 14-8 p108 : Mode
+#define TCCRxA_MODE_0_NORMAL                   0b00000000
+#define TCCRxB_MODE_0_NORMAL                   0b00000000
+#define TCCRxA_MODE_1_PWM_PHASE_CORRECT            0b00000001
+#define TCCRxB_MODE_1_PWM_PHASE_CORRECT            0b00000000
+#define TCCRxA_MODE_2_CTC                          0b00000010
+#define TCCRxB_MODE_2_CTC                          0b00000000
+#define TCCRxA_MODE_3_FAST_PWM                 0b00000011
+#define TCCRxB_MODE_3_FAST_PWM                 0b00000000
+#define TCCRxA_MODE_4_RESERVED                 0b00000000
+#define TCCRxB_MODE_4_RESERVED                 0b00001000
+#define TCCRxA_MODE_5_PWM_PHASE_CORRECT_OCR 0b00000001
+#define TCCRxB_MODE_5_PWM_PHASE_CORRECT_OCR    0b00001000
+#define TCCRxA_MODE_6_RESERVED                 0b00000010
+#define TCCRxB_MODE_6_RESERVED                 0b00001000
+#define TCCRxA_MODE_7_FAST_PWM_OCR          0b00000011
+#define TCCRxB_MODE_7_FAST_PWM_OCR          0b00001000
+
+// For Timer 1
+
+// Interrupt mask
+#define TIMSK1ICIE      0b00100000  // Input interrupt enable
+
+// Table 15-4 p136 : Mode
+#define TCCR1A_MODE_0_NORMAL                        0b00000000
+#define TCCR1B_MODE_0_NORMAL                           0b00000000
+#define TCCR1A_MODE_1_PWM_PHASE_CORRECT_8BIT        0b00000001
+#define TCCR1B_MODE_1_PWM_PHASE_CORRECT_8BIT        0b00000000
+#define TCCR1A_MODE_2_PWM_PHASE_CORRECT_9BIT        0b00000010
+#define TCCR1B_MODE_2_PWM_PHASE_CORRECT_9BIT        0b00000000
+#define TCCR1A_MODE_3_PWM_PHASE_CORRECT_10BIT          0b00000011
+#define TCCR1B_MODE_3_PWM_PHASE_CORRECT_10BIT          0b00000000
+#define TCCR1A_MODE_4_CTC_OCR                          0b00000000
+#define TCCR1B_MODE_4_CTC_OCR                       0b00001000
+#define TCCR1A_MODE_5_FAST_PWM_8BIT                        0b00000001
+#define TCCR1B_MODE_5_FAST_PWM_8BIT                        0b00001000
+#define TCCR1A_MODE_6_FAST_PWM_9BIT                 0b00000010
+#define TCCR1B_MODE_6_FAST_PWM_9BIT                 0b00001000
+#define TCCR1A_MODE_7_FAST_PWM_10BIT                0b00000011
+#define TCCR1B_MODE_7_FAST_PWM_10BIT                0b00001000
+#define TCCR1A_MODE_8_PWM_PHASE_FREQ_CORRECT_ICR       0b00000000
+#define TCCR1B_MODE_8_PWM_PHASE_FREQ_CORRECT_ICR       0b00010000
+#define TCCR1A_MODE_9_PWM_PHASE_FREQ_CORRECT_OCR       0b00000001
+#define TCCR1B_MODE_9_PWM_PHASE_FREQ_CORRECT_OCR       0b00010000
+#define TCCR1A_MODE_10_PWM_PHASE_CORRECT_ICR        0b00000010
+#define TCCR1B_MODE_10_PWM_PHASE_CORRECT_ICR        0b00010000
+#define TCCR1A_MODE_11_PWM_PHASE_CORRECT_OCR        0b00000011
+#define TCCR1B_MODE_11_PWM_PHASE_CORRECT_OCR        0b00010000
+#define TCCR1A_MODE_12_CTC_ICR                      0b00000000
+#define TCCR1B_MODE_12_CTC_ICR                      0b00011000
+#define TCCR1A_MODE_13_RESERVED                                0b00000001
+#define TCCR1B_MODE_13_RESERVED                                0b00011000
+#define TCCR1A_MODE_14_FAST_PWM_ICR                 0b00000010
+#define TCCR1B_MODE_14_FAST_PWM_ICR                 0b00011000
+#define TCCR1A_MODE_15_FAST_PWM_OCR                 0b00000011
+#define TCCR1B_MODE_15_FAST_PWM_OCR                 0b00011000
+
+// For Timer 2
+
+//  Table 17-9 p162 : Prescaler
+#define CS2_STOP    0b00000000
+#define CS2_NOSCALE 0b00000001
+#define CS2_0008    0b00000010
+#define CS2_0032    0b00000011
+#define CS2_0064    0b00000100
+#define CS2_0128    0b00000101
+#define CS2_0256    0b00000110
+#define CS2_1024    0b00000111
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..be7a89a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,80 @@
+
+TARGET = UnoBLDC
+SRC = $(TARGET).cpp arduino.cpp uart.cpp
+
+SHELL = sh
+CC = avr-gcc
+OBJCOPY = avr-objcopy
+
+AVRDUDE = avrdude
+
+OBJ = $(SRC:.cpp=.o)
+LST = $(SRC:.cpp=.lst)
+
+MCU = atmega328p
+F_CPU = 16000000
+FORMAT = ihex
+
+OPT = 3
+CSTANDARD = -std=gnu++0x
+
+CDEFS = -DF_CPU=$(F_CPU)UL
+
+CFLAGS += $(CDEFS)
+CFLAGS += -O$(OPT)
+CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
+CFLAGS += -Wall
+CFLAGS += -Wa,-adhlns=$(<:.cpp=.lst)
+CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
+CFLAGS += $(CSTANDARD)
+
+ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
+
+LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
+LDFLAGS += $(EXTMEMOPTS)
+
+AVRDUDE_PROGRAMMER = arduino
+AVRDUDE_PORT = /dev/ttyACM?    # programmer connected to Arduino
+#AVRDUDE_PORT = /dev/ttyUSB?    # programmer connected to UB232
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
+AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -b 115200
+AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
+AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
+AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
+
+# Default target.
+all: clean program pretty term
+
+clean:
+       rm -vf *.elf *.hex *.o *.map *.lst
+
+%.o : %.cpp
+       @echo
+       @echo $(MSG_COMPILING) $<
+       $(CC) -c $(ALL_CFLAGS) $< -o $@
+
+$(TARGET).elf: $(OBJ)
+       @echo
+       @echo $(MSG_LINKING) $@
+       $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
+
+$(TARGET).hex: $(TARGET).elf
+       @echo
+       @echo $(MSG_FLASH) $@
+       $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
+
+build: $(TARGET).hex
+
+program: build
+       $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
+
+pretty:
+       indent *.cpp *.h
+       sed -i 's/0 b/0b/g' *.cpp *.h
+
+run:
+       ls $(AVRDUDE_PORT)
+       mv -vf minicom.log minicom-`date +%N`.log || true
+       minicom -o -D $(AVRDUDE_PORT) -C minicom.log
+
+.PHONY: run pretty
diff --git a/UnoBLDC.cpp b/UnoBLDC.cpp
new file mode 100644 (file)
index 0000000..945b2d6
--- /dev/null
@@ -0,0 +1,158 @@
+#include "arduino.h"
+
+#define TRINARY
+#define STEP 12
+
+static const int MARKCONTROL = A0;
+static const int SPACECONTROL = A1;
+static int MarkInput = 0;
+static int SpaceInput = 0;
+static unsigned char StepCount = 0;
+
+#if STEP == 6
+
+static const unsigned char Step[] = {
+    0b010010,
+    0b011000,
+    0b001001,
+    0b100001,
+    0b100100,
+    0b000110
+};
+
+static const unsigned char Polarity[] = {
+    0b001,
+    0b010,
+    0b010,
+    0b100,
+    0b100,
+    0b001
+};
+
+static const unsigned char On[] = {
+    0b101,
+    0b110,
+    0b011,
+    0b101,
+    0b110,
+    0b011
+};
+
+#elif STEP == 12
+
+static const unsigned char Step[] = {
+    0b101001,
+    0b100001,
+    0b100101,
+    0b100100,
+    0b100110,
+    0b000110,
+    0b010110,
+    0b010010,
+    0b011010,
+    0b011000,
+    0b011001,
+    0b001001
+};
+
+static const unsigned char Polarity[] = {
+    0b110,
+    0b100,
+    0b100,
+    0b100,
+    0b101,
+    0b001,
+    0b001,
+    0b001,
+    0b011,
+    0b010,
+    0b010,
+    0b010
+};
+
+static const unsigned char On[] = {
+    0b111,
+    0b101,
+    0b111,
+    0b110,
+    0b111,
+    0b011,
+    0b111,
+    0b101,
+    0b111,
+    0b110,
+    0b111,
+    0b011
+};
+
+#endif
+void UARTReceive (char Character)
+{
+}
+
+static const unsigned char StepNumber = sizeof (Step);
+
+void setup ()
+{
+    puts ("Grahams Simple Motor Test, " __DATE__ " (" __TIME__ ")");
+
+#ifdef TRINARY
+    DDRB = 0b00000000;         // set 8-13 off (input)
+    PORTB = 0b00000000;        // set 8-13 low (no pullup)
+#else
+    DDRB = 0b00111111;         // set 8-13 output
+#endif
+}
+
+static const unsigned long DELAY = 100;
+
+void Gap (const int CONTROL, int &Input)
+{
+    Input = analogRead (CONTROL);
+
+    for (volatile unsigned long intCount = DELAY * Input; intCount > 0; intCount--);
+}
+
+void loop ()
+{
+#if 1
+    if (StepCount >= StepNumber)
+        StepCount = 0;
+
+# ifdef TRINARY
+    DDRB = (On[StepCount] << 3);    // set the direction
+    PORTB = (~Polarity[StepCount] << 3);    // set the bits
+# else
+    PORTB = Step[StepCount];    // set the bits
+# endif
+    Gap (MARKCONTROL, MarkInput);
+
+    printf ("Step = ");
+
+    for (unsigned char BitCount = 0b100; BitCount > 0; BitCount >>= 1)
+    {
+        if ((On[StepCount] & BitCount) > 0)
+            puts (((Polarity[StepCount] & BitCount) > 0) ? "+" : "-");
+        else
+            puts (" ");
+    }
+
+    printf ("\t");
+    printf ("MarkInput = ");
+    printf ("%d\n", MarkInput);
+
+    printf ("\t");
+    printf ("SpaceInput = ");
+    printf ("%d\n", SpaceInput);
+
+# if 1
+#  ifdef TRINARY
+    DDRB = 0b00000000;         // set the direction
+#  endif
+    PORTB = 0b00000000;        // set the LED off
+    Gap (SPACECONTROL, SpaceInput);
+# endif
+
+    StepCount++;
+#endif
+}
diff --git a/arduino.cpp b/arduino.cpp
new file mode 100644 (file)
index 0000000..724ccce
--- /dev/null
@@ -0,0 +1,108 @@
+#include "arduino.h"
+
+#ifndef F_CPU
+# define F_CPU 16000000UL
+#endif
+
+#define ADSC_SET _BV(ADSC)
+
+unsigned short analogRead (const unsigned char channel)
+{
+    ADMUX &= 0b11111000;
+    ADMUX |= channel;
+
+    ADCSRA |= ADSC_SET;
+
+    while ((ADCSRA & ADSC_SET))
+        ;
+
+    return ADC;
+}
+
+void analogWrite (const unsigned char channel, const unsigned char pwm)
+{
+    if (pwm == 0)
+    {
+        TCCR2A &= ~_BV (COM2B1);    // Turn off PWM
+        PORTD &= ~0b00001000;  // Set low
+
+        return;
+    }
+    else if (pwm == 255)
+    {
+        TCCR2A &= ~_BV (COM2B1);    // Turn off PWM
+        PORTD |= 0b00001000;   // Set high
+
+        return;
+    }
+    else
+    {
+        TCCR2A |= _BV (COM2B1);
+    }
+
+    switch (channel)
+    {
+        case 3:
+            OCR2B = pwm;
+            break;
+        case 5:
+            OCR0B = pwm;
+            break;
+        case 6:
+            OCR0A = pwm;
+            break;
+        case 9:
+            OCR1A = pwm;
+            break;
+        case 10:
+            OCR1B = pwm;
+            break;
+        case 11:
+            OCR2A = pwm;
+            break;
+        default:
+            printf ("No PWM on %d\n", channel);
+    }
+}
+
+void init ()
+{
+    UARTInit ();
+
+//-- Control ADC interrupt
+    ADCSRA |= _BV (ADPS2) | _BV (ADPS1) | _BV (ADPS0);
+    ADCSRA |= _BV (ADEN);
+    ADMUX |= _BV (REFS0);
+
+#if 0
+    // No PWM on PortD or PortB
+#elif 1
+    TCCR2A |= _BV (COM2B1) | _BV (WGM21) | _BV (WGM20); // Fast PWM on port D pin 3
+    TCCR2B |= _BV (CS20);
+#elif 0
+    TCCR2A |= _BV (COM2A1) | _BV (WGM20);   // Aligned PWM on port B pin 3
+    TCCR2B |= _BV (CS20);
+#elif 0
+    TCCR2A |= _BV (COM2A1) | _BV (WGM21) | _BV (WGM20); // Fast PWM on port B pin 3
+    TCCR2B |= _BV (CS20);
+#elif 0
+    TCCR2A |= _BV (COM2B1) | _BV (WGM20);   // Aligned PWM on port D pin 3
+    TCCR2B |= _BV (CS20);
+#endif
+//    DDRB |= _BV (PB3);          // pin 11 as output;
+    DDRD |= _BV (PD3);          // pin 3 as output;
+}
+
+int main ()
+{
+    init ();
+
+    setup ();
+
+    sei ();
+
+    for (;;)
+        loop ();
+
+    return 0;
+}
diff --git a/arduino.h b/arduino.h
new file mode 100644 (file)
index 0000000..fe7150c
--- /dev/null
+++ b/arduino.h
@@ -0,0 +1,28 @@
+#ifndef ARDUINO_H
+# define ARDUINO_H
+
+# include <stdio.h>
+# include <string.h>
+# include <stdlib.h>
+
+# include <util/delay.h>
+# include "uart.h"
+
+# define delay _delay_ms
+
+static const unsigned char A0 = 0;
+static const unsigned char A1 = 1;
+static const unsigned char A2 = 2;
+static const unsigned char A3 = 3;
+static const unsigned char A4 = 4;
+static const unsigned char A5 = 5;
+
+unsigned short analogRead (const unsigned char adcx);
+void analogWrite (const unsigned char pin, const unsigned char pwm);
+
+extern void setup ();
+extern void loop ();
+
+extern void UARTReceive (char);
+
+#endif // ARDUINO_H
diff --git a/uart.cpp b/uart.cpp
new file mode 100644 (file)
index 0000000..160dd3a
--- /dev/null
+++ b/uart.cpp
@@ -0,0 +1,71 @@
+/*
+ * Demonstration on how to redirect stdio to UART. 
+ *
+ * http://appelsiini.net/2011/simple-usart-with-avr-libc
+ *
+ * To compile and upload run: make clean; make; make program;
+ * Connect to serial with: screen /dev/tty.usbserial-*
+ *
+ * Copyright 2011 Mika Tuupola
+ *
+ * Licensed under the MIT license:
+ *   http://www.opensource.org/licenses/mit-license.php
+ *
+ */
+#include <stdio.h>
+
+#include "uart.h"
+
+#ifndef F_CPU
+# define F_CPU 16000000UL
+#endif
+
+#ifndef BAUD
+# define BAUD 115200
+#endif
+// Increasing BAUD tolerance to remove warning
+//  http://www.elektroda.pl/rtvforum/topic2639230.html (Polish)
+//#define BAUD_TOL 4
+#include <util/setbaud.h>
+
+/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
+
+ISR (USART_RX_vect)
+{
+    UARTReceive (UDR0);
+}
+
+static inline int UARTPutchar (char c, FILE * stream)
+{
+    if (c == '\n')
+        UARTPutchar ('\r', stream);
+
+    loop_until_bit_is_set (UCSR0A, UDRE0);
+    UDR0 = c;
+
+    return 0;
+}
+
+static FILE UARTOutput;
+
+void UARTInit (void)
+{
+    UBRR0H = UBRRH_VALUE;
+    UBRR0L = UBRRL_VALUE;
+
+#if USE_2X
+    UCSR0A |= _BV (U2X0);
+#else
+    UCSR0A &= ~(_BV (U2X0));
+#endif
+
+    UCSR0C = _BV (UCSZ01) | _BV (UCSZ00);   /* 8-bit data */
+    UCSR0B = _BV (RXEN0) | _BV (TXEN0); /* Enable RX and TX */
+
+    UCSR0B |= _BV (RXCIE0);     /* Enable RX interrupt */
+
+    fdev_setup_stream (&UARTOutput, UARTPutchar, NULL, _FDEV_SETUP_WRITE);
+
+//    stderr = &UARTOutput;
+    stdout = &UARTOutput;
+}
diff --git a/uart.h b/uart.h
new file mode 100644 (file)
index 0000000..de6ebb0
--- /dev/null
+++ b/uart.h
@@ -0,0 +1,11 @@
+#ifndef UART_H
+# define UART_H
+
+# include <avr/io.h>
+# include <avr/interrupt.h>
+
+extern void UARTReceive (char chrReceived);
+
+void UARTInit (void);
+
+#endif /* UART_H */