Skip to content

Commit

Permalink
Merge branch 'zero-port'
Browse files Browse the repository at this point in the history
  • Loading branch information
MightyPork committed May 4, 2018
2 parents 30d1762 + 0bce534 commit 315da1f
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 32 deletions.
2 changes: 2 additions & 0 deletions TinyFrame/TF_Integration.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len)
else if (gActiveComport == COMPORT_USART) {
iface_uart_transmit(buff, len);
}
#if SUPPORT_NRF
else if (gActiveComport == COMPORT_NORDIC) {
iface_nordic_transmit(buff, len);
}
#endif // SUPPORT_NRF
else {
// TODO other transports
trap("not implemented.");
Expand Down
23 changes: 19 additions & 4 deletions comm/iface_nordic.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "system_settings.h"
#include "utils/hexdump.h"

#if SUPPORT_NRF

extern osSemaphoreId semVcomTxReadyHandle;

#define RX_PIPE_NUM 0
Expand Down Expand Up @@ -70,15 +72,17 @@ bool iface_nordic_init(void)
hw_periph_clock_enable(NRF_SPI);

// SPI pins
assert_param(E_SUCCESS == hw_configure_gpiorsc_af(NRF_R_SCK, NRF_SPI_AF));
assert_param(E_SUCCESS == hw_configure_gpiorsc_af(NRF_R_MOSI, NRF_SPI_AF));
assert_param(E_SUCCESS == hw_configure_gpiorsc_af(NRF_R_MISO, NRF_SPI_AF));
assert_param(E_SUCCESS == hw_configure_gpiorsc_af(NRF_R_SCK, NRF_SCK_AF));
assert_param(E_SUCCESS == hw_configure_gpiorsc_af(NRF_R_MOSI, NRF_MOSI_AF));
assert_param(E_SUCCESS == hw_configure_gpiorsc_af(NRF_R_MISO, NRF_MISO_AF));

// Manual pins
LL_GPIO_SetPinMode(NRF_NSS_GPIO_Port, NRF_NSS_Pin, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinMode(NRF_CE_GPIO_Port, NRF_CE_Pin, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinMode(NRF_RST_GPIO_Port, NRF_RST_Pin, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinMode(NRF_IRQ_GPIO_Port, NRF_IRQ_Pin, LL_GPIO_MODE_INPUT);

// set up SPI
LL_SPI_Disable(NRF_SPI);
{
LL_SPI_SetBaudRatePrescaler(NRF_SPI, LL_SPI_BAUDRATEPRESCALER_DIV32);
Expand All @@ -97,11 +101,19 @@ bool iface_nordic_init(void)
}
LL_SPI_Enable(NRF_SPI);

// reset the radio / enable its power supply
NRF_Reset(1);
LL_mDelay(5);
NRF_Reset(0);

dbg("configure nrf module");

// Now configure the radio
NRF_Init(NRF_SPEED_2M); // TODO configurable speed
if (!NRF_Init(NRF_SPEED_2M)) {// TODO configurable speed - also maybe better to use slower
dbg("--- NRF not present!");
return false;
}

NRF_SetChannel(SystemSettings.nrf_channel);
NRF_SetBaseAddress(SystemSettings.nrf_network);
NRF_SetRxAddress(RX_PIPE_NUM, SystemSettings.nrf_address);
Expand Down Expand Up @@ -133,6 +145,7 @@ void iface_nordic_deinit(void)
hw_deinit_pin_rsc(NRF_R_NSS);
hw_deinit_pin_rsc(NRF_R_CE);
hw_deinit_pin_rsc(NRF_R_IRQ);
hw_deinit_pin_rsc(NRF_R_RST);
}

// FIXME
Expand Down Expand Up @@ -173,3 +186,5 @@ void iface_nordic_transmit(const uint8_t *buff, uint32_t len)
// give when it's done
xSemaphoreGive(semVcomTxReadyHandle); // similar to how it's done in USB - this is called in the Tx Done handler
}

#endif // SUPPORT_NRF
4 changes: 4 additions & 0 deletions comm/iface_nordic.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "platform.h"

#if SUPPORT_NRF

void iface_nordic_claim_resources(void);

void iface_nordic_free_resources(void);
Expand All @@ -17,4 +19,6 @@ bool iface_nordic_init(void);

void iface_nordic_transmit(const uint8_t *buff, uint32_t len);

#endif // SUPPORT_NRF

#endif //GEX_F072_IFACE_NORDIC_H
10 changes: 9 additions & 1 deletion comm/interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "iface_usb.h"
#include "iface_nordic.h"

// those correspond to the ENUM values
const char * COMPORT_NAMES[] = {
"NONE",
"USB",
Expand All @@ -24,7 +25,6 @@ const char * COMPORT_NAMES[] = {
"LORA",
};


enum ComportSelection gActiveComport = COMPORT_USB; // start with USB so the handlers work correctly initially

static uint32_t last_switch_time = 0; // started with USB
Expand Down Expand Up @@ -86,9 +86,11 @@ void com_claim_resources_for_alt_transfers(void)
iface_uart_claim_resources();
}

#if SUPPORT_NRF
if (SystemSettings.use_comm_nordic) {
iface_nordic_claim_resources();
}
#endif // SUPPORT_NRF
}

/** Release resources allocated for alternate transfers */
Expand All @@ -98,9 +100,11 @@ void com_release_resources_for_alt_transfers(void)
iface_uart_free_resources();
}

