USART in SPI mode

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I'm trying to use USART in SPI master mode. I'm developping on a SAM3S-EK2 (SAM3SD8C). Here is the code I did :

Quote:

#define ID_USART_SPI_MASTER ID_USART2
#define USART_SPI_MASTER_BASE USART2

void usart_spi_master_init(Usart *p_usart) {
static usart_spi_opt_t usart_spi_options = {
.baudrate = 192000,
.channel_mode = US_MR_CHMODE_NORMAL,
.char_length = US_MR_CHRL_8_BIT,
.spi_mode = SPI_MODE_0
};
sysclk_enable_peripheral_clock(ID_USART_SPI_MASTER);
usart_disable_writeprotect(p_usart);
usart_init_spi_master(p_usart, &usart_spi_options, sysclk_get_peripheral_hz());
usart_reset_tx(p_usart);
usart_disable_interrupt(p_usart, 0xffffffff);
usart_enable_tx(p_usart);
}

int main(void) {
[...]
usart_spi_master_init(USART_SPI_MASTER_BASE);
while(!usart_is_tx_ready(USART_SPI_MASTER_BASE));
usart_spi_force_chip_select(USART_SPI_MASTER_BASE);
usart_write(USART_SPI_MASTER_BASE, 0);
while(!usart_is_tx_ready(USART_SPI_MASTER_BASE));
usart_write(USART_SPI_MASTER_BASE, 'a');
usart_spi_release_chip_select(USART_SPI_MASTER_BASE);
}

But I don't receive anything on the slave.

If I manualy set RTS low, then clk low and high 16 times, I correctly receive 0x0000 on the receiver.

Any idea what could be wrong in my code?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried another approch using usart_spi.c/h (but when looking at the code I don't see any difference with my previous try) using this code :

Quote:

usart_spi_init(USART_SPI_MASTER);
usart_spi_setup_device(USART_SPI_MASTER, NULL, SPI_MODE_0, 19200, 0);

uint8_t data[2] = {0x00, 'a'};
usart_spi_select_device(USART_SPI_MASTER, NULL);
usart_spi_write_packet(USART_SPI_MASTER, data, 2);
usart_spi_deselect_device(USART_SPI_MASTER, NULL);

But still no luck, it still doesn't work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Could you solve this problem in the end?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, Any update on this? I tried the same thing and it seems to work, except for the MISO line.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You may be better off asking SAM3 questions in one of the forums for Cortex. While it's true this is the only section for ASF it is effectively in the "AVR bit" of this site so you may not get that many ARM user's eyeballs here.