##// END OF EJS Templates
Added cpuid getter...
Alexis Jeandet -
r104:cfe8b1e0657d dev_alexis draft
parent child
Show More
@@ -1,52 +1,67
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2013, 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 <gpio.h>
24 24
25 #define bsp_gpio_init_list(list) __bsp_gpio_init_list(list,sizeof(list)/sizeof(gpioinitlist_str))
26
27 typedef struct
28 {
29 gpio_t gpio;
30 gpioconfig_str config;
31 }gpioinitlist_str;
32
33
34
35 void __bsp_gpio_init_list(gpioinitlist_str* list, int len)
36 {
37 for(int i=0;i<len;i++)
38 {
39 gpioopenandconfig(list[i].gpio,&(list[i].config));
40 }
41 }
42
25 43 void bsp_gpio_set_outputs(gpio_t* GPIO_Out_init_List,int len)
26 44 {
27 45 gpio_t GPIO_init;
28 46 for(int i=0;i<len;i++)
29 47 {
30 48 GPIO_init = gpioopen(GPIO_Out_init_List[i]);
31 GPIO_init |= gpiohighspeed | gpiooutdir | gpiopushpulltype;
32 gpiosetconfig(&GPIO_init);
49 gpiosetconfig(GPIO_init,gpiooutdir,gpiohighspeed,gpiopushpulltype,gpionopulltype);
33 50 }
34 51 }
35 52
36 53 void bsp_gpio_set_inputs(gpio_t* GPIO_In_init_List,int len)
37 54 {
38 gpio_t GPIO_init;
39 55 for(int i=0;i<len;i++)
40 56 {
41 GPIO_init = gpioopen(GPIO_In_init_List[i]);
42 GPIO_init |= gpiohighspeed | gpioindir;
43 gpiosetconfig(&GPIO_init);
57 gpio_t GPIO_init = gpioopen(GPIO_In_init_List[i]);
58 gpiosetconfig(GPIO_init,gpioindir,gpiohighspeed,0,0);
44 59 }
45 60 }
46 61
47 62
48 63
49 64
50 65
51 66
52 67
@@ -1,314 +1,313
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@member.fsf.org
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 <stm32f4xx_gpio.h>
29 29 #include <stm32f4xx_fsmc.h>
30 30 #include <i2c.h>
31 31 #include <core.h>
32 32 #include <terminal.h>
33 33
34 34 uint32_t OSC0 =8000000;
35 35 uint32_t INTOSC =16000000;
36 36 uint32_t RTCOSC =32768;
37 37 uint32_t currentCpuFreq=0;
38 38 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
39 39 gpio_t TickLed = LED1;
40 40
41 41 LCD_IF_t lcdIF0={
42 42 .init = &bsp_FSMC_init,
43 43 .writereg = &bsp_lcd0_write_reg,
44 44 .readreg = &bsp_lcd0_read_reg,
45 45 .writeGRAM = &bsp_lcd0_writeGRAM,
46 46 .readGRAM = &bsp_lcd0_readGRAM
47 47 };
48 48
49 49 LCD_t lcd0={
50 50 .interface = &lcdIF0,
51 51 .init = &ili9328init,
52 52 .paint = &ili9328paint,
53 53 .paintText = &ili9328paintText,
54 54 .paintFilRect = &ili9328paintFilRect,
55 55 .getPix = &ili9328getPix,
56 56 .refreshenable = &ili9328refreshenable,
57 57 .width= 240,
58 58 .height = 320
59 59 };
60 60
61 61 terminal_t terminal0;
62 62
63 63 volatile int16_t* lcd0_CMD=(volatile int16_t*)0x60000000;
64 64 volatile int16_t* lcd0_DATA=(volatile int16_t*)0x61FFFFF0;
65 65
66 66 float VREF0 =(float)3.3;
67 67
68 68 int bsp_init()
69 69 {
70 70 int i=0;
71 71 for(i=0;i<32;i++)
72 72 {
73 73 __opnfiles__[i] = NULL;
74 74 }
75 75 bsp_GPIO_init();
76 76 bsp_uart_init();
77 77 bsp_iic_init();
78 78 bsp_FSMC_init();
79 79 bsp_GTerm_init();
80 80 printf("\r=====================\n\r");
81 81 printf( "=====================\n\r");
82 82 printf(BSP);
83 83 printf(" initialised\n\r");
84 84 printf( "=====================\n\r");
85 85 return 1;
86 86 }
87 87
88 88 void bsp_GPIO_init()
89 89 {
90 90 gpio_t gpio1 = gpioopen(LED1);
91 91 gpio_t gpio2 = gpioopen(LED2);
92 92 gpio_t gpio3 = gpioopen(LED3);
93 93 gpio_t gpio4 = gpioopen(PSU_DISABLE);
94 94 gpio_t gpio5 = gpioopen(PSU_ALERT_5V);
95 95 gpio_t gpio6 = gpioopen(PSU_ALERT_1_5V);
96 96 gpio_t gpio7 = gpioopen(PSU_ALERT_3_3V);
97 gpiosetspeed(&gpio1,gpiohighspeed);
98 gpiosetspeed(&gpio2,gpiohighspeed);
99 gpiosetspeed(&gpio3,gpiohighspeed);
100 gpiosetspeed(&gpio4,gpiohighspeed);
101 gpiosetspeed(&gpio5,gpiohighspeed);
102 gpiosetspeed(&gpio6,gpiohighspeed);
103 gpiosetspeed(&gpio7,gpiohighspeed);
97 gpiosetspeed(gpio1,gpiohighspeed);
98 gpiosetspeed(gpio2,gpiohighspeed);
99 gpiosetspeed(gpio3,gpiohighspeed);
100 gpiosetspeed(gpio4,gpiohighspeed);
101 gpiosetspeed(gpio5,gpiohighspeed);
102 gpiosetspeed(gpio6,gpiohighspeed);
103 gpiosetspeed(gpio7,gpiohighspeed);
104 104 gpioclr(PSU_DISABLE);
105 gpiosetdir(&gpio1,gpiooutdir);
106 gpiosetdir(&gpio3,gpiooutdir);
107 gpiosetdir(&gpio2,gpiooutdir);
108 gpiosetdir(&gpio4,gpiooutdir);
109 gpiosetdir(&gpio5,gpioindir);
110 gpiosetdir(&gpio6,gpioindir);
111 gpiosetdir(&gpio7,gpioindir);
105 gpiosetdir(gpio1,gpiooutdir);
106 gpiosetdir(gpio3,gpiooutdir);
107 gpiosetdir(gpio2,gpiooutdir);
108 gpiosetdir(gpio4,gpiooutdir);
109 gpiosetdir(gpio5,gpioindir);
110 gpiosetdir(gpio6,gpioindir);
111 gpiosetdir(gpio7,gpioindir);
112 112 gpioclr(PSU_DISABLE);
113 113 }
114 114
115 115 void bsp_uart_init()
116 116 {
117 117 //if(__opnfiles__[1]==NULL)
118 118 //{
119 119 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
120 120 // streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
121 121 // uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
122 122 //uartmkstreamdev(uart,fd1);
123 123 //__opnfiles__[1] = fd1;
124 124 //}
125 125 //else
126 126 //{
127 127 uartopenandconfig(0,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
128 128 //}
129 129 }
130 130
131 131
132 132
133 133 int bsp_FSMC_init()
134 134 {
135 135 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
136 136 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
137 137
138 138 gpio_t gpio1 = gpioopen(LCD_RESET);
139 gpiosetspeed(&gpio1,gpiohighspeed);
140 gpiosetdir(&gpio1,gpiooutdir);
139 gpiosetspeed(gpio1,gpiohighspeed);
140 gpiosetdir(gpio1,gpiooutdir);
141 141 gpioclr(LCD_RESET);
142 142
143 143 gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10,PE11,PE12,PE13,PE14,PE15\
144 144 ,PD8,PD9,PD10,PD4,PD5,PD7,PE4};
145 145 for(int i=0;i<20;i++)
146 146 {
147 147 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
148 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
149 gpiosetconfig(&LCD_DBx);
148 gpiosetconfig(LCD_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
150 149 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
151 150 }
152 151
153 152 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
154 153 FSMC_NORSRAMTimingInitTypeDef p;
155 154
156 155 /* Enable FSMC clock */
157 156 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
158 157
159 158 /*-- FSMC Configuration ------------------------------------------------------*/
160 159 /*----------------------- SRAM Bank 3 ----------------------------------------*/
161 160 /* FSMC_Bank1_NORSRAM4 configuration */
162 161 //p.FSMC_AddressSetupTime = 5;
163 162 p.FSMC_AddressSetupTime = 1;
164 163 p.FSMC_AddressHoldTime = 0;
165 164 //p.FSMC_DataSetupTime = 9;
166 165 p.FSMC_DataSetupTime = getCpuFreq()/14545450 ;// 11;
167 166 p.FSMC_BusTurnAroundDuration = 0;
168 167 p.FSMC_CLKDivision = 0;
169 168 p.FSMC_DataLatency = 0;
170 169 p.FSMC_AccessMode = FSMC_AccessMode_A;
171 170 /* Color LCD configuration ------------------------------------
172 171 LCD configured as follow:
173 172 - Data/Address MUX = Disable
174 173 - Memory Type = SRAM
175 174 - Data Width = 16bit
176 175 - Write Operation = Enable
177 176 - Extended Mode = Enable
178 177 - Asynchronous Wait = Disable */
179 178
180 179 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
181 180 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
182 181 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
183 182 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
184 183 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
185 184 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
186 185 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
187 186 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
188 187 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
189 188 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
190 189 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
191 190 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
192 191 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
193 192 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
194 193 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
195 194
196 195 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
197 196
198 197 /* Enable FSMC NOR/SRAM Bank1 */
199 198 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
200 199 gpioset(LCD_RESET);
201 200 lcd0.init(&lcd0);
202 201 return 1;
203 202 }
204 203
205 204 void bsp_spi_init()
206 205 {
207 206
208 207 }
209 208
210 209
211 210 void bsp_iic_init()
212 211 {
213 212 i2copenandconfig(i2c2,0,10000,PF0,PF1);
214 213 }
215 214
216 215 void bsp_SD_init()
217 216 {
218 217
219 218 }
220 219
221 220 void vs10XXclearXCS(){}
222 221 void vs10XXsetXCS(){}
223 222 int vs10XXDREQ()
224 223 {
225 224 return 1;
226 225 }
227 226
228 227
229 228 void bsppowersdcard(char onoff) //always ON
230 229 {
231 230
232 231 }
233 232
234 233 char bspsdcardpresent()
235 234 {
236 235 return 0;
237 236 }
238 237
239 238 char bspsdcardwriteprotected()
240 239 {
241 240 return 0;
242 241 }
243 242
244 243 void bspsdcardselect(char YESNO)
245 244 {
246 245
247 246 }
248 247
249 248
250 249 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
251 250 {
252 251 *lcd0_CMD=(uint16_t)reg;
253 252 *lcd0_DATA=(uint16_t)data;
254 253 }
255 254
256 255 uint32_t bsp_lcd0_read_reg(uint32_t reg)
257 256 {
258 257 *lcd0_CMD=(uint16_t)reg;
259 258 return (uint16_t)*lcd0_DATA;
260 259 }
261 260
262 261 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
263 262 {
264 263 *lcd0_CMD=(uint16_t)ILI9328_REGISTER_WRITEDATATOGRAM;
265 264 uint16_t* castedBuff=(uint16_t*)buffer;
266 265 for(int i=0;i<(int)count;i++)
267 266 {
268 267 *lcd0_DATA=castedBuff[i];
269 268 }
270 269 }
271 270
272 271 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
273 272 {
274 273 *lcd0_CMD=(uint16_t)ILI9328_REGISTER_WRITEDATATOGRAM;
275 274 uint16_t* castedBuff=(uint16_t*)buffer;
276 275 castedBuff[0]=*lcd0_DATA;
277 276 for(int i=0;i<(int)count;i++)
278 277 {
279 278 castedBuff[i]=*lcd0_DATA;
280 279 }
281 280 }
282 281
283 282
284 283 void bsp_GTerm_init()
285 284 {
286 285 if(__opnfiles__[1]==NULL)
287 286 {
288 287 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
289 288 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
290 289 terminal_init(&terminal0 ,&lcd0,&ComicSansMS_8,fd1);
291 290 __opnfiles__[1] = fd1;
292 291 }
293 292 else
294 293 {
295 294
296 295 }
297 296 }
298 297
299 298
300 299
301 300
302 301
303 302
304 303
305 304
306 305
307 306
308 307
309 308
310 309
311 310
312 311
313 312
314 313
@@ -1,127 +1,127
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@member.fsf.org
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 uint32_t OSC0 =8000000;
29 29 uint32_t INTOSC =16000000;
30 30 uint32_t RTCOSC =32768;
31 31 uint32_t currentCpuFreq=0;
32 32 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
33 33 gpio_t TickLed = LED1;
34 34
35 35 float VREF0 =(float)3.3;
36 36
37 37 int bsp_init()
38 38 {
39 39 int i=0;
40 40 for(i=0;i<32;i++)
41 41 {
42 42 __opnfiles__[i] = NULL;
43 43 }
44 44 bsp_GPIO_init();
45 45 bsp_uart_init();
46 46 printf("\r================================================================\n\r");
47 47 printf("================================================================\n\r");
48 48 printf(BSP);
49 49 printf(" initialised\n\r");
50 50 printf("================================================================\n\r");
51 51 return 1;
52 52 }
53 53
54 54 void bsp_GPIO_init()
55 55 {
56 56 gpio_t gpio1 = gpioopen(PC6);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009
57 57 gpio_t gpio2 = gpioopen(PC7);//gpioopen(LED2);
58 gpiosetspeed(&gpio1,gpiohighspeed);
59 gpiosetspeed(&gpio2,gpiohighspeed);
60 gpiosetdir(&gpio1,gpiooutdir);
61 gpiosetdir(&gpio2,gpiooutdir);
58 gpiosetspeed(gpio1,gpiohighspeed);
59 gpiosetspeed(gpio2,gpiohighspeed);
60 gpiosetdir(gpio1,gpiooutdir);
61 gpiosetdir(gpio2,gpiooutdir);
62 62 }
63 63
64 64 void bsp_uart_init()
65 65 {
66 66 if(__opnfiles__[1]==NULL)
67 67 {
68 68 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
69 69 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
70 70 uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,LED2,LED1,-1,-1);
71 71 uartmkstreamdev(uart,fd1);
72 72 __opnfiles__[1] = fd1;
73 73 }
74 74 else
75 75 {
76 76 uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,LED2,LED1,-1,-1);
77 77 }
78 78 }
79 79
80 80 void bsp_spi_init()
81 81 {
82 82
83 83 }
84 84
85 85
86 86 void bsp_iic_init()
87 87 {
88 88
89 89 }
90 90
91 91 void bsp_SD_init()
92 92 {
93 93
94 94 }
95 95
96 96 void vs10XXclearXCS(){}
97 97 void vs10XXsetXCS(){}
98 98 int vs10XXDREQ()
99 99 {
100 100 return 1;
101 101 }
102 102
103 103
104 104 void bsppowersdcard(char onoff) //always ON
105 105 {
106 106
107 107 }
108 108
109 109 char bspsdcardpresent()
110 110 {
111 111 return 0;
112 112 }
113 113
114 114 char bspsdcardwriteprotected()
115 115 {
116 116 return 0;
117 117 }
118 118
119 119 void bspsdcardselect(char YESNO)
120 120 {
121 121
122 122 }
123 123
124 124
125 125
126 126
127 127
@@ -1,144 +1,144
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@member.fsf.org
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 <spi.h>
28 28 #include <stdio.h>
29 29 #include <core.h>
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 gpio_t TickLed = LED1;
36 36
37 37 float VREF0 =(float)3.3;
38 38
39 39 int bsp_init()
40 40 {
41 41 int i=0;
42 42 for(i=0;i<32;i++)
43 43 {
44 44 __opnfiles__[i] = NULL;
45 45 }
46 46 bsp_GPIO_init();
47 47 bsp_uart_init();
48 48 bsp_spi_init();
49 49 printf("\r================================================================\n\r");
50 50 printf("================================================================\n\r");
51 51 printf(BSP);
52 52 printf(" initialised\n\r");
53 53 printf("================================================================\n\r");
54 54 return 1;
55 55 }
56 56
57 57 void bsp_GPIO_init()
58 58 {
59 59 gpio_t GPIOList[]={LED1,LED2,LED3,ADC_MODE0,ADC_MODE1,ADC_FSYNC
60 60 ,ADC_SYNC,ADC_CLKDIV};
61 61 gpio_t GPIOx;
62 62 for(int i=0;i<8;i++)
63 63 {
64 64 GPIOx = gpioopen(GPIOList[i]);
65 gpiosetspeed(&GPIOx,gpiohighspeed);
66 gpiosetdir(&GPIOx,gpiooutdir);
67 gpiosetouttype(&GPIOx,gpiopushpulltype);
65 gpiosetspeed(GPIOx,gpiohighspeed);
66 gpiosetdir(GPIOx,gpiooutdir);
67 gpiosetouttype(GPIOx,gpiopushpulltype);
68 68 }
69 69 gpioset(ADC_CLKDIV);
70 70 gpioset(ADC_MODE0);
71 71 gpioset(ADC_MODE1);
72 72 gpioclr(ADC_SYNC);
73 73 gpioclr(ADC_FSYNC);
74 74 delay_100us(10);
75 75 gpioset(ADC_SYNC);
76 76
77 77 }
78 78
79 79 void bsp_uart_init()
80 80 {
81 81 if(__opnfiles__[1]==NULL)
82 82 {
83 83 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
84 84 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
85 85 //uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,230400,PA9,PA10,-1,-1);
86 86 uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
87 87 uartmkstreamdev(uart,fd1);
88 88 __opnfiles__[1] = fd1;
89 89 }
90 90 else
91 91 {
92 92 // uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,230400,PA9,PA10,-1,-1);
93 93 uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
94 94 }
95 95 }
96 96
97 97 void bsp_spi_init()
98 98 {
99 99 spiopenandconfig(spi1,spi8bits | spimaster |spiclkinhlow | spiclkfirstedge | spimsbfirst,4000000,DAC_DIN,ADC_DOUT1,ADC_SCLK,-1);
100 100 }
101 101
102 102
103 103 void bsp_iic_init()
104 104 {
105 105
106 106 }
107 107
108 108 void bsp_SD_init()
109 109 {
110 110
111 111 }
112 112
113 113 void vs10XXclearXCS(){}
114 114 void vs10XXsetXCS(){}
115 115 int vs10XXDREQ()
116 116 {
117 117 return 1;
118 118 }
119 119
120 120
121 121 void bsppowersdcard(char onoff) //always ON
122 122 {
123 123
124 124 }
125 125
126 126 char bspsdcardpresent()
127 127 {
128 128 return 0;
129 129 }
130 130
131 131 char bspsdcardwriteprotected()
132 132 {
133 133 return 0;
134 134 }
135 135
136 136 void bspsdcardselect(char YESNO)
137 137 {
138 138
139 139 }
140 140
141 141
142 142
143 143
144 144
@@ -1,266 +1,265
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2013, 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 #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 <stm32f4xx_gpio.h>
29 29 #include <stm32f4xx_fsmc.h>
30 30 #include <i2c.h>
31 31 #include <core.h>
32 32 #include <terminal.h>
33 33
34 34
35 35 #define clr_RS gpioclr(LCD_RS)
36 36 #define set_RS gpioset(LCD_RS)
37 37
38 38 #define clr_CS gpioclr(LCD_CS)
39 39 #define set_CS gpioset(LCD_CS)
40 40
41 41 volatile int8_t* lcd0_CMD=(volatile int8_t*)0x60000000;
42 42 volatile int16_t* lcd0_CMD16=(volatile int16_t*)0x60000000;
43 43 volatile int8_t* lcd0_DATA=(volatile int8_t*)0x61FFFFF0;
44 44 volatile int16_t* lcd0_DATA16=(volatile int16_t*)0x61FFFFF0;
45 45
46 46 /*
47 47 D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7
48 48 D5 PE8 D6 PE9 D7 PE10
49 49 A20 PE4 = RS FSMC_NE1 PD7 CS FSMC_NWE PD5 W/S
50 50 FSMC_NOE PD4 RD
51 51 */
52 52
53 53 int bsp_FSMC_init()
54 54 {
55 55
56 56 gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\
57 57 ,PD4,PD5};
58 58 // gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\
59 59 // ,PD4,PD5,PD7,PE4};
60 60 for(int i=0;i<10;i++)
61 61 {
62 62 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
63 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
64 gpiosetconfig(&LCD_DBx);
63 gpiosetconfig(LCD_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
65 64 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
66 65 }
67 66
68 67 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
69 68 FSMC_NORSRAMTimingInitTypeDef p;
70 69
71 70 /* Enable FSMC clock */
72 71 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
73 72
74 73 /*-- FSMC Configuration ------------------------------------------------------*/
75 74 /*----------------------- SRAM Bank 3 ----------------------------------------*/
76 75 /* FSMC_Bank1_NORSRAM4 configuration */
77 76 p.FSMC_AddressSetupTime = 1;//3
78 77 p.FSMC_AddressHoldTime = 1;//3
79 78 //ili9328 -> data setup time > 10ns
80 79 p.FSMC_DataSetupTime = 6;
81 80 p.FSMC_CLKDivision = 3;
82 81 if(getCpuFreq()>=100*1000*1000)
83 82 {
84 83 p.FSMC_CLKDivision = 3;
85 84 p.FSMC_DataSetupTime = 6;// 11;
86 85 }
87 86 p.FSMC_BusTurnAroundDuration = 0;
88 87 p.FSMC_DataLatency = 3;
89 88 //ili9328 -> data hold time > 15ns
90 89 if(getCpuFreq()>66*1000*1000)
91 90 p.FSMC_DataLatency = 3;
92 91 p.FSMC_AccessMode = FSMC_AccessMode_D;
93 92
94 93
95 94 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
96 95 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
97 96 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
98 97 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
99 98 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
100 99 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
101 100 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
102 101 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
103 102 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
104 103 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
105 104 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
106 105 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; //Dis
107 106 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
108 107 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
109 108 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
110 109
111 110 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
112 111
113 112 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
114 113 gpioset(LCD_RESET);
115 114 gpioclr(LCD_RESET);
116 115 delay_100us(500);
117 116 gpioset(LCD_RESET);
118 117 delay_100us(500);
119 118 lcd0.init(&lcd0);
120 119 gpioset(LCD_BACKL);
121 120 return 1;
122 121 }
123 122
124 123
125 124
126 125 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
127 126 {
128 127 uint8_t* pt8 = (uint8_t*)(void*)(&reg);
129 128 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
130 129 *lcd0_CMD=pt8[3];
131 130 *lcd0_CMD=pt8[2];
132 131 pt8 = (uint8_t*)(void*)&data;
133 132 *lcd0_DATA=pt8[3];
134 133 *lcd0_DATA=pt8[2];
135 134 #else
136 135 clr_CS;
137 136 clr_RS;
138 137 *lcd0_CMD=pt8[1];
139 138 *lcd0_CMD=pt8[0];
140 139 set_RS;
141 140 pt8 = (uint8_t*)(void*)&data;
142 141 *lcd0_DATA=pt8[1];
143 142 *lcd0_DATA=pt8[0];
144 143 set_CS;
145 144 #endif
146 145
147 146 }
148 147
149 148 uint32_t bsp_lcd0_read_reg(uint32_t reg)
150 149 {
151 150 uint8_t* pt8 = (uint8_t*)(void*)(&reg);
152 151 uint32_t DATA=0;
153 152
154 153 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
155 154 *lcd0_CMD=pt8[3];
156 155 *lcd0_CMD=pt8[2];
157 156 pt8 = (uint8_t*)(void*)&DATA;
158 157 pt8[3]=*lcd0_DATA;
159 158 pt8[2]=*lcd0_DATA;
160 159 #else
161 160 clr_CS;
162 161 clr_RS;
163 162 *lcd0_CMD=pt8[1];
164 163 *lcd0_CMD=pt8[0];
165 164 set_RS;
166 165 pt8 = (uint8_t*)(void*)&DATA;
167 166 pt8[1]=*lcd0_DATA;
168 167 pt8[0]=*lcd0_DATA;
169 168 set_CS;
170 169 #endif
171 170
172 171 return DATA;
173 172 }
174 173
175 174 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
176 175 {
177 176 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
178 177 uint8_t* pt8 = (uint8_t*)(void*)(&reg);
179 178 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
180 179 *lcd0_CMD=pt8[3];
181 180 *lcd0_CMD=pt8[2];
182 181 pt8 = (uint8_t*)(void*)buffer;
183 182 for(int i=0;i<(int)count;i++)
184 183 {
185 184 *lcd0_DATA=pt8[(2*i) +1];
186 185 *lcd0_DATA=pt8[2*i];
187 186 }
188 187 #else
189 188 clr_CS;
190 189 clr_RS;
191 190 *lcd0_CMD=pt8[1];
192 191 *lcd0_CMD=pt8[0];
193 192 set_RS;
194 193 pt8 = (uint8_t*)(void*)buffer;
195 194 for(int i=0;i<(int)count;i++)
196 195 {
197 196
198 197 *lcd0_DATA=pt8[(2*i) +1];
199 198 *lcd0_DATA=pt8[2*i];
200 199 }
201 200 set_CS;
202 201 #endif
203 202 }
204 203
205 204
206 205 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
207 206 {
208 207 //uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
209 208 volatile uint8_t* pt8;// = (uint8_t*)(void*)&reg;
210 209 /* #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
211 210 *lcd0_CMD=pt8[3];
212 211 *lcd0_CMD=pt8[2];
213 212 pt8 = (uint8_t*)(void*)buffer;
214 213 for(int i=0;i<(int)count;i++)
215 214 {
216 215 pt8[(2*i) +1]=*lcd0_DATA;
217 216 pt8[2*i]=*lcd0_DATA;
218 217 }
219 218 #else
220 219 *lcd0_CMD=(uint8_t)0;
221 220 *lcd0_CMD=(uint8_t)0;
222 221 *lcd0_CMD=(uint8_t)0;
223 222 *lcd0_CMD=(uint8_t)0;
224 223 *lcd0_CMD=pt8[1];
225 224 *lcd0_CMD=pt8[0];
226 225 pt8 = (uint8_t*)buffer;*/
227 226 /*
228 227 * x dummy reads Cf ili9328 datasheet p79!
229 228 */
230 229 /* pt8[0]=*lcd0_DATA;
231 230 pt8[1]=*lcd0_DATA;
232 231
233 232 for(int i=0;i<(int)count;i++)
234 233 {
235 234 pt8[(2*i) +1]=*lcd0_DATA;
236 235 pt8[2*i]=*lcd0_DATA;
237 236 pt8[(2*i) +1]=*lcd0_DATA;
238 237 pt8[2*i]=*lcd0_DATA;
239 238 }
240 239 #endif*/
241 240 //clr_CS;
242 241 //clr_RS;
243 242 //*lcd0_CMD=(int8_t)0;
244 243 //*lcd0_CMD=(int8_t)0x22;
245 244 // *lcd0_CMD=(int8_t)0x00;
246 245 //set_RS;
247 246 pt8 = (uint8_t*)buffer;
248 247 //pt8[1]=*lcd0_DATA;
249 248 //pt8[0]=*lcd0_DATA;
250 249 pt8[0]=0xFF;
251 250 pt8[1]=0xFF;
252 251 /* for(int i=0;i<(int)count;i++)
253 252 {
254 253 pt8[(2*i)+1]= *lcd0_DATA;
255 254 pt8[2*i]= *lcd0_DATA;
256 255 }*/
257 256 set_CS;
258 257 }
259 258
260 259
261 260
262 261
263 262
264 263
265 264
266 265
@@ -1,70 +1,67
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2013, 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 #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 <stm32f4xx_gpio.h>
29 29 #include <stm32f4xx_fsmc.h>
30 30 #include <i2c.h>
31 31 #include <core.h>
32 32 #include <terminal.h>
33 33
34 34 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
35 35 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
36 36
37 37
38 38 void bsp_spi_init()
39 39 {
40 40 gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO};
41 41 for(int i=0;i<3;i++)
42 42 {
43 43 gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]);
44 SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
45 gpiosetconfig(&SPI_DBx);
44 gpiosetconfig(SPI_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
46 45 GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1);
47 46 }
48 47 spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1);
49 48
50 49 gpio_t SDSPI_DBxList[]={SDCARD2SCK,SDCARD2MOSI,SDCARD2MISO};
51 50 for(int i=0;i<3;i++)
52 51 {
53 52 gpio_t SPI_DBx = gpioopen(SDSPI_DBxList[i]);
54 SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
55 gpiosetconfig(&SPI_DBx);
53 gpiosetconfig(SPI_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
56 54 GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI3);
57 55 }
58 56 spiopenandconfig(SDCARD2SPI,spi8bits|spimaster|spimsbfirst,400*1000,SDCARD2MOSI,SDCARD2MISO,SDCARD2SCK,-1);
59 57
60 58 gpio_t TCSPI_DBxList[]={TC_CLK,TC_DIN,TC_DOUT};
61 59 for(int i=0;i<3;i++)
62 60 {
63 61 gpio_t SPI_DBx = gpioopen(TCSPI_DBxList[i]);
64 SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
65 gpiosetconfig(&SPI_DBx);
62 gpiosetconfig(SPI_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
66 63 GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI2);
67 64 }
68 65 spiopenandconfig(TC_SPI,spi8bits|spimaster|spimsbfirst,500*1000,TC_DIN,TC_DOUT,TC_CLK,-1);
69 66
70 67 }
@@ -1,141 +1,141
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@member.fsf.org
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 gpio_t TickLed = LED1;
36 36
37 37 float VREF0 =(float)3.3;
38 38
39 39 int bsp_init()
40 40 {
41 41 int i=0;
42 42 for(i=0;i<32;i++)
43 43 {
44 44 __opnfiles__[i] = NULL;
45 45 }
46 46 bsp_GPIO_init();
47 47 bsp_uart_init();
48 48 bsp_iic_init();
49 49 printf("\r================================================================\n\r");
50 50 printf("================================================================\n\r");
51 51 printf(BSP);
52 52 printf(" initialised\n\r");
53 53 printf("================================================================\n\r");
54 54 return 1;
55 55 }
56 56
57 57 void bsp_GPIO_init()
58 58 {
59 59 gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009
60 60 gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2);
61 61 gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2);
62 62 gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2);
63 63 gpio_t dacRst=gpioopen(PD4);
64 gpiosetspeed(&gpio1,gpiohighspeed);
65 gpiosetspeed(&gpio2,gpiohighspeed);
66 gpiosetspeed(&gpio3,gpiohighspeed);
67 gpiosetspeed(&gpio4,gpiohighspeed);
68 gpiosetspeed(&dacRst,gpiohighspeed);
69 gpiosetdir(&gpio1,gpiooutdir);
70 gpiosetdir(&gpio3,gpiooutdir);
71 gpiosetdir(&gpio2,gpiooutdir);
72 gpiosetdir(&gpio4,gpiooutdir);
73 gpiosetdir(&dacRst,gpiooutdir);
64 gpiosetspeed(gpio1,gpiohighspeed);
65 gpiosetspeed(gpio2,gpiohighspeed);
66 gpiosetspeed(gpio3,gpiohighspeed);
67 gpiosetspeed(gpio4,gpiohighspeed);
68 gpiosetspeed(dacRst,gpiohighspeed);
69 gpiosetdir(gpio1,gpiooutdir);
70 gpiosetdir(gpio3,gpiooutdir);
71 gpiosetdir(gpio2,gpiooutdir);
72 gpiosetdir(gpio4,gpiooutdir);
73 gpiosetdir(dacRst,gpiooutdir);
74 74 gpioset(dacRst);
75 75 }
76 76
77 77 void bsp_uart_init()
78 78 {
79 79 if(__opnfiles__[1]==NULL)
80 80 {
81 81 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
82 82 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
83 83 uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
84 84 uartmkstreamdev(uart,fd1);
85 85 __opnfiles__[1] = fd1;
86 86 }
87 87 else
88 88 {
89 89 uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
90 90 }
91 91 }
92 92
93 93 void bsp_spi_init()
94 94 {
95 95
96 96 }
97 97
98 98
99 99 void bsp_iic_init()
100 100 {
101 101 i2copenandconfig(i2c1,0,400000,PB9,PB6);
102 102 i2copenandconfig(i2c3,0,400000,PC9,PA8);
103 103 }
104 104
105 105 void bsp_SD_init()
106 106 {
107 107
108 108 }
109 109
110 110 void vs10XXclearXCS(){}
111 111 void vs10XXsetXCS(){}
112 112 int vs10XXDREQ()
113 113 {
114 114 return 1;
115 115 }
116 116
117 117
118 118 void bsppowersdcard(char onoff) //always ON
119 119 {
120 120
121 121 }
122 122
123 123 char bspsdcardpresent()
124 124 {
125 125 return 0;
126 126 }
127 127
128 128 char bspsdcardwriteprotected()
129 129 {
130 130 return 0;
131 131 }
132 132
133 133 void bspsdcardselect(char YESNO)
134 134 {
135 135
136 136 }
137 137
138 138
139 139
140 140
141 141
@@ -1,516 +1,485
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@member.fsf.org
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 <stm32f4xx_gpio.h>
29 29 #include <stm32f4xx_fsmc.h>
30 30 #include <i2c.h>
31 31 #include <core.h>
32 32 #include <terminal.h>
33 33 #include <pwm.h>
34 34 #include <spi.h>
35 #include <core.h>
36 #include "../../common/bsp_gpio.h"
35 37
36 38 uint32_t OSC0 =12000000;
37 39 uint32_t INTOSC =16000000;
38 40 uint32_t RTCOSC =32768;
39 41 uint32_t currentCpuFreq=0;
40 42 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
41 43 gpio_t TickLed = LED1;
42 44
43 45 LCD_IF_t lcdIF0={
44 46 .init = &bsp_FSMC_init,
45 47 .status = &bsp_lcd0_status,
46 48 .writereg = &bsp_lcd0_write_reg,
47 49 .readreg = &bsp_lcd0_read_reg,
48 50 .writeGRAM = &bsp_lcd0_writeGRAM,
49 51 .readGRAM = &bsp_lcd0_readGRAM
50 52 };
51 53
52 54 LCD_t lcd0={
53 55 .interface = &lcdIF0,
54 56 .init = &D51E5TA7601init,
55 57 .paint = &D51E5TA7601paint,
56 58 .paintFB = D51E5TA7601paintFB,
57 59 .setFrame = &D51E5TA7601setFrame,
58 60 .paintText = &D51E5TA7601paintText,
59 61 .paintFilRect = &D51E5TA7601paintFilRect,
60 62 .getPix = &D51E5TA7601getPix,
61 63 .refreshenable = &D51E5TA7601refreshenable,
62 64 .width= 320,
63 65 .height = 480,
64 66 .isFrameBuffer = 0
65 67 };
66 68
67 69 terminal_t terminal0;
68 70
69 71 volatile int16_t* lcd0_CMD=(volatile int16_t*)0x60000000;
70 72 volatile int16_t* lcd0_DATA=(volatile int16_t*)0x61FFFFF0;
71 73 volatile int16_t* SRAM_DATA=(volatile int16_t*)0x64000000;
72 74
73 75 // Frame buffers 300kB per buffer, RAM size=1M*16 -> 6.8 buffers max
74 76 volatile int16_t* FRAME_BUFFER0_data=(volatile int16_t*)0x64000000;
75 77 volatile int16_t* FRAME_BUFFER1_data=((volatile int16_t*)0x64000000)+(320*480); //300kB per buffer
76 78 LCD_t __attribute__ ((aligned (4))) FRAME_BUFFER0=
77 79 {
78 80 .interface.buffer=(int16_t*)0x64000000,
79 81 .init = &D51E5TA7601init_FrameBuff,
80 82 .paint = &D51E5TA7601paint_FrameBuff,
81 83 .setFrame = &D51E5TA7601setFrame_FrameBuff,
82 84 .paintText = &D51E5TA7601paintText_FrameBuff,
83 85 .paintFilRect = &D51E5TA7601paintFilRect_FrameBuff,
84 86 .getPix = &D51E5TA7601getPix_FrameBuff,
85 87 .refreshenable = &D51E5TA7601refreshenable_FrameBuff,
86 88 .width= 480,
87 89 .height = 320,
88 90 .isFrameBuffer = 1
89 91 };
90 92
91 93 LCD_t __attribute__ ((aligned (4))) FRAME_BUFFER1=
92 94 {
93 95 .interface.buffer=(((int16_t*)0x64000000)+(320*480)),
94 96 .init = &D51E5TA7601init_FrameBuff,
95 97 .paint = &D51E5TA7601paint_FrameBuff,
96 98 .setFrame = &D51E5TA7601setFrame_FrameBuff,
97 99 .paintText = &D51E5TA7601paintText_FrameBuff,
98 100 .paintFilRect = &D51E5TA7601paintFilRect_FrameBuff,
99 101 .getPix = &D51E5TA7601getPix_FrameBuff,
100 102 .refreshenable = &D51E5TA7601refreshenable_FrameBuff,
101 103 .width= 480,
102 104 .height = 320,
103 105 .isFrameBuffer = 1
104 106 };
105 107 LCD_t* FRAME_BUFFER=&FRAME_BUFFER0;
106 108
107 109 float VREF0 =(float)3.3;
108 110
111 // GPIO config list
112 gpioinitlist_str gpios[]={
113 {LED1,{gpiooutdir,gpiohighspeed,0,0}},
114 {LED2,{gpiooutdir,gpiohighspeed,0,0}},
115 {LED3,{gpiooutdir,gpiohighspeed,0,0}},
116 {PSU_DISABLE,{gpiooutdir,gpiohighspeed,gpiopushpulltype,0}},
117 {PSU_ALERT_5V,{gpioindir,gpiohighspeed,0,0}},
118 {PSU_ALERT_1_5V,{gpioindir,gpiohighspeed,0,0}},
119 {PSU_ALERT_3_3V,{gpioindir,gpiohighspeed,0,0}},
120 {BP1,{gpioindir,gpiohighspeed,0,0}},
121 {BP2,{gpioindir,gpiohighspeed,0,0}},
122 {BP3,{gpioindir,gpiohighspeed,0,0}},
123 {BP4,{gpioindir,gpiohighspeed,0,0}},
124 {TC_IRQ,{gpioindir,gpiohighspeed,0,gpiopulluptype}}
125 };
126
109 127 int bsp_init()
110 128 {
111 129 int i=0;
112 130 for(i=0;i<32;i++)
113 131 {
114 132 __opnfiles__[i] = NULL;
115 133 }
116 134 bsp_GPIO_init();
117 135 // bsp_uart_init();
118 136 bsp_iic_init();
119 137 bsp_spi_init();
120 138 bsp_TC_init();
121 139 bsp_FSMC_init();
122 140 bsp_GTerm_init();
123 141 printf("\r=====================\n\r");
124 142 printf( "=====================\n\r");
125 143 printf(BSP);
126 144 printf(" initialised\n\r");
127 145 printf( "=====================\n\r");
146 char uuid[12];
147 getCpuUUID(uuid);
148 printf("CPU UUID =");
149 for(int i=0;i<12;i++)
150 {
151 printf( "%02x",uuid[i]);
152 }
153 printf( "\n\r=====================\n\r");
128 154 return 1;
129 155 }
130 156
131 157 void bsp_GPIO_init()
132 158 {
133 gpio_t gpio1 = gpioopen(LED1);
134 gpio_t gpio2 = gpioopen(LED2);
135 gpio_t gpio3 = gpioopen(LED3);
136 gpio_t gpio4 = gpioopen(PSU_DISABLE);
137 gpio_t gpio5 = gpioopen(PSU_ALERT_5V);
138 gpio_t gpio6 = gpioopen(PSU_ALERT_1_5V);
139 gpio_t gpio7 = gpioopen(PSU_ALERT_3_3V);
140 gpio_t gpio8 = gpioopen(BP1);
141 gpio_t gpio9 = gpioopen(BP2);
142 gpio_t gpio10 = gpioopen(BP3);
143 gpio_t gpio11 = gpioopen(BP4);
144 gpio_t gpio12 = gpioopen(TC_IRQ);
145 gpiosetspeed(&gpio1,gpiohighspeed);
146 gpiosetspeed(&gpio2,gpiohighspeed);
147 gpiosetspeed(&gpio3,gpiohighspeed);
148 gpiosetspeed(&gpio4,gpiohighspeed);
149 gpiosetspeed(&gpio5,gpiohighspeed);
150 gpiosetspeed(&gpio6,gpiohighspeed);
151 gpiosetspeed(&gpio7,gpiohighspeed);
152 gpiosetspeed(&gpio8,gpiohighspeed);
153 gpiosetspeed(&gpio9,gpiohighspeed);
154 gpiosetspeed(&gpio10,gpiohighspeed);
155 gpiosetspeed(&gpio11,gpiohighspeed);
156 gpiosetspeed(&gpio12,gpiohighspeed);
157 gpioclr(PSU_DISABLE);
158 gpiosetdir(&gpio1,gpiooutdir);
159 gpiosetdir(&gpio3,gpiooutdir);
160 gpiosetdir(&gpio2,gpiooutdir);
161 gpiosetdir(&gpio4,gpiooutdir);
162 gpiosetdir(&gpio5,gpioindir);
163 gpiosetdir(&gpio6,gpioindir);
164 gpiosetdir(&gpio7,gpioindir);
165 gpiosetdir(&gpio8,gpioindir);
166 gpiosetdir(&gpio9,gpioindir);
167 gpiosetdir(&gpio10,gpioindir);
168 gpiosetdir(&gpio11,gpioindir);
169 gpiosetdir(&gpio12,gpioindir);
170 gpiosetpulltype(&gpio4,gpiopushpulltype);
171 gpiosetpulltype(&gpio12,gpiopulluptype);
159 bsp_gpio_init_list(gpios);
172 160 gpioclr(PSU_DISABLE);
173 161 pwmopen(PWM9_CH2,LCD_BACKLIGHT);
174 162 int error = pwmsetconfig(PWM9_CH2,100,80.0);
175 163 }
176 164
177 165 void bsp_uart_init()
178 166 {
179 167 if(__opnfiles__[1]==NULL)
180 168 {
181 169 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
182 170 uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
183 171 uartmkstreamdev(uart,fd1);
184 172 __opnfiles__[1] = fd1;
185 173 }
186 174 else
187 175 {
188 176 uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
189 177 }
190 178 }
191 179
192 180
193 181
194 182 int bsp_FSMC_init()
195 183 {
196 184 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
197 185 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
198 186
199 187 gpio_t gpio1 = gpioopen(LCD_RESET);
200 gpiosetspeed(&gpio1,gpiohighspeed);
201 gpiosetdir(&gpio1,gpiooutdir);
188 gpiosetspeed(gpio1,gpiohighspeed);
189 gpiosetdir(gpio1,gpiooutdir);
202 190 gpioclr(LCD_RESET);
203 191
204 192 gpio_t LCD_DBxList[]={
205 193 LCD_CS,
206 194 LCD_RS,
207 195 LCD_WR,
208 196 LCD_RD,
209 197 LCD_D0,
210 198 LCD_D1,
211 199 LCD_D2,
212 200 LCD_D3,
213 201 LCD_D4,
214 202 LCD_D5,
215 203 LCD_D6,
216 204 LCD_D7,
217 205 LCD_D8,
218 206 LCD_D9,
219 207 LCD_D10,
220 208 LCD_D11,
221 209 LCD_D12,
222 210 LCD_D13,
223 211 LCD_D14,
224 212 LCD_D15};
225 213
226 214 for(int i=0;i<20;i++)
227 215 {
228 216 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
229 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
230 gpiosetconfig(&LCD_DBx);
217 gpiosetconfig(LCD_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
231 218 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
232 219 }
233 220
234 221 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
235 222 FSMC_NORSRAMTimingInitTypeDef p,readtim;
236 223
237 224 /* Enable FSMC clock */
238 225 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
239 226
240 227 /*-- FSMC Configuration ------------------------------------------------------*/
241 228 /*----------------------- SRAM Bank 3 ----------------------------------------*/
242 229 /* FSMC_Bank1_NORSRAM3 configuration */
243 230 p.FSMC_AddressSetupTime = 0xf;
244 231 p.FSMC_AddressHoldTime = 0xf;
245 232 //ili9328 -> data setup time > 10ns
246 233 p.FSMC_DataSetupTime = 0xf;
247 234 if(getCpuFreq()>100*1000*1000)
248 235 p.FSMC_DataSetupTime = 0xf;// 11;
249 236 p.FSMC_BusTurnAroundDuration = 0xf;
250 237 p.FSMC_CLKDivision = 0;
251 238 p.FSMC_DataLatency = 0xf;
252 239 //ili9328 -> data hold time > 15ns
253 240 if(getCpuFreq()>66*1000*1000)
254 241 p.FSMC_DataLatency = 0xf;
255 242 p.FSMC_AccessMode = FSMC_AccessMode_A;
256 243
257 244 readtim.FSMC_AddressSetupTime = 0xf;
258 245 readtim.FSMC_AddressHoldTime = 0xf;
259 246 //p.FSMC_DataSetupTime = 9;
260 247 readtim.FSMC_DataSetupTime = 0xf ;// 11;
261 248 if(getCpuFreq()>100*1000*1000)
262 249 readtim.FSMC_DataSetupTime = 0xf;// 11;
263 250 readtim.FSMC_BusTurnAroundDuration = 0xf;
264 251 readtim.FSMC_CLKDivision = 2;
265 252 readtim.FSMC_DataLatency = 0xf;
266 253 if(getCpuFreq()>66*1000*1000)
267 254 readtim.FSMC_DataLatency = 0xf;
268 255 readtim.FSMC_AccessMode = FSMC_AccessMode_A;
269 256
270 257 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
271 258 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
272 259 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
273 260 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
274 261 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
275 262 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
276 263 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
277 264 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
278 265 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
279 266 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
280 267 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
281 268 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
282 269 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
283 270 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readtim;
284 271 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
285 272
286 273 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
287 274
288 275 /* Enable FSMC NOR/SRAM Bank1 */
289 276 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
290 277 gpio_t SRAM_DBxList[]={
291 278 SRAM_A0,
292 279 SRAM_A1,
293 280 SRAM_A2,
294 281 SRAM_A3,
295 282 SRAM_A4,
296 283 SRAM_A5,
297 284 SRAM_A6,
298 285 SRAM_A7,
299 286 SRAM_A8,
300 287 SRAM_A9,
301 288 SRAM_A10,
302 289 SRAM_A11,
303 290 SRAM_A12,
304 291 SRAM_A13,
305 292 SRAM_A14,
306 293 SRAM_A15,
307 294 SRAM_A16,
308 295 SRAM_A17,
309 296 SRAM_A18,
310 297 SRAM_A19,
311 298 SRAM_NBL0,
312 299 SRAM_NBL1,
313 300 SRAM_NCE
314 301 };
315 302
316 303 for(int i=0;i<23;i++)
317 304 {
318 305 gpio_t SRAM_DBx = gpioopen(SRAM_DBxList[i]);
319 SRAM_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
320 gpiosetconfig(&SRAM_DBx);
306 gpiosetconfig(SRAM_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
321 307 GPIO_PinAFConfig(GPIOGETPORT(SRAM_DBx), (uint8_t)(SRAM_DBx & 0xF), GPIO_AF_FSMC);
322 308 }
323 309 /*-- FSMC Configuration ------------------------------------------------------*/
324 310 // p.FSMC_AddressSetupTime = 3;
325 311 p.FSMC_AddressSetupTime = 1;
326 312 p.FSMC_AddressHoldTime = 1;
327 313 // p.FSMC_DataSetupTime = 6;
328 314 p.FSMC_DataSetupTime = 1;
329 315 p.FSMC_BusTurnAroundDuration = 1;
330 316 p.FSMC_CLKDivision = 0;
331 317 p.FSMC_DataLatency = 1;
332 318 p.FSMC_AccessMode = FSMC_AccessMode_A;
333 319
334 320 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
335 321 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
336 322 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_PSRAM;
337 323 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
338 324 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
339 325 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
340 326 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
341 327 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
342 328 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
343 329 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
344 330 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
345 331 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
346 332 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
347 333 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
348 334 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
349 335
350 336 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
351 337
352 338 /*!< Enable FSMC Bank1_SRAM2 Bank */
353 339 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);
354 340 gpioset(LCD_RESET);
355 341 delay_100us(500);
356 342 gpioclr(LCD_RESET);
357 343 delay_100us(500);
358 344 gpioset(LCD_RESET);
359 345 lcd0.init(&lcd0);
360 346 return 1;
361 347 }
362 348
363 349 void bsp_spi_init()
364 350 {
365 351
366 352 // ads7843init(&tc_dev,spi1,ADS7843_POWER_ADC_ON|ADS7843_MODE_12BITS,&TC_setnCS,&TC_busy);
367 spiopenandconfig(spi1,spimaster|spi8bits|spimsbfirst|spiclkfirstedge,10000000,TC_DIN,TC_DOUT,TC_CLK,-1);
368 353 }
369 354
370 355
371 356 void bsp_iic_init()
372 357 {
373 i2copenandconfig(i2c3,0,10000,PH8,PH7);
358 i2copenandconfig(i2c3,0,100000,PH8,PH7);
374 359 }
375 360
376 361 void bsp_SD_init()
377 362 {
378 363
379 364 }
380 365
381 366 void vs10XXclearXCS(){}
382 367 void vs10XXsetXCS(){}
383 368 int vs10XXDREQ()
384 369 {
385 370 return 1;
386 371 }
387 372
388 373
389 374 void bsppowersdcard(char onoff) //always ON
390 375 {
391 376
392 377 }
393 378
394 379 char bspsdcardpresent()
395 380 {
396 381 return 0;
397 382 }
398 383
399 384 char bspsdcardwriteprotected()
400 385 {
401 386 return 0;
402 387 }
403 388
404 389 void bspsdcardselect(char YESNO)
405 390 {
406 391
407 392 }
408 393
409 394
410 395 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
411 396 {
412 397 *lcd0_CMD=(uint16_t)reg;
413 398 *lcd0_DATA=(uint16_t)data;
414 399 }
415 400
416 401 uint32_t bsp_lcd0_read_reg(uint32_t reg)
417 402 {
418 403 uint16_t value;
419 404 *lcd0_CMD=(uint16_t)reg;
420 405 value = *lcd0_DATA;
421 406 value = *lcd0_DATA;
422 407 return value;
423 408 }
424 409
425 410 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
426 411 {
427 412 *lcd0_CMD=(uint16_t)D51E5TA7601_REGISTER_WRITEDATATOGRAM;
428 413 uint16_t* castedBuff=(uint16_t*)buffer;
429 414 for(int i=0;i<(int)count;i++)
430 415 {
431 416 *lcd0_DATA=castedBuff[i];
432 417 }
433 418 }
434 419
435 420 void bsp_lcd0_setGRAM()
436 421 {
437 422 *lcd0_CMD=(uint16_t)D51E5TA7601_REGISTER_WRITEDATATOGRAM;
438 423 }
439 424
440 425 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
441 426 {
442 427 uint16_t* castedBuff=(uint16_t*)buffer;
443 428 *lcd0_CMD=(uint16_t)D51E5TA7601_REGISTER_WRITEDATATOGRAM;
444 429 castedBuff[0]=lcd0_DATA[0];
445 430 for(int i=0;i<(int)count;i++)
446 431 {
447 432 castedBuff[i]=lcd0_DATA[0];
448 433 }
449 434 }
450 435
451 436 uint32_t bsp_lcd0_status()
452 437 {
453 438 uint16_t status = lcd0_CMD[0];
454 439 return (uint32_t)status;
455 440 }
456 441
457 442 void TC_setnCS(char val)
458 443 {
459 444 return gpiosetval(TC_BUSY,val);
460 445 }
461 446
462 447 int TC_busy()
463 448 {
464 449 return gpiogetval(TC_BUSY);
465 450 }
466 451
467 452 void bsp_TC_init()
468 453 {
469 454 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
470 455 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
471 456
472 457 gpio_t gpio1 = gpioopen(TC_BUSY);
473 gpio1 |= gpiohighspeed | gpioindir | gpionopulltype;
474 gpiosetconfig(&gpio1);
458 gpiosetconfig(gpio1,gpioindir,gpiohighspeed, 0, 0);
475 459
476 460 gpio1 = gpioopen(TC_IRQ);
477 gpio1 |= gpiohighspeed | gpioindir | gpiopulluptype;
478 gpiosetconfig(&gpio1);
461 gpiosetconfig(gpio1,gpioindir,gpiohighspeed, 0, 0);
479 462
480 463 gpio1 = gpioopen(TC_CS);
481 gpio1 |= gpiohighspeed | gpiooutdir | gpionopulltype;
482 gpiosetconfig(&gpio1);
483
484 gpio1 = gpioopen(TC_CLK);
485 gpio1 |= gpiohighspeed | gpioaf | gpionopulltype;
486 gpiosetconfig(&gpio1);
487 GPIO_PinAFConfig(GPIOGETPORT(TC_CLK), (uint8_t)(TC_CLK & 0xF), GPIO_AF_SPI1);
464 gpiosetconfig(gpio1, gpiooutdir, gpiohighspeed, 0, 0);
488 465
489 gpio1 = gpioopen(TC_DIN);
490 gpio1 |= gpiohighspeed | gpioaf | gpionopulltype;
491 gpiosetconfig(&gpio1);
492 GPIO_PinAFConfig(GPIOGETPORT(gpio1), (uint8_t)(gpio1 & 0xF), GPIO_AF_SPI1);
493
494 gpio1 = gpioopen(TC_DOUT);
495 gpio1 |= gpiohighspeed | gpioaf | gpionopulltype;
496 gpiosetconfig(&gpio1);
497 GPIO_PinAFConfig(GPIOGETPORT(gpio1), (uint8_t)(gpio1 & 0xF), GPIO_AF_SPI1);
466 spiopenandconfig(spi1,spimaster|spi8bits|spimsbfirst|spiclkfirstedge,10000000,TC_DIN,TC_DOUT,TC_CLK,-1);
498 467 }
499 468
500 469
501 470 void bsp_GTerm_init()
502 471 {
503 472 FRAME_BUFFER->init(FRAME_BUFFER);
504 473 if(__opnfiles__[1]==NULL)
505 474 {
506 475 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
507 476 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
508 477 terminal_init(&terminal0 ,FRAME_BUFFER,&ComicSansMS_8,fd1);
509 478 __opnfiles__[1] = fd1;
510 479 }
511 480 else
512 481 {
513 482
514 483 }
515 484 }
516 485
@@ -1,132 +1,150
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@member.fsf.org
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 "../../common/bsp_gpio.h"
28 29 uint32_t OSC0 =8000000;
29 30 uint32_t INTOSC =16000000;
30 31 uint32_t RTCOSC =32768;
31 32 uint32_t currentCpuFreq=0;
32 33 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
33 34 gpio_t TickLed = LED1;
34 35
35 36 float VREF0 =(float)3.3;
37 gpioinitlist_str gpios[]={
38 {LED1,{gpiooutdir,gpiohighspeed,0,0}},
39 {LED2,{gpiooutdir,gpiohighspeed,0,0}},
40 {BP0,{gpioindir,gpiohighspeed,0,0}}};
36 41
37 42 int bsp_init()
38 43 {
39 44 int i=0;
40 45 for(i=0;i<32;i++)
41 46 {
42 47 __opnfiles__[i] = NULL;
43 48 }
44 49 bsp_GPIO_init();
45 50 bsp_uart_init();
46 51 printf("\r================================================================\n\r");
47 52 printf("================================================================\n\r");
48 53 printf(BSP);
49 54 printf(" initialised\n\r");
50 55 printf("================================================================\n\r");
51 56 return 1;
52 57 }
53 58
54 59 void bsp_GPIO_init()
55 60 {
56 gpio_t gpio1 = gpioopen(LED1);
57 gpio_t gpio2 = gpioopen(LED2);
58 gpio_t gpio3 = gpioopen(BP0);
59 gpiosetspeed(&gpio1,gpiohighspeed);
60 gpiosetspeed(&gpio2,gpiohighspeed);
61 gpiosetspeed(&gpio3,gpiohighspeed);
62 gpiosetdir(&gpio1,gpiooutdir);
63 gpiosetdir(&gpio2,gpiooutdir);
64 gpiosetdir(&gpio3,gpioindir);
61 bsp_gpio_init_list(gpios);
65 62 }
66 63
67 64 void bsp_uart_init()
68 65 {
69 66 if(__opnfiles__[1]==NULL)
70 67 {
71 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
72 68 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
73 69 uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
74 70 uartmkstreamdev(uart,fd1);
75 71 __opnfiles__[1] = fd1; //stdo
76 72 __opnfiles__[0] = fd1; //stdi
77 73 }
78 74 else
79 75 {
80 76 uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
81 77 }
82 78 }
83 79
84 80 void bsp_spi_init()
85 81 {
86 82
87 83 }
88 84
89 85
90 86 void bsp_iic_init()
91 87 {
92 88
93 89 }
94 90
95 91 void bsp_SD_init()
96 92 {
97 93
98 94 }
99 95
96 /*
97 * IM0=0
98 * IM1=1
99 * IM2=1
100 * IM3=0
101 * 4-wire 8-bit data serial interface I
102 */
103 void bsp_LCD_init()
104 {
105
106 }
100 107
101 108 void vs10XXclearXCS(){}
102 109 void vs10XXsetXCS(){}
103 110 int vs10XXDREQ()
104 111 {
105 112 return 1;
106 113 }
107 114
108 115
109 116 void bsppowersdcard(char onoff) //always ON
110 117 {
111 118
112 119 }
113 120
114 121 char bspsdcardpresent()
115 122 {
116 123 return 0;
117 124 }
118 125
119 126 char bspsdcardwriteprotected()
120 127 {
121 128 return 0;
122 129 }
123 130
124 131 void bspsdcardselect(char YESNO)
125 132 {
126 133
127 134 }
128 135
129 136
130 137
138 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
139 {
140
141 }
142
143 uint32_t bsp_lcd0_read_reg(uint32_t reg)
144 {
145 uint16_t value;
146
147 return value;
148 }
131 149
132 150
@@ -1,80 +1,92
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@member.fsf.org
21 21 -------------------------------------------------------------------------------*/
22 22 #ifndef BSP_H
23 23 #define BSP_H
24 24 #include <stm32f4xx.h>
25 25 #include <gpio.h>
26 26
27 27 #define __MAX_OPENED_FILES__ 32
28 28 #define __FS_ROOT_SIZE__ 32
29 29
30 30 #define LED1 PG13
31 31 #define LED2 PG14
32 32
33 33 #define BP0 PA0
34 34
35 //LCD Signals
36 //#define LCD_RST PE2
37 #define LCD_DCX PD13
38 #define LCD_SCL PF7
39 #define LCD_SDA PF9
40 #define LCD_CSX PC2
41
42
35 43 extern float VREF0;
36 44
37 45 extern uint32_t OSC0;
38 46 extern uint32_t currentCpuFreq;
39 47
40 48
41 49
42 50 extern int bsp_init();
43 51
44 52 extern void bsp_GPIO_init();
45 53 extern void bsp_uart_init();
46 54 extern void bsp_iic_init();
47 55 extern void bsp_spi_init();
48 56 extern void bsp_SD_init();
57 extern void bsp_LCD_init();
49 58
50 59 /* VS1053 */
51 60 extern void clearXCS();
52 61 extern void setXCS();
53 62 extern int vs10XXDREQ();
54 63
55 64 /* SD CARD */
56 65 void bsppowersdcard(char onoff);
57 66 char bspsdcardpresent();
58 67 void bspsdcardselect(char YESNO);
59 68 char bspsdcardwriteprotected();
60 69
70 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data);
71 uint32_t bsp_lcd0_read_reg(uint32_t reg);
72
61 73 #endif
62 74
63 75
64 76
65 77
66 78
67 79
68 80
69 81
70 82
71 83
72 84
73 85
74 86
75 87
76 88
77 89
78 90
79 91
80 92
@@ -1,312 +1,311
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@member.fsf.org
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 <stm32f4xx_gpio.h>
29 29 #include <stm32f4xx_fsmc.h>
30 30 #include <stm32f4xx_gpio.h>
31 31 #include <stm32f4xx_rcc.h>
32 32 #include <core.h>
33 33
34 34 uint32_t OSC0 =8000000;
35 35 uint32_t INTOSC =16000000;
36 36 uint32_t RTCOSC =32768;
37 37 uint32_t currentCpuFreq=0;
38 38 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
39 39 gpio_t TickLed = LED1;
40 40
41 41 float VREF0 =(float)3.3;
42 42
43 43 LCD_IF_t lcdIF0={
44 44 .init = &bsp_FSMC_init,
45 45 .writereg = &bsp_lcd0_write_reg,
46 46 .readreg = &bsp_lcd0_read_reg,
47 47 .writeGRAM = &bsp_lcd0_writeGRAM,
48 48 .readGRAM = &bsp_lcd0_readGRAM
49 49 };
50 50
51 51 LCD_t lcd0={
52 52 .interface = &lcdIF0,
53 53 .init = &ssd2119init,
54 54 .paint = &ssd2119paint,
55 55 .paintText = &ssd2119paintText,
56 56 .paintFilRect = &ssd2119paintFilRect,
57 57 .refreshenable = &ssd2119refreshenable,
58 58 .width= 320,
59 59 .height = 240
60 60 };
61 61
62 62
63 63 volatile int16_t* lcd0_CMD=(volatile int16_t*) 0x60000000;
64 64 volatile int16_t* lcd0_DATA=(volatile int16_t*)(0x60FFFFF0);
65 65
66 66 int bsp_init()
67 67 {
68 68 int i=0;
69 69 for(i=0;i<32;i++)
70 70 {
71 71 __opnfiles__[i] = NULL;
72 72 }
73 73 bsp_GPIO_init();
74 74 bsp_uart_init();
75 75 bsp_FSMC_init();
76 76 printf("\r================================================================\n\r");
77 77 printf("================================================================\n\r");
78 78 printf(BSP);
79 79 printf(" initialised\n\r");
80 80 printf("================================================================\n\r");
81 81 return 1;
82 82 }
83 83
84 84 void bsp_GPIO_init()
85 85 {
86 86 gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009
87 87 // gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2);
88 88 gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2);
89 89 // gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2);
90 gpiosetspeed(&gpio1,gpiohighspeed);
90 gpiosetspeed(gpio1,gpiohighspeed);
91 91 //gpiosetspeed(&gpio2,gpiohighspeed);
92 gpiosetspeed(&gpio3,gpiohighspeed);
92 gpiosetspeed(gpio3,gpiohighspeed);
93 93 //gpiosetspeed(&gpio4,gpiohighspeed);
94 gpiosetdir(&gpio1,gpiooutdir);
95 gpiosetdir(&gpio3,gpiooutdir);
94 gpiosetdir(gpio1,gpiooutdir);
95 gpiosetdir(gpio3,gpiooutdir);
96 96 // gpiosetdir(&gpio2,gpiooutdir);
97 97 //gpiosetdir(&gpio4,gpiooutdir);
98 98 }
99 99
100 100 void bsp_uart_init()
101 101 {
102 102 if(__opnfiles__[1]==NULL)
103 103 {
104 104 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
105 105 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
106 106 uart_t uart = uartopenandconfig(uart6,uartparitynone | uart8bits | uartonestop,9600,PC6,PC7,-1,-1);
107 107 uartmkstreamdev(uart,fd1);
108 108 __opnfiles__[1] = fd1; //stdo
109 109 __opnfiles__[0] = fd1; //stdi
110 110 }
111 111 else
112 112 {
113 113 uartopenandconfig(uart6,uartparitynone | uart8bits | uartonestop,9600,PC6,PC7,-1,-1);
114 114 }
115 115 }
116 116
117 117
118 118 /*
119 119 D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7
120 120 D5 PE8 D6 PE9 D7 PE10 D8 PE11 D9 PE12
121 121 D10 PE13 D11 PE14 D12 PE15 D13 PD8 D14 PD9
122 122 D15 PD10
123 123 A0 PF0 = RS FSMC_NE3 PG10 CS FSMC_NWE PD5 W/S
124 124 FSMC_NOE PD4 RD
125 125 */
126 126 /*-- GPIOs Configuration -----------------------------------------------------*/
127 127 /*
128 128 +-------------------+--------------------+------------------+------------------+
129 129 + SRAM pins assignment +
130 130 +-------------------+--------------------+------------------+------------------+
131 131 | PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 |
132 132 | PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 |
133 133 | PD4 <-> FSMC_NOE | PE3 <-> FSMC_A19 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 |
134 134 | PD5 <-> FSMC_NWE | PE4 <-> FSMC_A20 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 |
135 135 | PD8 <-> FSMC_D13 | PE7 <-> FSMC_D4 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 |
136 136 | PD9 <-> FSMC_D14 | PE8 <-> FSMC_D5 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 |
137 137 | PD10 <-> FSMC_D15 | PE9 <-> FSMC_D6 | PF12 <-> FSMC_A6 | PG9 <-> FSMC_NE2 |
138 138 | PD11 <-> FSMC_A16 | PE10 <-> FSMC_D7 | PF13 <-> FSMC_A7 |------------------+
139 139 | PD12 <-> FSMC_A17 | PE11 <-> FSMC_D8 | PF14 <-> FSMC_A8 |
140 140 | PD13 <-> FSMC_A18 | PE12 <-> FSMC_D9 | PF15 <-> FSMC_A9 |
141 141 | PD14 <-> FSMC_D0 | PE13 <-> FSMC_D10 |------------------+
142 142 | PD15 <-> FSMC_D1 | PE14 <-> FSMC_D11 |
143 143 | | PE15 <-> FSMC_D12 |
144 144 +-------------------+--------------------+
145 145 */
146 146 int bsp_FSMC_init()
147 147 {
148 148 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
149 149 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
150 150
151 151 gpio_t gpio1 = gpioopen(LCD_RESET);
152 gpiosetspeed(&gpio1,gpiohighspeed);
153 gpiosetdir(&gpio1,gpiooutdir);
152 gpiosetspeed(gpio1,gpiohighspeed);
153 gpiosetdir(gpio1,gpiooutdir);
154 154 gpioclr(LCD_RESET);
155 155 gpio1 = gpioopen(LCD_CS);
156 gpiosetspeed(&gpio1,gpiohighspeed);
157 gpiosetdir(&gpio1,gpiooutdir);
156 gpiosetspeed(gpio1,gpiohighspeed);
157 gpiosetdir(gpio1,gpiooutdir);
158 158 gpioset(LCD_CS);
159 159 gpio_t LCD_DBxList[]={
160 160 PD0 ,PD1 ,PD4 ,PD5 ,PD8 ,PD9 ,PD10,PD14,PD15,
161 161 PE3 ,PE7 ,PE8 ,PE9 ,PE10,PE11,PE12,PE13,PE14,
162 162 PE15 };
163 163
164 164 for(int i=0;i<19;i++)
165 165 {
166 166 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
167 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
168 gpiosetconfig(&LCD_DBx);
167 gpiosetconfig(LCD_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
169 168 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
170 169 }
171 170
172 171 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
173 172 FSMC_NORSRAMTimingInitTypeDef p;
174 173
175 174 /* Enable FSMC clock */
176 175 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
177 176
178 177 /*-- FSMC Configuration ------------------------------------------------------*/
179 178 /*----------------------- SRAM Bank 3 ----------------------------------------*/
180 179 /* FSMC_Bank1_NORSRAM3 configuration */
181 180 p.FSMC_AddressSetupTime = 1;
182 181 p.FSMC_AddressHoldTime = 0;
183 182 p.FSMC_DataSetupTime = getCpuFreq()/14545450 ;// 11;
184 183 p.FSMC_BusTurnAroundDuration = 0;
185 184 p.FSMC_CLKDivision = 0;
186 185 p.FSMC_DataLatency = 0;
187 186 p.FSMC_AccessMode = FSMC_AccessMode_A;
188 187 /* Color LCD configuration ------------------------------------
189 188 LCD configured as follow:
190 189 - Data/Address MUX = Disable
191 190 - Memory Type = SRAM
192 191 - Data Width = 16bit
193 192 - Write Operation = Enable
194 193 - Extended Mode = Enable
195 194 - Asynchronous Wait = Disable */
196 195
197 196 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;
198 197 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
199 198 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
200 199 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
201 200 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
202 201 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
203 202 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
204 203 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
205 204 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
206 205 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
207 206 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
208 207 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
209 208 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
210 209 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
211 210 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
212 211
213 212 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
214 213
215 214 /* Enable FSMC NOR/SRAM Bank1 */
216 215 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
217 216 gpioset(LCD_RESET);
218 217 gpioclr(LCD_CS);
219 218 lcd0.init(&lcd0);
220 219 return 1;
221 220 }
222 221
223 222 void bsp_spi_init()
224 223 {
225 224
226 225 }
227 226
228 227
229 228 void bsp_iic_init()
230 229 {
231 230
232 231 }
233 232
234 233 void bsp_SD_init()
235 234 {
236 235
237 236 }
238 237
239 238
240 239 void vs10XXclearXCS(){}
241 240 void vs10XXsetXCS(){}
242 241 int vs10XXDREQ()
243 242 {
244 243 return 1;
245 244 }
246 245
247 246
248 247 void bsppowersdcard(char onoff) //always ON
249 248 {
250 249
251 250 }
252 251
253 252 char bspsdcardpresent()
254 253 {
255 254 return 0;
256 255 }
257 256
258 257 char bspsdcardwriteprotected()
259 258 {
260 259 return 0;
261 260 }
262 261
263 262 void bspsdcardselect(char YESNO)
264 263 {
265 264
266 265 }
267 266
268 267
269 268
270 269 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
271 270 {
272 271 gpioclr(LCD_CS);
273 272 *lcd0_CMD=(uint16_t)reg;
274 273 *lcd0_DATA=(uint16_t)data;
275 274 gpioset(LCD_CS);
276 275 }
277 276
278 277 uint32_t bsp_lcd0_read_reg(uint32_t reg)
279 278 {
280 279 gpioclr(LCD_CS);
281 280 *lcd0_CMD=(uint16_t)reg;
282 281 uint32_t data=(uint16_t)*lcd0_DATA;
283 282 gpioset(LCD_CS);
284 283 return data;
285 284 }
286 285
287 286 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
288 287 {
289 288 gpioclr(LCD_CS);
290 289 *lcd0_CMD=(uint16_t)ILI9328_REGISTER_WRITEDATATOGRAM;
291 290 uint16_t* castedBuff=(uint16_t*)buffer;
292 291 for(int i=0;i<(int)count;i++)
293 292 {
294 293 *lcd0_DATA=castedBuff[i];
295 294 }
296 295 gpioset(LCD_CS);
297 296 }
298 297
299 298 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
300 299 {
301 300 gpioclr(LCD_CS);
302 301 *lcd0_CMD=(uint16_t)ILI9328_REGISTER_WRITEDATATOGRAM;
303 302 uint16_t* castedBuff=(uint16_t*)buffer;
304 303 castedBuff[0]=*lcd0_DATA;
305 304 for(int i=0;i<(int)count;i++)
306 305 {
307 306 castedBuff[i]=*lcd0_DATA;
308 307 }
309 308 gpioset(LCD_CS);
310 309 }
311 310
312 311
@@ -1,379 +1,378
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@member.fsf.org
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 <ili9328.h>
29 29 #include <genericLCD_Controler.h>
30 30 #include <stm32f4xx.h>
31 31 #include <stm32f4xx_gpio.h>
32 32 #include <stm32f4xx_rcc.h>
33 33 #include <stm32f4xx_gpio.h>
34 34 #include <stm32f4xx_fsmc.h>
35 35 #include <terminal.h>
36 36 uint32_t OSC0 =8000000;
37 37 uint32_t INTOSC =16000000;
38 38 uint32_t RTCOSC =32768;
39 39 uint32_t currentCpuFreq=0;
40 40 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
41 41 gpio_t TickLed = LED1;
42 42
43 43 volatile uint8_t* lcd0_CMD=(volatile uint8_t*)0x60000000;
44 44 volatile uint8_t* lcd0_DATA=(volatile uint8_t*)0x61FFFFF0;
45 45 terminal_t terminal0;
46 46 volatile vs10XXDev audioCodec0;
47 47 sdcardDev sdcard2;
48 48 blkdevice sdcard2blkdev;
49 49 dikpartition sdcard2Part1;
50 50 FAT32fs sdcard2FAT32part1;
51 51 dikpartition sdcard2Part2;
52 52 FAT32fs sdcard2FAT32part2;
53 53 dikpartition sdcard2Part3;
54 54 FAT32fs sdcard2FAT32part3;
55 55 dikpartition sdcard2Part4;
56 56 FAT32fs sdcard2FAT32part4;
57 57
58 58 LCD_IF_t lcdIF0={
59 59 .init = &bsp_FSMC_init,
60 60 .writereg = &bsp_lcd0_write_reg,
61 61 .readreg = &bsp_lcd0_read_reg,
62 62 .writeGRAM = &bsp_lcd0_writeGRAM,
63 63 .readGRAM = &bsp_lcd0_readGRAM
64 64 };
65 65
66 66 LCD_t lcd0={
67 67 .interface = &lcdIF0,
68 68 .init = &ili9328init,
69 69 .paint = &ili9328paint,
70 70 .paintText = &ili9328paintText,
71 71 .paintFilRect = &ili9328paintFilRect,
72 72 .refreshenable = &ili9328refreshenable,
73 73 .width= 240,
74 74 .height = 320
75 75 };
76 76
77 77
78 78 float VREF0 =(float)3.3;
79 79
80 80 int bsp_init()
81 81 {
82 82 int i=0;
83 83 for(i=0;i<32;i++)
84 84 {
85 85 __opnfiles__[i] = NULL;
86 86 }
87 87 bsp_GPIO_init();
88 88 bsp_uart_init();
89 89 bsp_FSMC_init();
90 90 bsp_GTerm_init();
91 91 printf("\r================================================================\n\r");
92 92 printf("================================================================\n\r");
93 93 printf(BSP);
94 94 printf(" initialised\n\r");
95 95 printf("================================================================\n\r");
96 96 return 1;
97 97 }
98 98
99 99 void bsp_GPIO_init()
100 100 {
101 101 gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009
102 102 gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2);
103 103 gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2);
104 104 gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2);
105 gpiosetspeed(&gpio1,gpiohighspeed);
106 gpiosetspeed(&gpio2,gpiohighspeed);
107 gpiosetspeed(&gpio3,gpiohighspeed);
108 gpiosetspeed(&gpio4,gpiohighspeed);
109 gpiosetdir(&gpio1,gpiooutdir);
110 gpiosetdir(&gpio3,gpiooutdir);
111 gpiosetdir(&gpio2,gpiooutdir);
112 gpiosetdir(&gpio4,gpiooutdir);
105 gpiosetspeed(gpio1,gpiohighspeed);
106 gpiosetspeed(gpio2,gpiohighspeed);
107 gpiosetspeed(gpio3,gpiohighspeed);
108 gpiosetspeed(gpio4,gpiohighspeed);
109 gpiosetdir(gpio1,gpiooutdir);
110 gpiosetdir(gpio3,gpiooutdir);
111 gpiosetdir(gpio2,gpiooutdir);
112 gpiosetdir(gpio4,gpiooutdir);
113 113 }
114 114
115 115 void bsp_uart_init()
116 116 {
117 117 // if(__opnfiles__[1]==NULL)
118 118 // {
119 119 // //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
120 120 // streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
121 121 // uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,19200,PB10,PB11,-1,-1);
122 122 // uartmkstreamdev(uart,fd1);
123 123 // __opnfiles__[1] = fd1; //stdo
124 124 // __opnfiles__[0] = fd1; //stdi
125 125 // }
126 126 // else
127 127 // {
128 128 // uartopenandconfig(2,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
129 129 // }
130 130 }
131 131
132 132 void bsp_spi_init()
133 133 {
134 134
135 135 }
136 136
137 137
138 138 int bsp_FSMC_init()
139 139 {
140 140 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
141 141 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
142 142
143 143 gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\
144 144 ,PD4,PD5,PD7,PE4};
145 145 for(int i=0;i<12;i++)
146 146 {
147 147 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
148 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
149 gpiosetconfig(&LCD_DBx);
148 gpiosetconfig(LCD_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
150 149 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
151 150 }
152 151
153 152 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
154 153 FSMC_NORSRAMTimingInitTypeDef p,readtim;
155 154
156 155 /* Enable FSMC clock */
157 156 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
158 157
159 158 /*-- FSMC Configuration ------------------------------------------------------*/
160 159 /*----------------------- SRAM Bank 3 ----------------------------------------*/
161 160 /* FSMC_Bank1_NORSRAM4 configuration */
162 161 p.FSMC_AddressSetupTime = 3;
163 162 p.FSMC_AddressHoldTime = 3;
164 163 //ili9328 -> data setup time > 10ns
165 164 p.FSMC_DataSetupTime = 1;
166 165 if(getCpuFreq()>100*1000*1000)
167 166 p.FSMC_DataSetupTime = 2;// 11;
168 167 p.FSMC_BusTurnAroundDuration = 0;
169 168 p.FSMC_CLKDivision = 0;
170 169 p.FSMC_DataLatency = 0;
171 170 //ili9328 -> data hold time > 15ns
172 171 if(getCpuFreq()>66*1000*1000)
173 172 p.FSMC_DataLatency = 1;
174 173 p.FSMC_AccessMode = FSMC_AccessMode_A;
175 174
176 175 readtim.FSMC_AddressSetupTime = 0xF;
177 176 readtim.FSMC_AddressHoldTime = 0xF;
178 177 //p.FSMC_DataSetupTime = 9;
179 178 readtim.FSMC_DataSetupTime = 0xF ;// 11;
180 179 if(getCpuFreq()>100*1000*1000)
181 180 readtim.FSMC_DataSetupTime = 0xF;// 11;
182 181 readtim.FSMC_BusTurnAroundDuration = 0xf;
183 182 readtim.FSMC_CLKDivision = 0;
184 183 readtim.FSMC_DataLatency = 0xF;
185 184 if(getCpuFreq()>66*1000*1000)
186 185 readtim.FSMC_DataLatency = 0xF;
187 186 readtim.FSMC_AccessMode = FSMC_AccessMode_A;
188 187
189 188
190 189 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
191 190 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
192 191 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
193 192 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
194 193 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
195 194 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
196 195 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
197 196 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
198 197 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
199 198 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
200 199 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
201 200 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
202 201 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
203 202 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readtim;
204 203 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
205 204
206 205 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
207 206
208 207 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
209 208 gpioset(LCD_RESET);
210 209 gpioclr(LCD_RESET);
211 210 delay_100us(500);
212 211 gpioset(LCD_RESET);
213 212 delay_100us(500);
214 213 lcd0.init(&lcd0);
215 214 gpioset(LCD_BACKL);
216 215 return 1;
217 216 }
218 217
219 218
220 219 void bsp_iic_init()
221 220 {
222 221
223 222 }
224 223
225 224 void bsp_SD_init()
226 225 {
227 226
228 227 }
229 228
230 229
231 230 void vs10XXclearXCS(){}
232 231 void vs10XXsetXCS(){}
233 232 int vs10XXDREQ()
234 233 {
235 234 return 1;
236 235 }
237 236
238 237
239 238 void bsppowersdcard(char onoff) //always ON
240 239 {
241 240
242 241 }
243 242
244 243 char bspsdcardpresent()
245 244 {
246 245 return 0;
247 246 }
248 247
249 248 char bspsdcardwriteprotected()
250 249 {
251 250 return 0;
252 251 }
253 252
254 253 void bspsdcardselect(char YESNO)
255 254 {
256 255
257 256 }
258 257
259 258
260 259
261 260 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
262 261 {
263 262 uint8_t* pt8 = (uint8_t*)(void*)&reg;
264 263 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
265 264 *lcd0_CMD=pt8[3];
266 265 *lcd0_CMD=pt8[2];
267 266 pt8 = (uint8_t*)(void*)&data;
268 267 *lcd0_DATA=pt8[3];
269 268 *lcd0_DATA=pt8[2];
270 269 #else
271 270 *lcd0_CMD=pt8[1];
272 271 *lcd0_CMD=pt8[0];
273 272 pt8 = (uint8_t*)(void*)&data;
274 273 *lcd0_DATA=pt8[1];
275 274 *lcd0_DATA=pt8[0];
276 275 #endif
277 276
278 277 }
279 278
280 279 uint32_t bsp_lcd0_read_reg(uint32_t reg)
281 280 {
282 281 uint8_t* pt8 = (uint8_t*)(void*)&reg;
283 282 uint32_t DATA=0;
284 283 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
285 284 *lcd0_CMD=pt8[3];
286 285 *lcd0_CMD=pt8[2];
287 286 pt8 = (uint8_t*)(void*)&DATA;
288 287 pt8[3]=*lcd0_DATA;
289 288 pt8[2]=*lcd0_DATA;
290 289 #else
291 290
292 291 *lcd0_CMD=pt8[1];
293 292 *lcd0_CMD=pt8[0];
294 293 pt8 = (uint8_t*)(void*)&DATA;
295 294 pt8[1]=*lcd0_DATA;
296 295 pt8[0]=*lcd0_DATA;
297 296 #endif
298 297
299 298 return DATA;
300 299 }
301 300
302 301 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
303 302 {
304 303 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
305 304 uint8_t* pt8 = (uint8_t*)(void*)(&reg);
306 305 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
307 306 *lcd0_CMD=pt8[3];
308 307 *lcd0_CMD=pt8[2];
309 308 pt8 = (uint8_t*)(void*)buffer;
310 309 for(int i=0;i<(int)count;i++)
311 310 {
312 311 *lcd0_DATA=pt8[(2*i) +1];
313 312 *lcd0_DATA=pt8[2*i];
314 313 }
315 314 #else
316 315
317 316 *lcd0_CMD=pt8[1];
318 317 *lcd0_CMD=pt8[0];
319 318 pt8 = (uint8_t*)(void*)buffer;
320 319 for(int i=0;i<(int)count;i++)
321 320 {
322 321
323 322 *lcd0_DATA=pt8[(2*i) +1];
324 323 *lcd0_DATA=pt8[2*i];
325 324 }
326 325 #endif
327 326 }
328 327
329 328 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
330 329 {
331 330 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
332 331 uint8_t* pt8 = (uint8_t*)(void*)&reg;
333 332 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
334 333 *lcd0_CMD=pt8[3];
335 334 *lcd0_CMD=pt8[2];
336 335 pt8 = (uint8_t*)(void*)buffer;
337 336 for(int i=0;i<(int)count;i++)
338 337 {
339 338 pt8[(2*i) +1]=*lcd0_DATA;
340 339 pt8[2*i]=*lcd0_DATA;
341 340 }
342 341 #else
343 342 *lcd0_CMD=pt8[1];
344 343 *lcd0_CMD=pt8[0];
345 344 pt8 = (uint8_t*)(void*)buffer;
346 345 /*
347 346 * x dummy reads Cf ili9328 datasheet p79!
348 347 */
349 348 pt8[0]=*lcd0_DATA;
350 349 pt8[0]=*lcd0_DATA;
351 350
352 351 for(int i=0;i<(int)count;i++)
353 352 {
354 353 pt8[(2*i) +1]=*lcd0_DATA;
355 354 pt8[2*i]=*lcd0_DATA;
356 355 // pt8[(2*i) +1]=(uint8_t)0;
357 356 // pt8[(2*i)]=(uint8_t)0;
358 357 }
359 358 #endif
360 359 }
361 360
362 361 void bsp_GTerm_init()
363 362 {
364 363 if(__opnfiles__[1]==NULL)
365 364 {
366 365 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
367 366 terminal_init(&terminal0 ,&lcd0,&ComicSansMS_8,fd1);
368 367 __opnfiles__[1] = fd1;
369 368 }
370 369 else
371 370 {
372 371
373 372 }
374 373 }
375 374
376 375
377 376
378 377
379 378
@@ -1,135 +1,135
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@member.fsf.org
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 uint32_t OSC0 =8000000;
29 29 uint32_t INTOSC =16000000;
30 30 uint32_t RTCOSC =32768;
31 31 uint32_t currentCpuFreq=0;
32 32 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
33 33 gpio_t TickLed = LED1;
34 34
35 35 float VREF0 =(float)3.3;
36 36
37 37 int bsp_init()
38 38 {
39 39 int i=0;
40 40 for(i=0;i<32;i++)
41 41 {
42 42 __opnfiles__[i] = NULL;
43 43 }
44 44 bsp_GPIO_init();
45 45 bsp_uart_init();
46 46 printf("\r================================================================\n\r");
47 47 printf("================================================================\n\r");
48 48 printf(BSP);
49 49 printf(" initialised\n\r");
50 50 printf("================================================================\n\r");
51 51 return 1;
52 52 }
53 53
54 54 void bsp_GPIO_init()
55 55 {
56 56 gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009
57 57 gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2);
58 58 gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2);
59 59 gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2);
60 gpiosetspeed(&gpio1,gpiohighspeed);
61 gpiosetspeed(&gpio2,gpiohighspeed);
62 gpiosetspeed(&gpio3,gpiohighspeed);
63 gpiosetspeed(&gpio4,gpiohighspeed);
64 gpiosetdir(&gpio1,gpiooutdir);
65 gpiosetdir(&gpio3,gpiooutdir);
66 gpiosetdir(&gpio2,gpiooutdir);
67 gpiosetdir(&gpio4,gpiooutdir);
60 gpiosetspeed(gpio1,gpiohighspeed);
61 gpiosetspeed(gpio2,gpiohighspeed);
62 gpiosetspeed(gpio3,gpiohighspeed);
63 gpiosetspeed(gpio4,gpiohighspeed);
64 gpiosetdir(gpio1,gpiooutdir);
65 gpiosetdir(gpio3,gpiooutdir);
66 gpiosetdir(gpio2,gpiooutdir);
67 gpiosetdir(gpio4,gpiooutdir);
68 68 }
69 69
70 70 void bsp_uart_init()
71 71 {
72 72 if(__opnfiles__[1]==NULL)
73 73 {
74 74 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
75 75 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
76 76 uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,19200,PB10,PB11,-1,-1);
77 77 uartmkstreamdev(uart,fd1);
78 78 __opnfiles__[1] = fd1; //stdo
79 79 __opnfiles__[0] = fd1; //stdi
80 80 }
81 81 else
82 82 {
83 83 uartopenandconfig(2,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
84 84 }
85 85 }
86 86
87 87 void bsp_spi_init()
88 88 {
89 89
90 90 }
91 91
92 92
93 93 void bsp_iic_init()
94 94 {
95 95
96 96 }
97 97
98 98 void bsp_SD_init()
99 99 {
100 100
101 101 }
102 102
103 103
104 104 void vs10XXclearXCS(){}
105 105 void vs10XXsetXCS(){}
106 106 int vs10XXDREQ()
107 107 {
108 108 return 1;
109 109 }
110 110
111 111
112 112 void bsppowersdcard(char onoff) //always ON
113 113 {
114 114
115 115 }
116 116
117 117 char bspsdcardpresent()
118 118 {
119 119 return 0;
120 120 }
121 121
122 122 char bspsdcardwriteprotected()
123 123 {
124 124 return 0;
125 125 }
126 126
127 127 void bspsdcardselect(char YESNO)
128 128 {
129 129
130 130 }
131 131
132 132
133 133
134 134
135 135
@@ -1,151 +1,151
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@member.fsf.org
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 uint32_t OSC0 =8000000;
29 29 uint32_t INTOSC =16000000;
30 30 uint32_t RTCOSC =32768;
31 31 uint32_t currentCpuFreq=0;
32 32 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
33 33
34 34 gpio_t TickLed = LED1;
35 35
36 36 float VREF0 =(float)3.3;
37 37
38 38 LCD_IF_t lcdIF0={
39 39 .init = &bsp_FSMC_init,
40 40 .writereg = &bsp_lcd0_write_reg,
41 41 .readreg = &bsp_lcd0_read_reg,
42 42 .writeGRAM = &bsp_lcd0_writeGRAM,
43 43 .readGRAM = &bsp_lcd0_readGRAM
44 44 };
45 45
46 46 LCD_t lcd0={
47 47 .interface = &lcdIF0,
48 48 .init = &ili9328init,
49 49 .paint = &ili9328paint,
50 50 .paintText = &ili9328paintText,
51 51 .paintFilRect = &ili9328paintFilRect,
52 52 .getPix = &ili9328getPix,
53 53 .refreshenable = &ili9328refreshenable,
54 54 .width= 240,
55 55 .height = 320
56 56 };
57 57
58 58 int bsp_init()
59 59 {
60 60 int i=0;
61 61 for(i=0;i<32;i++)
62 62 {
63 63 __opnfiles__[i] = NULL;
64 64 }
65 65 bsp_GPIO_init();
66 66 bsp_uart_init();
67 67 bsp_FSMC_init();
68 68 printf("\r================================================================\n\r");
69 69 printf("================================================================\n\r");
70 70 printf(BSP);
71 71 printf(" initialised\n\r");
72 72 printf("================================================================\n\r");
73 73 return 1;
74 74 }
75 75
76 76 void bsp_GPIO_init()
77 77 {
78 78 gpio_t gpio1 = gpioopen(LED1);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009
79 79 gpio_t gpio2 = gpioopen(LED2);//gpioopen(LED2);
80 gpiosetspeed(&gpio1,gpiohighspeed);
81 gpiosetspeed(&gpio2,gpiohighspeed);
82 gpiosetdir(&gpio1,gpiooutdir);
83 gpiosetdir(&gpio2,gpiooutdir);
80 gpiosetspeed(gpio1,gpiohighspeed);
81 gpiosetspeed(gpio2,gpiohighspeed);
82 gpiosetdir(gpio1,gpiooutdir);
83 gpiosetdir(gpio2,gpiooutdir);
84 84 }
85 85
86 86 void bsp_uart_init()
87 87 {
88 88 if(__opnfiles__[1]==NULL)
89 89 {
90 90 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
91 91 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
92 92 uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,19200,PB10,PB11,-1,-1);
93 93 uartmkstreamdev(uart,fd1);
94 94 __opnfiles__[1] = fd1; //stdo
95 95 __opnfiles__[0] = fd1; //stdi
96 96 }
97 97 else
98 98 {
99 99 uartopenandconfig(2,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1);
100 100 }
101 101 }
102 102
103 103 void bsp_spi_init()
104 104 {
105 105
106 106 }
107 107
108 108
109 109 void bsp_iic_init()
110 110 {
111 111
112 112 }
113 113
114 114 void bsp_SD_init()
115 115 {
116 116
117 117 }
118 118
119 119
120 120 void vs10XXclearXCS(){}
121 121 void vs10XXsetXCS(){}
122 122 int vs10XXDREQ()
123 123 {
124 124 return 1;
125 125 }
126 126
127 127
128 128 void bsppowersdcard(char onoff) //always ON
129 129 {
130 130
131 131 }
132 132
133 133 char bspsdcardpresent()
134 134 {
135 135 return 0;
136 136 }
137 137
138 138 char bspsdcardwriteprotected()
139 139 {
140 140 return 0;
141 141 }
142 142
143 143 void bspsdcardselect(char YESNO)
144 144 {
145 145
146 146 }
147 147
148 148
149 149
150 150
151 151
@@ -1,267 +1,266
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2013, 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 #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 <stm32f4xx_gpio.h>
29 29 #include <stm32f4xx_fsmc.h>
30 30 #include <stm32f4xx_rcc.h>
31 31 #include <i2c.h>
32 32 #include <core.h>
33 33 #include <terminal.h>
34 34
35 35
36 36 #define clr_RS gpioclr(LCD_RS)
37 37 #define set_RS gpioset(LCD_RS)
38 38
39 39 #define clr_CS gpioclr(LCD_CS)
40 40 #define set_CS gpioset(LCD_CS)
41 41
42 42 volatile int8_t* lcd0_CMD=(volatile int8_t*)0x60000000;
43 43 volatile int16_t* lcd0_CMD16=(volatile int16_t*)0x60000000;
44 44 volatile int8_t* lcd0_DATA=(volatile int8_t*)0x61FFFFF0;
45 45 volatile int16_t* lcd0_DATA16=(volatile int16_t*)0x61FFFFF0;
46 46
47 47 /*
48 48 D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7
49 49 D5 PE8 D6 PE9 D7 PE10
50 50 A20 PE4 = RS FSMC_NE1 PD7 CS FSMC_NWE PD5 W/S
51 51 FSMC_NOE PD4 RD
52 52 */
53 53
54 54 int bsp_FSMC_init()
55 55 {
56 56
57 57 gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\
58 58 ,PD4,PD5};
59 59 // gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\
60 60 // ,PD4,PD5,PD7,PE4};
61 61 for(int i=0;i<10;i++)
62 62 {
63 63 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
64 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
65 gpiosetconfig(&LCD_DBx);
64 gpiosetconfig(LCD_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
66 65 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
67 66 }
68 67
69 68 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
70 69 FSMC_NORSRAMTimingInitTypeDef p;
71 70
72 71 /* Enable FSMC clock */
73 72 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
74 73
75 74 /*-- FSMC Configuration ------------------------------------------------------*/
76 75 /*----------------------- SRAM Bank 3 ----------------------------------------*/
77 76 /* FSMC_Bank1_NORSRAM4 configuration */
78 77 p.FSMC_AddressSetupTime = 1;//3
79 78 p.FSMC_AddressHoldTime = 1;//3
80 79 //ili9328 -> data setup time > 10ns
81 80 p.FSMC_DataSetupTime = 6;
82 81 p.FSMC_CLKDivision = 3;
83 82 if(getCpuFreq()>=100*1000*1000)
84 83 {
85 84 p.FSMC_CLKDivision = 3;
86 85 p.FSMC_DataSetupTime = 6;// 11;
87 86 }
88 87 p.FSMC_BusTurnAroundDuration = 0;
89 88 p.FSMC_DataLatency = 3;
90 89 //ili9328 -> data hold time > 15ns
91 90 if(getCpuFreq()>66*1000*1000)
92 91 p.FSMC_DataLatency = 3;
93 92 p.FSMC_AccessMode = FSMC_AccessMode_D;
94 93
95 94
96 95 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
97 96 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
98 97 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
99 98 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
100 99 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
101 100 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
102 101 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
103 102 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
104 103 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
105 104 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
106 105 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
107 106 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; //Dis
108 107 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
109 108 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
110 109 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
111 110
112 111 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
113 112
114 113 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
115 114 gpioset(LCD_RESET);
116 115 gpioclr(LCD_RESET);
117 116 delay_100us(500);
118 117 gpioset(LCD_RESET);
119 118 delay_100us(500);
120 119 lcd0.init(&lcd0);
121 120 gpioset(LCD_BACKL);
122 121 return 1;
123 122 }
124 123
125 124
126 125
127 126 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
128 127 {
129 128 uint8_t* pt8 = (uint8_t*)(void*)(&reg);
130 129 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
131 130 *lcd0_CMD=pt8[3];
132 131 *lcd0_CMD=pt8[2];
133 132 pt8 = (uint8_t*)(void*)&data;
134 133 *lcd0_DATA=pt8[3];
135 134 *lcd0_DATA=pt8[2];
136 135 #else
137 136 clr_CS;
138 137 clr_RS;
139 138 *lcd0_CMD=pt8[1];
140 139 *lcd0_CMD=pt8[0];
141 140 set_RS;
142 141 pt8 = (uint8_t*)(void*)&data;
143 142 *lcd0_DATA=pt8[1];
144 143 *lcd0_DATA=pt8[0];
145 144 set_CS;
146 145 #endif
147 146
148 147 }
149 148
150 149 uint32_t bsp_lcd0_read_reg(uint32_t reg)
151 150 {
152 151 uint8_t* pt8 = (uint8_t*)(void*)(&reg);
153 152 uint32_t DATA=0;
154 153
155 154 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
156 155 *lcd0_CMD=pt8[3];
157 156 *lcd0_CMD=pt8[2];
158 157 pt8 = (uint8_t*)(void*)&DATA;
159 158 pt8[3]=*lcd0_DATA;
160 159 pt8[2]=*lcd0_DATA;
161 160 #else
162 161 clr_CS;
163 162 clr_RS;
164 163 *lcd0_CMD=pt8[1];
165 164 *lcd0_CMD=pt8[0];
166 165 set_RS;
167 166 pt8 = (uint8_t*)(void*)&DATA;
168 167 pt8[1]=*lcd0_DATA;
169 168 pt8[0]=*lcd0_DATA;
170 169 set_CS;
171 170 #endif
172 171
173 172 return DATA;
174 173 }
175 174
176 175 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
177 176 {
178 177 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
179 178 uint8_t* pt8 = (uint8_t*)(void*)(&reg);
180 179 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
181 180 *lcd0_CMD=pt8[3];
182 181 *lcd0_CMD=pt8[2];
183 182 pt8 = (uint8_t*)(void*)buffer;
184 183 for(int i=0;i<(int)count;i++)
185 184 {
186 185 *lcd0_DATA=pt8[(2*i) +1];
187 186 *lcd0_DATA=pt8[2*i];
188 187 }
189 188 #else
190 189 clr_CS;
191 190 clr_RS;
192 191 *lcd0_CMD=pt8[1];
193 192 *lcd0_CMD=pt8[0];
194 193 set_RS;
195 194 pt8 = (uint8_t*)(void*)buffer;
196 195 for(int i=0;i<(int)count;i++)
197 196 {
198 197
199 198 *lcd0_DATA=pt8[(2*i) +1];
200 199 *lcd0_DATA=pt8[2*i];
201 200 }
202 201 set_CS;
203 202 #endif
204 203 }
205 204
206 205
207 206 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
208 207 {
209 208 //uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
210 209 volatile uint8_t* pt8;// = (uint8_t*)(void*)&reg;
211 210 /* #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
212 211 *lcd0_CMD=pt8[3];
213 212 *lcd0_CMD=pt8[2];
214 213 pt8 = (uint8_t*)(void*)buffer;
215 214 for(int i=0;i<(int)count;i++)
216 215 {
217 216 pt8[(2*i) +1]=*lcd0_DATA;
218 217 pt8[2*i]=*lcd0_DATA;
219 218 }
220 219 #else
221 220 *lcd0_CMD=(uint8_t)0;
222 221 *lcd0_CMD=(uint8_t)0;
223 222 *lcd0_CMD=(uint8_t)0;
224 223 *lcd0_CMD=(uint8_t)0;
225 224 *lcd0_CMD=pt8[1];
226 225 *lcd0_CMD=pt8[0];
227 226 pt8 = (uint8_t*)buffer;*/
228 227 /*
229 228 * x dummy reads Cf ili9328 datasheet p79!
230 229 */
231 230 /* pt8[0]=*lcd0_DATA;
232 231 pt8[1]=*lcd0_DATA;
233 232
234 233 for(int i=0;i<(int)count;i++)
235 234 {
236 235 pt8[(2*i) +1]=*lcd0_DATA;
237 236 pt8[2*i]=*lcd0_DATA;
238 237 pt8[(2*i) +1]=*lcd0_DATA;
239 238 pt8[2*i]=*lcd0_DATA;
240 239 }
241 240 #endif*/
242 241 //clr_CS;
243 242 //clr_RS;
244 243 //*lcd0_CMD=(int8_t)0;
245 244 //*lcd0_CMD=(int8_t)0x22;
246 245 // *lcd0_CMD=(int8_t)0x00;
247 246 //set_RS;
248 247 pt8 = (uint8_t*)buffer;
249 248 //pt8[1]=*lcd0_DATA;
250 249 //pt8[0]=*lcd0_DATA;
251 250 pt8[0]=0xFF;
252 251 pt8[1]=0xFF;
253 252 /* for(int i=0;i<(int)count;i++)
254 253 {
255 254 pt8[(2*i)+1]= *lcd0_DATA;
256 255 pt8[2*i]= *lcd0_DATA;
257 256 }*/
258 257 set_CS;
259 258 }
260 259
261 260
262 261
263 262
264 263
265 264
266 265
267 266
@@ -1,373 +1,370
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@member.fsf.org
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 <stm32f4xx_gpio.h>
29 29 #include <stm32f4xx_fsmc.h>
30 30 #include <i2c.h>
31 31 #include <core.h>
32 32 #include <terminal.h>
33 33 uint32_t OSC0 =8000000;
34 34 uint32_t INTOSC =16000000;
35 35 uint32_t RTCOSC =32768;
36 36 uint32_t currentCpuFreq=0;
37 37 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
38 38 gpio_t TickLed = LED1;
39 39
40 40 LCD_IF_t lcdIF0={
41 41 .init = &bsp_FSMC_init,
42 42 .writereg = &bsp_lcd0_write_reg,
43 43 .readreg = &bsp_lcd0_read_reg,
44 44 .writeGRAM = &bsp_lcd0_writeGRAM,
45 45 .readGRAM = &bsp_lcd0_readGRAM
46 46 };
47 47
48 48 LCD_t lcd0={
49 49 .interface = &lcdIF0,
50 50 .init = &ili9328init,
51 51 .paint = &ili9328paint,
52 52 .paintText = &ili9328paintText,
53 53 .paintFilRect = &ili9328paintFilRect,
54 54 .getPix = &ili9328getPix,
55 55 .refreshenable = &ili9328refreshenable,
56 56 .width= 240,
57 57 .height = 320
58 58 };
59 59
60 60 terminal_t terminal0;
61 61
62 62
63 63 volatile int16_t* lcd0_CMD=(volatile int16_t*) (0x60000000 | 0x08000000);
64 64 volatile int16_t* lcd0_DATA=((volatile int16_t*)(0x60000000 | 0x08000002));
65 65
66 66 float VREF0 =(float)3.3;
67 67
68 68 int bsp_init()
69 69 {
70 70 int i=0;
71 71 for(i=0;i<32;i++)
72 72 {
73 73 __opnfiles__[i] = NULL;
74 74 }
75 75 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF |
76 76 RCC_AHB1Periph_GPIOG, ENABLE);
77 77 bsp_GPIO_init();
78 78 bsp_uart_init();
79 79 bsp_iic_init();
80 80 bsp_FSMC_init();
81 81 bsp_GTerm_init();
82 82 printf("\r=====================\n\r");
83 83 printf( "=====================\n\r");
84 84 printf(BSP);
85 85 printf(" initialised\n\r");
86 86 printf( "=====================\n\r");
87 87 return 1;
88 88 }
89 89
90 90 void bsp_GPIO_init()
91 91 {
92 92 gpio_t gpio1 = gpioopen(LED1);
93 93 gpio_t gpio2 = gpioopen(LED2);
94 94 gpio_t gpio3 = gpioopen(LED3);
95 gpiosetspeed(&gpio1,gpiohighspeed);
96 gpiosetspeed(&gpio2,gpiohighspeed);
97 gpiosetspeed(&gpio3,gpiohighspeed);
98 gpiosetdir(&gpio1,gpiooutdir);
99 gpiosetdir(&gpio2,gpiooutdir);
100 gpiosetdir(&gpio3,gpiooutdir);
95 gpiosetspeed(gpio1,gpiohighspeed);
96 gpiosetspeed(gpio2,gpiohighspeed);
97 gpiosetspeed(gpio3,gpiohighspeed);
98 gpiosetdir(gpio1,gpiooutdir);
99 gpiosetdir(gpio2,gpiooutdir);
100 gpiosetdir(gpio3,gpiooutdir);
101 101 }
102 102
103 103 void bsp_uart_init()
104 104 {
105 105 //if(__opnfiles__[1]==NULL)
106 106 //{
107 107 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
108 108 // streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
109 109 // uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
110 110 //uartmkstreamdev(uart,fd1);
111 111 //__opnfiles__[1] = fd1;
112 112 //}
113 113 //else
114 114 //{
115 115 uartopenandconfig(0,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1);
116 116 //}
117 117 }
118 118
119 119 /*
120 120 D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7
121 121 D5 PE8 D6 PE9 D7 PE10 D8 PE11 D9 PE12
122 122 D10 PE13 D11 PE14 D12 PE15 D13 PD8 D14 PD9
123 123 D15 PD10
124 124 A0 PF0 = RS FSMC_NE3 PG10 CS FSMC_NWE PD5 W/S
125 125 FSMC_NOE PD4 RD
126 126 */
127 127 /*-- GPIOs Configuration -----------------------------------------------------*/
128 128 /*
129 129 +-------------------+--------------------+------------------+------------------+
130 130 + SRAM pins assignment +
131 131 +-------------------+--------------------+------------------+------------------+
132 132 | PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 |
133 133 | PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 |
134 134 | PD4 <-> FSMC_NOE | PE3 <-> FSMC_A19 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 |
135 135 | PD5 <-> FSMC_NWE | PE4 <-> FSMC_A20 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 |
136 136 | PD8 <-> FSMC_D13 | PE7 <-> FSMC_D4 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 |
137 137 | PD9 <-> FSMC_D14 | PE8 <-> FSMC_D5 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 |
138 138 | PD10 <-> FSMC_D15 | PE9 <-> FSMC_D6 | PF12 <-> FSMC_A6 | PG9 <-> FSMC_NE2 |
139 139 | PD11 <-> FSMC_A16 | PE10 <-> FSMC_D7 | PF13 <-> FSMC_A7 |------------------+
140 140 | PD12 <-> FSMC_A17 | PE11 <-> FSMC_D8 | PF14 <-> FSMC_A8 |
141 141 | PD13 <-> FSMC_A18 | PE12 <-> FSMC_D9 | PF15 <-> FSMC_A9 |
142 142 | PD14 <-> FSMC_D0 | PE13 <-> FSMC_D10 |------------------+
143 143 | PD15 <-> FSMC_D1 | PE14 <-> FSMC_D11 |
144 144 | | PE15 <-> FSMC_D12 |
145 145 +-------------------+--------------------+
146 146 */
147 147 int bsp_FSMC_init()
148 148 {
149 149 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
150 150 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
151 151
152 152 gpio_t LCD_DBxList[]={
153 153 PD0 ,PD1 ,PD4 ,PD5 ,PD8 ,PD9 ,PD10,PD11,PD12,PD13,PD14,PD15,
154 154 PE0 ,PE1 ,PE3 ,PE4 ,PE7 ,PE8 ,PE9 ,PE10,PE11,PE12,PE13,PE14,
155 155 PE15,PF0 ,PF1 ,PF2 ,PF3 ,PF4 ,PF5 ,PF12,PF13,PF14,PF15,PG0 ,
156 156 PG1 ,PG2 ,PG3 ,PG4 ,PG5 ,PG9 ,PG10
157 157 };
158 158
159 159 for(int i=0;i<43;i++)
160 160 {
161 161 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
162 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
163 gpiosetconfig(&LCD_DBx);
162 gpiosetconfig(LCD_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
164 163 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
165 164 }
166 165
167 166 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
168 167 FSMC_NORSRAMTimingInitTypeDef p,readtim;
169 168
170 169 /* Enable FSMC clock */
171 170 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
172 171
173 172 /*-- FSMC Configuration ------------------------------------------------------*/
174 173 /*----------------------- SRAM Bank 3 ----------------------------------------*/
175 174 /* FSMC_Bank1_NORSRAM3 configuration */
176 175 p.FSMC_AddressSetupTime = 0xf;
177 176 p.FSMC_AddressHoldTime = 0xf;
178 177 //ili9328 -> data setup time > 10ns
179 178 p.FSMC_DataSetupTime = 0xf;
180 179 if(getCpuFreq()>100*1000*1000)
181 180 p.FSMC_DataSetupTime = 0xf;// 11;
182 181 p.FSMC_BusTurnAroundDuration = 0xf;
183 182 p.FSMC_CLKDivision = 0;
184 183 p.FSMC_DataLatency = 0xf;
185 184 //ili9328 -> data hold time > 15ns
186 185 if(getCpuFreq()>66*1000*1000)
187 186 p.FSMC_DataLatency = 0xf;
188 187 p.FSMC_AccessMode = FSMC_AccessMode_A;
189 188
190 189 readtim.FSMC_AddressSetupTime = 0xf;
191 190 readtim.FSMC_AddressHoldTime = 0xf;
192 191 //p.FSMC_DataSetupTime = 9;
193 192 readtim.FSMC_DataSetupTime = 0xf ;// 11;
194 193 if(getCpuFreq()>100*1000*1000)
195 194 readtim.FSMC_DataSetupTime = 0xf;// 11;
196 195 readtim.FSMC_BusTurnAroundDuration = 0xf;
197 196 readtim.FSMC_CLKDivision = 0;
198 197 readtim.FSMC_DataLatency = 0xf;
199 198 if(getCpuFreq()>66*1000*1000)
200 199 readtim.FSMC_DataLatency = 0xf;
201 200 readtim.FSMC_AccessMode = FSMC_AccessMode_A;
202 201 /* Color LCD configuration ------------------------------------
203 202 LCD configured as follow:
204 203 - Data/Address MUX = Disable
205 204 - Memory Type = SRAM
206 205 - Data Width = 16bit
207 206 - Write Operation = Enable
208 207 - Extended Mode = Enable
209 208 - Asynchronous Wait = Disable */
210 209
211 210 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;
212 211 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
213 212 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
214 213 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
215 214 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
216 215 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
217 216 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
218 217 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
219 218 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
220 219 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
221 220 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
222 221 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
223 222 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
224 223 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readtim;
225 224 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
226 225
227 226 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
228 227
229 228 /* Enable FSMC NOR/SRAM Bank1 */
230 229 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
231 230
232 231 gpio_t SRAM_DBxList[]={
233 232 PF0,PF1,PF2,PF3,PF4,PF5,PF12,PF13,
234 233 PF14,PF15,
235 234 PD11,PD12,PD13,PG9,PE0,PE1};
236 235
237 236 for(int i=0;i<22;i++)
238 237 {
239 238 gpio_t SRAM_DBx = gpioopen(SRAM_DBxList[i]);
240 SRAM_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
241 gpiosetconfig(&SRAM_DBx);
239 gpiosetconfig(SRAM_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
242 240 GPIO_PinAFConfig(GPIOGETPORT(SRAM_DBx), (uint8_t)(SRAM_DBx & 0xF), GPIO_AF_FSMC);
243 241 }
244 242 /*-- FSMC Configuration ------------------------------------------------------*/
245 243 p.FSMC_AddressSetupTime = 3;
246 244 p.FSMC_AddressHoldTime = 0;
247 245 p.FSMC_DataSetupTime = 6;
248 246 p.FSMC_BusTurnAroundDuration = 1;
249 247 p.FSMC_CLKDivision = 0;
250 248 p.FSMC_DataLatency = 0;
251 249 p.FSMC_AccessMode = FSMC_AccessMode_A;
252 250
253 251 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
254 252 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
255 253 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_PSRAM;
256 254 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
257 255 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
258 256 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
259 257 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
260 258 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
261 259 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
262 260 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
263 261 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
264 262 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
265 263 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
266 264 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
267 265 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
268 266
269 267 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
270 268
271 269 /*!< Enable FSMC Bank1_SRAM2 Bank */
272 270 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);
273 271 lcd0.init(&lcd0);
274 272 return 1;
275 273 }
276 274
277 275 void bsp_spi_init()
278 276 {
279 277
280 278 }
281 279
282 280
283 281 void bsp_iic_init()
284 282 {
285 283 i2copenandconfig(i2c1,0,10000,PB9,PB6);
286 284 }
287 285
288 286 void bsp_SD_init()
289 287 {
290 288 gpio_t SDIO_DBxList[]={PC8 ,PC9 ,PC10 ,PC11 ,PC12,PD2};
291 289 for(int i=0;i<6;i++)
292 290 {
293 291 gpio_t SDIO_DBx = gpioopen(SDIO_DBxList[i]);
294 SDIO_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
295 gpiosetconfig(&SDIO_DBx);
292 gpiosetconfig(SDIO_DBx, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
296 293 GPIO_PinAFConfig(GPIOGETPORT(SDIO_DBx), (uint8_t)(SDIO_DBx & 0xF), GPIO_AF_SDIO);
297 294 }
298 295 }
299 296
300 297 void vs10XXclearXCS(){}
301 298 void vs10XXsetXCS(){}
302 299 int vs10XXDREQ()
303 300 {
304 301 return 1;
305 302 }
306 303
307 304
308 305 void bsppowersdcard(char onoff) //always ON
309 306 {
310 307
311 308 }
312 309
313 310 char bspsdcardpresent()
314 311 {
315 312 return 0;
316 313 }
317 314
318 315 char bspsdcardwriteprotected()
319 316 {
320 317 return 0;
321 318 }
322 319
323 320 void bspsdcardselect(char YESNO)
324 321 {
325 322
326 323 }
327 324
328 325
329 326 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
330 327 {
331 328 *lcd0_CMD=(uint16_t)reg;
332 329 *lcd0_DATA=(uint16_t)data;
333 330 }
334 331
335 332 uint32_t bsp_lcd0_read_reg(uint32_t reg)
336 333 {
337 334 *lcd0_CMD=(uint16_t)reg;
338 335 return (uint16_t)*lcd0_DATA;
339 336 }
340 337
341 338 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
342 339 {
343 340 *lcd0_CMD=(uint16_t)ILI9328_REGISTER_WRITEDATATOGRAM;
344 341 uint16_t* castedBuff=(uint16_t*)buffer;
345 342 for(int i=0;i<(int)count;i++)
346 343 {
347 344 *lcd0_DATA=castedBuff[i];
348 345 }
349 346 }
350 347
351 348 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
352 349 {
353 350 *lcd0_CMD=(uint16_t)ILI9328_REGISTER_WRITEDATATOGRAM;
354 351 uint16_t* castedBuff=(uint16_t*)buffer;
355 352 castedBuff[0]=*lcd0_DATA;
356 353 for(int i=0;i<(int)count;i++)
357 354 {
358 355 castedBuff[i]=*lcd0_DATA;
359 356 }
360 357 }
361 358
362 359 void bsp_GTerm_init()
363 360 {
364 361
365 362 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
366 363 ili9328paintFilRect(&lcd0,0,0,240,320,0x7FFF,5,0);
367 364 terminal_init(&terminal0,&lcd0,&ComicSansMS_8,fd1);
368 365 terminal_setgeometry(&terminal0,5,5,terminal0.LCD->width-10,(terminal0.LCD->height)-10);
369 366 __opnfiles__[1] = fd1;
370 367 }
371 368
372 369
373 370
@@ -1,45 +1,46
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2013, 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 alexis.jeandet@member.fsf.org
20 20 -------------------------------------------------------------------------------*/
21 21 /**
22 22 * This file is a simple hello world example it should run on any supported
23 23 * board/architecture couple. To change target board and architecture modify
24 24 * the project file. The value BSP contains the board name, and UCMODEL contain
25 25 * the target architecture.
26 26 */
27 #include <stdio.h>
27 28
28 29 int libuc_main()
29 30 {
30 31 /**
31 32 Depending on the board the printf can be directed on one uart or LCD screen or
32 33 any other stream device.
33 34 */
34 35 printf("hello world\n\r");
35 36 return 0;
36 37 }
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
@@ -1,20 +1,21
1 1 TEMPLATE = app
2 2
3 3 contains( UCMODEL , simulator ) {
4 4 BSP = SIMULATOR
5 5 }
6 6 contains( UCMODEL , stm32f4 ) {
7 7 #BSP = STM32F4Eval
8 BSP = SOSmartPSU
8 #BSP = SOSmartPSU
9 BSP = STM32F429Discovery
9 10 }
10 11
11 12 SOURCES += \
12 13 main.c
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
@@ -1,722 +1,737
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 alexis.jeandet@member.fsf.org
20 20 -------------------------------------------------------------------------------*/
21 21
22 22
23 23 /*! \file gpio.h
24 24 \brief GPIO api.
25 25
26 26 The gpio api gives you a standard way to drive any gpio on any processor.
27 27 With this api you will be able to open and configure your gpios and set/clear read them.
28 28
29 29 A simple example to drive PA0 and read PA1.
30 30 \code
31 31 // lets open PA0 and PA1
32 gpio_t mygpio1 gpioopen(PA0);
32 gpio_t mygpio1 = gpioopen(PA0);
33 33 gpioopen(PA1);
34 34 mygpio1 |= gpiohighspeed | gpiooutdir | gpiopushpulltype | gpionopulltype;
35 35 gpiosetconfig(mygpio1);
36 36 //you can also configure with the pin name
37 37 gpiosetconfig(PA1|gpiohighspeed | gpioindir);
38 38 //to set PA0
39 39 gpioset(mygpio1);
40 40 //to clear it
41 41 gpioclr(PA0); //PA0 also works and save 4 bytes!
42 42 //Let read PA1 and write it to PA0
43 43 gpiosetval(PA0,gpiogetval(PA1));
44 44 \endcode
45 45
46 46 \defgroup GPIO
47 47
48 48 \ingroup GPIO
49 49 */
50 50 #ifndef GPIO_H
51 51 #define GPIO_H
52 52 #include <stdint.h>
53 53 #ifdef __cplusplus
54 extern "C" {
54 extern "C" {
55 55 #endif
56 56
57 /**
57 /**
58 58 * @brief gpio handle
59 59 *
60 60 *gpio_t is the handle type you will use for all
61 61 *gpio related functions.
62 62 *
63 63 * \ingroup GPIO
64 64 */
65 65 typedef uint32_t gpio_t;
66 66
67 67
68 /**
68 /**
69 69 * @brief GPIO speed enum
70 70 *
71 71 * Use gpiospeed_t values to configure the speed of your GPIO pin with
72 * gpiosetspeed(gpio_t* gpio,gpiospeed_t speed) function or gpiosetconfig(gpio_t* gpio).
72 * gpiosetspeed(gpio_t gpio,gpiospeed_t speed) function or gpiosetconfig(gpio_t gpio).
73 73 * The speed values are realy platform dependant check for each target if you whant to
74 74 * know the meaning of each speed.
75 75 * Don't try to use numerical values directly!
76 * @sa gpiosetspeed(gpio_t* gpio,gpiospeed_t speed),gpiosetconfig(gpio_t* gpio)
76 * @sa gpiosetspeed(gpio_t gpio,gpiospeed_t speed),gpiosetconfig(gpio_t gpio)
77 77 *
78 78 * \ingroup GPIO
79 79 */
80 80 typedef enum gpiospeed_t
81 81 {
82 gpiolowspeed = 0x00000, /**< Lowest speed value*/
83 gpiomediumspeed = 0x10000, /**< Normal or medium speed value*/
84 gpiofastspeed = 0x20000, /**< Higher speed value*/
85 gpiohighspeed = 0x30000 /**< Highest speed value*/
82 gpiolowspeed = 0, /**< Lowest speed value*/
83 gpiomediumspeed, /**< Normal or medium speed value*/
84 gpiofastspeed , /**< Higher speed value*/
85 gpiohighspeed /**< Highest speed value*/
86 86 }gpiospeed_t;
87 87
88 88
89 89
90 /**
90 /**
91 91 * @brief GPIO direction enum
92 92 *
93 93 * Use gpiodir_t values to configure the direction of your GPIO pin with
94 * gpiosetdir(gpio_t* gpio,gpiodir_t dir) function or gpiosetconfig(gpio_t* gpio).
94 * gpiosetdir(gpio_t gpio,gpiodir_t dir) function or gpiosetconfig(gpio_t gpio).
95 95 * Don't try to use numerical values directly!
96 * @sa gpiosetdir(gpio_t* gpio,gpiodir_t dir),gpiosetconfig(gpio_t* gpio)
96 * @sa gpiosetdir(gpio_t gpio,gpiodir_t dir),gpiosetconfig(gpio_t gpio)
97 97 *
98 98 * \ingroup GPIO
99 99 */
100 100 typedef enum gpiodir_t
101 101 {
102 gpioindir = 0x00000, /**< Input direction*/
103 gpiooutdir = 0x40000, /**< Output direction*/
104 gpioaf = 0x80000, /**< Alternate function such as spi, uart, ...*/
105 gpioan = 0xC0000 /**< Configure the pin for analogic mode*/
102 gpioindir = 0,/**< Input direction*/
103 gpiooutdir, /**< Output direction*/
104 gpioaf, /**< Alternate function such as spi, uart, ...*/
105 gpioan /**< Configure the pin for analogic mode*/
106 106 }gpiodir_t;
107 107
108 108
109 109
110 /**
110 /**
111 111 * @brief GPIO output type enum
112 112 *
113 113 * Use gpioouttype_t values to configure the kind of output you want with
114 * gpiosetouttype(gpio_t* gpio, gpioouttype_t outtype) function or gpiosetconfig(gpio_t* gpio).
114 * gpiosetouttype(gpio_t gpio, gpioouttype_t outtype) function or gpiosetconfig(gpio_t gpio).
115 115 * Don't try to use numerical values directly!
116 * @sa gpiosetouttype(gpio_t* gpio, gpioouttype_t outtype),gpiosetconfig(gpio_t* gpio)
116 * @sa gpiosetouttype(gpio_t gpio, gpioouttype_t outtype),gpiosetconfig(gpio_t gpio)
117 117 *
118 118 * \ingroup GPIO
119 119 */
120 120 typedef enum gpioouttype_t
121 121 {
122 gpiopushpulltype = 0x000000, /**< Pushpull output*/
123 gpioopendraintype = 0x100000 /**< Open drain output*/
122 gpiopushpulltype = 0, /**< Pushpull output*/
123 gpioopendraintype /**< Open drain output*/
124 124 }gpioouttype_t;
125 125
126 126
127 127
128 /**
128 /**
129 129 * @brief GPIO pull resistor configuration enum
130 130 *
131 131 * Use gpiopulltype_t values to configure the pull resistor of your GPIO pin with
132 * gpiosetpulltype(gpio_t* gpio,gpiopulltype_t pulltype) function or gpiosetconfig(gpio_t* gpio).
132 * gpiosetpulltype(gpio_t gpio,gpiopulltype_t pulltype) function or gpiosetconfig(gpio_t gpio).
133 133 * Don't try to use numerical values directly!
134 * @sa gpiosetpulltype(gpio_t* gpio,gpiopulltype_t pulltype),gpiosetconfig(gpio_t* gpio)
134 * @sa gpiosetpulltype(gpio_t gpio,gpiopulltype_t pulltype),gpiosetconfig(gpio_t gpio)
135 135 *
136 136 * \ingroup GPIO
137 137 */
138 138 typedef enum gpiopulltype_t
139 139 {
140 gpionopulltype = 0x000000,
141 gpiopulluptype = 0x200000,
142 gpiopulldowntype = 0x400000
140 gpionopulltype = 0,
141 gpiopulluptype,
142 gpiopulldowntype
143 143 }gpiopulltype_t;
144 144
145 typedef struct
146 {
147 gpiodir_t direction;
148 gpiospeed_t speed;
149 gpioouttype_t outType;
150 gpiopulltype_t pullType;
151 }gpioconfig_str;
152
145 153 #ifndef DOXYGEN_SHOULD_SKIP_THIS
146 #define GPIOSPEEDMASK 0x30000
147 #define GPIODIRMASK 0xC0000
148 #define GPIOOUTTYPEMASK 0x100000
149 #define GPIOPULLTYPEMASK 0x600000
154 #define GPIOSPEEDMASK 0x30000
155 #define GPIODIRMASK 0xC0000
156 #define GPIOOUTTYPEMASK 0x100000
157 #define GPIOPULLTYPEMASK 0x600000
150 158 #endif
151 159
152 /**
160 /**
153 161 * @brief gpioopen
154 162 * @param gpio
155 163 * @return
156 164 * @sa gpioclose(gpio_t gpio)
157 * @ingroup GPIO
158 165 *
159 166 * \ingroup GPIO
160 167 */
161 168 extern gpio_t gpioopen(uint32_t gpio);
169 extern gpio_t gpioopenandconfig(uint32_t gpio, gpioconfig_str* config);
162 170 /**
163 171 * @brief gpioclose
164 172 * @param gpio
165 173 * @sa gpioopen(uint32_t gpio)
166 174 *
167 175 * \ingroup GPIO
168 176 */
169 177 extern void gpioclose(gpio_t gpio);
170 178 /**
171 179 * @brief gpiosetconfig
172 180 * @param gpio
181 * @param direction
182 * @param speed
183 * @param outType
184 * @param pullType
173 185 *
174 186 * \ingroup GPIO
175 187 */
176 extern void gpiosetconfig(gpio_t* gpio);
188 extern void gpiosetconfig(gpio_t gpio, gpiodir_t direction, gpiospeed_t speed, gpioouttype_t outType, gpiopulltype_t pullType);
189 extern void gpiosetconfigstr(gpio_t gpio, gpioconfig_str *config);
190
191
177 192 /**
178 193 * @brief gpiosetdir
179 194 * @param gpio
180 195 * @param dir
181 196 *
182 197 * \ingroup GPIO
183 198 */
184 extern void gpiosetdir(gpio_t* gpio,gpiodir_t dir);
199 extern void gpiosetdir(gpio_t gpio,gpiodir_t dir);
185 200 /**
186 201 * @brief gpiosetouttype
187 202 * @param gpio
188 203 * @param outtype
189 * @sa gpiosetconfig(gpio_t* gpio)
204 * @sa gpiosetconfig(gpio_t gpio)
190 205 *
191 206 * \ingroup GPIO
192 207 */
193 extern void gpiosetouttype(gpio_t* gpio, gpioouttype_t outtype);
208 extern void gpiosetouttype(gpio_t gpio, gpioouttype_t outtype);
194 209 /**
195 210 * @brief gpiosetpulltype
196 211 * @param gpio
197 212 * @param pulltype
198 * @sa gpiosetconfig(gpio_t* gpio)
213 * @sa gpiosetconfig(gpio_t gpio)
199 214 *
200 215 * \ingroup GPIO
201 216 */
202 extern void gpiosetpulltype(gpio_t* gpio,gpiopulltype_t pulltype);
217 extern void gpiosetpulltype(gpio_t gpio,gpiopulltype_t pulltype);
203 218 /**
204 219 * @brief gpiosetspeed
205 220 * @param gpio
206 221 * @param speed
207 * @sa gpiosetconfig(gpio_t* gpio)
222 * @sa gpiosetconfig(gpio_t gpio)
208 223 *
209 224 * \ingroup GPIO
210 225 */
211 extern void gpiosetspeed(gpio_t* gpio,gpiospeed_t speed);
226 extern void gpiosetspeed(gpio_t gpio,gpiospeed_t speed);
212 227 /**
213 228 * @brief gpioset
214 229 * @param gpio
215 * @sa gpiosetconfig(gpio_t* gpio)
230 * @sa gpiosetconfig(gpio_t gpio)
216 231 *
217 232 * \ingroup GPIO
218 233 */
219 234 extern void gpioset(gpio_t gpio);
220 235 /**
221 236 * @brief gpioclr
222 237 * @param gpio
223 238 *
224 239 * \ingroup GPIO
225 240 */
226 241 extern void gpioclr(gpio_t gpio);
227 242 /**
228 243 * @brief gpiosetval
229 244 * @param gpio
230 245 * @param val
231 246 *
232 247 * \ingroup GPIO
233 248 */
234 249 extern void gpiosetval(gpio_t gpio,int val);
235 250 /**
236 251 * @brief gpiogetval
237 252 * @param gpio
238 253 * @return
239 254 *
240 255 * \ingroup GPIO
241 256 */
242 257 extern int gpiogetval(gpio_t gpio);
243 258
244 259 #ifndef DOXYGEN_SHOULD_SKIP_THIS
245 260
246 #define GPIOISINPUT(gpio) (((gpio) & GPIODIRMASK)==gpioindir)
247 #define GPIOISOUTPUT(gpio) (((gpio) & GPIOSPEEDMASK)==gpiooutdir)
261 //#define GPIOISINPUT(gpio) (((gpio) & GPIODIRMASK)==gpioindir)
262 //#define GPIOISOUTPUT(gpio) (((gpio) & GPIOSPEEDMASK)==gpiooutdir)
248 263
249 #define GPIOISLOWSPEED(gpio) (((gpio) & GPIOSPEEDMASK)==gpiolowspeed)
250 #define GPIOISMEDIUMSPEED(gpio) (((gpio) & GPIOSPEEDMASK)==gpiomediumspeed)
251 #define GPIOISFASTPEED(gpio) (((gpio) & GPIOSPEEDMASK)==gpiofastspeed)
252 #define GPIOISHIGHSPEED(gpio) (((gpio) & GPIOSPEEDMASK)==gpiohighspeeds)
264 //#define GPIOISLOWSPEED(gpio) (((gpio) & GPIOSPEEDMASK)==gpiolowspeed)
265 //#define GPIOISMEDIUMSPEED(gpio) (((gpio) & GPIOSPEEDMASK)==gpiomediumspeed)
266 //#define GPIOISFASTPEED(gpio) (((gpio) & GPIOSPEEDMASK)==gpiofastspeed)
267 //#define GPIOISHIGHSPEED(gpio) (((gpio) & GPIOSPEEDMASK)==gpiohighspeeds)
253 268
254 #define GPIOISPUSHPULL(gpio) (((gpio) & GPIOOUTTYPEMASK)==gpiopushpulltype)
255 #define GPIOISOPENDRAIN(gpio) (((gpio) & GPIOOUTTYPEMASK)==gpioopendraintype)
269 //#define GPIOISPUSHPULL(gpio) (((gpio) & GPIOOUTTYPEMASK)==gpiopushpulltype)
270 //#define GPIOISOPENDRAIN(gpio) (((gpio) & GPIOOUTTYPEMASK)==gpioopendraintype)
256 271
257 #define GPIOISNOPULL(GPIO) (((gpio) & GPIOPULLTYPEMASK)==gpionopulltype)
258 #define GPIOISPULLUP(GPIO) (((gpio) & GPIOPULLTYPEMASK)==gpiopulluptype)
259 #define GPIOISPULLDOWN(GPIO) (((gpio) & GPIOPULLTYPEMASK)==gpiopulldowntype)
272 //#define GPIOISNOPULL(GPIO) (((gpio) & GPIOPULLTYPEMASK)==gpionopulltype)
273 //#define GPIOISPULLUP(GPIO) (((gpio) & GPIOPULLTYPEMASK)==gpiopulluptype)
274 //#define GPIOISPULLDOWN(GPIO) (((gpio) & GPIOPULLTYPEMASK)==gpiopulldowntype)
260 275
261 276
262 277 #define PA0 ((0<<8)+0)
263 278 #define PA1 ((0<<8)+1)
264 279 #define PA2 ((0<<8)+2)
265 280 #define PA3 ((0<<8)+3)
266 281 #define PA4 ((0<<8)+4)
267 282 #define PA5 ((0<<8)+5)
268 283 #define PA6 ((0<<8)+6)
269 284 #define PA7 ((0<<8)+7)
270 285 #define PA8 ((0<<8)+8)
271 286 #define PA9 ((0<<8)+9)
272 287 #define PA10 ((0<<8)+10)
273 288 #define PA11 ((0<<8)+11)
274 289 #define PA12 ((0<<8)+12)
275 290 #define PA13 ((0<<8)+13)
276 291 #define PA14 ((0<<8)+14)
277 292 #define PA15 ((0<<8)+15)
278 293 #define PAX ((0<<8)+255)
279 294 #define PB0 ((1<<8)+0)
280 295 #define PB1 ((1<<8)+1)
281 296 #define PB2 ((1<<8)+2)
282 297 #define PB3 ((1<<8)+3)
283 298 #define PB4 ((1<<8)+4)
284 299 #define PB5 ((1<<8)+5)
285 300 #define PB6 ((1<<8)+6)
286 301 #define PB7 ((1<<8)+7)
287 302 #define PB8 ((1<<8)+8)
288 303 #define PB9 ((1<<8)+9)
289 304 #define PB10 ((1<<8)+10)
290 305 #define PB11 ((1<<8)+11)
291 306 #define PB12 ((1<<8)+12)
292 307 #define PB13 ((1<<8)+13)
293 308 #define PB14 ((1<<8)+14)
294 309 #define PB15 ((1<<8)+15)
295 310 #define PBX ((1<<8)+255)
296 311 #define PC0 ((2<<8)+0)
297 312 #define PC1 ((2<<8)+1)
298 313 #define PC2 ((2<<8)+2)
299 314 #define PC3 ((2<<8)+3)
300 315 #define PC4 ((2<<8)+4)
301 316 #define PC5 ((2<<8)+5)
302 317 #define PC6 ((2<<8)+6)
303 318 #define PC7 ((2<<8)+7)
304 319 #define PC8 ((2<<8)+8)
305 320 #define PC9 ((2<<8)+9)
306 321 #define PC10 ((2<<8)+10)
307 322 #define PC11 ((2<<8)+11)
308 323 #define PC12 ((2<<8)+12)
309 324 #define PC13 ((2<<8)+13)
310 325 #define PC14 ((2<<8)+14)
311 326 #define PC15 ((2<<8)+15)
312 327 #define PCX ((2<<8)+255)
313 328 #define PD0 ((3<<8)+0)
314 329 #define PD1 ((3<<8)+1)
315 330 #define PD2 ((3<<8)+2)
316 331 #define PD3 ((3<<8)+3)
317 332 #define PD4 ((3<<8)+4)
318 333 #define PD5 ((3<<8)+5)
319 334 #define PD6 ((3<<8)+6)
320 335 #define PD7 ((3<<8)+7)
321 336 #define PD8 ((3<<8)+8)
322 337 #define PD9 ((3<<8)+9)
323 338 #define PD10 ((3<<8)+10)
324 339 #define PD11 ((3<<8)+11)
325 340 #define PD12 ((3<<8)+12)
326 341 #define PD13 ((3<<8)+13)
327 342 #define PD14 ((3<<8)+14)
328 343 #define PD15 ((3<<8)+15)
329 344 #define PDX ((3<<8)+255)
330 345 #define PE0 ((4<<8)+0)
331 346 #define PE1 ((4<<8)+1)
332 347 #define PE2 ((4<<8)+2)
333 348 #define PE3 ((4<<8)+3)
334 349 #define PE4 ((4<<8)+4)
335 350 #define PE5 ((4<<8)+5)
336 351 #define PE6 ((4<<8)+6)
337 352 #define PE7 ((4<<8)+7)
338 353 #define PE8 ((4<<8)+8)
339 354 #define PE9 ((4<<8)+9)
340 355 #define PE10 ((4<<8)+10)
341 356 #define PE11 ((4<<8)+11)
342 357 #define PE12 ((4<<8)+12)
343 358 #define PE13 ((4<<8)+13)
344 359 #define PE14 ((4<<8)+14)
345 360 #define PE15 ((4<<8)+15)
346 361 #define PEX ((4<<8)+255)
347 362 #define PF0 ((5<<8)+0)
348 363 #define PF1 ((5<<8)+1)
349 364 #define PF2 ((5<<8)+2)
350 365 #define PF3 ((5<<8)+3)
351 366 #define PF4 ((5<<8)+4)
352 367 #define PF5 ((5<<8)+5)
353 368 #define PF6 ((5<<8)+6)
354 369 #define PF7 ((5<<8)+7)
355 370 #define PF8 ((5<<8)+8)
356 371 #define PF9 ((5<<8)+9)
357 372 #define PF10 ((5<<8)+10)
358 373 #define PF11 ((5<<8)+11)
359 374 #define PF12 ((5<<8)+12)
360 375 #define PF13 ((5<<8)+13)
361 376 #define PF14 ((5<<8)+14)
362 377 #define PF15 ((5<<8)+15)
363 378 #define PFX ((5<<8)+255)
364 379 #define PG0 ((6<<8)+0)
365 380 #define PG1 ((6<<8)+1)
366 381 #define PG2 ((6<<8)+2)
367 382 #define PG3 ((6<<8)+3)
368 383 #define PG4 ((6<<8)+4)
369 384 #define PG5 ((6<<8)+5)
370 385 #define PG6 ((6<<8)+6)
371 386 #define PG7 ((6<<8)+7)
372 387 #define PG8 ((6<<8)+8)
373 388 #define PG9 ((6<<8)+9)
374 389 #define PG10 ((6<<8)+10)
375 390 #define PG11 ((6<<8)+11)
376 391 #define PG12 ((6<<8)+12)
377 392 #define PG13 ((6<<8)+13)
378 393 #define PG14 ((6<<8)+14)
379 394 #define PG15 ((6<<8)+15)
380 395 #define PGX ((6<<8)+255)
381 396 #define PH0 ((7<<8)+0)
382 397 #define PH1 ((7<<8)+1)
383 398 #define PH2 ((7<<8)+2)
384 399 #define PH3 ((7<<8)+3)
385 400 #define PH4 ((7<<8)+4)
386 401 #define PH5 ((7<<8)+5)
387 402 #define PH6 ((7<<8)+6)
388 403 #define PH7 ((7<<8)+7)
389 404 #define PH8 ((7<<8)+8)
390 405 #define PH9 ((7<<8)+9)
391 406 #define PH10 ((7<<8)+10)
392 407 #define PH11 ((7<<8)+11)
393 408 #define PH12 ((7<<8)+12)
394 409 #define PH13 ((7<<8)+13)
395 410 #define PH14 ((7<<8)+14)
396 411 #define PH15 ((7<<8)+15)
397 412 #define PHX ((7<<8)+255)
398 413 #define PI0 ((8<<8)+0)
399 414 #define PI1 ((8<<8)+1)
400 415 #define PI2 ((8<<8)+2)
401 416 #define PI3 ((8<<8)+3)
402 417 #define PI4 ((8<<8)+4)
403 418 #define PI5 ((8<<8)+5)
404 419 #define PI6 ((8<<8)+6)
405 420 #define PI7 ((8<<8)+7)
406 421 #define PI8 ((8<<8)+8)
407 422 #define PI9 ((8<<8)+9)
408 423 #define PI10 ((8<<8)+10)
409 424 #define PI11 ((8<<8)+11)
410 425 #define PI12 ((8<<8)+12)
411 426 #define PI13 ((8<<8)+13)
412 427 #define PI14 ((8<<8)+14)
413 428 #define PI15 ((8<<8)+15)
414 429 #define PIX ((8<<8)+255)
415 430 #define PJ0 ((9<<8)+0)
416 431 #define PJ1 ((9<<8)+1)
417 432 #define PJ2 ((9<<8)+2)
418 433 #define PJ3 ((9<<8)+3)
419 434 #define PJ4 ((9<<8)+4)
420 435 #define PJ5 ((9<<8)+5)
421 436 #define PJ6 ((9<<8)+6)
422 437 #define PJ7 ((9<<8)+7)
423 438 #define PJ8 ((9<<8)+8)
424 439 #define PJ9 ((9<<8)+9)
425 440 #define PJ10 ((9<<8)+10)
426 441 #define PJ11 ((9<<8)+11)
427 442 #define PJ12 ((9<<8)+12)
428 443 #define PJ13 ((9<<8)+13)
429 444 #define PJ14 ((9<<8)+14)
430 445 #define PJ15 ((9<<8)+15)
431 446 #define PJX ((9<<8)+255)
432 447 #define PK0 ((10<<8)+0)
433 448 #define PK1 ((10<<8)+1)
434 449 #define PK2 ((10<<8)+2)
435 450 #define PK3 ((10<<8)+3)
436 451 #define PK4 ((10<<8)+4)
437 452 #define PK5 ((10<<8)+5)
438 453 #define PK6 ((10<<8)+6)
439 454 #define PK7 ((10<<8)+7)
440 455 #define PK8 ((10<<8)+8)
441 456 #define PK9 ((10<<8)+9)
442 457 #define PK10 ((10<<8)+10)
443 458 #define PK11 ((10<<8)+11)
444 459 #define PK12 ((10<<8)+12)
445 460 #define PK13 ((10<<8)+13)
446 461 #define PK14 ((10<<8)+14)
447 462 #define PK15 ((10<<8)+15)
448 463 #define PKX ((10<<8)+255)
449 464 #define PL0 ((11<<8)+0)
450 465 #define PL1 ((11<<8)+1)
451 466 #define PL2 ((11<<8)+2)
452 467 #define PL3 ((11<<8)+3)
453 468 #define PL4 ((11<<8)+4)
454 469 #define PL5 ((11<<8)+5)
455 470 #define PL6 ((11<<8)+6)
456 471 #define PL7 ((11<<8)+7)
457 472 #define PL8 ((11<<8)+8)
458 473 #define PL9 ((11<<8)+9)
459 474 #define PL10 ((11<<8)+10)
460 475 #define PL11 ((11<<8)+11)
461 476 #define PL12 ((11<<8)+12)
462 477 #define PL13 ((11<<8)+13)
463 478 #define PL14 ((11<<8)+14)
464 479 #define PL15 ((11<<8)+15)
465 480 #define PLX ((11<<8)+255)
466 481 #define PM0 ((12<<8)+0)
467 482 #define PM1 ((12<<8)+1)
468 483 #define PM2 ((12<<8)+2)
469 484 #define PM3 ((12<<8)+3)
470 485 #define PM4 ((12<<8)+4)
471 486 #define PM5 ((12<<8)+5)
472 487 #define PM6 ((12<<8)+6)
473 488 #define PM7 ((12<<8)+7)
474 489 #define PM8 ((12<<8)+8)
475 490 #define PM9 ((12<<8)+9)
476 491 #define PM10 ((12<<8)+10)
477 492 #define PM11 ((12<<8)+11)
478 493 #define PM12 ((12<<8)+12)
479 494 #define PM13 ((12<<8)+13)
480 495 #define PM14 ((12<<8)+14)
481 496 #define PM15 ((12<<8)+15)
482 497 #define PMX ((12<<8)+255)
483 498 #define PN0 ((13<<8)+0)
484 499 #define PN1 ((13<<8)+1)
485 500 #define PN2 ((13<<8)+2)
486 501 #define PN3 ((13<<8)+3)
487 502 #define PN4 ((13<<8)+4)
488 503 #define PN5 ((13<<8)+5)
489 504 #define PN6 ((13<<8)+6)
490 505 #define PN7 ((13<<8)+7)
491 506 #define PN8 ((13<<8)+8)
492 507 #define PN9 ((13<<8)+9)
493 508 #define PN10 ((13<<8)+10)
494 509 #define PN11 ((13<<8)+11)
495 510 #define PN12 ((13<<8)+12)
496 511 #define PN13 ((13<<8)+13)
497 512 #define PN14 ((13<<8)+14)
498 513 #define PN15 ((13<<8)+15)
499 514 #define PNX ((13<<8)+255)
500 515 #define PO0 ((14<<8)+0)
501 516 #define PO1 ((14<<8)+1)
502 517 #define PO2 ((14<<8)+2)
503 518 #define PO3 ((14<<8)+3)
504 519 #define PO4 ((14<<8)+4)
505 520 #define PO5 ((14<<8)+5)
506 521 #define PO6 ((14<<8)+6)
507 522 #define PO7 ((14<<8)+7)
508 523 #define PO8 ((14<<8)+8)
509 524 #define PO9 ((14<<8)+9)
510 525 #define PO10 ((14<<8)+10)
511 526 #define PO11 ((14<<8)+11)
512 527 #define PO12 ((14<<8)+12)
513 528 #define PO13 ((14<<8)+13)
514 529 #define PO14 ((14<<8)+14)
515 530 #define PO15 ((14<<8)+15)
516 531 #define POX ((14<<8)+255)
517 532 #define PP0 ((15<<8)+0)
518 533 #define PP1 ((15<<8)+1)
519 534 #define PP2 ((15<<8)+2)
520 535 #define PP3 ((15<<8)+3)
521 536 #define PP4 ((15<<8)+4)
522 537 #define PP5 ((15<<8)+5)
523 538 #define PP6 ((15<<8)+6)
524 539 #define PP7 ((15<<8)+7)
525 540 #define PP8 ((15<<8)+8)
526 541 #define PP9 ((15<<8)+9)
527 542 #define PP10 ((15<<8)+10)
528 543 #define PP11 ((15<<8)+11)
529 544 #define PP12 ((15<<8)+12)
530 545 #define PP13 ((15<<8)+13)
531 546 #define PP14 ((15<<8)+14)
532 547 #define PP15 ((15<<8)+15)
533 548 #define PPX ((15<<8)+255)
534 549 #define PQ0 ((16<<8)+0)
535 550 #define PQ1 ((16<<8)+1)
536 551 #define PQ2 ((16<<8)+2)
537 552 #define PQ3 ((16<<8)+3)
538 553 #define PQ4 ((16<<8)+4)
539 554 #define PQ5 ((16<<8)+5)
540 555 #define PQ6 ((16<<8)+6)
541 556 #define PQ7 ((16<<8)+7)
542 557 #define PQ8 ((16<<8)+8)
543 558 #define PQ9 ((16<<8)+9)
544 559 #define PQ10 ((16<<8)+10)
545 560 #define PQ11 ((16<<8)+11)
546 561 #define PQ12 ((16<<8)+12)
547 562 #define PQ13 ((16<<8)+13)
548 563 #define PQ14 ((16<<8)+14)
549 564 #define PQ15 ((16<<8)+15)
550 565 #define PQX ((16<<8)+255)
551 566 #define PR0 ((17<<8)+0)
552 567 #define PR1 ((17<<8)+1)
553 568 #define PR2 ((17<<8)+2)
554 569 #define PR3 ((17<<8)+3)
555 570 #define PR4 ((17<<8)+4)
556 571 #define PR5 ((17<<8)+5)
557 572 #define PR6 ((17<<8)+6)
558 573 #define PR7 ((17<<8)+7)
559 574 #define PR8 ((17<<8)+8)
560 575 #define PR9 ((17<<8)+9)
561 576 #define PR10 ((17<<8)+10)
562 577 #define PR11 ((17<<8)+11)
563 578 #define PR12 ((17<<8)+12)
564 579 #define PR13 ((17<<8)+13)
565 580 #define PR14 ((17<<8)+14)
566 581 #define PR15 ((17<<8)+15)
567 582 #define PRX ((17<<8)+255)
568 583 #define PS0 ((18<<8)+0)
569 584 #define PS1 ((18<<8)+1)
570 585 #define PS2 ((18<<8)+2)
571 586 #define PS3 ((18<<8)+3)
572 587 #define PS4 ((18<<8)+4)
573 588 #define PS5 ((18<<8)+5)
574 589 #define PS6 ((18<<8)+6)
575 590 #define PS7 ((18<<8)+7)
576 591 #define PS8 ((18<<8)+8)
577 592 #define PS9 ((18<<8)+9)
578 593 #define PS10 ((18<<8)+10)
579 594 #define PS11 ((18<<8)+11)
580 595 #define PS12 ((18<<8)+12)
581 596 #define PS13 ((18<<8)+13)
582 597 #define PS14 ((18<<8)+14)
583 598 #define PS15 ((18<<8)+15)
584 599 #define PSX ((18<<8)+255)
585 600 #define PT0 ((19<<8)+0)
586 601 #define PT1 ((19<<8)+1)
587 602 #define PT2 ((19<<8)+2)
588 603 #define PT3 ((19<<8)+3)
589 604 #define PT4 ((19<<8)+4)
590 605 #define PT5 ((19<<8)+5)
591 606 #define PT6 ((19<<8)+6)
592 607 #define PT7 ((19<<8)+7)
593 608 #define PT8 ((19<<8)+8)
594 609 #define PT9 ((19<<8)+9)
595 610 #define PT10 ((19<<8)+10)
596 611 #define PT11 ((19<<8)+11)
597 612 #define PT12 ((19<<8)+12)
598 613 #define PT13 ((19<<8)+13)
599 614 #define PT14 ((19<<8)+14)
600 615 #define PT15 ((19<<8)+15)
601 616 #define PTX ((19<<8)+255)
602 617 #define PU0 ((20<<8)+0)
603 618 #define PU1 ((20<<8)+1)
604 619 #define PU2 ((20<<8)+2)
605 620 #define PU3 ((20<<8)+3)
606 621 #define PU4 ((20<<8)+4)
607 622 #define PU5 ((20<<8)+5)
608 623 #define PU6 ((20<<8)+6)
609 624 #define PU7 ((20<<8)+7)
610 625 #define PU8 ((20<<8)+8)
611 626 #define PU9 ((20<<8)+9)
612 627 #define PU10 ((20<<8)+10)
613 628 #define PU11 ((20<<8)+11)
614 629 #define PU12 ((20<<8)+12)
615 630 #define PU13 ((20<<8)+13)
616 631 #define PU14 ((20<<8)+14)
617 632 #define PU15 ((20<<8)+15)
618 633 #define PUX ((20<<8)+255)
619 634 #define PV0 ((21<<8)+0)
620 635 #define PV1 ((21<<8)+1)
621 636 #define PV2 ((21<<8)+2)
622 637 #define PV3 ((21<<8)+3)
623 638 #define PV4 ((21<<8)+4)
624 639 #define PV5 ((21<<8)+5)
625 640 #define PV6 ((21<<8)+6)
626 641 #define PV7 ((21<<8)+7)
627 642 #define PV8 ((21<<8)+8)
628 643 #define PV9 ((21<<8)+9)
629 644 #define PV10 ((21<<8)+10)
630 645 #define PV11 ((21<<8)+11)
631 646 #define PV12 ((21<<8)+12)
632 647 #define PV13 ((21<<8)+13)
633 648 #define PV14 ((21<<8)+14)
634 649 #define PV15 ((21<<8)+15)
635 650 #define PVX ((21<<8)+255)
636 651 #define PW0 ((22<<8)+0)
637 652 #define PW1 ((22<<8)+1)
638 653 #define PW2 ((22<<8)+2)
639 654 #define PW3 ((22<<8)+3)
640 655 #define PW4 ((22<<8)+4)
641 656 #define PW5 ((22<<8)+5)
642 657 #define PW6 ((22<<8)+6)
643 658 #define PW7 ((22<<8)+7)
644 659 #define PW8 ((22<<8)+8)
645 660 #define PW9 ((22<<8)+9)
646 661 #define PW10 ((22<<8)+10)
647 662 #define PW11 ((22<<8)+11)
648 663 #define PW12 ((22<<8)+12)
649 664 #define PW13 ((22<<8)+13)
650 665 #define PW14 ((22<<8)+14)
651 666 #define PW15 ((22<<8)+15)
652 667 #define PWX ((22<<8)+255)
653 668 #define PX0 ((23<<8)+0)
654 669 #define PX1 ((23<<8)+1)
655 670 #define PX2 ((23<<8)+2)
656 671 #define PX3 ((23<<8)+3)
657 672 #define PX4 ((23<<8)+4)
658 673 #define PX5 ((23<<8)+5)
659 674 #define PX6 ((23<<8)+6)
660 675 #define PX7 ((23<<8)+7)
661 676 #define PX8 ((23<<8)+8)
662 677 #define PX9 ((23<<8)+9)
663 678 #define PX10 ((23<<8)+10)
664 679 #define PX11 ((23<<8)+11)
665 680 #define PX12 ((23<<8)+12)
666 681 #define PX13 ((23<<8)+13)
667 682 #define PX14 ((23<<8)+14)
668 683 #define PX15 ((23<<8)+15)
669 684 #define PXX ((23<<8)+255)
670 685 #define PY0 ((24<<8)+0)
671 686 #define PY1 ((24<<8)+1)
672 687 #define PY2 ((24<<8)+2)
673 688 #define PY3 ((24<<8)+3)
674 689 #define PY4 ((24<<8)+4)
675 690 #define PY5 ((24<<8)+5)
676 691 #define PY6 ((24<<8)+6)
677 692 #define PY7 ((24<<8)+7)
678 693 #define PY8 ((24<<8)+8)
679 694 #define PY9 ((24<<8)+9)
680 695 #define PY10 ((24<<8)+10)
681 696 #define PY11 ((24<<8)+11)
682 697 #define PY12 ((24<<8)+12)
683 698 #define PY13 ((24<<8)+13)
684 699 #define PY14 ((24<<8)+14)
685 700 #define PY15 ((24<<8)+15)
686 701 #define PYX ((24<<8)+255)
687 702 #define PZ0 ((25<<8)+0)
688 703 #define PZ1 ((25<<8)+1)
689 704 #define PZ2 ((25<<8)+2)
690 705 #define PZ3 ((25<<8)+3)
691 706 #define PZ4 ((25<<8)+4)
692 707 #define PZ5 ((25<<8)+5)
693 708 #define PZ6 ((25<<8)+6)
694 709 #define PZ7 ((25<<8)+7)
695 710 #define PZ8 ((25<<8)+8)
696 711 #define PZ9 ((25<<8)+9)
697 712 #define PZ10 ((25<<8)+10)
698 713 #define PZ11 ((25<<8)+11)
699 714 #define PZ12 ((25<<8)+12)
700 715 #define PZ13 ((25<<8)+13)
701 716 #define PZ14 ((25<<8)+14)
702 717 #define PZ15 ((25<<8)+15)
703 718 #define PZX ((25<<8)+255)
704 719
705 720 #endif
706 721
707 722 #ifdef __cplusplus
708 }
723 }
709 724 #endif
710 725 #endif //GPIO_H
711 726
712 727
713 728
714 729
715 730
716 731
717 732
718 733
719 734
720 735
721 736
722 737
@@ -1,57 +1,59
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 <stdint.h>
23 23 #ifdef __cplusplus
24 24 extern "C" {
25 25 #endif
26 26 extern volatile uint32_t tickCounter;
27 27 extern uint32_t getAPB1Freq();
28 28 extern uint32_t getAPB2Freq();
29 29 extern uint32_t getCpuFreq();
30 30 extern int setCpuFreq(uint32_t freq);
31 31 extern int optimizePLLcfg(uint32_t freq, uint32_t srcfreq,uint32_t PLLM,uint32_t* PLLP, uint32_t* PLLN,uint8_t* AHBPRindx);
32 32 extern int setPll(uint32_t freq);
33 extern void getCpuUUID(char*uuid);
34 extern int getCpuUUIDLen();
33 35 extern void enable_FPU();
34 36 extern void reset_AHB1();
35 37 extern void reset_AHB2();
36 38 extern void reset_APB1();
37 39 extern void reset_APB2();
38 40 extern void configureSysTick();
39 41 extern void delay_us(uint32_t value);
40 42 extern void delay_100us(uint32_t value);
41 43 extern void SysTick_Handler(void);
42 44
43 45 #ifdef __cplusplus
44 46 }
45 47 #endif
46 48
47 49
48 50
49 51
50 52
51 53
52 54
53 55
54 56
55 57
56 58
57 59
@@ -1,535 +1,535
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 #define _PRVATE_ILI9328_
23 23 #include <ili9328.h>
24 24 #include <stdio.h>
25 25 #include <stddef.h>
26 26 #include <core.h>
27 27 #include <math.h>
28 28 #include <malloc.h>
29 29
30 30 #ifdef __OPTIMIZED_MATH
31 31 #include <optimised_math.h>
32 32 #endif
33 33
34 34
35 35 #define ilipaintLine(LCD,X,Y,W,buffer,buffsize) \
36 36 for(int l=0;l<1;l++)\
37 37 {\
38 38 ili9328setFrame(LCD,X,Y,W,1);\
39 39 int rem=(W)%buffsize;\
40 40 if(rem)LCD->interface.lcd_interface->writeGRAM(buffer,rem);\
41 41 for(int i=rem;i<(W);i+=buffsize)\
42 42 {\
43 43 LCD->interface.lcd_interface->writeGRAM(buffer,buffsize);\
44 44 }\
45 45 }
46 46
47 47 #define ilipaintHLineWithCont(LCD,X,Y,W,ContSz,bufferInt,buffIntsize,bufferCont,buffContsize) \
48 48 for(int l=0;l<1;l++)\
49 49 {\
50 50 ili9328setFrame(LCD,X,Y,W,1);\
51 51 int rem=(ContSz)%buffContsize;\
52 52 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
53 53 for(int i=rem;i<(ContSz);i+=buffContsize)\
54 54 {\
55 55 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
56 56 }\
57 57 if((2*ContSz)<W) \
58 58 {\
59 59 rem=(W-(2*ContSz))%buffIntsize;\
60 60 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferInt,rem);\
61 61 for(int i=rem;i<(W-(2*ContSz));i+=buffIntsize)\
62 62 {\
63 63 LCD->interface.lcd_interface->writeGRAM(bufferInt,buffIntsize);\
64 64 }\
65 65 }\
66 66 rem=(ContSz)%buffContsize;\
67 67 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
68 68 for(int i=rem;i<(ContSz);i+=buffContsize)\
69 69 {\
70 70 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
71 71 }\
72 72 }\
73 73
74 74
75 75 #define ilipaintVLineWithCont(LCD,X,Y,H,ContSz,bufferInt,buffIntsize,bufferCont,buffContsize) \
76 76 for(int l=0;l<1;l++)\
77 77 {\
78 78 ili9328setFrame(LCD,X,Y,1,H);\
79 79 int rem=(ContSz)%buffContsize;\
80 80 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
81 81 for(int i=rem;i<(ContSz);i+=buffContsize)\
82 82 {\
83 83 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
84 84 }\
85 85 if((2*ContSz)<H) \
86 86 {\
87 87 rem=(H-(2*ContSz))%buffIntsize;\
88 88 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferInt,rem);\
89 89 for(int i=rem;i<(H-(2*ContSz));i+=buffIntsize)\
90 90 {\
91 91 LCD->interface.lcd_interface->writeGRAM(bufferInt,buffIntsize);\
92 92 }\
93 93 }\
94 94 rem=(ContSz)%buffContsize;\
95 95 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
96 96 for(int i=rem;i<(ContSz);i+=buffContsize)\
97 97 {\
98 98 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
99 99 }\
100 100 }\
101 101
102 102
103 103 #define ilipaintHalfTopVLineWithCont(LCD,X,Y,H,ContSz,bufferInt,buffIntsize,bufferCont,buffContsize) \
104 104 for(int l=0;l<1;l++)\
105 105 {\
106 106 ili9328setFrame(LCD,X,Y,1,H);\
107 107 int rem=(ContSz)%buffContsize;\
108 108 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
109 109 for(int i=rem;i<(ContSz);i+=buffContsize)\
110 110 {\
111 111 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
112 112 }\
113 113 if(ContSz<H) \
114 114 {\
115 115 rem=(H-ContSz)%buffIntsize;\
116 116 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferInt,rem);\
117 117 for(int i=rem;i<(H-ContSz);i+=buffIntsize)\
118 118 {\
119 119 LCD->interface.lcd_interface->writeGRAM(bufferInt,buffIntsize);\
120 120 }\
121 121 }\
122 122 }\
123 123
124 124
125 125 #define ilipaintHalfBottomVLineWithCont(LCD,X,Y,H,ContSz,bufferInt,buffIntsize,bufferCont,buffContsize) \
126 126 for(int l=0;l<1;l++)\
127 127 {\
128 128 ili9328setFrame(LCD,X,Y,1,H);\
129 129 int rem;\
130 130 if(ContSz<H) \
131 131 {\
132 132 rem=(H-ContSz)%buffIntsize;\
133 133 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferInt,rem);\
134 134 for(int i=rem;i<(H-ContSz);i+=buffIntsize)\
135 135 {\
136 136 LCD->interface.lcd_interface->writeGRAM(bufferInt,buffIntsize);\
137 137 }\
138 138 }\
139 139 rem=(ContSz)%buffContsize;\
140 140 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
141 141 for(int i=rem;i<(ContSz);i+=buffContsize)\
142 142 {\
143 143 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
144 144 }\
145 145 }\
146 146
147 147
148 148 void ili9328setGRAMaddress(LCD_t* LCD,uint16_t Haddress,uint16_t Vaddress)
149 149 {
150 150 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_HORIZONTALGRAMADDRESSSET,Haddress);
151 151 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_VERTICALGRAMADDRESSSET,Vaddress);
152 152 }
153 153
154 154 void ili9328refreshenable(struct LCD_t* LCD,int enable)
155 155 {
156 156 if(enable)
157 157 {
158 158 //LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_ENTRYMODE,0x1018);
159 159 }
160 160 else
161 161 {
162 162 //LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_ENTRYMODE,0x1008);
163 163
164 164 }
165 165 }
166 166
167 167 void ili9328setFrame(LCD_t* LCD,uint16_t X,uint16_t Y,uint16_t W,uint16_t H)
168 168 {
169 169 if((X>(LCD->width-1)) || (Y>(LCD->height-1)) || ((X+W)>LCD->width) || ((Y+H)>LCD->height))
170 170 {
171 171 printf("Error painting out of screen!\n");
172 172 return;
173 173 }
174 174 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSSTARTPOSITION,(uint32_t)X);
175 175 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSENDPOSITION,(uint32_t)(W+X-1));
176 176 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_VERTICALADDRESSSTARTPOSITION,(uint32_t)Y);
177 177 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_VERTICALADDRESSENDPOSITION,(uint32_t)(Y+H-1));
178 178 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_HORIZONTALGRAMADDRESSSET,(uint32_t)X);
179 179 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_VERTICALGRAMADDRESSSET,(uint32_t)Y);
180 180 }
181 181
182 182 void ili9328paint(LCD_t* LCD,void* buffer,uint16_t Xpos,uint16_t Ypos,uint16_t Width,uint16_t Height)
183 183 {
184 184 if((LCD!=NULL) && (LCD->interface.lcd_interface!=NULL) && (LCD->interface.lcd_interface->writeGRAM!=NULL) && (LCD->width>=(Xpos+Width)) && (LCD->height>=(Ypos+Height)))
185 185 {
186 186 ili9328setFrame(LCD,Xpos,Ypos,Width,Height);
187 187 LCD->interface.lcd_interface->writeGRAM(buffer,Width*Height);
188 188 }
189 189 }
190 190
191 191 void ili9328paintFilCirc(LCD_t* LCD,uint16_t Xpos,uint16_t Ypos,uint16_t r,uint32_t contColor,uint16_t contSz,uint32_t fillColor)
192 192 {
193 193 //Based on the mid point circle algorithm from Wikipedia
194 194 //http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
195 195 uint16_t innerbuffer[16];
196 196 uint16_t outterbuffer[16];
197 197 for(int i=0;i<16;i++)innerbuffer[i]=fillColor;
198 198 for(int i=0;i<16;i++)outterbuffer[i]=contColor;
199 199 if(contSz<r)
200 200 {
201 201 int error = -r,error_int = -r+contSz;
202 202 int x = r,x_int=r-contSz;
203 203 int y = 0,y_int=0;
204 204 while (x >= y)
205 205 {
206 206 ilipaintHLineWithCont(LCD,(Xpos-x),(Ypos+y),(2*x),(x-x_int),innerbuffer,16,outterbuffer,16);
207 207 ilipaintHLineWithCont(LCD,(Xpos-x),(Ypos-y),(2*x),(x-x_int),innerbuffer,16,outterbuffer,16);
208 208 ilipaintHalfTopVLineWithCont(LCD,(Xpos+y),(Ypos-x),(x),(x-x_int),innerbuffer,16,outterbuffer,16);
209 209 ilipaintHalfTopVLineWithCont(LCD,(Xpos-y),(Ypos-x),(x),(x-x_int),innerbuffer,16,outterbuffer,16);
210 210 ilipaintHalfBottomVLineWithCont(LCD,(Xpos-y),(Ypos),(x),(x-x_int),innerbuffer,16,outterbuffer,16);
211 211 ilipaintHalfBottomVLineWithCont(LCD,(Xpos+y),(Ypos),(x),(x-x_int),innerbuffer,16,outterbuffer,16);
212 212 error += y;
213 213 ++y;
214 214 error += y;
215 215 error_int += y_int;
216 216 ++y_int;
217 217 error_int += y_int;
218 218 if(error >= 0)
219 219 {
220 220 error -= x;
221 221 --x;
222 222 error -= x;
223 223 }
224 224 if(error_int >= 0)
225 225 {
226 226 error_int -= x_int;
227 227 --x_int;
228 228 error_int -= x_int;
229 229 }
230 230 }
231 231
232 232
233 233
234 234 }
235 235
236 236 }
237 237
238 238 void ili9328paintFilCirc_old(LCD_t* LCD,uint16_t Xpos,uint16_t Ypos,uint16_t r,uint32_t contColor,uint16_t contSz,uint32_t fillColor)
239 239 {
240 240 if(contSz<r)
241 241 {
242 242 uint16_t innerbuffer[16];
243 243 uint16_t outterbuffer[16];
244 244 int32_t rr=(r*r),rr2=((r-contSz)*(r-contSz)),contSz2,Val1,Val2,X1,W,rem;
245 245 for(int i=0;i<16;i++)innerbuffer[i]=fillColor;
246 246 for(int i=0;i<16;i++)outterbuffer[i]=contColor;
247 247 /* Y = b +/- sqrt[r^2 - (x - a)^2] */
248 248 for(int32_t line=-r;line<r;line++)
249 249 {
250 250 #ifdef __OPTIMIZED_MATH
251 Val1 = (uint32_t)optimised_sqrt((float32_t)(rr - (line*line)) );
252 Val2 = (uint32_t)optimised_sqrt((float32_t)(rr2 - (line*line)) );
251 Val1 = (uint32_t)optimised_sqrt((float)(rr - (line*line)) );
252 Val2 = (uint32_t)optimised_sqrt((float)(rr2 - (line*line)) );
253 253 #else
254 254 Val1 = sqrt( (double)(rr - ((line)*(line))) );
255 255 Val2 = sqrt( (double)(rr2 - ((line)*(line))) );
256 256 #endif
257 257 X1=Xpos - Val1;
258 258 contSz2= Val1-Val2;
259 259 ili9328setFrame(LCD,X1,line+Ypos,2*Val1,1);
260 260 rem=(contSz2)%16;
261 261 if(rem)LCD->interface.lcd_interface->writeGRAM(outterbuffer,rem);
262 262 for(int i=rem;i<(contSz2);i+=16)
263 263 {
264 264 LCD->interface.lcd_interface->writeGRAM(outterbuffer,16);
265 265 }
266 266
267 267 W=2*Val1;
268 268 if(W>(2*contSz2))
269 269 {
270 270 W-=2*contSz2;
271 271 rem=(W)%16;
272 272 if(rem)LCD->interface.lcd_interface->writeGRAM(innerbuffer,rem);
273 273 for(int i=rem;i<(W);i+=16)
274 274 {
275 275 LCD->interface.lcd_interface->writeGRAM(innerbuffer,16);
276 276 }
277 277 }
278 278
279 279 rem=(contSz2)%16;
280 280 if(rem)LCD->interface.lcd_interface->writeGRAM(outterbuffer,rem);
281 281 for(int i=rem;i<(contSz2);i+=16)
282 282 {
283 283 LCD->interface.lcd_interface->writeGRAM(outterbuffer,16);
284 284 }
285 285 }
286 286 }
287 287 }
288 288
289 289
290 290
291 291
292 292 void ili9328paintFilRect(LCD_t* LCD,uint16_t Xpos,uint16_t Ypos,uint16_t w,uint16_t h,uint32_t contColor,uint16_t contSz,uint32_t fillColor)
293 293 {
294 294 ili9328setFrame(LCD,Xpos,Ypos,w,h);
295 295 uint16_t tmp[32];
296 296 for(int i=0;i<32;i++)tmp[i]=fillColor;
297 297 for(int i=0;i<(h*w);i+=32)
298 298 {
299 299 LCD->interface.lcd_interface->writeGRAM(tmp,32);
300 300 }
301 301 int rem=(w*h)%32;
302 302 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
303 303 if(contSz)
304 304 {
305 305 ili9328setFrame(LCD,Xpos,Ypos,w,contSz);
306 306 for(int i=0;i<32;i++)tmp[i]=contColor;
307 307 rem=(w*contSz)%32;
308 308 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
309 309 for(int i=rem;i<(w*contSz);i+=32)
310 310 {
311 311 LCD->interface.lcd_interface->writeGRAM(tmp,32);
312 312 }
313 313
314 314 ili9328setFrame(LCD,Xpos,Ypos+h-contSz,w,contSz);
315 315 rem=(w*contSz)%32;
316 316 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
317 317 for(int i=rem;i<(w*contSz);i+=32)
318 318 {
319 319 LCD->interface.lcd_interface->writeGRAM(tmp,32);
320 320 }
321 321
322 322 ili9328setFrame(LCD,Xpos,Ypos,contSz,h);
323 323 rem=(h*contSz)%32;
324 324 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
325 325 for(int i=rem;i<(h*contSz);i+=32)
326 326 {
327 327 LCD->interface.lcd_interface->writeGRAM(tmp,32);
328 328 }
329 329
330 330 ili9328setFrame(LCD,Xpos+w-contSz,Ypos,contSz,h);
331 331 rem=(h*contSz)%32;
332 332 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
333 333 for(int i=rem;i<(h*contSz);i+=32)
334 334 {
335 335 LCD->interface.lcd_interface->writeGRAM(tmp,32);
336 336 }
337 337 }
338 338 }
339 339
340 340 void ili9328paintText(LCD_t* LCD,char* buffer,uint16_t Xpos,uint16_t Ypos,sFONT* font,uint32_t color)
341 341 {
342 342 int w=font->Width,h=font->Height,bpl=font->bytesPerLine,pix=0,tableoffset=0;
343 343 uint16_t tmp[w];
344 344 uint16_t linenum=0,charnum=0;
345 345 uint8_t line=0;
346 346 while(*buffer!='\0')
347 347 {
348 348 if(*buffer<32)*buffer=32;
349 349 if(*buffer>127)*buffer=32;
350 350 LCD->setFrame(LCD,Xpos+(charnum*w),Ypos-h,w,1);
351 351 // LCD->interface->readGRAM(tmp,w);
352 352 LCD->getPix(LCD,tmp,Xpos+(charnum*w),Ypos-h,w,1);
353 353 for(int i=0;i<(h*w);i++)
354 354 {
355 355 if( ((i%w)==0) ) //read current line to apply text pixmap
356 356 {
357 357 if(linenum++>0)
358 358 {
359 359 LCD->setFrame(LCD,Xpos+(charnum*w),Ypos + linenum -h,w,1);
360 360 LCD->interface.lcd_interface->writeGRAM(tmp,w);
361 361 //ili9328setFrame(LCD,Xpos+(charnum*w),Ypos + linenum + 1-h,w,1);
362 362 // LCD->interface->readGRAM(tmp,w);
363 363 LCD->getPix(LCD,tmp,Xpos+(charnum*w),Ypos + linenum + 1-h,w,1);
364 364 pix=0;
365 365 }
366 366 }
367 367 if((pix%8) == 0)
368 368 {
369 369 line=font->table[(((*buffer)-32)*h*bpl)+tableoffset++];
370 370 }
371 371 //tmp[pix]=0;
372 372 if((line & (uint8_t)0x01)==(uint8_t)1)tmp[pix]=(uint16_t)color;
373 373 pix++;
374 374 line>>=1;
375 375 }
376 376 linenum=0;
377 377 tableoffset=0;
378 378 charnum++;
379 379 buffer++;
380 380 }
381 381 }
382 382
383 383
384 384 void ili9328paintChar(LCD_t* LCD,char buffer,uint16_t Xpos,uint16_t Ypos,sFONT* font,uint32_t color)
385 385 {
386 386 int w=font->Width,h=font->Height,bpl=font->bytesPerLine,pix=0,tableoffset=0;
387 387 uint16_t tmp[w];
388 388 uint16_t tmp2[w];
389 389 uint16_t linenum=0,charnum=0;
390 390 uint8_t line=0;
391 391 if(buffer<32)buffer=32;
392 392 if(buffer>127)buffer=32;
393 393 ili9328setFrame(LCD,Xpos,Ypos-h,w,1);
394 394 // LCD->interface->readGRAM(tmp,w);
395 395 ili9328getPix(LCD,tmp2,Xpos,Ypos-h,w,1);
396 396 for(int i=0;i<w;i++)
397 397 {
398 398 tmp[i]=0;
399 399 }
400 400 for(int i=0;i<(h*w);i++)
401 401 {
402 402 if( ((i%w)==0) ) //read current line to apply text pixmap
403 403 {
404 404 if(linenum++>0)
405 405 {
406 406 ili9328setFrame(LCD,Xpos,Ypos + linenum -h,w,1);
407 407 LCD->interface.lcd_interface->writeGRAM(tmp,w);
408 408 //ili9328setFrame(LCD,Xpos+(charnum*w),Ypos + linenum + 1-h,w,1);
409 409 // LCD->interface->readGRAM(tmp,w);
410 410 ili9328getPix(LCD,tmp2,Xpos,Ypos + linenum + 1-h,w,1);
411 411 for(int i=0;i<w;i++)
412 412 {
413 413 tmp[i]=0;
414 414 }
415 415 pix=0;
416 416 }
417 417 }
418 418 if((pix%8) == 0)
419 419 {
420 420 line=font->table[(((buffer)-32)*h*bpl)+tableoffset++];
421 421 }
422 422 //tmp[pix]=0;
423 423 if((line & (uint8_t)0x01)==(uint8_t)1)tmp[pix]=(uint16_t)color;
424 424 pix++;
425 425 line>>=1;
426 426 }
427 427
428 428 }
429 429
430 430 int ili9328init(struct LCD_t* LCD)
431 431 {
432 432 if((LCD!=NULL) && (LCD->interface.lcd_interface!=NULL) && (LCD->interface.lcd_interface->writereg!=NULL))
433 433 {
434 434 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_DRIVEROUTPUTCONTROL1, 0x0100); // Driver Output Control Register (R01h)
435 435 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_LCDDRIVINGCONTROL, 0x0700); // LCD Driving Waveform Control (R02h)
436 436 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_ENTRYMODE, 0x0230); // Entry Mode (R03h)
437 437 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL1, 0xC000);
438 438 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL2, 0x0302);
439 439 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL3, 0x0000);
440 440 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL4, 0x0000); // Fmark On
441 441 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL1, 0x0000); // Power Control 1 (R10h)
442 442 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL2, 0x0007); // Power Control 2 (R11h)
443 443 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL3, 0x0000); // Power Control 3 (R12h)
444 444 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL4, 0x0000); // Power Control 4 (R13h)
445 445 delay_100us(1000);
446 446 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL1, 0x14B0); // Power Control 1 (R10h)
447 447 delay_100us(500);
448 448 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL2, 0x0007); // Power Control 2 (R11h)
449 449 delay_100us(500);
450 450 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL3, 0x008E); // Power Control 3 (R12h)
451 451 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL4, 0x0C00); // Power Control 4 (R13h)
452 452 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_POWERCONTROL7, 0x0015); // NVM read data 2 (R29h)
453 453 delay_100us(500);
454 454 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL1, 0x0000); // Gamma Control 1
455 455 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL2, 0x0107); // Gamma Control 2
456 456 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL3, 0x0000); // Gamma Control 3
457 457 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL4, 0x0203); // Gamma Control 4
458 458 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL5, 0x0402); // Gamma Control 5
459 459 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL6, 0x0000); // Gamma Control 6
460 460 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL7, 0x0207); // Gamma Control 7
461 461 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL8, 0x0000); // Gamma Control 8
462 462 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL9, 0x0203); // Gamma Control 9
463 463 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_GAMMACONTROL10, 0x0403); // Gamma Control 10
464 464 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSSTARTPOSITION, 0x0000); // Window Horizontal RAM Address Start (R50h)
465 465 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSENDPOSITION, LCD->width - 1); // Window Horizontal RAM Address End (R51h)
466 466 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_VERTICALADDRESSSTARTPOSITION, 0X0000); // Window Vertical RAM Address Start (R52h)
467 467 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_VERTICALADDRESSENDPOSITION, LCD->height - 1); // Window Vertical RAM Address End (R53h)
468 468 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_DRIVEROUTPUTCONTROL2, 0xa700); // Driver Output Control (R60h)
469 469 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_BASEIMAGEDISPLAYCONTROL, 0x0003); // Driver Output Control (R61h) - enable VLE
470 470 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_PANELINTERFACECONTROL1, 0X0010); // Panel Interface Control 1 (R90h)
471 471 // Display On
472 472 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL1, 0x0133); // Display Control (R07h)
473 473 delay_100us(500);
474 474 LCD->paintFilRect(LCD,0,0,LCD->width,LCD->height,0,0,0xFFFF);
475 475 }
476 476 return 0;
477 477 }
478 478
479 479 void ili9328cpFrame(LCD_t* LCD,void* buffer,int x,int y,int w, int h)
480 480 {
481 481 #define __set__Address__(_x_,_y_) LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_HORIZONTALGRAMADDRESSSET,_x_); \
482 482 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_VERTICALGRAMADDRESSSET,_y_)
483 483
484 484 uint16_t* castedBuff = (uint16_t*)buffer;
485 485 int cx=x,cy=y;
486 486 for(int i=0;i<(w*h);i++)
487 487 {
488 488 __set__Address__(cx,cy);
489 489 LCD->interface.lcd_interface->readGRAM((void*)(&castedBuff[i]),1);
490 490 cx+=1;
491 491 if(cx>=(w+x))
492 492 {
493 493 cx=x;
494 494 cy+=1;
495 495 }
496 496 }
497 497 }
498 498
499 499
500 500
501 501 void ili9328getPix(struct LCD_t* LCD,uint16_t* buffer,uint16_t Xpos,uint16_t Ypos,uint16_t w,uint16_t h)
502 502 {
503 503 #define __set__Address__(_x_,_y_) LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_HORIZONTALGRAMADDRESSSET,_x_); \
504 504 LCD->interface.lcd_interface->writereg(ILI9328_REGISTER_VERTICALGRAMADDRESSSET,_y_)
505 505
506 506 int cx=Xpos,cy=Ypos;
507 507 ili9328setFrame(LCD,Xpos,Ypos,w,h);
508 508 for(int i=0;i<(w*h);i++)
509 509 {
510 510 __set__Address__(cx,cy);
511 511 LCD->interface.lcd_interface->readGRAM((void*)(&buffer[i]),1);
512 512 cx+=1;
513 513 if(cx>=(w+Xpos))
514 514 {
515 515 cx=Xpos;
516 516 cy+=1;
517 517 }
518 518 }
519 519
520 520 }
521 521
522 522
523 523
524 524
525 525
526 526
527 527
528 528
529 529
530 530
531 531
532 532
533 533
534 534
535 535
@@ -1,416 +1,416
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2013, 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 #include <ssd2119.h>
23 23 #include <stdio.h>
24 24 #include <stddef.h>
25 25 #include <core.h>
26 26 #include <math.h>
27 27
28 28 #ifdef __OPTIMIZED_MATH
29 29 #include <optimised_math.h>
30 30 #endif
31 31
32 32 #define _delay_(del) for(volatile int _d_e_l_=0;_d_e_l_<(del);_d_e_l_++);
33 33
34 34 #define ilipaintLine(LCD,X,Y,W,buffer,buffsize) \
35 35 for(int l=0;l<1;l++)\
36 36 {\
37 37 ssd2119setFrame(LCD,X,Y,W,1);\
38 38 int rem=(W)%buffsize;\
39 39 if(rem)LCD->interface.lcd_interface->writeGRAM(buffer,rem);\
40 40 for(int i=rem;i<(W);i+=buffsize)\
41 41 {\
42 42 LCD->interface.lcd_interface->writeGRAM(buffer,buffsize);\
43 43 }\
44 44 }
45 45
46 46 #define ilipaintHLineWithCont(LCD,X,Y,W,ContSz,bufferInt,buffIntsize,bufferCont,buffContsize) \
47 47 for(int l=0;l<1;l++)\
48 48 {\
49 49 ssd2119setFrame(LCD,X,Y,W,1);\
50 50 int rem=(ContSz)%buffContsize;\
51 51 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
52 52 for(int i=rem;i<(ContSz);i+=buffContsize)\
53 53 {\
54 54 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
55 55 }\
56 56 if((2*ContSz)<W) \
57 57 {\
58 58 rem=(W-(2*ContSz))%buffIntsize;\
59 59 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferInt,rem);\
60 60 for(int i=rem;i<(W-(2*ContSz));i+=buffIntsize)\
61 61 {\
62 62 LCD->interface.lcd_interface->writeGRAM(bufferInt,buffIntsize);\
63 63 }\
64 64 }\
65 65 rem=(ContSz)%buffContsize;\
66 66 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
67 67 for(int i=rem;i<(ContSz);i+=buffContsize)\
68 68 {\
69 69 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
70 70 }\
71 71 }\
72 72
73 73
74 74 #define ilipaintVLineWithCont(LCD,X,Y,H,ContSz,bufferInt,buffIntsize,bufferCont,buffContsize) \
75 75 for(int l=0;l<1;l++)\
76 76 {\
77 77 ssd2119setFrame(LCD,X,Y,1,H);\
78 78 int rem=(ContSz)%buffContsize;\
79 79 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
80 80 for(int i=rem;i<(ContSz);i+=buffContsize)\
81 81 {\
82 82 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
83 83 }\
84 84 if((2*ContSz)<H) \
85 85 {\
86 86 rem=(H-(2*ContSz))%buffIntsize;\
87 87 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferInt,rem);\
88 88 for(int i=rem;i<(H-(2*ContSz));i+=buffIntsize)\
89 89 {\
90 90 LCD->interface.lcd_interface->writeGRAM(bufferInt,buffIntsize);\
91 91 }\
92 92 }\
93 93 rem=(ContSz)%buffContsize;\
94 94 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
95 95 for(int i=rem;i<(ContSz);i+=buffContsize)\
96 96 {\
97 97 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
98 98 }\
99 99 }\
100 100
101 101
102 102 #define ilipaintHalfTopVLineWithCont(LCD,X,Y,H,ContSz,bufferInt,buffIntsize,bufferCont,buffContsize) \
103 103 for(int l=0;l<1;l++)\
104 104 {\
105 105 ssd2119setFrame(LCD,X,Y,1,H);\
106 106 int rem=(ContSz)%buffContsize;\
107 107 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
108 108 for(int i=rem;i<(ContSz);i+=buffContsize)\
109 109 {\
110 110 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
111 111 }\
112 112 if(ContSz<H) \
113 113 {\
114 114 rem=(H-ContSz)%buffIntsize;\
115 115 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferInt,rem);\
116 116 for(int i=rem;i<(H-ContSz);i+=buffIntsize)\
117 117 {\
118 118 LCD->interface.lcd_interface->writeGRAM(bufferInt,buffIntsize);\
119 119 }\
120 120 }\
121 121 }\
122 122
123 123
124 124 #define ilipaintHalfBottomVLineWithCont(LCD,X,Y,H,ContSz,bufferInt,buffIntsize,bufferCont,buffContsize) \
125 125 for(int l=0;l<1;l++)\
126 126 {\
127 127 ssd2119setFrame(LCD,X,Y,1,H);\
128 128 int rem;\
129 129 if(ContSz<H) \
130 130 {\
131 131 rem=(H-ContSz)%buffIntsize;\
132 132 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferInt,rem);\
133 133 for(int i=rem;i<(H-ContSz);i+=buffIntsize)\
134 134 {\
135 135 LCD->interface.lcd_interface->writeGRAM(bufferInt,buffIntsize);\
136 136 }\
137 137 }\
138 138 rem=(ContSz)%buffContsize;\
139 139 if(rem)LCD->interface.lcd_interface->writeGRAM(bufferCont,rem);\
140 140 for(int i=rem;i<(ContSz);i+=buffContsize)\
141 141 {\
142 142 LCD->interface.lcd_interface->writeGRAM(bufferCont,buffContsize);\
143 143 }\
144 144 }\
145 145
146 146
147 147 void ssd2119setGRAMaddress(LCD_t* LCD,uint16_t Haddress,uint16_t Vaddress)
148 148 {
149 149 LCD->interface.lcd_interface->writereg(SSD2119_REGISTER_X_RAM_ADDR,Haddress);
150 150 LCD->interface.lcd_interface->writereg(SSD2119_REGISTER_Y_RAM_ADDR,Vaddress);
151 151 }
152 152
153 153 void ssd2119refreshenable(struct LCD_t* LCD,int enable)
154 154 {
155 155 if(enable)
156 156 {
157 157 //LCD->interface->writereg(ssd2119_REGISTER_ENTRYMODE,0x1018);
158 158 }
159 159 else
160 160 {
161 161 //LCD->interface->writereg(ssd2119_REGISTER_ENTRYMODE,0x1008);
162 162
163 163 }
164 164 }
165 165
166 166 void ssd2119setFrame(LCD_t* LCD,uint16_t X,uint16_t Y,uint16_t W,uint16_t H)
167 167 {
168 168 LCD->interface.lcd_interface->writereg(SSD2119_REGISTER_X_RAM_ADDR,(uint32_t)X);
169 169 LCD->interface.lcd_interface->writereg(SSD2119_REGISTER_Y_RAM_ADDR,(uint32_t)Y);
170 170 LCD->interface.lcd_interface->writereg(SSD2119_REGISTER_H_RAM_START,(uint32_t)X);
171 171 LCD->interface.lcd_interface->writereg(SSD2119_REGISTER_H_RAM_END,(uint32_t)(W+X-1));
172 172 LCD->interface.lcd_interface->writereg(SSD2119_REGISTER_V_RAM_POS,(((uint32_t)(Y+H-1))<<8) + (uint32_t)Y);
173 173 }
174 174
175 175 void ssd2119paint(LCD_t* LCD,void* buffer,uint16_t Xpos,uint16_t Ypos,uint16_t Width,uint16_t Height)
176 176 {
177 177 if((LCD!=NULL) && (LCD->interface.lcd_interface!=NULL) && (LCD->interface.lcd_interface->writeGRAM!=NULL) && (LCD->width>(Xpos+Width)) && (LCD->height>(Ypos+Height)))
178 178 {
179 179 ssd2119setFrame(LCD,Xpos,Ypos,Width,Height);
180 180 LCD->interface.lcd_interface->writeGRAM(buffer,Width*Height);
181 181 }
182 182 }
183 183
184 184 void ssd2119paintFilCirc(LCD_t* LCD,uint16_t Xpos,uint16_t Ypos,uint16_t r,uint32_t contColor,uint16_t contSz,uint32_t fillColor)
185 185 {
186 186 //Based on the mid point circle algorithm from Wikipedia
187 187 //http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
188 188 uint16_t innerbuffer[16];
189 189 uint16_t outterbuffer[16];
190 190 for(int i=0;i<16;i++)innerbuffer[i]=fillColor;
191 191 for(int i=0;i<16;i++)outterbuffer[i]=contColor;
192 192 if(contSz<r)
193 193 {
194 194 int error = -r,error_int = -r+contSz;
195 195 int x = r,x_int=r-contSz;
196 196 int y = 0,y_int=0;
197 197 while (x >= y)
198 198 {
199 199 ilipaintHLineWithCont(LCD,(Xpos-x),(Ypos+y),(2*x),(x-x_int),innerbuffer,16,outterbuffer,16);
200 200 ilipaintHLineWithCont(LCD,(Xpos-x),(Ypos-y),(2*x),(x-x_int),innerbuffer,16,outterbuffer,16);
201 201 ilipaintHalfTopVLineWithCont(LCD,(Xpos+y),(Ypos-x),(x),(x-x_int),innerbuffer,16,outterbuffer,16);
202 202 ilipaintHalfTopVLineWithCont(LCD,(Xpos-y),(Ypos-x),(x),(x-x_int),innerbuffer,16,outterbuffer,16);
203 203 ilipaintHalfBottomVLineWithCont(LCD,(Xpos-y),(Ypos),(x),(x-x_int),innerbuffer,16,outterbuffer,16);
204 204 ilipaintHalfBottomVLineWithCont(LCD,(Xpos+y),(Ypos),(x),(x-x_int),innerbuffer,16,outterbuffer,16);
205 205 error += y;
206 206 ++y;
207 207 error += y;
208 208 error_int += y_int;
209 209 ++y_int;
210 210 error_int += y_int;
211 211 if(error >= 0)
212 212 {
213 213 error -= x;
214 214 --x;
215 215 error -= x;
216 216 }
217 217 if(error_int >= 0)
218 218 {
219 219 error_int -= x_int;
220 220 --x_int;
221 221 error_int -= x_int;
222 222 }
223 223 }
224 224
225 225
226 226
227 227 }
228 228
229 229 }
230 230
231 231 void ssd2119paintFilCirc_old(LCD_t* LCD,uint16_t Xpos,uint16_t Ypos,uint16_t r,uint32_t contColor,uint16_t contSz,uint32_t fillColor)
232 232 {
233 233 if(contSz<r)
234 234 {
235 235 uint16_t innerbuffer[16];
236 236 uint16_t outterbuffer[16];
237 237 int32_t rr=(r*r),rr2=((r-contSz)*(r-contSz)),contSz2,Val1,Val2,X1,W,rem;
238 238 for(int i=0;i<16;i++)innerbuffer[i]=fillColor;
239 239 for(int i=0;i<16;i++)outterbuffer[i]=contColor;
240 240 /* Y = b +/- sqrt[r^2 - (x - a)^2] */
241 241 for(int32_t line=-r;line<r;line++)
242 242 {
243 243 #ifdef __OPTIMIZED_MATH
244 Val1 = (uint32_t)optimised_sqrt((float32_t)(rr - (line*line)) );
245 Val2 = (uint32_t)optimised_sqrt((float32_t)(rr2 - (line*line)) );
244 Val1 = (uint32_t)optimised_sqrt((float)(rr - (line*line)) );
245 Val2 = (uint32_t)optimised_sqrt((float)(rr2 - (line*line)) );
246 246 #else
247 247 Val1 = sqrt( (double)(rr - ((line)*(line))) );
248 248 Val2 = sqrt( (double)(rr2 - ((line)*(line))) );
249 249 #endif
250 250 X1=Xpos - Val1;
251 251 contSz2= Val1-Val2;
252 252 ssd2119setFrame(LCD,X1,line+Ypos,2*Val1,1);
253 253 rem=(contSz2)%16;
254 254 if(rem)LCD->interface.lcd_interface->writeGRAM(outterbuffer,rem);
255 255 for(int i=rem;i<(contSz2);i+=16)
256 256 {
257 257 LCD->interface.lcd_interface->writeGRAM(outterbuffer,16);
258 258 }
259 259
260 260 W=2*Val1;
261 261 if(W>(2*contSz2))
262 262 {
263 263 W-=2*contSz2;
264 264 rem=(W)%16;
265 265 if(rem)LCD->interface.lcd_interface->writeGRAM(innerbuffer,rem);
266 266 for(int i=rem;i<(W);i+=16)
267 267 {
268 268 LCD->interface.lcd_interface->writeGRAM(innerbuffer,16);
269 269 }
270 270 }
271 271
272 272 rem=(contSz2)%16;
273 273 if(rem)LCD->interface.lcd_interface->writeGRAM(outterbuffer,rem);
274 274 for(int i=rem;i<(contSz2);i+=16)
275 275 {
276 276 LCD->interface.lcd_interface->writeGRAM(outterbuffer,16);
277 277 }
278 278 }
279 279 }
280 280 }
281 281
282 282
283 283
284 284
285 285 void ssd2119paintFilRect(LCD_t* LCD,uint16_t Xpos,uint16_t Ypos,uint16_t w,uint16_t h,uint32_t contColor,uint16_t contSz,uint32_t fillColor)
286 286 {
287 287 ssd2119setFrame(LCD,Xpos,Ypos,w,h);
288 288 uint16_t tmp[32];
289 289 for(int i=0;i<32;i++)tmp[i]=fillColor;
290 290 for(int i=0;i<(h*w);i+=32)
291 291 {
292 292 LCD->interface.lcd_interface->writeGRAM(tmp,32);
293 293 }
294 294 int rem=(w*h)%32;
295 295 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
296 296 if(contSz)
297 297 {
298 298 ssd2119setFrame(LCD,Xpos,Ypos,w,contSz);
299 299 for(int i=0;i<32;i++)tmp[i]=contColor;
300 300 rem=(w*contSz)%32;
301 301 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
302 302 for(int i=rem;i<(w*contSz);i+=32)
303 303 {
304 304 LCD->interface.lcd_interface->writeGRAM(tmp,32);
305 305 }
306 306
307 307 ssd2119setFrame(LCD,Xpos,Ypos+h-contSz,w,contSz);
308 308 rem=(w*contSz)%32;
309 309 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
310 310 for(int i=rem;i<(w*contSz);i+=32)
311 311 {
312 312 LCD->interface.lcd_interface->writeGRAM(tmp,32);
313 313 }
314 314
315 315 ssd2119setFrame(LCD,Xpos,Ypos,contSz,h);
316 316 rem=(h*contSz)%32;
317 317 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
318 318 for(int i=rem;i<(h*contSz);i+=32)
319 319 {
320 320 LCD->interface.lcd_interface->writeGRAM(tmp,32);
321 321 }
322 322
323 323 ssd2119setFrame(LCD,Xpos+w-contSz,Ypos,contSz,h);
324 324 rem=(h*contSz)%32;
325 325 if(rem)LCD->interface.lcd_interface->writeGRAM(tmp,rem);
326 326 for(int i=rem;i<(h*contSz);i+=32)
327 327 {
328 328 LCD->interface.lcd_interface->writeGRAM(tmp,32);
329 329 }
330 330 }
331 331 }
332 332
333 333 void ssd2119paintText(LCD_t* LCD,char* buffer,uint16_t Xpos,uint16_t Ypos,sFONT* font,uint32_t color)
334 334 {
335 335 int w=font->Width,h=font->Height,bpl=font->bytesPerLine,pix=0,tableoffset=0;
336 336 uint16_t tmp[w];
337 337 uint16_t linenum=0,charnum=0;
338 338 uint8_t line=0;
339 339 while(*buffer!='\0')
340 340 {
341 341 ssd2119setFrame(LCD,Xpos+(charnum*w),Ypos-h,w,1);
342 342 LCD->interface.lcd_interface->readGRAM(tmp,w);
343 343 for(int i=0;i<(h*w);i++)
344 344 {
345 345 if( ((i%w)==0) ) //read current line to apply text pixmap
346 346 {
347 347 if(linenum++>0)
348 348 {
349 349 ssd2119setFrame(LCD,Xpos+(charnum*(w-10)),Ypos + linenum -h,w,1); // TODO remove -10 and generate good fonts!
350 350 LCD->interface.lcd_interface->writeGRAM(tmp,w);
351 351 ssd2119setFrame(LCD,Xpos+(charnum*(w-10)),Ypos + linenum + 1-h,w,1);
352 352 LCD->interface.lcd_interface->readGRAM(tmp,w);
353 353 pix=0;
354 354 }
355 355 }
356 356 if((pix%8) == 0)
357 357 {
358 358 line=font->table[(((*buffer)-32)*h*bpl)+tableoffset++];
359 359 }
360 360 if((line & (uint8_t)0x01)==(uint8_t)1)tmp[pix]=(uint16_t)color;
361 361 pix++;
362 362 line>>=1;
363 363 }
364 364 linenum=0;
365 365 tableoffset=0;
366 366 charnum++;
367 367 buffer++;
368 368 }
369 369 }
370 370
371 371
372 372 int ssd2119init(struct LCD_t* LCD)
373 373 {
374 374 if((LCD!=NULL) && (LCD->interface.lcd_interface!=NULL) && (LCD->interface.lcd_interface->writereg!=NULL))
375 375 {
376 376 LCD->interface.lcd_interface->writereg(0x28,0x0006); // VCOM OTP - Page 55-56 of SSD2119 datasheet
377 377 LCD->interface.lcd_interface->writereg(0x00,0x0001); // start Oscillator - Page 36 of SSD2119 datasheet
378 378 LCD->interface.lcd_interface->writereg(0x10,0x0000); // Sleep mode - Page 49 of SSD2119 datasheet
379 379 LCD->interface.lcd_interface->writereg(0x01,0x72ef); // Driver Output Control - Page 36-39 of SSD2119 datasheet
380 380 LCD->interface.lcd_interface->writereg(0x02,0x0600); // LCD Driving Waveform Control - Page 40-42 of SSD2119 datasheet
381 381 LCD->interface.lcd_interface->writereg(0x03,0x6a38); // Power Control 1 - Page 43-44 of SSD2119 datasheet
382 382 LCD->interface.lcd_interface->writereg(0x11,0x6870); // Entry Mode - Page 50-52 of SSD2119 datasheet
383 383
384 384 // RAM WRITE DATA MASK
385 385 LCD->interface.lcd_interface->writereg(0x0f,0x0000); // Gate Scan Position - Page 49 of SSD2119 datasheet
386 386 // RAM WRITE DATA MASK
387 387 LCD->interface.lcd_interface->writereg(0x0b,0x5308); // Frame Cycle Control - Page 45 of SSD2119 datasheet
388 388 LCD->interface.lcd_interface->writereg(0x0c,0x0003); // Power Control 2 - Page 47 of SSD2119 datasheet
389 389 LCD->interface.lcd_interface->writereg(0x0d,0x000a); // Power Control 3 - Page 48 of SSD2119 datasheet
390 390 LCD->interface.lcd_interface->writereg(0x0e,0x2e00); // Power Control 4 - Page 48 of SSD2119 datasheet
391 391 LCD->interface.lcd_interface->writereg(0x1e,0x00be); // Power Control 5 - Page 53 of SSD2119 datasheet
392 392 LCD->interface.lcd_interface->writereg(0x25,0x8000); // Frame Frequency Control - Page 53 of SSD2119 datasheet
393 393 LCD->interface.lcd_interface->writereg(0x26,0x7800); // Analog setting - Page 54 of SSD2119 datasheet
394 394 LCD->interface.lcd_interface->writereg(0x4e,0x0000); // Ram Address Set - Page 58 of SSD2119 datasheet
395 395 LCD->interface.lcd_interface->writereg(0x4f,0x0000); // Ram Address Set - Page 58 of SSD2119 datasheet
396 396 LCD->interface.lcd_interface->writereg(0x12,0x08d9); // Sleep mode - Page 49 of SSD2119 datasheet
397 397
398 398 // -----------------Adjust the Gamma Curve----//
399 399 LCD->interface.lcd_interface->writereg(0x30,0x0000); //0007
400 400 LCD->interface.lcd_interface->writereg(0x31,0x0104); //0203
401 401 LCD->interface.lcd_interface->writereg(0x32,0x0100); //0001
402 402 LCD->interface.lcd_interface->writereg(0x33,0x0305); //0007
403 403 LCD->interface.lcd_interface->writereg(0x34,0x0505); //0007
404 404 LCD->interface.lcd_interface->writereg(0x35,0x0305); //0407
405 405 LCD->interface.lcd_interface->writereg(0x36,0x0707); //0407
406 406 LCD->interface.lcd_interface->writereg(0x37,0x0300); //0607
407 407 LCD->interface.lcd_interface->writereg(0x3a,0x1200); //0106
408 408 LCD->interface.lcd_interface->writereg(0x3b,0x0800);
409 409 LCD->interface.lcd_interface->writereg(0x07,0x0033); // Display Control - Page 45 of SSD2119 datasheet
410 410 }
411 411 return 0;
412 412 }
413 413
414 414
415 415
416 416
@@ -1,126 +1,132
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@member.fsf.org
21 21 -------------------------------------------------------------------------------*/
22 22 #include <core.h>
23 23 #include <stdint.h>
24 24 #include <stdlib.h>
25 25 #include <stdio.h>
26 26 #include <gpio.h>
27 27 #include <unistd.h>
28 28
29 29 extern uint32_t OSC0;
30 30 extern uint32_t INTOSC;
31 31 extern uint32_t RTCOSC;
32 32
33 33 volatile uint32_t tickCounter=0;
34 34
35 35
36 36 void SysTick_Handler(void)
37 37 {
38 38 tickCounter+=1;
39 39 // if((tickCounter&0xFFF)==0x800)
40 40 // gpiosetval(PC15,!gpiogetval(PC15));
41 41 }
42 42
43 43 void delay_us(uint32_t value)
44 44 {
45 45 usleep(value);
46 46 }
47 47
48 48 void delay_100us(uint32_t value)
49 49 {
50 50 usleep(value*100);
51 51 }
52 52
53 53 uint32_t getAPB1Freq()
54 54 {
55 55 return 0;
56 56 }
57 57
58 58 uint32_t getAPB2Freq()
59 59 {
60 60 return 0;
61 61 }
62 62
63 63
64 64 uint32_t getCpuFreq()
65 65 {
66 66 return 0;
67 67 }
68 68
69 69 void reset_AHB1()
70 70 {
71 71 }
72 72
73 73 void reset_AHB2()
74 74 {
75 75
76 76 }
77 77
78 78 void reset_APB1()
79 79 {
80 80
81 81 }
82 82
83 83 void reset_APB2()
84 84 {
85 85
86 86 }
87 87
88 88
89 89
90 90
91 91
92 92 int optimizePLLcfg(uint32_t freq, uint32_t srcfreq,uint32_t PLLM,uint32_t* PLLP, uint32_t* PLLN,uint8_t* AHBPRindx)
93 93 {
94 94
95 95 return 1;
96 96 }
97 97
98 98
99 99 int setPll(uint32_t freq)
100 100 {
101 101
102 102 return freq;
103 103 }
104 104
105 105 void configureSysTick()
106 106 {
107 107
108 108 }
109 109
110 110 int setCpuFreq(uint32_t freq)
111 111 {
112 112
113 113 return 0;
114 114 }
115 115
116 116
117 117 void enable_FPU()
118 118 {
119 119
120 120 }
121 121
122 void getCpuUUID(char*uuid)
123 {
124 }
125
126 int getCpuUUIDLen()
127 {
128 return 0;
129 }
122 130
123 131
124 132
125
126
@@ -1,379 +1,391
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@member.fsf.org
21 21 -------------------------------------------------------------------------------*/
22 22 #include <core.h>
23 23 #include <stm32f4xx_rcc.h>
24 24 #include <stdlib.h>
25 25 #include <stdio.h>
26 26 #include <core_cm4.h>
27 27 #include <gpio.h>
28 28
29 29
30 30 extern uint32_t OSC0;
31 31 extern uint32_t INTOSC;
32 32 extern uint32_t RTCOSC;
33 33 extern gpio_t TickLed;
34 34
35 35 volatile uint32_t tickCounter=0;
36 36
37 37
38 38 void SysTick_Handler(void)
39 39 {
40 40 tickCounter+=1;
41 41 if((tickCounter&0xFFF)==0x800)
42 42 gpiosetval(TickLed,!gpiogetval(TickLed));
43 43 }
44 44
45 45 void delay_us(uint32_t value)
46 46 {
47 47 extern uint32_t currentCpuFreq;
48 48 if(value)
49 49 {
50 50 uint32_t tickperus=currentCpuFreq/(1000*10);
51 51 uint32_t tickCounterSnap = SysTick->VAL+((value%100)*tickperus);
52 52 uint32_t targetVal=tickCounterSnap +(value/100);
53 53 if(targetVal < tickCounterSnap)
54 54 {
55 55 while(tickCounter > targetVal);
56 56 }
57 57 while((tickCounter < targetVal) | (SysTick->VAL<tickCounterSnap));
58 58 }
59 59 }
60 60
61 61 void delay_100us(uint32_t value)
62 62 {
63 63 if(value)
64 64 {
65 65 uint32_t tickCounterSnap = tickCounter;
66 66 uint32_t SysTickSnap = SysTick->VAL;
67 67 uint32_t targetVal=tickCounterSnap +(value);
68 68 if(targetVal < tickCounterSnap)
69 69 {
70 70 while(tickCounter > targetVal);
71 71 }
72 72 while((tickCounter < targetVal) | (SysTick->VAL<SysTickSnap));
73 73 }
74 74 }
75 75
76 76 uint32_t getAPB1Freq()
77 77 {
78 78 RCC_ClocksTypeDef RCC_ClocksStatus;
79 79 RCC_GetClocksFreq(&RCC_ClocksStatus);
80 80 return RCC_ClocksStatus.PCLK1_Frequency;
81 81 }
82 82
83 83 uint32_t getAPB2Freq()
84 84 {
85 85 RCC_ClocksTypeDef RCC_ClocksStatus;
86 86 RCC_GetClocksFreq(&RCC_ClocksStatus);
87 87 return RCC_ClocksStatus.PCLK2_Frequency;
88 88 }
89 89
90 90
91 91 uint32_t getCpuFreq()
92 92 {
93 93 uint32_t _freq_ = OSC0;
94 94 uint32_t PLLN,PLLM,PLLP;
95 95
96 96 if((RCC->CFGR & 0xC) == 8) //PLL used as sys clk
97 97 {
98 98 uint32_t _pllin_=INTOSC;
99 99 if((RCC->PLLCFGR & (1<<22)) == (1<<22))
100 100 {
101 101 _pllin_=OSC0;
102 102 }
103 103 PLLN = (RCC->PLLCFGR>>6) & 0x1FF;
104 104 PLLM = RCC->PLLCFGR & 0x3F;
105 105 PLLP = 1<<(((RCC->PLLCFGR>>16) & 3 )+1);
106 106 _freq_ = (_pllin_ * PLLN )/(PLLM*PLLP);
107 107 }
108 108 else if((RCC->CFGR & 0xC) == 0) //HSI used as sys clk
109 109 {
110 110 _freq_=INTOSC;
111 111 }
112 112 if((RCC->CFGR & (1<<7))==(1<<7))
113 113 {
114 114 return _freq_>>((RCC->CFGR & (7<<4))>>4);
115 115 }
116 116 return _freq_;
117 117 }
118 118
119 119 void reset_AHB1()
120 120 {
121 121 RCC->AHB1RSTR = -1;
122 122 RCC->AHB1RSTR = 0;
123 123 }
124 124
125 125 void reset_AHB2()
126 126 {
127 127 RCC->AHB2RSTR = -1;
128 128 RCC->AHB2RSTR = 0;
129 129 }
130 130
131 131 void reset_APB1()
132 132 {
133 133 RCC->APB1RSTR = -1;
134 134 RCC->APB1RSTR = 0;
135 135 }
136 136
137 137 void reset_APB2()
138 138 {
139 139 RCC->APB2RSTR = -1;
140 140 RCC->APB2RSTR = 0;
141 141 }
142 142
143 143
144 144
145 145 /*
146 146 | 2.7->3.6V
147 147 -------------------------
148 148 0WS | 0<HCLK<=30
149 149 1WS | 30<HCLK<=60
150 150 2WS | 60<HCLK<=90
151 151 3WS | 90<HCLK<=120
152 152 4WS | 120<HCLK<=150
153 153 5WS | 150<HCLK<=168
154 154
155 155 f(VCO clock) = f(PLL clock input) Γ— (PLLN / PLLM) [1]
156 156 64MHz <= f(VCO clock) <= 432MHz [2]
157 157
158 158 f(VCO clock input) must be between 1MHz and 2MHz and as close to 2MHz as possible!! [3]
159 159
160 160 f(PLL general clock output) = f(VCO clock) / PLLP [4]
161 161
162 162 CPU<168MHz AHB1<168MHz AHB2<168MHz APB1<42MHz APB2<84MHz [5]
163 163
164 164 ! 63<=PLLN<=432 [6]
165 165 ! 2<=PLLM<=63 [7]
166 166 ! PLLP=2,4,6,8 [8]
167 167 4<=PLLM*PLLP<=504
168 168
169 169 F= f(PLL clock input) * A/B with
170 170 63<=A<=432
171 171 4<=B<=504
172 172
173 173 */
174 174
175 175
176 176 int optimizePLLcfg(uint32_t freq, uint32_t srcfreq,uint32_t PLLM,uint32_t* PLLP, uint32_t* PLLN,uint8_t* AHBPRindx)
177 177 {
178 178 uint32_t AHBPRtbl[9]={1,2,4,8,16,64,128,256,512};
179 179 uint32_t AHBPR=0,AHBPR_r=0,PLLN_r=0,PLLP_r=0;
180 180 uint32_t Fplli=0;
181 181 int32_t f_error=100000000;
182 182 int32_t f_errornw=100000000;
183 183 Fplli = srcfreq / PLLM;
184 184 //not efficient but should find the best parameters
185 185 for((*AHBPRindx)=0;(*AHBPRindx)<9;(*AHBPRindx)++) //lowest priority
186 186 {
187 187 AHBPR = AHBPRtbl[(*AHBPRindx)];
188 188 for(*PLLP=2;*PLLP<9;*PLLP+=2)
189 189 {
190 190 *PLLN = (freq*(*PLLP)*AHBPR)/Fplli;
191 191 if(((*PLLN)>62) && ((*PLLN)<433) && ((Fplli * (*PLLN)) < 433000000))
192 192 {
193 193 f_errornw = abs((int32_t)((int32_t)((Fplli*(*PLLN))/((*PLLP)*AHBPR))-freq));
194 194 if( ( (f_error)>(f_errornw) ) || ( (*AHBPRindx==0)&&(*PLLP==2)&&(*PLLN==63) ) )
195 195 {
196 196 f_error=f_errornw;
197 197 PLLN_r = *PLLN;
198 198 PLLP_r = *PLLP;
199 199 AHBPR_r=*AHBPRindx;
200 200 if(f_error==0)
201 201 {
202 202 *PLLN = PLLN_r;
203 203 *PLLP = PLLP_r;
204 204 *AHBPRindx = AHBPR_r;
205 205 return 1;
206 206 }
207 207 }
208 208 }
209 209 }
210 210 }
211 211 *PLLN = PLLN_r;
212 212 *PLLP = PLLP_r;
213 213 *AHBPRindx = AHBPR_r;
214 214 return 1;
215 215 }
216 216
217 217
218 218 int setPll(uint32_t freq)
219 219 {
220 220 extern uint32_t OSC0;
221 221 extern uint32_t INTOSC;
222 222 uint32_t srcfreq = INTOSC;
223 223 uint8_t AHBPRindx;
224 224 uint32_t AHBPRtbl[9]={1,2,4,8,16,64,128,256,512};
225 225 uint32_t PLLN=0,PLLM=0,PLLP=0,AHBPR=0;
226 226 uint32_t Fplli=0;
227 227 if((RCC->PLLCFGR & (1<<22))==(1<<22))
228 228 {
229 229 srcfreq = OSC0;
230 230 }
231 231 PLLM = srcfreq / 1500000; // [3]
232 232 Fplli = srcfreq / PLLM;
233 233 optimizePLLcfg(freq,srcfreq,PLLM,&PLLP,&PLLN,&AHBPRindx);
234 234 srcfreq = (Fplli*PLLN)/(PLLP*AHBPRtbl[AHBPRindx]); //Put real clk freq in srcfreq for return value
235 235 //now switch to HSIs
236 236 if((RCC->CR & 1)==0)RCC->CR |= 1; //turn ON HSI
237 237 while((RCC->CR & 2)!=2); //wait for HSI Ready
238 238 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
239 239 RCC->CFGR |= RCC_CFGR_SW_HSI; //set HSI as main clk
240 240 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_HSI);
241 241 RCC->CR &= ~(1<<24); //Turn OFF PLL
242 242 RCC->PLLCFGR &= ~0x37FFF; //clear PLLP PLLM PLLN
243 243 RCC->PLLCFGR |= PLLM + (PLLN<<6) + (((PLLP>>1) -1)<<16);
244 244 RCC->CR |= RCC_CR_PLLON; //Turn ON PLL
245 245 while((RCC->CR & (1<<25))!=(1<<25)); //wait for PLL Ready
246 246 if(AHBPRindx!=0)AHBPRindx|=0x8;
247 247 RCC->CFGR &= ~(0xF<<4);
248 248 RCC->CFGR |= (uint32_t)(AHBPRindx<<4);
249 249 AHBPR=0;
250 250 while((srcfreq>>AHBPR)>42000000)AHBPR++; //[5] //Thune APB1 prescaler to keep APB1 CLK below 42MHz
251 251 if(AHBPR!=0)
252 252 {
253 253 AHBPR-=1;
254 254 AHBPR|=0x4;
255 255 }
256 256 RCC->CFGR &= ~(0x7<<10);
257 257 RCC->CFGR |= (uint32_t)(AHBPR<<10);
258 258 AHBPR=0;
259 259 while((srcfreq>>AHBPR)>84000000)AHBPR++; //[5] //Thune APB2 prescaler to keep APB2 CLK below 42MHz
260 260 if(AHBPR!=0)
261 261 {
262 262 AHBPR-=1;
263 263 AHBPR|=0x4;
264 264 }
265 265 RCC->CFGR &= ~(0x7<<13);
266 266 RCC->CFGR |= (uint32_t)(AHBPR<<13);
267 267 FLASH->ACR |= FLASH_ACR_LATENCY_7WS;
268 268 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));//Switch to PLL as main clk source
269 269 RCC->CFGR |= RCC_CFGR_SW_PLL;
270 270 /* Wait untill the main PLL is used as system clock source */
271 271 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
272 272 if(srcfreq>150000000)
273 273 {
274 274 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_5WS;
275 275 }
276 276 if((srcfreq<150000000) && (srcfreq>=120000000))
277 277 {
278 278 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_4WS;
279 279 }
280 280 if((srcfreq<120000000) && (srcfreq>=90000000))
281 281 {
282 282 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_3WS;
283 283 }
284 284 if((srcfreq<90000000) && (srcfreq>=60000000))
285 285 {
286 286 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_2WS;
287 287 }
288 288 if((srcfreq<60000000) && (srcfreq>=30000000))
289 289 {
290 290 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_1WS;
291 291 }
292 292 if(srcfreq<30000000)
293 293 {
294 294 FLASH->ACR &= (~7)|FLASH_ACR_LATENCY_0WS;
295 295 }
296 296 return srcfreq;
297 297 }
298 298
299 299 void configureSysTick()
300 300 {
301 301 extern uint32_t currentCpuFreq;
302 302 uint32_t us=currentCpuFreq/(1000*10);
303 303 SysTick_Config(us);
304 304 }
305 305
306 306 int setCpuFreq(uint32_t freq)
307 307 {
308 308 extern uint32_t OSC0;
309 309 extern uint32_t INTOSC;
310 310 uint8_t i=0;
311 311 uint32_t curentFeq = getCpuFreq();
312 312 if(curentFeq==freq)return curentFeq;
313 313 if((freq>2000000) && (freq<=250000000)) //be carefull with 250MHz!!!
314 314 {
315 315 if((RCC->CFGR & 0xC) == 8) //PLL used as sys clk
316 316 {
317 317 return setPll(freq);
318 318 }
319 319 else if((RCC->CFGR & 0xC) == 0) //HSI used as sys clk
320 320 {
321 321 if((INTOSC%freq)==0) //now check if we can directly divide HSI
322 322 {
323 323 if(freq==INTOSC)
324 324 {
325 325 RCC->CFGR &= ~(0xF<<4);
326 326 return freq;
327 327 }
328 328 for(i=1;i<8;i++)
329 329 {
330 330 if((freq<<i)==INTOSC)
331 331 {
332 332 RCC->CFGR &= ~(0xF<<4);
333 333 RCC->CFGR |= ((0x8|i)<<4);
334 334 return freq;
335 335 }
336 336 }
337 337 }
338 338 else
339 339 return setPll(freq);
340 340 }
341 341 else //HSE used as sys clk
342 342 {
343 343 if((OSC0%freq)==0) //now check if we can directly divide HSI
344 344 {
345 345 if(freq==OSC0)
346 346 {
347 347 RCC->CFGR &= ~(0xF<<4);
348 348 return freq;
349 349 }
350 350 for(i=1;i<8;i++)
351 351 {
352 352 if((freq<<i)==OSC0)
353 353 {
354 354 RCC->CFGR &= ~(0xF<<4);
355 355 RCC->CFGR |= ((0x8|i)<<4);
356 356 return freq;
357 357 }
358 358 }
359 359 }
360 360 else
361 361 return setPll(freq);
362 362 }
363 363 }
364 364 return 0;
365 365 }
366 366
367 367
368 368 void enable_FPU()
369 369 {
370 370 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
371 371 __asm__("dsb");
372 372 __asm__("isb");
373 373 }
374 374
375 375
376 376
377 377
378 378
379 void getCpuUUID(char*uuid)
380 {
381 volatile char* uuidreg = (char*)0x1FFF7A10;
382 for(int i=0;i<12;i++)
383 {
384 uuid[i]=uuidreg[i];
385 }
386 }
379 387
388 int getCpuUUIDLen()
389 {
390 return 96/8;
391 }
@@ -1,30 +1,29
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 OPTIMISED_MATH_H
23 23 #define OPTIMISED_MATH_H
24 #include <arm_math.h>
25 24
26 extern float32_t optimised_sqrt(float32_t value);
25 extern float optimised_sqrt(float value);
27 26
28 extern float optimised_sin(float32_t x);
27 extern float optimised_sin(float x);
29 28
30 29 #endif
@@ -1,22 +1,24
1 1 #include <optimised_math.h>
2 #include <arm_math.h>
3
2 4 float32_t optimised_sqrt(float32_t value)
3 5 {
4 6 float32_t out;
5 7 arm_sqrt_f32(value,&out);
6 8 return out;
7 9 }
8 10
9 11 float optimised_sin(float32_t x)
10 12 {
11 13 return arm_sin_f32(x);
12 14 }
13 15
14 16
15 17
16 18
17 19
18 20
19 21
20 22
21 23
22 24
@@ -1,140 +1,139
1 1 #include <stdio.h>
2 2 #include <timer.h>
3 3 #include <stm32f4xx_rcc.h>
4 4 #include <stm32f4xx_tim.h>
5 5 #include <stm32f4xx_dac.h>
6 6 #include <stm32f4xx_gpio.h>
7 7 #include <stm32f4xx_dma.h>
8 8 #include <stm32f4xx.h>
9 9 #include <gpio.h>
10 10 #include <stm32f4xx.h>
11 11 #include <dac.h>
12 12
13 13
14 14
15 15 const TIM_TypeDef* _timer_dev_table[14]={TIM1,TIM2,TIM3,TIM4,TIM5,TIM6,TIM7,
16 16 TIM8,TIM9,TIM10,TIM11,TIM12,TIM13,TIM14};
17 17
18 18
19 19
20 20 int dacopen(uint32_t pin)
21 21 {
22 22 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
23 23 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
24 24 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE);
25 25 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
26 26 gpio_t DACOUT;
27 27 DACOUT = gpioopen(pin);
28 DACOUT |= gpioan | gpionopulltype;
29 gpiosetconfig(&DACOUT);
28 gpiosetconfig(DACOUT, gpioan, gpiolowspeed, gpiopushpulltype, gpionopulltype);
30 29 return 0;
31 30 }
32 31
33 32 int dacsetconfig(int timer, int buffer_size, int dac, uint32_t dac_type, uint32_t samplingFreq, void *data)
34 33 {
35 34 //#include <core.h>
36 35 DAC_InitTypeDef DAC_InitStructure;
37 36 // TIM_TypeDef* tim = (TIM_TypeDef*)_timer_dev_table[timer];
38 37 TIM_TypeDef* tim = TIM6;
39 38 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
40 39 DMA_InitTypeDef DMA_InitStructure;
41 40 if(timer>=timer2 && timer <= timer7)RCC_APB1PeriphClockCmd((1<<(timer-1)), ENABLE);
42 41 if(timer==timer8)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
43 42 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
44 43 unsigned int __inputFreq__ = 80000000;//getCpuFreq()/2;
45 44 unsigned int period = __inputFreq__/samplingFreq - 1;
46 45 unsigned int PrescalerValue=0;
47 46 // while (period>=0x0FFFF)
48 47 // {
49 48 // PrescalerValue++;
50 49 // __inputFreq__ = getCpuFreq()/(2*(PrescalerValue+1));
51 50 // period = (__inputFreq__/samplingFreq) - 1;
52 51 // }
53 52
54 53 printf("period = %d \n\r prescaler = 0x%x 0x%x\n\r",period,data,__get_MSP());
55 54 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
56 55 TIM_TimeBaseStructure.TIM_Period = 0xff;
57 56 TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)0;
58 57 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
59 58 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
60 59 TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
61 60 TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
62 61 // TIM_ARRPreloadConfig(tim, ENABLE);
63 62 TIM_Cmd(TIM6, ENABLE);
64 63
65 64 // if (timer == timer2)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
66 65 // if (timer == timer4)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T4_TRGO;
67 66 // if (timer == timer5)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T5_TRGO;
68 67 // if (timer == timer6)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
69 68 // if (timer == timer7)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T7_TRGO;
70 69 // if (timer == timer8)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T8_TRGO;
71 70 DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
72 71
73 72 DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
74 73 DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
75 74 if (dac == DAC1){
76 75 printf("DAC1 Init ");
77 76 DAC_Init(DAC_Channel_1, &DAC_InitStructure);
78 77 DMA_DeInit(DMA1_Stream5);
79 78 }
80 79
81 80 if (dac == DAC2){
82 81 printf("DAC2 Init ");
83 82 DAC_Init(DAC_Channel_2, &DAC_InitStructure);
84 83 DMA_DeInit(DMA1_Stream6);
85 84 }
86 85 if (dac == DAC1){
87 86 DMA_DeInit(DMA1_Stream5);
88 87 DMA_Cmd(DMA1_Stream5, DISABLE);
89 88 }
90 89 if (dac == DAC2){
91 90 DMA_DeInit(DMA1_Stream6);
92 91 DMA_Cmd(DMA1_Stream6, DISABLE);
93 92 }
94 93 DMA_InitStructure.DMA_Channel = DMA_Channel_7;
95 94 // DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)dac_type;
96 95 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)DAC1_12bit_right_align;
97 96 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)data;
98 97 DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
99 98 DMA_InitStructure.DMA_BufferSize = buffer_size;
100 99 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
101 100 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
102 101 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
103 102 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
104 103 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
105 104 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
106 105 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
107 106 DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
108 107 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
109 108 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
110 109 DMA_Init(DMA1_Stream5, &DMA_InitStructure);
111 110 /* Enable DMA1_Stream6 */
112 111 DMA_Cmd(DMA1_Stream5, ENABLE);
113 112
114 113 /* Enable DAC Channel2 */
115 114 DAC_Cmd(DAC_Channel_1, ENABLE);
116 115
117 116 /* Enable DMA for DAC Channel2 */
118 117 DAC_DMACmd(DAC_Channel_1, ENABLE);
119 118
120 119 // if (dac == DAC1){
121 120 // printf("DAC1 DMA Init");
122 121 // DMA_Init(DMA1_Stream5, &DMA_InitStructure);
123 122 // DMA_Cmd(DMA1_Stream5, ENABLE);
124 123 // DAC_Cmd(DAC_Channel_1, ENABLE);
125 124 // DAC_DMACmd(DAC_Channel_1, ENABLE);
126 125
127 126 // }
128 127 // if (dac == DAC2){
129 128 // printf("DAC2 DMA Init");
130 129 // DMA_Init(DMA1_Stream6, &DMA_InitStructure);
131 130 // DMA_Cmd(DMA1_Stream6, ENABLE);
132 131 // DAC_Cmd(DAC_Channel_2, ENABLE);
133 132 // DAC_DMACmd(DAC_Channel_2, ENABLE);
134 133
135 134 // }
136 135 return 0;
137 136
138 137 }
139 138
140 139
@@ -1,392 +1,262
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 #include <gpio.h>
23 23 #include <stm32f4xx_gpio.h>
24 24 #include <stm32f4xx_rcc.h>
25 25
26 26 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
27 27 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
28 28
29 29
30 30 gpio_t gpioopen(uint32_t gpio)
31 31 {
32 gpio &= -1^GPIOSPEEDMASK;
33 gpio |= gpiolowspeed;
34 gpio &= -1^GPIODIRMASK;
35 gpio |= gpioindir;
36 gpio &= -1^GPIOOUTTYPEMASK;
37 gpio |= gpiopushpulltype;
38 gpio &= -1^GPIOPULLTYPEMASK;
39 gpio |= gpionopulltype;
40 gpiosetconfig(&gpio);
41 RCC_AHB1PeriphClockCmd(((uint32_t)0x00000001<<GPIOPORTNUM(gpio)), ENABLE);
42 return gpio;
32 gpiosetconfig(gpio,gpioindir,gpiolowspeed,gpiopulluptype,gpionopulltype);
33 RCC_AHB1PeriphClockCmd(((uint32_t)0x00000001<<GPIOPORTNUM(gpio)), ENABLE);
34 return gpio;
43 35 }
44 36
37 gpio_t gpioopenandconfig(uint32_t gpio, gpioconfig_str *config)
38 {
39 gpiosetconfig(gpio, config->direction, config->speed, config->outType, config->pullType);
40 RCC_AHB1PeriphClockCmd(((uint32_t)0x00000001<<GPIOPORTNUM(gpio)), ENABLE);
41 return gpio;
42 }
45 43
46 44 void gpioclose(gpio_t gpio)
47 45 {
48 46 }
49 47
50 void gpiosetconfig(gpio_t* gpio)
48 void gpiosetconfig(gpio_t gpio, gpiodir_t direction, gpiospeed_t speed, gpioouttype_t outType, gpiopulltype_t pullType)
51 49 {
52 gpiosetdir(gpio, (*gpio & GPIODIRMASK));
53 gpiosetspeed(gpio, (*gpio & GPIOSPEEDMASK));
54 gpiosetouttype(gpio, (*gpio & GPIOOUTTYPEMASK));
55 gpiosetpulltype(gpio, (*gpio & GPIOPULLTYPEMASK));
50 gpiosetdir(gpio, direction);
51 gpiosetspeed(gpio, speed);
52 gpiosetouttype(gpio, outType);
53 gpiosetpulltype(gpio, pullType);
54 }
55
56 void gpiosetconfigstr(gpio_t gpio, gpioconfig_str* config)
57 {
58 gpiosetconfig(gpio, config->direction, config->speed, config->outType, config->pullType);
56 59 }
57 60
58 extern void gpiosetspeed(gpio_t* gpio,gpiospeed_t speed)
61 extern void gpiosetspeed(gpio_t gpio,gpiospeed_t speed)
59 62 {
60 GPIO_TypeDef* GPIOx = GPIOGETPORT((*gpio));
61 switch(speed)
62 {
63 case gpiolowspeed :
64 if((*gpio & 0xFF)==0xFF)
65 {
66 for(int i=0;i<16;i++)
67 {
68 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2));
69 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_2MHz) << (i * 2));
70 }
71 }
72 else
73 {
74 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((*gpio & 0xFF) * 2));
75 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_2MHz) << ((*gpio & 0xFF) * 2));
76 }
77 *gpio &= ~GPIOSPEEDMASK;
78 *gpio |= gpiolowspeed;
79 break;
80 case gpiomediumspeed :
81 if((*gpio & 0xFF)==0xFF)
82 {
83 for(int i=0;i<16;i++)
84 {
85 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2));
86 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_25MHz) << (i * 2));
87 }
88 }
89 else
90 {
91 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((*gpio & 0xFF) * 2));
92 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_25MHz) << ((*gpio & 0xFF) * 2));
93 }
94 *gpio &= ~GPIOSPEEDMASK;
95 *gpio |= gpiomediumspeed;
96 break;
97 case gpiofastspeed :
98 if((*gpio & 0xFF)==0xFF)
99 {
100 for(int i=0;i<16;i++)
101 {
102 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2));
103 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_50MHz) << (i * 2));
104 }
105 }
106 else
107 {
108 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((*gpio & 0xFF) * 2));
109 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_50MHz) << ((*gpio & 0xFF) * 2));
110 }
111 *gpio &= ~GPIOSPEEDMASK;
112 *gpio |= gpiofastspeed;
113 break;
114 case gpiohighspeed :
115 if((*gpio & 0xFF)==0xFF)
116 {
117 for(int i=0;i<16;i++)
118 {
119 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2));
120 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_100MHz) << (i * 2));
121 }
122 }
123 else
124 {
125 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((*gpio & 0xFF) * 2));
126 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_100MHz) << ((*gpio & 0xFF) * 2));
127 }
128 *gpio &= ~GPIOSPEEDMASK;
129 *gpio |= gpiohighspeed;
130 break;
131 }
63 GPIO_TypeDef* GPIOx = GPIOGETPORT((gpio));
64 uint32_t speedMask = GPIO_Speed_2MHz;
65 switch(speed)
66 {
67 case gpiolowspeed :
68 speedMask = GPIO_Speed_2MHz;
69 break;
70 case gpiomediumspeed :
71 speedMask = GPIO_Speed_25MHz;
72 break;
73 case gpiofastspeed :
74 speedMask = GPIO_Speed_50MHz;
75 break;
76 case gpiohighspeed :
77 speedMask = GPIO_Speed_100MHz;
78 break;
79 }
80 if((gpio & 0xFF)==0xFF)
81 {
82 for(int i=0;i<16;i++)
83 {
84 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2));
85 GPIOx->OSPEEDR |= ((speedMask) << (i * 2));
86 }
87 }
88 else
89 {
90 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((gpio & 0xFF) * 2));
91 GPIOx->OSPEEDR |= ((speedMask) << ((gpio & 0xFF) * 2));
92 }
132 93 }
133 94
134 95
135 void gpiosetdir(gpio_t* gpio,gpiodir_t dir)
96 void gpiosetdir(gpio_t gpio, gpiodir_t dir)
136 97 {
137 GPIO_TypeDef* GPIOx = GPIOGETPORT((*gpio));
138 switch(dir)
139 {
140 case gpiooutdir:
141 if((*gpio & 0xFF)==0xFF)
142 {
143 for(int i=0;i<16;i++)
144 {
145 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2));
146 GPIOx->MODER |= (GPIO_Mode_OUT << (i * 2));
147 }
148 }
149 else
150 {
151 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((*gpio & 0xFF) * 2));
152 GPIOx->MODER |= (GPIO_Mode_OUT << ((*gpio & 0xFF) * 2));
153 }
154 *gpio &= ~GPIODIRMASK;
155 *gpio |= gpiooutdir;
156 break;
157 case gpioaf:
158 if((*gpio & 0xFF)==0xFF)
159 {
160 for(int i=0;i<16;i++)
161 {
162 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2));
163 GPIOx->MODER |= (GPIO_Mode_AF << (i * 2));
164 }
165 }
166 else
167 {
168 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((*gpio & 0xFF) * 2));
169 GPIOx->MODER |= (GPIO_Mode_AF << ((*gpio & 0xFF) * 2));
170 }
171 *gpio &= ~GPIODIRMASK;
172 *gpio |= gpioaf;
173 break;
174 case gpioan:
175 if((*gpio & 0xFF)==0xFF)
176 {
177 for(int i=0;i<16;i++)
178 {
179 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2));
180 GPIOx->MODER |= (GPIO_Mode_AN << (i * 2));
181 }
182 }
183 else
184 {
185 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((*gpio & 0xFF) * 2));
186 GPIOx->MODER |= (GPIO_Mode_AN << ((*gpio & 0xFF) * 2));
187 }
188 *gpio &= ~GPIODIRMASK;
189 *gpio |= gpioan;
190 break;
191 default :
192 if((*gpio & 0xFF)==0xFF)
193 {
194 for(int i=0;i<16;i++)
195 {
196 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2));
197 GPIOx->MODER |= (GPIO_Mode_IN << (i * 2));
198 }
199 }
200 else
201 {
202 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((*gpio & 0xFF) * 2));
203 GPIOx->MODER |= (GPIO_Mode_IN << ((*gpio & 0xFF) * 2));
204 }
205 *gpio &= ~GPIODIRMASK;
206 *gpio |= gpioindir;
207 break;
208 }
209
98 GPIO_TypeDef* GPIOx = GPIOGETPORT((gpio));
99 uint32_t directionMask = GPIO_Mode_IN;
100 switch(dir)
101 {
102 case gpiooutdir:
103 directionMask = GPIO_Mode_OUT;
104 break;
105 case gpioaf:
106 directionMask = GPIO_Mode_AF;
107 break;
108 case gpioan:
109 directionMask = GPIO_Mode_AN;
110 break;
111 }
112 if((gpio & 0xFF)==0xFF)
113 {
114 for(int i=0;i<16;i++)
115 {
116 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2));
117 GPIOx->MODER |= (directionMask << (i * 2));
118 }
119 }
120 else
121 {
122 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((gpio & 0xFF) * 2));
123 GPIOx->MODER |= (directionMask << ((gpio & 0xFF) * 2));
124 }
210 125 }
211 126
212 127
213 void gpiosetouttype(gpio_t* gpio, gpioouttype_t outtype)
128 void gpiosetouttype(gpio_t gpio, gpioouttype_t outtype)
214 129 {
215 GPIO_TypeDef* GPIOx = GPIOGETPORT((*gpio));
216 if(outtype == gpioopendraintype)
217 {
218 if((*gpio & 0xFF)==0xFF)
219 {
220 for(int i=0;i<16;i++)
221 {
222 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)i));
223 GPIOx->OTYPER |= (uint16_t)(GPIO_OType_OD<<((uint16_t)i));
224 }
225 }
226 else
227 {
228 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)(*gpio & 0xFF)));
229 GPIOx->OTYPER |= (uint16_t)(GPIO_OType_OD<<((uint16_t)(*gpio & 0xFF)));
230 }
231 *gpio &= ~GPIOOUTTYPEMASK;
232 *gpio |= gpioopendraintype;
233 }
234 else
235 {
236 if((*gpio & 0xFF)==0xFF)
237 {
238 for(int i=0;i<16;i++)
239 {
240 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)i));
241 GPIOx->OTYPER |= (uint16_t)(GPIO_OType_PP<<((uint16_t)i));
242 }
243 }
244 else
245 {
246 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)(*gpio & 0xFF)));
247 GPIOx->OTYPER |= (uint16_t)(GPIO_OType_PP<<((uint16_t)(*gpio & 0xFF)));
248 }
249 *gpio &= ~GPIOOUTTYPEMASK;
250 *gpio |= gpiopushpulltype;
251 }
130 GPIO_TypeDef* GPIOx = GPIOGETPORT((gpio));
131 uint16_t outTypeMask = GPIO_OType_PP;
132 if(outtype == gpioopendraintype)
133 outTypeMask = GPIO_OType_OD;
134 else
135 outTypeMask = GPIO_OType_PP;
136
137 if((gpio & 0xFF)==0xFF)
138 {
139 for(int i=0;i<16;i++)
140 {
141 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)i));
142 GPIOx->OTYPER |= (uint16_t)(outTypeMask<<((uint16_t)i));
143 }
144 }
145 else
146 {
147 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)(gpio & 0xFF)));
148 GPIOx->OTYPER |= (uint16_t)(outTypeMask<<((uint16_t)(gpio & 0xFF)));
149 }
252 150 }
253 151
254 152
255 void gpiosetpulltype(gpio_t* gpio,gpiopulltype_t pulltype)
153 void gpiosetpulltype(gpio_t gpio,gpiopulltype_t pulltype)
256 154 {
257 GPIO_TypeDef* GPIOx = GPIOGETPORT(*gpio);
258 switch(pulltype)
259 {
260 case gpiopulluptype:
261 if((*gpio & 0xFF)==0xFF)
262 {
263 for(int i=0;i<16;i++)
264 {
265 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)i * 2));
266 GPIOx->PUPDR |= (GPIO_PuPd_UP << (i * 2));
267 }
268 }
269 else
270 {
271 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)(*gpio & 0xFF) * 2));
272 GPIOx->PUPDR |= (GPIO_PuPd_UP << ((*gpio & 0xFF) * 2));
273 }
274 *gpio &= ~GPIOPULLTYPEMASK;
275 *gpio |= gpiopulluptype;
276 break;
277 case gpiopulldowntype:
278 if((*gpio & 0xFF)==0xFF)
279 {
280 for(int i=0;i<16;i++)
281 {
282 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)i * 2));
283 GPIOx->PUPDR |= (GPIO_PuPd_DOWN << (i * 2));
284 }
285 }
286 else
287 {
288 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)(*gpio & 0xFF) * 2));
289 GPIOx->PUPDR |= (GPIO_PuPd_DOWN << ((*gpio & 0xFF) * 2));
290 }
291 *gpio &= ~GPIOPULLTYPEMASK;
292 *gpio |= gpiopulldowntype;
293 break;
294 default :
295 if((*gpio & 0xFF)==0xFF)
296 {
297 for(int i=0;i<16;i++)
298 {
299 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)i * 2));
300 GPIOx->PUPDR |= (GPIO_PuPd_NOPULL << (i * 2));
301 }
302 }
303 else
304 {
305 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)(*gpio & 0xFF) * 2));
306 GPIOx->PUPDR |= (GPIO_PuPd_NOPULL << ((*gpio & 0xFF) * 2));
307 }
308 *gpio &= ~GPIOPULLTYPEMASK;
309 *gpio |= gpionopulltype;
310 break;
311 }
155 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
156 uint32_t pullTypeMask=GPIO_PuPd_NOPULL;
157 switch(pulltype)
158 {
159 case gpiopulluptype:
160 pullTypeMask=GPIO_PuPd_UP;
161 break;
162 case gpiopulldowntype:
163 pullTypeMask=GPIO_PuPd_DOWN;
164 break;
165 default :
166 pullTypeMask=GPIO_PuPd_NOPULL;
167 break;
168 }
169 if((gpio & 0xFF)==0xFF)
170 {
171 for(int i=0;i<16;i++)
172 {
173 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)i * 2));
174 GPIOx->PUPDR |= (pullTypeMask << (i * 2));
175 }
176 }
177 else
178 {
179 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)(gpio & 0xFF) * 2));
180 GPIOx->PUPDR |= (pullTypeMask << ((gpio & 0xFF) * 2));
181 }
312 182 }
313 183
314 184 void gpioset(gpio_t gpio)
315 185 {
316 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
317 if((gpio & 0xFF)==0xFF)
318 {
319 GPIOx->BSRRL = -1;
320 }
321 else
322 {
323 GPIOx->BSRRL = 1<<(gpio & 0xFF);
324 }
186 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
187 if((gpio & 0xFF)==0xFF)
188 {
189 GPIOx->BSRRL = -1;
190 }
191 else
192 {
193 GPIOx->BSRRL = 1<<(gpio & 0xFF);
194 }
325 195 }
326 196
327 197
328 198 void gpioclr(gpio_t gpio)
329 199 {
330 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
331 if((gpio & 0xFF)==0xFF)
332 {
333 GPIOx->BSRRH = -1;
334 }
335 else
336 {
337 GPIOx->BSRRH = 1<<(gpio & 0xFF);
338 }
200 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
201 if((gpio & 0xFF)==0xFF)
202 {
203 GPIOx->BSRRH = -1;
204 }
205 else
206 {
207 GPIOx->BSRRH = 1<<(gpio & 0xFF);
208 }
339 209 }
340 210
341 211 void gpiosetval(gpio_t gpio,int val)
342 212 {
343 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
344 if((gpio & 0xFF)==0xFF)
345 {
346 GPIOx->ODR = val;
347 }
348 else
349 {
213 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
214 if((gpio & 0xFF)==0xFF)
215 {
216 GPIOx->ODR = val;
217 }
218 else
219 {
350 220 if(val)
351 221 GPIOx->BSRRL = 1<<(gpio & 0xFF);
352 222 else
353 223 GPIOx->BSRRH = 1<<(gpio & 0xFF);
354 }
224 }
355 225 }
356 226
357 227 int gpiogetval(gpio_t gpio)
358 228 {
359 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
360 if((gpio & 0xFF)==0xFF)
361 {
362 return GPIOx->IDR;
363 }
364 else
365 {
366 return ((GPIOx->IDR>>(gpio & 0xFF)) & 1);
367 }
229 GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio);
230 if((gpio & 0xFF)==0xFF)
231 {
232 return GPIOx->IDR;
233 }
234 else
235 {
236 return ((GPIOx->IDR>>(gpio & 0xFF)) & 1);
237 }
368 238 }
369 239
370 240
371 241
372 242
373 243
374 244
375 245
376 246
377 247
378 248
379 249
380 250
381 251
382 252
383 253
384 254
385 255
386 256
387 257
388 258
389 259
390 260
391 261
392 262
@@ -1,388 +1,385
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 43 RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \
44 44 RCC_APB1PeriphClockCmd(_RCC_, ENABLE);
45 45
46 46 switch(count)
47 47 {
48 48 case i2c1:
49 49 _INIT_DEV(RCC_APB1Periph_I2C1);
50 50 return i2c1;
51 51 break;
52 52 case i2c2:
53 53 _INIT_DEV(RCC_APB1Periph_I2C2);
54 54 return i2c2;
55 55 break;
56 56 case i2c3:
57 57 _INIT_DEV(RCC_APB1Periph_I2C3);
58 58 return i2c3;
59 59 break;
60 60 default:
61 61 break;
62 62 }
63 63 return -1;
64 64 }
65 65
66 66 i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL)
67 67 {
68 68 i2c_t dev = i2copen(count);
69 69 if(dev!=-1)
70 70 {
71 71 i2cclose(dev);
72 72 i2csetpins(dev,SDA,SCL);
73 73 i2copen(count);
74 74 i2csetspeed(dev,speed);
75 75 i2cenable(count);
76 76 }
77 77 return dev;
78 78 }
79 79
80 80 int i2cclose(i2c_t dev)
81 81 {
82 82 switch(dev)
83 83 {
84 84 case i2c1:
85 85 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
86 86 break;
87 87 case i2c2:
88 88 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
89 89 break;
90 90 case i2c3:
91 91 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
92 92 break;
93 93 default:
94 94 break;
95 95 }
96 96 return 1;
97 97 }
98 98
99 99 int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL)
100 100 {
101 101 if((dev<i2c4)&&(dev>=i2c1))
102 102 {
103 103 gpio_t SDApin,SCLpin;
104 104 SDApin = gpioopen(SDA);
105 105 SCLpin = gpioopen(SCL);
106 SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
107 SCLpin |= gpiolowspeed | gpiooutdir | gpioopendraintype | gpionopulltype;
108 gpiosetconfig(&SCLpin);
106 gpiosetconfig(SCLpin, gpiooutdir, gpiolowspeed, gpioopendraintype, gpionopulltype);
109 107 for(int i=0;i<32;i++)
110 108 {
111 109 gpioclr(SCLpin);
112 110 for(int l=0;l<200;l++)
113 111 {__asm__("nop");}
114 112 gpioset(SCLpin);
115 113 for(int l=0;l<200;l++)
116 114 {__asm__("nop");}
117 115 }
118 116 SCLpin = gpioopen(SCL);
119 SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype;
120 gpiosetconfig(&SDApin);
121 gpiosetconfig(&SCLpin);
117 gpiosetconfig(SDApin, gpioaf, gpiolowspeed, gpioopendraintype, gpionopulltype);
118 gpiosetconfig(SCLpin, gpioaf, gpiolowspeed, gpioopendraintype, gpionopulltype);
122 119 uint8_t gpioAFi2cx = GPIO_AF_I2C1;
123 120 switch(dev)
124 121 {
125 122 case i2c1:
126 123 gpioAFi2cx = GPIO_AF_I2C1;
127 124 break;
128 125 case i2c2:
129 126 gpioAFi2cx = GPIO_AF_I2C2;
130 127 break;
131 128 case i2c3:
132 129 gpioAFi2cx = GPIO_AF_I2C3;
133 130 break;
134 131 default:
135 132 break;
136 133 }
137 134 GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx);
138 135 GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx);
139 136 return 0;
140 137 }
141 138 return -1;
142 139 }
143 140
144 141 int i2cenable(i2c_t dev)
145 142 {
146 143 if((dev<i2c4)&&(dev>=i2c1))
147 144 {
148 145 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
149 146 _dev_->CR1 |=1 ;
150 147 return 0;
151 148 }
152 149 return -1;
153 150 }
154 151
155 152 int i2cdisable(i2c_t dev)
156 153 {
157 154 if((dev<i2c4)&&(dev>=i2c1))
158 155 {
159 156 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
160 157 _dev_->CR1 &= ~1;
161 158 return 0;
162 159 }
163 160 return -1;
164 161 }
165 162
166 163
167 164 int i2csetspeed(i2c_t dev,uint32_t speed)
168 165 {
169 166 if((dev<i2c4)&&(dev>=i2c1))
170 167 {
171 168 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
172 169 int32_t APB1Freq=getAPB1Freq()/1000000;
173 170 if((APB1Freq>1)&&(APB1Freq<43))
174 171 {
175 172
176 173 uint16_t tmpreg=_dev_->CR2;
177 174 tmpreg &= ~(0x1f);
178 175 tmpreg |= APB1Freq;
179 176 _dev_->CR2=tmpreg;
180 177 i2cdisable(dev);
181 178 tmpreg=_dev_->CCR;
182 179 APB1Freq=getAPB1Freq();
183 180 if(speed>100000) //100kHz= standard mode, 400kHz= fast mode
184 181 {
185 182 if(speed<=400000)
186 183 {
187 184 tmpreg |= 1<<15;
188 185 tmpreg &= ~(1<<14);
189 186 tmpreg &= ~(0xfff);
190 187 tmpreg |= 0xfff & (APB1Freq/(3*speed));
191 188 }
192 189 }
193 190 else
194 191 {
195 192 tmpreg &= ~(1<<15);
196 193 tmpreg &= ~(0xfff);
197 194 tmpreg |= 0xfff & (APB1Freq/(2*speed));
198 195 }
199 196 _dev_->CCR=tmpreg;
200 197 tmpreg=_dev_->TRISE;
201 198 tmpreg &= ~(0x3f);
202 199 tmpreg |= (APB1Freq/1000000)+1;
203 200 _dev_->TRISE = tmpreg;
204 201 i2cenable(dev);
205 202 return 0;
206 203 }
207 204 }
208 205 return -1;
209 206 }
210 207
211 208 int i2cbusy(i2c_t dev)
212 209 {
213 210 if((dev<i2c4)&&(dev>=i2c1))
214 211 {
215 212 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
216 213 if((_dev_->SR2 & 2) ==2) return 1; /* Dev is busy */
217 214 return 0; /* Dev isn't busy */
218 215 }
219 216 return -1; /* Error, dev is out of range */
220 217 }
221 218
222 219 int i2cwrite(i2c_t dev,char address,char* data,int count)
223 220 {
224 221 if((dev<i2c4)&&(dev>=i2c1))
225 222 {
226 223 int timeout=i2ctimeout;
227 224 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
228 225 while(i2cbusy(dev))
229 226 {
230 227 if(0==(timeout--))
231 228 {
232 229 printf("Exited on timeout @ line %d\n\r",__LINE__);
233 230 return -1;
234 231 }
235 232 }
236 233 _dev_->CR1 |= 1<<8;
237 234 timeout=i2ctimeout;
238 235 while(!i2cStatusCheck(dev,((uint32_t)0x00030001)))
239 236 {
240 237 if(0==(timeout--))
241 238 {
242 239 printf("Exited on timeout @ line %d\n\r",__LINE__);
243 240 return -1;
244 241 }
245 242 }
246 243 _dev_->DR= address<<1;
247 244 timeout=i2ctimeout;
248 245 while(!i2cStatusCheck(dev, ((uint32_t)0x00070082)))
249 246 {
250 247 if(0==(timeout--))
251 248 {
252 249 printf("Exited on timeout @ line %d\n\r",__LINE__);
253 250 return -1;
254 251 }
255 252 }
256 253 address=_dev_->SR2;
257 254 for(int i=0;i<count;i++)
258 255 {
259 256 timeout=i2ctimeout;
260 257 while(!i2cStatusCheck(dev,((uint32_t)0x00070080)))
261 258 {
262 259 if(0==(timeout--))
263 260 {
264 261 printf("Exited on timeout @ line %d\n\r",__LINE__);
265 262 return -1;
266 263 }
267 264 }
268 265 _dev_->DR= data[i];
269 266 }
270 267 timeout=i2ctimeout;
271 268 while(!i2cStatusCheck(dev,1<<7))
272 269 {
273 270 if(0==(timeout--))
274 271 {
275 272 printf("Exited on timeout @ line %d\n\r",__LINE__);
276 273 return -1;
277 274 }
278 275 }
279 276 timeout=i2ctimeout;
280 277 while(!i2cStatusCheck(dev,1<<2))
281 278 {
282 279 if(0==(timeout--))
283 280 {
284 281 printf("Exited on timeout @ line %d\n\r",__LINE__);
285 282 return -1;
286 283 }
287 284 }
288 285 _dev_->CR1 |= 1<<9;
289 286 return count;
290 287 }
291 288 return -1;
292 289 }
293 290
294 291 int i2cread(i2c_t dev,char address,char* data,int count)
295 292 {
296 293 if((dev<i2c4)&&(dev>=i2c1))
297 294 {
298 295 int i=0;
299 296 int timeout=i2ctimeout;
300 297 while(i2cbusy(dev))
301 298 {
302 299 if(0==(timeout--))
303 300 {
304 301 printf("Exited on timeout @ line %d\n\r",__LINE__);
305 302 return -1;
306 303 }
307 304 }
308 305 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
309 306 _dev_->CR1 |= (1<<8) + (1<<10);
310 307 timeout=i2ctimeout;
311 308 while(!i2cStatusCheck(dev,0x00030001))
312 309 {
313 310 if(0==(timeout--))
314 311 {
315 312 printf("Exited on timeout @ line %d\n\r",__LINE__);
316 313 return -1;
317 314 }
318 315 }
319 316 _dev_->DR= (address<<1) + 1;
320 317 while(!i2cStatusCheck(dev,0x000002))
321 318 {
322 319 if(0==(timeout--))
323 320 {
324 321 printf("Exited on timeout @ line %d\n\r",__LINE__);
325 322 return -1;
326 323 }
327 324 }
328 325 if(count==1)
329 326 {
330 327 _dev_->CR1 &= ~(1<<10);
331 328 }
332 329 address=_dev_->SR2;
333 330 for(i=0;i<(count-1);i++)
334 331 {
335 332 timeout=i2ctimeout;
336 333 while(!i2cStatusCheck(dev,0x0000040))
337 334 {
338 335 if(0==(timeout--))
339 336 {
340 337 printf("Exited on timeout @ line %d\n\r",__LINE__);
341 338 return -1;
342 339 }
343 340 }
344 341 data[i]=_dev_->DR;
345 342 }
346 343 _dev_->CR1 &= ~(1<<10);
347 344 _dev_->CR1 |= 1<<9;
348 345 timeout=i2ctimeout;
349 346 while(!i2cStatusCheck(dev,0x0000040))
350 347 {
351 348 if(0==(timeout--))
352 349 {
353 350 printf("Exited on timeout @ line %d\n\r",__LINE__);
354 351 return -1;
355 352 }
356 353 }
357 354 data[i]=_dev_->DR;
358 355 timeout=i2ctimeout;
359 356 while(_dev_->CR1 & ((uint16_t)0x0200))
360 357 {
361 358 if(0==(timeout--))
362 359 {
363 360 printf("Exited on timeout @ line %d\n\r",__LINE__);
364 361 return -1;
365 362 }
366 363 }
367 364 _dev_->CR1 |= 1<<10;
368 365 return count;
369 366 }
370 367 return -1;
371 368 }
372 369
373 370
374 371 int i2cStatusCheck(i2c_t dev,int32_t flagMask)
375 372 {
376 373 int32_t flag;
377 374 if((dev<i2c4)&&(dev>=i2c1))
378 375 {
379 376 I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev];
380 377 flag= _dev_->SR1 + (_dev_->SR2<<16);
381 378 if(flagMask==(flag & flagMask))
382 379 return 1;
383 380 return 0;
384 381 }
385 382 return -1;
386 383 }
387 384
388 385
@@ -1,208 +1,207
1 1 #include <pwm.h>
2 2 #include <stm32f4xx_tim.h>
3 3 #include <stm32f4xx_gpio.h>
4 4 #include <stm32f4xx_rcc.h>
5 5 #include <stm32f4xx_tim.h>
6 6 #include <core.h>
7 7 #include <gpio.h>
8 8 #include <timer.h>
9 9
10 10 const TIM_TypeDef* _timer_dev_table[14]={TIM1,TIM2,TIM3,TIM4,TIM5,TIM6,TIM7,
11 11 TIM8,TIM9,TIM10,TIM11,TIM12,TIM13,TIM14};
12 12
13 13
14 14 #define PWMGETTIMNUMBER(PWM) (((uint32_t)(PWM) & (uint32_t)0x0000FF00)>>(uint32_t)8)
15 15
16 16 int pwmopen(int PWM,uint32_t pin)
17 17 {
18 18 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
19 19 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
20 20 int timer = PWMGETTIMNUMBER(PWM);
21 21 gpio_t PWMOUT;
22 22 PWMOUT = gpioopen(pin);
23 PWMOUT |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
24 gpiosetconfig(&PWMOUT);
23 gpiosetconfig(PWMOUT, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
25 24 uint8_t GPIO_AF = -1;
26 25 if(timer==timer1 || timer==timer2)GPIO_AF=1;
27 26 if(timer==timer3 || timer==timer4 || timer==timer5)GPIO_AF=2;
28 27 if(timer==timer8 || timer==timer9 || timer==timer10 || timer==timer11)GPIO_AF=3;
29 28 if(timer==timer12 || timer==timer13 || timer==timer14)GPIO_AF=9;
30 29
31 30 if(timer>=timer2 && timer <= timer7)RCC_APB1PeriphClockCmd((1<<(timer-1)), ENABLE);
32 31 if(timer>=timer12 && timer <= timer14)RCC_APB1PeriphClockCmd((1<<(timer-5)), ENABLE);
33 32 if(timer==timer1)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
34 33 if(timer==timer8)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
35 34 if(timer==timer9)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
36 35 if(timer==timer10)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
37 36 if(timer==timer11)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, ENABLE);
38 37
39 38 if(GPIO_AF!=-1)GPIO_PinAFConfig(GPIOGETPORT(PWMOUT), (uint8_t)(PWMOUT & 0xF), GPIO_AF);
40 39
41 40 return 0;
42 41 }
43 42
44 43 int pwmsetconfig(int PWM,uint32_t freq,float dutyCycle)
45 44 {
46 45 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
47 46 TIM_OCInitTypeDef TIM_OCInitStructure;
48 47 if(PWM==-1) return -1;
49 48 int timer = PWMGETTIMNUMBER(PWM);
50 49 TIM_TypeDef* tim = _timer_dev_table[timer];
51 50 uint32_t timfreq = getCpuFreq()/2;
52 51 uint32_t period = timfreq/freq - 1;
53 52 uint16_t PrescalerValue=0;
54 53
55 54 while (period>=0x0FFFF)
56 55 {
57 56 PrescalerValue++;
58 57 timfreq = getCpuFreq()/(2*(PrescalerValue+1));
59 58 period = (timfreq/freq) - 1;
60 59 }
61 60
62 61 TIM_TimeBaseStructure.TIM_Period = period;
63 62 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
64 63 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
65 64 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
66 65 TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure);
67 66
68 67 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
69 68 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
70 69 TIM_OCInitStructure.TIM_Pulse = (uint32_t)((period * dutyCycle)/100);
71 70 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
72 71
73 72 switch (PWM&0x0FF) {
74 73 case 0:
75 74 TIM_OC1Init(tim, &TIM_OCInitStructure);
76 75 TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable);
77 76 TIM_ARRPreloadConfig(tim, ENABLE);
78 77 TIM_Cmd(tim, ENABLE);
79 78 break;
80 79 case 1:
81 80 TIM_OC2Init(tim, &TIM_OCInitStructure);
82 81 TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable);
83 82 TIM_ARRPreloadConfig(tim, ENABLE);
84 83 TIM_Cmd(tim, ENABLE);
85 84 break;
86 85 case 2:
87 86 TIM_OC3Init(tim, &TIM_OCInitStructure);
88 87 TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable);
89 88 TIM_ARRPreloadConfig(tim, ENABLE);
90 89 TIM_Cmd(tim, ENABLE);
91 90 break;
92 91 case 3:
93 92 TIM_OC4Init(tim, &TIM_OCInitStructure);
94 93 TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable);
95 94 TIM_ARRPreloadConfig(tim, ENABLE);
96 95 TIM_Cmd(tim, ENABLE);
97 96 break;
98 97 default:
99 98 return -1;
100 99 break;
101 100 }
102 101 return 0;
103 102 }
104 103
105 104
106 105
107 106
108 107
109 108 int pwmsetdutycycle(int PWM,float dutyCycle)
110 109 {
111 110 if(PWM==-1) return -1;
112 111 int timer = PWMGETTIMNUMBER(PWM);
113 112 TIM_TypeDef* tim = _timer_dev_table[timer];
114 113 switch (PWM&0x0FF) {
115 114 case 0:
116 115 tim->CCR1 = (uint32_t)((tim->ARR * dutyCycle)/100);
117 116 break;
118 117 case 1:
119 118 tim->CCR2 = (uint32_t)((tim->ARR * dutyCycle)/100);
120 119 break;
121 120 case 2:
122 121 tim->CCR3 = (uint32_t)((tim->ARR * dutyCycle)/100);
123 122 break;
124 123 case 3:
125 124 tim->CCR4 = (uint32_t)((tim->ARR * dutyCycle)/100);
126 125 break;
127 126 default:
128 127 return -1;
129 128 break;
130 129 }
131 130 return 0;
132 131 }
133 132
134 133 float pwmgetdutycycle(int PWM)
135 134 {
136 135 if(PWM==-1) return -1;
137 136 int timer = PWMGETTIMNUMBER(PWM);
138 137 TIM_TypeDef* tim = _timer_dev_table[timer];
139 138 switch (PWM&0x0FF) {
140 139 case 0:
141 140 return (float)((tim->CCR1*100)/tim->ARR);
142 141 break;
143 142 case 1:
144 143 return (float)((tim->CCR2*100)/tim->ARR);
145 144 break;
146 145 case 2:
147 146 return (float)((tim->CCR3*100)/tim->ARR);
148 147 break;
149 148 case 3:
150 149 return (float)((tim->CCR4*100)/tim->ARR);
151 150 break;
152 151 default:
153 152 return -1;
154 153 break;
155 154 }
156 155 return 0;
157 156 }
158 157
159 158
160 159 int pwmsetfrequency(int PWM,uint32_t freq)
161 160 {
162 161 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
163 162 if(PWM==-1) return -1;
164 163 int timer = PWMGETTIMNUMBER(PWM);
165 164 TIM_TypeDef* tim = _timer_dev_table[timer];
166 165 uint32_t timfreq = getCpuFreq()/2;
167 166 uint32_t period = timfreq/freq - 1;
168 167 uint16_t PrescalerValue=0;
169 168 float dutyCycle = pwmgetdutycycle(PWM);
170 169 while (period>=0x0FFFF)
171 170 {
172 171 PrescalerValue++;
173 172 timfreq = getCpuFreq()/(2*(PrescalerValue+1));
174 173 period = (timfreq/freq) - 1;
175 174 }
176 175
177 176 TIM_TimeBaseStructure.TIM_Period = period;
178 177 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
179 178 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
180 179 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
181 180 TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure);
182 181
183 182 switch (PWM&0x0FF) {
184 183 case 0:
185 184 tim->CCR1 = (uint32_t)((tim->ARR * dutyCycle)/100);
186 185 TIM_ARRPreloadConfig(tim, ENABLE);
187 186 break;
188 187 case 1:
189 188 tim->CCR2 = (uint32_t)((tim->ARR * dutyCycle)/100);
190 189 TIM_ARRPreloadConfig(tim, ENABLE);
191 190 break;
192 191 case 2:
193 192 tim->CCR3 = (uint32_t)((tim->ARR * dutyCycle)/100);
194 193 TIM_ARRPreloadConfig(tim, ENABLE);
195 194 break;
196 195 case 3:
197 196 tim->CCR4 = (uint32_t)((tim->ARR * dutyCycle)/100);
198 197 TIM_ARRPreloadConfig(tim, ENABLE);
199 198 break;
200 199 default:
201 200 return -1;
202 201 break;
203 202 }
204 203 return 0;
205 204 }
206 205
207 206
208 207
@@ -1,484 +1,478
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@member.fsf.org
21 21 -------------------------------------------------------------------------------*/
22 22
23 23 #include <spi.h>
24 24 #include <stm32f4xx_rcc.h>
25 25 #include <stm32f4xx_gpio.h>
26 26 #include <gpio.h>
27 27 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
28 28 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
29 29
30 30
31 31 SPI_TypeDef* _spi_dev_table[3]={SPI1,SPI2,SPI3};
32 32
33 33 spi_t spiopen(int count)
34 34 {
35 35 #define _INIT_DEV(_RCC_) \
36 36 RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \
37 37 RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \
38 38 RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \
39 39 RCC_APB1PeriphClockCmd(_RCC_, ENABLE);
40 40
41 41 switch(count)
42 42 {
43 43 case spi1:
44 44 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
45 45 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
46 46 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
47 47 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
48 48 return spi1;
49 49 break;
50 50 case spi2:
51 51 _INIT_DEV(RCC_APB1Periph_SPI2);
52 52 return spi2;
53 53 break;
54 54 case spi3:
55 55 _INIT_DEV(RCC_APB1Periph_SPI3);
56 56 return spi3;
57 57 break;
58 58 default:
59 59 break;
60 60 }
61 61 return -1;
62 62 }
63 63
64 64 spi_t spiopenandconfig(int count, uint32_t cfg, uint32_t speed, uint32_t MOSIpin, uint32_t MISOpin, uint32_t SCKpin, uint32_t SCSpin)
65 65 {
66 66 spi_t dev = spiopen(count);
67 67 if(dev!=-1)
68 68 {
69 69 spidisable(dev);
70 70 spisetpins(dev,MOSIpin, MISOpin, SCKpin, SCSpin);
71 71 spienable(dev);
72 72 spisetconfig(dev,cfg,speed);
73 73 }
74 74 return dev;
75 75 }
76 76
77 77
78 78 int spiclose(spi_t spidev)
79 79 {
80 80 if((spidev<3)&&(spidev>=0))
81 81 {
82 82 switch(spidev)
83 83 {
84 84 case spi1:
85 85 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
86 86 break;
87 87 case spi2:
88 88 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
89 89 break;
90 90 case spi3:
91 91 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
92 92 break;
93 93 default:
94 94 return -1;
95 95 break;
96 96 }
97 97 return 1;
98 98 }
99 99 return -1;
100 100 }
101 101
102 102 int spisetpins(spi_t spidev,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin)
103 103 {
104 104 if((spidev<3)&&(spidev>=0))
105 105 {
106 106 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
107 107 gpio_t MISO,MOSI,SCK,SCS;
108 108 uint8_t gpioAFspix = GPIO_AF_SPI1;
109 109 switch(spidev)
110 110 {
111 111 case spi1:
112 112 gpioAFspix = GPIO_AF_SPI1;
113 113 break;
114 114 case spi2:
115 115 gpioAFspix = GPIO_AF_SPI2;
116 116 break;
117 117 case spi3:
118 118 gpioAFspix = GPIO_AF_SPI3;
119 119 break;
120 120 default:
121 121 break;
122 122 }
123 123 if(MISOpin!=-1)
124 124 {
125 125 MISO = gpioopen(MISOpin);
126 MISO |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
127 gpiosetconfig(&MISO);
126 gpiosetconfig(MISO, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
128 127 GPIO_PinAFConfig(GPIOGETPORT(MISO), (uint8_t)(MISO & 0xF), gpioAFspix);
129 128 }
130 129 if(MOSIpin!=-1)
131 130 {
132 131 MOSI = gpioopen(MOSIpin);
133 MOSI |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
134 gpiosetconfig(&MOSI);
132 gpiosetconfig(MOSI, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
135 133 GPIO_PinAFConfig(GPIOGETPORT(MOSI), (uint8_t)(MOSI & 0xF), gpioAFspix);
136 134 }
137
138 135 if(SCKpin!=-1)
139 136 {
140 137 SCK = gpioopen(SCKpin);
141 SCK |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
142 gpiosetconfig(&SCK);
138 gpiosetconfig(SCK, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
143 139 GPIO_PinAFConfig(GPIOGETPORT(SCK), (uint8_t)(SCK & 0xF), gpioAFspix);
144 140 }
145
146 141 if(SCSpin!=-1)
147 142 {
148 143 SCS = gpioopen(SCSpin);
149 SCS |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
150 gpiosetconfig(&SCS);
144 gpiosetconfig(SCS, gpioaf, gpiohighspeed, gpiopushpulltype, gpionopulltype);
151 145 GPIO_PinAFConfig(GPIOGETPORT(SCS), (uint8_t)(SCS & 0xF), gpioAFspix);
152 146 _dev_->CR2 |= (1<<2);
153 147 }
154 148 else
155 149 {
156 150 _dev_->CR2 &= ~(1<<2);
157 151 }
158 152 return 1;
159 153 }
160 154 return -1;
161 155 }
162 156
163 157
164 158 int spienable(spi_t spidev)
165 159 {
166 160 if((spidev<3)&&(spidev>=0))
167 161 {
168 162 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
169 163 _dev_->CR1 |= (1<<6);
170 164 return 1;
171 165 }
172 166 return -1;
173 167 }
174 168
175 169
176 170 int spidisable(spi_t spidev)
177 171 {
178 172 if((spidev<3)&&(spidev>=0))
179 173 {
180 174 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
181 175 _dev_->CR1 &= ~(1<<6);
182 176 return 1;
183 177 }
184 178 return -1;
185 179 }
186 180
187 181 int spisetconfig(spi_t spidev, uint32_t config, uint32_t speed)
188 182 {
189 183 if((spidev<3)&&(spidev>=0))
190 184 {
191 185 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
192 186 _dev_->CR2 |= (1<<2);
193 187 _dev_->CR1 |= (1<<2);
194 188 spisetspeed(spidev,speed);
195 189 spisetdatabits(spidev,config & SPIBITSMASK);
196 190 spisetbitorder(spidev,config & SPIBITORDERMASK);
197 191 spisetclkinhlevel(spidev,config & SPICLKINHLVLMASK);
198 192 spisetclkphase(spidev,config & SPICLKPHASEMASK);
199 193 return 0;
200 194 }
201 195 return 1;
202 196 }
203 197
204 198 int spisetspeed(spi_t spidev, uint32_t speed)
205 199 {
206 200 if((spidev<3)&&(spidev>=0))
207 201 {
208 202 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
209 203 uint32_t apbclock = 0x00;
210 204 RCC_ClocksTypeDef RCC_ClocksStatus;
211 205 RCC_GetClocksFreq(&RCC_ClocksStatus);
212 206 if (_dev_ == SPI1)
213 207 {
214 208 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
215 209 }
216 210 else
217 211 {
218 212 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
219 213 }
220 214 int32_t speederror = 0x7FFFFFFF; //max error
221 215 int32_t prev_speederror = 0x7FFFFFFF;
222 216 int32_t realspeed = 0;
223 217 unsigned char divider = 0;
224 218 do
225 219 {
226 220 divider ++;
227 221 prev_speederror = speederror;
228 222 realspeed = apbclock>>(divider);
229 223 speederror = realspeed - speed;
230 224 if(speederror<0)speederror=-speederror;
231 225 if(divider>8)break;
232 226 }while(speederror<prev_speederror);
233 227 speed = apbclock>>(divider-1);
234 228 divider-=2;
235 229 _dev_->CR1 &= 0xFFD7; // clear prescaler bits 3:5
236 230 _dev_->CR1 |= ((0x7 & divider)<<3);
237 231 return 1;
238 232 }
239 233 return -1;
240 234 }
241 235
242 236 uint32_t spigetspeed(spi_t spidev)
243 237 {
244 238 if((spidev<3)&&(spidev>=0))
245 239 {
246 240 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
247 241 uint32_t apbclock = 0x00;
248 242 RCC_ClocksTypeDef RCC_ClocksStatus;
249 243 RCC_GetClocksFreq(&RCC_ClocksStatus);
250 244 if (_dev_ == SPI1)
251 245 {
252 246 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
253 247 }
254 248 else
255 249 {
256 250 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
257 251 }
258 252 int BR= (_dev_->CR1>>3)&0x7;
259 253 return apbclock>>(BR+1);
260 254 }
261 255 return -1;
262 256 }
263 257
264 258 int spisetdatabits(spi_t spidev,spibits_t bitscnt)
265 259 {
266 260 if((spidev<3)&&(spidev>=0))
267 261 {
268 262 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
269 263 int result = 0;
270 264 switch(bitscnt)
271 265 {
272 266 case spi8bits:
273 267 _dev_->CR1 &= ~(1<<11);
274 268 result = 1;
275 269 break;
276 270 case spi16bits:
277 271 _dev_->CR1 |= (1<<11);
278 272 result = 1;
279 273 break;
280 274 default:
281 275 result =-1;
282 276 break;
283 277 }
284 278 return result;
285 279 }
286 280 return -1;
287 281 }
288 282
289 283 int spisetbitorder(spi_t spidev,spibitorder_t order)
290 284 {
291 285 if((spidev<3)&&(spidev>=0))
292 286 {
293 287 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
294 288 if(order==spimsbfirst)
295 289 {
296 290 _dev_->CR1 &= ~(1<<7);
297 291 return 1;
298 292 }
299 293 else
300 294 {
301 295 if(order==spilsbfirst)
302 296 {
303 297 _dev_->CR1 |= (1<<7);
304 298 return 1;
305 299 }
306 300 else return -1;
307 301 }
308 302 }
309 303 return -1;
310 304 }
311 305
312 306 int spisetclkinhlevel(spi_t spidev,spiclkinhlvl_t level)
313 307 {
314 308 if((spidev<3)&&(spidev>=0))
315 309 {
316 310 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
317 311 if(level==spiclkinhlow)
318 312 {
319 313 _dev_->CR1 &= ~(1<<1);
320 314 return 1;
321 315 }
322 316 else
323 317 {
324 318 if(level==spiclkinhhigh)
325 319 {
326 320 _dev_->CR1 |= (1<<1);
327 321 return 1;
328 322 }
329 323 else return -1;
330 324 }
331 325 }
332 326 return -1;
333 327 }
334 328
335 329 int spisetclkphase(spi_t spidev,spiclkphase_t phase)
336 330 {
337 331 if((spidev<3)&&(spidev>=0))
338 332 {
339 333 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
340 334 if(phase==spiclkfirstedge)
341 335 {
342 336 _dev_->CR1 &= ~1;
343 337 return 1;
344 338 }
345 339 else
346 340 {
347 341 if(phase==spiclksecondedge)
348 342 {
349 343 _dev_->CR1 |= 1;
350 344 return 1;
351 345 }
352 346 else return -1;
353 347 }
354 348 }
355 349 return -1;
356 350 }
357 351
358 352 int spiputw(spi_t spidev,uint16_t data)
359 353 {
360 354 if((spidev<3)&&(spidev>=0))
361 355 {
362 356 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
363 357 while(((_dev_->SR & (1<<1)) == 0) );
364 358 _dev_->DR = data;
365 359 while(((_dev_->SR & (1<<0)) == 0) );
366 360 return _dev_->DR;
367 361 }
368 362 return -1;
369 363 }
370 364 uint16_t spigetw(spi_t spidev)
371 365 {
372 366 if((spidev<3)&&(spidev>=0))
373 367 {
374 368 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
375 369 while(((_dev_->SR & (1<<1)) == 0) );
376 370 _dev_->DR = 0xFFFF;
377 371 while(((_dev_->SR & (1<<0)) == 0) );
378 372 return _dev_->DR;
379 373 }
380 374 return -1;
381 375 }
382 376
383 377
384 378 uint16_t spigetw2(spi_t spidev,uint16_t data)
385 379 {
386 380 if((spidev<3)&&(spidev>=0))
387 381 {
388 382 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
389 383 while(((_dev_->SR & (1<<1)) == 0) );
390 384 _dev_->DR = data;
391 385 while(((_dev_->SR & (1<<0)) == 0) );
392 386 return _dev_->DR;
393 387 }
394 388 return -1;
395 389 }
396 390
397 391 int spiputs(spi_t spidev,char* s)
398 392 {
399 393 while (*s) spiputw(spidev,*s++);
400 394 return 1;
401 395 }
402 396
403 397 int spigets(spi_t spidev,char* s)
404 398 {
405 399 do
406 400 {
407 401 (*s) = spigetw(spidev);
408 402 }
409 403 while(*s++);
410 404 return 1;
411 405 }
412 406
413 407 int spiputnw(spi_t spidev,uint16_t* w,int n)
414 408 {
415 409 while(n!=0)
416 410 {
417 411 spiputw(spidev,*w++);
418 412 n--;
419 413 }
420 414 return 1;
421 415 }
422 416
423 417 int spigetnw(spi_t spidev,uint16_t* w,int n)
424 418 {
425 419 while(n!=0)
426 420 {
427 421 *w++=spigetw(spidev);
428 422 n--;
429 423 }
430 424 return 1;
431 425 }
432 426
433 427 int spiputnc(spi_t spidev,char* c,int n)
434 428 {
435 429 while(n!=0)
436 430 {
437 431 spiputw(spidev,*c++);
438 432 n--;
439 433 }
440 434 return 1;
441 435 }
442 436
443 437 int spigetnc(spi_t spidev,char* c,int n)
444 438 {
445 439 while(n!=0)
446 440 {
447 441 *c++=spigetw(spidev);
448 442 n--;
449 443 }
450 444 return 1;
451 445 }
452 446
453 447 int spiavailiabledata(spi_t spidev)
454 448 {
455 449 return 0;
456 450 }
457 451
458 452
459 453 int spitransactionfinished(spi_t spidev)
460 454 {
461 455 SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev];
462 456 if((spidev<3)&&(spidev>=0))
463 457 {
464 458 if((_dev_->SR & (1<<7)) == (1<<7))return 1;
465 459 }
466 460 return 0;
467 461 }
468 462
469 463
470 464
471 465
472 466
473 467
474 468
475 469
476 470
477 471
478 472
479 473
480 474
481 475
482 476
483 477
484 478
@@ -1,464 +1,490
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@member.fsf.org
21 21 -------------------------------------------------------------------------------*/
22 22
23 23 #include <uart.h>
24 24 #include <stm32f4xx_rcc.h>
25 25 #include <stm32f4xx_gpio.h>
26 26 #include <gpio.h>
27 27 #include <stdio.h>
28 28
29 29 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
30 30 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
31 31
32 32 USART_TypeDef* _uart_dev_table[6]={USART1,USART2,USART3,UART4,UART5,USART6};
33 33
34 34
35 35 int _uartstrsetpos(streamdevice* device,int pos);
36 36 int _uartstrread(streamdevice* device,void* data,int size, int n);
37 37 int _uartstrwrite(streamdevice* device,void* data,int size, int n);
38 38
39 typedef struct _dma_pointer_
40 {
41 int pos;
42 int size;
43 }_dma_pointer_;
44
45 #ifdef UART_USES_DMA
46 static char _uart_dma_buff[1024];
47 _dma_pointer_ _uart1_dma_pointer={0,0};
48 _dma_pointer_ _uart2_dma_pointer={0,0};
49 _dma_pointer_ _uart3_dma_pointer={0,0};
50 _dma_pointer_ _uart4_dma_pointer={0,0};
51 _dma_pointer_ _uart5_dma_pointer={0,0};
52 _dma_pointer_ _uart6_dma_pointer={0,0};
53 _dma_pointer_* _uart_dma_pointer_table[6]={&_uart1_dma_pointer,&_uart2_dma_pointer,&_uart3_dma_pointer,&_uart4_dma_pointer,&_uart5_dma_pointer,&_uart6_dma_pointer};
54 #endif
55
39 56 streamdevice_ops UART_OPS=
40 57 {
41 58 .write = &_uartstrwrite,
42 59 .read = &_uartstrread,
43 60 .setpos= &_uartstrsetpos,
44 61 .close = NULL
45 62 };
46 63
47 64 uart_t uartopen(int count)
48 65 {
49 66
50 67 switch(count)
51 68 {
52 69 case uart1:
53 70 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
54 71 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
55 72 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
56 73 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
57 74 USART1->CR3 &= ~((1<<8) + (1<<9));
58 75 return uart1;
59 76 break;
60 77 case uart2:
61 78 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
62 79 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
63 80 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
64 81 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
65 82 USART2->CR3 &= ~((1<<8) + (1<<9));
66 83 return uart2;
67 84 break;
68 85 case uart3:
69 86 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
70 87 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
71 88 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
72 89 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
73 90 USART3->CR3 &= ~((1<<8) + (1<<9));
74 91 return uart3;
75 92 break;
76 93 case uart4:
77 94 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
78 95 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
79 96 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
80 97 UART4->CR3 &= ~((1<<8) + (1<<9));
81 98 return uart4;
82 99 break;
83 100 case uart5:
84 101 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
85 102 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
86 103 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
87 104 return uart5;
88 105 break;
89 106 case uart6:
90 107 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
91 108 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, DISABLE);
92 109 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
93 110 return uart6;
94 111 break;
95 112 default:
96 113 break;
97 114 }
98 115 return -1;
99 116 }
100 117
101 118 uart_t uartopenandconfig(int count, uint32_t cfg, uint32_t speed, uint32_t TXpin, uint32_t RXpin, uint32_t RTSpin, uint32_t CTSpin)
102 119 {
103 120 uart_t dev= uartopen(count);
104 121 uartsetconfig(dev,cfg,speed);
105 122 uartsetpins(dev,TXpin,RXpin,RTSpin,CTSpin);
106 123 return dev;
107 124 }
108 125
109 126
110 127 int uartclose(uart_t uart)
111 128 {
112 129 switch(uart)
113 130 {
114 131 case uart1:
115 132 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
116 133 break;
117 134 case uart2:
118 135 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
119 136 break;
120 137 case uart3:
121 138 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
122 139 break;
123 140 case uart4:
124 141 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
125 142 break;
126 143 case uart5:
127 144 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
128 145 break;
129 146 case uart6:
130 147 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
131 148 break;
132 149 default:
133 150 return -1;
134 151 break;
135 152 }
136 153 return 1;
137 154 }
138 155
139 156 int uartsetpins(uart_t uart,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin)
140 157 {
141 158 if(uart >5)return -1;
142 159 if(uart <0)return -1;
143 160 gpio_t TX,RX,CTS,RTS;
144 161 TX = gpioopen(TXpin);
145 162 RX = gpioopen(RXpin);
146 TX |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype;
147 RX |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype;
148 gpiosetconfig(&TX);
149 gpiosetconfig(&RX);
163 gpiosetconfig(TX,gpioaf,gpiolowspeed,gpiopushpulltype,gpionopulltype);
164 gpiosetconfig(RX,gpioaf,gpiolowspeed,gpiopushpulltype,gpionopulltype);
150 165 uint8_t gpioAFuartx = GPIO_AF_USART1;
151 166 switch(uart)
152 167 {
153 168 case uart1:
154 169 gpioAFuartx = GPIO_AF_USART1;
155 170 break;
156 171 case uart2:
157 172 gpioAFuartx = GPIO_AF_USART2;
158 173 break;
159 174 case uart3:
160 175 gpioAFuartx = GPIO_AF_USART3;
161 176 break;
162 177 case uart4:
163 178 gpioAFuartx = GPIO_AF_UART4;
164 179 break;
165 180 case uart5:
166 181 gpioAFuartx = GPIO_AF_UART5;
167 182 break;
168 183 case uart6:
169 184 gpioAFuartx = GPIO_AF_USART6;
170 185 break;
171 186 default:
172 187 return -1;
173 188 break;
174 189 }
175 190 GPIO_PinAFConfig(GPIOGETPORT(TX), (uint8_t)(TX & 0xF), gpioAFuartx);
176 191 GPIO_PinAFConfig(GPIOGETPORT(RX), (uint8_t)(RX & 0xF), gpioAFuartx);
177 192 if((gpioAFuartx!=GPIO_AF_UART5) && (gpioAFuartx!=GPIO_AF_UART4))
178 193 {
179 194 if(CTSpin!=-1)
180 195 {
181 196 CTS = gpioopen(CTSpin);
182 CTS |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype;
183 gpiosetconfig(&CTS);
197 gpiosetconfig(CTS,gpioaf,gpiolowspeed,gpiopushpulltype,gpionopulltype);
184 198 GPIO_PinAFConfig(GPIOGETPORT(CTS), (uint8_t)(CTS & 0xF), gpioAFuartx);
185 199 }
186 200
187 201 if(RTSpin!=-1)
188 202 {
189 203 RTS = gpioopen(RTSpin);
190 RTS |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype;
191 gpiosetconfig(&RTS);
204 gpiosetconfig(RTS,gpioaf,gpiolowspeed,gpiopushpulltype,gpionopulltype);
192 205 GPIO_PinAFConfig(GPIOGETPORT(RTS), (uint8_t)(RTS & 0xF), gpioAFuartx);
193 206 }
194 207 }
195 208 return 1;
196 209 }
197 210
198 211 int uartsetconfig(uart_t uart, uint32_t cfg, uint32_t speed)
199 212 {
200 213 int res=1;
201 214 uartdisable(uart);
202 215 uartsetspeed(uart,speed);
203 216 uartsetparity(uart,cfg & UARTPARITYMASK);
204 217 uartsetdatabits(uart,cfg & UARTBITSMASK);
205 218 uartsetstopbits(uart,cfg & UARTSTOPBITSMASK);
206 219 uartenable(uart);
207 220 return res;
208 221 }
209 222
210 223 int uartenable(uart_t uart)
211 224 {
212 225 if((uart<6)&&(uart>=0))
213 226 {
214 227 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
215 228 _dev_->CR1 |= (1<<13);
216 229 _dev_->CR1 |= (1<<2) + (1<<3);
217 230 _dev_->DR = ' ';
218 231 return 1;
219 232 }
220 233 return -1;
221 234 }
222 235
223 236 int uartdisable(uart_t uart)
224 237 {
225 238 if((uart<6)&&(uart>=0))
226 239 {
227 240 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
228 241 if((_dev_->CR1 & ((1<<3) +(1<<13)))==((1<<3) +(1<<13)))
229 242 {
230 243 while((_dev_->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7));
231 244 }
232 245 _dev_->CR1 &= ~((1<<2) + (1<<3) +(1<<13));
233 246 return 1;
234 247 }
235 248 return -1;
236 249 }
237 250
238 251 int uartsetspeed(uart_t uart,uint32_t speed)
239 252 {
240 253 if((uart<6)&&(uart>=0))
241 254 {
242 255 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
243 256 uint32_t tmpreg = 0x00, apbclock = 0x00;
244 257 uint32_t integerdivider = 0x00;
245 258 uint32_t fractionaldivider = 0x00;
246 259 RCC_ClocksTypeDef RCC_ClocksStatus;
247 260 RCC_GetClocksFreq(&RCC_ClocksStatus);
248 261
249 262 if ((_dev_ == USART1) || ((_dev_ == USART6)))
250 263 {
251 264 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
252 265 }
253 266 else
254 267 {
255 268 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
256 269 }
257 270 if (((_dev_->CR1) & USART_CR1_OVER8) != (uint16_t)0)
258 271 {
259 272 integerdivider = ((25 * apbclock) / (2 * (speed)));
260 273 }
261 274 else
262 275 {
263 276 integerdivider = ((25 * apbclock) / (4 * (speed)));
264 277 }
265 278 tmpreg = (integerdivider / 100) << 4;
266 279 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
267 280 if ((_dev_->CR1 & USART_CR1_OVER8) != (uint16_t)0)
268 281 {
269 282 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
270 283 }
271 284 else
272 285 {
273 286 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
274 287 }
275 288 _dev_->BRR = (uint16_t)tmpreg;
276 289 return 1;
277 290 }
278 291 return -1;
279 292 }
280 293
281 294 int uartsetparity(uart_t uart,uartparity_t parity)
282 295 {
283 296 if((uart<6)&&(uart>=0))
284 297 {
285 298 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
286 299 _dev_->CR1 &= ~(((1<<9)+(1<<10)));
287 300 switch(parity)
288 301 {
289 302 case uartparityeven:
290 303 _dev_->CR1 |= (1<<10);
291 304 break;
292 305 case uartparityodd:
293 306 _dev_->CR1 |= (1<<10) + (1<<9);
294 307 break;
295 308 case uartparitynone:
296 309 break;
297 310 default :
298 311 return 0;
299 312 break;
300 313 }
301 314 return 1;
302 315 }
303 316 return -1;
304 317 }
305 318
306 319 int uartsetdatabits(uart_t uart,uartbits_t databits)
307 320 {
308 321 if((uart<6)&&(uart>=0))
309 322 {
310 323 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
311 324 _dev_->CR1 &= ~(((1<<12)));
312 325 switch(databits)
313 326 {
314 327 case uart7bits:
315 328 return 0;
316 329 break;
317 330 case uart8bits:
318 331 break;
319 332 case uart9bits:
320 333 _dev_->CR1 |= (1<<12);
321 334 break;
322 335 default :
323 336 return 0;
324 337 break;
325 338 }
326 339 return 1;
327 340 }
328 341 return -1;
329 342 }
330 343
331 344 int uartsetstopbits(uart_t uart,uartstopbits_t stopbits)
332 345 {
333 346 if((uart<6)&&(uart>=0))
334 347 {
335 348 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
336 349 _dev_->CR2 &= ~(((1<<12)+(1<<13)));
337 350 switch(stopbits)
338 351 {
339 352 case uarthalfstop:
340 353 _dev_->CR2 |= (1<<12);
341 354 break;
342 355 case uartonestop:
343 356 break;
344 357 case uartonehalfstop:
345 358 _dev_->CR2 |= (1<<12) + (1<<13);
346 359 break;
347 360 case uarttwostop:
348 361 _dev_->CR2 |= (1<<13);
349 362 break;
350 363 default :
351 364 return 0;
352 365 break;
353 366 }
354 367 return 1;
355 368 }
356 369 return -1;
357 370 }
358 371
372 #ifdef UART_USES_DMA
359 373 int uartputc(uart_t uart,char c)
360 374 {
361 375 if((uart<6)&&(uart>=0))
362 376 {
363 377 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
364 378 while((_dev_->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7));
365 379 _dev_->DR = c;
366 380 return 1;
367 381 }
368 382 return -1;
369 383 }
370
384 #else
385 int uartputc(uart_t uart,char c)
386 {
387 if((uart<6)&&(uart>=0))
388 {
389 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
390 while((_dev_->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7));
391 _dev_->DR = c;
392 return 1;
393 }
394 return -1;
395 }
396 #endif
371 397 char uartgetc(uart_t uart)
372 398 {
373 399 if((uart<6)&&(uart>=0))
374 400 {
375 401 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
376 402 while(!(_dev_->SR & (1<<5)));
377 403 return (char)_dev_->DR;
378 404 }
379 405 return -1;
380 406 }
381 407
382 408 int uartputs(uart_t uart,char* s)
383 409 {
384 410 while (*s) uartputc(uart,*s++);
385 411 return 1;
386 412 }
387 413
388 414 int uartgets(uart_t uart,char* s)
389 415 {
390 416 do
391 417 {
392 418 (*s) = uartgetc(uart);
393 419 }
394 420 while(*s++);
395 421 return 1;
396 422 }
397 423
398 424 int uartputnc(uart_t uart,char* c,int n)
399 425 {
400 426 int l=0;
401 427 while(l<n)
402 428 {
403 429 uartputc(uart,*c++);
404 430 l++;
405 431 }
406 432 return n;
407 433 }
408 434
409 435 int uartgetnc(uart_t uart,char* c,int n)
410 436 {
411 437 int l=0;
412 438 while(l<n)
413 439 {
414 440 *c++=uartgetc(uart);
415 441 l++;
416 442 }
417 443 return n;
418 444 }
419 445
420 446 int uartavailiabledata(uart_t uart)
421 447 {
422 448 if((uart<6)&&(uart>=0))
423 449 {
424 450 USART_TypeDef* _dev_ = _uart_dev_table[(int)uart];
425 451 if(!(_dev_->SR & (1<<5)))
426 452 return 0;
427 453 else
428 454 return 1;
429 455 }
430 456 return -1;
431 457 }
432 458
433 459
434 460 int _uartstrwrite(streamdevice* device,void* data,int size, int n)
435 461 {
436 462 return uartputnc((uart_t) device->_stream,(char*) data,size*n);
437 463 }
438 464
439 465 int _uartstrread(streamdevice* device,void* data,int size, int n)
440 466 {
441 467 return uartgetnc((uart_t) device->_stream,(char*) data,size*n);
442 468 }
443 469
444 470 int _uartstrsetpos(streamdevice* device,int pos)
445 471 {
446 472 return 1;
447 473 }
448 474
449 475 int uartmkstreamdev(uart_t uart,streamdevice* strdev)
450 476 {
451 477 strdev->_stream = (UHANDLE)uart;/*
452 478 strdev->write = (write_t)&_uartstrwrite;
453 479 strdev->read = (read_t)&_uartstrread;
454 480 strdev->setpos = (setpos_t)&_uartstrsetpos;*/
455 481 strdev->ops = &UART_OPS;
456 482 strdev->streamPt = 0;
457 483 return 1;
458 484 }
459 485
460 486
461 487
462 488
463 489
464 490
@@ -1,10 +1,12
1 1 TEMPLATE = lib
2 2 CONFIG += libuc2lib
3 3
4 4 SOURCES += uart.c
5 5
6 6 UCMODEL=stm32f4
7 7
8 #DEFINES += UART_USES_DMA
9
8 10 HEADERS += \
9 11 ../../../include/PERIPHERALS/uart.h
10 12
General Comments 0
You need to be logged in to leave comments. Login now