sam3: Add enable_pclock() helper function

Add a helper function to enable peripheral clocks.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-12-28 19:00:29 -05:00
parent b4baabe408
commit 4683036f98
10 changed files with 32 additions and 22 deletions

View File

@ -34,9 +34,9 @@ gpio_adc_setup(uint8_t pin)
break;
}
if (!(PMC->PMC_PCSR1 & (1 << (ID_ADC-32)))) {
if (!is_enabled_pclock(ID_ADC)) {
// Setup ADC
PMC->PMC_PCER1 = 1 << (ID_ADC-32);
enable_pclock(ID_ADC);
uint32_t prescal = SystemCoreClock / (2 * ADC_FREQ_MAX) - 1;
ADC->ADC_MR = (ADC_MR_PRESCAL(prescal)
| ADC_MR_STARTUP_SUT768

View File

@ -110,7 +110,7 @@ gpio_in_setup(uint8_t pin, int8_t pull_up)
if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs))
goto fail;
uint32_t port = GPIO2PORT(pin);
PMC->PMC_PCER0 = 1 << (ID_PIOA + port);
enable_pclock(ID_PIOA + port);
struct gpio_in g = { .regs=digital_regs[port], .bit=GPIO2BIT(pin) };
gpio_in_reset(g, pull_up);
return g;

View File

@ -28,10 +28,7 @@
static void
i2c_init(Twi *p_twi, uint32_t rate)
{
uint32_t twi_id = (p_twi == TWI0) ? ID_TWI0 : ID_TWI1;
if ((PMC->PMC_PCSR0 & (1u << twi_id)) == 0) {
PMC->PMC_PCER0 = 1 << twi_id;
}
enable_pclock(p_twi == TWI0 ? ID_TWI0 : ID_TWI1);
if (p_twi == TWI0) {
gpio_peripheral(TWI0_SCL_GPIO, 'A', 0);
gpio_peripheral(TWI0_SDA_GPIO, 'A', 0);

View File

@ -16,5 +16,7 @@
#define GPIO2BIT(PIN) (1<<((PIN) % 32))
void gpio_peripheral(uint32_t gpio, char ptype, int32_t pull_up);
int is_enabled_pclock(uint32_t id);
void enable_pclock(uint32_t id);
#endif // internal.h

View File

@ -35,6 +35,26 @@ DECL_INIT(watchdog_init);
* misc functions
****************************************************************/
// Check if a peripheral clock has been enabled
int
is_enabled_pclock(uint32_t id)
{
if (id < 32)
return !!(PMC->PMC_PCSR0 & (1 << id));
else
return !!(PMC->PMC_PCSR1 & (1 << (id - 32)));
}
// Enable a peripheral clock
void
enable_pclock(uint32_t id)
{
if (id < 32)
PMC->PMC_PCER0 = 1 << id;
else
PMC->PMC_PCER1 = 1 << (id - 32);
}
void
command_reset(uint32_t *args)
{

View File

@ -57,10 +57,7 @@ static int
init_afec(Afec* afec) {
// Enable PMC
if (afec == AFEC0)
PMC->PMC_PCER0 = 1 << ID_AFEC0;
else
PMC->PMC_PCER0 = 1 << ID_AFEC1;
enable_pclock(afec == AFEC0 ? ID_AFEC0 : ID_AFEC1);
// If busy, return busy
if ((afec->AFE_ISR & AFE_ISR_DRDY) == AFE_ISR_DRDY) {

View File

@ -31,9 +31,7 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate)
// DUET_USART0_MISO as per dc42 CoreNG
gpio_peripheral(GPIO('B', 0), 'C', 1);
if ((PMC->PMC_PCSR0 & (1u << ID_USART0)) == 0) {
PMC->PMC_PCER0 = 1 << ID_USART0;
}
enable_pclock(ID_USART0);
p_usart = USART0;
} else if (bus == SSPI_USART1) {
// DUET_USART1_SCK as per dc42 CoreNG
@ -43,9 +41,7 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate)
// DUET_USART1_MISO as per dc42 CoreNG
gpio_peripheral(GPIO('A', 21), 'A', 1);
if ((PMC->PMC_PCSR0 & (1u << ID_USART1)) == 0) {
PMC->PMC_PCER0 = 1 << ID_USART1;
}
enable_pclock(ID_USART1);
p_usart = USART1;
}

View File

@ -31,7 +31,7 @@ serial_init(void)
gpio_peripheral(tx_pin, 'A', 0);
// Reset uart
PMC->PMC_PCER0 = 1 << Pmc_id;
enable_pclock(Pmc_id);
Port->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
Port->UART_CR = (UART_CR_RSTRX | UART_CR_RSTTX
| UART_CR_RXDIS | UART_CR_TXDIS);

View File

@ -23,9 +23,7 @@ spi_init(void)
gpio_peripheral(GPIO('A', 27), 'A', 0); // Arduino 76
// Enable SPI clocks
if (!(PMC->PMC_PCSR0 & (1u << PERIPH_ID))) {
PMC->PMC_PCER0 = (1 << PERIPH_ID);
}
enable_pclock(PERIPH_ID);
/* Disable SPI */
REGPTR->SPI_CR = SPI_CR_SPIDIS;

View File

@ -41,7 +41,7 @@ timer_init(void)
tc->TC_CCR = TC_CCR_CLKDIS;
tc->TC_IDR = 0xFFFFFFFF;
// Enable it
PMC->PMC_PCER0 = 1 << ID_TC0;
enable_pclock(ID_TC0);
tc->TC_CMR = TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1;
tc->TC_IER = TC_IER_CPAS;
NVIC_SetPriority(TC0_IRQn, 1);