Advertisement
Guest User

main.c

a guest
Jul 11th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "app_uart.h"
  5. #include "app_error.h"
  6. #include "nrf_delay.h"
  7. #include "nrf.h"
  8. #include "bsp.h"
  9. #include "nrf_drv_twi.h"
  10. #include "nrf_drv_uart.h"
  11.  
  12. // TWI --------------------------------------------------
  13.  
  14. #define DEVICE_SCL_PIN 29
  15. #define DEVICE_SDA_PIN 6
  16.  
  17.  
  18. nrf_drv_twi_t twi_instance = NRF_DRV_TWI_INSTANCE(0);
  19.  
  20. void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context){  
  21. }
  22.  
  23. void twi_init (void)
  24. {
  25.     ret_code_t err_code;
  26.    
  27.     const nrf_drv_twi_config_t twi_config = {
  28.        .scl                = DEVICE_SCL_PIN,
  29.        .sda                = DEVICE_SDA_PIN,
  30.        .frequency          = NRF_TWI_FREQ_100K,
  31.        .interrupt_priority = APP_IRQ_PRIORITY_HIGH
  32.     };
  33.    
  34.     err_code = nrf_drv_twi_init(&twi_instance, &twi_config, twi_handler, NULL);
  35.     APP_ERROR_CHECK(err_code);
  36.    
  37.     nrf_drv_twi_enable(&twi_instance);
  38. }
  39.  
  40.  
  41. //UART ----------------------------------------------------------
  42.  
  43.  
  44. #define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
  45. #define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
  46. #define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */
  47.  
  48. void uart_error_handle(app_uart_evt_t * p_event)
  49. {
  50.     if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
  51.     {
  52.         APP_ERROR_HANDLER(p_event->data.error_communication);
  53.     }
  54.     else if (p_event->evt_type == APP_UART_FIFO_ERROR)
  55.     {
  56.         APP_ERROR_HANDLER(p_event->data.error_code);
  57.     }
  58. }
  59.  
  60.  
  61.  
  62. #define RX_PIN_NUMBER  25
  63. #define TX_PIN_NUMBER  24
  64. #define CTS_PIN_NUMBER 23
  65. #define RTS_PIN_NUMBER 22
  66. #define HWFC           false
  67.  
  68. /**
  69.  * @brief Function for main application entry.
  70.  */
  71. int main(void)
  72. {
  73.     uint32_t err_code;
  74.    
  75.     twi_init();
  76.  
  77.     const app_uart_comm_params_t comm_params =
  78.       {
  79.           RX_PIN_NUMBER,
  80.           TX_PIN_NUMBER,
  81.           RTS_PIN_NUMBER,
  82.           CTS_PIN_NUMBER,
  83.           APP_UART_FLOW_CONTROL_ENABLED,
  84.           false,
  85.           UART_BAUDRATE_BAUDRATE_Baud250000
  86.       };
  87.  
  88.     APP_UART_FIFO_INIT(&comm_params,
  89.                          UART_RX_BUF_SIZE,
  90.                          UART_TX_BUF_SIZE,
  91.                          uart_error_handle,
  92.                          APP_IRQ_PRIORITY_LOWEST,
  93.                          err_code);
  94.  
  95.     APP_ERROR_CHECK(err_code);
  96.  
  97.     uint8_t test;
  98.  
  99.     while (true)
  100.     {
  101.         nrf_delay_ms(10);
  102.         unsigned char message2[] = " outcome";
  103.         message2[0] = 0xAA;
  104.         nrf_drv_twi_tx(&twi_instance, 8, message2, sizeof(message2), false);
  105.         nrf_delay_ms(10);
  106.        
  107.         //uint8_t data_buffer[2] = {4,2};
  108.         //nrf_drv_uart_selvtx(data_buffer,2);
  109.        
  110.         uint8_t ch = 0;
  111.        
  112.         app_uart_get(&ch);
  113.        
  114.         //uint8_t mess[] = {0x55, 1,2,3,4}
  115.         uint8_t tasd = 5;
  116.        
  117.         test = app_uart_put(tasd);
  118.         app_uart_flush();
  119.        
  120.        
  121.         uint8_t details[4] = {test, NRF_SUCCESS, NRF_SUCCESS==test, ch};
  122.         nrf_drv_twi_tx(&twi_instance, 8, details, sizeof(details), false);
  123.         nrf_delay_ms(10);
  124.        
  125.         app_uart_put(120);
  126.        
  127.        
  128.        
  129.         nrf_delay_ms(1000);
  130.        
  131.         // uint8_t cr;
  132.         // while (app_uart_get(&cr) != NRF_SUCCESS);
  133.         // while (app_uart_put(cr) != NRF_SUCCESS);
  134.         //
  135.         // if (cr == 'q' || cr == 'Q')
  136.         // {
  137.         //     printf(" \r\nExit!\r\n");
  138.         //
  139.         //     while (true)
  140.         //     {
  141.         //         // Do nothing.
  142.         //     }
  143.         // }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement