Appiko
main.c
1 /*
2  * main.c : Application for Bluey Demo
3  * Copyright (C) 2019 Appiko
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
28 #include <stdbool.h>
29 #include <stdint.h>
30 
31 #include "nrf.h"
32 
33 #include "boards.h"
34 #include "hal_clocks.h"
35 #include "hal_nop_delay.h"
36 #include "hal_gpio.h"
37 #include "common_util.h"
38 #include "tinyprintf.h"
39 #include "uart_printf.h"
40 #include "us_timer.h"
41 #include "nrf_util.h"
42 #include "ble_adv.h"
43 #include "ms_timer.h"
44 #include "profiler_timer.h"
45 
47 static void rgb_led_init(void)
48 {
49  hal_gpio_cfg_output(LED_RED, !(LEDS_ACTIVE_STATE));
50  hal_gpio_cfg_output(LED_GREEN, !(LEDS_ACTIVE_STATE));
51  hal_gpio_cfg_output(LED_BLUE, !(LEDS_ACTIVE_STATE));
52 }
53 
55 static void rgb_led_cycle(void)
56 {
57  hal_gpio_pin_write(LED_RED, (LEDS_ACTIVE_STATE));
58  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
59  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
60  hal_nop_delay_ms(250);
61  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
62  hal_gpio_pin_write(LED_GREEN, (LEDS_ACTIVE_STATE));
63  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
64  hal_nop_delay_ms(250);
65  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
66  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
67  hal_gpio_pin_write(LED_BLUE, (LEDS_ACTIVE_STATE));
68  hal_nop_delay_ms(250);
69  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
70  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
71  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
72 }
73 
74 static void log_dump_handler(void){
75  dump_log();
76  static uint8_t count = 0;
77  count++;
78  uint8_t adv_data[] = {
79  /* Len, type, data */
80  0x02, GAP_ADV_FLAGS, 0x04,
81  0x04, GAP_ADV_NAME_FULL,'E','f','H',
82  0x02, 0xFF, count
83  };
84  ble_adv_set_adv_data(sizeof(adv_data), adv_data);
85 }
86 
90 int main(void)
91 {
92  rgb_led_init();
93  rgb_led_cycle();
94  /* Initial printf */
96  tfp_printf("Hello World %d!\n", 42);
97 
98  ms_timer_init(APP_IRQ_PRIORITY_LOWEST);
99  us_timer_init(APP_IRQ_PRIORITY_LOWEST);
101 
104 
105  uint8_t adrs[] = {0x0B, 0x0E, 0x0A, 0x0C, 0x00, 0x01};
106  uint8_t adv_data[] = {
107  /* Len, type, data */
108  0x02, GAP_ADV_FLAGS, 0x04,
109  0x04, GAP_ADV_NAME_FULL,'E','f','H',
110  0x02, GAP_ADV_MANUF_DATA, 0
111  };
112 
113  uint8_t scan_rsp[] = {
114  0x02, GAP_ADV_TRANSMIT_PWR, 0
115  };
116 
118  ble_adv_param_t param = {ADV_INTERVAL_MS(500), ADV_SCAN_IND_PARAM, RANDOM_ADRS_PARAM, CH_ALL_PARAM};
119 
122  ble_adv_set_adv_data(sizeof(adv_data), adv_data);
123  ble_adv_set_adv_param(&param);
124  ble_adv_set_scan_rsp_data(sizeof(scan_rsp), scan_rsp);
125 
126  ble_adv_start();
127 
128  ms_timer_start(MS_TIMER0, MS_REPEATED_CALL, RTC_TICKS_MS(1027), log_dump_handler);
130 
131  while (true)
132  {
133  __WFI();
134  }
135 }
136 
CH_ALL_PARAM.
Definition: ble_adv.h:118
void profiler_timer_init(void)
void ble_adv_start(void)
Start advertising based on the parameters set.
Definition: ble_adv.c:550
void ble_adv_set_tx_power(int8_t pwr)
Set the advertising transmission power in dBm.
Definition: ble_adv.c:229
#define PROFILE_STOP
#define ADV_INTERVAL_MS(x)
Definition: ble_adv.h:62
void us_timer_init(uint32_t irq_priority)
Initialize the TIMER peripheral to use as a micro-second timer.
Definition: us_timer.c:56
#define LEDS_ACTIVE_STATE
Definition: bluey_1v1.h:44
void lfclk_init(lfclk_src_t lfclk_src)
Function to initialize the LF clock.
Definition: hal_clocks.c:23
Repeated call of the timer.
Definition: ms_timer.h:83
void ble_adv_set_adv_param(ble_adv_param_t *adv_param)
Set the advertising parameters to be used.
Definition: ble_adv.c:202
UART_PRINTF_BAUD_1M.
Definition: uart_printf.h:67
void ms_timer_init(uint32_t irq_priority)
Definition: ms_timer.c:111
void uart_printf_init(uart_printf_baud_t baud_rate)
Function to initialize the parameters of UART based on the configurations in boards....
Definition: uart_printf.c:179
void ble_adv_set_scan_rsp_data(uint8_t len, uint8_t *data_ptr)
Set the scan response data.
Definition: ble_adv.c:251
Millisecond Timer 0.
Definition: ms_timer.h:69
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
Definition: ms_timer.c:134
void ble_adv_set_random_adrs(uint8_t *rand_adrs)
Set the random address to be used.
Definition: ble_adv.c:242
The structure format for setting the advertisement parameters.
Definition: ble_adv.h:124
void hfclk_xtal_init_blocking(void)
Function to start the crystal oscillator to be used for HF clock. This function blocks until the crys...
Definition: hal_clocks.c:71
int main(void)
Function for application main entry.
Definition: main.c:90
void ble_adv_set_adv_data(uint8_t len, uint8_t *data_ptr)
Set the advertising data.
Definition: ble_adv.c:246
External crystal.
Definition: hal_clocks.h:38
#define PROFILE_START