summaryrefslogtreecommitdiff
path: root/firmware/main.c
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2011-10-13 20:33:30 +0200
committerYves Fischer <yvesf-git@xapek.org>2011-10-13 20:33:30 +0200
commit5d9d373a959271b498710817d08c2ea8e153aa6e (patch)
tree6867e887454a850b4e15616000ef342a1c661c7e /firmware/main.c
parent35a36b0a6c69ce136fd6ea2a203d34e48e1291e8 (diff)
downloadmini-octopus-5d9d373a959271b498710817d08c2ea8e153aa6e.tar.gz
mini-octopus-5d9d373a959271b498710817d08c2ea8e153aa6e.zip
correct firmware??
Diffstat (limited to 'firmware/main.c')
-rw-r--r--firmware/main.c344
1 files changed, 155 insertions, 189 deletions
diff --git a/firmware/main.c b/firmware/main.c
index 6bd7c32..ef802a2 100644
--- a/firmware/main.c
+++ b/firmware/main.c
@@ -16,19 +16,19 @@
#include "protocol.h"
#include "wait.h"
-#include "93c46.h"
+#include "io.h"
#include "adc.h"
-#include "can.h"
-#include "eeprom.h"
#include "i2c.h"
-#include "io.h"
-#include "pwm.h"
#include "spi.h"
+#include "pwm.h"
#include "uart.h"
-#include <stdio.h>
+#include "93c46.h"
#include "debug.h"
+//#define DEBUG 1
+
+
SIGNAL (SIG_INTERRUPT4)
{
@@ -48,218 +48,185 @@ SIGNAL (SIG_OVERFLOW0)
}
-void USBCommandRX (char *buf)
+void
+USBCommandRX (char *buf)
{
int i;
- debugf("receiving command: %X (length: %u)\n", buf[1]);
+#ifdef DEBUG
+ debug_write("get \0");
+ debug_SendHex(buf[1]);
+ debug_write("\r\n");
+#endif
/* a paket can be max 255 bytes */
if (octopus.long_rx_cmd == 1)
- {
- debugf("get next\n");
-
- for (i = 0; i < 64; i++)
- request[octopus.long_rx_index + i] = buf[i];
+ {
+#ifdef DEBUG
+ debug_write("get next\r\n");
+#endif
+ for (i = 0; i < 64; i++)
+ request[octopus.long_rx_index + i] = buf[i];
- octopus.long_rx_index = octopus.long_rx_index + i;
+ octopus.long_rx_index = octopus.long_rx_index + i;
- if (octopus.long_rx_index >= octopus.long_rx_bytes)
- {
- octopus.long_rx_cmd = 0;
- USBMessageIn (request);
- }
- }
+ if (octopus.long_rx_index >= octopus.long_rx_bytes)
+ {
+ octopus.long_rx_cmd = 0;
+ USBMessageIn (request);
+ }
+ }
else
- {
- debugf("get and execute\n");
-
- octopus.long_rx_index = 0;
- for (i = 0; i < 64; i++)
- request[octopus.long_rx_index + i] = buf[i];
-
- octopus.long_rx_index = i;
-
- if ((unsigned int) buf[1] < 64)
- {
- octopus.long_rx_cmd = 0;
- USBMessageIn (request);
- }
- else
- {
- octopus.long_rx_cmd = 1;
- octopus.long_rx_bytes = (unsigned int) buf[1];
- }
- }
+ {
+#ifdef DEBUG
+ debug_write("get and execute\r\n\0");
+#endif
+ octopus.long_rx_index = 0;
+ for (i = 0; i < 64; i++)
+ request[octopus.long_rx_index + i] = buf[i];
+
+ octopus.long_rx_index = i;
+
+ if ((unsigned int) buf[1] < 64)
+ {
+ octopus.long_rx_cmd = 0;
+ USBMessageIn (request);
+ }
+ else
+ {
+ octopus.long_rx_cmd = 1;
+ octopus.long_rx_bytes = (unsigned int) buf[1];
+ }
+ }
}
/* is called when received data from pc */
-void USBMessageIn (char *buf)
+void
+USBMessageIn (char *buf)
{
octopus.long_index = 0;
octopus.long_running = 0;
- if(octopus.status_led)
- STATUS_LED_on;
-
+ STATUS_LED_on;
+#ifdef DEBUG
+ debug_write("parser\r\n\0");
+#endif
int check = ((int) buf[0] >> 4) & 0x0F;
- debugf("parser, check: %X, command: %X\n", check, buf[0]);
-
switch (check)
- {
- case 0:
- if (buf[0] == CMD_EXTERNAL_DEVICE)
- {
- switch (buf[2])
- {
- case CMD_EXTERNAL_93C46:
- flash_93c46_parser(buf);
- break;
- }
- }
- else
- common_parser(buf);
-
- break;
-
- case 1:
- debugf("calling io_parser\n");
- io_parser (buf);
- break;
- case 2:
- debugf("calling adc_parser\n");
- adc_parser (buf);
- break;
- case 3:
- debugf("calling i2c_parser\n");
- i2c_parser (buf);
- break;
- case 4:
- debugf("calling spi_parser\n");
- spi_parser (buf);
- break;
- case 5:
- debugf("calling pwm_parser\n");
- pwm_parser (buf);
- break;
- case 6:
- debugf("calling adc_parser\n");
- uart_parser (buf);
- break;
- case 7:
-#ifdef OCTOPUS_CAN
- debugf("calling can_parser\n");
- can_parser ((uint8_t *)buf);
-#else
- debugf("can't call can_parser - no CAN-support\n");
-#endif
- break;
- case 8:
- debugf("calling eeprom_parser\n");
- eeprom_parser(buf);
- break;
- default:
- debugf("unknown cmd\n");
- answer[0] = buf[0];
- answer[1] = RSP_UNKOWN_CMD;
- answer[2] = 0;
- CommandAnswer(3);
- }
-
- if(octopus.status_led)
- STATUS_LED_off;
-}
-
+ {
+ case 0:
+ if (buf[0] == CMD_EXTERNAL_DEVICE)
+ {
+ switch (buf[2])
+ {
+ case CMD_EXTERNAL_93C46:
+ flash_93c46_parser (buf);
+ break;
+ default:
+ ;
+ }
+ }
+ else
+ common_parser (buf);
+ break;
+ case 1:
+ io_parser (buf);
+ break;
+ case 2:
+ adc_parser (buf);
+ break;
+ case 3:
+ i2c_parser (buf);
+ break;
+ case 4:
+ spi_parser (buf);
+ break;
+ case 5:
+ pwm_parser (buf);
+ break;
+ case 6:
+ uart_parser (buf);
+ break;
+ default:
+ answer[0] = buf[0];
+ answer[1] = RSP_UNKOWN_CMD;
+ answer[2] = 0;
+ CommandAnswer (3);
+ }
-unsigned int strlen(volatile unsigned char *str)
-{
- unsigned int len = 0;
-
- while (*str++)
- len++;
-
- return len;
+ STATUS_LED_off;
}
-void CommandAnswer(unsigned int length)
+void
+CommandAnswer (unsigned int length)
{
int i;
- // if first packet of a long message
+ // if first packet of a lang message
if (length > 64 && octopus.long_running == 0)
- {
- debugf("first packet of a long message (length: %d)\n", length);
- octopus.long_index = 0;
- octopus.long_bytes = length;
- octopus.long_running = 1;
- length = 64;
- }
+ {
+ octopus.long_index = 0;
+ octopus.long_bytes = length;
+ octopus.long_running = 1;
+ length = 64;
+ }
USBNWrite (FIFOTXC1, FLUSH);
for (i = 0; i < length; i++)
- USBNWrite (TXD1, answer[octopus.long_index + i]);
+ USBNWrite (TXD1, answer[octopus.long_index + i]);
/* control togl bit */
if (octopus.datatogl == 1)
- {
- USBNWrite (FIFOTXC1, TX_LAST + TX_EN + TX_TOGL);
- octopus.datatogl = 0;
- }
+ {
+ USBNWrite (FIFOTXC1, TX_LAST + TX_EN + TX_TOGL);
+ octopus.datatogl = 0;
+ }
else
- {
- USBNWrite (FIFOTXC1, TX_LAST + TX_EN);
- octopus.datatogl = 1;
- }
+ {
+ USBNWrite (FIFOTXC1, TX_LAST + TX_EN);
+ octopus.datatogl = 1;
+ }
}
-void CommandAnswerRest (void)
+void
+CommandAnswerRest (void)
{
if (octopus.long_running == 1)
- {
- debugf("octopus long running message\n");
- if (octopus.long_index < octopus.long_bytes)
- {
- debugf("octopus.long_index < octopus.long_bytes\n");
- int diff = octopus.long_bytes - octopus.long_index;
- octopus.long_index = octopus.long_index + 64;
-
- if (diff > 64)
- {
- debugf("not last packet: diff > 64\n");
- CommandAnswer (64);
- }
- else
- {
- debugf("last packet\n");
- /* last packet */
- CommandAnswer (diff);
- octopus.long_running = 0;
- }
- }
- }
+ {
+ if (octopus.long_index < octopus.long_bytes)
+ {
+ int dif = octopus.long_bytes - octopus.long_index;
+ octopus.long_index = octopus.long_index + 64;
+
+ if (dif > 64)
+ CommandAnswer (64);
+
+ else
+ {
+ /* last packet */
+ CommandAnswer (dif);
+ octopus.long_running = 0;
+ }
+ }
+ }
}
-int main(void)
+int
+main (void)
{
- cli();
+ DDRB = 0xFF;
+ PORTB = 0xFF;
- USBNInitMC ();
- USBNStartClock();
-
- DDRB = 0xFF;
- PORTB = 0x00;
-
- /* use status led */
- octopus.status_led = 1;
int interf;
int conf;
+
STATUS_LED_off;
USBNInit ();
@@ -270,19 +237,17 @@ int main(void)
USBNDeviceBCDDevice (0x0001); // you can use it as version e.g. version 1.02
char lang[] = { 0x09, 0x04, 0x00 };
- _USBNAddStringDescriptor (lang); // language descriptor
+ _USBNAddStringDescriptor (lang); // language descriptor
/* Attention!!! Descriptors must be a factor of 8 (error in the stack) */
USBNDeviceManufacture ("EmbeddedProjects");
USBNDeviceProduct ("OctopusUSB Interface Converter and I/O Extension");
- //USBNDeviceProduct ("Octopus USB v3.3");
- USBNDeviceSerialNumber ("20090718");
+ USBNDeviceSerialNumber ("20081108");
conf = USBNAddConfiguration ();
- // we reserve 500mA, because users could drive LEDs and need more power
- USBNConfigurationPower (conf, 200);
+ USBNConfigurationPower (conf, 100);
interf = USBNAddInterface (conf, 0);
USBNAlternateSetting (conf, interf, 0);
@@ -290,32 +255,33 @@ int main(void)
/* communication */
USBNAddInEndpoint (conf, interf, 1, 0x01, BULK, 64, 0, &CommandAnswerRest);
USBNAddOutEndpoint (conf, interf, 1, 0x01, BULK, 64, 0, &USBCommandRX);
- //USBNAddOutEndpoint (conf, interf, 1, 0x02, BULK, 64, 0, NULL);
+
+ USBNInitMC ();
octopus.datatogl = 0;
octopus.long_rx_cmd = 0;
+ /* start usb chip */
+ USBNStart ();
+
/* UARTInit2(38400,8,'N',1); */
- debug_init();
+ #ifdef DEBUG
+ debug_init();
+ #endif
/* hello world led pattern */
- DDRC = 0x7F;
- PORTC = 0x2A;
+ DDRC = 0xFF;
+ PORTC = 0xAA;
delay_250ms ();
- PORTC = 0x55;
+ PORTC = 0xD5;
delay_250ms ();
- PORTC = 0x00;
-
- common_init();
+ PORTC = 0x80;
-
- /* init connection between avr and usbn9604 */
- USBNInitMC ();
- /* start usb chip */
- USBNStart ();
- sei();
+ // testpin
+ //PORTB = 0xFF;
+ //PORTB = 0x00;
while (1);
+
return 0;
}
-