Change stuff to theoretically work

This commit is contained in:
Kelvin Ly 2020-05-16 10:35:11 -04:00
parent d0d74daa2b
commit 2586a07064
1 changed files with 76 additions and 91 deletions

View File

@ -8,13 +8,15 @@
#include "nrf_fprintf.h" #include "nrf_fprintf.h"
// LED definitions // LED definitions
#define LED_PIN 7 #define REG_EN_PIN 15
#define LED_BIT (1 << LED_PIN) #define REG_EN_BIT (1 << REG_EN_PIN)
#define PWR_EN_PIN 12
#define PWR_EN_BIT (1 << PWR_EN_PIN)
// UART definitions // UART definitions
#define UART_PIN_DISCONNECTED 0xFFFFFFFF #define UART_PIN_DISCONNECTED 0xFFFFFFFF
#define UART_TX_PIN 15 #define UART_TX_PIN 18
#define UART_RX_PIN 14 #define UART_RX_PIN 20
#define UART_RTS_PIN UART_PIN_DISCONNECTED #define UART_RTS_PIN UART_PIN_DISCONNECTED
#define UART_CTS_PIN UART_PIN_DISCONNECTED #define UART_CTS_PIN UART_PIN_DISCONNECTED
#define UART_BAUD NRF_UART_BAUDRATE_115200 #define UART_BAUD NRF_UART_BAUDRATE_115200
@ -50,124 +52,107 @@ nrfx_uart_t nrfx_uart =
NRF_UART0, 0 NRF_UART0, 0
}; };
int main(void) int main(void) {
{ uint32_t tix;
uint32_t tix;
mstimeout(&tix, 0); mstimeout(&tix, 0);
init_gpio(); init_gpio();
init_serial(); init_serial();
printf("\nNRF52 test\n"); printf("\nNRF52 test\n");
while (1) NRF_GPIO->OUT |= PWR_EN_BIT;
{ while (1) {
if (mstimeout(&tix, 500)) if (mstimeout(&tix, 500)) {
{ NRF_GPIO->OUT ^= REG_EN_BIT;
NRF_GPIO->OUT ^= LED_BIT; putch('.');
putch('.');
}
poll_serial();
} }
poll_serial();
}
} }
// Initialise I/O port, and system tick counter // Initialise I/O port, and system tick counter
void init_gpio(void) void init_gpio(void) {
{ nrf_gpio_cfg_output(REG_EN_PIN);
nrf_gpio_cfg_output(LED_PIN); nrf_gpio_cfg_output(PWR_EN_PIN);
nrf_drv_systick_init(); nrf_drv_systick_init();
} }
// Check for timeout in milliseconds // Check for timeout in milliseconds
int mstimeout(uint32_t *tickp, uint32_t msec) int mstimeout(uint32_t *tickp, uint32_t msec) {
{ uint32_t t, diff;
uint32_t t, diff;
t = nrf_systick_val_get(); t = nrf_systick_val_get();
diff = NRF_SYSTICK_VAL_MASK & (systicks - t); diff = NRF_SYSTICK_VAL_MASK & (systicks - t);
if (msec == 0) if (msec == 0) {
{ systicks = t;
systicks = t; *tickp = msticks;
*tickp = msticks; } else if ((diff /= SystemCoreClock/1000) > 0) {
systicks = t;
msticks += diff;
diff = msticks - *tickp;
if (diff >= msec) {
*tickp = msticks;
return(1);
} }
else if ((diff /= SystemCoreClock/1000) > 0) }
{ return(0);
systicks = t;
msticks += diff;
diff = msticks - *tickp;
if (diff >= msec)
{
*tickp = msticks;
return(1);
}
}
return(0);
} }
// Formatted print to serial port // Formatted print to serial port
int printf(const char *fmt, ...) int printf(const char *fmt, ...) {
{ va_list args;
va_list args; int n;
int n;
va_start(args, fmt); va_start(args, fmt);
nrf_fprintf(&fprintf_ctx, fmt, &args); nrf_fprintf(&fprintf_ctx, fmt, &args);
n = fprintf_ctx.io_buffer_cnt; n = fprintf_ctx.io_buffer_cnt;
nrf_fprintf_buffer_flush(&fprintf_ctx); nrf_fprintf_buffer_flush(&fprintf_ctx);
va_end(args); va_end(args);
return(n); return(n);
} }
// Initialise serial I/O // Initialise serial I/O
void init_serial(void) void init_serial(void) {
{ const nrfx_uart_config_t config = {
const nrfx_uart_config_t config = { UART_TX_PIN, UART_RX_PIN,
UART_TX_PIN, UART_RX_PIN, UART_CTS_PIN, UART_RTS_PIN, 0,
UART_CTS_PIN, UART_RTS_PIN, 0, NRF_UART_HWFC_DISABLED, 0, UART_BAUD,
NRF_UART_HWFC_DISABLED, 0, UART_BAUD, APP_IRQ_PRIORITY_LOWEST};
APP_IRQ_PRIORITY_LOWEST}; nrfx_uart_init(&nrfx_uart, &config, 0);
nrfx_uart_init(&nrfx_uart, &config, 0);
} }
// Poll serial interface, send next Tx char // Poll serial interface, send next Tx char
void poll_serial(void) void poll_serial(void) {
{ uint8_t b;
uint8_t b;
if (ser_txin!=ser_txout && !nrfx_uart_tx_in_progress(&nrfx_uart)) if (ser_txin!=ser_txout && !nrfx_uart_tx_in_progress(&nrfx_uart)) {
{ b = ser_txbuff[ser_txout++];
b = ser_txbuff[ser_txout++]; if (ser_txout >= SER_TX_BUFFLEN)
if (ser_txout >= SER_TX_BUFFLEN) ser_txout = 0;
ser_txout = 0; nrfx_uart_tx(&nrfx_uart, &b, 1);
nrfx_uart_tx(&nrfx_uart, &b, 1); }
}
} }
// Add character to serial Tx circular buffer // Add character to serial Tx circular buffer
// Discard character if buffer is full // Discard character if buffer is full
int putch(int c) int putch(int c) {
{ int in=ser_txin+1;
int in=ser_txin+1;
if (in >= SER_TX_BUFFLEN) if (in >= SER_TX_BUFFLEN)
in = 0; in = 0;
if (in != ser_txout) if (in != ser_txout) {
{ ser_txbuff[ser_txin] = (uint8_t)c;
ser_txbuff[ser_txin] = (uint8_t)c; ser_txin = in;
ser_txin = in; }
} return(c);
return(c);
} }
// Send data block to UART (via circular buffer) // Send data block to UART (via circular buffer)
void uart_tx(void const *ctxp, char const *buff, size_t len) void uart_tx(void const *ctxp, char const *buff, size_t len) {
{ while (len--)
while (len--) putch(*buff++);
putch(*buff++);
} }
// Dummy UART event handler // Dummy UART event handler
void uart_event_handler(nrfx_uart_event_t const * evtp, void *ctxp) void uart_event_handler(nrfx_uart_event_t const * evtp, void *ctxp) {
{
} }
// EOF