Mercurial > ~darius > hgwebdir.cgi > stm32temp
diff main.c @ 3:74e9b3baac1e
Jumbo commit to make things work.
Note I have t
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sun, 01 Jan 2012 11:01:13 +1030 |
parents | 274e01fa5a4c |
children | 2c87e30c982d |
line wrap: on
line diff
--- a/main.c Sat Oct 08 20:35:34 2011 +1030 +++ b/main.c Sun Jan 01 11:01:13 2012 +1030 @@ -1,25 +1,43 @@ #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 volatile struct { - char buf[40]; - uint8_t state; - uint8_t len; +typedef struct { + char buf[40]; + volatile uint8_t state; + uint8_t len; } consbuf_t; void Setup_HW(void); void NVIC_Configuration(void); -/* Called every millisecond */ + +/* 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; @@ -81,6 +99,10 @@ int main(void) { + char buf[40]; + struct tm nowtm; + time_t now; + cmd.state = cmd.len = 0; /* Setup USART etc */ @@ -89,27 +111,54 @@ /* NVIC configuration */ NVIC_Configuration(); - /* Setup SysTick Timer for 1 millisecond interrupts, also enables Systick and Systick-Interrupt */ - if (SysTick_Config(SystemCoreClock / 1000)) { + /* 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); +#define Bank1_LCD_D ((uint32_t)0x60020000) //disp Data ADDR +#define Bank1_LCD_C ((uint32_t)0x60000000) //disp Reg ADDR + while (1) { fputs("> ", stdout); while (cmd.state != 255) ; - if (cmd.len > 0) - printf("Got command '%s'\r\n", cmd.buf); + 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)) { + *(__IO uint16_t *) (Bank1_LCD_C) = 0x00; + printf("LCD ID = %hx\r\n", *(__IO uint16_t *) (Bank1_LCD_D)); + } else if (!strncmp("read", cmd.buf, 4)) { + printf("PB5 = %d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)); + } else if (!strncmp("zz", cmd.buf, 2)) { + NVIC_SystemReset(); + } else { + printf("Unknown command\r\n"); + } + } cmd.state = 0; } } @@ -120,15 +169,54 @@ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; - /* Enable USART1, GPIOA, GPIOD and AFIO clocks */ - RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOD - | RCC_APB2Periph_AFIO, ENABLE); - /* Enable USART2 clock */ - RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + /* 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 LSE */ + 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); - /* DMA1 clock enable */ - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); + /* 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 */ + /* Enable clocks we need */ + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | + RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, 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; @@ -140,21 +228,32 @@ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); - /* Init UART1 - 115200 8n1, no flow control TX & RX enabled */ + /* Configure PB5 as output push-pull */ + 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 */ + 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; - - /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); /* Enable interrupts on receive data */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); - /* Enable the USART1 */ + /* Enable USART */ USART_Cmd(USART1, ENABLE); }