ATtiny404 - Setting lockbit in source - Compile Error

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

This is the first time I have attempted to define the lock bits from withing the source code but I am confused as to why the following is not working.

This syntax works fine for defining the fuses.  Please point out my mistake.

Thanks.

Jim

 

#define avr/io.h

//works fine
unsigned char __lock __attribute__((section (".lock"))) =  LB_NOLOCK_gc; 

//error
LOCKBIT = {
	.LOCKBIT = (LB_NOLOCK_gc)
};

//Reported errors:
//expected identifier or '(' before 'LOCKBIT_t'
//expected ')' before numeric constant

 

This topic has a solution.
Last Edited: Sun. Jul 14, 2019 - 12:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you mean to use the following rather than "#define avr/io.h"?

#include <avr/io.h>

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You also need < avr/lock.h >

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your error is because you are using LOCKBIT, which is defined as-

//iotn404.h
#define LOCKBIT (*(LOCKBIT_t *) 0x128A) /* Lockbit */

 

 

either use the lock.h LOCKBITS define (notice the 'S')-

LOCKBITS = LB_NOLOCK_gc;

 

 

or if you want to simulate the FUSES style-

LOCKMEM LOCKBIT_t __lock = { .LOCKBIT = LB_NOLOCK_gc }; //C 

LOCKMEM LOCKBIT_t __lock = { LB_NOLOCK_gc }; //C++

 

or just bypass it all and do as you did 'manually' (and add 'used' to the attribute).

 

If you have macro expansion capability in your ide, use it. You would have seen the expansion of LOCKBIT, and would have given a good clue.

Something else to keep in mind, there is nothing mysterious about a lot of these things and it just takes a bit of digging and following the trail.

 

I was doing 'my own thing' in c++ for the tiny416/mega4809, and followed the same trail of defines to figure out what the compiler actually wanted for the fuses and lockbit. I also wanted to be able to use the known fuse settings at compile time so created a constexpr (which takes no space) and is used both to populate the fuse struct and give the ability to use a fuse setting in other code at compile time (I wanted to know what the clock fuse was set to, so my clock setting code did not have to lookup the fuse at runtime which adds a bit of code since in addition to the lookup, there are several paths for the code to follow)-

https://github.com/cv007/ATTiny416XplainedNano/blob/master/Fuse.hpp

https://github.com/cv007/ATTiny416XplainedNano/blob/master/Fuse.cpp

Now, I can do-

Clkctrl::clksel( 10000000 );

and that function will know what the fuse is set to, and a number of 'if/else if' gets reduced to a small bit of code to set the clock to the freq I want (at or below, to the closest).

(I also store the actual clock frequency in the Clkctrl class, so peripherals can get the actual freq without me having to keep track of what the clock freq is- which moves some things to runtime, but is worth it to me)

Last Edited: Sat. Jul 13, 2019 - 06:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, of course.  I had it correct in my code but hen I created the snippet to paste into my post is where I made the typo error.

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[

curtvm wrote:

either use the lock.h LOCKBITS define (notice the 'S')-

LOCKBITS = LB_NOLOCK_gc;

 

 

or if you want to simulate the FUSES style-

LOCKMEM LOCKBIT_t __lock = { .LOCKBIT = LB_NOLOCK_gc }; //C 

 

Hi -

Yes the LOCKBITS = LB_NOLOCK_gc;  works fine.

 

However, LOCKMEM LOCKBIT_t __lock = { .LOCKBIT = LB_NOLOCK_gc };  still throws a syntax error: 'expected identifier before '(' token'

 

Thanks for the response

 

 

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>However, LOCKMEM LOCKBIT_t __lock = { .LOCKBIT = LB_NOLOCK_gc };  still throws a syntax error: 'expected identifier before '(' token'

 

I didn't try the C version.

The preprocessor is 'going to work' on the LOCKBIT designated initializer, so you end up with-

__attribute__((__used__, __section__ (".lock")))

  LOCKBIT_t __lock = { .(*(LOCKBIT_t *) 0x128A) = LB_NOLOCK_gc };

 

Find the macro expansion feature on your ide, then things like this are easy to diagnose.

 

Probably a bad choice having struct member names as all caps, as now you get to clash with define names. I'm not sure why they use all caps when naming struct members.

 

I don't use defines, so have eliminated these type of problems :)

 

 

Last Edited: Sun. Jul 14, 2019 - 02:48 AM