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,19 +52,17 @@ 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();
@ -70,32 +70,27 @@ int main(void)
} }
// 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) {
else if ((diff /= SystemCoreClock/1000) > 0)
{
systicks = t; systicks = t;
msticks += diff; msticks += diff;
diff = msticks - *tickp; diff = msticks - *tickp;
if (diff >= msec) if (diff >= msec) {
{
*tickp = msticks; *tickp = msticks;
return(1); return(1);
} }
@ -104,8 +99,7 @@ int mstimeout(uint32_t *tickp, uint32_t msec)
} }
// 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;
@ -118,8 +112,7 @@ int printf(const char *fmt, ...)
} }
// 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,
@ -129,12 +122,10 @@ void init_serial(void)
} }
// 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;
@ -144,14 +135,12 @@ void poll_serial(void)
// 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;
} }
@ -159,15 +148,11 @@ int putch(int 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