Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice
Diff: main.cpp
- Revision:
- 29:806e9281af2f
- Parent:
- 28:7fccaef8fa72
- Child:
- 30:7873beb54744
--- a/main.cpp Sat Dec 22 21:15:49 2018 +0000
+++ b/main.cpp Fri Dec 28 10:04:33 2018 +0000
@@ -39,17 +39,19 @@
Mutex bufferLock;
//Queues
-EventQueue SDqueue(32*EVENTS_EVENT_SIZE);
+EventQueue SDqueue(32*EVENTS_EVENT_SIZE); //changed from 32
EventQueue LCDqueue(32*EVENTS_EVENT_SIZE);
EventQueue serialqueue(32*EVENTS_EVENT_SIZE);
+
+
//Threads
Thread producer_thread(osPriorityHigh);
Thread consumer_thread;
Thread serial_thread(osPriorityAboveNormal);
-Thread SDqueue_thread; //remove
+Thread SDqueue_thread; //take out queue in name?
Thread LCDqueue_thread;
-Thread Network_thread;
+Thread network_thread;
//TEST FOR SD CARD
Thread SDmount_thread;
@@ -88,7 +90,38 @@
int32_t Nsamples;
+//TEST FOR WATCHDOG
+char threadstates = 0;
+
+Timeout producer_tout;
+Timeout consumer_tout;
+Timeout serial_tout;
+Timeout SD_tout;
+Timeout LCD_tout;
+Timeout network_tout;
+
+Thread watchdog_thread(osPriorityHigh);
+
+void producer_toutISR(void);
+void consumer_toutISR(void);
+void serial_toutISR(void);
+void SD_toutISR(void);
+void LCD_toutISR(void);
+void network_toutISR(void);
+void watchdog(void);
+
+osThreadId main_thread;
+
+#define PRODUCER 1<<0
+#define CONSUMER 1<<1
+#define SERIAL 1<<2
+#define SD 1<<3
+#define LCD 1<<4
+#define NETWORK 1<<5
+
int main() {
+
+ main_thread = Thread::gettid();
timeData = new tm;
pc = new RawSerial(USBTX, USBRX);
@@ -102,20 +135,23 @@
SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever));
LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever));
serial_thread.start(callback(&serialqueue, &EventQueue::dispatch_forever));
- Network_thread.start(network);
+ network_thread.start(network);
producer_thread.start(sampleProducer);
consumer_thread.start(sampleConsumer);
//TEST FOR SD CARD
SDmount_thread.start(SDmount);
+ //TEST FOR WATCGDOG
+ watchdog_thread.start(watchdog);
+
//Attach ISRs
sample.attach(&sampleISR, sample_rate); //Allow sampling to start
pc->attach(serialISR, Serial::RxIrq);
//TEST FOR SD CARD MOUNT AND UNMOUNT
- usersw.rise(&userswRisingEdge);
+ usersw.rise(&userswRisingEdge);
//Flash to indicate goodness
while(true) {
@@ -142,16 +178,30 @@
void serialData()
{
static int i = 0;
+
if (pc->readable())
{
cmdBuffer[i] = pc->getc();
- if (cmdBuffer[i] == '\r')
+ if (i != 29)
{
- cmdBuffer[i+1]==NULL;
+
+ if (cmdBuffer[i] == '\b')
+ {
+ i = (i ? i-1 : 0);
+ }
+ else if (cmdBuffer[i] == '\r')
+ {
+ cmdBuffer[i+1]==NULL;
+ serialqueue.call(serialterm);
+ i = 0;
+ }
+ else i++;
+ }
+ else
+ {
serialqueue.call(serialterm);
- i = 0;
+ i = 0;
}
- else i++;
}
pc->attach(serialISR, Serial::RxIrq);
}
@@ -162,9 +212,13 @@
while(true)
{
//High priority thread
- Thread::signal_wait(TAKE_SAMPLE);
+ Thread::signal_wait(TAKE_SAMPLE);
+ //wd_thread.signal_set(PROD_SIGNAL);
+ //prod_stat = 0;
+ producer_tout.attach(producer_toutISR, TOUT_TIME);
Nspaces = spaceAvailable.wait(0); //Non-blocking
- bufferLock.lock();
+ bufferLock.lock();
+
//Update buffer
newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR
@@ -177,7 +231,6 @@
buffer[newestIndex].updatepress(sensor.getPressure());
buffer[newestIndex].updatelight(adcIn.read());
buffer[newestIndex].updateTime();
- //bufferLock.unlock(); //normally here, moved due to updating queues.
if (Nspaces != 0)
{
@@ -192,13 +245,13 @@
if(logging)
{
printlock.lock();
- pc->printf("%s: Sample placed in buffer at position %d\r\n", buffer[newestIndex].getTime(), newestIndex);
+ pc->printf("Sample placed in buffer at position %d\r\n", newestIndex);
pc->printf("Number of spaces available in buffer:%d\r\n\n",Nspaces);
printlock.unlock();
}
bufferLock.unlock();
-
+ producer_tout.detach();
}
}
@@ -210,6 +263,7 @@
//write to the SD card from oldestindex up to newestIndex.
Nsamples = samplesInBuffer.wait(); //Block if no samples to take - acquires
+ consumer_tout.attach(consumer_toutISR,TOUT_TIME);
if (sd_init)
{
@@ -260,7 +314,7 @@
samplesInBuffer.release();
}
-
+ consumer_tout.detach();
}
}
@@ -296,3 +350,111 @@
userswState = EDGE_FALLEN; //Flag state
userswTimeOut.attach(&userswTimeOutHandler, 0.2); //Start timeout counter - may want to increase this
}
+
+//TEST FOR WATCHDOG
+
+//ISR
+void producer_toutISR(void)
+{
+ threadstates |= PRODUCER;
+}
+
+void consumer_toutISR(void)
+{
+ threadstates |= CONSUMER;
+}
+
+void serial_toutISR(void)
+{
+ threadstates |= SERIAL;
+}
+
+void SD_toutISR(void)
+{
+ threadstates |= SD;
+}
+
+void LCD_toutISR(void)
+{
+ threadstates |= LCD;
+}
+
+void network_toutISR(void)
+{
+ threadstates |= NETWORK;
+}
+
+void watchdog(void)
+{
+ while(true)
+ {
+ Thread::wait(10000);
+
+ if(threadstates)
+ {
+ producer_thread.terminate();
+ consumer_thread.terminate();
+ serial_thread.terminate();
+ SDqueue_thread.terminate();
+ LCDqueue_thread.terminate();
+ network_thread.terminate();
+
+ pc->printf("THREAD PSW: 0x%x\n\r", threadstates);
+
+ switch (threadstates)
+ {
+ case (PRODUCER) :
+ pc->printf("PRODUCER THREAD DEADLOCK\r\n\n");
+ lcd.cls();
+ lcd.printf("PRODUCER\nDEADLOCK");
+ break;
+
+ case (CONSUMER) :
+ pc->printf("CONSUMER THREAD DEADLOCK\r\n\n");
+ lcd.cls();
+ lcd.printf("CONSUMER\nDEADLOCK");
+ break;
+
+ case (SERIAL) :
+ pc->printf("SERIAL THREAD DEADLOCK\r\n\n");
+ lcd.cls();
+ lcd.printf("SERIAL\nDEADLOCK");
+ break;
+
+ case (SD) :
+ pc->printf("SD CARD THREAD DEADLOCK\r\n\n");
+ lcd.cls();
+ lcd.printf("SD CARD\nDEADLOCK");
+ break;
+
+ case (LCD) :
+ pc->printf("LCD THREAD DEADLOCK\r\n\n");
+ lcd.cls();
+ lcd.printf("LCD\nDEADLOCK");
+ break;
+
+ case (NETWORK) :
+ pc->printf("NETWORK THREAD DEADLOCK\r\n\n");
+ lcd.cls();
+ lcd.printf("NETWORK\nDEADLOCK");
+ break;
+
+ default:
+ pc->printf("MULTIPLE THREAD DEADLOCK\r\n\n");
+ lcd.cls();
+ lcd.printf("DEADLOCK");
+ break;
+ }
+
+ for (int i = 0;i<50;i++)
+ {
+ redLED = 1;
+ wait(0.05);
+ redLED = 0;
+ wait(0.05);
+ }
+ NVIC_SystemReset();
+ }
+ else if (logging) {printlock.lock(); pc->printf("WATCHDOG RAN WITH NO DEADLOCKED THREADS\r\n"); printlock.unlock();}
+ }
+}
\ No newline at end of file
