Search |
 |
|
 |
| Author |
Message |
|
|
Posted: Apr 25, 2012 - 11:26 PM |
|

Joined: Sep 22, 2010
Posts: 41
|
|
Hi Guys,
I am just getting my hands wet with an AT32UC3, and I was trying to toggle an LED on and off. Here is my code thus far:
Code:
#include <avr32/io.h>
#include <stdint.h>
#define PM_BASE_ADDR 0xFFFF0400
#define PM_MCCTRL_OFF 0x00000000
#define PM_UNLOCK_OFF 0x00000058
#define GPIO_BASE_ADDR 0xFFFF2000
#define GPIO_PORT_SIZE 0x200
#define GPIO_ENABLE_OFF 0x00000000
#define GPIO_ODER_OFF 0x00000040
#define GPIO_OVR_OFF 0x00000050
#define GPIO_PORT_NUM 0
#define GPIO_PIN_NUM 4
int main(void)
{
uint32_t *mcctrl = (uint32_t *) (PM_BASE_ADDR + PM_MCCTRL_OFF);
uint32_t *unlock = (uint32_t *) (PM_BASE_ADDR + PM_UNLOCK_OFF);
uint32_t *gpio_enable = (uint32_t *) (GPIO_BASE_ADDR + (GPIO_PORT_NUM * GPIO_PORT_SIZE) + (GPIO_ENABLE_OFF));
uint32_t *gpio_oder = (uint32_t *) (GPIO_BASE_ADDR + (GPIO_PORT_NUM * GPIO_PORT_SIZE) + (GPIO_ODER_OFF));
uint32_t *gpio_ovr = (uint32_t *) (GPIO_BASE_ADDR + (GPIO_PORT_NUM * GPIO_PORT_SIZE) + (GPIO_OVR_OFF));
/* Write the address of mcctrl, and 0xAA to unlock register
* then select the 8MHz internal oscillator as clock source */
*unlock = (0xAA << 24) | (PM_MCCTRL_OFF);
*mcctrl = 5;
/* Configure PA04 to be an output and set it high */
*gpio_enable |= (1 << GPIO_PIN_NUM);
*gpio_oder |= (1 << GPIO_PIN_NUM);
*gpio_ovr &= ~(1 << GPIO_PIN_NUM);
while (1);
}
I am having two problems with this code, first off all, I get this weird compile time error.
Code:
Error 2 insn does not satisfy its constraints:
Error 3 in reload_cse_simplify_operands, at postreload.c:396
I tried removing the anding, and the oring from the code and replacing it by a set and the compile error vanished like so:
Code:
*gpio_enable = (1 << GPIO_PIN_NUM);
*gpio_oder = (1 << GPIO_PIN_NUM);
*gpio_ovr = ~(1 << GPIO_PIN_NUM);
This worked fine, but this sets the whole 32 bit register, which might effect other pins, So I want to know what is the problem with the original code.
Second, as you can see in my code I tried changing the clock source to be the 8MHz internal oscillator rather than the default 115KHz one. But the code always hangs after the mcctrl register set. I tried to fiddle with optimization options but that did not work.
Any help greatly appreciated
Abunada |
|
|
| |
|
|
|
|
|
Posted: Apr 26, 2012 - 12:28 AM |
|

Joined: Mar 28, 2010
Posts: 129
Location: Palmerston North, New Zealand
|
|
Hi Abunada,
My suggestion would be to start with one of the Example applications, as these have most of the configuration already sorted, and modify one from there (this is what I did to get started).
What hardware are you trying to run the application on, a development board from Atmel or your own?
Regards. |
|
|
| |
|
|
|
|
|
Posted: Apr 26, 2012 - 05:47 AM |
|

Joined: Sep 22, 2010
Posts: 41
|
|
Gee thanks daffniles, you're helping me out quite a lot, I hope I am not boring you with my stupid questions.
I am using a prototyping board for a TQFP 64 package, soldered a AT32UC3C2512C chip on it, plugged it into the breadboard, connected a 3v3 power supply it just like in the "Supply and Startup Considerations" chapter of the datasheet, and plugged in my JTAGICE 3 to the JTAG ports. I can read the Device ID and everything seems to be OK. I could even turn on and off the LED but the problems above I could not get around:
- Changing the clock setting (i.e. how to use the unlock register)
- Getting rid of the weird compile error I get everytime I try to dereference a pointer and at the same time perform a logic operation to it.
The problem with the examples is that they use ASF, although I have nothing against it; ASF is just too complicated for my tastes. I'd rather read the datasheet and know exactly whats going than try to debug the problem with code that somebody else has written for me. Plus I don't know what the prerequisites are for using ASF as far as the hardware is concerned. |
|
|
| |
|
|
|
|
|
Posted: Apr 26, 2012 - 10:48 AM |
|

