Skip to content

Commit 0fc9f2f

Browse files
committed
shrink size by not using sprintf()
1 parent 3791f93 commit 0fc9f2f

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

src/usb/uf2/ghostfat.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "flash_nrf5x.h"
3232
#include <string.h>
3333
#include <stdio.h>
34+
#include <stdlib.h>
3435

3536
#include "bootloader_settings.h"
3637
#include "bootloader.h"
@@ -119,7 +120,8 @@ STATIC_ASSERT(FAT_ENTRIES_PER_SECTOR == 256); // FAT
119120
char infoUf2File[128*3] =
120121
"UF2 Bootloader " UF2_VERSION "\r\n"
121122
"Model: " UF2_PRODUCT_NAME "\r\n"
122-
"Board-ID: " UF2_BOARD_ID "\r\n";
123+
"Board-ID: " UF2_BOARD_ID "\r\n"
124+
"Date: " __DATE__ "\r\n";
123125

124126
const char indexFile[] =
125127
"<!doctype html>\n"
@@ -250,17 +252,33 @@ void uf2_init(void)
250252
uint32_t const sd_id = SD_ID_GET(MBR_SIZE);
251253
uint32_t const sd_version = SD_VERSION_GET(MBR_SIZE);
252254

253-
uint32_t const ver1 = sd_version / 1000000;
254-
uint32_t const ver2 = (sd_version % 1000000)/1000;
255-
uint32_t const ver3 = sd_version % 1000;
255+
uint32_t ver[3];
256+
ver[0] = sd_version / 1000000;
257+
ver[1] = (sd_version - ver[0]*1000000)/1000;
258+
ver[2] = (sd_version - ver[0]*1000000 - ver[1]*1000);
256259

257-
sprintf(infoUf2File + strlen(infoUf2File), "S%lu version %lu.%lu.%lu\r\n", sd_id, ver1, ver2, ver3);
260+
char str[10];
261+
utoa(sd_id, str, 10);
262+
263+
strcat(infoUf2File, "S");
264+
strcat(infoUf2File, str);
265+
strcat(infoUf2File, " ");
266+
267+
utoa(ver[0], str, 10);
268+
strcat(infoUf2File, str);
269+
strcat(infoUf2File, ".");
270+
271+
utoa(ver[1], str, 10);
272+
strcat(infoUf2File, str);
273+
strcat(infoUf2File, ".");
274+
275+
utoa(ver[2], str, 10);
276+
strcat(infoUf2File, str);
277+
strcat(infoUf2File, "\r\n");
258278
}else
259279
{
260280
strcat(infoUf2File, "not found\r\n");
261281
}
262-
263-
strcat(infoUf2File, "Date: " __DATE__ "\r\n");
264282
}
265283

266284
/*------------------------------------------------------------------*/

src/usb/usb_desc.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
#include "usb_desc.h"
2626

2727
enum {
28-
ITF_STR_LANGUAGE = 0 ,
29-
ITF_STR_MANUFACTURER ,
30-
ITF_STR_PRODUCT ,
31-
ITF_STR_SERIAL ,
32-
ITF_STR_CDC ,
33-
ITF_STR_MSC
28+
STRID_LANGUAGE = 0 ,
29+
STRID_MANUFACTURER ,
30+
STRID_PRODUCT ,
31+
STRID_SERIAL ,
32+
STRID_CDC ,
33+
STRID_MSC
3434
};
3535

3636
// CDC + MSC or CDC only mode
@@ -90,10 +90,10 @@ uint8_t const desc_configuration_cdc_msc[] =
9090
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN, 0, 100),
9191

9292
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
93-
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, ITF_STR_CDC, 0x81, 8, 0x02, 0x82, 64),
93+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STRID_CDC, 0x81, 8, 0x02, 0x82, 64),
9494

9595
// Interface number, string index, EP Out & EP In address, EP size
96-
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, ITF_STR_MSC, 0x03, 0x83, 64),
96+
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, STRID_MSC, 0x03, 0x83, 64),
9797
};
9898

9999
uint8_t const desc_configuration_cdc_only[] =
@@ -102,7 +102,7 @@ uint8_t const desc_configuration_cdc_only[] =
102102
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL-1, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN, 0, 100),
103103

104104
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
105-
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, ITF_STR_CDC, 0x81, 8, 0x02, 0x82, 64),
105+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STRID_CDC, 0x81, 8, 0x02, 0x82, 64),
106106
};
107107

108108

@@ -127,7 +127,19 @@ void usb_desc_init(bool cdc_only)
127127
}
128128

129129
// Create Serial string descriptor
130-
sprintf(desc_str_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]);
130+
uint8_t const* device_id = (uint8_t const*) &NRF_FICR->DEVICEID;
131+
132+
for ( uint8_t i = 0; i < 8; i++ )
133+
{
134+
for ( uint8_t j = 0; j < 2; j++ )
135+
{
136+
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
137+
138+
uint8_t nibble = (device_id[i] >> (j * 4)) & 0xf;
139+
desc_str_serial[15 - (i * 2 + j)] = nibble_to_hex[nibble]; // memory is little endian
140+
}
141+
}
142+
desc_str_serial[16] = 0;
131143
}
132144

133145
//--------------------------------------------------------------------+
@@ -156,7 +168,7 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
156168

157169
uint8_t chr_count;
158170

159-
if ( index == 0)
171+
if ( index == STRID_LANGUAGE )
160172
{
161173
memcpy(&_desc_str[1], string_desc_arr[0], 2);
162174
chr_count = 1;

0 commit comments

Comments
 (0)