Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 5:3f93dd1d4cb3 1 /* mbed Microcontroller Library
sam_grove 5:3f93dd1d4cb3 2 * Copyright (c) 2006-2013 ARM Limited
sam_grove 5:3f93dd1d4cb3 3 *
sam_grove 5:3f93dd1d4cb3 4 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 5:3f93dd1d4cb3 5 * you may not use this file except in compliance with the License.
sam_grove 5:3f93dd1d4cb3 6 * You may obtain a copy of the License at
sam_grove 5:3f93dd1d4cb3 7 *
sam_grove 5:3f93dd1d4cb3 8 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 5:3f93dd1d4cb3 9 *
sam_grove 5:3f93dd1d4cb3 10 * Unless required by applicable law or agreed to in writing, software
sam_grove 5:3f93dd1d4cb3 11 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 5:3f93dd1d4cb3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 5:3f93dd1d4cb3 13 * See the License for the specific language governing permissions and
sam_grove 5:3f93dd1d4cb3 14 * limitations under the License.
sam_grove 5:3f93dd1d4cb3 15 */
sam_grove 5:3f93dd1d4cb3 16 #include "analogin_api.h"
sam_grove 5:3f93dd1d4cb3 17
sam_grove 5:3f93dd1d4cb3 18 #include "cmsis.h"
sam_grove 5:3f93dd1d4cb3 19 #include "pinmap.h"
sam_grove 5:3f93dd1d4cb3 20 #include "error.h"
sam_grove 5:3f93dd1d4cb3 21
sam_grove 5:3f93dd1d4cb3 22 #define ANALOGIN_MEDIAN_FILTER 1
sam_grove 5:3f93dd1d4cb3 23
sam_grove 5:3f93dd1d4cb3 24 #define ADC_10BIT_RANGE 0x3FF
sam_grove 5:3f93dd1d4cb3 25 #define ADC_12BIT_RANGE 0xFFF
sam_grove 5:3f93dd1d4cb3 26
sam_grove 5:3f93dd1d4cb3 27 static inline int div_round_up(int x, int y) {
sam_grove 5:3f93dd1d4cb3 28 return (x + (y - 1)) / y;
sam_grove 5:3f93dd1d4cb3 29 }
sam_grove 5:3f93dd1d4cb3 30
sam_grove 5:3f93dd1d4cb3 31 static const PinMap PinMap_ADC[] = {
sam_grove 5:3f93dd1d4cb3 32 {P0_23, ADC0_0, 1},
sam_grove 5:3f93dd1d4cb3 33 {P0_24, ADC0_1, 1},
sam_grove 5:3f93dd1d4cb3 34 {P0_25, ADC0_2, 1},
sam_grove 5:3f93dd1d4cb3 35 {P0_26, ADC0_3, 1},
sam_grove 5:3f93dd1d4cb3 36 {P1_30, ADC0_4, 3},
sam_grove 5:3f93dd1d4cb3 37 {P1_31, ADC0_5, 3},
sam_grove 5:3f93dd1d4cb3 38 {P0_2, ADC0_7, 2},
sam_grove 5:3f93dd1d4cb3 39 {P0_3, ADC0_6, 2},
sam_grove 5:3f93dd1d4cb3 40 {NC, NC, 0}
sam_grove 5:3f93dd1d4cb3 41 };
sam_grove 5:3f93dd1d4cb3 42
sam_grove 5:3f93dd1d4cb3 43 #define ADC_RANGE ADC_12BIT_RANGE
sam_grove 5:3f93dd1d4cb3 44
sam_grove 5:3f93dd1d4cb3 45 void analogin_init(analogin_t *obj, PinName pin) {
sam_grove 5:3f93dd1d4cb3 46 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
sam_grove 5:3f93dd1d4cb3 47 if (obj->adc == (ADCName)NC) {
sam_grove 5:3f93dd1d4cb3 48 error("ADC pin mapping failed");
sam_grove 5:3f93dd1d4cb3 49 }
sam_grove 5:3f93dd1d4cb3 50
sam_grove 5:3f93dd1d4cb3 51 // ensure power is turned on
sam_grove 5:3f93dd1d4cb3 52 LPC_SC->PCONP |= (1 << 12);
sam_grove 5:3f93dd1d4cb3 53
sam_grove 5:3f93dd1d4cb3 54 // set PCLK of ADC to /1
sam_grove 5:3f93dd1d4cb3 55 LPC_SC->PCLKSEL0 &= ~(0x3 << 24);
sam_grove 5:3f93dd1d4cb3 56 LPC_SC->PCLKSEL0 |= (0x1 << 24);
sam_grove 5:3f93dd1d4cb3 57 uint32_t PCLK = SystemCoreClock;
sam_grove 5:3f93dd1d4cb3 58
sam_grove 5:3f93dd1d4cb3 59 // calculate minimum clock divider
sam_grove 5:3f93dd1d4cb3 60 // clkdiv = divider - 1
sam_grove 5:3f93dd1d4cb3 61 uint32_t MAX_ADC_CLK = 13000000;
sam_grove 5:3f93dd1d4cb3 62 uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1;
sam_grove 5:3f93dd1d4cb3 63
sam_grove 5:3f93dd1d4cb3 64 // Set the generic software-controlled ADC settings
sam_grove 5:3f93dd1d4cb3 65 LPC_ADC->ADCR = (0 << 0) // SEL: 0 = no channels selected
sam_grove 5:3f93dd1d4cb3 66 | (clkdiv << 8) // CLKDIV: PCLK max ~= 25MHz, /25 to give safe 1MHz at fastest
sam_grove 5:3f93dd1d4cb3 67 | (0 << 16) // BURST: 0 = software control
sam_grove 5:3f93dd1d4cb3 68 | (0 << 17) // CLKS: not applicable
sam_grove 5:3f93dd1d4cb3 69 | (1 << 21) // PDN: 1 = operational
sam_grove 5:3f93dd1d4cb3 70 | (0 << 24) // START: 0 = no start
sam_grove 5:3f93dd1d4cb3 71 | (0 << 27); // EDGE: not applicable
sam_grove 5:3f93dd1d4cb3 72
sam_grove 5:3f93dd1d4cb3 73 pinmap_pinout(pin, PinMap_ADC);
sam_grove 5:3f93dd1d4cb3 74 }
sam_grove 5:3f93dd1d4cb3 75
sam_grove 5:3f93dd1d4cb3 76 static inline uint32_t adc_read(analogin_t *obj) {
sam_grove 5:3f93dd1d4cb3 77 // Select the appropriate channel and start conversion
sam_grove 5:3f93dd1d4cb3 78 LPC_ADC->ADCR &= ~0xFF;
sam_grove 5:3f93dd1d4cb3 79 LPC_ADC->ADCR |= 1 << (int)obj->adc;
sam_grove 5:3f93dd1d4cb3 80 LPC_ADC->ADCR |= 1 << 24;
sam_grove 5:3f93dd1d4cb3 81
sam_grove 5:3f93dd1d4cb3 82 // Repeatedly get the sample data until DONE bit
sam_grove 5:3f93dd1d4cb3 83 unsigned int data;
sam_grove 5:3f93dd1d4cb3 84 do {
sam_grove 5:3f93dd1d4cb3 85 data = LPC_ADC->ADGDR;
sam_grove 5:3f93dd1d4cb3 86 } while ((data & ((unsigned int)1 << 31)) == 0);
sam_grove 5:3f93dd1d4cb3 87
sam_grove 5:3f93dd1d4cb3 88 // Stop conversion
sam_grove 5:3f93dd1d4cb3 89 LPC_ADC->ADCR &= ~(1 << 24);
sam_grove 5:3f93dd1d4cb3 90
sam_grove 5:3f93dd1d4cb3 91 return (data >> 4) & ADC_RANGE; // 12 bit
sam_grove 5:3f93dd1d4cb3 92 }
sam_grove 5:3f93dd1d4cb3 93
sam_grove 5:3f93dd1d4cb3 94 static inline void order(uint32_t *a, uint32_t *b) {
sam_grove 5:3f93dd1d4cb3 95 if (*a > *b) {
sam_grove 5:3f93dd1d4cb3 96 uint32_t t = *a;
sam_grove 5:3f93dd1d4cb3 97 *a = *b;
sam_grove 5:3f93dd1d4cb3 98 *b = t;
sam_grove 5:3f93dd1d4cb3 99 }
sam_grove 5:3f93dd1d4cb3 100 }
sam_grove 5:3f93dd1d4cb3 101
sam_grove 5:3f93dd1d4cb3 102 static inline uint32_t adc_read_u32(analogin_t *obj) {
sam_grove 5:3f93dd1d4cb3 103 uint32_t value;
sam_grove 5:3f93dd1d4cb3 104 #if ANALOGIN_MEDIAN_FILTER
sam_grove 5:3f93dd1d4cb3 105 uint32_t v1 = adc_read(obj);
sam_grove 5:3f93dd1d4cb3 106 uint32_t v2 = adc_read(obj);
sam_grove 5:3f93dd1d4cb3 107 uint32_t v3 = adc_read(obj);
sam_grove 5:3f93dd1d4cb3 108 order(&v1, &v2);
sam_grove 5:3f93dd1d4cb3 109 order(&v2, &v3);
sam_grove 5:3f93dd1d4cb3 110 order(&v1, &v2);
sam_grove 5:3f93dd1d4cb3 111 value = v2;
sam_grove 5:3f93dd1d4cb3 112 #else
sam_grove 5:3f93dd1d4cb3 113 value = adc_read(obj);
sam_grove 5:3f93dd1d4cb3 114 #endif
sam_grove 5:3f93dd1d4cb3 115 return value;
sam_grove 5:3f93dd1d4cb3 116 }
sam_grove 5:3f93dd1d4cb3 117
sam_grove 5:3f93dd1d4cb3 118 uint16_t analogin_read_u16(analogin_t *obj) {
sam_grove 5:3f93dd1d4cb3 119 uint32_t value = adc_read_u32(obj);
sam_grove 5:3f93dd1d4cb3 120
sam_grove 5:3f93dd1d4cb3 121 return (value << 4) | ((value >> 8) & 0x000F); // 12 bit
sam_grove 5:3f93dd1d4cb3 122 }
sam_grove 5:3f93dd1d4cb3 123
sam_grove 5:3f93dd1d4cb3 124 float analogin_read(analogin_t *obj) {
sam_grove 5:3f93dd1d4cb3 125 uint32_t value = adc_read_u32(obj);
sam_grove 5:3f93dd1d4cb3 126 return (float)value * (1.0f / (float)ADC_RANGE);
sam_grove 5:3f93dd1d4cb3 127 }