Rename macros.
[bertos.git] / bertos / drv / ft245rl.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Function library for Accessing FT245RL USB interface.
34  *
35  * This module handles USB communication with FT245RL chip.
36  * This chip is a parallel USB interface with data flow control.
37  * A kfile-like interface is supplied.
38  *
39  * \version $Id$
40  * \author Francesco Sacchi <batt@develer.com>
41  */
42
43 #include "hw/hw_ft245rl.h"
44 #include "ft245rl.h"
45
46 #include <cfg/macros.h>
47 #include <cfg/debug.h>
48 #include <cfg/module.h>
49 #include <drv/timer.h>
50 #include <kern/kfile.h>
51
52 #include <string.h>
53
54
55 MOD_DEFINE(ft245rl);
56
57 /**
58  * Read \a size bytes in buffer \a buf, from fid \a _fd.
59  */
60 static size_t ft245rl_read(struct KFile *_fd, void *_buf, size_t size)
61 {
62         Ft245rl *fd = FT245RL_CAST(_fd);
63         (void)fd; //unused
64         uint8_t *buf = (uint8_t *)_buf;
65         size_t total_read = 0;
66
67         while (size--)
68         {
69                 while(!FT245RL_DATA_RDY())
70                         /* busy waiy */;
71
72                 *buf++ = FT245RL_GETDATA();
73                 total_read++;
74         }
75
76         return total_read;
77 }
78
79 /**
80  * Write \a size bytes from buffer \a buf, of fid \a _fd.
81  */
82 static size_t ft245rl_write(struct KFile *_fd, const void *_buf, size_t size)
83 {
84         Ft245rl *fd = FT245RL_CAST(_fd);
85         (void)fd; //unused
86         const uint8_t *buf = (const uint8_t *)_buf;
87         size_t total_write = 0;
88
89         while (size--)
90         {
91                 while(!FT245RL_TX_ALLOWED())
92                         /* busy waiy */;
93
94                 FT245RL_SETDATA(*buf++);
95                 total_write++;
96         }
97
98         return total_write;
99 }
100
101 /**
102  * Ft245rl init function.
103  */
104 void ft245rl_init(Ft245rl *fd)
105 {
106         memset(fd, 0, sizeof(*fd));
107         DB(fd->fd._type = KFT_FT245RL);
108
109         // Setup data ft245rl communication functions.
110         fd->fd.read = ft245rl_read;
111         fd->fd.write = ft245rl_write;
112
113         FT245RL_INIT();
114         while (FT245RL_DATA_RDY())
115                 FT245RL_GETDATA();
116
117         MOD_INIT(ft245rl);
118 }