/** * \brief This function will change the clock source of the AST module. * * \param ast Base address of the AST (i.e. &AVR32_AST). * \param osc_type The oscillator you want to use. If you need a better * accuracy, use the 32 KHz oscillator (i.e. AST_OSC_32KHZ). * \param psel The preselector value for the corresponding oscillator (4-bits). * To obtain this value, you can use this formula: * psel = log(Fosc/Fast)/log(2)-1, where Fosc is the frequency of the * oscillator you are using (32 KHz or 115 KHz) and Fast the frequency * desired. * * \return Boolean true if the initialization succeeds, false otherwise. */ bool ast_change_clk_source(volatile avr32_ast_t *ast, uint8_t osc_type, uint8_t psel) { uint32_t time_out = AST_POLL_TIMEOUT; while (ast_is_clkbusy(ast)) { if (--time_out == 0) { return false; } } /* Disable the clock */ ast->clock &= ~(AVR32_AST_CLOCK_CEN_MASK); time_out = AST_POLL_TIMEOUT; while (ast_is_clkbusy(ast)) { if (--time_out == 0) { return false; } } /* Change the clock source */ ast->clock = osc_type << AVR32_AST_CLOCK_CSSEL_OFFSET; time_out = AST_POLL_TIMEOUT; while (ast_is_clkbusy(ast)) { if (--time_out == 0) { return false; } } /* Enable the clock again */ ast->clock |= AVR32_AST_CLOCK_CEN_MASK; time_out = AST_POLL_TIMEOUT; while (ast_is_clkbusy(ast)) { if (--time_out == 0) { return false; } } /* Set the new prescalar value */ ast->cr = psel << AVR32_AST_CR_PSEL_OFFSET; /* Wait until the ast CTRL register is up-to-date */ while (ast_is_busy(ast)) { } return true; }
/** * \brief This function will initialize the AST module in counter Mode. * * \note If you use the 32 KHz oscillator, it must be enabled before calling * this function. * * \param ast Base address of the AST (i.e. &AVR32_AST). * \param osc_type The oscillator you want to use. If you need a better * accuracy, use the 32 KHz oscillator (i.e. AST_OSC_32KHZ). * \param psel The preselector value for the corresponding oscillator (4-bits). * To obtain this value, you can use this formula: * psel = log(Fosc/Fast)/log(2)-1, where Fosc is the frequency of the * oscillator you are using (32 KHz or 115 KHz) and Fast the frequency * desired. * \param ast_counter Startup counter value * * \return Boolean true if the initialization succeeds, false otherwise. */ bool ast_init_counter(volatile avr32_ast_t *ast, uint8_t osc_type, uint8_t psel, uint32_t ast_counter) { uint32_t time_out = AST_POLL_TIMEOUT; while (ast_is_clkbusy(ast)) { if (--time_out == 0) { return false; } } ast->clock = osc_type << AVR32_AST_CLOCK_CSSEL_OFFSET; time_out = AST_POLL_TIMEOUT; while (ast_is_clkbusy(ast)) { if (--time_out == 0) { return false; } } ast->clock |= AVR32_AST_CLOCK_CEN_MASK; time_out = AST_POLL_TIMEOUT; while (ast_is_clkbusy(ast)) { if (--time_out == 0) { return false; } } /* Set the new AST configuration */ ast->cr = (AST_MODE_COUNTER << AVR32_AST_CR_CAL_OFFSET) | (psel << AVR32_AST_CR_PSEL_OFFSET); /* Wait until the ast CTRL register is up-to-date */ while (ast_is_busy(ast)) { } /* Set the calendar */ ast_set_counter_value(ast, ast_counter); return true; }
int ast_init_calendar(volatile avr32_ast_t *ast, unsigned char osc_type, unsigned char psel, ast_calendar_t ast_calendar) { while (ast_is_clkbusy(ast)); ast->clock = AVR32_AST_CLOCK_CEN_MASK | osc_type << AVR32_AST_CLOCK_CSSEL_OFFSET; // Set the new AST configuration ast->cr = AST_MODE_CALENDAR << AVR32_AST_CR_CAL_OFFSET | psel << AVR32_AST_CR_PSEL_OFFSET ; // Wait until the ast CTRL register is up-to-date while (ast_is_busy(ast)); // Set the calendar ast_set_calendar_value(ast, ast_calendar); return 1; }