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