Skip to content

Commit

Permalink
pi-pico: initial blink example
Browse files Browse the repository at this point in the history
  • Loading branch information
deater committed Jun 24, 2021
1 parent db80b56 commit cbd6174
Show file tree
Hide file tree
Showing 21 changed files with 23,285 additions and 0 deletions.
66 changes: 66 additions & 0 deletions pi-pico/blink/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
CC = arm-none-eabi-gcc
AS = arm-none-eabi-as
LD = arm-none-eabi-ld

OBJDUMP = arm-none-eabi-objdump
OBJCOPY = arm-none-eabi-objcopy
#CFLAGS = -g -O2 -Wall
CFLAGS = -g -O3 -Wall -I..
CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m0plus
CFLAGS += -fno-common
CFLAGS += -ffunction-sections -fdata-sections
ASFLAGS =

ELF2UF2 = ../elf2uf2/elf2uf2

##### Project specific libraries #####
SRC_FILES = blink.c

all: blink.uf2 blink.dis

clean:
rm -f blink.bin blink.elf *~ *.o *.dis *.uf2

crt0.o: crt0.S
$(AS) $(ASFLAGS) -o crt0.o -c crt0.S

#boot2_w25q080.o: boot2_w25q080.S
# $(AS) $(ASFLAGS) -o boot2_w25q080.o -c boot2_w25q080.S

idivmod.o: idivmod.S
$(AS) $(ASFLAGS) -o idivmod.o -c idivmod.S

uldivmod.o: uldivmod.S
$(AS) $(ASFLAGS) -o uldivmod.o -c uldivmod.S

runtime.o: runtime.c
$(CC) $(CFLAGS) -c runtime.c

blink.elf: blink.o crt0.o bootrom.o runtime.o clocks.o gpio.o idivmod.o uldivmod.o
$(LD) -v -Tmemmap_default.ld -nostartfiles -o blink.elf blink.o \
bootrom.o crt0.o \
runtime.o clocks.o gpio.o idivmod.o uldivmod.o
# boot2_w25q080.o

bootrom.o: bootrom.c
$(CC) $(CFLAGS) -c bootrom.c

clocks.o: clocks.c
$(CC) $(CFLAGS) -c clocks.c

blink.o: blink.c
$(CC) $(CFLAGS) -c blink.c

blink.uf2: blink.elf
$(ELF2UF2) blink.elf blink.uf2

blink.dis: blink.elf
$(OBJDUMP) -h blink.elf > blink.dis
$(OBJDUMP) -d blink.elf >> blink.dis

####

gpio.o: gpio.c ../include/gpio.h
$(CC) $(CFLAGS) -c gpio.c


22 changes: 22 additions & 0 deletions pi-pico/blink/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Packages to install:


Setup:

Flashing:
~~~~~~~~~
+ Haven't got the drag-and-drop method going, but using the SWD method.

From the "getting started w Raspberry Pi Pico" document

Set up a Pi4 to be the host, and wire up according to the diagram.

git clone https://github.com/raspberrypi/openocd.git --recursive --branch rp2040 --depth=1
cd openocd
./bootstrap
./configure --enable-ftdi --enable-sysfsgpio --enable-bcm2835gpio
make -j4
sudo make install

openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c "program blink/blink.elf verify reset exit"
171 changes: 171 additions & 0 deletions pi-pico/blink/blink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#include <stdint.h>

#include "include/pi-pico.h"
#include "include/gpio.h"

typedef uint64_t absolute_time_t;

static void tight_loop_contents(void) {}

#define TIMER_BASE 0x40054000


#define NUM_TIMERS 4

typedef volatile uint32_t io_rw_32;
typedef const volatile uint32_t io_ro_32;
typedef volatile uint32_t io_wo_32;

typedef struct {
io_wo_32 timehw;
io_wo_32 timelw;
io_ro_32 timehr;
io_ro_32 timelr;
io_rw_32 alarm[NUM_TIMERS];
io_rw_32 armed;
io_ro_32 timerawh;
io_ro_32 timerawl;
io_rw_32 dbgpause;
io_rw_32 pause;
io_rw_32 intr;
io_rw_32 inte;
io_rw_32 intf;
io_ro_32 ints;
} timer_hw_t;

