got the reorganized gyro board code actually working
diff --git a/gyro_board/src/usb/LPCUSB/USB_SENSOR_STREAM.c b/gyro_board/src/usb/LPCUSB/USB_SENSOR_STREAM.c
index 09d9205..99218cf 100644
--- a/gyro_board/src/usb/LPCUSB/USB_SENSOR_STREAM.c
+++ b/gyro_board/src/usb/LPCUSB/USB_SENSOR_STREAM.c
@@ -286,10 +286,7 @@
USBHwEPWrite(ISOC_IN_EP, (unsigned char *)&usbPacket, sizeof(usbPacket));
}
-void vUSBTask(void *pvParameters) {
- portTickType xLastFlashTime;
-
- (void) pvParameters;
+void usb_init(void) {
DBG("Initialising USB stack\n");
xRxedChars = xQueueCreate(usbRXBUFFER_LEN, sizeof(char));
@@ -329,20 +326,6 @@
DBG("Connecting to USB bus\n");
USBHwConnect(TRUE);
- xLastFlashTime = xTaskGetTickCount();
-
- vTaskDelayUntil(&xLastFlashTime, 1000 / portTICK_RATE_MS * 100);
-
- //USBHwAllowConnect();
- // echo any character received (do USB stuff in interrupt)
- for (;;) {
- // c = VCOM_getchar();
- // if (c != EOF) {
- // // Echo character back with INCREMENT_ECHO_BY offset, so for example if
- // // INCREMENT_ECHO_BY is 1 and 'A' is received, 'B' will be echoed back.
- // VCOM_putchar(c + INCREMENT_ECHO_BY);
- // }
- vTaskDelayUntil(&xLastFlashTime, 1000 / portTICK_RATE_MS);
- }
+ // Enable USB. The PC has probably disconnected it now.
+ USBHwAllowConnect();
}
-
diff --git a/gyro_board/src/usb/ParTest.c b/gyro_board/src/usb/ParTest.c
index 0404edf..fa979cd 100644
--- a/gyro_board/src/usb/ParTest.c
+++ b/gyro_board/src/usb/ParTest.c
@@ -76,7 +76,7 @@
GPIO2->FIODIR |= partstFIO1_BITS;
/* Start will all LEDs off. */
- GPIO2->FIOCLR = partstFIO1_BITS;
+ GPIO2->FIOSET = partstFIO1_BITS;
}
/*-----------------------------------------------------------*/
diff --git a/gyro_board/src/usb/encoder.c b/gyro_board/src/usb/encoder.c
index 6c4999c..205f44e 100644
--- a/gyro_board/src/usb/encoder.c
+++ b/gyro_board/src/usb/encoder.c
@@ -475,9 +475,15 @@
}
void fillSensorPacket(struct DataStruct *packet) {
- packet->gyro_angle = gyro_output.angle;
- packet->old_gyro_reading = gyro_output.last_reading_bad;
- packet->bad_gyro = gyro_output.gyro_bad;
+ if (gyro_output.initialized) {
+ packet->gyro_angle = gyro_output.angle;
+ packet->old_gyro_reading = gyro_output.last_reading_bad;
+ packet->bad_gyro = gyro_output.gyro_bad;
+ } else {
+ packet->gyro_angle = 0;
+ packet->old_gyro_reading = 1;
+ packet->bad_gyro = 0;
+ }
packet->dip_switch0 = dip_switch(0);
packet->dip_switch1 = dip_switch(1);
diff --git a/gyro_board/src/usb/gyro.c b/gyro_board/src/usb/gyro.c
index d52a698..866e890 100644
--- a/gyro_board/src/usb/gyro.c
+++ b/gyro_board/src/usb/gyro.c
@@ -46,12 +46,13 @@
}
uint16_t low_value = spi_read();
gyro_disable_csel();
- if (__builtin_parity(low_value) != 1) {
- printf("low value 0x%"PRIx16" parity error\n", high_value);
+ uint32_t r = high_value << 16 | low_value;
+ if (__builtin_parity(r) != 1) {
+ printf("low value 0x%"PRIx16" parity error (r=%"PRIx32")\n", low_value, r);
*parity_error = 1;
}
- return high_value << 16 | low_value;
+ return r;
}
// Returns all of the non-data bits in the "header" except the parity from
@@ -184,6 +185,56 @@
}
static portTASK_FUNCTION(gyro_read_task, pvParameters) {
+ // Connect power and clock.
+ SC->PCONP |= PCONP_PCSSP0;
+ SC->PCLKSEL1 &= ~(3 << 10);
+ SC->PCLKSEL1 |= 1 << 10;
+
+ // Set up SSEL.
+ // It's is just a GPIO pin because we're the master (it would be special if we
+ // were a slave).
+ gyro_disable_csel();
+ GPIO0->FIODIR |= 1 << 16;
+ PINCON->PINSEL1 &= ~(3 << 0);
+ PINCON->PINSEL1 |= 0 << 0;
+
+ // Set up MISO0 and MOSI0.
+ PINCON->PINSEL1 &= ~(3 << 2 | 3 << 4);
+ PINCON->PINSEL1 |= 2 << 2 | 2 << 4;
+
+ // Set up SCK0.
+ PINCON->PINSEL0 &= ~(3 << 30);
+ PINCON->PINSEL0 |= (2 << 30);
+
+ // Make sure it's disabled.
+ SSP0->CR1 = 0;
+ SSP0->CR0 =
+ 0xF /* 16 bit transfer */ |
+ 0 << 4 /* SPI mode */ |
+ 0 << 6 /* CPOL = 0 */ |
+ 0 << 7 /* CPHA = 0 */;
+ // 14 clocks per cycle. This works out to a ~7.2MHz bus.
+ // The gyro is rated for a maximum of 8.08MHz.
+ SSP0->CPSR = 14;
+ // Finally, enable it.
+ // This has to be done after we're done messing with everything else.
+ SSP0->CR1 |= 1 << 1;
+
+ if (gyro_setup()) {
+ printf("gyro setup failed. deleting task\n");
+ gyro_output.angle = 0;
+ gyro_output.last_reading_bad = gyro_output.gyro_bad = 1;
+ gyro_output.initialized = 1;
+ vTaskDelete(NULL);
+ return;
+ } else {
+ gyro_output.initialized = 1;
+ }
+
+ gyro_output.angle = 0;
+ gyro_output.last_reading_bad = 1; // until we're started up
+ gyro_output.gyro_bad = 0;
+
// How many times per second to read the gyro value.
static const int kGyroReadFrequency = 200;
// How many times per second to flash the LED.
@@ -288,53 +339,7 @@
}
void gyro_init(void) {
- // Connect power and clock.
- SC->PCONP |= PCONP_PCSSP0;
- SC->PCLKSEL1 &= ~(3 << 10);
- SC->PCLKSEL1 |= 1 << 10;
-
- // Set up SSEL.
- // It's is just a GPIO pin because we're the master (it would be special if we
- // were a slave).
- gyro_disable_csel();
- GPIO0->FIODIR |= 1 << 16;
- PINCON->PINSEL1 &= ~(3 << 0);
- PINCON->PINSEL1 |= 0 << 0;
-
- // Set up MISO0 and MOSI0.
- PINCON->PINSEL1 &= ~(3 << 2 | 3 << 4);
- PINCON->PINSEL1 |= 2 << 2 | 2 << 2;
-
- // Set up SCK0.
- PINCON->PINSEL0 &= ~(3 << 30);
- PINCON->PINSEL0 |= (2 << 30);
-
- // Make sure it's disabled.
- SSP0->CR1 = 0;
- SSP0->CR0 =
- 0xF /* 16 bit transfer */ |
- 0 << 4 /* SPI mode */ |
- 0 << 6 /* CPOL = 0 */ |
- 0 << 7 /* CPHA = 0 */;
- // 14 clocks per cycle. This works out to a ~7.2MHz bus.
- // The gyro is rated for a maximum of 8.08MHz.
- SSP0->CPSR = 14;
- // Set it to master mode.
- SSP0->CR1 |= 1 << 2;
- // Finally, enable it.
- // This has to be done after we're done messing with everything else.
- SSP0->CR1 |= 1 << 1;
-
- if (gyro_setup()) {
- printf("gyro setup failed. not starting task\n");
- gyro_output.angle = 0;
- gyro_output.last_reading_bad = gyro_output.gyro_bad = 1;
- return;
- }
-
- gyro_output.angle = 0;
- gyro_output.last_reading_bad = 1; // until we're started up
- gyro_output.gyro_bad = 0;
+ gyro_output.initialized = 0;
xTaskCreate(gyro_read_task, (signed char *) "gyro",
configMINIMAL_STACK_SIZE + 100, NULL,
diff --git a/gyro_board/src/usb/gyro.h b/gyro_board/src/usb/gyro.h
index 7b9521e..2c74aad 100644
--- a/gyro_board/src/usb/gyro.h
+++ b/gyro_board/src/usb/gyro.h
@@ -11,6 +11,7 @@
int64_t angle;
int last_reading_bad;
int gyro_bad;
+ int initialized;
};
// This gets updated in a portENTER_CRITICAL/portEXIT_CRITICAL() block so all of
// the values will be in sync.
diff --git a/gyro_board/src/usb/main.c b/gyro_board/src/usb/main.c
index 4a5eca4..3ac21c8 100644
--- a/gyro_board/src/usb/main.c
+++ b/gyro_board/src/usb/main.c
@@ -39,10 +39,7 @@
#include "CAN.h"
#include "gyro.h"
-/*
- * The task that handles the USB stack.
- */
-extern void vUSBTask(void *pvParameters);
+extern void usb_init(void);
// Sets up (and connects) PLL0.
// The CPU will be running at 100 MHz with a 12 MHz clock input when this is
@@ -162,11 +159,6 @@
int main(void) {
setup_hardware();
- /* Create the USB task. */
- xTaskCreate(vUSBTask, (signed char *) "USB",
- configMINIMAL_STACK_SIZE + 1020, (void *) NULL,
- tskIDLE_PRIORITY + 3, NULL);
-
digital_init();
analog_init();
@@ -175,11 +167,10 @@
gyro_init();
- // Enable USB. The PC has probably disconnected it now.
- USBHwAllowConnect();
-
initCAN();
+ usb_init();
+
// Start the scheduler.
vTaskStartScheduler();