Split sam3x/sam3n support.

This commit is contained in:
Gareth McMullin
2013-05-19 19:08:42 +12:00
committed by Piotr Esden-Tempski
parent 4535a4c9b6
commit ce3e3dc39b
24 changed files with 295 additions and 36 deletions

62
lib/sam/common/gpio.c Normal file
View File

@@ -0,0 +1,62 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2012 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopencm3/sam/gpio.h>
void gpio_init(u32 port, u32 pins, enum gpio_flags flags)
{
switch (flags & 3) {
case GPIO_FLAG_GPINPUT:
/* input mode doesn't really exist, so we make a high
* output in open-drain mode
*/
PIO_SODR(port) = pins;
flags |= GPIO_FLAG_OPEN_DRAIN;
/* fall through */
case GPIO_FLAG_GPOUTPUT:
PIO_OER(port) = pins;
PIO_PER(port) = pins;
break;
case GPIO_FLAG_PERIPHA:
PIO_ABSR(port) &= ~pins;
PIO_PDR(port) = pins;
break;
case GPIO_FLAG_PERIPHB:
PIO_ABSR(port) |= pins;
PIO_PDR(port) = pins;
}
if (flags & GPIO_FLAG_OPEN_DRAIN)
PIO_MDER(port) = pins;
else
PIO_MDDR(port) = pins;
if (flags & GPIO_FLAG_PULL_UP)
PIO_PUER(port) = pins;
else
PIO_PUDR(port) = pins;
}
void gpio_toggle(u32 gpioport, u32 gpios)
{
u32 odsr = PIO_ODSR(gpioport);
PIO_CODR(gpioport) = odsr & gpios;
PIO_SODR(gpioport) = ~odsr & gpios;
}

81
lib/sam/common/pmc.c Normal file
View File

@@ -0,0 +1,81 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2013 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopencm3/sam/pmc.h>
#include <libopencm3/sam/eefc.h>
/** Default peripheral clock frequency after reset. */
u32 pmc_mck_frequency = 4000000;
void pmc_xtal_enable(bool en, u8 startup_time)
{
if (en) {
CKGR_MOR = (CKGR_MOR & ~CKGR_MOR_MOSCXTST_MASK) |
CKGR_MOR_KEY | CKGR_MOR_MOSCXTEN |
(startup_time << 8);
while (!(PMC_SR & PMC_SR_MOSCXTS));
} else {
CKGR_MOR = CKGR_MOR_KEY | (CKGR_MOR & ~CKGR_MOR_MOSCXTEN);
}
}
void pmc_plla_config(u8 mul, u8 div)
{
CKGR_PLLAR = CKGR_PLLAR_ONE | ((mul - 1) << 16) |
CKGR_PLLAR_PLLACOUNT_MASK | div;
while (!(PMC_SR & PMC_SR_LOCKA));
}
void pmc_peripheral_clock_enable(u8 pid)
{
if (pid < 32)
PMC_PCER0 = 1 << pid;
else
PMC_PCER1 = 1 << (pid & 31);
}
void pmc_peripheral_clock_disable(u8 pid)
{
if (pid < 32)
PMC_PCDR0 = 1 << pid;
else
PMC_PCDR1 = 1 << (pid & 31);
}
void pmc_mck_set_source(enum mck_src src)
{
PMC_MCKR = (PMC_MCKR & ~PMC_MCKR_CSS_MASK) | src;
while (!(PMC_SR & PMC_SR_MCKRDY));
}
void pmc_clock_setup_in_xtal_12mhz_out_84mhz(void)
{
eefc_set_latency(4);
/* 12MHz external xtal, maximum possible startup time */
pmc_xtal_enable(true, 0xff);
/* Select as main oscillator */
CKGR_MOR |= CKGR_MOR_KEY | CKGR_MOR_MOSCSEL;
/* Multiply by 7 for 84MHz */
pmc_plla_config(7, 1);
pmc_mck_set_source(MCK_SRC_PLLA);
pmc_mck_frequency = 84000000;
}

109
lib/sam/common/usart.c Normal file
View File

@@ -0,0 +1,109 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2013 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopencm3/sam/usart.h>
#include <libopencm3/sam/pmc.h>
void usart_set_baudrate(u32 usart, u32 baud)
{
USART_BRGR(usart) = pmc_mck_frequency / (16 * baud);
}
void usart_set_databits(u32 usart, int bits)
{
USART_MR(usart) = (USART_MR(usart) & ~USART_MR_CHRL_MASK) |
((bits - 5) << 6);
}
void usart_set_stopbits(u32 usart, enum usart_stopbits sb)
{
USART_MR(usart) = (USART_MR(usart) & ~USART_MR_NBSTOP_MASK) |
(sb << 12);
}
void usart_set_parity(u32 usart, enum usart_parity par)
{
USART_MR(usart) = (USART_MR(usart) & ~USART_MR_PAR_MASK) | (par << 9);
}
void usart_set_mode(u32 usart, enum usart_mode mode)
{
USART_CR(usart) =
(mode & USART_MODE_RX) ? USART_CR_RXEN : USART_CR_RXDIS;
USART_CR(usart) =
(mode & USART_MODE_TX) ? USART_CR_TXEN : USART_CR_TXDIS;
}
void usart_set_flow_control(u32 usart, enum usart_flowcontrol fc)
{
USART_MR(usart) = (USART_MR(usart) & ~USART_MR_MODE_MASK) |
(fc ? USART_MR_MODE_HW_HANDSHAKING : 0);
}
void usart_enable(u32 usart)
{
}
void usart_disable(u32 usart)
{
}
void usart_send(u32 usart, u16 data)
{
USART_THR(usart) = data;
}
u16 usart_recv(u32 usart)
{
return USART_RHR(usart) & 0x1f;
}
void usart_wait_send_ready(u32 usart)
{
while ((USART_CSR(usart) & USART_CSR_TXRDY) == 0);
}
void usart_wait_recv_ready(u32 usart)
{
while ((USART_CSR(usart) & USART_CSR_RXRDY) == 0);
}
void usart_send_blocking(u32 usart, u16 data)
{
usart_wait_send_ready(usart);
usart_send(usart, data);
}
u16 usart_recv_blocking(u32 usart)
{
usart_wait_recv_ready(usart);
return usart_recv(usart);
}
void usart_enable_rx_interrupt(u32 usart)
{
USART_IER(usart) = USART_CSR_RXRDY;
}
void usart_disable_rx_interrupt(u32 usart)
{
USART_IDR(usart) = USART_CSR_RXRDY;
}