Hi,
I fail receive data from SPI.
I have check the signal from oscilloscope, there slave does reply SPI data to master. So, It is definitely is code problem.
Below is my code;
#define AS3933_SPI_INSTANCE 0
static const nrf_drv_spi_t AS3933_SPI = NRF_DRV_SPI_INSTANCE(AS3933_SPI_INSTANCE);
volatile bool AS3933_SPI_XFER_Done;
void AS3933_SPI_Event_Handler(nrf_drv_spi_evt_t const * p_event, void *p_context)
{
AS3933_SPI_XFER_Done = true;
}
void as_SPI_TX_Byte(uint8_t v){
uint8_t SPI_RX;
AS3933_SPI_XFER_Done = false;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&AS3933_SPI, &v, 1, &SPI_RX, 1));
while(!AS3933_SPI_XFER_Done);
}
uint8_t as_SPI_Rx_Byte(uint8_t v){
uint8_t SPI_RX;
uint8_t SPI_TX = v;
AS3933_SPI_XFER_Done = false;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&AS3933_SPI, &SPI_TX, 1, &SPI_RX, 1));
while(!AS3933_SPI_XFER_Done);
return SPI_RX;
}
void as_write_reg(uint8_t no, uint8_t data){
if(no>=0 && no<=19){
nrf_gpio_pin_set(AS3933_SPI_SS_PIN);
as_SPI_TX_Byte(no);
as_SPI_TX_Byte(data);
nrf_gpio_pin_clear(AS3933_SPI_SS_PIN);
}
}
uint8_t as_read_reg(uint8_t no){
uint8_t data;
if(no>=0 && no<=19){
nrf_gpio_pin_set(AS3933_SPI_SS_PIN);
as_SPI_TX_Byte(0x40|no);
data = as_SPI_Rx_Byte(0);
nrf_gpio_pin_clear(AS3933_SPI_SS_PIN);
}
return data;
}
void Init_AS3933(void)
{
nrf_gpio_cfg_output(AS3933_SPI_SS_PIN);
nrf_gpio_cfg_input(AS3932_WAKE_PIN, NRF_GPIO_PIN_NOPULL);
nrf_drv_spi_config_t AS3933_SPI_Config = NRF_DRV_SPI_DEFAULT_CONFIG;
AS3933_SPI_Config.ss_pin = NRF_DRV_SPI_PIN_NOT_USED;
AS3933_SPI_Config.mode = NRF_DRV_SPI_MODE_1,
AS3933_SPI_Config.miso_pin = AS3933_SPI_MOSI_PIN;
AS3933_SPI_Config.mosi_pin = AS3933_SPI_MISO_PIN;
AS3933_SPI_Config.sck_pin = AS3933_SPI_SCK_PIN;
APP_ERROR_CHECK(nrf_drv_spi_init(&AS3933_SPI , &AS3933_SPI_Config, AS3933_SPI_Event_Handler, NULL));
nrf_gpio_pin_set(AS3933_SPI_SS_PIN);
as_SPI_TX_Byte(0);
as_SPI_TX_Byte(R0);
as_SPI_TX_Byte(R1);
as_SPI_TX_Byte(R2);
as_SPI_TX_Byte(R3);
as_SPI_TX_Byte(R4);
as_SPI_TX_Byte(R5);
as_SPI_TX_Byte(R6);
as_SPI_TX_Byte(R7);
as_SPI_TX_Byte(R8);
as_SPI_TX_Byte(R9);
nrf_gpio_pin_clear(AS3933_SPI_SS_PIN);
}
void Main(void){
uint8_t v;
Init_AS3933();
while(1){
v = as_read_reg(0);
nrf_delay_ms(10);
printf("%X ",v);
}
}
Printf print out value '0' which is wrong.
By right way, the printf should print out value '3'.
Please advise.
Thanks