Replace all calls and references to _delay_ms() in the code with the architecture-agnostic Delay_MS() function.
Improve code generation for the Delay_MS() function on the AVR8 architecture when called with a constant input.
/* Enable pull-up on the JTAG TCK pin so we can use it to select the mode */
PORTF |= (1 << 4);
- _delay_ms(10);
+ Delay_MS(10);
/* If the TCK pin is not jumpered to ground, start the user application instead */
RunBootloader = (!(PINF & (1 << 4)));
/** Function to perform a blocking delay for a specified number of milliseconds. The actual delay will be
* at a minimum the specified number of milliseconds, however due to loop overhead and internal calculations
* may be slightly higher.
+ *
+ * \param[in] Milliseconds Number of milliseconds to delay
*/
static inline void Delay_MS(uint8_t Milliseconds)
{
+ #if (ARCH == ARCH_AVR8)
+ if (__builtin_constant_p(Milliseconds))
+ {
+ _delay_ms(Milliseconds);
+ }
+ else
+ {
+ while (Milliseconds--)
+ _delay_ms(1);
+ }
+ #elif (ARCH == ARCH_UC3)
while (Milliseconds--)
{
- #if (ARCH == ARCH_AVR8)
- _delay_ms(1);
- #elif (ARCH == ARCH_UC3)
__builtin_mtsr(AVR32_COUNT, 0);
while (__builtin_mfsr(AVR32_COUNT) < (F_CPU / 1000));
- #endif
}
+ #endif
}
#endif
*
* // Turn on each of the four LEDs in turn
* LEDs_SetAllLEDs(LEDS_LED1);
- * _delay_ms(500);
+ * Delay_MS(500);
* LEDs_SetAllLEDs(LEDS_LED1);
- * _delay_ms(500);
+ * Delay_MS(500);
* LEDs_SetAllLEDs(LEDS_LED1);
- * _delay_ms(500);
+ * Delay_MS(500);
* LEDs_SetAllLEDs(LEDS_LED1);
- * _delay_ms(500);
+ * Delay_MS(500);
*
* // Turn on all LEDs
* LEDs_SetAllLEDs(LEDS_ALL_LEDS);
- * _delay_ms(1000);
+ * Delay_MS(1000);
*
* // Turn on LED 1, turn off LED 2, leaving LEDs 3 and 4 in their current state
* LEDs_ChangeLEDs((LEDS_LED1 | LEDS_LED2), LEDS_LED1);
break;
}
- _delay_ms(1);
+ Delay_MS(1);
}
if (HSOFIEnabled)
break;\r
}\r
\r
- _delay_ms(1);\r
+ Delay_MS(1);\r
}\r
\r
if (HSOFIEnabled)\r
static inline uint_reg_t USB_INT_GetGlobalEnableState(void)\r
{\r
GCC_MEMORY_BARRIER();\r
- return (__builtin_mfsr(AVR32_SR) & AVR32_SR_GM);\r
+ return __builtin_mfsr(AVR32_SR);\r
}\r
\r
static inline void USB_INT_SetGlobalEnableState(uint_reg_t GlobalIntState) ATTR_ALWAYS_INLINE;\r
static inline void USB_INT_SetGlobalEnableState(uint_reg_t GlobalIntState)\r
{\r
GCC_MEMORY_BARRIER();\r
- __builtin_ssrf(AVR32_SR_GM_OFFSET, GlobalIntState);\r
+ if (GlobalIntState & AVR32_SR_GM)\r
+ __builtin_ssrf(AVR32_SR_GM_OFFSET);\r
GCC_MEMORY_BARRIER();\r
}\r
\r
* cli();
*
* // Wait two seconds for the USB detachment to register on the host
- * for (uint8_t i = 0; i < 128; i++)
- * _delay_ms(16);
+ * Delay_MS(2000);
*
* // Set the bootloader key to the magic value and force a reset
* Boot_Key = MAGIC_BOOT_KEY;
void ISPProtocol_DelayMS(uint8_t DelayMS)
{
while (DelayMS-- && !(TimeoutExpired))
- _delay_ms(1);
+ Delay_MS(1);
}
#endif
/* Enable pull-up on the JTAG TDI pin so we can use it to select the mode */
PORTF |= (1 << 7);
- _delay_ms(10);
+ Delay_MS(10);
/* Select the firmware mode based on the JTD pin's value */
CurrentFirmwareMode = (PINF & (1 << 7)) ? MODE_USART_BRIDGE : MODE_PDI_PROGRAMMER;