Files
microchip-application-bmd38…/main.c
T
2023-08-07 10:43:46 +08:00

235 lines
6.4 KiB
C

#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include "app_config.h"
#include "apply_old_config.h"
#include "sdk_config.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "nrf_sdh_freertos.h"
#include "FreeRTOS.h"
#include "task.h"
#ifdef __cplusplus
}
#endif
#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
extern void le_scan_start(void);
extern void le_scan_stop(void);
extern void le_gap_phy_update(uint16_t conn_handle);
extern void le_gap_conn_param_update(uint16_t conn_handle);
extern void le_db_discovery_start(uint16_t conn_handle);
static void le_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
{
ret_code_t err_code;
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_ADV_REPORT: {
extern void le_scan_handler(ble_evt_t const *p_ble_evt, void *p_context);
le_scan_handler(p_ble_evt, p_context);
}
break;
case BLE_GAP_EVT_PHY_UPDATE:
NRF_LOG_INFO("PHY update procedure is complete.");
break;
case BLE_GAP_EVT_CONN_PARAM_UPDATE:
NRF_LOG_INFO("Connection parameters updated.");
break;
case BLE_GAP_EVT_CONNECTED:
NRF_LOG_INFO("Connected to peer.");
le_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle);
le_gap_conn_param_update(p_ble_evt->evt.gap_evt.conn_handle);
le_db_discovery_start(p_ble_evt->evt.gap_evt.conn_handle);
break;
case BLE_GAP_EVT_DISCONNECTED:
NRF_LOG_INFO("Disconnected from peer.");
break;
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
le_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle);
break;
case BLE_GATTS_EVT_SYS_ATTR_MISSING: {
ret_code_t err_code = sd_ble_gatts_sys_attr_set(
p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0);
APP_ERROR_CHECK(err_code);
}
break;
default:
break;
}
}
static void le_stack_Init(void)
{
ret_code_t err_code;
uint32_t ram_start = 0;
ble_cfg_t ble_cfg;
memset(&ble_cfg, 0x00, sizeof(ble_cfg));
ble_cfg.conn_cfg.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
ble_cfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = 12;
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &ble_cfg, ram_start);
APP_ERROR_CHECK(err_code);
// Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
// Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, le_evt_handler, NULL);
}
#define ROY_MEM_SEL NRF_GPIO_PIN_MAP(1, 9)
#define ROY_MEM_BZY NRF_GPIO_PIN_MAP(0, 8)
#define ROY_MEM_REQ NRF_GPIO_PIN_MAP(0, 6)
#define ROY_RAM_SEL NRF_GPIO_PIN_MAP(0, 5)
#define ROY_MEM_TEST_01 NRF_GPIO_PIN_MAP(0, 26)
#define ROY_MEM_TEST_02 NRF_GPIO_PIN_MAP(0, 17)
#define ROY_MEM_TEST_03 NRF_GPIO_PIN_MAP(0, 21)
#define ROY_MEM_TEST_04 NRF_GPIO_PIN_MAP(0, 19)
#define ROY_MEM_TEST_05 NRF_GPIO_PIN_MAP(0, 22)
#define ROY_LED1 NRF_GPIO_PIN_MAP(1, 10)
static void mem_pin(void *p_arg)
{
nrf_gpio_cfg_output(ROY_MEM_SEL);
nrf_gpio_cfg_output(ROY_MEM_BZY);
nrf_gpio_cfg_output(ROY_MEM_REQ);
nrf_gpio_cfg_output(ROY_RAM_SEL);
static uint8_t time = 0;
for (;;) {
NRF_LOG_INFO("mem_pin %d", time);
time++;
nrf_gpio_pin_set(ROY_MEM_SEL);
nrf_gpio_pin_set(ROY_MEM_BZY);
nrf_gpio_pin_set(ROY_MEM_REQ);
nrf_gpio_pin_set(ROY_RAM_SEL);
NRF_LOG_INFO("mem_pin on");
nrf_delay_ms(2000);
nrf_gpio_pin_clear(ROY_MEM_SEL);
nrf_gpio_pin_clear(ROY_MEM_BZY);
nrf_gpio_pin_clear(ROY_MEM_REQ);
nrf_gpio_pin_clear(ROY_RAM_SEL);
NRF_LOG_INFO("mem_pin off");
nrf_delay_ms(2000);
}
}
static void test_pin(void *p_arg)
{
nrf_gpio_cfg_output(ROY_MEM_TEST_01);
nrf_gpio_cfg_output(ROY_MEM_TEST_02);
nrf_gpio_cfg_output(ROY_MEM_TEST_03);
nrf_gpio_cfg_output(ROY_MEM_TEST_04);
nrf_gpio_cfg_output(ROY_MEM_TEST_05);
nrf_gpio_cfg_output(ROY_LED1);
static uint8_t time = 0;
for (;;) {
NRF_LOG_INFO("test_pin %d", time);
time++;
nrf_gpio_pin_set(ROY_MEM_TEST_01);
nrf_gpio_pin_set(ROY_MEM_TEST_02);
nrf_gpio_pin_set(ROY_MEM_TEST_03);
nrf_gpio_pin_set(ROY_MEM_TEST_04);
nrf_gpio_pin_set(ROY_MEM_TEST_05);
nrf_gpio_pin_set(ROY_LED1);
NRF_LOG_INFO("test_pin on");
nrf_delay_ms(1000);
nrf_gpio_pin_clear(ROY_MEM_TEST_01);
nrf_gpio_pin_clear(ROY_MEM_TEST_02);
nrf_gpio_pin_clear(ROY_MEM_TEST_03);
nrf_gpio_pin_clear(ROY_MEM_TEST_04);
nrf_gpio_pin_clear(ROY_MEM_TEST_05);
nrf_gpio_pin_clear(ROY_LED1);
NRF_LOG_INFO("test_pin off");
nrf_delay_ms(1000);
}
}
static void initialize(void *p_context)
{
extern void uart_drv_init(void);
uart_drv_init();
extern void mem_board_init(void);
mem_board_init();
extern void le_db_discovery_init(void);
le_db_discovery_init();
extern void le_dis_c_init(void);
le_dis_c_init();
extern void le_gatt_c_init(void);
le_gatt_c_init();
extern void le_gap_init(void);
le_gap_init();
extern void le_gatt_init(void);
le_gatt_init();
extern void le_scan_init(void);
le_scan_init();
extern void host_tasks_init(void);
host_tasks_init();
}
int main(void)
{
NRF_LOG_INIT(NULL, 0);
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("%s Build: %s %s", LE_DEVICE_NAME, __TIME__, __DATE__);
//le_stack_Init();
//nrf_sdh_freertos_init(initialize, NULL);
//xTaskCreate(mem_pin, "mem_pin", 512, NULL, 5, NULL);
xTaskCreate(test_pin, "test_pin", 512, NULL, 5, NULL);
vTaskStartScheduler();
for (;;)
{
// Will not get here unless there is insufficient RAM.
__BKPT(255);
}
}
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
{
__BKPT(255);
}