xref: /btstack/port/nrf5-cinnamon/main.c (revision fb5f22f21bf7c4de2188a2dc4e70cc313c8c7291)
1 /**
2  * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form, except as embedded into a Nordic
13  *    Semiconductor ASA integrated circuit in a product or a software update for
14  *    such product, must reproduce the above copyright notice, this list of
15  *    conditions and the following disclaimer in the documentation and/or other
16  *    materials provided with the distribution.
17  *
18  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
19  *    contributors may be used to endorse or promote products derived from this
20  *    software without specific prior written permission.
21  *
22  * 4. This software, with or without modification, must only be used with a
23  *    Nordic Semiconductor ASA integrated circuit.
24  *
25  * 5. Any software provided in binary form under this license must not be reverse
26  *    engineered, decompiled, modified and/or disassembled.
27  *
28  * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
29  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30  * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
34  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  */
40 /** @file
41  *
42  * @defgroup blinky_example_main main.c
43  * @{
44  * @ingroup blinky_example
45  * @brief Blinky Example Application main file.
46  *
47  * This file contains the source code for a sample application to blink LEDs.
48  *
49  */
50 
51 #include <stdbool.h>
52 #include <stdint.h>
53 #include "boards.h"
54 #include "SEGGER_RTT.h"
55 
56 #include "nrf.h"
57 #include "nrf52.h"
58 #include "nrf_delay.h"
59 #include "nrfx_clock.h"
60 #include "hal_timer.h"
61 #include "radio.h"
62 #include <stdio.h>
63 
64 #include "btstack_memory.h"
65 #include "btstack_run_loop_embedded.h"
66 #include "controller.h"
67 #include "btstack_tlv.h"
68 #include "btstack_tlv_none.h"
69 #include "ble/le_device_db_tlv.h"
70 #include "hci_dump.h"
71 #include "hci_dump_segger_rtt_stdout.h"
72 #include "hci_dump_segger_rtt_binary.h"
73 #include "hci_dump_embedded_stdout.h"
74 
btstack_assert_failed(const char * file,uint16_t line_nr)75 void btstack_assert_failed(const char * file, uint16_t line_nr){
76     printf("Assert: file %s, line %u\n", file, line_nr);
77     while (1);
78 }
79 
80 /** hal_time_ms.h */
81 #include "hal_time_ms.h"
82 extern uint32_t hal_timer_get_ticks(void);
hal_time_ms(void)83 uint32_t hal_time_ms(void){
84     uint32_t ticks = hal_timer_get_ticks();
85     uint32_t seconds = ticks >> 15; // / 32768
86     uint32_t remaining_ms = (ticks & 0x7fff) * 1000 / 32768;
87     return seconds * 1000 + remaining_ms;
88 }
89 
90 /** hal_cpu.h */
91 
92 // TODO: implement
hal_cpu_disable_irqs(void)93 void hal_cpu_disable_irqs(void){
94     __disable_irq();
95 }
96 
hal_cpu_enable_irqs(void)97 void hal_cpu_enable_irqs(void){
98     __enable_irq();
99 }
100 
hal_cpu_enable_irqs_and_sleep(void)101 void hal_cpu_enable_irqs_and_sleep(void){
102     __enable_irq();
103     __asm__("wfe");	// go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag
104 }
105 
lf_clock_init(void)106 static void lf_clock_init(void) {
107     // select 32.768 kHz XTAL as LF Clock source and start
108     NRF_CLOCK->LFCLKSRC = NRF_CLOCK_LFCLK_Xtal;
109     NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
110     NRF_CLOCK->TASKS_LFCLKSTART = 1;
111     while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
112 }
113 
114 void btstack_main(void);
main(void)115 int main(void){
116 
117     // system init
118     lf_clock_init();
119     hal_timer_init();
120 
121 #if 0
122     // get startup time, around 9 ticks and verify that we don't need to wait until it's disabled
123     uint32_t t0 = hal_timer_get_ticks();
124     radio_hf_clock_enable(true);
125     uint32_t t1 = hal_timer_get_ticks();
126     radio_hf_clock_disable();
127     radio_hf_clock_enable(true);
128     radio_hf_clock_disable();
129     printf("HF Startup time: %lu ticks\n", t1-t0);
130 #endif
131 
132 
133     btstack_memory_init();
134     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
135 
136     // initialize controller
137     controller_init();
138 
139     // get virtual HCI transpoft
140     const hci_transport_t * hci_transport = controller_get_hci_transport();
141 
142     // TODO: use flash storage
143 
144     const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_none_init_instance();
145     // setup global tlv
146     btstack_tlv_set_instance(btstack_tlv_impl, NULL);
147 
148     // setup LE Device DB using TLV
149     le_device_db_tlv_configure(btstack_tlv_impl, NULL);
150 
151     // init HCI
152     hci_init(hci_transport, NULL);
153 
154     // uncomment one of the options to enable packet logger
155 #ifdef ENABLE_SEGGER_RTT
156     // hci_dump_init(hci_dump_segger_rtt_stdout_get_instance());
157 
158     // hci_dump_segger_rtt_binary_open(HCI_DUMP_PACKETLOGGER);
159     // hci_dump_init(hci_dump_segger_rtt_binary_get_instance());
160 #else
161     // hci_dump_init(hci_dump_embedded_stdout_get_instance());
162 #endif
163 
164     // hand over to btstack embedded code
165     btstack_main();
166 
167     // go
168     btstack_run_loop_execute();
169 
170     while (1){};}
171 
172 /**
173  *@}
174  **/
175