2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
31 # define SLEEP_USB_EN (1 << 7)
32 # define SLEEP_XOSC_STB (1 << 6)
35 #define PERCFG_T1CFG_ALT_1 (0 << 6)
36 #define PERCFG_T1CFG_ALT_2 (1 << 6)
38 #define PERCFG_T3CFG_ALT_1 (0 << 5)
39 #define PERCFG_T3CFG_ALT_2 (1 << 5)
41 #define PERCFG_T4CFG_ALT_1 (0 << 4)
42 #define PERCFG_T4CFG_ALT_2 (1 << 4)
44 #define PERCFG_U1CFG_ALT_1 (0 << 1)
45 #define PERCFG_U1CFG_ALT_2 (1 << 1)
47 #define PERCFG_U0CFG_ALT_1 (0 << 0)
48 #define PERCFG_U0CFG_ALT_2 (1 << 0)
85 sfr at 0xE8 IRCON2; /* CPU Interrupt Flag 5 */
87 sbit at 0xE8 USBIF; /* USB interrupt flag (shared with Port2) */
88 sbit at 0xE8 P2IF; /* Port2 interrupt flag (shared with USB) */
89 sbit at 0xE9 UTX0IF; /* USART0 TX interrupt flag */
90 sbit at 0xEA UTX1IF; /* USART1 TX interrupt flag (shared with I2S TX) */
91 sbit at 0xEA I2STXIF; /* I2S TX interrupt flag (shared with USART1 TX) */
92 sbit at 0xEB P1IF; /* Port1 interrupt flag */
93 sbit at 0xEC WDTIF; /* Watchdog timer interrupt flag */
95 # define UxCSR_MODE_UART (1 << 7)
96 # define UxCSR_MODE_SPI (0 << 7)
97 # define UxCSR_RE (1 << 6)
98 # define UxCSR_SLAVE (1 << 5)
99 # define UxCSR_MASTER (0 << 5)
100 # define UxCSR_FE (1 << 4)
101 # define UxCSR_ERR (1 << 3)
102 # define UxCSR_RX_BYTE (1 << 2)
103 # define UxCSR_TX_BYTE (1 << 1)
104 # define UxCSR_ACTIVE (1 << 0)
109 # define UxUCR_FLUSH (1 << 7)
110 # define UxUCR_FLOW_DISABLE (0 << 6)
111 # define UxUCR_FLOW_ENABLE (1 << 6)
112 # define UxUCR_D9_EVEN_PARITY (0 << 5)
113 # define UxUCR_D9_ODD_PARITY (1 << 5)
114 # define UxUCR_BIT9_8_BITS (0 << 4)
115 # define UxUCR_BIT9_9_BITS (1 << 4)
116 # define UxUCR_PARITY_DISABLE (0 << 3)
117 # define UxUCR_PARITY_ENABLE (1 << 3)
118 # define UxUCR_SPB_1_STOP_BIT (0 << 2)
119 # define UxUCR_SPB_2_STOP_BITS (1 << 2)
120 # define UxUCR_STOP_LOW (0 << 1)
121 # define UxUCR_STOP_HIGH (1 << 1)
122 # define UxUCR_START_LOW (0 << 0)
123 # define UxUCR_START_HIGH (1 << 0)
128 # define UxGCR_CPOL_NEGATIVE (0 << 7)
129 # define UxGCR_CPOL_POSITIVE (1 << 7)
130 # define UxGCR_CPHA_FIRST_EDGE (0 << 6)
131 # define UxGCR_CPHA_SECOND_EDGE (1 << 6)
132 # define UxGCR_ORDER_LSB (0 << 5)
133 # define UxGCR_ORDER_MSB (1 << 5)
134 # define UxGCR_BAUD_E_MASK (0x1f)
135 # define UxGCR_BAUD_E_SHIFT 0
151 #define nop() _asm nop _endasm;
154 delay (unsigned char n)
166 * This version uses the USART in SPI mode
173 * Configure the peripheral pin choices
174 * for both of the serial ports
176 * Note that telemetrum will use U1CFG_ALT_2
177 * but that overlaps with SPI ALT_2, so until
178 * we can test that this works, we'll set this
181 PERCFG = (PERCFG_U1CFG_ALT_2 |
185 * Make the UART pins controlled by the UART
188 P1SEL |= ((1 << 6) | (1 << 7));
191 * UART mode with the receiver enabled
193 U1CSR = (UxCSR_MODE_UART |
196 * Pick a 38.4kbaud rate
199 U1GCR = 10 << UxGCR_BAUD_E_SHIFT; /* 38400 */
200 // U1GCR = 3 << UxGCR_BAUD_E_SHIFT; /* 300 */
202 * Reasonable serial parameters
204 U1UCR = (UxUCR_FLUSH |
206 UxUCR_D9_ODD_PARITY |
208 UxUCR_PARITY_DISABLE |
209 UxUCR_SPB_1_STOP_BIT |
215 usart_out_byte(uint8_t byte)
226 while ((U1CSR & UxCSR_RX_BYTE) == 0)
229 U1CSR &= ~UxCSR_RX_BYTE;
233 #define spi_init() usart_init()
234 #define spi_out_byte(b) usart_out_byte(b)
235 #define spi_in_byte() usart_in_byte()
237 static char string[] = "hello world\r\n";
245 while (!(SLEEP & SLEEP_XOSC_STB))
251 for (i = 0; i < sizeof(string) - 1; i++)
252 usart_out_byte(string[i]);