From ac209399e90f65cc04adacaba68c9ea09c10472d Mon Sep 17 00:00:00 2001 From: opetany Date: Fri, 19 Jan 2018 15:52:01 +0100 Subject: [PATCH] Initial commit --- .gitignore | 62 +++++++++++++++++++++++++ create-this-app | 114 ++++++++++++++++++++++++++++++++++++++++++++++ inc/delay.h | 16 +++++++ inc/pio_driver.h | 37 +++++++++++++++ inc/uart.h | 17 +++++++ src/delay.c | 32 +++++++++++++ src/main.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++ src/pio_driver.c | 46 +++++++++++++++++++ src/uart.c | 35 ++++++++++++++ 9 files changed, 475 insertions(+) create mode 100644 .gitignore create mode 100644 create-this-app create mode 100644 inc/delay.h create mode 100644 inc/pio_driver.h create mode 100644 inc/uart.h create mode 100644 src/delay.c create mode 100644 src/main.c create mode 100644 src/pio_driver.c create mode 100644 src/uart.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c24eb7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +*.bak + +*.rpt + +*.done + +*.summary + +*.jdi + +*.pin + +*.qsf + +*.sof + +*.html + +*.sopcinfo + +*.bsf + +*.txt + +*.qpf + +.qsys_edit + +ubcmcld +system +incremental_db + +db/ + +obj/ + +.force_relink + +.project + +.settings/ + +*.properties + +.sopc_builder/ + +*.objdump + +Makefile +.cproject +!src/ +*.qws +Pathfinder_BSP/ +obj/ +*.objdump +*.elf +!*.c +!*.h +!*.java +*.db +.DS_Store +*.map \ No newline at end of file diff --git a/create-this-app b/create-this-app new file mode 100644 index 0000000..bf9207f --- /dev/null +++ b/create-this-app @@ -0,0 +1,114 @@ +#!/bin/bash +# +# This script creates the hello_world_small application in this directory. + + +BSP_DIR=../KCC_bsp +QUARTUS_PROJECT_DIR=../../ +NIOS2_APP_GEN_ARGS="--elf-name KCC.elf --set APP_CFLAGS_OPTIMIZATION -Os --set OBJDUMP_INCLUDE_SOURCE 1 --src-files hello_world_small.c" + + +# First, check to see if $SOPC_KIT_NIOS2 environmental variable is set. +# This variable is required for the command line tools to execute correctly. +if [ -z "${SOPC_KIT_NIOS2}" ] +then + echo Required \$SOPC_KIT_NIOS2 Environmental Variable is not set! + exit 1 +fi + + +# Also make sure that the APP has not been created already. Check for +# existence of Makefile in the app directory +if [ -f ./Makefile ] +then + echo Application has already been created! Delete Makefile if you want to create a new application makefile + exit 1 +fi + + +# We are selecting hal_reduced_footprint bsp because it supports this application. +# Check to see if the hal_reduced_footprint has already been generated by checking for +# existence of the public.mk file. If not, we need to run +# create-this-bsp file to generate the bsp. +if [ ! -f ${BSP_DIR}/public.mk ]; then + # Since BSP doesn't exist, create the BSP + # Pass any command line arguments passed to this script to the BSP. + pushd ${BSP_DIR} >> /dev/null + ./create-this-bsp "$@" || { + echo "create-this-bsp failed" + exit 1 + } + popd >> /dev/null +fi + + +# Don't run make if create-this-app script is called with --no-make arg +SKIP_MAKE= +while [ $# -gt 0 ] +do + case "$1" in + --no-make) + SKIP_MAKE=1 + ;; + esac + shift +done + + +# Now we also need to go copy the sources for this application to the +# local directory. +find "${SOPC_KIT_NIOS2}/examples/software/hello_world_small/" -name '*.c' -or -name '*.h' -or -name 'hostfs*' | xargs -i cp -L {} ./ || { + echo "failed during copying example source files" + exit 1 +} + +find "${SOPC_KIT_NIOS2}/examples/software/hello_world_small/" -name 'readme.txt' -or -name 'Readme.txt' | xargs -i cp -L {} ./ || { + echo "failed copying readme file" +} + +if [ -d "${SOPC_KIT_NIOS2}/examples/software/hello_world_small/system" ] +then + cp -RL "${SOPC_KIT_NIOS2}/examples/software/hello_world_small/system" . || { + echo "failed during copying project support files" + exit 1 + } +fi + +chmod -R +w . || { + echo "failed during changing file permissions" + exit 1 +} + +cmd="nios2-app-generate-makefile --bsp-dir ${BSP_DIR} --set QUARTUS_PROJECT_DIR=${QUARTUS_PROJECT_DIR} ${NIOS2_APP_GEN_ARGS}" + +echo "create-this-app: Running \"${cmd}\"" +$cmd || { + echo "nios2-app-generate-makefile failed" + exit 1 +} + +if [ -z "$SKIP_MAKE" ]; then + cmd="make" + + echo "create-this-app: Running \"$cmd\"" + $cmd || { + echo "make failed" + exit 1 + } + + echo + echo "To download and run the application:" + echo " 1. Make sure the board is connected to the system." + echo " 2. Run 'nios2-configure-sof ' to configure the FPGA with the hardware design." + echo " 3. If you have a stdio device, run 'nios2-terminal' in a different shell." + echo " 4. Run 'make download-elf' from the application directory." + echo + echo "To debug the application:" + echo " Import the project into Nios II Software Build Tools for Eclipse." + echo " Refer to Nios II Software Build Tools for Eclipse Documentation for more information." + echo + echo -e "" +fi + + +exit 0 diff --git a/inc/delay.h b/inc/delay.h new file mode 100644 index 0000000..64718d5 --- /dev/null +++ b/inc/delay.h @@ -0,0 +1,16 @@ +/* + * delay.h + * + * Created on: 13 sty 2018 + * Author: opetany + */ + +#ifndef DELAY_H_ +#define DELAY_H_ + +#include + +void delayMs(uint32_t ms); + + +#endif /* DELAY_H_ */ diff --git a/inc/pio_driver.h b/inc/pio_driver.h new file mode 100644 index 0000000..479dcf4 --- /dev/null +++ b/inc/pio_driver.h @@ -0,0 +1,37 @@ + + +// ================= PORTS DEFINITIONS ============== +#define LED_PORT LEDS_BASE +#define PIO_PORT PIO_BASE + +// ================== LEDS DEFINITIONS ============== +#define LED_0 0x01 +#define LED_1 0x02 +#define LED_2 0x04 +#define LED_3 0x08 + +// ================= PINS DEFINITIONS =============== +#define PIN_0 0x0001 +#define PIN_1 0x0002 +#define PIN_2 0x0004 +#define PIN_3 0x0008 +#define PIN_4 0x0010 +#define PIN_5 0x0020 +#define PIN_6 0x0040 +#define PIN_7 0x0080 +#define PIN_8 0x0100 +#define PIN_9 0x0200 +#define PIN_10 0x0400 +#define PIN_11 0x0800 +#define PIN_12 0x1000 +#define PIN_13 0x2000 +#define PIN_14 0x4000 +#define PIN_15 0x8000 + +// ======================= Functions definitions ================================ +void PIO_Write(int port, int value); +int PIO_Read(int port); +void PIO_SetBit(int port, int value); +void PIO_ClearBit(int port, int value); +int PIO_ReadBit(int port, int pin); +void PIO_Direction(int port, int value); diff --git a/inc/uart.h b/inc/uart.h new file mode 100644 index 0000000..c8d556a --- /dev/null +++ b/inc/uart.h @@ -0,0 +1,17 @@ +/* + * uart.h + * + * Created on: 13 sty 2018 + * Author: opetany + */ + +#ifndef UART_H_ +#define UART_H_ + +#include + +void sendStrig(char* s); +void sendData(uint8_t* data, uint16_t size); + + +#endif /* UART_H_ */ diff --git a/src/delay.c b/src/delay.c new file mode 100644 index 0000000..a2ef885 --- /dev/null +++ b/src/delay.c @@ -0,0 +1,32 @@ +/* + * delay.c + * + * Created on: 13 sty 2018 + * Author: opetany + */ + +#include "../inc/delay.h" +#include "system.h" +#include "altera_avalon_timer_regs.h" + +void delayMs(uint32_t ms) +{ + uint32_t cnt = 0; + + // activate the time + IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_TIMER_BASE, + ALTERA_AVALON_TIMER_CONTROL_CONT_MSK + | ALTERA_AVALON_TIMER_CONTROL_START_MSK); + + while(cnt < ms) + { + while( !(ALTERA_AVALON_TIMER_STATUS_TO_MSK & IORD_ALTERA_AVALON_TIMER_STATUS(SYS_TIMER_BASE)) ); + IOWR_ALTERA_AVALON_TIMER_STATUS(SYS_TIMER_BASE, 0); + cnt++; + } + + IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_TIMER_BASE, + ALTERA_AVALON_TIMER_CONTROL_CONT_MSK + | ALTERA_AVALON_TIMER_CONTROL_STOP_MSK); +} + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..233ef0f --- /dev/null +++ b/src/main.c @@ -0,0 +1,116 @@ +/* + * "Small Hello World" example. + * + * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on + * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example + * designs. It requires a STDOUT device in your system's hardware. + * + * The purpose of this example is to demonstrate the smallest possible Hello + * World application, using the Nios II HAL library. The memory footprint + * of this hosted application is ~332 bytes by default using the standard + * reference design. For a more fully featured Hello World application + * example, see the example titled "Hello World". + * + * The memory footprint of this example has been reduced by making the + * following changes to the normal "Hello World" example. + * Check in the Nios II Software Developers Manual for a more complete + * description. + * + * In the SW Application project (small_hello_world): + * + * - In the C/C++ Build page + * + * - Set the Optimization Level to -Os + * + * In System Library project (small_hello_world_syslib): + * - In the C/C++ Build page + * + * - Set the Optimization Level to -Os + * + * - Define the preprocessor option ALT_NO_INSTRUCTION_EMULATION + * This removes software exception handling, which means that you cannot + * run code compiled for Nios II cpu with a hardware multiplier on a core + * without a the multiply unit. Check the Nios II Software Developers + * Manual for more details. + * + * - In the System Library page: + * - Set Periodic system timer and Timestamp timer to none + * This prevents the automatic inclusion of the timer driver. + * + * - Set Max file descriptors to 4 + * This reduces the size of the file handle pool. + * + * - Check Main function does not exit + * - Uncheck Clean exit (flush buffers) + * This removes the unneeded call to exit when main returns, since it + * won't. + * + * - Check Don't use C++ + * This builds without the C++ support code. + * + * - Check Small C library + * This uses a reduced functionality C library, which lacks + * support for buffering, file IO, floating point and getch(), etc. + * Check the Nios II Software Developers Manual for a complete list. + * + * - Check Reduced device drivers + * This uses reduced functionality drivers if they're available. For the + * standard design this means you get polled UART and JTAG UART drivers, + * no support for the LCD driver and you lose the ability to program + * CFI compliant flash devices. + * + * - Check Access device drivers directly + * This bypasses the device file system to access device drivers directly. + * This eliminates the space required for the device file system services. + * It also provides a HAL version of libc services that access the drivers + * directly, further reducing space. Only a limited number of libc + * functions are available in this configuration. + * + * - Use ALT versions of stdio routines: + * + * Function Description + * =============== ===================================== + * alt_printf Only supports %s, %x, and %c ( < 1 Kbyte) + * alt_putstr Smaller overhead than puts with direct drivers + * Note this function doesn't add a newline. + * alt_putchar Smaller overhead than putchar with direct drivers + * alt_getchar Smaller overhead than getchar with direct drivers + * + */ + +#include "sys/alt_stdio.h" +#include "sys/alt_irq.h" +#include + +#include "../inc/pio_driver.h" +#include "../inc/delay.h" + +//void handle_timer_interrupt(void* p, alt_u32 param) +//{ +// // clear irq status in order to prevent retriggering +// //IOWR_ALTERA_AVALON_TIMER_STATUS(SYS_TIMER_BASE, 0); +// +// // your isr code here +// // .... +//} + + +int main() +{ + alt_putstr("KCC Project!\n"); + + // register the timer irq to be serviced by handle_timer_interrupt() function +// alt_irq_register(SYS_TIMER_IRQ, 0, handle_timer_interrupt); + + while (1) + { + PIO_SetBit(LED_PORT, LED_0); + delayMs(400); + PIO_ClearBit(LED_PORT, LED_0); + delayMs(400); + + sendStrig("SIEMANO\n\r"); + } + + return 0; +} diff --git a/src/pio_driver.c b/src/pio_driver.c new file mode 100644 index 0000000..798a4f3 --- /dev/null +++ b/src/pio_driver.c @@ -0,0 +1,46 @@ +/* + * pio_driver.c + * + * Created on: 14 maj 2016 + * Author: opetany + */ + +#include "../inc/pio_driver.h" +#include "altera_avalon_pio_regs.h" +#include "system.h" + +// ================================================================================== +void PIO_Write(int port, int value) +{ + IOWR_ALTERA_AVALON_PIO_DATA(port, value); +} + +// ================================================================================== +int PIO_Read(int port) +{ + return IORD_ALTERA_AVALON_PIO_DATA(port); +} + +// ================================================================================== +int PIO_ReadBit(int port, int pin) +{ + return IORD_ALTERA_AVALON_PIO_DATA(port) & pin; +} + +// ================================================================================== +void PIO_SetBit(int port, int value) +{ + IOWR_ALTERA_AVALON_PIO_SET_BITS(port, value); +} + +// ================================================================================== +void PIO_ClearBit(int port, int value) +{ + IOWR_ALTERA_AVALON_PIO_CLEAR_BITS(port, value); +} + +// ================================================================================== +void PIO_Direction(int port, int value) +{ + IOWR_ALTERA_AVALON_PIO_DIRECTION(port, value); +} diff --git a/src/uart.c b/src/uart.c new file mode 100644 index 0000000..ff9444c --- /dev/null +++ b/src/uart.c @@ -0,0 +1,35 @@ +/* + * uart.c + * + * Created on: 13 sty 2018 + * Author: opetany + */ + +#include "../inc/uart.h" + +#include "system.h" +#include "altera_up_avalon_rs232_regs.h" + +static void sendByte(char byte) +{ + IOWR_ALT_UP_RS232_DATA(UART_0_BASE, byte); +} + +void sendStrig(char* s) +{ + while(*s) + { + while( ALT_UP_RS232_CONTROL_WI_MSK & IORD_ALT_UP_RS232_CONTROL(UART_0_BASE) ); + sendByte(*s++); + } +} + +void sendData(uint8_t* data, uint16_t size) +{ + uint16_t i; + + for(i = 0; i < size; i++) + { + sendByte((char)*data++); + } +}