OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_twi/] [main.c] - Diff between revs 65 and 68

Show entire file | Details | Blame | View Log

Rev 65 Rev 68
Line 55... Line 55...
// Prototypes
// Prototypes
void scan_twi(void);
void scan_twi(void);
void set_speed(void);
void set_speed(void);
void send_twi(void);
void send_twi(void);
uint32_t hexstr_to_uint(char *buffer, uint8_t length);
uint32_t hexstr_to_uint(char *buffer, uint8_t length);
 
void print_hex_byte(uint8_t data);
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * This program provides an interactive console to communicate with TWI devices.
 * This program provides an interactive console to communicate with TWI devices.
 *
 *
Line 102... Line 103...
 
 
  // info
  // info
  neorv32_uart0_printf("This program allows to create TWI transfers by hand.\n"
  neorv32_uart0_printf("This program allows to create TWI transfers by hand.\n"
                      "Type 'help' to see the help menu.\n\n");
                      "Type 'help' to see the help menu.\n\n");
 
 
  // configure TWI, second slowest clock, no clock-stretching
  // configure TWI, second slowest clock
  neorv32_twi_setup(CLK_PRSC_2048, 0);
  neorv32_twi_setup(CLK_PRSC_2048);
 
 
  // no active bus session yet
  // no active bus session yet
  bus_claimed = 0;
  bus_claimed = 0;
 
 
  // Main menu
  // Main menu
Line 217... Line 218...
  for (i=0; i<128; i++) {
  for (i=0; i<128; i++) {
    uint8_t twi_ack = neorv32_twi_start_trans((uint8_t)(2*i+1));
    uint8_t twi_ack = neorv32_twi_start_trans((uint8_t)(2*i+1));
    neorv32_twi_generate_stop();
    neorv32_twi_generate_stop();
 
 
    if (twi_ack == 0) {
    if (twi_ack == 0) {
      neorv32_uart0_printf("+ Found device at write-address 0x%x\n", (uint32_t)(2*i));
      neorv32_uart0_printf(" + Found device at write-address 0x");
 
      print_hex_byte(2*i);
 
      neorv32_uart0_printf("\n");
      num_devices++;
      num_devices++;
    }
    }
  }
  }
 
 
  if (!num_devices) {
  if (!num_devices) {
Line 240... Line 243...
  // enter data
  // enter data
  neorv32_uart0_printf("Enter TX data (2 hex chars): ");
  neorv32_uart0_printf("Enter TX data (2 hex chars): ");
  neorv32_uart0_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
  neorv32_uart0_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
  uint8_t tmp = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
  uint8_t tmp = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
  uint8_t res = neorv32_twi_trans(tmp);
  uint8_t res = neorv32_twi_trans(tmp);
  neorv32_uart0_printf("\nRX data:  0x%x\n", (uint32_t)neorv32_twi_get_data());
  neorv32_uart0_printf("\n RX data:  0x");
  neorv32_uart0_printf("Response: ");
  print_hex_byte((uint8_t)neorv32_twi_get_data());
 
  neorv32_uart0_printf("\n Response: ");
  if (res == 0)
  if (res == 0)
    neorv32_uart0_printf("ACK\n");
    neorv32_uart0_printf("ACK\n");
  else
  else
    neorv32_uart0_printf("NACK\n");
    neorv32_uart0_printf("NACK\n");
 
 
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Helper function to convert N hex chars string into uint32_T
 * Helper function to convert N hex chars string into uint32_t
 *
 *
 * @param[in,out] buffer Pointer to array of chars to convert into number.
 * @param[in,out] buffer Pointer to array of chars to convert into number.
 * @param[in,out] length Length of the conversion string.
 * @param[in,out] length Length of the conversion string.
 * @return Converted number.
 * @return Converted number.
 **************************************************************************/
 **************************************************************************/
Line 279... Line 283...
    res = res + (d << (length*4));
    res = res + (d << (length*4));
  }
  }
 
 
  return res;
  return res;
}
}
 No newline at end of file
 No newline at end of file
 
 
 
 
 
/**********************************************************************//**
 
 * Print byte as hex chars via UART0.
 
 *
 
 * @param data 8-bit data to be printed as two hex chars.
 
 **************************************************************************/
 
void print_hex_byte(uint8_t data) {
 
 
 
  static const char symbols[] = "0123456789abcdef";
 
 
 
  neorv32_uart0_putc(symbols[(data >> 4) & 15]);
 
  neorv32_uart0_putc(symbols[(data >> 0) & 15]);
 
}
 
 
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.