Create sample code probing for existence of AX5043

pull/1/head
Jonathan Brandenburg 8 years ago
parent 45af41e261
commit 3c6068328f

@ -0,0 +1,21 @@
all: probeTxRx
rebuild: clean
rebuild: all
clean:
rm -f probeTxRx
rm -f probeTxRx.o
rm -f spi/ax5043spi.o
probeTxRx: probeTxRx.o
probeTxRx: spi/ax5043spi.o
gcc -Wall -Wextra -o probeTxRx -lwiringPi probeTxRx.o spi/ax5043spi.o
probeTxRx.o: probeTxRx.c
probeTxRx.o: spi/ax5043spi.h
gcc -Wall -Wextra -c probeTxRx.c
spi/ax5043spi.o: spi/ax5043spi.c
spi/ax5043spi.o: spi/ax5043spi.h
gcc -Wall -Wextra -c -o spi/ax5043spi.o spi/ax5043spi.c

Binary file not shown.

@ -0,0 +1,55 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include "spi/ax5043spi.h"
#define AX5043_PWRMODE (0x002)
#define AX5043_PWRSTATE_POWERDOWN (0x000)
#define AX5043_SILICONREVISION (0x000)
#define AX5043_SCRATCH (0x001)
#define SILICONREV1 (0x51)
int main() {
setSpiChannel(SPI_CHANNEL);
setSpiSpeed(SPI_SPEED);
initializeSpi();
printf("INFO: Resetting AX5043\n");
uint8_t i;
// Initialize Interface
// Reset Device
ax5043WriteReg(AX5043_PWRMODE, 0x80);
ax5043WriteReg(AX5043_PWRMODE, AX5043_PWRSTATE_POWERDOWN);
// Wait some time for regulator startup
usleep(10000);
// Check Scratch
i = ax5043ReadReg(AX5043_SILICONREVISION);
i = ax5043ReadReg(AX5043_SILICONREVISION);
if (i != SILICONREV1) {
printf("ERROR: Unexpected hardware revision: %d\n", (int)i);
exit(EXIT_FAILURE);
}
ax5043WriteReg(AX5043_SCRATCH, 0x55);
if (ax5043ReadReg(AX5043_SCRATCH) != 0x55) {
printf("ERROR: Unable to communicate with the AX5043\n");
exit(EXIT_FAILURE);
}
ax5043WriteReg(AX5043_SCRATCH, 0xAA);
if (ax5043ReadReg(AX5043_SCRATCH) != 0xAA) {
printf("ERROR: Unable to communicate with the AX5043\n");
exit(EXIT_FAILURE);
}
printf("INFO: Successfully initialized the AX5043\n");
exit(EXIT_SUCCESS);
}

Binary file not shown.

@ -0,0 +1,97 @@
// Copyright (c) 2018 Brandenburg Tech, LLC
#include "ax5043spi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
//#include "dummyspi.h"
//#warning "For production builds, must not include dummyspi.h"
#include <wiringPiSPI.h>
#include <wiringPi.h>
int spiChannel = -1;
int spiSpeed = -1;
void setSpiChannel(int newSpiChannel) {
spiChannel = newSpiChannel;
}
void setSpiSpeed(int newSpiSpeed) {
spiSpeed = newSpiSpeed;
}
void initializeSpi() {
printf("INFO: Initializing SPI\n");
if (spiChannel < 0) {
fprintf(stderr, "ERROR: invalid SPI channel %d\n", spiChannel);
exit(EXIT_FAILURE);
}
if (spiSpeed < 0) {
fprintf(stderr, "ERROR: invalid SPI speed %d\n", spiSpeed);
exit(EXIT_FAILURE);
}
int fd;
wiringPiSetup();
fd = wiringPiSPISetup(spiChannel, spiSpeed);
if (fd < 0) {
fprintf(stderr, "ERROR: Cannot open SPI bus with error %d, %s\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
printf("INFO: Finished initializing SPI\n");
}
void ax5043WriteReg(uint16_t reg, uint8_t val) {
uint8_t buf[3];
int result;
if (spiChannel < 0) {
fprintf(stderr, "ERROR: invalid SPI channel %d\n", spiChannel);
exit(EXIT_FAILURE);
}
buf[0] = (unsigned char)(0x00f0 | ((reg & 0xf00) >> 8));
buf[1] = (reg & 0xff);
buf[2] = val & 0xff;
result = wiringPiSPIDataRW(spiChannel, buf, sizeof(buf));
if (result < 0) {
fprintf(stderr, "Failed to write the register with result %d and error %s\n",
result, strerror(result));
exit(EXIT_FAILURE);
}
}
uint8_t ax5043ReadReg(uint16_t reg) {
uint8_t buf[3];
int result;
if (spiChannel < 0) {
fprintf(stderr, "ERROR: invalid SPI channel %d\n", spiChannel);
exit(EXIT_FAILURE);
}
buf[0] = (unsigned char)(0x0070 | ((reg & 0xf00) >> 8));
buf[1] = (reg & 0xff);
buf[2] = 0x0000;
result = wiringPiSPIDataRW(spiChannel, buf, sizeof(buf));
if (result < 0) {
fprintf(stderr, "Failed to read register with result = %d and error %s\n",
result, strerror(errno));
exit(EXIT_FAILURE);
}
//printf("DEBUG: read value: %d\n", (int)buf[2]);
return (buf[2] & 0xff);
}

@ -0,0 +1,17 @@
// Copyright (c) 2018 Brandenburg Tech, LLC
#ifndef AX5043SPI_H_
#define AX5043SPI_H_
#include <stdint.h>
#define SPI_CHANNEL (0)
#define SPI_SPEED (32000000)
void setSpiChannel(int newSpiChannel);
void setSpiSpeed(int newSpiSpeed);
void initializeSpi();
void ax5043WriteReg(uint16_t reg, uint8_t val);
uint8_t ax5043ReadReg(uint16_t reg);
#endif /* AX5043SPI_H_ */

Binary file not shown.
Loading…
Cancel
Save

Powered by TurnKey Linux.