Skip to content

Commit 678268d

Browse files
committed
xxx more working but stil broken, dumb coexistence
1 parent 25d4f1e commit 678268d

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

lib/pbio/drv/adc/adc_ev3.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <stdint.h>
1010
#include <string.h>
1111

12-
#include "../core.h"
13-
1412
#include <pbdrv/adc.h>
1513
#include <pbdrv/clock.h>
1614
#include <pbdrv/gpio.h>
@@ -30,6 +28,8 @@
3028
#include "../drv/block_device/block_device_ev3.h"
3129
#include "../drv/gpio/gpio_ev3.h"
3230

31+
#include "../sys/storage.h"
32+
3333
#define PBDRV_CONFIG_ADC_EV3_NUM_DELAY_SAMPLES (2)
3434

3535
/**
@@ -85,6 +85,9 @@ static const uint32_t channel_cmd[PBDRV_CONFIG_ADC_EV3_ADC_NUM_CHANNELS + PBDRV_
8585
static volatile uint16_t channel_data[PBDRV_CONFIG_ADC_EV3_ADC_NUM_CHANNELS + PBDRV_CONFIG_ADC_EV3_NUM_DELAY_SAMPLES];
8686

8787
static int adc_soon;
88+
// Used to block ADC from interfering with flash upon shutdown
89+
static int shut_down_hack = 0;
90+
static int shut_down_hack_done = 0;
8891

8992
static pbdrv_adc_callback_t pbdrv_adc_callbacks[1];
9093
static uint32_t pbdrv_adc_callback_count = 0;
@@ -121,16 +124,21 @@ pbio_error_t pbdrv_adc_ev3_process_thread(pbio_os_state_t *state, void *context)
121124

122125
PBIO_OS_ASYNC_BEGIN(state);
123126

124-
PBIO_OS_AWAIT_UNTIL(state, pbdrv_block_device_ev3_init_is_done());
127+
// HACK: This waits until storage is completely done with SPI flash before we start
128+
PBIO_OS_AWAIT_UNTIL(state, pbsys_storage_settings_get_settings());
125129

126130
// Once SPI flash init is finished, there is nothing further for us to do.
127131
// We are ready to start sampling.
128-
pbdrv_init_busy_down();
129132

130133
pbio_os_timer_set(&timer, ADC_SAMPLE_PERIOD);
131134

132135
for (;;) {
133-
PBIO_OS_AWAIT_UNTIL(state, adc_soon || pbio_os_timer_is_expired(&timer));
136+
PBIO_OS_AWAIT_UNTIL(state, shut_down_hack || adc_soon || pbio_os_timer_is_expired(&timer));
137+
138+
if (shut_down_hack) {
139+
shut_down_hack_done = 1;
140+
break;
141+
}
134142

135143
if (adc_soon) {
136144
adc_soon = 0;
@@ -156,8 +164,9 @@ pbio_error_t pbdrv_adc_ev3_process_thread(pbio_os_state_t *state, void *context)
156164
}
157165

158166
void pbdrv_adc_init(void) {
159-
// Immediately go into async mode so that we can wait for the SPI flash driver
160-
pbdrv_init_busy_up();
167+
// Immediately go into async mode so that we can wait for the SPI flash driver.
168+
// We *don't* want to block the initial init phase, or else things will deadlock.
169+
161170
pbio_os_process_start(&pbdrv_adc_ev3_process, pbdrv_adc_ev3_process_thread, NULL);
162171
}
163172

@@ -175,4 +184,12 @@ void pbdrv_adc_ev3_configure_data_format() {
175184
SPICharLengthSet(SOC_SPI_0_REGS, 16, SPI_DATA_FORMAT1);
176185
}
177186

187+
void pbdrv_adc_ev3_shut_down_hack() {
188+
shut_down_hack = 1;
189+
pbio_os_request_poll();
190+
}
191+
int pbdrv_adc_ev3_is_shut_down_hack() {
192+
return shut_down_hack_done;
193+
}
194+
178195
#endif // PBDRV_CONFIG_ADC_EV3

lib/pbio/drv/block_device/block_device_ev3.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ static struct {
6565
/** HAL Transfer status */
6666
volatile spi_status_t spi_status;
6767

