Hello I am working on a project where I am interfacing an Arduino Uno with an IC device. I am trying to make a write method/function that writes configuration data to configuration registers.
I used an example document as a reference, and here is their write method (written for an STM32F105_VC microprocessor):
/*******************************************************************************
* Function Name: gp22_wr_config_reg
* Parameters: Address byte, 4 bytes of Configuration
*
* Return: none
*
* Description: Writes the config.reg. specified in GP21 with the data
*
******************************************************************************/
void gp22_wr_config_reg (void *bus_type, uint8_t opcode_address,
uint32_t config_reg_data)
{
uint8_t Data_Byte_Lo = config_reg_data;
uint8_t Data_Byte_Mid1 = config_reg_data>>8;
uint8_t Data_Byte_Mid2 = config_reg_data>>16;
uint8_t Data_Byte_Hi = config_reg_data>>24;
uint8_t common_delay = 10; // important delay (16) at SPI freq.=750kHz
// Deactivating Reset SPIx
if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(bus_type, opcode_address); // RAM WR OPCODE+ADDRESS
Simple_delay_750ns((void*)common_delay);
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(bus_type, Data_Byte_Hi); // DATA BYTE HIGH
Simple_delay_750ns((void*)common_delay);
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(bus_type, Data_Byte_Mid2); // DATA MID - 2
Simple_delay_750ns((void*)common_delay);
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(bus_type, Data_Byte_Mid1); // DATA MID - 1
Simple_delay_750ns((void*)common_delay);
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(bus_type, Data_Byte_Lo); // DATA LOW
Simple_delay_750ns((void*)common_delay);
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
Simple_delay_750ns((void*)common_delay);
// Reset to device SPIx
if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
}
And here is the write method that I have written for the Arduino:
void writeConfig( byte regAd, uint32_t regData){
/*Since the SPI.transfer function can only transfer one byte at a time,
it is necessary to decompose the 4 bytes of configuration data into
1 byte segments. The following byte variables are the four components of
the registration data.
*/
byte configByteLow = regData; //the least 4 significant bytes
byte configByteMid1 = regData>>8; //subsequent 4 bytes
byte configByteMid2 = regData>>16; //subsequent 4 bytes
byte configByteHi = regData>>24; //the highest 4 significant bytes
delayMicroseconds(100);
SPI.transfer(regAd); //transfers register address to be written to
//******Not sure if we need LSB or MSB first. NEED TO CHECK*****
//transfers 4 components of configuration data
SPI.transfer(configByteLow);
delayMicroseconds(100);
SPI.transfer(configByteMid1);
delayMicroseconds(100);
SPI.transfer(configByteMid2);
delayMicroseconds(100);
SPI.transfer(configByteHi);
delayMicroseconds(100);
digitalWrite(slaveSelectPin, HIGH); //deselects gp22
When I pass the configuration register address to the Arduino, how will it know what to do with the byte value? How will it know that it is a config register address?
Also, I am not sure if the way I wrote the method/function is correct for writing to registers...
Thanks,
Anthony