Programming ATMEGA32A-PU using an Arduino R3 - arduino

I am just starting out trying to program ATMEL's ATMEGA32A-PU microcontroller using my arduino uno R3 as the ISP programmer. I was following the tutorial found here: http://hackaday.com/2010/10/23/avr-programming-introduction/
This tutorial describes programming an ATMEGA168 chip. I looked up the datasheet for the ATMEGA32 and adjusted the wiring, I believe, correctly and I'm using avrdude to program the chip.
I am running Windows 10 x64. I downloaded WinAVR-20100110.
From the hackaday instructions I loaded my arduino with the ArduinoISP sketch from FILE->examples in the arduino IDE. I made no changes.
The pins on the atmega32 chip are connected thus:
6 (MOSI) to arduino 11
7 (MISO) to arduino 12
8 (SCK) to arduino 13
9 (RST) to arduino 10
10 (VCC) to 5v
11 (GND) to GND
30 (AVCC) to 5v
31 (GND) to GND
I am using the arduino's 5v and GND pins to supply power and ground to the AVR.
From a cmd prompt I am running: avrdude -v -P COM3 -b 19200 -c avrisp -p atmega32 -U flash:w:main.hex
avrdude: Version 5.10, compiled on Jan 19 2010 at 10:45:23
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2009 Joerg Wunsch
System wide configuration file is "C:\WinAVR-20100110\bin\avrdude.conf"
Using Port : COM3
Using Programmer : avrisp
Overriding Baud Rate : 19200
AVR Part : ATMEGA32
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 4 10 64 0 no 1024 4 0 9000 9000 0xff 0xff
flash 33 6 64 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
lock 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
calibration 0 0 0 0 no 4 0 0 0 0 0x00 0x00
Programmer Type : STK500
Description : Atmel AVR ISP
Hardware Version: 2
Firmware Version: 1.18
Topcard : Unknown
Vtarget : 0.0 V
Varef : 0.0 V
Oscillator : Off
SCK period : 0.1 us
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.06s
avrdude: Device signature = 0x1e9502
avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as 99
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "main.hex"
avrdude: input file main.hex auto detected as Intel Hex
avrdude: writing flash (144 bytes):
Writing | ################################################## | 100% 0.22s
avrdude: 144 bytes of flash written
avrdude: verifying flash memory against main.hex:
avrdude: load data flash data from input file main.hex:
avrdude: input file main.hex auto detected as Intel Hex
avrdude: input file main.hex contains 144 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.12s
avrdude: verifying ...
avrdude: 144 bytes of flash verified
avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as 99
avrdude: safemode: Fuses OK
avrdude done. Thank you.
It goes through the programming and says it succesfully wrote 144 bytes. I've also tried using stk500 since the output of the command above mentions it but I get a timeout error when I do that. For testing purposes all I'm trying to do is get 8 LED's from PORTD to light up. I'm including avr/io.h and
int main(void)
{
DDRD |= 0xff; //Set PortD Pins as an output
PORTD |= 0xff; //Set PortD Pins high to turn on LEDs
while(1) { } //Loop forever
}
Unfortunately, PD1 is the only led to light up. None of the rest do anything.
I have the microcontroller connected to a breadboard with pins 14-21 each connected to its own 470 ohm resistor. The resistors each connects to its own LED which then connects to ground.

