Skip to content

Commit

Permalink
revert to manual assignment of PIO resources fixes crashing and stran…
Browse files Browse the repository at this point in the history
…geness with new claim_pio and unclaim_pio functions.
  • Loading branch information
DESKTOP-M9CCUTI\ian committed Sep 3, 2024
1 parent bbcff78 commit 233c3b1
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 62 deletions.
54 changes: 33 additions & 21 deletions binmode/logicanalyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ volatile uint32_t la_sm_done = false;
//PIO pio = pio0;
//uint sm = 0;
//static uint offset = 0;
static const struct pio_program* pio_program_active;
//static const struct pio_program* pio_program_active=0;

void logicanalyzer_reset_led(void) {
icm_core0_send_message_synchronous(BP_ICM_DISABLE_RGB_UPDATES);
Expand Down Expand Up @@ -80,9 +80,9 @@ void logic_analyser_done(void) {
irq_remove_handler(PIO0_IRQ_0+ (PIO_NUM(pio_config.pio)*2), logic_analyser_done);
pio_sm_set_enabled(pio_config.pio, pio_config.sm, false);

if (pio_program_active) {
pio_remove_program_and_unclaim_sm(pio_program_active, pio_config.pio, pio_config.sm, pio_config.offset);
pio_program_active = 0;
if (pio_config.program) {
pio_remove_program_and_unclaim_sm(pio_config.program, pio_config.pio, pio_config.sm, pio_config.offset);
pio_config.program = 0;
}

busy_wait_ms(1);
Expand Down Expand Up @@ -184,9 +184,10 @@ bool logic_analyzer_configure(
// This can be useful for debugging. The position of sampling always start at the beginning of the buffer
//restart_dma(); //this moved to below because the PIO isn't yet assigned

if (pio_program_active) {
pio_remove_program_and_unclaim_sm(pio_program_active, pio_config.pio, pio_config.sm, pio_config.offset);
pio_program_active = 0;
if (pio_config.program) {
//pio_remove_program_and_unclaim_sm(pio_config.program, pio_config.pio, pio_config.sm, pio_config.offset);
pio_remove_program(pio_config.pio, pio_config.program, pio_config.offset);
pio_config.program = 0;
}

uint8_t trigger_pin = 0;
Expand All @@ -200,29 +201,39 @@ bool logic_analyzer_configure(
}
}
}
#define LA_BASE_PIN 8 //LA_BPIO0 //0

//TODO: make base pin configurable
#define LA_BASE_PIN 0 //LA_BPIO0 //0
pio_config.pio = PIO_MODE_PIO;
pio_config.sm = PIO_LOGIC_ANALYZER_SM;

if (trigger_ok) {
if (trigger_direction & 1u << trigger_pin) // high level trigger program
{
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&logicanalyzer_high_trigger_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, LA_BASE_PIN, 8, true);
hard_assert(success);
pio_program_active = &logicanalyzer_high_trigger_program;
//bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&logicanalyzer_high_trigger_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, LA_BASE_PIN, 8, true);
//hard_assert(success);
pio_config.program = &logicanalyzer_high_trigger_program;
pio_config.offset = pio_add_program(pio_config.pio, pio_config.program);
logicanalyzer_high_trigger_program_init(pio_config.pio, pio_config.sm, pio_config.offset, LA_BASE_PIN, LA_BASE_PIN + trigger_pin, freq, edge);
} else // low level trigger program
{
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&logicanalyzer_low_trigger_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, LA_BASE_PIN, 8, true);
hard_assert(success);
pio_program_active = &logicanalyzer_low_trigger_program;
//bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&logicanalyzer_low_trigger_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, LA_BASE_PIN, 8, true);
//hard_assert(success);
pio_config.program = &logicanalyzer_low_trigger_program;
pio_config.offset = pio_add_program(pio_config.pio, pio_config.program);
logicanalyzer_low_trigger_program_init(pio_config.pio, pio_config.sm, pio_config.offset, LA_BASE_PIN, LA_BASE_PIN + trigger_pin, freq, edge);
}
} else { // else no trigger program
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&logicanalyzer_no_trigger_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, LA_BASE_PIN, 8, true);
hard_assert(success);
pio_program_active = &logicanalyzer_no_trigger_program;
//bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&logicanalyzer_no_trigger_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, LA_BASE_PIN, 8, true);
//hard_assert(success);
pio_config.program = &logicanalyzer_no_trigger_program; //move this before to simplify add program
pio_config.offset = pio_add_program(pio_config.pio, pio_config.program);
logicanalyzer_no_trigger_program_init(pio_config.pio, pio_config.sm, pio_config.offset, LA_BASE_PIN, freq);
}
#ifdef BP_PIO_SHOW_ASSIGNMENT
printf("pio %d, sm %d, offset %d\n", PIO_NUM(pio_config.pio), pio_config.sm, pio_config.offset);

