Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some examples for the g0 #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions examples/stm32/g0/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
##
## This file is part of the libopencm3 project.
##
## Copyright (C) 2009 Uwe Hermann <[email protected]>
## Copyright (C) 2010 Piotr Esden-Tempski <[email protected]>
##
## This library is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This library 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 Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this library. If not, see <http://www.gnu.org/licenses/>.
##

LIBNAME = opencm3_stm32g0
DEFS += -DSTM32G0

FP_FLAGS ?= -msoft-float
ARCH_FLAGS = -mthumb -mcpu=cortex-m0 $(FP_FLAGS)

################################################################################
# OpenOCD specific variables

OOCD ?= openocd
OOCD_INTERFACE ?= stlink-v2-1
OOCD_TARGET ?= stm32g0x

################################################################################
# Black Magic Probe specific variables
# Set the BMP_PORT to a serial port and then BMP is used for flashing
BMP_PORT ?=

################################################################################
# texane/stlink specific variables
#STLINK_PORT ?= :4242


include ../../../../rules.mk
25 changes: 25 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/miniblink/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##
## This file is part of the libopencm3 project.
##
## Copyright (C) 2009 Uwe Hermann <[email protected]>
##
## This library is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This library 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 Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this library. If not, see <http://www.gnu.org/licenses/>.
##

BINARY = miniblink

LDSCRIPT = ../nucleo-g031k8.ld

include ../../Makefile.include

10 changes: 10 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/miniblink/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# README

This is the smallest-possible example program using libopencm3 and ST
NUCLEO-G031K8 eval board.

It should blink the blue LED on the board.

## Board connections

*none required*
75 changes: 75 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/miniblink/miniblink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2009 Uwe Hermann <[email protected]>
* Copyright (C) 2011 Stephen Caudle <[email protected]>
* Copyright (C) 2012 Karl Palsson <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/i2c.h>

#define PORT_LED GPIOC
#define PIN_LED GPIO6

static void gpio_setup(void)
{
/* Enable GPIOC clock. */
/* Manually: */
//RCC_AHBENR |= RCC_AHBENR_GPIOCEN;
/* Using API functions: */
rcc_periph_clock_enable(RCC_GPIOC);


/* Set GPIO8 (in GPIO port C) to 'output push-pull'. */
/* Using API functions: */
gpio_mode_setup(PORT_LED, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PIN_LED);
}

int main(void)
{
int i;

gpio_setup();

/* Blink the LED (PC8) on the board. */
while (1) {
/* Manually: */
// GPIOC_BSRR = PIN_LED; /* LED off */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
// GPIOC_BRR = PIN_LED; /* LED on */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");

/* Using API functions gpio_set()/gpio_clear(): */
// gpio_set(PORT_LED, PIN_LED); /* LED off */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
// gpio_clear(PORT_LED, PIN_LED); /* LED on */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");

/* Using API function gpio_toggle(): */
gpio_toggle(PORT_LED, PIN_LED); /* LED on/off */
for (i = 0; i < 1000000; i++) { /* Wait a bit. */
__asm__("nop");
}
}

return 0;
}
32 changes: 32 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/nucleo-g031k8.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2009 Uwe Hermann <[email protected]>
* Copyright (C) 2011 Stephen Caudle <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

/* Linker script for ST NUCLEO-G031K8 (STM32G031K8, 64K flash, 8K RAM). */

/* Define memory regions. */
MEMORY
{
rom (rx) : ORIGIN = 0x08000000, LENGTH = 64K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 8K
}

/* Include the common ld script. */
INCLUDE cortex-m-generic.ld

25 changes: 25 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/usart/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##
## This file is part of the libopencm3 project.
##
## Copyright (C) 2009 Uwe Hermann <[email protected]>
##
## This library is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This library 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 Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this library. If not, see <http://www.gnu.org/licenses/>.
##

BINARY = usart

LDSCRIPT = ../nucleo-g031k8.ld

include ../../Makefile.include

12 changes: 12 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/usart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# README

This example program sends repeating sequence of characters "0123456789" on
USART2 serial line of ST NUCLEO-G031K8 eval board.

The sending is done in a blocking way.

## Board connections

| Port | Function | Description |
| ----- | ------------- | --------------------------------- |
| `PA2` | `(USART2_TX)` | TTL serial output `(115200,8,N,1)` |
98 changes: 98 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/usart/usart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2009 Uwe Hermann <[email protected]>
* Copyright (C) 2011 Stephen Caudle <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h>

#include <stdio.h>

#define PORT_LED GPIOC
#define PIN_LED GPIO6

static void clock_setup(void)
{
/* Enable GPIOC clock for LED & USARTs. */
rcc_periph_clock_enable(RCC_GPIOC);
rcc_periph_clock_enable(RCC_GPIOA);

/* Enable clocks for USART. */
rcc_periph_clock_enable(RCC_USART2);
}

static void usart_setup(void)
{
/* Setup USART parameters. */
usart_set_baudrate(USART2, 115200);
usart_set_databits(USART2, 8);
usart_set_parity(USART2, USART_PARITY_NONE);
usart_set_stopbits(USART2, USART_CR2_STOPBITS_1);
usart_set_mode(USART2, USART_MODE_TX);
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);

/* Finally enable the USART. */
usart_enable(USART2);
}

static void gpio_setup(void)
{
/* Setup GPIO pin GPIO8/9 on GPIO port C for LEDs. */
gpio_mode_setup(PORT_LED, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PIN_LED);

/* Setup GPIO pins for USART transmit. */
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);

/* Setup USART TX pin as alternate function. */
gpio_set_af(GPIOA, GPIO_AF1, GPIO2);
}

void uart_out(char* data) {
while(*data) {
usart_send_blocking(USART2, *data++); /* USART2: Send byte. */
}
}

int main(void)
{
int i, j = 0, c = 0;
char buf[256];

clock_setup();
gpio_setup();
usart_setup();
sprintf(buf, "Hello world\r\n");
uart_out(buf);

/* Blink the LED on the board with every transmitted byte. */
while (1) {
gpio_toggle(PORT_LED, PIN_LED); /* LED on/off */
usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */
c = (c == 9) ? 0 : c + 1; /* Increment c. */
if ((j++ % 80) == 0) { /* Newline after line full. */
usart_send_blocking(USART2, '\r');
usart_send_blocking(USART2, '\n');
}
for (i = 0; i < 100000; i++) { /* Wait a bit. */
__asm__("NOP");
}
}

return 0;
}
26 changes: 26 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/usart_stdio/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
##
## This file is part of the libopencm3 project.
##
## Copyright (C) 2009 Uwe Hermann <[email protected]>
##
## This library is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This library 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 Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this library. If not, see <http://www.gnu.org/licenses/>.
##

BINARY = usart_stdio
CSTD = -std=gnu99

LDSCRIPT = ../nucleo-g031k8.ld

include ../../Makefile.include

12 changes: 12 additions & 0 deletions examples/stm32/g0/nucleo-g031k8/usart_stdio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# README

This example program sends a message "Pass: n" with increasing number n
from 0 to 200 on USART2 serial line of ST NUCLEO-G031K8 eval board.

The sending is done using newlib library in a blocking way.

## Board connections

| Port | Function | Description |
| ----- | ------------- | --------------------------------- |
| `PA2` | `(USART2_TX)` | TTL serial output `(115200,8,N,1)` |
Loading