So the problem was in the makefile. I didn't realize the AVR was listed in the makefile that I downloaded from the instructions from the hackaday tutorial. Once I changed the MCU, AVRDUDE_PROGRAMMER, and PORT in the makefile it worked like a charm.
# WinAVR Sample makefile written by Eric B. Weddington, J�rg Wunsch, et al.
# Modified (bringing often-changed options to the top) by Elliot Williams
# make all = Make software and program
# make clean = Clean out built project files.
# make coff = Convert ELF to AVR COFF (for use with AVR Studio 3.x or VMLAB).
# make extcoff = Convert ELF to AVR Extended COFF (for use with AVR Studio
# 4.07 or greater).
# make program = Download the hex file to the device, using avrdude. Please
# customize the avrdude settings below first!
# make filename.s = Just compile filename.c into the assembler code only
# To rebuild project do "make clean" then "make all".
# Microcontroller Type
# MCU = attiny13
# MCU = attiny2313
# MCU = atmega8
# MCU = atmega168
MCU = atmega32 # CHANGED THIS TO THE MCU THAT I'M TRYING TO PROGRAM
# Target file name (without extension).
TARGET = main
# Programming hardware: type avrdude -c ?
# to get a full listing.
# AVRDUDE_PROGRAMMER = dapa # official name of
# AVRDUDE_PROGRAMMER = dragon_isp
AVRDUDE_PROGRAMMER = avrisp # CHANGED THIS TO THE ISP I'M USING TO PROGRAM
AVRDUDE_PORT = COM3 # CHANGED THIS TO THE PORT ARDUINO IS CONNECTED TO
#AVRDUDE_PORT = usb # linux for dragon
#AVRDUDE_PORT = /dev/parport0 # linux
#AVRDUDE_PORT = lpt1 # windows
############# Don't need to change below here for most purposes (Elliot)
# Optimization level, can be [0, 1, 2, 3, s]. 0 turns off optimization.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c
# If there is more than one source file, append them above, or modify and
# uncomment the following:
#SRC += foo.c bar.c
# You can also wrap lines by appending a backslash to the end of the line:
#SRC += baz.c \
#xyzzy.c
# List Assembler source files here.
# Make them always end in a capital .S. Files ending in a lowercase .s
# will not be considered source files but generated files (assembler
# output from the compiler), and will be deleted upon "make clean"!
# Even though the DOS/Win* filesystem matches both .s and .S the same,
# it will preserve the spelling of the filenames, and gcc itself does
# care about how the name is spelled on its command-line.
ASRC =
# List any extra directories to look for include files here.
# Each directory must be seperated by a space.
EXTRAINCDIRS =
# Optional compiler flags.
# -g: generate debugging information (for GDB, or for COFF conversion)
# -O*: optimization level
# -f...: tuning, see gcc manual and avr-libc documentation
# -Wall...: warning level
# -Wa,...: tell GCC to pass this to the assembler.
# -ahlms: create assembler listing
CFLAGS = -g -O$(OPT) \
-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
-Wall -Wstrict-prototypes \
-Wa,-adhlns=$(<:.c=.lst) \
$(patsubst %,-I%,$(EXTRAINCDIRS))
# Set a "language standard" compiler flag.
# Unremark just one line below to set the language standard to use.
# gnu99 = C99 + GNU extensions. See GCC manual for more information.
#CFLAGS += -std=c89
#CFLAGS += -std=gnu89
#CFLAGS += -std=c99
CFLAGS += -std=gnu99
# Optional assembler flags.
# -Wa,...: tell GCC to pass this to the assembler.
# -ahlms: create listing
# -gstabs: have the assembler create line number information; note that
# for use in COFF files, additional information about filenames
# and function names needs to be present in the assembler source
# files -- see avr-libc docs [FIXME: not yet described there]
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
# Optional linker flags.
# -Wl,...: tell GCC to pass this to linker.
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
# Additional libraries
# Minimalistic printf version
#LDFLAGS += -Wl,-u,vfprintf -lprintf_min
# Floating point printf version (requires -lm below)
#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt
# -lm = math library
LDFLAGS += -lm
# Programming support using avrdude. Settings and variables.
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
# Uncomment the following if you want avrdude's erase cycle counter.
# Note that this counter needs to be initialized first using -Yn,
# see avrdude manual.
#AVRDUDE_ERASE += -y
# Uncomment the following if you do /not/ wish a verification to be
# performed after programming the device.
#AVRDUDE_FLAGS += -V
# Increase verbosity level. Please use this when submitting bug
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
# to submit bug reports.
#AVRDUDE_FLAGS += -v -v
#Run while cable attached or don't
#AVRDUDE_FLAGS += -E reset #keep chip disabled while cable attached
#AVRDUDE_FLAGS += -E noreset
#AVRDUDE_WRITE_FLASH = -U lfuse:w:0x04:m #run with 8 Mhz clock
#AVRDUDE_WRITE_FLASH = -U lfuse:w:0x21:m #run with 1 Mhz clock #default clock mode
#AVRDUDE_WRITE_FLASH = -U lfuse:w:0x01:m #run with 1 Mhz clock no start up time
# ---------------------------------------------------------------------------
# Define directories, if needed.
DIRAVR = c:/winavr
DIRAVRBIN = $(DIRAVR)/bin
DIRAVRUTILS = $(DIRAVR)/utils/bin
DIRINC = .
DIRLIB = $(DIRAVR)/avr/lib
# Define programs and commands.
SHELL = sh
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
# Programming support using avrdude.
AVRDUDE = avrdude
REMOVE = rm -f
COPY = cp
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
ELFSIZE = $(SIZE) -AC --mcu=$(MCU) $(TARGET).elf
# Define Messages
# English
MSG_ERRORS_NONE = Errors: none
MSG_BEGIN = -------- begin --------
MSG_END = -------- end --------
MSG_SIZE_BEFORE = Size before:
MSG_SIZE_AFTER = Size after:
MSG_COFF = Converting to AVR COFF:
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling:
MSG_ASSEMBLING = Assembling:
MSG_CLEANING = Cleaning project:
# Define all object files.
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
# Define all listing files.
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
# Default target: make program!
all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).eep \
$(TARGET).lss $(TARGET).sym sizeafter finished end
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
# Eye candy.
# AVR Studio 3.x does not check make's exit code but relies on
# the following magic strings to be generated by the compile job.
begin:
#echo
#echo $(MSG_BEGIN)
finished:
#echo $(MSG_ERRORS_NONE)
end:
#echo $(MSG_END)
#echo
# Display size of file.
sizebefore:
#if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
sizeafter:
#if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
# Display compiler version information.
gccversion :
#$(CC) --version
# Convert ELF to COFF for use in debugging / simulating in
# AVR Studio or VMLAB.
COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .data-0x800000 \
--change-section-address .bss-0x800000 \
--change-section-address .noinit-0x800000 \
--change-section-address .eeprom-0x810000
coff: $(TARGET).elf
#echo
#echo $(MSG_COFF) $(TARGET).cof
$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
extcoff: $(TARGET).elf
#echo
#echo $(MSG_EXTENDED_COFF) $(TARGET).cof
$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
# Program the device.
program: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
#echo
#echo $(MSG_FLASH) $#
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $#
%.eep: %.elf
#echo
#echo $(MSG_EEPROM) $#
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O $(FORMAT) $< $#
# Create extended listing file from ELF output file.
%.lss: %.elf
#echo
#echo $(MSG_EXTENDED_LISTING) $#
$(OBJDUMP) -h -S $< > $#
# Create a symbol table from ELF output file.
%.sym: %.elf
#echo
#echo $(MSG_SYMBOL_TABLE) $#
avr-nm -n $< > $#
# Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
#echo
#echo $(MSG_LINKING) $#
$(CC) $(ALL_CFLAGS) $(OBJ) --output $# $(LDFLAGS)
# Compile: create object files from C source files.
%.o : %.c
#echo
#echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS) $< -o $#
# Compile: create assembler files from C source files.
%.s : %.c
$(CC) -S $(ALL_CFLAGS) $< -o $#
# Assemble: create object files from assembler source files.
%.o : %.S
#echo
#echo $(MSG_ASSEMBLING) $<
$(CC) -c $(ALL_ASFLAGS) $< -o $#
# Target: clean project.
clean: begin clean_list finished end
clean_list :
#echo
#echo $(MSG_CLEANING)
$(REMOVE) $(TARGET).hex
$(REMOVE) $(TARGET).eep
$(REMOVE) $(TARGET).obj
$(REMOVE) $(TARGET).cof
$(REMOVE) $(TARGET).elf
$(REMOVE) $(TARGET).map
$(REMOVE) $(TARGET).obj
$(REMOVE) $(TARGET).a90
$(REMOVE) $(TARGET).sym
$(REMOVE) $(TARGET).lnk
$(REMOVE) $(TARGET).lss
$(REMOVE) $(OBJ)
$(REMOVE) $(LST)
$(REMOVE) $(SRC:.c=.s)
$(REMOVE) $(SRC:.c=.d)
$(REMOVE) *~
# Automatically generate C source code dependencies.
# (Code originally taken from the GNU make user manual and modified
# (See README.txt Credits).)
#
# Note that this will work with sh (bash) and sed that is shipped with WinAVR
# (see the SHELL variable defined above).
# This may not work with other shells or other seds.
#
%.d: %.c
set -e; $(CC) -MM $(ALL_CFLAGS) $< \
| sed 's,\(.*\)\.o[ :]*,\1.o \1.d : ,g' > $#; \
[ -s $# ] || rm -f $#
# Remove the '-' if you want to see the dependency files generated.
-include $(SRC:.c=.d)
# Listing of phony targets.
.PHONY : all begin finish end sizebefore sizeafter gccversion coff extcoff \
clean clean_list program

Related

How to read the fuses with avrdude

I have ea new stk500v2 programmer (Pololu) and want to read the fuses of an atmega8.
With my old programmer I uses the command
avrdude -v -p atmega8 -P /dev/cu.usbmodem002938642 -c stk500v2
and got the lfuse and hfuse on 2 lines
Now I only get
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9307 (probably m8)
Using
avrdude -v -p atmega8 -P /dev/cu.usbmodem002938642 -c stk500v2 -U hfuse:r:-:h
I get
avrdude: reading hfuse memory:
Reading | ################################################## | 100% 0.00s
avrdude: writing output file "<stdout>"
0xd9
Same with lfuse.
Is there a way to get hfuse and lfuse with the same command?
I believe AVRDUDE allows multiple -U options, so try putting this at the end of your command:
-U hfuse:r:-:h -U lfuse:r:-:h

In Makefile why my phony target is execute between two different generic rule

My problem is as follows:
I have two targets in my Makefile, toad4 and toad5.
Depending on which target is built some different files and compiler flags need to be set.
This works for the actual build and goes like this:
SDCC = /Volumes/Partition2/Users/nyholku/sdcc340/bin/sdcc
SDCCFLAGS = "-Wl -f 0xffff" -DTOAD_HW_VERSION=${HWVERSION} --verbose --no-crt --ivt-loc=0x800 -V -L /Volumes/Partition2/Users/nyholku/sdcc340/non-free/lib/pic16 -Wa,-S,0 -Wl,-m,-s18f45k50.lkr -mpic16 -p18f45k50 --disable-warning 85 --std-sdcc99 --obanksel=3 --use-non-free
.PHONY: toad4
toad4: $(OBJDIR)/$(TARGET).hex
toad4: HI_SPEED_IRQ_ASM :=hi_speed_irq-hw4.asm
toad4: HWVERSION := HW4
toad4: BOOTLOADER := ../diolan-plus2-toad4/fw/bootloader.hex
.PHONY: toad5
toad5: HI_SPEED_IRQ_ASM :=hi_speed_irq.asm
toad5: HWVERSION := HW5
toad5: BOOTLOADER := ../diolan-plus2-toad5/fw/bootloader.hex
toad5: $(OBJDIR)/$(TARGET).hex
$(OBJDIR)/%.o: %.c $$(#D)/.f
#echo $(PATH)
$(SDCC) -c $(SDCCFLAGS) $< -o $#
But later in the Make file I have rule to produce the C code dependencies:
# First include the dependencies
include $(addprefix $(OBJDIR)/, $(SRCS:.c=.dep))
# Then recreate them
$(OBJDIR)/%.dep: %.c $$(#D)/.f
set -e; rm -f $#; \
$(SDCC) -c -M $(SDCCFLAGS) $< > $#.$$$$; \
sed -e '1 s,^,$(OBJDIR)/,' -e 's,\($*\)\.o[ :]*,\1.o $# : ,g' < $#.$$$$ > $#; \
rm -f $#.$$$$
This was adapted from the GNU Makefile manual and worked before I introduced the dual target scheme.
When I comment that dependency generation out, the make works, the flag HWVERSION gets set for the C compilation (through the SDCCFLAGS), just as they it should, depending on the target.
But if I leave that dependency generation in then apparently that rule is applied before the toad4 and toad5 rules because the HWVERSION is empty and the C-compilation to produce the dependencies fails.
So why is the other generic C compile rule applied before the toad4/toad5 rules and the dependency generating rules is applied before?
I'm also ok with suggestion on how to organise this better for the goal of building to different binaries from mostly same files with different settings. This should be quite common need.
I'm using GNU Make 3.81 on macOS 11.3.1 and the compiler is Small Device C Compiler ie SDCC
For completeness the complete Make file is here
#-------------------------------------------------------------------------------
#
# Copyright (c) 2011, Kustaa Nyholm / SpareTimeLabs
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this list
# of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its
# contributors may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
#
# Everyone should have this
.SUFFIXES:
# The output file name
TARGET = toad4
# This is necessary so that pk2cmd (used by 'load' script) is found and for the
# pk2cmd to find its support files (it seems to search current dir which is wrong)
PATH := ${PATH}
UPLOADER := java -cp ~/EazyCNC-Project/classes:/Volumes/Partition2/Users/nyholku/EazyCNC-Project/lib/purejavahidapi-0.0.12.jar:/Volumes/Partition2/Users/nyholku/EazyCNC-Project/lib/jna-5.6.0.jar diolanupdater.UpdateFirmware
ENCODER := /Volumes/Partition2/Users/nyholku/diolan-plus2/encoder/build/encoder
HEXMATE := /Applications/microchip/xc8/v2.32/pic/bin/hexmate
# The source files that make up the project go here
SRCS = main.c toad4.c usb_hid.c usb_core.c usb_pic_defs.c usb_user_config.c command_queue.c crt0iz_toad4.c swuart.c
# The libraries that are used go here
LIBS = libc18f.lib libm18f.lib libsdcc.lib
# Where to find the compiler
SDCC = /Volumes/Partition2/Users/nyholku/sdcc340/bin/sdcc
# Compiler flags go here
# --use-crt=crt0.o TOAD_HW_VERSION
SDCCFLAGS = "-Wl -f 0xffff" -DTOAD_HW_VERSION=${HWVERSION} --verbose --no-crt --ivt-loc=0x800 -V -L /Volumes/Partition2/Users/nyholku/sdcc340/non-free/lib/pic16 -Wa,-S,0 -Wl,-m,-s18f45k50.lkr -mpic16 -p18f45k50 --disable-warning 85 --std-sdcc99 --obanksel=3 --use-non-free
# Where to store the target/intermediate/temporary/object files
OBJDIR = ../obj
#-------------------------------------------------------------------------------
#
.PHONY: toad4
toad4: $(OBJDIR)/$(TARGET).hex
toad4: HI_SPEED_IRQ_ASM :=hi_speed_irq-hw4.asm
toad4: HWVERSION := HW4
toad4: BOOTLOADER := ../diolan-plus2-toad4/fw/bootloader.hex
.PHONY: toad5
toad5: HI_SPEED_IRQ_ASM :=hi_speed_irq.asm
toad5: HWVERSION := HW5
toad5: BOOTLOADER := ../diolan-plus2-toad5/fw/bootloader.hex
toad5: $(OBJDIR)/$(TARGET).hex
#
#-------------------------------------------------------------------------------
.PHONY: load
load:
$(UPLOADER) $(OBJDIR)/$(TARGET)-encoded.hex
#-------------------------------------------------------------------------------
.PHONY: comparebootloaders
comparebootloaders:
$(HEXMATE) -o$../bootloader-backedp.hex -fill=0xFF#0x0000:0x07FF r0000-07FF,/Volumes/Partition2/Users/nyholku/diolan-plus2/fw-backup-25.7.2017/bootloader.hex r000802-FFFFFF,/Volumes/Partition2/Users/nyholku/diolan-plus2/fw-backup-25.7.2017/bootloader.hex
$(HEXMATE) -o$../bootloader-toad4.hex -fill=0xFF#0x0000:0x07FF r0000-07FF,../diolan-plus2-toad4/fw/bootloader.hex r000802-FFFFFF,../diolan-plus2-toad4/fw/bootloader.hex
$(HEXMATE) -o$../bootloader-toad5.hex -fill=0xFF#0x0000:0x07FF r0000-07FF,../diolan-plus2-toad5/fw/bootloader.hex r000802-FFFFFF,../diolan-plus2-toad5/fw/bootloader.hex
#
#
#-------------------------------------------------------------------------------
#
# This ensures that the object directory exists and re-creates it if necessary
#
# This requires make 3.81 or later, delete this section and all expressions that
# refer to .f if you have an older make
#
.SECONDEXPANSION:
# Uses a .f file as a flag file in each directory
%/.f:
mkdir -p $(dir $#)
touch $#
# dont' let make remove the flag files automatically
.PRECIOUS: %/.f
#
#-------------------------------------------------------------------------------
#
# Actual rules
#
# Compile the C-files
$(OBJDIR)/%.o: %.c $$(#D)/.f
#echo $(PATH)
$(SDCC) -c $(SDCCFLAGS) $< -o $#
KEY=${TOAD4PLUS_DIALON_KEY2}
# Link the compiled files and libraries
$(OBJDIR)/$(TARGET).hex: $(addprefix $(OBJDIR)/, $(SRCS:.c=.o)) $(OBJDIR)/hi_speed_irq.o
$(SDCC) $(SDCCFLAGS) -o $(OBJDIR)/$(TARGET).hex $(addprefix $(OBJDIR)/, $(SRCS:.c=.o)) $(LIBS) $(OBJDIR)/hi_speed_irq.o
# normalize the code filling un-used code memory with 0xFF so that encoding always works on known data
$(HEXMATE) -o$(OBJDIR)/$(TARGET)-normalized.hex -fill=0xFF#0x0800:0x7FFF r0800-7FFF,$(OBJDIR)/$(TARGET).hex
# sanitise the bootloader by keeping only the first 2kB (there is an extra jump code at 0x800 which overlaps with firmware code)
$(HEXMATE) -o$(OBJDIR)/bootloader-normalized.hex -fill=0xFF#0x0000:0x07FF r0000-07FF,$(BOOTLOADER) r000802-FFFFFF,$(BOOTLOADER)
# combine the bootloader and firmware to one hex file that can be programmed with pickit ready to run
$(HEXMATE) -o$(OBJDIR)/$(TARGET)-pickit.hex -fill=0xFFFF#0xF00001:0xF000FF -fill=0xA5#0xF00000:0xF00000 -fill=0x00#0x300000:0x30000D $(OBJDIR)/$(TARGET)-normalized.hex ../obj/bootloader-normalized.hex
# encode the bootloader for bootloading purposes, suppress output so as NOT to reveal the secret key
$(ENCODER) -ix $(OBJDIR)/$(TARGET)-normalized.hex -ox $(OBJDIR)/$(TARGET)-encoded.hex -e ${TOAD4PLUS_DIALON_KEY2}
# upload the encoded hex file using the bootload process
$(UPLOADER) $(OBJDIR)/$(TARGET)-encoded.hex
# Compile the high speed interrupt asm file
$(OBJDIR)/hi_speed_irq.o: ${HI_SPEED_IRQ_ASM}
gpasm -D TOAD_HW_VERSION=${HWVERSION} -o $(OBJDIR)/hi_speed_irq.o -c ${HI_SPEED_IRQ_ASM}
#
#-------------------------------------------------------------------------------
#
# Automatic generation of dependencies
#
# This magic code fragment from GNU make manual uses the SDCC compiler -M option
# to create a Makefile fragment for each C-source file describing the dependencies.
#
# Traditionally these fragments have the type '.d' but SDCC seems to delete them
# when it compiles files, so I use '.dep' here.
#
# Also SDCC '-M' option produces wrong dependency for the file being compiled
# in the sense that it does not contain the path, only the filename. Hence
# the 'sed' command has been mangled to inject the missing path to the fragment.
#
# First include the dependencies
include $(addprefix $(OBJDIR)/, $(SRCS:.c=.dep))
# Then recreate them
$(OBJDIR)/%.dep: %.c $$(#D)/.f
set -e; rm -f $#; \
$(SDCC) -c -M $(SDCCFLAGS) $< > $#.$$$$; \
sed -e '1 s,^,$(OBJDIR)/,' -e 's,\($*\)\.o[ :]*,\1.o $# : ,g' < $#.$$$$ > $#; \
rm -f $#.$$$$
#------------------------------------------------------------------------------
#
# pretty standard default target
#
all: toad5
#
#-------------------------------------------------------------------------------
#
# pretty standard clean that attempts to delete all that this Makefile may left behind
#
clean:
rm -f $(OBJDIR)/*.rel
rm -f $(OBJDIR)/*.lnk
rm -f $(OBJDIR)/*.S19
rm -f $(OBJDIR)/*.map
rm -f $(OBJDIR)/*.mem
rm -f $(OBJDIR)/*.asm
rm -f $(OBJDIR)/*.rst
rm -f $(OBJDIR)/*.sym
rm -f $(OBJDIR)/*.lst
rm -f $(OBJDIR)/*.o
rm -f $(OBJDIR)/*.dep
rm -f $(OBJDIR)/*.hex
#
# cleanall deletes all in the object directory, do not use this if target dir == source dir
cleanall:
rm $(OBJDIR)/*
#-------------------------------------------------------------------------------
Target-specific variable inheritance flows through the prerequisite dependency graph. That's what you're relying on here: when a target is built as a prerequisite of toad4 it gets one set of options and when it's built as a prerequisite of toad5 it gets a different set of options.
However, when you are building the dependency file the recipe isn't run as a prerequisite of either toad4 or toad5: as described in the manual the dependency file is built by make as part of parsing the makefile, because you included those dependency files. Because of that, none of the target-specific variables are set.
Stepping back it's not clear to me how you imagine this will work anyway. You have two different sets of prerequisites but you are trying to keep them both in the same file: that can't ever work. The only way it would be correct is if you rebuilt all the prerequisites every time you invoked make, so that they would be accurate for whatever target this particular make would want to build, but then of course everything in the makefile would be out of date and so everything would be rebuilt every time.
The right answer for a makefile that wants to build two different sets of targets from the same makefile, is to build two different sets of targets, not the same set of targets with different flags. Specifically, you should have two different object directories and put the generated files from one build in one object directory (say, obj-toad4) and the generated files from the other build in the other object directory (say, obj-toad5). Then they won't get mixed up and make can tell which are out of date and which are not.
Also, the method of handling automatic prerequisites described in the GNU make manual is out of date. You should consider the method described here: http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/

redirect from output file so can be captured

A program foo writes to an output file and prints diagnostic information to stdout.
Thus:
foo -o ./out -i ./input > log
results in the valuable stuff in ./out and some mumbo jumbo in log.
I need to read ./out into another program (an R script I am writing).
The intermediate step of writing to file ./out and reading again is slow, and I need to do this for big ./out files, hundreds of times.
I would like to perform some kind of redirection so that foo writes to a file descriptor rather than a file ./out, which I can read into my script directly. But stdout is already used.
Here is what I tried:
in R:
library(data.table)
fread(cmd = "foo -o /dev/stdout -i ./input > /dev/null")
but I get an error:
File '/data1/tmp/RtmpkKIpBm/fileeb789130da8e3' has size 0. Returning a NULL data.table.
foo -o >(cat) -i ./input > log
Demo:
$ cat foo
#!/bin/bash
if [ "$1" != "-o" ]; then
exit 2
fi
echo "mumbo jumbo stdout" >&1
echo "valueable info" > "$2"
echo "mumbo jumbo stderr" >&2 # threw in stderr for good measure
$ ./foo -o x
mumbo jumbo stdout
mumbo jumbo stderr
$ cat x
valueable info
$ rm x
$ ./foo -o /dev/stdout
mumbo jumbo stdout
valueable info
mumbo jumbo stderr
$ ./foo -o /dev/stdout &>/dev/null
$ ./foo -o >(cat) &>/dev/null
valueable info
Explanation:
Every process has its own stdout. with ./foo -o /dev/stdout &>/dev/null, you're telling foo to output its valuable info into its own stdout, which is /dev/null. But with ./foo -o >(cat) &>/dev/null, you're telling foo to output its valuable info into some pipe, and that pipe goes to cat, whose stdout is not /dev/null, but rather inherited from the shell.
In the demo, the shell's stdout is the terminal, but if it was coming from R's fread(), both the shell's stdout and cat's stdout would go to where fread() can read them.
Try
foo -o /dev/fd/3 -i ./input 3>&1 1>/dev/null
Redirections are processed before commands are run
Redirections are processed left-to-right
3>&1 causes anything written to file descriptor 3 to go to the current stdout
1>/dev/null causes anything written to stdout to go to /dev/null
Writing to /dev/fd/3 then writes to the original stdout
The code should work with any POSIX-compliant shell on any system that supports the /dev/fd directory.
The /dev/fd directory is not part of POSIX, but it is supported on many Unix-like systems, including Linux, FreeBSD, macOS, and Solaris.

Can't program ATtiny2313a with Arduino. Is my chip bricked?

I've been trying to burn a program into ATtiny2313A-PU with my Arduino Uno R3 used as a programmer.
First I tried to program it from Arduino IDE 1.0.1 (Windows 7) and it seemed to uploade the sketch, but the blink program didn't work.
Then I found Michael Holachek's tutorial and followed his instructions:
uploaded ArduinoISP sketch to my Arduino Uno
wired the circuit on a breadboard
installed WinAVR on my Windows 7.
downloaded Michael's template files (Makefile and main.c) and edited Makefile to match my settings (external 8 MHz crystal). I used this online fuse calculator to get correct fuse parameters.
then, in cmd.exe, changed directory to the directory where I saved Makefile and main.c and ran 'make flash'
Makefile
DEVICE = attiny2313a
CLOCK = 8000000
PROGRAMMER = -c arduino -P COM5 -b 19200
OBJECTS = main.o
FUSES = -U lfuse:w:0x5e:m -U hfuse:w:0xdd:m -U efuse:w:0xff:m
######################################################################
######################################################################
# Tune the lines below only if you know what you are doing:
AVRDUDE = avrdude $(PROGRAMMER) -p $(DEVICE)
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
# symbolic targets:
all: main.hex
.c.o:
$(COMPILE) -c $< -o $#
.S.o:
$(COMPILE) -x assembler-with-cpp -c $< -o $#
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.
.c.s:
$(COMPILE) -S $< -o $#
flash: all
$(AVRDUDE) -U flash:w:main.hex:i
fuse:
$(AVRDUDE) $(FUSES)
install: flash fuse
# if you use a bootloader, change the command below appropriately:
load: all
bootloadHID main.hex
clean:
rm -f main.hex main.elf $(OBJECTS)
# file targets:
main.elf: $(OBJECTS)
$(COMPILE) -o main.elf $(OBJECTS)
main.hex: main.elf
rm -f main.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
# If you have an EEPROM section, you must also create a hex file for the
# EEPROM and add it to the "flash" target.
# Targets for code debugging and analysis:
disasm: main.elf
avr-objdump -d main.elf
cpp:
$(COMPILE) -E main.c
main.c
Below is the output I got:
C:\Users>cd /D D:\electronics
D:\electronics>cd nikon/mi
D:\electronics\nikon\mi>make flash
avr-gcc -Wall -Os -DF_CPU=8000000 -mmcu=attiny2313a -c main.c -o main.o
main.c:6: error: stray '\342' in program
main.c:6: error: stray '\200' in program
main.c:6: error: stray '\250' in program
main.c: In function 'main':
main.c:9: error: stray '\342' in program
main.c:9: error: stray '\200' in program
main.c:9: error: stray '\250' in program
make: *** [main.o] Error 1
I suspect that I might brick the fuses on Attiny 2313a. If that is the case, I think I will have to build this AVR rescue shield.
Maybe the Makefile was configured incorrectly?
How can I identify the problem? How can I check whether the chip is still alive?
You are getting compile errors there. I would check the contents of main.c for what the compiler is telling you to check. It looks like in the copy and paste of the code, something was lost. Also
PORTD ^= (1 << PD6); // toggle PD6
can be replaced with
PIND = (1 << PD6); // or _BV(PD6), since you should be using avr-libc anyway.
as per Atmel's datasheets.
well, I googled a little bit more and found that the most common solution, when chips are identical, but have different suffixes (like attiny2313 and attiny2313a) is to use -F key for overriding the signature check.
So, I added the key in Makefile DEVICE = attiny2313 -F and my attiny2313a was programmed successfully!
TRON, thanks for pointing me in the direction of main.c! Unfortunately I can't upvote your answer because my reputation is 11.
I still wonder how those square characters could get there?

Troubles compiling in GPS (Ada IDE) with glib.h

i'm having some troubles when trying to compile Ada code in GPS. GPS says is missing when I include it on a package. I tried installing with apt-get, and it does, but the error is still there. What can I do next? I'm running GPS on a x64 Ubuntu 12.04.
Here's the error message I got:
gprbuild -d -P/media/LUISMUNYOZ/QUINTO/str/pendulum/pendulum_portatil/pendulum.gpr
-XEXTRA=True -XOPENGL=True -XGNOME=True -XBUILD=Production
print_barrier_sync.adb contrib.gpr:1:09: warning: no compiler specified for language "Xml",
ignoring all its sources x86_64-pc-linux-gnu-gcc -c lw.c In file included from
/media/LUISMUNYOZ/QUINTO/str/pendulum/pendulum_portatil/gtkada/testgtk/opengl/lw.c:20:0:
/media/LUISMUNYOZ/QUINTO/str/pendulum/pendulum_portatil/gtkada/testgtk/opengl/lw.h:23:18:
fatal error: glib.h: No such file or directory compilation terminated.
gprbuild:* compilation phase failed
[2012-11-21 13:24:47] process exited with status 4 (elapsed time: 02.06s) [2012-11-21 13:24:56]
Could not locate executable on path: svn SVN error:
[…]
The error triggers at this point:
#ifndef LW_H
#define LW_H
#include <glib.h> <------------------------------------------
#include <GL/gl.h>
#define LW_MAX_POINTS 200
#define LW_MAX_NAME_LEN 500
The file is lw.h, which is defined in the package GtkAda. I downloaded it from GPS page.
I'd pursue #Simon's approach, but a work-around based on 2.4.2. Using the command line might be a temporary alternative while you sort out the underlying problem.
As you are using linux, here's a Makefile for the basic Interaction demo.
# Make shared, static or debug targets.
OS := $(shell uname)
OBJ = obj
TARGET = interaction
GNATMAKE = gnatmake -D $(OBJ)
CARGS = -cargs -O3 -gnatp -gnatwu -gnatf
BARGS = -bargs
LARGS = -largs
.PHONEY: clean cleaner cleanest
all:
#echo ""
#echo "Build targets:"
#echo ""
#echo " shared Use the shared Ada libraries."
#echo " static Link the Ada libraries statically."
#echo " debug Enable debugging."
#echo ""
#echo "Support targets:"
#echo ""
#echo " clean Remove *.ali *.o b~.*"
#echo " cleaner Remove target, too."
#echo " cleanest Remove build directory, too."
#echo ""
shared: $(OBJ)
shared: INCLUDE = $(shell gtkada-config --cflags)
shared: BARGS += -shared
shared: LARGS += $(shell gtkada-config --libs)
shared: LARGS += -dead_strip
shared: *.ad[sb]
#echo "building with shared libraries:"
$(GNATMAKE) $(TARGET) $(INCLUDE) $(CARGS) $(BARGS) $(LARGS)
static: $(OBJ)
static: INCLUDE = $(shell gtkada-config --static --cflags)
static: BARGS += -static
static: LARGS += $(shell gtkada-config --static --libs)
static: LARGS += -dead_strip
static: *.ad[sb]
$(GNATMAKE) $(TARGET) $(INCLUDE) $(CARGS) $(BARGS) $(LARGS)
debug: $(OBJ)
debug: INCLUDE = $(shell gtkada-config --static --cflags)
debug: BARGS += -static
debug: LARGS += $(shell gtkada-config --static --libs)
debug: *.ad[sb]
$(GNATMAKE) -g $(TARGET) $(INCLUDE) $(LARGS)
$(OBJ):
mkdir $(OBJ)
clean:
${RM} $(OBJ)/* b~*
cleaner: clean
${RM} $(TARGET)
cleanest: cleaner
${RM} -r $(OBJ)
For reference, these packages were installed on Ubuntu 12.04:
$ dpkg --get-selections | egrep "gnat|gtkada"
gnat install
gnat-4.6 install
gnat-4.6-base install
gnat-gps install
gnat-gps-common install
gnat-gps-doc install
libgnat-4.6 install
libgnatprj4.6 install
libgnatvsn4.6 install
libgtkada-bin install
libgtkada2.24.1 install
libgtkada2.24.1-dev install
I've never used GtkAda. However ... I googled glib.h and got many hits, suggesting that for plain C builds one should use - for example, from this StackOverflow question -
# Sample Makefile
CFLAGS := $(shell pkg-config --cflags glib-2.0 gtk+-2.0)
LDFLAGS := $(shell pkg-config --libs glib-2.0 gtk+-2.0)
foo: foo.c
$(CC) $(CFLAGS) $< -o $# $(LDFLAGS)
However, we're talking gprbuild here, so perhaps the GtkAda documentation is relevant? It says you need to include with "gtkada"; in your GNAT Project file, and include the location of gtkada.gpr on your ADA_PROJECT_PATH if it isn't there already (see the output of gnatls -v).
If you've already done that, please show us the GPR file.

Resources