dbuezas / lgt8fx

Board Package for Logic Green LGT8F328P LGT8F328D and LGT8F88D
369 stars 88 forks source link

lgt8fx - Boards Manager Menu error / quirk - for Internal 16MHz / 32MHz #264

Closed jlsilicon closed 1 year ago

jlsilicon commented 1 year ago

I am working with the Lgt8f32x -32 / -48 chips. I purchased a batch of these boards - of both 32pin and 48pin Lpt8f328 chips.

The chips have on board 16MHz crystals, but I want to use them at 32MHz. But, when I tested them, they seemed to work as 16MHz even if set at Internal 32MHz speed.

At first, I though the chips might be limited to 16MHz - until I tested deeper - listed below :

So, I set : Board = Lgt8F328 , Internal 32MHz

But if I set : Board = Lgt8F328 , Internal 16MHz

To verify, I set: Board = Lgt8F328 , External 16MHz

So it seems that the Int 16MHz / Int 32MHz Settings are backwards

LaZsolt commented 1 year ago

Please attach a picture of your board setup like this:

kép

dbuezas commented 1 year ago

Actually, why are there "internal" options for the board with external clock? It should be enough to select the normal board (which uses the internal one). Can you try if that works fine? If so, I'll delete those variants

LaZsolt commented 1 year ago

I tested with this sketch all the combinations of frequencies, but I can't find any error.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(4800);
  Serial.println("Running...");
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}
vklimk commented 1 year ago

Actually, why are there "internal" options for the board with external clock? It should be enough to select the normal board (which uses the internal one). Can you try if that works fine? If so, I'll delete those variants

Please don't delete them. It is very useful sometimes to have ability to switch frequency from default one.

jlsilicon commented 1 year ago

My External Clk = 16MHz But I want to use it at 32MHz (Internal Clock)

*** Are you Testing on the -Lqfp48 Chip ? -- I am testing on the : Lqfp8f328P -LQFP48 - 48pin chip (not 32pin)

-- The menu config is fine and cool - KEEP IT

-- but the chip is processing Internal 32MHz & 16MHz backwards or it is receiving them backwards -- It just needs to be fixed

I am currently trying to locate the Fuse Registers define page. These look right , so I am not sure where the problem originates :

# Clock frequencies 328_12.menu.clock.32M_i=Internal 32 MHz (Not for 328D) 328_12.menu.clock.32M_i.build.clock_source=1 328_12.menu.clock.32M_i.build.f_osc=32000000L 328_12.menu.clock.32M_i.build.f_div=1

328_12.menu.clock.16M_i=Internal 16 MHz 328_12.menu.clock.16M_i.build.clock_source=1 328_12.menu.clock.16M_i.build.f_osc=32000000L 328_12.menu.clock.16M_i.build.f_div=2

328_12.menu.clock.8M_i=Internal 8 MHz 328_12.menu.clock.8M_i.build.clock_source=1 328_12.menu.clock.8M_i.build.f_osc=32000000L 328_12.menu.clock.8M_i.build.f_div=4

-

Is there another file with F_CPU , ifdefs , etc ? -- I thought that I saw this , but I can not find it now.

Also, if I run it at Internal 32MHz - why does delay(1000) run at 0.5 second (with baud at 2x speed) ???

jlsilicon commented 1 year ago

Reason for this testing - is that I am testing lower prescale rates / so higher SPS rates.

I found that lowering prescale to 3 instead of 5 - changes from 512Khz at Cpu=16MHz -to- 2MHz at Cpu=32MHz - seems to still give accurate Analog reads. Prescales 3/4/5 - seem to return generally the same Analog Values. But Prescale less than 3 - and the Lower Analog value bits start getting clipped.

ADCSRA |= 4 ; // - seems to be good at i=4+ (pre=16+) => 2MHz @ cpu=32MHz

Notice that AdcClk[3=>8] is sometimes less in value , in below testing ... :

