kinda works but unreliable

porting
Ondřej Hruška 7 years ago
parent f22934e236
commit 56191d621a
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 11
      Inc/main.h
  2. 16
      Src/gex_gateway.c
  3. 13
      Src/gpio.c
  4. 33
      Src/main.c

@ -74,14 +74,19 @@
/* Private define ------------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/
#define LED_Pin LL_GPIO_PIN_13
#define LED_GPIO_Port GPIOC #define LED_GPIO_Port GPIOC
#define NRF_IRQ_Pin LL_GPIO_PIN_2 #define LED1_Pin LL_GPIO_PIN_13
#define LEDRX_Pin LL_GPIO_PIN_14
#define LEDTX_Pin LL_GPIO_PIN_15
#define NRF_IRQ_Pin LL_GPIO_PIN_1
#define NRF_IRQ_GPIO_Port GPIOA #define NRF_IRQ_GPIO_Port GPIOA
#define NRF_CE_Pin LL_GPIO_PIN_3 #define NRF_CE_Pin LL_GPIO_PIN_3
#define NRF_CE_GPIO_Port GPIOA #define NRF_CE_GPIO_Port GPIOA
#define NRF_NSS_Pin LL_GPIO_PIN_4 #define NRF_NSS_Pin LL_GPIO_PIN_2
#define NRF_NSS_GPIO_Port GPIOA #define NRF_NSS_GPIO_Port GPIOA
#define RENUM_Pin LL_GPIO_PIN_0
#define RENUM_GPIO_Port GPIOA
/* ########################## Assert Selection ############################## */ /* ########################## Assert Selection ############################## */
/** /**

@ -48,8 +48,6 @@ static uint8_t txmsg_payload[MAX_FRAME_LEN]; // equal buffer size in GEX
static uint8_t txmsg_addr = 0; static uint8_t txmsg_addr = 0;
static uint8_t txmsg_cksum = 0; static uint8_t txmsg_cksum = 0;
#define MAGIC_GW_COMMAND 0x47U // 'G'
enum GW_CMD { enum GW_CMD {
CMD_GET_ID = 'i', // 105 - get network ID CMD_GET_ID = 'i', // 105 - get network ID
CMD_RESET = 'r', // reset the radio and network CMD_RESET = 'r', // reset the radio and network
@ -137,6 +135,7 @@ void handle_txframe_chunk(const uint8_t *buffer, uint16_t size)
} }
} }
LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDTX_Pin);
cmd_state = CMD_STATE_IDLE; cmd_state = CMD_STATE_IDLE;
} }
} }
@ -147,12 +146,6 @@ void gw_handle_usb_out(uint8_t *buffer)
PayloadParser pp = pp_start(buffer, 64, NULL); PayloadParser pp = pp_start(buffer, 64, NULL);
// handle binary commands for the gateway // handle binary commands for the gateway
// magic twice, one inverted - denotes a gateway command
const uint16_t magic1 = pp_u8(&pp);
const uint16_t magic2 = pp_u8(&pp);
if (magic1 == MAGIC_GW_COMMAND && magic2 == (0xFFU & (~MAGIC_GW_COMMAND))) {
// third byte is the command code
switch (pp_u8(&pp)) { switch (pp_u8(&pp)) {
case CMD_GET_ID: case CMD_GET_ID:
respond_gw_id(); respond_gw_id();
@ -179,6 +172,7 @@ void gw_handle_usb_out(uint8_t *buffer)
break; break;
} }
LL_GPIO_SetOutputPin(LED_GPIO_Port, LEDTX_Pin);
start_slave_cmd(slave_addr, frame_len, cksum); start_slave_cmd(slave_addr, frame_len, cksum);
dbg_nrf("Collecting frame for slave %02x: %d bytes", (int)slave_addr, (int)frame_len); dbg_nrf("Collecting frame for slave %02x: %d bytes", (int)slave_addr, (int)frame_len);
cmd_state = CMD_STATE_TXMSG; cmd_state = CMD_STATE_TXMSG;
@ -192,12 +186,9 @@ void gw_handle_usb_out(uint8_t *buffer)
default: default:
dbg("Bad cmd"); dbg("Bad cmd");
} }
} else {
// Bad frame??
dbg("Bad USB frame, starts %x,%x", buffer[0],buffer[1]);
}
} }
else if (cmd_state == CMD_STATE_TXMSG) { else if (cmd_state == CMD_STATE_TXMSG) {
LL_GPIO_SetOutputPin(LED_GPIO_Port, LEDTX_Pin);
handle_txframe_chunk(buffer, 64); handle_txframe_chunk(buffer, 64);
} }
} }
@ -288,6 +279,7 @@ void gw_setup_radio(void)
void EXTI2_IRQHandler(void) void EXTI2_IRQHandler(void)
{ {
LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_2); LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_2);
LL_GPIO_SetOutputPin(LED_GPIO_Port, LEDRX_Pin);
struct msg_data m; struct msg_data m;
m.msg_type = MSG_TYPE_DATA; m.msg_type = MSG_TYPE_DATA;

@ -79,20 +79,23 @@ void MX_GPIO_Init(void)
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
/**/ /**/
LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin); LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED1_Pin);
LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDRX_Pin);
LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDTX_Pin);
LL_GPIO_SetOutputPin(RENUM_GPIO_Port, RENUM_Pin);
/**/ /**/
// LL_GPIO_ResetOutputPin(GPIOA, NRF_CE_Pin|NRF_NSS_Pin); // LL_GPIO_ResetOutputPin(GPIOA, NRF_CE_Pin|NRF_NSS_Pin);
/**/ /**/
GPIO_InitStruct.Pin = LED_Pin; GPIO_InitStruct.Pin = LED1_Pin|LEDRX_Pin|LEDTX_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT; GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
/**/ /**/
GPIO_InitStruct.Pin = NRF_CE_Pin|NRF_NSS_Pin; GPIO_InitStruct.Pin = NRF_CE_Pin|NRF_NSS_Pin|RENUM_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT; GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
@ -100,10 +103,10 @@ void MX_GPIO_Init(void)
#if 1 #if 1
/**/ /**/
LL_GPIO_AF_SetEXTISource(LL_GPIO_AF_EXTI_PORTA, LL_GPIO_AF_EXTI_LINE2); LL_GPIO_AF_SetEXTISource(LL_GPIO_AF_EXTI_PORTA, LL_GPIO_AF_EXTI_LINE1); // IRQ on PA1
/**/ /**/
EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_2; EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_1;
EXTI_InitStruct.LineCommand = ENABLE; EXTI_InitStruct.LineCommand = ENABLE;
EXTI_InitStruct.Mode = LL_EXTI_MODE_IT; EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING; EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;

