Browse Source

start adding uart stuff

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@5372 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 16 years ago
parent
commit
33fbeb4335
4 changed files with 133 additions and 4 deletions
  1. +8
    -1
      firmware/config.h
  2. +68
    -0
      firmware/uart.c
  3. +50
    -0
      firmware/uart.h
  4. +7
    -3
      firmware/zoom.mcp

+ 8
- 1
firmware/config.h View File

@@ -3,7 +3,14 @@
#include <p33Fxxxx.h>
#define Fcy 20000000
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed int int16_t;
typedef unsigned int uint16_t;
typedef signed long int int32_t;
typedef unsigned long int uint32_t;
#define FCY 20000000
void config_init(void);


+ 68
- 0
firmware/uart.c View File

@@ -0,0 +1,68 @@
#include "config.h"
#include "uart.h"
void uart_init(int uart)
{
if (uart == 1) {
U1MODE = 0;
U1MODEbits.BRGH = 0;
U1STA = 0;
U1MODEbits.UARTEN = 1;
U1STAbits.UTXEN = 1;
} else if (uart == 2) {
U2MODE = 0;
U2MODEbits.BRGH = 0;
U2STA = 0;
U2MODEbits.UARTEN = 1;
U2STAbits.UTXEN = 1;
}
uart_set_rate(uart, 115200);
}
void uart_set_rate(int uart, int32_t rate)
{
int32_t brg;
brg = ((FCY + (8 * rate - 1)) / (16 * rate)) - 1;
if (brg < 1) brg = 1;
if (brg > 65535) brg = 65535;
if (uart == 1)
U1BRG = brg;
else if (uart == 2)
U2BRG = brg;
}
void uart_put(int uart, uint8_t x)
{
if (uart == 1) {
while (U1STAbits.UTXBF) continue;
U1TXREG = x;
} else if (uart == 2) {
while (U2STAbits.UTXBF) continue;
U2TXREG = x;
}
}
uint8_t uart_get(int uart)
{
uint8_t data = 0;
if (uart == 1) {
while (!U1STAbits.URXDA) continue;
data = U1RXREG;
if (U1STAbits.OERR)
U1STAbits.OERR = 0;
} else if (uart == 2) {
while (!U2STAbits.URXDA) continue;
data = U2RXREG;
if (U2STAbits.OERR)
U2STAbits.OERR = 0;
}
return data;
}
void uart_put_string(int uart, const char *s)
{
while(s && *s)
uart_put(uart, *s++);
}

+ 50
- 0
firmware/uart.h View File

@@ -0,0 +1,50 @@
#ifndef UART_H
#define UART_H
#include "config.h"
/* Init */
void uart_init(int uart);
void uart_set_rate(int uart, int32_t rate);
/* Blocking sends */
void uart_put(int uart, uint8_t x);
void uart_put_string(int uart, const char *s);
void uart_crlf(int uart);
void uart_put_hex(int uart, uint8_t x);
void uart_put_hex16(int uart, uint16_t x);
void uart_put_hex32(int uart, uint32_t x);
void uart_put_bin(int uart, uint8_t x);
void uart_put_dec(int uart, uint32_t x);
/* Blocking receives */
uint8_t uart_get(int uart);
/* Return true if get/put would not block */
int uart_can_get(int uart);
int uart_can_put(int uart);
/* Helpers to work with a specific uart */
#define uart1_init() uart_init(1)
#define uart1_set_rate(x) uart_set_rate(1,x)
#define uart1_put(x) uart_put(1,x)
#define uart1_put_string(x) uart_put_string(1,x)
#define uart1_crlf() uart_crlf(1)
#define uart1_put_hex(x) uart_put_hex(1,x)
#define uart1_put_hex16(x) uart_put_hex16(1,x)
#define uart1_put_hex32(x) uart_put_hex32(1,x)
#define uart1_put_bin(x) uart_put_bin(1,x)
#define uart1_put_dec(x) uart_put_dec(1,x)
#define uart2_init() uart_init(2)
#define uart2_set_rate(x) uart_set_rate(2,x)
#define uart2_put(x) uart_put(2,x)
#define uart2_put_string(x) uart_put_string(2,x)
#define uart2_crlf() uart_crlf(2)
#define uart2_put_hex(x) uart_put_hex(2,x)
#define uart2_put_hex16(x) uart_put_hex16(2,x)
#define uart2_put_hex32(x) uart_put_hex32(2,x)
#define uart2_put_bin(x) uart_put_bin(2,x)
#define uart2_put_dec(x) uart_put_dec(2,x)
#endif

+ 7
- 3
firmware/zoom.mcp View File

@@ -21,18 +21,22 @@ file_000=no
file_001=no
file_002=no
file_003=no
file_004=no
file_005=no
[FILE_INFO]
file_000=zoom.c
file_001=config.c
file_002=config.h
file_003=p33fj256gp710.gld
file_002=uart.c
file_003=config.h
file_004=uart.h
file_005=p33fj256gp710.gld
[SUITE_INFO]
suite_guid={479DDE59-4D56-455E-855E-FFF59A3DB57E}
suite_state=
[TOOL_SETTINGS]
TS{7D9C6ECE-785D-44CB-BA22-17BF2E119622}=-g
TS{25AC22BD-2378-4FDB-BFB6-7345A15512D3}=-g -Wall
TS{7DAC9A1D-4C45-45D6-B25A-D117C74E8F5A}=-o"$(BINDIR_)$(TARGETBASE).$(TARGETSUFFIX)" -Map="$(BINDIR_)$(TARGETBASE).map" --report-mem
TS{7DAC9A1D-4C45-45D6-B25A-D117C74E8F5A}=-Map="$(BINDIR_)$(TARGETBASE).map" --report-mem -o"$(BINDIR_)$(TARGETBASE).$(TARGETSUFFIX)"
TS{509E5861-1E2A-483B-8B6B-CA8DB7F2DD78}=
[INSTRUMENTED_TRACE]
enable=0


Loading…
Cancel
Save