/* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /* Copyright (c) 2016 King Kévin */ /* standard libraries */ #include // standard integer types #include // standard I/O facilities #include // standard utilities #include // standard streams #include // error number utilities /* STM32 (including CM3) libraries */ #include // real-time control clock library #include // general purpose input output library #include // vector table definition #include // Cortex M3 utilities #include #include /* own libraries */ #include "main.h" // board definitions #include "usart.h" // USART utilities #include "usb_cdcacm.h" // USB CDC ACM utilities #include "vfd.h" // VFD driver /* get the length of an array */ #define LENGTH(x) (sizeof(x) / sizeof((x)[0])) /* default output (i.e. for printf) */ int _write(int file, char *ptr, int len) { int i; if (file == STDOUT_FILENO || file == STDERR_FILENO) { for (i = 0; i < len; i++) { if (ptr[i] == '\n') { // add carrier return before line feed. this is recommended for most UART terminals usart_putchar_nonblocking('\r'); // a second line feed doesn't break the display cdcacm_putchar('\r'); // a second line feed doesn't break the display } usart_putchar_nonblocking(ptr[i]); // send byte over USART cdcacm_putchar(ptr[i]); // send byte over USB } return i; } errno = EIO; return -1; } static void gpio_setup(void) { rcc_periph_clock_enable(LED_RCC); //enable clock for LED gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull' } static void timer_setup(void) { #define VFD_TIMER TIM2 #define VFD_TIMER_RCC RCC_TIM2 #define VFD_TIMER_IRQ NVIC_TIM2_IRQ rcc_periph_clock_enable(VFD_TIMER_RCC); // enable clock for timer block timer_reset(VFD_TIMER); // reset timer state timer_set_mode(VFD_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(VFD_TIMER, (SYSTEM_CLOCK_FREQ/(1<<16))-1); // set the prescaler so this 16 bits timer overflows at 1Hz //timer_set_period(0xffff/(VFD_DIGITS+VFD_MATRIX)/150); // set the refresh frequency timer_set_period(VFD_TIMER, 0xffff); timer_enable_irq(VFD_TIMER, TIM_DIER_CC1IE); // enable interrupt for timer nvic_enable_irq(VFD_TIMER_IRQ); // allow interrupt for timer timer_enable_counter(VFD_TIMER); // enable timer } void tim2_isr(void) { if (timer_get_flag(VFD_TIMER, TIM_SR_UIF)) { timer_clear_flag(VFD_TIMER, TIM_SR_UIF); gpio_toggle(LED_PORT, LED_PIN); } } int main(void) { SCB_VTOR = (uint32_t) 0x08002000; // relocate vector table because of the bootloader rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock gpio_setup(); // setup main inputs/outputs usart_setup(); // setup USART (for printing) cdcacm_setup(); // setup USB CDC ACM (for printing) vfd_setup(); // setup VFD timer_setup(); setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print printf("welcome to the STM32F1 CuVoodoo display driver\n"); vfd_on(); // switch on VFD vfd_test(); vfd_shift(); for (uint32_t i = 0; i < 0x800000; i++) { __asm__("nop"); } vfd_clear(); vfd_shift(); vfd_digit(0,'0'); vfd_shift(); bool vfd_flag = false; for (uint8_t i=0; i