Sistema l'errore da me commesso in fase di conversione...
[bertos.git] / drv / ser_i196.c
1 /**
2  * \file
3  * <!--
4  * Copyright (C) 2003,2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright (C) 2000 Bernardo Innocenti <bernie@codewiz.org>
6  * This file is part of DevLib - See README.devlib for information.
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.7  2006/07/19 12:56:26  bernie
19  *#* Convert to new Doxygen style.
20  *#*
21  *#* Revision 1.6  2005/11/04 16:20:02  bernie
22  *#* Fix reference to README.devlib in header.
23  *#*
24  *#* Revision 1.5  2004/12/13 11:51:08  bernie
25  *#* DISABLE_INTS/ENABLE_INTS: Convert to IRQ_DISABLE/IRQ_ENABLE.
26  *#*
27  *#* Revision 1.4  2004/08/25 14:12:08  rasky
28  *#* Aggiornato il comment block dei log RCS
29  *#*
30  *#* Revision 1.3  2004/06/03 11:27:09  bernie
31  *#* Add dual-license information.
32  *#*
33  *#* Revision 1.2  2004/05/23 18:21:53  bernie
34  *#* Trim CVS logs and cleanup header info.
35  *#*
36  *#*/
37
38 #include "hw.h"
39 #include "serhw.h"
40
41 #define SER_HW_ENABLE_TX \
42         ATOMIC( \
43                 if (!ser_sending) \
44                 { \
45                         ser_sending = true; \
46                         (INT_PEND1 |= INT1F_TI) \
47                 } \
48         );
49
50 static volatile bool ser_sending;
51
52 // Serial TX intr
53 INTERRUPT(0x30) void TI_interrupt(void)
54 {
55         if (CANT_SEND)
56         {
57                 ser_sending = false;
58                 return;
59         }
60
61         /* Can we send two bytes at the same time? */
62         if (SP_STAT & SPSF_TX_EMPTY)
63         {
64                 SBUF = fifo_pop(&ser_txfifo);
65
66                 if (CANT_SEND)
67                 {
68                         ser_sending = false;
69                         return;
70                 }
71         }
72
73         SBUF = fifo_pop(&ser_txfifo);
74 }
75
76 INTERRUPT(0x32) void RI_interrupt(void)
77 {
78         ser_status |= SP_STAT &
79                 (SPSF_OVERRUN_ERROR | SPSF_PARITY_ERROR | SPSF_FRAMING_ERROR);
80         if (fifo_isfull(&ser_rxfifo))
81                 ser_status |= SERRF_RXFIFOOVERRUN;
82         else
83                 fifo_push(&ser_rxfifo, SBUF);
84 }
85
86 static void ser_setbaudrate(unsigned long rate)
87 {
88         // Calcola il periodo per la generazione del baud rate richiesto
89         uint16_t baud = (uint16_t)(((CLOCK_FREQ / 16) / rate) - 1) | 0x8000;
90         BAUD_RATE = (uint8_t)baud;
91         BAUD_RATE = (uint8_t)(baud >> 8);
92 }
93
94 static void ser_hw_init(void)
95 {
96         // Inizializza la porta seriale
97         SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
98         ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
99         IOC1 = ioc1_img;
100
101         // Svuota il buffer di ricezione
102         {
103                 uint8_t dummy = SBUF;
104         }
105
106         // Abilita gli interrupt
107         INT_MASK1 |= INT1F_TI | INT1F_RI;
108 }
109