Commits

Martin Vejnár committed 8ad2b18

Added the firmware project with a sketch of the PDI interface driver.

Comments (0)

Files changed (2)

+#include "avrlib/async_usart.hpp"
+#include "avrlib/usart1.hpp"
+
+#include "avrlib/timer1.hpp"
+#include "avrlib/counter.hpp"
+#include "avrlib/stopwatch.hpp"
+#include "avrlib/format.hpp"
+
+#include "avrlib/buffer.hpp"
+#include "avrlib/pin.hpp"
+#include "avrlib/portd.hpp"
+#include "avrlib/assert.hpp"
+
+typedef avrlib::pin<avrlib::portd, 4> pdi_clk;
+typedef avrlib::pin<avrlib::portd, 1> pdi_data;
+
+avrlib::async_usart<avrlib::usart1, 64, 64, avrlib::bootseq> com(38400, true);
+
+ISR(USART1_RXC_vect)
+{
+	com.process_rx();
+}
+
+typedef avrlib::counter<avrlib::timer1> clock_t;
+clock_t clock;
+
+ISR(TIMER1_OVF_vect)
+{
+	clock.tov_interrupt();
+}
+
+template <typename Clock, typename PdiClk, typename PdiData>
+class pdi_t
+{
+public:
+	explicit pdi_t(Clock & clock)
+		: m_clock(clock), m_state(st_disabled)
+	{
+	}
+
+	void clear()
+	{
+		PdiClk::output(false);
+		PdiClk::clear();
+
+		PdiData::output(false);
+		PdiData::clear();
+
+		UCSR0B = 0;
+
+		m_state = st_disabled;
+	}
+
+	void init()
+	{
+		AVRLIB_ASSERT(m_state == st_disabled);
+
+		PdiClk::clear();
+		PdiClk::output(true);
+
+		PdiData::set();
+		PdiData::output(true);
+
+		m_state = st_rst_disable;
+		m_time_base = m_clock.value();
+	}
+
+	bool tx_ready() const
+	{
+		return m_state == st_tx && !m_tx_buffer.full() && m_rx_count == 0;
+	}
+
+	bool rx_ready() const
+	{
+		return !m_rx_buffer.empty();
+	}
+
+	void write(uint8_t data, uint8_t rx_count)
+	{
+		AVRLIB_ASSERT(this->tx_ready());
+
+		UCSR0B = (1<<TXEN0);
+		m_tx_buffer.push(data);
+		m_rx_count = rx_count;
+		UCSR0B = (1<<TXEN0)|(1<<UDRIE0);
+	}
+
+	uint8_t read()
+	{
+		while (!this->rx_ready())
+		{
+		}
+
+		uint8_t res = m_rx_buffer.top();
+		m_rx_buffer.pop();
+		return res;
+	}
+
+	// Must be called at least every 50us
+	void process()
+	{
+		switch (m_state)
+		{
+		case st_disabled:
+			break;
+		case st_rst_disable:
+			if (m_clock.value() - m_time_base < 32) // FIXME: time constants
+			{
+				m_state = st_wait_ticks;
+				m_time_base += 32;
+			}
+			break;
+		case st_wait_ticks:
+			if (m_clock.value() - m_time_base < 128) // FIXME: time constants
+			{
+				UBRR0H = 0;
+				UBRR0L = 10;
+				UCSR0C = (1<<URSEL0) | (1<<UMSEL0)
+					| (1<<UPM01) | (1<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00) | (1<<UCPOL0);
+				UCSR0B = (1<<TXEN0);
+
+				m_state = st_tx;
+				m_rx_count = 0;
+			}
+			break;
+		case st_tx:
+			break;
+		}
+	}
+
+	void intr_rxc()
+	{
+		m_rx_buffer.push(UDR0);
+		if (--m_rx_count == 0)
+			UCSR0B = (1<<TXEN0);
+	}
+
+	void intr_udre()
+	{
+		UDR0 = m_tx_buffer.top();
+		m_tx_buffer.pop();
+		if (m_tx_buffer.empty())
+		{
+			if (m_rx_count)
+				UCSR0B = (1<<TXEN0)|(1<<TXCIE0);
+			else
+				UCSR0B = (1<<TXEN0);
+		}
+	}
+	
+	void intr_txc()
+	{
+		UCSR0B = (1<<RXEN0)|(1<<RXCIE0);
+	}
+
+private:
+	Clock & m_clock;
+	typename Clock::time_type m_time_base;
+	avrlib::buffer<uint8_t, 8> m_rx_buffer;
+	avrlib::buffer<uint8_t, 8> m_tx_buffer;
+	volatile uint8_t m_rx_count;
+
+	enum { st_disabled, st_rst_disable, st_wait_ticks, st_tx } m_state;
+};
+
+pdi_t<clock_t, pdi_clk, pdi_data> pdi(clock);
+
+ISR(USART0_RXC_vect)
+{
+	pdi.intr_rxc();
+}
+
+ISR(USART0_UDRE_vect)
+{
+	pdi.intr_udre();
+}
+
+ISR(USART0_TXC_vect)
+{
+	pdi.intr_txc();
+}
+
+int main()
+{
+	sei();
+
+	avrlib::timer1::tov_interrupt(true);
+	avrlib::timer1::clock_source(avrlib::timer_fosc_8);
+
+	for (;;)
+	{
+		if (!com.empty())
+		{
+			uint8_t ch = com.read();
+			switch (ch)
+			{
+			case 'a':
+				pdi.init();
+				break;
+			case 'A':
+				pdi.clear();
+				break;
+			case 'r':
+				pdi.write(0x80, 1);
+				break;
+			case 'T':
+				avrlib::send_hex(com, clock.value());
+				com.write('\n');
+				break;
+			}
+		}
+
+		if (pdi.rx_ready())
+			send_hex(com, pdi.read());
+
+		pdi.process();
+		com.process_tx();
+	}
+}

firmware/shupito-firmware.aps

+<AVRStudio><MANAGEMENT><ProjectName>shupito-firmware</ProjectName><Created>09-Mar-2011 19:47:32</Created><LastEdit>09-Mar-2011 19:49:47</LastEdit><ICON>241</ICON><ProjectType>0</ProjectType><Created>09-Mar-2011 19:47:32</Created><Version>4</Version><Build>4, 18, 0, 685</Build><ProjectTypeName>AVR GCC</ProjectTypeName></MANAGEMENT><CODE_CREATION><ObjectFile>default\shupito-firmware.elf</ObjectFile><EntryFile></EntryFile><SaveFolder>X:\checkouts\shupito\firmware\</SaveFolder></CODE_CREATION><DEBUG_TARGET><CURRENT_TARGET>AVR Simulator</CURRENT_TARGET><CURRENT_PART>ATmega162.xml</CURRENT_PART><BREAKPOINTS></BREAKPOINTS><IO_EXPAND><HIDE>false</HIDE></IO_EXPAND><REGISTERNAMES><Register>R00</Register><Register>R01</Register><Register>R02</Register><Register>R03</Register><Register>R04</Register><Register>R05</Register><Register>R06</Register><Register>R07</Register><Register>R08</Register><Register>R09</Register><Register>R10</Register><Register>R11</Register><Register>R12</Register><Register>R13</Register><Register>R14</Register><Register>R15</Register><Register>R16</Register><Register>R17</Register><Register>R18</Register><Register>R19</Register><Register>R20</Register><Register>R21</Register><Register>R22</Register><Register>R23</Register><Register>R24</Register><Register>R25</Register><Register>R26</Register><Register>R27</Register><Register>R28</Register><Register>R29</Register><Register>R30</Register><Register>R31</Register></REGISTERNAMES><COM>Auto</COM><COMType>0</COMType><WATCHNUM>0</WATCHNUM><WATCHNAMES><Pane0></Pane0><Pane1></Pane1><Pane2></Pane2><Pane3></Pane3></WATCHNAMES><BreakOnTrcaeFull>0</BreakOnTrcaeFull></DEBUG_TARGET><Debugger><Triggers></Triggers></Debugger><AVRGCCPLUGIN><FILES><SOURCEFILE>main.cc</SOURCEFILE><OTHERFILE>default\shupito-firmware.lss</OTHERFILE><OTHERFILE>default\shupito-firmware.map</OTHERFILE></FILES><CONFIGS><CONFIG><NAME>default</NAME><USESEXTERNALMAKEFILE>NO</USESEXTERNALMAKEFILE><EXTERNALMAKEFILE></EXTERNALMAKEFILE><PART>atmega162</PART><HEX>1</HEX><LIST>1</LIST><MAP>1</MAP><OUTPUTFILENAME>shupito-firmware.elf</OUTPUTFILENAME><OUTPUTDIR>default\</OUTPUTDIR><ISDIRTY>1</ISDIRTY><OPTIONS/><INCDIRS/><LIBDIRS/><LIBS/><LINKOBJECTS/><OPTIONSFORALL>-Wall -gdwarf-2 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums </OPTIONSFORALL><LINKEROPTIONS></LINKEROPTIONS><SEGMENTS/></CONFIG></CONFIGS><LASTCONFIG>default</LASTCONFIG><USES_WINAVR>1</USES_WINAVR><GCC_LOC>C:\Program Files (x86)\Atmel\AVR Tools\AVR Toolchain\bin\avr-gcc.exe</GCC_LOC><MAKE_LOC>C:\Program Files (x86)\Atmel\AVR Tools\AVR Toolchain\bin\make.exe</MAKE_LOC></AVRGCCPLUGIN><IOView><usergroups/><sort sorted="0" column="0" ordername="0" orderaddress="0" ordergroup="0"/></IOView><Files><File00000><FileId>00000</FileId><FileName>main.cc</FileName><Status>1</Status></File00000></Files><Events><Bookmarks></Bookmarks></Events><Trace><Filters></Filters></Trace></AVRStudio>