##// END OF EJS Templates
Sync
jeandet -
r80:c0daab91f95b dev_alexis
parent child
Show More
@@ -1,281 +1,282
1 #include <stdlib.h>
1 #include <stdlib.h>
2 #include <errno.h>
2 #include <errno.h>
3 #include <string.h>
3 #include <string.h>
4 #include <sys/stat.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
5 #include <sys/types.h>
6 #include <sys/times.h>
6 #include <sys/times.h>
7 #include <streamdevices.h>
7 #include <streamdevices.h>
8 #include <string.h>
8 #include <string.h>
9 #include <stdio.h>
9 #include <stdio.h>
10 #include <gpio.h>
10 #include <gpio.h>
11 #include <uart.h>
11 #include <uart.h>
12 #include <stdint.h>
12 #include <stdint.h>
13 #include <bsp.h>
13 #include <bsp.h>
14 #include <core.h>
14
15
15 #undef errno
16 #undef errno
16
17
17
18
18 #ifdef __cplusplus
19 #ifdef __cplusplus
19 extern "C" {
20 extern "C" {
20 #endif
21 #endif
21 extern int errno;
22 extern int errno;
22 extern int32_t __max_opened_files__;
23 extern int32_t __max_opened_files__;
23 extern streamdevice* __opnfiles__[];
24 extern streamdevice* __opnfiles__[];
24 extern int32_t* __fs_root__;
25 extern int32_t* __fs_root__;
25 extern int32_t __fs_root_size__;
26 extern int32_t __fs_root_size__;
26
27
27 char *__env[1] = { 0 };
28 char *__env[1] = { 0 };
28 char **environ = __env;
29 char **environ = __env;
29
30
30 int _exit()
31 int _exit()
31 {
32 {
32 while(1)
33 while(1)
33 {
34 {
34 delay_100us(10000);
35 delay_100us(10000);
35 gpioset(LED2);
36 gpioset(LED2);
36 delay_100us(10000);
37 delay_100us(10000);
37 gpioclr(LED2);
38 gpioclr(LED2);
38 }
39 }
39 }
40 }
40
41
41 int _close(int file)
42 int _close(int file)
42 {
43 {
43 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
44 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
44 {
45 {
45 return __opnfiles__[file]->ops->close( __opnfiles__[file]);
46 return __opnfiles__[file]->ops->close( __opnfiles__[file]);
46 }
47 }
47 return 0;
48 return 0;
48 }
49 }
49
50
50
51
51 int _write(int file, char *ptr, int len)
52 int _write(int file, char *ptr, int len)
52 {
53 {
53 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
54 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
54 {
55 {
55 if(__opnfiles__[file]->ops->write(__opnfiles__[file],ptr,1,len)) return len;
56 if(__opnfiles__[file]->ops->write(__opnfiles__[file],ptr,1,len)) return len;
56 }
57 }
57 return len;
58 return len;
58 }
59 }
59
60
60 int _execve(char *name, char **argv, char **env) {
61 int _execve(char *name, char **argv, char **env) {
61 errno = ENOMEM;
62 errno = ENOMEM;
62 return -1;
63 return -1;
63 }
64 }
64
65
65
66
66 int _fork(void) {
67 int _fork(void) {
67 errno = EAGAIN;
68 errno = EAGAIN;
68 return -1;
69 return -1;
69 }
70 }
70
71
71 int _fstat(int file, struct stat *st) {
72 int _fstat(int file, struct stat *st) {
72 st->st_mode = S_IFCHR;
73 st->st_mode = S_IFCHR;
73 return 0;
74 return 0;
74 }
75 }
75
76
76
77
77 int _getpid(void) {
78 int _getpid(void) {
78 return 1;
79 return 1;
79 }
80 }
80
81
81
82
82 int _isatty(int file) {
83 int _isatty(int file) {
83 return 1;
84 return 1;
84 }
85 }
85
86
86
87
87 int _kill(int pid, int sig) {
88 int _kill(int pid, int sig) {
88 errno = EINVAL;
89 errno = EINVAL;
89 return -1;
90 return -1;
90 }
91 }
91
92
92
93
93 int _link(char *old, char *_new) {
94 int _link(char *old, char *_new) {
94 errno = EMLINK;
95 errno = EMLINK;
95 return -1;
96 return -1;
96 }
97 }
97
98
98
99
99 int _lseek(int file, int ptr, int dir) {
100 int _lseek(int file, int ptr, int dir) {
100 return 0;
101 return 0;
101 }
102 }
102
103
103 #include <gpio.h>
104 #include <gpio.h>
104
105
105 int _open(const char *name, int flags, int mode)
106 int _open(const char *name, int flags, int mode)
106 {
107 {
107 if(!strncmp("UART", name, 4) && ((name[4] & 0x30)==0x30))
108 if(!strncmp("UART", name, 4) && ((name[4] & 0x30)==0x30))
108 {
109 {
109 //uart_t* uart1 = malloc(sizeof(uart_t));
110 //uart_t* uart1 = malloc(sizeof(uart_t));
110 streamdevice* fd1 = malloc(sizeof(streamdevice));
111 streamdevice* fd1 = malloc(sizeof(streamdevice));
111 uart_t uart=uartopen((name[4] & 0xF)-1);
112 uart_t uart=uartopen((name[4] & 0xF)-1);
112 if(uart!=-1);
113 if(uart!=-1);
113 {
114 {
114 uartmkstreamdev(uart,fd1);
115 uartmkstreamdev(uart,fd1);
115 int i=2;
116 int i=2;
116 while((i<__max_opened_files__) && (__opnfiles__[i]!=NULL))i++;
117 while((i<__max_opened_files__) && (__opnfiles__[i]!=NULL))i++;
117 if(i!=__max_opened_files__)
118 if(i!=__max_opened_files__)
118 {
119 {
119 __opnfiles__[i] = fd1;
120 __opnfiles__[i] = fd1;
120 return i;
121 return i;
121 }
122 }
122 else printf("Too much files opened\n\r");
123 else printf("Too much files opened\n\r");
123 }
124 }
124 }
125 }
125 return -1;
126 return -1;
126 }
127 }
127
128
128 int _read(int file, char *ptr, int len) {
129 int _read(int file, char *ptr, int len) {
129 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
130 if(file<__max_opened_files__ && __opnfiles__[file]!=NULL)
130 {
131 {
131 if(__opnfiles__[file]->ops->read(__opnfiles__[file],ptr,1,len)) return len;
132 if(__opnfiles__[file]->ops->read(__opnfiles__[file],ptr,1,len)) return len;
132 }
133 }
133 return 0;
134 return 0;
134 }
135 }
135
136
136
137
137
138
138 caddr_t _sbrk(int incr) {
139 caddr_t _sbrk(int incr) {
139 register char * stack_ptr __asm__ ("sp");
140 register char * stack_ptr __asm__ ("sp");
140 extern char end; /* Defined by the linker */
141 extern char end; /* Defined by the linker */
141 static char *heap_end;
142 static char *heap_end;
142 char *prev_heap_end;
143 char *prev_heap_end;
143 if (heap_end == 0) {
144 if (heap_end == 0) {
144 heap_end = &end;
145 heap_end = &end;
145 }
146 }
146 prev_heap_end = heap_end;
147 prev_heap_end = heap_end;
147 if (heap_end + incr > stack_ptr) {
148 if (heap_end + incr > stack_ptr) {
148 printf("Heap and stack collision\n");
149 printf("Heap and stack collision\n");
149 //abort ();
150 //abort ();
150 while(1)
151 while(1)
151 {
152 {
152 delay_100us(10000);
153 delay_100us(10000);
153 gpioset(LED2);
154 gpioset(LED2);
154 delay_100us(10000);
155 delay_100us(10000);
155 gpioclr(LED2);
156 gpioclr(LED2);
156 }
157 }
157 }
158 }
158 heap_end += incr;
159 heap_end += incr;
159 return (caddr_t) prev_heap_end;
160 return (caddr_t) prev_heap_end;
160 }
161 }
161
162
162
163
163
164
164 int _stat(char *file, struct stat *st) {
165 int _stat(char *file, struct stat *st) {
165 st->st_mode = S_IFCHR;
166 st->st_mode = S_IFCHR;
166 return 0;
167 return 0;
167 }
168 }
168
169
169
170
170 int _times(struct tms *buf) {
171 int _times(struct tms *buf) {
171 return -1;
172 return -1;
172 }
173 }
173
174
174
175
175 int _unlink(char *name) {
176 int _unlink(char *name) {
176 errno = ENOENT;
177 errno = ENOENT;
177 return -1;
178 return -1;
178 }
179 }
179
180
180 int _wait(int *status) {
181 int _wait(int *status) {
181 errno = ECHILD;
182 errno = ECHILD;
182 return -1;
183 return -1;
183 }
184 }
184
185
185
186
186 int _read_r (struct _reent *r, int file, char * ptr, int len)
187 int _read_r (struct _reent *r, int file, char * ptr, int len)
187 {
188 {
188 r = r;
189 r = r;
189 file = file;
190 file = file;
190 ptr = ptr;
191 ptr = ptr;
191 len = len;
192 len = len;
192 _read(file,ptr,len);
193 _read(file,ptr,len);
193 errno = EINVAL;
194 errno = EINVAL;
194 return -1;
195 return -1;
195 }
196 }
196
197
197 /***************************************************************************/
198 /***************************************************************************/
198
199
199 int _lseek_r (struct _reent *r, int file, int ptr, int dir)
200 int _lseek_r (struct _reent *r, int file, int ptr, int dir)
200 {
201 {
201 r = r;
202 r = r;
202 file = file;
203 file = file;
203 ptr = ptr;
204 ptr = ptr;
204 dir = dir;
205 dir = dir;
205
206
206 return 0;
207 return 0;
207 }
208 }
208
209
209 /***************************************************************************/
210 /***************************************************************************/
210
211
211 int _write_r (struct _reent *r, int file, char * ptr, int len)
212 int _write_r (struct _reent *r, int file, char * ptr, int len)
212 {
213 {
213 return _write(file, ptr, len);
214 return _write(file, ptr, len);
214 }
215 }
215
216
216 /***************************************************************************/
217 /***************************************************************************/
217
218
218 int _close_r (struct _reent *r, int file)
219 int _close_r (struct _reent *r, int file)
219 {
220 {
220 return 0;
221 return 0;
221 }
222 }
222
223
223 /***************************************************************************/
224 /***************************************************************************/
224
225
225 caddr_t _sbrk_r (struct _reent *r, int incr)
226 caddr_t _sbrk_r (struct _reent *r, int incr)
226 {
227 {
227 register char * stack_ptr __asm__ ("sp");
228 register char * stack_ptr __asm__ ("sp");
228 extern char end; /* Defined by the linker */
229 extern char end; /* Defined by the linker */
229 static char *heap_end;
230 static char *heap_end;
230 char *prev_heap_end;
231 char *prev_heap_end;
231 if (heap_end == 0) {
232 if (heap_end == 0) {
232 heap_end = &end;
233 heap_end = &end;
233 }
234 }
234 prev_heap_end = heap_end;
235 prev_heap_end = heap_end;
235 if (heap_end + incr > stack_ptr) {
236 if (heap_end + incr > stack_ptr) {
236 printf( "Heap and stack collision\n");
237 printf( "Heap and stack collision\n");
237 //abort ();
238 //abort ();
238 while(1)
239 while(1)
239 {
240 {
240 delay_100us(10000);
241 delay_100us(10000);
241 gpioset(LED2);
242 gpioset(LED2);
242 delay_100us(10000);
243 delay_100us(10000);
243 gpioclr(LED2);
244 gpioclr(LED2);
244 }
245 }
245 }
246 }
246 heap_end += incr;
247 heap_end += incr;
247 return (caddr_t) prev_heap_end;
248 return (caddr_t) prev_heap_end;
248 }
249 }
249
250
250 /***************************************************************************/
251 /***************************************************************************/
251
252
252 int _fstat_r (struct _reent *r, int file, struct stat * st)
253 int _fstat_r (struct _reent *r, int file, struct stat * st)
253 {
254 {
254 r = r;
255 r = r;
255 file = file;
256 file = file;
256
257
257 memset (st, 0, sizeof (* st));
258 memset (st, 0, sizeof (* st));
258 st->st_mode = S_IFCHR;
259 st->st_mode = S_IFCHR;
259 return 0;
260 return 0;
260 }
261 }
261
262
262 /***************************************************************************/
263 /***************************************************************************/
263 int _open_r(struct _reent *r,const char *name, int flags, int mode)
264 int _open_r(struct _reent *r,const char *name, int flags, int mode)
264 {
265 {
265 return _open(name, flags, mode);
266 return _open(name, flags, mode);
266 }
267 }
267
268
268 int _isatty_r(struct _reent *r, int fd)
269 int _isatty_r(struct _reent *r, int fd)
269 {
270 {
270 r = r;
271 r = r;
271 fd = fd;
272 fd = fd;
272
273
273 return 1;
274 return 1;
274 }
275 }
275
276
276
277
277 #ifdef __cplusplus
278 #ifdef __cplusplus
278 }
279 }
279 #endif
280 #endif
280
281
281
282
@@ -1,387 +1,388
1 /*------------------------------------------------------------------------------
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the libuc, microcontroler library
2 -- This file is a part of the libuc, microcontroler library
3 -- Copyright (C) 2012, Alexis Jeandet
3 -- Copyright (C) 2012, Alexis Jeandet
4 --
4 --
5 -- This program is free software; you can redistribute it and/or modify
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 3 of the License, or
7 -- the Free Software Foundation; either version 3 of the License, or
8 -- (at your option) any later version.
8 -- (at your option) any later version.
9 --
9 --
10 -- This program is distributed in the hope that it will be useful,
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
13 -- GNU General Public License for more details.
14 --
14 --
15 -- You should have received a copy of the GNU General Public License
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------
18 -------------------------------------------------------------------------------
19 -- Author : Alexis Jeandet
19 -- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@member.fsf.org
20 -- Mail : alexis.jeandet@member.fsf.org
21 -------------------------------------------------------------------------------*/
21 -------------------------------------------------------------------------------*/
22
22
23 #include <i2c.h>
23 #include <i2c.h>
24 #include <stm32f4xx_usart.h>
24 #include <stm32f4xx_usart.h>
25 #include <stm32f4xx_rcc.h>
25 #include <stm32f4xx_rcc.h>
26 #include <stm32f4xx_gpio.h>
26 #include <stm32f4xx_gpio.h>
27 #include <gpio.h>
27 #include <gpio.h>
28 #include <core.h>
28 #include <core.h>
29 #include <stdio.h>
29 #include <stdio.h>
30
30
31 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
31 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
32 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
32 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
33
33
34 int i2ctimeout=1000*1000;
34 int i2ctimeout=1000*1000;
35
35
36 I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3};
36 I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3};
37
37
38 i2c_t i2copen(int count)
38 i2c_t i2copen(int count)
39 {
39 {
40 #define _INIT_DEV(_RCC_) \
40 #define _INIT_DEV(_RCC_) \
41 RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \
41 RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \
42 RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \
42 RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \
43 RCC_APB1PeriphResetCmd(_RCC_, DISABLE);
43 RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \
44 RCC_APB1PeriphClockCmd(_RCC_, ENABLE);
44
45
45 switch(count)
46 switch(count)
46 {
47 {
47 case i2c1:
48 case i2c1:
48 _INIT_DEV(RCC_APB1Periph_I2C1);
49 _INIT_DEV(RCC_APB1Periph_I2C1);
49 return i2c1;
50 return i2c1;
50 break;
51 break;
51 case i2c2:
52 case i2c2:
52 _INIT_DEV(RCC_APB1Periph_I2C2);
53 _INIT_DEV(RCC_APB1Periph_I2C2);
53 return i2c2;
54 return i2c2;
54 break;
55 break;
55 case i2c3:
56 case i2c3:
56 _INIT_DEV(RCC_APB1Periph_I2C3);
57 _INIT_DEV(RCC_APB1Periph_I2C3);
57 return i2c3;
58 return i2c3;
58 break;
59 break;
59 default:
60 default:
60 break;
61 break;
61 }
62 }
62 return -1;
63 return -1;
63 }
64 }
64
65
65 i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL)
66 i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL)
66 {
67 {
67 i2c_t dev = i2copen(count);
68 i2c_t dev = i2copen(count);
68 if(dev!=-1)
69 if(dev!=-1)
69 {
70 {
70 i2cclose(dev);
71 i2cclose(dev);
71 i2csetpins(dev,SDA,SCL);
72 i2csetpins(dev,SDA,SCL);
72 i2copen(count);
73 i2copen(count);
73 i2csetspeed(dev,speed);
74 i2csetspeed(dev,speed);
74 i2cenable(count);
75 i2cenable(count);
75 }
76 }
76 return dev;
77 return dev;
77 }
78 }
78
79
79 int i2cclose(i2c_t dev)
80 int i2cclose(i2c_t dev)
80 {
81 {
81 switch(dev)
82 switch(dev)
82 {
83 {
83 case i2c1:
84 case i2c1:
84 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
85 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
85 break;
86 break;
86 case i2c2:
87 case i2c2:
87 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
88 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
88 break;
89 break;
89 case i2c3:
90 case i2c3:
90 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
91 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
91 break;
92 break;
92 default:
93 default:
93 break;
94 break;
94 }
95 }
95 return 1;
96 return 1;
96 }
97 }
97
98
98 int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL)
99 int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL)
99 {
100 {
100 if((dev<i2c4)&&(dev>=i2c1))
101 if((dev<i2c4)&&(dev>=i2c1))
101 {
102 {
102 gpio_t SDApin,SCLpin;
103 gpio_t SDApin,SCLpin;
103 SDApin = gpioopen(SDA);
104 SDApin = gpioopen(SDA);
104 SCLpin = gpioopen(SCL);
105 SCLpin = gpioopen(SCL);
105 SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
106 SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
106 SCLpin |= gpiolowspeed | gpiooutdir | gpioopendraintype | gpionopulltype;
107 SCLpin |= gpiolowspeed | gpiooutdir | gpioopendraintype | gpionopulltype;
107 gpiosetconfig(&SCLpin);
108 gpiosetconfig(&SCLpin);
108 for(int i=0;i<32;i++)
109 for(int i=0;i<32;i++)
109 {
110 {
110 gpioclr(SCLpin);
111 gpioclr(SCLpin);
111 for(int l=0;l<200;l++)
112 for(int l=0;l<200;l++)
112 {__asm__("nop");}
113 {__asm__("nop");}
113 gpioset(SCLpin);
114 gpioset(SCLpin);
114 for(int l=0;l<200;l++)
115 for(int l=0;l<200;l++)
115 {__asm__("nop");}
116 {__asm__("nop");}
116 }
117 }
117 SCLpin = gpioopen(SCL);
118 SCLpin = gpioopen(SCL);
118 SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
119 SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
119 gpiosetconfig(&SDApin);
120 gpiosetconfig(&SDApin);
120 gpiosetconfig(&SCLpin);
121 gpiosetconfig(&SCLpin);
121 uint8_t gpioAFi2cx = GPIO_AF_I2C1;
122 uint8_t gpioAFi2cx = GPIO_AF_I2C1;
122 switch(dev)
123 switch(dev)
123 {
124 {
124 case i2c1:
125 case i2c1:
125 gpioAFi2cx = GPIO_AF_I2C1;
126 gpioAFi2cx = GPIO_AF_I2C1;
126 break;
127 break;
127 case i2c2:
128 case i2c2:
128 gpioAFi2cx = GPIO_AF_I2C2;
129 gpioAFi2cx = GPIO_AF_I2C2;
129 break;
130 break;
130 case i2c3:
131 case i2c3:
131 gpioAFi2cx = GPIO_AF_I2C3;
132 gpioAFi2cx = GPIO_AF_I2C3;
132 break;
133 break;
133 default:
134 default:
134 break;
135 break;
135 }
136 }
136 GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx);
137 GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx);
137 GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx);
138 GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx);
138 return 0;
139 return 0;
139 }
140 }
140 return -1;
141 return -1;
141 }
142 }
142
143
143 int i2cenable(i2c_t dev)
144 int i2cenable(i2c_t dev)
144 {
145 {
145 if((dev<i2c4)&&(dev>=i2c1))
146 if((dev<i2c4)&&(dev>=i2c1))
146 {
147 {
147 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
148 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
148 _dev_->CR1 |=1 ;
149 _dev_->CR1 |=1 ;
149 return 0;
150 return 0;
150 }
151 }
151 return -1;
152 return -1;
152 }
153 }
153
154
154 int i2cdisable(i2c_t dev)
155 int i2cdisable(i2c_t dev)
155 {
156 {
156 if((dev<i2c4)&&(dev>=i2c1))
157 if((dev<i2c4)&&(dev>=i2c1))
157 {
158 {
158 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
159 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
159 _dev_->CR1 &= ~1;
160 _dev_->CR1 &= ~1;
160 return 0;
161 return 0;
161 }
162 }
162 return -1;
163 return -1;
163 }
164 }
164
165
165
166
166 int i2csetspeed(i2c_t dev,uint32_t speed)
167 int i2csetspeed(i2c_t dev,uint32_t speed)
167 {
168 {
168 if((dev<i2c4)&&(dev>=i2c1))
169 if((dev<i2c4)&&(dev>=i2c1))
169 {
170 {
170 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
171 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
171 int32_t APB1Freq=getAPB1Freq()/1000000;
172 int32_t APB1Freq=getAPB1Freq()/1000000;
172 if((APB1Freq>1)&&(APB1Freq<43))
173 if((APB1Freq>1)&&(APB1Freq<43))
173 {
174 {
174
175
175 uint16_t tmpreg=_dev_->CR2;
176 uint16_t tmpreg=_dev_->CR2;
176 tmpreg &= ~(0x1f);
177 tmpreg &= ~(0x1f);
177 tmpreg |= APB1Freq;
178 tmpreg |= APB1Freq;
178 _dev_->CR2=tmpreg;
179 _dev_->CR2=tmpreg;
179 i2cdisable(dev);
180 i2cdisable(dev);
180 tmpreg=_dev_->CCR;
181 tmpreg=_dev_->CCR;
181 APB1Freq=getAPB1Freq();
182 APB1Freq=getAPB1Freq();
182 if(speed>100000) //100kHz= standard mode, 400kHz= fast mode
183 if(speed>100000) //100kHz= standard mode, 400kHz= fast mode
183 {
184 {
184 if(speed<=400000)
185 if(speed<=400000)
185 {
186 {
186 tmpreg |= 1<<15;
187 tmpreg |= 1<<15;
187 tmpreg &= ~(1<<14);
188 tmpreg &= ~(1<<14);
188 tmpreg &= ~(0xfff);
189 tmpreg &= ~(0xfff);
189 tmpreg |= 0xfff & (APB1Freq/(3*speed));
190 tmpreg |= 0xfff & (APB1Freq/(3*speed));
190 }
191 }
191 }
192 }
192 else
193 else
193 {
194 {
194 tmpreg &= ~(1<<15);
195 tmpreg &= ~(1<<15);
195 tmpreg &= ~(0xfff);
196 tmpreg &= ~(0xfff);
196 tmpreg |= 0xfff & (APB1Freq/(2*speed));
197 tmpreg |= 0xfff & (APB1Freq/(2*speed));
197 }
198 }
198 _dev_->CCR=tmpreg;
199 _dev_->CCR=tmpreg;
199 tmpreg=_dev_->TRISE;
200 tmpreg=_dev_->TRISE;
200 tmpreg &= ~(0x3f);
201 tmpreg &= ~(0x3f);
201 tmpreg |= (APB1Freq/1000000)+1;
202 tmpreg |= (APB1Freq/1000000)+1;
202 _dev_->TRISE = tmpreg;
203 _dev_->TRISE = tmpreg;
203 i2cenable(dev);
204 i2cenable(dev);
204 return 0;
205 return 0;
205 }
206 }
206 }
207 }
207 return -1;
208 return -1;
208 }
209 }
209
210
210 int i2cbusy(i2c_t dev)
211 int i2cbusy(i2c_t dev)
211 {
212 {
212 if((dev<i2c4)&&(dev>=i2c1))
213 if((dev<i2c4)&&(dev>=i2c1))
213 {
214 {
214 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
215 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
215 if((_dev_->SR2 & 2) ==2) return 1; /* Dev is busy */
216 if((_dev_->SR2 & 2) ==2) return 1; /* Dev is busy */
216 return 0; /* Dev isn't busy */
217 return 0; /* Dev isn't busy */
217 }
218 }
218 return -1; /* Error, dev is out of range */
219 return -1; /* Error, dev is out of range */
219 }
220 }
220
221
221 int i2cwrite(i2c_t dev,char address,char* data,int count)
222 int i2cwrite(i2c_t dev,char address,char* data,int count)
222 {
223 {
223 if((dev<i2c4)&&(dev>=i2c1))
224 if((dev<i2c4)&&(dev>=i2c1))
224 {
225 {
225 int timeout=i2ctimeout;
226 int timeout=i2ctimeout;
226 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
227 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
227 while(i2cbusy(dev))
228 while(i2cbusy(dev))
228 {
229 {
229 if(0==(timeout--))
230 if(0==(timeout--))
230 {
231 {
231 printf("Exited on timeout @ line %d\n\r",__LINE__);
232 printf("Exited on timeout @ line %d\n\r",__LINE__);
232 return -1;
233 return -1;
233 }
234 }
234 }
235 }
235 _dev_->CR1 |= 1<<8;
236 _dev_->CR1 |= 1<<8;
236 timeout=i2ctimeout;
237 timeout=i2ctimeout;
237 while(!i2cStatusCheck(dev,((uint32_t)0x00030001)))
238 while(!i2cStatusCheck(dev,((uint32_t)0x00030001)))
238 {
239 {
239 if(0==(timeout--))
240 if(0==(timeout--))
240 {
241 {
241 printf("Exited on timeout @ line %d\n\r",__LINE__);
242 printf("Exited on timeout @ line %d\n\r",__LINE__);
242 return -1;
243 return -1;
243 }
244 }
244 }
245 }
245 _dev_->DR= address<<1;
246 _dev_->DR= address<<1;
246 timeout=i2ctimeout;
247 timeout=i2ctimeout;
247 while(!i2cStatusCheck(dev, ((uint32_t)0x00070082)))
248 while(!i2cStatusCheck(dev, ((uint32_t)0x00070082)))
248 {
249 {
249 if(0==(timeout--))
250 if(0==(timeout--))
250 {
251 {
251 printf("Exited on timeout @ line %d\n\r",__LINE__);
252 printf("Exited on timeout @ line %d\n\r",__LINE__);
252 return -1;
253 return -1;
253 }
254 }
254 }
255 }
255 address=_dev_->SR2;
256 address=_dev_->SR2;
256 for(int i=0;i<count;i++)
257 for(int i=0;i<count;i++)
257 {
258 {
258 timeout=i2ctimeout;
259 timeout=i2ctimeout;
259 while(!i2cStatusCheck(dev,((uint32_t)0x00070080)))
260 while(!i2cStatusCheck(dev,((uint32_t)0x00070080)))
260 {
261 {
261 if(0==(timeout--))
262 if(0==(timeout--))
262 {
263 {
263 printf("Exited on timeout @ line %d\n\r",__LINE__);
264 printf("Exited on timeout @ line %d\n\r",__LINE__);
264 return -1;
265 return -1;
265 }
266 }
266 }
267 }
267 _dev_->DR= data[i];
268 _dev_->DR= data[i];
268 }
269 }
269 timeout=i2ctimeout;
270 timeout=i2ctimeout;
270 while(!i2cStatusCheck(dev,1<<7))
271 while(!i2cStatusCheck(dev,1<<7))
271 {
272 {
272 if(0==(timeout--))
273 if(0==(timeout--))
273 {
274 {
274 printf("Exited on timeout @ line %d\n\r",__LINE__);
275 printf("Exited on timeout @ line %d\n\r",__LINE__);
275 return -1;
276 return -1;
276 }
277 }
277 }
278 }
278 timeout=i2ctimeout;
279 timeout=i2ctimeout;
279 while(!i2cStatusCheck(dev,1<<2))
280 while(!i2cStatusCheck(dev,1<<2))
280 {
281 {
281 if(0==(timeout--))
282 if(0==(timeout--))
282 {
283 {
283 printf("Exited on timeout @ line %d\n\r",__LINE__);
284 printf("Exited on timeout @ line %d\n\r",__LINE__);
284 return -1;
285 return -1;
285 }
286 }
286 }
287 }
287 _dev_->CR1 |= 1<<9;
288 _dev_->CR1 |= 1<<9;
288 return count;
289 return count;
289 }
290 }
290 return -1;
291 return -1;
291 }
292 }
292
293
293 int i2cread(i2c_t dev,char address,char* data,int count)
294 int i2cread(i2c_t dev,char address,char* data,int count)
294 {
295 {
295 if((dev<i2c4)&&(dev>=i2c1))
296 if((dev<i2c4)&&(dev>=i2c1))
296 {
297 {
297 int i=0;
298 int i=0;
298 int timeout=i2ctimeout;
299 int timeout=i2ctimeout;
299 while(i2cbusy(dev))
300 while(i2cbusy(dev))
300 {
301 {
301 if(0==(timeout--))
302 if(0==(timeout--))
302 {
303 {
303 printf("Exited on timeout @ line %d\n\r",__LINE__);
304 printf("Exited on timeout @ line %d\n\r",__LINE__);
304 return -1;
305 return -1;
305 }
306 }
306 }
307 }
307 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
308 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
308 _dev_->CR1 |= (1<<8) + (1<<10);
309 _dev_->CR1 |= (1<<8) + (1<<10);
309 timeout=i2ctimeout;
310 timeout=i2ctimeout;
310 while(!i2cStatusCheck(dev,0x00030001))
311 while(!i2cStatusCheck(dev,0x00030001))
311 {
312 {
312 if(0==(timeout--))
313 if(0==(timeout--))
313 {
314 {
314 printf("Exited on timeout @ line %d\n\r",__LINE__);
315 printf("Exited on timeout @ line %d\n\r",__LINE__);
315 return -1;
316 return -1;
316 }
317 }
317 }
318 }
318 _dev_->DR= (address<<1) + 1;
319 _dev_->DR= (address<<1) + 1;
319 while(!i2cStatusCheck(dev,0x000002))
320 while(!i2cStatusCheck(dev,0x000002))
320 {
321 {
321 if(0==(timeout--))
322 if(0==(timeout--))
322 {
323 {
323 printf("Exited on timeout @ line %d\n\r",__LINE__);
324 printf("Exited on timeout @ line %d\n\r",__LINE__);
324 return -1;
325 return -1;
325 }
326 }
326 }
327 }
327 if(count==1)
328 if(count==1)
328 {
329 {
329 _dev_->CR1 &= ~(1<<10);
330 _dev_->CR1 &= ~(1<<10);
330 }
331 }
331 address=_dev_->SR2;
332 address=_dev_->SR2;
332 for(i=0;i<(count-1);i++)
333 for(i=0;i<(count-1);i++)
333 {
334 {
334 timeout=i2ctimeout;
335 timeout=i2ctimeout;
335 while(!i2cStatusCheck(dev,0x0000040))
336 while(!i2cStatusCheck(dev,0x0000040))
336 {
337 {
337 if(0==(timeout--))
338 if(0==(timeout--))
338 {
339 {
339 printf("Exited on timeout @ line %d\n\r",__LINE__);
340 printf("Exited on timeout @ line %d\n\r",__LINE__);
340 return -1;
341 return -1;
341 }
342 }
342 }
343 }
343 data[i]=_dev_->DR;
344 data[i]=_dev_->DR;
344 }
345 }
345 _dev_->CR1 &= ~(1<<10);
346 _dev_->CR1 &= ~(1<<10);
346 _dev_->CR1 |= 1<<9;
347 _dev_->CR1 |= 1<<9;
347 timeout=i2ctimeout;
348 timeout=i2ctimeout;
348 while(!i2cStatusCheck(dev,0x0000040))
349 while(!i2cStatusCheck(dev,0x0000040))
349 {
350 {
350 if(0==(timeout--))
351 if(0==(timeout--))
351 {
352 {
352 printf("Exited on timeout @ line %d\n\r",__LINE__);
353 printf("Exited on timeout @ line %d\n\r",__LINE__);
353 return -1;
354 return -1;
354 }
355 }
355 }
356 }
356 data[i]=_dev_->DR;
357 data[i]=_dev_->DR;
357 timeout=i2ctimeout;
358 timeout=i2ctimeout;
358 while(_dev_->CR1 & ((uint16_t)0x0200))
359 while(_dev_->CR1 & ((uint16_t)0x0200))
359 {
360 {
360 if(0==(timeout--))
361 if(0==(timeout--))
361 {
362 {
362 printf("Exited on timeout @ line %d\n\r",__LINE__);
363 printf("Exited on timeout @ line %d\n\r",__LINE__);
363 return -1;
364 return -1;
364 }
365 }
365 }
366 }
366 _dev_->CR1 |= 1<<10;
367 _dev_->CR1 |= 1<<10;
367 return count;
368 return count;
368 }
369 }
369 return -1;
370 return -1;
370 }
371 }
371
372
372
373
373 int i2cStatusCheck(i2c_t dev,int32_t flagMask)
374 int i2cStatusCheck(i2c_t dev,int32_t flagMask)
374 {
375 {
375 int32_t flag;
376 int32_t flag;
376 if((dev<i2c4)&&(dev>=i2c1))
377 if((dev<i2c4)&&(dev>=i2c1))
377 {
378 {
378 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
379 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
379 flag= _dev_->SR1 + (_dev_->SR2<<16);
380 flag= _dev_->SR1 + (_dev_->SR2<<16);
380 if(flagMask==(flag & flagMask))
381 if(flagMask==(flag & flagMask))
381 return 1;
382 return 1;
382 return 0;
383 return 0;
383 }
384 }
384 return -1;
385 return -1;
385 }
386 }
386
387
387
388
General Comments 0
You need to be logged in to leave comments. Login now