From 9db34c1a101c060c3de7e9f1ab269d0000e709dc Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 23 Mar 2013 23:49:51 +0100 Subject: digital/io-hub/src/apbirthday: add interface to program zb over usb --- digital/io-hub/src/apbirthday/Config | 2 + digital/io-hub/src/apbirthday/Makefile | 4 +- digital/io-hub/src/apbirthday/hardware.hh | 4 +- digital/io-hub/src/apbirthday/hardware.host.cc | 6 ++ digital/io-hub/src/apbirthday/hardware.stm32.cc | 22 +++++++ digital/io-hub/src/apbirthday/robot.cc | 2 + digital/io-hub/src/common-cc/zb_avrisp.stm32.cc | 88 +++++++++++++++++++++++++ digital/io-hub/src/common-cc/zb_avrisp.stm32.hh | 32 +++++++++ 8 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 digital/io-hub/src/apbirthday/Config create mode 100644 digital/io-hub/src/common-cc/zb_avrisp.stm32.cc create mode 100644 digital/io-hub/src/common-cc/zb_avrisp.stm32.hh (limited to 'digital') diff --git a/digital/io-hub/src/apbirthday/Config b/digital/io-hub/src/apbirthday/Config new file mode 100644 index 00000000..7d5f805a --- /dev/null +++ b/digital/io-hub/src/apbirthday/Config @@ -0,0 +1,2 @@ +[hal/usb] +stream_nb = 2 diff --git a/digital/io-hub/src/apbirthday/Makefile b/digital/io-hub/src/apbirthday/Makefile index 8e27941c..473acdff 100644 --- a/digital/io-hub/src/apbirthday/Makefile +++ b/digital/io-hub/src/apbirthday/Makefile @@ -3,13 +3,15 @@ BASE = ../../../ucoolib TARGETS = host stm32f4 PROGS = apbirthday apbirthday_SOURCES = main.cc robot.cc hardware.host.cc hardware.stm32.cc \ + zb_avrisp.stm32.cc \ i2c_queue.cc asserv.cc chrono.host.cc chrono.stm32.cc \ outputs.cc \ top.cc init.cc move.cc \ angfsm.host.c angfsm_gen_arm_AI.arm.c fsm_queue.cc \ $(AVR_SOURCES) -MODULES = utils hal/gpio hal/uart hal/usb hal/i2c base/proto +MODULES = utils hal/gpio hal/uart hal/usb hal/i2c hal/spi \ + base/proto dev/avrisp # Hack mode, include old code from AVR modules. AVR_MODULES = math/geometry math/fixed diff --git a/digital/io-hub/src/apbirthday/hardware.hh b/digital/io-hub/src/apbirthday/hardware.hh index 3cf5a50f..f3378170 100644 --- a/digital/io-hub/src/apbirthday/hardware.hh +++ b/digital/io-hub/src/apbirthday/hardware.hh @@ -53,7 +53,7 @@ struct Hardware ucoo::Uart dev_uart, zb_uart; #ifdef TARGET_stm32 ucoo::UsbStreamControl usb_control; - ucoo::UsbStream usb; + ucoo::UsbStream usb, zb_usb_avrisp; ucoo::I2cHard main_i2c; #else ucoo::HostStream usb; @@ -87,6 +87,8 @@ struct Hardware Hardware (); // Wait until next cycle. void wait (); + // Handle zb programmation. + void zb_handle (); }; #endif // hardware_hh diff --git a/digital/io-hub/src/apbirthday/hardware.host.cc b/digital/io-hub/src/apbirthday/hardware.host.cc index d2deecad..fcb27030 100644 --- a/digital/io-hub/src/apbirthday/hardware.host.cc +++ b/digital/io-hub/src/apbirthday/hardware.host.cc @@ -75,3 +75,9 @@ Hardware::wait () node.wait (node.date () + 4); } +void +Hardware::zb_handle () +{ + // No zb on host. +} + diff --git a/digital/io-hub/src/apbirthday/hardware.stm32.cc b/digital/io-hub/src/apbirthday/hardware.stm32.cc index 8eb6f5a5..a3cd4c1d 100644 --- a/digital/io-hub/src/apbirthday/hardware.stm32.cc +++ b/digital/io-hub/src/apbirthday/hardware.stm32.cc @@ -27,9 +27,12 @@ #include #include "ucoolib/hal/gpio/gpio.hh" +#include "zb_avrisp.stm32.hh" + Hardware::Hardware () : dev_uart (4), zb_uart (2), usb_control ("APBTeam", "APBirthday"), usb (usb_control, 0), + zb_usb_avrisp (usb_control, 1), main_i2c (2), raw_jack (GPIOD, 12), ihm_color (GPIOD, 14), @@ -72,6 +75,7 @@ Hardware::Hardware () zb_uart.block (false); // usb usb.block (false); + zb_usb_avrisp.block (false); // main_i2c gpio_mode_setup (GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8); gpio_mode_setup (GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9); @@ -96,3 +100,21 @@ Hardware::wait () TIM3_SR = ~TIM_SR_UIF; } +void +Hardware::zb_handle () +{ + // Switch to AVRISP? + if (zb_usb_avrisp.poll ()) + { + // Uart pins are reused. + zb_uart.disable (); + gpio_mode_setup (GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO8 | GPIO9); + // Go to special AVRISP mode. + zb_avrisp (zb_usb_avrisp); + // Restore. + gpio_mode_setup (GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8 | GPIO9); + gpio_set_af (GPIOD, GPIO_AF7, GPIO8 | GPIO9); + zb_uart.enable (38400, ucoo::Uart::EVEN, 1); + } +} + diff --git a/digital/io-hub/src/apbirthday/robot.cc b/digital/io-hub/src/apbirthday/robot.cc index 87b08b18..25ea7059 100644 --- a/digital/io-hub/src/apbirthday/robot.cc +++ b/digital/io-hub/src/apbirthday/robot.cc @@ -98,6 +98,8 @@ Robot::main_loop () dev_proto.accept (); zb_proto.accept (); usb_proto.accept (); + // Handle zb programmation. + hardware.zb_handle (); // Send stats. proto_stats (); } diff --git a/digital/io-hub/src/common-cc/zb_avrisp.stm32.cc b/digital/io-hub/src/common-cc/zb_avrisp.stm32.cc new file mode 100644 index 00000000..3360c4d9 --- /dev/null +++ b/digital/io-hub/src/common-cc/zb_avrisp.stm32.cc @@ -0,0 +1,88 @@ +// io-hub - Modular Input/Output. {{{ +// +// Copyright (C) 2013 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 "zb_avrisp.stm32.hh" + +#include "ucoolib/dev/avrisp/avrisp_frame.hh" +#include "ucoolib/hal/gpio/gpio.hh" +#include "ucoolib/hal/spi/spi_soft.hh" +#include "ucoolib/utils/delay.hh" + +class ZbAvrIspIntf : public ucoo::AvrIspIntf +{ + public: + ZbAvrIspIntf (ucoo::Io &reset, ucoo::Io &sck, ucoo::Io &mosi, + ucoo::Io &miso) + : reset_ (reset), sck_ (sck), spi_ (sck, mosi, miso), active_ (true) + { + } + uint8_t send_and_recv (uint8_t tx) + { + return spi_.send_and_recv (tx); + } + void enable (uint8_t sck_duration_us) + { + reset_.output (); + reset_.reset (); + sck_.output (); + sck_.reset (); + ucoo::delay_us (1000); + reset_.set (); + ucoo::delay_us (100); + reset_.reset (); + int freq = 1000000 / sck_duration_us; + spi_.enable (freq, ucoo::SPI_MODE_0); + } + void disable () + { + spi_.disable (); + reset_.input (); + active_ = false; + } + void sck_pulse () + { + sck_.toggle (); + ucoo::delay_us (100); + sck_.toggle (); + } + bool active () const { return active_; } + private: + ucoo::Io &reset_; + ucoo::Io &sck_; + ucoo::SpiSoftMaster spi_; + bool active_; +}; + +void +zb_avrisp (ucoo::Stream &s) +{ + ucoo::Gpio reset (GPIOD, 10), sck (GPIOE, 7), mosi (GPIOD, 8), + miso (GPIOD, 9); + ZbAvrIspIntf intf (reset, sck, mosi, miso); + ucoo::AvrIsp isp (intf); + ucoo::AvrIspProto proto (isp); + ucoo::AvrIspFrame frame (proto); + while (intf.active ()) + frame.read_and_write (s); +} + diff --git a/digital/io-hub/src/common-cc/zb_avrisp.stm32.hh b/digital/io-hub/src/common-cc/zb_avrisp.stm32.hh new file mode 100644 index 00000000..eda82f7d --- /dev/null +++ b/digital/io-hub/src/common-cc/zb_avrisp.stm32.hh @@ -0,0 +1,32 @@ +#ifndef zb_avrisp_stm32_hh +#define zb_avrisp_stm32_hh +// io-hub - Modular Input/Output. {{{ +// +// Copyright (C) 2013 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 "ucoolib/intf/stream.hh" + +/// Start AVRISP on given stream. +void +zb_avrisp (ucoo::Stream &s); + +#endif // zb_avrisp_stm32_hh -- cgit v1.2.3