Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hasumikin committed Dec 16, 2022
1 parent d683d36 commit 9a85394
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 235 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ add_executable(${PROJECT_NAME} ${SRCS})

set(PICORBC ${CMAKE_SOURCE_DIR}/lib/picoruby/bin/picorbc)

pico_generate_pio_header(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/uart_tx.pio)
pico_generate_pio_header(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/ws2812.pio)
pico_generate_pio_header(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/sounder.pio)

Expand Down
19 changes: 0 additions & 19 deletions include/uart.h

This file was deleted.

6 changes: 2 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "../include/msc_disk.h"
#include "../include/gpio.h"
#include "../include/usb_descriptors.h"
#include "../include/uart.h"
#include "../include/ws2812.h"
#include "../include/rotary_encoder.h"
#include "../include/joystick.h"
Expand Down Expand Up @@ -334,18 +333,17 @@ int main() {
#endif
GPIO_INIT();
USB_INIT();
UART_INIT();
mrbc_load_model(object);
mrbc_load_model(float_ext);
mrbc_create_task(usb_task, 0);
tcb_rgb = mrbc_create_task(rgb_task, 0);
mrbc_define_method(0, mrbc_class_object, "autoreload_ready?", c_autoreload_ready_q);
mrbc_define_method(0, mrbc_class_object, "_prk_description", c__prk_description);
mrbc_define_method(0, mrbc_class_object, "_prk_description", c__prk_description);
#ifdef PRK_NO_MSC
mrbc_create_task(keymap, 0);
autoreload_state = AUTORELOAD_NONE;
#else
mrbc_define_method(0, mrbc_class_object, "resume_rgb_task", c_resume_rgb_task);
mrbc_define_method(0, mrbc_class_object, "resume_rgb_task", c_resume_rgb_task);
autoreload_state = AUTORELOAD_READY;
#endif
mrbc_run();
Expand Down
3 changes: 1 addition & 2 deletions src/sounder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#include "hardware/clocks.h"
#include "hardware/irq.h"
#include "ws2812.pio.h"
#include "uart_tx.pio.h"
#include "sounder.pio.h"

#include "../include/sounder.h"
#include "picoruby-prk-sounder/include/prk-sounder.h"

#define PIO_SOUNDER_INST_HEAD ( uart_tx_program.length + ws2812_program.length )
#define PIO_SOUNDER_INST_HEAD (ws2812_program.length)
#define SOUNDER_PIO_FINISH_IRQ_ID PIO1_IRQ_1

static PIO pio = pio1;
Expand Down
160 changes: 15 additions & 145 deletions src/uart.c
Original file line number Diff line number Diff line change
@@ -1,124 +1,38 @@
#include "../include/uart.h"

#include <mrubyc.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
#include "hardware/pio.h"
#include "uart_tx.pio.h"
#include "pico/multicore.h"

#define UART_ID uart0
#define BAUD_RATE 115200
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
#include "picoruby-prk-keyboard/include/prk-keyboard.h"

#define DATA_BITS 8
#define SAMPLING_INTERVAL 10
#define SAMPLING_COUNT 8
/* (eg) 1-bit interval of 9600 bps is about 104 microseconds (usec) */
#define BIT_INTERVAL (SAMPLING_INTERVAL * SAMPLING_COUNT)
#define LONG_STOP_BITS 8
#define SHORT_STOP_BITS 2
#define IDLE_BITS 8
#define NIL 0xFFFFFF

#define PIO_UART_INST_HEAD 0

static int uart_pin;
static bool mutual = false;

/*
* Mutual UART on a single line may break your microcontroller.
*/
void
c_mutual_uart_eq(mrb_vm *vm, mrb_value *v, int argc)
{
if (GET_ARG(1).tt == MRBC_TT_TRUE) {
mutual = true;
} else {
mutual = false;
}
}

void
c_uart_anchor_init(mrb_vm *vm, mrb_value *v, int argc)
{
if (mutual) {
uart_pin = GET_INT_ARG(1);
gpio_init(uart_pin);
gpio_set_dir(uart_pin, GPIO_OUT);
gpio_put(uart_pin, 0);
} else {
uart_init(UART_ID, 2400);
gpio_set_function(GET_INT_ARG(1), GPIO_FUNC_UART); // Set the RX pin
uart_set_baudrate(UART_ID, BAUD_RATE);
uart_set_hw_flow(UART_ID, false, false); // Turn UART flow control off
uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY); // Set data format
uart_set_fifo_enabled(UART_ID, true); // Turn on FIFO
}
}

