-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
006a673
commit 887b6be
Showing
8 changed files
with
545 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,46 @@ | ||
// | ||
// Created by volundr on 7/6/16. | ||
// Created by volundr on 7/19/16. | ||
// | ||
|
||
#include <vector> | ||
#include "Bno055Interface.h" | ||
|
||
namespace bno055 { | ||
|
||
} | ||
bool Bno055Interface::init(const char* bno055File) { | ||
bool wasSuccess = true; | ||
printf("foo0\n"); | ||
wasSuccess &= uart.openPort(bno055File, BNO055_BAUD_RATE); | ||
printf("foo1\n"); | ||
wasSuccess &= writeByte(OPR_MODE, CONFIG); | ||
wasSuccess &= writeByte(PWR_MODE, NORMAL); | ||
wasSuccess &= writeByte(Page_ID, 0); | ||
wasSuccess &= writeByte(SYS_TRIGGER, 0); | ||
wasSuccess &= writeByte(UNIT_SEL, 0x83); | ||
wasSuccess &= writeByte(AXIS_MAP_CONFIG, 0x24); | ||
wasSuccess &= writeByte(AXIS_MAP_SIGN, 0x06); | ||
wasSuccess &= writeByte(OPR_MODE, NDOF); | ||
|
||
hasInit = wasSuccess; | ||
return wasSuccess; | ||
} | ||
|
||
bool Bno055Interface::write(uint8_t regAddr, uint8_t length, uint8_t* data) { | ||
RegisterWritePacket packetToSend(regAddr, length, data); | ||
uart.sendData(packetToSend.bytes(), packetToSend.length); | ||
ReceivedAck ack; | ||
ack.readFrom(uart); | ||
return (ack.isValidAck() && !ack.isErrorStatus()); | ||
} | ||
|
||
bool Bno055Interface::writeByte(uint8_t regAddr, uint8_t data) { | ||
return write(regAddr, 1, &data); | ||
} | ||
|
||
bool Bno055Interface::isLive() { | ||
return hasInit; | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
// | ||
// Created by volundr on 7/6/16. | ||
// Created by volundr on 7/19/16. | ||
// | ||
|
||
#ifndef BNO055_BNO055INTERFACE_H | ||
#define BNO055_BNO055INTERFACE_H | ||
|
||
#include "bno055Enum.h" | ||
#include "UartInterface.h" | ||
#include "bno055DataSheet.h" | ||
|
||
namespace bno055 { | ||
|
||
class Bno055Interface { | ||
UartInterface uart; | ||
bool hasInit = false; | ||
|
||
bool write(uint8_t regAddr, uint8_t length, uint8_t* data); | ||
bool writeByte(uint8_t regAddr, uint8_t data); | ||
public: | ||
bool init(const char* bno055File); | ||
bool isLive(); | ||
}; | ||
|
||
} | ||
|
||
#endif //BNO055_BNO055INTERFACE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// Created by volundr on 7/6/16. | ||
// | ||
|
||
#include "UartInterface.h" | ||
|
||
namespace bno055 { | ||
|
||
/** | ||
* Opens communications with the UART device found at location 'deviceFile' | ||
* This code was taken from the 'Using the UART' tutorial page found at | ||
* http://www.raspberry-projects.com/pi/programming-in-c/uart-serial-port/using-the-uart | ||
*/ | ||
bool UartInterface::openPort(const char* deviceFile, tcflag_t baudRate) { | ||
/* | ||
* Close any existing serial communications | ||
*/ | ||
closePort(); | ||
/* | ||
* OPEN THE UART | ||
* The flags (defined in fcntl.h): | ||
* Access modes (use 1 of these): | ||
* O_RDONLY - Open for reading only. | ||
* O_RDWR - Open for reading and writing. | ||
* O_WRONLY - Open for writing only. | ||
* | ||
* O_NDELAY / O_NONBLOCK (same function) - | ||
* Enables nonblocking mode. When set read requests on the file can return immediately with a failure status | ||
* if there is no input immediately available (instead of blocking). Likewise, sendData requests can also return | ||
* immediately with a failure status if the output can't be written immediately. | ||
* | ||
* O_NOCTTY - | ||
* When set and path identifies a terminal device, open() shall not cause the terminal device to become the | ||
* controlling terminal for the process. | ||
*/ | ||
uartFile = open(deviceFile, O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/sendData mode | ||
if (uartFile == -1) { | ||
return false; | ||
} | ||
|
||
/* | ||
* CONFIGURE THE UART | ||
* The flags (defined in /usr/include/termios.h - | ||
* see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html): | ||
* | ||
* Baud rate: B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, | ||
* B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000 | ||
* CSIZE : CS5, CS6, CS7, CS8 | ||
* CLOCAL : Ignore modem status lines | ||
* CREAD : Enable receiver | ||
* IGNPAR : Ignore characters with parity errors | ||
* ICRNL : Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters | ||
* - don't use for bianry comms!) | ||
* PARENB : Parity enable | ||
* PARODD : Odd parity (else even) | ||
*/ | ||
struct termios options; | ||
tcgetattr(uartFile, &options); | ||
options.c_cflag = baudRate | CS8 | CLOCAL | CREAD; //<Set baud rate | ||
options.c_iflag = IGNPAR; | ||
options.c_oflag = 0; | ||
options.c_lflag = 0; | ||
tcflush(uartFile, TCIFLUSH); | ||
tcsetattr(uartFile, TCSANOW, &options); | ||
|
||
return true; | ||
} | ||
|
||
bool UartInterface::closePort() { | ||
if (uartFile != -1) { | ||
close(uartFile); | ||
uartFile = -1; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
UartInterface::~UartInterface() { | ||
if (uartFile != -1) { | ||
close(uartFile); | ||
} | ||
} | ||
|
||
int64_t UartInterface::sendData(uint8_t* data, uint32_t length) { | ||
if (uartFile == -1) { | ||
return -1; | ||
} else { | ||
return (int64_t)write(uartFile, data, length); | ||
} | ||
} | ||
|
||
int64_t UartInterface::recvData(uint8_t* empty, uint32_t maxLength) { | ||
if (uartFile == -1) { | ||
return -1; | ||
} else { | ||
return (int64_t)read(uartFile, (void*)empty, maxLength); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Created by volundr on 7/6/16. | ||
// | ||
|
||
#ifndef BNO055_UARTINTERFACE_H | ||
#define BNO055_UARTINTERFACE_H | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> //Used for UART | ||
#include <fcntl.h> //Used for UART | ||
#include <termios.h> //Used for UART | ||
#include <stdint.h> | ||
|
||
namespace bno055 { | ||
|
||
class UartInterface { | ||
int uartFile = -1; | ||
public: | ||
~UartInterface(); | ||
bool openPort(const char* deviceFile, tcflag_t baudRate); | ||
bool closePort(); | ||
int64_t sendData(uint8_t* data, uint32_t length); | ||
int64_t recvData(uint8_t* empty, uint32_t maxLength); | ||
}; | ||
|
||
} | ||
|
||
#endif //BNO055_UARTINTERFACE_H |
Oops, something went wrong.