##// END OF EJS Templates
sync
jeandet@PC-DE-JEANDET.lab-lpp.local -
r24:5d5838023b3d default
parent child
Show More
@@ -1,136 +1,136
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2011, 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@gmail.com
21 21 -------------------------------------------------------------------------------*/
22 22 #include "bsp.h"
23 23 #include <streamdevices.h>
24 24 #include <malloc.h>
25 25 #include <gpio.h>
26 26 #include <uart.h>
27 27 #include <stdio.h>
28 28 #include <i2c.h>
29 29
30 30 uint32_t OSC0 =8000000;
31 31 uint32_t INTOSC =16000000;
32 32 uint32_t RTCOSC =32768;
33 33 uint32_t currentCpuFreq=0;
34 34 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
35 35
36 36 float VREF0 =(float)3.3;
37 37
38 38 int bsp_init()
39 39 {
40 40 int i=0;
41 41 for(i=0;i<32;i++)
42 42 {
43 43 __opnfiles__[i] = NULL;
44 44 }
45 45 bsp_GPIO_init();
46 46 bsp_uart_init();
47 47 bsp_iic_init();
48 48 printf("\r================================================================\n\r");
49 49 printf("================================================================\n\r");
50 50 printf(BSP);
51 51 printf(" initialised\n\r");
52 52 printf("================================================================\n\r");
53 53 return 1;
54 54 }
55 55
56 56 void bsp_GPIO_init()
57 57 {
58 58 gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009
59 59 gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2);
60 60 gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2);
61 61 gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2);
62 62 gpiosetspeed(&gpio1,gpiohighspeed);
63 63 gpiosetspeed(&gpio2,gpiohighspeed);
64 64 gpiosetspeed(&gpio3,gpiohighspeed);
65 65 gpiosetspeed(&gpio4,gpiohighspeed);
66 66 gpiosetdir(&gpio1,gpiooutdir);
67 67 gpiosetdir(&gpio3,gpiooutdir);
68 68 gpiosetdir(&gpio2,gpiooutdir);
69 69 gpiosetdir(&gpio4,gpiooutdir);
70 70 }
71 71
72 72 void bsp_uart_init()
73 73 {
74 74 if(__opnfiles__[1]==NULL)
75 75 {
76 76 uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
77 77 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
78 78 uartopenandconfig(2,uart1,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
79 79 uartmkstreamdev(uart1,fd1);
80 80 __opnfiles__[1] = fd1;
81 81 }
82 82 else
83 83 {
84 84 uartopenandconfig(2,(uart_t*)__opnfiles__[1]->_stream,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
85 85 }
86 86 }
87 87
88 88 void bsp_spi_init()
89 89 {
90 90
91 91 }
92 92
93 93
94 94 void bsp_iic_init()
95 95 {
96 if(0==i2copenandconfig(i2c1,0,100000,PB9,PB6))printf("I2C1 opened\n\r");
96 if(0==i2copenandconfig(i2c1,0,400000,PB9,PB6))printf("I2C1 opened\n\r");
97 97 i2cenable(i2c1);
98 98 }
99 99
100 100 void bsp_SD_init()
101 101 {
102 102
103 103 }
104 104
105 105 void vs10XXclearXCS(){}
106 106 void vs10XXsetXCS(){}
107 107 int vs10XXDREQ()
108 108 {
109 109 return 1;
110 110 }
111 111
112 112
113 113 void bsppowersdcard(char onoff) //always ON
114 114 {
115 115
116 116 }
117 117
118 118 char bspsdcardpresent()
119 119 {
120 120 return 0;
121 121 }
122 122
123 123 char bspsdcardwriteprotected()
124 124 {
125 125 return 0;
126 126 }
127 127
128 128 void bspsdcardselect(char YESNO)
129 129 {
130 130
131 131 }
132 132
133 133
134 134
135 135
136 136
@@ -1,41 +1,45
1 1 #include <stdio.h>
2 2 #include <fat32.h>
3 3 #include <gpio.h>
4 4 #include <uart.h>
5 5 #include <stm32f4xx.h>
6 6 #include <bsp.h>
7 7 #include <i2c.h>
8 8
9 9
10 10 extern streamdevice* __opnfiles__[];
11 11
12 12 int main()
13 13 {
14 14 //gpioset(PSU_DISABLE);
15 gpio_t dacRst=gpioopen(PD4);
16 gpiosetspeed(&dacRst,gpiohighspeed);
17 gpiosetdir(&dacRst,gpiooutdir);
18 gpioset(dacRst);
15 19 printf("i2C Control register 1 (I2C_CR1) = %x\n\r",I2C1->CR1);
16 20 printf("i2C Control register 2 (I2C_CR2) = %x\n\r",I2C1->CR2);
17 21 printf("i2C Status register 1 (I2C_SR1) = %x\n\r",I2C1->SR1);
18 22 printf("i2C Status register 2 (I2C_SR2) = %x\n\r",I2C1->SR2);
19 23 printf("i2C Clock control register (I2C_CCR) = %x\n\r",I2C1->CCR);
20 24 while(1)
21 25 {
22 26 for(volatile int i=0;i<1024*2048;i++);
23 27 gpioset(LED1);
24 28 gpioclr(LED2);
25 29 for(volatile int i=0;i<1024*2048;i++);
26 30 gpioclr(LED1);
27 31 gpioset(LED2);
28 i2cwrite(i2c1,0x4a,"test",4);
32 i2cwrite(i2c1,0x4a,"test",1);
29 33 }
30 34 printf("hello world\n\r");
31 35 return 0;
32 36 }
33 37
34 38
35 39
36 40
37 41
38 42
39 43
40 44
41 45
@@ -1,71 +1,72
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@gmail.com
21 21 -------------------------------------------------------------------------------*/
22 22 #ifndef I2C_H
23 23 #define I2C_H
24 24 #include <stdint.h>
25 25 #include <uhandle.h>
26 26 #include <streamdevices.h>
27 27
28 28 #ifdef __cplusplus
29 29 extern "C" {
30 30 #endif
31 31
32 32 typedef int i2c_t;
33 33
34 34 #define i2c1 0
35 35 #define i2c2 1
36 36 #define i2c3 2
37 37 #define i2c4 3
38 38 #define i2c5 4
39 39 #define i2c6 5
40 40 #define i2c7 6
41 41 #define i2c8 7
42 42 #define i2c9 8
43 43
44 44 extern i2c_t i2copen(int count);
45 45 extern i2c_t i2copenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL);
46 46 extern int i2cclose(i2c_t dev);
47 47 extern int i2csetpins(i2c_t dev,uint32_t SDA,uint32_t SCL);
48 48 extern int i2cenable(i2c_t dev);
49 49 extern int i2cdisable(i2c_t dev);
50 50 //extern int i2csetconfig(i2c_t dev);
51 51 extern int i2csetspeed(i2c_t dev,uint32_t speed);
52 52 //extern int i2csetdatabits(i2c_t* dev,uartbits_t databits);
53 53 extern int i2cwrite(i2c_t dev,char address,char* data,int count);
54 54 extern int i2cread(i2c_t dev,char address,char* data,int count);
55
55 extern int i2cbusy(i2c_t dev);
56 extern int i2cStatusCheck(i2c_t dev,int32_t flagMask);
56 57 #ifdef __cplusplus
57 58 }
58 59 #endif
59 60 #endif //I2C_H
60 61
61 62
62 63
63 64
64 65
65 66
66 67
67 68
68 69
69 70
70 71
71 72
@@ -1,254 +1,282
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@gmail.com
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
30 30 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
31 31 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
32 32
33 33
34 34 I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3};
35 35
36 36 i2c_t i2copen(int count)
37 37 {
38 38 #define _INIT_DEV(_RCC_) \
39 39 RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \
40 40 RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \
41 41 RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \
42 42 RCC_APB1PeriphClockCmd(_RCC_, ENABLE);
43 43
44 44 switch(count)
45 45 {
46 46 case 0:
47 47 _INIT_DEV(RCC_APB1Periph_I2C1);
48 48 return (i2c_t) 0;
49 49 break;
50 50 case 1:
51 51 _INIT_DEV(RCC_APB1Periph_I2C2);
52 52 return (i2c_t) 1;
53 53 break;
54 54 case 2:
55 55 _INIT_DEV(RCC_APB1Periph_I2C3);
56 56 return (i2c_t) 2;
57 57 break;
58 58 default:
59 59 break;
60 60 }
61 61 return -1;
62 62 }
63 63
64 64 i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL)
65 65 {
66 66 i2c_t dev = i2copen(count);
67 67 if(dev!=-1)
68 68 {
69 i2cclose(dev);
70 i2csetpins(dev,SDA,SCL);
71 i2copen(count);
69 72 i2cenable(count);
70 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
71 _dev_->CR1 = (1<<15);
72 _dev_->CR1 &=~(1<<15);
73 i2cdisable(count);
74 i2csetpins(dev,SDA,SCL);
73 //I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
75 74 i2csetspeed(dev,speed);
76 75 i2cenable(count);
77 _dev_->CR1|=1<<15;
78 _dev_->CR1&=~(1<<15);
79 76 }
80 77 return dev;
81 78 }
82 79
83 80 int i2cclose(i2c_t dev)
84 81 {
85 82 switch((int)dev)
86 83 {
87 84 case (int)0:
88 85 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
89 86 break;
90 87 case (int)1:
91 88 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
92 89 break;
93 90 case (int)2:
94 91 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
95 92 break;
96 93 default:
97 94 break;
98 95 }
99 96 return 1;
100 97 }
101 98
102 99 int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL)
103 100 {
104 101 if((dev<3)&&(dev>=0))
105 102 {
106 103 gpio_t SDApin,SCLpin;
107 104 SDApin = gpioopen(SDA);
108 105 SCLpin = gpioopen(SCL);
109 106 SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
110 107 SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
111 108 gpiosetconfig(&SDApin);
112 109 gpiosetconfig(&SCLpin);
113 110 uint8_t gpioAFi2cx = GPIO_AF_I2C1;
114 111 switch((int)dev)
115 112 {
116 113 case 0:
117 114 gpioAFi2cx = GPIO_AF_I2C1;
118 115 break;
119 116 case 1:
120 117 gpioAFi2cx = GPIO_AF_I2C2;
121 118 break;
122 119 case 2:
123 120 gpioAFi2cx = GPIO_AF_I2C3;
124 121 break;
125 122 default:
126 123 break;
127 124 }
128 125 GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx);
129 126 GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx);
130 127 return 0;
131 128 }
132 129 return -1;
133 130 }
134 131
135 132 int i2cenable(i2c_t dev)
136 133 {
137 134 if((dev<3)&&(dev>=0))
138 135 {
139 136 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
140 137 _dev_->CR1 |=1 ;
141 138 return 0;
142 139 }
143 140 return -1;
144 141 }
145 142
146 143 int i2cdisable(i2c_t dev)
147 144 {
148 145 if((dev<3)&&(dev>=0))
149 146 {
150 147 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
151 148 _dev_->CR1 &= ~1;
152 149 return 0;
153 150 }
154 151 return -1;
155 152 }
156 153
157 154
158 155 int i2csetspeed(i2c_t dev,uint32_t speed)
159 156 {
160 157 if((dev<3)&&(dev>=0))
161 158 {
162 159 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
163 160 int32_t APB1Freq=getAPB1Freq()/1000000;
164 161 if((APB1Freq>1)&&(APB1Freq<43))
165 162 {
166 i2cdisable(dev);
163
167 164 uint16_t tmpreg=_dev_->CR2;
168 165 tmpreg &= ~(0x1f);
169 166 tmpreg |= APB1Freq;
170 167 _dev_->CR2=tmpreg;
171 tmpreg=_dev_->TRISE;
172 tmpreg &= ~(0x3f);
173 tmpreg |= APB1Freq+1;
174 _dev_->TRISE = tmpreg;
168 i2cdisable(dev);
175 169 tmpreg=_dev_->CCR;
176 170 APB1Freq=getAPB1Freq();
177 171 if(speed>100000) //100kHz= standard mode, 400kHz= fast mode
178 172 {
179 173 if(speed<=400000)
180 174 {
181 175 tmpreg |= 1<<15;
182 176 tmpreg &= ~(1<<14);
183 177 tmpreg &= ~(0xfff);
184 178 tmpreg |= 0xfff & (APB1Freq/(3*speed));
185 179 }
186 180 }
187 181 else
188 182 {
189 183 tmpreg &= ~(1<<15);
190 184 tmpreg &= ~(0xfff);
191 185 tmpreg |= 0xfff & (APB1Freq/(2*speed));
192 186 }
193 187 _dev_->CCR=tmpreg;
188 tmpreg=_dev_->TRISE;
189 tmpreg &= ~(0x3f);
190 tmpreg |= (APB1Freq/1000000)+1;
191 _dev_->TRISE = tmpreg;
192 i2cenable(dev);
194 193 return 0;
195 194 }
196 195 }
197 196 return -1;
198 197 }
199 198
199 int i2cbusy(i2c_t dev)
200 {
201 if((dev<3)&&(dev>=0))
202 {
203 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
204 if((_dev_->SR2 & 2) ==2) return 1; /* Dev is busy */
205 return 0; /* Dev isn't busy */
206 }
207 return -1; /* Error, dev is out of range */
208 }
209
200 210 int i2cwrite(i2c_t dev,char address,char* data,int count)
201 211 {
202 212 if((dev<3)&&(dev>=0))
203 213 {
204 214 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
215 while(i2cbusy(dev));
205 216 _dev_->CR1 |= 1<<8;
206 while((_dev_->SR1&1)==0);
217 while(!i2cStatusCheck(dev,((uint32_t)0x00030001)));
207 218 _dev_->DR= address<<1;
208 while((_dev_->SR1 & (1<<3))==0);
219 while(!i2cStatusCheck(dev, ((uint32_t)0x00070082)));
209 220 address=_dev_->SR2;
210 221 for(int i=0;i<count;i++)
211 222 {
212 while((_dev_->SR1 & (1<<7))==0);
223 while(!i2cStatusCheck(dev,((uint32_t)0x00070080)));
213 224 _dev_->DR= data[i];
214 225 }
215 while((_dev_->SR1 & (1<<7))==0);
216 while((_dev_->SR1 & (1<<2))==0);
226 while(!i2cStatusCheck(dev,1<<7));
227 while(!i2cStatusCheck(dev,1<<2));
217 228 _dev_->CR1 |= 1<<9;
218 229 return count;
219 230 }
220 231 return -1;
221 232 }
222 233
223 234 int i2cread(i2c_t dev,char address,char* data,int count)
224 235 {
225 236 if((dev<3)&&(dev>=0))
226 237 {
238 while(i2cbusy(dev));
227 239 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
228 240 _dev_->CR1 |= (1<<8) | (1<<10);
229 241 if(count==1)
230 242 {
231 243 _dev_->CR1 &= ~(1<<10);
232 244 }
233 while((_dev_->SR1&1)==0);
245 while(!i2cStatusCheck(dev,0x00030001));
234 246 _dev_->DR= (address<<1) + 1;
235 while((_dev_->SR1 & (1<<3))==0);
247 while(!i2cStatusCheck(dev, ((uint32_t)0x00070082)));
236 248 address=_dev_->SR2;
237 249 for(int i=0;i<count-1;i++)
238 250 {
239 251 while((_dev_->SR1 & (1<<6))==0);
240 252 data[i]=_dev_->DR;
241 253 }
242 254 _dev_->CR1 &= ~(1<<10);
243 255 _dev_->CR1 |= 1<<9;
244 256 while((_dev_->SR1 & (1<<6))==0);
245 257 data[count-1]=_dev_->DR;
246 258 return count;
247 259 }
248 260 return -1;
249 261 }
250 262
251 263
264 int i2cStatusCheck(i2c_t dev,int32_t flagMask)
265 {
266 int32_t flag;
267 __asm__("nop");
268 __asm__("nop");
269 __asm__("nop");
270 __asm__("nop");
271 if((dev<3)&&(dev>=0))
272 {
273 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
274 flag= _dev_->SR1 + (_dev_->SR2<<16);
275 if(flagMask==(flag & flagMask))
276 return 1;
277 return 0;
278 }
279 return -1;
280 }
252 281
253 282
254
General Comments 0
You need to be logged in to leave comments. Login now