changeset 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 2b92d9632999
files lcd.c lcd.h main.c
diffstat 3 files changed, 37 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/lcd.c	Tue Jan 03 22:37:18 2012 +1030
+++ b/lcd.c	Fri Jan 06 09:46:28 2012 +1030
@@ -62,8 +62,6 @@
 #define Bank1_LCD_C	((uint32_t)0x60000000)    /* Register Address */
 #define Bank1_LCD_D	((uint32_t)0x60020000)    /* Data Address */
 
-unsigned long colour1 = 0;
-
 void
 LCD_WR_Reg(uint16_t index, uint16_t val) {
     *(__IO uint16_t *)(Bank1_LCD_C) = index;
@@ -222,11 +220,7 @@
     SPI_InitTypeDef			SPI_InitStructure;
     FSMC_NORSRAMInitTypeDef		FSMC_NORSRAMInitStructure;
     FSMC_NORSRAMTimingInitTypeDef	p;
-#if 0
     TIM_TimeBaseInitTypeDef		TIM_TimeBaseStructure;
-    TIM_OCInitTypeDef			TIM_OCInitStructure;
-    TIM_BDTRInitTypeDef			TIM_BDTRInitStructure;  
-#endif
     uint16_t				id;
     
     /* Enable FSMC clock */
@@ -235,6 +229,9 @@
     /* Enable GPIOD clock */
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
 
+    /* Enable alternate function IO clock */
+    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
+
     /* Configures LCD Control lines (FSMC Pins) in alternate function Push-Pull mode.
      *
      * PD0(D2), PD1(D3), PD4(NOE), PD5(NWE), PD7(NE1/CS), PD8(D13), PD9(D14),
@@ -254,11 +251,12 @@
     GPIO_Init(GPIOE, &GPIO_InitStructure);
 
     /* Configure backlight control (PD13/FSMC_A18 remapped to TIM4_CH2) */
-#if 0
     /* Enable TIM4 clock */
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
 
-    /* Enable timer function */
+    /* Enable timer function
+     * Note source clock is SYSCLK / 2 = 36MHz
+     */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
@@ -271,44 +269,20 @@
     TIM_DeInit(TIM4);
 
     /* Time Base configuration */
-    TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
-    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
-    TIM_TimeBaseStructure.TIM_Period = 0x8000;
-    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
-    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
-    TIM_TimeBaseInit(TIM4 ,&TIM_TimeBaseStructure);
+    TIM_TimeBaseStructure.TIM_Period = 999;
+    TIM_TimeBaseStructure.TIM_Prescaler = 0;
+    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
+    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
 
-    /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
-    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; 
-    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
-    TIM_OCInitStructure.TIM_Pulse = 0; 
-    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; 
-    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;         
-    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
-    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;          
-    TIM_OC2Init(TIM4, &TIM_OCInitStructure); 
+    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
 
-    /* Automatic Output enable, Break, dead time and lock configuration*/
-    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
-    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
-    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; 
-    TIM_BDTRInitStructure.TIM_DeadTime = 0x75;
-    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
-    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
-    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
-
-    TIM_BDTRConfig(TIM4, &TIM_BDTRInitStructure);
+    /* Init to full brightness */
+    lcd_setpwm(1000);
 
-    /* TIM1 counter enable */
-    TIM_Cmd(TIM4,ENABLE);
-#else
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
-    GPIO_Init(GPIOD, &GPIO_InitStructure);
-
-    GPIO_SetBits(GPIOD, GPIO_Pin_13);
-#endif
+    /* Enable timer */
+    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
+    TIM_ARRPreloadConfig(TIM4, ENABLE);
+    TIM_Cmd(TIM4, ENABLE);
 
     /* Configure reset pin (PE1) as GPIO out PP */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
@@ -444,10 +418,9 @@
     LCD_WR_Reg(0x20, 0);
     LCD_WR_Reg(0x21, 0x013f);
     *(__IO uint16_t *)(Bank1_LCD_C) = 34;
-    for (colour1 = 0; colour1 < 76800; colour1++) {
+    for (uint32_t i = 0; i < 320 * 240; i++) {
 	LCD_WR_Data(0xffff);
     }
-    colour1 = 0;
 
     /* Configure touch screen controller
      *
@@ -498,3 +471,17 @@
     SPI_Cmd(SPI1, ENABLE);  
 
 }
+
+void
+lcd_setpwm(uint16_t val) {
+    TIM_OCInitTypeDef			TIM_OCInitStructure;
+
+    /* Channel 2 configuration in PWM mode */
+    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
+    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
+    TIM_OCInitStructure.TIM_Pulse = val;
+    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
+
+    TIM_OC2Init(TIM4, &TIM_OCInitStructure);
+
+}
--- a/lcd.h	Tue Jan 03 22:37:18 2012 +1030
+++ b/lcd.h	Fri Jan 06 09:46:28 2012 +1030
@@ -48,5 +48,7 @@
 
 void		FlashWriteCtl(int enable);
 
+void		lcd_setpwm(uint16_t val);
+
 #endif
 
--- a/main.c	Tue Jan 03 22:37:18 2012 +1030
+++ b/main.c	Fri Jan 06 09:46:28 2012 +1030
@@ -197,6 +197,8 @@
 			fputs(" ", stdout);
 		    }
 		printf("(0x%02x)\r\n", status);
+	    } else if (!strncmp("pwm ", cmd.buf, 4)) {
+		lcd_setpwm(atoi(cmd.buf + 4));
 	    } else if (!strncmp("zz", cmd.buf, 2)) {
 		NVIC_SystemReset();
 	    } else {
@@ -226,7 +228,7 @@
      BKP_DeInit();
     */
 
-    /* Enable LSE */
+    /* Enable Low Speed External clock */
     RCC_LSEConfig(RCC_LSE_ON);
 
     /* Wait till LSE is ready */