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

Committer:
Bobymicjohn
Date:
Thu Apr 06 20:36:51 2017 +0000
Revision:
54:f1f5648dfacf
Parent:
32:5badeff825dc
Child:
87:15fcf7891bf9
Adjusted the angle. Commented out all USB Server code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hazheng 30:ff7f83ad6369 1 #include "CamRegBuf.h"
hazheng 30:ff7f83ad6369 2
hazheng 30:ff7f83ad6369 3 #include "PinAssignment.h"
hazheng 30:ff7f83ad6369 4 #include "Core.h"
hazheng 30:ff7f83ad6369 5 #include "SWUSBServer.h"
hazheng 30:ff7f83ad6369 6
hazheng 30:ff7f83ad6369 7 #include "CamRegDefinitions.h"
hazheng 30:ff7f83ad6369 8
hazheng 30:ff7f83ad6369 9 CamRegBuf::CamRegBuf(SW::Core & core, uint8_t writeAddr, uint8_t readAddr) :
hazheng 30:ff7f83ad6369 10 m_writeAddr(writeAddr),
hazheng 30:ff7f83ad6369 11 m_readAddr(readAddr),
hazheng 30:ff7f83ad6369 12 m_core(core),
hazheng 30:ff7f83ad6369 13 m_sccbCtrl(I2C(PIN_ORB_SDA, PIN_ORB_SCL))
hazheng 30:ff7f83ad6369 14 {
hazheng 30:ff7f83ad6369 15
hazheng 30:ff7f83ad6369 16 }
hazheng 30:ff7f83ad6369 17
hazheng 30:ff7f83ad6369 18 CamRegBuf::~CamRegBuf()
hazheng 30:ff7f83ad6369 19 {
hazheng 30:ff7f83ad6369 20
hazheng 30:ff7f83ad6369 21 }
hazheng 30:ff7f83ad6369 22
hazheng 30:ff7f83ad6369 23 //Blocking method. Do not use during the running state!!
hazheng 30:ff7f83ad6369 24 void CamRegBuf::SCCBWrite(uint8_t RegAddr, uint8_t Data)
hazheng 30:ff7f83ad6369 25 {
hazheng 30:ff7f83ad6369 26 m_sccbCtrl.lock();
hazheng 30:ff7f83ad6369 27 m_sccbCtrl.frequency(CAM_REG_I2CFREQ);
hazheng 30:ff7f83ad6369 28
hazheng 30:ff7f83ad6369 29 m_sccbCtrl.start();
hazheng 30:ff7f83ad6369 30
hazheng 30:ff7f83ad6369 31 m_sccbCtrl.write(m_writeAddr);
hazheng 30:ff7f83ad6369 32 wait_us(CAM_REG_WRITEWAIT);
hazheng 30:ff7f83ad6369 33 m_sccbCtrl.write(RegAddr);
hazheng 30:ff7f83ad6369 34 wait_us(CAM_REG_WRITEWAIT);
hazheng 30:ff7f83ad6369 35 m_sccbCtrl.write(Data);
hazheng 30:ff7f83ad6369 36 wait_us(CAM_REG_WRITEWAIT);
hazheng 30:ff7f83ad6369 37
hazheng 30:ff7f83ad6369 38 m_sccbCtrl.stop();
hazheng 30:ff7f83ad6369 39
hazheng 30:ff7f83ad6369 40 m_sccbCtrl.unlock();
hazheng 30:ff7f83ad6369 41 }
hazheng 30:ff7f83ad6369 42
hazheng 30:ff7f83ad6369 43 //Blocking method. Do not use during the running state!!
hazheng 30:ff7f83ad6369 44 uint8_t CamRegBuf::SCCBRead(const uint8_t RegAddr)
hazheng 30:ff7f83ad6369 45 {
hazheng 30:ff7f83ad6369 46 m_sccbCtrl.lock();
hazheng 30:ff7f83ad6369 47 m_sccbCtrl.frequency(CAM_REG_I2CFREQ);
hazheng 30:ff7f83ad6369 48
hazheng 30:ff7f83ad6369 49 m_sccbCtrl.start();
hazheng 30:ff7f83ad6369 50
hazheng 30:ff7f83ad6369 51 m_sccbCtrl.write(m_writeAddr);
hazheng 30:ff7f83ad6369 52 wait_us(CAM_REG_WRITEWAIT);
hazheng 30:ff7f83ad6369 53 m_sccbCtrl.write(RegAddr);
hazheng 32:5badeff825dc 54 wait_us(CAM_REG_WRITEWAIT);
hazheng 32:5badeff825dc 55 m_sccbCtrl.stop();
hazheng 30:ff7f83ad6369 56
hazheng 30:ff7f83ad6369 57 m_sccbCtrl.start();
hazheng 30:ff7f83ad6369 58 m_sccbCtrl.write(m_readAddr);
hazheng 30:ff7f83ad6369 59 wait_us(CAM_REG_WRITEWAIT);
hazheng 30:ff7f83ad6369 60 char readValue = m_sccbCtrl.read(CAM_REG_NOACK);
hazheng 30:ff7f83ad6369 61
hazheng 30:ff7f83ad6369 62 m_sccbCtrl.stop();
hazheng 30:ff7f83ad6369 63
hazheng 30:ff7f83ad6369 64 m_sccbCtrl.unlock();
hazheng 30:ff7f83ad6369 65
hazheng 30:ff7f83ad6369 66 return readValue;
hazheng 30:ff7f83ad6369 67 }
hazheng 30:ff7f83ad6369 68
hazheng 30:ff7f83ad6369 69
hazheng 30:ff7f83ad6369 70 //Blocking method. Do not use during the running state!!
hazheng 30:ff7f83ad6369 71 void CamRegBuf::ReadRegisters()
hazheng 30:ff7f83ad6369 72 {
hazheng 30:ff7f83ad6369 73 for(int i = 0; i <= 0xff; ++i)
hazheng 30:ff7f83ad6369 74 {
Bobymicjohn 54:f1f5648dfacf 75 //char buf[10];
Bobymicjohn 54:f1f5648dfacf 76 //sprintf(buf, "%#x-%#x", i, SCCBRead(i));
Bobymicjohn 54:f1f5648dfacf 77 //m_core.GetUSBServer().PushUnreliableMsg('D', buf);
hazheng 30:ff7f83ad6369 78
hazheng 30:ff7f83ad6369 79 wait(0.1);
hazheng 30:ff7f83ad6369 80 }
hazheng 30:ff7f83ad6369 81 }
hazheng 30:ff7f83ad6369 82
hazheng 32:5badeff825dc 83 /*
hazheng 32:5badeff825dc 84 int32_t CamRegBuf::I2CBufferRead(int32_t ucDevAddr, uint8_t *ucBuffer, int32_t ulSize)
hazheng 32:5badeff825dc 85 {
hazheng 32:5badeff825dc 86 m_sccbCtrl.start();
hazheng 32:5badeff825dc 87
hazheng 32:5badeff825dc 88 m_sccbCtrl.write(ucDevAddr);
hazheng 32:5badeff825dc 89 if(ulSize == 1){
hazheng 32:5badeff825dc 90 ucBuffer[0] = m_sccbCtrl.read(0);
hazheng 32:5badeff825dc 91 }else{
hazheng 32:5badeff825dc 92 for(int i=0;i<ulSize;i++){
hazheng 32:5badeff825dc 93 ucBuffer[i] = m_sccbCtrl.read(0);
hazheng 32:5badeff825dc 94 }
hazheng 32:5badeff825dc 95 }
hazheng 32:5badeff825dc 96
hazheng 32:5badeff825dc 97 m_sccbCtrl.stop();
hazheng 32:5badeff825dc 98
hazheng 32:5badeff825dc 99 return 0;
hazheng 32:5badeff825dc 100 }
hazheng 32:5badeff825dc 101
hazheng 32:5badeff825dc 102 int32_t CamRegBuf::I2CBufferWrite(int32_t ucDevAddr, uint8_t *ucBuffer, int32_t ulSize)
hazheng 32:5badeff825dc 103 {
hazheng 32:5badeff825dc 104 m_sccbCtrl.start();
hazheng 32:5badeff825dc 105
hazheng 32:5badeff825dc 106 // Set I2C slave write address
hazheng 32:5badeff825dc 107 m_sccbCtrl.write(ucDevAddr);
hazheng 32:5badeff825dc 108
hazheng 32:5badeff825dc 109 if(ulSize == 1){
hazheng 32:5badeff825dc 110 m_sccbCtrl.write(ucBuffer[0]);
hazheng 32:5badeff825dc 111 }else{
hazheng 32:5badeff825dc 112 for(int i=0;i<ulSize;i++){
hazheng 32:5badeff825dc 113 m_sccbCtrl.write(ucBuffer[i]);
hazheng 32:5badeff825dc 114 }
hazheng 32:5badeff825dc 115 }
hazheng 32:5badeff825dc 116
hazheng 32:5badeff825dc 117 m_sccbCtrl.stop();
hazheng 32:5badeff825dc 118
hazheng 32:5badeff825dc 119 return 0;
hazheng 32:5badeff825dc 120 }
hazheng 32:5badeff825dc 121 */
hazheng 30:ff7f83ad6369 122 void CamRegBuf::WriteRegSet(const struct sensor_reg * reglist)
hazheng 30:ff7f83ad6369 123 {
hazheng 30:ff7f83ad6369 124 unsigned int i = 0;
hazheng 30:ff7f83ad6369 125 while ((reglist[i].reg != 0xff) | (reglist[i].val != 0xff))
hazheng 30:ff7f83ad6369 126 {
hazheng 30:ff7f83ad6369 127 SCCBWrite(reglist[i].reg, reglist[i].val);
hazheng 30:ff7f83ad6369 128 ++i;
hazheng 32:5badeff825dc 129 wait_ms(1);
hazheng 30:ff7f83ad6369 130 }
hazheng 30:ff7f83ad6369 131 }