git subrepo clone https://github.com/libopencm3/libopencm3.git
subrepo: subdir: "libopencm3" merged: "88e91c9a7cce" upstream: origin: "https://github.com/libopencm3/libopencm3.git" branch: "master" commit: "88e91c9a7cce" git-subrepo: version: "0.4.3" origin: "???" commit: "???"
This commit is contained in:
50
libopencm3/lib/nrf/51/Makefile
Normal file
50
libopencm3/lib/nrf/51/Makefile
Normal file
@@ -0,0 +1,50 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
## Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
## Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
##
|
||||
## 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/>.
|
||||
##
|
||||
|
||||
LIBNAME = libopencm3_nrf51
|
||||
SRCLIBDIR ?= ../..
|
||||
|
||||
CC = $(PREFIX)gcc
|
||||
AR = $(PREFIX)ar
|
||||
|
||||
FP_FLAGS ?= -msoft-float
|
||||
|
||||
TGT_CFLAGS = -Os -Wall -Wextra -I../../../include -fno-common \
|
||||
-mcpu=cortex-m0 -mthumb $(FP_FLAGS) \
|
||||
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
|
||||
-ffunction-sections -fdata-sections -MD -DNRF51
|
||||
|
||||
TGT_CFLAGS += $(DEBUG_FLAGS)
|
||||
TGT_CFLAGS += $(STANDARD_FLAGS)
|
||||
# ARFLAGS = rcsv
|
||||
ARFLAGS = rcs
|
||||
|
||||
OBJS += clock_common.o clock.o
|
||||
OBJS += gpio.o
|
||||
OBJS += i2c_common.o
|
||||
OBJS += ppi.o
|
||||
OBJS += rtc.o
|
||||
OBJS += radio_common.o ./radio.o
|
||||
OBJS += timer.o
|
||||
OBJS += uart.o
|
||||
|
||||
VPATH += ../../cm3:../common
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
45
libopencm3/lib/nrf/51/clock.c
Normal file
45
libopencm3/lib/nrf/51/clock.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/** @addtogroup clock_file CLOCK peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the NRF51 Clock Controller </b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Roel Postelmans
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the unicore-mx project.
|
||||
*
|
||||
* 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/nrf/clock.h>
|
||||
#include <libopencm3/nrf/periph.h>
|
||||
/**@{*/
|
||||
|
||||
/** @brief Select nominal frequency of external crystal for HFCLK.
|
||||
*
|
||||
* @details This register has to match the actual crystal used in design to
|
||||
* enable correct behaviour.
|
||||
*
|
||||
* @param[in] freq enum clock_xtal_freq
|
||||
* */
|
||||
void clock_set_xtal_freq(enum clock_xtal_freq freq)
|
||||
{
|
||||
CLOCK_XTALFREQ = freq;
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
66
libopencm3/lib/nrf/51/radio.c
Normal file
66
libopencm3/lib/nrf/51/radio.c
Normal file
@@ -0,0 +1,66 @@
|
||||
/** @addtogroup radio_file RADIO peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the NRF51 2.4 GHz Radio </b>
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Maxim Sloyko <maxims@google.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the unicore-mx project.
|
||||
*
|
||||
* 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/nrf/ficr.h>
|
||||
#include <libopencm3/nrf/radio.h>
|
||||
|
||||
/**@{*/
|
||||
|
||||
/** @brief Set radio mode.
|
||||
*
|
||||
* @details The function also performs all required overrides for BLE and NRF mode.
|
||||
*
|
||||
* @param[in] mode the new mode.
|
||||
* */
|
||||
void radio_set_mode(enum radio_mode mode)
|
||||
{
|
||||
/* This is alias to memory register, thus volatile */
|
||||
volatile uint32_t *override_pos = 0;
|
||||
if ((RADIO_MODE_BLE_1MBIT == mode)
|
||||
&& (FICR_OVERRIDEEN & ~FICR_OVERRIDEEN_BLE_1MBIT)) {
|
||||
/* Need to use Override */
|
||||
override_pos = &FICR_BLE_1MBIT0;
|
||||
} else if ((RADIO_MODE_NRF_1MBIT == mode)
|
||||
&& (FICR_OVERRIDEEN & ~FICR_OVERRIDEEN_NRF_1MBIT)) {
|
||||
override_pos = &FICR_NRF_1MBIT0;
|
||||
}
|
||||
|
||||
if (override_pos) {
|
||||
uint8_t i;
|
||||
for (i = 0; i <= 4; ++i, ++override_pos) {
|
||||
RADIO_OVERRIDE(i) = *override_pos;
|
||||
}
|
||||
|
||||
RADIO_OVERRIDE(4) |= RADIO_OVERRIDE4_ENABLE;
|
||||
} else {
|
||||
RADIO_OVERRIDE(4) &= ~RADIO_OVERRIDE4_ENABLE;
|
||||
}
|
||||
|
||||
RADIO_MODE = mode;
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
50
libopencm3/lib/nrf/52/Makefile
Normal file
50
libopencm3/lib/nrf/52/Makefile
Normal file
@@ -0,0 +1,50 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
## Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
##
|
||||
## 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/>.
|
||||
##
|
||||
|
||||
LIBNAME = libopencm3_nrf52
|
||||
SRCLIBDIR ?= ../..
|
||||
|
||||
CC = $(PREFIX)gcc
|
||||
AR = $(PREFIX)ar
|
||||
|
||||
FP_FLAGS ?= -mfloat-abi=hard -mfpu=fpv4-sp-d16
|
||||
|
||||
TGT_CFLAGS = -Os -Wall -Wextra -I../../../include -fno-common \
|
||||
-mcpu=cortex-m4 -mthumb $(FP_FLAGS) \
|
||||
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
|
||||
-ffunction-sections -fdata-sections -MD -DNRF52
|
||||
TGT_CFLAGS += $(DEBUG_FLAGS)
|
||||
TGT_CFLAGS += $(STANDARD_FLAGS)
|
||||
# ARFLAGS = rcsv
|
||||
ARFLAGS = rcs
|
||||
|
||||
OBJS += clock_common.o
|
||||
OBJS += gpio.o
|
||||
OBJS += i2c.o i2c_common.o
|
||||
OBJS += ppi.o
|
||||
OBJS += radio_common.o
|
||||
OBJS += rtc.o
|
||||
OBJS += timer.o
|
||||
OBJS += uart.o
|
||||
|
||||
VPATH += ../../cm3:../common
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
62
libopencm3/lib/nrf/52/i2c.c
Normal file
62
libopencm3/lib/nrf/52/i2c.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/** @addtogroup i2c_file I2C peripheral API
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2022 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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/nrf/52/i2c.h>
|
||||
|
||||
/**@{*/
|
||||
|
||||
/** Configure I2C transmit buffer properties
|
||||
*
|
||||
* Configures transmit buffer for EasyDMA transaction. This API
|
||||
* is only available if @ref I2C_MODE_MASTER mode is activated.
|
||||
*
|
||||
* Configures linear TX buffer for EasyDMA transmission.
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @param[in] buffer address of buffer start
|
||||
* @param[in] len length of data in the buffer
|
||||
*/
|
||||
void i2c_set_tx_buffer(uint32_t i2c, const uint8_t *buffer, uint8_t len)
|
||||
{
|
||||
I2C_TXDPTR(i2c) = (uint32_t) buffer;
|
||||
I2C_TXDMAXCNT(i2c) = len;
|
||||
I2C_TXDLIST(i2c) = 0;
|
||||
}
|
||||
|
||||
/** Configure I2C receive buffer properties
|
||||
*
|
||||
* Configures receive buffer for EasyDMA transaction. This API
|
||||
* is only available if @ref I2C_MODE_MASTER mode is activated.
|
||||
*
|
||||
* Configures linear RX buffer for EasyDMA transmission.
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @param[in] buffer address of buffer start
|
||||
* @param[in] len length of the buffer
|
||||
*/
|
||||
void i2c_set_rx_buffer(uint32_t i2c, uint8_t *buffer, uint8_t len)
|
||||
{
|
||||
I2C_RXDPTR(i2c) = (uint32_t) buffer;
|
||||
I2C_RXDMAXCNT(i2c) = len;
|
||||
I2C_RXDLIST(i2c) = 0;
|
||||
}
|
||||
|
||||
|
||||
/** @} */
|
||||
83
libopencm3/lib/nrf/common/clock_common.c
Normal file
83
libopencm3/lib/nrf/common/clock_common.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/** @addtogroup clock_file CLOCK peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the Clock Controller</b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016 Maxim Sloyko <maxims@google.com>
|
||||
* @author @htmlonly © @endhtmlonly 2021 Eduard Drusa <ventyl86 at netkosice dot sk>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
* Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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/nrf/clock.h>
|
||||
#include <libopencm3/nrf/periph.h>
|
||||
/**@{*/
|
||||
|
||||
/** @brief Start Low Frequency Clock
|
||||
*
|
||||
* @param[in] wait bool: If true, will busy wait for the clock to start.
|
||||
*/
|
||||
void clock_start_lfclk(bool wait)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(CLOCK_TASK_LFCLKSTART);
|
||||
if (wait) {
|
||||
while (!(CLOCK_LFCLKSTAT & CLOCK_LFCLKSTAT_STATE));
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Stop Low Frequency Clock */
|
||||
void clock_stop_lfclk()
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(CLOCK_TASK_LFCLKSTOP);
|
||||
}
|
||||
|
||||
/** @brief Start High Frequency Crystal Oscillator.
|
||||
*
|
||||
* @details Oscillator needs to be running for the radio to work.
|
||||
*
|
||||
* @param[in] wait bool If true, will busy wait for the clock to start.
|
||||
*/
|
||||
void clock_start_hfclk(bool wait)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(CLOCK_TASK_HFCLKSTART);
|
||||
if (wait) {
|
||||
while (!(CLOCK_HFCLKSTAT & CLOCK_HFCLKSTAT_STATE));
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Stop High Frequency Crystal Oscillator */
|
||||
void clock_stop_hfclk()
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(CLOCK_TASK_HFCLKSTOP);
|
||||
}
|
||||
|
||||
/** @brief Low Frequency Clock Source.
|
||||
*
|
||||
* @param[in] lfclk_src enum clock_lfclk_src
|
||||
*/
|
||||
void clock_set_lfclk_src(enum clock_lfclk_src lfclk_src)
|
||||
{
|
||||
CLOCK_LFCLKSRC = lfclk_src;
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
208
libopencm3/lib/nrf/common/gpio.c
Normal file
208
libopencm3/lib/nrf/common/gpio.c
Normal file
@@ -0,0 +1,208 @@
|
||||
/** @addtogroup gpio_file GPIO peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the I/O Controller</b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Maxim Sloyko <maxims@google.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
* Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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/nrf/gpio.h>
|
||||
|
||||
/** @brief Atomic set output
|
||||
*
|
||||
* @param[in] gpioport Port identifier @ref gpio_port_id
|
||||
* @param[in] gpios Pin identifiers @ref gpio_pin_id
|
||||
*/
|
||||
void gpio_set(uint32_t gpioport, uint32_t gpios)
|
||||
{
|
||||
(void) gpioport;
|
||||
GPIO_OUTSET = gpios;
|
||||
}
|
||||
|
||||
/** @brief Atomic clear output
|
||||
*
|
||||
* @param[in] gpioport Port identifier @ref gpio_port_id
|
||||
* @param[in] gpios Pin identifiers @ref gpio_pin_id
|
||||
*/
|
||||
void gpio_clear(uint32_t gpioport, uint32_t gpios)
|
||||
{
|
||||
(void) gpioport;
|
||||
GPIO_OUTCLR = gpios;
|
||||
}
|
||||
|
||||
/** @brief Toggle output
|
||||
*
|
||||
* @param[in] gpioport Port identifier @ref gpio_port_id
|
||||
* @param[in] gpios Pin identifiers @ref gpio_pin_id
|
||||
*/
|
||||
void gpio_toggle(uint32_t gpioport, uint32_t gpios)
|
||||
{
|
||||
(void) gpioport;
|
||||
uint32_t reg_val = GPIO_OUT;
|
||||
GPIO_OUTCLR = reg_val & gpios;
|
||||
GPIO_OUTSET = (~reg_val) & gpios;
|
||||
}
|
||||
|
||||
/** @brief Read GPIO values
|
||||
*
|
||||
* @param[in] gpioport Port identifier @ref gpio_port_id
|
||||
* @param[in] gpios Pin identifiers @ref gpio_pin_id
|
||||
*/
|
||||
uint32_t gpio_get(uint32_t gpioport, uint32_t gpios)
|
||||
{
|
||||
(void) gpioport;
|
||||
return GPIO_IN & gpios;
|
||||
}
|
||||
|
||||
/** @brief Set GPIO Pin Mode
|
||||
*
|
||||
* Sets the mode (input/output) and configuration (analog/digitial and
|
||||
* open drain/push pull), for a set of GPIO pins on a given GPIO port.
|
||||
*
|
||||
* @param[in] gpioport Port identifier @ref gpio_port_id
|
||||
* @param[in] mode Pin mode @ref gpio_mode
|
||||
* @param[in] pull_up_down Pull up / pull down configuration @ref gpio_pupd
|
||||
* @param[in] gpios Pin identifiers @ref gpio_pin_id
|
||||
* If multiple pins are to be set, use bitwise OR '|' to separate
|
||||
* them.
|
||||
*/
|
||||
void gpio_mode_setup(uint32_t gpioport, uint32_t mode, uint32_t pull_up_down,
|
||||
uint32_t gpios)
|
||||
{
|
||||
(void) gpioport;
|
||||
|
||||
uint8_t i = 0;
|
||||
while (gpios) {
|
||||
if (gpios & 1) {
|
||||
GPIO_PIN_CNF(i) = (
|
||||
GPIO_PIN_CNF(i) &
|
||||
~((GPIO_CNF_MODE_MASK << GPIO_CNF_MODE_SHIFT)
|
||||
| (GPIO_CNF_PUPD_MASK << GPIO_CNF_PUPD_SHIFT)
|
||||
)
|
||||
) | (mode << GPIO_CNF_MODE_SHIFT)
|
||||
| (pull_up_down << GPIO_CNF_PUPD_SHIFT);
|
||||
}
|
||||
++i;
|
||||
gpios >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/** Configure GPIO pin input and output specifics.
|
||||
*
|
||||
* Configure drive strength and input sensing for given GPIO port.
|
||||
* @param [in] gpioport GPIO port identifier, see @ref gpio_port_id
|
||||
* @param [in] drive Drive schema used to drive pin, see @ref gpio_drive
|
||||
* @param [in] sense Pin sensing mechanism, see @ref gpio_sense
|
||||
* @param[in] gpios Pin identifiers @ref gpio_pin_id
|
||||
* If multiple pins are to be set, use bitwise OR '|' to separate
|
||||
* them.
|
||||
*/
|
||||
void gpio_set_options(uint32_t gpioport, uint32_t drive, uint32_t sense,
|
||||
uint32_t gpios)
|
||||
{
|
||||
(void) gpioport;
|
||||
|
||||
uint8_t i = 0;
|
||||
while (gpios) {
|
||||
if (gpios & 1) {
|
||||
GPIO_PIN_CNF(i) = (GPIO_PIN_CNF(i) &
|
||||
~((GPIO_CNF_DRIVE_MASK << GPIO_CNF_DRIVE_SHIFT)
|
||||
| (GPIO_CNF_SENSE_MASK << GPIO_CNF_SENSE_SHIFT)
|
||||
)
|
||||
) | (drive << GPIO_CNF_DRIVE_SHIFT)
|
||||
| (sense << GPIO_CNF_SENSE_SHIFT);
|
||||
}
|
||||
++i;
|
||||
gpios >>= 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** @brief Configure Task in GPIO TE Module
|
||||
*
|
||||
* @param[in] task_num uint8_t Task number (0-3)
|
||||
* @param[in] pin_num uint8_t GPIO Pin number (0-31)
|
||||
* @param[in] polarity uint8_t polarity Operation to perform when task is triggered.
|
||||
* @param[in] init uint8_t Initial state of the pin, non-zero means initially active,
|
||||
* zero means initially inactive
|
||||
*/
|
||||
void gpio_configure_task(uint8_t task_num,
|
||||
uint8_t pin_num, uint8_t polarity, uint32_t init)
|
||||
{
|
||||
/* any non-zero value means, that pin is active */
|
||||
if (init) {
|
||||
init = GPIO_TE_CONFIG_OUTINIT;
|
||||
}
|
||||
|
||||
GPIO_TE_CONFIG(task_num) = (GPIO_TE_MODE_TASK << GPIO_TE_CONFIG_MODE_SHIFT)
|
||||
| (pin_num << GPIO_TE_CONFIG_PSEL_SHIFT)
|
||||
| (polarity << GPIO_TE_CONFIG_POLARITY_SHIFT)
|
||||
| init;
|
||||
}
|
||||
|
||||
/** @brief Configure Event in GPIO TE Module
|
||||
*
|
||||
* @param[in] event_num Event number (0-3)
|
||||
* @param[in] pin_num GPIO Pin number (0-31)
|
||||
* @param[in] polarity Operation to perform when task is triggered.
|
||||
*/
|
||||
void gpio_configure_event(uint8_t event_num, uint8_t pin_num, uint8_t polarity)
|
||||
{
|
||||
GPIO_TE_CONFIG(event_num) = (GPIO_TE_MODE_EVENT << GPIO_TE_CONFIG_MODE_SHIFT)
|
||||
| (pin_num << GPIO_TE_CONFIG_PSEL_SHIFT)
|
||||
| (polarity << GPIO_TE_CONFIG_POLARITY_SHIFT);
|
||||
}
|
||||
|
||||
/** @brief Enable GPIO interrupts
|
||||
*
|
||||
* @param[in] mask interrupts to enable.
|
||||
*/
|
||||
void gpio_enable_interrupts(uint32_t mask)
|
||||
{
|
||||
GPIO_INTENSET = mask;
|
||||
}
|
||||
|
||||
/** @brief Disable GPIO interrupts
|
||||
*
|
||||
* @param[in] mask interrupts to disable.
|
||||
*/
|
||||
void gpio_disable_interrupts(uint32_t mask)
|
||||
{
|
||||
GPIO_INTENCLR = mask;
|
||||
}
|
||||
|
||||
/** @brief Disable all GPIO interrupts
|
||||
*
|
||||
*/
|
||||
void gpio_clear_interrupts(void)
|
||||
{
|
||||
GPIO_INTENCLR = 0xffffffff;
|
||||
}
|
||||
|
||||
/** @} */
|
||||
206
libopencm3/lib/nrf/common/i2c_common.c
Normal file
206
libopencm3/lib/nrf/common/i2c_common.c
Normal file
@@ -0,0 +1,206 @@
|
||||
/** @addtogroup i2c_file I2C peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the I2C Controller</b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Maxim Sloyko <maxims@google.com>
|
||||
* @author @htmlonly © @endhtmlonly 2021 - 2022
|
||||
* Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
* Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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/nrf/i2c.h>
|
||||
|
||||
/**@{*/
|
||||
|
||||
/** @brief Enable I2C peripheral
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @param[in] mode i2c @ref i2c_mode
|
||||
*/
|
||||
void i2c_enable(uint32_t i2c, uint32_t mode)
|
||||
{
|
||||
I2C_ENABLE(i2c) = mode;
|
||||
}
|
||||
|
||||
/** @brief Disable I2C peripheral
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
*/
|
||||
void i2c_disable(uint32_t i2c)
|
||||
{
|
||||
I2C_ENABLE(i2c) = 0;
|
||||
}
|
||||
|
||||
/** @brief Start I2C transmission.
|
||||
*
|
||||
* Starts STARTTX task, which generates start condition on I2C bus and
|
||||
* transmits address previously configured by @ref i2c_set_address.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block.
|
||||
*/
|
||||
void i2c_start_tx(uint32_t i2c)
|
||||
{
|
||||
I2C_TASK_STARTTX(i2c) = 1;
|
||||
}
|
||||
|
||||
/** @brief Start I2C reception.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block.
|
||||
*/
|
||||
void i2c_start_rx(uint32_t i2c)
|
||||
{
|
||||
I2C_TASK_STARTRX(i2c) = 1;
|
||||
}
|
||||
|
||||
/** @brief Signal stop on I2C line.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block.
|
||||
*/
|
||||
void i2c_send_stop(uint32_t i2c)
|
||||
{
|
||||
I2C_TASK_STOP(i2c) = 1;
|
||||
}
|
||||
|
||||
/** @brief Select Fast (400kHz) mode.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block.
|
||||
*/
|
||||
void i2c_set_fast_mode(uint32_t i2c)
|
||||
{
|
||||
I2C_FREQUENCY(i2c) = I2C_FREQUENCY_400K;
|
||||
}
|
||||
|
||||
/** @brief Select Standard (100kHz) mode.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block.
|
||||
*/
|
||||
void i2c_set_standard_mode(uint32_t i2c)
|
||||
{
|
||||
I2C_FREQUENCY(i2c) = I2C_FREQUENCY_100K;
|
||||
}
|
||||
|
||||
/** @brief Set I2C frequency.
|
||||
*
|
||||
* In addition to Standard (100kHz) and Fast (400kHz) modes
|
||||
* this peripheral also supports 250kHz mode.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @param[in] freq frequency constant. See @ref i2c_freq_const for details
|
||||
* and note that this is not actually a frequency in Hz or kHz.
|
||||
*/
|
||||
void i2c_set_frequency(uint32_t i2c, uint32_t freq)
|
||||
{
|
||||
I2C_FREQUENCY(i2c) = freq;
|
||||
}
|
||||
|
||||
/** @brief Write Data to TXD register to be sent.
|
||||
*
|
||||
* Writes one byte into transmission buffer. This API is only
|
||||
* available if @ref I2C_MODE_LEGACY is activated.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @param[in] data byte to send next.
|
||||
*/
|
||||
void i2c_send_data(uint32_t i2c, uint8_t data)
|
||||
{
|
||||
I2C_TXD(i2c) = data;
|
||||
}
|
||||
|
||||
/** @brief Read Data from RXD register.
|
||||
*
|
||||
* Reads one byte from reception buffer. This API is only
|
||||
* available if @ref I2C_MODE_LEGACY is activated.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @returns data from RXD register.
|
||||
*/
|
||||
uint8_t i2c_get_data(uint32_t i2c)
|
||||
{
|
||||
return (uint8_t)I2C_RXD(i2c);
|
||||
}
|
||||
|
||||
/** @brief Select GPIO pins to be used by this peripheral.
|
||||
*
|
||||
* Configures GPIO pins assigned to SCL and SDA signals. These pins are only occupied
|
||||
* by I2C peripheral whenever it is enabled using @ref i2c_enable. It is possible to
|
||||
* ignore any given signal and not map it to pin by using special value of
|
||||
* @ref GPIO_UNCONNECTED instead of @ref gpio_pin_id values.
|
||||
*
|
||||
* This needs to be configured when no transaction is in progress.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @param[in] scl_pin GPIO pin used for SCL signal
|
||||
* @param[in] sda_pin GPIO pin used for SDA signal
|
||||
*/
|
||||
void i2c_select_pins(uint32_t i2c, uint32_t scl_pin, uint32_t sda_pin)
|
||||
{
|
||||
if (scl_pin != GPIO_UNCONNECTED) {
|
||||
I2C_PSELSCL(i2c) = __GPIO2PIN(scl_pin);
|
||||
} else {
|
||||
I2C_PSELSCL(i2c) = scl_pin;
|
||||
}
|
||||
|
||||
if (sda_pin != GPIO_UNCONNECTED) {
|
||||
I2C_PSELSDA(i2c) = __GPIO2PIN(sda_pin);
|
||||
} else {
|
||||
I2C_PSELSDA(i2c) = sda_pin;
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Set 7bit I2C address of the device you wish to communicate with.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @param[in] addr device address (7bit).
|
||||
*/
|
||||
void i2c_set_address(uint32_t i2c, uint8_t addr)
|
||||
{
|
||||
I2C_ADDRESS(i2c) = addr;
|
||||
}
|
||||
|
||||
/** @brief Resume I2C transaction.
|
||||
*
|
||||
* This function is unusual, but required to implement
|
||||
* i2c exchange with this peripheral.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
*/
|
||||
void i2c_resume(uint32_t i2c)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(I2C_TASK_RESUME(i2c));
|
||||
}
|
||||
|
||||
/** Configure event -> task shortcuts
|
||||
*
|
||||
* Sets new shortcut configuration bitmask for I2C peripheral.
|
||||
*
|
||||
* @param[in] i2c i2c peripheral base, see @ref i2c_block
|
||||
* @param[in] shorts @ref i2c_shorts activated
|
||||
*/
|
||||
void i2c_set_shorts(uint32_t i2c, uint32_t shorts)
|
||||
{
|
||||
I2C_SHORTS(i2c) = shorts;
|
||||
}
|
||||
/**@}*/
|
||||
145
libopencm3/lib/nrf/common/ppi.c
Normal file
145
libopencm3/lib/nrf/common/ppi.c
Normal file
@@ -0,0 +1,145 @@
|
||||
/** @addtogroup ppi_file PPI peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the Programmable Peripheral Interconnect </b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Maxim Sloyko <maxims@google.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
* Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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 <stdint.h>
|
||||
|
||||
#include <libopencm3/nrf/ppi.h>
|
||||
/**@{*/
|
||||
|
||||
/** @brief Configure PPI Channel.
|
||||
*
|
||||
* @param[in] chan_num uint8_t Channel number (0-15).
|
||||
* @param[in] eep uint32_t Event endpoint. Memory address of the event endpoint.
|
||||
* @param[in] tep uint32_t Task endpoint. Memory address of the task endpoint.
|
||||
*/
|
||||
void ppi_configure_channel(uint8_t chan_num, uint32_t eep, uint32_t tep)
|
||||
{
|
||||
PPI_CH_EEP(chan_num) = eep;
|
||||
PPI_CH_TEP(chan_num) = tep;
|
||||
}
|
||||
|
||||
/** @brief Enable PPI channels, given the channels mask.
|
||||
*
|
||||
* @param[in] channels uint32_t mask of the channels to enable.
|
||||
*/
|
||||
void ppi_enable_channels(uint32_t channels)
|
||||
{
|
||||
PPI_CHENSET = channels;
|
||||
}
|
||||
|
||||
/** @brief Disable PPI channels, given the channels mask.
|
||||
*
|
||||
* @param[in] channels uint32_t mask of the channels to disable.
|
||||
*/
|
||||
void ppi_disable_channels(uint32_t channels)
|
||||
{
|
||||
PPI_CHENCLR = channels;
|
||||
}
|
||||
|
||||
/** @brief Set channels group, given channels mask.
|
||||
*
|
||||
* @param[in] group uint8_t group number (0-3)
|
||||
* @param[in] channels uint32_t mask of the channels to group together.
|
||||
*/
|
||||
void ppi_set_group(uint8_t group, uint32_t channels)
|
||||
{
|
||||
PPI_CHG(group) = channels;
|
||||
}
|
||||
|
||||
/** @brief Enable previously configured group of channels.
|
||||
*
|
||||
* @param[in] group uint8_t group number (0-3)
|
||||
*/
|
||||
void ppi_enable_group(uint8_t group)
|
||||
{
|
||||
PPI_TASK_CHG_EN(group) = 1;
|
||||
}
|
||||
|
||||
/** @brief Disable previously configured group of channels.
|
||||
*
|
||||
* @param[in] group uint8_t group number (0-3)
|
||||
*/
|
||||
void ppi_disable_group(uint8_t group)
|
||||
{
|
||||
PPI_TASK_CHG_DIS(group) = 1;
|
||||
}
|
||||
|
||||
/** @brief Configure new channel.
|
||||
*
|
||||
* This is the alternative API, which requires the caller to store the mask of used channels.
|
||||
*
|
||||
* @param chan_map uint32_t* The mask of channels that are already in use.
|
||||
* For the first call initialize with zero and pass in.
|
||||
* @param[in] eep uint32_t Event endpoint.
|
||||
* @param[in] tep uint32_t Task endpoint.
|
||||
* @param enable bool If true, enable the channel immediately.
|
||||
* @return The number of the new channel. If there are no channels available, returns 0xff.
|
||||
*/
|
||||
uint8_t ppi_add_channel(uint32_t *chan_map, uint32_t eep, uint32_t tep, bool enable)
|
||||
{
|
||||
/* Find a free channel */
|
||||
uint8_t i;
|
||||
uint32_t chan_bit;
|
||||
for (i = 0, chan_bit = 1; i <= PPI_MAX_PROG_CHANNEL; ++i, chan_bit <<= 1) {
|
||||
if (!(chan_bit & *chan_map)) {
|
||||
*chan_map |= chan_bit;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* If all channels are taken, return error. */
|
||||
if (i > PPI_MAX_PROG_CHANNEL) {
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
ppi_configure_channel(i, eep, tep);
|
||||
if (enable) {
|
||||
ppi_enable_channels(chan_bit);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/** @brief Disable channel and remove it from the map of used channels.
|
||||
*
|
||||
* This is the alternative API, which requires the caller to store the mask of used channels.
|
||||
*
|
||||
* @param chan_map uint32_t* The mask of channels that are already in use.
|
||||
* For the first call initialize with zero and pass in.
|
||||
* @param[in] chan_num uint8_t the number of the channel to remove from the map.
|
||||
*/
|
||||
void ppi_remove_channel(uint32_t *chan_map, uint8_t chan_num)
|
||||
{
|
||||
ppi_disable_channels(PPI_CH(chan_num));
|
||||
*chan_map &= ~(PPI_CH(chan_num));
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
250
libopencm3/lib/nrf/common/radio_common.c
Normal file
250
libopencm3/lib/nrf/common/radio_common.c
Normal file
@@ -0,0 +1,250 @@
|
||||
/** @addtogroup radio_file RADIO peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the 2.4 GHz Radio </b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Maxim Sloyko <maxims@google.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
* Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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/nrf/ficr.h>
|
||||
#include <libopencm3/nrf/radio.h>
|
||||
|
||||
/**@{*/
|
||||
|
||||
/** @brief Set radio transmission power.
|
||||
*
|
||||
* @details Note, not all supported power levels are BLE compliant.
|
||||
*
|
||||
* @param[in] txpower enum radio_txpower
|
||||
* */
|
||||
void radio_set_txpower(enum radio_txpower txpower)
|
||||
{
|
||||
RADIO_TXPOWER = txpower;
|
||||
}
|
||||
|
||||
/** @brief Set bit transmission order to LSB first. */
|
||||
void radio_set_lsbfirst(void)
|
||||
{
|
||||
RADIO_PCNF1 &= ~RADIO_PCNF1_ENDIAN_BIG;
|
||||
}
|
||||
|
||||
/** @brief Set bit transmission order to MSB first. */
|
||||
void radio_set_msbfirst(void)
|
||||
{
|
||||
RADIO_PCNF1 |= RADIO_PCNF1_ENDIAN_BIG;
|
||||
}
|
||||
|
||||
/** @brief Enable on the air data whitening
|
||||
*
|
||||
* @details the in-memory data will remain unwhitened.
|
||||
* */
|
||||
void radio_enable_whitening(void)
|
||||
{
|
||||
RADIO_PCNF1 |= RADIO_PCNF1_WHITEEN;
|
||||
}
|
||||
|
||||
/** @brief Disable on the air data whitening. */
|
||||
void radio_disable_whitening(void)
|
||||
{
|
||||
RADIO_PCNF1 &= ~RADIO_PCNF1_WHITEEN;
|
||||
}
|
||||
|
||||
/** @brief Set CRC length in number of bytes.
|
||||
*
|
||||
* @param[in] crc_len uint8_t CRC length in number of bytes (1-3), 0 = CRC disabled.
|
||||
*/
|
||||
void radio_set_crclen(uint8_t crc_len)
|
||||
{
|
||||
uint32_t reg_crc = RADIO_CRCCNF;
|
||||
reg_crc &= ~RADIO_CRCCNF_LEN_MASK;
|
||||
RADIO_CRCCNF = reg_crc | RADIO_CRCCNF_LEN_MASKED(crc_len);
|
||||
}
|
||||
|
||||
/** @brief Disable CRC calculation. */
|
||||
void radio_disable_crc(void)
|
||||
{
|
||||
RADIO_CRCCNF &= ~RADIO_CRCCNF_LEN_MASK;
|
||||
}
|
||||
|
||||
/** @brief Enable the peripheral. */
|
||||
void radio_enable(void)
|
||||
{
|
||||
RADIO_POWER = RADIO_POWER_ENABLED;
|
||||
}
|
||||
|
||||
/** @brief Disable the peripheral. */
|
||||
void radio_disable(void)
|
||||
{
|
||||
RADIO_POWER = RADIO_POWER_DISABLED;
|
||||
}
|
||||
|
||||
|
||||
/** @brief Set Base Address length.
|
||||
*
|
||||
* @param[in] ba_len uint8_t Base Address length in number of bytes (2-4).
|
||||
*/
|
||||
void radio_set_balen(uint8_t ba_len)
|
||||
{
|
||||
uint32_t reg_pcnf1 = RADIO_PCNF1;
|
||||
reg_pcnf1 &= ~RADIO_PCNF1_BALEN_MASK;
|
||||
RADIO_PCNF1 = reg_pcnf1 | RADIO_PCNF1_BALEN_MASKED(ba_len);
|
||||
}
|
||||
|
||||
/** @brief Set maximum transmission length in number of bytes.
|
||||
*
|
||||
* @param[in] maxlen uint8_t maximum transmission length.
|
||||
*/
|
||||
void radio_set_maxlen(uint8_t maxlen)
|
||||
{
|
||||
uint32_t reg_pcnf1 = RADIO_PCNF1;
|
||||
reg_pcnf1 &= ~RADIO_PCNF1_MAXLEN_MASK;
|
||||
RADIO_PCNF1 = reg_pcnf1 | RADIO_PCNF1_MAXLEN_MASKED(maxlen);
|
||||
}
|
||||
|
||||
/** @brief Exclude access address from CRC calculation.
|
||||
*
|
||||
* @param[in] is_skip_addr bool If true, CRC will be calculated over PDU only,
|
||||
* if false, it will also include the Access Address.
|
||||
*/
|
||||
void radio_set_crc_skipaddr(bool is_skip_addr)
|
||||
{
|
||||
if (is_skip_addr) {
|
||||
RADIO_CRCCNF |= RADIO_CRCCNF_SKIPADDR;
|
||||
} else {
|
||||
RADIO_CRCCNF &= ~RADIO_CRCCNF_SKIPADDR;
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Configure the radio to be used in BLE mode.
|
||||
*
|
||||
* @details This needs to be called before the radio can be used in BLE mode.
|
||||
* It will set som BLE standard parameters, like Inter-Frame Spacing time,
|
||||
* LSB first, enable whitening, properly configure CRC (for advertising) and address length.
|
||||
*/
|
||||
void radio_configure_ble(void)
|
||||
{
|
||||
#ifdef NRF51
|
||||
radio_set_mode(RADIO_MODE_BLE_1MBIT);
|
||||
#endif
|
||||
RADIO_TIFS = RADIO_BLE_TIFS;
|
||||
radio_set_lsbfirst();
|
||||
radio_enable_whitening();
|
||||
|
||||
radio_set_crclen(RADIO_BLE_CRCLEN);
|
||||
RADIO_CRCPOLY = RADIO_BLE_CRCPOLY;
|
||||
RADIO_CRCINIT = RADIO_BLE_CRCINIT;
|
||||
radio_set_crc_skipaddr(true);
|
||||
radio_set_balen(3);
|
||||
}
|
||||
|
||||
/** @brief Configure the packet.
|
||||
*
|
||||
* @details See the data sheet for details.
|
||||
*/
|
||||
void radio_configure_packet(uint8_t lf_len_bits, uint8_t s0_len_bytes, uint8_t s1_len_bits)
|
||||
{
|
||||
RADIO_PCNF0 = RADIO_PCNF0_LFLEN_MASKED(lf_len_bits)
|
||||
| RADIO_PCNF0_S0LEN_MASKED(s0_len_bytes)
|
||||
| RADIO_PCNF0_S1LEN_MASKED(s1_len_bits);
|
||||
}
|
||||
|
||||
/** @brief Set radio frequency.
|
||||
*
|
||||
* @param[in] freq uint8_t Frequency offset from 2.4GHz in MHz, for example "29" will
|
||||
* tune the radio to 2429MHz
|
||||
*/
|
||||
void radio_set_frequency(uint8_t freq)
|
||||
{
|
||||
RADIO_FREQUENCY = freq;
|
||||
}
|
||||
|
||||
/** @brief Set Data Whitening Initialization Vector.
|
||||
*
|
||||
* @param[in] iv uint8_t Initialization Vector. For BLE, this is channel index.
|
||||
*/
|
||||
void radio_set_datawhiteiv(uint8_t iv)
|
||||
{
|
||||
RADIO_DATAWHITEIV = iv;
|
||||
}
|
||||
|
||||
/* @brief Set Address (base and prefix)
|
||||
*
|
||||
* @details Note that bases are shared between addresses 1-7,
|
||||
* so changing one of them will change others too.
|
||||
*
|
||||
* @param[in] addr_index uint8_t address index (0-7)
|
||||
* @param[in] base uint32_t base part of the address. If balen < 4, appropriate number
|
||||
* of LSBs will be thrown away.
|
||||
* @param[in] prefix uint8_t Address prefix.
|
||||
*/
|
||||
void radio_set_addr(uint8_t addr_index, uint32_t base, uint8_t prefix)
|
||||
{
|
||||
if (addr_index == 0) {
|
||||
RADIO_BASE0 = base;
|
||||
} else {
|
||||
RADIO_BASE1 = base;
|
||||
}
|
||||
|
||||
uint32_t reg_prefix = RADIO_PREFIX_AP(addr_index);
|
||||
reg_prefix &= ~RADIO_PREFIX_AP_MASK(addr_index);
|
||||
RADIO_PREFIX_AP_SET(addr_index, reg_prefix | RADIO_PREFIX_AP_MASKED(addr_index, prefix));
|
||||
}
|
||||
|
||||
/* @brief Set TX address index
|
||||
*
|
||||
* @details The address needs to be previously configured with radio_set_addr()
|
||||
*
|
||||
* @param[in] address_index uint8_t address index (0-7)
|
||||
*/
|
||||
void radio_set_tx_address(uint8_t addr_index)
|
||||
{
|
||||
RADIO_TXADDRESS = addr_index;
|
||||
}
|
||||
|
||||
/* @brief Set pointer for RX/TX data
|
||||
*
|
||||
* @param[in] packet_ptr uint8_t* packet buffer address.
|
||||
*/
|
||||
void radio_set_packet_ptr(uint8_t *packet_ptr)
|
||||
{
|
||||
RADIO_PACKETPTR = (uint32_t)packet_ptr;
|
||||
}
|
||||
|
||||
/* @brief Enable radio Transmitter */
|
||||
void radio_enable_tx(void)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(RADIO_TASK_TXEN);
|
||||
}
|
||||
|
||||
/* @brief Enable radio Receiver */
|
||||
void radio_enable_rx(void)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(RADIO_TASK_RXEN);
|
||||
}
|
||||
|
||||
|
||||
/**@}*/
|
||||
|
||||
116
libopencm3/lib/nrf/common/rtc.c
Normal file
116
libopencm3/lib/nrf/common/rtc.c
Normal file
@@ -0,0 +1,116 @@
|
||||
/** @addtogroup rtc_file RTC peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the Real Time Counter Controller </b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Maxim Sloyko <maxims@google.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
* Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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/nrf/rtc.h>
|
||||
/**@{*/
|
||||
|
||||
/** @brief RTC set Prescaler value.
|
||||
*
|
||||
* @details The clock needs to be stopped for this to have any effect.
|
||||
*
|
||||
* @param[in] rtc uint32_t RTC base
|
||||
* @param[in] presc uint16_t 12 bit prescaler value.
|
||||
*/
|
||||
void rtc_set_prescaler(uint32_t rtc, uint16_t presc)
|
||||
{
|
||||
RTC_PRESCALER(rtc) = presc & 0xfff;
|
||||
}
|
||||
|
||||
/** @brief RTC get Counter value.
|
||||
*
|
||||
* @param[in] rtc uint32_t RTC base
|
||||
*/
|
||||
uint32_t rtc_get_counter(uint32_t rtc)
|
||||
{
|
||||
return RTC_COUNTER(rtc);
|
||||
}
|
||||
|
||||
/** @brief Enable events
|
||||
*
|
||||
* @param[in] rtc uint32_t RTC base
|
||||
* @param[in] mask uint32_t which events to enable
|
||||
*/
|
||||
void rtc_enable_events(uint32_t rtc, uint32_t mask)
|
||||
{
|
||||
RTC_EVTENSET(rtc) = mask;
|
||||
}
|
||||
|
||||
/** @brief Disable events
|
||||
*
|
||||
* @param[in] rtc uint32_t RTC base
|
||||
* @param[in] mask uint32_t which events to disable
|
||||
*/
|
||||
void rtc_disable_events(uint32_t rtc, uint32_t mask)
|
||||
{
|
||||
RTC_EVTENCLR(rtc) = mask;
|
||||
}
|
||||
|
||||
/** @brief Start the RTC
|
||||
*
|
||||
* @param[in] rtc uint32_t RTC base
|
||||
*/
|
||||
void rtc_start(uint32_t rtc)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(RTC_TASK_START(rtc));
|
||||
}
|
||||
|
||||
/** @brief Stop the RTC
|
||||
*
|
||||
* @param[in] rtc uint32_t RTC base
|
||||
*/
|
||||
void rtc_stop(uint32_t rtc)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(RTC_TASK_STOP(rtc));
|
||||
}
|
||||
|
||||
/** @brief Clear the RTC
|
||||
*
|
||||
* @param[in] rtc uint32_t RTC base
|
||||
*/
|
||||
void rtc_clear(uint32_t rtc)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(RTC_TASK_CLEAR(rtc));
|
||||
}
|
||||
|
||||
/** @brief Set compare register
|
||||
*
|
||||
* @param[in] rtc uint32_t RTC base
|
||||
* @param[in] cmp uint8_t compare number (0-3)
|
||||
* @param[in] value uint32_t compare value
|
||||
*/
|
||||
void rtc_set_compare(uint32_t rtc, uint8_t cmp, uint32_t value)
|
||||
{
|
||||
if (cmp < 4) {
|
||||
RTC_CC(rtc, cmp) = value;
|
||||
}
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
147
libopencm3/lib/nrf/common/timer.c
Normal file
147
libopencm3/lib/nrf/common/timer.c
Normal file
@@ -0,0 +1,147 @@
|
||||
/** @addtogroup timer_file TIMER peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the Timer/Counter </b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Maxim Sloyko <maxims@google.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
* Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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/nrf/timer.h>
|
||||
#include <libopencm3/nrf/clock.h>
|
||||
/**@{*/
|
||||
|
||||
/** @brief Get timer ticks
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
* @returns current ticks value
|
||||
*/
|
||||
uint32_t timer_get_ticks(uint32_t timer)
|
||||
{
|
||||
uint32_t ticks;
|
||||
uint32_t cc;
|
||||
|
||||
/* TODO: Check WTF is this doing? */
|
||||
cc = TIMER_CC(0, 0);
|
||||
TIMER_TASK_CAPTURE(timer, 0) = 1;
|
||||
ticks = TIMER_CC(timer, 0);
|
||||
TIMER_CC(timer, 0) = cc;
|
||||
return ticks;
|
||||
}
|
||||
|
||||
/** @brief Set timer mode (counter/timer)
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
* @param[in] mode enum timer_mode
|
||||
*/
|
||||
void timer_set_mode(uint32_t timer, enum timer_mode mode)
|
||||
{
|
||||
TIMER_MODE(timer) = mode;
|
||||
}
|
||||
|
||||
/** @brief Set timer bit mode (width)
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
* @param[in] bitmode enum timer_bitmode
|
||||
*/
|
||||
void timer_set_bitmode(uint32_t timer, enum timer_bitmode bitmode)
|
||||
{
|
||||
TIMER_BITMODE(timer) = bitmode;
|
||||
}
|
||||
|
||||
/** @brief Start the timer
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
*/
|
||||
void timer_start(uint32_t timer)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(TIMER_TASK_START(timer));
|
||||
}
|
||||
|
||||
/** @brief Stop the timer
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
*/
|
||||
void timer_stop(uint32_t timer)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(TIMER_TASK_STOP(timer));
|
||||
}
|
||||
|
||||
/** @brief Clear the timer
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
*/
|
||||
void timer_clear(uint32_t timer)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(TIMER_TASK_CLEAR(timer));
|
||||
}
|
||||
|
||||
/** @brief Set prescaler value
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
* @param[in] presc uint8_t prescaler value
|
||||
*/
|
||||
void timer_set_prescaler(uint32_t timer, uint8_t presc)
|
||||
{
|
||||
TIMER_PRESCALER(timer) = presc & TIMER_PRESCALER_MASK;
|
||||
}
|
||||
|
||||
/** @brief Set compare register
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
* @param[in] compare_num uint8_t compare number (0-3)
|
||||
* @param[in] compare_val uint32_t compare value
|
||||
*/
|
||||
void timer_set_compare(uint32_t timer, uint8_t compare_num, uint32_t compare_val)
|
||||
{
|
||||
if (compare_num > 3) {
|
||||
return;
|
||||
}
|
||||
|
||||
TIMER_CC(timer, compare_num) = compare_val;
|
||||
}
|
||||
|
||||
/** @brief Get the timer tick frequency
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
* @returns frequency of ticking
|
||||
*/
|
||||
uint32_t timer_get_freq(uint32_t timer)
|
||||
{
|
||||
return CLOCK_PCLK/(1<<TIMER_PRESCALER(timer));
|
||||
}
|
||||
|
||||
/** @brief Get compare register
|
||||
*
|
||||
* @param[in] timer uint32_t timer base
|
||||
* @param[in] compare_num uint8_t compare number (0-3)
|
||||
* @returns compare register value
|
||||
*/
|
||||
uint32_t timer_get_cc(uint32_t timer, uint8_t compare_num)
|
||||
{
|
||||
return TIMER_CC(timer, compare_num);
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
171
libopencm3/lib/nrf/common/uart.c
Normal file
171
libopencm3/lib/nrf/common/uart.c
Normal file
@@ -0,0 +1,171 @@
|
||||
/** @addtogroup uart_file UART peripheral API
|
||||
*
|
||||
* @brief <b>Access functions for the UART controller</b>
|
||||
*
|
||||
* @ingroup peripheral_apis
|
||||
* LGPL License Terms @ref lgpl_license
|
||||
* @author @htmlonly © @endhtmlonly 2016
|
||||
* Maxim Sloyko <maxims@google.com>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
|
||||
* Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
|
||||
*
|
||||
* 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/nrf/uart.h>
|
||||
#include <libopencm3/nrf/gpio.h>
|
||||
/**@{*/
|
||||
|
||||
/** @brief Enable the peripheral
|
||||
*
|
||||
* @param[in] uart uint32_t uart base
|
||||
*/
|
||||
void uart_enable(uint32_t uart)
|
||||
{
|
||||
UART_ENABLE(uart) = UART_ENABLE_ENABLED;
|
||||
}
|
||||
|
||||
/** @brief Disable the peripheral
|
||||
*
|
||||
* @param[in] uart uint32_t uart base
|
||||
*/
|
||||
void uart_disable(uint32_t uart)
|
||||
{
|
||||
UART_ENABLE(uart) = UART_ENABLE_DISABLED;
|
||||
}
|
||||
|
||||
/** @brief Configure UART parameters in single call
|
||||
*
|
||||
* @details Any pin number can be set to 0xff (or any number larger than UART_MAX_PIN)
|
||||
* to disconnect that pin.
|
||||
*
|
||||
* @param[in] uart uint32_t uart base
|
||||
* @param[in] tx_pin uint8_t TX pin number
|
||||
* @param[in] rx_pin uint8_t RX pin number
|
||||
* @param[in] rts_pin uint8_t RTS pin number
|
||||
* @param[in] cts_pin uint8_t CTS pin number
|
||||
* @param[in] br enum uart_baud baud rate
|
||||
* @param[in] enable_parity bool If true, enable parity bit
|
||||
*/
|
||||
void uart_configure(uint32_t uart,
|
||||
uint32_t tx_pin, uint32_t rx_pin, uint32_t rts_pin, uint32_t cts_pin,
|
||||
enum uart_baud br, bool enable_parity)
|
||||
{
|
||||
uart_set_pins(uart, rx_pin, tx_pin, cts_pin, rts_pin);
|
||||
|
||||
uint32_t reg_config = enable_parity ? UART_CONFIG_PARITY : 0;
|
||||
if (rts_pin <= UART_MAX_PIN || cts_pin <= UART_MAX_PIN) {
|
||||
reg_config |= UART_CONFIG_HWFC;
|
||||
}
|
||||
|
||||
UART_CONFIG(uart) = reg_config;
|
||||
uart_set_baudrate(uart, br);
|
||||
}
|
||||
|
||||
/** @brief Select GPIO pins to be used by this peripheral.
|
||||
*
|
||||
* This needs to be configured while UART peripheral is disabled.
|
||||
*
|
||||
* @param[in] uart uart peripheral base.
|
||||
* @param[in] rx RX pin. Use GPIO defines in @ref gpio_pin_id or GPIO_UNCONNECTED
|
||||
* if signal shall not be connected to any pin.
|
||||
* @param[in] tx TX pin. Use GPIO defines in @ref gpio_pin_id or GPIO_UNCONNECTED
|
||||
* if signal shall not be connected to any pin.
|
||||
* @param[in] cts CTS pin. Use GPIO defines in @ref gpio_pin_id or GPIO_UNCONNECTED
|
||||
* if signal shall not be connected to any pin.
|
||||
* @param[in] rts RTS pin. Use GPIO defines in @ref gpio_pin_id or GPIO_UNCONNECTED
|
||||
* if signal shall not be connected to any pin.
|
||||
|
||||
*/
|
||||
void uart_set_pins(uint32_t uart, uint32_t rx, uint32_t tx, uint32_t cts, uint32_t rts)
|
||||
{
|
||||
if (rx != GPIO_UNCONNECTED) {
|
||||
UART_PSELRXD(uart) = __GPIO2PIN(rx);
|
||||
} else {
|
||||
UART_PSELRXD(uart) = rx;
|
||||
}
|
||||
|
||||
if (tx != GPIO_UNCONNECTED) {
|
||||
UART_PSELTXD(uart) = __GPIO2PIN(tx);
|
||||
} else {
|
||||
UART_PSELTXD(uart) = tx;
|
||||
}
|
||||
|
||||
if (cts != GPIO_UNCONNECTED) {
|
||||
UART_PSELCTS(uart) = __GPIO2PIN(cts);
|
||||
} else {
|
||||
UART_PSELCTS(uart) = cts;
|
||||
}
|
||||
|
||||
if (rts != GPIO_UNCONNECTED) {
|
||||
UART_PSELRTS(uart) = __GPIO2PIN(rts);
|
||||
} else {
|
||||
UART_PSELRTS(uart) = rts;
|
||||
}
|
||||
}
|
||||
|
||||
#undef _LOG2
|
||||
|
||||
void uart_set_baudrate(uint32_t uart, enum uart_baud br)
|
||||
{
|
||||
UART_BAUDRATE(uart) = br;
|
||||
}
|
||||
|
||||
void uart_set_parity(uint32_t uart, int parity)
|
||||
{
|
||||
UART_CONFIG(uart) |= parity ? UART_CONFIG_PARITY : 0;
|
||||
}
|
||||
|
||||
void uart_set_flow_control(uint32_t uart, int flow)
|
||||
{
|
||||
UART_CONFIG(uart) |= flow ? UART_CONFIG_HWFC : 0;
|
||||
}
|
||||
|
||||
void uart_start_tx(uint32_t uart)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(UART_TASK_STARTTX((uart)));
|
||||
}
|
||||
|
||||
void uart_send(uint32_t uart, uint16_t byte)
|
||||
{
|
||||
UART_TXD((uart)) = byte;
|
||||
}
|
||||
|
||||
void uart_stop_tx(uint32_t uart)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(UART_TASK_STOPTX((uart)));
|
||||
}
|
||||
|
||||
void uart_start_rx(uint32_t uart)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(UART_TASK_STARTRX((uart)));
|
||||
}
|
||||
|
||||
uint16_t uart_recv(uint32_t uart)
|
||||
{
|
||||
return UART_RXD(uart);
|
||||
}
|
||||
|
||||
void uart_stop_rx(uint32_t uart)
|
||||
{
|
||||
PERIPH_TRIGGER_TASK(UART_TASK_STOPRX((uart)));
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
Reference in New Issue
Block a user