cancel
Showing results for 
Search instead for 
Did you mean: 

How to define an 8-bit parallel bidirectional databus (PD0-PD7)?

danesh
Associate II
Posted on July 15, 2015 at 15:25

Hi all,

I am using a STM32F103VEH MCU which controls an external PN512 (NXP semiconductors) NFC RF reader. The MCU and PN512 communicate over a 8-bit bidirectional parallel interface as databus to exchange information between the MCU and PN512. I have written following functions which will be used to make the bus either output form MCU to send data to PN512 or input to MCU to read data from PN512:

void PN512_set_pins_input() {

  GPIO_InitTypeDef GPIO_InitStruct;

  // ---------- Setup ports for GPIOC ----------

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0

                           | GPIO_Pin_1

                           | GPIO_Pin_2

                           | GPIO_Pin_3

                           | GPIO_Pin_4

                           | GPIO_Pin_5

                           | GPIO_Pin_6

                           | GPIO_Pin_7;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOC, &GPIO_InitStruct);

}

void PN512_set_pins_output() {

  GPIO_InitTypeDef GPIO_InitStruct;

  // ---------- Setup ports for GPIOC ----------

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0

                           | GPIO_Pin_1

                           | GPIO_Pin_2

                           | GPIO_Pin_3

                           | GPIO_Pin_4

                           | GPIO_Pin_5

                           | GPIO_Pin_6

                           | GPIO_Pin_7;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOC, &GPIO_InitStruct);

}

I have measured the pin voltages and it seems that the output from MCU to the chip works fine when I call ''PN512_set_pins_output'' before sending data. Now the problem is that I cannot receive the correct data that I expect from PN512 to the MCU, after I call ''PN512_set_pins_input'' and the received data is always same. I am not sure whether I define the pins as input to the MCU in wrong way or I am send wrong command to PN512 chip. Is there anyone who can guide me through this problem?

Sincerely,

Dan.

5 REPLIES 5
raptorhal2
Lead
Posted on July 15, 2015 at 17:49

I don't see anything wrong with the init sequence.

Are external pull ups or pull downs needed for data/address or control lines ?

Does the PN512 indicate data ready?

Can't see your command/response sequence. Provide it if you need more in depth problem support.

Cheers, Hal

raptorhal2
Lead
Posted on July 15, 2015 at 17:53

But you did say PD GPIOs in the title and PC in the code. Is that the disconnect ?

Cheers, Hal

danesh
Associate II
Posted on July 15, 2015 at 18:44

Sorry it was my mistake! GPIOC is used for parallel databus as it is in the code. The title is wrong. I will post right now more code to show how I communicate with PN512.

danesh
Associate II
Posted on July 15, 2015 at 18:53

This it the routine I call to read a register at the address ''addr'' in PN512 from STM32F103:

uint8_t PN512_read_reg(uint8_t addr) {

  PN512_select();

  // Write the address to the parallel bus.

  PN512_set_pins_output();

  PN512_enable_address_latch();

  PN512_write_to_bus(addr);

  PN512_disable_address_latch();

  // Read the data at the given address from the parallel bus.

  PN512_set_pins_input();

  PN512_enable_read();

  uint8_t data = PN512_read_from_bus();

  PN512_disable_read();

  PN512_deselect();

  return data;

}

and here are the functions are call during the register reading:

void PN512_enable_address_latch() {

  GPIO_SetBits(GPIOE, ALATCH_PIN);

}

void PN512_disable_address_latch() {

  GPIO_ResetBits(GPIOE, ALATCH_PIN);

}

void PN512_enable_read() {

  GPIO_ResetBits(GPIOE, READ_PIN);

}

void PN512_disable_read() {

  GPIO_SetBits(GPIOE, READ_PIN);

}

void PN512_select() {

  GPIO_ResetBits(GPIOE, SEL_PIN);

}

void PN512_deselect() {

  GPIO_SetBits(GPIOE, SEL_PIN);

}

void PN512_write_to_bus(uint8_t data) {

  word32.data = GPIOC->ODR;

  word16.data = word32.words.w0;

  word16.bytes.B0 = data;

  word32.words.w0 = word16.data;

  GPIOC->ODR = word32.data;

}

uint8_t PN512_read_from_bus() {

  word16.data = GPIO_ReadInputData(GPIOC);

  return word16.bytes.B0;

}

I tried to follow what is stated in PN512's datasheet which can be viewed here:

http://www.nxp.com/documents/data_sheet/PN512.pdf

I really appreciate your help.

Regards,

Dan.

danesh
Associate II
Posted on July 15, 2015 at 18:59

One more thing! When the pins are defined as output (Push/Pull) the first 6 bits (lower bits) are used for address, so according to datasheet, the data is set using lower 5 bits, the latch signal is enabled to latch the address, then either read or write signals would be triggered. If the read signal would be triggered, then the data could be written to the databus and can be written by MCU (that's why I call PN512_set_pins_input()), but if write signal is enabled, the data should be written to the databus prior to enabling the write signal, and then the data will be written to the given address as soon as write signal is enabled. First of all, I have tried to read a register i.e. version register of the chip to test if everything work as they should, but the returned value is always same no matter what address I send to PN512.