Skip to content

Commit d4dab00

Browse files
committed
Started PrimaryConverter BLE firmware
Can receive data from MCU, working on transmitting data
1 parent b7b8fd2 commit d4dab00

File tree

8 files changed

+464
-0
lines changed

8 files changed

+464
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<project>
3+
<gatt in="gatt.xml" />
4+
<hardware in="hardware.xml" />
5+
<script in="UART.bgs" />
6+
<device type="ble112" />
7+
<boot fw="bootota" />
8+
<image out="UART.hex" />
9+
<ota out="UART.ota" firmware="false" />
10+
<!-- firmware=false only updates the BGScript and GATT, =true updates the BT Stack and BGAPI, but is MUCH bigger -->
11+
<!-- <ota out="UART.ota" firmware="true" /> -->
12+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
2+
# OTA Variables
3+
dim command
4+
dim dfu_pointer # current pointer to flash location to upload firmware
5+
dim erase_page # current page to erase
6+
const MAX_ERASE_PAGE = 64
7+
8+
9+
# Handles OTA Control Point Attribute (commands) and OTA Data Attribute (firmware update) writes
10+
# and performs the necessary actions
11+
procedure handle_ota_control(connection, offset, value_len, value_data())
12+
# Check if OTA control point attribute is written by the remote device and execute the command
13+
# Command 0 : Erase flash block 0 (0x0-0x1FFFF)
14+
# Command 1 : Erase flash block 1 (0x10000-0x3FFFF)
15+
# Command 2 : Reset DFU data pointer
16+
# Command 3 : Boot to DFU mode
17+
# Command 4 : Power up external flash
18+
# In case of errors application error code 0x80 is returned to the remote device
19+
# In case the flash comms fails error code 0x90 is returned to the remote device
20+
21+
# Attribute is user attribute, reason is always write_request_user
22+
if value_len > 1 || offset > 0 then
23+
# Not a valid command -> report application error code : 0x80
24+
call attributes_user_write_response(connection, $80)
25+
else
26+
command = value_data(0:1)
27+
28+
if command > 4 then # Unknown command -> report application error code : 0x80
29+
call attributes_user_write_response(connection, $80)
30+
else
31+
if command = 3 then # Command 3 received -> Boot to DFU mode
32+
call system_reset(1)
33+
else
34+
# Other commands are not used, but still accepted in order
35+
# to be compatible with the external flash OTA
36+
# implementation
37+
call attributes_user_write_response(connection, $0)
38+
end if
39+
end if
40+
end if
41+
end
42+
43+
44+
# Incoming data event listener
45+
event attributes_value(connection, reason, handle, offset, value_len, value_data)
46+
47+
if (handle = device_reset) then
48+
command=value_data(0:1)
49+
# Command 1 received, reset device
50+
if command=1 then
51+
call system_reset(0)
52+
end if
53+
end if
54+
55+
# Both ota_control endpoints run the same code, however, the wo_response just ignores most of this
56+
if handle = ota_control || handle = ota_control_wo_response then
57+
call handle_ota_control(connection, offset, value_len, value_data(0:value_len))
58+
end if
59+
60+
# Check if OTA data attribute is written which carries the firmware update
61+
# and store the data to the internal flash
62+
if handle = ota_data || handle = ota_data_w_response then
63+
call flash_write_data(dfu_pointer, value_len, value_data(0:value_len))
64+
dfu_pointer = dfu_pointer + value_len
65+
end if
66+
end
67+
68+
69+
70+
# Boot event listener
71+
event system_boot(major ,minor ,patch ,build ,ll_version ,protocol_version ,hw)
72+
73+
#Set timer to generate event every 1s
74+
#call hardware_set_soft_timer(32768, 1, 0)
75+
76+
# configure P0.7 as output
77+
call hardware_io_port_config_direction(0, $80)
78+
79+
# Disable P0.7 pin. Parameters: I/O port to write to 0/1/2,
80+
# bitmask of pins to modify,
81+
# bitmask of pin values to set
82+
call hardware_io_port_write(0, $80, 0)
83+
84+
call gap_set_mode(gap_general_discoverable, gap_undirected_connectable) # Start advertisement
85+
call sm_set_bondable_mode(1)
86+
87+
call system_endpoint_set_watermarks(system_endpoint_uart0, 12, 0)
88+
89+
end
90+
91+
# Connection event listener
92+
event connection_status(connection, flags, address, address_type, conn_interval, timeout, latency, bonding)
93+
end
94+
95+
# Disconnection event listener
96+
event connection_disconnected(connection, reason)
97+
call gap_set_mode(gap_general_discoverable, gap_undirected_connectable) # Start advertisement
98+
end
99+
100+
# endpoint data in
101+
dim in(12)
102+
dim in_len
103+
dim result
104+
dim port
105+
dim data
106+
107+
# System endpoint watermark event listener
108+
# Generated when there is data available from UART
109+
event system_endpoint_watermark_rx(endpoint, size)
110+
call hardware_io_port_read(0, $80)(result, port, data)
111+
112+
if data & $80 then
113+
call hardware_io_port_write(0, $80, 0)
114+
else
115+
call hardware_io_port_write(0, $80, $80)
116+
end if
117+
118+
if endpoint = system_endpoint_uart0 then
119+
in_len = size
120+
121+
call system_endpoint_rx(system_endpoint_uart0, in_len)(result, in_len, in(0:in_len)) # read data from UART
122+
call system_endpoint_set_watermarks(system_endpoint_uart0, 0, $ff) # disable RX watermark
123+
call system_endpoint_set_watermarks(system_endpoint_uart0, 12, $ff) # enable RX watermark
124+
call attributes_write(xgatt_sensor, 0, in_len, in(0:in_len)) # Write data to GATT
125+
#call system_endpoint_tx(system_endpoint_uart0, 7, "Hello\n")
126+
end if
127+
end
128+
129+
dim out(20) # endpoint data out
130+
dim out_len
131+
132+
event attributes_value(connection, reason, handle, offset, value_len, value_data)
133+
134+
if handle = xgatt_led then
135+
out(0:value_len) = value_data(0:value_len)
136+
out_len = value_len
137+
call system_endpoint_set_watermarks(system_endpoint_uart0, $ff, out_len) # set TX watermark
138+
end if
139+
140+
end
141+
142+
dim tx_len
143+
144+
event system_endpoint_watermark_tx(curr_endpoint, size)
145+
146+
if curr_endpoint = system_endpoint_uart0 then
147+
#out_len = size #################
148+
call system_endpoint_set_watermarks(system_endpoint_uart0, $ff, 0) # disable TX watermark
149+
call system_endpoint_tx(system_endpoint_uart0, out_len, out(0:out_len))
150+
call attributes_user_write_response(0, 0)
151+
end if
152+
153+
end
154+
155+
#event system_endpoint_watermark_tx(endpoint,size)
156+
157+
# tx_len = size
158+
159+
# if endpoint = system_endpoint_uart0 then
160+
161+
# call system_endpoint_set_watermarks(endpoint, $ff, 0) # disable TX watermark
162+
# call system_endpoint_tx(system_endpoint_uart0, tx_len, "Hello\n")
163+
164+
# end if
165+
#end
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
xgatt_serial_number 14
2+
ota_control 27
3+
ota_data 29
4+
ota_data_w_response 31
5+
ota_control_wo_response 33
6+
device_reset 36
7+
xgatt_debug 40
8+
xgatt_write 45
9+
xgatt_who 48
10+
xgatt_pwr1 52
11+
xgatt_pwr2 56
12+
xgatt_ratediv 60
13+
xgatt_sensor 64
14+
xgatt_led 69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<config>
3+
<!-- <user_data size="0x20000" /> -->
4+
<!-- Increase script timeout from default to allow flash erase in loop -->
5+
<!--<script_timeout value="10000" />-->
6+
<connections value="2"/>
7+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<configuration>
3+
4+
<service uuid="1800">
5+
<description>Generic Access Profile</description>
6+
7+
<characteristic uuid="2a00">
8+
<properties read="true" const="true" />
9+
<value>Sponsored by Duleepa</value>
10+
</characteristic>
11+
12+
<characteristic uuid="2a01">
13+
<properties read="true" const="true" />
14+
<value type="hex">0000</value>
15+
</characteristic>
16+
</service>
17+
18+
<service type="primary" uuid="180A" id="manufacturer">
19+
<description>Device Information</description>
20+
21+
<characteristic uuid="2A29">
22+
<description>Manufacturer Name String</description>
23+
<properties read="true" const="true" />
24+
<value>Robot Pajamas</value>
25+
</characteristic>
26+
27+
<characteristic uuid="2A24">
28+
<description>Model Number String</description>
29+
<properties read="true" const="true" />
30+
<value>RPJ-1</value>
31+
</characteristic>
32+
33+
<characteristic uuid="2A25" id="xgatt_serial_number">
34+
<description>Serial Number String</description>
35+
<properties read="true" />
36+
<value length="17" />
37+
</characteristic>
38+
39+
<characteristic uuid="2A27">
40+
<description>Hardware Revision String</description>
41+
<properties read="true" const="true" />
42+
<value>RPJ-1</value>
43+
</characteristic>
44+
45+
<characteristic uuid="2A26">
46+
<description>Firmware Revision String</description>
47+
<properties read="true" const="true" />
48+
<value>0.1.0</value>
49+
</characteristic>
50+
51+
<characteristic uuid="2A28">
52+
<description>Software Revision String</description>
53+
<properties read="true" const="true" />
54+
<value>0.1.0</value>
55+
</characteristic>
56+
</service>
57+
58+
59+
<service uuid="1d14d6ee-fd63-4fa1-bfa4-8f47b42119f0">
60+
<description>OTA Service</description>
61+
62+
<!-- Standard OTA endpoints for BLEGUI to work -->
63+
<characteristic uuid="f7bf3564-fb6d-4e53-88a4-5e37e0326063" id="ota_control">
64+
<properties write="true" />
65+
<value length="1" type="user" />
66+
</characteristic>
67+
<characteristic uuid="984227f3-34fc-4045-a5d0-2c581f81a153" id="ota_data">
68+
<properties write_no_response="true" />
69+
<value length="20" />
70+
</characteristic>
71+
72+
<!-- More robust OTA endpoints for Android and iOS (BLEGUI won't work with these) -->
73+
<characteristic uuid="00737572-6573-686a-6f73-68692e636f6d" id="ota_data_w_response">
74+
<properties write="true" />
75+
<value length="20" />
76+
</characteristic>
77+
<characteristic uuid="01737572-6573-686a-6f73-68692e636f6d" id="ota_control_wo_response">
78+
<properties write_no_response="true" />
79+
<value length="1" type="user" />
80+
</characteristic>
81+
82+
</service>
83+
84+
<service uuid="00766963-6172-6173-6f6c-7574696f6e73">
85+
<description>Device Control</description>
86+
<characteristic uuid="01766963-6172-6173-6f6c-7574696f6e73" id="device_reset">
87+
<description>Device reset</description>
88+
<properties write_no_response="true" />
89+
<value length="1" />
90+
</characteristic>
91+
</service>
92+
93+
<service uuid="DEADBEEF-CDCD-CDCD-CDCD-CDCDCDCDCDCD">
94+
<characteristic uuid="DEADBEEF-0000-0000-0000-000000000000" id="xgatt_debug">
95+
<description>Debugging output</description>
96+
<properties read="true" notify="true" />
97+
<value length="20" />
98+
</characteristic>
99+
</service>
100+
101+
<service uuid="aaa51666-e7cb-469b-8e4d-2742f1ba7aaa" advertise="true">
102+
<description>Write Buffer</description>
103+
104+
<characteristic uuid="bbbdd780-b042-4876-aae1-112855353bbb" id="xgatt_write">
105+
<description>Written Data</description>
106+
<properties write="true" />
107+
<value variable_length="true" length="20" />
108+
</characteristic>
109+
110+
<characteristic uuid="0dddd780-b042-4876-aae1-112855353ddd" id="xgatt_who">
111+
<description>Who Am I</description>
112+
<properties read="true" notify="true" />
113+
<value length="1" />
114+
</characteristic>
115+
116+
<characteristic uuid="1dddd780-b042-4876-aae1-112855353ddd" id="xgatt_pwr1">
117+
<description>Power Config 1</description>
118+
<properties read="true" notify="true" />
119+
<value length="1" />
120+
</characteristic>
121+
122+
<characteristic uuid="2dddd780-b042-4876-aae1-112855353ddd" id="xgatt_pwr2">
123+
<description>Power Config 2</description>
124+
<properties read="true" notify="true" />
125+
<value length="1" />
126+
</characteristic>
127+
128+
<characteristic uuid="adddd780-b042-4876-aae1-112855353ddd" id="xgatt_ratediv">
129+
<description>RateDivider</description>
130+
<properties read="true" notify="true" />
131+
<value length="1" />
132+
</characteristic>
133+
134+
<characteristic uuid="acce1000-0000-0000-0000-1234567890ab" id="xgatt_sensor">
135+
<description>Sensor Raw</description>
136+
<properties read="true" notify="true" />
137+
<value length="12" />
138+
</characteristic>
139+
</service>
140+
141+
<service uuid="384abbc5-9ad6-4eaa-86af-1ee629ba9838" advertise="true">
142+
<description>LED Control Service</description>
143+
<characteristic uuid="ee7c328f-6d47-4935-96ae-7ab28942074c" id="xgatt_led">
144+
<description>LED Toggle</description>
145+
<properties read="true" write="true" />
146+
<value length="1" type="hex">01</value>
147+
</characteristic>
148+
</service>
149+
150+
</configuration>
151+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<hardware>
4+
<sleeposc enable="true" ppm="50" />
5+
<usart channel="0" alternate="1" baud="9600" endpoint="none" mode="uart" flow="false" />
6+
<sleep enable="false" />
7+
<usb enable="false" endpoint="api" />
8+
<txpower power="15" bias="5" />
9+
<script enable="true" />
10+
<otaboot source="internal" />
11+
</hardware>

0 commit comments

Comments
 (0)