Compare commits

..

10 Commits

Author SHA1 Message Date
Ikatono
805925c92d started Neopixel structs 2023-12-19 01:38:32 -06:00
2185c25f12 added Queue.h 2023-12-08 22:42:15 -06:00
1b1734c15e added workspace file 2023-12-08 21:22:21 -06:00
1f492d0835 my first commit 2023-12-08 21:13:49 -06:00
Andrew Scheller
eca13acf57 Fix broken link in README.md (#397) 2023-06-11 15:15:45 -05:00
graham sanderson
cbf8072072 Merge branch 'master' into develop 2023-06-08 15:22:59 -05:00
pmarques-dev
465a2a3824 coding style fixes, copyright waiver (#395)
Co-authored-by: Paulo Marques <pm@quant-insight.com>
2023-06-08 15:22:29 -05:00
graham sanderson
0d4a9f28c0 build fix for non pico boards 2023-06-08 10:04:05 -05:00
Frederico Pereira
654aabc686 Update picow_ntp_client.c (#391)
Fix "invalid conversion from 'void*' to 'NTP_T*'"
2023-06-06 10:51:55 -05:00
pmarques-dev
8df09c9a7c quadrature encoder: reduce PIO code size from 29 to 24 (#390)
Co-authored-by: Paulo Marques <pm@quant-insight.com>
2023-06-05 10:54:37 -05:00
17 changed files with 433 additions and 129 deletions

View File

@@ -202,7 +202,7 @@ App|Description
[picow_bt_example_spp_flowcontrol](https://github.com/bluekitchen/btstack/tree/master/example/spp_flowcontrol.c) | SPP Server - RFCOMM Flow Control.
[picow_bt_example_spp_streamer_client](https://github.com/bluekitchen/btstack/tree/master/example/spp_streamer_client.c) | Performance - Stream Data over SPP (Client.c).
[picow_bt_example_spp_streamer](https://github.com/bluekitchen/btstack/tree/master/example/spp_streamer.c) | Performance - Stream Data over SPP (Server.c).
[picow_bt_example_ublox_spp_le_counter](pico_w/bt/ublox_spp_le_counter.c) | LE u-blox SPP-like Heartbeat Server.
[picow_bt_example_ublox_spp_le_counter](https://github.com/bluekitchen/btstack/blob/master/example/ublox_spp_le_counter.c) | LE u-blox SPP-like Heartbeat Server.
Some Standalone Bluetooth examples (without all the common example build infrastructure) are also available:

View File

@@ -35,7 +35,7 @@ int power_source(bool *battery_powered) {
int power_voltage(float *voltage_result) {
#ifndef PICO_VSYS_PIN
return PICO_ERROR_NO_DATA;
#endif
#else
#if CYW43_USES_VSYS_PIN
cyw43_thread_enter();
// Make sure cyw43 is awake
@@ -73,4 +73,5 @@ int power_voltage(float *voltage_result) {
const float conversion_factor = 3.3f / (1 << 12);
*voltage_result = vsys * 3 * conversion_factor;
return PICO_OK;
#endif
}

View File

@@ -0,0 +1,11 @@
{
"folders": [
{
"path": "."
},
{
"path": "../../pico-sdk"
}
],
"settings": {}
}

View File

@@ -1,10 +1,11 @@
if (TARGET tinyusb_device)
add_executable(hello_usb
hello_usb.c
hello_usb.cpp
)
set(PICO_CXX_ENABLE_EXCEPTIONS 1)
# pull in common dependencies
target_link_libraries(hello_usb pico_stdlib)
target_link_libraries(hello_usb pico_stdlib pico_multicore pico_util)
target_compile_definitions(hello_usb PRIVATE PARAM_ASSERTIONS_ENABLE_ALL=1)
# enable usb output, disable uart output
pico_enable_stdio_usb(hello_usb 1)

View File

@@ -1,16 +0,0 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
int main() {
stdio_init_all();
while (true) {
printf("Hello, world!\n");
sleep_ms(1000);
}
}

View File

@@ -0,0 +1,44 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/mutex.h"
#include "pico/multicore.h"
#include "wrappers/Queue.h"
#include <string>
auto_init_mutex(printf_lock);
Queue<std::string> queue(10);
//Queue<int> int_queue(10);
void main1() {
while (true) {
std::string message;
queue.blocking_remove(&message);
printf(message.c_str());
printf("\n");
//int count;
//int_queue.blocking_remove(&count);
//printf("%i\n", count);
}
}
extern "C"
int main() {
stdio_init_all();
multicore_launch_core1(main1);
int count = 0;
while (true) {
std::string message;
message = std::to_string(count++);
printf("Is wrapped: %i\n", queue.wrapped);
printf("Enqueue %s\n", message.c_str());
queue.blocking_add(&message);
//int_queue.blocking_add(&count);
sleep_ms(1000);
}
}

View File

@@ -0,0 +1,21 @@
#include "CriticalSection.h"
CriticalSection::CriticalSection()
{
critical_section_init(&_critical);
}
void CriticalSection::lock()
{
critical_section_enter_blocking(&_critical);
}
void CriticalSection::unlock()
{
critical_section_exit(&_critical);
}
CriticalSection::~CriticalSection()
{
critical_section_deinit(&_critical);
}

View File

@@ -0,0 +1,20 @@
#ifndef H_10337EC23C94444EB1628220BA87E2D7
#define H_10337EC23C94444EB1628220BA87E2D7
#include "pico/sync.h"
class CriticalSection
{
private:
critical_section_t _critical;
public:
CriticalSection();
void lock();
void unlock();
~CriticalSection();
};
#endif //H_10337EC23C94444EB1628220BA87E2D7

View File

@@ -0,0 +1,16 @@
#include "Mutex.h"
Mutex::Mutex()
{
mutex_init(&_mutex);
}
void Mutex::lock()
{
mutex_enter_blocking(&_mutex);
}
void Mutex::unlock()
{
mutex_exit(&_mutex);
}

View File

@@ -0,0 +1,18 @@
#ifndef H_A295368D75B1464CB9B5336EEFFB6239
#define H_A295368D75B1464CB9B5336EEFFB6239
#include "pico/stdlib.h"
#include "pico/mutex.h"
class Mutex
{
private:
mutex_t _mutex;
public:
Mutex();
void lock();
void unlock();
};
#endif //H_A295368D75B1464CB9B5336EEFFB6239

View File

@@ -0,0 +1,42 @@
#include "NeoPacket.h"
namespace NeoPacket
{
GreenPixel& GreenPixel::operator=(GreenPixel& other)
{
this->value = other.value;
return *this;
}
GreenPixel& GreenPixel::operator=(uint8_t val)
{
this->value = val;
return *this;
}
RedPixel& RedPixel::operator=(RedPixel& other)
{
this->value = other.value;
return *this;
}
RedPixel& RedPixel::operator=(uint8_t val)
{
this->value = val;
return *this;
}
BluePixel& BluePixel::operator=(BluePixel& other)
{
this->value = other.value;
return *this;
}
BluePixel& BluePixel::operator=(uint8_t val)
{
this->value = val;
return *this;
}
}

View File

@@ -0,0 +1,83 @@
#ifndef H_3FE5AF534D5E46BD9700F872DB5C50CD
#define H_3FE5AF534D5E46BD9700F872DB5C50CD
#include <array>
#include <cstdint>
namespace NeoPacket
{
class RedPixel
{
public:
RedPixel& operator=(RedPixel& other);
RedPixel& operator=(uint8_t val);
uint8_t val() const;
private:
uint8_t padding1;
uint8_t value;
uint8_t padding2;
};
class GreenPixel
{
public:
GreenPixel& operator=(GreenPixel& other);
GreenPixel& operator=(uint8_t val);
uint8_t val() const;
private:
uint8_t value;
uint8_t padding1;
uint8_t padding2;
};
class BluePixel
{
public:
BluePixel& operator=(BluePixel& other);
BluePixel& operator=(uint8_t val);
uint8_t val() const;
private:
uint8_t padding1;
uint8_t padding2;
uint8_t value;
};
class Pixel
{
public:
uint8_t green;
uint8_t red;
uint8_t blue;
void apply(GreenPixel g);
void apply(RedPixel r);
void apply(BluePixel b);
};
template <std::size_t N>
class NeoPacket
{
public:
std::array<GreenPixel, N>& as_green()
{
return reinterpret_cast<std::array<GreenPixel, N>&>(*this);
}
std::array<RedPixel, N>& as_red()
{
return reinterpret_cast<std::array<RedPixel, N>&>(*this);
}
std::array<BluePixel, N>& as_blue()
{
return reinterpret_cast<std::array<BluePixel, N>&>(*this);
}
private:
std::array<Pixel, N> data;
};
}
#endif //H_3FE5AF534D5E46BD9700F872DB5C50CD

View File

@@ -0,0 +1,85 @@
#ifndef H_97BF73781549459A88697C808E132C22
#define H_97BF73781549459A88697C808E132C22
#include "pico/util/queue.h"
#include <type_traits>
//#include <utility>
template <typename T>
class Queue
{
private:
queue_t queue;
public:
static constexpr bool wrapped = !std::is_pod<T>();
Queue(uint size)
{
if constexpr (wrapped)
{
queue_init(&queue, sizeof(T*), size);
}
else
{
queue_init(&queue, sizeof(T), size);
}
}
bool try_add(T *item)
{
if constexpr (wrapped)
{
T* temp = new T(*item);
return queue_try_add(&queue, &temp);
}
else
{
return queue_try_add(&queue, item);
}
}
void blocking_add(T* item)
{
if constexpr (wrapped)
{
T* temp = new T(*item);
queue_add_blocking(&queue, &temp);
}
else
{
queue_add_blocking(&queue, item);
}
}
bool try_remove(T* item)
{
if constexpr (wrapped)
{
T** temp;
bool success = queue_try_remove(&queue, temp);
if (success)
*item = **temp;
return success;
}
else
{
return queue_try_remove(&queue, item);
}
}
void blocking_remove(T* item)
{
if constexpr (wrapped)
{
T** temp;
queue_remove_blocking(&queue, temp);
*item = **temp;
}
else
{
queue_remove_blocking(&queue, item);
}
}
~Queue()
{
queue_free(&queue);
}
};
#endif //H_97BF73781549459A88697C808E132C22

View File

View File

@@ -108,7 +108,7 @@ static void ntp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_ad
// Perform initialisation
static NTP_T* ntp_init(void) {
NTP_T *state = calloc(1, sizeof(NTP_T));
NTP_T *state = (NTP_T*)calloc(1, sizeof(NTP_T));
if (!state) {
printf("failed to allocate state\n");
return NULL;
@@ -183,4 +183,4 @@ int main() {
run_ntp_test();
cyw43_arch_deinit();
return 0;
}
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2021 pmarques-dev @ github
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
@@ -44,8 +44,10 @@ int main() {
PIO pio = pio0;
const uint sm = 0;
uint offset = pio_add_program(pio, &quadrature_encoder_program);
quadrature_encoder_program_init(pio, sm, offset, PIN_AB, 0);
// we don't really need to keep the offset, as this program must be loaded
// at offset 0
pio_add_program(pio, &quadrature_encoder_program);
quadrature_encoder_program_init(pio, sm, PIN_AB, 0);
while (1) {
// note: thanks to two's complement arithmetic delta will always

View File

@@ -1,13 +1,12 @@
;
; Copyright (c) 2021 pmarques-dev @ github
; Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
;
; SPDX-License-Identifier: BSD-3-Clause
;
.program quadrature_encoder
; this code must be loaded into address 0, but at 29 instructions, it probably
; wouldn't be able to share space with other programs anyway
; the code must be loaded at address 0, because it uses computed jumps
.origin 0
@@ -20,82 +19,70 @@
; keeps the current encoder count and is incremented / decremented according to
; the steps sampled
; writing any non zero value to the TX FIFO makes the state machine push the
; current count to RX FIFO between 6 to 18 clocks afterwards. The worst case
; sampling loop takes 14 cycles, so this program is able to read step rates up
; to sysclk / 14 (e.g., sysclk 125MHz, max step rate = 8.9 Msteps/sec)
; the program keeps trying to write the current count to the RX FIFO without
; blocking. To read the current count, the user code must drain the FIFO first
; and wait for a fresh sample (takes ~4 SM cycles on average). The worst case
; sampling loop takes 10 cycles, so this program is able to read step rates up
; to sysclk / 10 (e.g., sysclk 125MHz, max step rate = 12.5 Msteps/sec)
; 00 state
JMP update ; read 00
JMP decrement ; read 01
JMP increment ; read 10
JMP update ; read 11
JMP update ; read 00
JMP decrement ; read 01
JMP increment ; read 10
JMP update ; read 11
; 01 state
JMP increment ; read 00
JMP update ; read 01
JMP update ; read 10
JMP decrement ; read 11
JMP increment ; read 00
JMP update ; read 01
JMP update ; read 10
JMP decrement ; read 11
; 10 state
JMP decrement ; read 00
JMP update ; read 01
JMP update ; read 10
JMP increment ; read 11
JMP decrement ; read 00
JMP update ; read 01
JMP update ; read 10
JMP increment ; read 11
; to reduce code size, the last 2 states are implemented in place and become the
; target for the other jumps
; 11 state
JMP update ; read 00
JMP increment ; read 01
JMP update ; read 00
JMP increment ; read 01
decrement:
; note: the target of this instruction must be the next address, so that
; the effect of the instruction does not depend on the value of Y. The
; same is true for the "JMP X--" below. Basically "JMP Y--, <next addr>"
; is just a pure "decrement Y" instruction, with no other side effects
JMP Y--, update ; read 10
; note: the target of this instruction must be the next address, so that
; the effect of the instruction does not depend on the value of Y. The
; same is true for the "JMP X--" below. Basically "JMP Y--, <next addr>"
; is just a pure "decrement Y" instruction, with no other side effects
JMP Y--, update ; read 10
; this is where the main loop starts
; this is where the main loop starts
.wrap_target
update:
; we start by checking the TX FIFO to see if the main code is asking for
; the current count after the PULL noblock, OSR will have either 0 if
; there was nothing or the value that was there
SET X, 0
PULL noblock
; since there are not many free registers, and PULL is done into OSR, we
; have to do some juggling to avoid losing the state information and
; still place the values where we need them
MOV X, OSR
MOV OSR, ISR
; the main code did not ask for the count, so just go to "sample_pins"
JMP !X, sample_pins
; if it did ask for the count, then we push it
MOV ISR, Y ; we trash ISR, but we already have a copy in OSR
PUSH
MOV ISR, Y ; read 11
PUSH noblock
sample_pins:
; we shift into ISR the last state of the 2 input pins (now in OSR) and
; the new state of the 2 pins, thus producing the 4 bit target for the
; computed jump into the correct action for this state
MOV ISR, NULL
IN OSR, 2
IN PINS, 2
MOV PC, ISR
; we shift into ISR the last state of the 2 input pins (now in OSR) and
; the new state of the 2 pins, thus producing the 4 bit target for the
; computed jump into the correct action for this state. Both the PUSH
; above and the OUT below zero out the other bits in ISR
OUT ISR, 2
IN PINS, 2
; the PIO does not have a increment instruction, so to do that we do a
; negate, decrement, negate sequence
; save the state in the OSR, so that we can use ISR for other purposes
MOV OSR, ISR
; jump to the correct state machine action
MOV PC, ISR
; the PIO does not have a increment instruction, so to do that we do a
; negate, decrement, negate sequence
increment:
MOV X, !Y
JMP X--, increment_cont
MOV Y, ~Y
JMP Y--, increment_cont
increment_cont:
MOV Y, !X
.wrap ; the .wrap here avoids one jump instruction and saves a cycle too
MOV Y, ~Y
.wrap ; the .wrap here avoids one jump instruction and saves a cycle too
@@ -106,60 +93,49 @@ increment_cont:
// max_step_rate is used to lower the clock of the state machine to save power
// if the application doesn't require a very high sampling rate. Passing zero
// will set the clock to the maximum, which gives a max step rate of around
// 8.9 Msteps/sec at 125MHz
// will set the clock to the maximum
static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint offset, uint pin, int max_step_rate)
static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint pin, int max_step_rate)
{
pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
gpio_pull_up(pin);
gpio_pull_up(pin + 1);
pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
gpio_pull_up(pin);
gpio_pull_up(pin + 1);
pio_sm_config c = quadrature_encoder_program_get_default_config(offset);
sm_config_set_in_pins(&c, pin); // for WAIT, IN
sm_config_set_jmp_pin(&c, pin); // for JMP
// shift to left, autopull disabled
sm_config_set_in_shift(&c, false, false, 32);
// don't join FIFO's
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_NONE);
pio_sm_config c = quadrature_encoder_program_get_default_config(0);
// passing "0" as the sample frequency,
if (max_step_rate == 0) {
sm_config_set_clkdiv(&c, 1.0);
} else {
// one state machine loop takes at most 14 cycles
float div = (float)clock_get_hz(clk_sys) / (14 * max_step_rate);
sm_config_set_clkdiv(&c, div);
}
sm_config_set_in_pins(&c, pin); // for WAIT, IN
sm_config_set_jmp_pin(&c, pin); // for JMP
// shift to left, autopull disabled
sm_config_set_in_shift(&c, false, false, 32);
// don't join FIFO's
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_NONE);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
// passing "0" as the sample frequency,
if (max_step_rate == 0) {
sm_config_set_clkdiv(&c, 1.0);
} else {
// one state machine loop takes at most 10 cycles
float div = (float)clock_get_hz(clk_sys) / (10 * max_step_rate);
sm_config_set_clkdiv(&c, div);
}
// When requesting the current count we may have to wait a few cycles (average
// ~11 sysclk cycles) for the state machine to reply. If we are reading multiple
// encoders, we may request them all in one go and then fetch them all, thus
// avoiding doing the wait multiple times. If we are reading just one encoder,
// we can use the "get_count" function to request and wait
static inline void quadrature_encoder_request_count(PIO pio, uint sm)
{
pio->txf[sm] = 1;
}
static inline int32_t quadrature_encoder_fetch_count(PIO pio, uint sm)
{
while (pio_sm_is_rx_fifo_empty(pio, sm))
tight_loop_contents();
return pio->rxf[sm];
pio_sm_init(pio, sm, 0, &c);
pio_sm_set_enabled(pio, sm, true);
}
static inline int32_t quadrature_encoder_get_count(PIO pio, uint sm)
{
quadrature_encoder_request_count(pio, sm);
return quadrature_encoder_fetch_count(pio, sm);
uint ret;
int n;
// if the FIFO has N entries, we fetch them to drain the FIFO,
// plus one entry which will be guaranteed to not be stale
n = pio_sm_get_rx_fifo_level(pio, sm) + 1;
while (n > 0) {
ret = pio_sm_get_blocking(pio, sm);
n--;
}
return ret;
}
%}