Commit b5000d75 authored by Daniel Frejek's avatar Daniel Frejek

Some cleanup

parent 1cc2bd43
......@@ -30,7 +30,7 @@ static void process_config_packet(uint8_t *packet, uint8_t *dmx_mask);
static void process_data_packet(uint8_t *packet, uint8_t *dmx_buffer, uint8_t *dmx_mask);
// Sets the timer for the next (scheduled) hop.
// check_hop_timer will return 1 has soon as this timer expires.
// check_hop_timer() will return 1 has soon as this timer expires.
static void set_hop_timer(uint8_t val);
static inline uint8_t check_hop_timer();
static void init_hop_timer();
......@@ -64,6 +64,7 @@ void sendstr(const char *str)
sendraw(str, strlen(str));
}
/*
void rx_wait(char *buffer, uint16_t size)
{
char c;
......@@ -80,6 +81,7 @@ void rx_wait(char *buffer, uint16_t size)
(*buffer) = 0;
}
*/
void hexdump(const uint8_t *data, uint16_t len)
{
......@@ -104,28 +106,7 @@ void hexdump(const uint8_t *data, uint16_t len)
}
}
uint8_t dehex(uint8_t *buf)
{
//deadlock if len >= 128 ^^
uint8_t size = 0;
// De-hex buffer
while(buf[size * 2] && buf[size * 2 + 1])
{
if(buf[size * 2] > '9') buf[size * 2] -= ('A' - 10);
else buf[size * 2] -= '0';
if(buf[size * 2 + 1] > '9') buf[size * 2 + 1] -= ('A' - 10);
else buf[size * 2 + 1] -= '0';
buf[size] = (buf[size * 2] << 4) | buf[size * 2 + 1];
size++;
}
return size;
}
uint8_t RFM_test();
//uint8_t RFM_test();
int main()
{
......@@ -199,6 +180,9 @@ int main()
WATCHDOG_FEED();
calibrate_rc_osc();
// Begin with a short hop interval to find the current
// transmitter channel
set_hop_timer(HOP_TIMEOUT_SEARCH);
while (1)
......@@ -217,6 +201,7 @@ int main()
if (hoptime)
{
// Hop announced, set the timer to the announced time
set_hop_timer(hoptime);
putchar('H');
hexdump(&hoptime, 1);
......@@ -224,6 +209,7 @@ int main()
}
else
{
// No hop announced, set the hop timer to the value for a locked frequency
set_hop_timer(HOP_TIMEOUT_LOCKED);
}
......@@ -252,13 +238,18 @@ int main()
if (check_hop_timer())
{
// Do hop
if (channel_locked)
{
// Currently on a valid frequency
// -> Use long timeout
sendstr("\nLOCKED");
set_hop_timer(HOP_TIMEOUT_LOCKED);
}
else
{
// Not on a valid frequency
// -> Use shot timeout for searching the current transmitter channel
sendstr("\nSEARCH");
// Reset dmx buffer
......@@ -267,6 +258,9 @@ int main()
set_hop_timer(HOP_TIMEOUT_SEARCH);
}
// Reset the channel_locked status when hopping
// This is set again, when the first packet is received
channel_locked = 0;
current_channel = next_channel;
if (current_channel > NUM_RF_CHANNELS)
......@@ -276,6 +270,8 @@ int main()
next_channel = current_channel + 1;
sendstr("\nHOP TO ");
hexdump(&current_channel, 1);
// Reset the FIFO in order listen on the new channel
RFM_rx_packet(&received_message, current_channel, 1);
}
......@@ -290,49 +286,12 @@ int main()
sendstr("TIMEOUT\n");
}
}
/*
while(1)
{
rx_wait((char *)dmx_buffer, sizeof(dmx_buffer));
res = dehex(dmx_buffer);
sendstr("\nset DMX: ");
hexdump(dmx_buffer, sizeof(dmx_buffer));
dmx_set(dmx_buffer);
sendstr("\ndone");
}
*/
//RFM_test();
/*
while(1)
{
while ((UART1_SR & USART_SR_RXNE) == 0);
c = UART1_DR;
UART1_DR = c;
}
*/
/*
// Configure pins
PB_DDR = 1 << 5;
PB_CR1 = 1 << 5;
// Loop
do {
PB_ODR ^= 1 << 5;
for(d = 0; d<1900; d++)
{
for(c = 0; c<5; c++) ;
}
} while(1);
*/
}
static void process_config_packet(uint8_t *packet, uint8_t *dmx_mask)
{
// The contents of the message are the bitfield defining which channels are addressed by the values form the data message
memcpy(dmx_mask, packet, DMX_MASK_BYTES);
}
......@@ -346,7 +305,7 @@ static void process_data_packet(uint8_t *packet, uint8_t *dmx_buffer, uint8_t *d
// Check if current channel is mapped
if (dmx_mask[dmx_cur >> 3] & (0x80 >> (dmx_cur & 0x07)))
{
// Mapped -> Update channel
// Mapped -> Set to next data value
dmx_buffer[dmx_cur] = packet[input_cur];
input_cur++;
if (input_cur >= DMX_CHANNELS_PER_PACKET)
......@@ -356,6 +315,7 @@ static void process_data_packet(uint8_t *packet, uint8_t *dmx_buffer, uint8_t *d
}
else
{
// Not mapped -> set to 0
dmx_buffer[dmx_cur] = 0;
}
}
......@@ -410,6 +370,11 @@ static void init_hop_timer()
/*
* This function calibrates the internal RC oscillator agianst a 1 MHz signal applied to the pin PB4.
* On the HC-12 module, this pin is wired to the GPIO0 of the Si4463 modem,
* which is configured to output a 1MHz signal on this pin.
* Because the modem has a crystal, the generated 1MHz can be assumed to be fairly accurate.
*
* Not sure if I need this, could be removed if I run out of flash?
*/
static void calibrate_rc_osc()
......@@ -425,16 +390,24 @@ static void calibrate_rc_osc()
calibrate_rc_count();
WATCHDOG_FEED();
/*
* The calibrate_rc_count function counts the number of edges in 32768 CPU cylces.
* We try all 16 settings for the RC oscillator (CLK_HSITRIMR) and compare the
* measured number of edges to the expedted number of edges for the desired CPU clock.a
* We then use the calibration with the lowest deviation.
*/
for (cur = 0; cur < 16; cur++)
{
WATCHDOG_FEED();
CLK_HSITRIMR = cur;
for (delay = 0; delay < 5000; delay++)
{
}
for (delay = 0; delay < 5000; delay++); // Wait for the clock to stabilize
WATCHDOG_FEED();
// Get the count
val = calibrate_rc_count();
// Get absolute error
if (val > DESIRED_RC_CAL_CNT)
{
err = val - DESIRED_RC_CAL_CNT;
......@@ -450,6 +423,7 @@ static void calibrate_rc_osc()
sendstr(" is ");
hexdump(&err, 2);
// Store the best setting
if (err < best_err)
{
best_err = err;
......
......@@ -102,7 +102,7 @@ uint16_t rx_wait(char *buffer, uint16_t size)
return len;
}
uint8_t RFM_test();
//uint8_t RFM_test();
int main()
{
......@@ -111,8 +111,8 @@ int main()
// Buffer for reading the serial port.
// 1 Byte Message code
// 2 Byte Channel as hex
// 2 Byte Time in 32ms ticks till next hop as hex
// 2 Bytes Channel as hex
// 2 Bytes Time in 32ms ticks till next hop as hex
// 2 Bytes Next hop channel
// DMX_CHANNELS_PER_PACKET * 2 Data as hex
// 1 Byte Terminator
......@@ -230,9 +230,9 @@ int main()
message.type = MESSAGE_TYPE_CONFIG_MASK | time_to_next_channel;
break;
case 'T':
RFM_test();
break;
//case 'T':
// RFM_test();
// break;
default:
sendstr("Invalid message type\n");
......@@ -252,42 +252,4 @@ int main()
RFM_tx_packet(&message, channel);
//sendstr("\nDONE\n");
}
/*
while(1)
{
rx_wait((char *)buf, sizeof(buf) / 2);
res = dehex(buf);
sendstr("\nset DMX data with length: ");
hexdump(&res, 1);
dmx_set(res, buf);
sendstr("\ndone");
}
*/
//RFM_test();
/*
while(1)
{
while ((UART1_SR & USART_SR_RXNE) == 0);
c = UART1_DR;
UART1_DR = c;
}
*/
/*
// Configure pins
PB_DDR = 1 << 5;
PB_CR1 = 1 << 5;
// Loop
do {
PB_ODR ^= 1 << 5;
for(d = 0; d<1900; d++)
{
for(c = 0; c<5; c++) ;
}
} while(1);
*/
}
......@@ -605,6 +605,7 @@ uint8_t RFM_raw_cmd(const void *cmd, uint8_t cmd_size, void *result, uint8_t res
return rfm_command(cmd, cmd_size, result, result_size);
}
#if 0
#include "utils.h"
#include <string.h>
......@@ -817,3 +818,4 @@ uint8_t RFM_test()
}
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment