2 Copyright 2005, 2006, 2007 Dennis van Weeren
\r
4 This file is part of Minimig
\r
6 Minimig is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 3 of the License, or
\r
9 (at your option) any later version.
\r
11 Minimig is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
19 Hardware control routines
\r
21 27-11-2005 -started coding
\r
22 29-01-2006 -done a lot of work
\r
23 31-01-2006 -added key repeat
\r
24 06-02-2006 -took out all button handling stuff
\r
26 -- Goran Ljubojevic --
\r
27 2009-11-13 - OsdCommand added
\r
28 2009-11-21 - small cleanup
\r
29 2009-12-20 - systimer reset on every hardware init to support propper timings on reset
\r
30 2009-12-30 - Support for new FPGA firmware added in header file
\r
31 - GetFPGAStatus function added
\r
32 2010-01-29 - ResetFPGA() macro added to header file.
\r
33 2010-08-21 - YQ100818 FPGA core support
\r
34 2010-08-26 - Added firmwareConfiguration.h
\r
35 2010-09-09 - Added _SPI macro to help save space on multiple SPI calls
\r
36 2010-10-15 - Moved over to minimigmac, coding style changes, etc...
\r
41 #include "hardware.h"
\r
43 static volatile unsigned short systimer;
\r
45 /*initialize hardware*/
\r
46 void hardware_init(void)
\r
48 /* Disable analog inputs */
\r
49 ADCON1 = 0b00000110;
\r
51 /* Initalize output register */
\r
56 /* Enable PORTB weak pullup */
\r
57 INTCON2bits.RBPU = 0;
\r
59 /* Initialize SPI to master mode 0, sample on posedge, 5Mhz */
\r
63 /* Initialize input/ouput configuration */
\r
68 /* Initialize serial port */
\r
69 SPBRG = 10; /*115200 BAUD @ 20MHz*/
\r
73 /* Init timer0, internal clk, prescaler 1:256 */
\r
78 /* Enable interrupt for timer 0 */
\r
79 INTCONbits.TMR0IE = 1;
\r
83 /* interrupt service routine */
\r
84 static void isr(void) interrupt 1
\r
87 static unsigned char button_state;
\r
89 /* Clear timer 0 interrupt flag */
\r
90 INTCONbits.TMR0IF = 0;
\r
92 /* Set timer to timeout every 10ms
\r
93 * @20Mhz --> instruction = 200ns
\r
94 * 200ns * 256 * 195 = 10ms
\r
96 * Not sure we can just cast, so for now be gross
\r
97 * since SDCC doesn't give us a 16-bit TMR0
\r
100 t |= ((unsigned short)TMR0H) << 8;
\r
105 /* Increment system timer */
\r
108 /* Debounce button */
\r
109 if (check_button()) {
\r
110 if (button_state < 10) {
\r
112 if (button_state == 10) {
\r
127 /* Get system timer + offset (handy for lots of things)*/
\r
128 unsigned short get_timer(unsigned short offset)
\r
132 /* Get system time SAFELY */
\r
133 INTCONbits.GIE = 0;
\r
135 INTCONbits.GIE = 1;
\r
143 /* Check if timer is past given time in <t>
\r
144 * it may be maximum 30000 ticks in the future
\r
146 char check_timer(unsigned short t)
\r
148 /* calculate difference */
\r
149 INTCONbits.GIE = 0;
\r
151 INTCONbits.GIE = 1;
\r
153 /* check if <t> has passed */
\r
157 void wait_timer(unsigned short time)
\r
159 time = get_timer(time);
\r
160 while (!check_timer(time))
\r
164 /* SPI-bus. Should inline ? */
\r
165 unsigned char do_spi(unsigned char d)
\r
168 while (!SSPSTATbits.BF); /*Wait untill controller is ready*/
\r
169 return(SSPBUF); /*Return with received value*/
\r
172 /* FPAG configuration serial interface */
\r
173 void shift_fpga(unsigned char byte)
\r
232 /* Put out a chacter to the serial port */
\r
233 void putchar(char ch) __wparam
\r
235 while(TXSTAbits.TRMT == 0);
\r
238 while(TXSTAbits.TRMT == 0);
\r