@ -128,6 +128,11 @@ int main(void)
gw_setup_radio(); gw_setup_radio();
mq_init(&usb_inq); mq_init(&usb_inq);
// re-enumerate USB
LL_GPIO_SetOutputPin(RENUM_GPIO_Port, RENUM_Pin);
LL_mDelay(100);
LL_GPIO_ResetOutputPin(RENUM_GPIO_Port, RENUM_Pin);
dbg("Main loop starts."); dbg("Main loop starts.");
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
@ -135,8 +140,12 @@ int main(void)
uint8_t buff[MQ_SLOT_LEN]; uint8_t buff[MQ_SLOT_LEN];
while (1) { while (1) {
if (cnt++ > 500000) { cnt++;
LL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); if (cnt > 1400000) {
LL_GPIO_SetOutputPin(LED_GPIO_Port, LED1_Pin);
}
if (cnt > 1500000) {
LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED1_Pin);
cnt = 0; cnt = 0;
} }
@ -144,6 +153,9 @@ int main(void)
if (!usb_tx_busy) { if (!usb_tx_busy) {
mq_read(&usb_inq, buff); mq_read(&usb_inq, buff);
CDC_Transmit_FS(buff, MQ_SLOT_LEN); CDC_Transmit_FS(buff, MQ_SLOT_LEN);
// blinking - maybe this will work .. not sure
LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDRX_Pin);
} }
} }
} }
@ -160,15 +172,13 @@ void SystemClock_Config(void)
LL_FLASH_SetLatency(LL_FLASH_LATENCY_2); LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2) if (LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2) {
{
Error_Handler(); Error_Handler();
} }
LL_RCC_HSE_Enable(); LL_RCC_HSE_Enable();
/* Wait till HSE is ready */ /* Wait till HSE is ready */
while(LL_RCC_HSE_IsReady() != 1) while (LL_RCC_HSE_IsReady() != 1) {
{
} }
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9); LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
@ -176,8 +186,7 @@ void SystemClock_Config(void)
LL_RCC_PLL_Enable(); LL_RCC_PLL_Enable();
/* Wait till PLL is ready */ /* Wait till PLL is ready */
while(LL_RCC_PLL_IsReady() != 1) while (LL_RCC_PLL_IsReady() != 1) {
{
} }
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
@ -189,8 +198,7 @@ void SystemClock_Config(void)
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
/* Wait till System clock is ready */ /* Wait till System clock is ready */
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
{
} }
LL_Init1msTick(72000000); LL_Init1msTick(72000000);
@ -219,13 +227,13 @@ void _Error_Handler(char *file, int line)
{ {
/* USER CODE BEGIN Error_Handler_Debug */ /* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */ /* User can add his own implementation to report the HAL error return state */
while(1) while (1) {
{
} }
/* USER CODE END Error_Handler_Debug */ /* USER CODE END Error_Handler_Debug */
} }
#ifdef USE_FULL_ASSERT #ifdef USE_FULL_ASSERT
/** /**
* @brief Reports the name of the source file and the source line number * @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred. * where the assert_param error has occurred.
@ -240,6 +248,7 @@ void assert_failed(uint8_t* file, uint32_t line)
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */ /* USER CODE END 6 */
} }
#endif /* USE_FULL_ASSERT */ #endif /* USE_FULL_ASSERT */
/** /**

Loading…
Cancel
Save