-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi-if.c
108 lines (96 loc) · 2.96 KB
/
wifi-if.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/************************************************************************//**
* \file
* \brief This module allows to control the WiFi chip mounted on MeGaWiFi
* cartridges.
*
* \author Jesús Alonso (doragasu)
* \date 2016
****************************************************************************/
#include "wifi-if.h"
#include "slip.h"
#include "util.h"
#include <LUFA/Drivers/Board/LEDs.h>
#define TEST_CHR "0123456789ABCDEF"
/************************************************************************//**
* \brief Initializes WiFi interface. Must be called once before any other
* function in the module, and after calling CifInit().
****************************************************************************/
void WiFiInit(void) {
// Initialize UART
SlipInit();
// Set the module in RESET, and prepare for starting bootloader.
WiFiReset();
WiFiPwrUp();
WiFiPrgEnable();
}
/************************************************************************//**
* \brief Sends an array of characters using polling method.
*
* \param[in] data Data buffer to send.
* \param[in] dataLen Number of bytes to send.
*
* \return The number of bytes sent.
* \warning This function blocks, eating all the CPU until buffer is sent.
****************************************************************************/
uint16_t WiFiPollSend(uint8_t data[], uint16_t dataLen) {
uint16_t i;
uint8_t j;
for (i = 0; i < dataLen;) {
// Wait until FIFO empty, and send in bursts of up to 16 characters.
while (!UartTxFifoEmpty());
for (j = 0; j < 16 && i < dataLen; i++, j++) {
UartPutchar(data[i]);
}
}
return i;
}
/************************************************************************//**
* \brief Receives an array of characters using polling method.
*
* \param[in] data Data buffer holding the received characters.
* \param[in] dataLen Number of bytes to read.
*
* \return The number of bytes received.
* \warning This function blocks, eating the CPU until buffer is received.
****************************************************************************/
uint16_t WiFiPollRecv(uint8_t data[], uint16_t dataLen) {
uint16_t i;
for (i = 0; i < dataLen; i++) {
// Wait until there's data on FIFO
while (!UartRxFifoData());
data[i] = UartGetchar();
}
return i;
}
void WiFiSendTest(void) {
// Uart Write Test
while (TRUE) {
// Wait until FIFO empty
while (!UartTxFifoEmpty());
// FIFO empty, load 16 characters
int i;
for (i = 0; i < 16; i++) UartPutchar(TEST_CHR[i]);
}
}
void WiFiEchoTest(void) {
uint8_t c = 0;
while (TRUE) {
// Wait until FIFO empty and write char
while (!UartTxFifoEmpty());
UartPutchar(c);
// Waitn until char is echoed and compare with sent
while (!UartRxFifoData());
if (c != UartGetchar()) {
// Received character does not match!
LEDs_TurnOffLEDs(LEDS_LED2);
LEDs_ToggleLEDs(LEDS_LED1);
}
c++;
}
}
void WiFiEchoServerTest(void) {
while (TRUE) {
while (!UartRxFifoData());
UartPutchar(UartGetchar());
}
}