commit - 87b91c488bec8a48b8f71d07ec67ad2e5e16cc51
commit + b80acd6a41798ab67b5cb246110c525287c26fc3
blob - 3afaead3b1ab8960c6e9c4e8fb673857ebaff134
blob + 26fec9c60684c7d333705fe2e2603ef22ba4fdc1
--- CMakeLists.txt
+++ CMakeLists.txt
pico_sdk_init()
add_executable(morse
- morse.cpp
+ morse.cpp
libmorse.cpp
- )
+ )
# pull in common dependencies
target_link_libraries(morse
pico_stdlib
pico_malloc
+ hardware_pwm
)
+add_compile_options(
+ -Wall
+ -Wno-format
+ -Wno-unused-function
+ -Wno-maybe-uninitialized
+ )
+
pico_enable_stdio_usb(morse 1)
pico_enable_stdio_uart(morse 0)
blob - 30cabb0d515fb6b0ce46c8989a0f5fadf46ddcf3
blob + 2c5b61712cfc7cfd9c8b7fdca28b4a40f6130a82
--- libmorse.cpp
+++ libmorse.cpp
#include "pico/stdlib.h"
#include "pico/malloc.h"
+#include "hardware/pwm.h"
+
#include "libmorse.h"
#define DIT 1.0
#define W_SP 7.0
#define D_WPM 10
+#define ST 600
#define UNIT_T(x) (60.0 / (50.0 * (float)x)) * 1000.0
#define bit_read(value, bit) (((value) >> (bit)) & 0x01)
static volatile uint8_t gpio_wpm, gpio_tx_pin, gpio_stop_now;
static volatile uint64_t gpio_pause;
+static volatile uint8_t dac_wpm, dac_tx_pin, dac_stop_now;
+static volatile uint64_t dac_pause;
+
static volatile float gpio_unit_t;
+static volatile float dac_unit_t;
static volatile uint8_t gpio_tx_sending = 0;
static volatile uint8_t gpio_unit_handled;
+static volatile uint8_t dac_tx_sending = 0;
+static volatile uint8_t dac_unit_handled;
+
static uint8_t ctob(uint8_t);
static uint8_t gpio_this_index = 0, gpio_next_index = 0;
static uint8_t gpio_handle_unit, gpio_bit;
static uint8_t gpio_digraph = 0, gpio_inited = 0;
static unsigned long gpio_handle_unit_millis;
+static unsigned long dac_handle_unit_millis;
+static uint8_t dac_this_index = 0, dac_next_index = 0;
+static uint8_t dac_handle_unit, dac_bit;
+static uint8_t dac_digraph = 0, dac_inited = 0;
+
static void gpio_stop(struct morse_cb_args *);
static void gpio_handle_chars(struct morse_cb_args *);
static void gpio_handle_units(struct morse_cb_args *);
-static int64_t gpio_unit_handled_cb(alarm_id_t, void *);
-static int64_t gpio_pause_handled_cb(alarm_id_t, void *);
-static int64_t gpio_tx_handled_cb(alarm_id_t, void *);
+static void dac_stop(struct morse_cb_args *);
+static void dac_handle_chars(struct morse_cb_args *);
+static void dac_handle_units(struct morse_cb_args *);
+static void pwm_set_freq_duty(uint8_t, uint8_t, uint16_t, uint8_t);
+
+static int64_t gpio_unit_handled_cb(alarm_id_t, void *);
+static int64_t gpio_pause_handled_cb(alarm_id_t, void *);
+static int64_t gpio_tx_handled_cb(alarm_id_t, void *);
+
+static int64_t dac_unit_handled_cb(alarm_id_t, void *);
+static int64_t dac_pause_handled_cb(alarm_id_t, void *);
+static int64_t dac_tx_handled_cb(alarm_id_t, void *);
+
+uint8_t slice, channel;
+
Morse::Morse(uint8_t type, uint8_t the_pin, unsigned long the_pause)
{
Morse(type, the_pin, the_pause, D_WPM);
Morse::Morse(uint8_t type, uint8_t the_pin, unsigned long the_pause,
uint8_t the_wpm)
{
+ Morse(type, the_pin, the_pause, the_wpm, ST);
+}
+
+Morse::Morse(uint8_t type, uint8_t the_pin, unsigned long the_pause,
+ uint8_t the_wpm, uint16_t st_freq)
+{
switch(type) {
case M_GPIO:
if (!gpio_inited) {
}
break;
case M_DAC:
- // we don't have a dac, but we will setup pwm
+ if (!dac_inited) {
+ dac_wpm = the_wpm;
+ dac_pause = the_pause * 1000;
+ dac_tx_pin = the_pin;
+ dac_unit_t = UNIT_T(dac_wpm);
+ dac_inited = 1;
+
+ gpio_set_function(dac_tx_pin, GPIO_FUNC_PWM);
+ slice = pwm_gpio_to_slice_num(dac_tx_pin);
+ channel = pwm_gpio_to_channel(dac_tx_pin);
+ pwm_set_freq_duty(slice, channel, st_freq, 50);
+ pwm_set_enabled(slice, false);
+ }
break;
default:
exit(1);
}
}
+/* gpio */
+
void
Morse::gpio_set_transmitting(void)
{
}
}
+/* dac */
+
+void
+Morse::dac_set_transmitting(void)
+{
+ dac_tx_sending = 1;
+}
+
+uint8_t
+Morse::dac_get_transmitting(void)
+{
+ return dac_tx_sending;
+}
+
+void
+Morse::dac_tx_stop(void)
+{
+ dac_stop_now = 1;
+}
+
+void
+Morse::dac_tx(const char *morse)
+{
+ struct morse_cb_args *mcb = NULL;
+ int i;
+
+ if (dac_tx_sending)
+ return;
+ else {
+ mcb = (struct morse_cb_args *)
+ malloc(sizeof(struct morse_cb_args *));
+
+ pwm_set_enabled(slice, false);
+ dac_tx_sending = 1;
+ dac_stop_now = 0;
+
+ mcb->len = strlen(morse);
+ mcb->morse = strndup(morse, mcb->len);
+
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ add_alarm_in_ms(dac_pause, dac_tx_handled_cb, mcb,
+ false);
+ }
+}
+
+static void
+dac_stop(struct morse_cb_args *mcb)
+{
+ dac_digraph = 0;
+ dac_tx_sending = 0;
+ dac_this_index = 0;
+ dac_next_index = 0;
+ free(mcb);
+}
+
+static int64_t
+dac_tx_handled_cb(alarm_id_t id, void *data)
+{
+ struct morse_cb_args *mcb = (struct morse_cb_args *) data;
+
+ dac_this_index = 0;
+ dac_next_index = 1;
+
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ dac_handle_chars(mcb);
+
+ return 0;
+}
+
+static int64_t
+dac_unit_handled_cb(alarm_id_t id, void *data)
+{
+ struct morse_cb_args *mcb = (struct morse_cb_args *) data;
+
+ pwm_set_enabled(slice, false);
+ dac_unit_handled = 1;
+ dac_bit++;
+
+ // handle IC_SP, or handle C_SP
+ if (mcb->c >> (dac_bit + 1) || dac_digraph)
+ dac_handle_unit_millis = IC_SP * dac_unit_t;
+ else
+ dac_handle_unit_millis = C_SP * dac_unit_t;
+
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ add_alarm_in_ms(dac_handle_unit_millis, dac_pause_handled_cb,
+ mcb,false);
+
+ return 0;
+}
+
+static int64_t
+dac_pause_handled_cb(alarm_id_t id, void *data)
+{
+ struct morse_cb_args *mcb = (struct morse_cb_args *) data;
+
+ dac_unit_handled = 0;
+ dac_handle_unit = 0;
+
+ // hit the end of that byte
+ if (!(mcb->c >> (dac_bit + 1))) {
+ dac_bit = 0;
+ dac_next_index = 1;
+ mcb->morse++;
+ dac_this_index++;
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ dac_handle_chars(mcb);
+ } else {
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ dac_handle_chars(mcb);
+ }
+
+ return 0;
+}
+
+static void
+dac_handle_chars(struct morse_cb_args *mcb)
+{
+ if (dac_this_index == mcb->len)
+ dac_stop(mcb);
+ if (dac_next_index) {
+ dac_next_index = 0;
+ dac_handle_unit = 0;
+ dac_unit_handled = 0;
+ if (*mcb->morse == 126) {
+ mcb->morse++;
+ dac_this_index++;
+ dac_next_index = 1;
+ } else if (*mcb->morse == 96) {
+ dac_digraph = !dac_digraph;
+ mcb->morse++;
+ dac_this_index++;
+ dac_next_index = 1;
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ dac_handle_chars(mcb);
+ } else {
+ mcb->c = ctob(*mcb->morse);
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ dac_handle_units(mcb);
+ dac_bit = 0;
+ }
+ } else if (dac_tx_sending) {
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ dac_handle_units(mcb);
+ }
+
+}
+
+static void
+dac_handle_units(struct morse_cb_args *mcb)
+{
+ // set led on and space off
+ if (!dac_next_index && !dac_handle_unit && !dac_unit_handled &&
+ dac_tx_sending) {
+ if (mcb->c == 1)
+ dac_handle_unit_millis = W_SP * dac_unit_t;
+ else {
+ pwm_set_enabled(slice, true);
+ if (bit_read(mcb->c, dac_bit))
+ dac_handle_unit_millis = DAH * dac_unit_t;
+ else
+ dac_handle_unit_millis = DIT * dac_unit_t;
+ }
+ dac_handle_unit = 1;
+
+ if (dac_stop_now)
+ dac_stop(mcb);
+ else
+ add_alarm_in_ms(dac_handle_unit_millis,
+ dac_unit_handled_cb, mcb, false);
+ }
+}
+
+/* other */
+
+/* edited from
+ * https://www.i-programmer.info/programming/hardware/14849-the-pico-in-c-basic-pwm.html?start=2
+ */
+
+static
+void pwm_set_freq_duty(uint8_t slice, uint8_t channel, uint16_t freq,
+ uint8_t duty)
+{
+ uint32_t clock, divider16, wrap;
+
+ clock = 125000000;
+ divider16 = clock / freq / 4096 + (clock % (freq * 4096) != 0);
+
+ if (divider16 / 16 == 0)
+ divider16 = 16;
+
+ wrap = clock * 16 / divider16 / freq - 1;
+ pwm_set_clkdiv_int_frac(slice, divider16/16, divider16 & 0xF);
+ pwm_set_wrap(slice, wrap);
+ pwm_set_chan_level(slice, channel, wrap * duty / 100);
+}
+
static uint8_t
ctob(uint8_t c)
{
blob - 88bcceb79a4274c00ea66b44a2cedbd054856d6a
blob + a64f4639497fe63ee41a64b6f74b541ce6216e77
--- libmorse.h
+++ libmorse.h
unsigned long the_pause);
Morse(uint8_t type, uint8_t the_pin,
unsigned long the_pause, uint8_t the_wpm);
+ Morse(uint8_t type, uint8_t the_pin,
+ unsigned long the_pause, uint8_t the_wpm, uint16_t st_freq);
uint8_t gpio_get_transmitting(void);
void gpio_set_transmitting(void);
void gpio_tx(const char *);
void gpio_tx_stop(void);
+
+ uint8_t dac_get_transmitting(void);
+ void dac_set_transmitting(void);
+
+ void dac_tx(const char *);
+ void dac_tx_stop(void);
};
enum
blob - 25ec03420b49f697a204655708ea42c9394b3fe8
blob + 9f636887f2db1f8af539ff2cb369566d2df4f99e
--- morse.cpp
+++ morse.cpp
#define CALLS 3
#define WPM 15
+#define DAC_WPM 10
+
#define TX_PAUSE 3 // pause before transmitting and between TXs
+#define DAC_PAUSE 5
+#define DAC_FREQ 550
+#define DAC_PIN 8
const uint pin = PICO_DEFAULT_LED_PIN;
Morse morse_led(M_GPIO, pin, TX_PAUSE, WPM);
+Morse morse_dac(M_DAC, DAC_PIN, DAC_PAUSE, DAC_WPM, DAC_FREQ);
int main(void);
}
};
+ const char *dac_morse[][CALLS] = {
+ {
+ "de bz4kz `ar`",
+ "cq cq pota cq de bz4kz k",
+ "qst qst qst gm de bz4kz 73 bk",
+ }
+ };
+
stdio_init_all();
while (1) {
if (!morse_led.gpio_get_transmitting()) {
n = 0 + (rand() % CALLS);
- printf("Sending transmission %d: %s\n", n,
+ printf("Sending LED transmission %d: %s\n", n,
led_morse[0][n]);
morse_led.gpio_tx(led_morse[0][n]);
}
+
+ if (!morse_dac.dac_get_transmitting()) {
+ n = 0 + (rand() % CALLS);
+ printf("Sending DAC transmission %d: %s\n", n,
+ dac_morse[0][n]);
+ morse_dac.dac_tx(dac_morse[0][n]);
+ }
}
return 0;