Skip to content
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

boards/common/atmega: gracefully handle CKDIV8 fuse #8952

Merged
merged 1 commit into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions boards/common/atmega/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
#include "irq.h"
#include "periph/gpio.h"

#ifndef CPU_ATMEGA_CLK_SCALE_INIT
#define CPU_ATMEGA_CLK_SCALE_INIT CPU_ATMEGA_CLK_SCALE_DIV1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go into ./boards/common/arduino-atmega/include/periph_conf.h preferably under the CLOCK_CORECLOCK definition and with a small bit of doxygen.

Can be done without the #ifndef construction but I don't mind keeping it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem though: not all boards using boards/common/atmega are Arduinos.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can solve that problem in a follow up PR. Discussed a bit offline with @bergzand and maybe found a solution.

#endif

void led_init(void);

void board_init(void)
{
atmega_set_prescaler(CPU_ATMEGA_CLK_SCALE_INIT);
atmega_stdio_init();
cpu_init();
led_init();
Expand Down
31 changes: 31 additions & 0 deletions cpu/atmega_common/include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,37 @@ __attribute__((always_inline)) static inline void cpu_print_last_instruction(voi
printf("Stack Pointer: 0x%04x\n", ptr);
}

/**
* @brief ATmega system clock prescaler settings
*
* Some CPUs may not support the highest prescaler settings
*/
enum {
CPU_ATMEGA_CLK_SCALE_DIV1 = 0,
CPU_ATMEGA_CLK_SCALE_DIV2 = 1,
CPU_ATMEGA_CLK_SCALE_DIV4 = 2,
CPU_ATMEGA_CLK_SCALE_DIV8 = 3,
CPU_ATMEGA_CLK_SCALE_DIV16 = 4,
CPU_ATMEGA_CLK_SCALE_DIV32 = 5,
CPU_ATMEGA_CLK_SCALE_DIV64 = 6,
CPU_ATMEGA_CLK_SCALE_DIV128 = 7,
CPU_ATMEGA_CLK_SCALE_DIV256 = 8,
CPU_ATMEGA_CLK_SCALE_DIV512 = 9,
};

/**
* @brief Initializes system clock prescaler
*/
static inline void atmega_set_prescaler(uint8_t clk_scale)
{
/* Enable clock change */
/* Must be assignment to set all other bits to zero, see datasheet */
CLKPR = (1 << CLKPCE);

/* Write clock within 4 cycles */
CLKPR = clk_scale;
}

/**
* @brief Initializes avrlibc stdio
*/
Expand Down