Thanks for responding,
I'm using the SPI library in mbed, which has a function format where I can specify the clock speed and mode
mbed-SPI Code
lines 31-36
void SPI::format(int bits, int mode) {
_bits = bits;
_mode = mode;
SPI::_owner = NULL;
aquire();
}
lines 47-53
void SPI::aquire() {
if (_owner != this) {
spi_format(&_spi, _bits, _mode, 0);
spi_frequency(&_spi, _hz);
_owner = this;
}
}
the function spi_format() called in aquire() is used to set up the registers for the clock speed and SPI mode.
from lines 95-113 in mbed-source code for spi_api.c
void spi_format(spi_t *obj, int bits, int mode, int slave) {
if (bits != 8) {
error("Only 8bits SPI supported");
}
if ((mode < 0) || (mode > 3)) {
error("SPI mode unsupported");
}
uint8_t polarity = (mode & 0x2) ? 1 : 0;
uint8_t phase = (mode & 0x1) ? 1 : 0;
uint8_t c1_data = ((!slave) << 4) | (polarity << 3) | (phase << 2);
obj->spi->C1 &= ~(0x7 << 2);
obj->spi->C1 |= c1_data;
}
This code appears to be working fine, and I have been able to properly set up SPI in mode 0 and mode 3. It just doesn't seem to work for mode 1. I typically get zeros from the chip.
Below is my code.
#include "mbed.h"
#define RREG_COMMAND 0x20 //read command
#define WREG_COMMAND 0x40 //write command
SPI spi(PTC6, PTC7, PTC5); // mosi, miso, sclk
DigitalOut cs(PTD5); //chip select
DigitalOut afe(PTD0); //other SPI chip's chip select, set high throughout code to insure it doesn't interfere.
//Serial pc(USBTX, USBRX);
#define ID_ADDRESS 0x00 //register address to get ID of chip
#define CONFIG1_ADDRESS 0x01 //config 1 address
#define CONFIG2_ADDRESS 0x02 //config 2 address
#define LOFF_ADDRESS 0x03
#define CH1SET_ADDRESS 0x04
#define CH2SET_ADDRESS 0x05
#define RLD_SENS_ADDRESS 0x06
#define LOFF_SENS_ADDRESS 0x07
#define LOFF_STAT_ADDRESS 0x08
#define RESP1_ADDRESS 0x09
#define RESP2_ADDRESS 0x0A
#define GPIO_ADDRESS 0x0B
int read_register(int address) {
cs = 0;
wait(1);
int firstCommandByte = RREG_COMMAND | (address & 0x1F);
int secondCommandByte = 0x00;
printf("Sending 1 bit: %d\n",firstCommandByte);
printf("Sending 2 bit: %d\n",secondCommandByte);
spi.write(firstCommandByte);
spi.write(secondCommandByte);
int registerVal = spi.write(0x00);
printf("Result is %d\n",registerVal);
wait(1);
cs = 1;
return registerVal;
}
void write_register(int address, int value) {
cs = 0;
wait(1);
int firstCommandByte = WREG_COMMAND | (address & 0x1F);
int secondCommandByte = 0x00;
spi.write(firstCommandByte);
spi.write(secondCommandByte);
spi.write(value);
printf("Wrote %d in %d register\n",value,address);
wait(1);
cs = 1;
}
int main() {
cs =1;
afe = 1;
// Setup the spi for 8 bit data, mode 1
spi.format(8,1);
spi.frequency(2000000);
while(1) {
int value = read_register(ID_ADDRESS);
if (value == 115) {
printf("SUCCESS!");
}
else {
printf("%d\n",value);
}
value = read_register(CONFIG1_ADDRESS);
printf("%d\n",value);
wait(2);
//check if it's an issue with the function
cs = 0;
wait_ms(1000);
int firstCommandByte = RREG_COMMAND | (0x01 & 0x1F);
int secondCommandByte = 0x00;
printf("Sending 1 bit: %d\n",firstCommandByte);
printf("Sending 2 bit: %d\n",secondCommandByte);
spi.write(firstCommandByte);
spi.write(secondCommandByte);
int registerVal = spi.write(0x00);
printf("Result is %d\n",registerVal);
wait_ms(1000);
cs = 1;
write_register(CONFIG1_ADDRESS,0x01);
int confVal = read_register(CONFIG1_ADDRESS);
}
}