Joined: Aug 25, 2011
Posts: 395
Location: Europe
|
|
While I agree that the ASF can be a bit cumbersome, it’s quite convenient at other times, especially for parts of your code where performance is not an issue.
For example, you’re trying to use the internal 8 MHz oscillator but haven’t started it yet (hence the hang). The ASF has a function for that: scif_start_rc8M(). Takes care of unlocking the register as well. Similarly pm_set_mclk_source(...) lets you change the clock source without having to do any unlocking yourself.
If you want to do things without the ASF, don’t make it overly complicated. If you want to set the value of a pin, use the port macros and structs:
Code:
AVR32_GPIO.port[SOME_PIN >> 5].ovrs = 1 << (SOME_PIN & 0x1F);
I suggest having a look into the corresponding ASF functions to figure out how stuff works. Reading only the datasheet can be quite confusing and also it looks like you’re not reading it carefully enough. Otherwise you would’ve known that you need to enable the 8 MHz oscillator before using it, as mentioned above.
Abunada wrote:
This worked fine, but this sets the whole 32 bit register, which might effect other pins, [...]
This is not an issue, but it looks like you didn’t read the datasheet well enough. If you had used the ASF or at least named the pointers correctly, you would’ve noticed that there are “set” and “clear” registers, like “ovrs” and “ovrc”. These set or clear the bits you write into them, but leave all other bits (pins) alone.
Regarding the error you’re getting: Which gcc version are you using? |
|
|
| |
|
|
|
|
|
Posted: Apr 26, 2012 - 08:22 PM |
|

Joined: Sep 22, 2010
Posts: 41
|
|
Thanks a lot catweax I can't believe that I missed the enabling part, that's completely my bad. I did not know that there was a section other than PM that had to do with the oscillator, so I only read that.
Quote:
For example, you’re trying to use the internal 8 MHz oscillator but haven’t started it yet (hence the hang). The ASF has a function for that: scif_start_rc8M(). Takes care of unlocking the register as well. Similarly pm_set_mclk_source(...) lets you change the clock source without having to do any unlocking yourself.
The problem is that there isn't enough documentation on the use of ASF (not the actual API) so its hard to get started. For instance, I don't know what is the hardware/software requirements I need to use it. Maybe if I had been programming on an Atmel board It would have been another story. Whereas the datasheet is concise and detailed so you're bound to find all the information you need, (Although you would need to forgive my laziness this time ).
Quote:
If you want to do things without the ASF, don’t make it overly complicated. If you want to set the value of a pin, use the port macros and structs:
Code:
AVR32_GPIO.port[SOME_PIN >> 5].ovrs = 1 << (SOME_PIN & 0x1F);
I am just starting so I did not go through the header <avr32/io.h> yet, which I guess is where these definitions are defined.
Quote:
This is not an issue, but it looks like you didn’t read the datasheet well enough. If you had used the ASF or at least named the pointers correctly, you would’ve noticed that there are “set” and “clear” registers, like “ovrs” and “ovrc”. These set or clear the bits you write into them, but leave all other bits (pins) alone.
That does not mean that you can't edit the registers ovr and oder directly, if you look in the datasheet you will notice that they are read/write. I know about the set and clear but I wanted to know why the error was popping out.
Quote:
Regarding the error you’re getting: Which gcc version are you using?
Thats really odd, I distinctly remember editing the post right after it was posted, and adding the IDE and compiler I am using. Anyway, I declared my variables as volatile, which is again something trivial I should have done, and everything worked like a charm. For reference, my GCC version is 3.3.1.27, and I am using AVR Studio 5.1 |
|
|
| |
|
|
|
|
|
Posted: Apr 27, 2012 - 09:57 AM |
|

Joined: Aug 25, 2011
Posts: 395
Location: Europe
|
|
|
Abunada wrote:
The problem is that there isn't enough documentation on the use of ASF (not the actual API) so its hard to get started.
Try some of the example projects that both AVR32 Studio and AVR Studio 5 come with. Examples exist for almost all modules, showing you the most important things.
Abunada wrote:
For instance, I don't know what is the hardware/software requirements I need to use it.
I’m not sure what you mean. The ASF can be used with all AVR32 MCUs.
Abunada wrote:
I am just starting so I did not go through the header <avr32/io.h> yet, which I guess is where these definitions are defined.
The address, bit mask and whatever macros are defined in various header files. I really suggest you have a look at the example projects and see how it’s done there.
Abunada wrote:
That does not mean that you can't edit the registers ovr and oder directly, if you look in the datasheet you will notice that they are read/write. I know about the set and clear but I wanted to know why the error was popping out.
Oh sorry. It sounded like you didn’t know about those yet.
Abunada wrote:
For reference, my GCC version is 3.3.1.27, and I am using AVR Studio 5.1
Odd. Did that GCC version come with AVR Studio 5.1? My older AVR32 Studio already came with GCC 4.3.3. |
|
|
| |
|
|
|
|
|
Posted: Apr 27, 2012 - 01:06 PM |
|

Joined: Sep 22, 2010
Posts: 41
|
|
WOW!
I must say I am a complete idiot for not using ASF, I was just intimidated by the abstraction. BUT I decided to try it out. Boy oh boy, was I in for a pleasant surprise, I cant believe I got USART communication with a PC, I2C to my sensor array, SPI for communication to another secondary microcontroller and PWM for my motor control all working in the same day!!!
Quote:
Odd. Did that GCC version come with AVR Studio 5.1? My older AVR32 Studio already came with GCC 4.3.3.
Now that you mention it ... The version number I mentioned was the name of the folder found in "C:\Program Files (x86)\Atmel\AVR Studio 5.1\extensions\Atmel\AVRGCC" and I immediately presumed its the version of my compiler. I failed to notice its in the 3's. I ran the command line -v option and I got a 4.5.1 sorry for the confusion.
Anyway thanks for the help guys, greatly appreciated! |
|
|
| |
|
|
|
|
|
|
|
|