Newbie Question: Using HUBSET in C
MisterHemi
Posts: 9
in Propeller 2
Hello,
I'm new to the Propeller P2. I'm used to using PIC32MZ's.... I do have a question about configuring the clock speed.
How would I implement this in C:
HUBSET ##%0_000000_0000000000_1111_00_00 // set 20 MHz+ (RCFAST) mode
HUBSET ##%1_100111_0100101000_1111_10_00 // enable crystal+PLL, stay in RCFAST mode
WAITX ##20_000_000/100 // wait ~10ms for crystal+PLL to stabilize
HUBSET ##%1_000000_0000010000_1111_01_10 // set the clock to 320 MHz
Also.... where would I insert the code for this in the beginning of the main.c file, in the beginning of the main.c function or where?
Would I have to create an assembly file first and if so how would I implement that?
Thank you!
Comments
Use this - https://obex.parallax.com/obex/pllset/
If this is for FlexProp C and this is just the initial setting of the clock, I do it like this:
enum { _clkfreq = 300_000_000};
Lol, good point. I presumed he wanted to dynamically change at runtime but you're right, probably just wants to set it.
Here's an example using the above object anyway:
I can only answer for Catalina, but I'm sure the other C compilers would be similar.
First, you would not usually either want or need to do this in C. One reason is that unless you also tell the various C libraries and supporting drivers (Catalina calls these 'plugins') that you are changing the clock speed (which your code does not do) some things may stop working - such as your file system or serial ports. So you would usually at least want to use the C library functions to do this - e.g. in Catalina this might be done using
_clockinit(mode, frequency)
.However, in Catalina, you generally configure the clock mode and speed in the platform support file, which is used by the C startup code before the C program is even launched. Or you specify the clock speed on the compilation command line. Either way, this allows Catalina to pass the information on to everything that needs to know the clock speed.
However, you CAN do exactly what you have specified, using inline PASM. For example, here is what your code would look like in a complete Catalina C program - I have just modified your formatting to make it acceptable to the C compiler:
Here is the code actually generated by compiling this:
Thanks to all of you!
I'm using simpleIDE and the P2 Edge Module
Let me see if any of those are applicable to simpleIDE.
I want to set the speed upon booting/starting the P2
I'm not sure SimpleIDE works with P2... Does it?
If not, I'd try FlexProp or Catalina.
Supposedly it does, IIRC according to the note/comment in the downloads section, but I'll see.
Otherwise I'll have to get FlexProp or Catalina.
Ok... FYI... I did find this and it builds without errors but I have yet to test it.
define clkset (0xF6, 320000000 ) // set the clock to 320 MHz (external 20MHz clock/oscillator)
SimpleIDE does not support the P2. The instruction set and boot process are totally different and will not work with SimpleIDE.
Mike
Thanks Mike... I installed flex prop. That solved some problems. I was able to set the clock speed but now I have to solve some other problems which may be something minor.
If it helps someone here is how I set the clock speed:
void initSys(){
__asm {
HUBSET ##0_000000_0000000000_1111_00_00 // set 20 MHz+ (RCFAST) mode\n"
HUBSET ##1_100111_0100101000_1111_10_00 // enable crystal+PLL, stay in RCFAST mode\n"
WAITX ##20_000_000/100 // wait ~10ms for crystal+PLL to stabilize\n"
HUBSET ##1_000000_0000010000_1111_01_10 // set the clock to 320 MHz\n"
}
}
That's not an ideal solution. In fact it has a bug as well, but that's beside the point. There is two well proven methods already provided.
If you just want to set initial clock frequency then use the
enum {}
method built into the compiler.If you want to dynamically change the clock frequency multiple times then use the "pllset" object I linked.
Here is a sample flex prop program:
Note no clock is set at all. Flex prop uses a default value if none is set (160Mhz).
You can see this because flex prop creates an assembly version along with the binary file name ending in P2ASM.
I like to run my P2 as 200Mhz but you will see at the top of the program it has done the hubset for you with the correct values.
I have one of the different P2 units that has an oscillator running at 19.2Mhz instead of the 20Mhz.
Mike
Thank you everyone!