#define timer_hw ((timer_hw_t *const)TIMER_BASE)


static uint64_t to_us_since_boot(absolute_time_t t) {
return t;
}


static void update_us_since_boot(absolute_time_t *t, uint64_t us_since_boot) {
//#ifdef NDEBUG
*t = us_since_boot;
//#else
// assert(us_since_boot <= INT64_MAX);
// t->_private_us_since_boot = us_since_boot;
//#endif
}


uint64_t time_us_64(void) {
// Need to make sure that the upper 32 bits of the timer
// don't change, so read that first
uint32_t hi = timer_hw->timerawh;
uint32_t lo;
do {
// Read the lower 32 bits
lo = timer_hw->timerawl;
// Now read the upper 32 bits again and
// check that it hasn't incremented. If it has loop around
// and read the lower 32 bits again to get an accurate value
uint32_t next_hi = timer_hw->timerawh;
if (hi == next_hi) break;
hi = next_hi;
} while (1);
return ((uint64_t) hi << 32u) | lo;
}


void busy_wait_until(absolute_time_t t) {
uint64_t target = to_us_since_boot(t);
uint32_t hi_target = (uint32_t)(target >> 32u);
uint32_t hi = timer_hw->timerawh;
while (hi < hi_target) {
hi = timer_hw->timerawh;
tight_loop_contents();
}
while (hi == hi_target && timer_hw->timerawl < (uint32_t) target) {
hi = timer_hw->timerawh;
tight_loop_contents();
}
}


void busy_wait_us(uint64_t delay_us) {
uint64_t base = time_us_64();
uint64_t target = base + delay_us;
if (target < base) {
target = (uint64_t)-1;
}
absolute_time_t t;
update_us_since_boot(&t, target);
busy_wait_until(t);
}


void busy_wait_us_32(uint32_t delay_us) {
if (0 <= (int32_t)delay_us) {
// we only allow 31 bits, otherwise we could have a race in the loop below with
// values very close to 2^32
uint32_t start = timer_hw->timerawl;
while (timer_hw->timerawl - start < delay_us) {
tight_loop_contents();
}
} else {
busy_wait_us(delay_us);
}
}

static absolute_time_t get_absolute_time(void) {
absolute_time_t t;
update_us_since_boot(&t, time_us_64());
return t;
}


static inline absolute_time_t delayed_by_us(const absolute_time_t t, uint64_t us) {
absolute_time_t t2;
uint64_t base = to_us_since_boot(t);
uint64_t delayed = base + us;
if (delayed < base) {
delayed = (uint64_t)-1;
}
update_us_since_boot(&t2, delayed);
return t2;
}


static absolute_time_t make_timeout_time_us(uint64_t us) {
return delayed_by_us(get_absolute_time(), us);
}


void sleep_us(uint64_t us) {
//#if !PICO_TIME_DEFAULT_ALARM_POOL_DISABLED
// sleep_until(make_timeout_time_us(us));
//#else
if (us >> 32u) {
busy_wait_until(make_timeout_time_us(us));
} else {
busy_wait_us_32(us);
}
//#endif

}

void sleep_ms(uint32_t ms) {
sleep_us(ms * 1024ull); // FIXME
}


int main(int argc, char **argv) {

const uint32_t LED_PIN = PICO_DEFAULT_LED_PIN;

gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);

while (1) {
gpio_put(LED_PIN, 1);
sleep_ms(250);
gpio_put(LED_PIN, 0);
sleep_ms(250);
}

return 0;

}
40 changes: 40 additions & 0 deletions pi-pico/blink/bootrom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <stdint.h>

//#include "pico/bootrom.h"

/// \tag::table_lookup[]

// Bootrom function: rom_table_lookup
// Returns the 32 bit pointer into the ROM if found or NULL otherwise.
typedef void *(*rom_table_lookup_fn)(uint16_t *table, uint32_t code);

// Convert a 16 bit pointer stored at the given rom address into a 32 bit pointer
#define rom_hword_as_ptr(rom_address) (void *)(uintptr_t)(*(uint16_t *)rom_address)

void *rom_func_lookup(uint32_t code) {
rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) rom_hword_as_ptr(0x18);
uint16_t *func_table = (uint16_t *) rom_hword_as_ptr(0x14);
return rom_table_lookup(func_table, code);
}

void *rom_data_lookup(uint32_t code) {
rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) rom_hword_as_ptr(0x18);
uint16_t *data_table = (uint16_t *) rom_hword_as_ptr(0x16);
return rom_table_lookup(data_table, code);
}
/// \end::table_lookup[]

int rom_funcs_lookup(uint32_t *table, unsigned int count) {
int ok = 1;
for (unsigned int i = 0; i < count; i++) {
table[i] = (uintptr_t) rom_func_lookup(table[i]);
if (!table[i]) ok = 0;
}
return ok;
}
23 changes: 23 additions & 0 deletions pi-pico/blink/bs2_default_padded_checksummed.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Padded and checksummed version of: /home/vince/research/vmw-meter.git/pi-pico/pico/pico-examples/build/pico-sdk/src/rp2_common/boot_stage2/bs2_default.bin

.cpu cortex-m0plus
.thumb

.section .boot2, "ax"

.byte 0x00, 0xb5, 0x32, 0x4b, 0x21, 0x20, 0x58, 0x60, 0x98, 0x68, 0x02, 0x21, 0x88, 0x43, 0x98, 0x60
.byte 0xd8, 0x60, 0x18, 0x61, 0x58, 0x61, 0x2e, 0x4b, 0x00, 0x21, 0x99, 0x60, 0x02, 0x21, 0x59, 0x61
.byte 0x01, 0x21, 0xf0, 0x22, 0x99, 0x50, 0x2b, 0x49, 0x19, 0x60, 0x01, 0x21, 0x99, 0x60, 0x35, 0x20
.byte 0x00, 0xf0, 0x44, 0xf8, 0x02, 0x22, 0x90, 0x42, 0x14, 0xd0, 0x06, 0x21, 0x19, 0x66, 0x00, 0xf0
.byte 0x34, 0xf8, 0x19, 0x6e, 0x01, 0x21, 0x19, 0x66, 0x00, 0x20, 0x18, 0x66, 0x1a, 0x66, 0x00, 0xf0
.byte 0x2c, 0xf8, 0x19, 0x6e, 0x19, 0x6e, 0x19, 0x6e, 0x05, 0x20, 0x00, 0xf0, 0x2f, 0xf8, 0x01, 0x21
.byte 0x08, 0x42, 0xf9, 0xd1, 0x00, 0x21, 0x99, 0x60, 0x1b, 0x49, 0x19, 0x60, 0x00, 0x21, 0x59, 0x60
.byte 0x1a, 0x49, 0x1b, 0x48, 0x01, 0x60, 0x01, 0x21, 0x99, 0x60, 0xeb, 0x21, 0x19, 0x66, 0xa0, 0x21
.byte 0x19, 0x66, 0x00, 0xf0, 0x12, 0xf8, 0x00, 0x21, 0x99, 0x60, 0x16, 0x49, 0x14, 0x48, 0x01, 0x60
.byte 0x01, 0x21, 0x99, 0x60, 0x01, 0xbc, 0x00, 0x28, 0x00, 0xd0, 0x00, 0x47, 0x12, 0x48, 0x13, 0x49
.byte 0x08, 0x60, 0x03, 0xc8, 0x80, 0xf3, 0x08, 0x88, 0x08, 0x47, 0x03, 0xb5, 0x99, 0x6a, 0x04, 0x20
.byte 0x01, 0x42, 0xfb, 0xd0, 0x01, 0x20, 0x01, 0x42, 0xf8, 0xd1, 0x03, 0xbd, 0x02, 0xb5, 0x18, 0x66
.byte 0x18, 0x66, 0xff, 0xf7, 0xf2, 0xff, 0x18, 0x6e, 0x18, 0x6e, 0x02, 0xbd, 0x00, 0x00, 0x02, 0x40
.byte 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x07, 0x00, 0x00, 0x03, 0x5f, 0x00, 0x21, 0x22, 0x00, 0x00
.byte 0xf4, 0x00, 0x00, 0x18, 0x22, 0x20, 0x00, 0xa0, 0x00, 0x01, 0x00, 0x10, 0x08, 0xed, 0x00, 0xe0
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0xb2, 0x4e, 0x7a
Loading

0 comments on commit cbd6174

Please sign in to comment.