##// END OF EJS Templates
sync
jeandet@PC-DE-JEANDET.lab-lpp.local -
r22:3d19a50d01c6 default
parent child
Show More
@@ -0,0 +1,12
1 TEMPLATE = app
2 CONFIG += console
3 CONFIG -= qt
4
5 #BSP = BEAGLESYNTH
6 BSP = SOLAR_LFR_PSU
7 include($$(libuc2)/bsp/cfg/$$BSP/bsp.pri)
8 #include($$(libuc2)/rules/stm32f4-arm-none-eabi-gcc/rules.pri)
9
10 SOURCES += \
11 main.c
12
@@ -0,0 +1,121
1 #!/usr/bin/python
2
3 # Written by Antonio Galea - 2010/11/18
4 # Distributed under Gnu LGPL 3.0
5 # see http://www.gnu.org/licenses/lgpl-3.0.txt
6
7 import sys,struct,zlib,os
8 from optparse import OptionParser
9
10 DEFAULT_DEVICE="0x0483:0xdf11"
11
12 def named(tuple,names):
13 return dict(zip(names.split(),tuple))
14 def consume(fmt,data,names):
15 n = struct.calcsize(fmt)
16 return named(struct.unpack(fmt,data[:n]),names),data[n:]
17 def cstring(string):
18 return string.split('\0',1)[0]
19 def compute_crc(data):
20 return 0xFFFFFFFF & -zlib.crc32(data) -1
21
22 def parse(file,dump_images=False):
23 print 'File: "%s"' % file
24 data = open(file,'rb').read()
25 crc = compute_crc(data[:-4])
26 prefix, data = consume('<5sBIB',data,'signature version size targets')
27 print '%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix
28 for t in range(prefix['targets']):
29 tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements')
30 tprefix['num'] = t
31 if tprefix['named']:
32 tprefix['name'] = cstring(tprefix['name'])
33 else:
34 tprefix['name'] = ''
35 print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix
36 tsize = tprefix['size']
37 target, data = data[:tsize], data[tsize:]
38 for e in range(tprefix['elements']):
39 eprefix, target = consume('<2I',target,'address size')
40 eprefix['num'] = e
41 print ' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix
42 esize = eprefix['size']
43 image, target = target[:esize], target[esize:]
44 if dump_images:
45 out = '%s.target%d.image%d.bin' % (file,t,e)
46 open(out,'wb').write(image)
47 print ' DUMPED IMAGE TO "%s"' % out
48 if len(target):
49 print "target %d: PARSE ERROR" % t
50 suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc')
51 print 'usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix
52 if crc != suffix['crc']:
53 print "CRC ERROR: computed crc32 is 0x%08x" % crc
54 data = data[16:]
55 if data:
56 print "PARSE ERROR"
57
58 def build(file,targets,device=DEFAULT_DEVICE):
59 data = ''
60 for t,target in enumerate(targets):
61 tdata = ''
62 for image in target:
63 tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data']
64 tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata
65 data += tdata
66 data = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data
67 v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
68 data += struct.pack('<4H3sB',0,d,v,0x011a,'UFD',16)
69 crc = compute_crc(data)
70 data += struct.pack('<I',crc)
71 open(file,'wb').write(data)
72
73 if __name__=="__main__":
74 usage = """
75 %prog [-d|--dump] infile.dfu
76 %prog {-b|--build} address:file.bin [-b address:file.bin ...] [{-D|--device}=vendor:device] outfile.dfu"""
77 parser = OptionParser(usage=usage)
78 parser.add_option("-b", "--build", action="append", dest="binfiles",
79 help="build a DFU file from given BINFILES", metavar="BINFILES")
80 parser.add_option("-D", "--device", action="store", dest="device",
81 help="build for DEVICE, defaults to %s" % DEFAULT_DEVICE, metavar="DEVICE")
82 parser.add_option("-d", "--dump", action="store_true", dest="dump_images",
83 default=False, help="dump contained images to current directory")
84 (options, args) = parser.parse_args()
85
86 if options.binfiles and len(args)==1:
87 target = []
88 for arg in options.binfiles:
89 try:
90 address,binfile = arg.split(':',1)
91 except ValueError:
92 print "Address:file couple '%s' invalid." % arg
93 sys.exit(1)
94 try:
95 address = int(address,0) & 0xFFFFFFFF
96 except ValueError:
97 print "Address %s invalid." % address
98 sys.exit(1)
99 if not os.path.isfile(binfile):
100 print "Unreadable file '%s'." % binfile
101 sys.exit(1)
102 target.append({ 'address': address, 'data': open(binfile,'rb').read() })
103 outfile = args[0]
104 device = DEFAULT_DEVICE
105 if options.device:
106 device=options.device
107 try:
108 v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
109 except:
110 print "Invalid device '%s'." % device
111 sys.exit(1)
112 build(outfile,[target],device)
113 elif len(args)==1:
114 infile = args[0]
115 if not os.path.isfile(infile):
116 print "Unreadable file '%s'." % infile
117 sys.exit(1)
118 parse(infile, dump_images=options.dump_images)
119 else:
120 parser.print_help()
121 sys.exit(1)
@@ -0,0 +1,41
1 #include <stdio.h>
2 #include <fat32.h>
3 #include <gpio.h>
4 #include <uart.h>
5 #include <stm32f4xx.h>
6 #include <bsp.h>
7 #include <i2c.h>
8
9
10 extern streamdevice* __opnfiles__[];
11
12 int main()
13 {
14 //gpioset(PSU_DISABLE);
15 printf("i2C Control register 1 (I2C_CR1) = %x\n\r",I2C1->CR1);
16 printf("i2C Control register 2 (I2C_CR2) = %x\n\r",I2C1->CR2);
17 printf("i2C Status register 1 (I2C_SR1) = %x\n\r",I2C1->SR1);
18 printf("i2C Status register 2 (I2C_SR2) = %x\n\r",I2C1->SR2);
19 printf("i2C Clock control register (I2C_CCR) = %x\n\r",I2C1->CCR);
20 while(1)
21 {
22 for(volatile int i=0;i<1024*2048;i++);
23 gpioset(LED1);
24 gpioclr(LED2);
25 for(volatile int i=0;i<1024*2048;i++);
26 gpioclr(LED1);
27 gpioset(LED2);
28 i2cwrite(i2c1,0x4a,"test",4);
29 }
30 printf("hello world\n\r");
31 return 0;
32 }
33
34
35
36
37
38
39
40
41
@@ -0,0 +1,71
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the libuc, microcontroler library
3 -- Copyright (C) 2012, Alexis Jeandet
4 --
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
7 -- the Free Software Foundation; either version 3 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
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
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------
19 -- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@gmail.com
21 -------------------------------------------------------------------------------*/
22 #ifndef I2C_H
23 #define I2C_H
24 #include <stdint.h>
25 #include <uhandle.h>
26 #include <streamdevices.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 typedef int i2c_t;
33
34 #define i2c1 0
35 #define i2c2 1
36 #define i2c3 2
37 #define i2c4 3
38 #define i2c5 4
39 #define i2c6 5
40 #define i2c7 6
41 #define i2c8 7
42 #define i2c9 8
43
44 extern i2c_t i2copen(int count);
45 extern i2c_t i2copenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL);
46 extern int i2cclose(i2c_t dev);
47 extern int i2csetpins(i2c_t dev,uint32_t SDA,uint32_t SCL);
48 extern int i2cenable(i2c_t dev);
49 extern int i2cdisable(i2c_t dev);
50 //extern int i2csetconfig(i2c_t dev);
51 extern int i2csetspeed(i2c_t dev,uint32_t speed);
52 //extern int i2csetdatabits(i2c_t* dev,uartbits_t databits);
53 extern int i2cwrite(i2c_t dev,char address,char* data,int count);
54 extern int i2cread(i2c_t dev,char address,char* data,int count);
55
56 #ifdef __cplusplus
57 }
58 #endif
59 #endif //I2C_H
60
61
62
63
64
65
66
67
68
69
70
71
@@ -0,0 +1,246
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the libuc, microcontroler library
3 -- Copyright (C) 2012, Alexis Jeandet
4 --
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
7 -- the Free Software Foundation; either version 3 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
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
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------
19 -- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@gmail.com
21 -------------------------------------------------------------------------------*/
22
23 #include <i2c.h>
24 #include <stm32f4xx_usart.h>
25 #include <stm32f4xx_rcc.h>
26 #include <stm32f4xx_gpio.h>
27 #include <gpio.h>
28 #include <core.h>
29
30 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
31 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
32
33
34 I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3};
35
36 i2c_t i2copen(int count)
37 {
38 #define _INIT_DEV(_RCC_) \
39 RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \
40 RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \
41 RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \
42 RCC_APB1PeriphClockCmd(_RCC_, ENABLE);
43
44 switch(count)
45 {
46 case 0:
47 _INIT_DEV(RCC_APB1Periph_I2C1);
48 return (i2c_t) 0;
49 break;
50 case 1:
51 _INIT_DEV(RCC_APB1Periph_I2C2);
52 return (i2c_t) 1;
53 break;
54 case 2:
55 _INIT_DEV(RCC_APB1Periph_I2C3);
56 return (i2c_t) 2;
57 break;
58 default:
59 break;
60 }
61 return -1;
62 }
63
64 i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL)
65 {
66 i2c_t dev = i2copen(count);
67 printf("dev = %d\n\r",dev);
68 if(dev!=-1)
69 {
70 i2cenable(count);
71 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
72 _dev_->CR1 = (1<<15);
73 _dev_->CR1 &=~(1<<15);
74 i2cdisable(count);
75 i2csetpins(dev,SDA,SCL);
76 i2csetspeed(dev,speed);
77 i2cenable(count);
78 }
79 return dev;
80 }
81
82 int i2cclose(i2c_t dev)
83 {
84 switch((int)dev)
85 {
86 case (int)0:
87 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
88 break;
89 case (int)1:
90 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
91 break;
92 case (int)2:
93 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
94 break;
95 default:
96 break;
97 }
98 return 1;
99 }
100
101 int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL)
102 {
103 if((dev<3)&&(dev>=0))
104 {
105 gpio_t SDApin,SCLpin;
106 SDApin = gpioopen(SDA);
107 SCLpin = gpioopen(SCL);
108 SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
109 SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
110 gpiosetconfig(&SDApin);
111 gpiosetconfig(&SCLpin);
112 uint8_t gpioAFi2cx = GPIO_AF_I2C1;
113 switch((int)dev)
114 {
115 case 0:
116 gpioAFi2cx = GPIO_AF_I2C1;
117 break;
118 case 1:
119 gpioAFi2cx = GPIO_AF_I2C2;
120 break;
121 case 2:
122 gpioAFi2cx = GPIO_AF_I2C3;
123 break;
124 default:
125 break;
126 }
127 GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx);
128 GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx);
129 return 0;
130 }
131 return -1;
132 }
133
134 int i2cenable(i2c_t dev)
135 {
136 if((dev<3)&&(dev>=0))
137 {
138 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
139 _dev_->CR1 |=1 ;
140 return 0;
141 }
142 return -1;
143 }
144
145 int i2cdisable(i2c_t dev)
146 {
147 if((dev<3)&&(dev>=0))
148 {
149 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
150 _dev_->CR1 &= ~1;
151 return 0;
152 }
153 return -1;
154 }
155
156
157 int i2csetspeed(i2c_t dev,uint32_t speed)
158 {
159 if((dev<3)&&(dev>=0))
160 {
161 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
162 int32_t APB1Freq=getAPB1Freq()/1000000;
163 if((APB1Freq>1)&&(APB1Freq<43))
164 {
165 int enabled=((_dev_->CR1&1)==1);
166 i2cdisable(dev);
167 _dev_->CR2 &= ~(0x1f);
168 _dev_->CR2 |= APB1Freq;
169 if(speed>100000) //100kHz= standard mode, 400kHz= fast mode
170 {
171 if(speed<=400000)
172 {
173 _dev_->CCR |= 1<<15;
174 _dev_->CCR &= ~(1<<14);
175 _dev_->CCR &= ~(0xfff);
176 _dev_->CCR |= 0xfff & (APB1Freq/(3*speed));
177 }
178 }
179 else
180 {
181 _dev_->CCR &= ~(1<<15);
182 _dev_->CCR &= ~(0xfff);
183 _dev_->CCR |= 0xfff & (APB1Freq/(2*speed));
184 }
185 if(enabled)i2cenable(dev);
186 return 0;
187 }
188 }
189 return -1;
190 }
191
192 int i2cwrite(i2c_t dev,char address,char* data,int count)
193 {
194 if((dev<3)&&(dev>=0))
195 {
196 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
197 _dev_->CR1 |= 1<<8;
198 while((_dev_->SR1&1)==0);
199 _dev_->DR= address<<1;
200 while((_dev_->SR1 & (1<<3))==0);
201 address=_dev_->SR2;
202 for(int i=0;i<count;i++)
203 {
204 while((_dev_->SR1 & (1<<7))==0);
205 _dev_->DR= data[i];
206 }
207 while((_dev_->SR1 & (1<<7))==0);
208 while((_dev_->SR1 & (1<<2))==0);
209 _dev_->CR1 |= 1<<9;
210 return count;
211 }
212 return -1;
213 }
214
215 int i2cread(i2c_t dev,char address,char* data,int count)
216 {
217 if((dev<3)&&(dev>=0))
218 {
219 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
220 _dev_->CR1 |= (1<<8) | (1<<10);
221 if(count==1)
222 {
223 _dev_->CR1 &= ~(1<<10);
224 }
225 while((_dev_->SR1&1)==0);
226 _dev_->DR= (address<<1) + 1;
227 while((_dev_->SR1 & (1<<3))==0);
228 address=_dev_->SR2;
229 for(int i=0;i<count-1;i++)
230 {
231 while((_dev_->SR1 & (1<<6))==0);
232 data[i]=_dev_->DR;
233 }
234 _dev_->CR1 &= ~(1<<10);
235 _dev_->CR1 |= 1<<9;
236 while((_dev_->SR1 & (1<<6))==0);
237 data[count-1]=_dev_->DR;
238 return count;
239 }
240 return -1;
241 }
242
243
244
245
246
@@ -0,0 +1,13
1 TEMPLATE = lib
2 CONFIG += console
3 CONFIG -= qt
4 include($$(libuc2)/rules/stm32f4-arm-none-eabi-gcc/rules.pri)
5
6 SOURCES += \
7 i2c.c
8
9
10 HEADERS += \
11 $$(libuc2)/lib/includes/i2c.h
12
13
@@ -1,93 +1,96
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 #ifndef BSP_H
23 23 #define BSP_H
24 24 #include <stm32f4xx.h>
25 25 #include <stm32f4xx_gpio.h>
26 26 #include <stm32f4xx_rcc.h>
27 27 #include <gpio.h>
28 28
29 29 #define __MAX_OPENED_FILES__ 4
30 30 #define __FS_ROOT_SIZE__ 4
31 31 /*
32 32 #ifndef PD8
33 33 #define PD8
34 34 #endif
35 35 #ifndef PD9
36 36 #define PD9
37 37 #endif
38 38 */
39 39
40 #define LED1 PD8
41 #define LED2 PD9
40 #define LED1 PD12
41 #define LED2 PD13
42 #define LED3 PD14
43 #define LED4 PD15
44
42 45
43 46 #define LED3ON GPIOB->BSRRH = GPIO_Pin_15
44 47 #define LED2ON GPIOD->BSRRH = GPIO_Pin_8
45 48 #define LED1ON GPIOD->BSRRH = GPIO_Pin_9
46 49
47 50 #define LED3OFF GPIOB->BSRRL = GPIO_Pin_15
48 51 #define LED2OFF GPIOD->BSRRL = GPIO_Pin_8
49 52 #define LED1OFF GPIOD->BSRRL = GPIO_Pin_9
50 53
51 54 extern float VREF0;
52 55
53 56 extern uint32_t currentCpuFreq;
54 57
55 58 extern int bsp_init();
56 59
57 60 extern void bsp_GPIO_init();
58 61 extern void bsp_uart_init();
59 62 extern void bsp_iic_init();
60 63 extern void bsp_spi_init();
61 64 extern void bsp_SD_init();
62 65
63 66 /* VS1053 */
64 67 extern void clearXCS();
65 68 extern void setXCS();
66 69 extern int vs10XXDREQ();
67 70
68 71 /* SD CARD */
69 72 void bsppowersdcard(char onoff);
70 73 char bspsdcardpresent();
71 74 void bspsdcardselect(char YESNO);
72 75 char bspsdcardwriteprotected();
73 76
74 77 #endif
75 78
76 79
77 80
78 81
79 82
80 83
81 84
82 85
83 86
84 87
85 88
86 89
87 90
88 91
89 92
90 93
91 94
92 95
93 96
@@ -1,132 +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 #include <i2c.h>
29
28 30 uint32_t OSC0 =8000000;
29 31 uint32_t INTOSC =16000000;
30 32 uint32_t RTCOSC =32768;
31 33 uint32_t currentCpuFreq=0;
32 34 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
33 35
34 36 float VREF0 =(float)3.3;
35 37
36 38 int bsp_init()
37 39 {
38 40 int i=0;
39 41 for(i=0;i<32;i++)
40 42 {
41 43 __opnfiles__[i] = NULL;
42 44 }
43 45 bsp_GPIO_init();
44 46 bsp_uart_init();
47 bsp_iic_init();
45 48 printf("\r================================================================\n\r");
46 49 printf("================================================================\n\r");
47 50 printf(BSP);
48 51 printf(" initialised\n\r");
49 52 printf("================================================================\n\r");
50 53 return 1;
51 54 }
52 55
53 56 void bsp_GPIO_init()
54 57 {
55 58 gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009
56 59 gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2);
57 60 gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2);
58 61 gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2);
59 62 gpiosetspeed(&gpio1,gpiohighspeed);
60 63 gpiosetspeed(&gpio2,gpiohighspeed);
61 64 gpiosetspeed(&gpio3,gpiohighspeed);
62 65 gpiosetspeed(&gpio4,gpiohighspeed);
63 66 gpiosetdir(&gpio1,gpiooutdir);
64 67 gpiosetdir(&gpio3,gpiooutdir);
65 68 gpiosetdir(&gpio2,gpiooutdir);
66 69 gpiosetdir(&gpio4,gpiooutdir);
67 70 }
68 71
69 72 void bsp_uart_init()
70 73 {
71 74 if(__opnfiles__[1]==NULL)
72 75 {
73 76 uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
74 77 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
75 78 uartopenandconfig(2,uart1,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
76 79 uartmkstreamdev(uart1,fd1);
77 80 __opnfiles__[1] = fd1;
78 81 }
79 82 else
80 83 {
81 84 uartopenandconfig(2,(uart_t*)__opnfiles__[1]->_stream,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
82 85 }
83 86 }
84 87
85 88 void bsp_spi_init()
86 89 {
87 90
88 91 }
89 92
90 93
91 94 void bsp_iic_init()
92 95 {
93
96 if(0==i2copenandconfig(i2c1,0,100000,PB9,PB6))printf("I2C1 opened\n\r");
97 i2cenable(i2c1);
94 98 }
95 99
96 100 void bsp_SD_init()
97 101 {
98 102
99 103 }
100 104
101 105 void vs10XXclearXCS(){}
102 106 void vs10XXsetXCS(){}
103 107 int vs10XXDREQ()
104 108 {
105 109 return 1;
106 110 }
107 111
108 112
109 113 void bsppowersdcard(char onoff) //always ON
110 114 {
111 115
112 116 }
113 117
114 118 char bspsdcardpresent()
115 119 {
116 120 return 0;
117 121 }
118 122
119 123 char bspsdcardwriteprotected()
120 124 {
121 125 return 0;
122 126 }
123 127
124 128 void bspsdcardselect(char YESNO)
125 129 {
126 130
127 131 }
128 132
129 133
130 134
131 135
132 136
@@ -1,11 +1,12
1 1 TEMPLATE = app
2 2 CONFIG += console
3 3 CONFIG -= qt
4 4
5 BSP = BEAGLESYNTH
5 #BSP = BEAGLESYNTH
6 BSP = SOLAR_LFR_PSU
6 7 include($$(libuc2)/bsp/cfg/$$BSP/bsp.pri)
7 8 #include($$(libuc2)/rules/stm32f4-arm-none-eabi-gcc/rules.pri)
8 9
9 10 SOURCES += \
10 11 main.c
11 12
@@ -1,45 +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
8 8
9 9 extern streamdevice* __opnfiles__[];
10 10
11 11 int main()
12 12 {
13 gpioset(PSU_DISABLE);
13 //gpioset(PSU_DISABLE);
14 14 while(1)
15 15 {
16 16 for(volatile int i=0;i<1024*2048;i++);
17 17 gpioset(LED1);
18 18 gpioclr(LED2);
19 19 for(volatile int i=0;i<1024*2048;i++);
20 20 gpioclr(LED1);
21 21 gpioset(LED2);
22 22 }
23 23 printf("hello world\n\r");
24 24 return 0;
25 25 }
26 26
27 27
28 28
29 29 void USART3_IRQHandler(void)
30 30 {
31 31 while(1)
32 32 {
33 33 for(volatile int i=0;i<1024*2048;i++);
34 34 gpioset(PD12);
35 35 gpioclr(PD14);
36 36 for(volatile int i=0;i<1024*2048;i++);
37 37 gpioclr(PD12);
38 38 gpioset(PD14);
39 39 }
40 40 }
41 41
42 42
43 43
44 44
45 45
@@ -1,12 +1,13
1 1 TEMPLATE = subdirs
2 2 CONFIG += ordered
3 3 SUBDIRS += QtTest/test.pro \
4 4 SOLAR_PSU_HELLO/hello.pro \
5 5 SDCARD \
6 STM32F4IT
6 STM32F4IT \
7 BeagleSynthHello
7 8
8 9
9 10
10 11
11 12
12 13
@@ -1,53 +1,62
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 <stm32f4xx.h>
23 23
24 24 /*
25 25 #define pll0useInternal 0
26 26 #define pll0useMainOsc 1
27 27 #define pll0useRTC 2
28 28 */
29 29
30 30 //#define pll0setClksrc(src) if((unsigned int)(src)<3) LPC_SC->CLKSRCSEL=(src)
31 31 //#define pll0getClksrc (LPC_SC->CLKSRCSEL & 3)
32 32 #ifdef __cplusplus
33 33 extern "C" {
34 34 #endif
35 35
36 extern void coresetCpuFreq(unsigned int freq);
37 extern unsigned int coregetCpuFreq();
36 extern uint32_t getAPB1Freq();
37 extern uint32_t getAPB2Freq();
38 extern uint32_t getCpuFreq();
39 extern int setCpuFreq(uint32_t freq);
40 extern int optimizePLLcfg(uint32_t freq, uint32_t srcfreq,uint32_t PLLM,uint32_t* PLLP, uint32_t* PLLN,uint8_t* AHBPRindx);
41 extern int setPll(uint32_t freq);
42 extern void enable_FPU();
43 extern void reset_AHB1();
44 extern void reset_AHB2();
45 extern void reset_APB1();
46 extern void reset_APB2();
38 47
39 48 #ifdef __cplusplus
40 49 }
41 50 #endif
42 51
43 52
44 53
45 54
46 55
47 56
48 57
49 58
50 59
51 60
52 61
53 62
@@ -1,59 +1,329
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 "core.h"
23 23 #include <stm32f4xx_rcc.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <stdio.h>
24 27
25 //extern uint32_t OSC0;
28 extern uint32_t OSC0;
26 29 extern uint32_t INTOSC;
27 30 extern uint32_t RTCOSC;
28 31
29 void coresetCpuFreq(unsigned int freq)
32
33 uint32_t getAPB1Freq()
30 34 {
35 RCC_ClocksTypeDef RCC_ClocksStatus;
36 RCC_GetClocksFreq(&RCC_ClocksStatus);
37 return RCC_ClocksStatus.PCLK1_Frequency;
38 }
39
40 uint32_t getAPB2Freq()
41 {
42 RCC_ClocksTypeDef RCC_ClocksStatus;
43 RCC_GetClocksFreq(&RCC_ClocksStatus);
44 return RCC_ClocksStatus.PCLK2_Frequency;
45 }
31 46
32 47
48 uint32_t getCpuFreq()
49 {
50 uint32_t cpufreq = OSC0;
51 uint32_t PLLN,PLLM,PLLP;
52
53 if((RCC->CFGR & 0xC) == 8) //PLL used as sys clk
54 {
55 uint32_t pllinput=INTOSC;
56 if((RCC->PLLCFGR & (1<<22)) == (1<<22))
57 {
58 pllinput=OSC0;
59 }
60 PLLN = (RCC->PLLCFGR>>6) & 0x1FF;
61 PLLM = RCC->PLLCFGR & 0x3F;
62 PLLP = 1<<(((RCC->PLLCFGR>>16) & 3 )+1);
63 cpufreq = (pllinput * PLLN )/(PLLM*PLLP);
64 }
65 else if((RCC->CFGR & 0xC) == 0) //HSI used as sys clk
66 {
67 cpufreq=INTOSC;
68 }
69 if((RCC->CFGR & (1<<7))==1<<7)
70 {
71 return cpufreq>>((RCC->CFGR & (7<<4))>>4);
72 }
73 return cpufreq;
74 }
75
76 void reset_AHB1()
77 {
78 RCC->AHB1RSTR = -1;
79 RCC->AHB1RSTR = 0;
80 }
81
82 void reset_AHB2()
83 {
84 RCC->AHB2RSTR = -1;
85 RCC->AHB2RSTR = 0;
86 }
87
88 void reset_APB1()
89 {
90 RCC->APB1RSTR = -1;
91 RCC->APB1RSTR = 0;
92 }
93
94 void reset_APB2()
95 {
96 RCC->APB2RSTR = -1;
97 RCC->APB2RSTR = 0;
33 98 }
34 99
35 100
36 101
102 /*
103 | 2.7->3.6V
104 -------------------------
105 0WS | 0<HCLK<=30
106 1WS | 30<HCLK<=60
107 2WS | 60<HCLK<=90
108 3WS | 90<HCLK<=120
109 4WS | 120<HCLK<=150
110 5WS | 150<HCLK<=168
37 111
38 unsigned int coregetCpuFreq()
112 f(VCO clock) = f(PLL clock input) Γ— (PLLN / PLLM) [1]
113 64MHz <= f(VCO clock) <= 432MHz [2]
114
115 f(VCO clock input) must be between 1MHz and 2MHz and as close to 2MHz as possible!! [3]
116
117 f(PLL general clock output) = f(VCO clock) / PLLP [4]
118
119 CPU<168MHz AHB1<168MHz AHB2<168MHz APB1<42MHz APB2<84MHz [5]
120
121 ! 63<=PLLN<=432 [6]
122 ! 2<=PLLM<=63 [7]
123 ! PLLP=2,4,6,8 [8]
124 4<=PLLM*PLLP<=504
125
126 F= f(PLL clock input) * A/B with
127 63<=A<=432
128 4<=B<=504
129
130 */
131
132
133 int optimizePLLcfg(uint32_t freq, uint32_t srcfreq,uint32_t PLLM,uint32_t* PLLP, uint32_t* PLLN,uint8_t* AHBPRindx)
134 {
135 uint32_t AHBPRtbl[9]={1,2,4,8,16,64,128,256,512};
136 uint32_t AHBPR=0,AHBPR_r=0,PLLN_r=0,PLLP_r=0;
137 uint32_t Fplli=0;
138 int32_t f_error=100000000;
139 int32_t f_errornw=100000000;
140 Fplli = srcfreq / PLLM;
141 //not efficient but should find the best parameters
142 for((*AHBPRindx)=0;(*AHBPRindx)<9;(*AHBPRindx)++) //lowest priority
143 {
144 AHBPR = AHBPRtbl[(*AHBPRindx)];
145 for(*PLLP=2;*PLLP<9;*PLLP+=2)
146 {
147 *PLLN = (freq*(*PLLP)*AHBPR)/Fplli;
148 if(((*PLLN)>62) && ((*PLLN)<433) && ((Fplli * (*PLLN)) < 433000000))
149 {
150 f_errornw = abs((int32_t)((int32_t)((Fplli*(*PLLN))/((*PLLP)*AHBPR))-freq));
151 if( ( (f_error)>(f_errornw) ) || ( (*AHBPRindx==0)&&(*PLLP==2)&&(*PLLN==63) ) )
152 {
153 f_error=f_errornw;
154 PLLN_r = *PLLN;
155 PLLP_r = *PLLP;
156 AHBPR_r=*AHBPRindx;
157 if(f_error==0)
158 {
159 *PLLN = PLLN_r;
160 *PLLP = PLLP_r;
161 *AHBPRindx = AHBPR_r;
162 return 1;
163 }
164 }
165 }
166 }
167 }
168 *PLLN = PLLN_r;
169 *PLLP = PLLP_r;
170 *AHBPRindx = AHBPR_r;
171 return 1;
172 }
173
174
175 int setPll(uint32_t freq)
39 176 {
40 RCC_ClocksTypeDef RCC_ClocksStatus;
41 RCC_GetClocksFreq(&RCC_ClocksStatus);
42 return RCC_ClocksStatus.SYSCLK_Frequency;
177 extern uint32_t OSC0;
178 extern uint32_t INTOSC;
179 uint32_t srcfreq = INTOSC;
180 uint8_t AHBPRindx;
181 uint32_t AHBPRtbl[9]={1,2,4,8,16,64,128,256,512};
182 uint32_t PLLN=0,PLLM=0,PLLP=0,AHBPR=0;
183 uint32_t Fplli=0;
184 if((RCC->PLLCFGR & (1<<22))==(1<<22))
185 {
186 srcfreq = OSC0;
187 }
188 PLLM = srcfreq / 1500000; // [3]
189 Fplli = srcfreq / PLLM;
190 optimizePLLcfg(freq,srcfreq,PLLM,&PLLP,&PLLN,&AHBPRindx);
191 srcfreq = (Fplli*PLLN)/(PLLP*AHBPRtbl[AHBPRindx]); //Put real clk freq in srcfreq for return value
192 //now switch to HSIs
193 if((RCC->CR & 1)==0)RCC->CR |= 1; //turn ON HSI
194 while((RCC->CR & 2)!=2); //wait for HSI Ready
195 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
196 RCC->CFGR |= RCC_CFGR_SW_HSI; //set HSI as main clk
197 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_HSI);
198 RCC->CR &= ~(1<<24); //Turn OFF PLL
199 RCC->PLLCFGR &= ~0x37FFF; //clear PLLP PLLM PLLN
200 RCC->PLLCFGR |= PLLM + (PLLN<<6) + (((PLLP>>1) -1)<<16);
201 RCC->CR |= RCC_CR_PLLON; //Turn ON PLL
202 while((RCC->CR & (1<<25))!=(1<<25)); //wait for PLL Ready
203 if(AHBPRindx!=0)AHBPRindx|=0x8;
204 RCC->CFGR &= ~(0xF<<4);
205 RCC->CFGR |= (uint32_t)(AHBPRindx<<4);
206 AHBPR=0;
207 while((srcfreq>>AHBPR)>42000000)AHBPR++; //[5] //Thune APB1 prescaler to keep APB1 CLK below 42MHz
208 if(AHBPR!=0)
209 {
210 AHBPR-=1;
211 AHBPR|=0x4;
212 }
213 RCC->CFGR &= ~(0x7<<10);
214 RCC->CFGR |= (uint32_t)(AHBPR<<10);
215 AHBPR=0;
216 while((srcfreq>>AHBPR)>84000000)AHBPR++; //[5] //Thune APB2 prescaler to keep APB2 CLK below 42MHz
217 if(AHBPR!=0)
218 {
219 AHBPR-=1;
220 AHBPR|=0x4;
221 }
222 RCC->CFGR &= ~(0x7<<13);
223 RCC->CFGR |= (uint32_t)(AHBPR<<13);
224 FLASH->ACR |= FLASH_ACR_LATENCY_7WS;
225 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));//Switch to PLL as main clk source
226 RCC->CFGR |= RCC_CFGR_SW_PLL;
227 /* Wait till the main PLL is used as system clock source */
228 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
229 if(srcfreq>150000000)
230 {
231 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_5WS;
232 }
233 if((srcfreq<150000000) && (srcfreq>=120000000))
234 {
235 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_4WS;
236 }
237 if((srcfreq<120000000) && (srcfreq>=90000000))
238 {
239 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_3WS;
240 }
241 if((srcfreq<90000000) && (srcfreq>=60000000))
242 {
243 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_2WS;
244 }
245 if((srcfreq<60000000) && (srcfreq>=30000000))
246 {
247 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_1WS;
248 }
249 if(srcfreq<30000000)
250 {
251 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_0WS;
252 }
253 return srcfreq;
254 }
255
256 int setCpuFreq(uint32_t freq)
257 {
258 extern uint32_t OSC0;
259 extern uint32_t INTOSC;
260 uint8_t i=0;
261 uint32_t curentFeq = getCpuFreq();
262 if(curentFeq==freq)return curentFeq;
263 if((freq>2000000) && (freq<=250000000)) //be carefull with 250MHz!!!
264 {
265 if((RCC->CFGR & 0xC) == 8) //PLL used as sys clk
266 {
267 return setPll(freq);
268 }
269 else if((RCC->CFGR & 0xC) == 0) //HSI used as sys clk
270 {
271 if((INTOSC%freq)==0) //now check if we can directly divide HSI
272 {
273 if(freq==INTOSC)
274 {
275 RCC->CFGR &= ~(0xF<<4);
276 return freq;
277 }
278 for(i=1;i<8;i++)
279 {
280 if((freq<<i)==INTOSC)
281 {
282 RCC->CFGR &= ~(0xF<<4);
283 RCC->CFGR |= ((0x8|i)<<4);
284 return freq;
285 }
286 }
287 }
288 else
289 return setPll(freq);
290 }
291 else //HSE used as sys clk
292 {
293 if((OSC0%freq)==0) //now check if we can directly divide HSI
294 {
295 if(freq==OSC0)
296 {
297 RCC->CFGR &= ~(0xF<<4);
298 return freq;
299 }
300 for(i=1;i<8;i++)
301 {
302 if((freq<<i)==OSC0)
303 {
304 RCC->CFGR &= ~(0xF<<4);
305 RCC->CFGR |= ((0x8|i)<<4);
306 return freq;
307 }
308 }
309 }
310 else
311 return setPll(freq);
312 }
313 }
314 return 0;
315 }
316
317
318 void enable_FPU()
319 {
320 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
321 __asm__("dsb");
322 __asm__("isb");
43 323 }
44 324
45 325
46 326
47 327
48 328
49 329
50
51
52
53
54
55
56
57
58
59
@@ -1,337 +1,59
1 1 #include "stm32f4xx.h"
2 2 #include <stdint.h>
3 3 #include <stdlib.h>
4 4 #include <stdio.h>
5 5 #include <bsp.h>
6 6 #include <stm32f4xx_rcc.h>
7
8 void reset_AHB1()
9 {
10 RCC->AHB1RSTR = -1;
11 RCC->AHB1RSTR = 0;
12 }
13
14 void reset_AHB2()
15 {
16 RCC->AHB2RSTR = -1;
17 RCC->AHB2RSTR = 0;
18 }
19
20 void reset_APB1()
21 {
22 RCC->APB1RSTR = -1;
23 RCC->APB1RSTR = 0;
24 }
25
26 void reset_APB2()
27 {
28 RCC->APB2RSTR = -1;
29 RCC->APB2RSTR = 0;
30 }
31
32
33
34 uint32_t getCpuFreq()
35 {
36 extern uint32_t OSC0;
37 extern uint32_t INTOSC;
38 uint32_t cpufreq = OSC0;
39 uint32_t PLLN,PLLM,PLLP;
40
41 if((RCC->CFGR & 0xC) == 8) //PLL used as sys clk
42 {
43 uint32_t pllinput=INTOSC;
44 if((RCC->PLLCFGR & (1<<22)) == (1<<22))
45 {
46 pllinput=OSC0;
47 }
48 PLLN = (RCC->PLLCFGR>>6) & 0x1FF;
49 PLLM = RCC->PLLCFGR & 0x3F;
50 PLLP = 1<<(((RCC->PLLCFGR>>16) & 3 )+1);
51 cpufreq = (pllinput * PLLN )/(PLLM*PLLP);
52 }
53 else if((RCC->CFGR & 0xC) == 0) //HSI used as sys clk
54 {
55 cpufreq=INTOSC;
56 }
57 if((RCC->CFGR & (1<<7))==1<<7)
58 {
59 return cpufreq>>((RCC->CFGR & (7<<4))>>4);
60 }
61 return cpufreq;
62 }
63
64
65 /*
66 | 2.7->3.6V
67 -------------------------
68 0WS | 0<HCLK<=30
69 1WS | 30<HCLK<=60
70 2WS | 60<HCLK<=90
71 3WS | 90<HCLK<=120
72 4WS | 120<HCLK<=150
73 5WS | 150<HCLK<=168
74
75 f(VCO clock) = f(PLL clock input) Γ— (PLLN / PLLM) [1]
76 64MHz <= f(VCO clock) <= 432MHz [2]
77
78 f(VCO clock input) must be between 1MHz and 2MHz and as close to 2MHz as possible!! [3]
79
80 f(PLL general clock output) = f(VCO clock) / PLLP [4]
81
82 CPU<168MHz AHB1<168MHz AHB2<168MHz APB1<42MHz APB2<84MHz [5]
83
84 ! 63<=PLLN<=432 [6]
85 ! 2<=PLLM<=63 [7]
86 ! PLLP=2,4,6,8 [8]
87 4<=PLLM*PLLP<=504
88
89 F= f(PLL clock input) * A/B with
90 63<=A<=432
91 4<=B<=504
92
93 */
94
95
96 int optimizePLLcfg(uint32_t freq, uint32_t srcfreq,uint32_t PLLM,uint32_t* PLLP, uint32_t* PLLN,uint8_t* AHBPRindx)
97 {
98 uint32_t AHBPRtbl[9]={1,2,4,8,16,64,128,256,512};
99 uint32_t AHBPR=0,AHBPR_r=0,PLLN_r=0,PLLP_r=0;
100 uint32_t Fplli=0;
101 int32_t f_error=100000000;
102 int32_t f_errornw=100000000;
103 Fplli = srcfreq / PLLM;
104 //not efficient but should find the best parameters
105 for((*AHBPRindx)=0;(*AHBPRindx)<9;(*AHBPRindx)++) //lowest priority
106 {
107 AHBPR = AHBPRtbl[(*AHBPRindx)];
108 for(*PLLP=2;*PLLP<9;*PLLP+=2)
109 {
110 *PLLN = (freq*(*PLLP)*AHBPR)/Fplli;
111 if(((*PLLN)>62) && ((*PLLN)<433) && ((Fplli * (*PLLN)) < 433000000))
112 {
113 f_errornw = abs((int32_t)((int32_t)((Fplli*(*PLLN))/((*PLLP)*AHBPR))-freq));
114 if( ( (f_error)>(f_errornw) ) || ( (*AHBPRindx==0)&&(*PLLP==2)&&(*PLLN==63) ) )
115 {
116 f_error=f_errornw;
117 PLLN_r = *PLLN;
118 PLLP_r = *PLLP;
119 AHBPR_r=*AHBPRindx;
120 if(f_error==0)
121 {
122 *PLLN = PLLN_r;
123 *PLLP = PLLP_r;
124 *AHBPRindx = AHBPR_r;
125 return 1;
126 }
127 }
128 }
129 }
130 }
131 *PLLN = PLLN_r;
132 *PLLP = PLLP_r;
133 *AHBPRindx = AHBPR_r;
134 return 1;
135 }
7 #include <core.h>
8 extern int main();
136 9
137 10
138 int setPll(uint32_t freq)
139 {
140 extern uint32_t OSC0;
141 extern uint32_t INTOSC;
142 uint32_t srcfreq = INTOSC;
143 uint8_t AHBPRindx;
144 uint32_t AHBPRtbl[9]={1,2,4,8,16,64,128,256,512};
145 uint32_t PLLN=0,PLLM=0,PLLP=0,AHBPR=0;
146 uint32_t Fplli=0;
147 if((RCC->PLLCFGR & (1<<22))==(1<<22))
148 {
149 srcfreq = OSC0;
150 }
151 PLLM = srcfreq / 1500000; // [3]
152 Fplli = srcfreq / PLLM;
153 optimizePLLcfg(freq,srcfreq,PLLM,&PLLP,&PLLN,&AHBPRindx);
154 srcfreq = (Fplli*PLLN)/(PLLP*AHBPRtbl[AHBPRindx]); //Put real clk freq in srcfreq for return value
155 //now switch to HSIs
156 if((RCC->CR & 1)==0)RCC->CR |= 1; //turn ON HSI
157 while((RCC->CR & 2)!=2); //wait for HSI Ready
158 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
159 RCC->CFGR |= RCC_CFGR_SW_HSI; //set HSI as main clk
160 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_HSI);
161 RCC->CR &= ~(1<<24); //Turn OFF PLL
162 RCC->PLLCFGR &= ~0x37FFF; //clear PLLP PLLM PLLN
163 RCC->PLLCFGR |= PLLM + (PLLN<<6) + (((PLLP>>1) -1)<<16);
164 RCC->CR |= RCC_CR_PLLON; //Turn ON PLL
165 while((RCC->CR & (1<<25))!=(1<<25)); //wait for PLL Ready
166 if(AHBPRindx!=0)AHBPRindx|=0x8;
167 RCC->CFGR &= ~(0xF<<4);
168 RCC->CFGR |= (uint32_t)(AHBPRindx<<4);
169 AHBPR=0;
170 while((srcfreq>>AHBPR)>42000000)AHBPR++; //[5] //Thune APB1 prescaler to keep APB1 CLK below 42MHz
171 if(AHBPR!=0)
172 {
173 AHBPR-=1;
174 AHBPR|=0x4;
175 }
176 RCC->CFGR &= ~(0x7<<10);
177 RCC->CFGR |= (uint32_t)(AHBPR<<10);
178 AHBPR=0;
179 while((srcfreq>>AHBPR)>84000000)AHBPR++; //[5] //Thune APB2 prescaler to keep APB2 CLK below 42MHz
180 if(AHBPR!=0)
181 {
182 AHBPR-=1;
183 AHBPR|=0x4;
184 }
185 RCC->CFGR &= ~(0x7<<13);
186 RCC->CFGR |= (uint32_t)(AHBPR<<13);
187 FLASH->ACR |= FLASH_ACR_LATENCY_7WS;
188 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));//Switch to PLL as main clk source
189 RCC->CFGR |= RCC_CFGR_SW_PLL;
190 /* Wait till the main PLL is used as system clock source */
191 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
192 if(srcfreq>150000000)
193 {
194 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_5WS;
195 }
196 if((srcfreq<150000000) && (srcfreq>=120000000))
197 {
198 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_4WS;
199 }
200 if((srcfreq<120000000) && (srcfreq>=90000000))
201 {
202 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_3WS;
203 }
204 if((srcfreq<90000000) && (srcfreq>=60000000))
205 {
206 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_2WS;
207 }
208 if((srcfreq<60000000) && (srcfreq>=30000000))
209 {
210 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_1WS;
211 }
212 if(srcfreq<30000000)
213 {
214 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_0WS;
215 }
216 return srcfreq;
217 }
218
219 int setCpuFreq(uint32_t freq)
220 {
221 extern uint32_t OSC0;
222 extern uint32_t INTOSC;
223 uint8_t i=0;
224 uint32_t curentFeq = getCpuFreq();
225 if(curentFeq==freq)return curentFeq;
226 if((freq>2000000) && (freq<=250000000)) //be carefull with 250MHz!!!
227 {
228 if((RCC->CFGR & 0xC) == 8) //PLL used as sys clk
229 {
230 return setPll(freq);
231 }
232 else if((RCC->CFGR & 0xC) == 0) //HSI used as sys clk
233 {
234 if((INTOSC%freq)==0) //now check if we can directly divide HSI
235 {
236 if(freq==INTOSC)
237 {
238 RCC->CFGR &= ~(0xF<<4);
239 return freq;
240 }
241 for(i=1;i<8;i++)
242 {
243 if((freq<<i)==INTOSC)
244 {
245 RCC->CFGR &= ~(0xF<<4);
246 RCC->CFGR |= ((0x8|i)<<4);
247 return freq;
248 }
249 }
250 }
251 else
252 return setPll(freq);
253 }
254 else //HSE used as sys clk
255 {
256 if((OSC0%freq)==0) //now check if we can directly divide HSI
257 {
258 if(freq==OSC0)
259 {
260 RCC->CFGR &= ~(0xF<<4);
261 return freq;
262 }
263 for(i=1;i<8;i++)
264 {
265 if((freq<<i)==OSC0)
266 {
267 RCC->CFGR &= ~(0xF<<4);
268 RCC->CFGR |= ((0x8|i)<<4);
269 return freq;
270 }
271 }
272 }
273 else
274 return setPll(freq);
275 }
276 }
277 return 0;
278 }
279
280 #include <stdio.h>
281 extern int main();
282
283 inline void enable_FPU()
284 {
285 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
286 __asm__("dsb");
287 __asm__("isb");
288 }
289 11
290 12 void cpu_init()
291 13 {
292 14 extern uint32_t currentCpuFreq;
293 15 currentCpuFreq = 80000000;
294 16 enable_FPU();
295 17 RCC->CR |= (uint32_t)0x00000001;
296 18 FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
297 19 reset_AHB1();
298 20 reset_AHB2();
299 21 reset_APB1();
300 22 reset_APB2();
301 23 RCC->CR |= (uint32_t)0x00000001;
302 24 FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
303 25 RCC->CFGR = 0x00000000;
304 26 RCC->CIR = 0x00000000;
305 27 SCB->VTOR = FLASH_BASE;
306 28 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
307 29 PWR->CR |= PWR_CR_PMODE;
308 30 bsp_init();
309 31 printf("Configure PLL to reach %uHz\n\r",(unsigned int)currentCpuFreq);
310 32 currentCpuFreq=setCpuFreq(currentCpuFreq);
311 33 bsp_uart_init();
312 34 RCC_ClocksTypeDef RCC_ClocksStatus;
313 35 RCC_GetClocksFreq(&RCC_ClocksStatus);
314 36 printf("PLL Configured got:\n\r SYS=%uHz\n\r CPU=%uHz\n\r APB1=%uHz\n\r APB2=%uHz\n\r",(unsigned int)RCC_ClocksStatus.SYSCLK_Frequency,(unsigned int)RCC_ClocksStatus.HCLK_Frequency,(unsigned int)RCC_ClocksStatus.PCLK1_Frequency,(unsigned int)RCC_ClocksStatus.PCLK2_Frequency);
315 37 printf("Enter Main\n\r");
316 38 int res=main();
317 39 printf("\n\rprogram exited with code ");
318 40 printf("%u",res);
319 41 printf("\n\r");
320 42 while(1)
321 43 {
322 44 for(volatile int i=0;i<1024*64;i++);
323 45 gpioset(PD14);
324 46 for(volatile int i=0;i<1024*64;i++);
325 47 gpioclr(PD14);
326 48 }
327 49 }
328 50
329 51
330 52
331 53
332 54
333 55
334 56
335 57
336 58
337 59
@@ -1,430 +1,430
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
23 23 #include <uart.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 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
29 29 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
30 30
31 31 int uartopen(int count ,uart_t* uart)
32 32 {
33 33
34 34 switch(count)
35 35 {
36 36 case 0:
37 37 uart->_dev = (void*)USART1;
38 38 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
39 39 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
40 40 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
41 41 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
42 42 ((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9));
43 43 //uartenable(uart);
44 44 break;
45 45 case 1:
46 46 uart->_dev = (void*)USART2;
47 47 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
48 48 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
49 49 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
50 50 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
51 51 ((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9));
52 52 //uartenable(uart);
53 53 break;
54 54 case 2:
55 55 uart->_dev = (void*)USART3;
56 56 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
57 57 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
58 58 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
59 59 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
60 60 ((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9));
61 61 //uartenable(uart);
62 62 break;
63 63 case 3:
64 64 uart->_dev = (void*)UART4;
65 65 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
66 66 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
67 67 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
68 68 ((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9));
69 69 //uartenable(uart);
70 70 break;
71 71 case 4:
72 72 uart->_dev = (void*)UART5;
73 73 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
74 74 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
75 75 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
76 76 break;
77 77 case 5:
78 78 uart->_dev = (void*)USART6;
79 79 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
80 80 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, DISABLE);
81 81 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
82 82 break;
83 83 default:
84 84 break;
85 85 }
86 86 return 1;
87 87 }
88 88
89 89 int uartopenandconfig(int count ,uart_t* uart,uint32_t cfg,uint32_t speed,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin)
90 90 {
91 91 uartopen(count ,uart);
92 92 uart->cfg = cfg;
93 93 uart->speed = speed;
94 94 uartsetconfig(uart);
95 95 uartsetpins(uart,TXpin,RXpin,RTSpin,CTSpin);
96 96 return 1;
97 97 }
98 98
99 99
100 100 int uartclose(uart_t* uart)
101 101 {
102 102 switch((int)uart->_dev)
103 103 {
104 104 case (int)(void*)USART1:
105 105 uart->_dev = (void*)USART1;
106 106 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
107 107 break;
108 108 case (int)(void*)USART2:
109 109 uart->_dev = (void*)USART2;
110 110 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
111 111 break;
112 112 case (int)(void*)USART3:
113 113 uart->_dev = (void*)USART3;
114 114 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
115 115 break;
116 116 case (int)(void*)UART4:
117 117 uart->_dev = (void*)UART4;
118 118 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
119 119 break;
120 120 case (int)(void*)UART5:
121 121 uart->_dev = (void*)UART5;
122 122 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
123 123 break;
124 124 case (int)(void*)USART6:
125 125 uart->_dev = (void*)USART6;
126 126 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
127 127 break;
128 128 default:
129 129 break;
130 130 }
131 131 return 1;
132 132 }
133 133
134 134 int uartsetpins(uart_t* uart,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin)
135 135 {
136 136 gpio_t TX,RX,CTS,RTS;
137 137 TX = gpioopen(TXpin);
138 138 RX = gpioopen(RXpin);
139 139 TX |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype;
140 140 RX |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype;
141 141 gpiosetconfig(&TX);
142 142 gpiosetconfig(&RX);
143 143 uint8_t gpioAFuartx = GPIO_AF_USART1;
144 144 switch((int)uart->_dev)
145 145 {
146 146 case (int)(void*)USART1:
147 147 gpioAFuartx = GPIO_AF_USART1;
148 148 break;
149 149 case (int)(void*)USART2:
150 150 gpioAFuartx = GPIO_AF_USART2;
151 151 break;
152 152 case (int)(void*)USART3:
153 153 gpioAFuartx = GPIO_AF_USART3;
154 154 break;
155 155 case (int)(void*)UART4:
156 156 gpioAFuartx = GPIO_AF_UART4;
157 157 break;
158 158 case (int)(void*)UART5:
159 159 gpioAFuartx = GPIO_AF_UART5;
160 160 break;
161 161 case (int)(void*)USART6:
162 162 gpioAFuartx = GPIO_AF_USART6;
163 163 break;
164 164 default:
165 165 break;
166 166 }
167 167 GPIO_PinAFConfig(GPIOGETPORT(TX), (uint8_t)(TX & 0xF), gpioAFuartx);
168 GPIO_PinAFConfig(GPIOGETPORT(TX), (uint8_t)(RX & 0xF), gpioAFuartx);
168 GPIO_PinAFConfig(GPIOGETPORT(RX), (uint8_t)(RX & 0xF), gpioAFuartx);
169 169 if((gpioAFuartx!=GPIO_AF_UART5) && (gpioAFuartx!=GPIO_AF_UART4))
170 170 {
171 171 if(CTSpin!=-1)
172 172 {
173 173 CTS = gpioopen(CTSpin);
174 174 CTS |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype;
175 175 gpiosetconfig(&CTS);
176 176 GPIO_PinAFConfig(GPIOGETPORT(CTS), (uint8_t)(CTS & 0xF), gpioAFuartx);
177 177 }
178 178
179 179 if(RTSpin!=-1)
180 180 {
181 181 RTS = gpioopen(RTSpin);
182 182 RTS |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype;
183 183 gpiosetconfig(&RTS);
184 184 GPIO_PinAFConfig(GPIOGETPORT(RTS), (uint8_t)(RTS & 0xF), gpioAFuartx);
185 185 }
186 186 }
187 187 return 1;
188 188 }
189 189
190 190 int uartsetconfig(uart_t* uart)
191 191 {
192 192 int res=1;
193 193 uartdisable(uart);
194 194 uartsetspeed(uart,uart->speed);
195 195 uartsetparity(uart,uart->cfg & UARTPARITYMASK);
196 196 uartsetdatabits(uart,uart->cfg & UARTBITSMASK);
197 197 uartsetstopbits(uart,uart->cfg & UARTSTOPBITSMASK);
198 198 uartenable(uart);
199 199 return res;
200 200 }
201 201
202 202 int uartenable(uart_t* uart)
203 203 {
204 204 ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<13);
205 205 ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<2) + (1<<3);
206 206 ((USART_TypeDef *)(uart->_dev))->DR = ' ';
207 207 return 1;
208 208 }
209 209
210 210 int uartdisable(uart_t* uart)
211 211 {
212 212 if((((USART_TypeDef *)(uart->_dev))->CR1 & ((1<<3) +(1<<13)))==((1<<3) +(1<<13)))
213 213 {
214 214 while((((USART_TypeDef *)(uart->_dev))->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7));
215 215 }
216 216 ((USART_TypeDef *)(uart->_dev))->CR1 &= ~((1<<2) + (1<<3) +(1<<13));
217 217 return 1;
218 218 }
219 219
220 220 int uartsetspeed(uart_t* uart,int speed)
221 221 {
222 222 uart->speed = speed;
223 223 uint32_t tmpreg = 0x00, apbclock = 0x00;
224 224 uint32_t integerdivider = 0x00;
225 225 uint32_t fractionaldivider = 0x00;
226 226 RCC_ClocksTypeDef RCC_ClocksStatus;
227 227 RCC_GetClocksFreq(&RCC_ClocksStatus);
228 228
229 229 if (((USART_TypeDef *)(uart->_dev) == USART1) || (((USART_TypeDef *)(uart->_dev) == USART6)))
230 230 {
231 231 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
232 232 }
233 233 else
234 234 {
235 235 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
236 236 }
237 237 if (((((USART_TypeDef *)(uart->_dev))->CR1) & USART_CR1_OVER8) != (uint16_t)0)
238 238 {
239 239 integerdivider = ((25 * apbclock) / (2 * (speed)));
240 240 }
241 241 else
242 242 {
243 243 integerdivider = ((25 * apbclock) / (4 * (speed)));
244 244 }
245 245 tmpreg = (integerdivider / 100) << 4;
246 246 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
247 247 if ((((USART_TypeDef *)(uart->_dev))->CR1 & USART_CR1_OVER8) != (uint16_t)0)
248 248 {
249 249 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
250 250 }
251 251 else
252 252 {
253 253 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
254 254 }
255 255 ((USART_TypeDef *)(uart->_dev))->BRR = (uint16_t)tmpreg;
256 256 return 1;
257 257 }
258 258
259 259 int uartsetparity(uart_t* uart,uartparity_t parity)
260 260 {
261 261 uart->cfg &= ~(UARTPARITYMASK);
262 262 uart->cfg |= parity;
263 263 ((USART_TypeDef *)(uart->_dev))->CR1 &= ~(((1<<9)+(1<<10)));
264 264 switch(parity)
265 265 {
266 266 case uartparityeven:
267 267 ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<10);
268 268 break;
269 269 case uartparityodd:
270 270 ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<10) + (1<<9);
271 271 break;
272 272 case uartparitynone:
273 273 break;
274 274 default :
275 275 return 0;
276 276 break;
277 277 }
278 278 return 1;
279 279 }
280 280
281 281 int uartsetdatabits(uart_t* uart,uartbits_t databits)
282 282 {
283 283 uart->cfg &= ~UARTBITSMASK;
284 284 uart->cfg |= databits;
285 285 ((USART_TypeDef *)(uart->_dev))->CR1 &= ~(((1<<12)));
286 286 switch(databits)
287 287 {
288 288 case uart7bits:
289 289 return 0;
290 290 break;
291 291 case uart8bits:
292 292 break;
293 293 case uart9bits:
294 294 ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<12);
295 295 break;
296 296 default :
297 297 return 0;
298 298 break;
299 299 }
300 300 return 1;
301 301 }
302 302
303 303 int uartsetstopbits(uart_t* uart,uartstopbits_t stopbits)
304 304 {
305 305 uart->cfg &= ~UARTSTOPBITSMASK;
306 306 uart->cfg |= stopbits;
307 307 ((USART_TypeDef *)(uart->_dev))->CR2 &= ~(((1<<12)+(1<<13)));
308 308 switch(stopbits)
309 309 {
310 310 case uarthalfstop:
311 311 ((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<12);
312 312 break;
313 313 case uartonestop:
314 314 break;
315 315 case uartonehalfstop:
316 316 ((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<12) + (1<<13);
317 317 break;
318 318 case uarttwostop:
319 319 ((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<13);
320 320 break;
321 321 default :
322 322 return 0;
323 323 break;
324 324 }
325 325 return 1;
326 326 }
327 327
328 328 int uartputc(uart_t* uart,char c)
329 329 {
330 330 while((((USART_TypeDef *)(uart->_dev))->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7));
331 331 ((USART_TypeDef *)(uart->_dev))->DR = c;
332 332 return 1;
333 333 }
334 334
335 335 char uartgetc(uart_t* uart)
336 336 {
337 337 while(!(((USART_TypeDef *)(uart->_dev))->SR & (1<<5)));
338 338 return (char)((USART_TypeDef *)(uart->_dev))->DR;
339 339 }
340 340
341 341 int uartputs(uart_t* uart,char* s)
342 342 {
343 343 while (*s) uartputc(uart,*s++);
344 344 return 1;
345 345 }
346 346
347 347 int uartgets(uart_t* uart,char* s)
348 348 {
349 349 do
350 350 {
351 351 (*s) = uartgetc(uart);
352 352 }
353 353 while(*s++);
354 354 return 1;
355 355 }
356 356
357 357 int uartputnc(uart_t* uart,char* c,int n)
358 358 {
359 359 int l=0;
360 360 while(l<n)
361 361 {
362 362 uartputc(uart,*c++);
363 363 l++;
364 364 }
365 365 return n;
366 366 }
367 367
368 368 int uartgetnc(uart_t* uart,char* c,int n)
369 369 {
370 370 int l=0;
371 371 while(l<n)
372 372 {
373 373 *c++=uartgetc(uart);
374 374 l++;
375 375 }
376 376 return n;
377 377 }
378 378
379 379 int uartavailiabledata(uart_t* uart)
380 380 {
381 381 if(!(((USART_TypeDef *)(uart->_dev))->SR & (1<<5)))
382 382 return 0;
383 383 else
384 384 return 1;
385 385 }
386 386
387 387
388 388 int _uartstrwrite(streamdevice* device,void* data,int size, int n)
389 389 {
390 390 return uartputnc((uart_t*) device->_stream,(char*) data,size*n);
391 391 }
392 392
393 393 int _uartstrread(streamdevice* device,void* data,int size, int n)
394 394 {
395 395 return uartgetnc((uart_t*) device->_stream,(char*) data,size*n);
396 396 }
397 397
398 398 int _uartstrsetpos(streamdevice* device,int pos)
399 399 {
400 400 return 1;
401 401 }
402 402
403 403 int uartmkstreamdev(uart_t* uart,streamdevice* strdev)
404 404 {
405 405 strdev->_stream = (UHANDLE)uart;
406 406 strdev->write = (write_t)&_uartstrwrite;
407 407 strdev->read = (read_t)&_uartstrread;
408 408 strdev->setpos = (setpos_t)&_uartstrsetpos;
409 409 strdev->streamPt = 0;
410 410 return 1;
411 411 }
412 412
413 413
414 414
415 415
416 416
417 417
418 418
419 419
420 420
421 421
422 422
423 423
424 424
425 425
426 426
427 427
428 428
429 429
430 430
@@ -1,11 +1,12
1 1 TEMPLATE = subdirs
2 2 CONFIG += ordered
3 3 SUBDIRS = CORE/core.pro \
4 4 CPU/cpu.pro \
5 5 GPIO/gpio.pro \
6 6 UART/uart.pro \
7 SPI/spi.pro
7 SPI/spi.pro \
8 I2C/i2c.pro
8 9
9 10
10 11
11 12
@@ -1,207 +1,212
1 1 #---------------------------------------------------------------------------------
2 2 # LIBRARY DEFINITIONS
3 3 #---------------------------------------------------------------------------------
4 4 LIBUC_LIBS_DIR = $$libuc2/lib
5 5
6 6 INCLUDEPATH += $$LIBUC_LIBS_DIR/includes \
7 7 $$LIBUC_LIBS_DIR/includes/$$ARCH
8 8
9 9 LIBUC_BIN_LIBS_DIR = $$LIBUC_LIBS_DIR/bin/$$ARCH
10 10
11
12 11 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
13 12 -lsdcard
14 13
15 14 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
16 15 -lfat32
17 16
18 17 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
19 18 -lmbr
20 19
21 20 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
22 21 -luart
23 22
24 23 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
24 -li2c
25
26 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
25 27 -lgpio
26 28
27 29 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
28 30 -lspi
29 31
30 32 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
31 33 -lcpu
32 34
35 LIBS += -L$$LIBUC_LIBS_DIR/bin/$$ARCH \
36 -lcore
37
33 38
34 39
35 40
36 41 LIBUC_LIBS_CORE = -L $(LIBUC_LIBS_DIR_CORE) -static -lcore
37 42 LIBUC_LIBS_DIR_CORE = $(LIBUC_LIBS_DIR)
38 43 LIBUC_LIBS_DIR_CORE_CMD = -L $(LIBUC_LIBS_DIR_CORE)
39 44 LIBUC_INC_DIR_CORE = $(LIBUC_INC_DIR)
40 45 LIBUC_INC_DIR_CORE_CMD =
41 46
42 47 LIBUC_LIBS_CPU = -static -lcpu
43 48 LIBUC_LIBS_DIR_CPU = $(LIBUC_LIBS_DIR)
44 49 LIBUC_LIBS_DIR_CPU_CMD = -L $(LIBUC_LIBS_DIR_CPU)
45 50 LIBUC_INC_DIR_CPU = $(LIBUC_INC_DIR)/$(ARCH)
46 51 LIBUC_INC_DIR_CPU_CMD = -I $(LIBUC_INC_DIR_CPU)
47 52
48 53 LIBUC_LIBS_GPIO = -static -lgpio
49 54 LIBUC_LIBS_DIR_GPIO = $(LIBUC_LIBS_DIR)
50 55 LIBUC_LIBS_DIR_GPIO_CMD = -L $(LIBUC_LIBS_DIR_GPIO)
51 56 LIBUC_INC_DIR_GPIO = $(LIBUC_INC_DIR)
52 57 LIBUC_INC_DIR_GPIO_CMD =
53 58
54 59 LIBUC_LIBS_APB = -static -lapb
55 60 LIBUC_LIBS_DIR_APB = $(LIBUC_LIBS_DIR)
56 61 LIBUC_LIBS_DIR_APB_CMD = -L $(LIBUC_LIBS_DIR_APB)
57 62 LIBUC_INC_DIR_APB = $(LIBUC_INC_DIR)
58 63 LIBUC_INC_DIR_APB_CMD =
59 64
60 65 LIBUC_LIBS_UCSTRINGS = -static -lucstrings
61 66 LIBUC_LIBS_DIR_UCSTRINGS = $(LIBUC_LIBS_DIR)
62 67 LIBUC_LIBS_DIR_UCSTRINGS_CMD = -L $(LIBUC_LIBS_DIR_UCSTRINGS)
63 68 LIBUC_INC_DIR_UCSTRINGS = $(LIBUC_INC_DIR)
64 69 LIBUC_INC_DIR_UCSTRINGS_CMD =
65 70
66 71 LIBUC_LIBS_UART = -static -luart
67 72 LIBUC_LIBS_DIR_UART = $(LIBUC_LIBS_DIR)
68 73 LIBUC_LIBS_DIR_UART_CMD = -L $(LIBUC_LIBS_DIR_UART)
69 74 LIBUC_INC_DIR_UART = $(LIBUC_INC_DIR)
70 75 LIBUC_INC_DIR_UART_CMD =
71 76
72 77 LIBUC_LIBS_SPI = -static -lspi
73 78 LIBUC_LIBS_DIR_SPI = $(LIBUC_LIBS_DIR)
74 79 LIBUC_LIBS_DIR_SPI_CMD = -L $(LIBUC_LIBS_DIR_SPI)
75 80 LIBUC_INC_DIR_SPI = $(LIBUC_INC_DIR)
76 81 LIBUC_INC_DIR_SPI_CMD =
77 82
78 83 LIBUC_LIBS_SSP = -static -lssp
79 84 LIBUC_LIBS_DIR_SSP = $(LIBUC_LIBS_DIR)
80 85 LIBUC_LIBS_DIR_SSP_CMD = -L $(LIBUC_LIBS_DIR_SSP)
81 86 LIBUC_INC_DIR_SSP = $(LIBUC_INC_DIR)
82 87 LIBUC_INC_DIR_SSP_CMD =
83 88
84 89 LIBUC_LIBS_IIC = -static -liic
85 90 LIBUC_LIBS_DIR_IIC = $(LIBUC_LIBS_DIR)
86 91 LIBUC_LIBS_DIR_IIC_CMD = -L $(LIBUC_LIBS_DIR_IIC)
87 92 LIBUC_INC_DIR_IIC = $(LIBUC_INC_DIR)
88 93 LIBUC_INC_DIR_IIC_CMD =
89 94
90 95 LIBUC_LIBS_ADC = -static -ladc
91 96 LIBUC_LIBS_DIR_ADC = $(LIBUC_LIBS_DIR)
92 97 LIBUC_LIBS_DIR_ADC_CMD = -L $(LIBUC_LIBS_DIR_ADC)
93 98 LIBUC_INC_DIR_ADC = $(LIBUC_INC_DIR)
94 99 LIBUC_INC_DIR_ADC_CMD =
95 100
96 101 LIBUC_LIBS_24LC0X = -static -l24lc0X
97 102 LIBUC_LIBS_DIR_24LC0X = $(LIBUC_LIBS_DIR)
98 103 LIBUC_LIBS_DIR_24LC0X_CMD = -L $(LIBUC_LIBS_DIR_24LC0X)
99 104 LIBUC_INC_DIR_24LC0X = $(LIBUC_INC_DIR)
100 105 LIBUC_INC_DIR_24LC0X_CMD =
101 106
102 107 LIBUC_LIBS_SDCARD = -static -lsdcard
103 108 LIBUC_LIBS_DIR_SDCARD = $(LIBUC_LIBS_DIR)
104 109 LIBUC_LIBS_DIR_SDCARD_CMD = -L $(LIBUC_LIBS_DIR_SDCARD)
105 110 LIBUC_INC_DIR_SDCARD = $(LIBUC_INC_DIR)
106 111 LIBUC_INC_DIR_SDCARD_CMD =
107 112
108 113 LIBUC_LIBS_VS10XX = -static -lvs10XX
109 114 LIBUC_LIBS_DIR_VS10XX = $(LIBUC_LIBS_DIR)
110 115 LIBUC_LIBS_DIR_VS10XX_CMD = -L $(LIBUC_LIBS_DIR_VS10XX)
111 116 LIBUC_INC_DIR_VS10XX = $(LIBUC_INC_DIR)
112 117 LIBUC_INC_DIR_VS10XX_CMD =
113 118
114 119 LIBUC_LIBS_BLKDEVICE = -static -lblkdevice
115 120 LIBUC_LIBS_DIR_BLKDEVICE = $(LIBUC_LIBS_DIR)
116 121 LIBUC_LIBS_DIR_BLKDEVICE_CMD = -L $(LIBUC_LIBS_DIR_BLKDEVICE)
117 122 LIBUC_INC_DIR_BLKDEVICE = $(LIBUC_INC_DIR)
118 123 LIBUC_INC_DIR_BLKDEVICE_CMD =
119 124
120 125 LIBUC_LIBS_FAT32 = -static -lfat32
121 126 LIBUC_LIBS_DIR_FAT32 = $(LIBUC_LIBS_DIR)
122 127 LIBUC_LIBS_DIR_FAT32_CMD = -L $(LIBUC_LIBS_DIR_FAT32)
123 128 LIBUC_INC_DIR_FAT32 = $(LIBUC_INC_DIR)
124 129 LIBUC_INC_DIR_FAT32_CMD =
125 130
126 131 LIBUC_LIBS_UHANDLE = -static -luhandle
127 132 LIBUC_LIBS_DIR_UHANDLE = $(LIBUC_LIBS_DIR)
128 133 LIBUC_LIBS_DIR_UHANDLE_CMD = -L $(LIBUC_LIBS_DIR_UHANDLE)
129 134 LIBUC_INC_DIR_UHANDLE = $(LIBUC_INC_DIR)
130 135 LIBUC_INC_DIR_UHANDLE_CMD =
131 136
132 137 LIBUC_LIBS_STREAMDEVICES = -static -lstreamdevices
133 138 LIBUC_LIBS_DIR_STREAMDEVICES = $(LIBUC_LIBS_DIR)
134 139 LIBUC_LIBS_DIR_STREAMDEVICES_CMD = -L $(LIBUC_LIBS_DIR_STREAMDEVICES)
135 140 LIBUC_INC_DIR_STREAMDEVICES = $(LIBUC_INC_DIR)
136 141 LIBUC_INC_DIR_STREAMDEVICES_CMD =
137 142
138 143 LIBUC_LIBS_HEXVIEWER = -static -lhexviewer
139 144 LIBUC_LIBS_DIR_HEXVIEWER = $(LIBUC_LIBS_DIR)
140 145 LIBUC_LIBS_DIR_HEXVIEWER_CMD = -L $(LIBUC_LIBS_DIR_HEXVIEWER)
141 146 LIBUC_INC_DIR_HEXVIEWER = $(LIBUC_INC_DIR)
142 147 LIBUC_INC_DIR_HEXVIEWER_CMD =
143 148
144 149 LIBUC_LIBS_MBR = -static -lmbr
145 150 LIBUC_LIBS_DIR_MBR = $(LIBUC_LIBS_DIR)
146 151 LIBUC_LIBS_DIR_MBR_CMD = -L $(LIBUC_LIBS_DIR_MBR)
147 152 LIBUC_INC_DIR_MBR = $(LIBUC_INC_DIR)
148 153 LIBUC_INC_DIR_MBR_CMD =
149 154
150 155 LIBUC_LIBS_UCDIRENT = -static -lucdirent
151 156 LIBUC_LIBS_DIR_UCDIRENT = $(LIBUC_LIBS_DIR)
152 157 LIBUC_LIBS_DIR_UCDIRENT_CMD = -L $(LIBUC_LIBS_DIR_UCDIRENT)
153 158 LIBUC_INC_DIR_UCDIRENT = $(LIBUC_INC_DIR)
154 159 LIBUC_INC_DIR_UCDIRENT_CMD =
155 160
156 161 LIBUC_LIBS_FS = -static -lfs
157 162 LIBUC_LIBS_DIR_FS = $(LIBUC_LIBS_DIR)
158 163 LIBUC_LIBS_DIR_FS_CMD = -L $(LIBUC_LIBS_DIR_FS)
159 164 LIBUC_INC_DIR_FS = $(LIBUC_INC_DIR)
160 165 LIBUC_INC_DIR_FS_CMD =
161 166
162 167 LIBUC_LIBS_FSEXPLORER = -static -lfsexplorer
163 168 LIBUC_LIBS_DIR_FSEXPLORER = $(LIBUC_LIBS_DIR)
164 169 LIBUC_LIBS_DIR_FSEXPLORER_CMD = -L $(LIBUC_LIBS_DIR_FSEXPLORER)
165 170 LIBUC_INC_DIR_FSEXPLORER = $(LIBUC_INC_DIR)
166 171 LIBUC_INC_DIR_FSEXPLORER_CMD =
167 172
168 173 LIBUC_LIBS_DIR_ALLFS_CMD = $(LIBUC_LIBS_DIR_UCDIRENT_CMD) $(LIBUC_LIBS_DIR_FS_CMD) $(LIBUC_LIBS_DIR_MBR_CMD) $(LIBUC_LIBS_DIR_FAT32_CMD) $(LIBUC_LIBS_DIR_SDCARD_CMD) $(LIBUC_LIBS_DIR_BLKDEVICE_CMD)
169 174 LIBUC_INC_DIR_ALLFS_CMD = $(LIBUC_INC_DIR_UCDIRENT_CMD) $(LIBUC_INC_DIR_FS_CMD) $(LIBUC_INC_DIR_MBR_CMD) $(LIBUC_INC_DIR_FAT32_CMD) $(LIBUC_INC_DIR_SDCARD_CMD) $(LIBUC_INC_DIR_BLKDEVICE_CMD)
170 175 LIBUC_LIBS_ALLFS = $(LIBUC_LIBS_UCDIRENT) $(LIBUC_LIBS_FS) $(LIBUC_LIBS_MBR) $(LIBUC_LIBS_FAT32) $(LIBUC_LIBS_SDCARD) $(LIBUC_LIBS_BLKDEVICE)
171 176
172 177
173 178 LIBUC_LIBS_ADS127X = -static -lads127X
174 179 LIBUC_LIBS_DIR_ADS127X = $(LIBUC_LIBS_DIR)
175 180 LIBUC_LIBS_DIR_ADS127X_CMD = -L $(LIBUC_LIBS_DIR_ADS127X)
176 181 LIBUC_INC_DIR_ADS127X = $(LIBUC_INC_DIR)
177 182 LIBUC_INC_DIR_ADS127X_CMD =
178 183
179 184
180 185 LIBUC_LIBS_NXPLIB = -static -lnxplib
181 186 LIBUC_LIBS_DIR_NXPLIB = $(LIBUC_LIBS_DIR)
182 187 LIBUC_LIBS_DIR_NXPLIB_CMD = -L $(LIBUC_LIBS_DIR_NXPLIB)
183 188 LIBUC_INC_DIR_NXPLIB = $(LIBUC_INC_DIR)/LPCXXXX
184 189 LIBUC_INC_DIR_NXPLIB_CMD = -I $(LIBUC_INC_DIR_NXPLIB)
185 190
186 191 LIBUC_LIBS_ARMMATH = -static -larmmath
187 192 LIBUC_LIBS_DIR_ARMMATH = $(LIBUC_LIBS_DIR)
188 193 LIBUC_LIBS_DIR_ARMMATH_CMD = -L $(LIBUC_LIBS_DIR_ARMMATH)
189 194 LIBUC_INC_DIR_ARMMATH = $(LIBUC_INC_DIR)/ARM
190 195 LIBUC_INC_DIR_ARMMATH_CMD = $(LIBUC_INC_DIR_ARMMATH)
191 196
192 197
193 198
194 199
195 200
196 201
197 202
198 203
199 204
200 205
201 206
202 207
203 208
204 209
205 210
206 211
207 212
General Comments 0
You need to be logged in to leave comments. Login now