#endif

restart_dma(); //do after PIO and SM are assigned

// interrupt on done notification
Expand Down Expand Up @@ -254,9 +265,10 @@ bool logic_analyzer_cleanup(void) {
}

// pio_clear_instruction_memory(pio);
if (pio_program_active) {
pio_remove_program_and_unclaim_sm(pio_program_active, pio_config.pio, pio_config.sm, pio_config.offset);
pio_program_active = 0;
if (pio_config.program) {
//pio_remove_program_and_unclaim_sm(pio_config.program, pio_config.pio, pio_config.sm, pio_config.offset);
pio_remove_program(pio_config.pio, pio_config.program, pio_config.offset);
pio_config.program = 0;
}

mem_free(la_buf);
Expand Down
8 changes: 4 additions & 4 deletions commands/uart/simcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ void simcard_handler(struct command_result *res){
if(!ui_help_check_vout_vref()) return;

//uart_rx_program_init(M_I2C_PIO, M_I2C_PIO_SM, pio_loaded_offset, bio2bufiopin[BIO0], 9600);
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&uart_rx_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, bio2bufiopin[BIO0], 1, true);
hard_assert(success);
printf("PIO: pio=%d, sm=%d, offset=%d\r\n", PIO_NUM(pio_config.pio), pio_config.sm, pio_config.offset);
pio_remove_program_and_unclaim_sm(&uart_rx_program, pio_config.pio, pio_config.sm, pio_config.offset);
//bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&uart_rx_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, bio2bufiopin[BIO0], 1, true);
//hard_assert(success);
//printf("PIO: pio=%d, sm=%d, offset=%d\r\n", PIO_NUM(pio_config.pio), pio_config.sm, pio_config.offset);
//pio_remove_program_and_unclaim_sm(&uart_rx_program, pio_config.pio, pio_config.sm, pio_config.offset);

}
30 changes: 21 additions & 9 deletions lib/pico_ir_nec/nec_transmit.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ static struct _pio_config pio_config_control;
int nec_tx_init(uint pin_num) {

// install the carrier_burst program in the PIO shared instruction space
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&nec_carrier_burst_program, &pio_config_burst.pio, &pio_config_burst.sm, &pio_config_burst.offset, pin_num, 1, true);
//bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&nec_carrier_burst_program, &pio_config_burst.pio, &pio_config_burst.sm, &pio_config_burst.offset, pin_num, 1, true);
//hard_assert(success);
if(!success) {
/*if(!success) {
printf("Failed to claim free state machine for carrier_burst program\r\n");
return -1;
}
}*/
pio_config_burst.pio = PIO_MODE_PIO;
pio_config_burst.sm = 0;
pio_config_burst.program = &nec_carrier_burst_program;
pio_config_burst.offset = pio_add_program(pio_config_burst.pio, pio_config_burst.program);
#ifdef BP_PIO_SHOW_ASSIGNMENT
printf("PIO: pio=%d, sm=%d, offset=%d\r\n", PIO_NUM(pio_config_burst.pio), pio_config_burst.sm, pio_config_burst.offset);

#endif
// configure and enable the state machine
nec_carrier_burst_program_init(pio_config_burst.pio,
pio_config_burst.sm,
Expand All @@ -46,13 +51,18 @@ int nec_tx_init(uint pin_num) {
38.222e3); // 38.222 kHz carrier

// install the carrier_control program in the PIO shared instruction space
success = pio_claim_free_sm_and_add_program_for_gpio_range(&nec_carrier_control_program, &pio_config_control.pio, &pio_config_control.sm, &pio_config_control.offset, pin_num, 1, true);
/*success = pio_claim_free_sm_and_add_program_for_gpio_range(&nec_carrier_control_program, &pio_config_control.pio, &pio_config_control.sm, &pio_config_control.offset, pin_num, 1, true);
hard_assert(success);
if(!success) {
return -1;
}
} */
pio_config_control.pio = PIO_MODE_PIO;
pio_config_control.sm = 1;
pio_config_control.program = &nec_carrier_control_program;
pio_config_control.offset = pio_add_program(pio_config_control.pio, pio_config_control.program);
#ifdef BP_PIO_SHOW_ASSIGNMENT
printf("PIO: pio=%d, sm=%d, offset=%d\r\n", PIO_NUM(pio_config_control.pio), pio_config_control.sm, pio_config_control.offset);

#endif
// configure and enable the state machine
nec_carrier_control_program_init(pio_config_control.pio,
pio_config_control.sm,
Expand All @@ -64,8 +74,10 @@ int nec_tx_init(uint pin_num) {
}

void nec_tx_deinit(void) {
pio_remove_program_and_unclaim_sm(&nec_carrier_burst_program, pio_config_burst.pio, pio_config_burst.sm, pio_config_burst.offset);
pio_remove_program_and_unclaim_sm(&nec_carrier_control_program, pio_config_control.pio, pio_config_control.sm, pio_config_control.offset);
//pio_remove_program_and_unclaim_sm(&nec_carrier_burst_program, pio_config_burst.pio, pio_config_burst.sm, pio_config_burst.offset);
//pio_remove_program_and_unclaim_sm(&nec_carrier_control_program, pio_config_control.pio, pio_config_control.sm, pio_config_control.offset);
pio_remove_program(pio_config_burst.pio, pio_config_burst.program, pio_config_burst.offset);
pio_remove_program(pio_config_control.pio, pio_config_control.program, pio_config_control.offset);
}


Expand Down
25 changes: 17 additions & 8 deletions mode/hwled.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,33 @@ uint32_t hwled_setup(void)

uint32_t hwled_setup_exc(void){
bool success;
pio_config.pio = PIO_MODE_PIO;
pio_config.sm = 0;
switch(mode_config.device){
case M_LED_WS2812:
mode_config.baudrate=800000;
bio_buf_output(M_LED_SDO);
success = pio_claim_free_sm_and_add_program_for_gpio_range(&ws2812_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, bio2bufiopin[M_LED_SDO], 1, true);
hard_assert(success);
//success = pio_claim_free_sm_and_add_program_for_gpio_range(&ws2812_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, bio2bufiopin[M_LED_SDO], 1, true);
//hard_assert(success);
pio_config.program = &ws2812_program;
pio_config.offset = pio_add_program(pio_config.pio, pio_config.program);
#ifdef BP_PIO_SHOW_ASSIGNMENT
printf("PIO: pio=%d, sm=%d, offset=%d\r\n", PIO_NUM(pio_config.pio), pio_config.sm, pio_config.offset);
ws2812_program_init(pio_config.pio, pio_config.sm, pio_config.offset, bio2bufiopin[M_LED_SDO], (float)mode_config.baudrate, false);
#endif
ws2812_program_init(pio_config.pio, pio_config.sm, pio_config.offset, bio2bufiopin[M_LED_SDO], (float)mode_config.baudrate, false);
system_bio_claim(true, M_LED_SDO, BP_PIN_MODE, pin_labels[0]);
break;
case M_LED_APA102:
mode_config.baudrate=(5 * 1000 * 1000);
bio_buf_output(M_LED_SDO);
bio_buf_output(M_LED_SCL);
success = pio_claim_free_sm_and_add_program_for_gpio_range(&apa102_mini_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, bio2bufiopin[M_LED_SDO], 1, true);
hard_assert(success);
//success = pio_claim_free_sm_and_add_program_for_gpio_range(&apa102_mini_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, bio2bufiopin[M_LED_SDO], 1, true);
//hard_assert(success);
pio_config.program = &apa102_mini_program;
pio_config.offset = pio_add_program(pio_config.pio, pio_config.program);
#ifdef BP_PIO_SHOW_ASSIGNMENT
printf("PIO: pio=%d, sm=%d, offset=%d\r\n", PIO_NUM(pio_config.pio), pio_config.sm, pio_config.offset);
#endif
apa102_mini_program_init(pio_config.pio, pio_config.sm, pio_config.offset, mode_config.baudrate, bio2bufiopin[M_LED_SCL], bio2bufiopin[M_LED_SDO]);
system_bio_claim(true, M_LED_SDO, BP_PIN_MODE, pin_labels[0]);
system_bio_claim(true, M_LED_SCL, BP_PIN_MODE, pin_labels[1]);
Expand Down Expand Up @@ -214,10 +224,9 @@ void hwled_macro(uint32_t macro){
void hwled_cleanup(void){
switch(device_cleanup){
case M_LED_WS2812:
pio_remove_program_and_unclaim_sm(&ws2812_program, pio_config.pio, pio_config.sm, pio_config.offset);
break;
case M_LED_APA102:
pio_remove_program_and_unclaim_sm(&apa102_mini_program, pio_config.pio, pio_config.sm, pio_config.offset);
//pio_remove_program_and_unclaim_sm(pio_config.program, pio_config.pio, pio_config.sm, pio_config.offset);
pio_remove_program(pio_config.pio, pio_config.program, pio_config.offset);
break;
case M_LED_WS2812_ONBOARD:
rgb_irq_enable(true);
Expand Down
1 change: 1 addition & 0 deletions pio_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ struct _pio_config{
PIO pio;
int sm;
uint offset;
const struct pio_program* program;
};
13 changes: 13 additions & 0 deletions pirate.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ void lcd_irq_enable(int16_t repeat_interval);
void lcd_irq_disable(void);
void spi_busy_wait(bool enable);

#if BP_VER ==6
#define PIO_RGB_LED_PIO pio2
#define PIO_RGB_LED_SM 0
#else
#define PIO_RGB_LED_PIO pio0
#define PIO_RGB_LED_SM 0
#endif

#define PIO_LOGIC_ANALYZER_PIO pio0
#define PIO_LOGIC_ANALYZER_SM 1

#define PIO_MODE_PIO pio1
// all SM reserved for mode

// 1wire settings
#define M_OW_OWD BIO3
Expand Down
12 changes: 9 additions & 3 deletions pirate/hw1wire_pio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ void onewire_init(uint pin, uint dir){
//owobj.sm = sm;
owobj.pin = pin;
owobj.dir = dir;
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&onewire_program, &owobj.pio, &owobj.sm, &owobj.offset, dir, 9, true);
hard_assert(success);
//bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&onewire_program, &owobj.pio, &owobj.sm, &owobj.offset, dir, 9, true);
//hard_assert(success);
owobj.pio = PIO_MODE_PIO;
owobj.sm = 0;
owobj.offset = pio_add_program(owobj.pio, &onewire_program);
#ifdef BP_PIO_SHOW_ASSIGNMENT
printf("pio %d, sm %d, offset %d\n", PIO_NUM(owobj.pio), owobj.sm, owobj.offset);
#endif
onewire_program_init(owobj.pio, owobj.sm, owobj.offset, owobj.pin, owobj.dir);
onewire_set_fifo_thresh(8);
pio_sm_set_enabled(owobj.pio, owobj.sm, true);
}

void onewire_cleanup(void){
pio_remove_program_and_unclaim_sm(&onewire_program, owobj.pio, owobj.sm, owobj.offset);
//pio_remove_program_and_unclaim_sm(&onewire_program, owobj.pio, owobj.sm, owobj.offset);
pio_remove_program(owobj.pio, &onewire_program, owobj.offset);
}

void onewire_set_fifo_thresh(uint thresh) {
Expand Down
13 changes: 10 additions & 3 deletions pirate/hw2wire_pio.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ const int PIO_HW2WIRE_NAK_LSB = 0;
static struct _pio_config pio_config;

void pio_hw2wire_init(uint sda, uint scl, uint dir_sda, uint dir_scl, uint baudrate){
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&hw2wire_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, RGB_CDO, 1, true);
hard_assert(success);
//bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&hw2wire_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, RGB_CDO, 1, true);
//hard_assert(success);
pio_config.pio = PIO_MODE_PIO;
pio_config.sm = 0;
pio_config.program = &hw2wire_program;
pio_config.offset = pio_add_program(pio_config.pio, pio_config.program);
#ifdef BP_PIO_SHOW_ASSIGNMENT
printf("PIO: pio=%d, sm=%d, offset=%d\r\n", PIO_NUM(pio_config.pio), pio_config.sm, pio_config.offset);
#endif
hw2wire_program_init(pio_config.pio, pio_config.sm, pio_config.offset, sda, scl, dir_sda, dir_scl, baudrate);
}

void pio_hw2wire_cleanup(void){
pio_remove_program_and_unclaim_sm(&hw2wire_program, pio_config.pio, pio_config.sm, pio_config.offset);
//pio_remove_program_and_unclaim_sm(&hw2wire_program, pio_config.pio, pio_config.sm, pio_config.offset);
pio_remove_program(pio_config.pio, pio_config.program, pio_config.offset);
}

bool pio_hw2wire_check_error(PIO pio, uint sm) {
Expand Down
14 changes: 10 additions & 4 deletions pirate/hwi2c_pio.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ const int PIO_I2C_DATA_LSB = 1;
const int PIO_I2C_NAK_LSB = 0;

void pio_i2c_init(uint sda, uint scl, uint dir_sda, uint dir_scl, uint baudrate){
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&i2c_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, dir_sda, 10, true);
hard_assert(success);
//bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&i2c_program, &pio_config.pio, &pio_config.sm, &pio_config.offset, dir_sda, 10, true);
//hard_assert(success);
pio_config.pio = PIO_MODE_PIO;
pio_config.sm = 0;
pio_config.program = &i2c_program;
pio_config.offset = pio_add_program(pio_config.pio, pio_config.program);
#ifdef BP_PIO_SHOW_ASSIGNMENT
printf("PIO: pio=%d, sm=%d, offset=%d\r\n", PIO_NUM(pio_config.pio), pio_config.sm, pio_config.offset);

#endif
i2c_program_init(pio_config.pio, pio_config.sm, pio_config.offset, sda, scl, dir_sda, dir_scl, baudrate);
}

void pio_i2c_cleanup(void){
pio_remove_program_and_unclaim_sm(&i2c_program, pio_config.pio, pio_config.sm, pio_config.offset);
//pio_remove_program_and_unclaim_sm(&i2c_program, pio_config.pio, pio_config.sm, pio_config.offset);
pio_remove_program(pio_config.pio, pio_config.program, pio_config.offset);
}

bool pio_i2c_check_error(void) {
Expand Down
Loading

0 comments on commit 233c3b1

Please sign in to comment.