-
Notifications
You must be signed in to change notification settings - Fork 1
[pic32cz] Fix gpio and clock drivers. Implement blink example for pic32cz curiosity board #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,38 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <wolfHAL/wolfHAL.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "pic32cz_curiosity_ultra.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| volatile size_t g_tick = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| volatile uint8_t g_waiting = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| volatile uint8_t g_tickOverflow = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void SysTick_Handler() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void SysTick_Handler() | |
| void SysTick_Handler(void) |
Copilot
AI
Feb 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WaitMs() uses extra shared flags (g_waiting/g_tickOverflow) and a non-atomic handshake that can miss an overflow if wrap happens between startCount = g_tick; and g_waiting = 1;, causing an immediate underflow in currentCount - startCount. You can simplify and make this wrap-safe by relying on unsigned wraparound: loop until (size_t)(g_tick - startCount) >= ms (also fixes the current off-by-one from using > instead of >=).
| size_t tickBefore = g_tick++; | |
| if (g_waiting) { | |
| if (tickBefore > g_tick) | |
| g_tickOverflow = 1; | |
| } | |
| } | |
| void WaitMs(size_t ms) | |
| { | |
| size_t startCount = g_tick; | |
| g_waiting = 1; | |
| while (1) { | |
| size_t currentCount = g_tick; | |
| if (g_tickOverflow) { | |
| if ((SIZE_MAX - startCount) + currentCount > ms) { | |
| break; | |
| } | |
| } else if (currentCount - startCount > ms) { | |
| break; | |
| } | |
| } | |
| g_waiting = 0; | |
| g_tickOverflow = 0; | |
| /* Increment the millisecond tick counter. Wraparound is handled in WaitMs. */ | |
| g_tick++; | |
| } | |
| void WaitMs(size_t ms) | |
| { | |
| size_t startCount = g_tick; | |
| /* Busy-wait until the requested number of milliseconds has elapsed. | |
| * This uses unsigned wraparound: the difference (g_tick - startCount) | |
| * is well-defined even if g_tick wraps between calls. */ | |
| while ((size_t)(g_tick - startCount) < ms) { | |
| /* spin */ | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,11 +34,15 @@ | |
| #define PIC32CZ_OSCCTRL_PLLxPOSTDIVA_POSTDIV0 WHAL_MASK_RANGE(5, 0) /* Output 0 divider */ | ||
| #define PIC32CZ_OSCCTRL_PLLxPOSTDIVA_OUTEN0 WHAL_MASK(7) /* Output 0 enable */ | ||
|
|
||
| /* OSCCTRL Status Register - PLL lock and oscillator ready flags */ | ||
| #define PIC32CZ_OSCCTRL_STATUS_REG (PIC32CZ_OSCCTRL + 0x10) | ||
| #define PIC32CZ_OSCCTRL_STATUS_PLLxLOCK(pllInst) WHAL_MASK((24 + (pllInst))) | ||
|
|
||
| /* GCLK - Generic Clock Controller (base offset 0x10000) */ | ||
| #define PIC32CZ_GCLK 0x10000 | ||
|
|
||
| /* Generator Control Register - configures clock source and divider per generator */ | ||
| #define PIC32CZ_GCLK_GENCTRLx_REG(gclkInst) (PIC32CZ_GCLK + 0x20 + (gclkInst * 0x4)) | ||
| #define PIC32CZ_GCLK_GENCTRLx_REG(gclkInst) ((PIC32CZ_GCLK + 0x20 + (gclkInst * 0x4))) | ||
| #define PIC32CZ_GCLK_GENCTRLx_SRC WHAL_MASK_RANGE(4, 0) /* Source selection */ | ||
| #define PIC32CZ_GCLK_GENCTRLx_GENEN WHAL_MASK(8) /* Generator enable */ | ||
| #define PIC32CZ_GCLK_GENCTRLx_DIV WHAL_MASK_RANGE(31, 16) /* Division factor */ | ||
|
|
@@ -48,11 +52,19 @@ | |
| #define PIC32CZ_GCLK_PCHCTRLx_GEN WHAL_MASK_RANGE(3, 0) /* Generator selection */ | ||
| #define PIC32CZ_GCLK_PCHCTRLx_CHEN WHAL_MASK(6) /* Channel enable */ | ||
|
|
||
| /* GCLK Synchronization Busy Register - poll after writing GENCTRLx */ | ||
| #define PIC32CZ_GCLK_SYNCBUSY_REG (PIC32CZ_GCLK + 0x04) | ||
| #define PIC32CZ_GCLK_SYNCBUSY_GENCTRLx(gclkInst) WHAL_MASK((2 + (gclkInst))) | ||
|
|
||
| /* MCLK - Main Clock Controller (base offset 0x12000) */ | ||
| #define PIC32CZ_MCLK 0x12000 | ||
|
|
||
| /* MCLK Interrupt Flag Register - clock ready status */ | ||
| #define PIC32CZ_MCLK_INTFLAG_REG (PIC32CZ_MCLK + 0x08) | ||
| #define PIC32CZ_MCLK_INTFLAG_CKRDY WHAL_MASK(0) /* Clock ready */ | ||
|
|
||
| /* CPU Clock Divider Register */ | ||
| #define PIC32CZ_MCLK_DIV1_REG (PIC32CZ_MCLK + 0x14) | ||
| #define PIC32CZ_MCLK_DIV1_REG (PIC32CZ_MCLK + 0x10) | ||
| #define PIC32CZ_MCLK_DIV1 WHAL_MASK_RANGE(7, 0) /* CPU clock divider */ | ||
|
|
||
| /* Peripheral Clock Mask Registers - enable/disable bus clocks to peripherals */ | ||
|
|
@@ -109,6 +121,7 @@ whal_Error whal_Pic32czClockPll_Init(whal_Clock *clkDev) | |
| size_t PLLxFBDIV_REG; | ||
| size_t PLLxREFDIV_REG; | ||
| size_t PLLxPOSTDIVA_REG; | ||
| size_t status; | ||
|
|
||
| if (!clkDev) { | ||
| return WHAL_EINVAL; | ||
|
|
@@ -151,10 +164,22 @@ whal_Error whal_Pic32czClockPll_Init(whal_Clock *clkDev) | |
| whal_SetBits(PIC32CZ_OSCCTRL_PLLxCTRL_REFSEL, oscCtrlCfg->refSel) | | ||
| whal_SetBits(PIC32CZ_OSCCTRL_PLLxCTRL_BWSEL, oscCtrlCfg->bwSel)); | ||
|
|
||
| /* Wait for PLL to lock */ | ||
| do { | ||
| whal_Reg_Get(clkDev->regmap.base, PIC32CZ_OSCCTRL_STATUS_REG, | ||
| PIC32CZ_OSCCTRL_STATUS_PLLxLOCK(oscCtrlCfg->pllInst), &status); | ||
| } while (!status); | ||
|
|
||
| /* Configure CPU clock divider in MCLK */ | ||
| whal_Reg_Update(clkDev->regmap.base, PIC32CZ_MCLK_DIV1_REG, PIC32CZ_MCLK_DIV1, | ||
| whal_SetBits(PIC32CZ_MCLK_DIV1, mclkCfg->div)); | ||
|
|
||
| /* Wait for clock divider change to take effect */ | ||
| do { | ||
| whal_Reg_Get(clkDev->regmap.base, PIC32CZ_MCLK_INTFLAG_REG, | ||
| PIC32CZ_MCLK_INTFLAG_CKRDY, &status); | ||
| } while (!status); | ||
|
|
||
| /* Configure each GCLK generator with its source and divider */ | ||
| for (uint8_t i = 0; i < cfg->gclkCfgCount; ++i) { | ||
| whal_Pic32czClock_GclkCfg *gclkCfg = &cfg->gclkCfg[i]; | ||
|
|
@@ -163,6 +188,12 @@ whal_Error whal_Pic32czClockPll_Init(whal_Clock *clkDev) | |
| whal_SetBits(PIC32CZ_GCLK_GENCTRLx_SRC, gclkCfg->genSrc) | | ||
| whal_SetBits(PIC32CZ_GCLK_GENCTRLx_GENEN, 1) | | ||
| whal_SetBits(PIC32CZ_GCLK_GENCTRLx_DIV, gclkCfg->genDiv)); | ||
|
|
||
| /* Wait for generator synchronization */ | ||
| do { | ||
| whal_Reg_Get(clkDev->regmap.base, PIC32CZ_GCLK_SYNCBUSY_REG, | ||
| PIC32CZ_GCLK_SYNCBUSY_GENCTRLx(gclkCfg->gen), &status); | ||
| } while (status); | ||
| } | ||
|
Comment on lines
167
to
197
|
||
|
|
||
| return WHAL_SUCCESS; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #ifndef WHAL_CORTEX_M7_H | ||
| #define WHAL_CORTEX_M7_H | ||
|
|
||
| #include <wolfHAL/timer/systick.h> | ||
|
|
||
| #define WHAL_CORTEX_M7_SYSTICK_DEVICE \ | ||
| .regmap = { \ | ||
| .base = 0xE000E010, \ | ||
| .size = 0x400, \ | ||
| }, \ | ||
| .driver = &whal_SysTick_Driver | ||
|
|
||
| #endif /* WHAL_CORTEX_M7_H */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples/pic32cz/main.cnow usesuint8_tandSIZE_MAX, but the file doesn't include a header that defines them. This will fail to compile (or fail under-Werror) depending on the toolchain. Include the appropriate standard header(s) (e.g.,<stdint.h>foruint8_tandSIZE_MAX).