+++ /dev/null
-##\r
-## spreadspace avr utils\r
-##\r
-##\r
-## Copyright (C) 2012 Christian Pointner <equinox@spreadspace.org>\r
-##\r
-## This file is part of spreadspace avr utils.\r
-##\r
-## spreadspace avr utils is free software: you can redistribute it and/or modify\r
-## it under the terms of the GNU General Public License as published by\r
-## the Free Software Foundation, either version 3 of the License, or\r
-## any later version.\r
-##\r
-## spreadspace avr utils is distributed in the hope that it will be useful,\r
-## but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-## GNU General Public License for more details.\r
-##\r
-## You should have received a copy of the GNU General Public License\r
-## along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.\r
-##\r
-\r
-NAME := r3cam-steppermotor\r
-BOARD_TYPE := minimus32\r
-OBJ := $(NAME).o\r
-LIBS := util led lufa-descriptor-usbserial\r
-EXTERNAL_LIBS := lufa\r
-\r
-LUFA_PATH := ../contrib/LUFA-120219\r
-LUFA_OPTS = -D USB_DEVICE_ONLY\r
-LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0\r
-LUFA_OPTS += -D ORDERED_EP_CONFIG\r
-LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8\r
-LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1\r
-LUFA_OPTS += -D USE_FLASH_DESCRIPTORS\r
-LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"\r
-LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT\r
-\r
-LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\""\r
-LUFA_OPTS += -D USB_PRODUCT="L\"$(NAME)\""\r
-\r
-include ../include.mk\r
+++ /dev/null
-/*\r
- * spreadspace avr utils\r
- *\r
- *\r
- * Copyright (C) 2012 Christian Pointner <equinox@spreadspace.org>\r
- *\r
- * This file is part of spreadspace avr utils.\r
- *\r
- * spreadspace avr utils is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * any later version.\r
- *\r
- * spreadspace avr utils is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-\r
-\r
-#include <avr/io.h>\r
-#include <avr/wdt.h>\r
-#include <avr/interrupt.h>\r
-#include <avr/power.h>\r
-\r
-#include "util.h"\r
-#include "led.h"\r
-\r
-/*\r
- LUFA Library\r
- Copyright (C) Dean Camera, 2012.\r
-\r
- dean [at] fourwalledcubicle [dot] com\r
- www.lufa-lib.org\r
-*/\r
-#include <LUFA/Drivers/USB/USB.h>\r
-#include "lufa-descriptor-usbserial.h"\r
-\r
-USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =\r
- {\r
- .Config =\r
- {\r
- .ControlInterfaceNumber = 0,\r
-\r
- .DataINEndpointNumber = CDC_TX_EPNUM,\r
- .DataINEndpointSize = CDC_TXRX_EPSIZE,\r
- .DataINEndpointDoubleBank = false,\r
-\r
- .DataOUTEndpointNumber = CDC_RX_EPNUM,\r
- .DataOUTEndpointSize = CDC_TXRX_EPSIZE,\r
- .DataOUTEndpointDoubleBank = false,\r
-\r
- .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,\r
- .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,\r
- .NotificationEndpointDoubleBank = false,\r
- },\r
- };\r
-\r
-void EVENT_USB_Device_ConfigurationChanged(void)\r
-{\r
- CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);\r
-}\r
-\r
-void EVENT_USB_Device_ControlRequest(void)\r
-{\r
- CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);\r
-}\r
-/* end LUFA CDC-ACM specific definitions*/\r
-\r
-\r
-/* Start Our Code */\r
-\r
-#define M_PORT PORTB\r
-#define M_DDR DDRB\r
-#define M_DIRECTION 0\r
-#define M_CLK 1\r
-#define M_ENABLE 2\r
-#define M_RESET 3\r
-#define M_FULLSTEPS 4\r
-\r
-#define M_START_DIVISOR 2;\r
-\r
-static uint8_t cur_speed = 0xFF - M_START_DIVISOR;\r
-static uint8_t m_clk_divisor_ = M_START_DIVISOR;\r
-static uint8_t m_clk_divisor_counter_ = 0;\r
-static uint16_t m_steps_to_go_ = 0;\r
-static uint16_t m_steps_to_go_back_ = 0;\r
-\r
-inline void m_timer_enable(void)\r
-{\r
- TIMSK0 |= (1<<TOIE0); \r
-}\r
-\r
-inline void m_timer_disable(void)\r
-{\r
- TIMSK0 &= ~(1<<TOIE0); \r
-}\r
-\r
-void motor_stop(void)\r
-{\r
- m_timer_disable();\r
- M_PORT &= ~(1 << M_ENABLE);\r
- m_steps_to_go_ = 0;\r
- m_steps_to_go_back_ = 0;\r
-}\r
-\r
-ISR(TIMER0_OVF_vect)\r
-{\r
- //if (m_steps_to_go_ == 0)\r
- //{\r
- //if(m_steps_to_go_back_)\r
- //{ \r
- // M_PORT ^= (1 << M_DIRECTION);\r
- // m_steps_to_go_=m_steps_to_go_back_;\r
- //m_steps_to_go_back_=0;\r
- //} else \r
- // motor_stop();\r
- //} \r
- if (m_clk_divisor_counter_ == 0)\r
- {\r
- m_clk_divisor_counter_ = m_clk_divisor_;\r
- M_PORT ^= (1 << M_CLK);\r
- m_steps_to_go_--;\r
- }\r
- else \r
- m_clk_divisor_counter_--;\r
-}\r
-\r
-static char set_speed_msg[] = "m_clk_divisor_ = %d\n\r";\r
-\r
-void motor_set_speed(uint8_t speed)\r
-{\r
- m_clk_divisor_ = 0xFF - speed;\r
- char tmp[sizeof(set_speed_msg)+4];\r
- sprintf(tmp, set_speed_msg,m_clk_divisor_);\r
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, tmp);\r
-}\r
-\r
-void motor_run(uint16_t steps, uint8_t direction,uint16_t steps_back)\r
-{\r
- //reset by pulling reset low for 100ms\r
- M_PORT &= ~(1 << M_RESET);\r
- _delay_ms(100);\r
- M_PORT |= (1 << M_RESET);\r
- \r
- m_clk_divisor_counter_ = 0;\r
- m_steps_to_go_ = steps;\r
- m_steps_to_go_back_ = steps_back;\r
- \r
- if (direction)\r
- M_PORT |= (1 << M_DIRECTION);\r
- else\r
- M_PORT &= ~(1 << M_DIRECTION);\r
- \r
- //enable motor\r
- M_PORT |= (1 << M_ENABLE);\r
- m_timer_enable(); \r
-}\r
-\r
-void init_pins()\r
-{\r
- M_DDR = 0x0F;\r
- M_PORT = (1 << M_RESET) | (1 << M_FULLSTEPS);\r
- \r
- // Configure timer 0 to generate a timer overflow interrupt every\r
- // 560us (NEC Protocol)\r
- TCCR0A = 0x00;\r
- TIMSK0 = (0<<TOIE0);\r
- //TCCR0B = 1<<WGM02 | 1<<CS02 | 0<<CS01| 1<<CS00; //Teiler 1024\r
- TCCR0B = 1<<WGM02 | 1<<CS02 | 0<<CS01| 0<<CS00; //Teiler 256\r
- //OCR0A = 139; // (1+139)*8 = 1120 -> 70us @ 16 MHz -> 1*alpha\r
- OCR0A = 255; // (1+139)*8 = 1120 -> 70us @ 16 MHz -> 1*alpha\r
-}\r
-\r
-void handle_cmd(uint8_t cmd)\r
-{\r
- switch(cmd) {\r
- case '0': led_off(); break;\r
- case '1': led_on(); break;\r
- case 't': led_toggle(); break;\r
- case 'r': reset2bootloader(); break;\r
- case 's': motor_stop(); break;\r
- case 'y': motor_run(30,0,20); break;\r
- //case 'x': motor_run(140,0,40); break;\r
- //case 'c': motor_run(60,0,20); break;\r
- //case 'v': motor_run(40,0,20); break;\r
- //case 'q': motor_run(140,1,40); break;\r
- //case 'w': motor_run(60,1,20); break;\r
- //case 'e': motor_run(40,1,20); break;\r
- //case 'C': motor_run(330,0,40); break;\r
- //case 'W': motor_run(330,1,40); break;\r
- case '+': motor_set_speed(++cur_speed); break;\r
- case '-': motor_set_speed(--cur_speed); break;\r
- default: CDC_Device_SendString(&VirtualSerial_CDC_Interface, "error\n\r"); return;\r
- }\r
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "ok\n\r");\r
-}\r
-\r
-\r
-\r
-int main(void)\r
-{\r
- MCUSR &= ~(1 << WDRF);\r
- wdt_disable();\r
-\r
- cpu_init();\r
- led_init();\r
- USB_Init();\r
- init_pins();\r
- sei();\r
-\r
- for(;;) {\r
- int16_t BytesReceived = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);\r
- while(BytesReceived > 0) {\r
- int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);\r
- if(!(ReceivedByte < 0)) {\r
- handle_cmd(ReceivedByte);\r
- }\r
- BytesReceived--;\r
- }\r
-\r
- CDC_Device_USBTask(&VirtualSerial_CDC_Interface);\r
- USB_USBTask();\r
- }\r
-}\r