Files
microchip-application-bmd38…/main.c
T
chain40 d9a2a7b4e2 feat: 實作 sw_drv, sw_drv_if, adgs1412 driver
1. sw_t 是一個 64位元的資料結構, 最大可支援 64 pin sw (adgs1412 最多支援 16 顆)
2024-03-29 00:28:42 +08:00

171 lines
4.7 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"
#include "timers.h"
#include "adc_drv.h"
#include "dac_drv.h"
#include "elite_pin_ctrl.h"
#include "led_drv.h"
#include "sw_drv.h"
#ifdef __cplusplus
}
#endif
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
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_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("Connect to peer.");
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN, p_ble_evt->evt.gap_evt.conn_handle, 8);
APP_ERROR_CHECK(err_code);
led_set(LED_IDEL_CONNECTED);
}
break;
case BLE_GAP_EVT_DISCONNECTED: {
NRF_LOG_INFO("Disconnect from peer.");
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, p_ble_evt->evt.gap_evt.conn_handle, 0);
APP_ERROR_CHECK(err_code);
}
break;
case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
NRF_LOG_INFO("PHY update response. (AUTO)");
ble_gap_phys_t const phys = {
.rx_phys = BLE_GAP_PHY_AUTO, // adv&connection: 1M PHY, After connection: mobile-2M, PC-1M
.tx_phys = BLE_GAP_PHY_AUTO,
};
err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
APP_ERROR_CHECK(err_code);
}
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);
}
static void nrf_sdh_freertos_task_hook(void *p_context)
{
UNUSED_PARAMETER(p_context);
gpio_init();
twi_init();
spi_init();
led_init();
sw_init();
adc_init();
dac_init();
if (1)
{
sw_t sw;
uint32_t sw_cnt;
sw_count(&sw_cnt);
sw_read(&sw);
sw.sw0 = 1;
sw.sw2 = 1;
sw.sw4 = 1;
sw.sw5 = 1;
sw_write(sw);
}
led_set(LED_IDEL_DISCONNECT);
le_stack_Init(); // enable ble stack, but unscannable!! register "le_evt_handler" handle(CB)
extern void le_gap_init(const char *device_name, uint16_t usAppearance);
le_gap_init(ELITE_DEVICE_NAME, BLE_APPEARANCE_UNKNOWN); // set BT name & connection interval
extern void le_gatt_init(void);
le_gatt_init();
extern void le_srv_init(void);
le_srv_init(); // service
extern void le_adv_init(uint8_t ble_conn_cfg_tag);
le_adv_init(APP_BLE_CONN_CFG_TAG); // could be scanned
}
int main(void)
{
NRF_LOG_INIT(NULL, 0);
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("%s Build: %s %s", ELITE_DEVICE_NAME, __TIME__, __DATE__);
nrf_sdh_freertos_init(nrf_sdh_freertos_task_hook, NULL); // create event: softdevice_task - wireless protocal stack
vTaskStartScheduler();
for (;;)
{
// Will not get here unless there is insufficient RAM.
__BKPT(255);
}
}