comparison lcd.c @ 5:efa2c22266e3

PWM control LED backlight. Not sure why the AFIO clock needs to be on for this but not, say USART.
author Daniel O'Connor <darius@dons.net.au>
date Fri, 06 Jan 2012 09:46:28 +1030
parents 2c87e30c982d
children 9404b9869c27
comparison
equal deleted inserted replaced
4:2c87e30c982d 5:efa2c22266e3
60 #include "lcd.h" 60 #include "lcd.h"
61 61
62 #define Bank1_LCD_C ((uint32_t)0x60000000) /* Register Address */ 62 #define Bank1_LCD_C ((uint32_t)0x60000000) /* Register Address */
63 #define Bank1_LCD_D ((uint32_t)0x60020000) /* Data Address */ 63 #define Bank1_LCD_D ((uint32_t)0x60020000) /* Data Address */
64 64
65 unsigned long colour1 = 0;
66
67 void 65 void
68 LCD_WR_Reg(uint16_t index, uint16_t val) { 66 LCD_WR_Reg(uint16_t index, uint16_t val) {
69 *(__IO uint16_t *)(Bank1_LCD_C) = index; 67 *(__IO uint16_t *)(Bank1_LCD_C) = index;
70 *(__IO uint16_t *)(Bank1_LCD_D) = val; 68 *(__IO uint16_t *)(Bank1_LCD_D) = val;
71 } 69 }
220 LCD_init(void) { 218 LCD_init(void) {
221 GPIO_InitTypeDef GPIO_InitStructure; 219 GPIO_InitTypeDef GPIO_InitStructure;
222 SPI_InitTypeDef SPI_InitStructure; 220 SPI_InitTypeDef SPI_InitStructure;
223 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; 221 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
224 FSMC_NORSRAMTimingInitTypeDef p; 222 FSMC_NORSRAMTimingInitTypeDef p;
225 #if 0
226 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 223 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
227 TIM_OCInitTypeDef TIM_OCInitStructure;
228 TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
229 #endif
230 uint16_t id; 224 uint16_t id;
231 225
232 /* Enable FSMC clock */ 226 /* Enable FSMC clock */
233 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 227 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
234 228
235 /* Enable GPIOD clock */ 229 /* Enable GPIOD clock */
236 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); 230 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
231
232 /* Enable alternate function IO clock */
233 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
237 234
238 /* Configures LCD Control lines (FSMC Pins) in alternate function Push-Pull mode. 235 /* Configures LCD Control lines (FSMC Pins) in alternate function Push-Pull mode.
239 * 236 *
240 * PD0(D2), PD1(D3), PD4(NOE), PD5(NWE), PD7(NE1/CS), PD8(D13), PD9(D14), 237 * PD0(D2), PD1(D3), PD4(NOE), PD5(NWE), PD7(NE1/CS), PD8(D13), PD9(D14),
241 * PD10(D15), PD11(A16/RS) PD14(D0), PD15(D1) 238 * PD10(D15), PD11(A16/RS) PD14(D0), PD15(D1)
252 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | 249 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
253 GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; 250 GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
254 GPIO_Init(GPIOE, &GPIO_InitStructure); 251 GPIO_Init(GPIOE, &GPIO_InitStructure);
255 252
256 /* Configure backlight control (PD13/FSMC_A18 remapped to TIM4_CH2) */ 253 /* Configure backlight control (PD13/FSMC_A18 remapped to TIM4_CH2) */
257 #if 0
258 /* Enable TIM4 clock */ 254 /* Enable TIM4 clock */
259 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); 255 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
260 256
261 /* Enable timer function */ 257 /* Enable timer function
258 * Note source clock is SYSCLK / 2 = 36MHz
259 */
262 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; 260 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
263 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 261 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
264 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 262 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
265 GPIO_Init(GPIOD, &GPIO_InitStructure); 263 GPIO_Init(GPIOD, &GPIO_InitStructure);
266 264
269 267
270 /* Reset TIM4 */ 268 /* Reset TIM4 */
271 TIM_DeInit(TIM4); 269 TIM_DeInit(TIM4);
272 270
273 /* Time Base configuration */ 271 /* Time Base configuration */
274 TIM_TimeBaseStructure.TIM_Prescaler = 0x00; 272 TIM_TimeBaseStructure.TIM_Period = 999;
275 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 273 TIM_TimeBaseStructure.TIM_Prescaler = 0;
276 TIM_TimeBaseStructure.TIM_Period = 0x8000; 274 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
277 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 275 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
278 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; 276
279 TIM_TimeBaseInit(TIM4 ,&TIM_TimeBaseStructure); 277 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
280 278
281 /* Channel 1, 2, 3 and 4 Configuration in PWM mode */ 279 /* Init to full brightness */
282 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; 280 lcd_setpwm(1000);
283 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 281
284 TIM_OCInitStructure.TIM_Pulse = 0; 282 /* Enable timer */
285 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; 283 TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
286 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; 284 TIM_ARRPreloadConfig(TIM4, ENABLE);
287 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; 285 TIM_Cmd(TIM4, ENABLE);
288 TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
289 TIM_OC2Init(TIM4, &TIM_OCInitStructure);
290
291 /* Automatic Output enable, Break, dead time and lock configuration*/
292 TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
293 TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
294 TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
295 TIM_BDTRInitStructure.TIM_DeadTime = 0x75;
296 TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
297 TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
298 TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
299
300 TIM_BDTRConfig(TIM4, &TIM_BDTRInitStructure);
301
302 /* TIM1 counter enable */
303 TIM_Cmd(TIM4,ENABLE);
304 #else
305 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
306 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
307 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
308 GPIO_Init(GPIOD, &GPIO_InitStructure);
309
310 GPIO_SetBits(GPIOD, GPIO_Pin_13);
311 #endif
312 286
313 /* Configure reset pin (PE1) as GPIO out PP */ 287 /* Configure reset pin (PE1) as GPIO out PP */
314 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; 288 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
315 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 289 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
316 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 290 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
442 416
443 /* Fill panel with white */ 417 /* Fill panel with white */
444 LCD_WR_Reg(0x20, 0); 418 LCD_WR_Reg(0x20, 0);
445 LCD_WR_Reg(0x21, 0x013f); 419 LCD_WR_Reg(0x21, 0x013f);
446 *(__IO uint16_t *)(Bank1_LCD_C) = 34; 420 *(__IO uint16_t *)(Bank1_LCD_C) = 34;
447 for (colour1 = 0; colour1 < 76800; colour1++) { 421 for (uint32_t i = 0; i < 320 * 240; i++) {
448 LCD_WR_Data(0xffff); 422 LCD_WR_Data(0xffff);
449 } 423 }
450 colour1 = 0;
451 424
452 /* Configure touch screen controller 425 /* Configure touch screen controller
453 * 426 *
454 * Connected to SPI1 which is shared with the AT45DB161D. 427 * Connected to SPI1 which is shared with the AT45DB161D.
455 * 428 *
496 469
497 /* SPI1 enable */ 470 /* SPI1 enable */
498 SPI_Cmd(SPI1, ENABLE); 471 SPI_Cmd(SPI1, ENABLE);
499 472
500 } 473 }
474
475 void
476 lcd_setpwm(uint16_t val) {
477 TIM_OCInitTypeDef TIM_OCInitStructure;
478
479 /* Channel 2 configuration in PWM mode */
480 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
481 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
482 TIM_OCInitStructure.TIM_Pulse = val;
483 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
484
485 TIM_OC2Init(TIM4, &TIM_OCInitStructure);
486
487 }