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