/*
* It seems RX pin can't be inverted to TX on Raspi Pico.
* That's why using PIO instead.
*/
static PIO pio = pio1;
static uint sm = 1;
void
c_uart_partner_init(mrb_vm *vm, mrb_value *v, int argc)
Keyboard_uart_anchor_init(uint32_t pin)
{
if (mutual) {
uart_pin = GET_INT_ARG(1);
gpio_init(uart_pin);
gpio_set_dir(uart_pin, GPIO_IN);
gpio_pull_up(uart_pin);
} else {
if( pio_can_add_program_at_offset(pio, &uart_tx_program, PIO_UART_INST_HEAD) ) {
pio_add_program_at_offset(pio, &uart_tx_program, PIO_UART_INST_HEAD);
uart_tx_program_init(pio, sm, PIO_UART_INST_HEAD, GET_INT_ARG(1), BAUD_RATE);
}
}
}

/*
* mutual == false
*/
uint32_t
oneway_anchor_get24(void)
{
uint32_t data24 = 0;
for (int i = 0; i < 24; i += 8) {
if (uart_is_readable(UART_ID) == 0) {
data24 |= 0xFF << i;
} else {
data24 |= uart_getc(UART_ID) << i;
}
}
/* Getting rid of surpluses */
while (uart_is_readable(UART_ID)) uart_getc(UART_ID);
return data24;
uart_pin = pin;
gpio_init(uart_pin);
gpio_set_dir(uart_pin, GPIO_OUT);
gpio_put(uart_pin, 0);
}

void
oneway_partner_put24(uint32_t data24)
Keyboard_uart_partner_init(uint32_t pin)
{
uint8_t data1 = data24 & 0xFF;;
uint8_t data2 = (data24 >> 8) & 0xFF;
uint8_t data3 = data24 >> 16;
if (data1 != 0xFF)
pio_sm_put_blocking(pio, sm, data1);
if (data1 != data2)
pio_sm_put_blocking(pio, sm, data2);
if (data2 != data3 && data1 != data3)
pio_sm_put_blocking(pio, sm, data3);
uart_pin = pin;
gpio_init(uart_pin);
gpio_set_dir(uart_pin, GPIO_IN);
gpio_pull_up(uart_pin);
}

/*
* mutual == true
*/

uint32_t
__not_in_flash_func(mutual_anchor_put8_get24_nonblocking)(uint8_t data)
__not_in_flash_func(Keyboard_mutual_anchor_put8_get24_nonblocking)(uint8_t data)
{
uint8_t i;
{ /* TX */
Expand Down Expand Up @@ -233,7 +147,7 @@ __not_in_flash_func(mutual_anchor_put8_get24_nonblocking)(uint8_t data)
* Idling : 0 Until invoked
*/
uint8_t
__not_in_flash_func(mutual_partner_get8_put24_blocking)(uint32_t data24)
__not_in_flash_func(Keyboard_mutual_partner_get8_put24_blocking)(uint32_t data24)
{
uint8_t data, i;
for (;;) {
Expand Down Expand Up @@ -299,47 +213,3 @@ __not_in_flash_func(mutual_partner_get8_put24_blocking)(uint32_t data24)
return data;
}

void
c_uart_anchor(mrb_vm *vm, mrb_value *v, int argc)
{
uint32_t data;
if (mutual) {
data = mutual_anchor_put8_get24_nonblocking(GET_INT_ARG(1));
} else {
data = oneway_anchor_get24();
}
SET_INT_RETURN(data);
}

static uint32_t buffer = 0;
static int buffer_index = 0;

void
c_uart_partner_push8(mrb_vm *vm, mrb_value *v, int argc)
{
int keycode = GET_INT_ARG(1);
if (buffer_index > 2) return;
buffer |= keycode << (buffer_index * 8);
buffer_index++;
}

void
c_uart_partner(mrb_vm *vm, mrb_value *v, int argc)
{
switch (buffer_index) {
case 0: buffer = NIL; break;
case 1: buffer |= 0xFFFF00; break;
case 2: buffer |= 0xFF0000; break;
}
uint8_t data;
if (mutual) {
data = mutual_partner_get8_put24_blocking(buffer);
} else {
oneway_partner_put24(buffer);
data = 0;
}
SET_INT_RETURN(data);
buffer = 0;
buffer_index = 0;
}

61 changes: 0 additions & 61 deletions src/uart_tx.pio

This file was deleted.

3 changes: 1 addition & 2 deletions src/ws2812.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include <stdlib.h>
#include "hardware/pio.h"
#include "ws2812.pio.h"
#include "uart_tx.pio.h"
#include "hardware/clocks.h"
#include "hardware/dma.h"
#include "hardware/sync.h"

#include "../include/ws2812.h"

#define PIO_WS2812_INST_HEAD uart_tx_program.length
#define PIO_WS2812_INST_HEAD 0

static PIO pio = pio1;
static uint sm = 0;
Expand Down

0 comments on commit 9a85394

Please sign in to comment.