summaryrefslogtreecommitdiff
path: root/digital/dev2
diff options
context:
space:
mode:
Diffstat (limited to 'digital/dev2')
-rw-r--r--digital/dev2/src/common/usb_isp.c119
-rw-r--r--digital/dev2/src/common/usb_isp.h31
-rw-r--r--digital/dev2/src/usb_serial_isp/Makefile4
-rw-r--r--digital/dev2/src/usb_serial_isp/avrconfig.h40
-rw-r--r--digital/dev2/src/usb_serial_isp/main.c44
5 files changed, 192 insertions, 46 deletions
diff --git a/digital/dev2/src/common/usb_isp.c b/digital/dev2/src/common/usb_isp.c
new file mode 100644
index 00000000..8494fcab
--- /dev/null
+++ b/digital/dev2/src/common/usb_isp.c
@@ -0,0 +1,119 @@
+/* usb_isp.c */
+/* dev2 - Multi-purpose development board using USB and Ethernet. {{{
+ *
+ * Copyright (C) 2009 Nicolas Schodet
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+#include "common.h"
+
+#include "io.h"
+
+#include "usb_isp.h"
+#include "descriptors.h"
+
+#include "modules/isp/isp.h"
+#include "modules/isp/isp_frame.h"
+#include "modules/isp/isp_proto.h"
+#include "modules/spi/spi.h"
+#include "modules/usb/usb.h"
+#include "modules/utils/utils.h"
+
+#define RESET D, 4
+#define SCK AC_SPI0_SOFT_SCK_IO
+
+static uint8_t usb_isp_sent;
+
+void
+usb_isp_send_char (uint8_t c)
+{
+ Endpoint_SelectEndpoint (ISP_TX_EPNUM);
+ /* Wait endpoint to become ready. */
+ while (!Endpoint_ReadWriteAllowed ())
+ ;
+ Endpoint_Write_Byte (c);
+ /* If at end of endpoint buffer, send. */
+ if (!Endpoint_ReadWriteAllowed ())
+ Endpoint_ClearCurrentBank ();
+ /* Select back RX endpoint. */
+ Endpoint_SelectEndpoint (ISP_RX_EPNUM);
+ /* Will need extra clear at end of all transfers. */
+ usb_isp_sent = 1;
+}
+
+void
+usb_isp_spi_enable (void)
+{
+ /* Reset slave AVR. */
+ IO_DDR (RESET) |= IO_BV (RESET);
+ /* Set SCK to low. */
+ IO_DDR (SCK) |= IO_BV (SCK);
+ /* Reset pulse, at least 2 clock cycle later, be conservative. */
+ utils_delay_ms (1);
+ IO_PORT (RESET) |= IO_BV (RESET);
+ IO_PORT (RESET) &= ~IO_BV (RESET);
+ /* Enable SPI. */
+ spi_init (SPI_MASTER, SPI_MODE_0, SPI_MSB_FIRST, isp_proto_sck_duration);
+}
+
+void
+usb_isp_spi_disable (void)
+{
+ /* Disable SPI. */
+ spi_uninit ();
+ /* Release reset, SCK is handled by SPI driver. */
+ IO_DDR (RESET) &= ~IO_BV (RESET);
+}
+
+void
+usb_isp_spi_sck_pulse (void)
+{
+ IO_PORT (SCK) ^= IO_BV (SCK);
+ IO_PORT (SCK) ^= IO_BV (SCK);
+}
+
+uint8_t
+usb_isp_spi_tx (uint8_t data)
+{
+ return spi_send_and_recv (data);
+}
+
+void
+usb_isp_task (void)
+{
+ Endpoint_SelectEndpoint (ISP_RX_EPNUM);
+ /* If data is available from USB: */
+ if (Endpoint_ReadWriteAllowed ())
+ {
+ /* Read as much as possible, and clear endpoint. */
+ do {
+ isp_frame_accept_char (Endpoint_Read_Byte ());
+ } while (Endpoint_ReadWriteAllowed ());
+ Endpoint_ClearCurrentBank ();
+ }
+ /* If data has been sent, sent a ZLP or finalise last packet. */
+ if (usb_isp_sent)
+ {
+ Endpoint_SelectEndpoint (ISP_TX_EPNUM);
+ Endpoint_ClearCurrentBank ();
+ usb_isp_sent = 0;
+ }
+}
+
diff --git a/digital/dev2/src/common/usb_isp.h b/digital/dev2/src/common/usb_isp.h
new file mode 100644
index 00000000..ff3840f4
--- /dev/null
+++ b/digital/dev2/src/common/usb_isp.h
@@ -0,0 +1,31 @@
+#ifndef usb_isp_h
+#define usb_isp_h
+/* usb_isp.h */
+/* dev2 - Multi-purpose development board using USB and Ethernet. {{{
+ *
+ * Copyright (C) 2009 Nicolas Schodet
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+
+void
+usb_isp_task (void);
+
+#endif /* usb_isp_h */
diff --git a/digital/dev2/src/usb_serial_isp/Makefile b/digital/dev2/src/usb_serial_isp/Makefile
index d2124ae1..6034b9b0 100644
--- a/digital/dev2/src/usb_serial_isp/Makefile
+++ b/digital/dev2/src/usb_serial_isp/Makefile
@@ -1,7 +1,7 @@
BASE = ../../../avr
AVR_PROGS = dev2_serial_isp
-dev2_serial_isp_SOURCES = main.c descriptors.c serial.c select.c
-MODULES = usb uart isp
+dev2_serial_isp_SOURCES = main.c descriptors.c serial.c select.c usb_isp.c
+MODULES = usb uart isp spi
CONFIGFILE = avrconfig.h
AVR_MCU = at90usb162
# -O2 : speed
diff --git a/digital/dev2/src/usb_serial_isp/avrconfig.h b/digital/dev2/src/usb_serial_isp/avrconfig.h
index 37b1b21e..1dfd63d2 100644
--- a/digital/dev2/src/usb_serial_isp/avrconfig.h
+++ b/digital/dev2/src/usb_serial_isp/avrconfig.h
@@ -73,15 +73,51 @@
#define AC_UART1_SEND_BUFFER_FULL WAIT
#define AC_UART1_HOST_DRIVER PTS
+/* spi - SPI module. */
+/** Select driver: HARD, SOFT, or NONE. */
+#define AC_SPI0_DRIVER SOFT
+/** For software SPI, specify SCK IO port. */
+#define AC_SPI0_SOFT_SCK_IO D, 5
+/** For software SPI, specify MOSI IO port. */
+#define AC_SPI0_SOFT_MOSI_IO D, 7
+/** For software SPI, specify MISO IO port. */
+#define AC_SPI0_SOFT_MISO_IO D, 6
+/** Same thing for an optionnal second SPI driver. */
+#define AC_SPI1_DRIVER NONE
+
/* isp - ISP module. */
/** Size of isp_frame buffer. */
#define AC_ISP_FRAME_BUFFER_SIZE 275
/** Should be implemented by the user to send a character. */
-#define AC_ISP_FRAME_SEND_CHAR isp_send_char
+#define AC_ISP_FRAME_SEND_CHAR usb_isp_send_char
/** Should be implemented by the user (isp_proto) to accept a frame. */
#define AC_ISP_FRAME_ACCEPT_FRAME isp_proto_accept
-/** Should be implemeted by the user to send a frame. */
+/** Should be implemented by the user to send a frame. */
#define AC_ISP_PROTO_SEND isp_frame_send_frame
+/** Programmer signature. */
+#define AC_ISP_PROTO_SIGNATURE "APBisp_2"
+/** Programmer build number. */
+#define AC_ISP_PROTO_BUILD_NUMBER 0x0100
+/** Programmer hardware version. */
+#define AC_ISP_PROTO_HW_VERSION 0x02
+/** Programmer software version. */
+#define AC_ISP_PROTO_SW_VERSION 0x0204
+/** Should be implemented by the user to enable SPI programming:
+ * - set RESET and SCK to low,
+ * - power on, or if not possible, do a positive RESET pulse,
+ * - enable SPI. */
+#define AC_ISP_SPI_ENABLE usb_isp_spi_enable
+/** Should be implemented by the user to disable SPI programming:
+ * - disable SPI,
+ * - release RESET,
+ * - power off if desired. */
+#define AC_ISP_SPI_DISABLE usb_isp_spi_disable
+/** Should be implemented by the user to do a pulse on SCK. This is used to
+ * try to resynchronise. */
+#define AC_ISP_SPI_SCK_PULSE usb_isp_spi_sck_pulse
+/** Should be implemented by the user to send and receive a byte using the SPI
+ * bus. */
+#define AC_ISP_SPI_TX usb_isp_spi_tx
/* usb */
#include "modules/usb/lufaconfig.h"
diff --git a/digital/dev2/src/usb_serial_isp/main.c b/digital/dev2/src/usb_serial_isp/main.c
index 849b9942..9639fd83 100644
--- a/digital/dev2/src/usb_serial_isp/main.c
+++ b/digital/dev2/src/usb_serial_isp/main.c
@@ -32,6 +32,7 @@
#include "descriptors.h"
#include "common/serial.h"
+#include "common/usb_isp.h"
#include "common/select.h"
HANDLES_EVENT (USB_Connect);
@@ -41,47 +42,6 @@ HANDLES_EVENT (USB_UnhandledControlPacket);
volatile uint8_t usb_connected, usb_configured;
-uint8_t isp_sent;
-
-void
-isp_send_char (uint8_t c)
-{
- Endpoint_SelectEndpoint (ISP_TX_EPNUM);
- /* Wait endpoint to become ready. */
- while (!Endpoint_ReadWriteAllowed ())
- ;
- Endpoint_Write_Byte (c);
- /* If at end of endpoint buffer, send. */
- if (!Endpoint_ReadWriteAllowed ())
- Endpoint_ClearCurrentBank ();
- /* Select back RX endpoint. */
- Endpoint_SelectEndpoint (ISP_RX_EPNUM);
- /* Will need extra clear at end of all transfers. */
- isp_sent = 1;
-}
-
-static void
-isp_task (void)
-{
- Endpoint_SelectEndpoint (ISP_RX_EPNUM);
- /* If data is available from USB: */
- if (Endpoint_ReadWriteAllowed ())
- {
- /* Read as much as possible, and clear endpoint. */
- do {
- isp_frame_accept_char (Endpoint_Read_Byte ());
- } while (Endpoint_ReadWriteAllowed ());
- Endpoint_ClearCurrentBank ();
- }
- /* If data has been sent, sent a ZLP or finalise last packet. */
- if (isp_sent)
- {
- Endpoint_SelectEndpoint (ISP_TX_EPNUM);
- Endpoint_ClearCurrentBank ();
- isp_sent = 0;
- }
-}
-
int
main (void)
{
@@ -102,7 +62,7 @@ main (void)
if (usb_configured)
{
serial_task ();
- isp_task ();
+ usb_isp_task ();
}
}
}