3cfac12c260f4b1892eab12d8583dc8524e91f31
[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
50 #include <drv/timer.h>
51
52 #include <kern/kfile.h>
53
54 #include <string.h>
55
56
57 MOD_DEFINE(ft245rl);
58
59 /**
60  * Read \a size bytes in buffer \a buf, from fid \a _fd.
61  */
62 static size_t ft245rl_read(struct KFile *_fd, void *_buf, size_t size)
63 {
64         Ft245rl *fd = FT245RL_CAST(_fd);
65         (void)fd; //unused
66         uint8_t *buf = (uint8_t *)_buf;
67         size_t total_read = 0;
68
69         while (size--)
70         {
71                 while(!FT245RL_DATA_RDY())
72                         /* busy waiy */;
73
74                 *buf++ = FT245RL_GETDATA();
75                 total_read++;
76         }
77
78         return total_read;
79 }
80
81 /**
82  * Write \a size bytes from buffer \a buf, of fid \a _fd.
83  */
84 static size_t ft245rl_write(struct KFile *_fd, const void *_buf, size_t size)
85 {
86         Ft245rl *fd = FT245RL_CAST(_fd);
87         (void)fd; //unused
88         const uint8_t *buf = (const uint8_t *)_buf;
89         size_t total_write = 0;
90
91         while (size--)
92         {
93                 while(!FT245RL_TX_ALLOWED())
94                         /* busy waiy */;
95
96                 FT245RL_SETDATA(*buf++);
97                 total_write++;
98         }
99
100         return total_write;
101 }
102
103 /**
104  * Ft245rl init function.
105  */
106 void ft245rl_init(Ft245rl *fd)
107 {
108         memset(fd, 0, sizeof(*fd));
109         DB(fd->fd._type = KFT_FT245RL);
110
111         // Setup data ft245rl communication functions.
112         fd->fd.read = ft245rl_read;
113         fd->fd.write = ft245rl_write;
114
115         FT245RL_INIT();
116         while (FT245RL_DATA_RDY())
117                 FT245RL_GETDATA();
118
119         MOD_INIT(ft245rl);
120 }