diff --git a/src/FSMCIO.cpp b/src/FSMCIO.cpp index 2965c54..6e1bd9c 100644 --- a/src/FSMCIO.cpp +++ b/src/FSMCIO.cpp @@ -4,6 +4,8 @@ #define STM32F412Zx #endif +#ifdef STM32F412Zx + #include "FSMCIO.h" #include "dma.h" @@ -135,3 +137,5 @@ void FSMCIO::startSend(const void *txBuffer, uint32_t txSize, PVoidCallback done } } // namespace codal + +#endif \ No newline at end of file diff --git a/src/STMLowLevelTimer.cpp b/src/STMLowLevelTimer.cpp index 770a82d..18f99db 100644 --- a/src/STMLowLevelTimer.cpp +++ b/src/STMLowLevelTimer.cpp @@ -39,7 +39,8 @@ void timer_irq_handler(uint8_t index) __HAL_TIM_CLEAR_IT(timHandle, TIM_IT_CC4); } - instances[index]->timer_pointer(channel_bitmsk); + if (instances[index]->timer_pointer) + instances[index]->timer_pointer(channel_bitmsk); } extern "C" void TIM1_IRQHandler() @@ -70,6 +71,7 @@ STMLowLevelTimer::STMLowLevelTimer(TIM_TypeDef* timer, uint8_t irqn) : LowLevelT this->timer_instance = timer; this->irqN = irqn; memset(&TimHandle, 0, sizeof(TIM_HandleTypeDef)); + disableIRQ(); disableIRQ(); // otherwise it might hit us while we're initializing diff --git a/src/USB.cpp b/src/USB.cpp index dff0d41..1af3057 100644 --- a/src/USB.cpp +++ b/src/USB.cpp @@ -88,7 +88,7 @@ void usb_configure(uint8_t numEndpoints) #ifdef USB __HAL_RCC_USB_CLK_ENABLE(); - irqn = USB_IRQn; + irqn = USB_HP_CAN1_TX_IRQn; pcd.Init.speed = PCD_SPEED_FULL; #else /* USB_OTG_FS */ __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); @@ -112,8 +112,14 @@ void usb_configure(uint8_t numEndpoints) HAL_PCDEx_SetTxFiFo(&pcd, i, FIFO_EP_WORDS); #endif /* USB */ + // USB_HP_CAN1_TX_IRQn + // USB_LP_CAN1_RX_IRQn + +NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 5); + NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); NVIC_SetPriority(irqn, 5); NVIC_EnableIRQ(irqn); + } #if 0 @@ -150,6 +156,8 @@ static UsbEndpointOut *findOutEp(int ep) extern "C" { + void USB_LP_IRQHandler(void) { HAL_PCD_IRQHandler(&pcd); } + void USB_HP_IRQHandler(void) { HAL_PCD_IRQHandler(&pcd); } void OTG_FS_IRQHandler(void) { HAL_PCD_IRQHandler(&pcd); } @@ -337,25 +345,33 @@ static void writeEP(uint8_t *data, uint8_t ep, int len) { usb_assert(len <= USB_MAX_PKT_SIZE); uint32_t len32b = (len + 3) / 4; - +#ifndef USB DBG("%d write space: %d words", (int)codal::system_timer_current_time_us(), USBx_INEP(ep)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV); while ((USBx_INEP(ep)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) < len32b) ; - DBG("write: %p len=%d at IN %d", data, len, ep); + DBG("write: %p len=%d at IN %d", data, len, ep); CHK(HAL_PCD_EP_Transmit(&pcd, ep | 0x80, data, len)); if (len) CHK(USB_WritePacket(pcd.Instance, data, ep, len, 0)); +#else + DBG("write: %p len=%d at IN %d", data, len, ep); + CHK(HAL_PCD_EP_Transmit(&pcd, ep | 0x80, data, len)); + + if (len) + CHK(USB_WritePacket(pcd.Instance, data, ep, len)); +#endif +#ifndef USB USBx_DEVICE->DIEPEMPMSK &= ~0x1U << ep; while (!(USB_ReadDevInEPInterrupt(pcd.Instance, ep) & USB_OTG_DIEPINT_XFRC)) ; CLEAR_IN_EP_INTR(ep, USB_OTG_DIEPINT_XFRC); - +#endif DBG("%d write done, ctl=%p", (int)codal::system_timer_current_time_us(), USBx_OUTEP(0)->DOEPCTL); diff --git a/src/ZSingleWireSerial.cpp b/src/ZSingleWireSerial.cpp index 40fd3a1..2ffc22a 100644 --- a/src/ZSingleWireSerial.cpp +++ b/src/ZSingleWireSerial.cpp @@ -30,6 +30,7 @@ static ZSingleWireSerial *instances[4]; static int enable_clock(uint32_t instance) { + DMESG("CLK EN"); switch (instance) { case USART1_BASE: @@ -42,6 +43,11 @@ static int enable_clock(uint32_t instance) NVIC_SetPriority(USART2_IRQn, 2); NVIC_EnableIRQ(USART2_IRQn); return HAL_RCC_GetPCLK1Freq(); + case USART3_BASE: + __HAL_RCC_USART3_CLK_ENABLE(); + DMESG("PCLK1 %d", HAL_RCC_GetPCLK1Freq()); + NVIC_EnableIRQ(USART3_IRQn); + return HAL_RCC_GetPCLK1Freq(); #ifdef USART6_BASE case USART6_BASE: __HAL_RCC_USART6_CLK_ENABLE(); @@ -116,6 +122,7 @@ extern "C" void HAL_UART_ErrorCallback(UART_HandleTypeDef *hspi) DEFIRQ(USART1_IRQHandler, USART1_BASE) DEFIRQ(USART2_IRQHandler, USART2_BASE) +DEFIRQ(USART3_IRQHandler, USART3_BASE) #ifdef USART6_BASE DEFIRQ(USART6_IRQHandler, USART6_BASE) #endif @@ -197,8 +204,9 @@ int ZSingleWireSerial::configureTx(int enable) if (enable && !(status & TX_CONFIGURED)) { uint8_t pin = (uint8_t)p.name; - pin_mode(pin, PullNone); + // pin_mode(pin, PullNone); pin_function(pin, pinmap_function(pin, PinMap_UART_TX)); + pin_mode(pin,PullUp); uart.Init.Mode = UART_MODE_TX; HAL_HalfDuplex_Init(&uart); status |= TX_CONFIGURED; @@ -291,6 +299,7 @@ int ZSingleWireSerial::sendDMA(uint8_t* data, int len) this->bufLen = len; int res = HAL_UART_Transmit_DMA(&uart, data, len); + DMESG("TXDMA RES %d ",res); CODAL_ASSERT(res == HAL_OK, DEVICE_HARDWARE_CONFIGURATION_ERROR); @@ -326,8 +335,11 @@ int ZSingleWireSerial::getBytesReceived() { if (!(status & RX_CONFIGURED)) return DEVICE_INVALID_PARAMETER; - +#ifdef STM32F1 + return hdma_rx.Instance->CNDTR; +#else return hdma_rx.Instance->NDTR; +#endif } int ZSingleWireSerial::getBytesTransmitted() @@ -335,7 +347,11 @@ int ZSingleWireSerial::getBytesTransmitted() if (!(status & TX_CONFIGURED)) return DEVICE_INVALID_PARAMETER; +#ifdef STM32F1 + return hdma_tx.Instance->CNDTR; +#else return hdma_tx.Instance->NDTR; +#endif } int ZSingleWireSerial::sendBreak() diff --git a/src/codal_target_hal.cpp b/src/codal_target_hal.cpp index c6b6489..f1befa9 100644 --- a/src/codal_target_hal.cpp +++ b/src/codal_target_hal.cpp @@ -55,7 +55,11 @@ int target_random(int max) device and in any context. These bits can never be altered by the user. The 96-bit unique device identifier can also be read in single bytes/half-words/words in different ways and then be concatenated using a custom algorithm. */ +#ifdef STM32F1 +#define STM32_UUID ((uint32_t *)0x1FFFF7E8) +#else #define STM32_UUID ((uint32_t *)0x1FFF7A10) +#endif uint64_t target_get_serial() { // uuid[1] is the wafer number plus the lot number, need to check the uniqueness of this... diff --git a/src/dma_f1.c b/src/dma_f1.c index bf3b794..156b5c2 100644 --- a/src/dma_f1.c +++ b/src/dma_f1.c @@ -1,8 +1,9 @@ #include "dma.h" #include "CodalDmesg.h" +#include "ErrorNo.h" #ifdef STM32F1 - +#include "stm32f1xx.h" #define NUM_STREAMS 7 #define NUM_DMA 2 @@ -61,7 +62,7 @@ MBED_WEAK const DmaMap TheDmaMap[] = // {UART4_BASE, DMA_RX, 13}, {UART4_BASE, DMA_TX, 15}, {DAC_BASE, DMA_TX, 13}, -#endif +#endif // The end {0, 0, 0}}; @@ -71,7 +72,6 @@ static DMA_HandleTypeDef *handles[7 + 5]; #define HANDLE_IDX(id) (id < 10 ? id - 1 : id - 4) static void irq_callback(int id) { - LOG("DMA irq %d", id); id = HANDLE_IDX(id); if (handles[id]) HAL_DMA_IRQHandler(handles[id]); @@ -97,6 +97,23 @@ void DMA2_Channel4_5_IRQHandler() irq_callback(15); } +int lookup_dma(uint32_t peripheral, uint8_t rxdx) +{ + int id = -1; + const DmaMap *map; + + for (map = TheDmaMap; map->peripheral; map++) + { + if (map->peripheral == peripheral && map->rxdx == rxdx) + { + id = HANDLE_IDX(map->channel); + break; + } + } + + return id; +} + int dma_init(uint32_t peripheral, uint8_t rxdx, DMA_HandleTypeDef *obj, int flags) { memset(obj, 0, sizeof(*obj)); @@ -117,7 +134,7 @@ int dma_init(uint32_t peripheral, uint8_t rxdx, DMA_HandleTypeDef *obj, int flag } } - CODAL_ASSERT(map->peripheral); + CODAL_ASSERT(map->peripheral, DEVICE_HARDWARE_CONFIGURATION_ERROR); obj->Instance = streams[id].instance; @@ -150,7 +167,7 @@ int dma_init(uint32_t peripheral, uint8_t rxdx, DMA_HandleTypeDef *obj, int flag #endif int res = HAL_DMA_Init(obj); - CODAL_ASSERT(res == HAL_OK); + CODAL_ASSERT(res == HAL_OK, DEVICE_HARDWARE_CONFIGURATION_ERROR); LOG("DMA init %p irq=%d ch=%d str=%d", obj->Instance, streams[id].irqn, map->channel, map->stream); @@ -160,4 +177,15 @@ int dma_init(uint32_t peripheral, uint8_t rxdx, DMA_HandleTypeDef *obj, int flag return 0; } +void dma_set_irq_priority(uint32_t peripheral, uint8_t rxdx, int priority) +{ + int id = lookup_dma(peripheral, rxdx); + + CODAL_ASSERT(id != -1, DEVICE_HARDWARE_CONFIGURATION_ERROR); + + NVIC_DisableIRQ(streams[id].irqn); + NVIC_SetPriority(streams[id].irqn, priority); + NVIC_EnableIRQ(streams[id].irqn); +} + #endif \ No newline at end of file diff --git a/src/pinmap.c b/src/pinmap.c index c83934d..6430f1c 100644 --- a/src/pinmap.c +++ b/src/pinmap.c @@ -30,6 +30,7 @@ #include "pinmap.h" //#include "PortNames.h" #include "pin_device.h" +#include "CodalDmesg.h" #define error MBED_ERROR