Jaesung Oh
/
mbed-os-tcp-server-example
LAN2CAN Test
main.cpp@0:e367ec7e677f, 2019-10-15 (annotated)
- Committer:
- jsoh91
- Date:
- Tue Oct 15 03:41:29 2019 +0000
- Revision:
- 0:e367ec7e677f
fast CAN timer;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jsoh91 | 0:e367ec7e677f | 1 | #if !FEATURE_LWIP |
jsoh91 | 0:e367ec7e677f | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
jsoh91 | 0:e367ec7e677f | 3 | #endif |
jsoh91 | 0:e367ec7e677f | 4 | |
jsoh91 | 0:e367ec7e677f | 5 | |
jsoh91 | 0:e367ec7e677f | 6 | #include "mbed.h" |
jsoh91 | 0:e367ec7e677f | 7 | #include "EthernetInterface.h" |
jsoh91 | 0:e367ec7e677f | 8 | |
jsoh91 | 0:e367ec7e677f | 9 | #define IP "10.12.3.26" |
jsoh91 | 0:e367ec7e677f | 10 | #define NETMASK "255.255.255.0" |
jsoh91 | 0:e367ec7e677f | 11 | #define GATEWAY "192.168.137.1" |
jsoh91 | 0:e367ec7e677f | 12 | |
jsoh91 | 0:e367ec7e677f | 13 | #define serv_IP "10.12.3.25" |
jsoh91 | 0:e367ec7e677f | 14 | |
jsoh91 | 0:e367ec7e677f | 15 | #define TMR3_COUNT 54000000 // loop 5k |
jsoh91 | 0:e367ec7e677f | 16 | |
jsoh91 | 0:e367ec7e677f | 17 | #include <cstdlib> |
jsoh91 | 0:e367ec7e677f | 18 | #include <iostream> |
jsoh91 | 0:e367ec7e677f | 19 | |
jsoh91 | 0:e367ec7e677f | 20 | using namespace std; |
jsoh91 | 0:e367ec7e677f | 21 | |
jsoh91 | 0:e367ec7e677f | 22 | float calc_time = 0.0f; |
jsoh91 | 0:e367ec7e677f | 23 | |
jsoh91 | 0:e367ec7e677f | 24 | Timer t; |
jsoh91 | 0:e367ec7e677f | 25 | |
jsoh91 | 0:e367ec7e677f | 26 | extern "C" void TIM3_IRQHandler(void) |
jsoh91 | 0:e367ec7e677f | 27 | { |
jsoh91 | 0:e367ec7e677f | 28 | if ( TIM3->SR & TIM_SR_UIF ) { |
jsoh91 | 0:e367ec7e677f | 29 | |
jsoh91 | 0:e367ec7e677f | 30 | |
jsoh91 | 0:e367ec7e677f | 31 | |
jsoh91 | 0:e367ec7e677f | 32 | } |
jsoh91 | 0:e367ec7e677f | 33 | TIM3->SR = 0x0; |
jsoh91 | 0:e367ec7e677f | 34 | } |
jsoh91 | 0:e367ec7e677f | 35 | |
jsoh91 | 0:e367ec7e677f | 36 | void Init_TMR3() |
jsoh91 | 0:e367ec7e677f | 37 | { |
jsoh91 | 0:e367ec7e677f | 38 | |
jsoh91 | 0:e367ec7e677f | 39 | |
jsoh91 | 0:e367ec7e677f | 40 | RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // enable TIM3 clock |
jsoh91 | 0:e367ec7e677f | 41 | |
jsoh91 | 0:e367ec7e677f | 42 | //ISR Setup |
jsoh91 | 0:e367ec7e677f | 43 | |
jsoh91 | 0:e367ec7e677f | 44 | NVIC_EnableIRQ(TIM3_IRQn); //Enable TIM3 IRQ |
jsoh91 | 0:e367ec7e677f | 45 | |
jsoh91 | 0:e367ec7e677f | 46 | TIM3->DIER |= TIM_DIER_UIE; // enable update interrupt |
jsoh91 | 0:e367ec7e677f | 47 | TIM3->CR1 = 0x40; // CMS = 10, interrupt only when counting up // Center-aligned mode |
jsoh91 | 0:e367ec7e677f | 48 | TIM3->CR1 |= TIM_CR1_UDIS; |
jsoh91 | 0:e367ec7e677f | 49 | TIM3->CR1 |= TIM_CR1_ARPE; // autoreload on, |
jsoh91 | 0:e367ec7e677f | 50 | TIM3->RCR |= 0x001; // update event once per up/down count of TIM3 |
jsoh91 | 0:e367ec7e677f | 51 | TIM3->EGR |= TIM_EGR_UG; |
jsoh91 | 0:e367ec7e677f | 52 | |
jsoh91 | 0:e367ec7e677f | 53 | TIM3->PSC = 0x4; // no prescaler, timer counts up in sync with the peripheral clock |
jsoh91 | 0:e367ec7e677f | 54 | TIM3->ARR = TMR3_COUNT; // set auto reload, 5 khz |
jsoh91 | 0:e367ec7e677f | 55 | TIM3->CCER |= ~(TIM_CCER_CC1NP); // Interupt when low side is on. |
jsoh91 | 0:e367ec7e677f | 56 | TIM3->CR1 |= TIM_CR1_CEN; // enable TIM4 |
jsoh91 | 0:e367ec7e677f | 57 | } |
jsoh91 | 0:e367ec7e677f | 58 | |
jsoh91 | 0:e367ec7e677f | 59 | |
jsoh91 | 0:e367ec7e677f | 60 | int main() |
jsoh91 | 0:e367ec7e677f | 61 | { |
jsoh91 | 0:e367ec7e677f | 62 | Init_TMR3(); |
jsoh91 | 0:e367ec7e677f | 63 | TIM3->CR1 ^= TIM_CR1_UDIS; |
jsoh91 | 0:e367ec7e677f | 64 | NVIC_SetPriority(TIM3_IRQn, 2); |
jsoh91 | 0:e367ec7e677f | 65 | |
jsoh91 | 0:e367ec7e677f | 66 | t.reset(); |
jsoh91 | 0:e367ec7e677f | 67 | t.start(); |
jsoh91 | 0:e367ec7e677f | 68 | // |
jsoh91 | 0:e367ec7e677f | 69 | // wait(1.0); |
jsoh91 | 0:e367ec7e677f | 70 | // printf("Basic HTTP client example\n"); |
jsoh91 | 0:e367ec7e677f | 71 | // |
jsoh91 | 0:e367ec7e677f | 72 | // EthernetInterface eth; |
jsoh91 | 0:e367ec7e677f | 73 | // eth.set_network(IP, NETMASK, GATEWAY); |
jsoh91 | 0:e367ec7e677f | 74 | // eth.connect(); |
jsoh91 | 0:e367ec7e677f | 75 | //// |
jsoh91 | 0:e367ec7e677f | 76 | //// int eth_state = eth.connect(); |
jsoh91 | 0:e367ec7e677f | 77 | //// |
jsoh91 | 0:e367ec7e677f | 78 | // printf("The target IP address is '%s'\n", eth.get_ip_address()); |
jsoh91 | 0:e367ec7e677f | 79 | // |
jsoh91 | 0:e367ec7e677f | 80 | // TCPSocket clt_sock; |
jsoh91 | 0:e367ec7e677f | 81 | // clt_sock.open(ð); |
jsoh91 | 0:e367ec7e677f | 82 | // clt_sock.connect(serv_IP,80); |
jsoh91 | 0:e367ec7e677f | 83 | // |
jsoh91 | 0:e367ec7e677f | 84 | // // UDPSocket clt_sock; |
jsoh91 | 0:e367ec7e677f | 85 | //// clt_sock.open(ð); |
jsoh91 | 0:e367ec7e677f | 86 | ////// clt_sock.connect("192.168.137.25",80); |
jsoh91 | 0:e367ec7e677f | 87 | // |
jsoh91 | 0:e367ec7e677f | 88 | //// SocketAddress serv_IP; |
jsoh91 | 0:e367ec7e677f | 89 | // |
jsoh91 | 0:e367ec7e677f | 90 | // char tbuffer[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
jsoh91 | 0:e367ec7e677f | 91 | // char rbuffer[128]; |
jsoh91 | 0:e367ec7e677f | 92 | |
jsoh91 | 0:e367ec7e677f | 93 | while (true) { |
jsoh91 | 0:e367ec7e677f | 94 | |
jsoh91 | 0:e367ec7e677f | 95 | cout << "t : " << calc_time << endl; |
jsoh91 | 0:e367ec7e677f | 96 | wait(0.2f); |
jsoh91 | 0:e367ec7e677f | 97 | |
jsoh91 | 0:e367ec7e677f | 98 | |
jsoh91 | 0:e367ec7e677f | 99 | |
jsoh91 | 0:e367ec7e677f | 100 | // t.reset(); |
jsoh91 | 0:e367ec7e677f | 101 | // t.start(); |
jsoh91 | 0:e367ec7e677f | 102 | // clt_sock.send(tbuffer, strlen(tbuffer)); |
jsoh91 | 0:e367ec7e677f | 103 | // t.stop(); |
jsoh91 | 0:e367ec7e677f | 104 | // printf("send time: %f\n",t.read()*1000.0f); |
jsoh91 | 0:e367ec7e677f | 105 | // |
jsoh91 | 0:e367ec7e677f | 106 | // t.reset(); |
jsoh91 | 0:e367ec7e677f | 107 | // t.start(); |
jsoh91 | 0:e367ec7e677f | 108 | // clt_sock.recv(rbuffer,50); |
jsoh91 | 0:e367ec7e677f | 109 | // t.stop(); |
jsoh91 | 0:e367ec7e677f | 110 | // printf("recv time: %f\n",t.read()*1000.0f); |
jsoh91 | 0:e367ec7e677f | 111 | |
jsoh91 | 0:e367ec7e677f | 112 | } |
jsoh91 | 0:e367ec7e677f | 113 | } |