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