switched from system board with external RTC to blue pill

This commit is contained in:
King Kévin 2016-04-03 17:36:04 +02:00
parent 352b3664f9
commit 28f2c1fbe5
2 changed files with 6 additions and 82 deletions

View File

@ -26,7 +26,7 @@ BINARY = firmware
# which development board is used
# supported are: SYSTEM_BOARD, MAPLE_MINI, BLUE_PILL
BOARD = SYSTEM_BOARD
BOARD = BLUE_PILL
# source files
CSRC = $(wildcard *.c)

86
main.c
View File

@ -45,7 +45,6 @@
#include "usart.h" // USART utilities
#include "usb_cdcacm.h" // USB CDC ACM utilities
#include "led_ws2812b.h" // WS2812b LEDs utilities
#include "rtc_ds1307.h" // Real Time Clock DS1307 utilities
/** @defgroup main_flags flag set in interrupts to be processed in main task
* @{
@ -70,22 +69,6 @@ const uint32_t ticks_hour = 60*60*TICKS_PER_SECOND;
const uint32_t ticks_midday = 12*60*60*TICKS_PER_SECOND;
/** @} */
/** @defgroup square_wave_timer timer peripheral used to count timer based on RTC IC square wave output
* @{
*/
#define SQUARE_WAVE_FREQUENCY 4096 /**< square wave output frequency from the RTC IC */
#define SQUARE_WAVE_TIMER TIM2 /**< timer peripheral */
#define SQUARE_WAVE_TIMER_RCC RCC_TIM2 /**< timer peripheral clock */
#define SQUARE_WAVE_TIMER_IC TIM_IC1 /**< input capture channel (for TIM2_CH1) */
#define SQUARE_WAVE_TIMER_IN TIM_IC_IN_TI1 /**< input capture input source (TIM2_CH1 becomes TI1, then TI1F, then TI1FP1) */
#define SQUARE_WAVE_TIMER_TS TIM_SMCR_TS_IT1FP1 /**< input capture trigger (actually TI1FP1) */
#define SQUARE_WAVE_TIMER_IRQ NVIC_TIM2_IRQ /**< timer interrupt */
#define SQUARE_WAVE_TIMER_ISR tim2_isr /**< timer interrupt service routine */
#define SQUARE_WAVE_GPIO_RCC RCC_GPIOA /**< timer port peripheral clock (TIM2_CH1 on PA0)*/
#define SQUARE_WAVE_GPIO_PORT GPIOA /**< timer port (TIM2_CH1 on PA0) */
#define SQUARE_WAVE_GPIO_PIN GPIO_TIM2_CH1_ETR /**< timer pin input, connect to RTC IC square wave output (TIM2_CH1 on PA0) */
/** @} */
/** @defgroup battery_adc ADC used to measure battery voltage
* @{
*/
@ -325,32 +308,15 @@ static void process_command(char* str)
/* parse command */
if (0==strcmp(word,"help")) {
printf("available commands:\n");
printf("date [YYYY-MM-DD]\n");
printf("time [HH:MM:SS]\n");
} else if (0==strcmp(word,"date")) {
word = strtok(NULL,delimiter);
if (!word) {
printf("current date: %04d-%02d-%02d\n", rtc_read_year(), rtc_read_month(), rtc_read_date());
} else if (strlen(word)!=10 || word[0]!='2' || word[1]!='0' || word[2]<'0' || word[2]>'9' || word[3]<'0' || word[3]>'9' || word[5]<'0' || word[5]>'1' || word[6]<'0' || word[6]>'9' || word[8]<'0' || word[8]>'3' || word[9]<'0' || word[9]>'9') {
goto error;
} else {
if (!rtc_write_year((word[0]-'0')*1000+(word[1]-'0')*100+(word[2]-'0')*10+(word[3]-'0')*1)) {
printf("setting year failed\n");
} else if (!rtc_write_month((word[5]-'0')*10+(word[6]-'0')*1)) {
printf("setting month failed\n");
} else if (!rtc_write_date((word[8]-'0')*10+(word[9]-'0')*1)) {
printf("setting day failed\n");
} else {
printf("date set\n");
}
}
} else if (0==strcmp(word,"time")) {
word = strtok(NULL,delimiter);
if (!word) {
printf("current time: %02d:%02d:%02d\n", rtc_read_hours(), rtc_read_minutes(), rtc_read_seconds());
//printf("current time: %02d:%02d:%02d\n", rtc_read_hours(), rtc_read_minutes(), rtc_read_seconds());
} else if (strlen(word)!=8 || word[0]<'0' || word[0]>'2' || word[1]<'0' || word[1]>'9' || word[3]<'0' || word[3]>'5' || word[4]<'0' || word[4]>'9' || word[6]<'0' || word[6]>'5' || word[7]<'0' || word[7]>'9') {
goto error;
} else {
/*
if (!rtc_write_hours((word[0]-'0')*10+(word[1]-'0')*1)) {
printf("setting hours failed\n");
} else if (!rtc_write_minutes((word[3]-'0')*10+(word[4]-'0')*1)) {
@ -362,6 +328,7 @@ static void process_command(char* str)
rtc_oscillator_enable(); // be sure the oscillation is enabled
printf("time set\n");
}
*/
}
} else {
goto error;
@ -412,29 +379,6 @@ int main(void)
clock_leds_set(); // set the colors of all LEDs
ws2812b_transmit(); // transmit set color
// setup RTC module
rtc_setup(); // setup RTC module
rtc_write_square_wave(SQUARE_WAVE_FREQUENCY); // set square wave output frequency
// setup timer to generate tick from square wave output */
rcc_periph_clock_enable(SQUARE_WAVE_GPIO_RCC); // enable clock for GPIO peripheral
gpio_set_mode(SQUARE_WAVE_GPIO_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, SQUARE_WAVE_GPIO_PIN); // set pin as input
gpio_set(SQUARE_WAVE_GPIO_PORT, SQUARE_WAVE_GPIO_PIN); // enable pull-up
rcc_periph_clock_enable(SQUARE_WAVE_TIMER_RCC); // enable clock for timer peripheral
timer_reset(SQUARE_WAVE_TIMER); // reset timer state
timer_ic_set_input(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_IC, SQUARE_WAVE_TIMER_IN); // configure channel as input capture
timer_ic_set_filter(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_IC, TIM_IC_OFF); // use no input capture filter
timer_ic_set_polarity(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_IC, TIM_IC_FALLING); //capture on falling edge
timer_slave_set_trigger(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_TS); // select trigger
timer_slave_set_mode(SQUARE_WAVE_TIMER, TIM_SMCR_SMS_ECM1); // select external clock more 1 as input
timer_ic_enable(SQUARE_WAVE_TIMER, SQUARE_WAVE_TIMER_IC); // enable input capture
timer_set_mode(SQUARE_WAVE_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
timer_set_prescaler(SQUARE_WAVE_TIMER, 0); // no need to prescale
timer_set_period(SQUARE_WAVE_TIMER, SQUARE_WAVE_FREQUENCY/TICKS_PER_SECOND-1); // set the tick period
timer_enable_irq(SQUARE_WAVE_TIMER, TIM_DIER_UIE); // enable interrupt for timer
nvic_enable_irq(SQUARE_WAVE_TIMER_IRQ); // allow interrupt for timer
timer_enable_counter(SQUARE_WAVE_TIMER); // enable timer to count ticks
// setup ADC to ready battery voltage (single conversion of a regular channel without interrupt)
rcc_periph_clock_enable(BATTERY_PORT_RCC); // enable clock for GPIO peripheral
gpio_set_mode(BATTERY_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, BATTERY_PIN); // set GPIO as analogue input for the ADC
@ -455,11 +399,6 @@ int main(void)
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
led_on(); // switch on LED to indicate setup completed
// verify is RTC is running
if (rtc_oscillator_disabled()) {
printf("/!\\ RTC oscillator is disabled\n");
}
// read internal reference 1.2V voltage
uint8_t channels[] = {ADC_CHANNEL17}; // voltages to convert
adc_set_regular_sequence(BATTERY_ADC, LENGTH(channels), channels); // set channels to convert
@ -479,9 +418,7 @@ int main(void)
printf("battery voltage: %.2fV\n", battery_voltage);
// get date and time
uint16_t* rtc_time = rtc_read_time(); // get RTC time/date
current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // the current time
printf("current date: %04d-%02d-%02d %02d:%02d:%02d\n", rtc_time[6], rtc_time[5], rtc_time[4], rtc_time[2], rtc_time[1], rtc_time[0]);
current_time = 6*ticks_hour+15*ticks_minute; // the current time
clock_animate_time(current_time); // set time with animation
printf("input commands\n");
@ -531,8 +468,7 @@ int main(void)
//printf("%02lu:%02lu:%02lu\n", current_time/ticks_hour, (current_time%ticks_hour)/ticks_minute, (current_time%ticks_minute)/ticks_second); // display time
}
if ((current_time%ticks_minute)==0) { // one minute passed
rtc_time = rtc_read_time(); // get RTC time/date
current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // sync current time
//current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // sync current time
printf("it is now %02lu:%02lu:%02lu\n", current_time/ticks_hour, (current_time%ticks_hour)/ticks_minute, (current_time%ticks_minute)/ticks_second); // display time
}
if ((current_time%ticks_hour)==0) { // one hours passed
@ -560,15 +496,3 @@ void BUTTON_ISR(void)
button_flag = true; // perform button action
}
#endif
#if defined(SQUARE_WAVE_TIMER_IRQ) && defined(SQUARE_WAVE_TIMER_ISR)
/** @brief timer interrupt when square wave output accumulated to a tick */
void SQUARE_WAVE_TIMER_ISR(void)
{
if (timer_get_flag(SQUARE_WAVE_TIMER, TIM_SR_UIF)) { // overflow even happened
timer_clear_flag(SQUARE_WAVE_TIMER, TIM_SR_UIF); // clear flag
current_time++; // increment time
time_flag = true; // update flag
}
}
#endif