` CpLgt8_ADC-Speed_Tests :

Cpu=32MHz , AdcClk[0=>1]=AdcClk=32000KHz=32MHz , ADC:A_R= 1381 Cpu=32MHz , AdcClk[1=>2]=AdcClk=16000KHz=16MHz , ADC:A_R= 1493 Cpu=32MHz , AdcClk[2=>4]=AdcClk=8000KHz=8MHz , ADC:A_R= 1572 Cpu=32MHz , AdcClk[3=>8]=AdcClk=4000KHz=4MHz , ADC:A_R= 1626 Cpu=32MHz , AdcClk[4=>16]=AdcClk=2000KHz=2MHz , ADC:A_R= 1640 Cpu=32MHz , AdcClk[5=>32]=AdcClk=1000KHz=1MHz , ADC:A_R= 1645

Cpu=32MHz , AdcClk[0=>1]=AdcClk=32000KHz=32MHz , ADC:A_R= 1477 Cpu=32MHz , AdcClk[1=>2]=AdcClk=16000KHz=16MHz , ADC:A_R= 1477 Cpu=32MHz , AdcClk[2=>4]=AdcClk=8000KHz=8MHz , ADC:A_R= 1567 Cpu=32MHz , AdcClk[3=>8]=AdcClk=4000KHz=4MHz , ADC:A_R= 1626 Cpu=32MHz , AdcClk[4=>16]=AdcClk=2000KHz=2MHz , ADC:A_R= 1639 Cpu=32MHz , AdcClk[5=>32]=AdcClk=1000KHz=1MHz , ADC:A_R= 1644

Cpu=32MHz , AdcClk[0=>1]=AdcClk=32000KHz=32MHz , ADC:A_R= 1477 Cpu=32MHz , AdcClk[1=>2]=AdcClk=16000KHz=16MHz , ADC:A_R= 1477 Cpu=32MHz , AdcClk[2=>4]=AdcClk=8000KHz=8MHz , ADC:A_R= 1572 Cpu=32MHz , AdcClk[3=>8]=AdcClk=4000KHz=4MHz , ADC:A_R= 1626 Cpu=32MHz , AdcClk[4=>16]=AdcClk=2000KHz=2MHz , ADC:A_R= 1640 Cpu=32MHz , AdcClk[5=>32]=AdcClk=1000KHz=1MHz , ADC:A_R= 1645

Cpu=32MHz , AdcClk[0=>1]=AdcClk=32000KHz=32MHz , ADC:A_R= 1493 Cpu=32MHz , AdcClk[1=>2]=AdcClk=16000KHz=16MHz , ADC:A_R= 1477 Cpu=32MHz , AdcClk[2=>4]=AdcClk=8000KHz=8MHz , ADC:A_R= 1567 Cpu=32MHz , AdcClk[3=>8]=AdcClk=4000KHz=4MHz , ADC:A_R= 1626 Cpu=32MHz , AdcClk[4=>16]=AdcClk=2000KHz=2MHz , ADC:A_R= 1642 Cpu=32MHz , AdcClk[5=>32]=AdcClk=1000KHz=1MHz , ADC:A_R= 1643

Cpu=32MHz , AdcClk[0=>1]=AdcClk=32000KHz=32MHz , ADC:A_R= 1493 Cpu=32MHz , AdcClk[1=>2]=AdcClk=16000KHz=16MHz , ADC:A_R= 1477 Cpu=32MHz , AdcClk[2=>4]=AdcClk=8000KHz=8MHz , ADC:A_R= 1567 Cpu=32MHz , AdcClk[3=>8]=AdcClk=4000KHz=4MHz , ADC:A_R= 1624 Cpu=32MHz , AdcClk[4=>16]=AdcClk=2000KHz=2MHz , ADC:A_R= 1641 Cpu=32MHz , AdcClk[5=>32]=AdcClk=1000KHz=1MHz , ADC:A_R= 1644 `

jlsilicon commented 1 year ago

Either of these give the same 32MHz running speed (Delay(1000)=>0.5sec , baud4800=>9600baud on Terminal) : -- If I change either of these Menues to Clock=Internal 32MHz , then it runs slower (Delay() / terminal baud) ...

image

image

dbuezas commented 1 year ago

Why have a dedicated "Clock Source Internal" inside the board "LGT8F328 + 16 MHz crystal"? Isn't this the same as just picking the "LGT8F328" board?

I don't mean to remove the "LGT8F328 + 16 MHz crystal", just the option within it to use the internal clock (which should be the same as the plain "LGT8f328" board.

jlsilicon commented 1 year ago

It can stay.

But the Int32MHz / Int16MHz problem is still there - with either Option: LGT8F328 or LGT8F328 +16MHzCrystal.

I can use it - but setting Clock=Int-16MHz to get 32MHz clock speed seems weird.

dbuezas commented 1 year ago

I cannot reproduce any of this ( am using a LQFP48) and neither can @LaZsolt. With internal or external clock selections, all is working as intended

image

@vklimk The menus are duplicated: internal clocks on "LGT8F328 + 16 MHz crystal" are the same as picking "LGT8F328" as a board. Is there anything else that I am missing? This duplication makes the boards.txt longer than necessary and harder to update so I intend to delete unless there is something I'm not seeing :)

LaZsolt commented 1 year ago

I do not rule out that this is another defective chip series. If the clock prescaler bit 0 stuck to zero, the delay(1000) will be 0.5 s long when internal 16 or 4 or 1 MHz selected.

jlsilicon commented 1 year ago

Its not stuck - the baudrate needed for the Terminal changes with the Clk=##MHz setting chosen.

If you want to leave the Menu as-is then that is fine I guess. -- Just make a note of it for anybody else discovering this issue.

Thanks for the assistance.

BTW: Where is the file with F_CPU & CLKPR that calculates this ? (not the boards.h fuses file) So I can try analyzing it. -- I was looking at this earlier issue : https://forum.arduino.cc/t/lgt8f328p-lqfp32-minievb-external-crystal-enabling/967143/6

