Clean up comments, USB flash, NULLs

This commit is contained in:
Scott Lahteine 2020-10-24 17:13:10 -05:00
parent 00fbe50bbe
commit ec23e37a4a
45 changed files with 231 additions and 238 deletions

View File

@ -84,7 +84,7 @@ Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) {
card.getSd2Card().readData(sector_buf); card.getSd2Card().readData(sector_buf);
// RAM -> USB // RAM -> USB
if (!udi_msc_trans_block(true, sector_buf, SD_MMC_BLOCK_SIZE, NULL)) { if (!udi_msc_trans_block(true, sector_buf, SD_MMC_BLOCK_SIZE, nullptr)) {
card.getSd2Card().readStop(); card.getSd2Card().readStop();
return CTRL_FAIL; return CTRL_FAIL;
} }
@ -120,7 +120,7 @@ Ctrl_status sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector) {
while (nb_sector--) { while (nb_sector--) {
// USB -> RAM // USB -> RAM
if (!udi_msc_trans_block(false, sector_buf, SD_MMC_BLOCK_SIZE, NULL)) { if (!udi_msc_trans_block(false, sector_buf, SD_MMC_BLOCK_SIZE, nullptr)) {
card.getSd2Card().writeStop(); card.getSd2Card().writeStop();
return CTRL_FAIL; return CTRL_FAIL;
} }

View File

@ -184,7 +184,7 @@ int i2s_init() {
// Allocate the array of pointers to the buffers // Allocate the array of pointers to the buffers
dma.buffers = (uint32_t **)malloc(sizeof(uint32_t*) * DMA_BUF_COUNT); dma.buffers = (uint32_t **)malloc(sizeof(uint32_t*) * DMA_BUF_COUNT);
if (dma.buffers == nullptr) return -1; if (!dma.buffers) return -1;
// Allocate each buffer that can be used by the DMA controller // Allocate each buffer that can be used by the DMA controller
for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) { for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) {
@ -194,7 +194,7 @@ int i2s_init() {
// Allocate the array of DMA descriptors // Allocate the array of DMA descriptors
dma.desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * DMA_BUF_COUNT); dma.desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * DMA_BUF_COUNT);
if (dma.desc == nullptr) return -1; if (!dma.desc) return -1;
// Allocate each DMA descriptor that will be used by the DMA controller // Allocate each DMA descriptor that will be used by the DMA controller
for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) { for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) {

View File

@ -40,7 +40,7 @@ size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
bool PersistentStore::access_start() { bool PersistentStore::access_start() {
const char eeprom_erase_value = 0xFF; const char eeprom_erase_value = 0xFF;
FILE * eeprom_file = fopen(filename, "rb"); FILE * eeprom_file = fopen(filename, "rb");
if (eeprom_file == nullptr) return false; if (!eeprom_file) return false;
fseek(eeprom_file, 0L, SEEK_END); fseek(eeprom_file, 0L, SEEK_END);
std::size_t file_size = ftell(eeprom_file); std::size_t file_size = ftell(eeprom_file);
@ -59,7 +59,7 @@ bool PersistentStore::access_start() {
bool PersistentStore::access_finish() { bool PersistentStore::access_finish() {
FILE * eeprom_file = fopen(filename, "wb"); FILE * eeprom_file = fopen(filename, "wb");
if (eeprom_file == nullptr) return false; if (!eeprom_file) return false;
fwrite(buffer, sizeof(uint8_t), sizeof(buffer), eeprom_file); fwrite(buffer, sizeof(uint8_t), sizeof(buffer), eeprom_file);
fclose(eeprom_file); fclose(eeprom_file);
return true; return true;

View File

@ -86,10 +86,10 @@ public:
GpioEvent::Type evt_type = value > 1 ? GpioEvent::SET_VALUE : value > pin_map[pin].value ? GpioEvent::RISE : value < pin_map[pin].value ? GpioEvent::FALL : GpioEvent::NOP; GpioEvent::Type evt_type = value > 1 ? GpioEvent::SET_VALUE : value > pin_map[pin].value ? GpioEvent::RISE : value < pin_map[pin].value ? GpioEvent::FALL : GpioEvent::NOP;
pin_map[pin].value = value; pin_map[pin].value = value;
GpioEvent evt(Clock::nanos(), pin, evt_type); GpioEvent evt(Clock::nanos(), pin, evt_type);
if (pin_map[pin].cb != nullptr) { if (pin_map[pin].cb) {
pin_map[pin].cb->interrupt(evt); pin_map[pin].cb->interrupt(evt);
} }
if (Gpio::logger != nullptr) Gpio::logger->log(evt); if (Gpio::logger) Gpio::logger->log(evt);
} }
static uint16_t get(pin_type pin) { static uint16_t get(pin_type pin) {
@ -105,8 +105,8 @@ public:
if (!valid_pin(pin)) return; if (!valid_pin(pin)) return;
pin_map[pin].mode = value; pin_map[pin].mode = value;
GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETM); GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETM);
if (pin_map[pin].cb != nullptr) pin_map[pin].cb->interrupt(evt); if (pin_map[pin].cb) pin_map[pin].cb->interrupt(evt);
if (Gpio::logger != nullptr) Gpio::logger->log(evt); if (Gpio::logger) Gpio::logger->log(evt);
} }
static uint8_t getMode(pin_type pin) { static uint8_t getMode(pin_type pin) {
@ -118,8 +118,8 @@ public:
if (!valid_pin(pin)) return; if (!valid_pin(pin)) return;
pin_map[pin].dir = value; pin_map[pin].dir = value;
GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETD); GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETD);
if (pin_map[pin].cb != nullptr) pin_map[pin].cb->interrupt(evt); if (pin_map[pin].cb) pin_map[pin].cb->interrupt(evt);
if (Gpio::logger != nullptr) Gpio::logger->log(evt); if (Gpio::logger) Gpio::logger->log(evt);
} }
static uint8_t getDir(pin_type pin) { static uint8_t getDir(pin_type pin) {

View File

@ -300,7 +300,7 @@ uint16_t HAL_adc_result;
DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
DMA_STEPSEL_SRC // STEPSEL DMA_STEPSEL_SRC // STEPSEL
); );
if (descriptor != nullptr) if (descriptor)
descriptor->BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_BEAT; descriptor->BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_BEAT;
adc0DMAProgram.startJob(); adc0DMAProgram.startJob();
} }
@ -337,7 +337,7 @@ uint16_t HAL_adc_result;
DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE DMA_ADDRESS_INCREMENT_STEP_SIZE_1, // STEPSIZE
DMA_STEPSEL_SRC // STEPSEL DMA_STEPSEL_SRC // STEPSEL
); );
if (descriptor != nullptr) if (descriptor)
descriptor->BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_BEAT; descriptor->BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_BEAT;
adc1DMAProgram.startJob(); adc1DMAProgram.startJob();
} }

View File

@ -35,10 +35,10 @@ uint8_t QSPIFlash::_buf[SFLASH_SECTOR_SIZE];
uint32_t QSPIFlash::_addr = INVALID_ADDR; uint32_t QSPIFlash::_addr = INVALID_ADDR;
void QSPIFlash::begin() { void QSPIFlash::begin() {
if (_flashBase != nullptr) return; if (_flashBase) return;
_flashBase = new Adafruit_SPIFlashBase(new Adafruit_FlashTransport_QSPI()); _flashBase = new Adafruit_SPIFlashBase(new Adafruit_FlashTransport_QSPI());
_flashBase->begin(NULL); _flashBase->begin(nullptr);
} }
size_t QSPIFlash::size() { size_t QSPIFlash::size() {

View File

@ -99,7 +99,7 @@ void XPT2046::Init() {
#endif #endif
} }
else { else {
SPIx.Instance = NULL; SPIx.Instance = nullptr;
SET_INPUT(TOUCH_MISO_PIN); SET_INPUT(TOUCH_MISO_PIN);
SET_OUTPUT(TOUCH_MOSI_PIN); SET_OUTPUT(TOUCH_MOSI_PIN);
SET_OUTPUT(TOUCH_SCK_PIN); SET_OUTPUT(TOUCH_SCK_PIN);

View File

@ -109,7 +109,7 @@
// Private Variables // Private Variables
// ------------------------ // ------------------------
HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { NULL }; HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
// ------------------------ // ------------------------
// Public functions // Public functions

View File

@ -124,7 +124,7 @@ void HAL_idletask();
#endif #endif
#ifndef digitalPinHasPWM #ifndef digitalPinHasPWM
#define digitalPinHasPWM(P) (PIN_MAP[P].timer_device != nullptr) #define digitalPinHasPWM(P) !!PIN_MAP[P].timer_device
#define NO_COMPILE_TIME_PWM #define NO_COMPILE_TIME_PWM
#endif #endif

View File

@ -656,7 +656,7 @@ static const spi_pins* dev_to_spi_pins(spi_dev *dev) {
#if BOARD_NR_SPI >= 3 #if BOARD_NR_SPI >= 3
case RCC_SPI3: return board_spi_pins + 2; case RCC_SPI3: return board_spi_pins + 2;
#endif #endif
default: return NULL; default: return nullptr;
} }
} }

