Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
main.c
Go to the documentation of this file.
1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
2 *
3 * The information contained herein is property of Nordic Semiconductor ASA.
4 * Terms and conditions of usage are described in detail in NORDIC
5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
6 *
7 * Licensees are granted free, non-transferable use of the information. NO
8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
9 * the file.s
10 */
11 
12 #include <stdio.h>
13 #include <stdint.h>
14 #include "nrf_assert.h"
15 #include "nrf_gzll.h"
16 #include "app_print.h"
17 #include "simple_uart.h"
18 #include "wireless_debug_config.h"
19 #include "nrf_gpio.h"
20 #include "boards.h"
21 
30 #define DEBUG_POLL 0
31 #define DEBUG_PKT 1
32 #define DEBUG_STR 2
33 #define PIPE_NUMBER 0
34 #define ACK_PAYLOAD_LENGTH 1
35 #define DEBUG_PAYLOAD_LENGTH 32
36 #define DEBUG_LINE_LENGTH 80
37 
38 // Assert setup
39 #define DEAD_BEEF 0xDEADBEEF
40 
41 static uint8_t data_payload[NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH];
43 
44 static bool pkt_received;
45 
48 static void uart_config(uint32_t baudrate,
49  uint8_t rts_pin_number,
50  uint8_t txd_pin_number,
51  uint8_t cts_pin_number,
52  uint8_t rxd_pin_number,
53  bool hwfc)
54 {
55  nrf_gpio_cfg_output(txd_pin_number);
56  nrf_gpio_cfg_input(rxd_pin_number, NRF_GPIO_PIN_NOPULL);
57 
58  NRF_UART0->PSELTXD = txd_pin_number;
59  NRF_UART0->PSELRXD = rxd_pin_number;
60  if (hwfc)
61  {
62  nrf_gpio_cfg_output(rts_pin_number);
63  nrf_gpio_cfg_input(cts_pin_number, NRF_GPIO_PIN_NOPULL);
64  NRF_UART0->PSELCTS = cts_pin_number;
65  NRF_UART0->PSELRTS = rts_pin_number;
66  NRF_UART0->CONFIG = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos);
67  }
68 
69  NRF_UART0->BAUDRATE = (baudrate << UART_BAUDRATE_BAUDRATE_Pos);
70  NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
71  NRF_UART0->TASKS_STARTTX = 1;
72  NRF_UART0->TASKS_STARTRX = 1;
73  NRF_UART0->EVENTS_RXDRDY = 0;
74 }
75 
76 
79 static bool m_uart_chars_available(void)
80 {
81  return (NRF_UART0->EVENTS_RXDRDY == 1);
82 }
83 
86 static void line_print(void)
87 {
88  int i;
89 
90  for(i = 0; i < DEBUG_LINE_LENGTH; i++)
91  {
92  print_string("-");
93  }
94  print_string("\r\n");
95 }
96 
99 static char * state_string_get(uint8_t state)
100 {
101  switch(state)
102  {
103  case 0:
104  return "PRU Null";
105  case 1:
106  return "PRU Boot";
107  case 2:
108  return "PRU On";
109  case 3:
110  return "PRU System Error";
111  default:
112  return "Invalid state";
113  }
114 
115 }
116 
121 static void debug_output_print(uint8_t *debug_data)
122 {
124  uint16_t tmp16_1, tmp16_2, tmp16_3, tmp16_4, tmp16_5;
125  int16_t temperature;
126 
127  // Status
128  line_print();
129  print_string("StatusStart\r\n");
130  line_print();
131  print_string("State\r\n");
132  sprintf(temp_string, "%s\r\n", state_string_get(debug_data[0]));
133  print_string(temp_string);
134 
135  // Dynamic parameters
136  line_print();
137  print_string("Dynamic parameters\r\n");
138  line_print();
139  tmp16_1 = (uint16_t)debug_data[2] << 8 | (uint16_t)debug_data[1];
140  tmp16_2 = (uint16_t)debug_data[4] << 8 | (uint16_t)debug_data[3];
141  tmp16_3 = (uint16_t)debug_data[6] << 8 | (uint16_t)debug_data[5];
142  tmp16_4 = (uint16_t)debug_data[8] << 8 | (uint16_t)debug_data[7];
143  temperature = (int16_t)((uint16_t)(debug_data[10]<<8 | (uint16_t)debug_data[9]));
144  print_string("Vrect\tIrect\tVout\tIout\tTemp\tCharged\tP_adj\r\n");
145  sprintf(temp_string, "%u\t%u\t%u\t%u\t%u\t%u\t%u\r\n", tmp16_1, tmp16_2, tmp16_3, tmp16_4, temperature, debug_data[11], debug_data[24]);
146  print_string(temp_string);
147 
148  // Static parameters
149  line_print();
150  print_string("Static parameters\r\n");
151  line_print();
152  tmp16_1 = (uint16_t)debug_data[13] << 8 | (uint16_t)debug_data[12];
153  tmp16_2 = (uint16_t)debug_data[15] << 8 | (uint16_t)debug_data[14];
154  tmp16_3 = (uint16_t)debug_data[17] << 8 | (uint16_t)debug_data[16];
155  tmp16_4 = (uint16_t)debug_data[20] << 8 | (uint16_t)debug_data[19];
156  tmp16_5 = (uint16_t)debug_data[22] << 8 | (uint16_t)debug_data[21];
157  print_string("Vrset\tVrmin\tVrmax\tTempmax\tVrmax\tPrmax\r\n");
158  sprintf(temp_string, "%u\t%u\t%u\t%u\t%u\t%u\r\n", tmp16_1, tmp16_2, tmp16_3, debug_data[18], tmp16_4, tmp16_5);
159  print_string(temp_string);
160 
161  // Previous received control data
162  line_print();
163  print_string("Control packet\r\n");
164  line_print();
165 
166  print_string("En_out\tEn_Chin\tAdj_pow\tPerm\tTset\r\n");
167  sprintf(temp_string, "%u\t%u\t%u\t%u\t%u\r\n", debug_data[25], debug_data[26], debug_data[27], debug_data[28], debug_data[29]);
168  print_string(temp_string);
169 
170  if(debug_data[23] != 0)
171  {
172  print_string("\r\n");
173  print_string("Alert sent\r\n");
174  }
175  print_string("StatusEnd\r\n");
176 }
177 
180 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
181 {
182  // This call can be used for debug purposes during development of an application.
183  // @note CAUTION: Activating this code will write the stack to flash on an error.
184  // This function should NOT be used in a final product.
185  // It is intended STRICTLY for development/debugging purposes.
186  // The flash write will happen EVEN if the radio is active, thus interrupting
187  // any communication.
188  // Use with care. Un-comment the line below to use.
189 
190  // On assert, the system can only recover with a reset.;
191  NVIC_SystemReset();
192 }
193 
194 
197 void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
198 {
199  app_error_handler(DEAD_BEEF, line_num, p_file_name);
200 }
201 
202 
205 int main()
206 {
207  bool res;
208  uint8_t channels[NRF_GZLL_CHANNEL_TABLE_SIZE] = NRF_GZLL_CHANNELS;
209  pkt_received = false;
210  res = nrf_gzll_init(NRF_GZLL_MODE_HOST);
211  ASSERT(res);
212  res = nrf_gzll_set_base_address_0(NRF_GZLL_BASE_ADDRESS_0);
213  ASSERT(res);
214  res = nrf_gzll_set_channel_table(channels, NRF_GZLL_CHANNEL_TABLE_SIZE);
215  ASSERT(res);
216  res = nrf_gzll_enable();
217  ASSERT(res);
218  uart_config(UART_BAUDRATE_BAUDRATE_Baud460800, RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);
219  while(m_uart_chars_available())
220  (void)simple_uart_get();
221  while(1)
222  {
223  if (pkt_received)
224  {
225  if (data_payload[0] == DEBUG_PKT)
227  else if (data_payload[0] == DEBUG_STR)
228  print_string((char *)&data_payload[1]);
229  pkt_received = false;
230  }
231  }
232 }
233 
237 void nrf_gzll_host_rx_data_ready(uint32_t pipe, nrf_gzll_host_rx_info_t rx_info)
238 {
239  bool res;
240  uint32_t data_payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH;
241  res = nrf_gzll_fetch_packet_from_rx_fifo(pipe, data_payload, &data_payload_length);
242  ASSERT(res);
243  if (data_payload[0] != DEBUG_POLL)
244  pkt_received = true;
246  {
248  res = nrf_gzll_add_packet_to_tx_fifo(pipe, ack_payload, ACK_PAYLOAD_LENGTH);
249  ASSERT(res);
250  }
251 }
252 
256 void nrf_gzll_device_tx_success(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info)
257 {
258 }
259 
263 void nrf_gzll_device_tx_failed(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info)
264 {
265 }
266 
271 {
272 }
273 
static void debug_output_print(uint8_t *debug_data)
Print debug output.
Definition: main.c:121
uint8_t simple_uart_get(void)
Function for reading a character from UART. Execution is blocked until UART peripheral detects charac...
Definition: simple_uart.c:22
void nrf_gzll_host_rx_data_ready(uint32_t pipe, nrf_gzll_host_rx_info_t rx_info)
GZLL data packet received callback.
Definition: main.c:237
void nrf_gzll_disabled()
GZLL Disabled callback.
Definition: main.c:270
int main()
Application main function.
Definition: main.c:90
static bool m_uart_chars_available(void)
Checks if there are any characters available in the UART.
Definition: main.c:79
void nrf_gzll_device_tx_failed(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info)
GZLL Transmission failed callback.
Definition: main.c:263
static void uart_config(uint32_t baudrate, uint8_t rts_pin_number, uint8_t txd_pin_number, uint8_t cts_pin_number, uint8_t rxd_pin_number, bool hwfc)
Local version of uart configure that take baud rate.
Definition: main.c:48
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
Error handler function, which is called when an error has occurred.
Definition: main.c:180
static void line_print(void)
Print a dashed line to the UART.
Definition: main.c:86
#define DEBUG_PKT
Packed type used for packet with payload.
Definition: main.c:31
void nrf_gzll_device_tx_success(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info)
GZLL ACK received callback.
Definition: main.c:256
static char * state_string_get(uint8_t state)
Convert state to string.
Definition: main.c:99
static bool pkt_received
True when a packet with first byte = DEBUG_POLL received.
Definition: main.c:44
static uint8_t data_payload[NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH]
GZLL data payload.
Definition: main.c:41
static uint8_t ack_payload[ACK_PAYLOAD_LENGTH]
GZLL ack payload.
Definition: main.c:42
void print_string(char *char_array)
Decomposes char array and send chars through UART TX.
Definition: app_print.c:25
#define DEAD_BEEF
Error code for assert.
Definition: main.c:39
#define DEBUG_LINE_LENGTH
Debug string length.
Definition: main.c:36
static char temp_string[DEBUG_LINE_LENGTH]
#define ACK_PAYLOAD_LENGTH
We use 1 byte payload length when transmitting.
Definition: main.c:34
#define DEBUG_POLL
Packed type used for polling opnly.
Definition: main.c:30
void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
Assert macro callback function.
Definition: main.c:197
#define NRF_GZLL_CHANNEL_TABLE_SIZE
#define DEBUG_STR
Packed type used for packet with string.
Definition: main.c:32
Simple UART driver.