-
Notifications
You must be signed in to change notification settings - Fork 0
PIC18 UART Driver #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AndrewWang001
wants to merge
8
commits into
main
Choose a base branch
from
uart-driver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8433b4d
add pic18 ltt uart driver
AndrewWang001 e8bc2ba
removed pps init, added bool controlled flow control and dynamic baud…
AndrewWang001 4a1aea6
added param checks and updated .h
AndrewWang001 5e99f23
fixed clang format
AndrewWang001 9012f55
fix syntax and format
AndrewWang001 3b2ab1a
fixed clang-format
AndrewWang001 f1d9a62
rough update based on pr review
AndrewWang001 f14431b
removed canlib dependancy etc
AndrewWang001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #ifndef ROCKETLIB_UART_H | ||
| #define ROCKETLIB_UART_H | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
|
|
||
| #include "common.h" | ||
|
|
||
| /* | ||
| * Initializes the UART1 module. | ||
| * | ||
| * baud_rate: Desired baud rate (e.g., 9600, 115200) | ||
| * fosc: Clock frequency (must be 12 MHz or 48 MHz) | ||
| * enable_flow_control: true to enable RTS flow control, false to disable | ||
| * | ||
| * Returns: W_SUCCESS if setup is successful, otherwise W_INVALID_PARAM | ||
| */ | ||
| w_status_t uart_init(uint32_t baud_rate, uint32_t fosc, bool enable_flow_control); | ||
|
|
||
| /* | ||
| * Transmits a single byte over UART (blocking). | ||
| * | ||
| * waits until the transmit buffer is empty, then sends the byte. | ||
| */ | ||
| void uart_transmit_byte(uint8_t byte); | ||
| // void uart_transmit_buffer(uint8_t *tx, uint8_t len); | ||
|
|
||
| /* | ||
| * Interrupt handler for UART1. | ||
| * | ||
| * Handles RX and optional TX, | ||
| * and calls user-defined uart_rx_callback() on received bytes. | ||
| */ | ||
| void uart_interrupt_handler(void); | ||
|
|
||
| /* | ||
| * Internal callback function called when a byte is received via UART1. | ||
| * | ||
| * This is implemented in uart.c and currently echoes the received byte back. | ||
| * It is not intended to be overridden by user code due to MPLAB limitations. | ||
| */ | ||
| void uart_rx_callback(uint8_t byte); | ||
|
|
||
| #endif /* ROCKETLIB_UART_H */ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <xc.h> | ||
|
|
||
| #include "common.h" | ||
| #include "uart.h" | ||
|
|
||
| void uart_rx_callback(uint8_t byte) { | ||
| uart_transmit_byte(byte); // echos back recieved byte | ||
| } | ||
|
|
||
| w_status_t uart_init(uint32_t baud_rate, uint32_t fosc, bool enable_flow_control) { | ||
| // make sure to Configure UART1 pins before running!!! | ||
|
|
||
| // only 12 or 48 MHz support for pic | ||
| if (fosc != 12000000UL && fosc != 48000000UL) { | ||
| return W_INVALID_PARAM; | ||
| } | ||
|
|
||
| // bool controlled flow control. | ||
| U1CON2bits.FLO = enable_flow_control ? 0b10 : 0b00; | ||
|
|
||
| // low speed | ||
| U1CON0bits.BRGS = 0; | ||
| uint32_t divisor = (U1CON0bits.BRGS == 1) ? 4 : 16; | ||
|
|
||
| // checks for valid baud rate | ||
| if (baud_rate == 0 || baud_rate > (fosc / divisor)) { | ||
| return W_INVALID_PARAM; | ||
| } | ||
|
|
||
| // disable autodetect baudrate | ||
| U1CON0bits.ABDEN = 0; | ||
| // normal mode (8 bit, no parity, no 9th bit) | ||
| U1CON0bits.MODE = 0; | ||
| // enable transmit | ||
| U1CON0bits.TXEN = 1; | ||
| // enable receive | ||
| U1CON0bits.RXEN = 1; | ||
|
|
||
| // keep running on overflow, never stop receiving | ||
| U1CON2bits.RUNOVF = 1; | ||
|
|
||
| // dynamic baud rate | ||
| uint16_t brg = (fosc / (divisor * baud_rate)) - 1; | ||
| U1BRGH = (brg >> 8) & 0xFF; | ||
| U1BRGL = brg & 0xFF; | ||
|
|
||
| // enable UART module | ||
| U1CON1bits.ON = 1; | ||
|
|
||
| // enable receive interrupt | ||
| IPR3bits.U1RXIP = 1; | ||
| PIE3bits.U1RXIE = 1; | ||
|
|
||
| // do not enable TX interrupt initially; only needed when there's queued data | ||
|
|
||
| return W_SUCCESS; | ||
| } | ||
|
|
||
| void uart_transmit_byte(uint8_t byte) { | ||
| while (!PIR3bits.U1TXIF) | ||
| ; // blocking wait | ||
| U1TXB = byte; | ||
| } | ||
|
|
||
| void uart_interrupt_handler(void) { | ||
| // TX interrupt | ||
| if (PIR3bits.U1TXIF) { | ||
| PIR3bits.U1TXIF = 0; | ||
| } | ||
|
|
||
| // RX interrupt | ||
| if (PIR3bits.U1RXIF) { | ||
| PIR3bits.U1RXIF = 0; | ||
| uint8_t rcv = U1RXB; | ||
| uart_rx_callback(rcv); // invoke user callback | ||
| } | ||
|
|
||
| // UART error interrupt | ||
| if (PIR3bits.U1EIF) { | ||
| PIR3bits.U1EIF = 0; | ||
| // optionally handle framing/parity/overflow errors | ||
| } | ||
|
|
||
| // global UART interrupt | ||
| if (PIR3bits.U1IF) { | ||
| PIR3bits.U1IF = 0; | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.