View File

@ -51,7 +51,7 @@
#define IS_INPUT(IO) (_GET_MODE(IO) == GPIO_INPUT_FLOATING || _GET_MODE(IO) == GPIO_INPUT_ANALOG || _GET_MODE(IO) == GPIO_INPUT_PU || _GET_MODE(IO) == GPIO_INPUT_PD) #define IS_INPUT(IO) (_GET_MODE(IO) == GPIO_INPUT_FLOATING || _GET_MODE(IO) == GPIO_INPUT_ANALOG || _GET_MODE(IO) == GPIO_INPUT_PU || _GET_MODE(IO) == GPIO_INPUT_PD)
#define IS_OUTPUT(IO) (_GET_MODE(IO) == GPIO_OUTPUT_PP || _GET_MODE(IO) == GPIO_OUTPUT_OD) #define IS_OUTPUT(IO) (_GET_MODE(IO) == GPIO_OUTPUT_PP || _GET_MODE(IO) == GPIO_OUTPUT_OD)
#define PWM_PIN(IO) (PIN_MAP[IO].timer_device != nullptr) #define PWM_PIN(IO) !!PIN_MAP[IO].timer_device
// digitalRead/Write wrappers // digitalRead/Write wrappers
#define extDigitalRead(IO) digitalRead(IO) #define extDigitalRead(IO) digitalRead(IO)

View File

@ -245,7 +245,7 @@ public:
#endif #endif
// The code value pointer was set // The code value pointer was set
FORCE_INLINE static bool has_value() { return value_ptr != nullptr; } FORCE_INLINE static bool has_value() { return !!value_ptr; }
// Seen a parameter with a value // Seen a parameter with a value
static inline bool seenval(const char c) { return seen(c) && has_value(); } static inline bool seenval(const char c) { return seen(c) && has_value(); }

View File

