C8051F32x firmware infrastructure

Sign in or create your account | Project List | Help

C8051F32x firmware infrastructure Git Source Tree

Root/fw/common/uart.c

1/*
2 * common/uart.c - UART initialization and debug output
3 *
4 * Written 2008, 2009 by Werner Almesberger
5 * Copyright 2008, 2009 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdarg.h>
15#include <stdio.h>
16
17#include "regs.h"
18#include "uart.h"
19
20
21/* gpio j4=0 */
22
23
24#ifndef CONFIG_USB_PUTCHAR
25
26void putchar(char c)
27{
28    if (c == '\n')
29        putchar('\r');
30    SBUF0 = c;
31    while (!TI0);
32    TI0 = 0;
33}
34
35#endif /* !CONFIG_USB_PUTCHAR */
36
37
38/* for now, we always use printf_tiny, which doesn't have a vprintf* version */
39#ifdef noCONFIG_PRINTK
40
41void printk(const char *fmt, ...)
42{
43    va_list ap;
44    __bit saved;
45
46    va_start(ap, fmt);
47    saved = EA;
48    EA = 0;
49    vprintf(fmt, ap);
50    EA = saved;
51    va_end(ap);
52}
53
54#endif /* CONFIG_PRINTK */
55
56
57void uart_init(uint8_t brg_mhz)
58{
59    /*
60     * UART: Enable only transmitter, no interrupts.
61     */
62    SCON0 = 0; /* also clears TI0 */
63    SMOD0 = S0DL1 | S0DL0;
64
65    /*
66     * Configure the UART to 115200bps, see table 13.1
67     *
68     * We can's support 1.5MHz (that is, without using the USB clock, but
69     * why would one want to run the core at 1.5MHz when USB is around ?)
70     *
71     * The closes settings would be:
72     *
73     * SBRL0 = 0xfff9; -- 107142.9 bps, error = -7%
74     * SBRL0 = 0xfffa; -- 125000 bps, error = +8.5%
75     *
76     * Depending on signal quality, we would need something like +/-5%.
77     */
78
79#if defined(UART_115200_BPS)
80    switch (brg_mhz) {
81    case 3:
82        SBRL0 = 0xfff3;
83        break;
84    case 6:
85        SBRL0 = 0xffe6;
86        break;
87    case 12:
88        SBRL0 = 0xffcc;
89        break;
90    case 24:
91        SBRL0 = 0xff98;
92        break;
93    case 48:
94        SBRL0 = 0xff30;
95        break;
96    }
97#elif defined(UART_57600_BPS)
98    switch (brg_mhz) {
99    case 3:
100        SBRL0 = 0xffe6;
101        break;
102    case 6:
103        SBRL0 = 0xffcc;
104        break;
105    case 12:
106        SBRL0 = 0xff98;
107        break;
108    case 24:
109        SBRL0 = 0xff30;
110        break;
111    case 48:
112        SBRL0 = 0xfe5f;
113        break;
114    }
115#else
116#error "must set either UART_115200_BPS or UART_57600_BPS"
117#endif
118
119    SBCON0 = SB0RUN | SB0PS0 | SB0PS1;
120
121    /*
122     * Make TX a push-pull output
123     */
124    P0MDOUT |= 1 << 4;
125}
126

Archive Download this file

Branches:
master



interactive