stm32f1/main.c

86 lines
2.7 KiB
C
Raw Normal View History

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
/* own libraries */
#include "main.h" // board definitions
#include "usart.h" // USART utilities
/* 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
}
usart_putchar_nonblocking(ptr[i]); // send byte over USART
}
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
}
static void gpio_setup(void)
{
/* set LED pin to 'output push-pull' */
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN);
}
int main(void)
{
int i, c = 0;
SCB_VTOR = (uint32_t) 0x08002000; // relocate vector table because of the bootloader
clock_setup();
gpio_setup();
usart_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
/* blink the LED with every transmitted character */
while (1) {
gpio_toggle(LED_PORT, LED_PIN); /* LED on/off */
printf("%c",c+'0');
c = (c == 9) ? 0 : c + 1; /* increment c */
for (i = 0; i < 8000000; i++) /* wait a bit */
__asm__("nop");
}
return 0;
}