- в модулях Wire, SPI, Serial приведена в соответствие нумерация используемых экземпляров и периферии микроконтроллера.
- в функции analogWrite() перед запуском ШИМ проверяется, не запущен ли уже указанный канал.
- добавлена возможность переопределения функции main() в скетчах.
- при старте программы задается граница кэшируемой области SPIFI в соответствии с размером текущего исполняемого кода.
- исправление выявленных ошибок.
Co-authored-by: KLASSENTS <klassen@elron.tech>
Co-committed-by: KLASSENTS <klassen@elron.tech>
This commit was merged in pull request #25.
This commit is contained in:
klassents authored and klassents committed 2025-05-30 12:24:08 +03:00
1 parent f1e091bf77
commit eb9b1aa0c9
37 files changed
+248 -156

No files matched your search

+5 -2
View File
@@ -8,10 +8,13 @@
#include "wiring_LL.h"
// HardwareSerial class objects for use in Arduino IDE
HardwareSerial Serial(0);
HardwareSerial Serial0(0);
#if SERIAL_PORT_QTY > 1
HardwareSerial Serial1(1);
#endif
// default interface
HardwareSerial& Serial = DEFAULT_SERIAL;
void serialEvent() __attribute__((weak));
bool Serial0_available() __attribute__((weak));
@@ -144,7 +147,7 @@ extern "C" void __attribute__((optimize("O3"))) serial_interrupt_handler(uint8_t
{
if (uartNumInt == 0)
{
Serial.rx_complete_irq();
Serial0.rx_complete_irq();
}
else if ((uartNumInt == 1) && (SERIAL_PORT_QTY > 1))
{
+3 -1
View File
@@ -103,10 +103,12 @@ class HardwareSerial : public Stream
inline void rx_complete_irq(void) __attribute__((always_inline, optimize("O3")));
};
extern HardwareSerial Serial;
extern HardwareSerial Serial0;
#if SERIAL_PORT_QTY > 1
extern HardwareSerial Serial1;
#endif
// default interface
extern HardwareSerial& Serial;
extern void serialEventRun(void) __attribute__((weak));
+36
View File
@@ -3,6 +3,40 @@
#include "mik32_hal_irq.h"
#include "Arduino.h"
// --------------------- flash cache limit --------------------- //
#include "mcu32_memory_map.h"
#include "mik32_hal_spifi_w25.h"
// define board flash size
#if defined(ARDUINO_START_MIK32_V1)
#define FLASH_SIZE 0x00400000 // 4 Mb
#elif defined(ARDUINO_ACE_UNO_16MB)
#define FLASH_SIZE 0x01000000 // 16 Mb
#elif defined(ARDUINO_ACE_UNO_32MB)
#define FLASH_SIZE 0x02000000 // 32 Mb
#else
#define FLASH_SIZE 0x00800000 // 8 Mb
#endif
// define margin from flash end
#define FLASH_END_OFFSET 0x1000
// values from ld script
extern uint8_t __RODATA__[];
// RAM-function for updating flash cache limit by app size to improve performance
__attribute__((section(".ram_text"))) void updateCacheLimit(void)
{
uint32_t new_limit = (uintptr_t)__RODATA__ - SPIFI_BASE_ADDRESS;
// limit cache size by flash size with margin
if (new_limit > (FLASH_SIZE - FLASH_END_OFFSET))
new_limit = FLASH_SIZE - FLASH_END_OFFSET;
uint32_t MCMDbackup = SPIFI_CONFIG->MCMD; // save current value from MCMD
SPIFI_CONFIG->STAT |= SPIFI_CONFIG_STAT_RESET_M; // reset MCMD mode for writing new CLIMIT
SPIFI_CONFIG->CLIMIT = new_limit; // set new CLIMIT
SPIFI_CONFIG->MCMD = MCMDbackup; // restore MCMD value
}
// --------------------- init --------------------- //
// called from crt0.S before constructors initialization
extern "C" void SystemInit(void)
@@ -20,6 +54,8 @@ extern "C" void SystemInit(void)
// for delays
SysTick_Init();
// update flash cache limit by app size
updateCacheLimit();
}
// called after setup()
+2 -3
View File
@@ -7,6 +7,7 @@
void initVariant() __attribute__((weak));
void initVariant() { }
extern "C" int main(void) __attribute__((weak));
int main()
{
initVariant(); // for freeRTOS
@@ -17,6 +18,4 @@ int main()
{
loop();
}
}
}
@@ -21,11 +21,10 @@ SECTIONS {
*crt0.S.o(.text .text.*)
*(.text.smallsysteminit)
*(.text.SmallSystemInit)
/*. = ORIGIN(rom) + 0xC0;*/
/*KEEP(*crt0.S.o(.trap_text))*/
*(.text)
*(.text.*)
PROVIDE(__RODATA__ = .);
*(.rodata)
*(.rodata.*)
. = ALIGN(CL_SIZE);
+23 -6
View File
@@ -104,9 +104,23 @@ It is recommended to enable the timer in the following order:
*/
void analogWrite(uint32_t PinNumber, uint32_t writeVal)
{
if (digitalPinHasPWM(PinNumber))
if (writeVal > WriteValMax) writeVal = WriteValMax;
if (digitalPinPwmIsOn(PinNumber) > 0) // pin has pwm and pwm is already on
{
if (writeVal > WriteValMax) writeVal = WriteValMax;
// we can only change writeVal if it is differ from current value
TIMER32_TypeDef* timer = pwmPinToTimer(PinNumber);
uint32_t newOCR = (uint32_t) (((uint64_t)pwmTopVal * writeVal) / WriteValMax);
if (timer->CHANNELS[pwmPinToTimerChannel(PinNumber)].OCR != newOCR)
{
// if new ocr differs from current, set new ocr
timer->CHANNELS[pwmPinToTimerChannel(PinNumber)].OCR = newOCR;
}
}
else if (digitalPinPwmIsOn(PinNumber) == 0) // pin has pwm and pwm is off
{
// init pin as pwm
uint32_t OCRval = (uint32_t) (((uint64_t)pwmTopVal * writeVal) / WriteValMax);
// initialization of the required timer
htimer32.Instance = pwmPinToTimer(PinNumber);
@@ -127,7 +141,7 @@ void analogWrite(uint32_t PinNumber, uint32_t writeVal)
htimer32_channel.PWM_Invert = TIMER32_CHANNEL_NON_INVERTED_PWM;
htimer32_channel.Mode = TIMER32_CHANNEL_MODE_PWM;
htimer32_channel.CaptureEdge = TIMER32_CHANNEL_CAPTUREEDGE_RISING;
htimer32_channel.OCR = (uint32_t) (((uint64_t)pwmTopVal * writeVal) / WriteValMax);
htimer32_channel.OCR = OCRval;
htimer32_channel.Noise = TIMER32_CHANNEL_FILTER_OFF;
HAL_Timer32_Channel_Init(&htimer32_channel);
@@ -137,7 +151,7 @@ void analogWrite(uint32_t PinNumber, uint32_t writeVal)
HAL_Timer32_Start(&htimer32);
pwmIsInited++; // increase inited channels qty
}
else
else // pin doesn't have pwm
ErrorMsgHandler("analogWrite(): invalid pwm pin number");
}
@@ -168,14 +182,17 @@ It is recommended to turn off the timer in the following order:
- Write 0 to the INT_CLEAR register;
- Set TIM_EN to 0.
*/
// use only if digitalPinPwmIsOn(PinNumber) > 0
void analogWriteStop(uint32_t PinNumber)
{
if (digitalPinHasPWM(PinNumber) && (pwmIsInited > 0) && digitalPinPwmIsOn(PinNumber))
if ((pwmIsInited > 0))
{
// load the timer address and channel number corresponding to the specified pin
htimer32.Instance = pwmPinToTimer(PinNumber);
htimer32_channel.TimerInstance = htimer32.Instance;
htimer32_channel.ChannelIndex = pwmPinToTimerChannel(PinNumber);
htimer32.Instance->CHANNELS[htimer32_channel.ChannelIndex].OCR = 0;
htimer32_channel.TimerInstance = htimer32.Instance;
// deinit channel
HAL_Timer32_Channel_DeInit(&htimer32_channel);
pwmIsInited--; // decrease inited channels qty
+14 -16
View File
@@ -56,25 +56,23 @@ enum BitOrder
#define NOT_AN_INTERRUPT -1
// Math
#ifdef __cplusplus
#include <algorithm>
using std::min;
using std::max;
#else // C
#include <stdlib.h>
#ifndef abs
#define abs(x) ((x)>0?(x):-(x))
#endif // abs
#include <stdlib.h>
// undefine stdlib's abs if encountered
#ifdef abs
#undef abs
#endif
#ifndef abs
#define abs(x) ((x)>0?(x):-(x))
#endif // abs
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif // min
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif // min
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif // max
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif // max
#endif // __cplusplus
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
+5 -5
View File
@@ -37,8 +37,8 @@ void pinMode(uint32_t PinNumber, uint32_t PinMode)
return;
}
if (digitalPinHasPWM(PinNumber))
// if the pin can use PWM, disable PWM
if (digitalPinPwmIsOn(PinNumber))
// if the pin use PWM, disable PWM
analogWriteStop(PinNumber);
// determine the port and the pin number in the port
@@ -104,7 +104,7 @@ __attribute__((noinline, section(".ram_text"))) void digitalWrite(uint32_t PinNu
}
if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM
// if the pin use PWM, disable PWM
analogWriteStop(PinNumber);
if (Val == HIGH)
@@ -123,7 +123,7 @@ __attribute__((noinline, section(".ram_text"))) int digitalRead(uint32_t PinNumb
}
if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM
// if the pin use PWM, disable PWM
analogWriteStop(PinNumber);
return GPIO_READ_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
@@ -139,7 +139,7 @@ __attribute__((noinline, section(".ram_text"))) void digitalToggle(uint32_t PinN
}
if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM
// if the pin use PWM, disable PWM
analogWriteStop(PinNumber);
GPIO_TOGGLE_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
+1 -1
View File
@@ -13,7 +13,7 @@ unsigned long pulseIn(int pin, int state, unsigned long timeout)
ErrorMsgHandler("pulseIn(): pin number exceeds the total number of pins");
return -1;
}
if (digitalPinHasPWM(pin))
if (digitalPinPwmIsOn(pin) > 0)
// pwm off
analogWriteStop(pin);