git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@5373 ddd99763-3ecb-0310-9145-efcb8ce7c51ftags/zoom-1.0
@@ -1,27 +1,27 @@ | |||
#include "config.h" | |||
/* Configuration words */ | |||
_FOSC(FCKSM_CSECMD | POSCMD_XT); | |||
_FOSCSEL(FNOSC_FRC); | |||
_FWDT(FWDTEN_OFF); | |||
void config_init(void) | |||
{ | |||
/* Disable analog inputs */ | |||
AD1PCFGL = 0xffff; | |||
AD1PCFGH = 0xffff; | |||
AD2PCFGL = 0xffff; | |||
/* Configure PLL to multiply from 8 -> 40 MHz */ | |||
PLLFBD = 38 - 2; | |||
CLKDIVbits.PLLPRE = 0; | |||
CLKDIVbits.PLLPOST = 0; | |||
/* Switch to XTPLL clock */ | |||
__builtin_write_OSCCONH(0x03); | |||
__builtin_write_OSCCONL(0x01); | |||
/* Wait for lock */ | |||
while (!OSCCONbits.LOCK) | |||
continue; | |||
} | |||
#include "config.h" | |||
/* Configuration words */ | |||
_FOSC(FCKSM_CSECMD | POSCMD_XT); | |||
_FOSCSEL(FNOSC_FRC); | |||
_FWDT(FWDTEN_OFF); | |||
void config_init(void) | |||
{ | |||
/* Disable analog inputs */ | |||
AD1PCFGL = 0xffff; | |||
AD1PCFGH = 0xffff; | |||
AD2PCFGL = 0xffff; | |||
/* Configure PLL to multiply from 8 -> 40 MHz */ | |||
PLLFBD = 38 - 2; | |||
CLKDIVbits.PLLPRE = 0; | |||
CLKDIVbits.PLLPOST = 0; | |||
/* Switch to XTPLL clock */ | |||
__builtin_write_OSCCONH(0x03); | |||
__builtin_write_OSCCONL(0x01); | |||
/* Wait for lock */ | |||
while (!OSCCONbits.LOCK) | |||
continue; | |||
} |
@@ -1,18 +1,18 @@ | |||
#ifndef CONFIG_H | |||
#define CONFIG_H | |||
#include <p33Fxxxx.h> | |||
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); | |||
#endif | |||
#ifndef CONFIG_H | |||
#define CONFIG_H | |||
#include <p33Fxxxx.h> | |||
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); | |||
#endif | |||
@@ -1,68 +1,115 @@ | |||
#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++); | |||
} | |||
#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++); | |||
} | |||
void uart_put_bin(int uart, uint8_t x) | |||
{ | |||
int i; | |||
for(i=0;i<8;i++) { | |||
uart_put(uart, (x & 0x80) ? '1' : '0'); | |||
x <<= 1; | |||
} | |||
} | |||
void uart1_put_hex(int uart, uint8_t x) | |||
{ | |||
uart_put(uart, hex[x >> 4]); | |||
uart_put(uart, hex[x & 15]); | |||
} | |||
void uart_put_hex16(int uart, uint16_t x) | |||
{ | |||
uart_put_hex(uart, (x >> 8) & 0xFF); | |||
uart_put_hex(uart, (x) & 0xFF); | |||
} | |||
void uart_put_hex32(int uart, uint32_t x) | |||
{ | |||
uart_put_hex(uart, (x >> 24) & 0xFF); | |||
uart_put_hex(uart, (x >> 16) & 0xFF); | |||
uart_put_hex(uart, (x >> 8) & 0xFF); | |||
uart_put_hex(uart, (x) & 0xFF); | |||
} | |||
void uart_put_dec(int uart, uint32_t x) | |||
{ | |||
uint32_t place = 1; | |||
while (x / place > 9) | |||
place *= 10; | |||
while (place > 0) { | |||
uart_put(uart, x / place + '0'); | |||
x %= place; | |||
place /= 10; | |||
} | |||
} | |||
void uart_crlf(int uart) | |||
{ | |||
uart_put(uart, '\r'); | |||
uart_put(uart, '\n'); | |||
} |
@@ -1,50 +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 | |||
#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 |
@@ -0,0 +1,26 @@ | |||
#include "config.h" | |||
#include "util.h" | |||
/* Convert from ASCII hex. Returns | |||
the value, or 16 if it was space/newline, or | |||
32 if some other character. */ | |||
uint8_t from_hex(uint8_t ch) | |||
{ | |||
if(ch==' ' || ch=='\r' || ch=='\n') | |||
return 16; | |||
if(ch < '0') | |||
goto bad; | |||
if(ch <= '9') | |||
return ch - '0'; | |||
ch |= 0x20; | |||
if(ch < 'a') | |||
goto bad; | |||
if(ch <= 'f') | |||
return ch - 'a' + 10; | |||
bad: | |||
return 32; | |||
} | |||
const uint8_t hex[16]={'0','1','2','3','4','5','6','7', | |||
'8','9','a','b','c','d','e','f'}; |
@@ -0,0 +1,14 @@ | |||
#ifndef UTIL_H | |||
#define UTIL_H | |||
#include "config.h" | |||
/* Convert from ascii hex digit to number */ | |||
uint8_t from_hex(uint8_t ch); | |||
extern const uint8_t hex[16]; | |||
/* Array length */ | |||
#define array_len(x) (sizeof(x)/sizeof(x[0])) | |||
#endif | |||
@@ -1,8 +1,8 @@ | |||
#include "config.h" | |||
int main(void) | |||
{ | |||
config_init(); | |||
return 0; | |||
} | |||
#include "config.h" | |||
int main(void) | |||
{ | |||
config_init(); | |||
return 0; | |||
} |