68-
// Used to sequence startup with ADC.
69-
int init_done;
70-
7168
// This is used when SPI only needs to receive. It should always stay as 0.
7269
uint8_t tx_dummy_byte;
7370
// This is used when received data is to be discarded. Its value should be ignored.
@@ -536,7 +533,6 @@ pbio_error_t pbdrv_block_device_read(pbio_os_state_t *state, uint32_t offset, ui
536533

537534
// Set address for this read request and send it.
538535
set_address_be(&read_address[1], PBDRV_CONFIG_BLOCK_DEVICE_EV3_START_ADDRESS + offset + size_done);
539-
PBIO_OS_AWAIT_WHILE(state, bdev.spi_status & SPI_STATUS_WAIT_ANY);
540536
err = spi_begin_for_flash(read_address, sizeof(read_address), 0, buffer + size_done, size_now);
541537
if (err != PBIO_SUCCESS) {
542538
return err;
@@ -557,7 +553,6 @@ static pbio_error_t flash_wait_write(pbio_os_state_t *state) {
557553
PBIO_OS_ASYNC_BEGIN(state);
558554

559555
do {
560-
PBIO_OS_AWAIT_WHILE(state, bdev.spi_status & SPI_STATUS_WAIT_ANY);
561556
err = spi_begin_for_flash(cmd_status, sizeof(cmd_status), 0, 0, 0);
562557
if (err != PBIO_SUCCESS) {
563558
return err;
@@ -585,10 +580,16 @@ pbio_error_t pbdrv_block_device_store(pbio_os_state_t *state, uint8_t *buffer, u
585580
return PBIO_ERROR_INVALID_ARG;
586581
}
587582

583+
#if PBDRV_CONFIG_ADC_EV3
584+
// HACK
585+
// We only store on shutdown. Block ADC.
586+
pbdrv_adc_ev3_shut_down_hack();
587+
PBIO_OS_AWAIT_UNTIL(state, pbdrv_adc_ev3_is_shut_down_hack());
588+
#endif
589+
588590
// Erase sector by sector.
589591
for (offset = 0; offset < size; offset += FLASH_SIZE_ERASE) {
590592
// Enable writing
591-
PBIO_OS_AWAIT_WHILE(state, bdev.spi_status & SPI_STATUS_WAIT_ANY);
592593
err = spi_begin_for_flash(cmd_write_enable, sizeof(cmd_write_enable), 0, 0, 0);
593594
if (err != PBIO_SUCCESS) {
594595
return err;
@@ -597,7 +598,6 @@ pbio_error_t pbdrv_block_device_store(pbio_os_state_t *state, uint8_t *buffer, u
597598

598599
// Erase this block
599600
set_address_be(&erase_address[1], PBDRV_CONFIG_BLOCK_DEVICE_EV3_START_ADDRESS + offset);
600-
PBIO_OS_AWAIT_WHILE(state, bdev.spi_status & SPI_STATUS_WAIT_ANY);
601601
err = spi_begin_for_flash(erase_address, sizeof(erase_address), 0, 0, 0);
602602
if (err != PBIO_SUCCESS) {
603603
return err;
@@ -616,7 +616,6 @@ pbio_error_t pbdrv_block_device_store(pbio_os_state_t *state, uint8_t *buffer, u
616616
size_now = pbio_int_math_min(size - size_done, FLASH_SIZE_WRITE);
617617

618618
// Enable writing
619-
PBIO_OS_AWAIT_WHILE(state, bdev.spi_status & SPI_STATUS_WAIT_ANY);
620619
err = spi_begin_for_flash(cmd_write_enable, sizeof(cmd_write_enable), 0, 0, 0);
621620
if (err != PBIO_SUCCESS) {
622621
return err;
@@ -625,7 +624,6 @@ pbio_error_t pbdrv_block_device_store(pbio_os_state_t *state, uint8_t *buffer, u
625624

626625
// Write this block
627626
set_address_be(&write_address[1], PBDRV_CONFIG_BLOCK_DEVICE_EV3_START_ADDRESS + size_done);
628-
PBIO_OS_AWAIT_WHILE(state, bdev.spi_status & SPI_STATUS_WAIT_ANY);
629627
err = spi_begin_for_flash(write_address, sizeof(write_address), buffer + size_done, 0, size_now);
630628
if (err != PBIO_SUCCESS) {
631629
return err;
@@ -663,8 +661,6 @@ pbio_error_t pbdrv_block_device_ev3_init_process_thread(pbio_os_state_t *state,
663661

664662
// Initialization done.
665663
pbdrv_init_busy_down();
666-
bdev.init_done = 1;
667-
pbio_os_request_poll();
668664

669665
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
670666
}
@@ -720,11 +716,6 @@ void pbdrv_block_device_init(void) {
720716
pbio_os_process_start(&pbdrv_block_device_ev3_init_process, pbdrv_block_device_ev3_init_process_thread, NULL);
721717
}
722718

723-
// ADC glue functions
724-
int pbdrv_block_device_ev3_init_is_done() {
725-
return bdev.init_done;
726-
}
727-
728719
int pbdrv_block_device_ev3_is_busy() {
729720
return bdev.spi_status & SPI_STATUS_WAIT_ANY;
730721
}

lib/pbio/drv/block_device/block_device_ev3.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ void pbdrv_block_device_ev3_spi_rx_complete();
1616
// these two drivers have tight dependencies on each other.
1717
// These functions orchestrate the connection.
1818

19-
int pbdrv_block_device_ev3_init_is_done();
2019
int pbdrv_block_device_ev3_is_busy();
2120
pbio_error_t pbdrv_block_device_ev3_spi_begin_for_adc(const uint32_t *cmds, volatile uint16_t *data, unsigned int len);
2221

0 commit comments

Comments
 (0)