SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.

Dependencies:   TSI USBDevice mbed-dev

Fork of SmartWheels by haofan Zheng

Hardwares/CamRegBuf.cpp

Committer:
hazheng
Date:
2017-03-02
Revision:
30:ff7f83ad6369
Child:
32:5badeff825dc

File content as of revision 30:ff7f83ad6369:

#include "CamRegBuf.h"

#include "PinAssignment.h"
#include "Core.h"
#include "SWUSBServer.h"

#include "CamRegDefinitions.h"

CamRegBuf::CamRegBuf(SW::Core & core, uint8_t writeAddr,  uint8_t readAddr) : 
    m_writeAddr(writeAddr),
    m_readAddr(readAddr),
    m_core(core),
    m_sccbCtrl(I2C(PIN_ORB_SDA, PIN_ORB_SCL))
{
    
}

CamRegBuf::~CamRegBuf()
{
    
}

//Blocking method. Do not use during the running state!!
void CamRegBuf::SCCBWrite(uint8_t RegAddr, uint8_t Data)
{   
    m_sccbCtrl.lock();
    m_sccbCtrl.frequency(CAM_REG_I2CFREQ);
    
    m_sccbCtrl.start();
    
    m_sccbCtrl.write(m_writeAddr);
    wait_us(CAM_REG_WRITEWAIT);
    m_sccbCtrl.write(RegAddr);
    wait_us(CAM_REG_WRITEWAIT);
    m_sccbCtrl.write(Data);
    wait_us(CAM_REG_WRITEWAIT);
    
    m_sccbCtrl.stop();
    
    m_sccbCtrl.unlock();
}

//Blocking method. Do not use during the running state!!
uint8_t CamRegBuf::SCCBRead(const uint8_t RegAddr)
{
    m_sccbCtrl.lock();
    m_sccbCtrl.frequency(CAM_REG_I2CFREQ);
    
    m_sccbCtrl.start();
    
    m_sccbCtrl.write(m_writeAddr);
    wait_us(CAM_REG_WRITEWAIT);
    m_sccbCtrl.write(RegAddr);
    m_sccbCtrl.stop();
    wait_us(CAM_REG_WRITEWAIT);    
 
    m_sccbCtrl.start();
    m_sccbCtrl.write(m_readAddr);
    wait_us(CAM_REG_WRITEWAIT);
    char readValue = m_sccbCtrl.read(CAM_REG_NOACK);
    
    m_sccbCtrl.stop();
    
    m_sccbCtrl.unlock();
    
    return readValue;
}


//Blocking method. Do not use during the running state!!
void CamRegBuf::ReadRegisters()
{
    for(int i = 0; i <= 0xff; ++i)
    {
        char buf[10];
        sprintf(buf, "%#x-%#x", i, SCCBRead(i));
        m_core.GetUSBServer().PushUnreliableMsg('D', buf);
        
        wait(0.1);
    }
}

void CamRegBuf::WriteRegSet(const struct sensor_reg * reglist)
{
    unsigned int i = 0;
    while ((reglist[i].reg != 0xff) | (reglist[i].val != 0xff))
    {
        SCCBWrite(reglist[i].reg, reglist[i].val);
        ++i;
    }
}