Skip to content
Open
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
19 changes: 19 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Remove sources:
* system_nrf51.c
* gcc_startup_nrf51.s

Remove headers:
* boards.h
* pca10001.h
* system_nrf51.h
* nrf.h
* nrf51.h
* nrf51_bitfields.h

Copy files:
* core_cm0.h
* core_cmInstr.h
* core_cmFunc.h
* gcc_nrf51_common.ld
* gcc_nrf51_blank_xxaa.ld
* gcc_nrf51_blank_xxab.ld
6 changes: 3 additions & 3 deletions examples/ll-scanner/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
#include <blessed/bdaddr.h>
#include <blessed/log.h>

#include "nrf_delay.h"

#include "ll.h"
#include "delay.h"

#define SCAN_WINDOW 200000
#define SCAN_INTERVAL 500000
#define SCAN_DURATION 1100000 /* 1.1 s */

static const bdaddr_t addr = { { 0x14, 0x20, 0xCC, 0xDD, 0xEE, 0xFF },
BDADDR_TYPE_RANDOM };
Expand Down Expand Up @@ -84,7 +84,7 @@ int main(void)
ll_scan_start(LL_SCAN_PASSIVE, SCAN_INTERVAL, SCAN_WINDOW,
adv_report_cb);

nrf_delay_ms(1100);
delay(SCAN_DURATION);

ll_scan_stop();

Expand Down
12 changes: 3 additions & 9 deletions platform/nrf51822/Makefile.platform
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,15 @@ PLATFORM_LDFLAGS = -T$(SDK_TEMPLATE_PATH)/gcc/$(LINKER_SCRIPT) \
-mthumb \
-mcpu=$(CPU)

PLATFORM_SOURCE_PATHS = $(SDK_SOURCE_PATH) \
$(SDK_SOURCE_PATH)/app_common \
$(SDK_SOURCE_PATH)/nrf_delay \
$(SDK_TEMPLATE_PATH) \
PLATFORM_SOURCE_PATHS = $(SDK_TEMPLATE_PATH) \
$(SDK_TEMPLATE_PATH/gcc) \
$(PLATFORM_PATH)

PLATFORM_SOURCE_FILES = system_nrf51.c \
app_uart.c \
app_fifo.c \
app_gpiote.c \
nrf_delay.c \
nrf51822.c \
delay.c \
log.c \
timer.c \
uart.c \
radio.c

PLATFORM_ASM_PATHS = $(SDK_TEMPLATE_PATH)/gcc
Expand Down
32 changes: 23 additions & 9 deletions platform/nrf51822/nrf51822.c → platform/nrf51822/delay.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2013 Paulo B. de Oliveira Filho <pauloborgesfilho@gmail.com>
* Copyright (c) 2013 Claudio Takahasi <claudio.takahasi@gmail.com>
* Copyright (c) 2013 João Paulo Rechi Vita <jprvita@gmail.com>
* Copyright (c) 2014 Paulo B. de Oliveira Filho <pauloborgesfilho@gmail.com>
* Copyright (c) 2014 Claudio Takahasi <claudio.takahasi@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,12 +24,27 @@
*/

#include <stdint.h>
#include <string.h>

#include <blessed/log.h>

void app_error_handler(uint32_t error_code, uint32_t line_num,
const uint8_t *p_file_name)
void delay(uint32_t us)
{
ERROR("NRF error 0x%04x %s:%d", error_code, p_file_name, line_num);
asm volatile (
"start: nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"nop\n\t"
"sub %[cycles], #1\n\t"
"bne start\n\t"

: /* empty */ : [cycles] "l" (us)
);
}
48 changes: 18 additions & 30 deletions platform/nrf51822/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,28 @@
*/

#include <stdint.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include <app_uart.h>
#include <nrf_delay.h>
#include <boards.h>
#include <nrf51.h>
#include <nrf51_bitfields.h>

#include <blessed/errcodes.h>
#include <blessed/log.h>

#include "nrf51822.h"
#include "delay.h"
#include "uart.h"

#ifndef CONFIG_LOG_BUFFER_LEN
#define CONFIG_LOG_BUFFER_LEN 128
#endif

#define UART_INIT_DELAY 1000 /* 1 ms */

#define UNINITIALIZED 0
#define READY 1
#define BUSY 2
Expand All @@ -53,22 +57,15 @@ static volatile uint16_t len;
static volatile uint8_t buffer[CONFIG_LOG_BUFFER_LEN] __attribute__ ((aligned));
static volatile uint8_t state = UNINITIALIZED;

static __inline void tx_next_byte(void)

static void send_next_octet(void)
{
if (pos == len) {
state = READY;
return;
}

app_uart_put(buffer[pos++]);
}

