1a556acab39e91bdb8fb2b2ccc9196a0286432f1
[bertos.git] / drv / ser_i196.c
1 /*!
2  * \file
3  * <!--
4  * Copyright (C) 2003 Develer S.r.l. (http://www.develer.com/)
5  * Copyright (C) 2000 Bernardo Innocenti <bernie@codewiz.org>
6  * All Rights Reserved.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief CPU specific serial I/O driver
14  */
15
16 /*
17  * $Log$
18  * Revision 1.2  2004/05/23 18:21:53  bernie
19  * Trim CVS logs and cleanup header info.
20  *
21  */
22
23 #include "hw.h"
24 #include "serhw.h"
25
26 #define SER_HW_ENABLE_TX \
27         DISABLE_INTS; \
28         if (!ser_sending) \
29         { \
30                 ser_sending = true; \
31                 (INT_PEND1 |= INT1F_TI) \
32         } \
33         ENABLE_INTS;
34
35 static volatile bool ser_sending;
36
37 // Serial TX intr
38 INTERRUPT(0x30) void TI_interrupt(void)
39 {
40         if (CANT_SEND)
41         {
42                 ser_sending = false;
43                 return;
44         }
45
46         /* Can we send two bytes at the same time? */
47         if (SP_STAT & SPSF_TX_EMPTY)
48         {
49                 SBUF = fifo_pop(&ser_txfifo);
50
51                 if (CANT_SEND)
52                 {
53                         ser_sending = false;
54                         return;
55                 }
56         }
57
58         SBUF = fifo_pop(&ser_txfifo);
59 }
60
61 INTERRUPT(0x32) void RI_interrupt(void)
62 {
63         ser_status |= SP_STAT &
64                 (SPSF_OVERRUN_ERROR | SPSF_PARITY_ERROR | SPSF_FRAMING_ERROR);
65         if (fifo_isfull(&ser_rxfifo))
66                 ser_status |= SERRF_RXFIFOOVERRUN;
67         else
68                 fifo_push(&ser_rxfifo, SBUF);
69 }
70
71 static void ser_setbaudrate(unsigned long rate)
72 {
73         // Calcola il periodo per la generazione del baud rate richiesto
74         uint16_t baud = (uint16_t)(((CLOCK_FREQ / 16) / rate) - 1) | 0x8000;
75         BAUD_RATE = (uint8_t)baud;
76         BAUD_RATE = (uint8_t)(baud >> 8);
77 }
78
79 static void ser_hw_init(void)
80 {
81         // Inizializza la porta seriale
82         SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
83         ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
84         IOC1 = ioc1_img;
85
86         // Svuota il buffer di ricezione
87         {
88                 uint8_t dummy = SBUF;
89         }
90
91         // Abilita gli interrupt
92         INT_MASK1 |= INT1F_TI | INT1F_RI;
93 }
94