code
stringlengths
1
1.05M
repo_name
stringlengths
6
83
path
stringlengths
3
242
language
stringclasses
222 values
license
stringclasses
20 values
size
int64
1
1.05M
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #include "../platforms.h" #ifdef HAL_STM32 #include "../../inc/MarlinConfig.h" #if ALL(USE_OTG_USB_HOST, USBHOST) #include "usb_host.h" #include "../shared/Marduino.h" #include "usbh_core.h" #include "usbh_msc.h" USBH_HandleTypeDef hUsbHost; USBHost usb; BulkStorage bulk(&usb); static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id) { switch(id) { case HOST_USER_SELECT_CONFIGURATION: //SERIAL_ECHOLNPGM("APPLICATION_SELECT_CONFIGURATION"); break; case HOST_USER_DISCONNECTION: //SERIAL_ECHOLNPGM("APPLICATION_DISCONNECT"); usb.setUsbTaskState(USB_STATE_INIT); break; case HOST_USER_CLASS_ACTIVE: //SERIAL_ECHOLNPGM("APPLICATION_READY"); usb.setUsbTaskState(USB_STATE_RUNNING); break; case HOST_USER_CONNECTION: break; default: break; } } bool USBHost::start() { if (USBH_Init(&hUsbHost, USBH_UserProcess, TERN(USE_USB_HS_IN_FS, HOST_HS, HOST_FS)) != USBH_OK) { SERIAL_ECHOLNPGM("Error: USBH_Init"); return false; } if (USBH_RegisterClass(&hUsbHost, USBH_MSC_CLASS) != USBH_OK) { SERIAL_ECHOLNPGM("Error: USBH_RegisterClass"); return false; } if (USBH_Start(&hUsbHost) != USBH_OK) { SERIAL_ECHOLNPGM("Error: USBH_Start"); return false; } return true; } void USBHost::Task() { USBH_Process(&hUsbHost); } uint8_t USBHost::getUsbTaskState() { return usb_task_state; } void USBHost::setUsbTaskState(uint8_t state) { usb_task_state = state; if (usb_task_state == USB_STATE_RUNNING) { MSC_LUNTypeDef info; USBH_MSC_GetLUNInfo(&hUsbHost, usb.lun, &info); capacity = info.capacity.block_nbr / 2000; block_size = info.capacity.block_size; block_count = info.capacity.block_nbr; //SERIAL_ECHOLNPGM("info.capacity.block_nbr : %ld\n", info.capacity.block_nbr); //SERIAL_ECHOLNPGM("info.capacity.block_size: %d\n", info.capacity.block_size); //SERIAL_ECHOLNPGM("capacity : %d MB\n", capacity); } }; bool BulkStorage::LUNIsGood(uint8_t t) { return USBH_MSC_IsReady(&hUsbHost) && USBH_MSC_UnitIsReady(&hUsbHost, t); } uint32_t BulkStorage::GetCapacity(uint8_t lun) { return usb->block_count; } uint16_t BulkStorage::GetSectorSize(uint8_t lun) { return usb->block_size; } uint8_t BulkStorage::Read(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, uint8_t *buf) { return USBH_MSC_Read(&hUsbHost, lun, addr, buf, blocks) != USBH_OK; } uint8_t BulkStorage::Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, const uint8_t * buf) { return USBH_MSC_Write(&hUsbHost, lun, addr, const_cast<uint8_t*>(buf), blocks) != USBH_OK; } #endif // USE_OTG_USB_HOST && USBHOST #endif // HAL_STM32
2301_81045437/Marlin
Marlin/src/HAL/STM32/usb_host.cpp
C++
agpl-3.0
3,571
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <stdint.h> typedef enum { USB_STATE_INIT, USB_STATE_ERROR, USB_STATE_RUNNING, } usb_state_t; class USBHost { public: bool start(); void Task(); uint8_t getUsbTaskState(); void setUsbTaskState(uint8_t state); uint8_t regRd(uint8_t reg) { return 0x0; }; uint8_t usb_task_state = USB_STATE_INIT; uint8_t lun = 0; uint32_t capacity = 0; uint16_t block_size = 0; uint32_t block_count = 0; }; class BulkStorage { public: BulkStorage(USBHost *usb) : usb(usb) {}; bool LUNIsGood(uint8_t t); uint32_t GetCapacity(uint8_t lun); uint16_t GetSectorSize(uint8_t lun); uint8_t Read(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, uint8_t *buf); uint8_t Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, const uint8_t * buf); USBHost *usb; }; extern USBHost usb; extern BulkStorage bulk;
2301_81045437/Marlin
Marlin/src/HAL/STM32/usb_host.h
C++
agpl-3.0
1,732
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #include "../platforms.h" #ifdef HAL_STM32 #include "../../inc/MarlinConfigPre.h" #if ENABLED(EMERGENCY_PARSER) && (USBD_USE_CDC || USBD_USE_CDC_MSC) #include "usb_serial.h" #include "../../feature/e_parser.h" EmergencyParser::State emergency_state = EmergencyParser::State::EP_RESET; int8_t (*USBD_CDC_Receive_original) (uint8_t *Buf, uint32_t *Len) = nullptr; static int8_t USBD_CDC_Receive_hook(uint8_t *Buf, uint32_t *Len) { for (uint32_t i = 0; i < *Len; i++) emergency_parser.update(emergency_state, Buf[i]); return USBD_CDC_Receive_original(Buf, Len); } typedef struct _USBD_CDC_Itf { int8_t (* Init)(void); int8_t (* DeInit)(void); int8_t (* Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length); int8_t (* Receive)(uint8_t *Buf, uint32_t *Len); int8_t (* Transferred)(void); } USBD_CDC_ItfTypeDef; extern USBD_CDC_ItfTypeDef USBD_CDC_fops; void USB_Hook_init() { USBD_CDC_Receive_original = USBD_CDC_fops.Receive; USBD_CDC_fops.Receive = USBD_CDC_Receive_hook; } #endif // EMERGENCY_PARSER && USBD_USE_CDC #endif // HAL_STM32
2301_81045437/Marlin
Marlin/src/HAL/STM32/usb_serial.cpp
C++
agpl-3.0
1,931
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once void USB_Hook_init();
2301_81045437/Marlin
Marlin/src/HAL/STM32/usb_serial.h
C
agpl-3.0
898
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #include "HAL.h" #include "adc.h" uint16_t adc_results[ADC_COUNT]; // ------------------------ // Serial ports // ------------------------ #if defined(SERIAL_USB) && !HAS_SD_HOST_DRIVE USBSerial SerialUSB; DefaultSerial1 MSerial0(true, SerialUSB); #if ENABLED(EMERGENCY_PARSER) #include "../libmaple/usb/stm32f1/usb_reg_map.h" #include "libmaple/usb_cdcacm.h" // The original callback is not called (no way to retrieve address). // That callback detects a special STM32 reset sequence: this functionality is not essential // as M997 achieves the same. void my_rx_callback(unsigned int, void*) { // max length of 16 is enough to contain all emergency commands uint8 buf[16]; //rx is usbSerialPart.endpoints[2] uint16 len = usb_get_ep_rx_count(USB_CDCACM_RX_ENDP); uint32 total = usb_cdcacm_data_available(); if (len == 0 || total == 0 || !WITHIN(total, len, COUNT(buf))) return; // cannot get character by character due to bug in composite_cdcacm_peek_ex len = usb_cdcacm_peek(buf, total); for (uint32 i = 0; i < len; i++) emergency_parser.update(MSerial0.emergency_state, buf[i + total - len]); } #endif #endif // ------------------------ // Watchdog Timer // ------------------------ #if ENABLED(USE_WATCHDOG) #include <libmaple/iwdg.h> void watchdogSetup() { // do whatever. don't remove this function. } /** * The watchdog clock is 40Khz. So for a 4s or 8s interval use a /256 preescaler and 625 or 1250 reload value (counts down to 0). */ #define STM32F1_WD_RELOAD TERN(WATCHDOG_DURATION_8S, 1250, 625) // 4 or 8 second timeout /** * @brief Initialize the independent hardware watchdog. * * @return No return * * @details The watchdog clock is 40Khz. So for a 4s or 8s interval use a /256 preescaler and 625 or 1250 reload value (counts down to 0). */ void MarlinHAL::watchdog_init() { #if DISABLED(DISABLE_WATCHDOG_INIT) iwdg_init(IWDG_PRE_256, STM32F1_WD_RELOAD); #endif } // Reset watchdog. MUST be called every 4 or 8 seconds after the // first watchdog_init or the STM32F1 will reset. void MarlinHAL::watchdog_refresh() { #if DISABLED(PINS_DEBUGGING) && PIN_EXISTS(LED) TOGGLE(LED_PIN); // heartbeat indicator #endif iwdg_feed(); } #endif // USE_WATCHDOG // ------------------------ // ADC // ------------------------ // Watch out for recursion here! Our pin_t is signed, so pass through to Arduino -> analogRead(uint8_t) uint16_t analogRead(const pin_t pin) { const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG; return is_analog ? analogRead(uint8_t(pin)) : 0; } // Wrapper to maple unprotected analogWrite void analogWrite(const pin_t pin, int pwm_val8) { if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8); } uint16_t MarlinHAL::adc_result; #ifndef VOXELAB_N32 #include <STM32ADC.h> // Init the AD in continuous capture mode void MarlinHAL::adc_init() { static const uint8_t adc_pins[] = { OPTITEM(HAS_TEMP_ADC_0, TEMP_0_PIN) OPTITEM(HAS_TEMP_ADC_1, TEMP_1_PIN) OPTITEM(HAS_TEMP_ADC_2, TEMP_2_PIN) OPTITEM(HAS_TEMP_ADC_3, TEMP_3_PIN) OPTITEM(HAS_TEMP_ADC_4, TEMP_4_PIN) OPTITEM(HAS_TEMP_ADC_5, TEMP_5_PIN) OPTITEM(HAS_TEMP_ADC_6, TEMP_6_PIN) OPTITEM(HAS_TEMP_ADC_7, TEMP_7_PIN) OPTITEM(HAS_HEATED_BED, TEMP_BED_PIN) OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER_PIN) OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE_PIN) OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER_PIN) OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD_PIN) OPTITEM(HAS_TEMP_SOC, TEMP_SOC_PIN) OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH_PIN) OPTITEM(HAS_ADC_BUTTONS, ADC_KEYPAD_PIN) OPTITEM(HAS_JOY_ADC_X, JOY_X_PIN) OPTITEM(HAS_JOY_ADC_Y, JOY_Y_PIN) OPTITEM(HAS_JOY_ADC_Z, JOY_Z_PIN) OPTITEM(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN) OPTITEM(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN) }; static STM32ADC adc(ADC1); // Configure the ADC adc.calibrate(); adc.setSampleRate((F_CPU > 72000000) ? ADC_SMPR_71_5 : ADC_SMPR_41_5); // 71.5 or 41.5 ADC cycles adc.setPins((uint8_t *)adc_pins, ADC_COUNT); adc.setDMA(adc_results, uint16_t(ADC_COUNT), uint32_t(DMA_MINC_MODE | DMA_CIRC_MODE), nullptr); adc.setScanMode(); adc.setContinuous(); adc.startConversion(); } #endif // !VOXELAB_N32 void MarlinHAL::adc_start(const pin_t pin) { #define __TCASE(N,I) case N: pin_index = I; break; #define _TCASE(C,N,I) TERN_(C, __TCASE(N, I)) ADCIndex pin_index; switch (pin) { default: return; _TCASE(HAS_TEMP_ADC_0, TEMP_0_PIN, TEMP_0) _TCASE(HAS_TEMP_ADC_1, TEMP_1_PIN, TEMP_1) _TCASE(HAS_TEMP_ADC_2, TEMP_2_PIN, TEMP_2) _TCASE(HAS_TEMP_ADC_3, TEMP_3_PIN, TEMP_3) _TCASE(HAS_TEMP_ADC_4, TEMP_4_PIN, TEMP_4) _TCASE(HAS_TEMP_ADC_5, TEMP_5_PIN, TEMP_5) _TCASE(HAS_TEMP_ADC_6, TEMP_6_PIN, TEMP_6) _TCASE(HAS_TEMP_ADC_7, TEMP_7_PIN, TEMP_7) _TCASE(HAS_HEATED_BED, TEMP_BED_PIN, TEMP_BED) _TCASE(HAS_TEMP_CHAMBER, TEMP_CHAMBER_PIN, TEMP_CHAMBER) _TCASE(HAS_TEMP_ADC_PROBE, TEMP_PROBE_PIN, TEMP_PROBE) _TCASE(HAS_TEMP_COOLER, TEMP_COOLER_PIN, TEMP_COOLER) _TCASE(HAS_TEMP_BOARD, TEMP_BOARD_PIN, TEMP_BOARD) _TCASE(HAS_TEMP_SOC, TEMP_SOC_PIN, TEMP_SOC) _TCASE(HAS_JOY_ADC_X, JOY_X_PIN, JOY_X) _TCASE(HAS_JOY_ADC_Y, JOY_Y_PIN, JOY_Y) _TCASE(HAS_JOY_ADC_Z, JOY_Z_PIN, JOY_Z) _TCASE(FILAMENT_WIDTH_SENSOR, FILWIDTH_PIN, FILWIDTH) _TCASE(HAS_ADC_BUTTONS, ADC_KEYPAD_PIN, ADC_KEY) _TCASE(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN, POWERMON_CURRENT) _TCASE(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN, POWERMON_VOLTAGE) } adc_result = (adc_results[(int)pin_index] & 0xFFF) >> (12 - HAL_ADC_RESOLUTION); // shift out unused bits } // ------------------------ // Public functions // ------------------------ void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) { uint32_t reg_value; uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07); // only values 0..7 are used reg_value = SCB->AIRCR; // read old register configuration reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk); // clear bits to change reg_value = (reg_value | ((uint32_t)0x5FA << SCB_AIRCR_VECTKEY_Pos) | (PriorityGroupTmp << 8)); // Insert write key & priority group SCB->AIRCR = reg_value; } void flashFirmware(const int16_t) { hal.reboot(); } // // Leave PA11/PA12 intact if USBSerial is not used // #if SERIAL_USB namespace wirish { namespace priv { #if SERIAL_PORT > 0 #if SERIAL_PORT2 #if SERIAL_PORT2 > 0 void board_setup_usb() {} #endif #else void board_setup_usb() {} #endif #endif } } #endif TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial()); // ------------------------ // MarlinHAL class // ------------------------ void MarlinHAL::init() { NVIC_SetPriorityGrouping(0x3); #if PIN_EXISTS(LED) OUT_WRITE(LED_PIN, LOW); #endif #if HAS_SD_HOST_DRIVE MSC_SD_init(); #elif ALL(SERIAL_USB, EMERGENCY_PARSER) usb_cdcacm_set_hooks(USB_CDCACM_HOOK_RX, my_rx_callback); #endif #if PIN_EXISTS(USB_CONNECT) OUT_WRITE(USB_CONNECT_PIN, !USB_CONNECT_INVERTING); // USB clear connection delay(1000); // Give OS time to notice WRITE(USB_CONNECT_PIN, USB_CONNECT_INVERTING); #endif TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the minimal serial handler } // HAL idle task void MarlinHAL::idletask() { #if HAS_SHARED_MEDIA /** * When Marlin is using the SD card it should be locked to prevent it being * accessed from a PC over USB. * Other HALs use (IS_SD_PRINTING() || IS_SD_FILE_OPEN()) to check for access * but this won't reliably detect other file operations. To be safe we just lock * the drive whenever Marlin has it mounted. LCDs should include an Unmount * command so drives can be released as needed. */ /* Copied from LPC1768 framework. Should be fixed later to process HAS_SD_HOST_DRIVE */ //if (!drive_locked()) // TODO MarlinMSC.loop(); // Process USB mass storage device class loop #endif } void MarlinHAL::reboot() { nvic_sys_reset(); } #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/HAL.cpp
C++
agpl-3.0
9,847
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) */ #define CPU_32_BIT #include "../../core/macros.h" #include "../shared/Marduino.h" #include "../shared/math_32bit.h" #include "../shared/HAL_SPI.h" #include "fastio.h" #include <stdint.h> #include <util/atomic.h> #include "../../inc/MarlinConfigPre.h" #if HAS_SD_HOST_DRIVE #include "msc_sd.h" #endif #include "MarlinSerial.h" // ------------------------ // Defines // ------------------------ // // Default graphical display delays // #define CPU_ST7920_DELAY_1 300 #define CPU_ST7920_DELAY_2 40 #define CPU_ST7920_DELAY_3 340 #ifndef STM32_FLASH_SIZE #if ANY(MCU_STM32F103RE, MCU_STM32F103VE, MCU_STM32F103ZE) #define STM32_FLASH_SIZE 512 #else #define STM32_FLASH_SIZE 256 #endif #endif // ------------------------ // Serial ports // ------------------------ #ifdef SERIAL_USB typedef ForwardSerial1Class< USBSerial > DefaultSerial1; extern DefaultSerial1 MSerial0; #if HAS_SD_HOST_DRIVE #define UsbSerial MarlinCompositeSerial #else #define UsbSerial MSerial0 #endif #endif #define _MSERIAL(X) MSerial##X #define MSERIAL(X) _MSERIAL(X) #if ANY(STM32_HIGH_DENSITY, STM32_XL_DENSITY) #define NUM_UARTS 5 #else #define NUM_UARTS 3 #endif #if SERIAL_PORT == -1 #define MYSERIAL1 UsbSerial #elif WITHIN(SERIAL_PORT, 1, NUM_UARTS) #define MYSERIAL1 MSERIAL(SERIAL_PORT) #else #define MYSERIAL1 MSERIAL(1) // dummy port static_assert(false, "SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.") #endif #ifdef SERIAL_PORT_2 #if SERIAL_PORT_2 == -1 #define MYSERIAL2 UsbSerial #elif WITHIN(SERIAL_PORT_2, 1, NUM_UARTS) #define MYSERIAL2 MSERIAL(SERIAL_PORT_2) #else #define MYSERIAL2 MSERIAL(1) // dummy port static_assert(false, "SERIAL_PORT_2 must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.") #endif #endif #ifdef SERIAL_PORT_3 #if SERIAL_PORT_3 == -1 #define MYSERIAL3 UsbSerial #elif WITHIN(SERIAL_PORT_3, 1, NUM_UARTS) #define MYSERIAL3 MSERIAL(SERIAL_PORT_3) #else #define MYSERIAL3 MSERIAL(1) // dummy port static_assert(false, "SERIAL_PORT_3 must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.") #endif #endif #ifdef MMU2_SERIAL_PORT #if MMU2_SERIAL_PORT == -1 #define MMU2_SERIAL UsbSerial #elif WITHIN(MMU2_SERIAL_PORT, 1, NUM_UARTS) #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT) #else #define MMU2_SERIAL MSERIAL(1) // dummy port static_assert(false, "MMU2_SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.") #endif #endif #ifdef LCD_SERIAL_PORT #if LCD_SERIAL_PORT == -1 #define LCD_SERIAL UsbSerial #elif WITHIN(LCD_SERIAL_PORT, 1, NUM_UARTS) #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT) #else #define LCD_SERIAL MSERIAL(1) // dummy port static_assert(false, "LCD_SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.") #endif #if HAS_DGUS_LCD #define LCD_SERIAL_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite() #endif #endif /** * TODO: review this to return 1 for pins that are not analog input */ #ifndef analogInputToDigitalPin #define analogInputToDigitalPin(p) (p) #endif #ifndef digitalPinHasPWM #define digitalPinHasPWM(P) !!PIN_MAP[P].timer_device #define NO_COMPILE_TIME_PWM #endif // Reset Reason #define RST_POWER_ON 1 #define RST_EXTERNAL 2 #define RST_BROWN_OUT 4 #define RST_WATCHDOG 8 #define RST_JTAG 16 #define RST_SOFTWARE 32 #define RST_BACKUP 64 // ------------------------ // Types // ------------------------ typedef int8_t pin_t; // ------------------------ // Interrupts // ------------------------ #define CRITICAL_SECTION_START() const bool irqon = !__get_primask(); (void)__iCliRetVal() #define CRITICAL_SECTION_END() if (!irqon) (void)__iSeiRetVal() #define cli() noInterrupts() #define sei() interrupts() // ------------------------ // ADC // ------------------------ #ifdef ADC_RESOLUTION #define HAL_ADC_RESOLUTION ADC_RESOLUTION #else #define HAL_ADC_RESOLUTION 12 #endif #define HAL_ADC_VREF_MV 3300 uint16_t analogRead(const pin_t pin); // need hal.adc_enable() first void analogWrite(const pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!? // // Pin Mapping for M42, M43, M226 // #define GET_PIN_MAP_PIN(index) index #define GET_PIN_MAP_INDEX(pin) pin #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval) #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY) #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE) #ifndef PLATFORM_M997_SUPPORT #define PLATFORM_M997_SUPPORT #endif void flashFirmware(const int16_t); #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment #ifndef PWM_FREQUENCY #define PWM_FREQUENCY 1000 // Default PWM Frequency #endif // ------------------------ // Class Utilities // ------------------------ // Memory related #define __bss_end __bss_end__ extern "C" char* _sbrk(int incr); void NVIC_SetPriorityGrouping(uint32_t PriorityGroup); #pragma GCC diagnostic push #if GCC_VERSION <= 50000 #pragma GCC diagnostic ignored "-Wunused-function" #endif static inline int freeMemory() { volatile char top; return &top - _sbrk(0); } #pragma GCC diagnostic pop // ------------------------ // MarlinHAL Class // ------------------------ class MarlinHAL { public: // Earliest possible init, before setup() MarlinHAL() {} // Watchdog static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {}); static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {}); static void init(); // Called early in setup() static void init_board() {} // Called less early in setup() static void reboot(); // Restart the firmware from 0x0 // Interrupts static bool isr_state() { return !__get_primask(); } static void isr_on() { ((void)__iSeiRetVal()); } static void isr_off() { ((void)__iCliRetVal()); } static void delay_ms(const int ms) { delay(ms); } // Tasks, called from idle() static void idletask(); // Reset static uint8_t get_reset_source() { return RST_POWER_ON; } static void clear_reset_source() {} // Free SRAM static int freeMemory() { return ::freeMemory(); } // // ADC Methods // static uint16_t adc_result; // Called by Temperature::init once at startup static void adc_init(); // Called by Temperature::init for each sensor at startup static void adc_enable(const pin_t pin) { pinMode(pin, INPUT_ANALOG); } // Begin ADC sampling on the given pin. Called from Temperature::isr! static void adc_start(const pin_t pin); // Is the ADC ready for reading? static bool adc_ready() { return true; } // The current value of the ADC register static uint16_t adc_value() { return adc_result; } /** * Set the PWM duty cycle for the pin to the given value. * Optionally invert the duty cycle [default = false] * Optionally change the maximum size of the provided value to enable finer PWM duty control [default = 255] * The timer must be pre-configured with set_pwm_frequency() if the default frequency is not desired. */ static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false); /** * Set the frequency of the timer for the given pin. * All Timer PWM pins run at the same frequency. */ static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired); }; // ------------------------ // Types // ------------------------ #define __I #define __IO volatile typedef struct { __I uint32_t CPUID; /*!< Offset: 0x000 (R/ ) CPUID Base Register */ __IO uint32_t ICSR; /*!< Offset: 0x004 (R/W) Interrupt Control and State Register */ __IO uint32_t VTOR; /*!< Offset: 0x008 (R/W) Vector Table Offset Register */ __IO uint32_t AIRCR; /*!< Offset: 0x00C (R/W) Application Interrupt and Reset Control Register */ __IO uint32_t SCR; /*!< Offset: 0x010 (R/W) System Control Register */ __IO uint32_t CCR; /*!< Offset: 0x014 (R/W) Configuration Control Register */ __IO uint8_t SHP[12]; /*!< Offset: 0x018 (R/W) System Handlers Priority Registers (4-7, 8-11, 12-15) */ __IO uint32_t SHCSR; /*!< Offset: 0x024 (R/W) System Handler Control and State Register */ __IO uint32_t CFSR; /*!< Offset: 0x028 (R/W) Configurable Fault Status Register */ __IO uint32_t HFSR; /*!< Offset: 0x02C (R/W) HardFault Status Register */ __IO uint32_t DFSR; /*!< Offset: 0x030 (R/W) Debug Fault Status Register */ __IO uint32_t MMFAR; /*!< Offset: 0x034 (R/W) MemManage Fault Address Register */ __IO uint32_t BFAR; /*!< Offset: 0x038 (R/W) BusFault Address Register */ __IO uint32_t AFSR; /*!< Offset: 0x03C (R/W) Auxiliary Fault Status Register */ __I uint32_t PFR[2]; /*!< Offset: 0x040 (R/ ) Processor Feature Register */ __I uint32_t DFR; /*!< Offset: 0x048 (R/ ) Debug Feature Register */ __I uint32_t ADR; /*!< Offset: 0x04C (R/ ) Auxiliary Feature Register */ __I uint32_t MMFR[4]; /*!< Offset: 0x050 (R/ ) Memory Model Feature Register */ __I uint32_t ISAR[5]; /*!< Offset: 0x060 (R/ ) Instruction Set Attributes Register */ uint32_t RESERVED0[5]; __IO uint32_t CPACR; /*!< Offset: 0x088 (R/W) Coprocessor Access Control Register */ } SCB_Type; // ------------------------ // System Control Space // ------------------------ #define SCS_BASE (0xE000E000UL) /*!< System Control Space Base Address */ #define SCB_BASE (SCS_BASE + 0x0D00UL) /*!< System Control Block Base Address */ #define SCB ((SCB_Type *) SCB_BASE ) /*!< SCB configuration struct */ /* SCB Application Interrupt and Reset Control Register Definitions */ #define SCB_AIRCR_VECTKEY_Pos 16 /*!< SCB AIRCR: VECTKEY Position */ #define SCB_AIRCR_VECTKEY_Msk (0xFFFFUL << SCB_AIRCR_VECTKEY_Pos) /*!< SCB AIRCR: VECTKEY Mask */ #define SCB_AIRCR_PRIGROUP_Pos 8 /*!< SCB AIRCR: PRIGROUP Position */ #define SCB_AIRCR_PRIGROUP_Msk (7UL << SCB_AIRCR_PRIGROUP_Pos) /*!< SCB AIRCR: PRIGROUP Mask */
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/HAL.h
C++
agpl-3.0
12,179
/** * Marlin 3D Printer Firmware * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) * Specifically for VOXELAB_N32. TODO: Rework for generic N32 MCU. */ #if defined(__STM32F1__) && defined(VOXELAB_N32) #include "../../inc/MarlinConfig.h" #include "HAL_N32.h" #include "HAL.h" #include "adc.h" void ADC_Init(ADC_Module* NS_ADCx, ADC_InitType* ADC_InitStruct) { uint32_t tmpreg1 = 0; uint8_t tmpreg2 = 0; /*---------------------------- ADCx CTRL1 Configuration -----------------*/ /* Get the ADCx CTRL1 value */ tmpreg1 = NS_ADCx->CTRL1; /* Clear DUALMOD and SCAN bits */ tmpreg1 &= CTRL1_CLR_MASK; /* Configure ADCx: Dual mode and scan conversion mode */ /* Set DUALMOD bits according to WorkMode value */ /* Set SCAN bit according to MultiChEn value */ tmpreg1 |= (uint32_t)(ADC_InitStruct->WorkMode | ((uint32_t)ADC_InitStruct->MultiChEn << 8)); /* Write to ADCx CTRL1 */ NS_ADCx->CTRL1 = tmpreg1; /*---------------------------- ADCx CTRL2 Configuration -----------------*/ /* Get the ADCx CTRL2 value */ tmpreg1 = NS_ADCx->CTRL2; /* Clear CONT, ALIGN and EXTSEL bits */ tmpreg1 &= CTRL2_CLR_MASK; /* Configure ADCx: external trigger event and continuous conversion mode */ /* Set ALIGN bit according to DatAlign value */ /* Set EXTSEL bits according to ExtTrigSelect value */ /* Set CONT bit according to ContinueConvEn value */ tmpreg1 |= (uint32_t)(ADC_InitStruct->DatAlign | ADC_InitStruct->ExtTrigSelect | ((uint32_t)ADC_InitStruct->ContinueConvEn << 1)); /* Write to ADCx CTRL2 */ NS_ADCx->CTRL2 = tmpreg1; /*---------------------------- ADCx RSEQ1 Configuration -----------------*/ /* Get the ADCx RSEQ1 value */ tmpreg1 = NS_ADCx->RSEQ1; /* Clear L bits */ tmpreg1 &= RSEQ1_CLR_MASK; /* Configure ADCx: regular channel sequence length */ /* Set L bits according to ChsNumber value */ tmpreg2 |= (uint8_t)(ADC_InitStruct->ChsNumber - (uint8_t)1); tmpreg1 |= (uint32_t)tmpreg2 << 20; /* Write to ADCx RSEQ1 */ NS_ADCx->RSEQ1 = tmpreg1; } /**================================================================ * ADC reset ================================================================*/ void ADC_DeInit(ADC_Module* NS_ADCx) { uint32_t reg_temp; if (NS_ADCx == NS_ADC1) { /* Enable ADC1 reset state */ reg_temp = ADC_RCC_AHBPRST; reg_temp |= RCC_AHB_PERIPH_ADC1; ADC_RCC_AHBPRST = reg_temp; // ADC module reunion position ADC_RCC_AHBPRST = 0x00000000; // ADC module reset and clear } else if (NS_ADCx == NS_ADC2) { /* Enable ADC2 reset state */ reg_temp = ADC_RCC_AHBPRST; reg_temp |= RCC_AHB_PERIPH_ADC2; ADC_RCC_AHBPRST = reg_temp; // ADC module reunion position ADC_RCC_AHBPRST = 0x00000000; // ADC module reset and clear } else if (NS_ADCx == NS_ADC3) { /* Enable ADC2 reset state */ reg_temp = ADC_RCC_AHBPRST; reg_temp |= RCC_AHB_PERIPH_ADC3; ADC_RCC_AHBPRST = reg_temp; // ADC module reunion position ADC_RCC_AHBPRST = 0x00000000; // ADC module reset and clear } else if (NS_ADCx == NS_ADC4) { /* Enable ADC3 reset state */ reg_temp = ADC_RCC_AHBPRST; reg_temp |= RCC_AHB_PERIPH_ADC4; ADC_RCC_AHBPRST = reg_temp; // ADC module reunion position ADC_RCC_AHBPRST = 0x00000000; // ADC module reset and clear } } /**================================================================ * ADC module enable ================================================================*/ void ADC_Enable(ADC_Module* NS_ADCx, uint32_t Cmd) { if (Cmd) /* Set the AD_ON bit to wake up the ADC from power down mode */ NS_ADCx->CTRL2 |= CTRL2_AD_ON_SET; else /* Disable the selected ADC peripheral */ NS_ADCx->CTRL2 &= CTRL2_AD_ON_RESET; } /**================================================================ * Get the ADC status logo bit ================================================================*/ uint32_t ADC_GetFlagStatusNew(ADC_Module* NS_ADCx, uint8_t ADC_FLAG_NEW) { uint32_t bitstatus = 0; /* Check the status of the specified ADC flag */ if ((NS_ADCx->CTRL3 & ADC_FLAG_NEW) != (uint8_t)0) /* ADC_FLAG_NEW is set */ bitstatus = 1; else /* ADC_FLAG_NEW is reset */ bitstatus = 0; /* Return the ADC_FLAG_NEW status */ return bitstatus; } /**================================================================ * Open ADC calibration ================================================================*/ void ADC_StartCalibration(ADC_Module* NS_ADCx) { /* Enable the selected ADC calibration process */ if (NS_ADCx->CALFACT == 0) NS_ADCx->CTRL2 |= CTRL2_CAL_SET; } /**================================================================ * Enable ADC DMA ================================================================*/ void ADC_EnableDMA(ADC_Module* NS_ADCx, uint32_t Cmd) { if (Cmd != 0) /* Enable the selected ADC DMA request */ NS_ADCx->CTRL2 |= CTRL2_DMA_SET; else /* Disable the selected ADC DMA request */ NS_ADCx->CTRL2 &= CTRL2_DMA_RESET; } /**================================================================ * Configure ADC interrupt enable enable ================================================================*/ void ADC_ConfigInt(ADC_Module* NS_ADCx, uint16_t ADC_IT, uint32_t Cmd) { uint8_t itmask = 0; /* Get the ADC IT index */ itmask = (uint8_t)ADC_IT; if (Cmd != 0) /* Enable the selected ADC interrupts */ NS_ADCx->CTRL1 |= itmask; else /* Disable the selected ADC interrupts */ NS_ADCx->CTRL1 &= (~(uint32_t)itmask); } /**================================================================ * Get ADC calibration status ================================================================*/ uint32_t ADC_GetCalibrationStatus(ADC_Module* NS_ADCx) { uint32_t bitstatus = 0; /* Check the status of CAL bit */ if ((NS_ADCx->CTRL2 & CTRL2_CAL_SET) != (uint32_t)0) /* CAL bit is set: calibration on going */ bitstatus = 1; else /* CAL bit is reset: end of calibration */ bitstatus = 0; if (NS_ADCx->CALFACT != 0) bitstatus = 0; /* Return the CAL bit status */ return bitstatus; } /**================================================================ * Configure the ADC channel ================================================================*/ void ADC_ConfigRegularChannel(ADC_Module* NS_ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime) { uint32_t tmpreg1 = 0, tmpreg2 = 0; if (ADC_Channel == ADC_CH_18) { tmpreg1 = NS_ADCx->SAMPT3; tmpreg1 &= (~0x00000007); tmpreg1 |= ADC_SampleTime; NS_ADCx->SAMPT3 = tmpreg1; } else if (ADC_Channel > ADC_CH_9) { /* if ADC_CH_10 ... ADC_CH_17 is selected */ /* Get the old register value */ tmpreg1 = NS_ADCx->SAMPT1; /* Calculate the mask to clear */ tmpreg2 = SAMPT1_SMP_SET << (3 * (ADC_Channel - 10)); /* Clear the old channel sample time */ tmpreg1 &= ~tmpreg2; /* Calculate the mask to set */ tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10)); /* Set the new channel sample time */ tmpreg1 |= tmpreg2; /* Store the new register value */ NS_ADCx->SAMPT1 = tmpreg1; } else { /* ADC_Channel include in ADC_Channel_[0..9] */ /* Get the old register value */ tmpreg1 = NS_ADCx->SAMPT2; /* Calculate the mask to clear */ tmpreg2 = SAMPT2_SMP_SET << (3 * ADC_Channel); /* Clear the old channel sample time */ tmpreg1 &= ~tmpreg2; /* Calculate the mask to set */ tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel); /* Set the new channel sample time */ tmpreg1 |= tmpreg2; /* Store the new register value */ NS_ADCx->SAMPT2 = tmpreg1; } /* For Rank 1 to 6 */ if (Rank < 7) { /* Get the old register value */ tmpreg1 = NS_ADCx->RSEQ3; /* Calculate the mask to clear */ tmpreg2 = SQR3_SEQ_SET << (5 * (Rank - 1)); /* Clear the old SQx bits for the selected rank */ tmpreg1 &= ~tmpreg2; /* Calculate the mask to set */ tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1)); /* Set the SQx bits for the selected rank */ tmpreg1 |= tmpreg2; /* Store the new register value */ NS_ADCx->RSEQ3 = tmpreg1; } /* For Rank 7 to 12 */ else if (Rank < 13) { /* Get the old register value */ tmpreg1 = NS_ADCx->RSEQ2; /* Calculate the mask to clear */ tmpreg2 = SQR2_SEQ_SET << (5 * (Rank - 7)); /* Clear the old SQx bits for the selected rank */ tmpreg1 &= ~tmpreg2; /* Calculate the mask to set */ tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7)); /* Set the SQx bits for the selected rank */ tmpreg1 |= tmpreg2; /* Store the new register value */ NS_ADCx->RSEQ2 = tmpreg1; } /* For Rank 13 to 16 */ else { /* Get the old register value */ tmpreg1 = NS_ADCx->RSEQ1; /* Calculate the mask to clear */ tmpreg2 = SQR1_SEQ_SET << (5 * (Rank - 13)); /* Clear the old SQx bits for the selected rank */ tmpreg1 &= ~tmpreg2; /* Calculate the mask to set */ tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13)); /* Set the SQx bits for the selected rank */ tmpreg1 |= tmpreg2; /* Store the new register value */ NS_ADCx->RSEQ1 = tmpreg1; } } /**================================================================ * Start ADC conversion ================================================================*/ void ADC_EnableSoftwareStartConv(ADC_Module* NS_ADCx, uint32_t Cmd) { if (Cmd != 0) /* Enable the selected ADC conversion on external event and start the selected ADC conversion */ NS_ADCx->CTRL2 |= CTRL2_EXT_TRIG_SWSTART_SET; else /* Disable the selected ADC conversion on external event and stop the selected ADC conversion */ NS_ADCx->CTRL2 &= CTRL2_EXT_TRIG_SWSTART_RESET; } /**================================================================ * Get the ADC status logo bit ================================================================*/ uint32_t ADC_GetFlagStatus(ADC_Module* NS_ADCx, uint8_t ADC_FLAG) { uint32_t bitstatus = 0; /* Check the status of the specified ADC flag */ if ((NS_ADCx->STS & ADC_FLAG) != (uint8_t)0) /* ADC_FLAG is set */ bitstatus = 1; else /* ADC_FLAG is reset */ bitstatus = 0; /* Return the ADC_FLAG status */ return bitstatus; } /**================================================================ * Clear status logo bit ================================================================*/ void ADC_ClearFlag(ADC_Module* NS_ADCx, uint8_t ADC_FLAG) { /* Clear the selected ADC flags */ NS_ADCx->STS &= ~(uint32_t)ADC_FLAG; } /**================================================================ * Get ADC sampling value ================================================================*/ uint16_t ADC_GetDat(ADC_Module* NS_ADCx) { /* Return the selected ADC conversion value */ return (uint16_t)NS_ADCx->DAT; } /**================================================================ * Initialize ADC clock ================================================================*/ void enable_adc_clk(uint8_t cmd) { uint32_t reg_temp; if (cmd) { /** Make PWR clock */ reg_temp = ADC_RCC_APB1PCLKEN; reg_temp |= RCC_APB1Periph_PWR; ADC_RCC_APB1PCLKEN = reg_temp; /** Enable expansion mode */ reg_temp = NS_PWR_CR3; reg_temp |= 0x00000001; NS_PWR_CR3 = reg_temp; /** Make ADC clock */ reg_temp = ADC_RCC_AHBPCLKEN; reg_temp |= ( RCC_AHB_PERIPH_ADC1 | RCC_AHB_PERIPH_ADC2 | RCC_AHB_PERIPH_ADC3 | RCC_AHB_PERIPH_ADC4 ); ADC_RCC_AHBPCLKEN = reg_temp; /** Reset */ reg_temp = ADC_RCC_AHBPRST; reg_temp |= ( RCC_AHB_PERIPH_ADC1 | RCC_AHB_PERIPH_ADC2 | RCC_AHB_PERIPH_ADC3 | RCC_AHB_PERIPH_ADC4 ); ADC_RCC_AHBPRST = reg_temp; // ADC module reunion position ADC_RCC_AHBPRST &= ~reg_temp; // ADC module reset and clear /** Set ADC 1M clock */ reg_temp = ADC_RCC_CFG2; reg_temp &= CFG2_ADC1MSEL_RESET_MASK; // HSI as an ADC 1M clock reg_temp &= CFG2_ADC1MPRES_RESET_MASK; reg_temp |= 7 << 11; // Adc1m 8m / 8 = 1m /** Set the ADC PLL frequency split coefficient */ reg_temp &= CFG2_ADCPLLPRES_RESET_MASK; reg_temp |= RCC_ADCPLLCLK_DIV4; // ADC PLL frequency split coefficient /** Set the ADC HCLK frequency frequency coefficient */ reg_temp &= CFG2_ADCHPRES_RESET_MASK; reg_temp |= RCC_ADCHCLK_DIV4; // ADC HCLK frequency split coefficient ADC_RCC_CFG2 = reg_temp; // Write to register } else { /** Turn off the ADC clock */ reg_temp = ADC_RCC_AHBPCLKEN; reg_temp &= ~( RCC_AHB_PERIPH_ADC1 | RCC_AHB_PERIPH_ADC2 | RCC_AHB_PERIPH_ADC3 | RCC_AHB_PERIPH_ADC4 ); ADC_RCC_AHBPCLKEN = reg_temp; } } /**================================================================ * Initialize ADC peripheral parameters ================================================================*/ void ADC_Initial(ADC_Module* NS_ADCx) { ADC_InitType ADC_InitStructure; /* ADC configuration ------------------------------------------------------*/ ADC_InitStructure.WorkMode = ADC_WORKMODE_INDEPENDENT; // Independent mode ADC_InitStructure.MultiChEn = 1; // Multi-channel enable ADC_InitStructure.ContinueConvEn = 1; // Continuous enable ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_NONE; // Non-trigger ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R; // Right alignment ADC_InitStructure.ChsNumber = 2; // Scan channel number ADC_Init(NS_ADCx, &ADC_InitStructure); /* ADC regular channel14 configuration */ ADC_ConfigRegularChannel(NS_ADCx, ADC2_Channel_05_PC4, 2, ADC_SAMP_TIME_55CYCLES5); ADC_ConfigRegularChannel(NS_ADCx, ADC2_Channel_12_PC5, 1, ADC_SAMP_TIME_55CYCLES5); /** 使能ADC DMA */ ADC_EnableDMA(NS_ADCx, 1); /* Enable ADC */ ADC_Enable(NS_ADCx, 1); while(ADC_GetFlagStatusNew(NS_ADCx, ADC_FLAG_RDY) == 0); /* Start ADC calibration */ ADC_StartCalibration(NS_ADCx); while (ADC_GetCalibrationStatus(NS_ADCx)); /* Start ADC Software Conversion */ ADC_EnableSoftwareStartConv(NS_ADCx, 1); } /**================================================================ * Single independent sampling ================================================================*/ uint16_t ADC_GetData(ADC_Module* NS_ADCx, uint8_t ADC_Channel) { uint16_t dat; /** Set channel parameters */ ADC_ConfigRegularChannel(NS_ADCx, ADC_Channel, 1, ADC_SAMP_TIME_239CYCLES5); /* Start ADC Software Conversion */ ADC_EnableSoftwareStartConv(NS_ADCx, 1); while(ADC_GetFlagStatus(NS_ADCx, ADC_FLAG_ENDC) == 0); ADC_ClearFlag(NS_ADCx, ADC_FLAG_ENDC); ADC_ClearFlag(NS_ADCx, ADC_FLAG_STR); dat = ADC_GetDat(NS_ADCx); return dat; } void DMA_DeInit(DMA_ChannelType* DMAyChx) { /* Disable the selected DMAy Channelx */ DMAyChx->CHCFG &= (uint16_t)(~DMA_CHCFG1_CHEN); /* Reset DMAy Channelx control register */ DMAyChx->CHCFG = 0; /* Reset DMAy Channelx remaining bytes register */ DMAyChx->TXNUM = 0; /* Reset DMAy Channelx peripheral address register */ DMAyChx->PADDR = 0; /* Reset DMAy Channelx memory address register */ DMAyChx->MADDR = 0; if (DMAyChx == DMA1_CH1) { /* Reset interrupt pending bits for DMA1 Channel1 */ DMA1->INTCLR |= DMA1_CH1_INT_MASK; } else if (DMAyChx == DMA1_CH2) { /* Reset interrupt pending bits for DMA1 Channel2 */ DMA1->INTCLR |= DMA1_CH2_INT_MASK; } else if (DMAyChx == DMA1_CH3) { /* Reset interrupt pending bits for DMA1 Channel3 */ DMA1->INTCLR |= DMA1_CH3_INT_MASK; } else if (DMAyChx == DMA1_CH4) { /* Reset interrupt pending bits for DMA1 Channel4 */ DMA1->INTCLR |= DMA1_CH4_INT_MASK; } else if (DMAyChx == DMA1_CH5) { /* Reset interrupt pending bits for DMA1 Channel5 */ DMA1->INTCLR |= DMA1_CH5_INT_MASK; } else if (DMAyChx == DMA1_CH6) { /* Reset interrupt pending bits for DMA1 Channel6 */ DMA1->INTCLR |= DMA1_CH6_INT_MASK; } else if (DMAyChx == DMA1_CH7) { /* Reset interrupt pending bits for DMA1 Channel7 */ DMA1->INTCLR |= DMA1_CH7_INT_MASK; } else if (DMAyChx == DMA1_CH8) { /* Reset interrupt pending bits for DMA1 Channel8 */ DMA1->INTCLR |= DMA1_CH8_INT_MASK; } else if (DMAyChx == DMA2_CH1) { /* Reset interrupt pending bits for DMA2 Channel1 */ DMA2->INTCLR |= DMA2_CH1_INT_MASK; } else if (DMAyChx == DMA2_CH2) { /* Reset interrupt pending bits for DMA2 Channel2 */ DMA2->INTCLR |= DMA2_CH2_INT_MASK; } else if (DMAyChx == DMA2_CH3) { /* Reset interrupt pending bits for DMA2 Channel3 */ DMA2->INTCLR |= DMA2_CH3_INT_MASK; } else if (DMAyChx == DMA2_CH4) { /* Reset interrupt pending bits for DMA2 Channel4 */ DMA2->INTCLR |= DMA2_CH4_INT_MASK; } else if (DMAyChx == DMA2_CH5) { /* Reset interrupt pending bits for DMA2 Channel5 */ DMA2->INTCLR |= DMA2_CH5_INT_MASK; } else if (DMAyChx == DMA2_CH6) { /* Reset interrupt pending bits for DMA2 Channel6 */ DMA2->INTCLR |= DMA2_CH6_INT_MASK; } else if (DMAyChx == DMA2_CH7) { /* Reset interrupt pending bits for DMA2 Channel7 */ DMA2->INTCLR |= DMA2_CH7_INT_MASK; } else if (DMAyChx == DMA2_CH8) /* Reset interrupt pending bits for DMA2 Channel8 */ DMA2->INTCLR |= DMA2_CH8_INT_MASK; } void DMA_Init(DMA_ChannelType* DMAyChx, DMA_InitType* DMA_InitParam) { uint32_t tmpregister = 0; /*--------------------------- DMAy Channelx CHCFG Configuration --------------*/ /* Get the DMAyChx CHCFG value */ tmpregister = DMAyChx->CHCFG; /* Clear MEM2MEM, PL, MSIZE, PSIZE, MINC, PINC, CIRC and DIR bits */ tmpregister &= CCR_CLEAR_Mask; /* Configure DMAy Channelx: data transfer, data size, priority level and mode */ /* Set DIR bit according to Direction value */ /* Set CIRC bit according to CircularMode value */ /* Set PINC bit according to PeriphInc value */ /* Set MINC bit according to DMA_MemoryInc value */ /* Set PSIZE bits according to PeriphDataSize value */ /* Set MSIZE bits according to MemDataSize value */ /* Set PL bits according to Priority value */ /* Set the MEM2MEM bit according to Mem2Mem value */ tmpregister |= DMA_InitParam->Direction | DMA_InitParam->CircularMode | DMA_InitParam->PeriphInc | DMA_InitParam->DMA_MemoryInc | DMA_InitParam->PeriphDataSize | DMA_InitParam->MemDataSize | DMA_InitParam->Priority | DMA_InitParam->Mem2Mem; /* Write to DMAy Channelx CHCFG */ DMAyChx->CHCFG = tmpregister; /*--------------------------- DMAy Channelx TXNUM Configuration --------------*/ /* Write to DMAy Channelx TXNUM */ DMAyChx->TXNUM = DMA_InitParam->BufSize; /*--------------------------- DMAy Channelx PADDR Configuration --------------*/ /* Write to DMAy Channelx PADDR */ DMAyChx->PADDR = DMA_InitParam->PeriphAddr; /*--------------------------- DMAy Channelx MADDR Configuration --------------*/ /* Write to DMAy Channelx MADDR */ DMAyChx->MADDR = DMA_InitParam->MemAddr; } void DMA_EnableChannel(DMA_ChannelType* DMAyChx, uint32_t Cmd) { if (Cmd != 0) { /* Enable the selected DMAy Channelx */ DMAyChx->CHCFG |= DMA_CHCFG1_CHEN; } else { /* Disable the selected DMAy Channelx */ DMAyChx->CHCFG &= (uint16_t)(~DMA_CHCFG1_CHEN); } } /**================================================================ * Initialize the DMA of ADC ================================================================*/ void ADC_DMA_init() { DMA_InitType DMA_InitStructure; uint32_t reg_temp; /** Make DMA clock */ reg_temp = ADC_RCC_AHBPCLKEN; reg_temp |= ( RCC_AHB_PERIPH_DMA1 | RCC_AHB_PERIPH_DMA2 ); ADC_RCC_AHBPCLKEN = reg_temp; /* DMA channel configuration*/ DMA_DeInit(USE_DMA_CH); DMA_InitStructure.PeriphAddr = (uint32_t)&USE_ADC->DAT; DMA_InitStructure.MemAddr = (uint32_t)adc_results; DMA_InitStructure.Direction = DMA_DIR_PERIPH_SRC; // Peripheral-> memory DMA_InitStructure.BufSize = 2; DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE; DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE; // Memory ++ DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD; DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.CircularMode = DMA_MODE_CIRCULAR; DMA_InitStructure.Priority = DMA_PRIORITY_HIGH; DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE; DMA_Init(USE_DMA_CH, &DMA_InitStructure); /* Enable DMA channel1 */ DMA_EnableChannel(USE_DMA_CH, 1); } /**============================================================================= * n32g452 - end ==============================================================================*/ #define NS_PINRT(V...) do{ SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR(V); }while(0) // Init the AD in continuous capture mode void MarlinHAL::adc_init() { uint32_t reg_temp; //SERIAL_ECHO_MSG("\r\n n32g45x HAL_adc_init\r\n"); // GPIO settings reg_temp = ADC_RCC_APB2PCLKEN; reg_temp |= 0x0f; // Make PORT mouth clock ADC_RCC_APB2PCLKEN = reg_temp; //reg_temp = NS_GPIOC_PL_CFG; //reg_temp &= 0XFF00FFFF; //NS_GPIOC_PL_CFG = reg_temp; // PC4/5 analog input enable_adc_clk(1); // Make ADC clock ADC_DMA_init(); // DMA initialization ADC_Initial(NS_ADC2); // ADC initialization delay(2); //NS_PINRT("get adc1 = ", adc_results[0], "\r\n"); //NS_PINRT("get adc2 = ", adc_results[1], "\r\n"); } #endif // __STM32F1__ && VOXELAB_N32
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/HAL_N32.cpp
C++
agpl-3.0
23,083
/** * Marlin 3D Printer Firmware * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) * Specifically for VOXELAB_N32 (N32G452). TODO: Rework for generic N32 MCU. */ #include <STM32ADC.h> typedef struct { uint32_t WorkMode; uint32_t MultiChEn; uint32_t ContinueConvEn; uint32_t ExtTrigSelect; uint32_t DatAlign; uint8_t ChsNumber; } ADC_InitType; typedef struct { __IO uint32_t STS; __IO uint32_t CTRL1; __IO uint32_t CTRL2; __IO uint32_t SAMPT1; __IO uint32_t SAMPT2; __IO uint32_t JOFFSET1; __IO uint32_t JOFFSET2; __IO uint32_t JOFFSET3; __IO uint32_t JOFFSET4; __IO uint32_t WDGHIGH; __IO uint32_t WDGLOW; __IO uint32_t RSEQ1; __IO uint32_t RSEQ2; __IO uint32_t RSEQ3; __IO uint32_t JSEQ; __IO uint32_t JDAT1; __IO uint32_t JDAT2; __IO uint32_t JDAT3; __IO uint32_t JDAT4; __IO uint32_t DAT; __IO uint32_t DIFSEL; __IO uint32_t CALFACT; __IO uint32_t CTRL3; __IO uint32_t SAMPT3; } ADC_Module; #define NS_ADC1_BASE ((uint32_t)0x40020800) #define NS_ADC2_BASE ((uint32_t)0x40020c00) #define NS_ADC3_BASE ((uint32_t)0x40021800) #define NS_ADC4_BASE ((uint32_t)0x40021c00) #define NS_ADC1 ((ADC_Module*)NS_ADC1_BASE) #define NS_ADC2 ((ADC_Module*)NS_ADC2_BASE) #define NS_ADC3 ((ADC_Module*)NS_ADC3_BASE) #define NS_ADC4 ((ADC_Module*)NS_ADC4_BASE) #define ADC1_Channel_01_PA0 ((uint8_t)0x01) #define ADC1_Channel_02_PA1 ((uint8_t)0x02) #define ADC1_Channel_03_PA6 ((uint8_t)0x03) #define ADC1_Channel_04_PA3 ((uint8_t)0x04) #define ADC1_Channel_05_PF4 ((uint8_t)0x05) #define ADC1_Channel_06_PC0 ((uint8_t)0x06) #define ADC1_Channel_07_PC1 ((uint8_t)0x07) #define ADC1_Channel_08_PC2 ((uint8_t)0x08) #define ADC1_Channel_09_PC3 ((uint8_t)0x09) #define ADC1_Channel_10_PF2 ((uint8_t)0x0A) #define ADC1_Channel_11_PA2 ((uint8_t)0x0B) #define ADC2_Channel_01_PA4 ((uint8_t)0x01) #define ADC2_Channel_02_PA5 ((uint8_t)0x02) #define ADC2_Channel_03_PB1 ((uint8_t)0x03) #define ADC2_Channel_04_PA7 ((uint8_t)0x04) #define ADC2_Channel_05_PC4 ((uint8_t)0x05) #define ADC2_Channel_06_PC0 ((uint8_t)0x06) #define ADC2_Channel_07_PC1 ((uint8_t)0x07) #define ADC2_Channel_08_PC2 ((uint8_t)0x08) #define ADC2_Channel_09_PC3 ((uint8_t)0x09) #define ADC2_Channel_10_PF2 ((uint8_t)0x0A) #define ADC2_Channel_11_PA2 ((uint8_t)0x0B) #define ADC2_Channel_12_PC5 ((uint8_t)0x0C) #define ADC2_Channel_13_PB2 ((uint8_t)0x0D) #define ADC3_Channel_01_PB11 ((uint8_t)0x01) #define ADC3_Channel_02_PE9 ((uint8_t)0x02) #define ADC3_Channel_03_PE13 ((uint8_t)0x03) #define ADC3_Channel_04_PE12 ((uint8_t)0x04) #define ADC3_Channel_05_PB13 ((uint8_t)0x05) #define ADC3_Channel_06_PE8 ((uint8_t)0x06) #define ADC3_Channel_07_PD10 ((uint8_t)0x07) #define ADC3_Channel_08_PD11 ((uint8_t)0x08) #define ADC3_Channel_09_PD12 ((uint8_t)0x09) #define ADC3_Channel_10_PD13 ((uint8_t)0x0A) #define ADC3_Channel_11_PD14 ((uint8_t)0x0B) #define ADC3_Channel_12_PB0 ((uint8_t)0x0C) #define ADC3_Channel_13_PE7 ((uint8_t)0x0D) #define ADC3_Channel_14_PE10 ((uint8_t)0x0E) #define ADC3_Channel_15_PE11 ((uint8_t)0x0F) #define ADC4_Channel_01_PE14 ((uint8_t)0x01) #define ADC4_Channel_02_PE15 ((uint8_t)0x02) #define ADC4_Channel_03_PB12 ((uint8_t)0x03) #define ADC4_Channel_04_PB14 ((uint8_t)0x04) #define ADC4_Channel_05_PB15 ((uint8_t)0x05) #define ADC4_Channel_06_PE8 ((uint8_t)0x06) #define ADC4_Channel_07_PD10 ((uint8_t)0x07) #define ADC4_Channel_08_PD11 ((uint8_t)0x08) #define ADC4_Channel_09_PD12 ((uint8_t)0x09) #define ADC4_Channel_10_PD13 ((uint8_t)0x0A) #define ADC4_Channel_11_PD14 ((uint8_t)0x0B) #define ADC4_Channel_12_PD8 ((uint8_t)0x0C) #define ADC4_Channel_13_PD9 ((uint8_t)0x0D) #define ADC_RCC_BASE ((uint32_t)0x40021000) #define ADC_RCC_CTRL *((uint32_t*)(ADC_RCC_BASE + 0x00)) #define ADC_RCC_CFG *((uint32_t*)(ADC_RCC_BASE + 0x04)) #define ADC_RCC_CLKINT *((uint32_t*)(ADC_RCC_BASE + 0x08)) #define ADC_RCC_APB2PRST *((uint32_t*)(ADC_RCC_BASE + 0x0c)) #define ADC_RCC_APB1PRST *((uint32_t*)(ADC_RCC_BASE + 0x10)) #define ADC_RCC_AHBPCLKEN *((uint32_t*)(ADC_RCC_BASE + 0x14)) #define ADC_RCC_APB2PCLKEN *((uint32_t*)(ADC_RCC_BASE + 0x18)) #define ADC_RCC_APB1PCLKEN *((uint32_t*)(ADC_RCC_BASE + 0x1c)) #define ADC_RCC_BDCTRL *((uint32_t*)(ADC_RCC_BASE + 0x20)) #define ADC_RCC_CTRLSTS *((uint32_t*)(ADC_RCC_BASE + 0x24)) #define ADC_RCC_AHBPRST *((uint32_t*)(ADC_RCC_BASE + 0x28)) #define ADC_RCC_CFG2 *((uint32_t*)(ADC_RCC_BASE + 0x2c)) #define ADC_RCC_CFG3 *((uint32_t*)(ADC_RCC_BASE + 0x30)) #define NS_PWR_CR3 *((uint32_t*)(0x40007000 + 0x0C)) #define RCC_APB1Periph_PWR ((uint32_t)0x10000000) /////////////////////////////// #define NS_GPIOA_BASE ((uint32_t)0x40010800) #define NS_GPIOA_PL_CFG *((uint32_t*)(NS_GPIOA_BASE + 0x00)) #define NS_GPIOA_PH_CFG *((uint32_t*)(NS_GPIOA_BASE + 0x04)) #define NS_GPIOC_BASE ((uint32_t)0x40011000) #define NS_GPIOC_PL_CFG *((uint32_t*)(NS_GPIOC_BASE + 0x00)) #define NS_GPIOC_PH_CFG *((uint32_t*)(NS_GPIOC_BASE + 0x04)) /* CFG2 register bit mask */ #define CFG2_TIM18CLKSEL_SET_MASK ((uint32_t)0x20000000) #define CFG2_TIM18CLKSEL_RESET_MASK ((uint32_t)0xDFFFFFFF) #define CFG2_RNGCPRES_SET_MASK ((uint32_t)0x1F000000) #define CFG2_RNGCPRES_RESET_MASK ((uint32_t)0xE0FFFFFF) #define CFG2_ADC1MSEL_SET_MASK ((uint32_t)0x00000400) #define CFG2_ADC1MSEL_RESET_MASK ((uint32_t)0xFFFFFBFF) #define CFG2_ADC1MPRES_SET_MASK ((uint32_t)0x0000F800) #define CFG2_ADC1MPRES_RESET_MASK ((uint32_t)0xFFFF07FF) #define CFG2_ADCPLLPRES_SET_MASK ((uint32_t)0x000001F0) #define CFG2_ADCPLLPRES_RESET_MASK ((uint32_t)0xFFFFFE0F) #define CFG2_ADCHPRES_SET_MASK ((uint32_t)0x0000000F) #define CFG2_ADCHPRES_RESET_MASK ((uint32_t)0xFFFFFFF0) #define RCC_ADCPLLCLK_DISABLE ((uint32_t)0xFFFFFEFF) #define RCC_ADCPLLCLK_DIV1 ((uint32_t)0x00000100) #define RCC_ADCPLLCLK_DIV2 ((uint32_t)0x00000110) #define RCC_ADCPLLCLK_DIV4 ((uint32_t)0x00000120) #define RCC_ADCPLLCLK_DIV6 ((uint32_t)0x00000130) #define RCC_ADCPLLCLK_DIV8 ((uint32_t)0x00000140) #define RCC_ADCPLLCLK_DIV10 ((uint32_t)0x00000150) #define RCC_ADCPLLCLK_DIV12 ((uint32_t)0x00000160) #define RCC_ADCPLLCLK_DIV16 ((uint32_t)0x00000170) #define RCC_ADCPLLCLK_DIV32 ((uint32_t)0x00000180) #define RCC_ADCPLLCLK_DIV64 ((uint32_t)0x00000190) #define RCC_ADCPLLCLK_DIV128 ((uint32_t)0x000001A0) #define RCC_ADCPLLCLK_DIV256 ((uint32_t)0x000001B0) #define RCC_ADCPLLCLK_DIV_OTHERS ((uint32_t)0x000001C0) #define RCC_ADCHCLK_DIV1 ((uint32_t)0x00000000) #define RCC_ADCHCLK_DIV2 ((uint32_t)0x00000001) #define RCC_ADCHCLK_DIV4 ((uint32_t)0x00000002) #define RCC_ADCHCLK_DIV6 ((uint32_t)0x00000003) #define RCC_ADCHCLK_DIV8 ((uint32_t)0x00000004) #define RCC_ADCHCLK_DIV10 ((uint32_t)0x00000005) #define RCC_ADCHCLK_DIV12 ((uint32_t)0x00000006) #define RCC_ADCHCLK_DIV16 ((uint32_t)0x00000007) #define RCC_ADCHCLK_DIV32 ((uint32_t)0x00000008) #define RCC_ADCHCLK_DIV_OTHERS ((uint32_t)0x00000008) #define SAMPT1_SMP_SET ((uint32_t)0x00000007) #define SAMPT2_SMP_SET ((uint32_t)0x00000007) #define SQR4_SEQ_SET ((uint32_t)0x0000001F) #define SQR3_SEQ_SET ((uint32_t)0x0000001F) #define SQR2_SEQ_SET ((uint32_t)0x0000001F) #define SQR1_SEQ_SET ((uint32_t)0x0000001F) #define CTRL1_CLR_MASK ((uint32_t)0xFFF0FEFF) #define RSEQ1_CLR_MASK ((uint32_t)0xFF0FFFFF) #define CTRL2_CLR_MASK ((uint32_t)0xFFF1F7FD) #define ADC_CH_0 ((uint8_t)0x00) #define ADC_CH_1 ((uint8_t)0x01) #define ADC_CH_2 ((uint8_t)0x02) #define ADC_CH_3 ((uint8_t)0x03) #define ADC_CH_4 ((uint8_t)0x04) #define ADC_CH_5 ((uint8_t)0x05) #define ADC_CH_6 ((uint8_t)0x06) #define ADC_CH_7 ((uint8_t)0x07) #define ADC_CH_8 ((uint8_t)0x08) #define ADC_CH_9 ((uint8_t)0x09) #define ADC_CH_10 ((uint8_t)0x0A) #define ADC_CH_11 ((uint8_t)0x0B) #define ADC_CH_12 ((uint8_t)0x0C) #define ADC_CH_13 ((uint8_t)0x0D) #define ADC_CH_14 ((uint8_t)0x0E) #define ADC_CH_15 ((uint8_t)0x0F) #define ADC_CH_16 ((uint8_t)0x10) #define ADC_CH_17 ((uint8_t)0x11) #define ADC_CH_18 ((uint8_t)0x12) #define ADC_WORKMODE_INDEPENDENT ((uint32_t)0x00000000) #define ADC_WORKMODE_REG_INJECT_SIMULT ((uint32_t)0x00010000) #define ADC_WORKMODE_REG_SIMULT_ALTER_TRIG ((uint32_t)0x00020000) #define ADC_WORKMODE_INJ_SIMULT_FAST_INTERL ((uint32_t)0x00030000) #define ADC_WORKMODE_INJ_SIMULT_SLOW_INTERL ((uint32_t)0x00040000) #define ADC_WORKMODE_INJ_SIMULT ((uint32_t)0x00050000) #define ADC_WORKMODE_REG_SIMULT ((uint32_t)0x00060000) #define ADC_WORKMODE_FAST_INTERL ((uint32_t)0x00070000) #define ADC_WORKMODE_SLOW_INTERL ((uint32_t)0x00080000) #define ADC_WORKMODE_ALTER_TRIG ((uint32_t)0x00090000) #define ADC_EXT_TRIGCONV_T1_CC3 ((uint32_t)0x00040000) //!< For ADC1, ADC2 , ADC3 and ADC4 #define ADC_EXT_TRIGCONV_NONE ((uint32_t)0x000E0000) //!< For ADC1, ADC2 , ADC3 and ADC4 #define ADC_DAT_ALIGN_R ((uint32_t)0x00000000) #define ADC_DAT_ALIGN_L ((uint32_t)0x00000800) #define ADC_FLAG_RDY ((uint8_t)0x20) #define ADC_FLAG_PD_RDY ((uint8_t)0x40) #define CTRL2_AD_ON_SET ((uint32_t)0x00000001) #define CTRL2_AD_ON_RESET ((uint32_t)0xFFFFFFFE) #define CTRL2_CAL_SET ((uint32_t)0x00000004) /* ADC Software start mask */ #define CTRL2_EXT_TRIG_SWSTART_SET ((uint32_t)0x00500000) #define CTRL2_EXT_TRIG_SWSTART_RESET ((uint32_t)0xFFAFFFFF) #define ADC_SAMP_TIME_1CYCLES5 ((uint8_t)0x00) #define ADC_SAMP_TIME_7CYCLES5 ((uint8_t)0x01) #define ADC_SAMP_TIME_13CYCLES5 ((uint8_t)0x02) #define ADC_SAMP_TIME_28CYCLES5 ((uint8_t)0x03) #define ADC_SAMP_TIME_41CYCLES5 ((uint8_t)0x04) #define ADC_SAMP_TIME_55CYCLES5 ((uint8_t)0x05) #define ADC_SAMP_TIME_71CYCLES5 ((uint8_t)0x06) #define ADC_SAMP_TIME_239CYCLES5 ((uint8_t)0x07) #define ADC_FLAG_AWDG ((uint8_t)0x01) #define ADC_FLAG_ENDC ((uint8_t)0x02) #define ADC_FLAG_JENDC ((uint8_t)0x04) #define ADC_FLAG_JSTR ((uint8_t)0x08) #define ADC_FLAG_STR ((uint8_t)0x10) #define ADC_FLAG_EOC_ANY ((uint8_t)0x20) #define ADC_FLAG_JEOC_ANY ((uint8_t)0x40) /* ADC DMA mask */ #define CTRL2_DMA_SET ((uint32_t)0x00000100) #define CTRL2_DMA_RESET ((uint32_t)0xFFFFFEFF) typedef struct { uint32_t PeriphAddr; uint32_t MemAddr; uint32_t Direction; uint32_t BufSize; uint32_t PeriphInc; uint32_t DMA_MemoryInc; uint32_t PeriphDataSize; uint32_t MemDataSize; uint32_t CircularMode; uint32_t Priority; uint32_t Mem2Mem; } DMA_InitType; typedef struct { __IO uint32_t CHCFG; __IO uint32_t TXNUM; __IO uint32_t PADDR; __IO uint32_t MADDR; __IO uint32_t CHSEL; } DMA_ChannelType; #define DMA_DIR_PERIPH_DST ((uint32_t)0x00000010) #define DMA_DIR_PERIPH_SRC ((uint32_t)0x00000000) #define DMA_PERIPH_INC_ENABLE ((uint32_t)0x00000040) #define DMA_PERIPH_INC_DISABLE ((uint32_t)0x00000000) #define DMA_MEM_INC_ENABLE ((uint32_t)0x00000080) #define DMA_MEM_INC_DISABLE ((uint32_t)0x00000000) #define DMA_PERIPH_DATA_SIZE_BYTE ((uint32_t)0x00000000) #define DMA_PERIPH_DATA_SIZE_HALFWORD ((uint32_t)0x00000100) #define DMA_PERIPH_DATA_SIZE_WORD ((uint32_t)0x00000200) #define DMA_MemoryDataSize_Byte ((uint32_t)0x00000000) #define DMA_MemoryDataSize_HalfWord ((uint32_t)0x00000400) #define DMA_MemoryDataSize_Word ((uint32_t)0x00000800) #define DMA_MODE_CIRCULAR ((uint32_t)0x00000020) #define DMA_MODE_NORMAL ((uint32_t)0x00000000) #define DMA_M2M_ENABLE ((uint32_t)0x00004000) #define DMA_M2M_DISABLE ((uint32_t)0x00000000) #define RCC_AHB_PERIPH_DMA1 ((uint32_t)0x00000001) #define RCC_AHB_PERIPH_DMA2 ((uint32_t)0x00000002) /******************* Bit definition for DMA_CHCFG1 register *******************/ #define DMA_CHCFG1_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG1_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG1_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG1_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG1_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG1_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG1_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG1_MINC ((uint16_t)0x0080) //!< Memory increment mode #define NS_DMA1_BASE (0x40020000) #define DMA1_CH1_BASE (NS_DMA1_BASE + 0x0008) #define DMA1_CH2_BASE (NS_DMA1_BASE + 0x001C) #define DMA1_CH3_BASE (NS_DMA1_BASE + 0x0030) #define DMA1_CH4_BASE (NS_DMA1_BASE + 0x0044) #define DMA1_CH5_BASE (NS_DMA1_BASE + 0x0058) #define DMA1_CH6_BASE (NS_DMA1_BASE + 0x006C) #define DMA1_CH7_BASE (NS_DMA1_BASE + 0x0080) #define DMA1_CH8_BASE (NS_DMA1_BASE + 0x0094) #define NS_DMA2_BASE (0x40020400) #define DMA2_CH1_BASE (NS_DMA2_BASE + 0x008) #define DMA2_CH2_BASE (NS_DMA2_BASE + 0x01C) #define DMA2_CH3_BASE (NS_DMA2_BASE + 0x0030) #define DMA2_CH4_BASE (NS_DMA2_BASE + 0x0044) #define DMA2_CH5_BASE (NS_DMA2_BASE + 0x0058) #define DMA2_CH6_BASE (NS_DMA2_BASE + 0x006C) #define DMA2_CH7_BASE (NS_DMA2_BASE + 0x0080) #define DMA2_CH8_BASE (NS_DMA2_BASE + 0x0094) #define DMA1 ((DMA_Module*)NS_DMA1_BASE) #define DMA2 ((DMA_Module*)NS_DMA2_BASE) #define DMA1_CH1 ((DMA_ChannelType*)DMA1_CH1_BASE) #define DMA1_CH2 ((DMA_ChannelType*)DMA1_CH2_BASE) #define DMA1_CH3 ((DMA_ChannelType*)DMA1_CH3_BASE) #define DMA1_CH4 ((DMA_ChannelType*)DMA1_CH4_BASE) #define DMA1_CH5 ((DMA_ChannelType*)DMA1_CH5_BASE) #define DMA1_CH6 ((DMA_ChannelType*)DMA1_CH6_BASE) #define DMA1_CH7 ((DMA_ChannelType*)DMA1_CH7_BASE) #define DMA1_CH8 ((DMA_ChannelType*)DMA1_CH8_BASE) #define DMA2_CH1 ((DMA_ChannelType*)DMA2_CH1_BASE) #define DMA2_CH2 ((DMA_ChannelType*)DMA2_CH2_BASE) #define DMA2_CH3 ((DMA_ChannelType*)DMA2_CH3_BASE) #define DMA2_CH4 ((DMA_ChannelType*)DMA2_CH4_BASE) #define DMA2_CH5 ((DMA_ChannelType*)DMA2_CH5_BASE) #define DMA2_CH6 ((DMA_ChannelType*)DMA2_CH6_BASE) #define DMA2_CH7 ((DMA_ChannelType*)DMA2_CH7_BASE) #define DMA2_CH8 ((DMA_ChannelType*)DMA2_CH8_BASE) /******************************************************************************/ /* */ /* DMA Controller */ /* */ /******************************************************************************/ /******************* Bit definition for DMA_INTSTS register ********************/ #define DMA_INTSTS_GLBF1 ((uint32_t)0x00000001) //!< Channel 1 Global interrupt flag #define DMA_INTSTS_TXCF1 ((uint32_t)0x00000002) //!< Channel 1 Transfer Complete flag #define DMA_INTSTS_HTXF1 ((uint32_t)0x00000004) //!< Channel 1 Half Transfer flag #define DMA_INTSTS_ERRF1 ((uint32_t)0x00000008) //!< Channel 1 Transfer Error flag #define DMA_INTSTS_GLBF2 ((uint32_t)0x00000010) //!< Channel 2 Global interrupt flag #define DMA_INTSTS_TXCF2 ((uint32_t)0x00000020) //!< Channel 2 Transfer Complete flag #define DMA_INTSTS_HTXF2 ((uint32_t)0x00000040) //!< Channel 2 Half Transfer flag #define DMA_INTSTS_ERRF2 ((uint32_t)0x00000080) //!< Channel 2 Transfer Error flag #define DMA_INTSTS_GLBF3 ((uint32_t)0x00000100) //!< Channel 3 Global interrupt flag #define DMA_INTSTS_TXCF3 ((uint32_t)0x00000200) //!< Channel 3 Transfer Complete flag #define DMA_INTSTS_HTXF3 ((uint32_t)0x00000400) //!< Channel 3 Half Transfer flag #define DMA_INTSTS_ERRF3 ((uint32_t)0x00000800) //!< Channel 3 Transfer Error flag #define DMA_INTSTS_GLBF4 ((uint32_t)0x00001000) //!< Channel 4 Global interrupt flag #define DMA_INTSTS_TXCF4 ((uint32_t)0x00002000) //!< Channel 4 Transfer Complete flag #define DMA_INTSTS_HTXF4 ((uint32_t)0x00004000) //!< Channel 4 Half Transfer flag #define DMA_INTSTS_ERRF4 ((uint32_t)0x00008000) //!< Channel 4 Transfer Error flag #define DMA_INTSTS_GLBF5 ((uint32_t)0x00010000) //!< Channel 5 Global interrupt flag #define DMA_INTSTS_TXCF5 ((uint32_t)0x00020000) //!< Channel 5 Transfer Complete flag #define DMA_INTSTS_HTXF5 ((uint32_t)0x00040000) //!< Channel 5 Half Transfer flag #define DMA_INTSTS_ERRF5 ((uint32_t)0x00080000) //!< Channel 5 Transfer Error flag #define DMA_INTSTS_GLBF6 ((uint32_t)0x00100000) //!< Channel 6 Global interrupt flag #define DMA_INTSTS_TXCF6 ((uint32_t)0x00200000) //!< Channel 6 Transfer Complete flag #define DMA_INTSTS_HTXF6 ((uint32_t)0x00400000) //!< Channel 6 Half Transfer flag #define DMA_INTSTS_ERRF6 ((uint32_t)0x00800000) //!< Channel 6 Transfer Error flag #define DMA_INTSTS_GLBF7 ((uint32_t)0x01000000) //!< Channel 7 Global interrupt flag #define DMA_INTSTS_TXCF7 ((uint32_t)0x02000000) //!< Channel 7 Transfer Complete flag #define DMA_INTSTS_HTXF7 ((uint32_t)0x04000000) //!< Channel 7 Half Transfer flag #define DMA_INTSTS_ERRF7 ((uint32_t)0x08000000) //!< Channel 7 Transfer Error flag #define DMA_INTSTS_GLBF8 ((uint32_t)0x10000000) //!< Channel 7 Global interrupt flag #define DMA_INTSTS_TXCF8 ((uint32_t)0x20000000) //!< Channel 7 Transfer Complete flag #define DMA_INTSTS_HTXF8 ((uint32_t)0x40000000) //!< Channel 7 Half Transfer flag #define DMA_INTSTS_ERRF8 ((uint32_t)0x80000000) //!< Channel 7 Transfer Error flag /******************* Bit definition for DMA_INTCLR register *******************/ #define DMA_INTCLR_CGLBF1 ((uint32_t)0x00000001) //!< Channel 1 Global interrupt clear #define DMA_INTCLR_CTXCF1 ((uint32_t)0x00000002) //!< Channel 1 Transfer Complete clear #define DMA_INTCLR_CHTXF1 ((uint32_t)0x00000004) //!< Channel 1 Half Transfer clear #define DMA_INTCLR_CERRF1 ((uint32_t)0x00000008) //!< Channel 1 Transfer Error clear #define DMA_INTCLR_CGLBF2 ((uint32_t)0x00000010) //!< Channel 2 Global interrupt clear #define DMA_INTCLR_CTXCF2 ((uint32_t)0x00000020) //!< Channel 2 Transfer Complete clear #define DMA_INTCLR_CHTXF2 ((uint32_t)0x00000040) //!< Channel 2 Half Transfer clear #define DMA_INTCLR_CERRF2 ((uint32_t)0x00000080) //!< Channel 2 Transfer Error clear #define DMA_INTCLR_CGLBF3 ((uint32_t)0x00000100) //!< Channel 3 Global interrupt clear #define DMA_INTCLR_CTXCF3 ((uint32_t)0x00000200) //!< Channel 3 Transfer Complete clear #define DMA_INTCLR_CHTXF3 ((uint32_t)0x00000400) //!< Channel 3 Half Transfer clear #define DMA_INTCLR_CERRF3 ((uint32_t)0x00000800) //!< Channel 3 Transfer Error clear #define DMA_INTCLR_CGLBF4 ((uint32_t)0x00001000) //!< Channel 4 Global interrupt clear #define DMA_INTCLR_CTXCF4 ((uint32_t)0x00002000) //!< Channel 4 Transfer Complete clear #define DMA_INTCLR_CHTXF4 ((uint32_t)0x00004000) //!< Channel 4 Half Transfer clear #define DMA_INTCLR_CERRF4 ((uint32_t)0x00008000) //!< Channel 4 Transfer Error clear #define DMA_INTCLR_CGLBF5 ((uint32_t)0x00010000) //!< Channel 5 Global interrupt clear #define DMA_INTCLR_CTXCF5 ((uint32_t)0x00020000) //!< Channel 5 Transfer Complete clear #define DMA_INTCLR_CHTXF5 ((uint32_t)0x00040000) //!< Channel 5 Half Transfer clear #define DMA_INTCLR_CERRF5 ((uint32_t)0x00080000) //!< Channel 5 Transfer Error clear #define DMA_INTCLR_CGLBF6 ((uint32_t)0x00100000) //!< Channel 6 Global interrupt clear #define DMA_INTCLR_CTXCF6 ((uint32_t)0x00200000) //!< Channel 6 Transfer Complete clear #define DMA_INTCLR_CHTXF6 ((uint32_t)0x00400000) //!< Channel 6 Half Transfer clear #define DMA_INTCLR_CERRF6 ((uint32_t)0x00800000) //!< Channel 6 Transfer Error clear #define DMA_INTCLR_CGLBF7 ((uint32_t)0x01000000) //!< Channel 7 Global interrupt clear #define DMA_INTCLR_CTXCF7 ((uint32_t)0x02000000) //!< Channel 7 Transfer Complete clear #define DMA_INTCLR_CHTXF7 ((uint32_t)0x04000000) //!< Channel 7 Half Transfer clear #define DMA_INTCLR_CERRF7 ((uint32_t)0x08000000) //!< Channel 7 Transfer Error clear #define DMA_INTCLR_CGLBF8 ((uint32_t)0x10000000) //!< Channel 7 Global interrupt clear #define DMA_INTCLR_CTXCF8 ((uint32_t)0x20000000) //!< Channel 7 Transfer Complete clear #define DMA_INTCLR_CHTXF8 ((uint32_t)0x40000000) //!< Channel 7 Half Transfer clear #define DMA_INTCLR_CERRF8 ((uint32_t)0x80000000) //!< Channel 7 Transfer Error clear /******************* Bit definition for DMA_CHCFG1 register *******************/ #define DMA_CHCFG1_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG1_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG1_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG1_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG1_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG1_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG1_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG1_MINC ((uint16_t)0x0080) //!< Memory increment mode #define DMA_CHCFG1_PSIZE ((uint16_t)0x0300) //!< PSIZE[1:0] bits (Peripheral size) #define DMA_CHCFG1_PSIZE_0 ((uint16_t)0x0100) //!< Bit 0 #define DMA_CHCFG1_PSIZE_1 ((uint16_t)0x0200) //!< Bit 1 #define DMA_CHCFG1_MSIZE ((uint16_t)0x0C00) //!< MSIZE[1:0] bits (Memory size) #define DMA_CHCFG1_MSIZE_0 ((uint16_t)0x0400) //!< Bit 0 #define DMA_CHCFG1_MSIZE_1 ((uint16_t)0x0800) //!< Bit 1 #define DMA_CHCFG1_PRIOLVL ((uint16_t)0x3000) //!< PL[1:0] bits(Channel Priority level) #define DMA_CHCFG1_PRIOLVL_0 ((uint16_t)0x1000) //!< Bit 0 #define DMA_CHCFG1_PRIOLVL_1 ((uint16_t)0x2000) //!< Bit 1 #define DMA_CHCFG1_MEM2MEM ((uint16_t)0x4000) //!< Memory to memory mode /******************* Bit definition for DMA_CHCFG2 register *******************/ #define DMA_CHCFG2_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG2_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG2_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG2_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG2_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG2_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG2_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG2_MINC ((uint16_t)0x0080) //!< Memory increment mode #define DMA_CHCFG2_PSIZE ((uint16_t)0x0300) //!< PSIZE[1:0] bits (Peripheral size) #define DMA_CHCFG2_PSIZE_0 ((uint16_t)0x0100) //!< Bit 0 #define DMA_CHCFG2_PSIZE_1 ((uint16_t)0x0200) //!< Bit 1 #define DMA_CHCFG2_MSIZE ((uint16_t)0x0C00) //!< MSIZE[1:0] bits (Memory size) #define DMA_CHCFG2_MSIZE_0 ((uint16_t)0x0400) //!< Bit 0 #define DMA_CHCFG2_MSIZE_1 ((uint16_t)0x0800) //!< Bit 1 #define DMA_CHCFG2_PRIOLVL ((uint16_t)0x3000) //!< PL[1:0] bits (Channel Priority level) #define DMA_CHCFG2_PRIOLVL_0 ((uint16_t)0x1000) //!< Bit 0 #define DMA_CHCFG2_PRIOLVL_1 ((uint16_t)0x2000) //!< Bit 1 #define DMA_CHCFG2_MEM2MEM ((uint16_t)0x4000) //!< Memory to memory mode /******************* Bit definition for DMA_CHCFG3 register *******************/ #define DMA_CHCFG3_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG3_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG3_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG3_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG3_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG3_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG3_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG3_MINC ((uint16_t)0x0080) //!< Memory increment mode #define DMA_CHCFG3_PSIZE ((uint16_t)0x0300) //!< PSIZE[1:0] bits (Peripheral size) #define DMA_CHCFG3_PSIZE_0 ((uint16_t)0x0100) //!< Bit 0 #define DMA_CHCFG3_PSIZE_1 ((uint16_t)0x0200) //!< Bit 1 #define DMA_CHCFG3_MSIZE ((uint16_t)0x0C00) //!< MSIZE[1:0] bits (Memory size) #define DMA_CHCFG3_MSIZE_0 ((uint16_t)0x0400) //!< Bit 0 #define DMA_CHCFG3_MSIZE_1 ((uint16_t)0x0800) //!< Bit 1 #define DMA_CHCFG3_PRIOLVL ((uint16_t)0x3000) //!< PL[1:0] bits (Channel Priority level) #define DMA_CHCFG3_PRIOLVL_0 ((uint16_t)0x1000) //!< Bit 0 #define DMA_CHCFG3_PRIOLVL_1 ((uint16_t)0x2000) //!< Bit 1 #define DMA_CHCFG3_MEM2MEM ((uint16_t)0x4000) //!< Memory to memory mode /*!<****************** Bit definition for DMA_CHCFG4 register *******************/ #define DMA_CHCFG4_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG4_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG4_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG4_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG4_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG4_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG4_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG4_MINC ((uint16_t)0x0080) //!< Memory increment mode #define DMA_CHCFG4_PSIZE ((uint16_t)0x0300) //!< PSIZE[1:0] bits (Peripheral size) #define DMA_CHCFG4_PSIZE_0 ((uint16_t)0x0100) //!< Bit 0 #define DMA_CHCFG4_PSIZE_1 ((uint16_t)0x0200) //!< Bit 1 #define DMA_CHCFG4_MSIZE ((uint16_t)0x0C00) //!< MSIZE[1:0] bits (Memory size) #define DMA_CHCFG4_MSIZE_0 ((uint16_t)0x0400) //!< Bit 0 #define DMA_CHCFG4_MSIZE_1 ((uint16_t)0x0800) //!< Bit 1 #define DMA_CHCFG4_PRIOLVL ((uint16_t)0x3000) //!< PL[1:0] bits (Channel Priority level) #define DMA_CHCFG4_PRIOLVL_0 ((uint16_t)0x1000) //!< Bit 0 #define DMA_CHCFG4_PRIOLVL_1 ((uint16_t)0x2000) //!< Bit 1 #define DMA_CHCFG4_MEM2MEM ((uint16_t)0x4000) //!< Memory to memory mode /****************** Bit definition for DMA_CHCFG5 register *******************/ #define DMA_CHCFG5_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG5_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG5_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG5_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG5_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG5_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG5_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG5_MINC ((uint16_t)0x0080) //!< Memory increment mode #define DMA_CHCFG5_PSIZE ((uint16_t)0x0300) //!< PSIZE[1:0] bits (Peripheral size) #define DMA_CHCFG5_PSIZE_0 ((uint16_t)0x0100) //!< Bit 0 #define DMA_CHCFG5_PSIZE_1 ((uint16_t)0x0200) //!< Bit 1 #define DMA_CHCFG5_MSIZE ((uint16_t)0x0C00) //!< MSIZE[1:0] bits (Memory size) #define DMA_CHCFG5_MSIZE_0 ((uint16_t)0x0400) //!< Bit 0 #define DMA_CHCFG5_MSIZE_1 ((uint16_t)0x0800) //!< Bit 1 #define DMA_CHCFG5_PRIOLVL ((uint16_t)0x3000) //!< PL[1:0] bits (Channel Priority level) #define DMA_CHCFG5_PRIOLVL_0 ((uint16_t)0x1000) //!< Bit 0 #define DMA_CHCFG5_PRIOLVL_1 ((uint16_t)0x2000) //!< Bit 1 #define DMA_CHCFG5_MEM2MEM ((uint16_t)0x4000) //!< Memory to memory mode enable /******************* Bit definition for DMA_CHCFG6 register *******************/ #define DMA_CHCFG6_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG6_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG6_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG6_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG6_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG6_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG6_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG6_MINC ((uint16_t)0x0080) //!< Memory increment mode #define DMA_CHCFG6_PSIZE ((uint16_t)0x0300) //!< PSIZE[1:0] bits (Peripheral size) #define DMA_CHCFG6_PSIZE_0 ((uint16_t)0x0100) //!< Bit 0 #define DMA_CHCFG6_PSIZE_1 ((uint16_t)0x0200) //!< Bit 1 #define DMA_CHCFG6_MSIZE ((uint16_t)0x0C00) //!< MSIZE[1:0] bits (Memory size) #define DMA_CHCFG6_MSIZE_0 ((uint16_t)0x0400) //!< Bit 0 #define DMA_CHCFG6_MSIZE_1 ((uint16_t)0x0800) //!< Bit 1 #define DMA_CHCFG6_PRIOLVL ((uint16_t)0x3000) //!< PL[1:0] bits (Channel Priority level) #define DMA_CHCFG6_PRIOLVL_0 ((uint16_t)0x1000) //!< Bit 0 #define DMA_CHCFG6_PRIOLVL_1 ((uint16_t)0x2000) //!< Bit 1 #define DMA_CHCFG6_MEM2MEM ((uint16_t)0x4000) //!< Memory to memory mode /******************* Bit definition for DMA_CHCFG7 register *******************/ #define DMA_CHCFG7_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG7_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG7_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG7_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG7_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG7_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG7_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG7_MINC ((uint16_t)0x0080) //!< Memory increment mode #define DMA_CHCFG7_PSIZE , ((uint16_t)0x0300) //!< PSIZE[1:0] bits (Peripheral size) #define DMA_CHCFG7_PSIZE_0 ((uint16_t)0x0100) //!< Bit 0 #define DMA_CHCFG7_PSIZE_1 ((uint16_t)0x0200) //!< Bit 1 #define DMA_CHCFG7_MSIZE ((uint16_t)0x0C00) //!< MSIZE[1:0] bits (Memory size) #define DMA_CHCFG7_MSIZE_0 ((uint16_t)0x0400) //!< Bit 0 #define DMA_CHCFG7_MSIZE_1 ((uint16_t)0x0800) //!< Bit 1 #define DMA_CHCFG7_PRIOLVL ((uint16_t)0x3000) //!< PL[1:0] bits (Channel Priority level) #define DMA_CHCFG7_PRIOLVL_0 ((uint16_t)0x1000) //!< Bit 0 #define DMA_CHCFG7_PRIOLVL_1 ((uint16_t)0x2000) //!< Bit 1 #define DMA_CHCFG7_MEM2MEM ((uint16_t)0x4000) //!< Memory to memory mode enable /******************* Bit definition for DMA_CHCFG8 register *******************/ #define DMA_CHCFG8_CHEN ((uint16_t)0x0001) //!< Channel enable #define DMA_CHCFG8_TXCIE ((uint16_t)0x0002) //!< Transfer complete interrupt enable #define DMA_CHCFG8_HTXIE ((uint16_t)0x0004) //!< Half Transfer interrupt enable #define DMA_CHCFG8_ERRIE ((uint16_t)0x0008) //!< Transfer error interrupt enable #define DMA_CHCFG8_DIR ((uint16_t)0x0010) //!< Data transfer direction #define DMA_CHCFG8_CIRC ((uint16_t)0x0020) //!< Circular mode #define DMA_CHCFG8_PINC ((uint16_t)0x0040) //!< Peripheral increment mode #define DMA_CHCFG8_MINC ((uint16_t)0x0080) //!< Memory increment mode #define DMA_CHCFG8_PSIZE , ((uint16_t)0x0300) //!< PSIZE[1:0] bits (Peripheral size) #define DMA_CHCFG8_PSIZE_0 ((uint16_t)0x0100) //!< Bit 0 #define DMA_CHCFG8_PSIZE_1 ((uint16_t)0x0200) //!< Bit 1 #define DMA_CHCFG8_MSIZE ((uint16_t)0x0C00) //!< MSIZE[1:0] bits (Memory size) #define DMA_CHCFG8_MSIZE_0 ((uint16_t)0x0400) //!< Bit 0 #define DMA_CHCFG8_MSIZE_1 ((uint16_t)0x0800) //!< Bit 1 #define DMA_CHCFG8_PRIOLVL ((uint16_t)0x3000) //!< PL[1:0] bits (Channel Priority level) #define DMA_CHCFG8_PRIOLVL_0 ((uint16_t)0x1000) //!< Bit 0 #define DMA_CHCFG8_PRIOLVL_1 ((uint16_t)0x2000) //!< Bit 1 #define DMA_CHCFG8_MEM2MEM ((uint16_t)0x4000) //!< Memory to memory mode enable /****************** Bit definition for DMA_TXNUM1 register ******************/ #define DMA_TXNUM1_NDTX ((uint16_t)0xFFFF) //!< Number of data to Transfer /****************** Bit definition for DMA_TXNUM2 register ******************/ #define DMA_TXNUM2_NDTX ((uint16_t)0xFFFF) //!< Number of data to Transfer /****************** Bit definition for DMA_TXNUM3 register ******************/ #define DMA_TXNUM3_NDTX ((uint16_t)0xFFFF) //!< Number of data to Transfer /****************** Bit definition for DMA_TXNUM4 register ******************/ #define DMA_TXNUM4_NDTX ((uint16_t)0xFFFF) //!< Number of data to Transfer /****************** Bit definition for DMA_TXNUM5 register ******************/ #define DMA_TXNUM5_NDTX ((uint16_t)0xFFFF) //!< Number of data to Transfer /****************** Bit definition for DMA_TXNUM6 register ******************/ #define DMA_TXNUM6_NDTX ((uint16_t)0xFFFF) //!< Number of data to Transfer /****************** Bit definition for DMA_TXNUM7 register ******************/ #define DMA_TXNUM7_NDTX ((uint16_t)0xFFFF) //!< Number of data to Transfer /****************** Bit definition for DMA_TXNUM8 register ******************/ #define DMA_TXNUM8_NDTX ((uint16_t)0xFFFF) //!< Number of data to Transfer /****************** Bit definition for DMA_PADDR1 register *******************/ #define DMA_PADDR1_ADDR ((uint32_t)0xFFFFFFFF) //!< Peripheral Address /****************** Bit definition for DMA_PADDR2 register *******************/ #define DMA_PADDR2_ADDR ((uint32_t)0xFFFFFFFF) //!< Peripheral Address /****************** Bit definition for DMA_PADDR3 register *******************/ #define DMA_PADDR3_ADDR ((uint32_t)0xFFFFFFFF) //!< Peripheral Address /****************** Bit definition for DMA_PADDR4 register *******************/ #define DMA_PADDR4_ADDR ((uint32_t)0xFFFFFFFF) //!< Peripheral Address /****************** Bit definition for DMA_PADDR5 register *******************/ #define DMA_PADDR5_ADDR ((uint32_t)0xFFFFFFFF) //!< Peripheral Address /****************** Bit definition for DMA_PADDR6 register *******************/ #define DMA_PADDR6_ADDR ((uint32_t)0xFFFFFFFF) //!< Peripheral Address /****************** Bit definition for DMA_PADDR7 register *******************/ #define DMA_PADDR7_ADDR ((uint32_t)0xFFFFFFFF) //!< Peripheral Address /****************** Bit definition for DMA_PADDR8 register *******************/ #define DMA_PADDR8_ADDR ((uint32_t)0xFFFFFFFF) //!< Peripheral Address /****************** Bit definition for DMA_MADDR1 register *******************/ #define DMA_MADDR1_ADDR ((uint32_t)0xFFFFFFFF) //!< Memory Address /****************** Bit definition for DMA_MADDR2 register *******************/ #define DMA_MADDR2_ADDR ((uint32_t)0xFFFFFFFF) //!< Memory Address /****************** Bit definition for DMA_MADDR3 register *******************/ #define DMA_MADDR3_ADDR ((uint32_t)0xFFFFFFFF) //!< Memory Address /****************** Bit definition for DMA_MADDR4 register *******************/ #define DMA_MADDR4_ADDR ((uint32_t)0xFFFFFFFF) //!< Memory Address /****************** Bit definition for DMA_MADDR5 register *******************/ #define DMA_MADDR5_ADDR ((uint32_t)0xFFFFFFFF) //!< Memory Address /****************** Bit definition for DMA_MADDR6 register *******************/ #define DMA_MADDR6_ADDR ((uint32_t)0xFFFFFFFF) //!< Memory Address /****************** Bit definition for DMA_MADDR7 register *******************/ #define DMA_MADDR7_ADDR ((uint32_t)0xFFFFFFFF) //!< Memory Address /****************** Bit definition for DMA_MADDR8 register *******************/ #define DMA_MADDR8_ADDR ((uint32_t)0xFFFFFFFF) //!< Memory Address /****************** Bit definition for DMA_CHSEL1 register *******************/ #define DMA_CHSEL1_CH_SEL ((uint32_t)0x0000003F) //!< Channel Select /****************** Bit definition for DMA_CHSEL2 register *******************/ #define DMA_CHSEL2_CH_SEL ((uint32_t)0x0000003F) //!< Channel Select /****************** Bit definition for DMA_CHSEL3 register *******************/ #define DMA_CHSEL3_CH_SEL ((uint32_t)0x0000003F) //!< Channel Select /****************** Bit definition for DMA_CHSEL4 register *******************/ #define DMA_CHSEL4_CH_SEL ((uint32_t)0x0000003F) //!< Channel Select /****************** Bit definition for DMA_CHSEL5 register *******************/ #define DMA_CHSEL5_CH_SEL ((uint32_t)0x0000003F) //!< Channel Select /****************** Bit definition for DMA_CHSEL6 register *******************/ #define DMA_CHSEL6_CH_SEL ((uint32_t)0x0000003F) //!< Channel Select /****************** Bit definition for DMA_CHSEL7 register *******************/ #define DMA_CHSEL7_CH_SEL ((uint32_t)0x0000003F) //!< Channel Select /****************** Bit definition for DMA_CHSEL8 register *******************/ #define DMA_CHSEL8_CH_SEL ((uint32_t)0x0000003F) //!< Channel Select /****************** Bit definition for DMA_CHMAPEN register *******************/ #define DMA_CHMAPEN_MAP_EN ((uint32_t)0x00000001) //!< Channel Map Enable /* DMA1 Channelx interrupt pending bit masks */ #define DMA1_CH1_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF1 | DMA_INTSTS_TXCF1 | DMA_INTSTS_HTXF1 | DMA_INTSTS_ERRF1)) #define DMA1_CH2_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF2 | DMA_INTSTS_TXCF2 | DMA_INTSTS_HTXF2 | DMA_INTSTS_ERRF2)) #define DMA1_CH3_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF3 | DMA_INTSTS_TXCF3 | DMA_INTSTS_HTXF3 | DMA_INTSTS_ERRF3)) #define DMA1_CH4_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF4 | DMA_INTSTS_TXCF4 | DMA_INTSTS_HTXF4 | DMA_INTSTS_ERRF4)) #define DMA1_CH5_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF5 | DMA_INTSTS_TXCF5 | DMA_INTSTS_HTXF5 | DMA_INTSTS_ERRF5)) #define DMA1_CH6_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF6 | DMA_INTSTS_TXCF6 | DMA_INTSTS_HTXF6 | DMA_INTSTS_ERRF6)) #define DMA1_CH7_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF7 | DMA_INTSTS_TXCF7 | DMA_INTSTS_HTXF7 | DMA_INTSTS_ERRF7)) #define DMA1_CH8_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF8 | DMA_INTSTS_TXCF8 | DMA_INTSTS_HTXF8 | DMA_INTSTS_ERRF8)) /* DMA2 Channelx interrupt pending bit masks */ #define DMA2_CH1_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF1 | DMA_INTSTS_TXCF1 | DMA_INTSTS_HTXF1 | DMA_INTSTS_ERRF1)) #define DMA2_CH2_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF2 | DMA_INTSTS_TXCF2 | DMA_INTSTS_HTXF2 | DMA_INTSTS_ERRF2)) #define DMA2_CH3_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF3 | DMA_INTSTS_TXCF3 | DMA_INTSTS_HTXF3 | DMA_INTSTS_ERRF3)) #define DMA2_CH4_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF4 | DMA_INTSTS_TXCF4 | DMA_INTSTS_HTXF4 | DMA_INTSTS_ERRF4)) #define DMA2_CH5_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF5 | DMA_INTSTS_TXCF5 | DMA_INTSTS_HTXF5 | DMA_INTSTS_ERRF5)) #define DMA2_CH6_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF6 | DMA_INTSTS_TXCF6 | DMA_INTSTS_HTXF6 | DMA_INTSTS_ERRF6)) #define DMA2_CH7_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF7 | DMA_INTSTS_TXCF7 | DMA_INTSTS_HTXF7 | DMA_INTSTS_ERRF7)) #define DMA2_CH8_INT_MASK ((uint32_t)(DMA_INTSTS_GLBF8 | DMA_INTSTS_TXCF8 | DMA_INTSTS_HTXF8 | DMA_INTSTS_ERRF8)) typedef struct { __IO uint32_t INTSTS; __IO uint32_t INTCLR; __IO DMA_ChannelType DMA_Channel[8]; __IO uint32_t CHMAPEN; } DMA_Module; #define RCC_AHB_PERIPH_ADC1 ((uint32_t)0x00001000) #define RCC_AHB_PERIPH_ADC2 ((uint32_t)0x00002000) #define RCC_AHB_PERIPH_ADC3 ((uint32_t)0x00004000) #define RCC_AHB_PERIPH_ADC4 ((uint32_t)0x00008000) void ADC_Init(ADC_Module* NS_ADCx, ADC_InitType* ADC_InitStruct); /**================================================================ * ADC reset ================================================================*/ void ADC_DeInit(ADC_Module* NS_ADCx); /**================================================================ * ADC module enable ================================================================*/ void ADC_Enable(ADC_Module* NS_ADCx, uint32_t Cmd); /**================================================================ * Get the ADC status logo bit ================================================================*/ uint32_t ADC_GetFlagStatusNew(ADC_Module* NS_ADCx, uint8_t ADC_FLAG_NEW); /**================================================================ * Open ADC calibration ================================================================*/ void ADC_StartCalibration(ADC_Module* NS_ADCx); /**================================================================ * Enable ADC DMA ================================================================*/ void ADC_EnableDMA(ADC_Module* NS_ADCx, uint32_t Cmd); /**================================================================ * Configure ADC interrupt enable enable ================================================================*/ void ADC_ConfigInt(ADC_Module* NS_ADCx, uint16_t ADC_IT, uint32_t Cmd); /**================================================================ * Get ADC calibration status ================================================================*/ uint32_t ADC_GetCalibrationStatus(ADC_Module* NS_ADCx); /**================================================================ * Configure the ADC channel ================================================================*/ void ADC_ConfigRegularChannel(ADC_Module* NS_ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime); /**================================================================ * Start ADC conversion ================================================================*/ void ADC_EnableSoftwareStartConv(ADC_Module* NS_ADCx, uint32_t Cmd); /**================================================================ * Get the ADC status logo bit ================================================================*/ uint32_t ADC_GetFlagStatus(ADC_Module* NS_ADCx, uint8_t ADC_FLAG); /**================================================================ * Clear status logo bit ================================================================*/ void ADC_ClearFlag(ADC_Module* NS_ADCx, uint8_t ADC_FLAG); /**================================================================ * Get ADC sampling value ================================================================*/ uint16_t ADC_GetDat(ADC_Module* NS_ADCx); //////////////////////////////////////////////////////////////////////////////// typedef struct { __IO uint32_t CR; /* Completely compatible */ __IO uint32_t CFGR; /* Not compatible: ADC frequency is not set here */ __IO uint32_t CIR; /* Completely compatible */ __IO uint32_t APB2RSTR; /* Completely compatible */ __IO uint32_t APB1RSTR; /* Completely compatible */ __IO uint32_t AHBENR; /* Not compatible: ADC clock enables settings here */ __IO uint32_t APB2ENR; /* Not compatible: ADC clock enables to be here */ __IO uint32_t APB1ENR; /* compatible */ __IO uint32_t BDCR; /* compatible */ __IO uint32_t CSR; /* compatible */ __IO uint32_t AHBRSTR; /* Not compatible, ADC reset here settings */ __IO uint32_t CFGR2; /* Not compatible, ADC clock settings here */ __IO uint32_t CFGR3; /* Not compatible, add a new register */ } RCC_TypeDef; #define RCC ((RCC_TypeDef *) ADC_RCC_BASE) /**================================================================ * Initialize ADC clock ================================================================*/ void enable_adc_clk(uint8_t cmd); /**================================================================ * Initialize ADC peripheral parameters ================================================================*/ void ADC_Initial(ADC_Module* NS_ADCx); /**================================================================ * Single independent sampling ================================================================*/ uint16_t ADC_GetData(ADC_Module* NS_ADCx, uint8_t ADC_Channel); void DMA_DeInit(DMA_ChannelType* DMAyChx); #define CCR_CLEAR_Mask ((uint32_t)0xFFFF800F) void DMA_Init(DMA_ChannelType* DMAyChx, DMA_InitType* DMA_InitParam); void DMA_EnableChannel(DMA_ChannelType* DMAyChx, uint32_t Cmd); #define USE_ADC NS_ADC2 #define USE_DMA_CH DMA1_CH8 /**================================================================ * Initialize the DMA of ADC ================================================================*/ void ADC_DMA_init();
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/HAL_N32.h
C
agpl-3.0
44,323
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Software SPI functions originally from Arduino Sd2Card Library * Copyright (c) 2009 by William Greiman * Adapted to the STM32F1 HAL */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #include <SPI.h> // ------------------------ // Public functions // ------------------------ #if ENABLED(SOFTWARE_SPI) // ------------------------ // Software SPI // ------------------------ #error "Software SPI not supported for STM32F1. Use hardware SPI." #else // ------------------------ // Hardware SPI // ------------------------ /** * VGPV SPI speed start and F_CPU/2, by default 72/2 = 36Mhz */ /** * @brief Begin SPI port setup * * @return Nothing * * @details Only configures SS pin since libmaple creates and initialize the SPI object */ void spiBegin() { #if PIN_EXISTS(SD_SS) OUT_WRITE(SD_SS_PIN, HIGH); #endif } /** * @brief Initialize SPI port to required speed rate and transfer mode (MSB, SPI MODE 0) * * @param spiRate Rate as declared in HAL.h (speed do not match AVR) * @return Nothing * * @details */ void spiInit(uint8_t spiRate) { /** * STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz * STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1 * so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2 */ #if SPI_DEVICE == 1 #define SPI_CLOCK_MAX SPI_CLOCK_DIV4 #else #define SPI_CLOCK_MAX SPI_CLOCK_DIV2 #endif uint8_t clock; switch (spiRate) { case SPI_FULL_SPEED: clock = SPI_CLOCK_MAX ; break; case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4 ; break; case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8 ; break; case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break; case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break; case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break; default: clock = SPI_CLOCK_DIV2; // Default from the SPI library } SPI.setModule(SPI_DEVICE); SPI.begin(); SPI.setClockDivider(clock); SPI.setBitOrder(MSBFIRST); SPI.setDataMode(SPI_MODE0); } /** * @brief Receive a single byte from the SPI port. * * @return Byte received * * @details */ uint8_t spiRec() { uint8_t returnByte = SPI.transfer(0xFF); return returnByte; } /** * @brief Receive a number of bytes from the SPI port to a buffer * * @param buf Pointer to starting address of buffer to write to. * @param nbyte Number of bytes to receive. * @return Nothing * * @details Uses DMA */ void spiRead(uint8_t *buf, uint16_t nbyte) { SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte); } /** * @brief Send a single byte on SPI port * * @param b Byte to send * * @details */ void spiSend(uint8_t b) { SPI.send(b); } /** * @brief Write token and then write from 512 byte buffer to SPI (for SD card) * * @param buf Pointer with buffer start address * @return Nothing * * @details Use DMA */ void spiSendBlock(uint8_t token, const uint8_t *buf) { SPI.send(token); SPI.dmaSend(const_cast<uint8_t*>(buf), 512); } #if ENABLED(SPI_EEPROM) // Read single byte from specified SPI channel uint8_t spiRec(uint32_t chan) { return SPI.transfer(0xFF); } // Write single byte to specified SPI channel void spiSend(uint32_t chan, byte b) { SPI.send(b); } // Write buffer to specified SPI channel void spiSend(uint32_t chan, const uint8_t *buf, size_t n) { for (size_t p = 0; p < n; p++) spiSend(chan, buf[p]); } #endif // SPI_EEPROM #endif // SOFTWARE_SPI #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/HAL_SPI.cpp
C++
agpl-3.0
4,355
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <SPI.h> /** * Marlin currently requires 3 SPI classes: * * SPIClass: * This class is normally provided by frameworks and has a semi-default interface. * This is needed because some libraries reference it globally. * * SPISettings: * Container for SPI configs for SPIClass. As above, libraries may reference it globally. * * These two classes are often provided by frameworks so we cannot extend them to add * useful methods for Marlin. * * MarlinSPI: * Provides the default SPIClass interface plus some Marlin goodies such as a simplified * interface for SPI DMA transfer. * */ using MarlinSPI = SPIClass;
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/MarlinSPI.h
C
agpl-3.0
1,515
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #include "MarlinSerial.h" #include <libmaple/usart.h> // Copied from ~/.platformio/packages/framework-arduinoststm32-maple/STM32F1/system/libmaple/usart_private.h // Changed to handle Emergency Parser FORCE_INLINE void my_usart_irq(ring_buffer *rb, ring_buffer *wb, usart_reg_map *regs, MSerialT &serial) { /* Handle RXNEIE and TXEIE interrupts. * RXNE signifies availability of a byte in DR. * * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15. * We enable RXNEIE. */ uint32_t srflags = regs->SR, cr1its = regs->CR1; if ((cr1its & USART_CR1_RXNEIE) && (srflags & USART_SR_RXNE)) { if (srflags & USART_SR_FE || srflags & USART_SR_PE ) { // framing error or parity error regs->DR; // Read and throw away the data, which also clears FE and PE } else { uint8_t c = (uint8)regs->DR; #ifdef USART_SAFE_INSERT // If the buffer is full and the user defines USART_SAFE_INSERT, // ignore new bytes. rb_safe_insert(rb, c); #else // By default, push bytes around in the ring buffer. rb_push_insert(rb, c); #endif #if ENABLED(EMERGENCY_PARSER) if (serial.emergency_parser_enabled()) emergency_parser.update(serial.emergency_state, c); #endif } } else if (srflags & USART_SR_ORE) { // overrun and empty data, just do a dummy read to clear ORE // and prevent a raise condition where a continuous interrupt stream (due to ORE set) occurs // (see chapter "Overrun error" ) in STM32 reference manual regs->DR; } // TXE signifies readiness to send a byte to DR. if ((cr1its & USART_CR1_TXEIE) && (srflags & USART_SR_TXE)) { if (!rb_is_empty(wb)) regs->DR=rb_remove(wb); else regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE } } // Not every MarlinSerial port should handle emergency parsing. // It would not make sense to parse G-Code from TMC responses, for example. constexpr bool serial_handles_emergency(int port) { return (false #ifdef SERIAL_PORT || (SERIAL_PORT) == port #endif #ifdef SERIAL_PORT_2 || (SERIAL_PORT_2) == port #endif #ifdef LCD_SERIAL_PORT || (LCD_SERIAL_PORT) == port #endif ); } #define DEFINE_HWSERIAL_MARLIN(name, n) \ MSerialT name(serial_handles_emergency(n),\ USART##n, \ BOARD_USART##n##_TX_PIN, \ BOARD_USART##n##_RX_PIN); \ extern "C" void __irq_usart##n(void) { \ my_usart_irq(USART##n->rb, USART##n->wb, USART##n##_BASE, MSerial##n); \ } #define DEFINE_HWSERIAL_UART_MARLIN(name, n) \ MSerialT name(serial_handles_emergency(n), \ UART##n, \ BOARD_USART##n##_TX_PIN, \ BOARD_USART##n##_RX_PIN); \ extern "C" void __irq_usart##n(void) { \ my_usart_irq(UART##n->rb, UART##n->wb, UART##n##_BASE, MSerial##n); \ } // Instantiate all UARTs even if they are not needed // This avoids a bunch of logic to figure out every serial // port which may be in use on the system. #if DISABLED(MKS_WIFI_MODULE) DEFINE_HWSERIAL_MARLIN(MSerial1, 1); #endif DEFINE_HWSERIAL_MARLIN(MSerial2, 2); DEFINE_HWSERIAL_MARLIN(MSerial3, 3); #if ANY(STM32_HIGH_DENSITY, STM32_XL_DENSITY) DEFINE_HWSERIAL_UART_MARLIN(MSerial4, 4); DEFINE_HWSERIAL_UART_MARLIN(MSerial5, 5); #endif // Check the type of each serial port by passing it to a template function. // HardwareSerial is known to sometimes hang the controller when an error occurs, // so this case will fail the static assert. All other classes are assumed to be ok. template <typename T> constexpr bool IsSerialClassAllowed(const T&) { return true; } constexpr bool IsSerialClassAllowed(const HardwareSerial&) { return false; } #define CHECK_CFG_SERIAL(A) static_assert(IsSerialClassAllowed(A), STRINGIFY(A) " is defined incorrectly"); #define CHECK_AXIS_SERIAL(A) static_assert(IsSerialClassAllowed(A##_HARDWARE_SERIAL), STRINGIFY(A) "_HARDWARE_SERIAL must be defined in the form MSerial1, rather than Serial1"); // If you encounter this error, replace SerialX with MSerialX, for example MSerial3. // Non-TMC ports were already validated in HAL.h, so do not require verbose error messages. #ifdef MYSERIAL1 CHECK_CFG_SERIAL(MYSERIAL1); #endif #ifdef MYSERIAL2 CHECK_CFG_SERIAL(MYSERIAL2); #endif #ifdef LCD_SERIAL CHECK_CFG_SERIAL(LCD_SERIAL); #endif #if AXIS_HAS_HW_SERIAL(X) CHECK_AXIS_SERIAL(X); #endif #if AXIS_HAS_HW_SERIAL(X2) CHECK_AXIS_SERIAL(X2); #endif #if AXIS_HAS_HW_SERIAL(Y) CHECK_AXIS_SERIAL(Y); #endif #if AXIS_HAS_HW_SERIAL(Y2) CHECK_AXIS_SERIAL(Y2); #endif #if AXIS_HAS_HW_SERIAL(Z) CHECK_AXIS_SERIAL(Z); #endif #if AXIS_HAS_HW_SERIAL(Z2) CHECK_AXIS_SERIAL(Z2); #endif #if AXIS_HAS_HW_SERIAL(Z3) CHECK_AXIS_SERIAL(Z3); #endif #if AXIS_HAS_HW_SERIAL(Z4) CHECK_AXIS_SERIAL(Z4); #endif #if AXIS_HAS_HW_SERIAL(I) CHECK_AXIS_SERIAL(I); #endif #if AXIS_HAS_HW_SERIAL(J) CHECK_AXIS_SERIAL(J); #endif #if AXIS_HAS_HW_SERIAL(K) CHECK_AXIS_SERIAL(K); #endif #if AXIS_HAS_HW_SERIAL(E0) CHECK_AXIS_SERIAL(E0); #endif #if AXIS_HAS_HW_SERIAL(E1) CHECK_AXIS_SERIAL(E1); #endif #if AXIS_HAS_HW_SERIAL(E2) CHECK_AXIS_SERIAL(E2); #endif #if AXIS_HAS_HW_SERIAL(E3) CHECK_AXIS_SERIAL(E3); #endif #if AXIS_HAS_HW_SERIAL(E4) CHECK_AXIS_SERIAL(E4); #endif #if AXIS_HAS_HW_SERIAL(E5) CHECK_AXIS_SERIAL(E5); #endif #if AXIS_HAS_HW_SERIAL(E6) CHECK_AXIS_SERIAL(E6); #endif #if AXIS_HAS_HW_SERIAL(E7) CHECK_AXIS_SERIAL(E7); #endif #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/MarlinSerial.cpp
C++
agpl-3.0
6,499
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <HardwareSerial.h> #include <libmaple/usart.h> #include <WString.h> #include "../../inc/MarlinConfigPre.h" #include "../../core/serial_hook.h" // Increase priority of serial interrupts, to reduce overflow errors #define UART_IRQ_PRIO 1 struct MarlinSerial : public HardwareSerial { MarlinSerial(struct usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin) : HardwareSerial(usart_device, tx_pin, rx_pin) { } #ifdef UART_IRQ_PRIO // Shadow the parent methods to set IRQ priority after begin() void begin(uint32 baud) { MarlinSerial::begin(baud, SERIAL_8N1); } void begin(uint32 baud, uint8_t config) { HardwareSerial::begin(baud, config); nvic_irq_set_priority(c_dev()->irq_num, UART_IRQ_PRIO); } #endif }; typedef Serial1Class<MarlinSerial> MSerialT; extern MSerialT MSerial1; extern MSerialT MSerial2; extern MSerialT MSerial3; #if ANY(STM32_HIGH_DENSITY, STM32_XL_DENSITY) extern MSerialT MSerial4; extern MSerialT MSerial5; #endif
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/MarlinSerial.h
C
agpl-3.0
1,872
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../inc/MarlinConfigPre.h" #if ENABLED(POSTMORTEM_DEBUGGING) #include "../shared/MinSerial.h" #include <libmaple/usart.h> #include <libmaple/rcc.h> #include <libmaple/nvic.h> /* Instruction Synchronization Barrier */ #define isb() __asm__ __volatile__ ("isb" : : : "memory") /* Data Synchronization Barrier */ #define dsb() __asm__ __volatile__ ("dsb" : : : "memory") static void TXBegin() { #if !WITHIN(SERIAL_PORT, 1, 6) #warning "Using POSTMORTEM_DEBUGGING requires a physical U(S)ART hardware in case of severe error." #warning "Disabling the severe error reporting feature currently because the used serial port is not a HW port." #else // We use MYSERIAL1 here, so we need to figure out how to get the linked register struct usart_dev* dev = MYSERIAL1.c_dev(); // Or use this if removing libmaple // int irq = dev->irq_num; // int nvicUART[] = { NVIC_USART1 /* = 37 */, NVIC_USART2 /* = 38 */, NVIC_USART3 /* = 39 */, NVIC_UART4 /* = 52 */, NVIC_UART5 /* = 53 */ }; // Disabling irq means setting the bit in the NVIC ICER register located at // Disable UART interrupt in NVIC nvic_irq_disable(dev->irq_num); // Use this if removing libmaple //SBI(NVIC_BASE->ICER[1], irq - 32); // We NEED memory barriers to ensure Interrupts are actually disabled! // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the ) dsb(); isb(); rcc_clk_disable(dev->clk_id); rcc_clk_enable(dev->clk_id); usart_reg_map *regs = dev->regs; regs->CR1 = 0; // Reset the USART regs->CR2 = 0; // 1 stop bit // If we don't touch the BRR (baudrate register), we don't need to recompute. Else we would need to call usart_set_baud_rate(dev, 0, BAUDRATE); regs->CR1 = (USART_CR1_TE | USART_CR1_UE); // 8 bits, no parity, 1 stop bit #endif } // A SW memory barrier, to ensure GCC does not overoptimize loops #define sw_barrier() __asm__ volatile("": : :"memory"); static void TX(char c) { #if WITHIN(SERIAL_PORT, 1, 6) struct usart_dev* dev = MYSERIAL1.c_dev(); while (!(dev->regs->SR & USART_SR_TXE)) { hal.watchdog_refresh(); sw_barrier(); } dev->regs->DR = c; #endif } void install_min_serial() { HAL_min_serial_init = &TXBegin; HAL_min_serial_out = &TX; } #if DISABLED(DYNAMIC_VECTORTABLE) && DISABLED(STM32F0xx) // Cortex M0 can't branch to a symbol that's too far, so we have a specific hack for them extern "C" { __attribute__((naked)) void JumpHandler_ASM() { __asm__ __volatile__ ( "b CommonHandler_ASM\n" ); } void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_hardfault(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_busfault(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_usagefault(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_memmanage(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_nmi(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception7(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception8(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception9(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception10(); void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception13(); } #endif #endif // POSTMORTEM_DEBUGGING #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/MinSerial.cpp
C++
agpl-3.0
4,430
/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *****************************************************************************/ /** * @author Marti Bolivar <mbolivar@leaflabs.com> * @brief Wirish SPI implementation. */ #ifdef __STM32F1__ #include <SPI.h> #include <libmaple/timer.h> #include <libmaple/util.h> #include <libmaple/rcc.h> #include <boards.h> #include <wirish.h> #include "../../inc/MarlinConfig.h" #include "spi_pins.h" /** Time in ms for DMA receive timeout */ #define DMA_TIMEOUT 100 #if CYCLES_PER_MICROSECOND != 72 #warning "Unexpected clock speed; SPI frequency calculation will be incorrect" #endif struct spi_pins { uint8_t nss, sck, miso, mosi; }; static const spi_pins* dev_to_spi_pins(spi_dev *dev); static void configure_gpios(spi_dev *dev, bool as_master); static spi_baud_rate determine_baud_rate(spi_dev *dev, uint32_t freq); #if BOARD_NR_SPI >= 3 && !defined(STM32_HIGH_DENSITY) #error "The SPI library is misconfigured: 3 SPI ports only available on high density STM32 devices" #endif static const spi_pins board_spi_pins[] __FLASH__ = { #if BOARD_NR_SPI >= 1 { BOARD_SPI1_NSS_PIN, BOARD_SPI1_SCK_PIN, BOARD_SPI1_MISO_PIN, BOARD_SPI1_MOSI_PIN }, #endif #if BOARD_NR_SPI >= 2 { BOARD_SPI2_NSS_PIN, BOARD_SPI2_SCK_PIN, BOARD_SPI2_MISO_PIN, BOARD_SPI2_MOSI_PIN }, #endif #if BOARD_NR_SPI >= 3 { BOARD_SPI3_NSS_PIN, BOARD_SPI3_SCK_PIN, BOARD_SPI3_MISO_PIN, BOARD_SPI3_MOSI_PIN }, #endif }; #if BOARD_NR_SPI >= 1 static void *_spi1_this; #endif #if BOARD_NR_SPI >= 2 static void *_spi2_this; #endif #if BOARD_NR_SPI >= 3 static void *_spi3_this; #endif /** * @brief Wait until TXE (tx empty) flag is set and BSY (busy) flag unset. */ static inline void waitSpiTxEnd(spi_dev *spi_d) { while (spi_is_tx_empty(spi_d) == 0) { /* nada */ } // wait until TXE=1 while (spi_is_busy(spi_d) != 0) { /* nada */ } // wait until BSY=0 } /** * Constructor */ SPIClass::SPIClass(uint32_t spi_num) { _currentSetting = &_settings[spi_num - 1]; // SPI channels are called 1 2 and 3 but the array is zero indexed switch (spi_num) { #if BOARD_NR_SPI >= 1 case 1: _currentSetting->spi_d = SPI1; _spi1_this = (void*)this; break; #endif #if BOARD_NR_SPI >= 2 case 2: _currentSetting->spi_d = SPI2; _spi2_this = (void*)this; break; #endif #if BOARD_NR_SPI >= 3 case 3: _currentSetting->spi_d = SPI3; _spi3_this = (void*)this; break; #endif default: ASSERT(0); } // Init things specific to each SPI device // clock divider setup is a bit of hack, and needs to be improved at a later date. #if BOARD_NR_SPI >= 1 _settings[0].spi_d = SPI1; _settings[0].clockDivider = determine_baud_rate(_settings[0].spi_d, _settings[0].clock); _settings[0].spiDmaDev = DMA1; _settings[0].spiTxDmaChannel = DMA_CH3; _settings[0].spiRxDmaChannel = DMA_CH2; #endif #if BOARD_NR_SPI >= 2 _settings[1].spi_d = SPI2; _settings[1].clockDivider = determine_baud_rate(_settings[1].spi_d, _settings[1].clock); _settings[1].spiDmaDev = DMA1; _settings[1].spiTxDmaChannel = DMA_CH5; _settings[1].spiRxDmaChannel = DMA_CH4; #endif #if BOARD_NR_SPI >= 3 _settings[2].spi_d = SPI3; _settings[2].clockDivider = determine_baud_rate(_settings[2].spi_d, _settings[2].clock); _settings[2].spiDmaDev = DMA2; _settings[2].spiTxDmaChannel = DMA_CH2; _settings[2].spiRxDmaChannel = DMA_CH1; #endif // added for DMA callbacks. _currentSetting->state = SPI_STATE_IDLE; } SPIClass::SPIClass(int8_t mosi, int8_t miso, int8_t sclk, int8_t ssel) : SPIClass(1) { #if BOARD_NR_SPI >= 1 if (mosi == BOARD_SPI1_MOSI_PIN) setModule(1); #endif #if BOARD_NR_SPI >= 2 if (mosi == BOARD_SPI2_MOSI_PIN) setModule(2); #endif #if BOARD_NR_SPI >= 3 if (mosi == BOARD_SPI3_MOSI_PIN) setModule(3); #endif } /** * Set up/tear down */ void SPIClass::updateSettings() { uint32_t flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | _currentSetting->dataSize | SPI_SW_SLAVE | SPI_SOFT_SS); spi_master_enable(_currentSetting->spi_d, (spi_baud_rate)_currentSetting->clockDivider, (spi_mode)_currentSetting->dataMode, flags); } void SPIClass::begin() { spi_init(_currentSetting->spi_d); configure_gpios(_currentSetting->spi_d, 1); updateSettings(); // added for DMA callbacks. _currentSetting->state = SPI_STATE_READY; } void SPIClass::beginSlave() { spi_init(_currentSetting->spi_d); configure_gpios(_currentSetting->spi_d, 0); uint32_t flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | _currentSetting->dataSize); spi_slave_enable(_currentSetting->spi_d, (spi_mode)_currentSetting->dataMode, flags); // added for DMA callbacks. _currentSetting->state = SPI_STATE_READY; } void SPIClass::end() { if (!spi_is_enabled(_currentSetting->spi_d)) return; // Follows RM0008's sequence for disabling a SPI in master/slave // full duplex mode. while (spi_is_rx_nonempty(_currentSetting->spi_d)) { // FIXME [0.1.0] remove this once you have an interrupt based driver volatile uint16_t rx __attribute__((unused)) = spi_rx_reg(_currentSetting->spi_d); } waitSpiTxEnd(_currentSetting->spi_d); spi_peripheral_disable(_currentSetting->spi_d); // added for DMA callbacks. // Need to add unsetting the callbacks for the DMA channels. _currentSetting->state = SPI_STATE_IDLE; } /* Roger Clark added 3 functions */ void SPIClass::setClockDivider(uint32_t clockDivider) { _currentSetting->clockDivider = clockDivider; uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_BR); _currentSetting->spi_d->regs->CR1 = cr1 | (clockDivider & SPI_CR1_BR); } void SPIClass::setBitOrder(BitOrder bitOrder) { _currentSetting->bitOrder = bitOrder; uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_LSBFIRST); if (bitOrder == LSBFIRST) cr1 |= SPI_CR1_LSBFIRST; _currentSetting->spi_d->regs->CR1 = cr1; } /** * Victor Perez. Added to test changing datasize from 8 to 16 bit modes on the fly. * Input parameter should be SPI_CR1_DFF set to 0 or 1 on a 32bit word. */ void SPIClass::setDataSize(uint32_t datasize) { _currentSetting->dataSize = datasize; uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_DFF); uint8_t en = spi_is_enabled(_currentSetting->spi_d); spi_peripheral_disable(_currentSetting->spi_d); _currentSetting->spi_d->regs->CR1 = cr1 | (datasize & SPI_CR1_DFF) | en; } void SPIClass::setDataMode(uint8_t dataMode) { /** * Notes: * As far as we know the AVR numbers for dataMode match the numbers required by the STM32. * From the AVR doc https://www.atmel.com/images/doc2585.pdf section 2.4 * * SPI Mode CPOL CPHA Shift SCK-edge Capture SCK-edge * 0 0 0 Falling Rising * 1 0 1 Rising Falling * 2 1 0 Rising Falling * 3 1 1 Falling Rising * * On the STM32 it appears to be * * bit 1 - CPOL : Clock polarity * (This bit should not be changed when communication is ongoing) * 0 : CLK to 0 when idle * 1 : CLK to 1 when idle * * bit 0 - CPHA : Clock phase * (This bit should not be changed when communication is ongoing) * 0 : The first clock transition is the first data capture edge * 1 : The second clock transition is the first data capture edge * * If someone finds this is not the case or sees a logic error with this let me know ;-) */ _currentSetting->dataMode = dataMode; uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_CPOL|SPI_CR1_CPHA); _currentSetting->spi_d->regs->CR1 = cr1 | (dataMode & (SPI_CR1_CPOL|SPI_CR1_CPHA)); } void SPIClass::beginTransaction(uint8_t pin, const SPISettings &settings) { setBitOrder(settings.bitOrder); setDataMode(settings.dataMode); setDataSize(settings.dataSize); setClockDivider(determine_baud_rate(_currentSetting->spi_d, settings.clock)); begin(); } void SPIClass::beginTransactionSlave(const SPISettings &settings) { setBitOrder(settings.bitOrder); setDataMode(settings.dataMode); setDataSize(settings.dataSize); beginSlave(); } void SPIClass::endTransaction() { } /** * I/O */ uint16_t SPIClass::read() { while (!spi_is_rx_nonempty(_currentSetting->spi_d)) { /* nada */ } return (uint16_t)spi_rx_reg(_currentSetting->spi_d); } void SPIClass::read(uint8_t *buf, uint32_t len) { if (len == 0) return; spi_rx_reg(_currentSetting->spi_d); // clear the RX buffer in case a byte is waiting on it. spi_reg_map * regs = _currentSetting->spi_d->regs; // start sequence: write byte 0 regs->DR = 0x00FF; // write the first byte // main loop while (--len) { while (!(regs->SR & SPI_SR_TXE)) { /* nada */ } // wait for TXE flag noInterrupts(); // go atomic level - avoid interrupts to surely get the previously received data regs->DR = 0x00FF; // write the next data item to be transmitted into the SPI_DR register. This clears the TXE flag. while (!(regs->SR & SPI_SR_RXNE)) { /* nada */ } // wait till data is available in the DR register *buf++ = (uint8)(regs->DR); // read and store the received byte. This clears the RXNE flag. interrupts(); // let systick do its job } // read remaining last byte while (!(regs->SR & SPI_SR_RXNE)) { /* nada */ } // wait till data is available in the Rx register *buf++ = (uint8)(regs->DR); // read and store the received byte } void SPIClass::write(uint16_t data) { /* Added for 16bit data Victor Perez. Roger Clark * Improved speed by just directly writing the single byte to the SPI data reg and wait for completion, * by taking the Tx code from transfer(byte) * This almost doubles the speed of this function. */ spi_tx_reg(_currentSetting->spi_d, data); // write the data to be transmitted into the SPI_DR register (this clears the TXE flag) waitSpiTxEnd(_currentSetting->spi_d); } void SPIClass::write16(uint16_t data) { // Added by stevestrong: write two consecutive bytes in 8 bit mode (DFF=0) spi_tx_reg(_currentSetting->spi_d, data>>8); // write high byte while (!spi_is_tx_empty(_currentSetting->spi_d)) { /* nada */ } // Wait until TXE=1 spi_tx_reg(_currentSetting->spi_d, data); // write low byte waitSpiTxEnd(_currentSetting->spi_d); } void SPIClass::write(uint16_t data, uint32_t n) { // Added by stevstrong: Repeatedly send same data by the specified number of times spi_reg_map * regs = _currentSetting->spi_d->regs; while (n--) { regs->DR = data; // write the data to be transmitted into the SPI_DR register (this clears the TXE flag) while (!(regs->SR & SPI_SR_TXE)) { /* nada */ } // wait till Tx empty } while (regs->SR & SPI_SR_BSY) { /* nada */ } // wait until BSY=0 before returning } void SPIClass::write(const void *data, uint32_t length) { spi_dev * spi_d = _currentSetting->spi_d; spi_tx(spi_d, data, length); // data can be array of bytes or words waitSpiTxEnd(spi_d); } uint8_t SPIClass::transfer(uint8_t byte) const { spi_dev * spi_d = _currentSetting->spi_d; spi_rx_reg(spi_d); // read any previous data spi_tx_reg(spi_d, byte); // Write the data item to be transmitted into the SPI_DR register waitSpiTxEnd(spi_d); return (uint8)spi_rx_reg(spi_d); // "... and read the last received data." } uint16_t SPIClass::transfer16(uint16_t data) const { // Modified by stevestrong: write & read two consecutive bytes in 8 bit mode (DFF=0) // This is more effective than two distinct byte transfers spi_dev * spi_d = _currentSetting->spi_d; spi_rx_reg(spi_d); // read any previous data spi_tx_reg(spi_d, data>>8); // write high byte waitSpiTxEnd(spi_d); // wait until TXE=1 and then wait until BSY=0 uint16_t ret = spi_rx_reg(spi_d)<<8; // read and shift high byte spi_tx_reg(spi_d, data); // write low byte waitSpiTxEnd(spi_d); // wait until TXE=1 and then wait until BSY=0 ret += spi_rx_reg(spi_d); // read low byte return ret; } /** * Roger Clark and Victor Perez, 2015 * Performs a DMA SPI transfer with at least a receive buffer. * If a TX buffer is not provided, FF is sent over and over for the length of the transfer. * On exit TX buffer is not modified, and RX buffer contains the received data. * Still in progress. */ void SPIClass::dmaTransferSet(const void *transmitBuf, void *receiveBuf) { dma_init(_currentSetting->spiDmaDev); //spi_rx_dma_enable(_currentSetting->spi_d); //spi_tx_dma_enable(_currentSetting->spi_d); dma_xfer_size dma_bit_size = (_currentSetting->dataSize==DATA_SIZE_16BIT) ? DMA_SIZE_16BITS : DMA_SIZE_8BITS; dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size, receiveBuf, dma_bit_size, (DMA_MINC_MODE | DMA_TRNS_CMPLT ));// receive buffer DMA if (!transmitBuf) { transmitBuf = &ff; dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size, (volatile void*)transmitBuf, dma_bit_size, (DMA_FROM_MEM));// Transmit FF repeatedly } else { dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size, (volatile void*)transmitBuf, dma_bit_size, (DMA_MINC_MODE | DMA_FROM_MEM ));// Transmit buffer DMA } dma_set_priority(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, DMA_PRIORITY_LOW); dma_set_priority(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, DMA_PRIORITY_VERY_HIGH); } uint8_t SPIClass::dmaTransferRepeat(uint16_t length) { if (length == 0) return 0; if (spi_is_rx_nonempty(_currentSetting->spi_d) == 1) spi_rx_reg(_currentSetting->spi_d); _currentSetting->state = SPI_STATE_TRANSFER; dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, length); dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length); dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);// enable receive dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit spi_rx_dma_enable(_currentSetting->spi_d); spi_tx_dma_enable(_currentSetting->spi_d); if (_currentSetting->receiveCallback) return 0; //uint32_t m = millis(); uint8_t b = 0; uint32_t m = millis(); while (!(dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & DMA_ISR_TCIF1)) { // Avoid interrupts and just loop waiting for the flag to be set. if ((millis() - m) > DMA_TIMEOUT) { b = 2; break; } } waitSpiTxEnd(_currentSetting->spi_d); // until TXE=1 and BSY=0 spi_tx_dma_disable(_currentSetting->spi_d); spi_rx_dma_disable(_currentSetting->spi_d); dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel); dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel); dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); _currentSetting->state = SPI_STATE_READY; return b; } /** * Roger Clark and Victor Perez, 2015 * Performs a DMA SPI transfer with at least a receive buffer. * If a TX buffer is not provided, FF is sent over and over for the length of the transfer. * On exit TX buffer is not modified, and RX buffer contains the received data. * Still in progress. */ uint8_t SPIClass::dmaTransfer(const void *transmitBuf, void *receiveBuf, uint16_t length) { dmaTransferSet(transmitBuf, receiveBuf); return dmaTransferRepeat(length); } /** * Roger Clark and Victor Perez, 2015 * Performs a DMA SPI send using a TX buffer. * On exit TX buffer is not modified. * Still in progress. * 2016 - stevstrong - reworked to automatically detect bit size from SPI setting */ void SPIClass::dmaSendSet(const void * transmitBuf, bool minc) { uint32_t flags = ( (DMA_MINC_MODE*minc) | DMA_FROM_MEM | DMA_TRNS_CMPLT); dma_init(_currentSetting->spiDmaDev); dma_xfer_size dma_bit_size = (_currentSetting->dataSize==DATA_SIZE_16BIT) ? DMA_SIZE_16BITS : DMA_SIZE_8BITS; dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size, (volatile void*)transmitBuf, dma_bit_size, flags);// Transmit buffer DMA dma_set_priority(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, DMA_PRIORITY_LOW); } uint8_t SPIClass::dmaSendRepeat(uint16_t length) { if (length == 0) return 0; dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length); _currentSetting->state = SPI_STATE_TRANSMIT; dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); // enable transmit spi_tx_dma_enable(_currentSetting->spi_d); if (_currentSetting->transmitCallback) return 0; uint32_t m = millis(); uint8_t b = 0; while (!(dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & DMA_ISR_TCIF1)) { // Avoid interrupts and just loop waiting for the flag to be set. if ((millis() - m) > DMA_TIMEOUT) { b = 2; break; } } waitSpiTxEnd(_currentSetting->spi_d); // until TXE=1 and BSY=0 spi_tx_dma_disable(_currentSetting->spi_d); dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); _currentSetting->state = SPI_STATE_READY; return b; } uint8_t SPIClass::dmaSend(const void * transmitBuf, uint16_t length, bool minc) { dmaSendSet(transmitBuf, minc); return dmaSendRepeat(length); } uint8_t SPIClass::dmaSendAsync(const void * transmitBuf, uint16_t length, bool minc) { uint8_t b = 0; if (_currentSetting->state != SPI_STATE_READY) { uint32_t m = millis(); while (!(dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & DMA_ISR_TCIF1)) { //Avoid interrupts and just loop waiting for the flag to be set. //delayMicroseconds(10); if ((millis() - m) > DMA_TIMEOUT) { b = 2; break; } } waitSpiTxEnd(_currentSetting->spi_d); // until TXE=1 and BSY=0 spi_tx_dma_disable(_currentSetting->spi_d); dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); _currentSetting->state = SPI_STATE_READY; } if (length == 0) return 0; uint32_t flags = ( (DMA_MINC_MODE*minc) | DMA_FROM_MEM | DMA_TRNS_CMPLT); dma_init(_currentSetting->spiDmaDev); // TX dma_xfer_size dma_bit_size = (_currentSetting->dataSize==DATA_SIZE_16BIT) ? DMA_SIZE_16BITS : DMA_SIZE_8BITS; dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size, (volatile void*)transmitBuf, dma_bit_size, flags);// Transmit buffer DMA dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length); dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit spi_tx_dma_enable(_currentSetting->spi_d); _currentSetting->state = SPI_STATE_TRANSMIT; return b; } /** * New functions added to manage callbacks. * Victor Perez 2017 */ void SPIClass::onReceive(void(*callback)()) { _currentSetting->receiveCallback = callback; if (callback) { switch (_currentSetting->spi_d->clk_id) { #if BOARD_NR_SPI >= 1 case RCC_SPI1: dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &SPIClass::_spi1EventCallback); break; #endif #if BOARD_NR_SPI >= 2 case RCC_SPI2: dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &SPIClass::_spi2EventCallback); break; #endif #if BOARD_NR_SPI >= 3 case RCC_SPI3: dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &SPIClass::_spi3EventCallback); break; #endif default: ASSERT(0); } } else { dma_detach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel); } } void SPIClass::onTransmit(void(*callback)()) { _currentSetting->transmitCallback = callback; if (callback) { switch (_currentSetting->spi_d->clk_id) { #if BOARD_NR_SPI >= 1 case RCC_SPI1: dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &SPIClass::_spi1EventCallback); break; #endif #if BOARD_NR_SPI >= 2 case RCC_SPI2: dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &SPIClass::_spi2EventCallback); break; #endif #if BOARD_NR_SPI >= 3 case RCC_SPI3: dma_attach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &SPIClass::_spi3EventCallback); break; #endif default: ASSERT(0); } } else { dma_detach_interrupt(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); } } /** * TODO: check if better to first call the customer code, next disable the DMA requests. * Also see if we need to check whether callbacks are set or not, may be better to be checked * during the initial setup and only set the callback to EventCallback if they are set. */ void SPIClass::EventCallback() { waitSpiTxEnd(_currentSetting->spi_d); switch (_currentSetting->state) { case SPI_STATE_TRANSFER: while (spi_is_rx_nonempty(_currentSetting->spi_d)) { /* nada */ } _currentSetting->state = SPI_STATE_READY; spi_tx_dma_disable(_currentSetting->spi_d); spi_rx_dma_disable(_currentSetting->spi_d); //dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); //dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel); if (_currentSetting->receiveCallback) _currentSetting->receiveCallback(); break; case SPI_STATE_TRANSMIT: _currentSetting->state = SPI_STATE_READY; spi_tx_dma_disable(_currentSetting->spi_d); //dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel); if (_currentSetting->transmitCallback) _currentSetting->transmitCallback(); break; default: break; } } void SPIClass::attachInterrupt() { // Should be enableInterrupt() } void SPIClass::detachInterrupt() { // Should be disableInterrupt() } /** * Pin accessors */ uint8_t SPIClass::misoPin() { return dev_to_spi_pins(_currentSetting->spi_d)->miso; } uint8_t SPIClass::mosiPin() { return dev_to_spi_pins(_currentSetting->spi_d)->mosi; } uint8_t SPIClass::sckPin() { return dev_to_spi_pins(_currentSetting->spi_d)->sck; } uint8_t SPIClass::nssPin() { return dev_to_spi_pins(_currentSetting->spi_d)->nss; } /** * Deprecated functions */ uint8_t SPIClass::send(uint8_t data) { write(data); return 1; } uint8_t SPIClass::send(uint8_t *buf, uint32_t len) { write(buf, len); return len; } uint8_t SPIClass::recv() { return read(); } /** * DMA call back functions, one per port. */ #if BOARD_NR_SPI >= 1 void SPIClass::_spi1EventCallback() { reinterpret_cast<class SPIClass*>(_spi1_this)->EventCallback(); } #endif #if BOARD_NR_SPI >= 2 void SPIClass::_spi2EventCallback() { reinterpret_cast<class SPIClass*>(_spi2_this)->EventCallback(); } #endif #if BOARD_NR_SPI >= 3 void SPIClass::_spi3EventCallback() { reinterpret_cast<class SPIClass*>(_spi3_this)->EventCallback(); } #endif /** * Auxiliary functions */ static const spi_pins* dev_to_spi_pins(spi_dev *dev) { switch (dev->clk_id) { #if BOARD_NR_SPI >= 1 case RCC_SPI1: return board_spi_pins; #endif #if BOARD_NR_SPI >= 2 case RCC_SPI2: return board_spi_pins + 1; #endif #if BOARD_NR_SPI >= 3 case RCC_SPI3: return board_spi_pins + 2; #endif default: return nullptr; } } static void disable_pwm(const stm32_pin_info *i) { if (i->timer_device) timer_set_mode(i->timer_device, i->timer_channel, TIMER_DISABLED); } static void configure_gpios(spi_dev *dev, bool as_master) { const spi_pins *pins = dev_to_spi_pins(dev); if (!pins) return; const stm32_pin_info *nssi = &PIN_MAP[pins->nss], *scki = &PIN_MAP[pins->sck], *misoi = &PIN_MAP[pins->miso], *mosii = &PIN_MAP[pins->mosi]; disable_pwm(nssi); disable_pwm(scki); disable_pwm(misoi); disable_pwm(mosii); spi_config_gpios(dev, as_master, nssi->gpio_device, nssi->gpio_bit, scki->gpio_device, scki->gpio_bit, misoi->gpio_bit, mosii->gpio_bit); } static const spi_baud_rate baud_rates[8] __FLASH__ = { SPI_BAUD_PCLK_DIV_2, SPI_BAUD_PCLK_DIV_4, SPI_BAUD_PCLK_DIV_8, SPI_BAUD_PCLK_DIV_16, SPI_BAUD_PCLK_DIV_32, SPI_BAUD_PCLK_DIV_64, SPI_BAUD_PCLK_DIV_128, SPI_BAUD_PCLK_DIV_256, }; /** * Note: This assumes you're on a LeafLabs-style board * (CYCLES_PER_MICROSECOND == 72, APB2 at 72MHz, APB1 at 36MHz). */ static spi_baud_rate determine_baud_rate(spi_dev *dev, uint32_t freq) { uint32_t clock = 0; switch (rcc_dev_clk(dev->clk_id)) { case RCC_AHB: case RCC_APB2: clock = STM32_PCLK2; break; // 72 Mhz case RCC_APB1: clock = STM32_PCLK1; break; // 36 Mhz } clock >>= 1; uint8_t i = 0; while (i < 7 && freq < clock) { clock >>= 1; i++; } return baud_rates[i]; } SPIClass SPI(SPI_DEVICE); #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/SPI.cpp
C++
agpl-3.0
27,116
/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *****************************************************************************/ #pragma once #include <libmaple/libmaple_types.h> #include <libmaple/spi.h> #include <libmaple/dma.h> #include <boards.h> #include <stdint.h> #include <wirish.h> // Number of SPI ports #ifdef BOARD_SPI3_SCK_PIN #define BOARD_NR_SPI 3 #elif defined(BOARD_SPI2_SCK_PIN) #define BOARD_NR_SPI 2 #elif defined(BOARD_SPI1_SCK_PIN) #define BOARD_NR_SPI 1 #endif // SPI_HAS_TRANSACTION means SPI has // - beginTransaction() // - endTransaction() // - usingInterrupt() // - SPISetting(clock, bitOrder, dataMode) //#define SPI_HAS_TRANSACTION #define SPI_CLOCK_DIV2 SPI_BAUD_PCLK_DIV_2 #define SPI_CLOCK_DIV4 SPI_BAUD_PCLK_DIV_4 #define SPI_CLOCK_DIV8 SPI_BAUD_PCLK_DIV_8 #define SPI_CLOCK_DIV16 SPI_BAUD_PCLK_DIV_16 #define SPI_CLOCK_DIV32 SPI_BAUD_PCLK_DIV_32 #define SPI_CLOCK_DIV64 SPI_BAUD_PCLK_DIV_64 #define SPI_CLOCK_DIV128 SPI_BAUD_PCLK_DIV_128 #define SPI_CLOCK_DIV256 SPI_BAUD_PCLK_DIV_256 /** * Roger Clark. 20150106 * Commented out redundant AVR defined * #define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR #define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR #define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR // define SPI_AVR_EIMSK for AVR boards with external interrupt pins #ifdef EIMSK #define SPI_AVR_EIMSK EIMSK #elif defined(GICR) #define SPI_AVR_EIMSK GICR #elif defined(GIMSK) #define SPI_AVR_EIMSK GIMSK #endif */ #ifndef STM32_LSBFIRST #define STM32_LSBFIRST 0 #endif #ifndef STM32_MSBFIRST #define STM32_MSBFIRST 1 #endif // PC13 or PA4 #define BOARD_SPI_DEFAULT_SS PA4 //#define BOARD_SPI_DEFAULT_SS PC13 #define SPI_MODE0 SPI_MODE_0 #define SPI_MODE1 SPI_MODE_1 #define SPI_MODE2 SPI_MODE_2 #define SPI_MODE3 SPI_MODE_3 #define DATA_SIZE_8BIT SPI_CR1_DFF_8_BIT #define DATA_SIZE_16BIT SPI_CR1_DFF_16_BIT typedef enum { SPI_STATE_IDLE, SPI_STATE_READY, SPI_STATE_RECEIVE, SPI_STATE_TRANSMIT, SPI_STATE_TRANSFER } spi_mode_t; class SPISettings { public: SPISettings(uint32_t inClock, BitOrder inBitOrder, uint8_t inDataMode) { if (__builtin_constant_p(inClock)) init_AlwaysInline(inClock, inBitOrder, inDataMode, DATA_SIZE_8BIT); else init_MightInline(inClock, inBitOrder, inDataMode, DATA_SIZE_8BIT); } SPISettings(uint32_t inClock, BitOrder inBitOrder, uint8_t inDataMode, uint32_t inDataSize) { if (__builtin_constant_p(inClock)) init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize); else init_MightInline(inClock, inBitOrder, inDataMode, inDataSize); } SPISettings(uint32_t inClock) { if (__builtin_constant_p(inClock)) init_AlwaysInline(inClock, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT); else init_MightInline(inClock, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT); } SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT); } private: void init_MightInline(uint32_t inClock, BitOrder inBitOrder, uint8_t inDataMode, uint32_t inDataSize) { init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize); } void init_AlwaysInline(uint32_t inClock, BitOrder inBitOrder, uint8_t inDataMode, uint32_t inDataSize) __attribute__((__always_inline__)) { clock = inClock; bitOrder = inBitOrder; dataMode = inDataMode; dataSize = inDataSize; //state = SPI_STATE_IDLE; } uint32_t clock; uint32_t dataSize; uint32_t clockDivider; BitOrder bitOrder; uint8_t dataMode; uint8_t _SSPin; volatile spi_mode_t state; spi_dev *spi_d; dma_channel spiRxDmaChannel, spiTxDmaChannel; dma_dev* spiDmaDev; void (*receiveCallback)() = nullptr; void (*transmitCallback)() = nullptr; friend class SPIClass; }; /** * Kept for compat. */ static const uint8_t ff = 0xFF; /** * @brief Wirish SPI interface. * * This implementation uses software slave management, so the caller * is responsible for controlling the slave select line. */ class SPIClass { public: /** * @param spiPortNumber Number of the SPI port to manage. */ SPIClass(uint32_t spiPortNumber); /** * Init using pins */ SPIClass(int8_t mosi, int8_t miso, int8_t sclk, int8_t ssel=-1); /** * @brief Equivalent to begin(SPI_1_125MHZ, MSBFIRST, 0). */ void begin(); /** * @brief Turn on a SPI port and set its GPIO pin modes for use as a slave. * * SPI port is enabled in full duplex mode, with software slave management. * * @param bitOrder Either LSBFIRST (little-endian) or MSBFIRST(big-endian) * @param mode SPI mode to use */ void beginSlave(uint32_t bitOrder, uint32_t mode); /** * @brief Equivalent to beginSlave(MSBFIRST, 0). */ void beginSlave(); /** * @brief Disables the SPI port, but leaves its GPIO pin modes unchanged. */ void end(); void beginTransaction(const SPISettings &settings) { beginTransaction(BOARD_SPI_DEFAULT_SS, settings); } void beginTransaction(uint8_t pin, const SPISettings &settings); void endTransaction(); void beginTransactionSlave(const SPISettings &settings); void setClockDivider(uint32_t clockDivider); void setBitOrder(BitOrder bitOrder); void setDataMode(uint8_t dataMode); // SPI Configuration methods void attachInterrupt(); void detachInterrupt(); /* Victor Perez. Added to change datasize from 8 to 16 bit modes on the fly. * Input parameter should be SPI_CR1_DFF set to 0 or 1 on a 32bit word. * Requires an added function spi_data_size on STM32F1 / cores / maple / libmaple / spi.c */ void setDataSize(uint32_t ds); uint32_t getDataSize() { return _currentSetting->dataSize; } /* Victor Perez 2017. Added to set and clear callback functions for callback * on DMA transfer completion. * onReceive used to set the callback in case of dmaTransfer (tx/rx), once rx is completed * onTransmit used to set the callback in case of dmaSend (tx only). That function * will NOT be called in case of TX/RX */ void onReceive(void(*)()); void onTransmit(void(*)()); /** * I/O */ /** * @brief Return the next unread byte/word. * * If there is no unread byte/word waiting, this function will block * until one is received. */ uint16_t read(); /** * @brief Read length bytes, storing them into buffer. * @param buffer Buffer to store received bytes into. * @param length Number of bytes to store in buffer. This * function will block until the desired number of * bytes have been read. */ void read(uint8_t *buffer, uint32_t length); /** * @brief Transmit one byte/word. * @param data to transmit. */ void write(uint16_t data); void write16(uint16_t data); // write 2 bytes in 8 bit mode (DFF=0) /** * @brief Transmit one byte/word a specified number of times. * @param data to transmit. */ void write(uint16_t data, uint32_t n); /** * @brief Transmit multiple bytes/words. * @param buffer Bytes/words to transmit. * @param length Number of bytes/words in buffer to transmit. */ void write(const void * buffer, uint32_t length); /** * @brief Transmit a byte, then return the next unread byte. * * This function transmits before receiving. * * @param data Byte to transmit. * @return Next unread byte. */ uint8_t transfer(uint8_t data) const; uint16_t transfer16(uint16_t data) const; /** * @brief Sets up a DMA Transfer for "length" bytes. * The transfer mode (8 or 16 bit mode) is evaluated from the SPI peripheral setting. * * This function transmits and receives to buffers. * * @param transmitBuf buffer Bytes to transmit. If passed as 0, it sends FF repeatedly for "length" bytes * @param receiveBuf buffer Bytes to save received data. * @param length Number of bytes in buffer to transmit. */ uint8_t dmaTransfer(const void * transmitBuf, void * receiveBuf, uint16_t length); void dmaTransferSet(const void *transmitBuf, void *receiveBuf); uint8_t dmaTransferRepeat(uint16_t length); /** * @brief Sets up a DMA Transmit for SPI 8 or 16 bit transfer mode. * The transfer mode (8 or 16 bit mode) is evaluated from the SPI peripheral setting. * * This function only transmits and does not care about the RX fifo. * * @param data buffer half words to transmit, * @param length Number of bytes in buffer to transmit. * @param minc Set to use Memory Increment mode, clear to use Circular mode. */ uint8_t dmaSend(const void * transmitBuf, uint16_t length, bool minc = 1); void dmaSendSet(const void * transmitBuf, bool minc); uint8_t dmaSendRepeat(uint16_t length); uint8_t dmaSendAsync(const void * transmitBuf, uint16_t length, bool minc = 1); /** * Pin accessors */ /** * @brief Return the number of the MISO (master in, slave out) pin */ uint8_t misoPin(); /** * @brief Return the number of the MOSI (master out, slave in) pin */ uint8_t mosiPin(); /** * @brief Return the number of the SCK (serial clock) pin */ uint8_t sckPin(); /** * @brief Return the number of the NSS (slave select) pin */ uint8_t nssPin(); /* Escape hatch */ /** * @brief Get a pointer to the underlying libmaple spi_dev for * this HardwareSPI instance. */ spi_dev* c_dev() { return _currentSetting->spi_d; } spi_dev* dev() { return _currentSetting->spi_d; } /** * @brief Sets the number of the SPI peripheral to be used by * this HardwareSPI instance. * * @param spi_num Number of the SPI port. 1-2 in low density devices * or 1-3 in high density devices. */ void setModule(int spi_num) { _currentSetting = &_settings[spi_num - 1];// SPI channels are called 1 2 and 3 but the array is zero indexed } /* -- The following methods are deprecated --------------------------- */ /** * @brief Deprecated. * * Use HardwareSPI::transfer() instead. * * @see HardwareSPI::transfer() */ uint8_t send(uint8_t data); /** * @brief Deprecated. * * Use HardwareSPI::write() in combination with * HardwareSPI::read() (or HardwareSPI::transfer()) instead. * * @see HardwareSPI::write() * @see HardwareSPI::read() * @see HardwareSPI::transfer() */ uint8_t send(uint8_t *data, uint32_t length); /** * @brief Deprecated. * * Use HardwareSPI::read() instead. * * @see HardwareSPI::read() */ uint8_t recv(); private: SPISettings _settings[BOARD_NR_SPI]; SPISettings *_currentSetting; void updateSettings(); /** * Functions added for DMA transfers with Callback. * Experimental. */ void EventCallback(); #if BOARD_NR_SPI >= 1 static void _spi1EventCallback(); #endif #if BOARD_NR_SPI >= 2 static void _spi2EventCallback(); #endif #if BOARD_NR_SPI >= 3 static void _spi3EventCallback(); #endif /* spi_dev *spi_d; uint8_t _SSPin; uint32_t clockDivider; uint8_t dataMode; BitOrder bitOrder; */ }; extern SPIClass SPI;
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/SPI.h
C++
agpl-3.0
12,258
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #if HAS_SERVOS uint8_t ServoCount = 0; #include "Servo.h" //#include "Servo.h" #include <boards.h> #include <io.h> #include <pwm.h> #include <wirish_math.h> /** * 20 millisecond period config. For a 1-based prescaler, * * (prescaler * overflow / CYC_MSEC) msec = 1 timer cycle = 20 msec * => prescaler * overflow = 20 * CYC_MSEC * * This uses the smallest prescaler that allows an overflow < 2^16. */ #define MAX_OVERFLOW UINT16_MAX // _BV(16) - 1 #define CYC_MSEC (1000 * CYCLES_PER_MICROSECOND) #define TAU_MSEC 20 #define TAU_USEC (TAU_MSEC * 1000) #define TAU_CYC (TAU_MSEC * CYC_MSEC) #define SERVO_PRESCALER (TAU_CYC / MAX_OVERFLOW + 1) #define SERVO_OVERFLOW ((uint16_t)round((double)TAU_CYC / SERVO_PRESCALER)) // Unit conversions #define US_TO_COMPARE(us) uint16_t(map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW)) #define COMPARE_TO_US(c) uint32_t(map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC)) #define ANGLE_TO_US(a) uint16_t(map((a), minAngle, maxAngle, SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW)) #define US_TO_ANGLE(us) int16_t(map((us), SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW, minAngle, maxAngle)) void libServo::servoWrite(uint8_t inPin, uint16_t duty_cycle) { #ifdef MF_TIMER_SERVO0 if (servoIndex == 0) { pwmSetDuty(duty_cycle); return; } #endif timer_dev *tdev = PIN_MAP[inPin].timer_device; uint8_t tchan = PIN_MAP[inPin].timer_channel; if (tdev) timer_set_compare(tdev, tchan, duty_cycle); } libServo::libServo() { servoIndex = ServoCount < MAX_SERVOS ? ServoCount++ : INVALID_SERVO; HAL_timer_set_interrupt_priority(MF_TIMER_SERVO0, SERVO0_TIMER_IRQ_PRIO); } bool libServo::attach(const int32_t inPin, const int32_t inMinAngle, const int32_t inMaxAngle) { if (servoIndex >= MAX_SERVOS) return false; if (inPin >= BOARD_NR_GPIO_PINS) return false; minAngle = inMinAngle; maxAngle = inMaxAngle; angle = -1; #ifdef MF_TIMER_SERVO0 if (servoIndex == 0 && setupSoftPWM(inPin)) { pin = inPin; // set attached() return true; } #endif if (!PWM_PIN(inPin)) return false; timer_dev *tdev = PIN_MAP[inPin].timer_device; //uint8_t tchan = PIN_MAP[inPin].timer_channel; SET_PWM(inPin); servoWrite(inPin, 0); timer_pause(tdev); timer_set_prescaler(tdev, SERVO_PRESCALER - 1); // prescaler is 1-based timer_set_reload(tdev, SERVO_OVERFLOW); timer_generate_update(tdev); timer_resume(tdev); pin = inPin; // set attached() return true; } bool libServo::detach() { if (!attached()) return false; angle = -1; servoWrite(pin, 0); return true; } int32_t libServo::read() const { if (attached()) { #ifdef MF_TIMER_SERVO0 if (servoIndex == 0) return angle; #endif timer_dev *tdev = PIN_MAP[pin].timer_device; uint8_t tchan = PIN_MAP[pin].timer_channel; return US_TO_ANGLE(COMPARE_TO_US(timer_get_compare(tdev, tchan))); } return 0; } void libServo::move(const int32_t value) { constexpr uint16_t servo_delay[] = SERVO_DELAY; static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long."); if (attached()) { angle = constrain(value, minAngle, maxAngle); servoWrite(pin, US_TO_COMPARE(ANGLE_TO_US(angle))); safe_delay(servo_delay[servoIndex]); TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach()); } } #ifdef MF_TIMER_SERVO0 extern "C" void Servo_IRQHandler() { static timer_dev *tdev = HAL_get_timer_dev(MF_TIMER_SERVO0); uint16_t SR = timer_get_status(tdev); if (SR & TIMER_SR_CC1IF) { // channel 1 off #ifdef SERVO0_PWM_OD OUT_WRITE_OD(SERVO0_PIN, HIGH); // off #else OUT_WRITE(SERVO0_PIN, LOW); #endif timer_reset_status_bit(tdev, TIMER_SR_CC1IF_BIT); } if (SR & TIMER_SR_CC2IF) { // channel 2 resume #ifdef SERVO0_PWM_OD OUT_WRITE_OD(SERVO0_PIN, LOW); // on #else OUT_WRITE(SERVO0_PIN, HIGH); #endif timer_reset_status_bit(tdev, TIMER_SR_CC2IF_BIT); } } bool libServo::setupSoftPWM(const int32_t inPin) { timer_dev *tdev = HAL_get_timer_dev(MF_TIMER_SERVO0); if (!tdev) return false; #ifdef SERVO0_PWM_OD OUT_WRITE_OD(inPin, HIGH); #else OUT_WRITE(inPin, LOW); #endif timer_pause(tdev); timer_set_mode(tdev, 1, TIMER_OUTPUT_COMPARE); // counter with isr timer_oc_set_mode(tdev, 1, TIMER_OC_MODE_FROZEN, 0); // no pin output change timer_oc_set_mode(tdev, 2, TIMER_OC_MODE_FROZEN, 0); // no pin output change timer_set_prescaler(tdev, SERVO_PRESCALER - 1); // prescaler is 1-based timer_set_reload(tdev, SERVO_OVERFLOW); timer_set_compare(tdev, 1, SERVO_OVERFLOW); timer_set_compare(tdev, 2, SERVO_OVERFLOW); timer_attach_interrupt(tdev, 1, Servo_IRQHandler); timer_attach_interrupt(tdev, 2, Servo_IRQHandler); timer_generate_update(tdev); timer_resume(tdev); return true; } void libServo::pwmSetDuty(const uint16_t duty_cycle) { timer_dev *tdev = HAL_get_timer_dev(MF_TIMER_SERVO0); timer_set_compare(tdev, 1, duty_cycle); timer_generate_update(tdev); if (duty_cycle) { timer_enable_irq(tdev, 1); timer_enable_irq(tdev, 2); } else { timer_disable_irq(tdev, 1); timer_disable_irq(tdev, 2); #ifdef SERVO0_PWM_OD OUT_WRITE_OD(pin, HIGH); // off #else OUT_WRITE(pin, LOW); #endif } } void libServo::pauseSoftPWM() { // detach timer_dev *tdev = HAL_get_timer_dev(MF_TIMER_SERVO0); timer_pause(tdev); pwmSetDuty(0); } #else bool libServo::setupSoftPWM(const int32_t inPin) { return false; } void libServo::pwmSetDuty(const uint16_t duty_cycle) {} void libServo::pauseSoftPWM() {} #endif #endif // HAS_SERVOS #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/Servo.cpp
C++
agpl-3.0
6,713
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once // Pin number of unattached pins #define NOT_ATTACHED (-1) #define INVALID_SERVO 255 #ifndef MAX_SERVOS #define MAX_SERVOS 3 #endif #define SERVO_DEFAULT_MIN_PW 544 #define SERVO_DEFAULT_MAX_PW 2400 #define SERVO_DEFAULT_MIN_ANGLE 0 #define SERVO_DEFAULT_MAX_ANGLE 180 class libServo; typedef libServo hal_servo_t; class libServo { public: libServo(); bool attach(const int32_t pin, const int32_t minAngle=SERVO_DEFAULT_MIN_ANGLE, const int32_t maxAngle=SERVO_DEFAULT_MAX_ANGLE); bool attached() const { return pin != NOT_ATTACHED; } bool detach(); void move(const int32_t value); int32_t read() const; private: void servoWrite(uint8_t pin, const uint16_t duty_cycle); uint8_t servoIndex; // index into the channel data for this servo int32_t pin = NOT_ATTACHED; int32_t minAngle; int32_t maxAngle; int32_t angle; bool setupSoftPWM(const int32_t pin); void pauseSoftPWM(); void pwmSetDuty(const uint16_t duty_cycle); };
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/Servo.h
C++
agpl-3.0
1,957
/** * Marlin 3D Printer Firmware * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) * * adc.h - Define enumerated indices for enabled ADC Features */ #include "../../inc/MarlinConfig.h" enum ADCIndex : uint8_t { OPTITEM(HAS_TEMP_ADC_0, TEMP_0 ) OPTITEM(HAS_TEMP_ADC_1, TEMP_1 ) OPTITEM(HAS_TEMP_ADC_2, TEMP_2 ) OPTITEM(HAS_TEMP_ADC_3, TEMP_3 ) OPTITEM(HAS_TEMP_ADC_4, TEMP_4 ) OPTITEM(HAS_TEMP_ADC_5, TEMP_5 ) OPTITEM(HAS_TEMP_ADC_6, TEMP_6 ) OPTITEM(HAS_TEMP_ADC_7, TEMP_7 ) OPTITEM(HAS_TEMP_ADC_BED, TEMP_BED ) OPTITEM(HAS_TEMP_ADC_CHAMBER, TEMP_CHAMBER ) OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE ) OPTITEM(HAS_TEMP_ADC_COOLER, TEMP_COOLER ) OPTITEM(HAS_TEMP_ADC_BOARD, TEMP_BOARD ) OPTITEM(HAS_TEMP_ADC_SOC, TEMP_SOC ) OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH ) OPTITEM(HAS_ADC_BUTTONS, ADC_KEY ) OPTITEM(HAS_JOY_ADC_X, JOY_X ) OPTITEM(HAS_JOY_ADC_Y, JOY_Y ) OPTITEM(HAS_JOY_ADC_Z, JOY_Z ) OPTITEM(POWER_MONITOR_CURRENT, POWERMON_CURRENT ) OPTITEM(POWER_MONITOR_VOLTAGE, POWERMON_VOLTAGE ) ADC_COUNT }; extern uint16_t adc_results[ADC_COUNT];
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/adc.h
C
agpl-3.0
2,230
from __future__ import print_function import sys #dynamic build flags for generic compile options if __name__ == "__main__": args = " ".join([ "-std=gnu++14", "-Os", "-mcpu=cortex-m3", "-mthumb", "-fsigned-char", "-fno-move-loop-invariants", "-fno-strict-aliasing", "-fsingle-precision-constant", "--specs=nano.specs", "--specs=nosys.specs", "-IMarlin/src/HAL/STM32F1", "-MMD", "-MP", "-DTARGET_STM32F1" ]) for i in range(1, len(sys.argv)): args += " " + sys.argv[i] print(args) # extra script for linker options else: import pioutil if pioutil.is_pio_build(): from SCons.Script import DefaultEnvironment env = DefaultEnvironment() env.Append( ARFLAGS=["rcs"], ASFLAGS=["-x", "assembler-with-cpp"], CXXFLAGS=[ "-fabi-version=0", "-fno-use-cxa-atexit", "-fno-threadsafe-statics" ], LINKFLAGS=[ "-Os", "-mcpu=cortex-m3", "-ffreestanding", "-mthumb", "--specs=nano.specs", "--specs=nosys.specs", "-u_printf_float", ], )
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/build_flags.py
Python
agpl-3.0
1,351
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../../inc/MarlinConfig.h" #if ALL(HAS_MARLINUI_U8GLIB, FORCE_SOFT_SPI) #include <U8glib-HAL.h> #include "../../shared/HAL_SPI.h" #ifndef LCD_SPI_SPEED #define LCD_SPI_SPEED SPI_FULL_SPEED // Fastest //#define LCD_SPI_SPEED SPI_QUARTER_SPEED // Slower #endif static uint8_t SPI_speed = LCD_SPI_SPEED; static inline uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) { for (uint8_t i = 0; i < 8; ++i) { if (spi_speed == 0) { WRITE(DOGLCD_MOSI, !!(b & 0x80)); WRITE(DOGLCD_SCK, HIGH); b <<= 1; if (miso_pin >= 0 && READ(miso_pin)) b |= 1; WRITE(DOGLCD_SCK, LOW); } else { const uint8_t state = (b & 0x80) ? HIGH : LOW; for (uint8_t j = 0; j < spi_speed; ++j) WRITE(DOGLCD_MOSI, state); for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); ++j) WRITE(DOGLCD_SCK, HIGH); b <<= 1; if (miso_pin >= 0 && READ(miso_pin)) b |= 1; for (uint8_t j = 0; j < spi_speed; ++j) WRITE(DOGLCD_SCK, LOW); } } return b; } static inline uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) { for (uint8_t i = 0; i < 8; ++i) { const uint8_t state = (b & 0x80) ? HIGH : LOW; if (spi_speed == 0) { WRITE(DOGLCD_SCK, LOW); WRITE(DOGLCD_MOSI, state); WRITE(DOGLCD_MOSI, state); // need some setup time WRITE(DOGLCD_SCK, HIGH); } else { for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); ++j) WRITE(DOGLCD_SCK, LOW); for (uint8_t j = 0; j < spi_speed; ++j) WRITE(DOGLCD_MOSI, state); for (uint8_t j = 0; j < spi_speed; ++j) WRITE(DOGLCD_SCK, HIGH); } b <<= 1; if (miso_pin >= 0 && READ(miso_pin)) b |= 1; } return b; } static void u8g_sw_spi_shift_out(uint8_t val) { #if ENABLED(FYSETC_MINI_12864) swSpiTransfer_mode_3(val, SPI_speed); #else swSpiTransfer_mode_0(val, SPI_speed); #endif } static uint8_t swSpiInit(const uint8_t spi_speed) { #if PIN_EXISTS(LCD_RESET) SET_OUTPUT(LCD_RESET_PIN); #endif SET_OUTPUT(DOGLCD_A0); OUT_WRITE(DOGLCD_SCK, LOW); OUT_WRITE(DOGLCD_MOSI, LOW); OUT_WRITE(DOGLCD_CS, HIGH); return spi_speed; } uint8_t u8g_com_HAL_STM32F1_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_INIT: SPI_speed = swSpiInit(LCD_SPI_SPEED); break; case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_RESET: #if PIN_EXISTS(LCD_RESET) WRITE(LCD_RESET_PIN, arg_val); #endif break; case U8G_COM_MSG_CHIP_SELECT: #if ENABLED(FYSETC_MINI_12864) // This LCD SPI is running mode 3 while SD card is running mode 0 if (arg_val) { // SCK idle state needs to be set to the proper idle state before // the next chip select goes active WRITE(DOGLCD_SCK, HIGH); // Set SCK to mode 3 idle state before CS goes active WRITE(DOGLCD_CS, LOW); } else { WRITE(DOGLCD_CS, HIGH); WRITE(DOGLCD_SCK, LOW); // Set SCK to mode 0 idle state after CS goes inactive } #else WRITE(DOGLCD_CS, !arg_val); #endif break; case U8G_COM_MSG_WRITE_BYTE: u8g_sw_spi_shift_out(arg_val); break; case U8G_COM_MSG_WRITE_SEQ: { uint8_t *ptr = (uint8_t *)arg_ptr; while (arg_val > 0) { u8g_sw_spi_shift_out(*ptr++); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t *ptr = (uint8_t *)arg_ptr; while (arg_val > 0) { u8g_sw_spi_shift_out(u8g_pgm_read(ptr)); ptr++; arg_val--; } } break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ WRITE(DOGLCD_A0, arg_val); break; } return 1; } #endif // HAS_MARLINUI_U8GLIB && FORCE_SOFT_SPI #endif // STM32F1
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/dogm/u8g_com_stm32duino_swspi.cpp
C++
agpl-3.0
4,888
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ /** * PersistentStore for Arduino-style EEPROM interface * with simple implementations supplied by Marlin. */ #include "../../inc/MarlinConfig.h" #if ENABLED(IIC_BL24CXX_EEPROM) #include "../shared/eeprom_if.h" #include "../shared/eeprom_api.h" // // PersistentStore // #ifndef MARLIN_EEPROM_SIZE #error "MARLIN_EEPROM_SIZE is required for IIC_BL24CXX_EEPROM." #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } bool PersistentStore::access_start() { eeprom_init(); return true; } bool PersistentStore::access_finish() { return true; } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { uint16_t written = 0; while (size--) { uint8_t v = *value; uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos); if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed! eeprom_write_byte(p, v); if (++written & 0x7F) delay(2); else safe_delay(2); // Avoid triggering watchdog during long EEPROM writes if (eeprom_read_byte(p) != v) { SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE); return true; } } crc16(crc, &v, 1); pos++; value++; } return false; } bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) { do { const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos)); if (writing) *value = c; crc16(crc, &c, 1); pos++; value++; } while (--size); return false; } #endif // IIC_BL24CXX_EEPROM #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/eeprom_bl24cxx.cpp
C++
agpl-3.0
2,519
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * persistent_store_flash.cpp * HAL for stm32duino and compatible (STM32F1) * Implementation of EEPROM settings in SDCard */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #if ENABLED(FLASH_EEPROM_EMULATION) #include "../shared/eeprom_api.h" #include <flash_stm32.h> #include <EEPROM.h> // Store settings in the last two pages #ifndef MARLIN_EEPROM_SIZE #define MARLIN_EEPROM_SIZE ((EEPROM_PAGE_SIZE) * 2) #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0}; static bool eeprom_dirty = false; bool PersistentStore::access_start() { const uint32_t *source = reinterpret_cast<const uint32_t*>(EEPROM_PAGE0_BASE); uint32_t *destination = reinterpret_cast<uint32_t*>(ram_eeprom); static_assert(0 == (MARLIN_EEPROM_SIZE) % 4, "MARLIN_EEPROM_SIZE is corrupted. (Must be a multiple of 4.)"); // Ensure copying as uint32_t is safe constexpr size_t eeprom_size_u32 = (MARLIN_EEPROM_SIZE) / 4; for (size_t i = 0; i < eeprom_size_u32; ++i, ++destination, ++source) *destination = *source; eeprom_dirty = false; return true; } bool PersistentStore::access_finish() { if (eeprom_dirty) { FLASH_Status status; // Instead of erasing all (both) pages, maybe in the loop we check what page we are in, and if the // data has changed in that page. We then erase the first time we "detect" a change. In theory, if // nothing changed in a page, we wouldn't need to erase/write it. // Or, instead of checking at this point, turn eeprom_dirty into an array of bool the size of number // of pages. Inside write_data, we set the flag to true at that time if something in that // page changes...either way, something to look at later. FLASH_Unlock(); #define ACCESS_FINISHED(TF) { FLASH_Lock(); eeprom_dirty = false; return TF; } status = FLASH_ErasePage(EEPROM_PAGE0_BASE); if (status != FLASH_COMPLETE) ACCESS_FINISHED(true); status = FLASH_ErasePage(EEPROM_PAGE1_BASE); if (status != FLASH_COMPLETE) ACCESS_FINISHED(true); const uint16_t *source = reinterpret_cast<const uint16_t*>(ram_eeprom); for (size_t i = 0; i < MARLIN_EEPROM_SIZE; i += 2, ++source) { if (FLASH_ProgramHalfWord(EEPROM_PAGE0_BASE + i, *source) != FLASH_COMPLETE) ACCESS_FINISHED(false); } ACCESS_FINISHED(true); } return true; } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { for (size_t i = 0; i < size; ++i) ram_eeprom[pos + i] = value[i]; eeprom_dirty = true; crc16(crc, value, size); pos += size; return false; // return true for any error } bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uint16_t *crc, const bool writing/*=true*/) { const uint8_t * const buff = writing ? &value[0] : &ram_eeprom[pos]; if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[pos + i]; crc16(crc, buff, size); pos += size; return false; // return true for any error } #endif // FLASH_EEPROM_EMULATION #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/eeprom_flash.cpp
C++
agpl-3.0
4,004
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Platform-independent Arduino functions for I2C EEPROM. * Enable USE_SHARED_EEPROM if not supplied by the framework. */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #if ENABLED(IIC_BL24CXX_EEPROM) #include "../../libs/BL24CXX.h" #include "../shared/eeprom_if.h" void eeprom_init() { BL24CXX::init(); } // ------------------------ // Public functions // ------------------------ void eeprom_write_byte(uint8_t *pos, uint8_t value) { const unsigned eeprom_address = (unsigned)pos; return BL24CXX::writeOneByte(eeprom_address, value); } uint8_t eeprom_read_byte(uint8_t *pos) { const unsigned eeprom_address = (unsigned)pos; return BL24CXX::readOneByte(eeprom_address); } #endif // IIC_BL24CXX_EEPROM #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/eeprom_if_iic.cpp
C++
agpl-3.0
1,618
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) * Implementation of EEPROM settings in SD Card */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #if ENABLED(SDCARD_EEPROM_EMULATION) #include "../shared/eeprom_api.h" #include "../../sd/cardreader.h" #define EEPROM_FILENAME "eeprom.dat" #ifndef MARLIN_EEPROM_SIZE #define MARLIN_EEPROM_SIZE 0x1000 // 4KB #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } #define _ALIGN(x) __attribute__ ((aligned(x))) // SDIO uint32_t* compat. static char _ALIGN(4) HAL_eeprom_data[MARLIN_EEPROM_SIZE]; bool PersistentStore::access_start() { if (!card.isMounted()) return false; MediaFile file, root = card.getroot(); if (!file.open(&root, EEPROM_FILENAME, O_RDONLY)) return true; // false aborts the save int bytes_read = file.read(HAL_eeprom_data, MARLIN_EEPROM_SIZE); if (bytes_read < 0) return false; for (; bytes_read < MARLIN_EEPROM_SIZE; bytes_read++) HAL_eeprom_data[bytes_read] = 0xFF; file.close(); return true; } bool PersistentStore::access_finish() { if (!card.isMounted()) return false; MediaFile file, root = card.getroot(); int bytes_written = 0; if (file.open(&root, EEPROM_FILENAME, O_CREAT | O_WRITE | O_TRUNC)) { bytes_written = file.write(HAL_eeprom_data, MARLIN_EEPROM_SIZE); file.close(); } return (bytes_written == MARLIN_EEPROM_SIZE); } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { for (size_t i = 0; i < size; i++) HAL_eeprom_data[pos + i] = value[i]; crc16(crc, value, size); pos += size; return false; } bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uint16_t *crc, const bool writing/*=true*/) { for (size_t i = 0; i < size; i++) { uint8_t c = HAL_eeprom_data[pos + i]; if (writing) value[i] = c; crc16(crc, &c, 1); } pos += size; return false; } #endif // SDCARD_EEPROM_EMULATION #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/eeprom_sdcard.cpp
C++
agpl-3.0
2,881
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL PersistentStore for STM32F1 */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #if USE_WIRED_EEPROM #include "../shared/eeprom_if.h" #include "../shared/eeprom_api.h" #ifndef MARLIN_EEPROM_SIZE #error "MARLIN_EEPROM_SIZE is required for I2C / SPI EEPROM." #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } bool PersistentStore::access_finish() { return true; } bool PersistentStore::access_start() { eeprom_init(); #if ENABLED(SPI_EEPROM) #if SPI_CHAN_EEPROM1 == 1 SET_OUTPUT(BOARD_SPI1_SCK_PIN); SET_OUTPUT(BOARD_SPI1_MOSI_PIN); SET_INPUT(BOARD_SPI1_MISO_PIN); SET_OUTPUT(SPI_EEPROM1_CS_PIN); #endif spiInit(0); #endif return true; } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { uint16_t written = 0; while (size--) { uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos); uint8_t v = *value; if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed! eeprom_write_byte(p, v); if (++written & 0x7F) delay(2); else safe_delay(2); // Avoid triggering watchdog during long EEPROM writes if (eeprom_read_byte(p) != v) { SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE); return true; } } crc16(crc, &v, 1); pos++; value++; } return false; } bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) { do { const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos)); if (writing && value) *value = c; crc16(crc, &c, 1); pos++; value++; } while (--size); return false; } #endif // USE_WIRED_EEPROM #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/eeprom_wired.cpp
C++
agpl-3.0
2,666
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Endstop interrupts for Libmaple STM32F1 based targets. * * On STM32F, all pins support external interrupt capability. * Any pin can be used for external interrupts, but there are some restrictions. * At most 16 different external interrupts can be used at one time. * Further, you can’t just pick any 16 pins to use. This is because every pin on the STM32 * connects to what is called an EXTI line, and only one pin per EXTI line can be used for external interrupts at a time * Check the Reference Manual of the MCU to confirm which line is used by each pin */ /** * Endstop Interrupts * * Without endstop interrupts the endstop pins must be polled continually in * the temperature-ISR via endstops.update(), most of the time finding no change. * With this feature endstops.update() is called only when we know that at * least one endstop has changed state, saving valuable CPU cycles. * * This feature only works when all used endstop pins can generate an 'external interrupt'. * * Test whether pins issue interrupts on your board by flashing 'pin_interrupt_test.ino'. * (Located in Marlin/buildroot/share/pin_interrupt_test/pin_interrupt_test.ino) */ #include "../../module/endstops.h" // One ISR for all EXT-Interrupts void endstop_ISR() { endstops.update(); } void setup_endstop_interrupts() { #define _ATTACH(P) attachInterrupt(P, endstop_ISR, CHANGE) TERN_(USE_X_MAX, _ATTACH(X_MAX_PIN)); TERN_(USE_X_MIN, _ATTACH(X_MIN_PIN)); TERN_(USE_Y_MAX, _ATTACH(Y_MAX_PIN)); TERN_(USE_Y_MIN, _ATTACH(Y_MIN_PIN)); TERN_(USE_Z_MAX, _ATTACH(Z_MAX_PIN)); TERN_(USE_Z_MIN, _ATTACH(Z_MIN_PIN)); TERN_(USE_X2_MAX, _ATTACH(X2_MAX_PIN)); TERN_(USE_X2_MIN, _ATTACH(X2_MIN_PIN)); TERN_(USE_Y2_MAX, _ATTACH(Y2_MAX_PIN)); TERN_(USE_Y2_MIN, _ATTACH(Y2_MIN_PIN)); TERN_(USE_Z2_MAX, _ATTACH(Z2_MAX_PIN)); TERN_(USE_Z2_MIN, _ATTACH(Z2_MIN_PIN)); TERN_(USE_Z3_MAX, _ATTACH(Z3_MAX_PIN)); TERN_(USE_Z3_MIN, _ATTACH(Z3_MIN_PIN)); TERN_(USE_Z4_MAX, _ATTACH(Z4_MAX_PIN)); TERN_(USE_Z4_MIN, _ATTACH(Z4_MIN_PIN)); TERN_(USE_Z_MIN_PROBE, _ATTACH(Z_MIN_PROBE_PIN)); TERN_(USE_I_MAX, _ATTACH(I_MAX_PIN)); TERN_(USE_I_MIN, _ATTACH(I_MIN_PIN)); TERN_(USE_J_MAX, _ATTACH(J_MAX_PIN)); TERN_(USE_J_MIN, _ATTACH(J_MIN_PIN)); TERN_(USE_K_MAX, _ATTACH(K_MAX_PIN)); TERN_(USE_K_MIN, _ATTACH(K_MIN_PIN)); TERN_(USE_U_MAX, _ATTACH(U_MAX_PIN)); TERN_(USE_U_MIN, _ATTACH(U_MIN_PIN)); TERN_(USE_V_MAX, _ATTACH(V_MAX_PIN)); TERN_(USE_V_MIN, _ATTACH(V_MIN_PIN)); TERN_(USE_W_MAX, _ATTACH(W_MAX_PIN)); TERN_(USE_W_MIN, _ATTACH(W_MIN_PIN)); }
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/endstop_interrupts.h
C
agpl-3.0
3,620
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #include <pwm.h> #define NR_TIMERS TERN(STM32_XL_DENSITY, 14, 8) // Maple timers, 14 for STM32_XL_DENSITY (F/G chips), 8 for HIGH density (C D E) static uint16_t timer_freq[NR_TIMERS]; inline uint8_t timer_and_index_for_pin(const pin_t pin, timer_dev **timer_ptr) { *timer_ptr = PIN_MAP[pin].timer_device; for (uint8_t i = 0; i < NR_TIMERS; i++) if (*timer_ptr == HAL_get_timer_dev(i)) return i; return 0; } void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) { const uint16_t duty = invert ? v_size - v : v; if (PWM_PIN(pin)) { timer_dev *timer; if (timer_freq[timer_and_index_for_pin(pin, &timer)] == 0) set_pwm_frequency(pin, PWM_FREQUENCY); const uint8_t channel = PIN_MAP[pin].timer_channel; timer_set_compare(timer, channel, duty); timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode } else { pinMode(pin, OUTPUT); digitalWrite(pin, duty < v_size / 2 ? LOW : HIGH); } } void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) { if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer timer_dev *timer; timer_freq[timer_and_index_for_pin(pin, &timer)] = f_desired; // Protect used timers if (timer == HAL_get_timer_dev(MF_TIMER_TEMP)) return; if (timer == HAL_get_timer_dev(MF_TIMER_STEP)) return; #if MF_TIMER_PULSE != MF_TIMER_STEP if (timer == HAL_get_timer_dev(MF_TIMER_PULSE)) return; #endif if (!(timer->regs.bas->SR & TIMER_CR1_CEN)) // Ensure the timer is enabled timer_init(timer); const uint8_t channel = PIN_MAP[pin].timer_channel; timer_set_mode(timer, channel, TIMER_PWM); // Preload (resolution) cannot be equal to duty of 255 otherwise it may not result in digital off or on. uint16_t preload = 254; int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1; if (prescaler > 65535) { // For low frequencies increase prescaler prescaler = 65535; preload = (HAL_TIMER_RATE) / (prescaler + 1) / f_desired - 1; } if (prescaler < 0) return; // Too high frequency timer_set_reload(timer, preload); timer_set_prescaler(timer, prescaler); } #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/fast_pwm.cpp
C++
agpl-3.0
3,196
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Fast I/O interfaces for STM32F1 * These use GPIO functions instead of Direct Port Manipulation, as on AVR. */ #include <libmaple/gpio.h> #define READ(IO) (PIN_MAP[IO].gpio_device->regs->IDR & _BV32(PIN_MAP[IO].gpio_bit) ? HIGH : LOW) #define WRITE(IO,V) (PIN_MAP[IO].gpio_device->regs->BSRR = _BV32(PIN_MAP[IO].gpio_bit) << ((V) ? 0 : 16)) #define TOGGLE(IO) TBI32(PIN_MAP[IO].gpio_device->regs->ODR, PIN_MAP[IO].gpio_bit) #define _GET_MODE(IO) gpio_get_mode(PIN_MAP[IO].gpio_device, PIN_MAP[IO].gpio_bit) #define _SET_MODE(IO,M) gpio_set_mode(PIN_MAP[IO].gpio_device, PIN_MAP[IO].gpio_bit, M) #define _SET_OUTPUT(IO) _SET_MODE(IO, GPIO_OUTPUT_PP) #define _SET_OUTPUT_OD(IO) _SET_MODE(IO, GPIO_OUTPUT_OD) #define OUT_WRITE(IO,V) do{ _SET_OUTPUT(IO); WRITE(IO,V); }while(0) #define OUT_WRITE_OD(IO,V) do{ _SET_OUTPUT_OD(IO); WRITE(IO,V); }while(0) #define SET_INPUT(IO) _SET_MODE(IO, GPIO_INPUT_FLOATING) #define SET_INPUT_PULLUP(IO) _SET_MODE(IO, GPIO_INPUT_PU) #define SET_INPUT_PULLDOWN(IO) _SET_MODE(IO, GPIO_INPUT_PD) #define SET_OUTPUT(IO) OUT_WRITE(IO, LOW) #define SET_PWM(IO) pinMode(IO, PWM) // do{ gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, GPIO_AF_OUTPUT_PP); timer_set_mode(PIN_MAP[pin].timer_device, PIN_MAP[pin].timer_channel, TIMER_PWM); }while(0) #define SET_PWM_OD(IO) pinMode(IO, PWM_OPEN_DRAIN) #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 PWM_PIN(IO) !!PIN_MAP[IO].timer_device // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) // // Pins Definitions // #define PA0 0x00 #define PA1 0x01 #define PA2 0x02 #define PA3 0x03 #define PA4 0x04 #define PA5 0x05 #define PA6 0x06 #define PA7 0x07 #define PA8 0x08 #define PA9 0x09 #define PA10 0x0A #define PA11 0x0B #define PA12 0x0C #define PA13 0x0D #define PA14 0x0E #define PA15 0x0F #define PB0 0x10 #define PB1 0x11 #define PB2 0x12 #define PB3 0x13 #define PB4 0x14 #define PB5 0x15 #define PB6 0x16 #define PB7 0x17 // 36 pins (F103T) #define PB8 0x18 #define PB9 0x19 #define PB10 0x1A #define PB11 0x1B #define PB12 0x1C #define PB13 0x1D #define PB14 0x1E #define PB15 0x1F #if defined(MCU_STM32F103CB) || defined(MCU_STM32F103C8) #define PC13 0x20 #define PC14 0x21 #define PC15 0x22 #else #define PC0 0x20 #define PC1 0x21 #define PC2 0x22 #define PC3 0x23 #define PC4 0x24 #define PC5 0x25 #define PC6 0x26 #define PC7 0x27 #define PC8 0x28 #define PC9 0x29 #define PC10 0x2A #define PC11 0x2B #define PC12 0x2C #define PC13 0x2D #define PC14 0x2E #define PC15 0x2F #endif #define PD0 0x30 #define PD1 0x31 #define PD2 0x32 // 64 pins (F103R) #define PD3 0x33 #define PD4 0x34 #define PD5 0x35 #define PD6 0x36 #define PD7 0x37 #define PD8 0x38 #define PD9 0x39 #define PD10 0x3A #define PD11 0x3B #define PD12 0x3C #define PD13 0x3D #define PD14 0x3E #define PD15 0x3F #define PE0 0x40 #define PE1 0x41 #define PE2 0x42 #define PE3 0x43 #define PE4 0x44 #define PE5 0x45 #define PE6 0x46 #define PE7 0x47 #define PE8 0x48 #define PE9 0x49 #define PE10 0x4A #define PE11 0x4B #define PE12 0x4C #define PE13 0x4D #define PE14 0x4E #define PE15 0x4F // 100 pins (F103V) #define PF0 0x50 #define PF1 0x51 #define PF2 0x52 #define PF3 0x53 #define PF4 0x54 #define PF5 0x55 #define PF6 0x56 #define PF7 0x57 #define PF8 0x58 #define PF9 0x59 #define PF10 0x5A #define PF11 0x5B #define PF12 0x5C #define PF13 0x5D #define PF14 0x5E #define PF15 0x5F #define PG0 0x60 #define PG1 0x61 #define PG2 0x62 #define PG3 0x63 #define PG4 0x64 #define PG5 0x65 #define PG6 0x66 #define PG7 0x67 #define PG8 0x68 #define PG9 0x69 #define PG10 0x6A #define PG11 0x6B #define PG12 0x6C #define PG13 0x6D #define PG14 0x6E #define PG15 0x6F // 144 pins (F103Z)
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/fastio.h
C
agpl-3.0
5,113
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/inc/Conditionals_LCD.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #ifdef USE_USB_COMPOSITE //#warning "SD_CHECK_AND_RETRY isn't needed with USE_USB_COMPOSITE." #undef SD_CHECK_AND_RETRY #if DISABLED(NO_SD_HOST_DRIVE) #define HAS_SD_HOST_DRIVE 1 #endif #endif
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/inc/Conditionals_adv.h
C
agpl-3.0
1,081
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once // If no real EEPROM, Flash emulation, or SRAM emulation is available fall back to SD emulation #if USE_FALLBACK_EEPROM #define SDCARD_EEPROM_EMULATION #elif ANY(I2C_EEPROM, SPI_EEPROM) #define USE_SHARED_EEPROM 1 #endif // Allow for no media drives #if !HAS_MEDIA #undef ONBOARD_SDIO #endif
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/inc/Conditionals_post.h
C
agpl-3.0
1,175
/** * Marlin 3D Printer Firmware * Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/inc/Conditionals_type.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Test STM32F1-specific configuration values for errors at compile-time. */ #if ENABLED(SDCARD_EEPROM_EMULATION) && !HAS_MEDIA #undef SDCARD_EEPROM_EMULATION // Avoid additional error noise #if USE_FALLBACK_EEPROM #warning "EEPROM type not specified. Fallback is SDCARD_EEPROM_EMULATION." #endif #error "SDCARD_EEPROM_EMULATION requires SDSUPPORT. Enable SDSUPPORT or choose another EEPROM emulation." #endif #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED) #error "SERIAL_STATS_MAX_RX_QUEUED is not supported on the STM32F1 platform." #elif ENABLED(SERIAL_STATS_DROPPED_RX) #error "SERIAL_STATS_DROPPED_RX is not supported on the STM32F1 platform." #endif #if ENABLED(NEOPIXEL_LED) && DISABLED(FYSETC_MINI_12864_2_1) #error "NEOPIXEL_LED (Adafruit NeoPixel) is not supported for HAL/STM32F1. Comment out this line to proceed at your own risk!" #endif // Emergency Parser needs at least one serial with HardwareSerial or USBComposite. // The USBSerial maple don't allow any hook to implement EMERGENCY_PARSER. // And copy all USBSerial code to marlin space to support EMERGENCY_PARSER, when we have another options, don't worth it. #if ENABLED(EMERGENCY_PARSER) && !defined(USE_USB_COMPOSITE) && ((SERIAL_PORT == -1 && !defined(SERIAL_PORT_2)) || (SERIAL_PORT_2 == -1 && !defined(SERIAL_PORT))) #error "EMERGENCY_PARSER is only supported by HardwareSerial or USBComposite in HAL/STM32F1." #endif
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/inc/SanityCheck.h
C
agpl-3.0
2,293
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * Copyright (c) 2019 BigTreeTech [https://github.com/bigtreetech] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../inc/MarlinConfigPre.h" #if HAS_SD_HOST_DRIVE #include "msc_sd.h" #include "SPI.h" #include "usb_reg_map.h" #define PRODUCT_ID 0x29 USBMassStorage MarlinMSC; Serial1Class<USBCompositeSerial> MarlinCompositeSerial(true); #include "../../inc/MarlinConfig.h" #if SD_CONNECTION_IS(ONBOARD) #include "onboard_sd.h" static bool MSC_Write(const uint8_t *writebuff, uint32_t startSector, uint16_t numSectors) { return (disk_write(0, writebuff, startSector, numSectors) == RES_OK); } static bool MSC_Read(uint8_t *readbuff, uint32_t startSector, uint16_t numSectors) { return (disk_read(0, readbuff, startSector, numSectors) == RES_OK); } #endif #if ENABLED(EMERGENCY_PARSER) // The original callback is not called (no way to retrieve address). // That callback detects a special STM32 reset sequence: this functionality is not essential // as M997 achieves the same. void my_rx_callback(unsigned int, void*) { // max length of 16 is enough to contain all emergency commands uint8 buf[16]; //rx is usbSerialPart.endpoints[2] uint16 len = usb_get_ep_rx_count(usbSerialPart.endpoints[2].address); uint32 total = composite_cdcacm_data_available(); if (len == 0 || total == 0 || !WITHIN(total, len, COUNT(buf))) return; // cannot get character by character due to bug in composite_cdcacm_peek_ex len = composite_cdcacm_peek(buf, total); for (uint32 i = 0; i < len; i++) emergency_parser.update(MarlinCompositeSerial.emergency_state, buf[i+total-len]); } #endif void MSC_SD_init() { USBComposite.setProductId(PRODUCT_ID); // Just set MarlinCompositeSerial enabled to true // because when MarlinCompositeSerial.begin() is used in setup() // it clears all USBComposite devices. MarlinCompositeSerial.begin(); USBComposite.end(); USBComposite.clear(); // Set api and register mass storage #if SD_CONNECTION_IS(ONBOARD) uint32_t cardSize; if (disk_initialize(0) == RES_OK) { if (disk_ioctl(0, GET_SECTOR_COUNT, (void *)(&cardSize)) == RES_OK) { MarlinMSC.setDriveData(0, cardSize, MSC_Read, MSC_Write); MarlinMSC.registerComponent(); } } #endif // Register composite Serial MarlinCompositeSerial.registerComponent(); USBComposite.begin(); #if ENABLED(EMERGENCY_PARSER) composite_cdcacm_set_hooks(USBHID_CDCACM_HOOK_RX, my_rx_callback); #endif } #endif // HAS_SD_HOST_DRIVE #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/msc_sd.cpp
C++
agpl-3.0
3,409
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * Copyright (c) 2019 BigTreeTech [https://github.com/bigtreetech] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <USBComposite.h> #include "../../inc/MarlinConfigPre.h" #include "../../core/serial_hook.h" extern USBMassStorage MarlinMSC; extern Serial1Class<USBCompositeSerial> MarlinCompositeSerial; void MSC_SD_init();
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/msc_sd.h
C
agpl-3.0
1,163
/** * STM32F1: MMCv3/SDv1/SDv2 (SPI mode) control module * * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * Copyright (c) 2019 BigTreeTech [https://github.com/bigtreetech] * Copyright (C) 2015, ChaN, all right reserved. * * This software is a free software and there is NO WARRANTY. * No restriction on use. You can use, modify and redistribute it for * personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY. * Redistributions of source code must retain the above copyright notice. */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" #if SD_CONNECTION_IS(ONBOARD) #include "onboard_sd.h" #include "SPI.h" #include "fastio.h" #ifndef ONBOARD_SPI_DEVICE #define ONBOARD_SPI_DEVICE SPI_DEVICE #endif #if HAS_SD_HOST_DRIVE #define ONBOARD_SD_SPI SPI #else SPIClass OnboardSPI(ONBOARD_SPI_DEVICE); #define ONBOARD_SD_SPI OnboardSPI #endif #if ONBOARD_SPI_DEVICE == 1 #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_4 #else #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_2 #endif #if PIN_EXISTS(ONBOARD_SD_CS) && ONBOARD_SD_CS_PIN != SD_SS_PIN #define CS_LOW() WRITE(ONBOARD_SD_CS_PIN, LOW) // Set OnboardSPI cs low #define CS_HIGH() WRITE(ONBOARD_SD_CS_PIN, HIGH) // Set OnboardSPI cs high #else #define CS_LOW() #define CS_HIGH() #endif #define FCLK_FAST() ONBOARD_SD_SPI.setClockDivider(SPI_CLOCK_MAX) #define FCLK_SLOW() ONBOARD_SD_SPI.setClockDivider(SPI_BAUD_PCLK_DIV_256) /*-------------------------------------------------------------------------- Module Private Functions ---------------------------------------------------------------------------*/ /* MMC/SD command */ #define CMD0 (0) // GO_IDLE_STATE #define CMD1 (1) // SEND_OP_COND (MMC) #define ACMD41 (0x80+41) // SEND_OP_COND (SDC) #define CMD8 (8) // SEND_IF_COND #define CMD9 (9) // SEND_CSD #define CMD10 (10) // SEND_CID #define CMD12 (12) // STOP_TRANSMISSION #define ACMD13 (0x80+13) // SD_STATUS (SDC) #define CMD16 (16) // SET_BLOCKLEN #define CMD17 (17) // READ_SINGLE_BLOCK #define CMD18 (18) // READ_MULTIPLE_BLOCK #define CMD23 (23) // SET_BLOCK_COUNT (MMC) #define ACMD23 (0x80+23) // SET_WR_BLK_ERASE_COUNT (SDC) #define CMD24 (24) // WRITE_BLOCK #define CMD25 (25) // WRITE_MULTIPLE_BLOCK #define CMD32 (32) // ERASE_ER_BLK_START #define CMD33 (33) // ERASE_ER_BLK_END #define CMD38 (38) // ERASE #define CMD48 (48) // READ_EXTR_SINGLE #define CMD49 (49) // WRITE_EXTR_SINGLE #define CMD55 (55) // APP_CMD #define CMD58 (58) // READ_OCR static volatile DSTATUS Stat = STA_NOINIT; // Physical drive status static volatile UINT timeout; static BYTE CardType; // Card type flags /*-----------------------------------------------------------------------*/ /* Send/Receive data to the MMC (Platform dependent) */ /*-----------------------------------------------------------------------*/ /* Exchange a byte */ static BYTE xchg_spi ( BYTE dat // Data to send ) { BYTE returnByte = ONBOARD_SD_SPI.transfer(dat); return returnByte; } /* Receive multiple byte */ static void rcvr_spi_multi ( BYTE *buff, // Pointer to data buffer UINT btr // Number of bytes to receive (16, 64 or 512) ) { ONBOARD_SD_SPI.dmaTransfer(0, const_cast<uint8_t*>(buff), btr); } #if _DISKIO_WRITE // Send multiple bytes static void xmit_spi_multi ( const BYTE *buff, // Pointer to the data UINT btx // Number of bytes to send (multiple of 16) ) { ONBOARD_SD_SPI.dmaSend(const_cast<uint8_t*>(buff), btx); } #endif // _DISKIO_WRITE /*-----------------------------------------------------------------------*/ /* Wait for card ready */ /*-----------------------------------------------------------------------*/ static int wait_ready ( // 1:Ready, 0:Timeout UINT wt // Timeout [ms] ) { BYTE d; timeout = millis() + wt; do { d = xchg_spi(0xFF); // This loop takes a while. Insert rot_rdq() here for multitask environment. } while (d != 0xFF && (timeout > millis())); // Wait for card goes ready or timeout return (d == 0xFF) ? 1 : 0; } /*-----------------------------------------------------------------------*/ /* Deselect card and release SPI */ /*-----------------------------------------------------------------------*/ static void deselect() { CS_HIGH(); // CS = H xchg_spi(0xFF); // Dummy clock (force DO hi-z for multiple slave SPI) } /*-----------------------------------------------------------------------*/ /* Select card and wait for ready */ /*-----------------------------------------------------------------------*/ static int select() { // 1:OK, 0:Timeout CS_LOW(); // CS = L xchg_spi(0xFF); // Dummy clock (force DO enabled) if (wait_ready(500)) return 1; // Leading busy check: Wait for card ready deselect(); // Timeout return 0; } /*-----------------------------------------------------------------------*/ /* Control SPI module (Platform dependent) */ /*-----------------------------------------------------------------------*/ // Enable SSP module and attach it to I/O pads static void sd_power_on() { ONBOARD_SD_SPI.setModule(ONBOARD_SPI_DEVICE); ONBOARD_SD_SPI.begin(); ONBOARD_SD_SPI.setBitOrder(MSBFIRST); ONBOARD_SD_SPI.setDataMode(SPI_MODE0); CS_HIGH(); } // Disable SPI function static void sd_power_off() { select(); // Wait for card ready deselect(); } /*-----------------------------------------------------------------------*/ /* Receive a data packet from the MMC */ /*-----------------------------------------------------------------------*/ static int rcvr_datablock ( // 1:OK, 0:Error BYTE *buff, // Data buffer UINT btr // Data block length (byte) ) { BYTE token; timeout = millis() + 200; do { // Wait for DataStart token in timeout of 200ms token = xchg_spi(0xFF); // This loop will take a while. Insert rot_rdq() here for multitask environment. } while ((token == 0xFF) && (timeout > millis())); if (token != 0xFE) return 0; // Function fails if invalid DataStart token or timeout rcvr_spi_multi(buff, btr); // Store trailing data to the buffer xchg_spi(0xFF); xchg_spi(0xFF); // Discard CRC return 1; // Function succeeded } /*-----------------------------------------------------------------------*/ /* Send a data packet to the MMC */ /*-----------------------------------------------------------------------*/ #if _DISKIO_WRITE static int xmit_datablock( // 1:OK, 0:Failed const BYTE *buff, // Pointer to 512 byte data to be sent BYTE token // Token ) { BYTE resp; if (!wait_ready(500)) return 0; // Leading busy check: Wait for card ready to accept data block xchg_spi(token); // Send token if (token == 0xFD) return 1; // Do not send data if token is StopTran xmit_spi_multi(buff, 512); // Data xchg_spi(0xFF); xchg_spi(0xFF); // Dummy CRC resp = xchg_spi(0xFF); // Receive data resp return (resp & 0x1F) == 0x05 ? 1 : 0; // Data was accepted or not // Busy check is done at next transmission } #endif // _DISKIO_WRITE /*-----------------------------------------------------------------------*/ /* Send a command packet to the MMC */ /*-----------------------------------------------------------------------*/ static BYTE send_cmd( // Return value: R1 resp (bit7==1:Failed to send) BYTE cmd, // Command index DWORD arg // Argument ) { BYTE n, res; if (cmd & 0x80) { // Send a CMD55 prior to ACMD<n> cmd &= 0x7F; res = send_cmd(CMD55, 0); if (res > 1) return res; } // Select the card and wait for ready except to stop multiple block read if (cmd != CMD12) { deselect(); if (!select()) return 0xFF; } // Send command packet xchg_spi(0x40 | cmd); // Start + command index xchg_spi((BYTE)(arg >> 24)); // Argument[31..24] xchg_spi((BYTE)(arg >> 16)); // Argument[23..16] xchg_spi((BYTE)(arg >> 8)); // Argument[15..8] xchg_spi((BYTE)arg); // Argument[7..0] n = 0x01; // Dummy CRC + Stop if (cmd == CMD0) n = 0x95; // Valid CRC for CMD0(0) if (cmd == CMD8) n = 0x87; // Valid CRC for CMD8(0x1AA) xchg_spi(n); // Receive command response if (cmd == CMD12) xchg_spi(0xFF); // Discard the following byte when CMD12 n = 10; // Wait for response (10 bytes max) do res = xchg_spi(0xFF); while ((res & 0x80) && --n); return res; // Return received response } /*-------------------------------------------------------------------------- Public Functions ---------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/ /* Initialize disk drive */ /*-----------------------------------------------------------------------*/ DSTATUS disk_initialize ( BYTE drv // Physical drive number (0) ) { BYTE n, cmd, ty, ocr[4]; if (drv) return STA_NOINIT; // Supports only drive 0 sd_power_on(); // Initialize SPI if (Stat & STA_NODISK) return Stat; // Is a card existing in the socket? FCLK_SLOW(); for (n = 10; n; n--) xchg_spi(0xFF); // Send 80 dummy clocks ty = 0; if (send_cmd(CMD0, 0) == 1) { // Put the card SPI state timeout = millis() + 1000; // Initialization timeout = 1 sec if (send_cmd(CMD8, 0x1AA) == 1) { // Is the catd SDv2? for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); // Get 32 bit return value of R7 resp if (ocr[2] == 0x01 && ocr[3] == 0xAA) { // Does the card support 2.7-3.6V? while ((timeout > millis()) && send_cmd(ACMD41, 1UL << 30)); // Wait for end of initialization with ACMD41(HCS) if ((timeout > millis()) && send_cmd(CMD58, 0) == 0) { // Check CCS bit in the OCR for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; // Check if the card is SDv2 } } } else { // Not an SDv2 card if (send_cmd(ACMD41, 0) <= 1) { // SDv1 or MMCv3? ty = CT_SD1; cmd = ACMD41; // SDv1 (ACMD41(0)) } else { ty = CT_MMC; cmd = CMD1; // MMCv3 (CMD1(0)) } while ((timeout > millis()) && send_cmd(cmd, 0)); // Wait for the card leaves idle state if (!(timeout > millis()) || send_cmd(CMD16, 512) != 0) // Set block length: 512 ty = 0; } } CardType = ty; // Card type deselect(); if (ty) { // OK FCLK_FAST(); // Set fast clock Stat &= ~STA_NOINIT; // Clear STA_NOINIT flag } else { // Failed sd_power_off(); Stat = STA_NOINIT; } return Stat; } /*-----------------------------------------------------------------------*/ /* Get disk status */ /*-----------------------------------------------------------------------*/ DSTATUS disk_status ( BYTE drv // Physical drive number (0) ) { if (drv) return STA_NOINIT; // Supports only drive 0 return Stat; // Return disk status } /*-----------------------------------------------------------------------*/ /* Read sector(s) */ /*-----------------------------------------------------------------------*/ DRESULT disk_read ( BYTE drv, // Physical drive number (0) BYTE *buff, // Pointer to the data buffer to store read data DWORD sector, // Start sector number (LBA) UINT count // Number of sectors to read (1..128) ) { BYTE cmd; if (drv || !count) return RES_PARERR; // Check parameter if (Stat & STA_NOINIT) return RES_NOTRDY; // Check if drive is ready if (!(CardType & CT_BLOCK)) sector *= 512; // LBA ot BA conversion (byte addressing cards) FCLK_FAST(); cmd = count > 1 ? CMD18 : CMD17; // READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK if (send_cmd(cmd, sector) == 0) { do { if (!rcvr_datablock(buff, 512)) break; buff += 512; } while (--count); if (cmd == CMD18) send_cmd(CMD12, 0); // STOP_TRANSMISSION } deselect(); return count ? RES_ERROR : RES_OK; // Return result } /*-----------------------------------------------------------------------*/ /* Write sector(s) */ /*-----------------------------------------------------------------------*/ #if _DISKIO_WRITE DRESULT disk_write( BYTE drv, // Physical drive number (0) const BYTE *buff, // Pointer to the data to write DWORD sector, // Start sector number (LBA) UINT count // Number of sectors to write (1..128) ) { if (drv || !count) return RES_PARERR; // Check parameter if (Stat & STA_NOINIT) return RES_NOTRDY; // Check drive status if (Stat & STA_PROTECT) return RES_WRPRT; // Check write protect FCLK_FAST(); if (!(CardType & CT_BLOCK)) sector *= 512; // LBA ==> BA conversion (byte addressing cards) if (count == 1) { // Single sector write if ((send_cmd(CMD24, sector) == 0) // WRITE_BLOCK && xmit_datablock(buff, 0xFE)) { count = 0; } } else { // Multiple sector write if (CardType & CT_SDC) send_cmd(ACMD23, count); // Predefine number of sectors if (send_cmd(CMD25, sector) == 0) { // WRITE_MULTIPLE_BLOCK do { if (!xmit_datablock(buff, 0xFC)) break; buff += 512; } while (--count); if (!xmit_datablock(0, 0xFD)) count = 1; // STOP_TRAN token } } deselect(); return count ? RES_ERROR : RES_OK; // Return result } #endif // _DISKIO_WRITE /*-----------------------------------------------------------------------*/ /* Miscellaneous drive controls other than data read/write */ /*-----------------------------------------------------------------------*/ #if _DISKIO_IOCTL DRESULT disk_ioctl ( BYTE drv, // Physical drive number (0) BYTE cmd, // Control command code void *buff // Pointer to the conrtol data ) { DRESULT res; BYTE n, csd[16], *ptr = (BYTE *)buff; DWORD *dp, st, ed, csize; #if _DISKIO_ISDIO SDIO_CMD *sdio = buff; BYTE rc, *buf; UINT dc; #endif if (drv) return RES_PARERR; // Check parameter if (Stat & STA_NOINIT) return RES_NOTRDY; // Check if drive is ready res = RES_ERROR; FCLK_FAST(); switch (cmd) { case CTRL_SYNC: // Wait for end of internal write process of the drive if (select()) res = RES_OK; break; case GET_SECTOR_COUNT: // Get drive capacity in unit of sector (DWORD) if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { if ((csd[0] >> 6) == 1) { // SDC ver 2.00 csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1; *(DWORD*)buff = csize << 10; } else { // SDC ver 1.XX or MMC ver 3 n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2; csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1; *(DWORD*)buff = csize << (n - 9); } res = RES_OK; } break; case GET_BLOCK_SIZE: // Get erase block size in unit of sector (DWORD) if (CardType & CT_SD2) { // SDC ver 2.00 if (send_cmd(ACMD13, 0) == 0) { // Read SD status xchg_spi(0xFF); if (rcvr_datablock(csd, 16)) { // Read partial block for (n = 64 - 16; n; n--) xchg_spi(0xFF); // Purge trailing data *(DWORD*)buff = 16UL << (csd[10] >> 4); res = RES_OK; } } } else { // SDC ver 1.XX or MMC if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { // Read CSD if (CardType & CT_SD1) { // SDC ver 1.XX *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1); } else { // MMC *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1); } res = RES_OK; } } break; case CTRL_TRIM: // Erase a block of sectors (used when _USE_TRIM in ffconf.h is 1) if (!(CardType & CT_SDC)) break; // Check if the card is SDC if (disk_ioctl(drv, MMC_GET_CSD, csd)) break; // Get CSD if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; // Check if sector erase can be applied to the card dp = (DWORD *)buff; st = dp[0]; ed = dp[1]; // Load sector block if (!(CardType & CT_BLOCK)) { st *= 512; ed *= 512; } if (send_cmd(CMD32, st) == 0 && send_cmd(CMD33, ed) == 0 && send_cmd(CMD38, 0) == 0 && wait_ready(30000)) { // Erase sector block res = RES_OK; // FatFs does not check result of this command } break; // The following commands are never used by FatFs module case MMC_GET_TYPE: // Get MMC/SDC type (BYTE) *ptr = CardType; res = RES_OK; break; case MMC_GET_CSD: // Read CSD (16 bytes) if (send_cmd(CMD9, 0) == 0 && rcvr_datablock(ptr, 16)) { res = RES_OK; } break; case MMC_GET_CID: // Read CID (16 bytes) if (send_cmd(CMD10, 0) == 0 && rcvr_datablock(ptr, 16)) { res = RES_OK; } break; case MMC_GET_OCR: // Read OCR (4 bytes) if (send_cmd(CMD58, 0) == 0) { for (n = 4; n; n--) *ptr++ = xchg_spi(0xFF); res = RES_OK; } break; case MMC_GET_SDSTAT: // Read SD status (64 bytes) if (send_cmd(ACMD13, 0) == 0) { xchg_spi(0xFF); if (rcvr_datablock(ptr, 64)) res = RES_OK; } break; #if _DISKIO_ISDIO case ISDIO_READ: sdio = buff; if (send_cmd(CMD48, 0x80000000 | sdio->func << 28 | sdio->addr << 9 | ((sdio->ndata - 1) & 0x1FF)) == 0) { for (Timer1 = 1000; (rc = xchg_spi(0xFF)) == 0xFF && Timer1; ) ; if (rc == 0xFE) { for (buf = sdio->data, dc = sdio->ndata; dc; dc--) *buf++ = xchg_spi(0xFF); for (dc = 514 - sdio->ndata; dc; dc--) xchg_spi(0xFF); res = RES_OK; } } break; case ISDIO_WRITE: sdio = buff; if (send_cmd(CMD49, 0x80000000 | sdio->func << 28 | sdio->addr << 9 | ((sdio->ndata - 1) & 0x1FF)) == 0) { xchg_spi(0xFF); xchg_spi(0xFE); for (buf = sdio->data, dc = sdio->ndata; dc; dc--) xchg_spi(*buf++); for (dc = 514 - sdio->ndata; dc; dc--) xchg_spi(0xFF); if ((xchg_spi(0xFF) & 0x1F) == 0x05) res = RES_OK; } break; case ISDIO_MRITE: sdio = buff; if (send_cmd(CMD49, 0x84000000 | sdio->func << 28 | sdio->addr << 9 | sdio->ndata >> 8) == 0) { xchg_spi(0xFF); xchg_spi(0xFE); xchg_spi(sdio->ndata); for (dc = 513; dc; dc--) xchg_spi(0xFF); if ((xchg_spi(0xFF) & 0x1F) == 0x05) res = RES_OK; } break; #endif // _DISKIO_ISDIO default: res = RES_PARERR; } deselect(); return res; } #endif // _DISKIO_IOCTL #endif // SD_CONNECTION_IS(ONBOARD) #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/onboard_sd.cpp
C++
agpl-3.0
21,550
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /*----------------------------------------------------------------------- / * Copyright (c) 2019 BigTreeTech [https://github.com/bigtreetech] / * Low level disk interface module include file (c) ChaN, 2015 /-----------------------------------------------------------------------*/ #define _DISKIO_WRITE 1 /* 1: Enable disk_write function */ #define _DISKIO_IOCTL 1 /* 1: Enable disk_ioctl function */ #define _DISKIO_ISDIO 0 /* 1: Enable iSDIO control function */ typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned long DWORD; typedef unsigned int UINT; /* Status of Disk Functions */ typedef BYTE DSTATUS; /* Results of Disk Functions */ typedef enum { RES_OK = 0, /* 0: Successful */ RES_ERROR, /* 1: R/W Error */ RES_WRPRT, /* 2: Write Protected */ RES_NOTRDY, /* 3: Not Ready */ RES_PARERR /* 4: Invalid Parameter */ } DRESULT; #if _DISKIO_ISDIO /* Command structure for iSDIO ioctl command */ typedef struct { BYTE func; /* Function number: 0..7 */ WORD ndata; /* Number of bytes to transfer: 1..512, or mask + data */ DWORD addr; /* Register address: 0..0x1FFFF */ void* data; /* Pointer to the data (to be written | read buffer) */ } SDIO_CMD; #endif /*---------------------------------------*/ /* Prototypes for disk control functions */ DSTATUS disk_initialize(BYTE pdrv); DSTATUS disk_status(BYTE pdrv); DRESULT disk_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count); #if _DISKIO_WRITE DRESULT disk_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); #endif #if _DISKIO_IOCTL DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff); #endif /* Disk Status Bits (DSTATUS) */ #define STA_NOINIT 0x01 /* Drive not initialized */ #define STA_NODISK 0x02 /* No medium in the drive */ #define STA_PROTECT 0x04 /* Write protected */ /* Command code for disk_ioctrl function */ /* Generic command (Used by FatFs) */ #define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */ #define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */ #define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */ #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */ #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */ /* Generic command (Not used by FatFs) */ #define CTRL_FORMAT 5 /* Create physical format on the media */ #define CTRL_POWER_IDLE 6 /* Put the device idle state */ #define CTRL_POWER_OFF 7 /* Put the device off state */ #define CTRL_LOCK 8 /* Lock media removal */ #define CTRL_UNLOCK 9 /* Unlock media removal */ #define CTRL_EJECT 10 /* Eject media */ /* MMC/SDC specific ioctl command (Not used by FatFs) */ #define MMC_GET_TYPE 50 /* Get card type */ #define MMC_GET_CSD 51 /* Get CSD */ #define MMC_GET_CID 52 /* Get CID */ #define MMC_GET_OCR 53 /* Get OCR */ #define MMC_GET_SDSTAT 54 /* Get SD status */ #define ISDIO_READ 55 /* Read data form SD iSDIO register */ #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ /* ATA/CF specific ioctl command (Not used by FatFs) */ #define ATA_GET_REV 60 /* Get F/W revision */ #define ATA_GET_MODEL 61 /* Get model name */ #define ATA_GET_SN 62 /* Get serial number */ /* MMC card type flags (MMC_GET_TYPE) */ #define CT_MMC 0x01 /* MMC ver 3 */ #define CT_SD1 0x02 /* SD ver 1 */ #define CT_SD2 0x04 /* SD ver 2 */ #define CT_SDC (CT_SD1|CT_SD2) /* SD */ #define CT_BLOCK 0x08 /* Block addressing */
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/onboard_sd.h
C
agpl-3.0
4,662
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Support routines for MAPLE_STM32F1 */ /** * Translation of routines & variables used by pinsDebug.h */ #ifndef BOARD_NR_GPIO_PINS // Only in MAPLE_STM32F1 #error "Expected BOARD_NR_GPIO_PINS not found" #endif #include "fastio.h" extern const stm32_pin_info PIN_MAP[BOARD_NR_GPIO_PINS]; #define NUM_DIGITAL_PINS BOARD_NR_GPIO_PINS #define NUMBER_PINS_TOTAL BOARD_NR_GPIO_PINS #define VALID_PIN(pin) (pin >= 0 && pin < BOARD_NR_GPIO_PINS) #define GET_ARRAY_PIN(p) pin_t(pin_array[p].pin) #define digitalRead_mod(p) extDigitalRead(p) #define PRINT_PIN(p) do{ sprintf_P(buffer, PSTR("%3hd "), int16_t(p)); SERIAL_ECHO(buffer); }while(0) #define PRINT_PIN_ANALOG(p) do{ sprintf_P(buffer, PSTR(" (A%2d) "), DIGITAL_PIN_TO_ANALOG_PIN(pin)); SERIAL_ECHO(buffer); }while(0) #define PRINT_ARRAY_NAME(x) do{ sprintf_P(buffer, PSTR("%-" STRINGIFY(MAX_NAME_LENGTH) "s"), pin_array[x].name); SERIAL_ECHO(buffer); }while(0) #define MULTI_NAME_PAD 21 // space needed to be pretty if not first name assigned to a pin // pins that will cause hang/reset/disconnect in M43 Toggle and Watch utilities #ifndef M43_NEVER_TOUCH #define M43_NEVER_TOUCH(Q) (Q >= 9 && Q <= 12) // SERIAL/USB pins PA9(TX) PA10(RX) #endif int8_t get_pin_mode(const pin_t pin) { return VALID_PIN(pin) ? _GET_MODE(pin) : -1; } pin_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t pin) { if (!VALID_PIN(pin)) return -1; pin_t adc_channel = pin_t(PIN_MAP[pin].adc_channel); #ifdef NUM_ANALOG_INPUTS if (adc_channel >= NUM_ANALOG_INPUTS) adc_channel = (pin_t)ADCx; #endif return adc_channel; } bool IS_ANALOG(const pin_t pin) { if (!VALID_PIN(pin)) return false; if (PIN_MAP[pin].adc_channel != ADCx) { #ifdef NUM_ANALOG_INPUTS if (PIN_MAP[pin].adc_channel >= NUM_ANALOG_INPUTS) return false; #endif return _GET_MODE(pin) == GPIO_INPUT_ANALOG && !M43_NEVER_TOUCH(pin); } return false; } bool GET_PINMODE(const pin_t pin) { return VALID_PIN(pin) && !IS_INPUT(pin); } bool GET_ARRAY_IS_DIGITAL(const int16_t array_pin) { const pin_t pin = GET_ARRAY_PIN(array_pin); return (!IS_ANALOG(pin) #ifdef NUM_ANALOG_INPUTS || PIN_MAP[pin].adc_channel >= NUM_ANALOG_INPUTS #endif ); } #include "../../inc/MarlinConfig.h" // Allow pins/pins.h to set density void pwm_details(const pin_t pin) { if (PWM_PIN(pin)) { timer_dev * const tdev = PIN_MAP[pin].timer_device; const uint8_t channel = PIN_MAP[pin].timer_channel; const char num = ( #if ANY(STM32_HIGH_DENSITY, STM32_XL_DENSITY) tdev == &timer8 ? '8' : tdev == &timer5 ? '5' : #endif tdev == &timer4 ? '4' : tdev == &timer3 ? '3' : tdev == &timer2 ? '2' : tdev == &timer1 ? '1' : '?' ); char buffer[10]; sprintf_P(buffer, PSTR(" TIM%c CH%c"), num, ('0' + channel)); SERIAL_ECHO(buffer); } } bool pwm_status(const pin_t pin) { return PWM_PIN(pin); } void print_port(const pin_t pin) { const char port = 'A' + char(pin >> 4); // pin div 16 const int16_t gbit = PIN_MAP[pin].gpio_bit; char buffer[8]; sprintf_P(buffer, PSTR("P%c%hd "), port, gbit); if (gbit < 10) SERIAL_CHAR(' '); SERIAL_ECHO(buffer); }
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/pinsDebug.h
C
agpl-3.0
4,048
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef ARDUINO_ARCH_STM32F1 #include <libmaple/stm32.h> #include "../../inc/MarlinConfig.h" // Allow pins/pins.h to set density #if ANY(STM32_HIGH_DENSITY, STM32_XL_DENSITY) #include "sdio.h" SDIO_CardInfoTypeDef SdCard; bool SDIO_Init() { uint32_t count = 0U; SdCard.CardType = SdCard.CardVersion = SdCard.Class = SdCard.RelCardAdd = SdCard.BlockNbr = SdCard.BlockSize = SdCard.LogBlockNbr = SdCard.LogBlockSize = 0; sdio_begin(); sdio_set_dbus_width(SDIO_CLKCR_WIDBUS_1BIT); dma_init(SDIO_DMA_DEV); dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); dma_set_priority(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, DMA_PRIORITY_MEDIUM); if (!SDIO_CmdGoIdleState()) return false; if (!SDIO_CmdGoIdleState()) return false; /* Hotplugged cards tends to miss first CMD0, so give them a second chance. */ SdCard.CardVersion = SDIO_CmdOperCond() ? CARD_V2_X : CARD_V1_X; do { if (count++ == SDMMC_MAX_VOLT_TRIAL) return false; SDIO_CmdAppOperCommand(SdCard.CardVersion == CARD_V2_X ? SDMMC_HIGH_CAPACITY : SDMMC_STD_CAPACITY); } while ((SDIO_GetResponse(SDIO_RESP1) & 0x80000000) == 0); SdCard.CardType = (SDIO_GetResponse(SDIO_RESP1) & SDMMC_HIGH_CAPACITY) ? CARD_SDHC_SDXC : CARD_SDSC; if (!SDIO_CmdSendCID()) return false; if (!SDIO_CmdSetRelAdd(&SdCard.RelCardAdd)) return false; /* Send CMD3 SET_REL_ADDR with argument 0. SD Card publishes its RCA. */ if (!SDIO_CmdSendCSD(SdCard.RelCardAdd << 16U)) return false; SdCard.Class = (SDIO_GetResponse(SDIO_RESP2) >> 20U); if (SdCard.CardType == CARD_SDHC_SDXC) { SdCard.LogBlockNbr = SdCard.BlockNbr = (((SDIO_GetResponse(SDIO_RESP2) & 0x0000003FU) << 26U) | ((SDIO_GetResponse(SDIO_RESP3) & 0xFFFF0000U) >> 6U)) + 1024; SdCard.LogBlockSize = SdCard.BlockSize = 512U; } else { SdCard.BlockNbr = ((((SDIO_GetResponse(SDIO_RESP2) & 0x000003FFU) << 2U ) | ((SDIO_GetResponse(SDIO_RESP3) & 0xC0000000U) >> 30U)) + 1U) * (4U << ((SDIO_GetResponse(SDIO_RESP3) & 0x00038000U) >> 15U)); SdCard.BlockSize = 1U << ((SDIO_GetResponse(SDIO_RESP2) >> 16) & 0x0FU); SdCard.LogBlockNbr = (SdCard.BlockNbr) * ((SdCard.BlockSize) / 512U); SdCard.LogBlockSize = 512U; } if (!SDIO_CmdSelDesel(SdCard.RelCardAdd << 16U)) return false; if (!SDIO_CmdAppSetClearCardDetect(SdCard.RelCardAdd << 16U)) return false; if (!SDIO_CmdAppSetBusWidth(SdCard.RelCardAdd << 16U, 2)) return false; sdio_set_dbus_width(SDIO_CLKCR_WIDBUS_4BIT); sdio_set_clock(SDIO_CLOCK); return true; } bool SDIO_ReadBlock_DMA(uint32_t blockAddress, uint8_t *data) { if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false; if (blockAddress >= SdCard.LogBlockNbr) return false; if ((0x03 & (uint32_t)data)) return false; // misaligned data if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; } dma_setup_transfer(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, &SDIO->FIFO, DMA_SIZE_32BITS, data, DMA_SIZE_32BITS, DMA_MINC_MODE); dma_set_num_transfers(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, 128); dma_clear_isr_bits(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); dma_enable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); sdio_setup_transfer(SDIO_DATA_TIMEOUT * (F_CPU / 1000U), 512, SDIO_BLOCKSIZE_512 | SDIO_DCTRL_DMAEN | SDIO_DCTRL_DTEN | SDIO_DIR_RX); if (!SDIO_CmdReadSingleBlock(blockAddress)) { SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); return false; } while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) { /* wait */ } //If there were SDIO errors, do not wait DMA. if (SDIO->STA & SDIO_STA_TRX_ERROR_FLAGS) { SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); return false; } //Wait for DMA transaction to complete while ((DMA2_BASE->ISR & (DMA_ISR_TEIF4|DMA_ISR_TCIF4)) == 0 ) { /* wait */ } if (DMA2_BASE->ISR & DMA_ISR_TEIF4) { dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); return false; } dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); if (SDIO->STA & SDIO_STA_RXDAVL) { while (SDIO->STA & SDIO_STA_RXDAVL) (void)SDIO->FIFO; SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); return false; } if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) { SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); return false; } SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); return true; } bool SDIO_ReadBlock(uint32_t blockAddress, uint8_t *data) { uint8_t retries = SDIO_READ_RETRIES; while (retries--) { if (SDIO_ReadBlock_DMA(blockAddress, data)) return true; #if SD_RETRY_DELAY_MS delay(SD_RETRY_DELAY_MS); #endif } return false; } uint32_t millis(); bool SDIO_WriteBlock(uint32_t blockAddress, const uint8_t *data) { if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false; if (blockAddress >= SdCard.LogBlockNbr) return false; if ((0x03 & (uint32_t)data)) return false; // misaligned data if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; } dma_setup_transfer(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, &SDIO->FIFO, DMA_SIZE_32BITS, (volatile void *) data, DMA_SIZE_32BITS, DMA_MINC_MODE | DMA_FROM_MEM); dma_set_num_transfers(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, 128); dma_clear_isr_bits(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); dma_enable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); if (!SDIO_CmdWriteSingleBlock(blockAddress)) { dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); return false; } sdio_setup_transfer(SDIO_DATA_TIMEOUT * (F_CPU / 1000U), 512U, SDIO_BLOCKSIZE_512 | SDIO_DCTRL_DMAEN | SDIO_DCTRL_DTEN); while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) { /* wait */ } dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) { SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); return false; } SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); uint32_t timeout = millis() + SDIO_WRITE_TIMEOUT; while (timeout > millis()) { if (SDIO_GetCardState() == SDIO_CARD_TRANSFER) { return true; } } return false; } inline uint32_t SDIO_GetCardState() { return SDIO_CmdSendStatus(SdCard.RelCardAdd << 16U) ? (SDIO_GetResponse(SDIO_RESP1) >> 9U) & 0x0FU : SDIO_CARD_ERROR; } // No F1 board with SDIO + MSC using Maple, that I aware of... bool SDIO_IsReady() { return true; } uint32_t SDIO_GetCardSize() { return 0; } // ------------------------ // SD Commands and Responses // ------------------------ void SDIO_SendCommand(uint16_t command, uint32_t argument) { SDIO->ARG = argument; SDIO->CMD = (uint32_t)(SDIO_CMD_CPSMEN | command); } uint8_t SDIO_GetCommandResponse() { return (uint8_t)(SDIO->RESPCMD); } uint32_t SDIO_GetResponse(uint32_t response) { return SDIO->RESP[response]; } bool SDIO_CmdGoIdleState() { SDIO_SendCommand(CMD0_GO_IDLE_STATE, 0); return SDIO_GetCmdError(); } bool SDIO_CmdSendCID() { SDIO_SendCommand(CMD2_ALL_SEND_CID, 0); return SDIO_GetCmdResp2(); } bool SDIO_CmdSetRelAdd(uint32_t *rca) { SDIO_SendCommand(CMD3_SET_REL_ADDR, 0); return SDIO_GetCmdResp6(SDMMC_CMD_SET_REL_ADDR, rca); } bool SDIO_CmdSelDesel(uint32_t address) { SDIO_SendCommand(CMD7_SEL_DESEL_CARD, address); return SDIO_GetCmdResp1(SDMMC_CMD_SEL_DESEL_CARD); } bool SDIO_CmdOperCond() { SDIO_SendCommand(CMD8_HS_SEND_EXT_CSD, SDMMC_CHECK_PATTERN); return SDIO_GetCmdResp7(); } bool SDIO_CmdSendCSD(uint32_t argument) { SDIO_SendCommand(CMD9_SEND_CSD, argument); return SDIO_GetCmdResp2(); } bool SDIO_CmdSendStatus(uint32_t argument) { SDIO_SendCommand(CMD13_SEND_STATUS, argument); return SDIO_GetCmdResp1(SDMMC_CMD_SEND_STATUS); } bool SDIO_CmdReadSingleBlock(uint32_t address) { SDIO_SendCommand(CMD17_READ_SINGLE_BLOCK, address); return SDIO_GetCmdResp1(SDMMC_CMD_READ_SINGLE_BLOCK); } bool SDIO_CmdWriteSingleBlock(uint32_t address) { SDIO_SendCommand(CMD24_WRITE_SINGLE_BLOCK, address); return SDIO_GetCmdResp1(SDMMC_CMD_WRITE_SINGLE_BLOCK); } bool SDIO_CmdAppCommand(uint32_t rsa) { SDIO_SendCommand(CMD55_APP_CMD, rsa); return SDIO_GetCmdResp1(SDMMC_CMD_APP_CMD); } bool SDIO_CmdAppSetBusWidth(uint32_t rsa, uint32_t argument) { if (!SDIO_CmdAppCommand(rsa)) return false; SDIO_SendCommand(ACMD6_APP_SD_SET_BUSWIDTH, argument); return SDIO_GetCmdResp2(); } bool SDIO_CmdAppOperCommand(uint32_t sdType) { if (!SDIO_CmdAppCommand(0)) return false; SDIO_SendCommand(ACMD41_SD_APP_OP_COND , SDMMC_VOLTAGE_WINDOW_SD | sdType); return SDIO_GetCmdResp3(); } bool SDIO_CmdAppSetClearCardDetect(uint32_t rsa) { if (!SDIO_CmdAppCommand(rsa)) return false; SDIO_SendCommand(ACMD42_SD_APP_SET_CLR_CARD_DETECT, 0); return SDIO_GetCmdResp2(); } // Wait until given flags are unset or till timeout #define SDIO_WAIT(FLAGS) do{ \ uint32_t count = 1 + (SDIO_CMDTIMEOUT) * ((F_CPU) / 8U / 1000U); \ do { if (!--count) return false; } while (!SDIO_GET_FLAG(FLAGS)); \ }while(0) bool SDIO_GetCmdError() { SDIO_WAIT(SDIO_STA_CMDSENT); SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); return true; } bool SDIO_GetCmdResp1(uint8_t command) { SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) { SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT); return false; } if (SDIO_GetCommandResponse() != command) return false; SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); return (SDIO_GetResponse(SDIO_RESP1) & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO; } bool SDIO_GetCmdResp2() { SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) { SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT); return false; } SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); return true; } bool SDIO_GetCmdResp3() { SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); if (SDIO_GET_FLAG(SDIO_STA_CTIMEOUT)) { SDIO_CLEAR_FLAG(SDIO_STA_CTIMEOUT); return false; } SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); return true; } bool SDIO_GetCmdResp6(uint8_t command, uint32_t *rca) { SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) { SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT); return false; } if (SDIO_GetCommandResponse() != command) return false; SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); if (SDIO_GetResponse(SDIO_RESP1) & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) return false; *rca = SDIO_GetResponse(SDIO_RESP1) >> 16; return true; } bool SDIO_GetCmdResp7() { SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); if (SDIO_GET_FLAG(SDIO_STA_CTIMEOUT)) { SDIO_CLEAR_FLAG(SDIO_STA_CTIMEOUT); return false; } if (SDIO_GET_FLAG(SDIO_STA_CMDREND)) { SDIO_CLEAR_FLAG(SDIO_STA_CMDREND); } return true; } #endif // STM32_HIGH_DENSITY || STM32_XL_DENSITY #endif // ARDUINO_ARCH_STM32F1
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/sdio.cpp
C++
agpl-3.0
11,827
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../inc/MarlinConfig.h" // Allow pins/pins.h to override SDIO clock / retries #include <libmaple/sdio.h> #include <libmaple/dma.h> // ------------------------ // Defines // ------------------------ #define SDMMC_CMD_GO_IDLE_STATE ((uint8_t)0) /* Resets the SD memory card. */ #define SDMMC_CMD_ALL_SEND_CID ((uint8_t)2) /* Asks any card connected to the host to send the CID numbers on the CMD line. */ #define SDMMC_CMD_SET_REL_ADDR ((uint8_t)3) /* Asks the card to publish a new relative address (RCA). */ #define SDMMC_CMD_SEL_DESEL_CARD ((uint8_t)7) /* Selects the card by its own relative address and gets deselected by any other address */ #define SDMMC_CMD_HS_SEND_EXT_CSD ((uint8_t)8) /* Sends SD Memory Card interface condition, which includes host supply voltage information and asks the card whether card supports voltage. */ #define SDMMC_CMD_SEND_CSD ((uint8_t)9) /* Addressed card sends its card specific data (CSD) on the CMD line. */ #define SDMMC_CMD_SEND_STATUS ((uint8_t)13) /*!< Addressed card sends its status register. */ #define SDMMC_CMD_READ_SINGLE_BLOCK ((uint8_t)17) /* Reads single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of fixed 512 bytes in case of SDHC and SDXC. */ #define SDMMC_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24) /* Writes single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of fixed 512 bytes in case of SDHC and SDXC. */ #define SDMMC_CMD_APP_CMD ((uint8_t)55) /* Indicates to the card that the next command is an application specific command rather than a standard command. */ #define SDMMC_ACMD_APP_SD_SET_BUSWIDTH ((uint8_t)6) /* (ACMD6) Defines the data bus width to be used for data transfer. The allowed data bus widths are given in SCR register. */ #define SDMMC_ACMD_SD_APP_OP_COND ((uint8_t)41) /* (ACMD41) Sends host capacity support information (HCS) and asks the accessed card to send its operating condition register (OCR) content in the response on the CMD line. */ #define SDMMC_ACMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /* (ACMD42) Connect/Disconnect the 50 KOhm pull-up resistor on CD/DAT3 (pin 1) of the card */ #define CMD0_GO_IDLE_STATE (uint16_t)(SDMMC_CMD_GO_IDLE_STATE | SDIO_CMD_WAIT_NO_RESP) #define CMD2_ALL_SEND_CID (uint16_t)(SDMMC_CMD_ALL_SEND_CID | SDIO_CMD_WAIT_LONG_RESP) #define CMD3_SET_REL_ADDR (uint16_t)(SDMMC_CMD_SET_REL_ADDR | SDIO_CMD_WAIT_SHORT_RESP) #define CMD7_SEL_DESEL_CARD (uint16_t)(SDMMC_CMD_SEL_DESEL_CARD | SDIO_CMD_WAIT_SHORT_RESP) #define CMD8_HS_SEND_EXT_CSD (uint16_t)(SDMMC_CMD_HS_SEND_EXT_CSD | SDIO_CMD_WAIT_SHORT_RESP) #define CMD9_SEND_CSD (uint16_t)(SDMMC_CMD_SEND_CSD | SDIO_CMD_WAIT_LONG_RESP) #define CMD13_SEND_STATUS (uint16_t)(SDMMC_CMD_SEND_STATUS | SDIO_CMD_WAIT_SHORT_RESP) #define CMD17_READ_SINGLE_BLOCK (uint16_t)(SDMMC_CMD_READ_SINGLE_BLOCK | SDIO_CMD_WAIT_SHORT_RESP) #define CMD24_WRITE_SINGLE_BLOCK (uint16_t)(SDMMC_CMD_WRITE_SINGLE_BLOCK | SDIO_CMD_WAIT_SHORT_RESP) #define CMD55_APP_CMD (uint16_t)(SDMMC_CMD_APP_CMD | SDIO_CMD_WAIT_SHORT_RESP) #define ACMD6_APP_SD_SET_BUSWIDTH (uint16_t)(SDMMC_ACMD_APP_SD_SET_BUSWIDTH | SDIO_CMD_WAIT_SHORT_RESP) #define ACMD41_SD_APP_OP_COND (uint16_t)(SDMMC_ACMD_SD_APP_OP_COND | SDIO_CMD_WAIT_SHORT_RESP) #define ACMD42_SD_APP_SET_CLR_CARD_DETECT (uint16_t)(SDMMC_ACMD_SD_APP_SET_CLR_CARD_DETECT | SDIO_CMD_WAIT_SHORT_RESP) #define SDMMC_ALLZERO 0x00000000U #define SDMMC_OCR_ERRORBITS 0xFDFFE008U #define SDMMC_R6_GENERAL_UNKNOWN_ERROR 0x00002000U #define SDMMC_R6_ILLEGAL_CMD 0x00004000U #define SDMMC_R6_COM_CRC_FAILED 0x00008000U #define SDMMC_VOLTAGE_WINDOW_SD 0x80100000U #define SDMMC_HIGH_CAPACITY 0x40000000U #define SDMMC_STD_CAPACITY 0x00000000U #define SDMMC_CHECK_PATTERN 0x000001AAU #define SDIO_TRANSFER_MODE_BLOCK 0x00000000U #define SDIO_DPSM_ENABLE 0x00000001U #define SDIO_TRANSFER_DIR_TO_CARD 0x00000000U #define SDIO_DATABLOCK_SIZE_512B 0x00000090U #define SDIO_TRANSFER_DIR_TO_SDIO 0x00000100U #define SDIO_DMA_ENABLE 0x00001000U #define CARD_V1_X 0x00000000U #define CARD_V2_X 0x00000001U #define CARD_SDSC 0x00000000U #define CARD_SDHC_SDXC 0x00000001U #define SDIO_RESP1 0 #define SDIO_RESP2 1 #define SDIO_RESP3 2 #define SDIO_RESP4 3 #define SDIO_GET_FLAG(__FLAG__) !!((SDIO->STA) & (__FLAG__)) #define SDIO_CLEAR_FLAG(__FLAG__) (SDIO->ICR = (__FLAG__)) #define SDMMC_MAX_VOLT_TRIAL 0x00000FFFU #define SDIO_CARD_TRANSFER 0x00000004U /* Card is in transfer state */ #define SDIO_CARD_ERROR 0x000000FFU /* Card response Error */ #define SDIO_CMDTIMEOUT 200U /* Command send and response timeout */ #define SDIO_DATA_TIMEOUT 100U /* Read data transfer timeout */ #define SDIO_WRITE_TIMEOUT 200U /* Write data transfer timeout */ #ifndef SDIO_CLOCK #define SDIO_CLOCK 18000000 /* 18 MHz */ #endif #ifndef SDIO_READ_RETRIES #define SDIO_READ_RETRIES 3 #endif // ------------------------ // Types // ------------------------ typedef struct { uint32_t CardType; // Card Type uint32_t CardVersion; // Card version uint32_t Class; // Class of the card class uint32_t RelCardAdd; // Relative Card Address uint32_t BlockNbr; // Card Capacity in blocks uint32_t BlockSize; // One block size in bytes uint32_t LogBlockNbr; // Card logical Capacity in blocks uint32_t LogBlockSize; // Logical block size in bytes } SDIO_CardInfoTypeDef; // ------------------------ // Public functions // ------------------------ inline uint32_t SDIO_GetCardState(); bool SDIO_CmdGoIdleState(); bool SDIO_CmdSendCID(); bool SDIO_CmdSetRelAdd(uint32_t *rca); bool SDIO_CmdSelDesel(uint32_t address); bool SDIO_CmdOperCond(); bool SDIO_CmdSendCSD(uint32_t argument); bool SDIO_CmdSendStatus(uint32_t argument); bool SDIO_CmdReadSingleBlock(uint32_t address); bool SDIO_CmdWriteSingleBlock(uint32_t address); bool SDIO_CmdAppCommand(uint32_t rsa); bool SDIO_CmdAppSetBusWidth(uint32_t rsa, uint32_t argument); bool SDIO_CmdAppOperCommand(uint32_t sdType); bool SDIO_CmdAppSetClearCardDetect(uint32_t rsa); void SDIO_SendCommand(uint16_t command, uint32_t argument); uint8_t SDIO_GetCommandResponse(); uint32_t SDIO_GetResponse(uint32_t response); bool SDIO_GetCmdError(); bool SDIO_GetCmdResp1(uint8_t command); bool SDIO_GetCmdResp2(); bool SDIO_GetCmdResp3(); bool SDIO_GetCmdResp6(uint8_t command, uint32_t *rca); bool SDIO_GetCmdResp7();
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/sdio.h
C
agpl-3.0
8,473
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) */ /** * STM32F1 Default SPI Pins * * SS SCK MISO MOSI * +-----------------------------+ * SPI1 | PA4 PA5 PA6 PA7 | * SPI2 | PB12 PB13 PB14 PB15 | * SPI3 | PA15 PB3 PB4 PB5 | * +-----------------------------+ * Any pin can be used for Chip Select (SD_SS_PIN) * SPI1 is enabled by default */ #ifndef SD_SCK_PIN #define SD_SCK_PIN PA5 #endif #ifndef SD_MISO_PIN #define SD_MISO_PIN PA6 #endif #ifndef SD_MOSI_PIN #define SD_MOSI_PIN PA7 #endif #ifndef SD_SS_PIN #define SD_SS_PIN PA4 #endif #undef SDSS #define SDSS SD_SS_PIN #ifndef SPI_DEVICE #define SPI_DEVICE 1 #endif
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/spi_pins.h
C
agpl-3.0
1,615
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../../inc/MarlinConfig.h" #if HAS_FSMC_TFT #include "tft_fsmc.h" #include <libmaple/fsmc.h> #include <libmaple/gpio.h> #include <libmaple/dma.h> LCD_CONTROLLER_TypeDef *TFT_FSMC::LCD; /** * FSMC LCD IO */ #define __ASM __asm #define __STATIC_INLINE static inline __attribute__((always_inline)) __STATIC_INLINE void __DSB() { __ASM volatile ("dsb 0xF":::"memory"); } #define FSMC_CS_NE1 PD7 #if ENABLED(STM32_XL_DENSITY) #define FSMC_CS_NE2 PG9 #define FSMC_CS_NE3 PG10 #define FSMC_CS_NE4 PG12 #define FSMC_RS_A0 PF0 #define FSMC_RS_A1 PF1 #define FSMC_RS_A2 PF2 #define FSMC_RS_A3 PF3 #define FSMC_RS_A4 PF4 #define FSMC_RS_A5 PF5 #define FSMC_RS_A6 PF12 #define FSMC_RS_A7 PF13 #define FSMC_RS_A8 PF14 #define FSMC_RS_A9 PF15 #define FSMC_RS_A10 PG0 #define FSMC_RS_A11 PG1 #define FSMC_RS_A12 PG2 #define FSMC_RS_A13 PG3 #define FSMC_RS_A14 PG4 #define FSMC_RS_A15 PG5 #endif #define FSMC_RS_A16 PD11 #define FSMC_RS_A17 PD12 #define FSMC_RS_A18 PD13 #define FSMC_RS_A19 PE3 #define FSMC_RS_A20 PE4 #define FSMC_RS_A21 PE5 #define FSMC_RS_A22 PE6 #define FSMC_RS_A23 PE2 #if ENABLED(STM32_XL_DENSITY) #define FSMC_RS_A24 PG13 #define FSMC_RS_A25 PG14 #endif /* Timing configuration */ #define FSMC_ADDRESS_SETUP_TIME 15 // AddressSetupTime #define FSMC_DATA_SETUP_TIME 15 // DataSetupTime static uint8_t fsmcInit = 0; void TFT_FSMC::init() { uint8_t cs = FSMC_CS_PIN, rs = FSMC_RS_PIN; uint32_t controllerAddress; #if ENABLED(LCD_USE_DMA_FSMC) dma_init(FSMC_DMA_DEV); dma_disable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); dma_set_priority(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, DMA_PRIORITY_MEDIUM); #endif struct fsmc_nor_psram_reg_map* fsmcPsramRegion; if (fsmcInit) return; fsmcInit = 1; switch (cs) { case FSMC_CS_NE1: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION1; fsmcPsramRegion = FSMC_NOR_PSRAM1_BASE; break; #if ENABLED(STM32_XL_DENSITY) case FSMC_CS_NE2: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION2; fsmcPsramRegion = FSMC_NOR_PSRAM2_BASE; break; case FSMC_CS_NE3: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION3; fsmcPsramRegion = FSMC_NOR_PSRAM3_BASE; break; case FSMC_CS_NE4: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION4; fsmcPsramRegion = FSMC_NOR_PSRAM4_BASE; break; #endif default: return; } #define _ORADDR(N) controllerAddress |= (_BV32(N) - 2) switch (rs) { #if ENABLED(STM32_XL_DENSITY) case FSMC_RS_A0: _ORADDR( 1); break; case FSMC_RS_A1: _ORADDR( 2); break; case FSMC_RS_A2: _ORADDR( 3); break; case FSMC_RS_A3: _ORADDR( 4); break; case FSMC_RS_A4: _ORADDR( 5); break; case FSMC_RS_A5: _ORADDR( 6); break; case FSMC_RS_A6: _ORADDR( 7); break; case FSMC_RS_A7: _ORADDR( 8); break; case FSMC_RS_A8: _ORADDR( 9); break; case FSMC_RS_A9: _ORADDR(10); break; case FSMC_RS_A10: _ORADDR(11); break; case FSMC_RS_A11: _ORADDR(12); break; case FSMC_RS_A12: _ORADDR(13); break; case FSMC_RS_A13: _ORADDR(14); break; case FSMC_RS_A14: _ORADDR(15); break; case FSMC_RS_A15: _ORADDR(16); break; #endif case FSMC_RS_A16: _ORADDR(17); break; case FSMC_RS_A17: _ORADDR(18); break; case FSMC_RS_A18: _ORADDR(19); break; case FSMC_RS_A19: _ORADDR(20); break; case FSMC_RS_A20: _ORADDR(21); break; case FSMC_RS_A21: _ORADDR(22); break; case FSMC_RS_A22: _ORADDR(23); break; case FSMC_RS_A23: _ORADDR(24); break; #if ENABLED(STM32_XL_DENSITY) case FSMC_RS_A24: _ORADDR(25); break; case FSMC_RS_A25: _ORADDR(26); break; #endif default: return; } rcc_clk_enable(RCC_FSMC); gpio_set_mode(GPIOD, 14, GPIO_AF_OUTPUT_PP); // FSMC_D00 gpio_set_mode(GPIOD, 15, GPIO_AF_OUTPUT_PP); // FSMC_D01 gpio_set_mode(GPIOD, 0, GPIO_AF_OUTPUT_PP); // FSMC_D02 gpio_set_mode(GPIOD, 1, GPIO_AF_OUTPUT_PP); // FSMC_D03 gpio_set_mode(GPIOE, 7, GPIO_AF_OUTPUT_PP); // FSMC_D04 gpio_set_mode(GPIOE, 8, GPIO_AF_OUTPUT_PP); // FSMC_D05 gpio_set_mode(GPIOE, 9, GPIO_AF_OUTPUT_PP); // FSMC_D06 gpio_set_mode(GPIOE, 10, GPIO_AF_OUTPUT_PP); // FSMC_D07 gpio_set_mode(GPIOE, 11, GPIO_AF_OUTPUT_PP); // FSMC_D08 gpio_set_mode(GPIOE, 12, GPIO_AF_OUTPUT_PP); // FSMC_D09 gpio_set_mode(GPIOE, 13, GPIO_AF_OUTPUT_PP); // FSMC_D10 gpio_set_mode(GPIOE, 14, GPIO_AF_OUTPUT_PP); // FSMC_D11 gpio_set_mode(GPIOE, 15, GPIO_AF_OUTPUT_PP); // FSMC_D12 gpio_set_mode(GPIOD, 8, GPIO_AF_OUTPUT_PP); // FSMC_D13 gpio_set_mode(GPIOD, 9, GPIO_AF_OUTPUT_PP); // FSMC_D14 gpio_set_mode(GPIOD, 10, GPIO_AF_OUTPUT_PP); // FSMC_D15 gpio_set_mode(GPIOD, 4, GPIO_AF_OUTPUT_PP); // FSMC_NOE gpio_set_mode(GPIOD, 5, GPIO_AF_OUTPUT_PP); // FSMC_NWE gpio_set_mode(PIN_MAP[cs].gpio_device, PIN_MAP[cs].gpio_bit, GPIO_AF_OUTPUT_PP); //FSMC_CS_NEx gpio_set_mode(PIN_MAP[rs].gpio_device, PIN_MAP[rs].gpio_bit, GPIO_AF_OUTPUT_PP); //FSMC_RS_Ax fsmcPsramRegion->BCR = FSMC_BCR_WREN | FSMC_BCR_MTYP_SRAM | FSMC_BCR_MWID_16BITS | FSMC_BCR_MBKEN; fsmcPsramRegion->BTR = (FSMC_DATA_SETUP_TIME << 8) | FSMC_ADDRESS_SETUP_TIME; afio_remap(AFIO_REMAP_FSMC_NADV); LCD = (LCD_CONTROLLER_TypeDef*)controllerAddress; } void TFT_FSMC::transmit(uint16_t data) { LCD->RAM = data; __DSB(); } void TFT_FSMC::writeReg(const uint16_t inReg) { LCD->REG = inReg; __DSB(); } uint32_t TFT_FSMC::getID() { uint32_t id; writeReg(0x0000); id = LCD->RAM; if (id == 0) id = readID(LCD_READ_ID); if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) id = readID(LCD_READ_ID4); if ((id & 0xFF00) == 0 && (id & 0xFF) != 0) id = readID(LCD_READ_ID4); return id; } uint32_t TFT_FSMC::readID(const uint16_t inReg) { uint32_t id; writeReg(inReg); id = LCD->RAM; // dummy read id = inReg << 24; id |= (LCD->RAM & 0x00FF) << 16; id |= (LCD->RAM & 0x00FF) << 8; id |= LCD->RAM & 0x00FF; return id; } bool TFT_FSMC::isBusy() { #define __IS_DMA_CONFIGURED(__DMAx__, __CHx__) (dma_channel_regs(__DMAx__, __CHx__)->CPAR != 0) if (!__IS_DMA_CONFIGURED(FSMC_DMA_DEV, FSMC_DMA_CHANNEL)) return false; // Check if DMA transfer error or transfer complete flags are set if ((dma_get_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL) & (DMA_ISR_TCIF | DMA_ISR_TEIF)) == 0) return true; __DSB(); abort(); return false; } void TFT_FSMC::abort() { dma_channel_reg_map *channel_regs = dma_channel_regs(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); dma_disable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); // Abort DMA transfer if any // Deconfigure DMA channel_regs->CCR = 0U; channel_regs->CNDTR = 0U; channel_regs->CMAR = 0U; channel_regs->CPAR = 0U; } void TFT_FSMC::transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count) { // TODO: HAL STM32 uses DMA2_Channel1 for FSMC on STM32F1 dma_setup_transfer(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, data, DMA_SIZE_16BITS, &LCD->RAM, DMA_SIZE_16BITS, DMA_MEM_2_MEM | memoryIncrease); dma_set_num_transfers(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, count); dma_clear_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); dma_enable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); TERN_(TFT_SHARED_IO, while (isBusy())); } void TFT_FSMC::transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count) { #if defined(FSMC_DMA_DEV) && defined(FSMC_DMA_CHANNEL) dma_setup_transfer(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, data, DMA_SIZE_16BITS, &LCD->RAM, DMA_SIZE_16BITS, DMA_MEM_2_MEM | memoryIncrease); dma_set_num_transfers(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, count); dma_clear_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); dma_enable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); while ((dma_get_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL) & (DMA_CCR_TEIE | DMA_CCR_TCIE)) == 0) {} abort(); #endif } #endif // HAS_FSMC_TFT #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/tft/tft_fsmc.cpp
C++
agpl-3.0
8,764
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #ifndef LCD_READ_ID #define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341) #endif #ifndef LCD_READ_ID4 #define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341) #endif #include <libmaple/dma.h> #ifndef FSMC_DMA_DEV #define FSMC_DMA_DEV DMA2 #endif #ifndef FSMC_DMA_CHANNEL #define FSMC_DMA_CHANNEL DMA_CH5 #endif #define DATASIZE_8BIT DMA_SIZE_8BITS #define DATASIZE_16BIT DMA_SIZE_16BITS #define TFT_IO_DRIVER TFT_FSMC #define DMA_MAX_WORDS 0xFFFF #define DMA_PINC_ENABLE DMA_PINC_MODE #define DMA_PINC_DISABLE 0 typedef struct { __IO uint16_t REG; __IO uint16_t RAM; } LCD_CONTROLLER_TypeDef; class TFT_FSMC { private: static LCD_CONTROLLER_TypeDef *LCD; static uint32_t readID(const uint16_t inReg); static void transmit(uint16_t data); static void transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count); static void transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count); public: static void init(); static uint32_t getID(); static bool isBusy(); static void abort(); static void dataTransferBegin(uint16_t dataWidth=DATASIZE_16BIT) {}; static void dataTransferEnd() {}; static void writeData(uint16_t data) { transmit(data); } static void writeReg(const uint16_t inReg); static void writeSequence_DMA(uint16_t *data, uint16_t count) { transmitDMA(DMA_PINC_ENABLE, data, count); } static void writeMultiple_DMA(uint16_t color, uint16_t count) { static uint16_t data; data = color; transmitDMA(DMA_PINC_DISABLE, &data, count); } static void writeSequence(uint16_t *data, uint16_t count) { transmit(DMA_PINC_ENABLE, data, count); } static void writeMultiple(uint16_t color, uint32_t count) { while (count > 0) { transmit(DMA_PINC_DISABLE, &color, count > DMA_MAX_WORDS ? DMA_MAX_WORDS : count); count = count > DMA_MAX_WORDS ? count - DMA_MAX_WORDS : 0; } } };
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/tft/tft_fsmc.h
C++
agpl-3.0
2,869
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../../inc/MarlinConfig.h" #if HAS_SPI_TFT #include "tft_spi.h" SPIClass TFT_SPI::SPIx(TFT_SPI_DEVICE); void TFT_SPI::init() { #if PIN_EXISTS(TFT_RESET) OUT_WRITE(TFT_RESET_PIN, HIGH); delay(100); #endif #if PIN_EXISTS(TFT_BACKLIGHT) OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH); #endif OUT_WRITE(TFT_DC_PIN, HIGH); OUT_WRITE(TFT_CS_PIN, HIGH); /** * STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz * STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1 * so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2 */ #if TFT_SPI_DEVICE == 1 #define SPI_CLOCK_MAX SPI_CLOCK_DIV4 #else #define SPI_CLOCK_MAX SPI_CLOCK_DIV2 #endif uint8_t clock; uint8_t spiRate = SPI_FULL_SPEED; switch (spiRate) { case SPI_FULL_SPEED: clock = SPI_CLOCK_MAX ; break; case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4 ; break; case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8 ; break; case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break; case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break; case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break; default: clock = SPI_CLOCK_DIV2; // Default from the SPI library } SPIx.setModule(TFT_SPI_DEVICE); SPIx.setClockDivider(clock); SPIx.setBitOrder(MSBFIRST); SPIx.setDataMode(SPI_MODE0); } void TFT_SPI::dataTransferBegin(uint16_t dataSize) { SPIx.setDataSize(dataSize); SPIx.begin(); WRITE(TFT_CS_PIN, LOW); } #ifdef TFT_DEFAULT_DRIVER #include "../../../lcd/tft_io/tft_ids.h" #endif uint32_t TFT_SPI::getID() { uint32_t id; id = readID(LCD_READ_ID); if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) { id = readID(LCD_READ_ID4); #ifdef TFT_DEFAULT_DRIVER if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) id = TFT_DEFAULT_DRIVER; #endif } return id; } uint32_t TFT_SPI::readID(const uint16_t inReg) { uint32_t data = 0; #if PIN_EXISTS(TFT_MISO) SPIx.setClockDivider(SPI_CLOCK_DIV16); dataTransferBegin(DATASIZE_8BIT); writeReg(inReg); for (uint8_t i = 0; i < 4; ++i) { uint8_t d; SPIx.read(&d, 1); data = (data << 8) | d; } dataTransferEnd(); SPIx.setClockDivider(SPI_CLOCK_MAX); #endif return data >> 7; } bool TFT_SPI::isBusy() { #define __IS_DMA_CONFIGURED(__DMAx__, __CHx__) (dma_channel_regs(__DMAx__, __CHx__)->CPAR != 0) if (!__IS_DMA_CONFIGURED(DMAx, DMA_CHx)) return false; if (dma_get_isr_bits(DMAx, DMA_CHx) & DMA_ISR_TEIF) { // You should not be here - DMA transfer error flag is set // Abort DMA transfer and release SPI } else { // Check if DMA transfer completed flag is set if (!(dma_get_isr_bits(DMAx, DMA_CHx) & DMA_ISR_TCIF)) return true; // Check if SPI TX butter is empty and SPI is idle if (!(SPIdev->regs->SR & SPI_SR_TXE) || (SPIdev->regs->SR & SPI_SR_BSY)) return true; } abort(); return false; } void TFT_SPI::abort() { dma_channel_reg_map *channel_regs = dma_channel_regs(DMAx, DMA_CHx); dma_disable(DMAx, DMA_CHx); // Abort DMA transfer if any spi_tx_dma_disable(SPIdev); // Deconfigure DMA channel_regs->CCR = 0U; channel_regs->CNDTR = 0U; channel_regs->CMAR = 0U; channel_regs->CPAR = 0U; dataTransferEnd(); } void TFT_SPI::transmit(uint16_t data) { SPIx.send(data); } void TFT_SPI::transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count) { dataTransferBegin(); SPIx.dmaSendAsync(data, count, memoryIncrease == DMA_MINC_ENABLE); TERN_(TFT_SHARED_IO, while (isBusy())); } void TFT_SPI::transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count) { WRITE(TFT_DC_PIN, HIGH); dataTransferBegin(); SPIx.dmaSend(data, count, memoryIncrease == DMA_MINC_ENABLE); dataTransferEnd(); } #endif // HAS_SPI_TFT #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/tft/tft_spi.cpp
C++
agpl-3.0
4,734
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../../inc/MarlinConfig.h" #include <SPI.h> #define IS_SPI(N) (BOARD_NR_SPI >= N && (TFT_SCK_PIN == BOARD_SPI##N##_SCK_PIN) && (TFT_MOSI_PIN == BOARD_SPI##N##_MOSI_PIN) && (TFT_MISO_PIN == BOARD_SPI##N##_MISO_PIN)) #if IS_SPI(1) #define TFT_SPI_DEVICE 1 #define SPIdev SPI1 #define DMAx DMA1 #define DMA_CHx DMA_CH3 #elif IS_SPI(2) #define TFT_SPI_DEVICE 2 #define SPIdev SPI2 #define DMAx DMA1 #define DMA_CHx DMA_CH5 #elif IS_SPI(3) #define TFT_SPI_DEVICE 3 #define SPIdev SPI3 #define DMAx DMA2 #define DMA_CHx DMA_CH2 #else #error "Invalid TFT SPI configuration." #endif #undef IS_SPI #ifndef LCD_READ_ID #define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341) #endif #ifndef LCD_READ_ID4 #define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341) #endif #define DATASIZE_8BIT DATA_SIZE_8BIT #define DATASIZE_16BIT DATA_SIZE_16BIT #define TFT_IO_DRIVER TFT_SPI #define DMA_MAX_WORDS 0xFFFF #define DMA_MINC_ENABLE DMA_MINC_MODE #define DMA_MINC_DISABLE 0 class TFT_SPI { private: static uint32_t readID(const uint16_t inReg); static void transmit(uint16_t data); static void transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count); static void transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count); public: static SPIClass SPIx; static void init(); static uint32_t getID(); static bool isBusy(); static void abort(); static void dataTransferBegin(uint16_t dataWidth=DATA_SIZE_16BIT); static void dataTransferEnd() { WRITE(TFT_CS_PIN, HIGH); SPIx.end(); }; static void dataTransferAbort(); static void writeData(uint16_t data) { transmit(data); } static void writeReg(const uint16_t inReg) { WRITE(TFT_DC_PIN, LOW); transmit(inReg); WRITE(TFT_DC_PIN, HIGH); } static void writeSequence_DMA(uint16_t *data, uint16_t count) { transmitDMA(DMA_MINC_ENABLE, data, count); } static void writeMultiple_DMA(uint16_t color, uint16_t count) { static uint16_t data; data = color; transmitDMA(DMA_MINC_DISABLE, &data, count); } static void writeSequence(uint16_t *data, uint16_t count) { transmit(DMA_MINC_ENABLE, data, count); } static void writeMultiple(uint16_t color, uint32_t count) { while (count > 0) { transmit(DMA_MINC_DISABLE, &color, count > DMA_MAX_WORDS ? DMA_MAX_WORDS : count); count = count > DMA_MAX_WORDS ? count - DMA_MAX_WORDS : 0; } } };
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/tft/tft_spi.h
C++
agpl-3.0
3,418
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __STM32F1__ #include "../../../inc/MarlinConfig.h" #if HAS_TFT_XPT2046 || HAS_RES_TOUCH_BUTTONS #include "xpt2046.h" #include <SPI.h> uint16_t delta(uint16_t a, uint16_t b) { return a > b ? a - b : b - a; } #if ENABLED(TOUCH_BUTTONS_HW_SPI) #include <SPI.h> SPIClass XPT2046::SPIx(TOUCH_BUTTONS_HW_SPI_DEVICE); static void touch_spi_init(uint8_t spiRate) { /** * STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz * STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1 * so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2 */ uint8_t clock; switch (spiRate) { case SPI_FULL_SPEED: clock = SPI_CLOCK_DIV4; break; case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4; break; case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8; break; case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break; case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break; case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break; default: clock = SPI_CLOCK_DIV2; // Default from the SPI library } XPT2046::SPIx.setModule(TOUCH_BUTTONS_HW_SPI_DEVICE); XPT2046::SPIx.setClockDivider(clock); XPT2046::SPIx.setBitOrder(MSBFIRST); XPT2046::SPIx.setDataMode(SPI_MODE0); } #endif // TOUCH_BUTTONS_HW_SPI void XPT2046::init() { SET_INPUT(TOUCH_MISO_PIN); SET_OUTPUT(TOUCH_MOSI_PIN); SET_OUTPUT(TOUCH_SCK_PIN); OUT_WRITE(TOUCH_CS_PIN, HIGH); #if PIN_EXISTS(TOUCH_INT) // Optional Pendrive interrupt pin SET_INPUT(TOUCH_INT_PIN); #endif TERN_(TOUCH_BUTTONS_HW_SPI, touch_spi_init(SPI_SPEED_6)); // Read once to enable pendrive status pin getRawData(XPT2046_X); } bool XPT2046::isTouched() { return isBusy() ? false : ( #if PIN_EXISTS(TOUCH_INT) READ(TOUCH_INT_PIN) != HIGH #else getRawData(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD #endif ); } bool XPT2046::getRawPoint(int16_t * const x, int16_t * const y) { if (isBusy() || !isTouched()) return false; *x = getRawData(XPT2046_X); *y = getRawData(XPT2046_Y); return isTouched(); } uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) { uint16_t data[3]; dataTransferBegin(); TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.begin()); for (uint16_t i = 0; i < 3 ; i++) { IO(coordinate); data[i] = (IO() << 4) | (IO() >> 4); } TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.end()); dataTransferEnd(); uint16_t delta01 = delta(data[0], data[1]), delta02 = delta(data[0], data[2]), delta12 = delta(data[1], data[2]); if (delta01 > delta02 || delta01 > delta12) data[delta02 > delta12 ? 0 : 1] = data[2]; return (data[0] + data[1]) >> 1; } uint16_t XPT2046::IO(uint16_t data) { return TERN(TOUCH_BUTTONS_HW_SPI, hardwareIO, softwareIO)(data); } #if ENABLED(TOUCH_BUTTONS_HW_SPI) uint16_t XPT2046::hardwareIO(uint16_t data) { uint16_t result = SPIx.transfer(data); return result; } #endif uint16_t XPT2046::softwareIO(uint16_t data) { uint16_t result = 0; for (uint8_t j = 0x80; j; j >>= 1) { WRITE(TOUCH_SCK_PIN, LOW); WRITE(TOUCH_MOSI_PIN, data & j ? HIGH : LOW); if (READ(TOUCH_MISO_PIN)) result |= j; WRITE(TOUCH_SCK_PIN, HIGH); } WRITE(TOUCH_SCK_PIN, LOW); return result; } #endif // HAS_TFT_XPT2046 || HAS_RES_TOUCH_BUTTONS #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/tft/xpt2046.cpp
C++
agpl-3.0
4,231
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../../inc/MarlinConfig.h" #if ENABLED(TOUCH_BUTTONS_HW_SPI) #include <SPI.h> #endif #ifndef TOUCH_MISO_PIN #define TOUCH_MISO_PIN SD_MISO_PIN #endif #ifndef TOUCH_MOSI_PIN #define TOUCH_MOSI_PIN SD_MOSI_PIN #endif #ifndef TOUCH_SCK_PIN #define TOUCH_SCK_PIN SD_SCK_PIN #endif #ifndef TOUCH_CS_PIN #define TOUCH_CS_PIN SD_SS_PIN #endif #ifndef TOUCH_INT_PIN #define TOUCH_INT_PIN -1 #endif #define XPT2046_DFR_MODE 0x00 #define XPT2046_SER_MODE 0x04 #define XPT2046_CONTROL 0x80 enum XPTCoordinate : uint8_t { XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Z1 = 0x30 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Z2 = 0x40 | XPT2046_CONTROL | XPT2046_DFR_MODE, }; #ifndef XPT2046_Z1_THRESHOLD #define XPT2046_Z1_THRESHOLD 10 #endif class XPT2046 { private: static bool isBusy() { return false; } static uint16_t getRawData(const XPTCoordinate coordinate); static bool isTouched(); static void dataTransferBegin() { WRITE(TOUCH_CS_PIN, LOW); }; static void dataTransferEnd() { WRITE(TOUCH_CS_PIN, HIGH); }; #if ENABLED(TOUCH_BUTTONS_HW_SPI) static uint16_t hardwareIO(uint16_t data); #endif static uint16_t softwareIO(uint16_t data); static uint16_t IO(uint16_t data = 0); public: #if ENABLED(TOUCH_BUTTONS_HW_SPI) static SPIClass SPIx; #endif static void init(); static bool getRawPoint(int16_t * const x, int16_t * const y); };
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/tft/xpt2046.h
C++
agpl-3.0
2,382
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) */ #ifdef __STM32F1__ #include "../../inc/MarlinConfig.h" // ------------------------ // Local defines // ------------------------ // ------------------------ // Public functions // ------------------------ /** * Timer_clock1: Prescaler 2 -> 36 MHz * Timer_clock2: Prescaler 8 -> 9 MHz * Timer_clock3: Prescaler 32 -> 2.25 MHz * Timer_clock4: Prescaler 128 -> 562.5 kHz */ /** * TODO: Calculate Timer prescale value, so we get the 32bit to adjust */ void HAL_timer_set_interrupt_priority(uint_fast8_t timer_num, uint_fast8_t priority) { nvic_irq_num irq_num; switch (timer_num) { case 1: irq_num = NVIC_TIMER1_CC; break; case 2: irq_num = NVIC_TIMER2; break; case 3: irq_num = NVIC_TIMER3; break; case 4: irq_num = NVIC_TIMER4; break; case 5: irq_num = NVIC_TIMER5; break; #ifdef STM32_HIGH_DENSITY // 6 & 7 are basic timers, avoid them case 8: irq_num = NVIC_TIMER8_CC; break; #endif default: /** * This should never happen. Add a Sanitycheck for timer number. * Should be a general timer since basic timers have no CC channels. */ return; } nvic_irq_set_priority(irq_num, priority); } void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { /** * Give the Stepper ISR a higher priority (lower number) * so it automatically preempts the Temperature ISR. */ switch (timer_num) { case MF_TIMER_STEP: timer_pause(STEP_TIMER_DEV); timer_set_mode(STEP_TIMER_DEV, STEP_TIMER_CHAN, TIMER_OUTPUT_COMPARE); // counter timer_set_count(STEP_TIMER_DEV, 0); timer_set_prescaler(STEP_TIMER_DEV, (uint16_t)(STEPPER_TIMER_PRESCALE - 1)); timer_set_reload(STEP_TIMER_DEV, 0xFFFF); timer_oc_set_mode(STEP_TIMER_DEV, STEP_TIMER_CHAN, TIMER_OC_MODE_FROZEN, TIMER_OC_NO_PRELOAD); // no output pin change timer_set_compare(STEP_TIMER_DEV, STEP_TIMER_CHAN, _MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (STEPPER_TIMER_RATE) / frequency)); timer_no_ARR_preload_ARPE(STEP_TIMER_DEV); // Need to be sure no preload on ARR register timer_attach_interrupt(STEP_TIMER_DEV, STEP_TIMER_CHAN, stepTC_Handler); HAL_timer_set_interrupt_priority(MF_TIMER_STEP, STEP_TIMER_IRQ_PRIO); timer_generate_update(STEP_TIMER_DEV); timer_resume(STEP_TIMER_DEV); break; case MF_TIMER_TEMP: timer_pause(TEMP_TIMER_DEV); timer_set_mode(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, TIMER_OUTPUT_COMPARE); timer_set_count(TEMP_TIMER_DEV, 0); timer_set_prescaler(TEMP_TIMER_DEV, (uint16_t)(TEMP_TIMER_PRESCALE - 1)); timer_set_reload(TEMP_TIMER_DEV, 0xFFFF); timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, _MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (F_CPU) / (TEMP_TIMER_PRESCALE) / frequency)); timer_attach_interrupt(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, tempTC_Handler); HAL_timer_set_interrupt_priority(MF_TIMER_TEMP, TEMP_TIMER_IRQ_PRIO); timer_generate_update(TEMP_TIMER_DEV); timer_resume(TEMP_TIMER_DEV); break; } } void HAL_timer_enable_interrupt(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: ENABLE_STEPPER_DRIVER_INTERRUPT(); break; case MF_TIMER_TEMP: ENABLE_TEMPERATURE_INTERRUPT(); break; } } void HAL_timer_disable_interrupt(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: DISABLE_STEPPER_DRIVER_INTERRUPT(); break; case MF_TIMER_TEMP: DISABLE_TEMPERATURE_INTERRUPT(); break; } } static inline bool HAL_timer_irq_enabled(const timer_dev * const dev, const uint8_t interrupt) { return bool(*bb_perip(&(dev->regs).gen->DIER, interrupt)); } bool HAL_timer_interrupt_enabled(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return HAL_timer_irq_enabled(STEP_TIMER_DEV, STEP_TIMER_CHAN); case MF_TIMER_TEMP: return HAL_timer_irq_enabled(TEMP_TIMER_DEV, TEMP_TIMER_CHAN); } return false; } timer_dev* HAL_get_timer_dev(int number) { switch (number) { #if STM32_HAVE_TIMER(1) case 1: return &timer1; #endif #if STM32_HAVE_TIMER(2) case 2: return &timer2; #endif #if STM32_HAVE_TIMER(3) case 3: return &timer3; #endif #if STM32_HAVE_TIMER(4) case 4: return &timer4; #endif #if STM32_HAVE_TIMER(5) case 5: return &timer5; #endif #if STM32_HAVE_TIMER(6) case 6: return &timer6; #endif #if STM32_HAVE_TIMER(7) case 7: return &timer7; #endif #if STM32_HAVE_TIMER(8) case 8: return &timer8; #endif #if STM32_HAVE_TIMER(9) case 9: return &timer9; #endif #if STM32_HAVE_TIMER(10) case 10: return &timer10; #endif #if STM32_HAVE_TIMER(11) case 11: return &timer11; #endif #if STM32_HAVE_TIMER(12) case 12: return &timer12; #endif #if STM32_HAVE_TIMER(13) case 13: return &timer13; #endif #if STM32_HAVE_TIMER(14) case 14: return &timer14; #endif default: return nullptr; } } #endif // __STM32F1__
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/timers.cpp
C++
agpl-3.0
5,972
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL for stm32duino.com based on Libmaple and compatible (STM32F1) */ #include "../../inc/MarlinConfig.h" #include "HAL.h" #include <libmaple/timer.h> // ------------------------ // Defines // ------------------------ /** * TODO: Check and confirm what timer we will use for each Temps and stepper driving. * We should probable drive temps with PWM. */ typedef uint16_t hal_timer_t; #define HAL_TIMER_TYPE_MAX 0xFFFF #define HAL_TIMER_RATE uint32_t(F_CPU) // frequency of timers peripherals #ifndef STEP_TIMER_CHAN #define STEP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts #endif #ifndef TEMP_TIMER_CHAN #define TEMP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts #endif /** * Note: Timers may be used by platforms and libraries * * FAN PWMs: * With FAN_SOFT_PWM disabled the Temperature class uses * FANx_PIN timers to generate FAN PWM signals. * * Speaker: * When SPEAKER is enabled, one timer is allocated by maple/tone.cpp. * - If BEEPER_PIN has a timer channel (and USE_PIN_TIMER is * defined in tone.cpp) it uses the pin's own timer. * - Otherwise it uses Timer 8 on boards with STM32_HIGH_DENSITY * or Timer 4 on other boards. */ #ifndef MF_TIMER_STEP #if defined(MCU_STM32F103CB) || defined(MCU_STM32F103C8) #define MF_TIMER_STEP 4 // For C8/CB boards, use timer 4 #else #define MF_TIMER_STEP 5 // for other boards, five is fine. #endif #endif #ifndef MF_TIMER_PULSE #define MF_TIMER_PULSE MF_TIMER_STEP #endif #ifndef MF_TIMER_TEMP #define MF_TIMER_TEMP 2 // Timer Index for Temperature //#define MF_TIMER_TEMP 4 // 2->4, Timer 2 for Stepper Current PWM #endif #if MB(BTT_SKR_MINI_E3_V1_0, BTT_SKR_E3_DIP, BTT_SKR_MINI_E3_V1_2, MKS_ROBIN_LITE, MKS_ROBIN_E3D, MKS_ROBIN_E3, VOXELAB_AQUILA) // SKR Mini E3 boards use PA8 as FAN0_PIN, so TIMER 1 is used for Fan PWM. #ifdef STM32_HIGH_DENSITY #define MF_TIMER_SERVO0 8 // tone.cpp uses Timer 4 #else #define MF_TIMER_SERVO0 3 // tone.cpp uses Timer 8 #endif #else #define MF_TIMER_SERVO0 1 // SERVO0 or BLTOUCH #endif #define STEP_TIMER_IRQ_PRIO 2 #define TEMP_TIMER_IRQ_PRIO 3 #define SERVO0_TIMER_IRQ_PRIO 1 #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency #define STEPPER_TIMER_PRESCALE 18 // prescaler for setting stepper timer, 4Mhz #define STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US timer_dev* HAL_get_timer_dev(int number); #define TIMER_DEV(num) HAL_get_timer_dev(num) #define STEP_TIMER_DEV TIMER_DEV(MF_TIMER_STEP) #define TEMP_TIMER_DEV TIMER_DEV(MF_TIMER_TEMP) #define ENABLE_STEPPER_DRIVER_INTERRUPT() timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN) #define DISABLE_STEPPER_DRIVER_INTERRUPT() timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN) #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP) #define ENABLE_TEMPERATURE_INTERRUPT() timer_enable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN) #define DISABLE_TEMPERATURE_INTERRUPT() timer_disable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN) #define HAL_timer_get_count(timer_num) timer_get_count(TIMER_DEV(timer_num)) // TODO change this #ifndef HAL_TEMP_TIMER_ISR #define HAL_TEMP_TIMER_ISR() extern "C" void tempTC_Handler() #endif #ifndef HAL_STEP_TIMER_ISR #define HAL_STEP_TIMER_ISR() extern "C" void stepTC_Handler() #endif extern "C" { void tempTC_Handler(); void stepTC_Handler(); } // ------------------------ // Public Variables // ------------------------ //static HardwareTimer StepperTimer(MF_TIMER_STEP); //static HardwareTimer TempTimer(MF_TIMER_TEMP); // ------------------------ // Public functions // ------------------------ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); void HAL_timer_enable_interrupt(const uint8_t timer_num); void HAL_timer_disable_interrupt(const uint8_t timer_num); bool HAL_timer_interrupt_enabled(const uint8_t timer_num); /** * NOTE: By default libmaple sets ARPE = 1, which means the Auto reload register is preloaded (will only update with an update event) * Thus we have to pause the timer, update the value, refresh, resume the timer. * That seems like a big waste of time and may be better to change the timer config to ARPE = 0, so ARR can be updated any time. * We are using a Channel in each timer in Capture/Compare mode. We could also instead use the Time Update Event Interrupt, but need to disable ARPE * so we can change the ARR value on the fly (without calling refresh), and not get an interrupt right there because we caused an UEV. * This mode pretty much makes 2 timers unusable for PWM since they have their counts updated all the time on ISRs. * The way Marlin manages timer interrupts doesn't make for an efficient usage in STM32F1 * Todo: Look at that possibility later. */ FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) { switch (timer_num) { case MF_TIMER_STEP: // NOTE: WE have set ARPE = 0, which means the Auto reload register is not preloaded // and there is no need to use any compare, as in the timer mode used, setting ARR to the compare value // will result in exactly the same effect, ie triggering an interrupt, and on top, set counter to 0 timer_set_reload(STEP_TIMER_DEV, compare); // We reload direct ARR as needed during counting up break; case MF_TIMER_TEMP: timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, compare); break; } } FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: // No counter to clear timer_generate_update(STEP_TIMER_DEV); return; case MF_TIMER_TEMP: timer_set_count(TEMP_TIMER_DEV, 0); timer_generate_update(TEMP_TIMER_DEV); return; } } #define HAL_timer_isr_epilogue(T) NOOP // No command is available in framework to turn off ARPE bit, which is turned on by default in libmaple. // Needed here to reset ARPE=0 for stepper timer FORCE_INLINE static void timer_no_ARR_preload_ARPE(timer_dev *dev) { bb_peri_set_bit(&(dev->regs).gen->CR1, TIMER_CR1_ARPE_BIT, 0); } void HAL_timer_set_interrupt_priority(uint_fast8_t timer_num, uint_fast8_t priority); #define TIMER_OC_NO_PRELOAD 0 // Need to disable preload also on compare registers.
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/timers.h
C
agpl-3.0
7,723
/** * Marlin 3D Printer Firmware * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * STM32F1 (Maple) LCD-specific defines */ uint8_t u8g_com_HAL_STM32F1_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); uint8_t u8g_com_stm32duino_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); // See U8glib-HAL #define U8G_COM_HAL_SW_SPI_FN u8g_com_HAL_STM32F1_sw_spi_fn #define U8G_COM_HAL_HW_SPI_FN u8g_com_stm32duino_hw_spi_fn // See U8glib-HAL #define U8G_COM_ST7920_HAL_SW_SPI u8g_com_std_sw_spi_fn // See U8glib-HAL #define U8G_COM_ST7920_HAL_HW_SPI u8g_com_stm32duino_hw_spi_fn // See U8glib-HAL
2301_81045437/Marlin
Marlin/src/HAL/STM32F1/u8g/LCD_defines.h
C
agpl-3.0
1,446
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL for Teensy 3.2 (MK20DX256) */ #ifdef __MK20DX256__ #include "HAL.h" #include "../shared/Delay.h" #include <Wire.h> // ------------------------ // Serial ports // ------------------------ #define _IMPLEMENT_SERIAL(X) DefaultSerial##X MSerial##X(false, Serial##X) #define IMPLEMENT_SERIAL(X) _IMPLEMENT_SERIAL(X) #if WITHIN(SERIAL_PORT, 0, 3) IMPLEMENT_SERIAL(SERIAL_PORT); #else #error "SERIAL_PORT must be from 0 to 3." #endif USBSerialType USBSerial(false, SerialUSB); // ------------------------ // MarlinHAL Class // ------------------------ void MarlinHAL::reboot() { _reboot_Teensyduino_(); } uint8_t MarlinHAL::get_reset_source() { switch (RCM_SRS0) { case 128: return RST_POWER_ON; break; case 64: return RST_EXTERNAL; break; case 32: return RST_WATCHDOG; break; // case 8: return RST_LOSS_OF_LOCK; break; // case 4: return RST_LOSS_OF_CLOCK; break; // case 2: return RST_LOW_VOLTAGE; break; } return 0; } // ------------------------ // Watchdog Timer // ------------------------ #if ENABLED(USE_WATCHDOG) #define WDT_TIMEOUT_MS TERN(WATCHDOG_DURATION_8S, 8000, 4000) // 4 or 8 second timeout void MarlinHAL::watchdog_init() { WDOG_TOVALH = 0; WDOG_TOVALL = WDT_TIMEOUT_MS; WDOG_STCTRLH = WDOG_STCTRLH_WDOGEN; } void MarlinHAL::watchdog_refresh() { // Watchdog refresh sequence WDOG_REFRESH = 0xA602; WDOG_REFRESH = 0xB480; } #endif // ------------------------ // ADC // ------------------------ void MarlinHAL::adc_init() { analog_init(); while (ADC0_SC3 & ADC_SC3_CAL) {}; // Wait for calibration to finish NVIC_ENABLE_IRQ(IRQ_FTM1); } void MarlinHAL::adc_start(const pin_t pin) { static const uint8_t pin2sc1a[] = { 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 0, 19, 3, 31, // 0-13, we treat them as A0-A13 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 (A0-A9) 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, // 24-33 0+64, 19+64, 3+64, 31+64, // 34-37 (A10-A13) 26, 22, 23, 27, 29, 30 // 38-43: temp. sensor, VREF_OUT, A14, bandgap, VREFH, VREFL. A14 isn't connected to anything in Teensy 3.0. }; ADC0_SC1A = pin2sc1a[pin]; } uint16_t MarlinHAL::adc_value() { return ADC0_RA; } // ------------------------ // Free Memory Accessor // ------------------------ extern "C" { extern char __bss_end; extern char __heap_start; extern void* __brkval; int freeMemory() { int free_memory; if ((int)__brkval == 0) free_memory = ((int)&free_memory) - ((int)&__bss_end); else free_memory = ((int)&free_memory) - ((int)__brkval); return free_memory; } } #endif // __MK20DX256__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/HAL.cpp
C++
agpl-3.0
3,500
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL for Teensy 3.2 (MK20DX256) */ #define CPU_32_BIT #include "../shared/Marduino.h" #include "../shared/math_32bit.h" #include "../shared/HAL_SPI.h" #include "fastio.h" #include <stdint.h> // ------------------------ // Defines // ------------------------ #define IS_32BIT_TEENSY 1 #define IS_TEENSY_31_32 1 #ifndef IS_TEENSY31 #define IS_TEENSY32 1 #endif #define CPU_ST7920_DELAY_1 600 #define CPU_ST7920_DELAY_2 750 #define CPU_ST7920_DELAY_3 750 // ------------------------ // Serial ports // ------------------------ #include "../../core/serial_hook.h" #define Serial0 Serial #define _DECLARE_SERIAL(X) \ typedef ForwardSerial1Class<decltype(Serial##X)> DefaultSerial##X; \ extern DefaultSerial##X MSerial##X #define DECLARE_SERIAL(X) _DECLARE_SERIAL(X) typedef ForwardSerial1Class<decltype(SerialUSB)> USBSerialType; extern USBSerialType USBSerial; #define _MSERIAL(X) MSerial##X #define MSERIAL(X) _MSERIAL(X) #if SERIAL_PORT == -1 #define MYSERIAL1 USBSerial #elif WITHIN(SERIAL_PORT, 0, 3) DECLARE_SERIAL(SERIAL_PORT); #define MYSERIAL1 MSERIAL(SERIAL_PORT) #else #error "The required SERIAL_PORT must be from 0 to 3, or -1 for Native USB." #endif // ------------------------ // Types // ------------------------ class libServo; typedef libServo hal_servo_t; typedef int8_t pin_t; // ------------------------ // Interrupts // ------------------------ uint32_t __get_PRIMASK(void); // CMSIS #define CRITICAL_SECTION_START() const bool irqon = !__get_PRIMASK(); __disable_irq() #define CRITICAL_SECTION_END() if (irqon) __enable_irq() // ------------------------ // ADC // ------------------------ #ifndef analogInputToDigitalPin #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1) #endif #define HAL_ADC_VREF_MV 3300 #define HAL_ADC_RESOLUTION 10 // // Pin Mapping for M42, M43, M226 // #define GET_PIN_MAP_PIN(index) index #define GET_PIN_MAP_INDEX(pin) pin #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval) // ------------------------ // Class Utilities // ------------------------ #pragma GCC diagnostic push #if GCC_VERSION <= 50000 #pragma GCC diagnostic ignored "-Wunused-function" #endif extern "C" int freeMemory(); #pragma GCC diagnostic pop // ------------------------ // MarlinHAL Class // ------------------------ class MarlinHAL { public: // Earliest possible init, before setup() MarlinHAL() {} // Watchdog static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {}); static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {}); static void init() {} // Called early in setup() static void init_board() {} // Called less early in setup() static void reboot(); // Restart the firmware from 0x0 // Interrupts static bool isr_state() { return !__get_PRIMASK(); } static void isr_on() { __enable_irq(); } static void isr_off() { __disable_irq(); } static void delay_ms(const int ms) { delay(ms); } // Tasks, called from idle() static void idletask() {} // Reset static uint8_t get_reset_source(); static void clear_reset_source() {} // Free SRAM static int freeMemory() { return ::freeMemory(); } // // ADC Methods // // Called by Temperature::init once at startup static void adc_init(); // Called by Temperature::init for each sensor at startup static void adc_enable(const pin_t ch) {} // Begin ADC sampling on the given channel. Called from Temperature::isr! static void adc_start(const pin_t ch); // Is the ADC ready for reading? static bool adc_ready() { return true; } // The current value of the ADC register static uint16_t adc_value(); /** * Set the PWM duty cycle for the pin to the given value. * No option to invert the duty cycle [default = false] * No option to change the scale of the provided value to enable finer PWM duty control [default = 255] */ static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); } };
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/HAL.h
C++
agpl-3.0
4,897
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __MK20DX256__ #include "../../inc/MarlinConfig.h" #include "HAL.h" #include <SPI.h> #include <pins_arduino.h> #include "spi_pins.h" static SPISettings spiConfig; /** * Standard SPI functions */ // Initialize SPI bus void spiBegin() { #if PIN_EXISTS(SD_SS) OUT_WRITE(SD_SS_PIN, HIGH); #endif SET_OUTPUT(SD_SCK_PIN); SET_INPUT(SD_MISO_PIN); SET_OUTPUT(SD_MOSI_PIN); #if 0 && DISABLED(SOFTWARE_SPI) // set SS high - may be chip select for another SPI device #if SET_SPI_SS_HIGH WRITE(SD_SS_PIN, HIGH); #endif // set a default rate spiInit(SPI_HALF_SPEED); // 1 #endif } // Configure SPI for specified SPI speed void spiInit(uint8_t spiRate) { // Use data rates Marlin uses uint32_t clock; switch (spiRate) { case SPI_FULL_SPEED: clock = 10000000; break; case SPI_HALF_SPEED: clock = 5000000; break; case SPI_QUARTER_SPEED: clock = 2500000; break; case SPI_EIGHTH_SPEED: clock = 1250000; break; case SPI_SPEED_5: clock = 625000; break; case SPI_SPEED_6: clock = 312500; break; default: clock = 4000000; // Default from the SPI library } spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0); SPI.begin(); } // SPI receive a byte uint8_t spiRec() { SPI.beginTransaction(spiConfig); const uint8_t returnByte = SPI.transfer(0xFF); SPI.endTransaction(); return returnByte; //SPDR = 0xFF; //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //return SPDR; } // SPI read data void spiRead(uint8_t *buf, uint16_t nbyte) { SPI.beginTransaction(spiConfig); SPI.transfer(buf, nbyte); SPI.endTransaction(); //if (nbyte-- == 0) return; // SPDR = 0xFF; //for (uint16_t i = 0; i < nbyte; i++) { // while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } // buf[i] = SPDR; // SPDR = 0xFF; //} //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //buf[nbyte] = SPDR; } // SPI send a byte void spiSend(uint8_t b) { SPI.beginTransaction(spiConfig); SPI.transfer(b); SPI.endTransaction(); //SPDR = b; //while (!TEST(SPSR, SPIF)) { /* nada */ } } // SPI send block void spiSendBlock(uint8_t token, const uint8_t *buf) { SPI.beginTransaction(spiConfig); SPDR = token; for (uint16_t i = 0; i < 512; i += 2) { while (!TEST(SPSR, SPIF)) { /* nada */ }; SPDR = buf[i]; while (!TEST(SPSR, SPIF)) { /* nada */ }; SPDR = buf[i + 1]; } while (!TEST(SPSR, SPIF)) { /* nada */ }; SPI.endTransaction(); } // Begin SPI transaction, set clock, bit order, data mode void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { spiConfig = SPISettings(spiClock, bitOrder, dataMode); SPI.beginTransaction(spiConfig); } #endif // __MK20DX256__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/HAL_SPI.cpp
C++
agpl-3.0
3,641
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <SPI.h> using MarlinSPI = SPIClass;
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/MarlinSPI.h
C
agpl-3.0
922
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __MK20DX256__ #include "../../inc/MarlinConfig.h" #if HAS_SERVOS #include "Servo.h" uint8_t servoPin[MAX_SERVOS] = { 0 }; int8_t libServo::attach(const int inPin) { if (servoIndex >= MAX_SERVOS) return -1; if (inPin > 0) servoPin[servoIndex] = inPin; return super::attach(servoPin[servoIndex]); } int8_t libServo::attach(const int inPin, const int inMin, const int inMax) { if (inPin > 0) servoPin[servoIndex] = inPin; return super::attach(servoPin[servoIndex], inMin, inMax); } void libServo::move(const int value) { constexpr uint16_t servo_delay[] = SERVO_DELAY; static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long."); if (attach(0) >= 0) { write(value); safe_delay(servo_delay[servoIndex]); TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach()); } } #endif // HAS_SERVOS #endif // __MK20DX256__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/Servo.cpp
C++
agpl-3.0
1,740
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <Servo.h> // Inherit and expand on the official library class libServo : public Servo { public: int8_t attach(const int pin); int8_t attach(const int pin, const int min, const int max); void move(const int value); private: typedef Servo super; uint16_t min_ticks; uint16_t max_ticks; uint8_t servoIndex; // index into the channel data for this servo };
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/Servo.h
C++
agpl-3.0
1,285
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __MK20DX256__ /** * HAL PersistentStore for Teensy 3.2 (MK20DX256) */ #include "../../inc/MarlinConfig.h" #if USE_WIRED_EEPROM #include "../shared/eeprom_api.h" #include <avr/eeprom.h> #ifndef MARLIN_EEPROM_SIZE #define MARLIN_EEPROM_SIZE size_t(E2END + 1) #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } bool PersistentStore::access_start() { return true; } bool PersistentStore::access_finish() { return true; } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { uint16_t written = 0; while (size--) { uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos); uint8_t v = *value; if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed! eeprom_write_byte(p, v); if (++written & 0x7F) delay(2); else safe_delay(2); // Avoid triggering watchdog during long EEPROM writes if (eeprom_read_byte(p) != v) { SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE); return true; } } crc16(crc, &v, 1); pos++; value++; } return false; } bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) { do { const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos)); if (writing) *value = c; crc16(crc, &c, 1); pos++; value++; } while (--size); return false; } #endif // USE_WIRED_EEPROM #endif // __MK20DX256__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/eeprom.cpp
C++
agpl-3.0
2,383
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Endstop Interrupts * * Without endstop interrupts the endstop pins must be polled continually in * the temperature-ISR via endstops.update(), most of the time finding no change. * With this feature endstops.update() is called only when we know that at * least one endstop has changed state, saving valuable CPU cycles. * * This feature only works when all used endstop pins can generate an 'external interrupt'. * * Test whether pins issue interrupts on your board by flashing 'pin_interrupt_test.ino'. * (Located in Marlin/buildroot/share/pin_interrupt_test/pin_interrupt_test.ino) */ #include "../../module/endstops.h" // One ISR for all EXT-Interrupts void endstop_ISR() { endstops.update(); } /** * Endstop interrupts for Due based targets. * On Due, all pins support external interrupt capability. */ void setup_endstop_interrupts() { #define _ATTACH(P) attachInterrupt(digitalPinToInterrupt(P), endstop_ISR, CHANGE) TERN_(USE_X_MAX, _ATTACH(X_MAX_PIN)); TERN_(USE_X_MIN, _ATTACH(X_MIN_PIN)); TERN_(USE_Y_MAX, _ATTACH(Y_MAX_PIN)); TERN_(USE_Y_MIN, _ATTACH(Y_MIN_PIN)); TERN_(USE_Z_MAX, _ATTACH(Z_MAX_PIN)); TERN_(USE_Z_MIN, _ATTACH(Z_MIN_PIN)); TERN_(USE_X2_MAX, _ATTACH(X2_MAX_PIN)); TERN_(USE_X2_MIN, _ATTACH(X2_MIN_PIN)); TERN_(USE_Y2_MAX, _ATTACH(Y2_MAX_PIN)); TERN_(USE_Y2_MIN, _ATTACH(Y2_MIN_PIN)); TERN_(USE_Z2_MAX, _ATTACH(Z2_MAX_PIN)); TERN_(USE_Z2_MIN, _ATTACH(Z2_MIN_PIN)); TERN_(USE_Z3_MAX, _ATTACH(Z3_MAX_PIN)); TERN_(USE_Z3_MIN, _ATTACH(Z3_MIN_PIN)); TERN_(USE_Z4_MAX, _ATTACH(Z4_MAX_PIN)); TERN_(USE_Z4_MIN, _ATTACH(Z4_MIN_PIN)); TERN_(USE_Z_MIN_PROBE, _ATTACH(Z_MIN_PROBE_PIN)); TERN_(USE_I_MAX, _ATTACH(I_MAX_PIN)); TERN_(USE_I_MIN, _ATTACH(I_MIN_PIN)); TERN_(USE_J_MAX, _ATTACH(J_MAX_PIN)); TERN_(USE_J_MIN, _ATTACH(J_MIN_PIN)); TERN_(USE_K_MAX, _ATTACH(K_MAX_PIN)); TERN_(USE_K_MIN, _ATTACH(K_MIN_PIN)); TERN_(USE_U_MAX, _ATTACH(U_MAX_PIN)); TERN_(USE_U_MIN, _ATTACH(U_MIN_PIN)); TERN_(USE_V_MAX, _ATTACH(V_MAX_PIN)); TERN_(USE_V_MIN, _ATTACH(V_MIN_PIN)); TERN_(USE_W_MAX, _ATTACH(W_MAX_PIN)); TERN_(USE_W_MIN, _ATTACH(W_MIN_PIN)); }
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/endstop_interrupts.h
C
agpl-3.0
3,179
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Fast I/O Routines for Teensy 3.5 and Teensy 3.6 * Use direct port manipulation to save scads of processor time. * Contributed by Triffid_Hunter and modified by Kliment, thinkyhead, Bob-the-Kuhn, et.al. */ #ifndef MASK #define MASK(PIN) _BV(PIN) #endif #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000) #define GPIO_BITBAND(reg, bit) (*(uint32_t *)GPIO_BITBAND_ADDR((reg), (bit))) /** * Magic I/O routines * * Now you can simply SET_OUTPUT(PIN); WRITE(PIN, HIGH); WRITE(PIN, LOW); * * Why double up on these macros? see https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Stringification.html */ #define _READ(P) bool(CORE_PIN ## P ## _PINREG & CORE_PIN ## P ## _BITMASK) #define _WRITE(P,V) do{ \ if (V) CORE_PIN ## P ## _PORTSET = CORE_PIN ## P ## _BITMASK; \ else CORE_PIN ## P ## _PORTCLEAR = CORE_PIN ## P ## _BITMASK; \ }while(0) #define _TOGGLE(P) (*(&(CORE_PIN ## P ## _PORTCLEAR)+1) = CORE_PIN ## P ## _BITMASK) #define _SET_INPUT(P) do{ \ CORE_PIN ## P ## _CONFIG = PORT_PCR_MUX(1); \ GPIO_BITBAND(CORE_PIN ## P ## _DDRREG , CORE_PIN ## P ## _BIT) = 0; \ }while(0) #define _SET_OUTPUT(P) do{ \ CORE_PIN ## P ## _CONFIG = PORT_PCR_MUX(1)|PORT_PCR_SRE|PORT_PCR_DSE; \ GPIO_BITBAND(CORE_PIN ## P ## _DDRREG , CORE_PIN ## P ## _BIT) = 1; \ }while(0) #define _SET_INPUT_PULLUP(P) do{ \ CORE_PIN ## P ## _CONFIG = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; \ GPIO_BITBAND(CORE_PIN ## P ## _DDRREG , CORE_PIN ## P ## _BIT) = 0; \ }while(0) #define _IS_INPUT(P) ((CORE_PIN ## P ## _DDRREG & CORE_PIN ## P ## _BITMASK) == 0) #define _IS_OUTPUT(P) ((CORE_PIN ## P ## _DDRREG & CORE_PIN ## P ## _BITMASK) == 0) #define READ(IO) _READ(IO) #define WRITE(IO,V) _WRITE(IO,V) #define TOGGLE(IO) _TOGGLE(IO) #define SET_INPUT(IO) _SET_INPUT(IO) #define SET_INPUT_PULLUP(IO) _SET_INPUT_PULLUP(IO) #define SET_INPUT_PULLDOWN SET_INPUT #define SET_OUTPUT(IO) _SET_OUTPUT(IO) #define SET_PWM SET_OUTPUT #define IS_INPUT(IO) _IS_INPUT(IO) #define IS_OUTPUT(IO) _IS_OUTPUT(IO) #define OUT_WRITE(IO,V) do{ SET_OUTPUT(IO); WRITE(IO,V); }while(0) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) #define PWM_PIN(P) digitalPinHasPWM(P) /** * Ports, functions, and pins */ #define DIO0_PIN 8
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/fastio.h
C
agpl-3.0
3,331
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/inc/Conditionals_LCD.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/inc/Conditionals_adv.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if USE_FALLBACK_EEPROM #define USE_WIRED_EEPROM 1 #endif
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/inc/Conditionals_post.h
C
agpl-3.0
936
/** * Marlin 3D Printer Firmware * Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/inc/Conditionals_type.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Test TEENSY35_36 specific configuration values for errors at compile-time. */ #if HAS_SPI_TFT || HAS_FSMC_TFT #error "Sorry! TFT displays are not available for Teensy 3.1/3.2." #endif #if ENABLED(EMERGENCY_PARSER) #error "EMERGENCY_PARSER is not yet implemented for Teensy 3.1/3.2. Disable EMERGENCY_PARSER to continue." #endif #if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY #error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported for Teensy 3.1/3.2." #endif #if HAS_TMC_SW_SERIAL #error "TMC220x Software Serial is not supported for Teensy 3.1/3.2." #endif #if ENABLED(POSTMORTEM_DEBUGGING) #error "POSTMORTEM_DEBUGGING is not yet supported for Teensy 3.1/3.2." #endif #if USING_PULLDOWNS #error "PULLDOWN pin mode is not available for Teensy 3.1/3.2." #endif
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/inc/SanityCheck.h
C
agpl-3.0
1,715
#error "PINS_DEBUGGING is not yet supported for Teensy 3.1 / 3.2!"
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/pinsDebug.h
C
agpl-3.0
67
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #define SD_SCK_PIN 13 #define SD_MISO_PIN 12 #define SD_MOSI_PIN 11 #define SD_SS_PIN 20 // SDSS // A.28, A.29, B.21, C.26, C.29
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/spi_pins.h
C
agpl-3.0
1,008
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL Timers for Teensy 3.2 (MK20DX256) */ #ifdef __MK20DX256__ #include "../../inc/MarlinConfig.h" /** \brief Instruction Synchronization Barrier Instruction Synchronization Barrier flushes the pipeline in the processor, so that all instructions following the ISB are fetched from cache or memory, after the instruction has been completed. */ FORCE_INLINE static void __ISB() { __asm__ __volatile__("isb 0xF":::"memory"); } /** \brief Data Synchronization Barrier This function acts as a special kind of Data Memory Barrier. It completes when all explicit memory accesses before this instruction complete. */ FORCE_INLINE static void __DSB() { __asm__ __volatile__("dsb 0xF":::"memory"); } void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { switch (timer_num) { case MF_TIMER_STEP: FTM0_MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; FTM0_SC = 0x00; // Set this to zero before changing the modulus FTM0_CNT = 0x0000; // Reset the count to zero FTM0_MOD = 0xFFFF; // max modulus = 65535 FTM0_C0V = (FTM0_TIMER_RATE) / frequency; // Initial FTM Channel 0 compare value FTM0_SC = (FTM_SC_CLKS(0b1) & FTM_SC_CLKS_MASK) | (FTM_SC_PS(FTM0_TIMER_PRESCALE_BITS) & FTM_SC_PS_MASK); // Bus clock 60MHz divided by prescaler 8 FTM0_C0SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSA; break; case MF_TIMER_TEMP: FTM1_MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; // Disable write protection, Enable FTM1 FTM1_SC = 0x00; // Set this to zero before changing the modulus FTM1_CNT = 0x0000; // Reset the count to zero FTM1_MOD = 0xFFFF; // max modulus = 65535 FTM1_C0V = (FTM1_TIMER_RATE) / frequency; // Initial FTM Channel 0 compare value 65535 FTM1_SC = (FTM_SC_CLKS(0b1) & FTM_SC_CLKS_MASK) | (FTM_SC_PS(FTM1_TIMER_PRESCALE_BITS) & FTM_SC_PS_MASK); // Bus clock 60MHz divided by prescaler 4 FTM1_C0SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSA; break; } } void HAL_timer_enable_interrupt(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: NVIC_ENABLE_IRQ(IRQ_FTM0); break; case MF_TIMER_TEMP: NVIC_ENABLE_IRQ(IRQ_FTM1); break; } } void HAL_timer_disable_interrupt(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: NVIC_DISABLE_IRQ(IRQ_FTM0); break; case MF_TIMER_TEMP: NVIC_DISABLE_IRQ(IRQ_FTM1); break; } // We NEED memory barriers to ensure Interrupts are actually disabled! // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the ) __DSB(); __ISB(); } bool HAL_timer_interrupt_enabled(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return NVIC_IS_ENABLED(IRQ_FTM0); case MF_TIMER_TEMP: return NVIC_IS_ENABLED(IRQ_FTM1); } return false; } void HAL_timer_isr_prologue(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: FTM0_CNT = 0x0000; FTM0_SC &= ~FTM_SC_TOF; // Clear FTM Overflow flag FTM0_C0SC &= ~FTM_CSC_CHF; // Clear FTM Channel Compare flag break; case MF_TIMER_TEMP: FTM1_CNT = 0x0000; FTM1_SC &= ~FTM_SC_TOF; // Clear FTM Overflow flag FTM1_C0SC &= ~FTM_CSC_CHF; // Clear FTM Channel Compare flag break; } } #endif // __MK20DX256__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/timers.cpp
C++
agpl-3.0
4,148
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL Timers for Teensy 3.2 (MK20DX256) */ #include <stdint.h> // ------------------------ // Defines // ------------------------ #define FORCE_INLINE __attribute__((always_inline)) inline typedef uint32_t hal_timer_t; #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF #define FTM0_TIMER_PRESCALE 8 #define FTM1_TIMER_PRESCALE 4 #define FTM0_TIMER_PRESCALE_BITS 0b011 #define FTM1_TIMER_PRESCALE_BITS 0b010 #define FTM0_TIMER_RATE (F_BUS / (FTM0_TIMER_PRESCALE)) // 60MHz / 8 = 7500kHz #define FTM1_TIMER_RATE (F_BUS / (FTM1_TIMER_PRESCALE)) // 60MHz / 4 = 15MHz #define HAL_TIMER_RATE (FTM0_TIMER_RATE) #ifndef MF_TIMER_STEP #define MF_TIMER_STEP 0 // Timer Index for Stepper #endif #ifndef MF_TIMER_PULSE #define MF_TIMER_PULSE MF_TIMER_STEP #endif #ifndef MF_TIMER_TEMP #define MF_TIMER_TEMP 1 // Timer Index for Temperature #endif #define TEMP_TIMER_FREQUENCY 1000 #define STEPPER_TIMER_RATE HAL_TIMER_RATE #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US) #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_STEP) #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_STEP) #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP) #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_TEMP) #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_TEMP) #ifndef HAL_STEP_TIMER_ISR #define HAL_STEP_TIMER_ISR() extern "C" void ftm0_isr() //void TC3_Handler() #endif #ifndef HAL_TEMP_TIMER_ISR #define HAL_TEMP_TIMER_ISR() extern "C" void ftm1_isr() //void TC4_Handler() #endif void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) { switch (timer_num) { case MF_TIMER_STEP: FTM0_C0V = compare; break; case MF_TIMER_TEMP: FTM1_C0V = compare; break; } } FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return FTM0_C0V; case MF_TIMER_TEMP: return FTM1_C0V; } return 0; } FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return FTM0_CNT; case MF_TIMER_TEMP: return FTM1_CNT; } return 0; } void HAL_timer_enable_interrupt(const uint8_t timer_num); void HAL_timer_disable_interrupt(const uint8_t timer_num); bool HAL_timer_interrupt_enabled(const uint8_t timer_num); void HAL_timer_isr_prologue(const uint8_t timer_num); #define HAL_timer_isr_epilogue(T) NOOP
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/timers.h
C
agpl-3.0
3,807
/** * Marlin 3D Printer Firmware * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Teensy 3.1/3.2 LCD-specific defines */
2301_81045437/Marlin
Marlin/src/HAL/TEENSY31_32/u8g/LCD_defines.h
C
agpl-3.0
923
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #if defined(__MK64FX512__) || defined(__MK66FX1M0__) #include "HAL.h" #include "../shared/Delay.h" #include <Wire.h> // ------------------------ // Serial ports // ------------------------ #define _IMPLEMENT_SERIAL(X) DefaultSerial##X MSerial##X(false, Serial##X) #define IMPLEMENT_SERIAL(X) _IMPLEMENT_SERIAL(X) #if WITHIN(SERIAL_PORT, 0, 3) IMPLEMENT_SERIAL(SERIAL_PORT); #endif USBSerialType USBSerial(false, SerialUSB); // ------------------------ // MarlinHAL Class // ------------------------ void MarlinHAL::reboot() { _reboot_Teensyduino_(); } uint8_t MarlinHAL::get_reset_source() { switch (RCM_SRS0) { case 128: return RST_POWER_ON; break; case 64: return RST_EXTERNAL; break; case 32: return RST_WATCHDOG; break; // case 8: return RST_LOSS_OF_LOCK; break; // case 4: return RST_LOSS_OF_CLOCK; break; // case 2: return RST_LOW_VOLTAGE; break; } return 0; } // ------------------------ // Watchdog Timer // ------------------------ #if ENABLED(USE_WATCHDOG) #define WDT_TIMEOUT_MS TERN(WATCHDOG_DURATION_8S, 8000, 4000) // 4 or 8 second timeout void MarlinHAL::watchdog_init() { WDOG_TOVALH = 0; WDOG_TOVALL = WDT_TIMEOUT_MS; WDOG_STCTRLH = WDOG_STCTRLH_WDOGEN; } void MarlinHAL::watchdog_refresh() { // Watchdog refresh sequence WDOG_REFRESH = 0xA602; WDOG_REFRESH = 0xB480; } #endif // ------------------------ // ADC // ------------------------ int8_t MarlinHAL::adc_select; void MarlinHAL::adc_init() { analog_init(); while (ADC0_SC3 & ADC_SC3_CAL) { /* Wait for calibration to finish */ } while (ADC1_SC3 & ADC_SC3_CAL) { /* Wait for calibration to finish */ } NVIC_ENABLE_IRQ(IRQ_FTM1); } void MarlinHAL::adc_start(const pin_t adc_pin) { static const uint8_t pin2sc1a[] = { 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 3, 19+128, 14+128, 15+128, // 0-13 -> A0-A13 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9 255, 255, 255, 255, 255, 255, 255, // 24-30 are digital only 14+128, 15+128, 17, 18, 4+128, 5+128, 6+128, 7+128, 17+128, // 31-39 are A12-A20 255, 255, 255, 255, 255, 255, 255, 255, 255, // 40-48 are digital only 10+128, 11+128, // 49-50 are A23-A24 255, 255, 255, 255, 255, 255, 255, // 51-57 are digital only 255, 255, 255, 255, 255, 255, // 58-63 (sd card pins) are digital only 3, 19+128, // 64-65 are A10-A11 23, 23+128,// 66-67 are A21-A22 (DAC pins) 1, 1+128, // 68-69 are A25-A26 (unused USB host port on Teensy 3.5) 26, // 70 is Temperature Sensor 18+128 // 71 is Vref }; const uint16_t pin = pin2sc1a[adc_pin]; if (pin == 0xFF) { adc_select = -1; // Digital only } else if (pin & 0x80) { adc_select = 1; ADC1_SC1A = pin & 0x7F; } else { adc_select = 0; ADC0_SC1A = pin; } } uint16_t MarlinHAL::adc_value() { switch (adc_select) { case 0: return ADC0_RA; case 1: return ADC1_RA; } return 0; } // ------------------------ // Free Memory Accessor // ------------------------ extern "C" { extern char __bss_end; extern char __heap_start; extern void* __brkval; int freeMemory() { int free_memory; if ((int)__brkval == 0) free_memory = ((int)&free_memory) - ((int)&__bss_end); else free_memory = ((int)&free_memory) - ((int)__brkval); return free_memory; } } #endif // __MK64FX512__ || __MK66FX1M0__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/HAL.cpp
C++
agpl-3.0
4,316
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #define CPU_32_BIT #include "../shared/Marduino.h" #include "../shared/math_32bit.h" #include "../shared/HAL_SPI.h" #include "fastio.h" #include <stdint.h> #include <util/atomic.h> // ------------------------ // Defines // ------------------------ #define IS_32BIT_TEENSY 1 #define IS_TEENSY_35_36 1 #ifdef __MK66FX1M0__ #define IS_TEENSY36 1 #else // __MK64FX512__ #define IS_TEENSY35 1 #endif #define CPU_ST7920_DELAY_1 600 #define CPU_ST7920_DELAY_2 750 #define CPU_ST7920_DELAY_3 750 #undef sq #define sq(x) ((x)*(x)) // ------------------------ // Serial ports // ------------------------ #include "../../core/serial_hook.h" #define Serial0 Serial #define _DECLARE_SERIAL(X) \ typedef ForwardSerial1Class<decltype(Serial##X)> DefaultSerial##X; \ extern DefaultSerial##X MSerial##X #define DECLARE_SERIAL(X) _DECLARE_SERIAL(X) typedef ForwardSerial1Class<decltype(SerialUSB)> USBSerialType; extern USBSerialType USBSerial; #define _MSERIAL(X) MSerial##X #define MSERIAL(X) _MSERIAL(X) #if SERIAL_PORT == -1 #define MYSERIAL1 USBSerial #elif WITHIN(SERIAL_PORT, 0, 3) #define MYSERIAL1 MSERIAL(SERIAL_PORT) DECLARE_SERIAL(SERIAL_PORT); #else #error "SERIAL_PORT must be from 0 to 3, or -1 for Native USB." #endif // ------------------------ // Types // ------------------------ class libServo; typedef libServo hal_servo_t; typedef int8_t pin_t; // ------------------------ // Interrupts // ------------------------ #define CRITICAL_SECTION_START() const bool irqon = !__get_primask(); __disable_irq() #define CRITICAL_SECTION_END() if (irqon) __enable_irq() // ------------------------ // ADC // ------------------------ #ifndef analogInputToDigitalPin #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1) #endif #define HAL_ADC_VREF_MV 3300 #define HAL_ADC_RESOLUTION 10 // // Pin Mapping for M42, M43, M226 // #define GET_PIN_MAP_PIN(index) index #define GET_PIN_MAP_INDEX(pin) pin #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval) // ------------------------ // Free Memory Accessor // ------------------------ #pragma GCC diagnostic push #if GCC_VERSION <= 50000 #pragma GCC diagnostic ignored "-Wunused-function" #endif extern "C" int freeMemory(); #pragma GCC diagnostic pop // ------------------------ // MarlinHAL Class // ------------------------ class MarlinHAL { public: // Earliest possible init, before setup() MarlinHAL() {} // Watchdog static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {}); static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {}); static void init() {} // Called early in setup() static void init_board() {} // Called less early in setup() static void reboot(); // Restart the firmware from 0x0 // Interrupts static bool isr_state() { return true; } static void isr_on() { __enable_irq(); } static void isr_off() { __disable_irq(); } static void delay_ms(const int ms) { delay(ms); } // Tasks, called from idle() static void idletask() {} // Reset static uint8_t get_reset_source(); static void clear_reset_source() {} // Free SRAM static int freeMemory() { return ::freeMemory(); } // // ADC Methods // static int8_t adc_select; // Called by Temperature::init once at startup static void adc_init(); // Called by Temperature::init for each sensor at startup static void adc_enable(const pin_t) {} // Begin ADC sampling on the given pin. Called from Temperature::isr! static void adc_start(const pin_t pin); // Is the ADC ready for reading? static bool adc_ready() { return true; } // The current value of the ADC register static uint16_t adc_value(); /** * Set the PWM duty cycle for the pin to the given value. * No option to invert the duty cycle [default = false] * No option to change the scale of the provided value to enable finer PWM duty control [default = 255] */ static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); } };
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/HAL.h
C++
agpl-3.0
4,996
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL SPI for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #if defined(__MK64FX512__) || defined(__MK66FX1M0__) #include "../../inc/MarlinConfig.h" #include "HAL.h" #include <SPI.h> #include <pins_arduino.h> #include "spi_pins.h" static SPISettings spiConfig; void spiBegin() { #if PIN_EXISTS(SD_SS) OUT_WRITE(SD_SS_PIN, HIGH); #endif SET_OUTPUT(SD_SCK_PIN); SET_INPUT(SD_MISO_PIN); SET_OUTPUT(SD_MOSI_PIN); #if 0 && DISABLED(SOFTWARE_SPI) // set SS high - may be chip select for another SPI device #if SET_SPI_SS_HIGH WRITE(SD_SS_PIN, HIGH); #endif // set a default rate spiInit(SPI_HALF_SPEED); // 1 #endif } void spiInit(uint8_t spiRate) { // Use Marlin data-rates uint32_t clock; switch (spiRate) { case SPI_FULL_SPEED: clock = 10000000; break; case SPI_HALF_SPEED: clock = 5000000; break; case SPI_QUARTER_SPEED: clock = 2500000; break; case SPI_EIGHTH_SPEED: clock = 1250000; break; case SPI_SPEED_5: clock = 625000; break; case SPI_SPEED_6: clock = 312500; break; default: clock = 4000000; // Default from the SPI library } spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0); SPI.begin(); } uint8_t spiRec() { SPI.beginTransaction(spiConfig); uint8_t returnByte = SPI.transfer(0xFF); SPI.endTransaction(); return returnByte; //SPDR = 0xFF; //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //return SPDR; } void spiRead(uint8_t *buf, uint16_t nbyte) { SPI.beginTransaction(spiConfig); SPI.transfer(buf, nbyte); SPI.endTransaction(); //if (nbyte-- == 0) return; // SPDR = 0xFF; //for (uint16_t i = 0; i < nbyte; i++) { // while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } // buf[i] = SPDR; // SPDR = 0xFF; //} //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //buf[nbyte] = SPDR; } void spiSend(uint8_t b) { SPI.beginTransaction(spiConfig); SPI.transfer(b); SPI.endTransaction(); //SPDR = b; //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } } void spiSendBlock(uint8_t token, const uint8_t *buf) { SPI.beginTransaction(spiConfig); SPDR = token; for (uint16_t i = 0; i < 512; i += 2) { while (!TEST(SPSR, SPIF)) { /* nada */ }; SPDR = buf[i]; while (!TEST(SPSR, SPIF)) { /* nada */ }; SPDR = buf[i + 1]; } while (!TEST(SPSR, SPIF)) { /* nada */ }; SPI.endTransaction(); } // Begin SPI transaction, set clock, bit order, data mode void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { spiConfig = SPISettings(spiClock, bitOrder, dataMode); SPI.beginTransaction(spiConfig); } #endif // __MK64FX512__ || __MK66FX1M0__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/HAL_SPI.cpp
C++
agpl-3.0
3,575
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <SPI.h> using MarlinSPI = SPIClass;
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/MarlinSPI.h
C
agpl-3.0
922
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL Servo for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #if defined(__MK64FX512__) || defined(__MK66FX1M0__) #include "../../inc/MarlinConfig.h" #if HAS_SERVOS #include "Servo.h" uint8_t servoPin[MAX_SERVOS] = { 0 }; int8_t libServo::attach(const int inPin) { if (servoIndex >= MAX_SERVOS) return -1; if (inPin > 0) servoPin[servoIndex] = inPin; return super::attach(servoPin[servoIndex]); } int8_t libServo::attach(const int inPin, const int inMin, const int inMax) { if (inPin > 0) servoPin[servoIndex] = inPin; return super::attach(servoPin[servoIndex], inMin, inMax); } void libServo::move(const int value) { constexpr uint16_t servo_delay[] = SERVO_DELAY; static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long."); if (attach(0) >= 0) { write(value); safe_delay(servo_delay[servoIndex]); TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach()); } } #endif // HAS_SERVOS #endif // __MK64FX512__ || __MK66FX1M0__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/Servo.cpp
C++
agpl-3.0
1,866
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL Servo for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #include <Servo.h> // Inherit and expand on core Servo library class libServo : public Servo { public: int8_t attach(const int pin); int8_t attach(const int pin, const int min, const int max); void move(const int value); private: typedef Servo super; uint16_t min_ticks; uint16_t max_ticks; uint8_t servoIndex; // Index into the channel data for this servo };
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/Servo.h
C++
agpl-3.0
1,345
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #if defined(__MK64FX512__) || defined(__MK66FX1M0__) /** * HAL PersistentStore for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #include "../../inc/MarlinConfig.h" #if USE_WIRED_EEPROM #include "../shared/eeprom_api.h" #include <avr/eeprom.h> #ifndef MARLIN_EEPROM_SIZE #define MARLIN_EEPROM_SIZE size_t(E2END + 1) #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } bool PersistentStore::access_start() { return true; } bool PersistentStore::access_finish() { return true; } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { uint16_t written = 0; while (size--) { uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos); uint8_t v = *value; if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed! eeprom_write_byte(p, v); if (++written & 0x7F) delay(2); else safe_delay(2); // Avoid triggering watchdog during long EEPROM writes if (eeprom_read_byte(p) != v) { SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE); return true; } } crc16(crc, &v, 1); pos++; value++; } return false; } bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) { do { const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos)); if (writing) *value = c; crc16(crc, &c, 1); pos++; value++; } while (--size); return false; } #endif // USE_WIRED_EEPROM #endif // __MK64FX512__ || __MK66FX1M0__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/eeprom.cpp
C++
agpl-3.0
2,458
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL Endstop Interrupts for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) * * Without endstop interrupts the endstop pins must be polled continually in * the temperature-ISR via endstops.update(), most of the time finding no change. * With this feature endstops.update() is called only when we know that at * least one endstop has changed state, saving valuable CPU cycles. * * This feature only works when all used endstop pins can generate an 'external interrupt'. * * Test whether pins issue interrupts on your board by flashing 'pin_interrupt_test.ino'. * (Located in Marlin/buildroot/share/pin_interrupt_test/pin_interrupt_test.ino) */ #include "../../module/endstops.h" // One ISR for all EXT-Interrupts void endstop_ISR() { endstops.update(); } /** * Endstop interrupts for Due based targets. * On Due, all pins support external interrupt capability. */ void setup_endstop_interrupts() { #define _ATTACH(P) attachInterrupt(digitalPinToInterrupt(P), endstop_ISR, CHANGE) TERN_(USE_X_MAX, _ATTACH(X_MAX_PIN)); TERN_(USE_X_MIN, _ATTACH(X_MIN_PIN)); TERN_(USE_Y_MAX, _ATTACH(Y_MAX_PIN)); TERN_(USE_Y_MIN, _ATTACH(Y_MIN_PIN)); TERN_(USE_Z_MAX, _ATTACH(Z_MAX_PIN)); TERN_(USE_Z_MIN, _ATTACH(Z_MIN_PIN)); TERN_(USE_X2_MAX, _ATTACH(X2_MAX_PIN)); TERN_(USE_X2_MIN, _ATTACH(X2_MIN_PIN)); TERN_(USE_Y2_MAX, _ATTACH(Y2_MAX_PIN)); TERN_(USE_Y2_MIN, _ATTACH(Y2_MIN_PIN)); TERN_(USE_Z2_MAX, _ATTACH(Z2_MAX_PIN)); TERN_(USE_Z2_MIN, _ATTACH(Z2_MIN_PIN)); TERN_(USE_Z3_MAX, _ATTACH(Z3_MAX_PIN)); TERN_(USE_Z3_MIN, _ATTACH(Z3_MIN_PIN)); TERN_(USE_Z4_MAX, _ATTACH(Z4_MAX_PIN)); TERN_(USE_Z4_MIN, _ATTACH(Z4_MIN_PIN)); TERN_(USE_Z_MIN_PROBE, _ATTACH(Z_MIN_PROBE_PIN)); TERN_(USE_I_MAX, _ATTACH(I_MAX_PIN)); TERN_(USE_I_MIN, _ATTACH(I_MIN_PIN)); TERN_(USE_J_MAX, _ATTACH(J_MAX_PIN)); TERN_(USE_J_MIN, _ATTACH(J_MIN_PIN)); TERN_(USE_K_MAX, _ATTACH(K_MAX_PIN)); TERN_(USE_K_MIN, _ATTACH(K_MIN_PIN)); TERN_(USE_U_MAX, _ATTACH(U_MAX_PIN)); TERN_(USE_U_MIN, _ATTACH(U_MIN_PIN)); TERN_(USE_V_MAX, _ATTACH(V_MAX_PIN)); TERN_(USE_V_MIN, _ATTACH(V_MIN_PIN)); TERN_(USE_W_MAX, _ATTACH(W_MAX_PIN)); TERN_(USE_W_MIN, _ATTACH(W_MIN_PIN)); }
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/endstop_interrupts.h
C
agpl-3.0
3,234
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Fast I/O Routines for Teensy 3.5 and Teensy 3.6 * Use direct port manipulation to save scads of processor time. * Contributed by Triffid_Hunter and modified by Kliment, thinkyhead, Bob-the-Kuhn, et.al. */ #ifndef MASK #define MASK(PIN) _BV(PIN) #endif #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000) #define GPIO_BITBAND(reg, bit) (*(uint32_t *)GPIO_BITBAND_ADDR((reg), (bit))) /** * Magic I/O routines * * Now you can simply SET_OUTPUT(PIN); WRITE(PIN, HIGH); WRITE(PIN, LOW); * * Why double up on these macros? see https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Stringification.html */ #define _READ(P) bool(CORE_PIN ## P ## _PINREG & CORE_PIN ## P ## _BITMASK) #define _WRITE(P,V) do{ \ if (V) CORE_PIN ## P ## _PORTSET = CORE_PIN ## P ## _BITMASK; \ else CORE_PIN ## P ## _PORTCLEAR = CORE_PIN ## P ## _BITMASK; \ }while(0) #define _TOGGLE(P) (*(&(CORE_PIN ## P ## _PORTCLEAR)+1) = CORE_PIN ## P ## _BITMASK) #define _SET_INPUT(P) do{ \ CORE_PIN ## P ## _CONFIG = PORT_PCR_MUX(1); \ GPIO_BITBAND(CORE_PIN ## P ## _DDRREG , CORE_PIN ## P ## _BIT) = 0; \ }while(0) #define _SET_OUTPUT(P) do{ \ CORE_PIN ## P ## _CONFIG = PORT_PCR_MUX(1)|PORT_PCR_SRE|PORT_PCR_DSE; \ GPIO_BITBAND(CORE_PIN ## P ## _DDRREG , CORE_PIN ## P ## _BIT) = 1; \ }while(0) #define _SET_INPUT_PULLUP(P) do{ \ CORE_PIN ## P ## _CONFIG = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; \ GPIO_BITBAND(CORE_PIN ## P ## _DDRREG , CORE_PIN ## P ## _BIT) = 0; \ }while(0) #define _IS_INPUT(P) ((CORE_PIN ## P ## _DDRREG & CORE_PIN ## P ## _BITMASK) == 0) #define _IS_OUTPUT(P) ((CORE_PIN ## P ## _DDRREG & CORE_PIN ## P ## _BITMASK) == 0) #define READ(IO) _READ(IO) #define WRITE(IO,V) _WRITE(IO,V) #define TOGGLE(IO) _TOGGLE(IO) #define SET_INPUT(IO) _SET_INPUT(IO) #define SET_INPUT_PULLUP(IO) _SET_INPUT_PULLUP(IO) #define SET_INPUT_PULLDOWN SET_INPUT #define SET_OUTPUT(IO) _SET_OUTPUT(IO) #define SET_PWM SET_OUTPUT #define IS_INPUT(IO) _IS_INPUT(IO) #define IS_OUTPUT(IO) _IS_OUTPUT(IO) #define OUT_WRITE(IO,V) do{ SET_OUTPUT(IO); WRITE(IO,V); }while(0) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) #define PWM_PIN(P) digitalPinHasPWM(P) /** * Ports, functions, and pins */ #define DIO0_PIN 8
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/fastio.h
C
agpl-3.0
3,331
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/inc/Conditionals_LCD.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/inc/Conditionals_adv.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if USE_FALLBACK_EEPROM #define USE_WIRED_EEPROM 1 #endif
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/inc/Conditionals_post.h
C
agpl-3.0
936
/** * Marlin 3D Printer Firmware * Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/inc/Conditionals_type.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Test TEENSY35_36 specific configuration values for errors at compile-time. */ #if HAS_SPI_TFT || HAS_FSMC_TFT #error "Sorry! TFT displays are not available for Teensy 3.5/3.6." #endif #if ENABLED(EMERGENCY_PARSER) #error "EMERGENCY_PARSER is not yet implemented for Teensy 3.5/3.6. Disable EMERGENCY_PARSER to continue." #endif #if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY #error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported for Teensy 3.5/3.6." #endif #if HAS_TMC_SW_SERIAL #error "TMC220x Software Serial is not supported for Teensy 3.5/3.6." #endif #if ENABLED(POSTMORTEM_DEBUGGING) #error "POSTMORTEM_DEBUGGING is not yet supported for Teensy 3.5/3.6." #endif #if USING_PULLDOWNS #error "PULLDOWN pin mode is not available for Teensy 3.5/3.6." #endif
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/inc/SanityCheck.h
C
agpl-3.0
1,715
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL Pins Debugging for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS #define MULTI_NAME_PAD 16 // space needed to be pretty if not first name assigned to a pin #define FTM0_CH0_PIN 22 #define FTM0_CH1_PIN 23 #define FTM0_CH2_PIN 9 #define FTM0_CH3_PIN 10 #define FTM0_CH4_PIN 6 #define FTM0_CH5_PIN 20 #define FTM0_CH6_PIN 21 #define FTM0_CH7_PIN 5 #define FTM1_CH0_PIN 3 #define FTM1_CH1_PIN 4 #define FTM2_CH0_PIN 29 #define FTM2_CH1_PIN 30 #define FTM3_CH0_PIN 2 #define FTM3_CH1_PIN 14 #define FTM3_CH2_PIN 7 #define FTM3_CH3_PIN 8 #define FTM3_CH4_PIN 35 #define FTM3_CH5_PIN 36 #define FTM3_CH6_PIN 37 #define FTM3_CH7_PIN 38 #ifdef __MK66FX1M0__ // Teensy3.6 #define TPM1_CH0_PIN 16 #define TPM1_CH1_PIN 17 #endif #define IS_ANALOG(P) ((P) >= analogInputToDigitalPin(0) && (P) <= analogInputToDigitalPin(9)) || ((P) >= analogInputToDigitalPin(12) && (P) <= analogInputToDigitalPin(20)) void print_analog_pin(char buffer[], int8_t pin) { if (pin <= 23) sprintf_P(buffer, PSTR("(A%2d) "), int(pin - 14)); else if (pin <= 39) sprintf_P(buffer, PSTR("(A%2d) "), int(pin - 19)); } void analog_pin_state(char buffer[], int8_t pin) { if (pin <= 23) sprintf_P(buffer, PSTR("Analog in =% 5d"), analogRead(pin - 14)); else if (pin <= 39) sprintf_P(buffer, PSTR("Analog in =% 5d"), analogRead(pin - 19)); } #define PWM_PRINT(V) do{ sprintf_P(buffer, PSTR("PWM: %4d"), 22); SERIAL_ECHO(buffer); }while(0) #define FTM_CASE(N,Z) \ case FTM##N##_CH##Z##_PIN: \ if (FTM##N##_C##Z##V) { \ PWM_PRINT(FTM##N##_C##Z##V); \ return true; \ } else return false /** * Print a pin's PWM status. * Return true if it's currently a PWM pin. */ bool pwm_status(int8_t pin) { char buffer[20]; // for the sprintf statements switch (pin) { FTM_CASE(0,0); FTM_CASE(0,1); FTM_CASE(0,2); FTM_CASE(0,3); FTM_CASE(0,4); FTM_CASE(0,5); FTM_CASE(0,6); FTM_CASE(0,7); FTM_CASE(1,0); FTM_CASE(1,1); FTM_CASE(2,0); FTM_CASE(2,1); FTM_CASE(3,0); FTM_CASE(3,1); FTM_CASE(3,2); FTM_CASE(3,3); FTM_CASE(3,4); FTM_CASE(3,5); FTM_CASE(3,6); FTM_CASE(3,7); case NOT_ON_TIMER: default: return false; } SERIAL_ECHOPGM(" "); } void pwm_details(uint8_t pin) { /* TODO */ }
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/pinsDebug.h
C
agpl-3.0
3,233
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL SPI Pins for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #define SD_SCK_PIN 13 #define SD_MISO_PIN 12 #define SD_MOSI_PIN 11 #define SD_SS_PIN 20 // SDSS // A.28, A.29, B.21, C.26, C.29
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/spi_pins.h
C
agpl-3.0
1,088
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL Timers for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #if defined(__MK64FX512__) || defined(__MK66FX1M0__) #include "../../inc/MarlinConfig.h" /** \brief Instruction Synchronization Barrier Instruction Synchronization Barrier flushes the pipeline in the processor, so that all instructions following the ISB are fetched from cache or memory, after the instruction has been completed. */ FORCE_INLINE static void __ISB() { __asm__ __volatile__("isb 0xF":::"memory"); } /** \brief Data Synchronization Barrier This function acts as a special kind of Data Memory Barrier. It completes when all explicit memory accesses before this instruction complete. */ FORCE_INLINE static void __DSB() { __asm__ __volatile__("dsb 0xF":::"memory"); } void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { switch (timer_num) { case MF_TIMER_STEP: FTM0_MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; FTM0_SC = 0x00; // Set this to zero before changing the modulus FTM0_CNT = 0x0000; // Reset the count to zero FTM0_MOD = 0xFFFF; // max modulus = 65535 FTM0_C0V = (FTM0_TIMER_RATE) / frequency; // Initial FTM Channel 0 compare value FTM0_SC = (FTM_SC_CLKS(0b1) & FTM_SC_CLKS_MASK) | (FTM_SC_PS(FTM0_TIMER_PRESCALE_BITS) & FTM_SC_PS_MASK); // Bus clock 60MHz divided by prescaler 8 FTM0_C0SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSA; break; case MF_TIMER_TEMP: FTM1_MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; // Disable write protection, Enable FTM1 FTM1_SC = 0x00; // Set this to zero before changing the modulus FTM1_CNT = 0x0000; // Reset the count to zero FTM1_MOD = 0xFFFF; // max modulus = 65535 FTM1_C0V = (FTM1_TIMER_RATE) / frequency; // Initial FTM Channel 0 compare value 65535 FTM1_SC = (FTM_SC_CLKS(0b1) & FTM_SC_CLKS_MASK) | (FTM_SC_PS(FTM1_TIMER_PRESCALE_BITS) & FTM_SC_PS_MASK); // Bus clock 60MHz divided by prescaler 4 FTM1_C0SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSA; break; } } void HAL_timer_enable_interrupt(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: NVIC_ENABLE_IRQ(IRQ_FTM0); break; case MF_TIMER_TEMP: NVIC_ENABLE_IRQ(IRQ_FTM1); break; } } void HAL_timer_disable_interrupt(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: NVIC_DISABLE_IRQ(IRQ_FTM0); break; case MF_TIMER_TEMP: NVIC_DISABLE_IRQ(IRQ_FTM1); break; } // We NEED memory barriers to ensure Interrupts are actually disabled! // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the ) __DSB(); __ISB(); } bool HAL_timer_interrupt_enabled(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return NVIC_IS_ENABLED(IRQ_FTM0); case MF_TIMER_TEMP: return NVIC_IS_ENABLED(IRQ_FTM1); } return false; } void HAL_timer_isr_prologue(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: FTM0_CNT = 0x0000; FTM0_SC &= ~FTM_SC_TOF; // Clear FTM Overflow flag FTM0_C0SC &= ~FTM_CSC_CHF; // Clear FTM Channel Compare flag break; case MF_TIMER_TEMP: FTM1_CNT = 0x0000; FTM1_SC &= ~FTM_SC_TOF; // Clear FTM Overflow flag FTM1_C0SC &= ~FTM_CSC_CHF; // Clear FTM Channel Compare flag break; } } #endif // Teensy3.5 or Teensy3.6
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/timers.cpp
C++
agpl-3.0
4,216
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL Timers for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0) */ #include <stdint.h> // ------------------------ // Defines // ------------------------ #define FORCE_INLINE __attribute__((always_inline)) inline typedef uint32_t hal_timer_t; #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF #define FTM0_TIMER_PRESCALE 8 #define FTM1_TIMER_PRESCALE 4 #define FTM0_TIMER_PRESCALE_BITS 0b011 #define FTM1_TIMER_PRESCALE_BITS 0b010 #define FTM0_TIMER_RATE (F_BUS / FTM0_TIMER_PRESCALE) // 60MHz / 8 = 7500kHz #define FTM1_TIMER_RATE (F_BUS / FTM1_TIMER_PRESCALE) // 60MHz / 4 = 15MHz #define HAL_TIMER_RATE (FTM0_TIMER_RATE) #ifndef MF_TIMER_STEP #define MF_TIMER_STEP 0 // Timer Index for Stepper #endif #ifndef MF_TIMER_PULSE #define MF_TIMER_PULSE MF_TIMER_STEP #endif #ifndef MF_TIMER_TEMP #define MF_TIMER_TEMP 1 // Timer Index for Temperature #endif #define TEMP_TIMER_FREQUENCY 1000 #define STEPPER_TIMER_RATE HAL_TIMER_RATE #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US) #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_STEP) #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_STEP) #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP) #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_TEMP) #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_TEMP) #ifndef HAL_STEP_TIMER_ISR #define HAL_STEP_TIMER_ISR() extern "C" void ftm0_isr() //void TC3_Handler() #endif #ifndef HAL_TEMP_TIMER_ISR #define HAL_TEMP_TIMER_ISR() extern "C" void ftm1_isr() //void TC4_Handler() #endif void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) { switch (timer_num) { case MF_TIMER_STEP: FTM0_C0V = compare; break; case MF_TIMER_TEMP: FTM1_C0V = compare; break; } } FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return FTM0_C0V; case MF_TIMER_TEMP: return FTM1_C0V; } return 0; } FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return FTM0_CNT; case MF_TIMER_TEMP: return FTM1_CNT; } return 0; } void HAL_timer_enable_interrupt(const uint8_t timer_num); void HAL_timer_disable_interrupt(const uint8_t timer_num); bool HAL_timer_interrupt_enabled(const uint8_t timer_num); void HAL_timer_isr_prologue(const uint8_t timer_num); #define HAL_timer_isr_epilogue(T) NOOP
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/timers.h
C
agpl-3.0
3,830
/** * Marlin 3D Printer Firmware * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Teensy 3.5/3.6 LCD-specific defines */
2301_81045437/Marlin
Marlin/src/HAL/TEENSY35_36/u8g/LCD_defines.h
C
agpl-3.0
923
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL for Teensy 4.0 / 4.1 (IMXRT1062) */ #ifdef __IMXRT1062__ #include "../../inc/MarlinConfig.h" #include "HAL.h" #include "../shared/Delay.h" #include "timers.h" #include <Wire.h> // ------------------------ // Serial ports // ------------------------ #define _IMPLEMENT_SERIAL(X) DefaultSerial##X MSerial##X(false, Serial##X) #define IMPLEMENT_SERIAL(X) _IMPLEMENT_SERIAL(X) #if WITHIN(SERIAL_PORT, 0, 8) IMPLEMENT_SERIAL(SERIAL_PORT); #endif #ifdef SERIAL_PORT_2 #if WITHIN(SERIAL_PORT_2, 0, 8) IMPLEMENT_SERIAL(SERIAL_PORT_2); #endif #endif #ifdef SERIAL_PORT_3 #if WITHIN(SERIAL_PORT_3, 0, 8) IMPLEMENT_SERIAL(SERIAL_PORT_3); #endif #endif USBSerialType USBSerial(false, SerialUSB); // ------------------------ // FastIO // ------------------------ bool is_output(pin_t pin) { const struct digital_pin_bitband_and_config_table_struct *p; p = digital_pin_to_info_PGM + pin; return (*(p->reg + 1) & p->mask); } // ------------------------ // MarlinHAL Class // ------------------------ void MarlinHAL::reboot() { _reboot_Teensyduino_(); } uint8_t MarlinHAL::get_reset_source() { switch (SRC_SRSR & 0xFF) { case 1: return RST_POWER_ON; break; case 2: return RST_SOFTWARE; break; case 4: return RST_EXTERNAL; break; //case 8: return RST_BROWN_OUT; break; case 16: return RST_WATCHDOG; break; case 64: return RST_JTAG; break; //case 128: return RST_OVERTEMP; break; } return 0; } void MarlinHAL::clear_reset_source() { uint32_t reset_source = SRC_SRSR; SRC_SRSR = reset_source; } // ------------------------ // Watchdog Timer // ------------------------ #if ENABLED(USE_WATCHDOG) #define WDT_TIMEOUT TERN(WATCHDOG_DURATION_8S, 8, 4) // 4 or 8 second timeout constexpr uint8_t timeoutval = (WDT_TIMEOUT - 0.5f) / 0.5f; void MarlinHAL::watchdog_init() { CCM_CCGR3 |= CCM_CCGR3_WDOG1(3); // enable WDOG1 clocks WDOG1_WMCR = 0; // disable power down PDE WDOG1_WCR |= WDOG_WCR_SRS | WDOG_WCR_WT(timeoutval); WDOG1_WCR |= WDOG_WCR_WDE | WDOG_WCR_WDT | WDOG_WCR_SRE; } void MarlinHAL::watchdog_refresh() { // Watchdog refresh sequence WDOG1_WSR = 0x5555; WDOG1_WSR = 0xAAAA; } #endif // ------------------------ // ADC // ------------------------ int8_t MarlinHAL::adc_select; void MarlinHAL::adc_init() { analog_init(); while (ADC1_GC & ADC_GC_CAL) { /* wait */ } while (ADC2_GC & ADC_GC_CAL) { /* wait */ } } void MarlinHAL::adc_start(const pin_t adc_pin) { static const uint8_t pin2sc1a[] = { 0x07, // 0/A0 AD_B1_02 0x08, // 1/A1 AD_B1_03 0x0C, // 2/A2 AD_B1_07 0x0B, // 3/A3 AD_B1_06 0x06, // 4/A4 AD_B1_01 0x05, // 5/A5 AD_B1_00 0x0F, // 6/A6 AD_B1_10 0x00, // 7/A7 AD_B1_11 0x0D, // 8/A8 AD_B1_08 0x0E, // 9/A9 AD_B1_09 0x01, // 24/A10 AD_B0_12 0x02, // 25/A11 AD_B0_13 0x83, // 26/A12 AD_B1_14 - only on ADC2, 3 0x84, // 27/A13 AD_B1_15 - only on ADC2, 4 0x07, // 14/A0 AD_B1_02 0x08, // 15/A1 AD_B1_03 0x0C, // 16/A2 AD_B1_07 0x0B, // 17/A3 AD_B1_06 0x06, // 18/A4 AD_B1_01 0x05, // 19/A5 AD_B1_00 0x0F, // 20/A6 AD_B1_10 0x00, // 21/A7 AD_B1_11 0x0D, // 22/A8 AD_B1_08 0x0E, // 23/A9 AD_B1_09 0x01, // 24/A10 AD_B0_12 0x02, // 25/A11 AD_B0_13 0x83, // 26/A12 AD_B1_14 - only on ADC2, 3 0x84, // 27/A13 AD_B1_15 - only on ADC2, 4 #ifdef ARDUINO_TEENSY41 0xFF, // 28 0xFF, // 29 0xFF, // 30 0xFF, // 31 0xFF, // 32 0xFF, // 33 0xFF, // 34 0xFF, // 35 0xFF, // 36 0xFF, // 37 0x81, // 38/A14 AD_B1_12 - only on ADC2, 1 0x82, // 39/A15 AD_B1_13 - only on ADC2, 2 0x09, // 40/A16 AD_B1_04 0x0A, // 41/A17 AD_B1_05 #endif }; const uint16_t pin = pin2sc1a[adc_pin]; if (pin == 0xFF) { adc_select = -1; // Digital only } else if (pin & 0x80) { adc_select = 1; ADC2_HC0 = pin & 0x7F; } else { adc_select = 0; ADC1_HC0 = pin; } } uint16_t MarlinHAL::adc_value() { switch (adc_select) { case 0: while (!(ADC1_HS & ADC_HS_COCO0)) { /* wait */ } return ADC1_R0; case 1: while (!(ADC2_HS & ADC_HS_COCO0)) { /* wait */ } return ADC2_R0; } return 0; } // ------------------------ // Free Memory Accessor // ------------------------ #define __bss_end _ebss extern "C" { extern char __bss_end; extern char __heap_start; extern void* __brkval; // Doesn't work on Teensy 4.x uint32_t freeMemory() { uint32_t free_memory; free_memory = ((uint32_t)&free_memory) - (((uint32_t)__brkval) ?: ((uint32_t)&__bss_end)); return free_memory; } } #endif // __IMXRT1062__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/HAL.cpp
C++
agpl-3.0
5,635
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A) */ #define CPU_32_BIT #include "../shared/Marduino.h" #include "../shared/math_32bit.h" #include "../shared/HAL_SPI.h" #include "fastio.h" #include <stdint.h> #include <util/atomic.h> #if HAS_ETHERNET #include "../../feature/ethernet.h" #endif // ------------------------ // Defines // ------------------------ #define IS_32BIT_TEENSY 1 #define IS_TEENSY_40_41 1 #ifndef IS_TEENSY40 #define IS_TEENSY41 1 #endif #define CPU_ST7920_DELAY_1 600 #define CPU_ST7920_DELAY_2 750 #define CPU_ST7920_DELAY_3 750 #undef sq #define sq(x) ((x)*(x)) // Don't place string constants in PROGMEM #undef PSTR #define PSTR(str) ({static const char *data = (str); &data[0];}) // ------------------------ // Serial ports // ------------------------ #include "../../core/serial_hook.h" #define Serial0 Serial #define _DECLARE_SERIAL(X) \ typedef ForwardSerial1Class<decltype(Serial##X)> DefaultSerial##X; \ extern DefaultSerial##X MSerial##X #define DECLARE_SERIAL(X) _DECLARE_SERIAL(X) typedef ForwardSerial1Class<decltype(SerialUSB)> USBSerialType; extern USBSerialType USBSerial; #define _MSERIAL(X) MSerial##X #define MSERIAL(X) _MSERIAL(X) #if SERIAL_PORT == -1 #define MYSERIAL1 USBSerial #elif WITHIN(SERIAL_PORT, 0, 8) DECLARE_SERIAL(SERIAL_PORT); #define MYSERIAL1 MSERIAL(SERIAL_PORT) #else #error "The required SERIAL_PORT must be from 0 to 8, or -1 for Native USB." #endif #ifdef SERIAL_PORT_2 #if SERIAL_PORT_2 == -1 #define MYSERIAL2 USBSerial #elif SERIAL_PORT_2 == -2 #define MYSERIAL2 ethernet.telnetClient #elif WITHIN(SERIAL_PORT_2, 0, 8) DECLARE_SERIAL(SERIAL_PORT_2); #define MYSERIAL2 MSERIAL(SERIAL_PORT_2) #else #error "SERIAL_PORT_2 must be from 0 to 8, or -1 for Native USB, or -2 for Ethernet." #endif #endif #ifdef SERIAL_PORT_3 #if SERIAL_PORT_3 == -1 #define MYSERIAL3 USBSerial #elif WITHIN(SERIAL_PORT_3, 0, 8) DECLARE_SERIAL(SERIAL_PORT_3); #define MYSERIAL3 MSERIAL(SERIAL_PORT_3) #else #error "SERIAL_PORT_3 must be from 0 to 8, or -1 for Native USB." #endif #endif // ------------------------ // Types // ------------------------ class libServo; typedef libServo hal_servo_t; typedef int8_t pin_t; // ------------------------ // Interrupts // ------------------------ #define CRITICAL_SECTION_START() const bool irqon = !__get_primask(); __disable_irq() #define CRITICAL_SECTION_END() if (irqon) __enable_irq() // ------------------------ // ADC // ------------------------ #ifndef analogInputToDigitalPin #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1) #endif #define HAL_ADC_VREF_MV 3300 #define HAL_ADC_RESOLUTION 10 #define HAL_ADC_FILTERED // turn off ADC oversampling // // Pin Mapping for M42, M43, M226 // #define GET_PIN_MAP_PIN(index) index #define GET_PIN_MAP_INDEX(pin) pin #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval) // FastIO bool is_output(pin_t pin); // ------------------------ // Free Memory Accessor // ------------------------ #pragma GCC diagnostic push #if GCC_VERSION <= 50000 #pragma GCC diagnostic ignored "-Wunused-function" #endif extern "C" uint32_t freeMemory(); #pragma GCC diagnostic pop // ------------------------ // MarlinHAL Class // ------------------------ class MarlinHAL { public: // Earliest possible init, before setup() MarlinHAL() {} // Watchdog static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {}); static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {}); static void init() {} // Called early in setup() static void init_board() {} // Called less early in setup() static void reboot(); // Restart the firmware from 0x0 // Interrupts static bool isr_state() { return !__get_primask(); } static void isr_on() { __enable_irq(); } static void isr_off() { __disable_irq(); } static void delay_ms(const int ms) { delay(ms); } // Tasks, called from idle() static void idletask() {} // Reset static uint8_t get_reset_source(); static void clear_reset_source(); // Free SRAM static int freeMemory() { return ::freeMemory(); } // // ADC Methods // static int8_t adc_select; // Called by Temperature::init once at startup static void adc_init(); // Called by Temperature::init for each sensor at startup static void adc_enable(const pin_t pin) {} // Begin ADC sampling on the given pin. Called from Temperature::isr! static void adc_start(const pin_t pin); // Is the ADC ready for reading? static bool adc_ready() { return true; } // The current value of the ADC register static uint16_t adc_value(); /** * Set the PWM duty cycle for the pin to the given value. * No option to invert the duty cycle [default = false] * No option to change the scale of the provided value to enable finer PWM duty control [default = 255] */ static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); } };
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/HAL.h
C++
agpl-3.0
5,934
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL SPI for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A) */ #ifdef __IMXRT1062__ #include "../../inc/MarlinConfig.h" #include "HAL.h" #include <SPI.h> #include <pins_arduino.h> #include "spi_pins.h" static SPISettings spiConfig; // ------------------------ // Public functions // ------------------------ #if ENABLED(SOFTWARE_SPI) // ------------------------ // Software SPI // ------------------------ #error "Software SPI not supported for Teensy 4. Use Hardware SPI." #else // ------------------------ // Hardware SPI // ------------------------ void spiBegin() { #if PIN_EXISTS(SD_SS) OUT_WRITE(SD_SS_PIN, HIGH); #endif //SET_OUTPUT(SD_SCK_PIN); //SET_INPUT(SD_MISO_PIN); //SET_OUTPUT(SD_MOSI_PIN); #if 0 && DISABLED(SOFTWARE_SPI) // set SS high - may be chip select for another SPI device #if SET_SPI_SS_HIGH WRITE(SD_SS_PIN, HIGH); #endif // set a default rate spiInit(SPI_HALF_SPEED); // 1 #endif } void spiInit(uint8_t spiRate) { // Use Marlin data-rates uint32_t clock; switch (spiRate) { case SPI_FULL_SPEED: clock = 10000000; break; case SPI_HALF_SPEED: clock = 5000000; break; case SPI_QUARTER_SPEED: clock = 2500000; break; case SPI_EIGHTH_SPEED: clock = 1250000; break; case SPI_SPEED_5: clock = 625000; break; case SPI_SPEED_6: clock = 312500; break; default: clock = 4000000; // Default from the SPI library } spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0); SPI.begin(); } uint8_t spiRec() { SPI.beginTransaction(spiConfig); uint8_t returnByte = SPI.transfer(0xFF); SPI.endTransaction(); return returnByte; //SPDR = 0xFF; //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //return SPDR; } void spiRead(uint8_t *buf, uint16_t nbyte) { SPI.beginTransaction(spiConfig); SPI.transfer(buf, nbyte); SPI.endTransaction(); //if (nbyte-- == 0) return; // SPDR = 0xFF; //for (uint16_t i = 0; i < nbyte; i++) { // while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } // buf[i] = SPDR; // SPDR = 0xFF; //} //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } //buf[nbyte] = SPDR; } void spiSend(uint8_t b) { SPI.beginTransaction(spiConfig); SPI.transfer(b); SPI.endTransaction(); //SPDR = b; //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ } } void spiSendBlock(uint8_t token, const uint8_t *buf) { SPI.beginTransaction(spiConfig); SPDR = token; for (uint16_t i = 0; i < 512; i += 2) { while (!TEST(SPSR, SPIF)) { /* nada */ }; SPDR = buf[i]; while (!TEST(SPSR, SPIF)) { /* nada */ }; SPDR = buf[i + 1]; } while (!TEST(SPSR, SPIF)) { /* nada */ }; SPI.endTransaction(); } // Begin SPI transaction, set clock, bit order, data mode void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { spiConfig = SPISettings(spiClock, bitOrder, dataMode); SPI.beginTransaction(spiConfig); } #endif // SOFTWARE_SPI #endif // __IMXRT1062__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/HAL_SPI.cpp
C++
agpl-3.0
3,887
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <SPI.h> using MarlinSPI = SPIClass;
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/MarlinSPI.h
C
agpl-3.0
922
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL Servo for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A) */ #ifdef __IMXRT1062__ #include "../../inc/MarlinConfig.h" #if HAS_SERVOS #include "Servo.h" int8_t libServo::attach(const int inPin) { if (inPin > 0) servoPin = inPin; return super::attach(servoPin); } int8_t libServo::attach(const int inPin, const int inMin, const int inMax) { if (inPin > 0) servoPin = inPin; return super::attach(servoPin, inMin, inMax); } void libServo::move(const int value) { constexpr uint16_t servo_delay[] = SERVO_DELAY; static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long."); if (attach(0) >= 0) { write(value); safe_delay(servo_delay[servoIndex]); TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach()); } } void libServo::detach() { // PWMServo library does not have detach() function //super::detach(); } #endif // HAS_SERVOS #endif // __IMXRT1062__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/Servo.cpp
C++
agpl-3.0
1,792
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL Servo for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A) */ #include <PWMServo.h> // Inherit and expand on core Servo library class libServo : public PWMServo { public: int8_t attach(const int pin); int8_t attach(const int pin, const int min, const int max); void move(const int value); void detach(void); private: typedef PWMServo super; uint8_t servoPin; uint16_t min_ticks; uint16_t max_ticks; uint8_t servoIndex; // Index into the channel data for this servo };
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/Servo.h
C++
agpl-3.0
1,400
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __IMXRT1062__ /** * HAL PersistentStore for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A) */ #include "../../inc/MarlinConfig.h" #if USE_WIRED_EEPROM #include "../shared/eeprom_api.h" #include <avr/eeprom.h> #ifndef MARLIN_EEPROM_SIZE #define MARLIN_EEPROM_SIZE size_t(E2END + 1) #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } bool PersistentStore::access_start() { return true; } bool PersistentStore::access_finish() { return true; } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { uint16_t written = 0; while (size--) { uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos); uint8_t v = *value; if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed! eeprom_write_byte(p, v); if (++written & 0x7F) delay(2); else safe_delay(2); // Avoid triggering watchdog during long EEPROM writes if (eeprom_read_byte(p) != v) { SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE); return true; } } crc16(crc, &v, 1); pos++; value++; } return false; } bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) { do { const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos)); if (writing) *value = c; crc16(crc, &c, 1); pos++; value++; } while (--size); return false; } #endif // USE_WIRED_EEPROM #endif // __IMXRT1062__
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/eeprom.cpp
C++
agpl-3.0
2,410
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL Endstop Interrupts for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A) * * Without endstop interrupts the endstop pins must be polled continually in * the temperature-ISR via endstops.update(), most of the time finding no change. * With this feature endstops.update() is called only when we know that at * least one endstop has changed state, saving valuable CPU cycles. * * This feature only works when all used endstop pins can generate an 'external interrupt'. * * Test whether pins issue interrupts on your board by flashing 'pin_interrupt_test.ino'. * (Located in Marlin/buildroot/share/pin_interrupt_test/pin_interrupt_test.ino) */ #include "../../module/endstops.h" // One ISR for all EXT-Interrupts void endstop_ISR() { endstops.update(); } /** * Endstop interrupts for Due based targets. * On Due, all pins support external interrupt capability. */ void setup_endstop_interrupts() { #define _ATTACH(P) attachInterrupt(digitalPinToInterrupt(P), endstop_ISR, CHANGE) TERN_(USE_X_MAX, _ATTACH(X_MAX_PIN)); TERN_(USE_X_MIN, _ATTACH(X_MIN_PIN)); TERN_(USE_Y_MAX, _ATTACH(Y_MAX_PIN)); TERN_(USE_Y_MIN, _ATTACH(Y_MIN_PIN)); TERN_(USE_Z_MAX, _ATTACH(Z_MAX_PIN)); TERN_(USE_Z_MIN, _ATTACH(Z_MIN_PIN)); TERN_(USE_X2_MAX, _ATTACH(X2_MAX_PIN)); TERN_(USE_X2_MIN, _ATTACH(X2_MIN_PIN)); TERN_(USE_Y2_MAX, _ATTACH(Y2_MAX_PIN)); TERN_(USE_Y2_MIN, _ATTACH(Y2_MIN_PIN)); TERN_(USE_Z2_MAX, _ATTACH(Z2_MAX_PIN)); TERN_(USE_Z2_MIN, _ATTACH(Z2_MIN_PIN)); TERN_(USE_Z3_MAX, _ATTACH(Z3_MAX_PIN)); TERN_(USE_Z3_MIN, _ATTACH(Z3_MIN_PIN)); TERN_(USE_Z4_MAX, _ATTACH(Z4_MAX_PIN)); TERN_(USE_Z4_MIN, _ATTACH(Z4_MIN_PIN)); TERN_(USE_Z_MIN_PROBE, _ATTACH(Z_MIN_PROBE_PIN)); TERN_(USE_I_MAX, _ATTACH(I_MAX_PIN)); TERN_(USE_I_MIN, _ATTACH(I_MIN_PIN)); TERN_(USE_J_MAX, _ATTACH(J_MAX_PIN)); TERN_(USE_J_MIN, _ATTACH(J_MIN_PIN)); TERN_(USE_K_MAX, _ATTACH(K_MAX_PIN)); TERN_(USE_K_MIN, _ATTACH(K_MIN_PIN)); TERN_(USE_U_MAX, _ATTACH(U_MAX_PIN)); TERN_(USE_U_MIN, _ATTACH(U_MIN_PIN)); TERN_(USE_V_MAX, _ATTACH(V_MAX_PIN)); TERN_(USE_V_MIN, _ATTACH(V_MIN_PIN)); TERN_(USE_W_MAX, _ATTACH(W_MAX_PIN)); TERN_(USE_W_MIN, _ATTACH(W_MIN_PIN)); }
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/endstop_interrupts.h
C
agpl-3.0
3,235
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Fast I/O interfaces for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A) * These use GPIO functions instead of Direct Port Manipulation, as on AVR. */ #ifndef PWM #define PWM OUTPUT #endif #define READ(IO) digitalRead(IO) #define WRITE(IO,V) digitalWrite(IO,V) #define _GET_MODE(IO) !is_output(IO) #define _SET_MODE(IO,M) pinMode(IO, M) #define _SET_OUTPUT(IO) pinMode(IO, OUTPUT) /*!< Output Push Pull Mode & GPIO_NOPULL */ #define OUT_WRITE(IO,V) do{ _SET_OUTPUT(IO); WRITE(IO,V); }while(0) #define SET_INPUT(IO) _SET_MODE(IO, INPUT) /*!< Input Floating Mode */ #define SET_INPUT_PULLUP(IO) _SET_MODE(IO, INPUT_PULLUP) /*!< Input with Pull-up activation */ #define SET_INPUT_PULLDOWN(IO) _SET_MODE(IO, INPUT_PULLDOWN) /*!< Input with Pull-down activation */ #define SET_OUTPUT(IO) OUT_WRITE(IO, LOW) #define SET_PWM(IO) _SET_MODE(IO, PWM) #define TOGGLE(IO) OUT_WRITE(IO, !READ(IO)) #define IS_INPUT(IO) !is_output(IO) #define IS_OUTPUT(IO) is_output(IO) #define PWM_PIN(P) digitalPinHasPWM(P) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V)
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/fastio.h
C
agpl-3.0
2,295
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/inc/Conditionals_LCD.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/inc/Conditionals_adv.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if USE_FALLBACK_EEPROM #define USE_WIRED_EEPROM 1 #endif
2301_81045437/Marlin
Marlin/src/HAL/TEENSY40_41/inc/Conditionals_post.h
C
agpl-3.0
936