static void uart_evt_handler(app_uart_evt_t *p_app_uart_evt)
{
if (p_app_uart_evt->evt_type != APP_UART_TX_EMPTY)
return;

tx_next_byte();
uart_send(buffer[pos++]);
}
#endif

Expand All @@ -89,7 +86,7 @@ int16_t log_print(const char *format, ...)
pos = 0;
state = BUSY;

tx_next_byte();
send_next_octet();
#endif

return 0;
Expand All @@ -98,30 +95,21 @@ int16_t log_print(const char *format, ...)
int16_t log_init(void)
{
#if CONFIG_LOG_ENABLE
uint32_t err_code;

UNUSED(err_code);

app_uart_comm_params_t params = {
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_ENABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
struct uart_config config = {
.baud = UART_BAUD_115200,
.rx_pin = RX_PIN_NUMBER,
.tx_pin = TX_PIN_NUMBER,
.parity_bit = false
};

if (state != UNINITIALIZED)
return -EALREADY;

APP_UART_INIT(&params, uart_evt_handler, IRQ_PRIORITY_HIGHEST,
err_code);

uart_init(config, send_next_octet);
state = READY;

/* Necessary to fully initialize the UART */
nrf_delay_ms(1);
delay(UART_INIT_DELAY);
log_print("\r\n");
#endif

Expand Down
126 changes: 126 additions & 0 deletions platform/nrf51822/uart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014 Paulo B. de Oliveira Filho <pauloborgesfilho@gmail.com>
* Copyright (c) 2014 Claudio Takahasi <claudio.takahasi@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stdint.h>
#include <stdbool.h>

#include <nrf51.h>
#include <nrf51_bitfields.h>

#include <blessed/errcodes.h>

#include "nrf51822.h"
#include "uart.h"

#define STATE_UNINITIALIZED 0
#define STATE_IDLE 1
#define STATE_BUSY 2

#define DISCONNECTED_PIN 0xFFFFFFFF

#if CONFIG_UART_ENABLE

/* nRF51 Series Reference Manual v2.1, section 28.9.10, page 166 */
static const uint32_t baudrates[] = {
0x0004F000UL, 0x0009D000UL, 0x0013B000UL, 0x00275000UL, 0x003B0000UL,
0x004EA000UL, 0x0075F000UL, 0x009D5000UL, 0x00EBF000UL, 0x013A9000UL,
0x01D7E000UL, 0x03AFB000UL, 0x04000000UL, 0x075F7000UL, 0x0EBEDFA4UL,
0x10000000UL
};

static uint8_t state = STATE_UNINITIALIZED;
static uart_sent_cb_t sent_cb = 0;

void UART0_IRQHandler(void)
{
if (NRF_UART0->EVENTS_TXDRDY) {
NRF_UART0->EVENTS_TXDRDY = 0UL;
NRF_UART0->TASKS_STOPTX = 1UL;

state = STATE_IDLE;

if (sent_cb)
sent_cb();
} else {
NRF_UART0->EVENTS_ERROR = 0UL;
NRF_UART0->TASKS_STOPTX = 1UL;

state = STATE_IDLE;
}
}

int16_t uart_send(uint8_t octet)
{
if (state != STATE_IDLE)
return -ENOREADY;

NRF_UART0->TASKS_STARTTX = 1UL;
NRF_UART0->TXD = octet;

state = STATE_BUSY;

return 0;
}

int16_t uart_init(struct uart_config config, uart_sent_cb_t cb)
{
if (state != STATE_UNINITIALIZED)
return -EALREADY;

sent_cb = cb;

NRF_UART0->PSELRTS = DISCONNECTED_PIN;
NRF_UART0->PSELCTS = DISCONNECTED_PIN;

NRF_UART0->PSELTXD = config.tx_pin;
NRF_UART0->PSELRXD = config.rx_pin;

NRF_UART0->BAUDRATE = baudrates[config.baud];

NRF_UART0->CONFIG = 0UL;

if (config.parity_bit)
NRF_UART0->CONFIG |= UART_CONFIG_PARITY_Included
<< UART_CONFIG_PARITY_Pos;
else
NRF_UART0->CONFIG |= UART_CONFIG_PARITY_Excluded
<< UART_CONFIG_PARITY_Pos;

NRF_UART0->INTENSET = UART_INTENSET_TXDRDY_Enabled
<< UART_INTENSET_TXDRDY_Pos;

NVIC_ClearPendingIRQ(UART0_IRQn);
NVIC_SetPriority(UART0_IRQn, IRQ_PRIORITY_HIGHEST);
NVIC_EnableIRQ(UART0_IRQn);

NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled
<< UART_ENABLE_ENABLE_Pos;

state = STATE_IDLE;

return 0;
}

#endif
Loading