diff --git a/CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_i2c.c b/CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_i2c.c index 48d3355..f9aaf8c 100644 --- a/CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_i2c.c +++ b/CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_i2c.c @@ -81,7 +81,7 @@ void I2C_Init(I2C_ModeTypeDef I2C_Mode, UINT32 I2C_ClockSpeed, I2C_DutyTypeDef I R16_I2C_CTRL1 &= ~(RB_I2C_SMBUS | RB_I2C_SMBTYPE | RB_I2C_ACK); R16_I2C_CTRL1 |= I2C_Mode | I2C_Ack; - R16_I2C_OADDR1 &= ~0xFFFF; + R16_I2C_OADDR1 &= ~0xFFFFU; R16_I2C_OADDR1 |= I2C_AckAddr | I2C_OwnAddress1; } diff --git a/Makefile b/Makefile index a4e6916..2601737 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,6 @@ CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usb2dev.c \ CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_spi1.c \ CH5xx_ble_firmware_library/RVMSIS/core_riscv.c \ src/main.c \ -src/debug.c \ src/leddrv.c \ src/button.c \ src/bmlist.c \ @@ -74,7 +73,6 @@ src/data.c \ src/usb/utils.c \ src/usb/setup.c \ src/usb/ctrl.c \ -src/usb/debug.c \ src/usb/dev.c \ src/usb/composite/hiddev.c \ src/usb/composite/cdc-serial.c \ @@ -84,6 +82,11 @@ src/animation.c \ src/font.c \ src/power.c \ +ifdef DEBUG +C_SOURCES += \ +src/debug.c \ +src/usb/debug.c +endif # ASM sources ASM_SOURCES = \ diff --git a/src/ble/common.h b/src/ble/common.h index 6c03121..e5354ad 100644 --- a/src/ble/common.h +++ b/src/ble/common.h @@ -10,7 +10,7 @@ #include typedef struct { - uint8_t *bytes; + const uint8_t *bytes; int size; } byte_t; diff --git a/src/bmlist.c b/src/bmlist.c index 420386e..14d8d25 100644 --- a/src/bmlist.c +++ b/src/bmlist.c @@ -1,7 +1,7 @@ #include "bmlist.h" #include -volatile static bm_t *current, *head, *tail; +static bm_t *current, *head, *tail; static void bm_add(bm_t *new, bm_t *prev, bm_t *next) { @@ -63,13 +63,14 @@ static void list_del(bm_t *prev, bm_t *next) bm_t *bmlist_drop(bm_t *bm) { + bm_t *next = bm->next; list_del(bm->prev, bm->next); if (bm == head) head = bm->next; if (bm == tail) tail = bm->prev; free(bm); - return bm->next; + return next; } bm_t *bm_new(uint16_t width) diff --git a/src/config.c b/src/config.c index ac91b85..af47422 100644 --- a/src/config.c +++ b/src/config.c @@ -4,7 +4,6 @@ */ #include "config.h" #include "resource.h" -#include "res/foss-asia-2.xbm" #include "debug.h" #include "util/crc.h" diff --git a/src/data.c b/src/data.c index 1261c90..e8e1f92 100644 --- a/src/data.c +++ b/src/data.c @@ -7,11 +7,16 @@ #include "data.h" #include "leddrv.h" -uint32_t bigendian16_sum(uint16_t *s, int len) +uint32_t bigendian16_sum(const uint8_t *s, int len) { uint32_t sum = 0; while (len-- > 0) { - sum += bswap16(s[len]); + // Read the big-endian bytes for this uint16_t + uint8_t high = s[len * 2]; // MSB (most significant byte) + uint8_t low = s[len * 2 + 1]; // LSB (least significant byte) + // Reconstruct the uint16_t value (big-endian to little-endian equivalent) + uint16_t val = (high << 8) | low; + sum += val; } return sum; } @@ -42,7 +47,7 @@ uint16_t data_flash2newmem(uint8_t **chunk, uint32_t n) return 0; uint16_t offs = LEGACY_HEADER_SIZE - + bigendian16_sum(header.sizes, n) * LED_ROWS; + + bigendian16_sum((const uint8_t*)header.sizes, n) * LED_ROWS; *chunk = malloc(size); EEPROM_READ(offs, *chunk, size); diff --git a/src/data.h b/src/data.h index ef4050c..b57f364 100644 --- a/src/data.h +++ b/src/data.h @@ -42,7 +42,7 @@ enum ANIMATION_MODES { static inline uint16_t bswap16(uint16_t i) { return (i >> 8) | (i << 8); } -uint32_t bigendian16_sum(uint16_t *s, int len); +uint32_t bigendian16_sum(const uint8_t *s, int len); uint32_t data_flatSave(uint8_t *data, uint32_t len); uint16_t data_flash2newmem(uint8_t **chunk, uint32_t n); diff --git a/src/legacyctrl.c b/src/legacyctrl.c index 582d6e4..4bb22c8 100644 --- a/src/legacyctrl.c +++ b/src/legacyctrl.c @@ -50,7 +50,7 @@ int legacy_ble_rx(uint8_t *val, uint16_t len) if (c == 1) { data_legacy_t *d = (data_legacy_t *)data; - n = bigendian16_sum(d->sizes, 8); + n = bigendian16_sum((uint8_t*)d->sizes, 8); data_len = LEGACY_HEADER_SIZE + LED_ROWS * n; PRINT("Data len: %d\n", data_len); data = realloc(data, data_len); @@ -96,7 +96,7 @@ int legacy_usb_rx(uint8_t *buf, uint16_t len) if (!data_len) { data_legacy_t *d = (data_legacy_t *)data; - uint16_t n = bigendian16_sum(d->sizes, 8); + uint16_t n = bigendian16_sum((uint8_t*)d->sizes, 8); data_len = LEGACY_HEADER_SIZE + LED_ROWS * n; data = realloc(data, data_len); } diff --git a/src/main.c b/src/main.c index 10a62e0..377e14e 100644 --- a/src/main.c +++ b/src/main.c @@ -49,7 +49,7 @@ enum MODES { static tmosTaskID common_taskid = INVALID_TASK_ID ; -volatile uint16_t fb[LED_COLS] = {0}; +uint16_t fb[LED_COLS] = {0}; volatile int mode, is_play_sequentially = 1; __HIGH_CODE @@ -435,7 +435,7 @@ int main() disp_charging(); cfg_init(); xbm_t spl = { - .bits = &(badge_cfg.splash_bm_bits), + .bits = badge_cfg.splash_bm_bits, .w = badge_cfg.splash_bm_w, .h = badge_cfg.splash_bm_h, .fh = badge_cfg.splash_bm_fh, diff --git a/src/ngctrl.c b/src/ngctrl.c index 5e78cbb..f2226fd 100644 --- a/src/ngctrl.c +++ b/src/ngctrl.c @@ -125,7 +125,7 @@ uint8_t load_fallback_cfg(uint8_t *val, uint16_t len) { PRINT(__func__); PRINT("\n"); - cfg_fallback(&badge_cfg); + cfg_fallback(); return 0; } diff --git a/src/power.c b/src/power.c index da6ec0f..1e6b4f9 100644 --- a/src/power.c +++ b/src/power.c @@ -27,8 +27,7 @@ void power_init() GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeIN_Floating); ADC_ExtSingleChSampInit(SampleFreq_3_2, ADC_PGA_0); - int16_t adc_calib = ADC_DataCalib_Rough(); - PRINT("RoughCalib_Value = %d \n", adc_calib); + PRINT("RoughCalib_Value = %d \n", ADC_DataCalib_Rough()); ADC_ChannelCfg(1); } @@ -38,7 +37,6 @@ int batt_raw() int ret = 0; PRINT("ADC reading: \n"); - uint16_t buf[20]; for(int i = 0; i < 20; i++) { uint16_t adc = ADC_ExcutSingleConver(); ret += adc; diff --git a/src/res/foss-asia-2.xbm b/src/res/foss-asia-2.xbm index 5a75f5d..cf9623a 100644 --- a/src/res/foss-asia-2.xbm +++ b/src/res/foss-asia-2.xbm @@ -1,6 +1,6 @@ #define foss_asia_2_width 44 #define foss_asia_2_height 22 -static unsigned char foss_asia_2_bits[] = { +unsigned const char foss_asia_2_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xf9, 0x8f, 0xff, 0xfc, 0x07, 0xfe, 0xfd, 0xdf, 0xff, 0xfe, 0x07, 0x06, 0x0c, 0xd8, 0x00, 0x06, 0x00, 0xff, 0x0d, 0xd8, 0x7f, 0xfe, 0x03, 0xff, 0x0d, 0x98, 0xff, 0xfc, 0x07, diff --git a/src/usb/composite/cdc-serial.c b/src/usb/composite/cdc-serial.c index 99f0c40..1295e61 100644 --- a/src/usb/composite/cdc-serial.c +++ b/src/usb/composite/cdc-serial.c @@ -13,8 +13,6 @@ #define USB_DESCTYPE_CS_INTERFACE 0x24 static __attribute__((aligned(4))) uint8_t noti_ep_buf[64 + 64]; -static uint8_t *const noti_ep_out = noti_ep_buf; -static uint8_t *const noti_ep_in = noti_ep_buf + 64; static __attribute__((aligned(4))) uint8_t data_ep_buf[64 + 64]; static uint8_t *const data_ep_out = data_ep_buf; diff --git a/src/usb/ctrl.c b/src/usb/ctrl.c index 30aa7dd..b506690 100644 --- a/src/usb/ctrl.c +++ b/src/usb/ctrl.c @@ -19,7 +19,7 @@ void usb_set_address(uint8_t ad) address = ad; } -void ctrl_start_load_block(void *buf, uint16_t len) +void ctrl_start_load_block(const void *buf, uint16_t len) { usb_start_load_block(ep0buf, buf, len, 1); } @@ -53,7 +53,9 @@ static void ep_handler() switch(token) { case UIS_TOKEN_SETUP: - print_setuppk(req); + #ifdef DEBUG + print_setuppk(request); + #endif if (recip == USB_REQ_RECIP_DEVICE) { handle_devreq(request); @@ -128,12 +130,16 @@ __HIGH_CODE void USB_IRQHandler(void) { uint8_t intflag = R8_USB_INT_FG; clear_handshake_sent_flag(); + #ifdef DEBUG PRINT("\nusb: new interrupt\n"); print_intflag_reg(); + #endif if (intflag & RB_UIF_TRANSFER) { + #ifdef DEBUG PRINT("usb: RX Length reg: %d\n", R8_USB_RX_LEN); print_status_reg(); + #endif route_enpoints(); } diff --git a/src/usb/dev.c b/src/usb/dev.c index 569dd25..cfca8fd 100644 --- a/src/usb/dev.c +++ b/src/usb/dev.c @@ -28,7 +28,7 @@ USB_DEV_DESCR dev_desc = { }; /* String Descriptor Zero, Specifying Languages Supported by the Device */ -static uint8_t lang_desc[] = { +static uint16_t lang_desc[] = { 0x04, /* bLength */ 0x03, /* bDescriptorType */ 0x09, 0x04 /* wLANGID - en-US */ @@ -91,7 +91,7 @@ static void desc_config(USB_SETUP_REQ *request) static void desc_string(USB_SETUP_REQ *request) { - uint8_t *string_index[32] = { + uint16_t *string_index[32] = { lang_desc, vendor_info, product_info, diff --git a/src/usb/usb.h b/src/usb/usb.h index f1d41f9..f89966c 100644 --- a/src/usb/usb.h +++ b/src/usb/usb.h @@ -5,11 +5,11 @@ void cdc_fill_IN(uint8_t *buf, uint8_t len); int cdc_tx_poll(uint8_t *buf, int len, uint16_t timeout_ms); -void cdc_onWrite(void (*cb)(uint8_t *buf, uint16_t len)); +void cdc_onWrite(int (*cb)(uint8_t *buf, uint16_t len)); void hiddev_fill_IN(uint8_t *buf, uint8_t len); int hiddev_tx_poll(uint8_t *buf, int len, uint16_t timeout_ms); -void hiddev_onWrite(void (*cb)(uint8_t *buf, uint16_t len)); +void hiddev_onWrite(int (*cb)(uint8_t *buf, uint16_t len)); void usb_start(); diff --git a/src/usb/utils.c b/src/usb/utils.c index 5c6a83e..9ca2cbc 100644 --- a/src/usb/utils.c +++ b/src/usb/utils.c @@ -35,9 +35,8 @@ static void __handshake(uint8_t ep_num, int dir, int type, int tog, uint8_t len) if (ep_num > 7) return; - char *type_mean[] = { "ACK", "NO_RESP", "NAK", "STALL"}; PRINT("Loaded %d byte, DATA%d with an %s to EP%dIN.\n", - len, tog != 0, type_mean[type], ep_num); + len, tog != 0, ((char *[]){ "ACK", "NO_RESP", "NAK", "STALL"})[type], ep_num); uint8_t ctrl = type << (dir ? 0 : 2); ctrl |= (tog) ? RB_UEP_T_TOG : RB_UEP_R_TOG; @@ -69,11 +68,11 @@ int handshake_sent() return res_sent; } -static uint8_t *p_buf; +static const uint8_t *p_buf; static uint16_t remain_len, req_len, _tog; static uint16_t -load_chunk(void *ep_buf, void *block, uint16_t block_len, uint16_t req_len, +load_chunk(void *ep_buf, const void *block, uint16_t block_len, uint16_t req_len, int tog) { _TRACE(); @@ -119,7 +118,7 @@ int usb_load_next_chunk() } uint16_t -usb_start_load_block(void *ep_IN_buf, void *buf, uint16_t len, int tog) +usb_start_load_block(void *ep_IN_buf, const void *buf, uint16_t len, int tog) { _TRACE(); req_len = len; diff --git a/src/usb/utils.h b/src/usb/utils.h index 595a8b7..921d354 100644 --- a/src/usb/utils.h +++ b/src/usb/utils.h @@ -16,8 +16,8 @@ int if_cb_register(uint8_t if_num, void (*cb)(USB_SETUP_REQ *request)); void dma_register(uint8_t endpoint_number, void *buf_addr); uint16_t -usb_start_load_block(void *ep_IN_buf, void *buf, uint16_t len, int tog); -void ctrl_start_load_block(void *buf, uint16_t len); +usb_start_load_block(void *ep_IN_buf, const void *buf, uint16_t len, int tog); +void ctrl_start_load_block(const void *buf, uint16_t len); int usb_load_next_chunk(); void usb_flush(); diff --git a/src/xbm.h b/src/xbm.h index 7bf9530..652cb64 100644 --- a/src/xbm.h +++ b/src/xbm.h @@ -7,7 +7,7 @@ #define ALIGN_8BIT(x) (ALIGN_1BYTE(x) * 8) typedef struct { - uint8_t *bits; + const uint8_t *bits; int w; // Width int h; // Height int fh; // Frame height