#if SUPPORT_NRF
if (SystemSettings.use_comm_nordic) {
iface_nordic_free_resources();
}
#endif // SUPPORT_NRF
}


Expand All @@ -115,9 +119,11 @@ static bool configure_interface(enum ComportSelection iface)
else if (gActiveComport == COMPORT_USART) {
iface_uart_deinit();
}
#if SUPPORT_NRF
else if (gActiveComport == COMPORT_NORDIC) {
iface_nordic_deinit();
}
#endif // SUPPORT_NRF


gActiveComport = iface;
Expand All @@ -130,9 +136,11 @@ static bool configure_interface(enum ComportSelection iface)
else if (iface == COMPORT_USART) {
return iface_uart_init();
}
#if SUPPORT_NRF
else if (iface == COMPORT_NORDIC) {
return iface_nordic_init();
}
#endif // SUPPORT_NRF
#if 0
else if (iface == COMPORT_LORA) {
// Try to configure nordic
Expand Down
19 changes: 18 additions & 1 deletion comm/nrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ static uint8_t spi(uint8_t tx) {
return LL_SPI_ReceiveData8(NRF_SPI);
}

void NRF_Reset(bool enable_reset)
{
if (enable_reset) {
// go HIGH - this closes the PMOS
LL_GPIO_SetOutputPin(NRF_RST_GPIO_Port, NRF_RST_Pin);
} else {
// LOW - open PMOS, enable power
LL_GPIO_ResetOutputPin(NRF_RST_GPIO_Port, NRF_RST_Pin);
}
}

//-------

/*
Expand Down Expand Up @@ -492,7 +503,7 @@ bool NRF_SendPacket(uint8_t PipeNum, const uint8_t *Packet, uint8_t Length)
return 0 != (st & RD_STATUS_TX_DS); // success
}

void NRF_Init(uint8_t pSpeed)
bool NRF_Init(uint8_t pSpeed)
{
// Set the required output pins
NSS(1);
Expand All @@ -505,6 +516,10 @@ void NRF_Init(uint8_t pSpeed)
nrf_pipe_enabled[i] = 0;
}

// this is a test if there's anything present
uint8_t awreg = NRF_ReadRegister(RG_SETUP_AW);
if (awreg == 0 || awreg > 3) return false;

// clear flags etc
NRF_PowerDown();
CHIPSELECT { spi(CMD_FLUSH_RX); }
Expand All @@ -527,4 +542,6 @@ void NRF_Init(uint8_t pSpeed)
// for (int i = 0; i < 6; i++) {
// NRF_WriteRegister(RG_RX_PW_P0+i, 32); // Receive 32 byte packets - XXX this is probably not needed with dynamic length
// }

return true;
}
10 changes: 9 additions & 1 deletion comm/nrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@
* Initialize the NRF module
*
* @param pSpeed
* @return success (0 if device not detected)
*/
void NRF_Init(uint8_t pSpeed);
bool NRF_Init(uint8_t pSpeed);

#define NRF_SPEED_500k 0b00100110
#define NRF_SPEED_2M 0b00001110
#define NRF_SPEED_1M 0b00000110

/**
* Set the reset pin (this is a PMOS controlling its power)
*
* @param enable_reset 1 to go into reset, 0 to enter operational state (will need some time to become ready)
*/
void NRF_Reset(bool enable_reset);

/**
* Set reception address
* @param pipenum - pipe to set
Expand Down
86 changes: 64 additions & 22 deletions comm/nrf_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,69 @@

#include "platform.h"

// TODO move those to plat_compat?
#define NRF_SPI SPI1
#define NRF_R_SPI R_SPI1

#define NRF_IRQ_Pin LL_GPIO_PIN_10
#define NRF_IRQ_GPIO_Port GPIOB
#define NRF_R_IRQ R_PB10
#define NRF_EXTI_LINENUM 10
#define NRF_SYSCFG_EXTI_PORT LL_SYSCFG_EXTI_PORTB

#define NRF_NSS_Pin LL_GPIO_PIN_11
#define NRF_NSS_GPIO_Port GPIOB
#define NRF_R_NSS R_PB11

#define NRF_CE_Pin LL_GPIO_PIN_12
#define NRF_CE_GPIO_Port GPIOB
#define NRF_R_CE R_PB12

#define NRF_R_SCK R_PA5
#define NRF_R_MISO R_PA6
#define NRF_R_MOSI R_PA7
#define NRF_SPI_AF LL_GPIO_AF_0
#if defined(GEX_PLAT_F072_DISCOVERY) || defined(GEX_PLAT_F072_HUB)

// This config was used only for development when NRF was enabled for those platforms. It is normally disabled.
#define NRF_SPI SPI1
#define NRF_R_SPI R_SPI1

#define NRF_IRQ_Pin LL_GPIO_PIN_10
#define NRF_IRQ_GPIO_Port GPIOB
#define NRF_R_IRQ R_PB10
#define NRF_EXTI_LINENUM 10
#define NRF_SYSCFG_EXTI_PORT LL_SYSCFG_EXTI_PORTB

#define NRF_NSS_Pin LL_GPIO_PIN_11
#define NRF_NSS_GPIO_Port GPIOB
#define NRF_R_NSS R_PB11

#define NRF_CE_Pin LL_GPIO_PIN_12
#define NRF_CE_GPIO_Port GPIOB
#define NRF_R_CE R_PB12

#define NRF_RST_Pin LL_GPIO_PIN_13
#define NRF_RST_GPIO_Port GPIOB
#define NRF_R_RST R_PB13

#define NRF_R_SCK R_PA5
#define NRF_SCK_AF LL_GPIO_AF_0
#define NRF_R_MISO R_PA6
#define NRF_MISO_AF LL_GPIO_AF_0
#define NRF_R_MOSI R_PA7
#define NRF_MOSI_AF LL_GPIO_AF_0

#elif defined(GEX_PLAT_F072_ZERO)

#define NRF_SPI SPI2
#define NRF_R_SPI R_SPI2

#define NRF_IRQ_Pin LL_GPIO_PIN_15
#define NRF_IRQ_GPIO_Port GPIOC
#define NRF_R_IRQ R_PC15
#define NRF_EXTI_LINENUM 15
#define NRF_SYSCFG_EXTI_PORT LL_SYSCFG_EXTI_PORTC

#define NRF_NSS_Pin LL_GPIO_PIN_13
#define NRF_NSS_GPIO_Port GPIOC
#define NRF_R_NSS R_PC13

#define NRF_CE_Pin LL_GPIO_PIN_14
#define NRF_CE_GPIO_Port GPIOC
#define NRF_R_CE R_PC14

#define NRF_RST_Pin LL_GPIO_PIN_12
#define NRF_RST_GPIO_Port GPIOC
#define NRF_R_RST R_PC12

#define NRF_R_SCK R_PB13
#define NRF_SCK_AF LL_GPIO_AF_0
#define NRF_R_MISO R_PC2
#define NRF_MISO_AF LL_GPIO_AF_1
#define NRF_R_MOSI R_PC3
#define NRF_MOSI_AF LL_GPIO_AF_1

#else
#error "Missing NRF config for this platform."
#endif

#endif //GEX_F072_NRF_PINS_H
4 changes: 4 additions & 0 deletions framework/system_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ void systemsettings_build_ini(IniWriter *iw)
iw_entry_s(iw, "com-uart", str_yn(SystemSettings.use_comm_uart));
iw_entry_d(iw, "com-uart-baud", SystemSettings.comm_uart_baud);

#if SUPPORT_NRF
iw_cmt_newline(iw);
iw_comment(iw, "nRF24L01+ radio");
iw_entry_s(iw, "com-nrf", str_yn(SystemSettings.use_comm_nordic));
Expand All @@ -212,6 +213,7 @@ void systemsettings_build_ini(IniWriter *iw)

iw_comment(iw, "Node address (1-255)");
iw_entry(iw, "nrf-address", "%d", (int)SystemSettings.nrf_address);
#endif // SUPPORT_NRF

// those aren't implement yet, don't tease the user
// TODO show pin-out, extra settings if applicable
Expand Down Expand Up @@ -274,6 +276,7 @@ bool systemsettings_load_ini(const char *restrict key, const char *restrict valu
if (suc) SystemSettings.comm_uart_baud = baud;
}

#if SUPPORT_NRF
if (streq(key, "com-nrf")) {
bool yn = cfg_bool_parse(value, &suc);
if (suc) SystemSettings.use_comm_nordic = yn;
Expand All @@ -290,6 +293,7 @@ bool systemsettings_load_ini(const char *restrict key, const char *restrict valu
if (streq(key, "nrf-network")) {
cfg_hex_parse(&SystemSettings.nrf_network[0], 4, value, &suc);
}
#endif // SUPPORT_NRF

#if 0
if (streq(key, "com-lora")) {
Expand Down
2 changes: 1 addition & 1 deletion platform/debug_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#define DEBUG_USART_BAUD 115200

#if GEX_PLAT_F072_DISCOVERY || GEX_PLAT_F072_HUB
#if GEX_PLAT_F072_DISCOVERY || GEX_PLAT_F072_HUB || GEX_PLAT_F072_ZERO

#define DEBUG_USART USART1
#define DEBUG_USART_RSC R_USART1
Expand Down
7 changes: 6 additions & 1 deletion platform/irq_dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ void* irqd_detach(void *periph, IrqCallback callback)

return oldArg;
} else {
trap("Detach IRQ %p() from %p but %p() bound instead", callback, periph, slot->callback);
// catch bugs, but ignore an attempt to unbind when not bound
if (slot->callback != NULL) {
trap("Detach IRQ %p() from %p but %p() bound instead", callback, periph, slot->callback);
} else {
return NULL; // the arg - none
}
}
}

Expand Down
Loading

0 comments on commit 315da1f

Please sign in to comment.