diff main.c @ 8:58d76cf522ff

Split out code into separate files.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 04 Feb 2012 13:29:31 +1030
parents 9404b9869c27
children 093bc0c3b1cc
line wrap: on
line diff
--- a/main.c	Sun Jan 22 17:10:51 2012 +1030
+++ b/main.c	Sat Feb 04 13:29:31 2012 +1030
@@ -8,9 +8,14 @@
 #include <stdlib.h>
 
 #include "stm32f10x.h"
+
+#include "comm.h"
+#include "delay.h"
+#include "flash.h"
+#include "hw.h"
 #include "lcd.h"
 #include "main.h"
-#include "comm.h"
+#include "touch.h"
 
 typedef struct {
     char		buf[40];
@@ -18,7 +23,6 @@
     uint8_t		len;
 } consbuf_t;
 
-void Setup_HW(void);
 void NVIC_Configuration(void);
 
 
@@ -105,8 +109,8 @@
     
     cmd.state = cmd.len = 0;
     
-    /* Setup USART etc */
-    Setup_HW();
+    /* Init hardware - configure IO ports and external peripherals */
+    hw_init();
 
     /* NVIC configuration */
     NVIC_Configuration();
@@ -119,9 +123,6 @@
 	    ;
     }
 
-    /* Init LCD panel */
-    LCD_init();
-
     /* Set stdout to unbuffered */
     setvbuf(stdout, NULL, _IONBF, 0);
     
@@ -148,16 +149,10 @@
 	    } else if (!strncmp("read", cmd.buf, 4)) {
 		printf("PB5 = %d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15));
 	    } else if (!strncmp("touch", cmd.buf, 5)) {
+		uint16_t	x, y, z1, z2;
+		float		t, t2;
 		for (int i = 0; i < 10; i++) {
-		    uint16_t	x, y, z1, z2;
-		    float	t, t2;
-		    
-		    x = TPRead(TP_READ_SEL(TP_CHAN_X, TP_MODE_12, TP_REF_DIFF, TP_PD_ON));
-		    y = TPRead(TP_READ_SEL(TP_CHAN_Y, TP_MODE_12, TP_REF_DIFF, TP_PD_ON));
-		    z1 = TPRead(TP_READ_SEL(TP_CHAN_Z1, TP_MODE_12, TP_REF_DIFF, TP_PD_ON));
-		    z2 = TPRead(TP_READ_SEL(TP_CHAN_Z2, TP_MODE_12, TP_REF_DIFF, TP_PD_ON));
-		    t = ((float)x / 4096.0) * (((float)z2 / (float)z1) - 1);
-		    t2 = (((float)x / 4096) * ((4096.0 / (float)z1) - 1)) - (1 - ((float)y / (float)4096.0));
+		    tp_getcoords(&x, &y, &z1, &z2, &t, &t2);
 		    printf("X = %5d Y = %5d Z1 = %5d Z2 = %5d T = %7.2f T2 = %7.2f\r\n", x, y, z1, z2, t, t2);
 		}
 	    } else if (!strncmp("fl", cmd.buf, 2)) {
@@ -173,8 +168,8 @@
 		    "BPL"
 		};
 		
-		printf("Flash ID = 0x%04hx (expect 0xbf41)\r\n", FlashReadID());
-		status = FlashReadStatus();
+		printf("Flash ID = 0x%04hx (expect 0xbf41)\r\n", flashreadid());
+		status = flashreadstatus();
 
 		fputs("Status = ", stdout);
 		for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++)
@@ -184,9 +179,9 @@
 		    }
 		printf("(0x%02x)\r\n", status);
 
-		FlashWriteStatus(0x00);
+		flashwritestatus(0x00);
 		
-		status = FlashReadStatus();
+		status = flashreadstatus();
 
 		fputs("Status = ", stdout);
 		for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++)
@@ -199,112 +194,40 @@
 		lcd_setpwm(atoi(cmd.buf + 4));
 	    } else if (!strncmp("timing", cmd.buf, 6)) {
 		fputs("Timing..\r\n", stdout);
-		Delay(10000);
+		delay(10000);
 		fputs("Done\r\n", stdout);
+	    } else if (!strncmp("circ ", cmd.buf, 5)) {
+		uint16_t	x, y, r, c;
+		char		col;
+		
+		if (sscanf(cmd.buf, "circ %hu %hu %hu %c", &x, &y, &r, &col) != 4) {
+		    printf("Unable to parse circ args\r\n");
+		    goto out;
+		}
+
+		col = toupper(col);
+		if (col == 'R')
+		    c = LCD_RED;
+		else if (col == 'G')
+		    c = LCD_GREEN;
+		else if (col == 'B')
+		    c = LCD_BLUE;
+		else if (col == 'L')
+		    c = LCD_BLACK;
+		else
+		    c = LCD_WHITE;
+		lcd_circle(x, y, r, c);
 	    } else if (!strncmp("zz", cmd.buf, 2)) {
 		NVIC_SystemReset();
 	    } else {
 		printf("Unknown command\r\n");
 	    }
 	}
+      out:
 	cmd.state = 0;
     }
 }
 
-/* Setup hardware (USART etc) */
-void
-Setup_HW(void) {
-    GPIO_InitTypeDef GPIO_InitStructure;
-    USART_InitTypeDef USART_InitStructure;
-
-    /* RTC stuff */
-    /* Enable PWR and BKP clocks */
-    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
-
-    /* Allow access to BKP Domain */
-    PWR_BackupAccessCmd(ENABLE);
-
-    /* Reset Backup Domain
-     *
-     * This resets the RTC etc back to 0 so probably only useful under user command
-     BKP_DeInit();
-    */
-
-    /* Enable Low Speed External clock */
-    RCC_LSEConfig(RCC_LSE_ON);
-
-    /* Wait till LSE is ready */
-    while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
-	;
-
-    /* Select LSE as RTC Clock Source */
-    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
-
-    /* Enable RTC Clock */
-    RCC_RTCCLKCmd(ENABLE);
-
-    /* Wait for RTC registers synchronization */
-    RTC_WaitForSynchro();
-
-    /* Wait until last write operation on RTC registers has finished */
-    RTC_WaitForLastTask();
-
-    /* Wait until last write operation on RTC registers has finished */
-    RTC_WaitForLastTask();
-
-    /* Set RTC prescaler: set RTC period to 1sec */
-    RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
-
-    /* Wait until last write operation on RTC registers has finished */
-    RTC_WaitForLastTask();
-
-    /* Clock setup */
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
-
-    /* Port configuration */
-    /* Configure USART1 TX (PA.09) as alternate function push-pull */
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
-    GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-    /* Configure USART1 RX (PA.10) as input floating */
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
-    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-    GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-    /* Enable GPIOB clock */
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
-    /* Configure PB5 as output push-pull for LED */
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
-    GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-    /* Configure PB15 as input pull-up push-pull for push button */
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
-    GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-    /* USART configuration */
-    /* USART1 - 115200 8n1, no flow control TX & RX enabled */
-    USART_InitStructure.USART_BaudRate = 115200;
-    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
-    USART_InitStructure.USART_StopBits = USART_StopBits_1;
-    USART_InitStructure.USART_Parity = USART_Parity_No;
-    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
-    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
-    USART_Init(USART1, &USART_InitStructure);
-
-    /* Enable interrupts on receive data */
-    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
-
-    /* Enable USART */
-    USART_Cmd(USART1, ENABLE);
-}
-
 /* Configure interrupt controller */
 #ifdef VECT_TAB_RAM
 /* vector-offset (TBLOFF) from bottom of SRAM. defined in linker script */