LaZsolt commented 1 year ago

platform.txt line 54

jlsilicon commented 1 year ago

Thats not the file - thats just compiler settings. I want the file with the calculations using #ifdefs and F_CLK

I hesitate to say that the chips are defective. They seem to work great other than the flipped Clk 16/32

LaZsolt commented 1 year ago

main.cpp https://github.com/dbuezas/lgt8fx/blob/e844e1c88a5d6dbef352ca13e38aacf561450f5d/lgt8f/cores/lgt8f/main.cpp#L146-L172

jlsilicon commented 1 year ago

Ahh, the cores thanks, I remembered it from somewhere.

It seems to follow the spec. Reference: https://wiki.ocrobot.com/lib/exe/fetch.php?media=ocrobot:alpha:8f328p-u:lgt8f88p_lgt8f168p_lgt8f328p_translated.pdf pp33-35

I dont know. I cant find the bug. But, I can still use it as long as I remember this quirk.

At least , I got the ADC from 0.5MSPS up to 2MSPS.

Thanks for trying.

LaZsolt commented 1 year ago

Can you test the delay(1000) and serial speed at internal 4 MHz (divider = 3) and at internal 1 MHz (divider = 5) and external 8 MHz (divider = 1)?

jlsilicon commented 1 year ago

Hold on ... I am testing on the Menu: 8MHz , 4MHz :

16MHz: baud=9600=>19000onterm , delay(1000)=>0.5sec

8MHz: baud=9600=38400onterm , delay(1000)=>0.25sec

4MHz: baud=9600=>74880onterm , delay(1000)=>0.1sec

2MHz: baud=9600=>150000onterm , delay(1000)=> fastflash

1MHz: baud=9600=>??? , delay(1000)=> fastflash

-- Wow, Looks like the Divisor is backwards

If you want me to test by Divisor specifically, then you need to supply the code for me.

dbuezas commented 1 year ago

Yes, that just changes the divider (divisor = crystal/selected_clock).

LaZsolt commented 1 year ago

-- Wow, Looks like the Divisor is backwards

Very strage. It means the divisor does not work at all and the CPU always running at 32 MHz internal or 16 Mhz external.

jlsilicon commented 1 year ago

Bummer

Well, at least I can purchase Arduino MicroPro clone and 48pin at $1.50

Note that setting Int32MHz seems to run same as setting Ext16MHz

jlsilicon commented 1 year ago

Hold on ... I am going to try loop cmd timing tests ...

for(i=0; i<10000000; i++) { c+=i; } Int 32MHz : Int 16MHz : => 4sec Ext 16MHz : Int 8MHz : Int 1MHz : => 4sec

yep

dbuezas commented 1 year ago

is it the one on the far right of these?

image

I'm testing on that one (LQFP48) and clock selection works just fine

jlsilicon commented 1 year ago

for(i=0; i<10000000; i++) { c+=i; } Int 32MHz : Int 16MHz : => 4sec Ext 16MHz : Int 8MHz : Int 1MHz : => 4sec

jlsilicon commented 1 year ago

This did change it :

` int R; // if(mode == INT_OSC) { // prescaler settings CLKPR = 0x80; CLKPR = 0x00; // CLKPR = 0x01; // switch to internal crystal R = PMCR & 0x9f; PMCR = 0x80; PMCR = R; // disable external crystal R = PMCR & 0xfb; PMCR = 0x80; PMCR = R; // }

CLKPR = 0x80 ; CLKPR = 0x00; // => 4sec // CLKPR = 0x01; // => 8sec // CLKPR = 0x02; // => 16sec // CLKPR = 0x03; // CLKPR = 0x04; // CLKPR = 0x05; // CLKPR = 0x06; // CLKPR = 0x07; // CLKPR = 0x08;

long i , c=0 ; for(i=0; i<=1000; i++) { c+= i; } `

jlsilicon commented 1 year ago

Can you test your -48 chip with this :

` void setup() { // This runs MCU at FULL SPEED (32MHz) . See comment below. clock_prescale_set(clock_div_1); // RCMCAL = 255; // for overclock to 42MHz

// Wait for serial to initialise Serial.begin(9600); // - term at 9600/4800? if Xtal=32MHz/16MHz ? while ( ! Serial ) {} Serial.println("Booting: ... please wait ... ");

pinMode(13, OUTPUT); digitalWrite(13, HIGH);

long i , c=0; Serial.println("START");

while(1) { for(i=0; i<=10000000; i++) { c += i ; }

digitalWrite(13, LOW); Serial.print("LOW "); Serial.println(c);

for(i=0; i<=10000000; i++) { c += i ; }

digitalWrite(13, HIGH); Serial.print("HIGH "); Serial.println(c); } } `

jlsilicon commented 1 year ago

Is this giving me the 32MHz ? :

clock_prescale_set(clock_div_1);

So, i am overriding it to always 32MHz ... Looks good then.

Sorry - I missed that. Thanks again - for the help.