Closed mchacher closed 10 months ago
If the lgt8f328p is 100% compatible with the ATmega328p then there should be no problems. Just install the board support packages and load the library as usual. The sketch upload mechanism is different to the ATmega328p, but this won't affect the building of binary files.
But I'm thinking that there may be some issues with the WDT triggering options, if it is not actually 100% compatible, as noted here. It may just be a fuse setting issue for the individual boards, which can be corrected in boards.txt
(by comparison with the lgt8f328p datasheet), but it may be a hardware implementation issue. And if so then one of the other Timers will need to be configured to provide the System Tick.
Any luck? If so, please let me know.
Hi I didn’t dig further since I don’t have the tech skills. I would be still interested having support. I can do some testing to help
I have noticed that the lgt8f328p has four timers (one more than the ATmega328P), so it might be possible to use Timer3 (8 bit 16 bit timer) for tick generation ...
Maybe it's possible to use THIS tool to generate the appropriate code for timer3.
Guglielmo
Yes. Did not know that. That is good as it won't break Arduino code. And it will be more configurable for different intervals.
Further examples at avrfreertos, show how Timer0 (an 8 bit Timer) can be configured for ATmega328p. Timer3 will probably be no more difficult than exchanging 0 for 3 in the code in this case.
Note that ATmega1284p Timer3 is 16 bit so is not a good example here.
thanks all for your feedback. Not sure I am following. Finally what would be your recommendation to make it work? I confirm that the code as it is not working.
Here are the constants for Timer0 which may be the same as Timer3.
Here is the interrupt configuration. And here the interrupt needs to be adjusted too.
Basically read the FreeRTOS10 code and replace Timer0 with Timer3 (assuming that it is configured the same, confirmed by reading the data sheet). These relevant functions need to be added (or exchanged) into the library code to implement the special 8 bit Timer.
Regarding my previous post, upon closer inspection of the various "translations from Chinese" of the datasheet, it appears that Timer 3 of the LGT8F328 is a 16-bit timer, not an 8-bit timer.
From the datasheet:
Overview TC3 is a general purpose 16-bit Timer/Counter module, with PWM support. It allows accurate wave generation.
Guglielmo
Some news ... ... using the "dbuezas" Arduino core for LGT8F328 we have the capability to use the WDT in "interrupt" mode (_see the wdtinterrupt example).
Unfortunately, the WDT basically works with an internal clock of 32 KHz and the shortest interrupt time that can be achieved with this clock is 64 ms.
I am working on modifying the library (WDT.h) so that it has the ability to have the wdt work both at 32 KHz and at (32 MHz / 16), thus being able to have the possibility of interrupts at 16 ms (and even less: 8, 4, 2, 1).
Once this is done, I will try to modify the port.c to use the wdt in this way ... Any suggestions?
Thanks a lot,
Guglielmo
Good to hear. I suggest following the conventions in avrfreertos port.c
for naming the System Tick source.
For example portUSE_WDTO
is used for the standard WDT. Perhaps use portUSE_WDT_LGT8F328
or similar.
These definitions are selected in FreeRTOSBoardDefs.h
based on the Board being used, from compiler provided definitions.
Sorry Phillip, but all the suggestions I see you making are related to avrfreertos ... however, I believe that both the OP and I, we are interested in changing the Arduino_FreeRTOS_Library library ...
Can you make any suggestions and refer to the files of the said library? Thanks in advance,
Guglielmo
I'm providing avrfreertos
as an example or reference, as the kind of thing you are trying to achieve has been done there already. Specifically alternate MCUs, alternate boards, and alternate Timers. Just treat it as reference material.
Arduino_FreeRTOS
is simple by design with one Timer setup and all the potential alternatives and definitions are not present. So there's no examples there to see, unfortunately.
In an Arduino Library, it also difficult to make things configurable. And all the MCU resources are normally utilised by Arduino, so it becomes quite tricky not to break something a new or less technical user would expect to "just work".
I've pushed an update that might make things work, provided the board definitions are installed correctly.
I'm a bit confused at to what the most generic term for the LGT8F328P is. It is referred to in the variant files under different definitions, and there doesn't seem to be a hierarchy present so I went with below.
#if defined(__LGT8FX8P__) || defined(__LGT8FX8E__) || defined(__LGT8FX8P48__)
#endif
Please test it to see what happens. I'm not holding my breath though.
Yes, actually it is not going to work properly, as the WDTO_15MS
definition doesn't match what's provided, and so the timing will be completely out.
There are many further changes required to how the WDT is configured, as you point out @gpb01. It would be best to get the fast WDT interrupt code sorted out by PR into the Library first, then I can pick that up and continue here. Otherwise I end up replicating what should be device specific library code.
Hi Phillip, just a point of clarification ... looking at the detail of the compilation string used by core of "dbuezas" I see:
-mmcu=atmega328p -DSERIAL_RX_BUFFER_SIZE=64 -DCLOCK_SOURCE=2 -DF_CPU=(16000000L/1) -DF_OSC=16000000L -DF_DIV=1 -DARDUINO=10819 -DARDUINO_AVR_LARDU_328E -DARDUINO_ARCH_AVR
so ... no reference to the id referenced in your previous post (maybe they are automatically defined by the compiler).
In any case, since the change to WDT is only present in the core of "dbuezas" and not in other cores for LGT8F328P, I suggest using:
#ifdef ARDUINO_AVR_LARDU_328E
...
#endif
...what I believe is unique to the above core :-)
Guglielmo
Thanks, I'll modify to use that definition soon.
I also need to add the lgtwdt.begin()
function to the startup()
code too.
It would be ideal if your core WDT improvement PR could define similarly named functions for use of the WDT as are in use with the 'avr-libc/wdt.h` file and the additional interrupt function and interrupt+reset function I've added, that way only the correct header would need to be selected and defined, rather than needing to define alternate functions too.
I don't know if that would create namespace issues, but I think not.
Phillip, thinking about it, to avoid modifications with the WDT and make the library independent of a specific core, would perhaps be better to use Timer 3, which, by the way, does not exist on the ATmega328P and is therefore normally free ...
This is the initialization routine to get an interrupt every 15 msec:
void setupTimer3() {
noInterrupts();
// Clear registers
TCCR3A = 0;
TCCR3B = 0;
TCNT3 = 0;
// 66.66000066660001 Hz (16000000/((30002+1)*8))
OCR3A = 30002;
// CTC
TCCR3B |= (1 << WGM32);
// Prescaler 8
TCCR3B |= (1 << CS31);
// Output Compare Match A Interrupt Enable
TIMSK3 |= (1 << OCIE3A);
interrupts();
}
and this is the definition of ISR being called:
ISR(TIMER3_vect) {
if (TIFR3 & (1 << OCF3A)) {
TIFR3 = 1 << OCF3A;
/* on OCR3A match */
...
...
}
}
... could make it simpler and more generic (not tied to a specific core)?
Guglielmo
thinking about it, to avoid modifications with the WDT and make the library independent of a specific core, would perhaps be better to use Timer 3, which, by the way, does not exist on the ATmega328P and is therefore normally free.
Recently an alternate configuration of Timer 0 was removed from the main code and moved to an external definition, together with documentation on how to define alternative System Tick Timer sources.
Rather than put more definitions in the main code base for just one clone MCU, I would prefer to have a PR suggestion to add the LGT8F Timer 3 using the mechanism documented. Then we can add code examples specifically for the LGT8F without clouding the code for all other (formerly) AVR devices.
If you could test the suggested PR and add some notes or documentation so that people can follow along, I can add that much more quickly I believe.
What I would find really nice is using the Timer defines mechanism from avrfreertos so that the configuration of Timers is then generalised as shown here. Though that may be too extensive for what we need to achieve here.
Hi Phillip, I looked the example you provide for Timer 0 and ...
I did not understand how and who includes the .cpp file you provide (tick_sources_timer0.cpp) in the project.
In the above file I see the function "prvSetupTimerInterrupt", I see the definition of the ISR, but I do NOT see:
2.1 vPortEndScheduler ... which, obviously, in the case of using a Timer, must be provided.
2.2 I don't understand how to connect the ISR defined in "tick_sources_timer0.cpp" with the various conditions where "configUSE_PREEMPTION" is 0 or is 1 (different things need to be done inside the ISR)
Can you provide clarification ...
Thank you,
Guglielmo
P.S.: Another strange thing ... If I compile the program that I indicated HERE everything is fine and there are no errors; if I try to include Timer 3 related definitions directly inside "FreeRTOSVariant.h" or inside "port.c" I get errors as if it no longer recognizes the existence of Timer 3 ... what am I doing wrong?
I get errors as if it no longer recognizes the existence of Timer 3 ... what am I doing wrong?
Ok ... problem solved ... I had to add the following:
#if defined( portUSE_LGT_TIMER3 )
#include <lgtx8p.h> // lgt8f328p spec
#endif
to include the definition file lgtx8p.h
Now I have an Arduino_FreeRTOS library working on LGT8F328 using Timer 3 for ticks :-) , but ... I inserted the code inside "port.c" because I am not clear on how to use the external .cpp as you have indicated :-\
Guglielmo
Now I have an Arduino_FreeRTOS library working on LGT8F328 using Timer 3 for ticks :-)
Good news.
Please prepare a PR so I can see exactly how you did it. I will use that as a guide, and further clarify the instructions for next time.
I'm AFK until late next week, so no rush. So I'll sort it when I'm back.
PR is waiting ... :-)
Guglielmo
Thanks for this great work. I would like to use FreeRTOS library with lgt8f328p from LogicGreen. Is this micro supported by chance? thanks a lot.