@@ -0,0 +1,18 | |||
|
1 | TEMPLATE = lib | |
|
2 | ||
|
3 | TARGET=bsp | |
|
4 | OBJECTS_DIR = obj | |
|
5 | ||
|
6 | BSP = OPLAYER | |
|
7 | ||
|
8 | UCMODEL=stm32f4 | |
|
9 | ||
|
10 | SOURCES += bsp.c | |
|
11 | ||
|
12 | HEADERS += bsp.h | |
|
13 | ||
|
14 | ||
|
15 | BSPFILE = bsp.pri | |
|
16 | ||
|
17 | ||
|
18 |
@@ -0,0 +1,423 | |||
|
1 | /*------------------------------------------------------------------------------ | |
|
2 | -- This file is a part of the libuc, microcontroler library | |
|
3 | -- Copyright (C) 2011, Alexis Jeandet | |
|
4 | -- | |
|
5 | -- This program is free software; you can redistribute it and/or modify | |
|
6 | -- it under the terms of the GNU General Public License as published by | |
|
7 | -- the Free Software Foundation; either version 3 of the License, or | |
|
8 | -- (at your option) any later version. | |
|
9 | -- | |
|
10 | -- This program is distributed in the hope that it will be useful, | |
|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
13 | -- GNU General Public License for more details. | |
|
14 | -- | |
|
15 | -- You should have received a copy of the GNU General Public License | |
|
16 | -- along with this program; if not, write to the Free Software | |
|
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
|
18 | ------------------------------------------------------------------------------- | |
|
19 | -- Author : Alexis Jeandet | |
|
20 | -- Mail : alexis.jeandet@gmail.com | |
|
21 | -------------------------------------------------------------------------------*/ | |
|
22 | #include "bsp.h" | |
|
23 | #include <streamdevices.h> | |
|
24 | #include <malloc.h> | |
|
25 | #include <gpio.h> | |
|
26 | #include <uart.h> | |
|
27 | #include <stdio.h> | |
|
28 | #include <stm32f4xx_gpio.h> | |
|
29 | #include <stm32f4xx_fsmc.h> | |
|
30 | #include <i2c.h> | |
|
31 | #include <core.h> | |
|
32 | #include <terminal.h> | |
|
33 | #include <sdcard-spi.h> | |
|
34 | #include <sdcard.h> | |
|
35 | #include <mbr.h> | |
|
36 | #include <VS10XX.h> | |
|
37 | ||
|
38 | uint32_t OSC0 =8000000; | |
|
39 | uint32_t INTOSC =16000000; | |
|
40 | uint32_t RTCOSC =32768; | |
|
41 | uint32_t currentCpuFreq=0; | |
|
42 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; | |
|
43 | ||
|
44 | LCD_IF_t lcdIF0={ | |
|
45 | .init = &bsp_FSMC_init, | |
|
46 | .writereg = &bsp_lcd0_write_reg, | |
|
47 | .readreg = &bsp_lcd0_read_reg, | |
|
48 | .writeGRAM = &bsp_lcd0_writeGRAM, | |
|
49 | .readGRAM = &bsp_lcd0_readGRAM | |
|
50 | }; | |
|
51 | ||
|
52 | LCD_t lcd0={ | |
|
53 | .interface = &lcdIF0, | |
|
54 | .init = &ili9328init, | |
|
55 | .paint = &ili9328paint, | |
|
56 | .paintText = &ili9328paintText, | |
|
57 | .paintFilRect = &ili9328paintFilRect, | |
|
58 | .refreshenable = &ili9328refreshenable, | |
|
59 | .width= 240, | |
|
60 | .height = 320 | |
|
61 | }; | |
|
62 | ||
|
63 | terminal_t terminal0; | |
|
64 | ||
|
65 | volatile int8_t* lcd0_CMD=(volatile int8_t*) 0x60000000; | |
|
66 | volatile int8_t* lcd0_DATA=(volatile int8_t*)(0x61FFFFF0); | |
|
67 | ||
|
68 | float VREF0 =(float)3.3; | |
|
69 | volatile vs10XXDev audioCodec0; | |
|
70 | ||
|
71 | int bsp_init() | |
|
72 | { | |
|
73 | int i=0; | |
|
74 | for(i=0;i<__MAX_OPENED_FILES__;i++) | |
|
75 | { | |
|
76 | __opnfiles__[i] = NULL; | |
|
77 | } | |
|
78 | bsp_GPIO_init(); | |
|
79 | bsp_uart_init(); | |
|
80 | bsp_iic_init(); | |
|
81 | bsp_FSMC_init(); | |
|
82 | bsp_GTerm_init(); | |
|
83 | bsp_spi_init(); | |
|
84 | bsp_SD_init(); | |
|
85 | bsp_Audio_init(); | |
|
86 | printf("\r=====================\n\r"); | |
|
87 | printf( "=====================\n\r"); | |
|
88 | printf(BSP); | |
|
89 | printf(" initialised\n\r"); | |
|
90 | printf( "=====================\n\r"); | |
|
91 | return 1; | |
|
92 | } | |
|
93 | ||
|
94 | void bsp_GPIO_init() | |
|
95 | { | |
|
96 | gpio_t GPIO_Out_init_List[]={LED1,LED2,LCD_RESET,LCD_BACKL,VS1053xCS,VS1053xDCS,VS1053xRESET,SDCARD2CS}; | |
|
97 | for(int i=0;i<8;i++) | |
|
98 | { | |
|
99 | gpio_t GPIO_init = gpioopen(GPIO_Out_init_List[i]); | |
|
100 | GPIO_init |= gpiohighspeed | gpiooutdir | gpiopushpulltype; | |
|
101 | gpiosetconfig(&GPIO_init); | |
|
102 | } | |
|
103 | gpio_t GPIO_In_init_List[]={VS1053DREQ,SDCARD2CD}; | |
|
104 | for(int i=0;i<2;i++) | |
|
105 | { | |
|
106 | gpio_t GPIO_init = gpioopen(GPIO_In_init_List[i]); | |
|
107 | GPIO_init |= gpiohighspeed | gpioindir; | |
|
108 | gpiosetconfig(&GPIO_init); | |
|
109 | } | |
|
110 | gpioclr(VS1053xRESET); | |
|
111 | gpioset(VS1053xCS); | |
|
112 | gpioset(VS1053xDCS); | |
|
113 | gpioset(SDCARD2CS); | |
|
114 | gpioclr(LCD_RESET); | |
|
115 | gpioclr(LCD_BACKL); | |
|
116 | } | |
|
117 | ||
|
118 | void bsp_uart_init() | |
|
119 | { | |
|
120 | ||
|
121 | } | |
|
122 | ||
|
123 | /* | |
|
124 | D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7 | |
|
125 | D5 PE8 D6 PE9 D7 PE10 | |
|
126 | A20 PE4 = RS FSMC_NE1 PD7 CS FSMC_NWE PD5 W/S | |
|
127 | FSMC_NOE PD4 RD | |
|
128 | */ | |
|
129 | ||
|
130 | int bsp_FSMC_init() | |
|
131 | { | |
|
132 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) | |
|
133 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) | |
|
134 | ||
|
135 | gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\ | |
|
136 | ,PD4,PD5,PD7,PE4}; | |
|
137 | for(int i=0;i<12;i++) | |
|
138 | { | |
|
139 | gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]); | |
|
140 | LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | |
|
141 | gpiosetconfig(&LCD_DBx); | |
|
142 | GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC); | |
|
143 | } | |
|
144 | ||
|
145 | FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; | |
|
146 | FSMC_NORSRAMTimingInitTypeDef p; | |
|
147 | ||
|
148 | /* Enable FSMC clock */ | |
|
149 | RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); | |
|
150 | ||
|
151 | /*-- FSMC Configuration ------------------------------------------------------*/ | |
|
152 | /*----------------------- SRAM Bank 3 ----------------------------------------*/ | |
|
153 | /* FSMC_Bank1_NORSRAM4 configuration */ | |
|
154 | //p.FSMC_AddressSetupTime = 5; | |
|
155 | //p.FSMC_AddressSetupTime = 1; | |
|
156 | p.FSMC_AddressSetupTime = 3; | |
|
157 | p.FSMC_AddressHoldTime = 3; | |
|
158 | //p.FSMC_DataSetupTime = 9; | |
|
159 | //ili9328 -> data setup time > 10ns | |
|
160 | p.FSMC_DataSetupTime = 1; | |
|
161 | if(getCpuFreq()>100*1000*1000) | |
|
162 | p.FSMC_DataSetupTime = 2;// 11; | |
|
163 | p.FSMC_BusTurnAroundDuration = 0; | |
|
164 | p.FSMC_CLKDivision = 0; | |
|
165 | p.FSMC_DataLatency = 0; | |
|
166 | //ili9328 -> data hold time > 15ns | |
|
167 | if(getCpuFreq()>66*1000*1000) | |
|
168 | p.FSMC_DataLatency = 0; | |
|
169 | p.FSMC_AccessMode = FSMC_AccessMode_A; | |
|
170 | ||
|
171 | FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; | |
|
172 | FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; | |
|
173 | FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; | |
|
174 | FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b; | |
|
175 | FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; | |
|
176 | FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; | |
|
177 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; | |
|
178 | FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; | |
|
179 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; | |
|
180 | FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; | |
|
181 | FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; | |
|
182 | FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; | |
|
183 | FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; | |
|
184 | FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; | |
|
185 | FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; | |
|
186 | ||
|
187 | FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); | |
|
188 | ||
|
189 | FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); | |
|
190 | gpioset(LCD_RESET); | |
|
191 | gpioclr(LCD_RESET); | |
|
192 | delay_100us(500); | |
|
193 | gpioset(LCD_RESET); | |
|
194 | delay_100us(500); | |
|
195 | lcd0.init(&lcd0); | |
|
196 | gpioset(LCD_BACKL); | |
|
197 | return 1; | |
|
198 | } | |
|
199 | ||
|
200 | void bsp_spi_init() | |
|
201 | { | |
|
202 | gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO}; | |
|
203 | for(int i=0;i<3;i++) | |
|
204 | { | |
|
205 | gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]); | |
|
206 | SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | |
|
207 | gpiosetconfig(&SPI_DBx); | |
|
208 | GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1); | |
|
209 | } | |
|
210 | spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1); | |
|
211 | ||
|
212 | gpio_t SDSPI_DBxList[]={SDCARD2SCK,SDCARD2MOSI,SDCARD2MISO}; | |
|
213 | for(int i=0;i<3;i++) | |
|
214 | { | |
|
215 | gpio_t SPI_DBx = gpioopen(SDSPI_DBxList[i]); | |
|
216 | SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | |
|
217 | gpiosetconfig(&SPI_DBx); | |
|
218 | GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI3); | |
|
219 | } | |
|
220 | spiopenandconfig(SDCARD2SPI,spi8bits|spimaster|spimsbfirst,400*1000,SDCARD2MOSI,SDCARD2MISO,SDCARD2SCK,-1); | |
|
221 | ||
|
222 | } | |
|
223 | ||
|
224 | ||
|
225 | void bsp_iic_init() | |
|
226 | { | |
|
227 | // i2copenandconfig(i2c2,0,10000,PF0,PF1); | |
|
228 | } | |
|
229 | ||
|
230 | ||
|
231 | void bsp_Audio_init() | |
|
232 | { | |
|
233 | vs10XXopen(&audioCodec0,VS1053SPI,vs1052setXCS,vs1052setRST,vs1052setXDCS,vs10XXDREQ); | |
|
234 | } | |
|
235 | ||
|
236 | void bsp_SD_init() | |
|
237 | { | |
|
238 | sdcardDev sdcard2; | |
|
239 | blkdevice sdcard2blkdev; | |
|
240 | dikpartition sdcard2Part1; | |
|
241 | sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed); | |
|
242 | sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected); | |
|
243 | sdcard2blkdev.initialize(&sdcard2blkdev); | |
|
244 | mbropen(&sdcard2blkdev,&sdcard2Part1,1); | |
|
245 | } | |
|
246 | ||
|
247 | void vs1052setXCS(char val) | |
|
248 | { | |
|
249 | gpiosetval(VS1053xCS,(int)val); | |
|
250 | } | |
|
251 | ||
|
252 | void vs1052setXDCS(char val) | |
|
253 | { | |
|
254 | gpiosetval(VS1053xDCS,(int)val); | |
|
255 | } | |
|
256 | ||
|
257 | void vs1052setRST(char val) | |
|
258 | { | |
|
259 | if(val) | |
|
260 | gpioset(VS1053xRESET); | |
|
261 | else | |
|
262 | gpioclr(VS1053xRESET); | |
|
263 | } | |
|
264 | ||
|
265 | int vs10XXDREQ() | |
|
266 | { | |
|
267 | return gpiogetval(VS1053DREQ); | |
|
268 | } | |
|
269 | ||
|
270 | ||
|
271 | void bsppowersdcard(char onoff) //always ON | |
|
272 | { | |
|
273 | ||
|
274 | } | |
|
275 | ||
|
276 | char bspsdcardpresent() | |
|
277 | { | |
|
278 | return gpiogetval(SDCARD2CD); | |
|
279 | } | |
|
280 | ||
|
281 | char bspsdcardwriteprotected() | |
|
282 | { | |
|
283 | return 0; | |
|
284 | } | |
|
285 | ||
|
286 | void bspsdcardselect(char YESNO) | |
|
287 | { | |
|
288 | if(YESNO) | |
|
289 | gpioclr(SDCARD2CS); | |
|
290 | else | |
|
291 | gpioset(SDCARD2CS); | |
|
292 | } | |
|
293 | ||
|
294 | ||
|
295 | void bsp_lcd0_write_reg(uint32_t reg,uint32_t data) | |
|
296 | { | |
|
297 | uint8_t* pt8 = (uint8_t*)(void*)® | |
|
298 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | |
|
299 | *lcd0_CMD=pt8[3]; | |
|
300 | *lcd0_CMD=pt8[2]; | |
|
301 | pt8 = (uint8_t*)(void*)&data; | |
|
302 | *lcd0_DATA=pt8[3]; | |
|
303 | *lcd0_DATA=pt8[2]; | |
|
304 | #else | |
|
305 | *lcd0_CMD=pt8[1]; | |
|
306 | *lcd0_CMD=pt8[0]; | |
|
307 | pt8 = (uint8_t*)(void*)&data; | |
|
308 | *lcd0_DATA=pt8[1]; | |
|
309 | *lcd0_DATA=pt8[0]; | |
|
310 | #endif | |
|
311 | ||
|
312 | } | |
|
313 | ||
|
314 | uint32_t bsp_lcd0_read_reg(uint32_t reg) | |
|
315 | { | |
|
316 | uint8_t* pt8 = (uint8_t*)(void*)® | |
|
317 | uint32_t DATA=0; | |
|
318 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | |
|
319 | *lcd0_CMD=pt8[3]; | |
|
320 | *lcd0_CMD=pt8[2]; | |
|
321 | pt8 = (uint8_t*)(void*)&DATA; | |
|
322 | pt8[3]=*lcd0_DATA; | |
|
323 | pt8[2]=*lcd0_DATA; | |
|
324 | #else | |
|
325 | ||
|
326 | *lcd0_CMD=pt8[1]; | |
|
327 | *lcd0_CMD=pt8[0]; | |
|
328 | pt8 = (uint8_t*)(void*)&DATA; | |
|
329 | pt8[1]=*lcd0_DATA; | |
|
330 | pt8[0]=*lcd0_DATA; | |
|
331 | #endif | |
|
332 | ||
|
333 | return DATA; | |
|
334 | } | |
|
335 | ||
|
336 | void bsp_lcd0_writeGRAM(void* buffer,uint32_t count) | |
|
337 | { | |
|
338 | uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM; | |
|
339 | uint8_t* pt8 = (uint8_t*)(void*)® | |
|
340 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | |
|
341 | *lcd0_CMD=pt8[3]; | |
|
342 | *lcd0_CMD=pt8[2]; | |
|
343 | pt8 = (uint8_t*)(void*)buffer; | |
|
344 | for(int i=0;i<(int)count;i++) | |
|
345 | { | |
|
346 | *lcd0_DATA=pt8[(2*i) +1]; | |
|
347 | *lcd0_DATA=pt8[2*i]; | |
|
348 | } | |
|
349 | #else | |
|
350 | ||
|
351 | *lcd0_CMD=pt8[1]; | |
|
352 | *lcd0_CMD=pt8[0]; | |
|
353 | pt8 = (uint8_t*)(void*)buffer; | |
|
354 | for(int i=0;i<(int)count;i++) | |
|
355 | { | |
|
356 | ||
|
357 | *lcd0_DATA=pt8[(2*i) +1]; | |
|
358 | *lcd0_DATA=pt8[2*i]; | |
|
359 | } | |
|
360 | #endif | |
|
361 | } | |
|
362 | ||
|
363 | void bsp_lcd0_readGRAM(void* buffer,uint32_t count) | |
|
364 | { | |
|
365 | uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM; | |
|
366 | uint8_t* pt8 = (uint8_t*)(void*)® | |
|
367 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | |
|
368 | *lcd0_CMD=pt8[3]; | |
|
369 | *lcd0_CMD=pt8[2]; | |
|
370 | pt8 = (uint8_t*)(void*)buffer; | |
|
371 | pt8[0] = *lcd0_DATA; | |
|
372 | pt8[0] = *lcd0_DATA; | |
|
373 | for(int i=0;i<(int)count;i++) | |
|
374 | { | |
|
375 | pt8[(2*i) +1]=*lcd0_DATA; | |
|
376 | pt8[2*i]=*lcd0_DATA; | |
|
377 | } | |
|
378 | #else | |
|
379 | ||
|
380 | *lcd0_CMD=pt8[1]; | |
|
381 | *lcd0_CMD=pt8[0]; | |
|
382 | pt8 = (uint8_t*)(void*)buffer; | |
|
383 | pt8[0] = *lcd0_DATA; | |
|
384 | pt8[0] = *lcd0_DATA; | |
|
385 | for(int i=0;i<(int)count;i++) | |
|
386 | { | |
|
387 | pt8[(2*i) +1]=*lcd0_DATA; | |
|
388 | pt8[2*i]=*lcd0_DATA; | |
|
389 | } | |
|
390 | #endif | |
|
391 | } | |
|
392 | ||
|
393 | void bsp_GTerm_init() | |
|
394 | { | |
|
395 | if(__opnfiles__[1]==NULL) | |
|
396 | { | |
|
397 | //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); | |
|
398 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); | |
|
399 | terminal_init(&terminal0 ,&lcd0,&ComicSansMS_8,fd1); | |
|
400 | __opnfiles__[1] = fd1; | |
|
401 | } | |
|
402 | else | |
|
403 | { | |
|
404 | ||
|
405 | } | |
|
406 | } | |
|
407 | ||
|
408 | ||
|
409 | ||
|
410 | ||
|
411 | ||
|
412 | ||
|
413 | ||
|
414 | ||
|
415 | ||
|
416 | ||
|
417 | ||
|
418 | ||
|
419 | ||
|
420 | ||
|
421 | ||
|
422 | ||
|
423 |
@@ -0,0 +1,116 | |||
|
1 | /*------------------------------------------------------------------------------ | |
|
2 | -- This file is a part of the libuc, microcontroler library | |
|
3 | -- Copyright (C) 2011, Alexis Jeandet | |
|
4 | -- | |
|
5 | -- This program is free software; you can redistribute it and/or modify | |
|
6 | -- it under the terms of the GNU General Public License as published by | |
|
7 | -- the Free Software Foundation; either version 3 of the License, or | |
|
8 | -- (at your option) any later version. | |
|
9 | -- | |
|
10 | -- This program is distributed in the hope that it will be useful, | |
|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
13 | -- GNU General Public License for more details. | |
|
14 | -- | |
|
15 | -- You should have received a copy of the GNU General Public License | |
|
16 | -- along with this program; if not, write to the Free Software | |
|
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
|
18 | ------------------------------------------------------------------------------- | |
|
19 | -- Author : Alexis Jeandet | |
|
20 | -- Mail : alexis.jeandet@gmail.com | |
|
21 | -------------------------------------------------------------------------------*/ | |
|
22 | #ifndef BSP_H | |
|
23 | #define BSP_H | |
|
24 | #include <stm32f4xx.h> | |
|
25 | #include <stm32f4xx_gpio.h> | |
|
26 | #include <stm32f4xx_rcc.h> | |
|
27 | #include <gpio.h> | |
|
28 | #include <spi.h> | |
|
29 | #include <ili9328.h> | |
|
30 | #include <genericLCD_Controler.h> | |
|
31 | #include <VS10XX.h> | |
|
32 | ||
|
33 | #define __MAX_OPENED_FILES__ 4 | |
|
34 | #define __FS_ROOT_SIZE__ 4 | |
|
35 | ||
|
36 | ||
|
37 | #define LED1 PC15 | |
|
38 | #define LED2 PC14 | |
|
39 | ||
|
40 | #define LCD_RESET PD10 | |
|
41 | #define LCD_BACKL PE11 | |
|
42 | ||
|
43 | #define VS1053SPI spi1 | |
|
44 | #define VS1053xCS PA3 | |
|
45 | #define VS1053xRESET PA2 | |
|
46 | #define VS1053xDCS PA4 | |
|
47 | #define VS1053DREQ PA1 | |
|
48 | #define VS1053MISO PA6 | |
|
49 | #define VS1053MOSI PA7 | |
|
50 | #define VS1053SCK PA5 | |
|
51 | ||
|
52 | ||
|
53 | ||
|
54 | #define SDCARD2SPI spi3 | |
|
55 | #define SDCARD2CS PB8 | |
|
56 | #define SDCARD2CD PB9 | |
|
57 | #define SDCARD2MISO PB4 | |
|
58 | #define SDCARD2MOSI PB5 | |
|
59 | #define SDCARD2SCK PB3 | |
|
60 | ||
|
61 | extern float VREF0; | |
|
62 | ||
|
63 | extern uint32_t currentCpuFreq; | |
|
64 | extern LCD_t lcd0; | |
|
65 | ||
|
66 | extern volatile vs10XXDev audioCodec0; | |
|
67 | ||
|
68 | extern int bsp_init(); | |
|
69 | ||
|
70 | extern void bsp_GPIO_init(); | |
|
71 | extern void bsp_uart_init(); | |
|
72 | extern void bsp_iic_init(); | |
|
73 | extern void bsp_spi_init(); | |
|
74 | extern void bsp_SD_init(); | |
|
75 | extern void bsp_Audio_init(); | |
|
76 | extern void bsp_GTerm_init(); | |
|
77 | extern int bsp_FSMC_init(); | |
|
78 | ||
|
79 | /* VS1053 */ | |
|
80 | extern void vs1052setXCS(char val); | |
|
81 | extern void vs1052setXDCS(char val); | |
|
82 | extern void vs1052setRST(char val); | |
|
83 | extern int vs10XXDREQ(); | |
|
84 | ||
|
85 | /* SD CARD */ | |
|
86 | void bsppowersdcard(char onoff); | |
|
87 | char bspsdcardpresent(); | |
|
88 | void bspsdcardselect(char YESNO); | |
|
89 | char bspsdcardwriteprotected(); | |
|
90 | ||
|
91 | ||
|
92 | void bsp_lcd0_write_reg(uint32_t reg,uint32_t data); | |
|
93 | uint32_t bsp_lcd0_read_reg(uint32_t reg); | |
|
94 | void bsp_lcd0_writeGRAM(void *buffer, uint32_t count); | |
|
95 | void bsp_lcd0_readGRAM(void *buffer, uint32_t count); | |
|
96 | ||
|
97 | #endif | |
|
98 | ||
|
99 | ||
|
100 | ||
|
101 | ||
|
102 | ||
|
103 | ||
|
104 | ||
|
105 | ||
|
106 | ||
|
107 | ||
|
108 | ||
|
109 | ||
|
110 | ||
|
111 | ||
|
112 | ||
|
113 | ||
|
114 | ||
|
115 | ||
|
116 |
@@ -1,16 +1,17 | |||
|
1 | 1 | TEMPLATE = subdirs |
|
2 | 2 | CONFIG += ordered |
|
3 | 3 | SUBDIRS += STM32F4Discovery \ |
|
4 | 4 | M4Stick \ |
|
5 | 5 | M4StickV2 \ |
|
6 | 6 | SOLAR_LFR_PSU \ |
|
7 | 7 | BEAGLESYNTH \ |
|
8 | 8 | STM32F4Eval \ |
|
9 | STM32F4Discovery-EXT | |
|
9 | STM32F4Discovery-EXT \ | |
|
10 | OPLAYER | |
|
10 | 11 | |
|
11 | 12 | |
|
12 | 13 | |
|
13 | 14 | |
|
14 | 15 | |
|
15 | 16 | |
|
16 | 17 |
@@ -1,10 +1,12 | |||
|
1 | 1 | TEMPLATE = app |
|
2 | 2 | CONFIG += cpu |
|
3 | 3 | |
|
4 | 4 | |
|
5 | 5 | UCMODEL=stm32f4 |
|
6 | BSP = BEAGLESYNTH | |
|
6 | BSP = OPLAYER | |
|
7 | ||
|
8 | DEFINES += CPUFREQ=160000000 | |
|
7 | 9 | |
|
8 | 10 | SOURCES += \ |
|
9 | 11 | main.c |
|
10 | 12 |
@@ -1,42 +1,54 | |||
|
1 | 1 | #include <stdio.h> |
|
2 | 2 | #include <fat32.h> |
|
3 | 3 | #include <gpio.h> |
|
4 | 4 | #include <uart.h> |
|
5 | 5 | #include <stm32f4xx.h> |
|
6 | 6 | #include <bsp.h> |
|
7 | 7 | #include <core.h> |
|
8 | #include <VS10XX.h> | |
|
9 | #include <bsp.h> | |
|
8 | 10 | |
|
9 | 11 | extern streamdevice* __opnfiles__[]; |
|
10 | 12 | |
|
13 | void randBoxesDemo() | |
|
14 | { | |
|
15 | int16_t x,y,w,h,t,r; | |
|
16 | x=rand()%240; | |
|
17 | y=rand()%320; | |
|
18 | w=rand()%(240-x); | |
|
19 | if(x>y) | |
|
20 | r=(rand()%(y))%(320-y); | |
|
21 | else | |
|
22 | r=(rand()%(x))%(240-x); | |
|
23 | h=rand()%(320-y); | |
|
24 | t=rand()%(10); | |
|
25 | ili9328paintFilRect(&lcd0,x,y,w,h,rand(),t,rand()); | |
|
26 | //ili9328paintFilCirc(&lcd0,x,y,r,rand(),t,rand()); | |
|
27 | //delay_100us(10); | |
|
28 | //for(volatile int i=0;i<(1024*2);i++); | |
|
29 | } | |
|
30 | ||
|
11 | 31 | int main() |
|
12 | 32 | { |
|
13 | 33 | while(1) |
|
14 | 34 | { |
|
15 | gpioset(LED3); | |
|
35 | // printf("Audio Codec reg0=0x%x\n",vs10XXcmdread(&audioCodec0,0)); | |
|
36 | // printf("hello "); | |
|
37 | // bsp_lcd0_write_reg(1,0x1234); | |
|
38 | // delay_100us(10000); | |
|
39 | // randBoxesDemo(); | |
|
40 | gpioset(LED1); | |
|
16 | 41 | delay_100us(10000); |
|
17 |
gpioclr(LED |
|
|
42 | gpioclr(LED1); | |
|
18 | 43 | delay_100us(10000); |
|
44 | ||
|
19 | 45 | } |
|
20 | 46 | printf("hello world\n\r"); |
|
21 | 47 | return 0; |
|
22 | 48 | } |
|
23 | 49 | |
|
24 | 50 | |
|
25 | 51 | |
|
26 | void USART3_IRQHandler(void) | |
|
27 | { | |
|
28 | while(1) | |
|
29 | { | |
|
30 | for(volatile int i=0;i<1024*2048;i++); | |
|
31 | gpioset(PD12); | |
|
32 | gpioclr(PD14); | |
|
33 | for(volatile int i=0;i<1024*2048;i++); | |
|
34 | gpioclr(PD12); | |
|
35 | gpioset(PD14); | |
|
36 | } | |
|
37 | } | |
|
38 | 52 | |
|
39 | 53 | |
|
40 | 54 | |
|
41 | ||
|
42 |
@@ -1,56 +1,72 | |||
|
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 |
#include |
|
|
23 |
#include |
|
|
22 | #include <spi.h> | |
|
23 | #include <VS10XX.h> | |
|
24 | #include <core.h> | |
|
24 | 25 | //#include <bsp.h> |
|
25 | 26 | |
|
26 | void vs10XXopen(vs10XXDev* codec,spi_t dev){ | |
|
27 | void vs10XXopen(vs10XXDev *codec, spi_t dev, void (*setxCS)(char), void (*setxRST)(char), void (*setxDCS)(char), int (*getDREQ)()) | |
|
28 | { | |
|
27 | 29 | codec->SPIdev = dev; |
|
28 | ||
|
30 | codec->setxCS = setxCS; | |
|
31 | codec->setxDCS = setxDCS; | |
|
32 | codec->setxRST = setxRST; | |
|
33 | codec->getDREQ = getDREQ; | |
|
34 | codec->setxDCS(1); | |
|
35 | codec->setxCS(1); | |
|
36 | codec->setxRST(0); | |
|
37 | delay_100us(2); | |
|
38 | codec->setxRST(1); | |
|
29 | 39 | } |
|
30 | 40 | |
|
31 | 41 | void vs10XXsoftreset(vs10XXDev* dev) |
|
32 | 42 | { |
|
33 | 43 | vs10XXcmdwrite(dev,VSMODE,(1<<2)); |
|
34 | 44 | } |
|
35 | 45 | |
|
36 | 46 | int vs10XXcmdread(vs10XXDev* dev,char address) |
|
37 | 47 | { |
|
38 | 48 |
|
|
39 |
|
|
|
40 | vs10XXclearXCS(); | |
|
41 |
spiputw(dev->SPIdev, |
|
|
42 |
|
|
|
43 | vs10XXsetXCS(); | |
|
49 | while(!dev->getDREQ()); | |
|
50 | dev->setxCS(0); | |
|
51 | spiputw(dev->SPIdev,3); | |
|
52 | spiputw(dev->SPIdev,address); | |
|
53 | result = (0xFF00 & (spigetw(dev->SPIdev)<<8)) + (0xFF & spigetw(dev->SPIdev)); | |
|
54 | dev->setxCS(1); | |
|
44 | 55 | return result; |
|
45 | 56 | } |
|
46 | 57 | |
|
47 | 58 | |
|
48 | 59 | void vs10XXcmdwrite(vs10XXDev* dev,char address,int value) |
|
49 | 60 | { |
|
50 |
|
|
|
51 | vs10XXclearXCS(); | |
|
52 |
spiputw(dev->SPIdev, |
|
|
61 | while(!dev->getDREQ()); | |
|
62 | dev->setxCS(0); | |
|
63 | spiputw(dev->SPIdev,2<<8); | |
|
64 | spiputw(dev->SPIdev,address); | |
|
65 | spiputw(dev->SPIdev,value>>8); | |
|
53 | 66 | spiputw(dev->SPIdev,value); |
|
54 | vs10XXsetXCS(); | |
|
67 | dev->setxCS(1); | |
|
55 | 68 | } |
|
56 | 69 | |
|
70 | ||
|
71 | ||
|
72 |
@@ -1,14 +1,15 | |||
|
1 | 1 | TEMPLATE = lib |
|
2 | 2 | OBJECTS_DIR = obj |
|
3 | 3 | TARGET = vs10XX |
|
4 | 4 | |
|
5 | 5 | SOURCES += VS10XX.c |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | HEADERS += ../../../includes/VS10XX.h |
|
9 | INCLUDEPATH += ../../../includes | |
|
9 | 10 | |
|
10 | 11 | UCMODEL=stm32f4 |
|
11 | 12 | |
|
12 | 13 | target.path = $$[QT_INSTALL_LIBS]/$$UCMODEL |
|
13 | 14 | INSTALLS += target |
|
14 | 15 |
@@ -1,494 +1,502 | |||
|
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 | #include <sdcard.h> |
|
23 | 23 | #include <sdcard-spi.h> |
|
24 | #include <stdio.h> | |
|
25 | #include <core.h> | |
|
24 | 26 | |
|
25 | 27 | int sdcardselect (blkdeviceptr _this); |
|
26 | 28 | void sdcarddeselect (blkdeviceptr _this); |
|
27 | 29 | int sdcardwait_ready (sdcardDev* sdcard); |
|
28 | 30 | int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token); |
|
29 | 31 | int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr); |
|
30 | 32 | char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg); |
|
31 | 33 | |
|
32 | 34 | DSTATUS sdcarddisk_status (blkdeviceptr _this); |
|
33 | 35 | DSTATUS sdcarddisk_initialize (blkdeviceptr _this); |
|
34 | 36 | DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count); |
|
35 | 37 | DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count); |
|
36 | 38 | DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff); |
|
37 | 39 | |
|
38 | 40 | static volatile |
|
39 | 41 | unsigned int Timer1, Timer2; /* 1kHz decrement timer stopped at zero (disk_timerproc()) */ |
|
40 | 42 | |
|
41 | 43 | void sdcarddeselect (blkdeviceptr _this) |
|
42 | 44 | { |
|
43 | 45 | char d; |
|
44 | 46 | _this->select(0); |
|
45 | 47 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); /* Dummy clock (force DO hi-z for multiple slave SPI) */ |
|
46 | 48 | } |
|
47 | 49 | |
|
48 | 50 | int sdcardselect (blkdeviceptr _this) /* 1:OK, 0:Timeout */ |
|
49 | 51 | { |
|
50 | 52 | char d; |
|
51 | 53 | _this->select(1); |
|
52 | 54 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); /* Dummy clock (force DO enabled) */ |
|
53 | 55 | |
|
54 | 56 | if (sdcardwait_ready(((sdcardDev*)_this->phy))) return 1; /* OK */ |
|
55 | 57 | _this->select(0); |
|
56 | 58 | return 0; /* Timeout */ |
|
57 | 59 | } |
|
58 | 60 | |
|
59 | 61 | int sdcardwait_ready (sdcardDev* sdcard) /* 1:OK, 0:Timeout */ |
|
60 | 62 | { |
|
61 |
|
|
|
63 | char d=0; | |
|
62 | 64 | unsigned int tmr; |
|
63 | 65 | volatile unsigned int i=0; |
|
64 | 66 | |
|
65 |
|
|
|
67 | for (tmr = 1000; tmr>0; tmr--) { /* Wait for ready in timeout of 500ms */ | |
|
66 | 68 | sdcard->rcvr_mmc(sdcard->phy,&d, 1); |
|
67 | 69 | if (d == 0xFF) break; |
|
68 | for(i=0;i<128;i++); | |
|
70 | delay_100us(10); | |
|
69 | 71 | } |
|
70 | 72 | |
|
71 |
|
|
|
73 | return tmr != 0; | |
|
72 | 74 | } |
|
73 | 75 | |
|
74 | 76 | int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr) |
|
75 | 77 | { |
|
76 | 78 | char d[2]; |
|
77 | 79 | unsigned int tmr; |
|
78 | 80 | volatile unsigned int i=0; |
|
79 | 81 | for (tmr = 50000; tmr; tmr--) { /* Wait for data packet in timeout of 100ms */ |
|
80 | 82 | sdcard->rcvr_mmc(sdcard->phy,d, 1); |
|
81 | 83 | if (d[0] != 0xFF) break; |
|
82 | 84 | for(i=0;i<64;i++); |
|
83 | 85 | } |
|
84 | 86 | if (d[0] != 0xFE) return 0; /* If not valid data token, return with error */ |
|
85 | 87 | |
|
86 | 88 | sdcard->rcvr_mmc(sdcard->phy,buff, btr); /* Receive the data block into buffer */ |
|
87 | 89 | sdcard->rcvr_mmc(sdcard->phy,d, 2); /* Discard CRC */ |
|
88 | 90 | |
|
89 | 91 | return 1; /* Return with success */ |
|
90 | 92 | } |
|
91 | 93 | |
|
92 | 94 | |
|
93 | 95 | int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token) |
|
94 | 96 | { |
|
95 | 97 | char d[2]; |
|
96 | 98 | |
|
97 | 99 | |
|
98 | 100 | if (!sdcardwait_ready(sdcard)) return 0; |
|
99 | 101 | |
|
100 | 102 | d[0] = token; |
|
101 | 103 | sdcard->xmit_mmc(sdcard->phy,d, 1); /* Xmit a token */ |
|
102 | 104 | if (token != 0xFD) { /* Is it data token? */ |
|
103 | 105 | sdcard->xmit_mmc(sdcard->phy,buff, 512); /* Xmit the 512 byte data block to MMC */ |
|
104 | 106 | sdcard->rcvr_mmc(sdcard->phy,d, 2); /* Xmit dummy CRC (0xFF,0xFF) */ |
|
105 | 107 | sdcard->rcvr_mmc(sdcard->phy,d, 1); /* Receive data response */ |
|
106 | 108 | if ((d[0] & 0x1F) != 0x05) /* If not accepted, return with error */ |
|
107 | 109 | return 0; |
|
108 | 110 | } |
|
109 | 111 | |
|
110 | 112 | return 1; |
|
111 | 113 | } |
|
112 | 114 | |
|
113 | 115 | |
|
114 | 116 | char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg) |
|
115 | 117 | { |
|
116 | 118 | char n, d, buf[6]; |
|
117 | 119 | |
|
118 | 120 | |
|
119 | 121 | if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */ |
|
120 | 122 | cmd &= 0x7F; |
|
121 | 123 | n = sdcardsend_cmd(_this,CMD55, 0); |
|
122 | 124 | if (n > 1) return n; |
|
123 | 125 | } |
|
124 | 126 | /* Select the card and wait for ready */ |
|
125 | 127 | sdcarddeselect(_this); |
|
126 |
|
|
|
128 | if (!sdcardselect(_this)) {printf("Can't select SDCARD\n");return 0xFF;} | |
|
127 | 129 | /* Send a command packet */ |
|
128 | 130 | buf[0] = 0x40 | cmd; /* Start + Command index */ |
|
129 | 131 | buf[1] = (char)(arg >> 24); /* Argument[31..24] */ |
|
130 | 132 | buf[2] = (char)(arg >> 16); /* Argument[23..16] */ |
|
131 | 133 | buf[3] = (char)(arg >> 8); /* Argument[15..8] */ |
|
132 | 134 | buf[4] = (char)arg; /* Argument[7..0] */ |
|
133 | 135 | n = 0x01; /* Dummy CRC + Stop */ |
|
134 | 136 | if (cmd == CMD0) n = 0x95; /* (valid CRC for CMD0(0)) */ |
|
135 | 137 | if (cmd == CMD8) n = 0x87; /* (valid CRC for CMD8(0x1AA)) */ |
|
136 | 138 | if (cmd == CMD55) n = 0x63; |
|
137 | 139 | buf[5] = n; |
|
138 | 140 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,buf, 6); |
|
139 | 141 | /* Receive command response */ |
|
140 | 142 | if (cmd == CMD12) {((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1);} /* Skip a stuff byte when stop reading */ |
|
141 | 143 | n = 10; /* Wait for a valid response in timeout of 10 attempts */ |
|
142 | 144 | do |
|
143 | 145 | { |
|
144 | 146 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); |
|
145 | 147 | //libucprintf("resp=%d\n\r",d); |
|
146 | 148 | } |
|
147 | 149 | while ((d & 0x80) && --n); |
|
148 | 150 | |
|
149 | 151 | return d; /* Return with the response value */ |
|
150 | 152 | } |
|
151 | 153 | |
|
152 | 154 | |
|
153 | 155 | |
|
154 | 156 | |
|
155 | 157 | DSTATUS sdcarddisk_status (blkdeviceptr _this) |
|
156 | 158 | { |
|
157 | 159 | DSTATUS s = ((sdcardDev*)_this->phy)->Stat; |
|
158 | 160 | char ocr[4]; |
|
159 | 161 | |
|
160 | 162 | |
|
161 | 163 | if ((_this==0) || !_this->detect()) { |
|
162 | 164 | s = STA_NODISK | STA_NOINIT; |
|
163 | 165 | } else { |
|
164 | 166 | s &= ~STA_NODISK; |
|
165 | 167 | if (_this->writeprotected()) /* Check card write protection */ |
|
166 | 168 | s |= STA_PROTECT; |
|
167 | 169 | else |
|
168 | 170 | s &= ~STA_PROTECT; |
|
169 | 171 | if (!(s & STA_NOINIT)) { |
|
170 | 172 | if (sdcardsend_cmd(_this,CMD58, 0)) /* Check if the card is kept initialized */ |
|
171 | 173 | s |= STA_NOINIT; |
|
172 | 174 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
173 | 175 | _this->select(0); |
|
174 | 176 | } |
|
175 | 177 | } |
|
176 | 178 | ((sdcardDev*)_this->phy)->Stat = s; |
|
177 | 179 | |
|
178 | 180 | return s; |
|
179 | 181 | } |
|
180 | 182 | |
|
181 | 183 | |
|
182 | 184 | |
|
183 | 185 | |
|
184 | 186 | DSTATUS sdcarddisk_initialize (blkdeviceptr _this) |
|
185 | 187 | { |
|
186 | 188 | char cmd, ty, ocr[4],ocr2[4]; |
|
187 | 189 | int n; |
|
188 | 190 | const char dummy=0xff; |
|
189 | 191 | uint32_t speed = 1000000; |
|
190 | 192 | |
|
191 | 193 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,350000); |
|
192 | 194 | if (_this==0) return STA_NOINIT; /* Supports only drive 0 */ |
|
193 | if (((sdcardDev*)_this->phy)->Stat & STA_NODISK) return ((sdcardDev*)_this->phy)->Stat; /* Is card existing in the soket? */ | |
|
194 | ||
|
195 | /* Is card existing in the soket? */ | |
|
196 | if (((sdcardDev*)_this->phy)->Stat & STA_NODISK) | |
|
197 | { | |
|
198 | printf("No SDCARD\n"); | |
|
199 | return ((sdcardDev*)_this->phy)->Stat; | |
|
200 | } | |
|
201 | printf("SDCARD Detected\n"); | |
|
195 | 202 | for (n = 10; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Send 80 dummy clocks */ |
|
196 | 203 | |
|
197 | 204 | ty = 0; |
|
198 | 205 | if (sdcardsend_cmd(_this,CMD0, 0) == 1) { /* Put the card SPI/Idle state */ |
|
206 | printf("SDCARD in Idle mode\n"); | |
|
199 | 207 | Timer1 = 1000; /* Initialization timeout = 1 sec */ |
|
200 | 208 | if (sdcardsend_cmd(_this,CMD8, 0x1AA) == 1) { /* SDv2? */ |
|
201 | 209 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
202 | 210 | //for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get 32 bit return value of R7 resp */ |
|
203 | 211 | if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* Is the card supports vcc of 2.7-3.6V? */ |
|
204 | 212 | while (Timer1 && sdcardsend_cmd(_this,ACMD41, 1UL << 30)) ; /* Wait for end of initialization with ACMD41(HCS) */ |
|
205 | 213 | if (Timer1 && sdcardsend_cmd(_this,CMD58, 0) == 0) { /* Check CCS bit in the OCR */ |
|
206 | 214 | //for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); |
|
207 | 215 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
208 | 216 | ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Card id SDv2 */ |
|
209 | 217 | } |
|
210 | 218 | } |
|
211 | 219 | } else { /* Not SDv2 card */ |
|
212 | 220 | if (sdcardsend_cmd(_this,ACMD41, 0) <= 1) { /* SDv1 or MMC? */ |
|
213 | 221 | ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */ |
|
214 | 222 | } else { |
|
215 | 223 | ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */ |
|
216 | 224 | } |
|
217 | 225 | while (Timer1 && sdcardsend_cmd(_this,cmd, 0)) ; /* Wait for end of initialization */ |
|
218 | 226 | if (!Timer1 || sdcardsend_cmd(_this,CMD16, 512) != 0) /* Set block length: 512 */ |
|
219 | 227 | ty = 0; |
|
220 | 228 | } |
|
221 | 229 | } |
|
222 | 230 | ((sdcardDev*)_this->phy)->CardType = ty; /* Card type */ |
|
223 | 231 | |
|
224 | 232 | if (ty) { /* OK */ |
|
225 | 233 | ((sdcardDev*)_this->phy)->Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */ |
|
226 | 234 | sdcardsend_cmd(_this,CMD58, 0); |
|
227 | 235 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
228 | 236 | do |
|
229 | 237 | { |
|
230 | 238 | speed+=1000000; |
|
231 | 239 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed); |
|
232 | 240 | sdcardsend_cmd(_this,CMD58, 0); |
|
233 | 241 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr2, 4); |
|
234 | 242 | for(n=0;n<4;n++) |
|
235 | 243 | { |
|
236 | 244 | if(ocr[n]!=ocr2[n]) |
|
237 | 245 | { |
|
238 | 246 | n=0; |
|
239 | 247 | break; |
|
240 | 248 | } |
|
241 | 249 | } |
|
242 | 250 | if(speed>(50*1000*1000)) |
|
243 | 251 | { |
|
244 | 252 | break; |
|
245 | 253 | } |
|
246 | 254 | }while(n); |
|
247 | 255 | if(!n) |
|
248 | 256 | { |
|
249 | 257 | speed-=1000000; |
|
250 | 258 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed); |
|
251 | 259 | } |
|
252 | 260 | |
|
253 | 261 | } else { /* Failed */ |
|
254 | 262 | //power_off(); |
|
255 | 263 | ((sdcardDev*)_this->phy)->Stat = STA_NOINIT; |
|
256 | 264 | } |
|
257 | 265 | sdcarddeselect (_this); |
|
258 | 266 | return ((sdcardDev*)_this->phy)->Stat; |
|
259 | 267 | } |
|
260 | 268 | |
|
261 | 269 | |
|
262 | 270 | |
|
263 | 271 | |
|
264 | 272 | |
|
265 | 273 | |
|
266 | 274 | |
|
267 | 275 | |
|
268 | 276 | |
|
269 | 277 | |
|
270 | 278 | DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count) |
|
271 | 279 | { |
|
272 | 280 | DSTATUS s; |
|
273 | 281 | |
|
274 | 282 | |
|
275 | 283 | s = sdcarddisk_status(_this); |
|
276 | 284 | if (s & STA_NOINIT) return RES_NOTRDY; |
|
277 | 285 | if (!count) return RES_PARERR; |
|
278 | 286 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */ |
|
279 | 287 | |
|
280 | 288 | if (count == 1) { /* Single block read */ |
|
281 | 289 | if ((sdcardsend_cmd(_this,CMD17, sector) == 0) && sdcardrcvr_datablock((sdcardDev*)(_this->phy),buff, 512)) |
|
282 | 290 | count = 0; |
|
283 | 291 | } |
|
284 | 292 | else { /* Multiple block read */ |
|
285 | 293 | if (sdcardsend_cmd(_this,CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */ |
|
286 | 294 | do { |
|
287 | 295 | if (!sdcardrcvr_datablock (((sdcardDev*)_this->phy),buff, 512)) break; |
|
288 | 296 | buff += 512; |
|
289 | 297 | } while (--count); |
|
290 | 298 | sdcardsend_cmd(_this,CMD12, 0); /* STOP_TRANSMISSION */ |
|
291 | 299 | } |
|
292 | 300 | } |
|
293 | 301 | sdcarddeselect (_this); |
|
294 | 302 | |
|
295 | 303 | return count ? RES_ERROR : RES_OK; |
|
296 | 304 | } |
|
297 | 305 | |
|
298 | 306 | |
|
299 | 307 | |
|
300 | 308 | |
|
301 | 309 | |
|
302 | 310 | DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count) |
|
303 | 311 | { |
|
304 | 312 | DSTATUS s; |
|
305 | 313 | |
|
306 | 314 | |
|
307 | 315 | s = sdcarddisk_status(_this); |
|
308 | 316 | if (s & STA_NOINIT) return RES_NOTRDY; |
|
309 | 317 | if (s & STA_PROTECT) return RES_WRPRT; |
|
310 | 318 | if (!count) return RES_PARERR; |
|
311 | 319 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */ |
|
312 | 320 | |
|
313 | 321 | if (count == 1) { /* Single block write */ |
|
314 | 322 | if ((sdcardsend_cmd(_this,CMD24, sector) == 0) /* WRITE_BLOCK */ |
|
315 | 323 | && sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFE)) |
|
316 | 324 | count = 0; |
|
317 | 325 | } |
|
318 | 326 | else { /* Multiple block write */ |
|
319 | 327 | if (((sdcardDev*)_this->phy)->CardType & CT_SDC) sdcardsend_cmd(_this,ACMD23, count); |
|
320 | 328 | if (sdcardsend_cmd(_this,CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */ |
|
321 | 329 | do { |
|
322 | 330 | if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFC)) break; |
|
323 | 331 | buff += 512; |
|
324 | 332 | } while (--count); |
|
325 | 333 | if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),0, 0xFD)) /* STOP_TRAN token */ |
|
326 | 334 | count = 1; |
|
327 | 335 | } |
|
328 | 336 | } |
|
329 | 337 | sdcarddeselect (_this); |
|
330 | 338 | |
|
331 | 339 | return count ? RES_ERROR : RES_OK; |
|
332 | 340 | } |
|
333 | 341 | |
|
334 | 342 | |
|
335 | 343 | DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff) |
|
336 | 344 | { |
|
337 | 345 | DRESULT res; |
|
338 | 346 | const char dummy=0xff; |
|
339 | 347 | char n, csd[16], *ptr = buff; |
|
340 | 348 | uint16_t csize; |
|
341 | 349 | uint32_t *dp, st, ed; |
|
342 | 350 | |
|
343 | 351 | |
|
344 | 352 | if (_this==0) return RES_PARERR; /* Check parameter */ |
|
345 | 353 | if (((sdcardDev*)_this->phy)->Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */ |
|
346 | 354 | |
|
347 | 355 | res = RES_ERROR; |
|
348 | 356 | |
|
349 | 357 | switch (ctrl) { |
|
350 | 358 | case CTRL_SYNC : /* Wait for end of internal write process of the drive */ |
|
351 | 359 | if (sdcardselect (_this)) { |
|
352 | 360 | sdcarddeselect (_this); |
|
353 | 361 | res = RES_OK; |
|
354 | 362 | } |
|
355 | 363 | break; |
|
356 | 364 | |
|
357 | 365 | case GET_SECTOR_COUNT : /* Get drive capacity in unit of sector (DWORD) */ |
|
358 | 366 | if ((sdcardsend_cmd(_this,CMD9, 0) == 0) && sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { |
|
359 | 367 | if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */ |
|
360 | 368 | csize = csd[9] + ((uint16_t)csd[8] << 8) + 1; |
|
361 | 369 | *(uint32_t*)buff = (uint32_t)csize << 10; |
|
362 | 370 | } else { /* SDC ver 1.XX or MMC ver 3 */ |
|
363 | 371 | n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2; |
|
364 | 372 | csize = (csd[8] >> 6) + ((uint16_t)csd[7] << 2) + ((uint16_t)(csd[6] & 3) << 10) + 1; |
|
365 | 373 | *(uint32_t*)buff = (uint32_t)csize << (n - 9); |
|
366 | 374 | } |
|
367 | 375 | res = RES_OK; |
|
368 | 376 | } |
|
369 | 377 | break; |
|
370 | 378 | |
|
371 | 379 | case GET_SECTOR_SIZE : /* Get sector size in unit of byte (WORD) */ |
|
372 | 380 | *(uint16_t*)buff = 512; |
|
373 | 381 | res = RES_OK; |
|
374 | 382 | break; |
|
375 | 383 | |
|
376 | 384 | case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */ |
|
377 | 385 | if (((sdcardDev*)_this->phy)->CardType & CT_SD2) { /* SDC ver 2.00 */ |
|
378 | 386 | if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* Read SD status */ |
|
379 | 387 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
380 | 388 | if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read partial block */ |
|
381 | 389 | for (n = 64 - 16; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Purge trailing data */ |
|
382 | 390 | *(uint32_t*)buff = 16UL << (csd[10] >> 4); |
|
383 | 391 | res = RES_OK; |
|
384 | 392 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
385 | 393 | } |
|
386 | 394 | } |
|
387 | 395 | } else { /* SDC ver 1.XX or MMC */ |
|
388 | 396 | if ((sdcardsend_cmd(_this,CMD9, 0) == 0) && sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read CSD */ |
|
389 | 397 | if (((sdcardDev*)_this->phy)->CardType & CT_SD1) { /* SDC ver 1.XX */ |
|
390 | 398 | *(uint32_t*)buff = (((csd[10] & 63) << 1) + ((uint16_t)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1); |
|
391 | 399 | } else { /* MMC */ |
|
392 | 400 | *(uint32_t*)buff = ((uint16_t)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1); |
|
393 | 401 | } |
|
394 | 402 | res = RES_OK; |
|
395 | 403 | } |
|
396 | 404 | } |
|
397 | 405 | break; |
|
398 | 406 | |
|
399 | 407 | case CTRL_ERASE_SECTOR : /* Erase a block of sectors (used when _USE_ERASE == 1) */ |
|
400 | 408 | if (!(((sdcardDev*)_this->phy)->CardType & CT_SDC)) break; /* Check if the card is SDC */ |
|
401 | 409 | if (sdcarddisk_ioctl(_this, MMC_GET_CSD, csd)) break; /* Get CSD */ |
|
402 | 410 | if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; /* Check if sector erase can be applied to the card */ |
|
403 | 411 | dp = buff; st = dp[0]; ed = dp[1]; /* Load sector block */ |
|
404 | 412 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) { |
|
405 | 413 | st *= 512; ed *= 512; |
|
406 | 414 | } |
|
407 | 415 | if (sdcardsend_cmd(_this,CMD32, st) == 0 && sdcardsend_cmd(_this,CMD33, ed) == 0 && sdcardsend_cmd(_this,CMD38, 0) == 0 && sdcardwait_ready(((sdcardDev*)_this->phy))) /* Erase sector block */ |
|
408 | 416 | res = RES_OK; /* FatFs does not check result of this command */ |
|
409 | 417 | break; |
|
410 | 418 | |
|
411 | 419 | /* Following command are not used by FatFs module */ |
|
412 | 420 | |
|
413 | 421 | case MMC_GET_TYPE : /* Get MMC/SDC type (BYTE) */ |
|
414 | 422 | *ptr = ((sdcardDev*)_this->phy)->CardType; |
|
415 | 423 | res = RES_OK; |
|
416 | 424 | break; |
|
417 | 425 | |
|
418 | 426 | case MMC_GET_CSD : /* Read CSD (16 bytes) */ |
|
419 | 427 | if (sdcardsend_cmd(_this,CMD9, 0) == 0 /* READ_CSD */ |
|
420 | 428 | && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16)) |
|
421 | 429 | res = RES_OK; |
|
422 | 430 | break; |
|
423 | 431 | |
|
424 | 432 | case MMC_GET_CID : /* Read CID (16 bytes) */ |
|
425 | 433 | if (sdcardsend_cmd(_this,CMD10, 0) == 0 /* READ_CID */ |
|
426 | 434 | && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16)) |
|
427 | 435 | res = RES_OK; |
|
428 | 436 | break; |
|
429 | 437 | |
|
430 | 438 | case MMC_GET_OCR : /* Read OCR (4 bytes) */ |
|
431 | 439 | if (sdcardsend_cmd(_this,CMD58, 0) == 0) { /* READ_OCR */ |
|
432 | 440 | //for (n = 4; n; n--) *ptr++ = |
|
433 | 441 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ptr, 4); |
|
434 | 442 | res = RES_OK; |
|
435 | 443 | } |
|
436 | 444 | break; |
|
437 | 445 | |
|
438 | 446 | case MMC_GET_SDSTAT : /* Read SD status (64 bytes) */ |
|
439 | 447 | if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* SD_STATUS */ |
|
440 | 448 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
441 | 449 | if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 64)) |
|
442 | 450 | res = RES_OK; |
|
443 | 451 | } |
|
444 | 452 | break; |
|
445 | 453 | |
|
446 | 454 | default: |
|
447 | 455 | res = RES_PARERR; |
|
448 | 456 | } |
|
449 | 457 | |
|
450 | 458 | sdcarddeselect (_this); |
|
451 | 459 | |
|
452 | 460 | return res; |
|
453 | 461 | } |
|
454 | 462 | |
|
455 | 463 | |
|
456 | 464 | |
|
457 | void sdcardmake(sdcardDev* sdcard,UHANDLE phy,void (*rcvr_mmc) (UHANDLE,char *,uint32_t),void (*xmit_mmc) (UHANDLE,const char *,uint32_t ),void (*setspeed) (UHANDLE phy,uint32_t speed),uint32_t (*getspeed) (UHANDLE phy)) | |
|
465 | void sdcardspimake(sdcardDev* sdcard,UHANDLE phy,void (*rcvr_mmc) (UHANDLE,char *,uint32_t),void (*xmit_mmc) (UHANDLE,const char *,uint32_t ),void (*setspeed) (UHANDLE phy,uint32_t speed),uint32_t (*getspeed) (UHANDLE phy)) | |
|
458 | 466 | { |
|
459 | 467 | sdcard->phy = phy; |
|
460 | 468 | sdcard->rcvr_mmc = rcvr_mmc; |
|
461 | 469 | sdcard->xmit_mmc = xmit_mmc; |
|
462 | 470 | sdcard->setspeed = setspeed; |
|
463 | 471 | sdcard->getspeed = getspeed; |
|
464 | 472 | } |
|
465 | 473 | |
|
466 | void sdcardmakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected) | |
|
474 | void sdcardspimakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected) | |
|
467 | 475 | { |
|
468 | 476 | dev->phy=sdcard; |
|
469 | 477 | dev->select=select; |
|
470 | 478 | dev->power = power; |
|
471 | 479 | dev->detect = detect; |
|
472 | 480 | dev->writeprotected = writeprotected; |
|
473 | 481 | dev->write = sdcarddisk_write; |
|
474 | 482 | dev->read = sdcarddisk_read; |
|
475 | 483 | dev->ioctl = sdcarddisk_ioctl; |
|
476 | 484 | dev->initialize = sdcarddisk_initialize; |
|
477 | 485 | dev->status = sdcarddisk_status; |
|
478 | 486 | } |
|
479 | 487 | |
|
480 | 488 | |
|
481 | 489 | |
|
482 | 490 | |
|
483 | 491 | |
|
484 | 492 | |
|
485 | 493 | |
|
486 | 494 | |
|
487 | 495 | |
|
488 | 496 | |
|
489 | 497 | |
|
490 | 498 | |
|
491 | 499 | |
|
492 | 500 | |
|
493 | 501 | |
|
494 | 502 |
@@ -1,17 +1,20 | |||
|
1 | 1 | TEMPLATE = lib |
|
2 | 2 | OBJECTS_DIR = obj |
|
3 | 3 | TARGET = sdcard |
|
4 | 4 | |
|
5 | 5 | SOURCES += \ |
|
6 | 6 | sdcard.c |
|
7 | 7 | |
|
8 | 8 | INCLUDEPATH += ../../../includes \ |
|
9 | 9 | ../../CPU/STM32F4xx_StdPeriph_Driver/inc \ |
|
10 | 10 | ../../CPU/CMSIS/Include |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | UCMODEL=stm32f4 |
|
14 | 14 | |
|
15 | 15 | target.path = $$[QT_INSTALL_LIBS]/$$UCMODEL |
|
16 | 16 | INSTALLS += target |
|
17 | 17 | |
|
18 | HEADERS += \ | |
|
19 | ../../../includes/sdcard.h | |
|
20 |
@@ -1,420 +1,420 | |||
|
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 | #include <ili9328.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 |
|
|
38 | 38 |
|
|
39 | 39 |
|
|
40 | 40 |
|
|
41 | 41 | {\ |
|
42 | 42 |
|
|
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 |
|
|
50 | 50 |
|
|
51 | 51 |
|
|
52 | 52 |
|
|
53 | 53 | {\ |
|
54 | 54 |
|
|
55 | 55 |
|
|
56 | 56 | if((2*ContSz)<W) \ |
|
57 | 57 | {\ |
|
58 | 58 |
|
|
59 | 59 |
|
|
60 | 60 |
|
|
61 | 61 | {\ |
|
62 | 62 |
|
|
63 | 63 |
|
|
64 | 64 | }\ |
|
65 | 65 | rem=(ContSz)%buffContsize;\ |
|
66 | 66 | if(rem)LCD->interface->writeGRAM(bufferCont,rem);\ |
|
67 | 67 | for(int i=rem;i<(ContSz);i+=buffContsize)\ |
|
68 | 68 | {\ |
|
69 | 69 |
|
|
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 |
|
|
78 | 78 |
|
|
79 | 79 |
|
|
80 | 80 |
|
|
81 | 81 | {\ |
|
82 | 82 |
|
|
83 | 83 |
|
|
84 | 84 | if((2*ContSz)<H) \ |
|
85 | 85 | {\ |
|
86 | 86 |
|
|
87 | 87 |
|
|
88 | 88 |
|
|
89 | 89 | {\ |
|
90 | 90 |
|
|
91 | 91 |
|
|
92 | 92 | }\ |
|
93 | 93 | rem=(ContSz)%buffContsize;\ |
|
94 | 94 | if(rem)LCD->interface->writeGRAM(bufferCont,rem);\ |
|
95 | 95 | for(int i=rem;i<(ContSz);i+=buffContsize)\ |
|
96 | 96 | {\ |
|
97 | 97 |
|
|
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 |
|
|
106 | 106 |
|
|
107 | 107 |
|
|
108 | 108 |
|
|
109 | 109 | {\ |
|
110 | 110 |
|
|
111 | 111 |
|
|
112 | 112 | if(ContSz<H) \ |
|
113 | 113 | {\ |
|
114 | 114 |
|
|
115 | 115 |
|
|
116 | 116 |
|
|
117 | 117 | {\ |
|
118 | 118 |
|
|
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 |
|
|
128 | 128 | int rem;\ |
|
129 | 129 | if(ContSz<H) \ |
|
130 | 130 | {\ |
|
131 | 131 |
|
|
132 | 132 |
|
|
133 | 133 |
|
|
134 | 134 | {\ |
|
135 | 135 |
|
|
136 | 136 |
|
|
137 | 137 | }\ |
|
138 | 138 | rem=(ContSz)%buffContsize;\ |
|
139 | 139 | if(rem)LCD->interface->writeGRAM(bufferCont,rem);\ |
|
140 | 140 | for(int i=rem;i<(ContSz);i+=buffContsize)\ |
|
141 | 141 | {\ |
|
142 | 142 |
|
|
143 | 143 | }\ |
|
144 | 144 | }\ |
|
145 | 145 | |
|
146 | 146 | |
|
147 | 147 | void ili9328setGRAMaddress(LCD_t* LCD,uint16_t Haddress,uint16_t Vaddress) |
|
148 | 148 | { |
|
149 | 149 | LCD->interface->writereg(ILI9328_REGISTER_HORIZONTALGRAMADDRESSSET,Haddress); |
|
150 | 150 | LCD->interface->writereg(ILI9328_REGISTER_VERTICALGRAMADDRESSSET,Vaddress); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | void ili9328refreshenable(struct LCD_t* LCD,int enable) |
|
154 | 154 | { |
|
155 | 155 | if(enable) |
|
156 | 156 | { |
|
157 | 157 | //LCD->interface->writereg(ILI9328_REGISTER_ENTRYMODE,0x1018); |
|
158 | 158 | } |
|
159 | 159 | else |
|
160 | 160 | { |
|
161 | 161 | //LCD->interface->writereg(ILI9328_REGISTER_ENTRYMODE,0x1008); |
|
162 | 162 | |
|
163 | 163 | } |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | void ili9328setFrame(LCD_t* LCD,uint16_t X,uint16_t Y,uint16_t W,uint16_t H) |
|
167 | 167 | { |
|
168 | 168 | LCD->interface->writereg(ILI9328_REGISTER_HORIZONTALGRAMADDRESSSET,(uint32_t)X); |
|
169 | 169 | LCD->interface->writereg(ILI9328_REGISTER_VERTICALGRAMADDRESSSET,(uint32_t)Y); |
|
170 | 170 | LCD->interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSSTARTPOSITION,(uint32_t)X); |
|
171 | 171 | LCD->interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSENDPOSITION,(uint32_t)(W+X-1)); |
|
172 | 172 | LCD->interface->writereg(ILI9328_REGISTER_VERTICALADDRESSSTARTPOSITION,(uint32_t)Y); |
|
173 | 173 | LCD->interface->writereg(ILI9328_REGISTER_VERTICALADDRESSENDPOSITION,(uint32_t)(Y+H-1)); |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | void ili9328paint(LCD_t* LCD,void* buffer,uint16_t Xpos,uint16_t Ypos,uint16_t Width,uint16_t Height) |
|
177 | 177 | { |
|
178 | 178 | if((LCD!=NULL) && (LCD->interface!=NULL) && (LCD->interface->writeGRAM!=NULL) && (LCD->width>(Xpos+Width)) && (LCD->height>(Ypos+Height))) |
|
179 | 179 | { |
|
180 | 180 | ili9328setFrame(LCD,Xpos,Ypos,Width,Height); |
|
181 | 181 | LCD->interface->writeGRAM(buffer,Width*Height); |
|
182 | 182 | } |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | void ili9328paintFilCirc(LCD_t* LCD,uint16_t Xpos,uint16_t Ypos,uint16_t r,uint32_t contColor,uint16_t contSz,uint32_t fillColor) |
|
186 | 186 | { |
|
187 | 187 | //Based on the mid point circle algorithm from Wikipedia |
|
188 | 188 | //http://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
|
189 | 189 | uint16_t innerbuffer[16]; |
|
190 | 190 | uint16_t outterbuffer[16]; |
|
191 | 191 | for(int i=0;i<16;i++)innerbuffer[i]=fillColor; |
|
192 | 192 | for(int i=0;i<16;i++)outterbuffer[i]=contColor; |
|
193 | 193 | if(contSz<r) |
|
194 | 194 | { |
|
195 | 195 | int error = -r,error_int = -r+contSz; |
|
196 | 196 | int x = r,x_int=r-contSz; |
|
197 | 197 | int y = 0,y_int=0; |
|
198 | 198 | while (x >= y) |
|
199 | 199 | { |
|
200 | 200 | ilipaintHLineWithCont(LCD,(Xpos-x),(Ypos+y),(2*x),(x-x_int),innerbuffer,16,outterbuffer,16); |
|
201 | 201 | ilipaintHLineWithCont(LCD,(Xpos-x),(Ypos-y),(2*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 | ilipaintHalfTopVLineWithCont(LCD,(Xpos-y),(Ypos-x),(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 | ilipaintHalfBottomVLineWithCont(LCD,(Xpos+y),(Ypos),(x),(x-x_int),innerbuffer,16,outterbuffer,16); |
|
206 | 206 | error += y; |
|
207 | 207 | ++y; |
|
208 | 208 | error += y; |
|
209 | 209 | error_int += y_int; |
|
210 | 210 | ++y_int; |
|
211 | 211 | error_int += y_int; |
|
212 | 212 | if(error >= 0) |
|
213 | 213 | { |
|
214 | 214 | error -= x; |
|
215 | 215 | --x; |
|
216 | 216 | error -= x; |
|
217 | 217 | } |
|
218 | 218 | if(error_int >= 0) |
|
219 | 219 | { |
|
220 | 220 | error_int -= x_int; |
|
221 | 221 | --x_int; |
|
222 | 222 | error_int -= x_int; |
|
223 | 223 | } |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | |
|
227 | 227 | |
|
228 | 228 | } |
|
229 | 229 | |
|
230 | 230 | } |
|
231 | 231 | |
|
232 | 232 | void ili9328paintFilCirc_old(LCD_t* LCD,uint16_t Xpos,uint16_t Ypos,uint16_t r,uint32_t contColor,uint16_t contSz,uint32_t fillColor) |
|
233 | 233 | { |
|
234 | 234 | if(contSz<r) |
|
235 | 235 | { |
|
236 | 236 | uint16_t innerbuffer[16]; |
|
237 | 237 | uint16_t outterbuffer[16]; |
|
238 | 238 | int32_t rr=(r*r),rr2=((r-contSz)*(r-contSz)),contSz2,Val1,Val2,X1,W,rem; |
|
239 | 239 | for(int i=0;i<16;i++)innerbuffer[i]=fillColor; |
|
240 | 240 | for(int i=0;i<16;i++)outterbuffer[i]=contColor; |
|
241 | 241 | /* Y = b +/- sqrt[r^2 - (x - a)^2] */ |
|
242 | 242 | for(int32_t line=-r;line<r;line++) |
|
243 | 243 | { |
|
244 | 244 | #ifdef __OPTIMIZED_MATH |
|
245 | 245 | Val1 = (uint32_t)optimised_sqrt((float32_t)(rr - (line*line)) ); |
|
246 | 246 | Val2 = (uint32_t)optimised_sqrt((float32_t)(rr2 - (line*line)) ); |
|
247 | 247 | #else |
|
248 | 248 | Val1 = sqrt( (double)(rr - ((line)*(line))) ); |
|
249 | 249 | Val2 = sqrt( (double)(rr2 - ((line)*(line))) ); |
|
250 | 250 | #endif |
|
251 | 251 | X1=Xpos - Val1; |
|
252 | 252 | contSz2= Val1-Val2; |
|
253 | 253 | ili9328setFrame(LCD,X1,line+Ypos,2*Val1,1); |
|
254 | 254 | rem=(contSz2)%16; |
|
255 | 255 | if(rem)LCD->interface->writeGRAM(outterbuffer,rem); |
|
256 | 256 | for(int i=rem;i<(contSz2);i+=16) |
|
257 | 257 | { |
|
258 | 258 | LCD->interface->writeGRAM(outterbuffer,16); |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | W=2*Val1; |
|
262 | 262 | if(W>(2*contSz2)) |
|
263 | 263 | { |
|
264 | 264 | W-=2*contSz2; |
|
265 | 265 | rem=(W)%16; |
|
266 | 266 | if(rem)LCD->interface->writeGRAM(innerbuffer,rem); |
|
267 | 267 | for(int i=rem;i<(W);i+=16) |
|
268 | 268 | { |
|
269 | 269 | LCD->interface->writeGRAM(innerbuffer,16); |
|
270 | 270 | } |
|
271 | 271 | } |
|
272 | 272 | |
|
273 | 273 | rem=(contSz2)%16; |
|
274 | 274 | if(rem)LCD->interface->writeGRAM(outterbuffer,rem); |
|
275 | 275 | for(int i=rem;i<(contSz2);i+=16) |
|
276 | 276 | { |
|
277 | 277 | LCD->interface->writeGRAM(outterbuffer,16); |
|
278 | 278 | } |
|
279 | 279 | } |
|
280 | 280 | } |
|
281 | 281 | } |
|
282 | 282 | |
|
283 | 283 | |
|
284 | 284 | |
|
285 | 285 | |
|
286 | 286 | 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) |
|
287 | 287 | { |
|
288 | 288 | ili9328setFrame(LCD,Xpos,Ypos,w,h); |
|
289 | 289 | uint16_t tmp[32]; |
|
290 | 290 | for(int i=0;i<32;i++)tmp[i]=fillColor; |
|
291 | 291 | for(int i=0;i<(h*w);i+=32) |
|
292 | 292 | { |
|
293 | 293 | LCD->interface->writeGRAM(tmp,32); |
|
294 | 294 | } |
|
295 | 295 | int rem=(w*h)%32; |
|
296 | 296 | if(rem)LCD->interface->writeGRAM(tmp,rem); |
|
297 | 297 | if(contSz) |
|
298 | 298 | { |
|
299 | 299 | ili9328setFrame(LCD,Xpos,Ypos,w,contSz); |
|
300 | 300 | for(int i=0;i<32;i++)tmp[i]=contColor; |
|
301 | 301 | rem=(w*contSz)%32; |
|
302 | 302 | if(rem)LCD->interface->writeGRAM(tmp,rem); |
|
303 | 303 | for(int i=rem;i<(w*contSz);i+=32) |
|
304 | 304 | { |
|
305 | 305 | LCD->interface->writeGRAM(tmp,32); |
|
306 | 306 | } |
|
307 | 307 | |
|
308 | 308 | ili9328setFrame(LCD,Xpos,Ypos+h-contSz,w,contSz); |
|
309 | 309 | rem=(w*contSz)%32; |
|
310 | 310 | if(rem)LCD->interface->writeGRAM(tmp,rem); |
|
311 | 311 | for(int i=rem;i<(w*contSz);i+=32) |
|
312 | 312 | { |
|
313 | 313 | LCD->interface->writeGRAM(tmp,32); |
|
314 | 314 | } |
|
315 | 315 | |
|
316 | 316 | ili9328setFrame(LCD,Xpos,Ypos,contSz,h); |
|
317 | 317 | rem=(h*contSz)%32; |
|
318 | 318 | if(rem)LCD->interface->writeGRAM(tmp,rem); |
|
319 | 319 | for(int i=rem;i<(h*contSz);i+=32) |
|
320 | 320 | { |
|
321 | 321 | LCD->interface->writeGRAM(tmp,32); |
|
322 | 322 | } |
|
323 | 323 | |
|
324 | 324 | ili9328setFrame(LCD,Xpos+w-contSz,Ypos,contSz,h); |
|
325 | 325 | rem=(h*contSz)%32; |
|
326 | 326 | if(rem)LCD->interface->writeGRAM(tmp,rem); |
|
327 | 327 | for(int i=rem;i<(h*contSz);i+=32) |
|
328 | 328 | { |
|
329 | 329 | LCD->interface->writeGRAM(tmp,32); |
|
330 | 330 | } |
|
331 | 331 | } |
|
332 | 332 | } |
|
333 | 333 | |
|
334 | 334 | void ili9328paintText(LCD_t* LCD,char* buffer,uint16_t Xpos,uint16_t Ypos,sFONT* font,uint32_t color) |
|
335 | 335 | { |
|
336 | 336 | int w=font->Width,h=font->Height,bpl=font->bytesPerLine,pix=0,tableoffset=0; |
|
337 | 337 | uint16_t tmp[w]; |
|
338 | 338 | uint16_t linenum=0,charnum=0; |
|
339 | 339 | uint8_t line=0; |
|
340 | 340 | while(*buffer!='\0') |
|
341 | 341 | { |
|
342 | 342 | ili9328setFrame(LCD,Xpos+(charnum*w),Ypos-h,w,1); |
|
343 | 343 | LCD->interface->readGRAM(tmp,w); |
|
344 | 344 | for(int i=0;i<(h*w);i++) |
|
345 | 345 | { |
|
346 | 346 | if( ((i%w)==0) ) //read current line to apply text pixmap |
|
347 | 347 | { |
|
348 | 348 | if(linenum++>0) |
|
349 | 349 | { |
|
350 | 350 | ili9328setFrame(LCD,Xpos+(charnum*w),Ypos + linenum -h,w,1); |
|
351 | 351 | LCD->interface->writeGRAM(tmp,w); |
|
352 | 352 | ili9328setFrame(LCD,Xpos+(charnum*w),Ypos + linenum + 1-h,w,1); |
|
353 | 353 | LCD->interface->readGRAM(tmp,w); |
|
354 | 354 | pix=0; |
|
355 | 355 | } |
|
356 | 356 | } |
|
357 | 357 | if((pix%8) == 0) |
|
358 | 358 | { |
|
359 | 359 | line=font->table[(((*buffer)-32)*h*bpl)+tableoffset++]; |
|
360 | 360 | } |
|
361 | 361 | if((line & (uint8_t)0x01)==(uint8_t)1)tmp[pix]=(uint16_t)color; |
|
362 | 362 | pix++; |
|
363 | 363 | line>>=1; |
|
364 | 364 | } |
|
365 | 365 | linenum=0; |
|
366 | 366 | tableoffset=0; |
|
367 | 367 | charnum++; |
|
368 | 368 | buffer++; |
|
369 | 369 | } |
|
370 | 370 | } |
|
371 | 371 | |
|
372 | 372 | int ili9328init(struct LCD_t* LCD) |
|
373 | 373 | { |
|
374 | 374 | if((LCD!=NULL) && (LCD->interface!=NULL) && (LCD->interface->writereg!=NULL)) |
|
375 | 375 | { |
|
376 | 376 | LCD->interface->writereg(ILI9328_REGISTER_DRIVEROUTPUTCONTROL1, 0x0100); // Driver Output Control Register (R01h) |
|
377 | 377 | LCD->interface->writereg(ILI9328_REGISTER_LCDDRIVINGCONTROL, 0x0700); // LCD Driving Waveform Control (R02h) |
|
378 | 378 | LCD->interface->writereg(ILI9328_REGISTER_ENTRYMODE, 0x1030); // Entry Mode (R03h) |
|
379 |
LCD->interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL2, 0x0 |
|
|
379 | LCD->interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL2, 0x0302); | |
|
380 | 380 | LCD->interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL3, 0x0000); |
|
381 | 381 | LCD->interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL4, 0x0000); // Fmark On |
|
382 | 382 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL1, 0x0000); // Power Control 1 (R10h) |
|
383 | 383 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL2, 0x0007); // Power Control 2 (R11h) |
|
384 | 384 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL3, 0x0000); // Power Control 3 (R12h) |
|
385 | 385 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL4, 0x0000); // Power Control 4 (R13h) |
|
386 | delay_100us(10); | |
|
386 | delay_100us(1000); | |
|
387 | 387 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL1, 0x14B0); // Power Control 1 (R10h) |
|
388 |
delay_100us( |
|
|
388 | delay_100us(500); | |
|
389 | 389 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL2, 0x0007); // Power Control 2 (R11h) |
|
390 |
delay_100us( |
|
|
390 | delay_100us(500); | |
|
391 | 391 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL3, 0x008E); // Power Control 3 (R12h) |
|
392 | 392 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL4, 0x0C00); // Power Control 4 (R13h) |
|
393 | 393 | LCD->interface->writereg(ILI9328_REGISTER_POWERCONTROL7, 0x0015); // NVM read data 2 (R29h) |
|
394 | 394 | delay_100us(500); |
|
395 | 395 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL1, 0x0000); // Gamma Control 1 |
|
396 | 396 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL2, 0x0107); // Gamma Control 2 |
|
397 | 397 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL3, 0x0000); // Gamma Control 3 |
|
398 | 398 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL4, 0x0203); // Gamma Control 4 |
|
399 | 399 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL5, 0x0402); // Gamma Control 5 |
|
400 | 400 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL6, 0x0000); // Gamma Control 6 |
|
401 | 401 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL7, 0x0207); // Gamma Control 7 |
|
402 | 402 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL8, 0x0000); // Gamma Control 8 |
|
403 | 403 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL9, 0x0203); // Gamma Control 9 |
|
404 | 404 | LCD->interface->writereg(ILI9328_REGISTER_GAMMACONTROL10, 0x0403); // Gamma Control 10 |
|
405 | 405 | LCD->interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSSTARTPOSITION, 0x0000); // Window Horizontal RAM Address Start (R50h) |
|
406 |
LCD->interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSENDPOSITION, |
|
|
406 | LCD->interface->writereg(ILI9328_REGISTER_HORIZONTALADDRESSENDPOSITION, LCD->width - 1); // Window Horizontal RAM Address End (R51h) | |
|
407 | 407 | LCD->interface->writereg(ILI9328_REGISTER_VERTICALADDRESSSTARTPOSITION, 0X0000); // Window Vertical RAM Address Start (R52h) |
|
408 |
LCD->interface->writereg(ILI9328_REGISTER_VERTICALADDRESSENDPOSITION, |
|
|
408 | LCD->interface->writereg(ILI9328_REGISTER_VERTICALADDRESSENDPOSITION, LCD->height - 1); // Window Vertical RAM Address End (R53h) | |
|
409 | 409 | LCD->interface->writereg(ILI9328_REGISTER_DRIVEROUTPUTCONTROL2, 0xa700); // Driver Output Control (R60h) |
|
410 | 410 | LCD->interface->writereg(ILI9328_REGISTER_BASEIMAGEDISPLAYCONTROL, 0x0003); // Driver Output Control (R61h) - enable VLE |
|
411 | 411 | LCD->interface->writereg(ILI9328_REGISTER_PANELINTERFACECONTROL1, 0X0010); // Panel Interface Control 1 (R90h) |
|
412 | 412 | // Display On |
|
413 | 413 | LCD->interface->writereg(ILI9328_REGISTER_DISPLAYCONTROL1, 0x0133); // Display Control (R07h) |
|
414 | 414 | delay_100us(500); |
|
415 | LCD->interface->writereg(ILI9328_REGISTER_ENTRYMODE, 0x0038); | |
|
415 | LCD->paintFilRect(LCD,0,0,LCD->width,LCD->height,0,0,0xFFFF); | |
|
416 | 416 | } |
|
417 | 417 | return 0; |
|
418 | 418 | } |
|
419 | 419 | |
|
420 | 420 |
@@ -1,78 +1,82 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | #-- This file is a part of the libuc, microcontroler library |
|
3 | 3 | #-- Copyright (C) 2011, Alexis Jeandet |
|
4 | 4 | #-- |
|
5 | 5 | #-- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | #-- it under the terms of the GNU General Public License as published by |
|
7 | 7 | #-- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | #-- (at your option) any later version. |
|
9 | 9 | #-- |
|
10 | 10 | #-- This program is distributed in the hope that it will be useful, |
|
11 | 11 | #-- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | #-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | #-- GNU General Public License for more details. |
|
14 | 14 | #-- |
|
15 | 15 | #-- You should have received a copy of the GNU General Public License |
|
16 | 16 | #-- along with this program; if not, write to the Free Software |
|
17 | 17 | #-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | #------------------------------------------------------------------------------- |
|
19 | 19 | #-- Author : Alexis Jeandet |
|
20 | 20 | #-- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | #-------------------------------------------------------------------------------*/ |
|
22 | 22 | #ifndef VS10XX_H |
|
23 | 23 | #define VS10XX_H |
|
24 | 24 | #include <uhandle.h> |
|
25 | 25 | #include <spi.h> |
|
26 | 26 | |
|
27 | 27 | #define VSMODE 0 |
|
28 | 28 | #define VSSTATUS 1 |
|
29 | 29 | #define VSBASS 2 |
|
30 | 30 | #define VSCLOCKF 3 |
|
31 | 31 | #define VSDECODE_TIME 4 |
|
32 | 32 | #define VSAUDATA 5 |
|
33 | 33 | #define VSWRAM 6 |
|
34 | 34 | #define VSWRAMADDR 7 |
|
35 | 35 | #define VSHDAT0 8 |
|
36 | 36 | #define VSHDAT1 9 |
|
37 | 37 | #define VSAIADDR 0xA |
|
38 | 38 | #define VSVOL 0xB |
|
39 | 39 | #define VSAICTRL0 0xC |
|
40 | 40 | #define VSAICTRL1 0xD |
|
41 | 41 | #define VSAICTRL2 0xE |
|
42 | 42 | #define VSAICTRL3 0xF |
|
43 | 43 | |
|
44 | 44 | typedef struct vs10XXDev |
|
45 | 45 | { |
|
46 | 46 | spi_t SPIdev; |
|
47 | void (*setxCS)(char); | |
|
48 | void (*setxRST)(char); | |
|
49 | void (*setxDCS)(char); | |
|
50 | int (*getDREQ)(); | |
|
47 | 51 | int a; |
|
48 | 52 | }vs10XXDev; |
|
49 | 53 | |
|
50 | 54 | |
|
51 | void vs10XXopen(vs10XXDev* codec,spi_t dev); | |
|
55 | void vs10XXopen(vs10XXDev* codec,spi_t dev, void (*setxCS)(char),void (*setxRST)(char),void (*setxDCS)(char),int (*getDREQ)()); | |
|
52 | 56 | void vs10XXsoftreset(vs10XXDev* dev); |
|
53 | 57 | int vs10XXcmdread(vs10XXDev* dev,char address); |
|
54 | 58 | void vs10XXcmdwrite(vs10XXDev* dev,char address,int value); |
|
55 | 59 | |
|
56 | 60 | extern void vs10XXclearXCS(); |
|
57 | 61 | extern void vs10XXsetXCS(); |
|
58 | 62 | extern int vs10XXDREQ(); |
|
59 | 63 | |
|
60 | 64 | #endif //VS10XX_H |
|
61 | 65 | |
|
62 | 66 | |
|
63 | 67 | |
|
64 | 68 | |
|
65 | 69 | |
|
66 | 70 | |
|
67 | 71 | |
|
68 | 72 | |
|
69 | 73 | |
|
70 | 74 | |
|
71 | 75 | |
|
72 | 76 | |
|
73 | 77 | |
|
74 | 78 | |
|
75 | 79 | |
|
76 | 80 | |
|
77 | 81 | |
|
78 | 82 |
@@ -1,106 +1,106 | |||
|
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 SDCARD_H |
|
23 | 23 | #define SDCARD_H |
|
24 | 24 | #include "blkdevice.h" |
|
25 | 25 | #include <libucstrings.h> |
|
26 | 26 | #include <uhandle.h> |
|
27 | 27 | |
|
28 | 28 | /* MMC/SD command */ |
|
29 | 29 | #define CMD0 (0) /* GO_IDLE_STATE */ |
|
30 | 30 | #define CMD1 (1) /* SEND_OP_COND (MMC) */ |
|
31 | 31 | #define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */ |
|
32 | 32 | #define CMD8 (8) /* SEND_IF_COND */ |
|
33 | 33 | #define CMD9 (9) /* SEND_CSD */ |
|
34 | 34 | #define CMD10 (10) /* SEND_CID */ |
|
35 | 35 | #define CMD12 (12) /* STOP_TRANSMISSION */ |
|
36 | 36 | #define ACMD13 (0x80+13) /* SD_STATUS (SDC) */ |
|
37 | 37 | #define CMD16 (16) /* SET_BLOCKLEN */ |
|
38 | 38 | #define CMD17 (17) /* READ_SINGLE_BLOCK */ |
|
39 | 39 | #define CMD18 (18) /* READ_MULTIPLE_BLOCK */ |
|
40 | 40 | #define CMD23 (23) /* SET_BLOCK_COUNT (MMC) */ |
|
41 | 41 | #define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */ |
|
42 | 42 | #define CMD24 (24) /* WRITE_BLOCK */ |
|
43 | 43 | #define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */ |
|
44 | 44 | #define CMD32 (32) /* ERASE_ER_BLK_START */ |
|
45 | 45 | #define CMD33 (33) /* ERASE_ER_BLK_END */ |
|
46 | 46 | #define CMD38 (38) /* ERASE */ |
|
47 | 47 | #define CMD55 (55) /* APP_CMD */ |
|
48 | 48 | #define CMD58 (58) /* READ_OCR */ |
|
49 | 49 | |
|
50 | 50 | /* Card type flags (CardType) */ |
|
51 | 51 | #define CT_MMC 0x01 /* MMC ver 3 */ |
|
52 | 52 | #define CT_SD1 0x02 /* SD ver 1 */ |
|
53 | 53 | #define CT_SD2 0x04 /* SD ver 2 */ |
|
54 | 54 | #define CT_SDC (CT_SD1|CT_SD2) /* SD */ |
|
55 | 55 | #define CT_BLOCK 0x08 /* Block addressing */ |
|
56 | 56 | |
|
57 | 57 | struct sdcard_str |
|
58 | 58 | { |
|
59 | 59 | UHANDLE phy; |
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
|
60 | int (*rcvr_mmc) (UHANDLE phy,char *buff,uint32_t bc); | |
|
61 | int (*xmit_mmc) (UHANDLE phy,const char *buff,uint32_t bc); | |
|
62 | int (*setspeed) (UHANDLE phy,uint32_t speed); | |
|
63 | 63 | uint32_t (*getspeed) (UHANDLE phy); |
|
64 | 64 | DSTATUS Stat; |
|
65 | 65 | char CardType; |
|
66 | 66 | }; |
|
67 | 67 | |
|
68 | 68 | typedef volatile struct sdcard_str sdcardDev; |
|
69 | 69 | |
|
70 | 70 | //extern void sdcardmake(sdcardDev* sdcard,UHANDLE phy,void (*rcvr_mmc) (UHANDLE,char *,uint32_t ),void (*xmit_mmc) (UHANDLE,const char *,uint32_t ),void (*setspeed) (UHANDLE phy,uint32_t speed),uint32_t (*getspeed) (UHANDLE phy)); |
|
71 | 71 | //extern void sdcardmakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected); |
|
72 | 72 | |
|
73 | 73 | |
|
74 | 74 | #endif |
|
75 | 75 | |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | |
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 |
@@ -1,149 +1,150 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2012, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | /* |
|
23 | 23 | TODO: |
|
24 | 24 | Add SPI slave mode |
|
25 | 25 | Add interrupt interface |
|
26 | 26 | */ |
|
27 | 27 | |
|
28 | 28 | #ifndef SPI_H |
|
29 | 29 | #define SPI_H |
|
30 | 30 | #include <stdint.h> |
|
31 | 31 | #include <uhandle.h> |
|
32 | 32 | |
|
33 | 33 | #ifdef __cplusplus |
|
34 | 34 | extern "C" { |
|
35 | 35 | #endif |
|
36 | 36 | |
|
37 | 37 | /* |
|
38 | 38 | typedef struct spi_t |
|
39 | 39 | { |
|
40 | 40 | void* _dev; |
|
41 | 41 | int cfg; |
|
42 | 42 | int speed; |
|
43 | 43 | }spi_t;*/ |
|
44 | 44 | |
|
45 | 45 | typedef int spi_t; |
|
46 | 46 | |
|
47 | 47 | #define spi1 ((spi_t)0) |
|
48 | 48 | #define spi2 ((spi_t)1) |
|
49 | 49 | #define spi3 ((spi_t)2) |
|
50 | 50 | #define spi4 ((spi_t)3) |
|
51 | 51 | #define spi5 ((spi_t)4) |
|
52 | 52 | #define spi6 ((spi_t)5) |
|
53 | 53 | #define spi7 ((spi_t)6) |
|
54 | 54 | #define spi8 ((spi_t)7) |
|
55 | 55 | #define spi9 ((spi_t)8) |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | typedef enum |
|
59 | 59 | { |
|
60 | 60 | spi4bits = 0x3, |
|
61 | 61 | spi5bits = 0x4, |
|
62 | 62 | spi6bits = 0x5, |
|
63 | 63 | spi7bits = 0x6, |
|
64 | 64 | spi8bits = 0x7, |
|
65 | 65 | spi9bits = 0x8, |
|
66 | 66 | spi10bits = 0x9, |
|
67 | 67 | spi11bits = 0xA, |
|
68 | 68 | spi12bits = 0xB, |
|
69 | 69 | spi13bits = 0xC, |
|
70 | 70 | spi14bits = 0xD, |
|
71 | 71 | spi15bits = 0xE, |
|
72 | 72 | spi16bits = 0xF |
|
73 | 73 | }spibits_t; |
|
74 | 74 | |
|
75 | 75 | #define SPIBITSMASK 0xF |
|
76 | 76 | |
|
77 | 77 | typedef enum |
|
78 | 78 | { |
|
79 | 79 | spislave = 0x00, |
|
80 | 80 | spimaster = 0x10 |
|
81 | 81 | }spimode_t; |
|
82 | 82 | |
|
83 | 83 | #define SPIMODEMASK 0x10 |
|
84 | 84 | |
|
85 | 85 | typedef enum |
|
86 | 86 | { |
|
87 | 87 | spiclkinhlow = 0x00, |
|
88 | 88 | spiclkinhhigh = 0x20 |
|
89 | 89 | }spiclkinhlvl_t; |
|
90 | 90 | |
|
91 | 91 | #define SPICLKINHLVLMASK 0x20 |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | typedef enum |
|
95 | 95 | { |
|
96 | 96 | spiclkfirstedge = 0x00, |
|
97 | 97 | spiclksecondedge = 0x40 |
|
98 | 98 | }spiclkphase_t; |
|
99 | 99 | |
|
100 | 100 | #define SPICLKPHASEMASK 0x40 |
|
101 | 101 | |
|
102 | 102 | typedef enum |
|
103 | 103 | { |
|
104 | 104 | spimsbfirst = 0x00, |
|
105 | 105 | spilsbfirst = 0x80 |
|
106 | 106 | }spibitorder_t; |
|
107 | 107 | |
|
108 | 108 | #define SPIBITORDERMASK 0x80 |
|
109 | 109 | |
|
110 | 110 | |
|
111 | 111 | extern spi_t spiopen(int count); |
|
112 | 112 | extern spi_t spiopenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin); |
|
113 | 113 | extern int spiclose(spi_t spidev); |
|
114 | 114 | extern int spisetpins(spi_t spidev,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin); |
|
115 | 115 | extern int spienable(spi_t spidev); |
|
116 | 116 | extern int spidisable(spi_t spidev); |
|
117 | 117 | extern int spitransactionfinished(spi_t spidev); |
|
118 | 118 | extern int spisetconfig(spi_t spidev,uint32_t config,uint32_t speed); |
|
119 | 119 | extern int spisetspeed(spi_t spidev, uint32_t speed); |
|
120 | extern uint32_t spigetspeed(spi_t spidev); | |
|
120 | 121 | extern int spisetbitorder(spi_t spidev,spibitorder_t order); |
|
121 | 122 | extern int spisetdatabits(spi_t spidev,spibits_t bitscnt); |
|
122 | 123 | extern int spisetclkinhlevel(spi_t spidev,spiclkinhlvl_t level); |
|
123 | 124 | extern int spisetclkphase(spi_t spidev,spiclkphase_t phase); |
|
124 | 125 | extern int spiputw(spi_t spidev,uint16_t data); |
|
125 | 126 | extern uint16_t spigetw(spi_t spidev); |
|
126 | 127 | extern int spiputs(spi_t spidev,char* s); |
|
127 | 128 | extern int spigets(spi_t spidev,char* s); |
|
128 | 129 | extern int spiputnw(spi_t spidev,uint16_t* w,int n); |
|
129 | 130 | extern int spigetnw(spi_t spidev,uint16_t* w,int n); |
|
130 | 131 | extern int spiputnc(spi_t spidev,char* c,int n); |
|
131 | 132 | extern int spigetnc(spi_t spidev,char* c,int n); |
|
132 | 133 | extern int spiavailiabledata(spi_t spidev); |
|
133 | 134 | |
|
134 | 135 | #ifdef __cplusplus |
|
135 | 136 | } |
|
136 | 137 | #endif |
|
137 | 138 | #endif //SPI_H |
|
138 | 139 | |
|
139 | 140 | |
|
140 | 141 | |
|
141 | 142 | |
|
142 | 143 | |
|
143 | 144 | |
|
144 | 145 | |
|
145 | 146 | |
|
146 | 147 | |
|
147 | 148 | |
|
148 | 149 | |
|
149 | 150 |
@@ -1,389 +1,392 | |||
|
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 | #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 | 32 | gpio &= -1^GPIOSPEEDMASK; |
|
33 | 33 | gpio |= gpiolowspeed; |
|
34 | 34 | gpio &= -1^GPIODIRMASK; |
|
35 | 35 | gpio |= gpioindir; |
|
36 | 36 | gpio &= -1^GPIOOUTTYPEMASK; |
|
37 | 37 | gpio |= gpiopushpulltype; |
|
38 | 38 | gpio &= -1^GPIOPULLTYPEMASK; |
|
39 | 39 | gpio |= gpionopulltype; |
|
40 | 40 | gpiosetconfig(&gpio); |
|
41 | 41 | RCC_AHB1PeriphClockCmd(((uint32_t)0x00000001<<GPIOPORTNUM(gpio)), ENABLE); |
|
42 | 42 | return gpio; |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | void gpioclose(gpio_t gpio) |
|
47 | 47 | { |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | void gpiosetconfig(gpio_t* gpio) |
|
51 | 51 | { |
|
52 | 52 | gpiosetdir(gpio, (*gpio & GPIODIRMASK)); |
|
53 | 53 | gpiosetspeed(gpio, (*gpio & GPIOSPEEDMASK)); |
|
54 | 54 | gpiosetouttype(gpio, (*gpio & GPIOOUTTYPEMASK)); |
|
55 | 55 | gpiosetpulltype(gpio, (*gpio & GPIOPULLTYPEMASK)); |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | extern void gpiosetspeed(gpio_t* gpio,gpiospeed_t speed) |
|
59 | 59 | { |
|
60 | 60 | GPIO_TypeDef* GPIOx = GPIOGETPORT((*gpio)); |
|
61 | 61 | switch(speed) |
|
62 | 62 | { |
|
63 | 63 | case gpiolowspeed : |
|
64 | 64 | if((*gpio & 0xFF)==0xFF) |
|
65 | 65 | { |
|
66 | 66 | for(int i=0;i<16;i++) |
|
67 | 67 | { |
|
68 | 68 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2)); |
|
69 | 69 | GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_2MHz) << (i * 2)); |
|
70 | 70 | } |
|
71 | 71 | } |
|
72 | 72 | else |
|
73 | 73 | { |
|
74 | 74 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((*gpio & 0xFF) * 2)); |
|
75 | 75 | GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_2MHz) << ((*gpio & 0xFF) * 2)); |
|
76 | 76 | } |
|
77 | 77 | *gpio &= ~GPIOSPEEDMASK; |
|
78 | 78 | *gpio |= gpiolowspeed; |
|
79 | 79 | break; |
|
80 | 80 | case gpiomediumspeed : |
|
81 | 81 | if((*gpio & 0xFF)==0xFF) |
|
82 | 82 | { |
|
83 | 83 | for(int i=0;i<16;i++) |
|
84 | 84 | { |
|
85 | 85 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2)); |
|
86 | 86 | GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_25MHz) << (i * 2)); |
|
87 | 87 | } |
|
88 | 88 | } |
|
89 | 89 | else |
|
90 | 90 | { |
|
91 | 91 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((*gpio & 0xFF) * 2)); |
|
92 | 92 | GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_25MHz) << ((*gpio & 0xFF) * 2)); |
|
93 | 93 | } |
|
94 | 94 | *gpio &= ~GPIOSPEEDMASK; |
|
95 | 95 | *gpio |= gpiomediumspeed; |
|
96 | 96 | break; |
|
97 | 97 | case gpiofastspeed : |
|
98 | 98 | if((*gpio & 0xFF)==0xFF) |
|
99 | 99 | { |
|
100 | 100 | for(int i=0;i<16;i++) |
|
101 | 101 | { |
|
102 | 102 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2)); |
|
103 | 103 | GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_50MHz) << (i * 2)); |
|
104 | 104 | } |
|
105 | 105 | } |
|
106 | 106 | else |
|
107 | 107 | { |
|
108 | 108 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((*gpio & 0xFF) * 2)); |
|
109 | 109 | GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_50MHz) << ((*gpio & 0xFF) * 2)); |
|
110 | 110 | } |
|
111 | 111 | *gpio &= ~GPIOSPEEDMASK; |
|
112 | 112 | *gpio |= gpiofastspeed; |
|
113 | 113 | break; |
|
114 | 114 | case gpiohighspeed : |
|
115 | 115 | if((*gpio & 0xFF)==0xFF) |
|
116 | 116 | { |
|
117 | 117 | for(int i=0;i<16;i++) |
|
118 | 118 | { |
|
119 | 119 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (i * 2)); |
|
120 | 120 | GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_100MHz) << (i * 2)); |
|
121 | 121 | } |
|
122 | 122 | } |
|
123 | 123 | else |
|
124 | 124 | { |
|
125 | 125 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << ((*gpio & 0xFF) * 2)); |
|
126 | 126 | GPIOx->OSPEEDR |= ((uint32_t)(GPIO_Speed_100MHz) << ((*gpio & 0xFF) * 2)); |
|
127 | 127 | } |
|
128 | 128 | *gpio &= ~GPIOSPEEDMASK; |
|
129 | 129 | *gpio |= gpiohighspeed; |
|
130 | 130 | break; |
|
131 | 131 | } |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | void gpiosetdir(gpio_t* gpio,gpiodir_t dir) |
|
136 | 136 | { |
|
137 | 137 | GPIO_TypeDef* GPIOx = GPIOGETPORT((*gpio)); |
|
138 | 138 | switch(dir) |
|
139 | 139 | { |
|
140 | 140 | case gpiooutdir: |
|
141 | 141 | if((*gpio & 0xFF)==0xFF) |
|
142 | 142 | { |
|
143 | 143 | for(int i=0;i<16;i++) |
|
144 | 144 | { |
|
145 | 145 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2)); |
|
146 | 146 | GPIOx->MODER |= (GPIO_Mode_OUT << (i * 2)); |
|
147 | 147 | } |
|
148 | 148 | } |
|
149 | 149 | else |
|
150 | 150 | { |
|
151 | 151 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((*gpio & 0xFF) * 2)); |
|
152 | 152 | GPIOx->MODER |= (GPIO_Mode_OUT << ((*gpio & 0xFF) * 2)); |
|
153 | 153 | } |
|
154 | 154 | *gpio &= ~GPIODIRMASK; |
|
155 | 155 | *gpio |= gpiooutdir; |
|
156 | 156 | break; |
|
157 | 157 | case gpioaf: |
|
158 | 158 | if((*gpio & 0xFF)==0xFF) |
|
159 | 159 | { |
|
160 | 160 | for(int i=0;i<16;i++) |
|
161 | 161 | { |
|
162 | 162 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2)); |
|
163 | 163 | GPIOx->MODER |= (GPIO_Mode_AF << (i * 2)); |
|
164 | 164 | } |
|
165 | 165 | } |
|
166 | 166 | else |
|
167 | 167 | { |
|
168 | 168 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((*gpio & 0xFF) * 2)); |
|
169 | 169 | GPIOx->MODER |= (GPIO_Mode_AF << ((*gpio & 0xFF) * 2)); |
|
170 | 170 | } |
|
171 | 171 | *gpio &= ~GPIODIRMASK; |
|
172 | 172 | *gpio |= gpioaf; |
|
173 | 173 | break; |
|
174 | 174 | case gpioan: |
|
175 | 175 | if((*gpio & 0xFF)==0xFF) |
|
176 | 176 | { |
|
177 | 177 | for(int i=0;i<16;i++) |
|
178 | 178 | { |
|
179 | 179 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2)); |
|
180 | 180 | GPIOx->MODER |= (GPIO_Mode_AN << (i * 2)); |
|
181 | 181 | } |
|
182 | 182 | } |
|
183 | 183 | else |
|
184 | 184 | { |
|
185 | 185 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((*gpio & 0xFF) * 2)); |
|
186 | 186 | GPIOx->MODER |= (GPIO_Mode_AN << ((*gpio & 0xFF) * 2)); |
|
187 | 187 | } |
|
188 | 188 | *gpio &= ~GPIODIRMASK; |
|
189 | 189 | *gpio |= gpioan; |
|
190 | 190 | break; |
|
191 | 191 | default : |
|
192 | 192 | if((*gpio & 0xFF)==0xFF) |
|
193 | 193 | { |
|
194 | 194 | for(int i=0;i<16;i++) |
|
195 | 195 | { |
|
196 | 196 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (i * 2)); |
|
197 | 197 | GPIOx->MODER |= (GPIO_Mode_IN << (i * 2)); |
|
198 | 198 | } |
|
199 | 199 | } |
|
200 | 200 | else |
|
201 | 201 | { |
|
202 | 202 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << ((*gpio & 0xFF) * 2)); |
|
203 | 203 | GPIOx->MODER |= (GPIO_Mode_IN << ((*gpio & 0xFF) * 2)); |
|
204 | 204 | } |
|
205 | 205 | *gpio &= ~GPIODIRMASK; |
|
206 | 206 | *gpio |= gpioindir; |
|
207 | 207 | break; |
|
208 | 208 | } |
|
209 | 209 | |
|
210 | 210 | } |
|
211 | 211 | |
|
212 | 212 | |
|
213 | 213 | void gpiosetouttype(gpio_t* gpio, gpioouttype_t outtype) |
|
214 | 214 | { |
|
215 | 215 | GPIO_TypeDef* GPIOx = GPIOGETPORT((*gpio)); |
|
216 | 216 | if(outtype == gpioopendraintype) |
|
217 | 217 | { |
|
218 | 218 | if((*gpio & 0xFF)==0xFF) |
|
219 | 219 | { |
|
220 | 220 | for(int i=0;i<16;i++) |
|
221 | 221 | { |
|
222 | 222 | GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)i)); |
|
223 | 223 | GPIOx->OTYPER |= (uint16_t)(GPIO_OType_OD<<((uint16_t)i)); |
|
224 | 224 | } |
|
225 | 225 | } |
|
226 | 226 | else |
|
227 | 227 | { |
|
228 | 228 | GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)(*gpio & 0xFF))); |
|
229 | 229 | GPIOx->OTYPER |= (uint16_t)(GPIO_OType_OD<<((uint16_t)(*gpio & 0xFF))); |
|
230 | 230 | } |
|
231 | 231 | *gpio &= ~GPIOOUTTYPEMASK; |
|
232 | 232 | *gpio |= gpioopendraintype; |
|
233 | 233 | } |
|
234 | 234 | else |
|
235 | 235 | { |
|
236 | 236 | if((*gpio & 0xFF)==0xFF) |
|
237 | 237 | { |
|
238 | 238 | for(int i=0;i<16;i++) |
|
239 | 239 | { |
|
240 | 240 | GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)i)); |
|
241 | 241 | GPIOx->OTYPER |= (uint16_t)(GPIO_OType_PP<<((uint16_t)i)); |
|
242 | 242 | } |
|
243 | 243 | } |
|
244 | 244 | else |
|
245 | 245 | { |
|
246 | 246 | GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)(*gpio & 0xFF))); |
|
247 | 247 | GPIOx->OTYPER |= (uint16_t)(GPIO_OType_PP<<((uint16_t)(*gpio & 0xFF))); |
|
248 | 248 | } |
|
249 | 249 | *gpio &= ~GPIOOUTTYPEMASK; |
|
250 | 250 | *gpio |= gpiopushpulltype; |
|
251 | 251 | } |
|
252 | 252 | } |
|
253 | 253 | |
|
254 | 254 | |
|
255 | 255 | void gpiosetpulltype(gpio_t* gpio,gpiopulltype_t pulltype) |
|
256 | 256 | { |
|
257 | 257 | GPIO_TypeDef* GPIOx = GPIOGETPORT(*gpio); |
|
258 | 258 | switch(pulltype) |
|
259 | 259 | { |
|
260 | 260 | case gpiopulluptype: |
|
261 | 261 | if((*gpio & 0xFF)==0xFF) |
|
262 | 262 | { |
|
263 | 263 | for(int i=0;i<16;i++) |
|
264 | 264 | { |
|
265 | 265 | GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)i * 2)); |
|
266 | 266 | GPIOx->PUPDR |= (GPIO_PuPd_UP << (i * 2)); |
|
267 | 267 | } |
|
268 | 268 | } |
|
269 | 269 | else |
|
270 | 270 | { |
|
271 | 271 | GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)(*gpio & 0xFF) * 2)); |
|
272 | 272 | GPIOx->PUPDR |= (GPIO_PuPd_UP << ((*gpio & 0xFF) * 2)); |
|
273 | 273 | } |
|
274 | 274 | *gpio &= ~GPIOPULLTYPEMASK; |
|
275 | 275 | *gpio |= gpiopulluptype; |
|
276 | 276 | break; |
|
277 | 277 | case gpiopulldowntype: |
|
278 | 278 | if((*gpio & 0xFF)==0xFF) |
|
279 | 279 | { |
|
280 | 280 | for(int i=0;i<16;i++) |
|
281 | 281 | { |
|
282 | 282 | GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)i * 2)); |
|
283 | 283 | GPIOx->PUPDR |= (GPIO_PuPd_DOWN << (i * 2)); |
|
284 | 284 | } |
|
285 | 285 | } |
|
286 | 286 | else |
|
287 | 287 | { |
|
288 | 288 | GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)(*gpio & 0xFF) * 2)); |
|
289 | 289 | GPIOx->PUPDR |= (GPIO_PuPd_DOWN << ((*gpio & 0xFF) * 2)); |
|
290 | 290 | } |
|
291 | 291 | *gpio &= ~GPIOPULLTYPEMASK; |
|
292 | 292 | *gpio |= gpiopulldowntype; |
|
293 | 293 | break; |
|
294 | 294 | default : |
|
295 | 295 | if((*gpio & 0xFF)==0xFF) |
|
296 | 296 | { |
|
297 | 297 | for(int i=0;i<16;i++) |
|
298 | 298 | { |
|
299 | 299 | GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)i * 2)); |
|
300 | 300 | GPIOx->PUPDR |= (GPIO_PuPd_NOPULL << (i * 2)); |
|
301 | 301 | } |
|
302 | 302 | } |
|
303 | 303 | else |
|
304 | 304 | { |
|
305 | 305 | GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)(*gpio & 0xFF) * 2)); |
|
306 | 306 | GPIOx->PUPDR |= (GPIO_PuPd_NOPULL << ((*gpio & 0xFF) * 2)); |
|
307 | 307 | } |
|
308 | 308 | *gpio &= ~GPIOPULLTYPEMASK; |
|
309 | 309 | *gpio |= gpionopulltype; |
|
310 | 310 | break; |
|
311 | 311 | } |
|
312 | 312 | } |
|
313 | 313 | |
|
314 | 314 | void gpioset(gpio_t gpio) |
|
315 | 315 | { |
|
316 | 316 | GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio); |
|
317 | 317 | if((gpio & 0xFF)==0xFF) |
|
318 | 318 | { |
|
319 | 319 | GPIOx->BSRRL = -1; |
|
320 | 320 | } |
|
321 | 321 | else |
|
322 | 322 | { |
|
323 | 323 | GPIOx->BSRRL = 1<<(gpio & 0xFF); |
|
324 | 324 | } |
|
325 | 325 | } |
|
326 | 326 | |
|
327 | 327 | |
|
328 | 328 | void gpioclr(gpio_t gpio) |
|
329 | 329 | { |
|
330 | 330 | GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio); |
|
331 | 331 | if((gpio & 0xFF)==0xFF) |
|
332 | 332 | { |
|
333 | 333 | GPIOx->BSRRH = -1; |
|
334 | 334 | } |
|
335 | 335 | else |
|
336 | 336 | { |
|
337 | 337 | GPIOx->BSRRH = 1<<(gpio & 0xFF); |
|
338 | 338 | } |
|
339 | 339 | } |
|
340 | 340 | |
|
341 | 341 | void gpiosetval(gpio_t gpio,int val) |
|
342 | 342 | { |
|
343 | 343 | GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio); |
|
344 | 344 | if((gpio & 0xFF)==0xFF) |
|
345 | 345 | { |
|
346 | 346 | GPIOx->ODR = val; |
|
347 | 347 | } |
|
348 | 348 | else |
|
349 | 349 | { |
|
350 | GPIOx->ODR = (val & 1)<<(gpio & 0xFF); | |
|
350 | if(val) | |
|
351 | GPIOx->BSRRL = 1<<(gpio & 0xFF); | |
|
352 | else | |
|
353 | GPIOx->BSRRH = 1<<(gpio & 0xFF); | |
|
351 | 354 | } |
|
352 | 355 | } |
|
353 | 356 | |
|
354 | 357 | int gpiogetval(gpio_t gpio) |
|
355 | 358 | { |
|
356 | 359 | GPIO_TypeDef* GPIOx = GPIOGETPORT(gpio); |
|
357 | 360 | if((gpio & 0xFF)==0xFF) |
|
358 | 361 | { |
|
359 | 362 | return GPIOx->IDR; |
|
360 | 363 | } |
|
361 | 364 | else |
|
362 | 365 | { |
|
363 | 366 | return ((GPIOx->IDR>>(gpio & 0xFF)) & 1); |
|
364 | 367 | } |
|
365 | 368 | } |
|
366 | 369 | |
|
367 | 370 | |
|
368 | 371 | |
|
369 | 372 | |
|
370 | 373 | |
|
371 | 374 | |
|
372 | 375 | |
|
373 | 376 | |
|
374 | 377 | |
|
375 | 378 | |
|
376 | 379 | |
|
377 | 380 | |
|
378 | 381 | |
|
379 | 382 | |
|
380 | 383 | |
|
381 | 384 | |
|
382 | 385 | |
|
383 | 386 | |
|
384 | 387 | |
|
385 | 388 | |
|
386 | 389 | |
|
387 | 390 | |
|
388 | 391 | |
|
389 | 392 |
@@ -1,449 +1,470 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2011, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | |
|
23 | 23 | #include <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 | 126 | MISO |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
127 | 127 | gpiosetconfig(&MISO); |
|
128 | 128 | GPIO_PinAFConfig(GPIOGETPORT(MISO), (uint8_t)(MISO & 0xF), gpioAFspix); |
|
129 | 129 | } |
|
130 | 130 | if(MOSIpin!=-1) |
|
131 | 131 | { |
|
132 | 132 | MOSI = gpioopen(MOSIpin); |
|
133 | 133 | MOSI |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
134 | 134 | gpiosetconfig(&MOSI); |
|
135 | 135 | GPIO_PinAFConfig(GPIOGETPORT(MOSI), (uint8_t)(MOSI & 0xF), gpioAFspix); |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | if(SCKpin!=-1) |
|
139 | 139 | { |
|
140 | 140 | SCK = gpioopen(SCKpin); |
|
141 | 141 | SCK |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
142 | 142 | gpiosetconfig(&SCK); |
|
143 | 143 | GPIO_PinAFConfig(GPIOGETPORT(SCK), (uint8_t)(SCK & 0xF), gpioAFspix); |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | if(SCSpin!=-1) |
|
147 | 147 | { |
|
148 | 148 | SCS = gpioopen(SCSpin); |
|
149 | 149 | SCS |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
150 | 150 | gpiosetconfig(&SCS); |
|
151 | 151 | GPIO_PinAFConfig(GPIOGETPORT(SCS), (uint8_t)(SCS & 0xF), gpioAFspix); |
|
152 | 152 | _dev_->CR2 |= (1<<2); |
|
153 | 153 | } |
|
154 | 154 | else |
|
155 | 155 | { |
|
156 | 156 | _dev_->CR2 &= ~(1<<2); |
|
157 | 157 | } |
|
158 | 158 | return 1; |
|
159 | 159 | } |
|
160 | 160 | return -1; |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | |
|
164 | 164 | int spienable(spi_t spidev) |
|
165 | 165 | { |
|
166 | 166 | if((spidev<3)&&(spidev>=0)) |
|
167 | 167 | { |
|
168 | 168 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
169 | 169 | _dev_->CR1 |= (1<<6); |
|
170 | 170 | return 1; |
|
171 | 171 | } |
|
172 | 172 | return -1; |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | 175 | |
|
176 | 176 | int spidisable(spi_t spidev) |
|
177 | 177 | { |
|
178 | 178 | if((spidev<3)&&(spidev>=0)) |
|
179 | 179 | { |
|
180 | 180 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
181 | 181 | _dev_->CR1 &= ~(1<<6); |
|
182 | 182 | return 1; |
|
183 | 183 | } |
|
184 | 184 | return -1; |
|
185 | 185 | } |
|
186 | 186 | |
|
187 | 187 | int spisetconfig(spi_t spidev, uint32_t config, uint32_t speed) |
|
188 | 188 | { |
|
189 | 189 | if((spidev<3)&&(spidev>=0)) |
|
190 | 190 | { |
|
191 | 191 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
192 | 192 | _dev_->CR2 |= (1<<2); |
|
193 | 193 | _dev_->CR1 |= (1<<2); |
|
194 | 194 | spisetspeed(spidev,speed); |
|
195 | 195 | spisetdatabits(spidev,config & SPIBITSMASK); |
|
196 | 196 | spisetbitorder(spidev,config & SPIBITORDERMASK); |
|
197 | 197 | spisetclkinhlevel(spidev,config & SPICLKINHLVLMASK); |
|
198 | 198 | spisetclkphase(spidev,config & SPICLKPHASEMASK); |
|
199 | 199 | return 0; |
|
200 | 200 | } |
|
201 | 201 | return 1; |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | int spisetspeed(spi_t spidev, uint32_t speed) |
|
205 | 205 | { |
|
206 | 206 | if((spidev<3)&&(spidev>=0)) |
|
207 | 207 | { |
|
208 | 208 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
209 | 209 | uint32_t apbclock = 0x00; |
|
210 | 210 | RCC_ClocksTypeDef RCC_ClocksStatus; |
|
211 | 211 | RCC_GetClocksFreq(&RCC_ClocksStatus); |
|
212 | 212 | if (_dev_ == SPI1) |
|
213 | 213 | { |
|
214 | 214 | apbclock = RCC_ClocksStatus.PCLK2_Frequency; |
|
215 | 215 | } |
|
216 | 216 | else |
|
217 | 217 | { |
|
218 | 218 | apbclock = RCC_ClocksStatus.PCLK1_Frequency; |
|
219 | 219 | } |
|
220 | 220 | int32_t speederror = 0x7FFFFFFF; //max error |
|
221 | 221 | int32_t prev_speederror = 0x7FFFFFFF; |
|
222 | 222 | int32_t realspeed = 0; |
|
223 | 223 | unsigned char divider = 0; |
|
224 | 224 | do |
|
225 | 225 | { |
|
226 | 226 | divider ++; |
|
227 | 227 | prev_speederror = speederror; |
|
228 | 228 | realspeed = apbclock>>(divider); |
|
229 | 229 | speederror = realspeed - speed; |
|
230 | 230 | if(speederror<0)speederror=-speederror; |
|
231 | 231 | if(divider>8)break; |
|
232 | 232 | }while(speederror<prev_speederror); |
|
233 | 233 | speed = apbclock>>(divider-1); |
|
234 | 234 | divider-=2; |
|
235 | 235 | _dev_->CR1 &= 0xFFD7; // clear prescaler bits 3:5 |
|
236 | 236 | _dev_->CR1 |= ((0x7 & divider)<<3); |
|
237 | 237 | return 1; |
|
238 | 238 | } |
|
239 | 239 | return -1; |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | uint32_t spigetspeed(spi_t spidev) | |
|
243 | { | |
|
244 | if((spidev<3)&&(spidev>=0)) | |
|
245 | { | |
|
246 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; | |
|
247 | uint32_t apbclock = 0x00; | |
|
248 | RCC_ClocksTypeDef RCC_ClocksStatus; | |
|
249 | RCC_GetClocksFreq(&RCC_ClocksStatus); | |
|
250 | if (_dev_ == SPI1) | |
|
251 | { | |
|
252 | apbclock = RCC_ClocksStatus.PCLK2_Frequency; | |
|
253 | } | |
|
254 | else | |
|
255 | { | |
|
256 | apbclock = RCC_ClocksStatus.PCLK1_Frequency; | |
|
257 | } | |
|
258 | int BR= (_dev_->CR1>>3)&0x7; | |
|
259 | return apbclock>>(BR+1); | |
|
260 | } | |
|
261 | return -1; | |
|
262 | } | |
|
242 | 263 | |
|
243 | 264 | int spisetdatabits(spi_t spidev,spibits_t bitscnt) |
|
244 | 265 | { |
|
245 | 266 | if((spidev<3)&&(spidev>=0)) |
|
246 | 267 | { |
|
247 | 268 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
248 | 269 | int result = 0; |
|
249 | 270 | switch(bitscnt) |
|
250 | 271 | { |
|
251 | 272 | case spi8bits: |
|
252 | 273 | _dev_->CR1 &= ~(1<<11); |
|
253 | 274 | result = 1; |
|
254 | 275 | break; |
|
255 | 276 | case spi16bits: |
|
256 | 277 | _dev_->CR1 |= (1<<11); |
|
257 | 278 | result = 1; |
|
258 | 279 | break; |
|
259 | 280 | default: |
|
260 | 281 | result =-1; |
|
261 | 282 | break; |
|
262 | 283 | } |
|
263 | 284 | return result; |
|
264 | 285 | } |
|
265 | 286 | return -1; |
|
266 | 287 | } |
|
267 | 288 | |
|
268 | 289 | int spisetbitorder(spi_t spidev,spibitorder_t order) |
|
269 | 290 | { |
|
270 | 291 | if((spidev<3)&&(spidev>=0)) |
|
271 | 292 | { |
|
272 | 293 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
273 | 294 | if(order==spimsbfirst) |
|
274 | 295 | { |
|
275 | 296 | _dev_->CR1 &= ~(1<<7); |
|
276 | 297 | return 1; |
|
277 | 298 | } |
|
278 | 299 | else |
|
279 | 300 | { |
|
280 | 301 | if(order==spilsbfirst) |
|
281 | 302 | { |
|
282 | 303 | _dev_->CR1 |= (1<<7); |
|
283 | 304 | return 1; |
|
284 | 305 | } |
|
285 | 306 | else return -1; |
|
286 | 307 | } |
|
287 | 308 | } |
|
288 | 309 | return -1; |
|
289 | 310 | } |
|
290 | 311 | |
|
291 | 312 | int spisetclkinhlevel(spi_t spidev,spiclkinhlvl_t level) |
|
292 | 313 | { |
|
293 | 314 | if((spidev<3)&&(spidev>=0)) |
|
294 | 315 | { |
|
295 | 316 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
296 | 317 | if(level==spiclkinhlow) |
|
297 | 318 | { |
|
298 | 319 | _dev_->CR1 &= ~(1<<1); |
|
299 | 320 | return 1; |
|
300 | 321 | } |
|
301 | 322 | else |
|
302 | 323 | { |
|
303 | 324 | if(level==spiclkinhhigh) |
|
304 | 325 | { |
|
305 | 326 | _dev_->CR1 |= (1<<1); |
|
306 | 327 | return 1; |
|
307 | 328 | } |
|
308 | 329 | else return -1; |
|
309 | 330 | } |
|
310 | 331 | } |
|
311 | 332 | return -1; |
|
312 | 333 | } |
|
313 | 334 | |
|
314 | 335 | int spisetclkphase(spi_t spidev,spiclkphase_t phase) |
|
315 | 336 | { |
|
316 | 337 | if((spidev<3)&&(spidev>=0)) |
|
317 | 338 | { |
|
318 | 339 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
319 | 340 | if(phase==spiclkfirstedge) |
|
320 | 341 | { |
|
321 | 342 | _dev_->CR1 &= ~1; |
|
322 | 343 | return 1; |
|
323 | 344 | } |
|
324 | 345 | else |
|
325 | 346 | { |
|
326 | 347 | if(phase==spiclksecondedge) |
|
327 | 348 | { |
|
328 | 349 | _dev_->CR1 |= 1; |
|
329 | 350 | return 1; |
|
330 | 351 | } |
|
331 | 352 | else return -1; |
|
332 | 353 | } |
|
333 | 354 | } |
|
334 | 355 | return -1; |
|
335 | 356 | } |
|
336 | 357 | |
|
337 | 358 | int spiputw(spi_t spidev,uint16_t data) |
|
338 | 359 | { |
|
339 | 360 | if((spidev<3)&&(spidev>=0)) |
|
340 | 361 | { |
|
341 | 362 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
342 | 363 | while(((_dev_->SR & (1<<1)) == 0) ); |
|
343 | 364 | _dev_->DR = data; |
|
344 | 365 | while(((_dev_->SR & (1<<0)) == 0) ); |
|
345 | 366 | return _dev_->DR; |
|
346 | 367 | } |
|
347 | 368 | return -1; |
|
348 | 369 | } |
|
349 | 370 | uint16_t spigetw(spi_t spidev) |
|
350 | 371 | { |
|
351 | 372 | if((spidev<3)&&(spidev>=0)) |
|
352 | 373 | { |
|
353 | 374 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
354 | 375 | while(((_dev_->SR & (1<<1)) == 0) ); |
|
355 | 376 | _dev_->DR = 0xFFFF; |
|
356 | 377 | while(((_dev_->SR & (1<<0)) == 0) ); |
|
357 | 378 | return _dev_->DR; |
|
358 | 379 | } |
|
359 | 380 | return -1; |
|
360 | 381 | } |
|
361 | 382 | |
|
362 | 383 | int spiputs(spi_t spidev,char* s) |
|
363 | 384 | { |
|
364 | 385 | while (*s) spiputw(spidev,*s++); |
|
365 | 386 | return 1; |
|
366 | 387 | } |
|
367 | 388 | |
|
368 | 389 | int spigets(spi_t spidev,char* s) |
|
369 | 390 | { |
|
370 | 391 | do |
|
371 | 392 | { |
|
372 | 393 | (*s) = spigetw(spidev); |
|
373 | 394 | } |
|
374 | 395 | while(*s++); |
|
375 | 396 | return 1; |
|
376 | 397 | } |
|
377 | 398 | |
|
378 | 399 | int spiputnw(spi_t spidev,uint16_t* w,int n) |
|
379 | 400 | { |
|
380 | 401 | while(n!=0) |
|
381 | 402 | { |
|
382 | 403 | spiputw(spidev,*w++); |
|
383 | 404 | n--; |
|
384 | 405 | } |
|
385 | 406 | return 1; |
|
386 | 407 | } |
|
387 | 408 | |
|
388 | 409 | int spigetnw(spi_t spidev,uint16_t* w,int n) |
|
389 | 410 | { |
|
390 | 411 | while(n!=0) |
|
391 | 412 | { |
|
392 | 413 | *w++=spigetw(spidev); |
|
393 | 414 | n--; |
|
394 | 415 | } |
|
395 | 416 | return 1; |
|
396 | 417 | } |
|
397 | 418 | |
|
398 | 419 | int spiputnc(spi_t spidev,char* c,int n) |
|
399 | 420 | { |
|
400 | 421 | while(n!=0) |
|
401 | 422 | { |
|
402 | 423 | spiputw(spidev,*c++); |
|
403 | 424 | n--; |
|
404 | 425 | } |
|
405 | 426 | return 1; |
|
406 | 427 | } |
|
407 | 428 | |
|
408 | 429 | int spigetnc(spi_t spidev,char* c,int n) |
|
409 | 430 | { |
|
410 | 431 | while(n!=0) |
|
411 | 432 | { |
|
412 | 433 | *c++=spigetw(spidev); |
|
413 | 434 | n--; |
|
414 | 435 | } |
|
415 | 436 | return 1; |
|
416 | 437 | } |
|
417 | 438 | |
|
418 | 439 | int spiavailiabledata(spi_t spidev) |
|
419 | 440 | { |
|
420 | 441 | return 0; |
|
421 | 442 | } |
|
422 | 443 | |
|
423 | 444 | |
|
424 | 445 | int spitransactionfinished(spi_t spidev) |
|
425 | 446 | { |
|
426 | 447 | SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; |
|
427 | 448 | if((spidev<3)&&(spidev>=0)) |
|
428 | 449 | { |
|
429 | 450 | if((_dev_->SR & (1<<7)) == (1<<7))return 1; |
|
430 | 451 | } |
|
431 | 452 | return 0; |
|
432 | 453 | } |
|
433 | 454 | |
|
434 | 455 | |
|
435 | 456 | |
|
436 | 457 | |
|
437 | 458 | |
|
438 | 459 | |
|
439 | 460 | |
|
440 | 461 | |
|
441 | 462 | |
|
442 | 463 | |
|
443 | 464 | |
|
444 | 465 | |
|
445 | 466 | |
|
446 | 467 | |
|
447 | 468 | |
|
448 | 469 | |
|
449 | 470 |
@@ -1,14 +1,14 | |||
|
1 | 1 | |
|
2 | 2 | INCLUDEPATH += $$[QT_INSTALL_HEADERS] \ |
|
3 | 3 | $$[QT_INSTALL_HEADERS]/GRAPHIC/CONTROLERS \ |
|
4 | 4 | $$[QT_INSTALL_HEADERS]/GRAPHIC/TC_CONTROLERS \ |
|
5 | 5 | $$[QT_INSTALL_HEADERS]/GRAPHIC/GUI/FONTS \ |
|
6 | 6 | $$[QT_INSTALL_HEADERS]/GRAPHIC/GUI/Widgets \ |
|
7 | 7 | $$[QT_INSTALL_HEADERS]/$$ARCH |
|
8 | 8 | |
|
9 | 9 | |
|
10 | LIBS += -L$$[QT_INSTALL_LIBS]/$$ARCH -lterminal -lCS43L22 -lina226 -lili9328 -lssd2119 -lSTMPE811 -lfonts -lsdcard -lfat32 -lmbr -luart -li2c -lgpio -lspi -lcpu -lcore -lm | |
|
10 | LIBS += -L$$[QT_INSTALL_LIBS]/$$ARCH -lterminal -lCS43L22 -lina226 -lili9328 -lssd2119 -lSTMPE811 -lvs10XX -lfonts -lsdcard -lfat32 -lmbr -luart -li2c -lgpio -lspi -lcpu -lcore -lm | |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 |
General Comments 0
You need to be logged in to leave comments.
Login now