|
| 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 |
0 commit comments