2016-01-17 14:54:54 +01:00
|
|
|
/* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* Copyright (c) 2016 King Kévin <kingkevin@cuvoodoo.info> */
|
|
|
|
|
|
|
|
/* standard libraries */
|
|
|
|
#include <stdint.h> // standard integer types
|
|
|
|
#include <stdio.h> // standard I/O facilities
|
|
|
|
#include <stdlib.h> // standard utilities
|
|
|
|
#include <unistd.h> // standard streams
|
|
|
|
#include <errno.h> // error number utilities
|
|
|
|
|
|
|
|
/* STM32 (including CM3) libraries */
|
|
|
|
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
|
|
|
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
|
|
|
#include <libopencm3/cm3/scb.h> // vector table definition
|
2016-01-18 16:23:35 +01:00
|
|
|
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
2016-01-17 14:54:54 +01:00
|
|
|
|
|
|
|
/* own libraries */
|
|
|
|
#include "main.h" // board definitions
|
|
|
|
#include "usart.h" // USART utilities
|
2016-01-18 16:23:35 +01:00
|
|
|
#include "usb_cdcacm.h" // USB CDC ACM utilities
|
2016-01-17 14:54:54 +01:00
|
|
|
|
|
|
|
/* 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
|
2016-01-18 16:23:35 +01:00
|
|
|
cdcacm_putchar('\r'); // a second line feed doesn't break the display
|
2016-01-17 14:54:54 +01:00
|
|
|
}
|
|
|
|
usart_putchar_nonblocking(ptr[i]); // send byte over USART
|
2016-01-18 16:23:35 +01:00
|
|
|
cdcacm_putchar(ptr[i]); // send byte over USB
|
2016-01-17 14:54:54 +01:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_setup(void)
|
|
|
|
{
|
|
|
|
rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock
|
|
|
|
rcc_periph_clock_enable(LED_RCC); //enable clock for LED
|
2016-01-18 16:27:51 +01:00
|
|
|
rcc_periph_clock_enable(VFD_RCC); //enable clock for VFD
|
2016-01-17 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gpio_setup(void)
|
|
|
|
{
|
2016-01-18 16:27:51 +01:00
|
|
|
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull'
|
|
|
|
gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, VFD_STR); // set VFD pin to 'output push-pull'
|
|
|
|
gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, VFD_NLE); // set VFD pin to 'output push-pull'
|
|
|
|
gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, VFD_CLK); // set VFD pin to 'output push-pull'
|
|
|
|
gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, VFD_DIN); // set VFD pin to 'output push-pull'
|
|
|
|
|
2016-01-17 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
2016-01-17 15:03:10 +01:00
|
|
|
{
|
2016-01-17 14:54:54 +01:00
|
|
|
SCB_VTOR = (uint32_t) 0x08002000; // relocate vector table because of the bootloader
|
|
|
|
|
2016-01-17 15:03:10 +01:00
|
|
|
clock_setup(); // setup main clock
|
|
|
|
gpio_setup(); // setup main inputs/ouputs
|
|
|
|
usart_setup(); // setup USART (for printing)
|
2016-01-18 16:27:51 +01:00
|
|
|
cdcacm_setup(); // setup USART (for printing)
|
2016-01-18 16:23:35 +01:00
|
|
|
|
2016-01-17 14:54:54 +01:00
|
|
|
setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print
|
|
|
|
setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print
|
|
|
|
|
2016-01-18 16:27:51 +01:00
|
|
|
printf("welcome to the STM32F1 CuVoodoo display driver\n");
|
|
|
|
|
|
|
|
printf("sending VFD data\n");
|
|
|
|
gpio_set(VFD_PORT, VFD_STR); // disable HV output
|
|
|
|
gpio_clear(VFD_PORT, VFD_NLE); // do not latch data
|
|
|
|
gpio_set(VFD_PORT, VFD_CLK); // clock is idle high
|
2016-01-18 16:23:35 +01:00
|
|
|
|
2016-01-18 16:27:51 +01:00
|
|
|
bool vfd_transmit = false;
|
|
|
|
uint32_t vfd_data[3] = {~0,~0,~0};
|
|
|
|
uint8_t bit_flip = 0;
|
2016-01-17 14:54:54 +01:00
|
|
|
/* blink the LED with every transmitted character */
|
|
|
|
while (1) {
|
2016-01-17 15:03:10 +01:00
|
|
|
while (usart_received) { // echo every received character
|
2016-01-18 16:23:35 +01:00
|
|
|
gpio_toggle(LED_PORT, LED_PIN); // toggle LED
|
2016-01-17 15:03:10 +01:00
|
|
|
printf("%c",usart_getchar()); // transmit receive character
|
2016-01-18 16:27:51 +01:00
|
|
|
vfd_transmit = true;
|
2016-01-17 15:03:10 +01:00
|
|
|
}
|
2016-01-18 16:23:35 +01:00
|
|
|
while (cdcacm_received) { // echo every received character
|
|
|
|
gpio_toggle(LED_PORT, LED_PIN); // toggle LED
|
|
|
|
printf("%c",cdcacm_getchar()); // transmit receive character
|
2016-01-18 16:27:51 +01:00
|
|
|
vfd_transmit = true;
|
|
|
|
}
|
|
|
|
while (vfd_transmit) {
|
|
|
|
vfd_transmit = false;
|
|
|
|
/* send data to VFD (data valid on clock high) to 3x32 lines */
|
|
|
|
gpio_set(VFD_PORT, VFD_STR); // disable HV output
|
|
|
|
gpio_clear(VFD_PORT, VFD_NLE); // do not latch data
|
|
|
|
gpio_set(VFD_PORT, VFD_CLK); // clock is idle high
|
|
|
|
for (uint8_t i=0; i<3; i++) {
|
|
|
|
printf("%08lx ", vfd_data[i]);
|
|
|
|
for (uint8_t b=0; b<32; b++) {
|
|
|
|
gpio_clear(VFD_PORT, VFD_CLK);
|
|
|
|
if (vfd_data[i]&(1<<b)) {
|
|
|
|
gpio_set(VFD_PORT, VFD_DIN);
|
|
|
|
} else {
|
|
|
|
gpio_clear(VFD_PORT, VFD_DIN);
|
|
|
|
}
|
|
|
|
gpio_set(VFD_PORT, VFD_CLK);
|
|
|
|
}
|
|
|
|
vfd_data[i] = ~0;
|
|
|
|
}
|
|
|
|
gpio_set(VFD_PORT, VFD_NLE); // latch data
|
|
|
|
gpio_clear(VFD_PORT, VFD_STR); // enable HV output
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
/* flip bit */
|
|
|
|
vfd_data[bit_flip/32] = ~(1<<(bit_flip%32));
|
|
|
|
bit_flip = (bit_flip+1)%(3*32);
|
2016-01-18 16:23:35 +01:00
|
|
|
}
|
|
|
|
__WFI(); // go to sleep
|
2016-01-17 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|