Skip to content

Commit

Permalink
Make ADC thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
ReservedField committed Jul 12, 2016
1 parent 3323fd7 commit 8f424dc
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/adc/ADC.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <M451Series.h>
#include <ADC.h>
#include <Thread.h>

/**
* \file
Expand Down Expand Up @@ -62,14 +63,13 @@ ADC_DEFINE_IRQ_HANDLER(3);

void ADC_UpdateCache(const uint8_t moduleNum[], uint8_t len, uint8_t isBlocking) {
uint8_t i, j, finishFlag;

// Enter critical section
__set_PRIMASK(1);
uint32_t primask;

for(i = 0; i < len; i++) {
// Find interrupt number for module number
for(j = 0; j < 4 && moduleNum[i] != ADC_moduleNum[j]; j++);

primask = Thread_IrqDisable();
if(!(EADC_GET_PENDING_CONV(EADC) & (1 << moduleNum[i]))) {
// Configure module
EADC_ConfigSampleModule(EADC, moduleNum[i], EADC_SOFTWARE_TRIGGER, moduleNum[i]);
Expand All @@ -81,11 +81,9 @@ void ADC_UpdateCache(const uint8_t moduleNum[], uint8_t len, uint8_t isBlocking)
// Start conversion
EADC_START_CONV(EADC, 1 << moduleNum[i]);
}
Thread_IrqRestore(primask);
}

// Exit critical section
__set_PRIMASK(0);

if(isBlocking) {
// Wait for modules to finish
// Keep in mind they could be restarted by another concurrent
Expand Down

0 comments on commit 8f424dc

Please sign in to comment.