Mercurial > ~darius > hgwebdir.cgi > stm32temp
view main.c @ 6:2b92d9632999
Add LCD app note.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 14 Jan 2012 11:42:24 +1030 |
parents | efa2c22266e3 |
children | 9404b9869c27 |
line wrap: on
line source
#include <ctype.h> #include <malloc.h> #include <stdio.h> #include <stdint.h> #include <time.h> #include <string.h> #include <sys/time.h> #include <stdlib.h> #include "stm32f10x.h" #include "lcd.h" #include "main.h" #include "comm.h" typedef struct { char buf[40]; volatile uint8_t state; uint8_t len; } consbuf_t; void Setup_HW(void); void NVIC_Configuration(void); /* Called every 1 / TICK_FREQ */ #define TICK_FREQ 10000 RAMFUNC void SysTick_Handler(void) { static uint32_t tick = 0; static int led = 0; tick++; if (tick % 10000 == 0) { led = !led; if (led) GPIO_SetBits(GPIOB, GPIO_Pin_5); else GPIO_ResetBits(GPIOB, GPIO_Pin_5); } } consbuf_t cmd; RAMFUNC void USART1_IRQHandler(void) { char c; int i; /* Recieved data */ while (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { c = USART_ReceiveData(USART1); /* End of line? */ if (c == '\n' || c == '\r') { cmd.buf[cmd.state] = '\0'; fputs("\r\n", stdout); cmd.len = cmd.state; cmd.state = 255; continue; } /* Ctrl-w / Ctrl-u */ if (c == 0x17 || c == 0x15) { for (i = 0; i < cmd.state; i++) fputs("\010\040\010", stdout); cmd.state = 0; continue; } /* Backspace/delete */ if (c == 0x08 || c == 0x7f) { if (cmd.state > 0) { cmd.state--; fputs("\010\040\010", stdout); } continue; } /* Anything unprintable just ignore it */ if (!isprint(c)) continue; cmd.buf[cmd.state] = tolower(c); /* Echo back to the user */ comm_put(cmd.buf[cmd.state]); cmd.state++; /* Over flow? */ if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) { fputs("\r\nLine too long", stdout); cmd.state = 0; continue; } } } int main(void) { char buf[40]; struct tm nowtm; time_t now; cmd.state = cmd.len = 0; /* Setup USART etc */ Setup_HW(); /* NVIC configuration */ NVIC_Configuration(); /* Setup SysTick Timer rate, also enables Systick and Systick-Interrupt */ if (SysTick_Config(SystemCoreClock / TICK_FREQ)) { /* Capture error */ comm_puts("Can't setup SysTick\r\n"); while (1) ; } /* Init LCD interface */ LCD_init(); /* Set stdout to unbuffered */ setvbuf(stdout, NULL, _IONBF, 0); /* Say hello */ fputs("\r\nHello world\r\n", stdout); while (1) { fputs("> ", stdout); while (cmd.state != 255) ; if (cmd.len > 0) { if (!strncmp("gc", cmd.buf, 2)) { now = time(NULL); gmtime_r(&now, &nowtm); strftime(buf, sizeof(buf) - 1, "Time is %Y/%m/%d %H:%M:%S UTC", &nowtm); printf("Time is %s (%d)\r\n", buf, (int)now); } else if (!strncmp("sc ", cmd.buf, 3)) { struct timeval tv; tv.tv_sec = atoi(cmd.buf + 3); tv.tv_usec = 0; settimeofday(&tv, NULL); } else if (!strncmp("lcd", cmd.buf, 3)) { printf("LCD ID = %hx\r\n", LCD_RD_Reg(0x00)); } 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)) { 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)); 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)) { uint8_t status; char *flstattbl[] = { "BUSY", "WEL", "BP0", "BP1", "BP2", "BP3", "AAI", "BPL" }; 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++) if (status & 1 << i) { fputs(flstattbl[i], stdout); fputs(" ", stdout); } printf("(0x%02x)\r\n", status); FlashWriteStatus(0x00); status = FlashReadStatus(); fputs("Status = ", stdout); for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++) if (status & 1 << i) { fputs(flstattbl[i], stdout); 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 { printf("Unknown command\r\n"); } } 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 */ extern uint32_t _isr_vectorsram_offs; #else extern uint32_t _isr_vectorsflash_offs; #endif void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000+_isr_vectorsram_offs */ NVIC_SetVectorTable(NVIC_VectTab_RAM, (uint32_t)&_isr_vectorsram_offs); #else /* Set the Vector Table base location at 0x08000000+_isr_vectorsflash_offs */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, (uint32_t)&_isr_vectorsflash_offs); #endif /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }