1.添加设置蓝牙MAC地址 ,名称,2.整合蓝牙收发接口,3.添加NST1002 测温传感器驱动,待验证

This commit is contained in:
helloyifa 2025-04-09 11:24:48 +08:00
parent 6233f7c07b
commit 3fceda8bf1
5 changed files with 329 additions and 195 deletions

View File

@ -128,21 +128,6 @@ void enter_sleep(){
//power_set_soft_poweroff();
}
static int led_state = 0;
void blink_led(){
if(led_state){
gpio_direction_output(IO_PORTA_07, 0);
led_state = 0;
}else{
led_state = 1;
gpio_direction_output(IO_PORTA_07, 1);
}
//int vbat = get_vbat_value();
//printf(">>>>>>>>>>>>>>>>> vbat:%d\n",vbat);
}
void alarm_isr_user_cbfun(u8 index)
{
printf("**** alarm %d : hello world ****\n", index);
@ -184,7 +169,7 @@ void app_main()
} else {
check_power_on_voltage();
}
temperature_ble_init();
#if TCFG_POWER_ON_NEED_KEY
check_power_on_key();
#endif

View File

@ -8,192 +8,333 @@
#include "app_power_manage.h"
#include "asm/charge.h"
#include "app_temperature.h"
#include "typedef.h"
#include "le_common.h"
#include "cpu.h"
#include "timer.h"
#include "os/os_api.h"
#include "system/includes.h"
#include "gatt_common/le_gatt_common.h"
#include "examples/trans_data/ble_trans_profile.h"
//#include "ble_trans_profile.h"
static int led_state = 0;
void temperature_blink_led(){
if(led_state){
void temperature_blink_led()
{
if (led_state)
{
gpio_direction_output(IO_PORTA_07, 0);
led_state = 0;
}else{
}
else
{
led_state = 1;
gpio_direction_output(IO_PORTA_07, 1);
}
}
/*********************** 宏定义 *********************************/
#define NTC_ADC_MAX 90+1
#define TEMPERA_START 3400
/**************** NTC热敏电阻转换成ADC的值 ***********************/
const uint16_t NTC_ADC_Tab[NTC_ADC_MAX]={
/*2559 ,2555 ,2551 ,2546 ,2542 ,2538 ,2534 ,2529 ,2525 ,2521 ,// 25.0~25.9 ℃
//2517 ,2513 ,2508 ,2504 ,2500 ,2496 ,2491 ,2487 ,2483 ,2479 ,// 26.0~26.9 ℃
2474 ,2470 ,2466 ,2461 ,2457 ,2453 ,2449 ,2444 ,2440 ,2436 ,// 27.0~27.9 ℃
2432 ,2427 ,2423 ,2419 ,2415 ,2410 ,2406 ,2402 ,2397 ,2393 ,// 28.0~28.9 ℃
2389 ,2385 ,2380 ,2376 ,2372 ,2368 ,2363 ,2359 ,2355 ,2350 ,// 29.0~29.9 ℃
2346 ,2342 ,2338 ,2333 ,2329 ,2325 ,2320 ,2316 ,2312 ,2308 ,// 30.0~30.9 ℃
2303 ,2299 ,2295 ,2291 ,2286 ,2282 ,2278 ,2273 ,2269 ,2265 ,// 31.0~31.9 ℃
2261 ,2256 ,2252 ,2248 ,2243 ,2239 ,2235 ,2231 ,2226 ,2222 ,// 32.0~32.9 ℃
2218 ,2214 ,2209 ,2205 ,2201 ,2196 ,2192 ,2188 ,2184 ,2179 ,// 33.0~33.9 ℃*/
//34.0
33806 ,33653 ,33500 ,33347 ,33194 ,33041 ,32888 ,32735 ,32582 ,32429,// 34.0~34.9 ℃
32436 ,32290 ,32144 ,31998 ,31852 ,31706 ,31560 ,31414 ,31268 ,31122,// 35.0~35.9 ℃
31127 ,30988 ,30849 ,30710 ,30571 ,30432 ,30293 ,30154 ,30015 ,29800,// 36.0~36.9 ℃
29876 ,29743 ,29610 ,29477 ,29344 ,29211 ,29078 ,28945 ,28812 ,28679,// 37.0~37.9 ℃
28680 ,28552 ,28424 ,28296 ,28168 ,28040 ,27912 ,27784 ,27656 ,27528,// 38.0~38.9 ℃
27535 ,27413 ,27291 ,27169 ,27047 ,26925 ,26803 ,26681 ,26559 ,26437,// 39.0~39.9 ℃
26441 ,26324 ,26207 ,26090 ,25973 ,25856 ,25739 ,25622 ,25505 ,25388,// 40.0~40.9 ℃
25394 ,25282 ,25170 ,25058 ,24946 ,24834 ,24722 ,24610 ,24498 ,24386,// 41.0~41.9 ℃
24393 ,24283 ,24173 ,24063 ,23953 ,23843 ,23733 ,23623 ,23513 ,23403 ,// 42.0~42.9 ℃
23300,
//43.0
/*
1801 ,1797 ,1793 ,1789 ,1785 ,1781 ,1777 ,1773 ,1769 ,1765 ,// 43.0~43.9 ℃
1761 ,1757 ,1753 ,1750 ,1746 ,1742 ,1738 ,1734 ,1730 ,1726 ,// 44.0~44.9 ℃
1722 ,// 45 ℃*/
};
void temperature_detect(void)
void ble_send_data(u8 *sdata,u8 len)
{
printf("***************** adc test ***************\n\r");
//adc_init();
//u32 adc_io = IO_PORTB_06;
//gpio_set_die(adc_io, 0); //将io设为模拟功能
//浮空输入
//gpio_set_direction(adc_io, 1); //方向设为输入
//gpio_set_pull_up(adc_io, 0); //关上拉10K
//gpio_set_pull_down(adc_io, 0); //关下拉10K
u32 ch_ntc_adc = AD_CH_PB4;
u32 ch_ntc_power_adc = AD_CH_PB6;
extern void wdt_clr();
u16 ntc_adc_val = 0;
u16 ntc_adc_vol = 0;
//u16 ntc_adc_val = adc_get_value(ch_ntc_adc);
//u16 ntc_adc_vol = adc_get_voltage(ch_ntc_adc);
u16 ntc_power_adc_val = 0;
u16 ntc_power_adc_vol = 0;
//u16 ntc_power_adc_val = adc_get_value(ch_ntc_power_adc);
//u16 ntc_power_adc_vol = adc_get_voltage(ch_ntc_power_adc);
for(int i = 0 ;i < 10 ;i ++){
u16 ntc_adc_val_temp = adc_get_value(ch_ntc_adc);
ntc_adc_val = ntc_adc_val + ntc_adc_val_temp;
u16 ntc_adc_vol_temp = adc_get_voltage(ch_ntc_adc);
ntc_adc_vol = ntc_adc_vol + ntc_adc_vol_temp;
u16 ntc_power_adc_val_temp = adc_get_value(ch_ntc_power_adc);
ntc_power_adc_val = ntc_power_adc_val + ntc_power_adc_val_temp;
u16 ntc_power_adc_vol_temp = adc_get_voltage(ch_ntc_power_adc);
ntc_power_adc_vol = ntc_power_adc_vol + ntc_power_adc_vol_temp;
//先查明当前连接的conn_handle
u16 connection_handle = ble_comm_dev_get_handle(0, GATT_ROLE_SERVER);
printf("connection_handle: %04x\n", connection_handle);
if(connection_handle == 0)//无连接
{
return;
}
//检查预备发送的数据包长度是否能填入,并且获取 notify or indicate 通知使能值
if (ble_comm_att_check_send(connection_handle, len) &&
ble_gatt_server_characteristic_ccc_get(connection_handle, ATT_CHARACTERISTIC_ae02_01_CLIENT_CONFIGURATION_HANDLE))
{
//使用notify方式发送
ble_comm_att_send_data(connection_handle, ATT_CHARACTERISTIC_ae02_01_VALUE_HANDLE, sdata, len, ATT_OP_NOTIFY);
}
}
ntc_adc_val = ntc_adc_val/10;
ntc_adc_vol = ntc_adc_vol/10;
ntc_power_adc_val = ntc_power_adc_val/10;
ntc_power_adc_vol = ntc_power_adc_vol/10;
//adc_get_value_by_isr(AD_CH_PB6, user_adc_cbfun);
//printf("vbg_val = %d\n", adc_get_voltage(AD_CH_LDOREF));
//printf("adc_val = %d\n", adc_val);
printf("ntc_adc = %d >>> %d mv\n", ntc_adc_val, ntc_adc_vol);
printf("ntc_power_adc = %d >>> %d mv\n", ntc_power_adc_val, ntc_power_adc_vol);
u32 i=(ntc_power_adc_vol-ntc_adc_vol)*100000/51000;//uA*100
printf("ntc I = %d \n", i );
//co_printf("i:%d\r\n",(uint16_t)i);
u32 R = ntc_adc_vol*1000*100/i;
printf("ntc R = %d \n", R );
u16 End = NTC_ADC_MAX - 1;// 数组下标最后一个数
u16 Front = 0;// 数组第一个数
u8 half = 0;
//u16 TempSingleADC = 0;// 单次转换的ADC值
u16 TempSingleValue = 0;// 单次换算成的温度值,用来函数返回值;
/**********二分法查表求温度值*********/
if ((R <= NTC_ADC_Tab[0]) && (R >= NTC_ADC_Tab[NTC_ADC_MAX - 1]))
{
for (half = 100; End - Front != 1;)
{
if (R > NTC_ADC_Tab[half])
{
End = half;
half = (End + Front) / 2;
void ble_receive_callback(u8* data,u16 len){
printf("ble_receive_callback: %s\n", data);
}
else if (R < NTC_ADC_Tab[half])
//有概率会被中断打断,导致延时变大
AT_VOLATILE_RAM_CODE
void delay_us_by_timer0(u32 usec)
{
Front = half;
half = (Front + End) / 2;
usec -= 2; //usec > 5,减掉此函数执行本身的耗时48M->2us24M->5us
JL_TIMER0->CON = BIT(14);
JL_TIMER0->CNT = 0;
JL_TIMER0->PRD = clk_get("lsb") / 2 / 1000000L * usec;
JL_TIMER0->CON = BIT(0) | BIT(6); //lsb clk 2分频
while ((JL_TIMER0->CON & BIT(15)) == 0);
JL_TIMER0->CON = BIT(14);
}
else // 正好等于表格中的值
//关中断,临界区延时
AT_VOLATILE_RAM_CODE
void delay_us_by_timer0_irq(u32 usec)
{
TempSingleValue = TEMPERA_START + half * 10 + (NTC_ADC_Tab[half] - R) * 10 / (NTC_ADC_Tab[half] - NTC_ADC_Tab[half + 1]);
usec -= 2; //usec > 5,减掉此函数执行本身的耗时48M->2us24M->5us
local_irq_disable();
JL_TIMER0->CON = BIT(14);
JL_TIMER0->CNT = 0;
JL_TIMER0->PRD = clk_get("lsb") / 2 / 1000000L * usec;
JL_TIMER0->CON = BIT(0) | BIT(6); //lsb clk 2分频
while ((JL_TIMER0->CON & BIT(15)) == 0);
JL_TIMER0->CON = BIT(14);
local_irq_enable();
}
//ms级延时一般要求不需要精准不用加关中断
AT_VOLATILE_RAM_CODE
void delay_ms_by_timer0(u32 msec)
{
JL_TIMER0->CON = BIT(14);
JL_TIMER0->CNT = 0;
JL_TIMER0->PRD = clk_get("lsb") / 64 / 1000L * msec;
JL_TIMER0->CON = BIT(0) | BIT(5) | BIT(4); //lsb clk 64分频
while ((JL_TIMER0->CON & BIT(15)) == 0);
JL_TIMER0->CON = BIT(14);
}
static void udelay(u32 usec)
{
/* if (set_to_close_timer0_delay) { */
/* JL_MCPWM->MCPWM_CON0 &= ~BIT(8 + 3); */
/* JL_MCPWM->TMR3_CNT = 0; */
/* JL_MCPWM->TMR3_PR = clk_get("lsb") / 1000000 * usec; */
/* JL_MCPWM->TMR3_CON = BIT(10) | BIT(0); */
/* JL_MCPWM->MCPWM_CON0 |= BIT(8 + 3); */
/* while (!(JL_MCPWM->TMR3_CON & BIT(12))); */
/* JL_MCPWM->TMR3_CON = BIT(10); */
/* JL_MCPWM->MCPWM_CON0 &= ~BIT(8 + 3); */
/* } else { */
JL_TIMER0->CON = BIT(14);
JL_TIMER0->CNT = 0;
JL_TIMER0->PRD = 16 * 1000000L / 1000000L * usec; //1us
JL_TIMER0->CON = BIT(0); //sys clk
while ((JL_TIMER0->CON & BIT(15)) == 0);
JL_TIMER0->CON = BIT(14);
/* } */
}
#ifdef BLE_TEMPERATURE_DEVICE_NST1002
//extern void handshake_timer_delay_us(u8 us);
//extern void handshake_timer_delay_ms(u8 ms);
#define ERR_NC -100
#define ERR_DONE -101
#define ERR_DAT -102
#define ERR_CRC -103
//-----------------by usr --- ------------------
#define pin_write(x, y) gpio_direction_output(x, y);//rt_pin_write(x, y)
//#define pin_mode(x, y) rt_pin_mode(x, y)
#define pin_read(x) gpio_read(x)
#define DQ_PIN IO_PORTB_04 //GET_PIN(A, 1)
#define delay_ms(x) delay_ms_by_timer0(x);//rt_thread_mdelay(x)
#define delay_us(x) udelay(x);//rt_hw_us_delay(x)
//--------------------------------------
static uint8_t reversal(uint8_t data)
{
uint8_t _data = data;
uint8_t i, retValue = 1;
for (i = 0; i < 8; i++)
{
unsigned char temp = _data & 0x01;
if (temp)
retValue |= 0x01;
else
retValue &= ~0x01;
if (i == 7)
break;
retValue <<= 1;
_data >>= 1;
}
return retValue;
}
if (End - Front == 1) // 在表格两个值之间的数
// crc8_maxim input and output reversal
//x8+x5+x4+1 0x131
static uint8_t crc8_maxim(uint8_t *pdat, uint8_t len)
{
TempSingleValue = TEMPERA_START + half * 10 + (NTC_ADC_Tab[half] - R) * 10 / (NTC_ADC_Tab[half] - NTC_ADC_Tab[half + 1]);
uint8_t ret;
uint8_t uCRC = 0x00; //CRC
for (uint8_t num = 0; num < len; num++)
{ ret = reversal(*pdat++);
uCRC = (ret) ^ uCRC; //
for (uint8_t x = 0; x < 8; x++)
{
if (uCRC & 0x80)
{
uCRC = uCRC << 1; //
uCRC = uCRC ^ 0x31; //
}
else //
{
uCRC = uCRC << 1; //
}
}
}
ret = reversal(uCRC);
return ret;
}
int nst1002_read_cal(uint16_t *pcal)
{
int retry = 0;
uint8_t crc;
uint8_t i, j;
uint8_t bit0 = 1;
uint8_t data[3] = {0, 0, 0};
uint8_t swap[2];
uint8_t read[4];
//--------------power up-----------------------
//pin_mode(DQ_PIN, PIN_MODE_OUTPUT_OD);
//pin_write(DQ_PIN, PIN_HIGH);
gpio_set_direction(DQ_PIN,0);
gpio_direction_output(DQ_PIN, 1);
delay_ms(15);
//--------------send start convert -----------------------------
gpio_direction_output(DQ_PIN, 0);//pin_write(DQ_PIN, PIN_LOW); // DQ low
delay_us(300);
gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH);
delay_us(10);
//-----------------check DQ pin status should be high---------------------------------------
gpio_set_direction(DQ_PIN,1);//pin_mode(DQ_PIN, PIN_MODE_INPUT);
delay_us(100);
if (pin_read(DQ_PIN) == 0)
{
*pcal = (uint16_t)(ERR_NC * 128);
return -1;
}
delay_ms(20);
//--------------wait for done pulse---------------
do
{
bit0 = pin_read(DQ_PIN);
delay_us(2);
retry++;
if (retry > 20000) // time out 40ms
{
*pcal = (uint16_t)(ERR_DONE * 128);
return -2;
}
} while (bit0);
delay_us(10);
//--------------read 24bit data---------------
gpio_set_direction(DQ_PIN,0);
gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH);
//pin_mode(DQ_PIN, PIN_MODE_OUTPUT_OD);
delay_us(200);
for (i = 0; i < 24; i++)
{
gpio_direction_output(DQ_PIN, 0);//pin_write(DQ_PIN, PIN_LOW); // Falling edge
delay_us(1);// Adjust according to the mcu clock cycle,It's not necessary
gpio_set_direction(DQ_PIN,1);//pin_mode(DQ_PIN, PIN_MODE_INPUT);
// If the output bit is 0, the DQ will remain low for about 14us
for (j = 0; j < 4; j++)
// It's not necessary to read it 4 timers , adjust according to the mcuclock cycle
{
read[j] = pin_read(DQ_PIN);
}
if (read[0] == read[1]) // It's not necessary
bit0 = read[1];
else
{
TempSingleValue = 0; // 温度超出数组范围,就返回0度
*pcal = (uint16_t)(ERR_DAT * 128);
return -3;
}
printf("T = %d \n", TempSingleValue );
// 输入参数
//float Vcc = 3.0; // 电源电压(单位:伏特)
//float R0B = 51000; // 固定电阻阻值(单位:欧姆)
//float V1 = io_vol/1000.0f; // 热敏电阻两端电压(单位:伏特)
data[i / 8] <<= 1;
data[i / 8] |= bit0;
// 计算热敏电阻的阻值
//float RR = calculateResistance(io_vol/1000.0f, _Vcc, R0B);
gpio_set_direction(DQ_PIN,0);
gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH); // DQ low
//float RR = R0 * (V1 / (Vcc - V1));
//printf("calculateResistance = %d ", RR );
/*
while (1) {
wdt_clr();
delay(1000000);
_vbg_val = adc_get_value(AD_CH_LDOREF);
adc_val = adc_get_value(AD_CH_PB6);
io_vol = adc_get_voltage(AD_CH_PB6);
//adc_get_value_by_isr(AD_CH_PB6, user_adc_cbfun);
printf("adc_val = %d >>> %d mv\n", adc_val, io_vol*4);
printf("adc_val = %d\n", adc_val);
//pin_mode(DQ_PIN, PIN_MODE_OUTPUT_OD);
delay_us(60);
}
*/
//------------------- NST1002 Big Endian,but mcu Little Endian
swap[0] = data[1];
swap[1] = data[0];
*pcal = *((uint16_t *)swap); // output data need swap
crc = crc8_maxim(data, 2);
if (crc == data[2])
return 0;
else
{
printf("data %02x %02x %02x CRC %02x\r\n", data[0], data[1], data[2], crc);
*pcal = ERR_CRC * 128;
return -4;
}
}
char err_code[4][20] = {"NOT Connected", "No DONE signal", "DAT Recv Err", "CRC Err"};
void read_temp(void)
{
int err, ei;
double dtmp;
int16_t cal;
err = nst1002_read_cal((uint16_t *)&cal);
if (err < 0)
{
if (ei < 4)
ei = -1 - err;
printf("NST1002 err %s\r\n", err_code[ei]);
return;
}
dtmp = cal / 128.0;
printf("Temp %3.7f\r\n", dtmp);
}
void temperature_detect(void)
{
u8 str_data [24] ={0};
sprintf(str_data,"%s","hello");
ble_send_data(&str_data,5);
read_temp();
}
#endif
void temperature_init(void){
printf(">>>>>>>>>>>>>>>>> temperature_init ...\n");
extern u16 get_vbat_trim();
extern u16 get_vbg_trim();
printf("vbat_trim:%d...vbg_trim:%d",get_vbat_trim(),get_vbg_trim());
//添加以上代码看打印值如果为15跟63那就要用烧录器先烧录一遍。
void temperature_ble_init(){
printf(">>>>>>>>>>>>>>>>> temperature_ble_init ...\n");
// GET DEVICE MAC
u8 bt_mac_addr[6] = {0};
bt_get_vm_mac_addr(&bt_mac_addr);
// le_controller_set_mac((void *)bt_mac_addr);
printf("device mac: %02x %02x %02x %02x %02x %02x \n", bt_mac_addr[0], bt_mac_addr[1], bt_mac_addr[2], bt_mac_addr[3], bt_mac_addr[4], bt_mac_addr[5]);
//设置MAC地址
le_controller_set_mac((void *)bt_mac_addr);
//设置名称
char ble_name[12] ={0};
sprintf(ble_name,"iT12_%02X%02X",bt_mac_addr[1],bt_mac_addr[0]);
ble_comm_set_config_name(&ble_name, 0);
}
void temperature_init(void)
{
printf(">>>>>>>>>>>>>>>>> temperature_init ...\n");
udelay(20);
printf(">>>>>>>>>>>>>>>>> temperature_init2 ...\n");
udelay(1000*200);
printf("mdelay 200ms s...\n");
delay_ms(200);
printf("mdelay 200ms e...\n");
extern u16 get_vbat_trim();
extern u16 get_vbg_trim();
printf("vbat_trim:%d...vbg_trim:%d", get_vbat_trim(), get_vbg_trim());
// 添加以上代码看打印值如果为15跟63那就要用烧录器先烧录一遍。
// NTC VDD
gpio_direction_output(IO_PORTB_05, 1);
@ -202,7 +343,8 @@ void temperature_init(void){
sys_s_hi_timer_add(NULL, temperature_detect, 1000);
}
void temperature_deinit(void){
void temperature_deinit(void)
{
printf(">>>>>>>>>>>>>>>>> temperature_deinit ...\n");
// NTC VDD
gpio_direction_output(IO_PORTB_05, 0);

View File

@ -32,7 +32,7 @@
#include "gatt_common/le_gatt_common.h"
#include "ble_trans.h"
#include "ble_trans_profile.h"
#include "app_temperature.h"
#if CONFIG_APP_SPP_LE
#if LE_DEBUG_PRINT_EN
@ -612,13 +612,16 @@ static int trans_att_write_callback(hci_con_handle_t connection_handle, uint16_t
log_info("\n-ae01_rx(%d):", buffer_size);
put_buf(buffer, buffer_size);
ble_receive_callback(buffer,buffer_size);
//收发测试,自动发送收到的数据;for test
//helloyifa
#if 0
if (ble_comm_att_check_send(connection_handle, buffer_size) &&
ble_gatt_server_characteristic_ccc_get(trans_con_handle, ATT_CHARACTERISTIC_ae02_01_CLIENT_CONFIGURATION_HANDLE)) {
log_info("-loop send1\n");
ble_comm_att_send_data(connection_handle, ATT_CHARACTERISTIC_ae02_01_VALUE_HANDLE, buffer, buffer_size, ATT_OP_AUTO_READ_CCC);
}
#endif
break;
case ATT_CHARACTERISTIC_ae03_01_VALUE_HANDLE:
@ -955,7 +958,9 @@ void bt_ble_init(void)
#if DOUBLE_BT_SAME_NAME
ble_comm_set_config_name(bt_get_local_name(), 0);
#else
ble_comm_set_config_name(bt_get_local_name(), 1);
//helloyifa
//ble_comm_set_config_name(bt_get_local_name(), 1);
//ble_comm_set_config_name("iT12", 1);
#endif
trans_con_handle = 0;
trans_server_init();

View File

@ -30,6 +30,8 @@ struct app_dev_config_t
extern struct app_dev_info_t dev_info;
void temperature_ble_init();
void temperature_init(void);
void ble_receive_callback(u8* data,u16 len);
#endif //_APP_TEMPERATURE_H_

View File

@ -82,7 +82,7 @@ void btstack_ble_start_before_init(const ble_init_cfg_t *cfg, int param)
tmp_ble_addr[3] = 0x44;
tmp_ble_addr[4] = 0x55;
tmp_ble_addr[5] = 0x66;
le_controller_set_mac((void *)tmp_ble_addr);
//le_controller_set_mac((void *)tmp_ble_addr);
log_info("---ble's address");
printf_buf((void *)tmp_ble_addr, 6);