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: mbed-rtos mbed-src
I2CDriver.cpp
- Committer:
- humlet
- Date:
- 2013-04-13
- Revision:
- 0:13c962fecb13
- Child:
- 1:90455d5bdd8c
File content as of revision 0:13c962fecb13:
#include "I2CDriver.h"
#include "error.h"
#define ISR2DRV_SIG (1<<0);
#define DRV_USR_SIG (1<<1);
Channel I2CDriver::channels[2]= {0,0};
void I2CDriver::channel_0_ISR()
{
osSignalSet( channels[0].driver, ISR2DRV_SIG);
NVIC_DisableIRQ(I2C1_IRQn); //I2C_IRQn
}
void I2CDriver::channel_1_ISR()
{
osSignalSet( channels[1].driver, ISR2DRV_SIG);
NVIC_DisableIRQ(I2C2_IRQn); //I2C_IRQn
}
void I2CDriver::threadFun(void* const args)
{
int channelIdx = (int)args;
Channel channel;
s_channels[channelIdx] = &channel;
channel.driver = Thread::gettid();
if(channelIdx==0)NVIC_SetVector(I2C1_IRQn, (uint32_t)I2CDriver::channel_0_ISR);
if(channelIdx==1)NVIC_SetVector(I2C2_IRQn, (uint32_t)I2CDriver::channel_1_ISR);
I2C i2c(c_sda[channelIdx], c_scl[channelIdx]);
while(1) {
osSignalWait(DRV_USR_SIG,0);
switch(channels[channel].transfer.cmd) {
case START:
if(channel.freq!=channel.transfer.freq) i2c.frequency
i2c.start();
break;
case STOP:
i2c.stop();
break;
}
s_threads[2]
}
}
I2CDriver::I2CDriver(PinName sda, PinName scl)
{
// check pins and determine i2c channel
int channel=0;
if(sda==sdas[0] && scl==scls[0]) channel=0; // I2C_1
else if (sda==sdas[1] && scl==scls[1]) channel=1; //I2C_2
else error("I2CDriver: Invalid I2C pinns selected");
if(s_channels[channel]==0)
new Thread(threadFun,(void *)channel,osPriorityRealtime);
m_channel = *(s_channel[channel]);
}
void I2CDriver::sendNwait(){
osSignalSet( m_channel.driver, DRV_USR_SIG);
osSignalWait(DRV_USR_SIG,osWaitForever);
}
void I2CDriver::frequency(int hz);
int I2CDriver::read(int address, char *data, int length, bool repeated = false);
int I2CDriver::read(int ack);
int I2CDriver::write(int address, const char *data, int length, bool repeated = false);
int I2CDriver::write(int data);
void I2CDriver::start(void){
lock();
m_channel.transfer.freq = _hz;
m_channel.transfer.cmd = START;
sendNwait();
unlock();
}
void I2CDriver::stop(void){
lock();
m_channel.transfer.freq = _hz;
m_channel.transfer.cmd = STOP;
sendNwait();
unlock();
}