blob: 46bb34a9c872bb44cd0a7f05eaca56dfb4efc01e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/*
* I2C.cpp
*
* Created on: 13 May 2019
* Author: David
*/
#include "I2C.h"
#include "Tasks.h"
#if defined(I2C_IFACE)
static bool i2cInitialised = false;
#endif
// Initialise the I2C interface, if not already done
void I2C::Init()
{
#if defined(I2C_IFACE)
if (!i2cInitialised)
{
MutexLocker lock(Tasks::GetI2CMutex());
if (!i2cInitialised) // test it again, now that we own the mutex
{
I2C_IFACE.BeginMaster(I2cClockFreq);
i2cInitialised = true;
}
}
#endif
}
#if defined(I2C_IFACE)
#include "RTOSIface/RTOSIface.h"
static TaskHandle_t twiTask = nullptr; // the task that is waiting for a TWI command to complete
extern "C" void WIRE_ISR_HANDLER()
{
WIRE_INTERFACE->TWI_IDR = 0xFFFFFFFF;
if (twiTask != nullptr)
{
BaseType_t higherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(twiTask, &higherPriorityTaskWoken); // wake up the task
twiTask = nullptr;
portYIELD_FROM_ISR(higherPriorityTaskWoken);
}
}
uint32_t I2C::statusWaitFunc(Twi *twi, uint32_t bitsToWaitFor)
{
uint32_t sr = twi->TWI_SR;
if ((sr & bitsToWaitFor) == 0)
{
// Suspend this task until we get an interrupt indicating that a status bit that we are interested in has been set
twiTask = xTaskGetCurrentTaskHandle();
twi->TWI_IER = bitsToWaitFor;
(void)TaskBase::Take(2);
sr = twi->TWI_SR;
twi->TWI_IDR = 0xFFFFFFFF;
}
return sr;
}
#endif
// End
|