@ -180,7 +180,7 @@ bool GCodeQueue::enqueue_one(const char* cmd) {
* Return 'true' if any commands were processed. * Return 'true' if any commands were processed.
*/ */
bool GCodeQueue::process_injected_command_P() { bool GCodeQueue::process_injected_command_P() {
if (injected_commands_P == nullptr) return false; if (!injected_commands_P) return false;
char c; char c;
size_t i = 0; size_t i = 0;
@ -480,7 +480,7 @@ void GCodeQueue::get_serial_commands() {
if (npos) { if (npos) {
bool M110 = strstr_P(command, PSTR("M110")) != nullptr; const bool M110 = !!strstr_P(command, PSTR("M110"));
if (M110) { if (M110) {
char* n2pos = strchr(command + 4, 'N'); char* n2pos = strchr(command + 4, 'N');

View File

@ -1036,7 +1036,7 @@ void MarlinUI::draw_status_screen() {
void MenuEditItemBase::draw_edit_screen(PGM_P const pstr, const char* const value/*=nullptr*/) { void MenuEditItemBase::draw_edit_screen(PGM_P const pstr, const char* const value/*=nullptr*/) {
ui.encoder_direction_normal(); ui.encoder_direction_normal();
uint8_t n = lcd_put_u8str_ind_P(0, 1, pstr, itemIndex, itemString, LCD_WIDTH - 1); uint8_t n = lcd_put_u8str_ind_P(0, 1, pstr, itemIndex, itemString, LCD_WIDTH - 1);
if (value != nullptr) { if (value) {
lcd_put_wchar(':'); n--; lcd_put_wchar(':'); n--;
const uint8_t len = utf8_strlen(value) + 1; // Plus one for a leading space const uint8_t len = utf8_strlen(value) + 1; // Plus one for a leading space
const lcd_uint_t valrow = n < len ? 2 : 1; // Value on the next row if it won't fit const lcd_uint_t valrow = n < len ? 2 : 1; // Value on the next row if it won't fit

View File

@ -863,7 +863,7 @@ void MarlinUI::draw_status_screen() {
lcd.setCursor(0, MIDDLE_Y); lcd.setCursor(0, MIDDLE_Y);
lcd.write(COLOR_EDIT); lcd.write(COLOR_EDIT);
lcd_put_u8str_P(pstr); lcd_put_u8str_P(pstr);
if (value != nullptr) { if (value) {
lcd.write(':'); lcd.write(':');
lcd.setCursor((LCD_WIDTH - 1) - (utf8_strlen(value) + 1), MIDDLE_Y); // Right-justified, padded by spaces lcd.setCursor((LCD_WIDTH - 1) - (utf8_strlen(value) + 1), MIDDLE_Y); // Right-justified, padded by spaces
lcd.write(' '); // Overwrite char if value gets shorter lcd.write(' '); // Overwrite char if value gets shorter

View File

@ -423,7 +423,7 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
if (onpage) lcd_put_u8str_ind_P(0, baseline, pstr, itemIndex, itemString); if (onpage) lcd_put_u8str_ind_P(0, baseline, pstr, itemIndex, itemString);
// If a value is included, print a colon, then print the value right-justified // If a value is included, print a colon, then print the value right-justified
if (value != nullptr) { if (value) {
lcd_put_wchar(':'); lcd_put_wchar(':');
if (extra_row) { if (extra_row) {
// Assume that value is numeric (with no descender) // Assume that value is numeric (with no descender)

View File

@ -346,7 +346,7 @@ uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, u
switch (msg) { switch (msg) {
case U8G_DEV_MSG_INIT: case U8G_DEV_MSG_INIT:
dev->com_fn(u8g, U8G_COM_MSG_INIT, U8G_SPI_CLK_CYCLE_NONE, NULL); dev->com_fn(u8g, U8G_COM_MSG_INIT, U8G_SPI_CLK_CYCLE_NONE, nullptr);
tftio.Init(); tftio.Init();
tftio.InitTFT(); tftio.InitTFT();

View File

@ -294,7 +294,7 @@ namespace Anycubic {
} }
void ChironTFT::SendtoTFTLN(PGM_P str = nullptr) { void ChironTFT::SendtoTFTLN(PGM_P str = nullptr) {
if (str != nullptr) { if (str) {
#if ACDEBUG(AC_SOME) #if ACDEBUG(AC_SOME)
SERIAL_ECHOPGM("> "); SERIAL_ECHOPGM("> ");
#endif #endif

View File

@ -218,12 +218,12 @@ void AnycubicTFTClass::OnUserConfirmRequired(const char * const msg) {
} }
float AnycubicTFTClass::CodeValue() { float AnycubicTFTClass::CodeValue() {
return (strtod(&TFTcmdbuffer[TFTbufindr][TFTstrchr_pointer - TFTcmdbuffer[TFTbufindr] + 1], NULL)); return (strtod(&TFTcmdbuffer[TFTbufindr][TFTstrchr_pointer - TFTcmdbuffer[TFTbufindr] + 1], nullptr));
} }
bool AnycubicTFTClass::CodeSeen(char code) { bool AnycubicTFTClass::CodeSeen(char code) {
TFTstrchr_pointer = strchr(TFTcmdbuffer[TFTbufindr], code); TFTstrchr_pointer = strchr(TFTcmdbuffer[TFTbufindr], code);
return (TFTstrchr_pointer != NULL); // Return True if a character was found return !!TFTstrchr_pointer; // Return True if a character was found
} }
bool AnycubicTFTClass::IsNozzleHomed() { bool AnycubicTFTClass::IsNozzleHomed() {
@ -536,7 +536,7 @@ void AnycubicTFTClass::OnPrintTimerStopped() {
} }
void AnycubicTFTClass::GetCommandFromTFT() { void AnycubicTFTClass::GetCommandFromTFT() {
char *starpos = NULL; char *starpos = nullptr;
while (LCD_SERIAL.available() > 0 && TFTbuflen < TFTBUFSIZE) { while (LCD_SERIAL.available() > 0 && TFTbuflen < TFTBUFSIZE) {
serial3_char = LCD_SERIAL.read(); serial3_char = LCD_SERIAL.read();
if (serial3_char == '\n' || if (serial3_char == '\n' ||
@ -549,10 +549,10 @@ void AnycubicTFTClass::GetCommandFromTFT() {
TFTcmdbuffer[TFTbufindw][serial3_count] = 0; // terminate string TFTcmdbuffer[TFTbufindw][serial3_count] = 0; // terminate string
if ((strchr(TFTcmdbuffer[TFTbufindw], 'A') != NULL)) { if ((strchr(TFTcmdbuffer[TFTbufindw], 'A') != nullptr)) {
int16_t a_command; int16_t a_command;
TFTstrchr_pointer = strchr(TFTcmdbuffer[TFTbufindw], 'A'); TFTstrchr_pointer = strchr(TFTcmdbuffer[TFTbufindw], 'A');
a_command = ((int)((strtod(&TFTcmdbuffer[TFTbufindw][TFTstrchr_pointer - TFTcmdbuffer[TFTbufindw] + 1], NULL)))); a_command = ((int)((strtod(&TFTcmdbuffer[TFTbufindw][TFTstrchr_pointer - TFTcmdbuffer[TFTbufindw] + 1], nullptr))));
#if ENABLED(ANYCUBIC_LCD_DEBUG) #if ENABLED(ANYCUBIC_LCD_DEBUG)
if ((a_command > 7) && (a_command != 20)) { // No debugging of status polls, please! if ((a_command > 7) && (a_command != 20)) { // No debugging of status polls, please!
@ -682,8 +682,7 @@ void AnycubicTFTClass::GetCommandFromTFT() {
else { else {
SelectedDirectory[0] = 0; SelectedDirectory[0] = 0;
if (starpos != NULL) if (starpos) *(starpos - 1) = '\0';
*(starpos - 1) = '\0';
strcpy(SelectedFile, TFTstrchr_pointer + 4); strcpy(SelectedFile, TFTstrchr_pointer + 4);
SENDLINE_DBG_PGM_VAL("J20", "TFT Serial Debug: File Selected... J20 ", SelectedFile); // J20 File Selected SENDLINE_DBG_PGM_VAL("J20", "TFT Serial Debug: File Selected... J20 ", SelectedFile); // J20 File Selected

View File

@ -146,9 +146,9 @@
uint16_t FTDI::get_utf8_char_width(utf8_char_t c, font_size_t fs) { uint16_t FTDI::get_utf8_char_width(utf8_char_t c, font_size_t fs) {
int x = 0, y = 0; int x = 0, y = 0;
#ifdef TOUCH_UI_UTF8_WESTERN_CHARSET #ifdef TOUCH_UI_UTF8_WESTERN_CHARSET
WesternCharSet::render_glyph(NULL, x, y, fs, c) || WesternCharSet::render_glyph(nullptr, x, y, fs, c) ||
#endif #endif
StandardCharSet::render_glyph(NULL, x, y, fs, c); StandardCharSet::render_glyph(nullptr, x, y, fs, c);
return x; return x;
} }
@ -165,7 +165,7 @@
*/ */
uint16_t FTDI::get_utf8_text_width(const char *str, font_size_t fs) { uint16_t FTDI::get_utf8_text_width(const char *str, font_size_t fs) {
return render_utf8_text(NULL, 0, 0, str, fs); return render_utf8_text(nullptr, 0, 0, str, fs);
} }
uint16_t FTDI::get_utf8_text_width(progmem_str pstr, font_size_t fs) { uint16_t FTDI::get_utf8_text_width(progmem_str pstr, font_size_t fs) {

View File

@ -110,7 +110,7 @@ static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
draw_return_ui(); draw_return_ui();
} }
else { else {
lv_kb_set_ta(kb, nullptr); /*De-assign the text area to hide it cursor if needed*/ lv_kb_set_ta(kb, nullptr); // De-assign the text area to hide it cursor if needed
lv_obj_del(kb); lv_obj_del(kb);
return; return;
} }
@ -174,7 +174,7 @@ static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
return; return;
} }
/*Add the characters to the text area if set*/ // Add the characters to the text area if set
if (!ext->ta) return; if (!ext->ta) return;
if (strcmp(txt, "Enter") == 0 || strcmp(txt, LV_SYMBOL_NEW_LINE) == 0) if (strcmp(txt, "Enter") == 0 || strcmp(txt, LV_SYMBOL_NEW_LINE) == 0)
@ -214,7 +214,7 @@ static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
void lv_draw_keyboard() { void lv_draw_keyboard() {
scr = lv_screen_create(KEY_BOARD_UI, ""); scr = lv_screen_create(KEY_BOARD_UI, "");
/*Create styles for the keyboard*/ // Create styles for the keyboard
static lv_style_t rel_style, pr_style; static lv_style_t rel_style, pr_style;
lv_style_copy(&rel_style, &lv_style_btn_rel); lv_style_copy(&rel_style, &lv_style_btn_rel);
@ -229,7 +229,7 @@ void lv_draw_keyboard() {
pr_style.body.main_color = lv_color_make(0x72, 0x42, 0x15); pr_style.body.main_color = lv_color_make(0x72, 0x42, 0x15);
pr_style.body.grad_color = lv_color_make(0x6A, 0x3A, 0x0C); pr_style.body.grad_color = lv_color_make(0x6A, 0x3A, 0x0C);
/*Create a keyboard and apply the styles*/ // Create a keyboard and apply the styles
lv_obj_t *kb = lv_kb_create(scr, nullptr); lv_obj_t *kb = lv_kb_create(scr, nullptr);
lv_obj_set_event_cb(kb, lv_kb_event_cb); lv_obj_set_event_cb(kb, lv_kb_event_cb);
lv_kb_set_cursor_manage(kb, true); lv_kb_set_cursor_manage(kb, true);
@ -243,7 +243,7 @@ void lv_draw_keyboard() {
} }
#endif #endif
/*Create a text area. The keyboard will write here*/ // Create a text area. The keyboard will write here
lv_obj_t *ta = lv_ta_create(scr, nullptr); lv_obj_t *ta = lv_ta_create(scr, nullptr);
lv_obj_align(ta, nullptr, LV_ALIGN_IN_TOP_MID, 0, 10); lv_obj_align(ta, nullptr, LV_ALIGN_IN_TOP_MID, 0, 10);
if (keyboard_value == gcodeCommand) { if (keyboard_value == gcodeCommand) {
@ -255,7 +255,7 @@ void lv_draw_keyboard() {
lv_ta_set_text(ta, ""); lv_ta_set_text(ta, "");
} }
/*Assign the text area to the keyboard*/ // Assign the text area to the keyboard
lv_kb_set_ta(kb, ta); lv_kb_set_ta(kb, ta);
} }

View File

@ -129,7 +129,7 @@ void MenuEditItemBase::edit_screen(strfunc_t strfunc, loadfunc_t loadfunc) {
if (ui.should_draw()) if (ui.should_draw())
draw_edit_screen(strfunc(ui.encoderPosition + minEditValue)); draw_edit_screen(strfunc(ui.encoderPosition + minEditValue));
if (ui.lcd_clicked || (liveEdit && ui.should_draw())) { if (ui.lcd_clicked || (liveEdit && ui.should_draw())) {
if (editValue != nullptr) loadfunc(editValue, ui.encoderPosition + minEditValue); if (editValue) loadfunc(editValue, ui.encoderPosition + minEditValue);
if (callbackFunc && (liveEdit || ui.lcd_clicked)) (*callbackFunc)(); if (callbackFunc && (liveEdit || ui.lcd_clicked)) (*callbackFunc)();
if (ui.use_click()) ui.goto_previous_screen(); if (ui.use_click()) ui.goto_previous_screen();
} }

View File

@ -79,7 +79,7 @@ void CANVAS::AddText(uint16_t x, uint16_t y, uint16_t color, uint8_t *string, ui
void CANVAS::AddImage(int16_t x, int16_t y, MarlinImage image, uint16_t *colors) { void CANVAS::AddImage(int16_t x, int16_t y, MarlinImage image, uint16_t *colors) {
uint16_t *data = (uint16_t *)Images[image].data; uint16_t *data = (uint16_t *)Images[image].data;
if (data == NULL) return; if (!data) return;
uint16_t image_width = Images[image].width, uint16_t image_width = Images[image].width,
image_height = Images[image].height; image_height = Images[image].height;

View File

@ -23,7 +23,7 @@
#include "tft_image.h" #include "tft_image.h"
#include <stddef.h> #include <stddef.h>
const tImage NoLogo = { (void *)NULL, 0, 0, NOCOLORS }; const tImage NoLogo = { nullptr, 0, 0, NOCOLORS };
const tImage MarlinLogo112x38x1 = { (void *)marlin_logo_112x38x1, 112, 38, GREYSCALE1 }; const tImage MarlinLogo112x38x1 = { (void *)marlin_logo_112x38x1, 112, 38, GREYSCALE1 };
const tImage MarlinLogo228x255x2 = { (void *)marlin_logo_228x255x2, 228, 255, GREYSCALE2 }; const tImage MarlinLogo228x255x2 = { (void *)marlin_logo_228x255x2, 228, 255, GREYSCALE2 };

View File

@ -30,19 +30,19 @@
uint8_t TFT_Queue::queue[]; uint8_t TFT_Queue::queue[];
uint8_t *TFT_Queue::end_of_queue = queue; uint8_t *TFT_Queue::end_of_queue = queue;
uint8_t *TFT_Queue::current_task = NULL; uint8_t *TFT_Queue::current_task = nullptr;
uint8_t *TFT_Queue::last_task = NULL; uint8_t *TFT_Queue::last_task = nullptr;
void TFT_Queue::reset() { void TFT_Queue::reset() {
tft.abort(); tft.abort();
end_of_queue = queue; end_of_queue = queue;
current_task = NULL; current_task = nullptr;
last_task = NULL; last_task = nullptr;
} }
void TFT_Queue::async() { void TFT_Queue::async() {
if (current_task == NULL) return; if (!current_task) return;
queueTask_t *task = (queueTask_t *)current_task; queueTask_t *task = (queueTask_t *)current_task;
// Check IO busy status // Check IO busy status
@ -63,7 +63,7 @@ void TFT_Queue::async() {
} }
void TFT_Queue::finish_sketch() { void TFT_Queue::finish_sketch() {
if (last_task == NULL) return; if (!last_task) return;
queueTask_t *task = (queueTask_t *)last_task; queueTask_t *task = (queueTask_t *)last_task;
if (task->state == TASK_STATE_SKETCH) { if (task->state == TASK_STATE_SKETCH) {
@ -71,7 +71,7 @@ void TFT_Queue::finish_sketch() {
task->nextTask = end_of_queue; task->nextTask = end_of_queue;
task->state = TASK_STATE_READY; task->state = TASK_STATE_READY;
if (current_task == NULL) current_task = (uint8_t *)task; if (!current_task) current_task = (uint8_t *)task;
} }
} }
@ -184,7 +184,7 @@ void TFT_Queue::fill(uint16_t x, uint16_t y, uint16_t width, uint16_t height, ui
task->state = TASK_STATE_READY; task->state = TASK_STATE_READY;
task->type = TASK_FILL; task->type = TASK_FILL;
if (current_task == NULL) current_task = (uint8_t *)task; if (!current_task) current_task = (uint8_t *)task;
} }
void TFT_Queue::canvas(uint16_t x, uint16_t y, uint16_t width, uint16_t height) { void TFT_Queue::canvas(uint16_t x, uint16_t y, uint16_t width, uint16_t height) {
@ -195,7 +195,7 @@ void TFT_Queue::canvas(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
task->state = TASK_STATE_SKETCH; task->state = TASK_STATE_SKETCH;
task->type = TASK_CANVAS; task->type = TASK_CANVAS;
task->nextTask = NULL; task->nextTask = nullptr;
end_of_queue += sizeof(queueTask_t); end_of_queue += sizeof(queueTask_t);
parametersCanvas_t *task_parameters = (parametersCanvas_t *)end_of_queue; parametersCanvas_t *task_parameters = (parametersCanvas_t *)end_of_queue;
@ -207,7 +207,7 @@ void TFT_Queue::canvas(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
task_parameters->height = height; task_parameters->height = height;
task_parameters->count = 0; task_parameters->count = 0;
if (current_task == NULL) current_task = (uint8_t *)task; if (!current_task) current_task = (uint8_t *)task;
} }
void TFT_Queue::set_background(uint16_t color) { void TFT_Queue::set_background(uint16_t color) {

View File

@ -42,7 +42,7 @@ void TFT_String::set_font(const uint8_t *font) {
font_header = (font_t *)font; font_header = (font_t *)font;
uint32_t glyph; uint32_t glyph;
for (glyph = 0; glyph < 256; glyph++) glyphs[glyph] = NULL; for (glyph = 0; glyph < 256; glyph++) glyphs[glyph] = nullptr;
DEBUG_ECHOLNPAIR("Format: ", font_header->Format); DEBUG_ECHOLNPAIR("Format: ", font_header->Format);
DEBUG_ECHOLNPAIR("BBXWidth: ", font_header->BBXWidth); DEBUG_ECHOLNPAIR("BBXWidth: ", font_header->BBXWidth);

View File

@ -119,9 +119,8 @@ void Touch::idle() {
NOMORE(y, current_control->y + current_control->height); NOMORE(y, current_control->y + current_control->height);
touch(current_control); touch(current_control);
} }
else { else
current_control = NULL; current_control = nullptr;
}
} }
else { else {
for (i = 0; i < controls_count; i++) { for (i = 0; i < controls_count; i++) {
@ -133,7 +132,7 @@ void Touch::idle() {
} }
} }
if (current_control == NULL) if (!current_control)
touch_time = last_touch_ms; touch_time = last_touch_ms;
} }
x = _x; x = _x;
@ -141,7 +140,7 @@ void Touch::idle() {
} }
else { else {
x = y = 0; x = y = 0;
current_control = NULL; current_control = nullptr;
touch_time = 0; touch_time = 0;
touch_control_type = NONE; touch_control_type = NONE;
time_to_hold = 0; time_to_hold = 0;

View File

@ -452,7 +452,7 @@ void MenuEditItemBase::draw_edit_screen(PGM_P const pstr, const char* const valu
void MenuItem_confirm::draw_select_screen(PGM_P const yes, PGM_P const no, const bool yesno, PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) { void MenuItem_confirm::draw_select_screen(PGM_P const yes, PGM_P const no, const bool yesno, PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
uint16_t line = 1; uint16_t line = 1;
if (string == NULL) line++; if (!string) line++;
menu_line(line++); menu_line(line++);
tft_string.set(pref); tft_string.set(pref);

View File

@ -457,7 +457,7 @@ void MenuEditItemBase::draw_edit_screen(PGM_P const pstr, const char* const valu
void MenuItem_confirm::draw_select_screen(PGM_P const yes, PGM_P const no, const bool yesno, PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) { void MenuItem_confirm::draw_select_screen(PGM_P const yes, PGM_P const no, const bool yesno, PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
uint16_t line = 1; uint16_t line = 1;
if (string == NULL) line++; if (!string) line++;
menu_line(line++); menu_line(line++);
tft_string.set(pref); tft_string.set(pref);
@ -945,7 +945,7 @@ static void drawBtn(int x, int y, const char* label, int32_t data, MarlinImage i
tft.add_image(0, 0, imgBtn52Rounded, bgColor, COLOR_BACKGROUND, COLOR_DARKGREY); tft.add_image(0, 0, imgBtn52Rounded, bgColor, COLOR_BACKGROUND, COLOR_DARKGREY);
// TODO: Make an add_text() taking a font arg // TODO: Make an add_text() taking a font arg
if (label != NULL) { if (label) {
tft_string.set(label); tft_string.set(label);
tft_string.trim(); tft_string.trim();
tft.add_text(tft_string.center(width), height / 2 - tft_string.font_height() / 2, bgColor, tft_string); tft.add_text(tft_string.center(width), height / 2 - tft_string.font_height() / 2, bgColor, tft_string);

View File

@ -89,7 +89,7 @@ heatshrink_decoder *heatshrink_decoder_alloc(uint16_t input_buffer_size, uint8_t
size_t buffers_sz = (1 << window_sz2) + input_buffer_size; size_t buffers_sz = (1 << window_sz2) + input_buffer_size;
size_t sz = sizeof(heatshrink_decoder) + buffers_sz; size_t sz = sizeof(heatshrink_decoder) + buffers_sz;
heatshrink_decoder *hsd = HEATSHRINK_MALLOC(sz); heatshrink_decoder *hsd = HEATSHRINK_MALLOC(sz);
if (hsd == nullptr) return nullptr; if (!hsd) return nullptr;
hsd->input_buffer_size = input_buffer_size; hsd->input_buffer_size = input_buffer_size;
hsd->window_sz2 = window_sz2; hsd->window_sz2 = window_sz2;
hsd->lookahead_sz2 = lookahead_sz2; hsd->lookahead_sz2 = lookahead_sz2;
@ -124,7 +124,7 @@ void heatshrink_decoder_reset(heatshrink_decoder *hsd) {
/* Copy SIZE bytes into the decoder's input buffer, if it will fit. */ /* Copy SIZE bytes into the decoder's input buffer, if it will fit. */
HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd, HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd,
uint8_t *in_buf, size_t size, size_t *input_size) { uint8_t *in_buf, size_t size, size_t *input_size) {
if (hsd == nullptr || in_buf == nullptr || input_size == nullptr) if (!hsd || !in_buf || !input_size)
return HSDR_SINK_ERROR_NULL; return HSDR_SINK_ERROR_NULL;
size_t rem = HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd) - hsd->input_size; size_t rem = HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd) - hsd->input_size;
@ -160,7 +160,7 @@ static HSD_state st_backref_count_lsb(heatshrink_decoder *hsd);
static HSD_state st_yield_backref(heatshrink_decoder *hsd, output_info *oi); static HSD_state st_yield_backref(heatshrink_decoder *hsd, output_info *oi);
HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd, uint8_t *out_buf, size_t out_buf_size, size_t *output_size) { HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd, uint8_t *out_buf, size_t out_buf_size, size_t *output_size) {
if (hsd == nullptr || out_buf == nullptr || output_size == nullptr) if (!hsd || !out_buf || !output_size)
return HSDR_POLL_ERROR_NULL; return HSDR_POLL_ERROR_NULL;
*output_size = 0; *output_size = 0;
@ -351,7 +351,7 @@ static uint16_t get_bits(heatshrink_decoder *hsd, uint8_t count) {
} }
HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd) { HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd) {
if (hsd == nullptr) { return HSDR_FINISH_ERROR_NULL; } if (!hsd) return HSDR_FINISH_ERROR_NULL;
switch (hsd->state) { switch (hsd->state) {
case HSDS_TAG_BIT: case HSDS_TAG_BIT:
return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE; return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;

View File

@ -141,7 +141,7 @@ matrix_3x3 matrix_3x3::transpose(const matrix_3x3 &original) {
} }
void matrix_3x3::debug(PGM_P const title) { void matrix_3x3::debug(PGM_P const title) {
if (title != nullptr) { if (title) {
serialprintPGM(title); serialprintPGM(title);
SERIAL_EOL(); SERIAL_EOL();
} }

View File

@ -141,7 +141,7 @@
// Pins for DOGM SPI LCD Support // Pins for DOGM SPI LCD Support
#define DOGLCD_A0 26 #define DOGLCD_A0 26
#define DOGLCD_CS 24 #define DOGLCD_CS 24
#define DOGLCD_MOSI -1 #define DOGLCD_MOSI -1 // Prevent auto-define by Conditionals_post.h
#define DOGLCD_SCK -1 #define DOGLCD_SCK -1
#define BTN_EN1 23 #define BTN_EN1 23

View File

@ -171,7 +171,7 @@
// Shared FSMC Configs // Shared FSMC Configs
#if HAS_FSMC_TFT #if HAS_FSMC_TFT
#define DOGLCD_MOSI -1 // prevent redefine Conditionals_post.h #define DOGLCD_MOSI -1 // Prevent auto-define by Conditionals_post.h
#define DOGLCD_SCK -1 #define DOGLCD_SCK -1
#define FSMC_CS_PIN PD7 // NE4 #define FSMC_CS_PIN PD7 // NE4

View File

@ -21,12 +21,12 @@
*/ */
#if __GNUC__ > 8 #if __GNUC__ > 8
// The NXP platform updated GCC from 7.2.1 to 9.2.1
// and this new warning apparently can be ignored.
#pragma GCC diagnostic ignored "-Waddress-of-packed-member" #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
#endif #endif
/** /**
* sd/SdBaseFile.cpp
*
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2009 by William Greiman * Copyright (c) 2009 by William Greiman
* *

View File

@ -22,11 +22,8 @@
#pragma once #pragma once
/** /**
* \file * sd/SdBaseFile.h
* \brief SdBaseFile class *
*/
/**
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2009 by William Greiman * Copyright (c) 2009 by William Greiman
* *

View File

@ -22,7 +22,8 @@
#pragma once #pragma once
/** /**
* SdFatConfig.h * sd/SdFatConfig.h
*
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2009 by William Greiman * Copyright (c) 2009 by William Greiman
* *

View File

@ -22,11 +22,8 @@
#pragma once #pragma once
/** /**
* \file * sd/SdFatStructs.h
* \brief FAT file structures *
*/
/**
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2009 by William Greiman * Copyright (c) 2009 by William Greiman
* *

View File

@ -21,6 +21,8 @@
*/ */
/** /**
* sd/SdFatUtil.cpp
*
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2008 by William Greiman * Copyright (c) 2008 by William Greiman
* *

View File

@ -22,6 +22,8 @@
#pragma once #pragma once
/** /**
* sd/SdFatUtil.h
*
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2008 by William Greiman * Copyright (c) 2008 by William Greiman
* *

View File

@ -21,6 +21,8 @@
*/ */
/** /**
* sd/SdFile.cpp
*
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2009 by William Greiman * Copyright (c) 2009 by William Greiman
* *

View File

@ -22,11 +22,8 @@
#pragma once #pragma once
/** /**
* \file * sd/SdFile.h
* \brief SdFile class *
*/
/**
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2009 by William Greiman * Copyright (c) 2009 by William Greiman
* *
@ -42,7 +39,7 @@
* \class SdFile * \class SdFile
* \brief SdBaseFile with Print. * \brief SdBaseFile with Print.
*/ */
class SdFile : public SdBaseFile/*, public Print*/ { class SdFile : public SdBaseFile {
public: public:
SdFile() {} SdFile() {}
SdFile(const char* name, uint8_t oflag); SdFile(const char* name, uint8_t oflag);

View File

@ -21,6 +21,8 @@
*/ */
/** /**
* sd/SdVolume.cpp
*
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2009 by William Greiman * Copyright (c) 2009 by William Greiman
* *

View File

@ -22,11 +22,8 @@
#pragma once #pragma once
/** /**
* \file * sd/SdVolume.h
* \brief SdVolume class *
*/
/**
* Arduino SdFat Library * Arduino SdFat Library
* Copyright (c) 2009 by William Greiman * Copyright (c) 2009 by William Greiman
* *

View File

@ -639,7 +639,7 @@ bool CardReader::fileExists(const char * const path) {
selectByName(*diveDir, fname); selectByName(*diveDir, fname);
diveDir->close(); diveDir->close();
} }
return fname != nullptr; return !!fname;
} }
// //
@ -684,7 +684,7 @@ void CardReader::write_command(char * const buf) {
char* end = buf + strlen(buf) - 1; char* end = buf + strlen(buf) - 1;
file.writeError = false; file.writeError = false;
if ((npos = strchr(buf, 'N')) != nullptr) { if ((npos = strchr(buf, 'N'))) {
begin = strchr(npos, ' ') + 1; begin = strchr(npos, ' ') + 1;
end = strchr(npos, '*') - 1; end = strchr(npos, '*') - 1;
} }

View File

@ -22,7 +22,10 @@
* Web : https://www.circuitsathome.com * Web : https://www.circuitsathome.com
* e-mail : support@circuitsathome.com * e-mail : support@circuitsathome.com
*/ */
/* USB functions */
//
// USB functions supporting Flash Drive
//
#include "../../../inc/MarlinConfigPre.h" #include "../../../inc/MarlinConfigPre.h"
@ -35,7 +38,7 @@ static uint8_t usb_task_state;
/* constructor */ /* constructor */
USB::USB() : bmHubPre(0) { USB::USB() : bmHubPre(0) {
usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; //set up state machine usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; // Set up state machine
init(); init();
} }
@ -45,13 +48,8 @@ void USB::init() {
bmHubPre = 0; bmHubPre = 0;
} }
uint8_t USB::getUsbTaskState() { uint8_t USB::getUsbTaskState() { return usb_task_state; }
return usb_task_state; void USB::setUsbTaskState(uint8_t state) { usb_task_state = state; }
}
void USB::setUsbTaskState(uint8_t state) {
usb_task_state = state;
}
EpInfo* USB::getEpInfoEntry(uint8_t addr, uint8_t ep) { EpInfo* USB::getEpInfoEntry(uint8_t addr, uint8_t ep) {
UsbDevice *p = addrPool.GetUsbDevicePtr(addr); UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
@ -70,9 +68,11 @@ EpInfo* USB::getEpInfoEntry(uint8_t addr, uint8_t ep) {
return nullptr; return nullptr;
} }
/* set device table entry */ /**
* Set device table entry
/* each device is different and has different number of endpoints. This function plugs endpoint record structure, defined in application, to devtable */ * Each device is different and has different number of endpoints.
* This function plugs endpoint record structure, defined in application, to devtable
*/
uint8_t USB::setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo* eprecord_ptr) { uint8_t USB::setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo* eprecord_ptr) {
if (!eprecord_ptr) if (!eprecord_ptr)
return USB_ERROR_INVALID_ARGUMENT; return USB_ERROR_INVALID_ARGUMENT;
@ -112,7 +112,7 @@ uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_l
USBTRACE2(" NAK Limit: ", nak_limit); USBTRACE2(" NAK Limit: ", nak_limit);
USBTRACE("\r\n"); USBTRACE("\r\n");
*/ */
regWr(rPERADDR, addr); //set peripheral address regWr(rPERADDR, addr); // Set peripheral address
uint8_t mode = regRd(rMODE); uint8_t mode = regRd(rMODE);
@ -121,8 +121,6 @@ uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_l
//Serial.print("\r\nLS: "); //Serial.print("\r\nLS: ");
//Serial.println(p->lowspeed, HEX); //Serial.println(p->lowspeed, HEX);
// Set bmLOWSPEED and bmHUBPRE in case of low-speed device, reset them otherwise // Set bmLOWSPEED and bmHUBPRE in case of low-speed device, reset them otherwise
regWr(rMODE, (p->lowspeed) ? mode | bmLOWSPEED | bmHubPre : mode & ~(bmHUBPRE | bmLOWSPEED)); regWr(rMODE, (p->lowspeed) ? mode | bmLOWSPEED | bmHubPre : mode & ~(bmHUBPRE | bmLOWSPEED));
@ -133,11 +131,10 @@ uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_l
/* depending on request. Actual requests are defined as inlines */ /* depending on request. Actual requests are defined as inlines */
/* return codes: */ /* return codes: */
/* 00 = success */ /* 00 = success */
/* 01-0f = non-zero HRSLT */ /* 01-0f = non-zero HRSLT */
uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi, uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr, USBReadParser *p) { uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr, USBReadParser *p) {
bool direction = false; //request direction, IN or OUT bool direction = false; // Request direction, IN or OUT
uint8_t rcode; uint8_t rcode;
SETUP_PKT setup_pkt; SETUP_PKT setup_pkt;
@ -157,15 +154,15 @@ uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bReque
setup_pkt.wIndex = wInd; setup_pkt.wIndex = wInd;
setup_pkt.wLength = total; setup_pkt.wLength = total;
bytesWr(rSUDFIFO, 8, (uint8_t*) & setup_pkt); //transfer to setup packet FIFO bytesWr(rSUDFIFO, 8, (uint8_t*) & setup_pkt); // Transfer to setup packet FIFO
rcode = dispatchPkt(tokSETUP, ep, nak_limit); //dispatch packet rcode = dispatchPkt(tokSETUP, ep, nak_limit); // Dispatch packet
if (rcode) return rcode; // Return HRSLT if not zero if (rcode) return rcode; // Return HRSLT if not zero
if (dataptr != nullptr) { //data stage, if present if (dataptr) { // Data stage, if present
if (direction) { //IN transfer if (direction) { // IN transfer
uint16_t left = total; uint16_t left = total;
pep->bmRcvToggle = 1; //bmRCVTOG1; pep->bmRcvToggle = 1; // BmRCVTOG1;
while (left) { while (left) {
// Bytes read into buffer // Bytes read into buffer
@ -174,7 +171,7 @@ uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bReque
rcode = InTransfer(pep, nak_limit, &read, dataptr); rcode = InTransfer(pep, nak_limit, &read, dataptr);
if (rcode == hrTOGERR) { if (rcode == hrTOGERR) {
// yes, we flip it wrong here so that next time it is actually correct! // Yes, we flip it wrong here so that next time it is actually correct!
pep->bmRcvToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1; pep->bmRcvToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
continue; continue;
} }
@ -189,21 +186,21 @@ uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bReque
if (read < nbytes) break; if (read < nbytes) break;
} }
} }
else { //OUT transfer else { // OUT transfer
pep->bmSndToggle = 1; //bmSNDTOG1; pep->bmSndToggle = 1; // BmSNDTOG1;
rcode = OutTransfer(pep, nak_limit, nbytes, dataptr); rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
} }
if (rcode) return rcode; // return error if (rcode) return rcode; // Return error
} }
// Status stage // Status stage
return dispatchPkt((direction) ? tokOUTHS : tokINHS, ep, nak_limit); //GET if direction return dispatchPkt((direction) ? tokOUTHS : tokINHS, ep, nak_limit); // GET if direction
} }
/* IN transfer to arbitrary endpoint. Assumes PERADDR is set. Handles multiple packets if necessary. Transfers 'nbytes' bytes. */ /**
/* Keep sending INs and writes data to memory area pointed by 'data' */ * IN transfer to arbitrary endpoint. Assumes PERADDR is set. Handles multiple packets if necessary. Transfers 'nbytes' bytes.
* Keep sending INs and writes data to memory area pointed by 'data'
/* rcode 0 if no errors. rcode 01-0f is relayed from dispatchPkt(). Rcode f0 means RCVDAVIRQ error, * rcode 0 if no errors. rcode 01-0f is relayed from dispatchPkt(). Rcode f0 means RCVDAVIRQ error, fe = USB xfer timeout
fe USB xfer timeout */ */
uint8_t USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data, uint8_t bInterval /*= 0*/) { uint8_t USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data, uint8_t bInterval /*= 0*/) {
EpInfo *pep = nullptr; EpInfo *pep = nullptr;
uint16_t nak_limit = 0; uint16_t nak_limit = 0;
@ -227,29 +224,29 @@ uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, ui
uint8_t maxpktsize = pep->maxPktSize; uint8_t maxpktsize = pep->maxPktSize;
*nbytesptr = 0; *nbytesptr = 0;
regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); //set toggle value regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); // Set toggle value
// use a 'break' to exit this loop // Use a 'break' to exit this loop
for (;;) { for (;;) {
rcode = dispatchPkt(tokIN, pep->epAddr, nak_limit); //IN packet to EP-'endpoint'. Function takes care of NAKS. rcode = dispatchPkt(tokIN, pep->epAddr, nak_limit); // IN packet to EP-'endpoint'. Function takes care of NAKS.
if (rcode == hrTOGERR) { if (rcode == hrTOGERR) {
// yes, we flip it wrong here so that next time it is actually correct! // Yes, we flip it wrong here so that next time it is actually correct!
pep->bmRcvToggle = (regRd(rHRSL) & bmRCVTOGRD) ? 0 : 1; pep->bmRcvToggle = (regRd(rHRSL) & bmRCVTOGRD) ? 0 : 1;
regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); //set toggle value regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); // Set toggle value
continue; continue;
} }
if (rcode) { if (rcode) {
//printf(">>>>>>>> Problem! dispatchPkt %2.2x\r\n", rcode); //printf(">>>>>>>> Problem! dispatchPkt %2.2x\r\n", rcode);
break; //should be 0, indicating ACK. Else return error code. break; // Should be 0, indicating ACK. Else return error code.
} }
/* check for RCVDAVIRQ and generate error if not present */ /* check for RCVDAVIRQ and generate error if not present */
/* the only case when absence of RCVDAVIRQ makes sense is when toggle error occurred. Need to add handling for that */ /* the only case when absence of RCVDAVIRQ makes sense is when toggle error occurred. Need to add handling for that */
if ((regRd(rHIRQ) & bmRCVDAVIRQ) == 0) { if ((regRd(rHIRQ) & bmRCVDAVIRQ) == 0) {
//printf(">>>>>>>> Problem! NO RCVDAVIRQ!\r\n"); //printf(">>>>>>>> Problem! NO RCVDAVIRQ!\r\n");
rcode = 0xF0; //receive error rcode = 0xF0; // Receive error
break; break;
} }
pktsize = regRd(rRCVBC); //number of received bytes pktsize = regRd(rRCVBC); // Number of received bytes
//printf("Got %i bytes \r\n", pktsize); //printf("Got %i bytes \r\n", pktsize);
// This would be OK, but... // This would be OK, but...
//assert(pktsize <= nbytes); //assert(pktsize <= nbytes);
@ -266,7 +263,7 @@ uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, ui
data = bytesRd(rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data); data = bytesRd(rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data);
regWr(rHIRQ, bmRCVDAVIRQ); // Clear the IRQ & free the buffer regWr(rHIRQ, bmRCVDAVIRQ); // Clear the IRQ & free the buffer
*nbytesptr += pktsize; // add this packet's byte count to total transfer length *nbytesptr += pktsize; // Add this packet's byte count to total transfer length
/* The transfer is complete under two conditions: */ /* The transfer is complete under two conditions: */
/* 1. The device sent a short packet (L.T. maxPacketSize) */ /* 1. The device sent a short packet (L.T. maxPacketSize) */
@ -284,10 +281,11 @@ uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, ui
return rcode; return rcode;
} }
/* OUT transfer to arbitrary endpoint. Handles multiple packets if necessary. Transfers 'nbytes' bytes. */ /**
/* Handles NAK bug per Maxim Application Note 4000 for single buffer transfer */ * OUT transfer to arbitrary endpoint. Handles multiple packets if necessary. Transfers 'nbytes' bytes.
* Handles NAK bug per Maxim Application Note 4000 for single buffer transfer
/* rcode 0 if no errors. rcode 01-0f is relayed from HRSL */ * rcode 0 if no errors. rcode 01-0f is relayed from HRSL
*/
uint8_t USB::outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* data) { uint8_t USB::outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* data) {
EpInfo *pep = nullptr; EpInfo *pep = nullptr;
uint16_t nak_limit = 0; uint16_t nak_limit = 0;
@ -300,7 +298,7 @@ uint8_t USB::outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dat
uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) { uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) {
uint8_t rcode = hrSUCCESS, retry_count; uint8_t rcode = hrSUCCESS, retry_count;
uint8_t *data_p = data; //local copy of the data pointer uint8_t *data_p = data; // Local copy of the data pointer
uint16_t bytes_tosend, nak_count; uint16_t bytes_tosend, nak_count;
uint16_t bytes_left = nbytes; uint16_t bytes_left = nbytes;
@ -311,17 +309,17 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
uint32_t timeout = (uint32_t)millis() + USB_XFER_TIMEOUT; uint32_t timeout = (uint32_t)millis() + USB_XFER_TIMEOUT;
regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); //set toggle value regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); // Set toggle value
while (bytes_left) { while (bytes_left) {
retry_count = 0; retry_count = 0;
nak_count = 0; nak_count = 0;
bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left; bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
bytesWr(rSNDFIFO, bytes_tosend, data_p); //filling output FIFO bytesWr(rSNDFIFO, bytes_tosend, data_p); // Filling output FIFO
regWr(rSNDBC, bytes_tosend); //set number of bytes regWr(rSNDBC, bytes_tosend); // Set number of bytes
regWr(rHXFR, (tokOUT | pep->epAddr)); //dispatch packet regWr(rHXFR, (tokOUT | pep->epAddr)); // Dispatch packet
while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); //wait for the completion IRQ while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); // Wait for the completion IRQ
regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ regWr(rHIRQ, bmHXFRDNIRQ); // Clear IRQ
rcode = (regRd(rHRSL) & 0x0F); rcode = (regRd(rHRSL) & 0x0F);
while (rcode && ((int32_t)((uint32_t)millis() - timeout) < 0L)) { while (rcode && ((int32_t)((uint32_t)millis() - timeout) < 0L)) {
@ -330,18 +328,18 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
nak_count++; nak_count++;
if (nak_limit && (nak_count == nak_limit)) if (nak_limit && (nak_count == nak_limit))
goto breakout; goto breakout;
//return ( rcode); //return rcode;
break; break;
case hrTIMEOUT: case hrTIMEOUT:
retry_count++; retry_count++;
if (retry_count == USB_RETRY_LIMIT) if (retry_count == USB_RETRY_LIMIT)
goto breakout; goto breakout;
//return ( rcode); //return rcode;
break; break;
case hrTOGERR: case hrTOGERR:
// yes, we flip it wrong here so that next time it is actually correct! // Yes, we flip it wrong here so that next time it is actually correct!
pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1; pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); //set toggle value regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); // Set toggle value
break; break;
default: default:
goto breakout; goto breakout;
@ -351,26 +349,27 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
regWr(rSNDBC, 0); regWr(rSNDBC, 0);
regWr(rSNDFIFO, *data_p); regWr(rSNDFIFO, *data_p);
regWr(rSNDBC, bytes_tosend); regWr(rSNDBC, bytes_tosend);
regWr(rHXFR, (tokOUT | pep->epAddr)); //dispatch packet regWr(rHXFR, (tokOUT | pep->epAddr)); // Dispatch packet
while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); //wait for the completion IRQ while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); // Wait for the completion IRQ
regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ regWr(rHIRQ, bmHXFRDNIRQ); // Clear IRQ
rcode = (regRd(rHRSL) & 0x0F); rcode = (regRd(rHRSL) & 0x0F);
} // while rcode && .... } // While rcode && ....
bytes_left -= bytes_tosend; bytes_left -= bytes_tosend;
data_p += bytes_tosend; data_p += bytes_tosend;
} // while bytes_left... } // While bytes_left...
breakout: breakout:
pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 1 : 0; //bmSNDTOG1 : bmSNDTOG0; //update toggle pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 1 : 0; // BmSNDTOG1 : bmSNDTOG0; // Update toggle
return ( rcode); //should be 0 in all cases return ( rcode); // Should be 0 in all cases
} }
/* dispatch USB packet. Assumes peripheral address is set and relevant buffer is loaded/empty */ /**
/* If NAK, tries to re-send up to nak_limit times */ * Dispatch USB packet. Assumes peripheral address is set and relevant buffer is loaded/empty
/* If nak_limit == 0, do not count NAKs, exit after timeout */ * If NAK, tries to re-send up to nak_limit times
/* If bus timeout, re-sends up to USB_RETRY_LIMIT times */ * If nak_limit == 0, do not count NAKs, exit after timeout
* If bus timeout, re-sends up to USB_RETRY_LIMIT times
/* return codes 0x00-0x0F are HRSLT( 0x00 being success ), 0xFF means timeout */ * return codes 0x00-0x0F are HRSLT( 0x00 being success ), 0xFF means timeout
*/
uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) { uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
uint32_t timeout = (uint32_t)millis() + USB_XFER_TIMEOUT; uint32_t timeout = (uint32_t)millis() + USB_XFER_TIMEOUT;
uint8_t tmpdata; uint8_t tmpdata;
@ -380,29 +379,28 @@ uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
while ((int32_t)((uint32_t)millis() - timeout) < 0L) { while ((int32_t)((uint32_t)millis() - timeout) < 0L) {
#if defined(ESP8266) || defined(ESP32) #if defined(ESP8266) || defined(ESP32)
yield(); // needed in order to reset the watchdog timer on the ESP8266 yield(); // Needed in order to reset the watchdog timer on the ESP8266
#endif #endif
regWr(rHXFR, (token | ep)); //launch the transfer regWr(rHXFR, (token | ep)); // Launch the transfer
rcode = USB_ERROR_TRANSFER_TIMEOUT; rcode = USB_ERROR_TRANSFER_TIMEOUT;
while ((int32_t)((uint32_t)millis() - timeout) < 0L) { //wait for transfer completion while ((int32_t)((uint32_t)millis() - timeout) < 0L) { // Wait for transfer completion
#if defined(ESP8266) || defined(ESP32) #if defined(ESP8266) || defined(ESP32)
yield(); // needed to reset the watchdog timer on the ESP8266 yield(); // Needed to reset the watchdog timer on the ESP8266
#endif #endif
tmpdata = regRd(rHIRQ); tmpdata = regRd(rHIRQ);
if (tmpdata & bmHXFRDNIRQ) { if (tmpdata & bmHXFRDNIRQ) {
regWr(rHIRQ, bmHXFRDNIRQ); //clear the interrupt regWr(rHIRQ, bmHXFRDNIRQ); // Clear the interrupt
rcode = 0x00; rcode = 0x00;
break; break;
} }
} // while millis() < timeout } // While millis() < timeout
//if (rcode != 0x00) //exit if timeout //if (rcode != 0x00) return rcode; // Exit if timeout
// return ( rcode);
rcode = (regRd(rHRSL) & 0x0F); //analyze transfer result rcode = (regRd(rHRSL) & 0x0F); // Analyze transfer result
switch (rcode) { switch (rcode) {
case hrNAK: case hrNAK:
@ -419,12 +417,12 @@ uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
return (rcode); return (rcode);
} }
} // while timeout > millis() } // While timeout > millis()
return rcode; return rcode;
} }
/* USB main task. Performs enumeration/cleanup */ // USB main task. Performs enumeration/cleanup
void USB::Task() { //USB state machine void USB::Task() { // USB state machine
uint8_t rcode; uint8_t rcode;
uint8_t tmpdata; uint8_t tmpdata;
static uint32_t delay = 0; static uint32_t delay = 0;
@ -437,19 +435,19 @@ void USB::Task() { //USB state machine
/* modify USB task state if Vbus changed */ /* modify USB task state if Vbus changed */
switch (tmpdata) { switch (tmpdata) {
case SE1: //illegal state case SE1: // Illegal state
usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL; usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL;
lowspeed = false; lowspeed = false;
break; break;
case SE0: //disconnected case SE0: // Disconnected
if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED) if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED)
usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
lowspeed = false; lowspeed = false;
break; break;
case LSHOST: case LSHOST:
lowspeed = true; lowspeed = true;
//intentional fallthrough // Intentional fallthrough
case FSHOST: //attached case FSHOST: // Attached
if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED) { if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED) {
delay = (uint32_t)millis() + USB_SETTLE_DELAY; delay = (uint32_t)millis() + USB_SETTLE_DELAY;
usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE; usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE;
@ -470,31 +468,31 @@ void USB::Task() { //USB state machine
usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE; usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE;
break; break;
case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: //just sit here case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: // Just sit here
break; break;
case USB_DETACHED_SUBSTATE_ILLEGAL: //just sit here case USB_DETACHED_SUBSTATE_ILLEGAL: // Just sit here
break; break;
case USB_ATTACHED_SUBSTATE_SETTLE: //settle time for just attached device case USB_ATTACHED_SUBSTATE_SETTLE: // Settle time for just attached device
if ((int32_t)((uint32_t)millis() - delay) >= 0L) if ((int32_t)((uint32_t)millis() - delay) >= 0L)
usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE; usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
else break; // don't fall through else break; // Don't fall through
case USB_ATTACHED_SUBSTATE_RESET_DEVICE: case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
regWr(rHCTL, bmBUSRST); //issue bus reset regWr(rHCTL, bmBUSRST); // Issue bus reset
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE; usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE;
break; break;
case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE: case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE:
if ((regRd(rHCTL) & bmBUSRST) == 0) { if ((regRd(rHCTL) & bmBUSRST) == 0) {
tmpdata = regRd(rMODE) | bmSOFKAENAB; //start SOF generation tmpdata = regRd(rMODE) | bmSOFKAENAB; // Start SOF generation
regWr(rMODE, tmpdata); regWr(rMODE, tmpdata);
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF; usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF;
//delay = (uint32_t)millis() + 20; //20ms wait after reset per USB spec //delay = (uint32_t)millis() + 20; // 20ms wait after reset per USB spec
} }
break; break;
case USB_ATTACHED_SUBSTATE_WAIT_SOF: //todo: change check order case USB_ATTACHED_SUBSTATE_WAIT_SOF: // Todo: change check order
if (regRd(rHIRQ) & bmFRAMEIRQ) { if (regRd(rHIRQ) & bmFRAMEIRQ) {
//when first SOF received _and_ 20ms has passed we can continue // When first SOF received _and_ 20ms has passed we can continue
/* /*
if (delay < (uint32_t)millis()) //20ms passed if (delay < (uint32_t)millis()) // 20ms passed
usb_task_state = USB_STATE_CONFIGURING; usb_task_state = USB_STATE_CONFIGURING;
*/ */
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET; usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET;
@ -503,7 +501,7 @@ void USB::Task() { //USB state machine
break; break;
case USB_ATTACHED_SUBSTATE_WAIT_RESET: case USB_ATTACHED_SUBSTATE_WAIT_RESET:
if ((int32_t)((uint32_t)millis() - delay) >= 0L) usb_task_state = USB_STATE_CONFIGURING; if ((int32_t)((uint32_t)millis() - delay) >= 0L) usb_task_state = USB_STATE_CONFIGURING;
else break; // don't fall through else break; // Don't fall through
case USB_STATE_CONFIGURING: case USB_STATE_CONFIGURING:
//Serial.print("\r\nConf.LS: "); //Serial.print("\r\nConf.LS: ");
@ -565,11 +563,11 @@ again:
if (rcode == USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET) { if (rcode == USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET) {
if (parent == 0) { if (parent == 0) {
// Send a bus reset on the root interface. // Send a bus reset on the root interface.
regWr(rHCTL, bmBUSRST); //issue bus reset regWr(rHCTL, bmBUSRST); // Issue bus reset
delay(102); // delay 102ms, compensate for clock inaccuracy. delay(102); // Delay 102ms, compensate for clock inaccuracy.
} }
else { else {
// reset parent port // Reset parent port
devConfig[parent]->ResetHubPort(port); devConfig[parent]->ResetHubPort(port);
} }
} }
@ -592,11 +590,11 @@ again:
// Issue a bus reset, because the device may be in a limbo state // Issue a bus reset, because the device may be in a limbo state
if (parent == 0) { if (parent == 0) {
// Send a bus reset on the root interface. // Send a bus reset on the root interface.
regWr(rHCTL, bmBUSRST); //issue bus reset regWr(rHCTL, bmBUSRST); // Issue bus reset
delay(102); // delay 102ms, compensate for clock inaccuracy. delay(102); // Delay 102ms, compensate for clock inaccuracy.
} }
else { else {
// reset parent port // Reset parent port
devConfig[parent]->ResetHubPort(port); devConfig[parent]->ResetHubPort(port);
} }
} }
@ -623,19 +621,19 @@ again:
* 4: set address * 4: set address
* 5: pUsb->setEpInfoEntry(bAddress, 1, epInfo), exit on fail * 5: pUsb->setEpInfoEntry(bAddress, 1, epInfo), exit on fail
* 6: while (configurations) { * 6: while (configurations) {
* for (each configuration) { * for (each configuration) {
* for (each driver) { * for (each driver) {
* 6a: Ask device if it likes configuration. Returns 0 on OK. * 6a: Ask device if it likes configuration. Returns 0 on OK.
* If successful, the driver configured device. * If successful, the driver configured device.
* The driver now owns the endpoints, and takes over managing them. * The driver now owns the endpoints, and takes over managing them.
* The following will need codes: * The following will need codes:
* Everything went well, instance consumed, exit with success. * Everything went well, instance consumed, exit with success.
* Instance already in use, ignore it, try next driver. * Instance already in use, ignore it, try next driver.
* Not a supported device, ignore it, try next driver. * Not a supported device, ignore it, try next driver.
* Not a supported configuration for this device, ignore it, try next driver. * Not a supported configuration for this device, ignore it, try next driver.
* Could not configure device, fatal, exit with fail. * Could not configure device, fatal, exit with fail.
* } * }
* } * }
* } * }
* 7: for (each driver) { * 7: for (each driver) {
* 7a: Ask device if it knows this VID/PID. Acts exactly like 6a, but using VID/PID * 7a: Ask device if it knows this VID/PID. Acts exactly like 6a, but using VID/PID
@ -671,7 +669,7 @@ uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
oldep_ptr = p->epinfo; oldep_ptr = p->epinfo;
// Temporary assign new pointer to epInfo to p->epinfo in order to // Temporary assign new pointer to epInfo to p->epinfo in order to
// avoid toggle inconsistence // Avoid toggle inconsistence
p->epinfo = &epInfo; p->epinfo = &epInfo;
@ -687,7 +685,7 @@ uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
return rcode; return rcode;
} }
// to-do? // To-do?
// Allocate new address according to device class // Allocate new address according to device class
//bAddress = addrPool.AllocAddress(parent, false, port); //bAddress = addrPool.AllocAddress(parent, false, port);
@ -698,11 +696,11 @@ uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
// Qualify with subclass too. // Qualify with subclass too.
// //
// VID/PID & class tests default to false for drivers not yet ported // VID/PID & class tests default to false for drivers not yet ported
// subclass defaults to true, so you don't have to define it if you don't have to. // Subclass defaults to true, so you don't have to define it if you don't have to.
// //
for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) { for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) {
if (!devConfig[devConfigIndex]) continue; // no driver if (!devConfig[devConfigIndex]) continue; // No driver
if (devConfig[devConfigIndex]->GetAddress()) continue; // consumed if (devConfig[devConfigIndex]->GetAddress()) continue; // Consumed
if (devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) { if (devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) {
rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed); rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED) if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED)
@ -712,20 +710,20 @@ uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
if (devConfigIndex < USB_NUMDEVICES) return rcode; if (devConfigIndex < USB_NUMDEVICES) return rcode;
// blindly attempt to configure // Blindly attempt to configure
for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) { for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) {
if (!devConfig[devConfigIndex]) continue; if (!devConfig[devConfigIndex]) continue;
if (devConfig[devConfigIndex]->GetAddress()) continue; // consumed if (devConfig[devConfigIndex]->GetAddress()) continue; // Consumed
if (devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) continue; // If this is true it means it must have returned USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED above if (devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) continue; // If this is true it means it must have returned USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED above
rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed); rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
//printf("ERROR ENUMERATING %2.2x\r\n", rcode); //printf("ERROR ENUMERATING %2.2x\r\n", rcode);
if (!(rcode == USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED || rcode == USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE)) { if (!(rcode == USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED || rcode == USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE)) {
// in case of an error dev_index should be reset to 0 // In case of an error dev_index should be reset to 0
// in order to start from the very beginning the // in order to start from the very beginning the
// next time the program gets here // next time the program gets here
//if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE) //if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE)
// devConfigIndex = 0; //devConfigIndex = 0;
return rcode; return rcode;
} }
} }
@ -744,20 +742,22 @@ uint8_t USB::ReleaseDevice(uint8_t addr) {
return 0; return 0;
} }
#if 1 //!defined(USB_METHODS_INLINE) // Get device descriptor
//get device descriptor
uint8_t USB::getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr) { uint8_t USB::getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr) {
return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, nullptr); return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, nullptr);
} }
//get configuration descriptor
// Get configuration descriptor
uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) { uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, nullptr); return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, nullptr);
} }
/* Requests Configuration Descriptor. Sends two Get Conf Descr requests. The first one gets the total length of all descriptors, then the second one requests this /**
total length. The length of the first request can be shorter ( 4 bytes ), however, there are devices which won't work unless this length is set to 9 */ * Requests Configuration Descriptor. Sends two Get Conf Descr requests.
* The first one gets the total length of all descriptors, then the second one requests this
* total length. The length of the first request can be shorter (4 bytes), however, there are
* devices which won't work unless this length is set to 9.
*/
uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser *p) { uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser *p) {
const uint8_t bufSize = 64; const uint8_t bufSize = 64;
uint8_t buf[bufSize]; uint8_t buf[bufSize];
@ -773,25 +773,23 @@ uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser
return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p); return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p);
} }
//get string descriptor // Get string descriptor
uint8_t USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) { uint8_t USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) {
return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, nullptr); return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, nullptr);
} }
//set address
// Set address
uint8_t USB::setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr) { uint8_t USB::setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr) {
uint8_t rcode = ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr); uint8_t rcode = ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
//delay(2); //per USB 2.0 sect.9.2.6.3 //delay(2); // Per USB 2.0 sect.9.2.6.3
delay(300); // Older spec says you should wait at least 200ms delay(300); // Older spec says you should wait at least 200ms
return rcode; return rcode;
//return ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr); //return ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
} }
//set configuration
// Set configuration
uint8_t USB::setConf(uint8_t addr, uint8_t ep, uint8_t conf_value) { uint8_t USB::setConf(uint8_t addr, uint8_t ep, uint8_t conf_value) {
return ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr); return ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
} }
#endif // defined(USB_METHODS_INLINE)
#endif // USB_FLASH_DRIVE_SUPPORT #endif // USB_FLASH_DRIVE_SUPPORT