diff --git a/examples/pic32cz/Makefile b/examples/pic32cz/Makefile index 004e268..551ff3c 100644 --- a/examples/pic32cz/Makefile +++ b/examples/pic32cz/Makefile @@ -12,6 +12,7 @@ SOURCE += $(wildcard $(WHAL_DIR)/src/*.c) SOURCE += $(wildcard $(WHAL_DIR)/src/*/gpio.c) SOURCE += $(wildcard $(WHAL_DIR)/src/*/clock.c) +SOURCE += $(wildcard $(WHAL_DIR)/src/*/uart.c) SOURCE += $(wildcard $(WHAL_DIR)/src/*/timer.c) SOURCE += $(wildcard $(WHAL_DIR)/src/*/supply.c) SOURCE += $(wildcard $(WHAL_DIR)/src/*/pic32cz_*.c) diff --git a/examples/pic32cz/main.c b/examples/pic32cz/main.c index 99fb5ac..ae50a1c 100644 --- a/examples/pic32cz/main.c +++ b/examples/pic32cz/main.c @@ -36,13 +36,24 @@ void WaitMs(size_t ms) void main(void) { whal_Error err; + uint8_t data[] = "Hello world!\r\n"; - err = whal_Clock_Init(&clock); + err = whal_Clock_Init(&g_whalClock); if (err) { goto loop; } - err = whal_Gpio_Init(&gpio); + err = whal_Gpio_Init(&g_whalGpio); + if (err) { + goto loop; + } + + err = whal_Uart_Init(&g_whalUart); + if (err) { + goto loop; + } + + err = whal_Uart_Send(&g_whalUart, data, sizeof(data)); if (err) { goto loop; } @@ -55,14 +66,15 @@ void main(void) whal_Timer_Start(&g_whalTimer); while (1) { - whal_Gpio_Set(&gpio, 0, 1); + whal_Gpio_Set(&g_whalGpio, 0, 1); WaitMs(1000); - whal_Gpio_Set(&gpio, 0, 0); + + whal_Gpio_Set(&g_whalGpio, 0, 0); + WaitMs(1000); } - loop: while (1); } diff --git a/examples/pic32cz/pic32cz_curiosity_ultra.c b/examples/pic32cz/pic32cz_curiosity_ultra.c index 0428168..d638392 100644 --- a/examples/pic32cz/pic32cz_curiosity_ultra.c +++ b/examples/pic32cz/pic32cz_curiosity_ultra.c @@ -1,17 +1,17 @@ #include #include -whal_Supply supply = { +whal_Supply g_whalSupply = { WHAL_PIC32CZ_SUPPLY_DEVICE, }; -whal_Clock clock = { +whal_Clock g_whalClock = { WHAL_PIC32CZ_CLOCK_PLL_DEVICE, .cfg = &(whal_Pic32czClock_Cfg) { /* 300MHz clock */ .oscCtrlCfg = &(whal_Pic32czClockPll_OscCtrlCfg) { - .supplyCtrl = &supply, + .supplyCtrl = &g_whalSupply, .supply = &(whal_Pic32czSupc_Supply){WHAL_PIC32CZ_SUPPLY_PLL}, .pllInst = WHAL_PIC32CZ_PLL0, @@ -40,25 +40,51 @@ whal_Clock clock = { }, }; -whal_Gpio gpio = { +whal_Gpio g_whalGpio = { WHAL_PIC32CZ_GPIO_DEVICE, .cfg = &(whal_Pic32czGpio_Cfg) { - .pinCfgCount = 1, - .pinCfg = &(whal_Pic32czGpio_PinCfg) { - .port = 1, - .pin = 21, - .dir = WHAL_PIC32CZ_DIR_OUTPUT, - .out = 0, + .pinCfgCount = 3, + .pinCfg = (whal_Pic32czGpio_PinCfg[]) { + { /* LED */ + .port = 1, + .pin = 21, + .dir = WHAL_PIC32CZ_DIR_OUTPUT, + .out = 0, + }, + { /* UART TX */ + .port = 2, + .pin = 21, + .pmuxEn = 1, + .pmux = WHAL_PIC32CZ_PMUX_SERCOM_ALT, + }, + { /* UART RX */ + .port = 2, + .pin = 22, + .pmuxEn = 1, + .pmux = WHAL_PIC32CZ_PMUX_SERCOM_ALT, + }, }, }, }; -whal_Pic32czClock_Clk periphClock = { - .gclkPeriphChannel = 0, - .gclkPeriphSrc = 0, - .mclkEnableInst = 0, - .mclkEnableMask = 0, +static whal_Pic32czClock_Clk uartClk = { + .gclkPeriphChannel = 25, /* SERCOM 4 */ + .gclkPeriphSrc = 0, /* GEN 0 */ + .mclkEnableInst = 1, /* Peripheral BUS Clock Enable Mask1 Register */ + .mclkEnableMask = WHAL_MASK(3), /* SERCOM 4 enable mask */ +}; + +whal_Uart g_whalUart = { + WHAL_PIC32CZ_SERCOM4_UART_DEVICE, + + .cfg = &(whal_Pic32czUart_Cfg) { + .clkCtrl = &g_whalClock, + .clk = &uartClk, + .baud = WHAL_PIC32CZ_UART_BAUD(115200, 300000000), + .txPad = WHAL_PIC32CZ_UART_TXPO_PAD0, + .rxPad = WHAL_PIC32CZ_UART_RXPO_PAD1, + }, }; whal_Timer g_whalTimer = { diff --git a/examples/pic32cz/pic32cz_curiosity_ultra.h b/examples/pic32cz/pic32cz_curiosity_ultra.h index 7b22621..1142d34 100644 --- a/examples/pic32cz/pic32cz_curiosity_ultra.h +++ b/examples/pic32cz/pic32cz_curiosity_ultra.h @@ -4,9 +4,10 @@ #include #include -extern whal_Supply supply; -extern whal_Clock clock; -extern whal_Gpio gpio; +extern whal_Supply g_whalSupply; +extern whal_Clock g_whalClock; +extern whal_Gpio g_whalGpio; extern whal_Timer g_whalTimer; +extern whal_Uart g_whalUart; #endif /* WHAL_PIC32CZ_CURIOSITY_ULTRA */ diff --git a/src/uart/pic32cz_uart.c b/src/uart/pic32cz_uart.c index 872378f..0b67a84 100644 --- a/src/uart/pic32cz_uart.c +++ b/src/uart/pic32cz_uart.c @@ -97,8 +97,6 @@ whal_Error whal_Pic32czUart_Init(whal_Uart *uartDev) whal_Error err; whal_Pic32czUart_Cfg *cfg; const whal_Regmap *reg; - size_t clockFreq; - uint16_t baudVal; if (!uartDev) { return WHAL_EINVAL; @@ -113,27 +111,6 @@ whal_Error whal_Pic32czUart_Init(whal_Uart *uartDev) return err; } - /* Get clock frequency for baud rate calculation */ - err = whal_Clock_GetRate(cfg->clkCtrl, &clockFreq); - if (err != WHAL_SUCCESS) { - return err; - } - - /* Software reset the SERCOM */ - whal_Reg_Update(reg->base, SERCOM_USART_CTRLA_REG, - SERCOM_USART_CTRLA_SWRST, - whal_SetBits(SERCOM_USART_CTRLA_SWRST, 1)); - - /* Wait for reset to complete */ - whal_Pic32czUart_WaitSync(reg, SERCOM_USART_SYNCBUSY_SWRST); - - /* - * Calculate baud value for 16x oversampling arithmetic mode: - * BAUD = 65536 * (1 - 16 * (f_baud / f_ref)) - * = 65536 - (65536 * 16 * f_baud) / f_ref - */ - baudVal = (uint16_t)(65536UL - ((65536UL * 16UL * cfg->baud) / clockFreq)); - /* Configure CTRLA: internal clock, async mode, LSB first, 16x sampling */ whal_Reg_Update(reg->base, SERCOM_USART_CTRLA_REG, SERCOM_USART_CTRLA_MODE_MASK | @@ -166,7 +143,7 @@ whal_Error whal_Pic32czUart_Init(whal_Uart *uartDev) /* Set baud rate */ whal_Reg_Update(reg->base, SERCOM_USART_BAUD_REG, 0xFFFF, - baudVal); + cfg->baud); /* Enable transmitter and receiver */ whal_Reg_Update(reg->base, SERCOM_USART_CTRLB_REG, diff --git a/wolfHAL/gpio/pic32cz_gpio.h b/wolfHAL/gpio/pic32cz_gpio.h index 68bbffa..9b9956e 100644 --- a/wolfHAL/gpio/pic32cz_gpio.h +++ b/wolfHAL/gpio/pic32cz_gpio.h @@ -35,20 +35,21 @@ enum { * I/O Multiplexing table for valid assignments. */ enum { - WHAL_PIC32CZ_PMUX_A = 0x0, /* Peripheral function A */ - WHAL_PIC32CZ_PMUX_B = 0x1, /* Peripheral function B */ - WHAL_PIC32CZ_PMUX_C = 0x2, /* Peripheral function C */ - WHAL_PIC32CZ_PMUX_D = 0x3, /* Peripheral function D */ - WHAL_PIC32CZ_PMUX_E = 0x4, /* Peripheral function E */ - WHAL_PIC32CZ_PMUX_F = 0x5, /* Peripheral function F */ - WHAL_PIC32CZ_PMUX_G = 0x6, /* Peripheral function G */ - WHAL_PIC32CZ_PMUX_H = 0x7, /* Peripheral function H */ - WHAL_PIC32CZ_PMUX_I = 0x8, /* Peripheral function I */ - WHAL_PIC32CZ_PMUX_J = 0x9, /* Peripheral function J */ - WHAL_PIC32CZ_PMUX_K = 0xA, /* Peripheral function K */ - WHAL_PIC32CZ_PMUX_L = 0xB, /* Peripheral function L */ - WHAL_PIC32CZ_PMUX_M = 0xC, /* Peripheral function M */ - WHAL_PIC32CZ_PMUX_N = 0xD, /* Peripheral function N */ + WHAL_PIC32CZ_PMUX_EIC = 0x0, /* External Interrupts */ + WHAL_PIC32CZ_PMUX_AC = 0x1, /* ADC and Analog Comparator */ + WHAL_PIC32CZ_PMUX_SERCOM = 0x2, /* SERCOMn (UART, I2C, SPI) */ + WHAL_PIC32CZ_PMUX_SERCOM_ALT = 0x3, /* SERCOMn (UART, I2C, SPI) */ + WHAL_PIC32CZ_PMUX_EBI = 0x4, /* External Bus Interface */ + WHAL_PIC32CZ_PMUX_TCC = 0x5, /* Timer/counter controller */ + WHAL_PIC32CZ_PMUX_TCC_ALT_PDEC = 0x6, /* Timer/counter controller + and positional decoder */ + WHAL_PIC32CZ_PMUX_COM = 0x7, /* SQI/CAN/USB */ + WHAL_PIC32CZ_PMUX_SDMMC = 0x8, /* SD/MMC Host Controller */ + WHAL_PIC32CZ_PMUX_SPI_IXS = 0x9, /* SPI_IXS Audio */ + WHAL_PIC32CZ_PMUX_PCC = 0xA, /* Parallel Capture Controller */ + WHAL_PIC32CZ_PMUX_ETH = 0xB, /* Ethernet */ + WHAL_PIC32CZ_PMUX_MISC = 0xC, /* GCLK/CCL/AC Alt */ + WHAL_PIC32CZ_PMUX_PTC = 0xD, /* Peripheral Touch Controller */ }; /* diff --git a/wolfHAL/platform/microchip/pic32cz.h b/wolfHAL/platform/microchip/pic32cz.h index 5867987..bab10f9 100644 --- a/wolfHAL/platform/microchip/pic32cz.h +++ b/wolfHAL/platform/microchip/pic32cz.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #define WHAL_PIC32CZ_SUPPLY_DEVICE \ @@ -27,6 +28,13 @@ }, \ .driver = &whal_Pic32czGpio_Driver +#define WHAL_PIC32CZ_SERCOM4_UART_DEVICE \ + .regmap = { \ + .base = 0x46004000, \ + .size = 0x2000, \ + }, \ + .driver = &whal_Pic32czUart_Driver + #define WHAL_PIC32CZ_SUPPLY_PLL \ .enableMask = (1 << 18) diff --git a/wolfHAL/uart/pic32cz_uart.h b/wolfHAL/uart/pic32cz_uart.h index b19561d..c3b2578 100644 --- a/wolfHAL/uart/pic32cz_uart.h +++ b/wolfHAL/uart/pic32cz_uart.h @@ -11,6 +11,9 @@ * @brief PIC32CZ SERCOM USART driver configuration. */ +#define WHAL_PIC32CZ_UART_BAUD(freq_baud, freq_ref) \ + (65536UL - (65536UL * 16UL * (uint64_t)(freq_baud) / (uint64_t)(freq_ref))) + /* * @brief PIC32CZ SERCOM USART TX pad output options. */